public static ModelVisual3D Representation3D(this XYPolygon Poly, IXYPoint refpoint, double height) { MeshBuilder mb = new MeshBuilder(); var pts = new Point3DCollection(); foreach (var p in Poly.Points) { pts.Add(new Point3D(refpoint.X - p.X, refpoint.Y - p.Y, height)); } // POLYGONS (flat and convex) var poly3D = new Polygon3D(pts); // Transform the polygon to 2D var poly2D = poly3D.Flatten(); // Triangulate var tri = poly2D.Triangulate(); if (tri != null) { // Add the triangle indices with the 3D points mb.Append(pts, tri); } var m = MaterialHelper.CreateMaterial(Colors.DimGray, 0.5); var mv3D = new ModelVisual3D(); mv3D.Content = new GeometryModel3D(mb.ToMesh(), m); return(mv3D); }
public void Flatten_PlanarPolygon2_ReturnsCorrectResult() { var p = new Polygon3D(); p.Points.Add(new Point3D(0, 0, 4)); p.Points.Add(new Point3D(1, 0, 4)); p.Points.Add(new Point3D(1, 1, 4.01)); p.Points.Add(new Point3D(0, 1, 4.01)); var p2 = p.Flatten(); Assert.AreEqual(p2.Points.Count, 4); var tri = p2.Triangulate(); Assert.AreEqual(6, tri.Count); }
/// <summary> /// The add face. /// </summary> /// <param name="values"> /// The values. /// </param> private void AddFace(string values) { // A polygonal face. The numbers are indexes into the arrays of vertex positions, // texture coordinates, and normals respectively. A number may be omitted if, // for example, texture coordinates are not being defined in the model. // There is no maximum number of vertices that a single polygon may contain. // The .obj file specification says that each face must be flat and convex. var fields = values.SplitOnWhitespace(); var points = new Point3DCollection(); var textureCoordinates = new PointCollection(); var normals = new Vector3DCollection(); foreach (var field in fields) { if (string.IsNullOrEmpty(field)) { continue; } var ff = field.Split('/'); int vi = int.Parse(ff[0]); int vti = ff.Length > 1 && ff[1].Length > 0 ? int.Parse(ff[1]) : int.MaxValue; int vni = ff.Length > 2 && ff[2].Length > 0 ? int.Parse(ff[2]) : int.MaxValue; if (vi < 0) { vi = this.Points.Count + vi; } if (vti < 0) { vti = this.TexCoords.Count + vti; } if (vni < 0) { vni = this.Normals.Count + vni; } if (vi - 1 < this.Points.Count) { points.Add(this.Points[vi - 1]); } if (vti < int.MaxValue && vti - 1 < this.TexCoords.Count) { textureCoordinates.Add(this.TexCoords[vti - 1]); } if (vni < int.MaxValue && vni - 1 < this.Normals.Count) { normals.Add(this.Normals[vni - 1]); } } if (textureCoordinates.Count == 0) { textureCoordinates = null; } if (normals.Count == 0) { normals = null; } if (normals == null) { // turn off normals in the mesh builder //this.CurrentGroup.MeshBuilder.Normals = null; } if (textureCoordinates == null) { // turn off texture coordinates in the mesh builder //this.CurrentGroup.MeshBuilder.TextureCoordinates = null; } // TRIANGLE if (points.Count == 3) { this.CurrentGroup.MeshBuilder.AddTriangles(points, normals, textureCoordinates); return; } // QUAD if (points.Count == 4) { this.CurrentGroup.MeshBuilder.AddQuads(points, normals, textureCoordinates); return; } // POLYGONS (flat and convex) { var poly3D = new Polygon3D(points); // Transform the polygon to 2D var poly2D = poly3D.Flatten(); // Triangulate var triangleIndices = poly2D.Triangulate(); if (triangleIndices != null) { // Add the triangle indices with the 3D points this.CurrentGroup.MeshBuilder.Append(points, triangleIndices, normals, textureCoordinates); } } }