Skip to content

Commit a2ad838

Browse files
committed
added Segment2.FastSquaredDistance, DGraph2 utility fns
1 parent ff75a5b commit a2ad838

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

core/ProfileUtil.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public virtual void Reset()
4848
public override string ToString()
4949
{
5050
TimeSpan t = Watch.Elapsed;
51-
return string.Format("{0:fffffff}", Watch.Elapsed);
51+
return string.Format("{0:ss}.{0:fffffff}", Watch.Elapsed);
5252
}
5353
}
5454

@@ -125,7 +125,7 @@ public string Elapsed(string label)
125125
}
126126
public string Accumulated(string label)
127127
{
128-
return string.Format("{0:fffffff}", Timers[label].Accumulated);
128+
return string.Format("{0:ss}.{0:fffffff}", Timers[label].Accumulated);
129129
}
130130

131131
public string AllTicks(string prefix = "Times:")

curve/DGraph2.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,30 @@ public bool GetEdgeV(int eID, ref Vector2d a, ref Vector2d b)
158158
return false;
159159
}
160160

161+
public Segment2d GetEdgeSegment(int eID)
162+
{
163+
if (edges_refcount.isValid(eID)) {
164+
int iv0 = 2 * edges[3 * eID];
165+
int iv1 = 2 * edges[3 * eID + 1];
166+
return new Segment2d(new Vector2d(vertices[iv0], vertices[iv0 + 1]),
167+
new Vector2d(vertices[iv1], vertices[iv1 + 1]));
168+
}
169+
throw new Exception("DGraph2.GetEdgeSegment: invalid segment with id " + eID);
170+
}
171+
172+
public Vector2d GetEdgeCenter(int eID)
173+
{
174+
if (edges_refcount.isValid(eID)) {
175+
int iv0 = 2 * edges[3 * eID];
176+
int iv1 = 2 * edges[3 * eID + 1];
177+
return new Vector2d((vertices[iv0] + vertices[iv1]) * 0.5,
178+
(vertices[iv0 + 1] + vertices[iv1 + 1]) * 0.5);
179+
}
180+
throw new Exception("DGraph2.GetEdgeCenter: invalid segment with id " + eID);
181+
}
161182

162183

163-
public int AppendVertex(Vector2d v)
184+
public int AppendVertex(Vector2d v)
164185
{
165186
int vid = vertices_refcount.allocate();
166187
int i = 2 * vid;

math/Segment2.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public double DistanceSquared(Vector2d p)
6060
else if ( t <= -Extent )
6161
return P0.DistanceSquared(p);
6262
Vector2d proj = Center + t * Direction;
63-
return (proj - p).LengthSquared;
63+
return proj.DistanceSquared(p);
6464
}
6565
public double DistanceSquared(Vector2d p, out double t)
6666
{
@@ -73,7 +73,7 @@ public double DistanceSquared(Vector2d p, out double t)
7373
return P0.DistanceSquared(p);
7474
}
7575
Vector2d proj = Center + t * Direction;
76-
return (proj - p).LengthSquared;
76+
return proj.DistanceSquared(p);
7777
}
7878

7979
public Vector2d NearestPoint(Vector2d p)
@@ -151,6 +151,33 @@ public void Reverse() {
151151
public IParametricCurve2d Clone() {
152152
return new Segment2d(this.Center, this.Direction, this.Extent);
153153
}
154+
155+
156+
157+
/// <summary>
158+
/// distance from pt to segment (a,b), with no square roots
159+
/// </summary>
160+
public static double FastDistanceSquared(ref Vector2d a, ref Vector2d b, ref Vector2d pt)
161+
{
162+
double vx = b.x - a.x, vy = b.y - a.y;
163+
double len2 = vx*vx + vy*vy;
164+
double dx = pt.x - a.x, dy = pt.y - a.y;
165+
if (len2 < 1e-13) {
166+
return dx * dx + dy * dy;
167+
}
168+
double t = (dx*vx + dy*vy);
169+
if (t <= 0) {
170+
return dx * dx + dy * dy;
171+
} else if (t >= len2) {
172+
dx = pt.x - b.x; dy = pt.y - b.y;
173+
return dx * dx + dy * dy;
174+
}
175+
176+
dx = pt.x - (a.x + ((t * vx)/len2));
177+
dy = pt.y - (a.y + ((t * vy)/len2));
178+
return dx * dx + dy * dy;
179+
}
180+
154181
}
155182

156183

@@ -233,6 +260,34 @@ void update_from_endpoints(Vector2f p0, Vector2f p1)
233260
Direction = p1 - p0;
234261
Extent = 0.5f * Direction.Normalize();
235262
}
263+
264+
265+
266+
267+
/// <summary>
268+
/// distance from pt to segment (a,b), with no square roots
269+
/// </summary>
270+
public static float FastDistanceSquared(ref Vector2f a, ref Vector2f b, ref Vector2f pt)
271+
{
272+
float vx = b.x - a.x, vy = b.y - a.y;
273+
float len2 = vx * vx + vy * vy;
274+
float dx = pt.x - a.x, dy = pt.y - a.y;
275+
if (len2 < 1e-7) {
276+
return dx * dx + dy * dy;
277+
}
278+
float t = (dx * vx + dy * vy);
279+
if (t <= 0) {
280+
return dx * dx + dy * dy;
281+
} else if (t >= len2) {
282+
dx = pt.x - b.x; dy = pt.y - b.y;
283+
return dx * dx + dy * dy;
284+
}
285+
286+
dx = pt.x - (a.x + ((t * vx) / len2));
287+
dy = pt.y - (a.y + ((t * vy) / len2));
288+
return dx * dx + dy * dy;
289+
}
290+
236291
}
237292

238293

0 commit comments

Comments
 (0)