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 box #9

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

support distance between point and box #9

stefanobortoli opened this issue Nov 7, 2018 · 2 comments

Comments

@stefanobortoli
Copy link

Hi, first of all, great work.

I could not find an implementation of Point to Box, so I created one. If you want you can add it to Distance3D. It is based on a solution I found on stack overflow. Not sure it is optimal, or 100%, but I let you judge. Its in scala unfortunately.

` def pointToBoxDistance(box: BoxLength3D_F64, point: Point3D_F64): JDouble = {

val origin = box.getP
val vectorX = new Vector3D_F64(box.lengthX, 0, 0)
val vectorY = new Vector3D_F64(0, box.lengthY, 0)
val vectorZ = new Vector3D_F64(0, 0, box.lengthZ)

val vectorOrigintoPoint = new Vector3D_F64(
  point.getX - origin.getX,
  point.getY - origin.getY,
  point.getZ - origin.getZ)

var tx: Double = vectorOrigintoPoint.dot(vectorX) / vectorX.dot(vectorX)
var ty: Double = vectorOrigintoPoint.dot(vectorY) / vectorY.dot(vectorY)
var tz: Double = vectorOrigintoPoint.dot(vectorZ) / vectorZ.dot(vectorZ)

tx = if (tx < 0d) { 0d } else if (tx > 1.0d) { 1.0 } else { tx }
ty = if (ty < 0d) { 0d } else if (ty > 1.0d) { 1.0 } else { ty }
ty = if (tz < 0d) { 0d } else if (tz > 1.0d) { 1.0 } else { tz }

val closestVector = vectorX.times(tx).plus(vectorY.times(ty)).plus(vectorZ.times(tz)).plus(origin)
val closestPoint: Point3D_F64 = new Point3D_F64(closestVector.getX, closestVector.getY, closestVector.getZ)

UtilPoint3D_F64.distance(point.getX, point.getY, point.getZ,
  closestPoint.getX, closestPoint.getY, closestPoint.getZ)

}`

@lessthanoptimal
Copy link
Owner

thanks for the submission! I'll add this feature in the next release hopefully.

@stefanobortoli
Copy link
Author

you are welcome :-)

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