Skip to content

Commit 94af999

Browse files
authored
Merge pull request MatterHackers#1389 from larsbrubaker/main
main
2 parents ef10243 + c125e9a commit 94af999

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

DataConverters3D/Object3D/Object3D.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@ public override void Dispose()
329329
if (item is Object3D object3D)
330330
{
331331
object3D.RebuildLockCount--;
332+
#if DEBUG
333+
if (object3D.RebuildLockCount < 0)
334+
{
335+
throw new Exception("Dispose is likely being called more than once");
336+
}
337+
#endif
332338
// item.DebugDepth($"Decrease Lock Count {object3D.RebuildLockCount}");
333339
}
334340
}

VectorMath/Vector3.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ public double Length
190190
}
191191
}
192192

193+
public double DistanceToSegment(Vector3 start, Vector3 end)
194+
{
195+
var segmentDelta = end - start;
196+
var segmentLength = segmentDelta.Length;
197+
var segmentNormal = segmentDelta.GetNormal();
198+
var deltaToStart = this - start;
199+
var distanceFromStart = segmentNormal.Dot(deltaToStart);
200+
if (distanceFromStart >= 0 && distanceFromStart < segmentLength)
201+
{
202+
var perpendicular = segmentNormal.GetPerpendicular(new Vector3(0, 0, 1));
203+
var distanceFromLine = Math.Abs(deltaToStart.Dot(perpendicular));
204+
return distanceFromLine;
205+
}
206+
207+
if (distanceFromStart < 0)
208+
{
209+
return deltaToStart.Length;
210+
}
211+
212+
var deltaToEnd = this - end;
213+
return deltaToEnd.Length;
214+
}
215+
216+
193217
#endregion public double Length
194218

195219
#region public double LengthSquared
@@ -680,7 +704,12 @@ public Vector3 GetPerpendicular()
680704
// the input vector has no length (no vector is perpendicular to it)
681705
return default(Vector3);
682706
}
683-
707+
708+
public Vector3 GetPerpendicular(Vector3 b)
709+
{
710+
return GetPerpendicular(this, b);
711+
}
712+
684713
public static Vector3 GetPerpendicular(Vector3 a, Vector3 b)
685714
{
686715
if (!Collinear(a, b, Zero))

0 commit comments

Comments
 (0)