Пример #1
0
        /// <summary>
        /// Creates a new mesh where no vertices are shared.
        /// </summary>
        /// <param name="input">
        /// The input mesh.
        /// </param>
        /// <returns>
        /// A new mesh.
        /// </returns>
        public static MeshGeometry3D NoSharedVertices(this MeshGeometry3D input)
        {
            var p  = new Point3DCollection();
            var ti = new Int32Collection();
            Vector3DCollection n = null;

            if (input.Normals != null)
            {
                n = new Vector3DCollection();
            }

            PointCollection tc = null;

            if (input.TextureCoordinates != null)
            {
                tc = new PointCollection();
            }

            for (int i = 0; i < input.TriangleIndices.Count; i += 3)
            {
                int i0     = i;
                int i1     = i + 1;
                int i2     = i + 2;
                int index0 = input.TriangleIndices[i0];
                int index1 = input.TriangleIndices[i1];
                int index2 = input.TriangleIndices[i2];
                var p0     = input.Positions[index0];
                var p1     = input.Positions[index1];
                var p2     = input.Positions[index2];
                p.Add(p0);
                p.Add(p1);
                p.Add(p2);
                ti.Add(i0);
                ti.Add(i1);
                ti.Add(i2);
                if (n != null)
                {
                    n.Add(input.Normals[index0]);
                    n.Add(input.Normals[index1]);
                    n.Add(input.Normals[index2]);
                }

                if (tc != null)
                {
                    tc.Add(input.TextureCoordinates[index0]);
                    tc.Add(input.TextureCoordinates[index1]);
                    tc.Add(input.TextureCoordinates[index2]);
                }
            }

#if SHARPDX
            return(new MeshGeometry3D {
                Positions = p, TriangleIndices = new SharpDX.Core.IntCollection(ti), Normals = n, TextureCoordinates = tc
            });
#else
            return(new MeshGeometry3D {
                Positions = p, TriangleIndices = ti, Normals = n, TextureCoordinates = tc
            });
#endif
        }
Пример #2
0
        /// <summary>
        /// Remove isolated(not connected to any triangles) vertices
        /// </summary>
        /// <param name="vertices"></param>
        /// <param name="triangles"></param>
        /// <param name="texture"></param>
        /// <param name="normals"></param>
        /// <param name="verticesOut"></param>
        /// <param name="trianglesOut"></param>
        /// <param name="textureOut"></param>
        /// <param name="normalOut"></param>
        public static void RemoveIsolatedVertices(IList <Point3D> vertices, IList <int> triangles, IList <Point> texture, IList <Vector3D> normals,
                                                  out Point3DCollection verticesOut, out Int32Collection trianglesOut, out PointCollection textureOut, out Vector3DCollection normalOut)
        {
            verticesOut  = null;
            trianglesOut = null;
            textureOut   = null;
            normalOut    = null;
            List <List <int> > tracking = new List <List <int> >(vertices.Count);

            Debug.WriteLine(string.Format("NumVert:{0}; NumTriangle:{1};", vertices.Count, triangles.Count));
            for (int i = 0; i < vertices.Count; ++i)
            {
                tracking.Add(new List <int>());
            }
            for (int i = 0; i < triangles.Count; ++i)
            {
                tracking[triangles[i]].Add(i);
            }

            List <int> vertToRemove = new List <int>(vertices.Count);

            for (int i = 0; i < vertices.Count; ++i)
            {
                if (tracking[i].Count == 0)
                {
                    vertToRemove.Add(i);
                }
            }
            verticesOut  = new Point3DCollection(vertices.Count - vertToRemove.Count);
            trianglesOut = new Int32Collection(triangles);
            if (texture != null)
            {
                textureOut = new PointCollection(vertices.Count - vertToRemove.Count);
            }
            if (normals != null)
            {
                normalOut = new Vector3DCollection(vertices.Count - vertToRemove.Count);
            }
            if (vertices.Count == vertToRemove.Count)
            {
                return;
            }
            int counter = 0;

            for (int i = 0; i < vertices.Count; ++i)
            {
                if (counter == vertToRemove.Count || i < vertToRemove[counter])
                {
                    verticesOut.Add(vertices[i]);
                    if (texture != null)
                    {
                        textureOut.Add(texture[i]);
                    }
                    if (normals != null)
                    {
                        normalOut.Add(normals[i]);
                    }
                    foreach (var t in tracking[i])
                    {
                        trianglesOut[t] -= counter;
                    }
                }
                else
                {
                    ++counter;
                }
            }
            Debug.WriteLine(string.Format("Remesh finished. Output NumVert:{0};", verticesOut.Count));
        }
Пример #3
0
 public static System.Windows.Media.PointCollection ToPointCollection(SharpDX.Core.Vector2Collection collection)
 {
     return(new System.Windows.Media.PointCollection(collection.Select(v => new System.Windows.Point(v.X, v.Y))));
 }