-
Notifications
You must be signed in to change notification settings - Fork 59
/
Segment.cs
313 lines (247 loc) · 10.1 KB
/
Segment.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// Copyright (c) 2017 Geri Borbás http://www.twitter.com/_eppz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace EPPZ.Geometry.Model
{
public class Segment
{
static public float defaultAccuracy = 1e-6f;
public enum ContainmentMethod { Default, Precise }
static public ContainmentMethod defaultContainmentMethod = ContainmentMethod.Default;
private Vector2 _a;
public virtual Vector2 a
{
get { return _a; }
set { _a = value; }
}
private Vector2 _b;
public virtual Vector2 b
{
get { return _b; }
set { _b = value; }
}
#region Calculations
// If `alwaysCalculate` is on, every `normal` and `perpendicular` property access invokes recalculation of values based on actual topology.
public bool alwaysCalculate = true;
private Vector2 _normal;
public Vector2 normal
{
get
{
if (_normal == Vector2.zero || alwaysCalculate) { CalculateNormal(); } // Lazy calculation or force calculate on every access
return _normal;
}
set
{ _normal = value; }
}
public Vector2 _perpendicular;
public Vector2 perpendicular
{
get
{
if (_perpendicular == Vector2.zero || alwaysCalculate) { CalculatePerpendicular(); } // Lazy calculation or force calculate on every access
return _perpendicular;
}
set
{ _perpendicular = value; }
}
public void CalculateNormal()
{
_normal = this.perpendicular.normalized;
}
public void CalculatePerpendicular()
{
Vector2 translated = (this.b - this.a); // Translate to origin
_perpendicular = new Vector2( -translated.y, translated.x); // Rotate CCW
}
#endregion
#region Factories
public static Segment SegmentWithSource(Source.Segment segmentSource)
{
return Segment.SegmentWithPointTransforms(segmentSource.points, segmentSource.coordinates);
}
public static Segment SegmentWithPointTransforms(Transform[] pointTransforms, Source.Segment.Coordinates coordinates = Source.Segment.Coordinates.World) // Uses Transform.localPosition.xy()
{
if (coordinates == Source.Segment.Coordinates.World)
{ return Segment.SegmentWithPoints(pointTransforms[0].position, pointTransforms[1].position); }
// Source.Segment.Coordinates.Local
return Segment.SegmentWithPoints(pointTransforms[0].localPosition, pointTransforms[1].localPosition);
}
public static Segment SegmentWithPoints(Vector2 a_, Vector2 b_)
{
Segment instance = new Segment();
instance.a = a_;
instance.b = b_;
return instance;
}
#endregion
#region Model updates
public void UpdateWithSource(Source.Segment segmentSource) // Assuming unchanged point count
{
UpdateWithTransforms(segmentSource.points, segmentSource.coordinates);
}
public void UpdateWithTransforms(Transform[] pointTransforms, Source.Segment.Coordinates coordinates = Source.Segment.Coordinates.World) // Assuming unchanged point count
{
if (coordinates == Source.Segment.Coordinates.World)
{
a = pointTransforms[0].position;
b = pointTransforms[1].position;
return;
}
// Source.Segment.Coordinates.Local
a = pointTransforms[0].localPosition;
b = pointTransforms[1].localPosition;
}
#endregion
#region Accessors
public Rect bounds // Readonly
{
get
{
Rect bounds_ = new Rect();
// Set bounds.
bounds_.xMin = Mathf.Min(a.x, b.x);
bounds_.xMax = Mathf.Max(a.x, b.x);
bounds_.yMin = Mathf.Min(a.y, b.y);
bounds_.yMax = Mathf.Max(a.y, b.y);
return bounds_;
}
}
public Rect ExpandedBounds(float accuracy)
{
float treshold = accuracy / 2.0f;
Rect bounds_ = this.bounds;
return Rect.MinMaxRect(
bounds_.xMin - treshold,
bounds_.yMin - treshold,
bounds_.xMax + treshold,
bounds_.yMax + treshold);
}
public bool ContainsPoint(Vector2 point)
{ return ContainsPoint(point, defaultAccuracy); }
public bool ContainsPoint(Vector2 point, float accuracy)
{ return ContainsPoint(point, accuracy, ContainmentMethod.Default); }
public bool IsPointLeft(Vector2 point)
{ return Geometry.PointIsLeftOfSegment(point, this.a, this.b); }
public bool ContainsPoint(Vector2 point, float accuracy, ContainmentMethod containmentMethod)
{
float treshold = accuracy / 2.0f;
// Expanded bounds containment test.
Rect expandedBounds = this.ExpandedBounds(accuracy);
bool expandedBoundsContainment = expandedBounds.Contains(point);
if (expandedBoundsContainment == false) return false; // Only if passed
// Line distance test.
float distance = this.DistanceToPoint(point);
bool lineDistanceTest = distance < treshold;
if (lineDistanceTest == false) return false; // Only if passed
if (containmentMethod == ContainmentMethod.Precise)
{
// Perpendicular segment.
Vector2 normalizedHalf = (this.b - this.a) / 2.0f;
float halfLength = normalizedHalf.magnitude;
Vector2 normalizedHalfPerpendicular = new Vector2( -normalizedHalf.y, normalizedHalf.x );
Vector2 perpendicular_a = this.a + normalizedHalf;
Vector2 perpendicular_b = this.a + normalizedHalf + normalizedHalfPerpendicular;
// Perpendicular line distance test.
float perpendicularDistance = Geometry.PointDistanceFromLine(point, perpendicular_a, perpendicular_b);
bool perpendicularDistanceTest = perpendicularDistance < halfLength;
// Endpoint distance test if previous failed.
if (perpendicularDistanceTest == false)
{
float distanceFromEndPoints = Mathf.Min( Vector2.Distance(this.a, point), Vector2.Distance(this.b, point) );
bool endpointDistanceTest = distanceFromEndPoints < treshold;
if (endpointDistanceTest == false) return false; // Only if passed
}
}
// All passed.
return true;
}
#endregion
#region Geometry features
/// <summary>
/// True when the two segments are intersecting. Not true when endpoints
/// are equal, nor when a point is contained by other segment.
/// Can say this algorithm has infinite precision.
/// </summary>
public bool IsIntersectingWithSegment(Segment segment)
{
// No intersecting if bounds don't even overlap (slight optimization).
bool boundsOverlaps = this.bounds.Overlaps(segment.bounds);
if (boundsOverlaps == false) return false;
// Do the Bryce Boe test.
return Geometry.AreSegmentsIntersecting(this.a, this.b, segment.a, segment.b);
}
/// <summary>
/// Returns intersection when the two segments are intersecting. Not returns anything when endpoints
/// are equal, nor when a point is contained by other segment.
/// Can say this algorithm has infinite precision.
/// </summary>
public bool IntersectionWithSegment(Segment segment, out Vector2 intersectionPoint)
{ return IntersectionWithSegmentWithAccuracy(segment, 0.0f, out intersectionPoint); }
public bool IntersectionWithSegmentWithAccuracy(Segment segment, float accuracy, out Vector2 intersectionPoint)
{ return IntersectionWithSegmentWithAccuracy(segment, accuracy, defaultContainmentMethod, out intersectionPoint); }
public bool IntersectionWithSegmentWithAccuracy(Segment segment, ContainmentMethod containmentMethod, out Vector2 intersectionPoint)
{ return IntersectionWithSegmentWithAccuracy(segment, defaultAccuracy, containmentMethod, out intersectionPoint); }
public bool IntersectionWithSegmentWithAccuracy(Segment segment, float accuracy, ContainmentMethod containmentMethod, out Vector2 intersectionPoint)
{
intersectionPoint = Vector2.zero; // Default
// No intersecting if bounds don't even overlap.
Rect expandedBounds = this.ExpandedBounds(accuracy);
Rect otherExpandedBounds = segment.ExpandedBounds(accuracy);
bool boundsOverlaps = expandedBounds.Overlaps(otherExpandedBounds);
if (boundsOverlaps == false)
{
return false; // No intersection
}
if (accuracy > 0.0f) // Only any accuracy is given
{
// Look up point containments.
bool containsA = this.ContainsPoint (segment.a, accuracy, containmentMethod);
if (containsA)
{
intersectionPoint = segment.a;
return true; // Intersecting
}
bool containsB = this.ContainsPoint (segment.b, accuracy, containmentMethod);
if (containsB)
{
intersectionPoint = segment.b;
return true; // Intersecting
}
bool otherContainsA = segment.ContainsPoint (this.a, accuracy, containmentMethod);
if (otherContainsA)
{
intersectionPoint = this.a;
return true; // Intersecting
}
bool otherContainsB = segment.ContainsPoint (this.b, accuracy, containmentMethod);
if (otherContainsB)
{
intersectionPoint = this.b;
return true; // Intersecting
}
}
// Do the Bryce Boe test.
bool isIntersecting = Geometry.AreSegmentsIntersecting(this.a, this.b, segment.a, segment.b);
if (isIntersecting == false)
{
return false; // No intersection
}
// All fine, intersection point can be determined.
intersectionPoint = Geometry.IntersectionPointOfLines(this.a, this.b, segment.a, segment.b); // Actually the intersection of lines defined by segments
return true; // Intersecting
}
public float DistanceToPoint(Vector2 point_)
{
return Geometry.PointDistanceFromLine(point_, this.a, this.b);
}
#endregion
}
}