Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
 Summary Activity SCM

# SCM Repository

[sml3d] View of /trunk/sml3d/src/geometry/planef.sml
 [sml3d] / trunk / sml3d / src / geometry / planef.sml

# View of /trunk/sml3d/src/geometry/planef.sml

Sun Feb 14 16:30:15 2010 UTC (9 years, 8 months ago) by jhr
File size: 1770 byte(s)
```  Remove "3" from plane module names
```
```(* planef.sml
*
* COPYRIGHT (c) 2009 John Reppy (http://cs.uchicago.edu/~jhr)
*
* Support for 3D planes using single-precision representation.
*)

structure Planef : PLANE where type flt = Float.flt =
struct

structure V = Vec3f

type flt = V.flt
type vec3 = V.vec3
type vec4 = V.vec4

(* a plane is represented as its normal vector <x, y, z> and the distance
* to the origin along that vector.
*)
type plane = {nx : flt, ny : flt, nz : flt, d : flt}

(* axis aligned planes; the order of the axes determines the plane's
* facing following the right-hand rule.
*)
val xyPlane : plane = {nx =  0.0, ny =  0.0, nz =  1.0, d = 0.0} (* points toward +Z *)
val xzPlane : plane = {nx =  0.0, ny = ~1.0, nz =  0.0, d = 0.0} (* points toward -Y *)
val yxPlane : plane = {nx =  0.0, ny =  0.0, nz = ~1.0, d = 0.0} (* points toward -Z *)
val yzPlane : plane = {nx =  1.0, ny =  0.0, nz =  0.0, d = 0.0} (* points toward +X *)
val zxPlane : plane = {nx =  0.0, ny =  1.0, nz =  0.0, d = 0.0} (* points toward +Y *)
val zyPlane : plane = {nx = ~1.0, ny =  0.0, nz =  0.0, d = 0.0} (* points toward -X *)

fun fromPtNorm {p, n} = let
val n = V.normalize n
val d = ~ (V.dot(p, n))
in
{nx = #x n, ny = #y n, nz = #z n, d = d}
end

fun fromNormDist {n : vec3, d : flt} = {nx = #x n, ny = #y n, nz = #z n, d = d}

(* return the plane's unit normal vector *)
fun norm (p : plane) = {x = #nx p, y = #ny y, z = #ny z}

(* return signed distance from plane to point (positive means the
* point is in front of the plane and negative means that it is
* behind.
*)
fun distance (p : plane, {x, y, z}) = (#nx p * x) + (#ny p * y) + (#nz p * z) + #d p

end
```