-
Notifications
You must be signed in to change notification settings - Fork 20
/
IEnvelope.cs
243 lines (214 loc) · 7.39 KB
/
IEnvelope.cs
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#region Disclaimer / License
// Copyright (C) 2014, Jackie Ng
// https://github.com/jumpinjackie/mapguide-maestro
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion Disclaimer / License
using System;
using System.ComponentModel;
namespace OSGeo.MapGuide.ObjectModels.Common
{
/// <summary>
/// Represents a rectangular bounding box
/// </summary>
public interface IEnvelope : INotifyPropertyChanged
{
/// <summary>
/// Gets or sets the min X.
/// </summary>
/// <value>The min X.</value>
double MinX { get; set; }
/// <summary>
/// Gets or sets the min Y.
/// </summary>
/// <value>The min Y.</value>
double MinY { get; set; }
/// <summary>
/// Gets or sets the max X.
/// </summary>
/// <value>The max X.</value>
double MaxX { get; set; }
/// <summary>
/// Gets or sets the max Y.
/// </summary>
/// <value>The max Y.</value>
double MaxY { get; set; }
}
/// <summary>
/// Extension method class
/// </summary>
public static class EnvelopeExtensions
{
static bool IsZero(this float val)
{
return Math.Abs(val) < float.Epsilon;
}
static bool IsZero(this double val)
{
return Math.Abs(val) < double.Epsilon;
}
/// <summary>
/// Gets whether the given instance is empty
/// </summary>
/// <param name="env"></param>
/// <returns></returns>
public static bool IsEmpty(this IEnvelope env)
{
return env == null ||
(env.MaxX.IsZero() &&
env.MaxY.IsZero() &&
env.MinX.IsZero() &&
env.MinY.IsZero());
}
/// <summary>
/// Gets the center of the specified envelope
/// </summary>
/// <param name="env"></param>
/// <returns></returns>
public static IPoint2D Center(this IEnvelope env)
{
Check.ArgumentNotNull(env, nameof(env));
return ObjectFactory.CreatePoint2D(
(env.MinX + env.MaxX) / 2,
(env.MinY + env.MaxY) / 2);
}
/// <summary>
/// Clones this instance
/// </summary>
/// <param name="env">The envelope.</param>
/// <returns></returns>
public static IEnvelope Clone(this IEnvelope env)
{
Check.ArgumentNotNull(env, nameof(env));
return ObjectFactory.CreateEnvelope(env.MinX, env.MinY, env.MaxX, env.MaxY);
}
/// <summary>
/// Expands this envelope to accomodate the given envelope
/// </summary>
/// <param name="env"></param>
/// <param name="e1"></param>
public static void ExpandToInclude(this IEnvelope env, IEnvelope e1)
{
Check.ArgumentNotNull(env, nameof(env));
if (e1 == null)
return;
if (e1.MinX < env.MinX)
env.MinX = e1.MinX;
if (e1.MinY < env.MinY)
env.MinY = e1.MinY;
if (e1.MaxX > env.MaxX)
env.MaxX = e1.MaxX;
if (e1.MaxY > env.MaxY)
env.MaxY = e1.MaxY;
}
/// <summary>
/// Indicates whether the specified coordinates are within this instance
/// </summary>
/// <param name="env">The env.</param>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>
/// <c>true</c> if [contains] [the specified env]; otherwise, <c>false</c>.
/// </returns>
public static bool Contains(this IEnvelope env, double x, double y)
{
Check.ArgumentNotNull(env, nameof(env));
return env.MinX <= x &&
env.MaxX >= x &&
env.MinY <= y &&
env.MaxY >= y;
}
/// <summary>
/// Indicates whether the specified envelope intersects this instance
/// </summary>
/// <param name="env">The env.</param>
/// <param name="other">The other.</param>
/// <returns></returns>
public static bool Intersects(this IEnvelope env, IEnvelope other)
{
Check.ArgumentNotNull(env, nameof(env));
if (other == null)
return false;
return !(other.MinX > env.MaxX || other.MaxX < env.MinX || other.MinY > env.MaxY || other.MaxY < env.MinY);
}
}
partial class Envelope : IEnvelope
{
internal Envelope()
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="minx"></param>
/// <param name="miny"></param>
/// <param name="maxx"></param>
/// <param name="maxy"></param>
public Envelope(double minx, double miny, double maxx, double maxy)
{
this.lowerLeftCoordinateField = new EnvelopeLowerLeftCoordinate() { X = minx, Y = miny };
this.upperRightCoordinateField = new EnvelopeUpperRightCoordinate() { X = maxx, Y = maxy };
}
/// <summary>
/// Gets or sets the lower X value
/// </summary>
public double MinX
{
get { return this.lowerLeftCoordinateField.X; }
set
{
this.lowerLeftCoordinateField.X = value;
OnPropertyChanged(nameof(MinX));
}
}
/// <summary>
/// Gets or set the lower Y value
/// </summary>
public double MinY
{
get { return this.lowerLeftCoordinateField.Y; }
set
{
this.lowerLeftCoordinateField.Y = value;
OnPropertyChanged(nameof(MinY));
}
}
/// <summary>
/// Gets or sets the upper X value
/// </summary>
public double MaxX
{
get { return this.upperRightCoordinateField.X; }
set
{
this.upperRightCoordinateField.X = value;
OnPropertyChanged(nameof(MaxX));
}
}
/// <summary>
/// Gets or sets the upper Y value
/// </summary>
public double MaxY
{
get { return this.upperRightCoordinateField.Y; }
set
{
this.upperRightCoordinateField.Y = value;
OnPropertyChanged(nameof(MaxY));
}
}
}
}