(* Crippled Development Version 0.16 05/6/2001 The OpenGL features are turned off to allow use of current notebooks with MMA3+ This package must be distributed without charge. Copyright is by the author. Changes must be notified in this header. This package is compatibel with MMA 4.0 upwards. Matthias Weber Mathematical Sciences Research Institute 1000 Centennial Drive Berkeley, CA 94720 USA email: weber@msri.org *) BeginPackage[ "Own`Mesh0`", { "Utilities`FilterOptions`"} ]; StereographicProjection::usage = "StereographicProjection[z] returns the image of z under stereographic \ projection."; Mesh2D::usage = "Mesh2D is a 2-dimensional graphics format for triangle meshes."; Boundary::usage = "Returns a list of vertice indices of edges for certain standard \ \ meshes."; Corners::usage= "Corners[mesh] Returns a list of vertice indices of the corners of the mesh"; SplitEdge::usage = "SplitEdge[mesh,e,pos] splits the boundary edge at position e in the \ \ boundary list of the Mesh2D object mesh into two edges at position pos."; FindVertex::usage= "FindVertex[mesh,v] gives a list of boundary edges and positions of \ points \ of the vertex v in the boundary of mesh."; Mesh3D::usage = "Mesh3D is a 3-dimensional graphics format for triangle meshes."; Vertices::usage = "Vertices[list] is a list of coordinates."; Faces::usage = "Faces[list] is a list of triangles."; Normals::usage = "Normals[list] is a list of normals."; Edges::usage="Edges[list] is a list of edges given by pairs of vertice indices"; EdgeList::usage="EdgeList[mesh] will return a list of edges of a mesh."; RectangularGrid::usage = "RectangularGrid[x,y] creates a Mesh2D object representing a rectangular \ \ grid with vertical and horizontal lines given by the lists x and y."; NRange::usage="NRange"; Subdivide::usage = "Subdivide is an option for Grid generating functions, specifying wether \ \ the mesh is to subdivided into triangles (True) or not (False)."; PolarGrid::usage = "PolarGrid[r,t] creates a Mesh2D object representing a circular grid with \ \ radial and circular lines given by the lists r and t."; FirstQuadrantGrid::usage = "FirstQuadrantGrid[x1,b,x,y] generates a mesh in the first quadrant with \ \ polar coordinates around x1>0. b is a free positive real parameter \ \ influencing the shape of the grid, and x and y are lists of real numbers. \ The \ y-list should be a range from 0 to Pi."; FullFirstQuadrantGrid::usage="not yet"; FullUpperHalfPlaneGrid::usage="Mesh in the upper half plane avoiding x1,x2, splitting at 0, Infinity. Free parameter c."; WedgeGrid::usage="WedgeGrid"; PolarWedgeGrid::usage= "PolarWedgeGrid[x,y] creates a triangle mesh with polar coordinates \ in a wedge with radii x and angles y."; QuarterDiskGrid::usage="QuarterDiskGrid[x,y,r] creates a triangle grid in \ the first quadrant with corners at the x- and y-coordinates within a circle \ of radius r."; RectangularPolarGrid::usage= "RectangularPolarGrid[x,y,b,a] creates a grid in a rectangle with \ corners at 0,1,i b, (1+i)b using polar coordinates centered at a and (1-a)i+i b. \ The x and y lists parameterize the gridlines."; RectangularPolarGrid1::usage= "RectangularPolarGrid1[x,y,b,a] creates a grid in a rectangle with \ corners at 0,1,i b, (1+i)b using polar coordinates centered at 0 and 1+i b. \ The x and y lists parameterize the gridlines."; RectangularSubGrid::usage="RectangularSubGrid[{m,n},xi,yi] creates an edgelist \ for a rectangular mesh of size m by n with the edges being specified by their x- and y-coordinates."; MeshApply::usage= "MeshApply[f,mesh] evaluates the complex function f on a 2-dimensional mesh and returns the image mesh."; RepeatEdges::usage="Given a mesh with vs vertices and an edgelist edge based on \ this mesh, RepeatEdges[edges, vs, n] creates an edgelist based on a mesh which \ replicates the original mesh n times."; ColorPlot::usage="ColorPlot[mesh,f[x,y],{x,y}] adds color information to a mesh, \ based on the function f."; TestMesh::usage="TestMesh[mesh] returns basic information about a mesh."; AddBoundaryColor::"AddBoundaryColor[mesh] adds color at the boundary vertices."; Mesh2DToTexturedGLGraphics::usage="Mesh2DToTexturedGLGraphics converts a Mesh2D \ object in a GLGraphics object with textured polygons."; Mesh2DToGraphics::usage = "Mesh2dToGraphics[mesh] converts a Mesh2D object in a graphics object."; Mesh2DToGLGraphics::usage = "Mesh2dToGraphics[mesh] converts a Mesh2D object in a GLGraphics object."; MeshPlot3D::usage = "MeshPlot3D[fn,m] evaluates the function fn on the mesh vertices of m and \ \ returns a Mesh3D object."; SymbolicMeshPlot3D::usage="SymbolicMeshPlot3D [fn,m] evaluates the function fn \ on the mesh vertices of m and returns a Mesh3D object."; MeshJoin::usage = "MeshJoin[mesh1,mesh2,merge] joins two meshes,thereby mergin the points \ \ in merge into pairs."; MeshJoin2::usage = "MeshJoin2[mesh1,mesh2,merge] joins two meshes,thereby mergin the points \ \ in merge into pairs."; MeshJoin3::usage="testing"; Mesh3DToGraphics3D::usage = "Mesh3DToGraphics3D[m] converts a Mesh3D object into a Graphics3D \ \ object."; Mesh3DToGLGraphics::usage = "Mesh3DToGLGraphics[m] converts a Mesh3D object into a GLGraphics \ \ object."; BoundaryToGLGraphics::usage="none"; BoundaryToGraphics3D::usage="none"; Edges::usage="Edges"; EdgesToGLGraphics::usage="EdgesToGLGraphics"; EdgesToGraphics3D::usage="EdgesToGLGraphics"; MeshToVrml1::usage = "MeshToVrml1[file, mesh] exports a Mesh to Vrml1."; MeshWrite::usage = "MeshWrite[file,name,mesh] writes a Mesh3D object to a file with name \ \ name."; MeshWrite2::usage = "MeshWrite2[file,name,mesh] writes a Mesh3D object to a file with name \ \ name."; MeshRotate::usage = "MeshRotate[mesh,edge] rotates a mesh about an edge"; MeshReflect::usage = "MeshReflect[mesh,plane] reflects a mesh at a plane"; MeshScrew::usage="MeshScrew[mesh, angle, trans] rotates mesh about the z-axes by angle \ and translates it vertically by trans."; DetectSymmetryLine::usage="DetectSymmetryLine[mesh] finds symmetry boundary \ lines of meshes and returns \ the corresponding normal."; VerifyPlanarBoundaryCurve::usage="VerifyPlanarBoundaryCurve[mesh3D,pos,n] returns the average distance of the boundary segment pos to the plane \ \ given by normal n, together with the mean quadratic error."; VerifyStraightBoundaryCurve::usage="VerifyStraightBoundaryCurve[mesh3D,pos,n] \ returns the segment vertices of the boundary segment pos, \ together \ with the mean quadratic error with respect to the direction vector n."; DetectBoundarySymmetry::usage="DetectBoundarySymmetry[mesh] returns a list of \ special \ boundary components."; DetectStraightBoundaryLine::usage="DetectStraightBoundaryLine[mesh2d] finds the \ straight boundary segments of a planar domain."; FindBoundarySymmetry::usage="FindBoundarySymmetry"; PlanarBoundaryCurve::usage="Primitive representing a plane."; StraightBoundaryCurve::usage="Primitive representing a line."; AdjustVertices::usage= "AdjustVertices[mesh,pos,v,n] sets the values of the vertices and normals \ at pos the v and n."; AdjustBoundaryCurve::usage="AdjustBoundaryCurve[mesh,pos,bdry] forces boundary components pos \ to lie on a symmetry line or plane."; GenerateDihedralSymmetry::usage="GenerateDihedralSymmetry[m,b1,b2] returns a \ Mesh3D object by reflecting m repeatedly about the two symmetry planes b1, b2."; ReflectAtBoundaryCurve::usage= "ReflectAtBoundaryCurve[m,b] reflects mesh m at the boundary curve b \ and returns the joined mesh."; FlipOrientation::usage = "FlipOrientation is an option for MeshReflect."; GLShow::usage = "Fake GLShow function."; Begin["`Private`"]; Unprotect[ StereographicProjection, Vertices, Edges, Faces, Boundary, Corners, FindVertex, SplitEdge, SplitEdgeList, Mesh2D, Mesh3D, MeshApply, RectangularGrid, PolarGrid, PolarWedgeGrid, FirstQuadrantGrid, WedgeGrid, FullFirstQuadrantGrid, FullUpperHalfPlaneGrid, QuarterDiskGrid, RectangularPolarGrid, RectangularPolarGrid1, Mesh2DToGraphics, Mesh3DToGraphics3D, MeshPlot3D, RectangularSubGrid, RepeatEdges, ColorPlot, MeshPlot3D, NRange, TestMesh, AddBoundaryColor, SetBoundaryColor, MeshJoin2, MeshJoin3, MeshJoin, MeshRotate, MeshScrew, MeshReflect, GenerateDihedralSymmetry, ReflectAtBoundaryCurve, DetectSymmetryLine, VerifyPlanarBoundaryCurve, VerifyStraightBoundaryCurve, DetectBoundarySymmetry, DetectStraightBoundaryLine, FindBoundarySymmetry, AdjustVertices, AdjustStraightBoundaryCurve, AdjustBoundaryCurve, AdjustPlanarBoundaryCurve, Mesh2DToGraphics, Mesh2DToGLGraphics, Mesh2DToTexturedGLGraphics, Mesh3DToGraphics3D, Mesh3DToGLGraphics, BoundaryToGLGraphics, BoundaryToGraphics3D, EdgesToGLGraphics, EdgesToGraphics3D, EdgeList, MeshWrite, MeshWrite2, MeshToVrml1 ]; eTolerance = 10.^-7; norm[v_]:=v.v; StereographicProjection[z_] := {(2*Re[z])/(1 + Abs[z]^2), (2*Im[z])/(1 + Abs[z]^2), 1 - 2/(1 + Abs[z]^2)}; Vertices[Mesh2D[Vertices[v_], Faces[f_], Boundary[b_]]] := v; Vertices[Mesh2D[Vertices[v_], Colors[c_], Faces[f_], Boundary[b_]]] := v; Faces[Mesh2D[Vertices[v_], Faces[f_], Boundary[b_]]] := f; Faces[Mesh2D[Vertices[v_], Colors[c_], Faces[f_], Boundary[b_]]] := f; Boundary[Mesh2D[Vertices[v_], Faces[f_], Boundary[b_]]] := b; Boundary[Mesh2D[Vertices[v_], Colors[c_], Faces[f_], Boundary[b_]]] := b; Corners[mesh_] := First/@Boundary[mesh]; Vertices[Mesh3D[Vertices[v_], Faces[f_], Boundary[b_]]] := v; Vertices[Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]]] := v; Vertices[Mesh3D[Vertices[v_], Normals[n_], Colors[c_], Faces[f_], Boundary[b_]]] := v; Boundary[Mesh3D[Vertices[v_], Faces[f_], Boundary[b_]]] := b; Boundary[Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]]] := b; Boundary[Mesh3D[Vertices[v_], Normals[n_], Colors[c_], Faces[f_], Boundary[b_]]] := b; (* Mesh2D utilities *) FindVertex[Mesh2D[Vertices[v_], Colors[c_], Faces[f_],Boundary[b_]],p_,eps_:0.0001]:= Position[Map[v[[#]]&,b],_?(norm[#-p] False}; PolarTriangleGrid[x_, y_] := Mesh2D[Vertices[ Flatten[Transpose[Outer[{#1Cos[#2], #1Sin[#2]} &, x, y]], 1]], Faces[Flatten[ Map[{{#, # + 1, # + Length[x]}, {# + 1, # + Length[x] + 1, # + Length[x]}} &, Flatten[Table[ i Length[x] + j, {i, 0, Length[y] - 2}, {j, 1, Length[x] - 1}], 1]], 1]], Boundary[ { Range[Length[x]], Length[x] Range[Length[y]], Range[Length[y]Length[x], (Length[y] - 1)Length[x] + 1, -1], Length[x]Range[Length[y] - 1, 0, -1] + 1 } ] ]; PolarWedgeGrid[x_,y_]:= Mesh2D[ Vertices[ Prepend[ Flatten[ Transpose[ Outer[ {#1Cos[#2],#1Sin[#2]}&, Drop[x,1],y] ],1 ],{0,0} ] ], Faces[ Join[ Table[ {1,2+i(Length[x]-1),2+(i+1)(Length[x]-1)}, {i,0,Length[y]-2} ], Flatten[ Map[ {{#,#+1,#+Length[x]-1},{#+1,#+Length[x],#+Length[x]-1}}&, Flatten[ Table[ i (Length[x]-1)+j,{i,0,Length[y]-2},{j,2,Length[x]-1}],1 ] ],1 ] ] ], Boundary[ {Range[Length[x]], (Length[x]-1) Range[Length[y]]+1,Append[ Range[Length[y](Length[x]-1)+1,(Length[y]-1)(Length[x]-1)+2,-1],1 ] } ] ]; PolarQuadGrid[x_, y_] := Mesh2D[Vertices[ Flatten[Transpose[Outer[{#1Cos[#2], #1Sin[#2]} &, x, y]], 1]], Faces[Flatten[ Map[{{#, # + 1, # + Length[x] + 1, # + Length[x]}} &, Flatten[Table[ i Length[x] + j, {i, 0, Length[y] - 2}, {j, 1, Length[x] - 1}], 1]], 1]], Boundary[{Range[Length[x]], Length[x] Range[Length[y]], Range[Length[y]Length[x], (Length[y] - 1)Length[x] + 1, -1], Length[x]Range[Length[y] - 1, 0, -1] + 1 } ] ]; PolarGrid[x_, y_, opts___] := With[{subdiv = Subdivide /. {opts} /. Options[PolarGrid]}, If[subdiv, PolarTriangleGrid[x, y], PolarQuadGrid[x, y]]]; Options[PolarGrid] = {Subdivide -> False}; reim[z_] := {Re[z], Im[z]}; ComplexForm[fn_][{x_, y_}] := reim[fn[x + I y]]; MeshApply[fn_, Mesh2D[Vertices[v_], Faces[f_], Boundary[b_]]] := Mesh2D[Vertices[Map[ComplexForm[fn], v]], Faces[f], Boundary[b]]; MeshApply[fn_, Mesh2D[Vertices[v_], Colors[c_], Faces[f_], Boundary[b_]]] := Mesh2D[Vertices[Map[ComplexForm[fn], v]], Colors[c], Faces[f], Boundary[b]]; roughinsert[l_, v_] := Join[Select[l, # < v - eTolerance &], {v}, Select[l, # > v + eTolerance &]]; roughposition[l_, v_] := Length[Select[l, # < v - eTolerance &]] + 1; FirstQuadrantGrid[x1_Real, b_Real, xRng_, yRng_, opts___] := Module[{xnewRng, pos}, xnewRng = roughinsert[xRng, Log[1/b]]; pos = Length[xnewRng] + 1 - roughposition[xRng, Log[1/b]]; SplitEdge[ MeshApply[ Chop[x1 Sqrt[1 + b E^#]] &, RectangularGrid[ xnewRng, yRng, opts] ], {3,pos} ] ]; Options[FirstQuadrantGrid] = {Subdivide -> False}; WedgeGrid[x1_Real, k_, xRng_, yRng_, opts___] := MeshApply[ Chop[x1(1 + 2/(-1 + I*E^#))^(2/k)] &, RectangularGrid[ xRng, Pi/2.+Pi/2.*yRng, opts] ]; Options[WedgeGrid] = {Subdivide -> False}; FullFirstQuadrantGrid[x1_Real, xRng_, yRng_,xsplt_:{}, opts___] := Module[{x2,xnewRng, pos}, x2=1/2*Log[(# + x1)/(# - x1)]&/@xsplt; xnewRng = Fold[roughinsert,xRng, x2]; pos = Sort[Length[xnewRng] + Range[Length[xsplt]] - Map[roughposition[xRng,#]&,x2]]; SplitEdgeList[MeshApply[ (x1 Tanh[#]) &, RectangularGrid[ xnewRng, yRng, opts]],{3,pos}] ]; Options[FullFirstQuadrantGrid] = {Subdivide -> False}; FullUpperHalfPlaneGrid[x1_Real, x2_Real, c0_, xRng_, yRng_, opts___] := Module[{v1,v2,xnewRng, pos}, v1=Log[Abs[x1/c0]]; v2=Log[Abs[x2/c0]]; xnewRng = Fold[roughinsert,xRng, {v1,v2}]; m1=RectangularGrid[ xnewRng, yRng, opts]; pos=FindVertex[m1,{v1,Pi}][[1]]; m1=SplitEdge[m1,pos]; pos=FindVertex[m1,{v2,0}][[1]]; m1=SplitEdge[m1,pos]; MeshApply[(c0 E^#+x1)/(c0 E^#/x2+1) &, m1] ]; Options[FullUpperHalfPlaneGrid] = {Subdivide -> False}; QuarterDiskGrid[phi_,xl_,yl_,r_]:=Module[{xs,ys,vs,y,cnt,pat}, xs=Union[r Cos/@phi,xl,{r}]; ys=Union[r Sin/@phi,yl,{r}]; vs=Map[ Function[{y},{#,y}&/@ Append[Select[xs,#^2+y^2 True}; screw[{x_,y_,z_}, angle_, trans_]:={Cos[angle]x+Sin[angle]y,-Sin[angle]x+Cos[angle]y, z+trans}; MeshScrew[ Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]], angle_, trans_] := Mesh3D[ Vertices[Map[screw[#,angle,trans] &, v]], Normals[Map[screw[#,angle,0.0] &, n]], Faces[f], Boundary[b] ]; reflect[p_,{pn_, d_}]:=p-2(p.pn-d)pn; nreflect[p_,pn_]:=p-2(p.pn)pn; MeshReflect[ Mesh2D[Vertices[v_], Faces[f_], Boundary[b_]], PlanarBoundaryCurve[pn_, d_], opts___] := With[{flip = FlipOrientation /. {opts} /. Options[MeshReflect]}, Mesh2D[ Vertices[Map[reflect[#, {pn, d}] &, v]], Faces[If[flip, Map[Reverse, f], f]], Boundary[b] ] ]; MeshReflect[ Mesh2D[Vertices[v_], Colors[c_],Faces[f_], Boundary[b_]], PlanarBoundaryCurve[pn_, d_], opts___] := With[{flip = FlipOrientation /. {opts} /. Options[MeshReflect]}, Mesh2D[ Vertices[Map[reflect[#, {pn, d}] &, v]], Colors[c], Faces[If[flip, Map[Reverse, f], f]], Boundary[b] ] ]; MeshReflect[ Mesh3D[Vertices[v_], Faces[f_], Boundary[b_]], PlanarBoundaryCurve[pn_, d_], opts___] := With[{flip = FlipOrientation /. {opts} /. Options[MeshReflect]}, Mesh3D[ Vertices[Map[reflect[#, {pn, d}] &, v]], Faces[If[flip, Map[Reverse, f], f]], Boundary[b] ] ]; MeshReflect[ Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]], PlanarBoundaryCurve[pn_, d_], opts___] := With[{flip = FlipOrientation /. {opts} /. Options[MeshReflect]}, Mesh3D[ Vertices[Map[reflect[#, {pn, d}] &, v]], Normals[If[flip, Map[nreflect[#, pn] &, n], -Map[nreflect[#, pn] &, n]]], Faces[If[flip, Map[Reverse, f], f]], Boundary[b] ] ]; MeshReflect[ Mesh3D[Vertices[v_], Normals[n_], Colors[c_], Faces[f_], Boundary[b_]], PlanarBoundaryCurve[pn_, d_], opts___] := With[{flip = FlipOrientation /. {opts} /. Options[MeshReflect]}, Mesh3D[ Vertices[Map[reflect[#, {pn, d}] &, v]], Normals[If[flip, Map[nreflect[#, pn] &, n], -Map[nreflect[#, pn] &, n]]], Colors[c], Faces[If[flip, Map[Reverse, f], f]], Boundary[b] ] ]; MeshReflect[ PlanarBoundaryCurve[pn1_, d1_],PlanarBoundaryCurve[pn_, d_], opts___] := With[{flip = FlipOrientation /. {opts} /. Options[MeshReflect]}, PlanarBoundaryCurve[ nreflect[pn1, pn], d1-2d(pn1.pn) ] ]; Options[MeshReflect] = {FlipOrientation -> True}; equalPBC[PlanarBoundaryCurve[n1_,d1_],PlanarBoundaryCurve[n2_,d2_],eps_:0.001]:= norm[Cross[n1,n2]]+(d1-d2)^2eps,False, ver= VerifyPlanarBoundaryCurve[ m,i,ls[[i,1]]]; If[ver[[2]]eps,False, PlanarBoundaryCurve[ls[[i,1]],ls[[i,2]]]]},{i,1,Length[Boundary[m]\ ]}]]; FindBoundarySymmetry[m_,PlanarBoundaryCurve[n_,d_],eps_:0.0001]:= Module[{i,ver}, Flatten[Position[Table[ ver= VerifyPlanarBoundaryCurve[ m,i,n]; ver[[2]]<0.0001 &&Abs[ver[[1]]-d]{1,1}]}]], GLGraphics[Join[dir,{tex},{ Quads[insertValues[Flatten[f], conv2D3D[v]], TextureCoords->{1,1}]}]] ]; Mesh3DToGraphics3D[Mesh3D[Vertices[v_], Faces[f_], Boundary[b_]],dir_:{}] := Graphics3D[Append[dir,Map[Polygon, insertValues[f, v]]]]; Mesh3DToGraphics3D[Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]],dir_:{}] := Graphics3D[Append[dir,Map[Polygon, insertValues[f, v]]]]; Mesh3DToGLGraphics=Mesh3DToGraphics3D; rescale[w_, e_] := w^e/(1 + w^e); BoundaryToGraphics3D[ Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]], bi_, dir_:{}] := Graphics3D[Join[dir, Map[Line[insertValues[b[[#]], v]]&,bi]]]; BoundaryToGLGraphics=BoundaryToGraphics3D; BoundaryToGraphics3D[ Mesh3D[Vertices[v_], Faces[f_], Boundary[b_]], bi_, dir_:{}] := Graphics3D[Join[dir, Map[Line[insertValues[b[[#]], v]]&,bi]]]; EdgesToGraphics3D[Mesh3D[Vertices[v_],Normals[n_],Faces[f_],Boundary[b_]], Edges[ed_] ,dir_:{}]:= Graphics3D[ Join[dir,Map[ Line[insertValues[#,v]]&,ed]]]; EdgesToGraphics3D[Mesh3D[Vertices[v_],Normals[n_],Colors[c_], Faces[f_],Boundary[b_]], Edges[ed_] ,dir_:{}]:= Graphics3D[ Join[dir,Map[ Line[insertValues[#,v]]&,ed]]]; EdgesToGraphics3D[Mesh3D[Vertices[v_],Faces[f_],Boundary[b_]], Edges[ed_] ,dir_:{}]:= Graphics3D[ Join[dir,Map[ Line[insertValues[#,v]]&,ed]]]; EdgesToGLGraphics=EdgesToGraphics3D; (* Export functions *) ClosedLine[l_] := Append[l, First[l]]; getEdges[l_] := Thread[{l, RotateLeft[l]}]; SortedEdges[l_] := Map[Sort, getEdges[l]]; edgelist[f_] := Union[Flatten[Map[SortedEdges, f], 1]]; EdgeList[Mesh3D[Vertices[v_], Normals[n_], Colors[c_], Faces[f_], Boundary[b_]]]:= Edges[edgelist[f]]; EdgeList[Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]]]:= Edges[edgelist[f]]; EdgeList[Mesh3D[Vertices[v_], Faces[f_], Boundary[b_]]]:= Edges[edgelist[f]]; EdgeList[Mesh2D[Vertices[v_], Faces[f_], Boundary[b_]]]:= Edges[edgelist[f]]; CompactForm[x_, digits_] := SetPrecision[Chop[x], digits] // CForm // ToString; PointToString[pts_List, prec_Integer, tchar_:" "] := StringJoin[{" ", CompactForm[#, prec]} & /@ pts, tchar]; MeshWrite[file_, name_, Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]]] := Module[ {strm = OpenWrite[file, FormatType -> FortranForm], i}, WriteString[strm, "createTopSegment verts ", "\n"]; WriteString[strm, "createTopSegment normals ", "\n"]; WriteString[strm, "createTopSegment faces ", "\n"]; WriteString[strm, "\n"]; WriteString[strm, "setSegment verts\n"]; WriteString[strm, "c 1 0 0\n"]; For[i = 1, i <= Length[v], i++, WriteString[strm, "v ", i - 1, " ", PointToString[v[[i]], 4], "\n"] ]; WriteString[strm, "\n"]; WriteString[strm, "setSegment faces\n"]; WriteString[strm, "setVertSegment verts\n"]; WriteString[strm, "c 0 1 0\n"]; For[i = 1, i <= Length[f], i++, WriteString[strm, "t ", i - 1, " ", f[[i, 1]] - 1, " ", f[[i, 2]] - \ 1, " ", f[[i, 3]] - 1, "\n"]]; Close[strm]]; MeshWrite2[file_, name_, Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]]] := Module[ {strm = OpenWrite[file, FormatType -> FortranForm], i, edges = edgelist[f]}, WriteString[strm, "createTopSegment verts ", "\n"]; WriteString[strm, "createTopSegment edges ", "\n"]; WriteString[strm, "createTopSegment normals ", "\n"]; WriteString[strm, "createTopSegment faces ", "\n"]; WriteString[strm, "\n"]; WriteString[strm, "setSegment verts\n"]; WriteString[strm, "c 1 0 0\n"]; For[i = 1, i <= Length[v], i++, WriteString[strm, "v ", i - 1, " ", PointToString[v[[i]], 4], "\n"] ]; WriteString[strm, "setSegment edges\n"]; WriteString[strm, "setVertSegment verts\n"]; WriteString[strm, "c 0 0 1\n"]; For[i = 1, i <= Length[edges], i++, WriteString[strm, "e ", i - 1, " ", edges[[i, 1]] - 1, " ", edges[[i, 2]] - 1, "\n"] ]; WriteString[strm, "\n"]; WriteString[strm, "setSegment faces\n"]; WriteString[strm, "setVertSegment verts\n"]; WriteString[strm, "c 0 1 0\n"]; For[i = 1, i <= Length[f], i++, WriteString[strm, "t ", i - 1, " ", f[[i, 1]] - 1, " ", f[[i, 2]] - \ 1, " ", f[[i, 3]] - 1, "\n"]]; Close[strm]]; MeshToVrml1[file_, Mesh3D[Vertices[v_], Normals[n_], Faces[f_], Boundary[b_]]] := Module[ {strm = OpenWrite[file, FormatType -> FortranForm], lf, i}, WriteString[strm, "#VRML V1.0 ascii", "\n"]; WriteString[strm, "\n"]; WriteString[strm, "Separator {", "\n"]; WriteString[strm, "Coordinate3 {point[", "\n"]; WriteString[strm, "\n"]; For[i = 1, i <= Length[v] - 1, i++, WriteString[strm, PointToString[v[[i]], 4], ",\n"] ]; WriteString[strm, PointToString[Last[v], 4], "\n"]; WriteString[strm, "]}\n"]; WriteString[strm, "\n"]; WriteString[strm, "Normal {vector[\n"]; For[i = 1, i <= Length[n] - 1, i++, WriteString[strm, PointToString[n[[i]], 4], ",\n"] ]; WriteString[strm, PointToString[Last[n], 4], "\n"]; WriteString[strm, "]}\n"]; WriteString[strm, "\n"]; WriteString[strm, "IndexedFaceSet {\n"]; WriteString[strm, "coordIndex[\n"]; lf = Length[f]; If[Length[f[[1]]] == 3, For[i = 1, i <= lf - 1, i++, WriteString[strm, f[[i, 1]] - 1, ", ", f[[i, 2]] - 1, ", ", f[[i, 3]] - 1, ", -1,\n"] ]; WriteString[strm, f[[lf, 1]] - 1, ", ", f[[lf, 2]] - 1, ", ", f[[lf, 3]] - 1, ", -1\n"],(*else*) For[i = 1, i <= lf - 1, i++, WriteString[strm, f[[i, 1]] - 1, ", ", f[[i, 2]] - 1, ", ", f[[i, 3]] - 1, ", ", f[[i, 4]] - 1, ", -1,\n"]]; WriteString[strm, f[[lf, 1]] - 1, ", ", f[[lf, 2]] - 1, ", ", f[[lf, 3]] - 1, ", ", f[[lf, 4]] - 1, ", -1\n"] ]; WriteString[strm, "]}}\n"]; Close[strm] ]; GLShow=Show; Protect[ StereographicProjection, Vertices, Edges, Faces, Boundary, Corners, FindVertex, SplitEdge, SplitEdgeList, Mesh2D, Mesh3D, MeshApply, RectangularGrid, PolarGrid, PolarWedgeGrid, FirstQuadrantGrid, WedgeGrid, FullFirstQuadrantGrid, FullUpperHalfPlaneGrid, QuarterDiskGrid, RectangularPolarGrid, RectangularPolarGrid1, Mesh2DToGraphics, Mesh3DToGraphics3D, MeshPlot3D, RectangularSubGrid, RepeatEdges, ColorPlot, MeshPlot3D, NRange, TestMesh, AddBoundaryColor, SetBoundaryColor, MeshJoin2, MeshJoin3, MeshJoin, MeshRotate, MeshScrew, MeshReflect, GenerateDihedralSymmetry, ReflectAtBoundaryCurve, DetectSymmetryLine, VerifyPlanarBoundaryCurve, VerifyStraightBoundaryCurve, DetectBoundarySymmetry, DetectStraightBoundaryLine, FindBoundarySymmetry, AdjustVertices, AdjustStraightBoundaryCurve, AdjustBoundaryCurve, AdjustPlanarBoundaryCurve, Mesh2DToGraphics, Mesh2DToGLGraphics, Mesh2DToTexturedGLGraphics, Mesh3DToGraphics3D, Mesh3DToGLGraphics, BoundaryToGLGraphics, BoundaryToGraphics3D, EdgesToGLGraphics, EdgesToGraphics3D, EdgeList, MeshWrite, MeshWrite2, MeshToVrml1 ]; End[]; EndPackage[];