From c835054c9d001a309f256d209a424b973c178572 Mon Sep 17 00:00:00 2001 From: rms Date: Mon, 16 Jan 2017 11:57:24 -0500 Subject: [PATCH] make some names in vector/segment consistent, warning fixes --- approximation/BiArcFit2.cs | 8 ++--- curve/Arc2.cs | 2 +- curve/Circle2.cs | 6 ++-- curve/EllipseArc2.cs | 4 +-- curve/PlanarSolid2d.cs | 4 +-- curve/Polygon2d.cs | 2 +- math/Segment2.cs | 62 ++++++++++++++++++++++++++++++++------ math/Segment3.cs | 61 +++++++++++++++++++++++++++++-------- math/Vector2d.cs | 14 +++++---- math/Vector2f.cs | 14 +++++---- math/Vector3f.cs | 2 +- queries/RayIntersection.cs | 2 +- 12 files changed, 133 insertions(+), 48 deletions(-) diff --git a/approximation/BiArcFit2.cs b/approximation/BiArcFit2.cs index d1862c77..027f3153 100644 --- a/approximation/BiArcFit2.cs +++ b/approximation/BiArcFit2.cs @@ -65,7 +65,7 @@ public Vector2d NearestPoint(Vector2d point) Segment1.NearestPoint(point) : Arc1.NearestPoint(point); Vector2d n2 = (Arc2IsSegment) ? Segment2.NearestPoint(point) : Arc2.NearestPoint(point); - return (n1.SquaredDist(point) < n2.SquaredDist(point)) ? n1 : n2; + return (n1.DistanceSquared(point) < n2.DistanceSquared(point)) ? n1 : n2; } public List Curves { @@ -138,9 +138,9 @@ Arc2d get_arc(int i) // [RMS] code above does not preserve CW/CCW of arcs. // It would be better to fix that. But for now, just check if // we preserved start and end points, and if not reverse curves. - if ( i == 0 && arc.SampleT(0.0).SquaredDist(Point1) > MathUtil.ZeroTolerance ) + if ( i == 0 && arc.SampleT(0.0).DistanceSquared(Point1) > MathUtil.ZeroTolerance ) arc.Reverse(); - if (i == 1 && arc.SampleT(1.0).SquaredDist(Point2) > MathUtil.ZeroTolerance) + if (i == 1 && arc.SampleT(1.0).DistanceSquared(Point2) > MathUtil.ZeroTolerance) arc.Reverse(); return arc; @@ -193,7 +193,7 @@ void Fit() bool perpT1 = MathUtil.EpsilonEqual(vDotT1, 0.0, Epsilon ); if (equalTangents && perpT1) { // we have two semicircles - Vector2d joint = p1 + 0.5 * v; + //Vector2d joint = p1 + 0.5 * v; // d1 = d2 = infinity here... diff --git a/curve/Arc2.cs b/curve/Arc2.cs index 13dc4f03..321d292a 100644 --- a/curve/Arc2.cs +++ b/curve/Arc2.cs @@ -101,7 +101,7 @@ public double Distance(Vector2d point) theta = MathUtil.Clamp(theta, AngleStartDeg * MathUtil.Deg2Rad, AngleEndDeg * MathUtil.Deg2Rad); double c = Math.Cos(theta), s = Math.Sin(theta); Vector2d pos = new Vector2d(Center.x + Radius * c, Center.y + Radius * s); - return pos.Dist(point); + return pos.Distance(point); } else { return Math.Abs(lengthPmC - Radius); } diff --git a/curve/Circle2.cs b/curve/Circle2.cs index 57936f13..5b389e3a 100644 --- a/curve/Circle2.cs +++ b/curve/Circle2.cs @@ -82,7 +82,7 @@ public Vector2d SampleArcLength(double a) { public bool Contains (Vector2d p ) { - double d = Center.SquaredDist(p); + double d = Center.DistanceSquared(p); return d <= Radius * Radius; } @@ -100,12 +100,12 @@ public double Area { public double SignedDistance(Vector2d pt) { - double d = Center.Dist(pt); + double d = Center.Distance(pt); return d - Radius; } public double Distance(Vector2d pt) { - double d = Center.Dist(pt); + double d = Center.Distance(pt); return Math.Abs(d - Radius); } diff --git a/curve/EllipseArc2.cs b/curve/EllipseArc2.cs index d9397f06..e40518d5 100644 --- a/curve/EllipseArc2.cs +++ b/curve/EllipseArc2.cs @@ -108,9 +108,9 @@ public Vector2d TangentT(double t) { double k = a1 * a1 + b1 * b1; double d = Math.Sqrt(k); - double k1 = (-a * b * sint) * d; + //double k1 = (-a * b * sint) * d; double ddt = 0.5 * (1 / d) * ((2 * a * a * sint * cost) - (2 * b * b * cost * sint)); - double k2 = ddt * (a * b * cost); + //double k2 = ddt * (a * b * cost); double dx = ( (-a * b * sint) * d - ddt * (a * b * cost) ) / k; double dy = ( ( a * b * cost) * d - ddt * (a * b * sint) ) / k; diff --git a/curve/PlanarSolid2d.cs b/curve/PlanarSolid2d.cs index 61a5c698..c0d0f6e5 100644 --- a/curve/PlanarSolid2d.cs +++ b/curve/PlanarSolid2d.cs @@ -14,7 +14,7 @@ namespace g3 public class PlanarSolid2d { IParametricCurve2d outer; - bool bOuterIsCW; + //bool bOuterIsCW; List holes = new List(); @@ -30,7 +30,7 @@ public void SetOuter(IParametricCurve2d loop, bool bIsClockwise) { Debug.Assert(loop.IsClosed); outer = loop; - bOuterIsCW = bIsClockwise; + //bOuterIsCW = bIsClockwise; } diff --git a/curve/Polygon2d.cs b/curve/Polygon2d.cs index c2e9adf2..65a05416 100644 --- a/curve/Polygon2d.cs +++ b/curve/Polygon2d.cs @@ -119,7 +119,7 @@ public double Perimeter double fPerim = 0; int N = vertices.Count; for (int i = 0; i < N; ++i) - fPerim += vertices[i].Dist( vertices[(i+1) % N] ); + fPerim += vertices[i].Distance( vertices[(i+1) % N] ); return fPerim; } } diff --git a/math/Segment2.cs b/math/Segment2.cs index 2eae1fcf..12724610 100644 --- a/math/Segment2.cs +++ b/math/Segment2.cs @@ -45,20 +45,13 @@ public Vector2d PointBetween(double t) { return Center + (2 * t - 1) * Extent * Direction; } - void update_from_endpoints(Vector2d p0, Vector2d p1) - { - Center = 0.5 * (p0 + p1); - Direction = p1 - p0; - Extent = 0.5 * Direction.Normalize(); - } - public double DistanceSquared(Vector2d p) { double t = (p - Center).Dot(Direction); if ( t >= Extent ) - return P1.SquaredDist(p); + return P1.DistanceSquared(p); else if ( t <= -Extent ) - return P0.SquaredDist(p); + return P0.DistanceSquared(p); Vector2d proj = Center + t * Direction; return (proj - p).LengthSquared; } @@ -73,6 +66,19 @@ public Vector2d NearestPoint(Vector2d p) return Center + t * Direction; } + public double Project(Vector2d p) + { + return (p - Center).Dot(Direction); + } + + void update_from_endpoints(Vector2d p0, Vector2d p1) + { + Center = 0.5 * (p0 + p1); + Direction = p1 - p0; + Extent = 0.5 * Direction.Normalize(); + } + + // IParametricCurve2d interface public bool IsClosed { get { return false; } } @@ -137,6 +143,44 @@ public Vector2f P1 } + // parameter is signed distance from center in direction + public Vector2f PointAt(float d) { + return Center + d * Direction; + } + + // t ranges from [0,1] over [P0,P1] + public Vector2f PointBetween(float t) { + return Center + (2.0f * t - 1.0f) * Extent * Direction; + } + + public float DistanceSquared(Vector2f p) + { + float t = (p - Center).Dot(Direction); + if ( t >= Extent ) + return P1.DistanceSquared(p); + else if ( t <= -Extent ) + return P0.DistanceSquared(p); + Vector2f proj = Center + t * Direction; + return (proj - p).LengthSquared; + } + + public Vector2f NearestPoint(Vector2f p) + { + float t = (p - Center).Dot(Direction); + if (t >= Extent) + return P1; + if (t <= -Extent) + return P0; + return Center + t * Direction; + } + + public float Project(Vector2f p) + { + return (p - Center).Dot(Direction); + } + + + void update_from_endpoints(Vector2f p0, Vector2f p1) { Center = 0.5f * (p0 + p1); diff --git a/math/Segment3.cs b/math/Segment3.cs index 0c1d2bae..352bb919 100644 --- a/math/Segment3.cs +++ b/math/Segment3.cs @@ -48,6 +48,33 @@ public Vector3d PointBetween(double t) { } + public double DistanceSquared(Vector3d p) + { + double t = (p - Center).Dot(Direction); + if ( t >= Extent ) + return P1.DistanceSquared(p); + else if ( t <= -Extent ) + return P0.DistanceSquared(p); + Vector3d proj = Center + t * Direction; + return (proj - p).LengthSquared; + } + + public Vector3d NearestPoint(Vector3d p) + { + double t = (p - Center).Dot(Direction); + if (t >= Extent) + return P1; + if (t <= -Extent) + return P0; + return Center + t * Direction; + } + + public double Project(Vector3d p) + { + return (p - Center).Dot(Direction); + } + + void update_from_endpoints(Vector3d p0, Vector3d p1) { Center = 0.5 * (p0 + p1); Direction = p1 - p0; @@ -119,26 +146,36 @@ public Vector3f PointBetween(float t) { } - public float Project(Vector3f p) + public float DistanceSquared(Vector3f p) + { + float t = (p - Center).Dot(Direction); + if ( t >= Extent ) + return P1.DistanceSquared(p); + else if ( t <= -Extent ) + return P0.DistanceSquared(p); + Vector3f proj = Center + t * Direction; + return (proj - p).LengthSquared; + } + + public Vector3f NearestPoint(Vector3f p) { - return (p - Center).Dot(Direction); + float t = (p - Center).Dot(Direction); + if (t >= Extent) + return P1; + if (t <= -Extent) + return P0; + return Center + t * Direction; } - public float DistanceSquared(Vector3f p) + + public float Project(Vector3f p) { - float t = (p - Center).Dot(Direction); - if (t <= -Extent) { - return (p - (Center - Extent * Direction)).LengthSquared; - } else if (t >= Extent) { - return (p - (Center + Extent * Direction)).LengthSquared; - } else { - Vector3f proj = Center + t * Direction; - return (proj - p).LengthSquared; - } + return (p - Center).Dot(Direction); } + void update_from_endpoints(Vector3f p0, Vector3f p1) { Center = 0.5f * (p0 + p1); diff --git a/math/Vector2d.cs b/math/Vector2d.cs index 736f4233..fb05b9a4 100644 --- a/math/Vector2d.cs +++ b/math/Vector2d.cs @@ -94,12 +94,14 @@ public double DotPerp(Vector2d v2) { } - public double SquaredDist(Vector2d o) { - return ((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y)); - } - public double Dist(Vector2d o) { - return (double)Math.Sqrt((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y)); - } + public double DistanceSquared(Vector2d v2) { + double dx = v2.x-x, dy = v2.y-y; + return dx*dx + dy*dy; + } + public double Distance(Vector2d v2) { + double dx = v2.x-x, dy = v2.y-y; + return Math.Sqrt(dx*dx + dy*dy); + } public void Set(Vector2d o) { diff --git a/math/Vector2f.cs b/math/Vector2f.cs index 153c79f0..5ca2795c 100644 --- a/math/Vector2f.cs +++ b/math/Vector2f.cs @@ -83,12 +83,14 @@ public float Cross(Vector2f v2) { - public float SquaredDist(Vector2f o) { - return ((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y)); - } - public float Dist(Vector2f o) { - return (float)Math.Sqrt((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y)); - } + public float DistanceSquared(Vector2f v2) { + float dx = v2.x-x, dy = v2.y-y; + return dx*dx + dy*dy; + } + public float Distance(Vector2f v2) { + float dx = v2.x-x, dy = v2.y-y; + return (float)Math.Sqrt(dx*dx + dy*dy); + } public void Set(Vector2f o) { diff --git a/math/Vector3f.cs b/math/Vector3f.cs index 5b9a0b85..c958bf24 100644 --- a/math/Vector3f.cs +++ b/math/Vector3f.cs @@ -121,7 +121,7 @@ public static float AngleR(Vector3f v1, Vector3f v2) { } - public float SqrDistance(Vector3f v2) { + public float DistanceSquared(Vector3f v2) { float dx = v2.x-x, dy = v2.y-y, dz = v2.z-z; return dx*dx + dy*dy + dz*dz; } diff --git a/queries/RayIntersection.cs b/queries/RayIntersection.cs index 4f149ddc..a3d27cd6 100644 --- a/queries/RayIntersection.cs +++ b/queries/RayIntersection.cs @@ -79,7 +79,7 @@ public static bool InfiniteCylinderSigned(Vector3f vOrigin, Vector3f vDirection, Vector3f AB = vCylAxis; Vector3f AO = (vOrigin - vCylOrigin); - if (AO.SqrDistance(AO.Dot(AB) * AB) > fRadius * fRadius) + if (AO.DistanceSquared(AO.Dot(AB) * AB) > fRadius * fRadius) return false; Vector3f AOxAB = AO.Cross(AB);