(* Seth Porter OFFWriter3d.sml Dump a simplicial complex to GeomView's OFF format. Currently only supports 3d vertices. *) functor OFFWriter3d(structure Complex : SIMPLICIAL_COMPLEX where type Vertex.point = RealGeometry3d.Point.point) : SIMP_COMP_WRITER where Complex = Complex = struct structure Complex = Complex structure Helper = WriterHelper(structure Complex = Complex) structure Point = RealGeometry3d.Point structure Simplex = Complex.Simplex structure Vertex = Simplex.Vertex fun write(filename, complex, dummy) = let val nv = Sequence.length(Complex.vertices complex) val faces = (Sequence.length(Complex.simplices complex 2)) fun preamble () = "OFF\n" ^ Int.toString(nv) ^ " " ^ Int.toString(faces) ^ " 1\n" fun format_vertex v = let val coords = Point.toSeq (Vertex.location v) fun toS(r, s) = s ^ " " ^ (Real.toString (RealGeometry3d.Number.toReal r)) in (Sequence.foldl toS "" coords) ^ "\n" end fun format_face(s, get_v_num) = let val verts = Sequence.map (fn v => (get_v_num v) - 1) (Simplex.vertices s) fun toS(i, s) = s ^ " " ^ (Int.toString i) in (Sequence.foldl toS "3 " verts) ^ "\n" end in Helper.write(complex, filename, preamble, format_vertex, format_face, dummy) end end