summaryrefslogtreecommitdiff
path: root/Data/Sector.cs
blob: 82c3825962f2a29e9ebdd229250436dfe5687084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;

namespace Data
{
    public class Sector
    {
        #region Attributes and structure

        public static float Ratio = Config.coordinatesRatio;

        private bool _isSelected = false;
        private string _Name;
        public Slice[] Slices;

        public struct Slice
        {
            public int Floor;
            public int Ceiling;
            public PointF[] Points;
        }

        #endregion

        #region Constructor

        public Sector(sectorsSector raw)
        {
            _Name = raw.name;
            Slices = new Slice[raw.slice.Length];
            this._isSelected = false;
            int indexSlice = 0;
            foreach (sectorsSectorSlice s in raw.slice)
            {
                Slices[indexSlice].Floor = s.floor;
                Slices[indexSlice].Ceiling = s.ceiling;

                Slices[indexSlice].Points = new PointF[s.vertex.Length];
                int indexVextex = 0;
                foreach (sectorsSectorSliceVertex v in s.vertex)
                {
                    Slices[indexSlice].Points[indexVextex] = MathCautra.ToCautra4(v.lat, v.lon);
                    indexVextex++;
                }
                indexSlice++;
            }
        }

        #endregion

        #region Methods

        static public bool PointInPolygon(PointF p, Sector sect)
        {
            PointF p1, p2;

            bool inside = false;

            foreach (Slice slice in sect.Slices)
            {
                PointF[] poly = slice.Points;

                if (poly.Length < 3)
                {
                    return inside;
                }

                PointF oldPoint = new PointF(

                poly[poly.Length - 1].X, poly[poly.Length - 1].Y);

                for (int i = 0; i < poly.Length; i++)
                {
                    PointF newPoint = new PointF(poly[i].X, poly[i].Y);

                    if (newPoint.X > oldPoint.X)
                    {
                        p1 = oldPoint;
                        p2 = newPoint;
                    }
                    else
                    {
                        p1 = newPoint;
                        p2 = oldPoint;
                    }

                    if ((newPoint.X < p.X) == (p.X <= oldPoint.X) && ((long)p.Y - (long)p1.Y) * (long)(p2.X - p1.X)
                                < ((long)p2.Y - (long)p1.Y) * (long)(p.X - p1.X))
                    {
                        inside = !inside;
                    }
                    oldPoint = newPoint;
                }
                if (inside == true) break;
            }
            return inside;
        }

        public override string ToString()
        {
            return _Name;
        }

        public void Draw(Graphics g, Boolean drawSectorsNames)
        {
            Pen drawLinesPen = new Pen(Config.sectorLinesColor, 0.5f);

            SolidBrush fillPolygonBrush = null;
            if (this.isSectorSelected)
            {
                fillPolygonBrush = new SolidBrush(Config.sectorSelectedFillColor);
            }
            else
            {
                fillPolygonBrush = new SolidBrush(Config.sectorFillColor);
            }

            //PointF p1, p2;
            PointF pCurr = new PointF(0, 0), pSuiv = new PointF(0, 0), pTransf = new PointF(0, 0);
            PointF[] tabPoints;

            int sliceCeiling;

            foreach (Slice s in Slices)
            {
                sliceCeiling = s.Ceiling;
                if (sliceCeiling > Config.sectorHighFloorValue)
                {
                    if (drawSectorsNames)
                    {
                        PointF sectorNamePos = s.Points[0];
                        SolidBrush sectorNameBrush = new SolidBrush(Config.sectorNameColor);
                        g.DrawString(this.Name, Config.sectorNameFont, sectorNameBrush, new PointF((float)PanAndZoomX(sectorNamePos.X / Ratio), (float)PanAndZoomY(sectorNamePos.Y / Ratio)));
                    }

                    tabPoints = new PointF[s.Points.Length-1 ];
                    for (int i = 0; i < (s.Points.Length-1 ); i++)
                    {
                        //p1 = s.Points[i] ;
                        //p2 = s.Points[i + 1];
                        //g.DrawLine(p, (float)PanAndZoomX(p1.X / Ratio), (float)PanAndZoomY(p1.Y / Ratio), (float)PanAndZoomX(p2.X / Ratio), (float)PanAndZoomY(p2.Y / Ratio));

                        pCurr = s.Points[i];
                        pSuiv = s.Points[i + 1];
                        g.DrawLine(drawLinesPen, (float)PanAndZoomX(pCurr.X / Ratio), (float)PanAndZoomY(pCurr.Y / Ratio), (float)PanAndZoomX(pSuiv.X / Ratio), (float)PanAndZoomY(pSuiv.Y / Ratio));
                        pTransf = new PointF((float)PanAndZoomX(pCurr.X / Ratio), (float)PanAndZoomY(pCurr.Y / Ratio));
                        tabPoints[i] = pTransf;
                    }
                    //pSuiv = s.Points[0];
                    //g.DrawLine(drawLinesPen, (float)PanAndZoomX(pCurr.X / Ratio), (float)PanAndZoomY(pCurr.Y / Ratio), (float)PanAndZoomX(pSuiv.X / Ratio), (float)PanAndZoomY(pSuiv.Y / Ratio));
                   
                    g.FillPolygon(fillPolygonBrush, tabPoints, System.Drawing.Drawing2D.FillMode.Alternate);
                }
            }
        }

        #endregion

        #region Getters and Setters

        public bool isSectorSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        #endregion

        #region Delegate Pan And Zoom

        public delegate double PanAndZoom(double input);

        [NonSerialized]
        static public PanAndZoom PanAndZoomX;
        [NonSerialized]
        static public PanAndZoom PanAndZoomY;

        #endregion

        #region createList
        public static Sector[] LoadSectorsFile(string fileName)
        {
             XmlSerializer serializer = new XmlSerializer(typeof(sectors));
            TextReader reader = new StreamReader(fileName);

            Sector[] Sectors = null;
            if (reader != null)
            {
                sectors sectorsXml = (sectors)serializer.Deserialize(reader);
                reader.Close();

                //Sectors = sectorsXml.GetPointList();
                Sectors = new Sector[sectorsXml.Items.Length];
                int indexSec = 0;
                foreach (sectorsSector s in sectorsXml.Items)
                {
                    Sectors[indexSec++] = new Sector(s);
                }
            }
            return Sectors;
        }
        #endregion
    }
}