Skip to content

Commit

Permalink
- Added functions which take in the parameters for a point
Browse files Browse the repository at this point in the history
- Renamed function to clarify that distance is signed.
  • Loading branch information
lessthanoptimal committed Nov 21, 2023
1 parent 51ab7ab commit b0c6835
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2020, Peter Abeles. All Rights Reserved.
* Copyright (C) 2022, Peter Abeles. All Rights Reserved.
*
* This file is part of Geometric Regression Library (GeoRegression).
*
Expand Down Expand Up @@ -31,7 +31,7 @@

/**
* Computes the signed Euclidean distance between a cylinder and a set of points, see
* {@link Distance3D_F64#distance(georegression.struct.shapes.Cylinder3D_F64, georegression.struct.point.Point3D_F64)}.
* {@link Distance3D_F64#distanceSigned(georegression.struct.shapes.Cylinder3D_F64, georegression.struct.point.Point3D_F64)}.
*
* See {@link CodecCylinder3D_F64} for how the model is parametrized.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public void setModel( Cylinder3D_F64 plane ) {

@Override
public /**/double distance( PlaneNormal3D_F64 point ) {
return Math.abs(Distance3D_F64.distance(cylinder, point.p));
return Math.abs(Distance3D_F64.distanceSigned(cylinder, point.p));
}

