(*:Title: MM2MESH *)

(*:Author: Alfred Gray *)

(* Copyright 1994  by Alfred Gray *)

(*:Package Version: 1.0 *)

(*:Mathematica Version: 2.2 *)

(*:Summary: 
	This package consists of programs that write files in Geomview's 
mesh format to displsy curves and surfaces. The files can then be displayed 
using the external program Geomview.
*)

(*:Keywords:
Write2COLMESH, Write3DCMESH, Write3DCNMESH, Write3DMESH
*)

Print["For a list of commands type mm2mesh."]

BeginPackage["MM2MESH`","Graphics`Colors`","SURFS`","GMCUR`"]

    mm2mesh:=Print[Select[Names["MM2MESH`*"],#!="mm2mesh"&]]

Write3DMESH::usage="Write3DMESH[file,expression,{u,umin,umax},{v,vmin,vmax},
PlotPoints->{udiv,vdiv},opts] outputs to a file in mesh format the
parametric plot of a surface."

Write3DCMESH::usage="Write3DCMESH[file,{expression,color},
{u,umin,umax},{v,vmin,vmax},PlotPoints->{udiv,vdiv},opts] outputs to a file
in mesh format the parametric plot of expression colored by color."

Write3DCNMESH::usage="Write3DCNMESH[file,{expression,unitn,color},
{u,umin,umax},{v,vmin,vmax},
PlotPoints->{udiv,vdiv},opts] outputs to a file in mesh format the
parametric plot of expression colored by color with normal info unitn."

Write2COLMESH::usage="Write2COLMESH[{color1,color2},file,expression,
{u,umin,umax},{v,vmin,vmax},
PlotPoints->{udiv,vdiv},opts] outputs to a file in mesh format the parametric plot of a surface with one side color1 and the other color2."

Begin["`Private`"]

