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/tri3d.sml
 [sml3d] / trunk / sml3d / src / geometry / tri3d.sml View of /trunk/sml3d/src/geometry/tri3d.sml

Tue Mar 16 20:42:23 2010 UTC (9 years, 7 months ago) by jhr
File size: 1427 byte(s)
Working on geometry library
(* tri3d.sml
*
* COPYRIGHT (c) 2009 John Reppy (http://cs.uchicago.edu/~jhr)
*
* Double-precision floating-point triangles in 3d.
*)

structure Tri3d : TRIANGLE3 where type flt = Vec3d.flt
= struct

structure V = Vec3d

type flt = V.flt
type vec = V.vec3

(* we assume CCW winding, which is the standard for a RHS
* coordinate system.
*)
type tri = vec SML3dTypes.triangle

(* flip the triangle (i.e., reverse the order of its vertices *)
fun flip {v1, v2, v3} = {v1 = v1, v2 = v3, v3 = v2}

(* return the triangle's normal vector (the zero vector
* if it is degenerate).
*)
fun normal {v1, v2, v3} =
V.normalize (V.cross(V.sub(v2, v1), V.sub(v3, v1)))

(* map barycentric coordinates to a point *)
fun barycentricCoords {v1, v2, v3} = let
fun toPt {alpha : flt, beta : flt} =
V.scale (alpha, v1),
beta, v2),
1.0 - alpha - beta, v3)
in
toPt
end

(* return the plane defined by a triangle; note that we don't normalize the norm, since
* fromPtNorm will do that.
*)
fun toPlane tri =
Planed.fromPtNorm {p = v1, n = V.cross(V.sub(v2, v1), V.sub(v3, v1))}

(* iterators *)
val app : (vec -> unit) -> tri -> unit = Triangle.app
val map : (vec -> 'a) -> tri -> 'a GTypes.triangle = Triangle.map

(* string conversion *)
val toString = Triangle.fmt V.toString

end

 root@smlnj-gforge.cs.uchicago.edu ViewVC Help Powered by ViewVC 1.0.0