public static MeshBuilder Planar(Polyline curve) { const double epsilon = 0.001; curve.ReducePoints(epsilon); var points = curve.Points.ToList(); if (points.Last().DistTo(points.First()) < epsilon) { points.RemoveAt(points.Count - 1); } var triIndices = CuttingEarsTriangulator.Triangulate(points.Select(p => new System.Windows.Point(p.X, p.Y)).ToList()); if (triIndices == null) { return(null); } MeshBuilder mb = new MeshBuilder(); for (int i = 0; i < triIndices.Count; i += 3) { var a = new Point3D(points[triIndices[i]].X, points[triIndices[i]].Y, 0); var b = new Point3D(points[triIndices[i + 1]].X, points[triIndices[i + 1]].Y, 0); var c = new Point3D(points[triIndices[i + 2]].X, points[triIndices[i + 2]].Y, 0); mb.AddTriangle(a, b, c); } return(mb); }
public static MeshBuilder Planar(PointString curve) { const double epsilon = 0.001; curve.ReducePoints(epsilon); var points = curve.Points.ToList(); if (points.Last().Dist(points.First()) < epsilon) { points.RemoveAt(points.Count - 1); } var triIndices = CuttingEarsTriangulator.Triangulate(points); if (triIndices == null) { return(null); } var mb = new MeshBuilder(); for (var i = 0; i < triIndices.Count; i += 3) { var a = new Vector(points[triIndices[i]].X, points[triIndices[i]].Y, 0); var b = new Vector(points[triIndices[i + 1]].X, points[triIndices[i + 1]].Y, 0); var c = new Vector(points[triIndices[i + 2]].X, points[triIndices[i + 2]].Y, 0); mb.AddTriangle(a, b, c); } return(mb); }
public void Triangulate_Discussion440914_ShouldBeValid() { var polygon = new[] { new Point(0, 0), new Point(0, 1.894), new Point(-2.536, 1.42), new Point(-5.072, 1.42), new Point(-5.072, 2.84), new Point(-10.144, 2.84), new Point(-10.144, 0) }; var result = CuttingEarsTriangulator.Triangulate(polygon); Assert.IsNotNull(result); Assert.AreEqual(5 * 3, result.Count); Assert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 0, 2, 3, 5, 6, 6, 2, 3 }, result); }
public void Triangulate_Discussion440914b_ShouldBeValid() { var polygon = new[] { new Point(0, 0), new Point(0, 2.97), new Point(-2.389999999999997, 2.97), new Point(-2.389999999999997, 1.4849999999999999), new Point(-4.7799999999999976, 1.4849999999999999), new Point(-4.7799999999999976, 2.97), new Point(-9.5599999999999987, 2.97), new Point(-9.5599999999999987, 0) }; var result = CuttingEarsTriangulator.Triangulate(polygon); Assert.IsNotNull(result); Assert.AreEqual(6 * 3, result.Count); Assert.AreEqual(new[] { 0, 1, 2, 4, 5, 6, 0, 2, 3, 4, 6, 7, 7, 0, 3, 3, 4, 7 }, result); }
private static void CreatePolygon(VectorTileFeature feature, Pallete pallete, Model3DGroup model3DGroup, Vector2 <int> shiftCoords) { PointCollection points = new PointCollection(); List <List <Vector2 <int> > > list = feature.Geometry <int>(); foreach (List <Vector2 <int> > item in list) { points.Clear(); foreach (Vector2 <int> point in item) { points.Add(new Point(point.X + shiftCoords.X, point.Y + shiftCoords.Y)); } points.RemoveAt(points.Count - 1); var model = new GeometryModel3D(); var meshbuilder = new MeshBuilder(true, true); var result = CuttingEarsTriangulator.Triangulate(points); List <int> tri = new List <int>(); for (int i = 0; i < result.Count; i++) { tri.Add(result[i]); if (tri.Count == 3) { //Console.WriteLine("Triangle " + (i / 3).ToString() + " : " + tri[0].ToString() + ", " + tri[1].ToString() + ", " + tri[2].ToString()); meshbuilder.AddTriangle(new Point3D(points[tri[0]].X, points[tri[0]].Y, 1), new Point3D(points[tri[1]].X, points[tri[1]].Y, 1), new Point3D(points[tri[2]].X, points[tri[2]].Y, 1)); tri.Clear(); } } model.Geometry = meshbuilder.ToMesh(); model.Material = MaterialHelper.CreateMaterial(pallete.MainFillColor.ToMediaColor()); model3DGroup.Children.Add(model); } }
//calculates a mold box to contour around the input mesh private MeshGeometry3D GetMold(List <Point> points, double zMax, double zMin) { if (points != null && points.Count > 0) { double z_height = zMax + 4; //highest point for the mold double offsetZ = zMin - 3; //the final contour MeshBuilder mb = new MeshBuilder() { CreateNormals = false, CreateTextureCoordinates = false }; points = SortPoints(points); //create contour face var result = CuttingEarsTriangulator.Triangulate(points); //create lower surface for (int t = 2; t < result.Count; t += 3) { Point3D p0 = new Point3D(points[result[t]].X, points[result[t]].Y, offsetZ); Point3D p1 = new Point3D(points[result[t - 1]].X, points[result[t - 1]].Y, offsetZ); Point3D p2 = new Point3D(points[result[t - 2]].X, points[result[t - 2]].Y, offsetZ); mb.AddTriangle(p0, p1, p2); } //create higher surface //point order is reversed to reverse the normals created double upper_offsetZ = z_height; for (int t = 2; t < result.Count; t += 3) { Point3D p0 = new Point3D(points[result[t - 2]].X, points[result[t - 2]].Y, upper_offsetZ); Point3D p1 = new Point3D(points[result[t - 1]].X, points[result[t - 1]].Y, upper_offsetZ); Point3D p2 = new Point3D(points[result[t]].X, points[result[t]].Y, upper_offsetZ); mb.AddTriangle(p0, p1, p2); } points.Add(points[0]); //create walls for (int t = 1; t < points.Count; t++) { Point3D p0 = new Point3D(points[t - 1].X, points[t - 1].Y, offsetZ); Point3D p1 = new Point3D(points[t].X, points[t].Y, offsetZ); Point3D p2 = new Point3D(points[t - 1].X, points[t - 1].Y, upper_offsetZ); mb.AddTriangle(p0, p1, p2); p0 = new Point3D(points[t].X, points[t].Y, upper_offsetZ); p1 = new Point3D(points[t - 1].X, points[t - 1].Y, upper_offsetZ); p2 = new Point3D(points[t].X, points[t].Y, offsetZ);; mb.AddTriangle(p0, p1, p2); } return(mb.ToMesh()); } return(null); }
public BolusMold(MeshGeometry3D Mesh, double VoxelSize, List <double> z_slices, bool alignmentLip = false) { if (Mesh != null) { Meshes = new List <MeshGeometry3D>(); var points = GetContour(Mesh.Positions, VoxelSize); //get a list of points that surround the mesh points = SortPoints(points); var result = CuttingEarsTriangulator.Triangulate(points); //a list of triangles, needs to be meshed points.Add(points[0]); // to allow loops to fully enclose the mesh //variables for the mesh heights double z_low = Mesh.Positions.Min(p => p.Z) - 2.4; //lowest vertex's z, minus an offset to make it lower double z_high = Mesh.Bounds.SizeZ / 2 + 3; //the wall height stops 3 mm below the slice's top //setup the lists for slicing List <Slice> slices = new List <Slice>(); List <double> low_slice = new List <double>(); List <double> high_slice = new List <double>(); low_slice.Add(z_low); for (int i = 0; i < z_slices.Count; i++) { low_slice.Add(z_slices[i]); high_slice.Add(z_slices[i]); } high_slice.Add(z_high); for (int i = 0; i < low_slice.Count; i++) { slices.Add(new Slice(low_slice[i], high_slice[i])); } //create the simple contoured meshes for each slice foreach (Slice slice in slices) { //building the mesh MeshBuilder mb = new MeshBuilder() { CreateNormals = false, CreateTextureCoordinates = false }; //mesh the bottom using result, from the cutting ears triangulator for (int t = 2; t < result.Count; t += 3) { Point3D p0 = new Point3D(points[result[t]].X, points[result[t]].Y, slice.Floor); Point3D p1 = new Point3D(points[result[t - 1]].X, points[result[t - 1]].Y, slice.Floor); Point3D p2 = new Point3D(points[result[t - 2]].X, points[result[t - 2]].Y, slice.Floor); mb.AddTriangle(p0, p1, p2); } //creating the triangles for the walls for (int i = 1; i < points.Count; i++) { Point3D p0 = new Point3D(points[i - 1].X, points[i - 1].Y, slice.Floor); Point3D p1 = new Point3D(points[i].X, points[i].Y, slice.Floor); Point3D p2 = new Point3D(points[i - 1].X, points[i - 1].Y, slice.Ceiling); mb.AddTriangle(p0, p1, p2); p0 = new Point3D(points[i].X, points[i].Y, slice.Floor); p1 = new Point3D(points[i].X, points[i].Y, slice.Ceiling); p2 = new Point3D(points[i - 1].X, points[i - 1].Y, slice.Ceiling); mb.AddTriangle(p0, p1, p2); } //create higher surface //point order is reversed to reverse the normals created for (int t = 2; t < result.Count; t += 3) { Point3D p0 = new Point3D(points[result[t - 2]].X, points[result[t - 2]].Y, slice.Ceiling); Point3D p1 = new Point3D(points[result[t - 1]].X, points[result[t - 1]].Y, slice.Ceiling); Point3D p2 = new Point3D(points[result[t]].X, points[result[t]].Y, slice.Ceiling); mb.AddTriangle(p0, p1, p2); } this.Meshes.Add(mb.ToMesh()); } } }