Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support distance between point and bounded cylinder #11

Open
stefanobortoli opened this issue Nov 8, 2018 · 2 comments
Open

support distance between point and bounded cylinder #11

stefanobortoli opened this issue Nov 8, 2018 · 2 comments

Comments

@stefanobortoli
Copy link

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

}`

@lessthanoptimal
Copy link
Owner

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.

@stefanobortoli
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants