private static void SaveMesh(Mesh mesh, string filename) { System.IO.File.Delete(filename); using (StreamWriter sw = File.AppendText(filename)) { STL.AppendMeshToSTL(mesh, sw); Vector3D aStart = H3Ruled.Transform(new Vector3D(0, 0, -1)); Vector3D aEnd = H3Ruled.Transform(new Vector3D(0, 0, 1)); Mesh m3 = new Mesh(); AddEdge(m3, aStart, aEnd); //STL.AppendMeshToSTL( m3, sw ); } }
/// <summary> /// Our approach will be: /// (1) Generate a portion of one cell. /// (2) Reflect all facets in the central facet, to get things filled-in inside the central facet. (Trim small edges here?) /// (3) Copy this region around the plane, and go back to step (2) if density is not high enough. /// (4) Map to Ball, trimming edges that become too small. /// NOTE: All verts are on the boundary, so we can reflect around // in circles on the plane at infinity, rather than spheres. /// </summary> public static void GenerateExotic(EHoneycomb honeycomb, H3.Settings settings) { settings.AngularThickness = 0.17; Tiling tiling; Tile baseTile; GetAssociatedTiling(honeycomb, out tiling, out baseTile); List <H3.Cell.Edge> edges = new List <H3.Cell.Edge>(); foreach (Segment seg in baseTile.Boundary.Segments) { edges.Add(new H3.Cell.Edge(seg.P1, seg.P2)); } settings.Position = Polytope.Projection.FaceCentered; double scale = 1; Vector3D offset = new Vector3D(); if (settings.Position == Polytope.Projection.FaceCentered) { scale = FaceCenteredScale(baseTile.VertexCircle); offset = new Vector3D(); } else if (settings.Position == Polytope.Projection.EdgeCentered) { scale = EdgeCenteredScale(baseTile); offset = baseTile.Boundary.Segments[0].Midpoint; } int iterations = m_params.Iterations; for (int i = 0; i < iterations; i++) { edges = DoOneStep(edges, tiling, baseTile.VertexCircle); } edges = CopyAndProject(edges, tiling, scale, offset); if (m_params.RemoveDangling) { Dictionary <H3.Cell.Edge, int> edgeDict = edges.ToDictionary(e => e, e => 1); H3.RemoveDanglingEdgesRecursive(edgeDict); edges = edgeDict.Keys.ToList(); } string outputFileName = H3.m_baseDir + Honeycomb.String(honeycomb, false); System.IO.File.Delete(outputFileName); if (m_params.Output == H3.Output.STL) { outputFileName += ".stl"; // Now mesh the edges. Shapeways mesh = new Shapeways(); foreach (H3.Cell.Edge edge in edges) { // Append to the file vs. writing out all at once because I was running out of memory otherwise. mesh = new Shapeways(); int div; H3Models.Ball.LODThin(edge.Start, edge.End, out div); mesh.Div = div; H3.Util.AddToMeshInternal(mesh, edge.Start, edge.End); mesh.Mesh.Scale(settings.Scale); STL.AppendMeshToSTL(mesh.Mesh, outputFileName); } } else { outputFileName += ".pov"; PovRay.WriteH3Edges(new PovRay.Parameters() { AngularThickness = settings.AngularThickness, Halfspace = settings.Halfspace, ThinEdges = settings.ThinEdges, }, edges.ToArray(), outputFileName); } }
public static void HoneycombHyperidealLegs(HoneycombDef def) { // This will be used to avoid duplicates. // The key is the cell center. Dictionary <Vector3D, H3.Cell> complete = new Dictionary <Vector3D, H3.Cell>(); m_thresh = 0.05; //m_thresh = 0.07; HoneycombHyperidealLegs(def, 3, complete); m_thresh = 0.01; //m_thresh = 0.02; HoneycombHyperidealLegs(def, 2, complete); m_thresh = 0.004; //m_thresh = 0.007; HoneycombHyperidealLegs(def, 1, complete); string filename = "cell.stl"; System.IO.File.Delete(filename); using (StreamWriter sw = File.AppendText(filename)) { HashSet <H3.Cell.Edge> edgesToMesh = new HashSet <H3.Cell.Edge>(new H3.Cell.EdgeEqualityComparer()); foreach (H3.Cell cell in complete.Values) { int depth = cell.Depths.Sum(); Mesh m = new Mesh(); Sphere normal = cell.Facets[0].Sphere; foreach (Mesh.Triangle tri in cell.Mesh.Triangles) { Mesh.Triangle[] thickened = Thicken(tri, normal); m.Triangles.AddRange(thickened.Select(t => Transform(t))); } List <object> boundary = new List <object>(); int skip = 2; int stride = (int)Math.Sqrt(cell.Mesh.Triangles.Count) / 2 + 1; int num = stride; boundary.Add(cell.AuxPoints.Skip(skip).Take(num)); skip += num; boundary.Add(cell.AuxPoints.Skip(skip).Take(num)); skip += num; num = 2 * m_div + 1; boundary.Add(cell.AuxPoints.Skip(skip).Take(num)); skip += num; boundary.Add(cell.AuxPoints.Skip(skip).Take(num)); skip += num; foreach (object e in boundary) { var enumerable = (IEnumerable <Vector3D>)e; //if( depth % 2 == 0 ) // enumerable = enumerable.Reverse(); Mesh m2 = ThickenBoundary(enumerable.ToArray(), normal); if (depth % 2 == 0) { ReverseTris(m2); } m.Triangles.AddRange(m2.Triangles.Select(t => Transform(t))); } STL.AppendMeshToSTL(m, sw); edgesToMesh.Add(new H3.Cell.Edge(cell.AuxPoints[0], cell.AuxPoints[1])); } /*foreach( H3.Cell.Edge e in edgesToMesh ) * { * Mesh m3 = new Mesh(); * AddEdge( m3, Transform( e.Start ), Transform( e.End ) ); * STL.AppendMeshToSTL( m3, sw ); * }*/ } }
public static void S3BiHelicoid() { double cutoff = 8.0; int div = 500; Matrix4D mat = Matrix4D.MatrixToRotateinCoordinatePlane(1 * Math.PI / 4, 0, 3); Mesh m1 = S3Helicoid(div, mat, reciprocal: false); //m1.Triangles = m1.Triangles.Where( t => t.a.Abs() < cutoff && t.b.Abs() < cutoff && t.c.Abs() < cutoff ).ToList(); Mesh m2 = S3Helicoid(div, mat, reciprocal: true); //m2.Triangles = m2.Triangles.Where( t => t.a.Abs() < cutoff && t.b.Abs() < cutoff && t.c.Abs() < cutoff ).ToList(); Mesh m3 = new Mesh(); System.Action <bool> addCore = recip => { List <Vector3D> circlePoints = new List <Vector3D>(); double aOffset = 2 * Math.PI / div; for (int i = 0; i <= div; i++) { double x = Math.Sin(aOffset * i); double y = Math.Cos(aOffset * i); circlePoints.Add(recip ? new Vector3D(x, y) : new Vector3D(0, 0, x, y)); } // partial transform to R3 here. circlePoints = circlePoints.Select(p => { p = mat.RotateVector(p); p = Sterographic.S3toR3(p); return(p); }).ToList(); List <Vector3D> ePoints = new List <Vector3D>(); List <double> eRadii = new List <double>(); foreach (Vector3D pNE in circlePoints) { Sphere sphere = SphereFuncBall(Geometry.Spherical, pNE, false); ePoints.Add(sphere.Center); eRadii.Add(sphere.Radius); } Shapeways shapeways = new Shapeways(); shapeways.AddClosedCurve(ePoints.ToArray(), eRadii.ToArray()); m3.Append(shapeways.Mesh); }; addCore(false); addCore(true); for (int i = 0; i < m3.Triangles.Count; i++) { Mesh.Triangle t = m3.Triangles[i]; m3.Triangles[i] = Transform(t, SphericalModels.StereoToEquidistant); } string filename = "helicoid.stl"; File.Delete(filename); using (StreamWriter sw = File.AppendText(filename)) { STL.AppendMeshToSTL(m1, sw); STL.AppendMeshToSTL(m2, sw); STL.AppendMeshToSTL(m3, sw); } //HelicoidHelper( thinMesh, boundaryPoints ); }
public static void HoneycombFiniteVertexFig(HoneycombDef def) { // This will be used to remove duplicates. // Our faces will be doubled-up. We'll make a hash from one of the interior meshPoints. Dictionary <Vector3D, H3.Cell> complete = new Dictionary <Vector3D, H3.Cell>(); m_thresh = 0.07; HoneycombFiniteVertexFig(def, 3, complete); m_thresh = 0.02; HoneycombFiniteVertexFig(def, 2, complete); m_thresh = 0.007; HoneycombFiniteVertexFig(def, 1, complete); //m_thresh = 0.005; //CreateHoneycombSTL( def, 0, complete ); string filename = "cell.stl"; System.IO.File.Delete(filename); using (StreamWriter sw = File.AppendText(filename)) { foreach (H3.Cell cell in complete.Values) { //STL.AppendMeshToSTL( cell.Mesh, sw ); bool reverse = cell.Depths.Sum() % 2 == 1; Mesh m = new Mesh(); Sphere normal = cell.Facets[0].Sphere; foreach (Mesh.Triangle tri in cell.Mesh.Triangles) { Mesh.Triangle[] thickened = ThickenSimple(tri, normal); m.Triangles.AddRange(thickened); } if (reverse) { ReverseTris(m); } STL.AppendMeshToSTL(m, sw); System.Func <Vector3D, System.Tuple <Vector3D, Vector3D> > thickenFn = v => ThickenSimple(v, normal); int stride = (int)Math.Sqrt(cell.Mesh.Triangles.Count) + 1; Vector3D[] e1 = cell.AuxPoints.Skip(1 + 0 * stride).Take(stride).ToArray(); Vector3D[] e2 = cell.AuxPoints.Skip(1 + 1 * stride).Take(stride).ToArray(); Vector3D[] e3 = cell.AuxPoints.Skip(1 + 2 * stride).Take(stride).ToArray(); Mesh m1 = ThickenBoundary(e1, thickenFn), m2 = ThickenBoundary(e2, thickenFn), m3 = ThickenBoundary(e3, thickenFn); if (reverse) { ReverseTris(m1); ReverseTris(m2); ReverseTris(m3); } STL.AppendMeshToSTL(m1, sw); STL.AppendMeshToSTL(m2, sw); STL.AppendMeshToSTL(m3, sw); } } }