GeoRegression
GeoRegression copied to clipboard
support distance between point and bounded cylinder
Hi again,
most of real world cylinders are not unbounded. I figured that out while testing your distance function. :-) I drafted an implementation for the distance with bounded cylinder. It uses some simple geometrical reasoning with planes. I am not sure it is optimal, but I am sure we can further improve it.
`def pointToCylinderDistance(pointObj: Point3D_F64, cylinder: CylinderBounded3D_F64): JDouble = {
val slope = new Vector3D_F64(cylinder.getEndA, cylinder.getEndB)
val plane1 = UtilPlane3D_F64.convert(new PlaneNormal3D_F64(cylinder.endA, slope), null)
val plane2 = UtilPlane3D_F64.convert(new PlaneNormal3D_F64(cylinder.endB, slope), null)
val unBoundedCylinder = new Cylinder3D_F64(
new LineParametric3D_F64(cylinder.endA, slope), cylinder.getRadius)
val unboundedDistance = Distance3D_F64.distance(unBoundedCylinder, pointObj)
val plane1Distance = Distance3D_F64.distance(plane1, pointObj)
val plane2Distance = Distance3D_F64.distance(plane2, pointObj)
val cylinderHeight = slope.norm()
val result : JDouble = if(plane1Distance + plane2Distance == cylinderHeight) {
unboundedDistance
} else if(plane1Distance > plane2Distance) {
if(unboundedDistance <= 0) {
plane2Distance
} else {
Math.sqrt(Math.pow(plane2Distance, 2.0) + Math.pow(unboundedDistance, 2.0))
}
} else {
if(unboundedDistance <= 0) {
plane1Distance
} else {
Math.sqrt(Math.pow(plane1Distance, 2.0) + Math.pow(unboundedDistance, 2.0))
}
}
result
}`
Again thanks for this code. It does help if you can include unit tests. Less development on my end results in getting the code out the door faster.
The idea is that we check the distance between the planes of the tops and bottom of the cylinder to decide whether the point is above, below, or on the side of the cylinder. If on the side, we use the same unbounded cylinder distance. If on top or below, we use the distance from the plans itself, otherwise we compute the diagonal using the distance from the side and the distance from the top/bottom of the cylinder. I have no unit test specific for this function, sorry.