Write3DMESH[file_String,expression_,
{u_,umin_,umax_},{v_,vmin_,vmax_},
PlotPoints->{udiv_,vdiv_},otmp___]:=
Module[{tmp1,tmp2,tmp3,tmp4},
OpenWrite[file];
WriteString[file,"MESH\n"];
WriteString[file,ToString[vdiv+1]];
WriteString[file," "];
WriteString[file,ToString[udiv+1]];
WriteString[file,"\n"];
tmp1=N[Table[expression,
    {u,umin,umax,(umax-umin)/udiv},
    {v,vmin,vmax,(vmax-vmin)/vdiv}]];
tmp2=Flatten[tmp1,1];
tmp3=Chop[tmp2,10^-4];
tmp4=Map[PaddedForm[#,{4,4}]&,tmp3,{2}];
Scan[
   (WriteString[file,First[#]];
    Scan[WriteString[file,"   ",#]&,Rest[#]];
    WriteString[file,"\n"])&,tmp4];
Close[file]]

Write3DCMESH[file_String,{expression_,color_},
{u_,umin_,umax_},{v_,vmin_,vmax_},
PlotPoints->{udiv_,vdiv_},otmp___]:=
Module[{tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7},
OpenWrite[file];
WriteString[file,"CMESH\n"];
WriteString[file,ToString[vdiv+1]];
WriteString[file," "];
WriteString[file,ToString[udiv+1]];
WriteString[file,"\n"];
tmp1=N[Table[{expression,HSBColor[color,1,1]},
    {u,umin,umax,(umax-umin)/udiv},
    {v,vmin,vmax,(vmax-vmin)/vdiv}]];
tmp2=Apply[List,tmp1,{3}];
tmp3=Map[Flatten[#]&,tmp2,{2}];
tmp4=Flatten[tmp3,1];
tmp5=Map[Append[#,1]&,tmp4];
tmp6=Chop[tmp5,10^-4];
tmp7=Map[PaddedForm[#,{4,4}]&,tmp6,{2}];
Scan[
   (WriteString[file,First[#]];
    Scan[WriteString[file,"   ",#]&,Rest[#]];
    WriteString[file,"\n"])&,tmp7];
Close[file]]



Write3DCNMESH[file_String,{expression_,unitn_,color_},
{u_,umin_,umax_},{v_,vmin_,vmax_},
PlotPoints->{udiv_,vdiv_},otmp___]:=
Module[{tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7},
OpenWrite[file];
WriteString[file,"CNMESH\n"];
WriteString[file,ToString[vdiv+1]];
WriteString[file," "];
WriteString[file,ToString[udiv+1]];
WriteString[file,"\n"];
tmp1=N[Table[{expression,unitn,HSBColor[color,1,1]},
    {u,umin,umax,(umax-umin)/udiv},
    {v,vmin,vmax,(vmax-vmin)/vdiv}]];
tmp2=Apply[List,tmp1,{3}];
tmp3=Map[Flatten[#]&,tmp2,{2}];
tmp4=Flatten[tmp3,1];
tmp5=Map[Append[#,1]&,tmp4];
tmp6=Chop[tmp5,10^-4];
tmp7=Map[PaddedForm[#,{4,4}]&,tmp6,{2}];
Scan[
   (WriteString[file,First[#]];
    Scan[WriteString[file,"   ",#]&,Rest[#]];
    WriteString[file,"\n"])&,tmp7];
Close[file]]

Write2COLMESH[file_String,expression_,
{u_,umin_,umax_},{v_,vmin_,vmax_},
PlotPoints->{udiv_,vdiv_},otmp___]:=
Module[{tmp1,tmp2,tmp3,tmp4},
OpenWrite[file];
WriteString[file,"appearance {\n"];
WriteString[file,"material {\n"];
WriteString[file,"emission 1 1 0\n"];
WriteString[file,"diffuse  1 0 0}}\n"];
WriteString[file,"MESH\n"];
WriteString[file,ToString[vdiv+1]];
WriteString[file," "];
WriteString[file,ToString[udiv+1]];
WriteString[file,"\n"];
tmp1=N[Table[expression,
    {u,umin,umax,(umax-umin)/udiv},
    {v,vmin,vmax,(vmax-vmin)/vdiv}]];
tmp2=Flatten[tmp1,1];
tmp3=Chop[tmp2,10^-4];
tmp4=Map[PaddedForm[#,{4,4}]&,tmp3,{2}];
Scan[
   (WriteString[file,First[#]];
    Scan[WriteString[file,"   ",#]&,Rest[#]];
    WriteString[file,"\n"])&,tmp4];
Close[file]]

Write2COLMESH[{color1_,color2_},file_String,expression_,
{u_,umin_,umax_},{v_,vmin_,vmax_},
PlotPoints->{udiv_,vdiv_},otmp___]:=
Module[{cc1,cc2,cc3,dd1,dd2,dd3,df,em,tmp1,tmp2,tmp3,tmp4},
OpenWrite[file];
WriteString[file,"appearance {\n"];
WriteString[file,"material {\n"];
{cc1,cc2,cc3}=List@@color1;
em=StringForm["emission `` `` ``\n",cc1,cc2,cc3];
WriteString[file,em];
{dd1,dd2,dd3}=List@@color2;
df=StringForm["diffuse `` `` ``}}\n",dd1,dd2,dd3];
WriteString[file,df];
WriteString[file,"MESH\n"];
WriteString[file,ToString[vdiv+1]];
WriteString[file," "];
WriteString[file,ToString[udiv+1]];
WriteString[file,"\n"];
tmp1=N[Table[expression,
    {u,umin,umax,(umax-umin)/udiv},
    {v,vmin,vmax,(vmax-vmin)/vdiv}]];
tmp2=Flatten[tmp1,1];
tmp3=Chop[tmp2,10^-4];
tmp4=Map[PaddedForm[#,{4,4}]&,tmp3,{2}];
Scan[
   (WriteString[file,First[#]];
    Scan[WriteString[file,"   ",#]&,Rest[#]];
    WriteString[file,"\n"])&,tmp4];
Close[file]]

End[]

EndPackage[]
