internal static Csg.Solid ToCsg(this Mesh mesh) { var vertices = new List <Csg.Vertex>(); foreach (var v in mesh.Vertices) { var vv = new Csg.Vertex(v.Position.ToCsgVector3(), v.UV.ToCsgVector2()); vertices.Add(vv); } var polygons = new List <Csg.Polygon>(); foreach (var t in mesh.Triangles) { var a = vertices[t.Vertices[0].Index]; var b = vertices[t.Vertices[1].Index]; var c = vertices[t.Vertices[2].Index]; var polygon = new Csg.Polygon(new List <Csg.Vertex>() { a, b, c }); polygons.Add(polygon); } return(Csg.Solid.FromPolygons(polygons)); }
private static Vertex ToElementsVertex(this Csg.Vertex v) { return(new Vertex(v.Pos.ToElementsVector())); }
internal static Csg.Solid ToCsg(this Solid solid) { var polygons = new List <Csg.Polygon>(); foreach (var f in solid.Faces.Values) { var tess = new Tess(); tess.NoEmptyPolygons = true; tess.AddContour(f.Outer.ToContourVertexArray(f)); if (f.Inner != null) { foreach (var loop in f.Inner) { tess.AddContour(loop.ToContourVertexArray(f)); } } tess.Tessellate(WindingRule.Positive, LibTessDotNet.Double.ElementType.Polygons, 3); Vector3 e1 = new Vector3(); Vector3 e2 = new Vector3(); var vertices = new List <Csg.Vertex>(); for (var i = 0; i < tess.ElementCount; i++) { var a = tess.Vertices[tess.Elements[i * 3]].ToCsgVector3(); var b = tess.Vertices[tess.Elements[i * 3 + 1]].ToCsgVector3(); var c = tess.Vertices[tess.Elements[i * 3 + 2]].ToCsgVector3(); Csg.Vertex av = null; Csg.Vertex bv = null; Csg.Vertex cv = null; // Merge vertices. foreach (var v in vertices) { if (v.Pos.IsAlmostEqualTo(a)) { av = v; } if (v.Pos.IsAlmostEqualTo(b)) { bv = v; } if (v.Pos.IsAlmostEqualTo(c)) { cv = v; } } if (i == 0) { var n = f.Plane().Normal; e1 = n.Cross(n.IsParallelTo(Vector3.XAxis) ? Vector3.YAxis : Vector3.XAxis).Unitized(); e2 = n.Cross(e1).Unitized(); } if (av == null) { var avv = new Vector3(a.X, a.Y, a.Z); av = new Csg.Vertex(a, new Csg.Vector2D(e1.Dot(avv), e2.Dot(avv))); vertices.Add(av); } if (bv == null) { var bvv = new Vector3(b.X, b.Y, b.Z); bv = new Csg.Vertex(b, new Csg.Vector2D(e1.Dot(bvv), e2.Dot(bvv))); vertices.Add(bv); } if (cv == null) { var cvv = new Vector3(c.X, c.Y, c.Z); cv = new Csg.Vertex(c, new Csg.Vector2D(e1.Dot(cvv), e2.Dot(cvv))); vertices.Add(cv); } var p = new Csg.Polygon(new List <Csg.Vertex>() { av, bv, cv }); polygons.Add(p); } } return(Csg.Solid.FromPolygons(polygons)); }
internal static Csg.Solid ToCsg(this Solid solid) { var polygons = new List <Csg.Polygon>(); foreach (var f in solid.Faces.Values) { var tess = new Tess(); tess.NoEmptyPolygons = true; tess.AddContour(f.Outer.ToContourVertexArray(f)); if (f.Inner != null) { foreach (var loop in f.Inner) { tess.AddContour(loop.ToContourVertexArray(f)); } } tess.Tessellate(WindingRule.Positive, LibTessDotNet.Double.ElementType.Polygons, 3); var vertices = new List <Csg.Vertex>(); for (var i = 0; i < tess.ElementCount; i++) { var a = tess.Vertices[tess.Elements[i * 3]].ToCsgVector3(); var b = tess.Vertices[tess.Elements[i * 3 + 1]].ToCsgVector3(); var c = tess.Vertices[tess.Elements[i * 3 + 2]].ToCsgVector3(); Csg.Vertex av = null; Csg.Vertex bv = null; Csg.Vertex cv = null; // Merge vertices. foreach (var v in vertices) { if (v.Pos.IsAlmostEqualTo(a)) { av = v; } if (v.Pos.IsAlmostEqualTo(b)) { bv = v; } if (v.Pos.IsAlmostEqualTo(c)) { cv = v; } } if (av == null) { av = new Csg.Vertex(a, new Csg.Vector2D()); vertices.Add(av); } if (bv == null) { bv = new Csg.Vertex(b, new Csg.Vector2D()); vertices.Add(bv); } if (cv == null) { cv = new Csg.Vertex(c, new Csg.Vector2D()); vertices.Add(cv); } // TODO: Add texture coordinates. var p = new Csg.Polygon(new List <Csg.Vertex>() { av, bv, cv }); polygons.Add(p); } } return(Csg.Solid.FromPolygons(polygons)); }