Distance from a Point to a Plane (3D)

Difference between version 0 and 1 - Previous - Next
I used to sleep a lot in math class... For this question https://groups.google.com/forum/m/#!topic/comp.lang.tcl/iOedyEIUC4E%|%Calculating Distance from a Point to a Plane
%|%I can answer... Maybe there’s a faster way to calculate...

======
namespace eval Vector3D {

    namespace export *

    proc sub_v3v3 {v0 v1} {

    lassign $v0 v0x v0y v0z
    lassign $v1 v1x v1y v1z

    return [list [expr {$v0x - $v1x}] \
                 [expr {$v0y - $v1y}] \
                 [expr {$v0z - $v1z}]]

    }

    proc dot_v3v3 {v0 v1} {

        lassign $v0 v0x v0y v0z
        lassign $v1 v1x v1y v1z

        return [expr {($v0x * $v1x) + ($v0y * $v1y) +  ($v0z * $v1z)}]

    }

    proc cross_v3v3 {v0 v1} {

        lassign $v0 vx0 vy0 vz0
        lassign $v1 vx1 vy1 vz1

        return [list [expr {($vy0 * $vz1) - ($vy1 * $vz0)}] \
                     [expr {($vz0 * $vx1) - ($vx0 * $vz1)}] \
                     [expr {($vx0 * $vy1) - ($vy0 * $vx1)}]]

    }

    proc norm {v} {

        lassign $v vx vy vz

        return [expr {sqrt($vx**2 + $vy**2 + $vz**2)}]

    }

    proc unit {v} {

        set n [norm $v]

        if {$n == 0} {
            error "Must be greatest than 0..."
        }

        lassign $v x y z

        return [list [expr {$x / double($n)}] \
                     [expr {$y / double($n)}] \
                     [expr {$z / double($n)}]]
        
    }


}
======

   * Utilisation :

======
namespace import Vector3D::*

set plan {{-10 -10 10} {10 -10 10} {0 10 10}} ; # 3 points on plan (3d)
set point {0 0 12} ; # point (3d)

lassign $plan v0 v1 v2

set u [sub_v3v3 $v1 $v0]
set v [sub_v3v3 $v2 $v0]
set normal [unit [cross_v3v3 $u $v]] ; # normal to plane

set sub [sub_v3v3 $point $v0]
set dist [dot_v3v3 $normal $sub]

puts "Distance 3D = $dist" 
# Distance 3D = 2.0
======
Here is my pure Tcl implementation of the task to compute the distance of a point to a plane in 3D. The procedures are optimized for maximum performance. I did not compare to above implementation.

======

## Get the distance of a point p to a plane given by origin point a and normal vector u.
proc DistPoint2Plane {p a u {abskey 1}} {
  # Normalize vector u to the length of one
  set u [VectorNormalize $u]
  # Get vector from a to p
  set ap [VectorsDifference $p $a]
  # Return the distance as scalar product of u and ap
  if {$abskey} {
    return [expr {abs([VectorsDotProduct $u $ap])}]
  } else {
    return [expr {[VectorsDotProduct $u $ap]}]
  }
}
## Normalize vector a to length 1
proc VectorNormalize {a} {
  set norm [VectorNorm $a]
  if {$norm!=0} {
    return [list [expr {[lindex $a 0]/double($norm)}] [expr {[lindex $a 1]/double($norm)}] [expr {[lindex $a 2]/double($norm)}]]
  } else {
    return $a
  }
}
## Compute the length of vector a
proc VectorNorm {a} {
  lassign $a a1 a2 a3
  return [expr {sqrt(pow($a1,2)+pow($a2,2)+pow($a3,2))}]
}
## compute the difference of vector a and b (a-b)
proc VectorsDifference {a b} {
  if {[lindex $a 3]=="" || [lindex $b 3]==""} {
    return [list [expr {[lindex $a 0]-[lindex $b 0]}] [expr {[lindex $a 1]-[lindex $b 1]}] [expr {[lindex $a 2]-[lindex $b 2]}]]
  } else {
    return [list [expr {[lindex $a 0]-[lindex $b 0]}] [expr {[lindex $a 1]-[lindex $b 1]}] [expr {[lindex $a 2]-[lindex $b 2]}] [expr {[lindex $a 3]-[lindex $b 3]}] [expr {[lindex $a 4]-[lindex $b 4]}] [expr {[lindex $a 5]-[lindex $b 5]}]]
  }
}
## Compute the dot product of vectors a and b
proc VectorsDotProduct {a b} {
  lassign $a a1 a2 a3
  lassign $b b1 b2 b3
  return [expr {$a1*$b1+$a2*$b2+$a3*$b3}]
}
======

Usage:
======
DistPoint2Plane {0 0 10} {0 0 0} {0 0 1}
======