@Override
public void distances( List<PlaneNormal3D_F64> list, /**/double[] errors ) {
for (int i = 0; i < list.size(); i++) {
errors[i] = Math.abs(Distance3D_F64.distance(cylinder, list.get(i).p));
errors[i] = Math.abs(Distance3D_F64.distanceSigned(cylinder, list.get(i).p));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2020, Peter Abeles. All Rights Reserved.
* Copyright (C) 2022, Peter Abeles. All Rights Reserved.
*
* This file is part of Geometric Regression Library (GeoRegression).
*
Expand Down Expand Up @@ -27,7 +27,7 @@

/**
* Computes the signed Euclidean distance between a sphere and a set of points, see
* {@link Distance3D_F64#distance(georegression.struct.shapes.Sphere3D_F64, georegression.struct.point.Point3D_F64)}.
* {@link Distance3D_F64#distanceSigned(georegression.struct.shapes.Sphere3D_F64, georegression.struct.point.Point3D_F64)}.
*
* See {@link georegression.fitting.sphere.CodecSphere3D_F64} for how the model is parametrized.
* For use in least-squares non-linear minimization.
Expand Down Expand Up @@ -65,7 +65,7 @@ public void process( /**/double[] input, /**/double[] output) {
codec.decode(input,sphere);

for( int i = 0; i < points.size(); i++ ) {
output[i] = Distance3D_F64.distance(sphere,points.get(i));
output[i] = Distance3D_F64.distanceSigned(sphere,points.get(i));
}
}
}
46 changes: 30 additions & 16 deletions main/src/georegression/metric/Distance3D_F64.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public class Distance3D_F64 {
* @param l1 Second line. Not modified.
* @return Distance between the closest point on both lines.
*/
public static double distance( LineParametric3D_F64 l0,
LineParametric3D_F64 l1 ) {
public static double distance( LineParametric3D_F64 l0, LineParametric3D_F64 l1 ) {
double x = l0.p.x - l1.p.x;
double y = l0.p.y - l1.p.y;
double z = l0.p.z - l1.p.z;
Expand Down Expand Up @@ -86,10 +85,22 @@ public static double distance( LineParametric3D_F64 l0,
* @return distance.
*/
public static double distance( LineParametric3D_F64 l, Point3D_F64 p ) {
return distance(l, p.x, p.y, p.z);
}

double x = l.p.x - p.x;
double y = l.p.y - p.y;
double z = l.p.z - p.z;
/**
* Distance from the point to the closest point on the line.
*
* @param l Line. Not modified.
* @param px x-axis coordinate of point
* @param py y-axis coordinate of point
* @param pz z-axis coordinate of point
* @return distance.
*/
public static double distance( LineParametric3D_F64 l, double px, double py, double pz ) {
double x = l.p.x - px;
double y = l.p.y - py;
double z = l.p.z - pz;

double cc = x*x + y*y + z*z;

Expand All @@ -115,8 +126,7 @@ public static double distance( LineParametric3D_F64 l, Point3D_F64 p ) {
* @param p Point. Not modified.
* @return distance.
*/
public static double distance( LineSegment3D_F64 l,
Point3D_F64 p ) {
public static double distance( LineSegment3D_F64 l, Point3D_F64 p ) {

double dx = p.x - l.a.x;
double dy = p.y - l.a.y;
Expand Down Expand Up @@ -163,47 +173,51 @@ public static double distanceSigned( PlaneGeneral3D_F64 plane, Point3D_F64 point
}

/**
* <p>
* Returns the signed distance a point is from the sphere's surface. If the point is outside of the sphere
* it's distance will be positive. If it is inside it will be negative.
* <p></p>
* </p>
* distance = ||sphere.center - point|| - r
*
* @param sphere The sphere
* @param point The point
* @return Signed distance
*/
public static double distance( Sphere3D_F64 sphere, Point3D_F64 point ) {
public static double distanceSigned( Sphere3D_F64 sphere, Point3D_F64 point ) {
double r = point.distance(sphere.center);
return r - sphere.radius;
}

/**
* Returns the signed distance a point is from the cylinder's surface. If the point is outside of the cylinder
* Returns the signed distance a point is from the cylinder's surface. If the point is outside the cylinder
* it's distance will be positive. If it is inside it will be negative.
*
* @param cylinder The cylinder
* @param point The point
* @return Signed distance
*/
public static double distance( Cylinder3D_F64 cylinder, Point3D_F64 point ) {
double r = Distance3D_F64.distance(cylinder.line, point);
public static double distanceSigned( Cylinder3D_F64 cylinder, Point3D_F64 point ) {
return distanceSigned(cylinder, point.x, point.y, point.z);
}

public static double distanceSigned( Cylinder3D_F64 cylinder, double px, double py, double pz ) {
double r = Distance3D_F64.distance(cylinder.line, px, py, pz);
return r - cylinder.radius;
}

/**
* Signed distance from a 3D point to 3D triangle. The sign indicates which side of the triangle the point
* Signed distance from a 3D point to 3D triangle. The sign indicates which side of the triangle the point
* is on. See {@link georegression.metric.alg.DistancePointTriangle3D_F64} for the details.
*
* @param triangle 3D triangle
* @param point Point for which the closest point on the triangle is found
* @return The closest point
*/
public static double distance( Triangle3D_F64 triangle, Point3D_F64 point ) {

DistancePointTriangle3D_F64 alg = new DistancePointTriangle3D_F64();
var alg = new DistancePointTriangle3D_F64();
alg.setTriangle(triangle.v0, triangle.v1, triangle.v2);

Point3D_F64 cp = new Point3D_F64();
var cp = new Point3D_F64();

alg.closestPoint(point, cp);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class TestShapeFittingRobustOps {
// See if the points lie on the found plane, except for the outlier
int count = 0;
for (int i = 0; i < points.size(); i++) {
if (Math.abs(Distance3D_F64.distance(found, points.get(i).p)) < 1) {
if (Math.abs(Distance3D_F64.distanceSigned(found, points.get(i).p)) < 1) {
count++;
}
}
Expand All @@ -165,7 +165,7 @@ class TestShapeFittingRobustOps {
// See if the points lie on the found plane, except for the outlier
int count = 0;
for (int i = 0; i < points.size(); i++) {
if (Math.abs(Distance3D_F64.distance(found, points.get(i).p)) < 1) {
if (Math.abs(Distance3D_F64.distanceSigned(found, points.get(i).p)) < 1) {
count++;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2020, Peter Abeles. All Rights Reserved.
* Copyright (C) 2022, Peter Abeles. All Rights Reserved.
*
* This file is part of Geometric Regression Library (GeoRegression).
*
Expand Down Expand Up @@ -63,7 +63,7 @@ void compareToDistance() {
alg.process(param,output);

for( int i = 0; i < points.size(); i++ ) {
double expected = Distance3D_F64.distance(cylinder, points.get(i));
double expected = Distance3D_F64.distanceSigned(cylinder, points.get(i));
assertEquals(expected,(double) output[i], GrlConstants.TEST_F64);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ class TestGenerateCylinderFromPointNormals_F64 {
PlaneNormal3D_F64 p1 = paramToPointOnCylinder(cylinder, rand.nextGaussian(), theta + Math.PI/2.0);

// Sanity check
assertEquals(0.0, Distance3D_F64.distance(cylinder, p0.p), UtilEjml.TEST_F64);
assertEquals(0.0, Distance3D_F64.distance(cylinder, p1.p), UtilEjml.TEST_F64);
assertEquals(0.0, Distance3D_F64.distanceSigned(cylinder, p0.p), UtilEjml.TEST_F64);
assertEquals(0.0, Distance3D_F64.distanceSigned(cylinder, p1.p), UtilEjml.TEST_F64);

assertTrue(alg.generate(List.of(p0, p1), found));

// Verify the found cylinder is correct by seeing if the two points lie on it
assertEquals(0.0, Distance3D_F64.distance(found, p0.p), UtilEjml.TEST_F64);
assertEquals(0.0, Distance3D_F64.distance(found, p1.p), UtilEjml.TEST_F64);
assertEquals(0.0, Distance3D_F64.distanceSigned(found, p0.p), UtilEjml.TEST_F64);
assertEquals(0.0, Distance3D_F64.distanceSigned(found, p1.p), UtilEjml.TEST_F64);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2020, Peter Abeles. All Rights Reserved.
* Copyright (C) 2022, Peter Abeles. All Rights Reserved.
*
* This file is part of Geometric Regression Library (GeoRegression).
*
Expand Down Expand Up @@ -60,7 +60,7 @@ void compareToDistance() {
alg.process(param,output);

for( int i = 0; i < points.size(); i++ ) {
double expected = Distance3D_F64.distance(sphere,points.get(i));
double expected = Distance3D_F64.distanceSigned(sphere,points.get(i));
assertEquals(expected,(double) output[i], GrlConstants.TEST_F64);
}
}
Expand Down
10 changes: 5 additions & 5 deletions main/test/georegression/metric/TestDistance3D_F64.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020, Peter Abeles. All Rights Reserved.
* Copyright (C) 2022, Peter Abeles. All Rights Reserved.
*
* This file is part of Geometric Regression Library (GeoRegression).
*
Expand Down Expand Up @@ -159,8 +159,8 @@ void distance_sphere_point() {
assertTrue(ro>4.5);
assertTrue(ri<4.5);

assertEquals(ro-4.5,Distance3D_F64.distance(sphere,outside), GrlConstants.TEST_F64);
assertEquals(ri-4.5,Distance3D_F64.distance(sphere,inside), GrlConstants.TEST_F64);
assertEquals(ro-4.5,Distance3D_F64.distanceSigned(sphere,outside), GrlConstants.TEST_F64);
assertEquals(ri-4.5,Distance3D_F64.distanceSigned(sphere,inside), GrlConstants.TEST_F64);
}

@Test
Expand All @@ -176,8 +176,8 @@ void distance_cylinder_point() {
assertTrue(ro>3.5);
assertTrue(ri<3.5);

assertEquals(ro-3.5,Distance3D_F64.distance(cylinder,outside), GrlConstants.TEST_F64);
assertEquals(ri-3.5,Distance3D_F64.distance(cylinder,inside), GrlConstants.TEST_F64);
assertEquals(ro-3.5,Distance3D_F64.distanceSigned(cylinder,outside), GrlConstants.TEST_F64);
assertEquals(ri-3.5,Distance3D_F64.distanceSigned(cylinder,inside), GrlConstants.TEST_F64);
}

@Test
Expand Down

0 comments on commit b0c6835

Please sign in to comment.