// // DMesh3 construction utilities // /// <summary> /// ultimate generic mesh-builder, pass it arrays of floats/doubles, or lists /// of Vector3d, or anything in-between. Will figure out how to interpret /// </summary> public static NGonsCore.geometry3Sharp.mesh.DMesh3 Build <VType, TType, NType>(IEnumerable <VType> Vertices, IEnumerable <TType> Triangles, IEnumerable <NType> Normals = null, IEnumerable <int> TriGroups = null) { NGonsCore.geometry3Sharp.mesh.DMesh3 mesh = new NGonsCore.geometry3Sharp.mesh.DMesh3(Normals != null, false, false, TriGroups != null); Vector3D[] v = BufferUtil.ToVector3d(Vertices); for (int i = 0; i < v.Length; ++i) { mesh.AppendVertex(v[i]); } if (Normals != null) { Vector3F[] n = BufferUtil.ToVector3f(Normals); if (n.Length != v.Length) { throw new Exception("DMesh3Builder.Build: incorrect number of normals provided"); } for (int i = 0; i < n.Length; ++i) { mesh.SetVertexNormal(i, n[i]); } } Index3i[] t = BufferUtil.ToIndex3i(Triangles); for (int i = 0; i < t.Length; ++i) { mesh.AppendTriangle(t[i]); } if (TriGroups != null) { List <int> groups = new List <int>(TriGroups); if (groups.Count != t.Length) { throw new Exception("DMesh3Builder.Build: incorect number of triangle groups"); } for (int i = 0; i < t.Length; ++i) { mesh.SetTriangleGroup(i, groups[i]); } } return(mesh); }
ConvertANSYSFacetListToDMesh( double[] ansysVertexPositions, int[] ansysFacet, double[] ansysVertexNormals ) { Vector3d[] p = BufferUtil.ToVector3d(ansysVertexPositions); Vector3d[] n = BufferUtil.ToVector3d(ansysVertexNormals); List <int> facets = new List <int>(ansysFacet.Count() / 4); int i = 0; while (i < ansysFacet.Length) { if (ansysFacet[i] == 3) { int iv0 = ansysFacet[++i]; int iv1 = ansysFacet[++i]; int iv2 = ansysFacet[++i]; Vector3d V0, V1, V2, N0; V0 = p[iv0]; V1 = p[iv1]; V2 = p[iv2]; N0 = n[iv0]; Vector3d edge1 = V1 - V0; Vector3d edge2 = V2 - V0; Vector3d normal = edge1.Cross(edge2); if (normal.Dot(N0) > 0) { facets.Add(iv0); facets.Add(iv1); facets.Add(iv2); } else { facets.Add(iv0); facets.Add(iv2); facets.Add(iv1); } } else { throw new InvalidDataException("Cannot convert an ANSYS facet list with a facet containing more than 3 vertices."); } i++; } return(facets); }
BuildFaceMessages(IGeoFace face) { List <BRepEntity> entities = new List <BRepEntity>(); var protoSurface = new Surface(); Vector3d[] p = BufferUtil.ToVector3d(face.Points); Vector3d[] n = BufferUtil.ToVector3d(face.Normals); foreach (var v in p) { var protoPoint = new Vector3 { X = v.x, Y = v.y, Z = v.z }; var uv = face.ParamAtPoint(new double[] { v.x, v.y, v.z }); var protoUV = new Vector2 { U = uv[0], V = uv[1] }; protoSurface.Points.Add(protoPoint); protoSurface.Parameters.Add(protoUV); } foreach (var v in n) { var protoNormal = new Vector3 { X = v.x, Y = v.y, Z = v.z }; protoSurface.Normals.Add(protoNormal); } // Get the indices into the proper form int i = 0; while (i < face.Indices.Length) { if (face.Indices[i] == 3) { int iv0 = face.Indices[++i]; int iv1 = face.Indices[++i]; int iv2 = face.Indices[++i]; Vector3d V0, V1, V2, N0; V0 = p[iv0]; V1 = p[iv1]; V2 = p[iv2]; N0 = n[iv0]; Vector3d edge1 = V1 - V0; Vector3d edge2 = V2 - V0; Vector3d normal = edge1.Cross(edge2); if (normal.Dot(N0) > 0) { protoSurface.Triangles.Add(new Facet { I = iv0, J = iv1, K = iv2 }); } else { protoSurface.Triangles.Add(new Facet { I = iv0, J = iv2, K = iv1 }); } } else { throw new InvalidDataException("Cannot convert an ANSYS facet " + "list with a facet containing more than 3 vertices."); } i++; } var protoFace = new Face { Id = face.Id, Surface = protoSurface }; var edges = (from e in face.Edges select(long) e.Id).ToList(); protoFace.Edges.AddRange(edges); foreach (var l in face.Loops) { var protoLoop = new Loop(); var loopEdges = (from e in l.Edges select(long) e.Id).ToList(); protoLoop.Edges.AddRange(loopEdges); protoFace.Loops.Add(protoLoop); } entities.Add(new BRepEntity { Face = protoFace }); return(entities); }