-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Labels
Description
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)
}`