/* * /// <summary> * /// * /// </summary> * /// <typeparam name="V"></typeparam> * /// <typeparam name="E"></typeparam> * /// <typeparam name="F"></typeparam> * /// <param name="mesh"></param> * /// <param name="getPosition"></param> * /// <param name="quadrangulator"></param> * /// <returns></returns> * public static M CreatePolySoup<V, E, F>(HeMesh<V, E, F> mesh, Func<V, Point3f> getPosition, Func<F, Color> getColor = null, IFaceQuadrangulator<V, E, F> quadrangulator = null) * where V : HeMeshBase<V, E, F>.Vertex * where E : HeMeshBase<V, E, F>.Halfedge * where F : HeMeshBase<V, E, F>.Face * { * var result = new M(); * var newVerts = result.Vertices; * var newColors = result.VertexColors; * var newFaces = result.Faces; * * // default quadrangulator * if (quadrangulator == null) * quadrangulator = FaceQuadrangulators.Strip.Create(mesh); * * // add vertices per face * foreach (var f in mesh.Faces) * { * if (f.IsUnused) continue; * int nv = newVerts.Count; * int degree = 0; * * // add face vertices * foreach (var v in f.Vertices) * { * newVerts.Add(getPosition(v)); * degree++; * } * * // add colors * if (getColor != null) * { * var c = getColor(f); * for (int i = 0; i < degree; i++) newColors.Add(c); * } * * // add face(s) * if (degree == 3) * { * newFaces.AddFace(nv, nv + 1, nv + 2); * } * else if (degree == 4) * { * newFaces.AddFace(nv, nv + 1, nv + 2, nv + 3); * } * else * { * foreach (var quad in quadrangulator.GetQuads(f)) * { * if (quad.Item4 == null) * newFaces.AddFace(nv + quad.Item1, nv + quad.Item2, nv + quad.Item3); * else * newFaces.AddFace(nv + quad.Item1, nv + quad.Item2, nv + quad.Item3, nv + quad.Item4); * } * } * } * * return result; * } */ /// <summary> /// /// </summary> /// <typeparam name="V"></typeparam> /// <typeparam name="E"></typeparam> /// <typeparam name="F"></typeparam> /// <param name="mesh"></param> /// <param name="getPosition"></param> /// <param name="quadrangulator"></param> /// <param name="getNormal"></param> /// <param name="getTexture"></param> /// <param name="getColor"></param> /// <returns></returns> public static T CreateFromHeMesh <V, E, F>(HeMesh <V, E, F> mesh, Func <V, Point3f> getPosition, Func <V, Vector3f> getNormal = null, Func <V, Point2f> getTexture = null, Func <V, Color> getColor = null, IFaceQuadrangulator <V, E, F> quadrangulator = null) where V : HeMesh <V, E, F> .Vertex where E : HeMesh <V, E, F> .Halfedge where F : HeMesh <V, E, F> .Face { var verts = mesh.Vertices; var result = new T(); var newVerts = result.Vertices; var newFaces = result.Faces; var newNorms = result.Normals; var newCoords = result.TextureCoordinates; var newColors = result.VertexColors; // default quadrangulator if (quadrangulator == null) { quadrangulator = FaceQuadrangulator.CreateStrip(mesh); } // add vertices for (int i = 0; i < verts.Count; i++) { var v = verts[i]; newVerts.Add(getPosition(v)); if (getNormal != null) { newNorms.Add(getNormal(v)); } if (getTexture != null) { newCoords.Add(getTexture(v)); } if (getColor != null) { newColors.Add(getColor(v)); } } // add faces foreach (var f in mesh.Faces) { if (f.IsUnused) { continue; } var he = f.First; int degree = f.Degree; if (degree == 3) { newFaces.AddFace( he.Start.Index, he.Next.Start.Index, he.Previous.Start.Index ); } else if (degree == 4) { newFaces.AddFace( he.Start.Index, he.Next.Start.Index, he.Next.Next.Start.Index, he.Previous.Start.Index ); } else { foreach (var quad in quadrangulator.GetQuads(f)) { if (quad.Item4 == null) { newFaces.AddFace(quad.Item1, quad.Item2, quad.Item3); } else { newFaces.AddFace(quad.Item1, quad.Item2, quad.Item3, quad.Item4); } } } } return(result); }
/// <summary> /// /// </summary> /// <typeparam name="V"></typeparam> /// <typeparam name="E"></typeparam> /// <typeparam name="F"></typeparam> /// <param name="mesh"></param> /// <param name="getPosition"></param> /// <param name="getColor"></param> /// <param name="quadrangulator"></param> /// <returns></returns> public static T CreatePolySoup <V, E, F>(HeMesh <V, E, F> mesh, Func <V, Point3f> getPosition, Func <F, Color> getColor = null, IFaceQuadrangulator <V, E, F> quadrangulator = null) where V : HeMesh <V, E, F> .Vertex where E : HeMesh <V, E, F> .Halfedge where F : HeMesh <V, E, F> .Face { var result = new T(); var newVerts = result.Vertices; var newColors = result.VertexColors; var newFaces = result.Faces; // default quadrangulator if (quadrangulator == null) { quadrangulator = FaceQuadrangulator.CreateStrip(mesh); } // add vertices per face foreach (var f in mesh.Faces) { if (f.IsUnused) { continue; } int degree = f.Degree; int nv = newVerts.Count; // add colors if (getColor != null) { var c = getColor(f); for (int i = 0; i < degree; i++) { newColors.Add(c); } } // handle n-gons if (degree > 4) { var quads = quadrangulator.GetQuads(f); // add first 2 vertices var first = quads.First(); newVerts.Add(getPosition(first.Item1)); newVerts.Add(getPosition(first.Item2)); // add remaining vertices and faces foreach (var quad in quads) { var v0 = quad.Item3; var v1 = quad.Item4; if (v1 == null) { newVerts.Add(getPosition(v0)); newFaces.AddFace(nv, nv + 1, nv + 2); break; } newVerts.Add(getPosition(v1)); newVerts.Add(getPosition(v0)); newFaces.AddFace(nv, nv + 1, nv + 3, nv + 2); nv += 2; } } else { // add face vertices foreach (var v in f.Vertices) { newVerts.Add(getPosition(v)); } // add face if (degree == 3) { newFaces.AddFace(nv, nv + 1, nv + 2); } else { newFaces.AddFace(nv, nv + 1, nv + 2, nv + 3); } } } return(result); }