Mathematical Principles for Scientific Computing and Visualization
http : // www.farinhansford.com/books/scv
by Gerald Farin and Dianne Hansford

This notebook demonstrates elements of marching cubes for contouring of 3d functions.

Figures  15.6 was created with this notebook.

See Chapter 15 : Volume Visualization for details.

Updated May 2008

(* Set the output folder *) SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];

(* Set some color variables *)
trigray=0.7;
pntgray=0.0;
cubegray=0.8; 

(* Plot range  -- this keeps Mathematica from clipping part of the picture *)
r0=-0.1;
r1=1.1;

(* Here is our cube centered at the origin, shown with edges only *)
cube = Graphics3D[{Opacity[0], EdgeForm[Thick],Boxed->False, Cuboid[]}];


(* Create points at select corners *)
(* This will mark vertices that are on or above the contour value *)

corner000=Graphics3D[{PointSize[0.1],Point[{0,0,0}]}];

corner100=Graphics3D[{PointSize[0.1],Point[{1,0,0}]}];

corner101=Graphics3D[{PointSize[0.1],Point[{1,0,1}]}];

corner001=Graphics3D[{PointSize[0.1],Point[{0,0,1}]}];

(* Create the marching cubes contour results that correspond to difference sets of vertices on or above the contour level *)

(* THis is a triangle surrounding one vertex *)
polygon0=Graphics3D[{GrayLevel[trigray],Polygon[{{0.5,0,0},{0,0.5,0},{0,0,0.5}}]}];

(* old stuff *)
(*
tpoint0=Graphics3D[{PointSize[0.03],Point[{0.0,0.5,0}],GrayLevel[trigray]}];

tpoint1=Graphics3D[{PointSize[0.03],Point[{0.0,0,0.5}]}]

tpoint2=Graphics3D[{PointSize[0.03],Point[{0.5,0,0}]}];

*)

pic1=Show[cube,corner000,polygon0,Boxed->False,PlotRange->{{r0,r1},{r0,r1},{r0,r1}}]

(* ---- next case ----- *)

polygon1=Graphics3D[{GrayLevel[0.7`],Polygon[{{0,0,0.5`},{0,0.5`,0},{1,0.5`,0}}]}];

polygon2=Graphics3D[{GrayLevel[0.7`],Polygon[{{0,0,0.5`},{1,0.5`,0},{1,0,0.5`}}]}];

(pic2=Show[cube,polygon1,polygon2,corner000,corner100,Boxed->False,ViewPoint->{2.208`,-1.112`,2.671`},PlotRange->{{r0,r1},{r0,r1},{r0,r1}}]) 



(* ---- next case ----- *)


polygon3=Graphics3D[{GrayLevel[trigray],Polygon[{{1,0,0.5`},{1,0.5`,1},{0.5`,0,1}}]}];

pic3=Show[cube,corner000,polygon0,corner101,polygon3,Boxed->False,PlotRange->{{r0,r1},{r0,r1},{r0,r1}}]


(* ---- next case ----- *)


polygon4=Graphics3D[{GrayLevel[trigray],Polygon[{{0,0.5`,0},{1,0.5`,0},{1,0,0.5`}}]}];

polygon5=Graphics3D[{GrayLevel[trigray],Polygon[{{0,0.5`,0},{1,0,0.5`},{0.5`,0,1}}]}];

polygon6=Graphics3D[{GrayLevel[trigray],Polygon[{{0,0.5`,0},{0.5`,0,1},{0,0.5`,1}}]}];

 pic4=Show[cube,corner000,corner100,corner001,polygon4,polygon5,polygon6,Boxed->False,ViewPoint->{2.208`,-1.112`,2.671`},PlotRange->{{r0,r1},{r0,r1},{r0,r1}}]

(* Put all the pics together *)
g=GraphicsRow[{pic1,pic2,pic3,pic4}];

 Show[g] 

(* Output pic to postscript file *)
(* Export["marchingcubes.eps",%] *)