Skip to content

Commit

Permalink
make some names in vector/segment consistent, warning fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Jan 16, 2017
1 parent 6602fbb commit c835054
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 48 deletions.
8 changes: 4 additions & 4 deletions approximation/BiArcFit2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IParametricCurve2d> Curves {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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...

Expand Down
2 changes: 1 addition & 1 deletion curve/Arc2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions curve/Circle2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions curve/EllipseArc2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions curve/PlanarSolid2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace g3
public class PlanarSolid2d
{
IParametricCurve2d outer;
bool bOuterIsCW;
//bool bOuterIsCW;

List<IParametricCurve2d> holes = new List<IParametricCurve2d>();

Expand All @@ -30,7 +30,7 @@ public void SetOuter(IParametricCurve2d loop, bool bIsClockwise)
{
Debug.Assert(loop.IsClosed);
outer = loop;
bOuterIsCW = bIsClockwise;
//bOuterIsCW = bIsClockwise;
}


Expand Down
2 changes: 1 addition & 1 deletion curve/Polygon2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
62 changes: 53 additions & 9 deletions math/Segment2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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; } }
Expand Down Expand Up @@ -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);
Expand Down
61 changes: 49 additions & 12 deletions math/Segment3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions math/Vector2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions math/Vector2f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion math/Vector3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion queries/RayIntersection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c835054

Please sign in to comment.