Exemple #1
0
        /// <summary>
        /// Gets the contour segments.
        /// </summary>
        /// <param name="mesh">
        /// The mesh.
        /// </param>
        /// <param name="plane">
        /// The plane origin.
        /// </param>
        /// <param name="normal">
        /// The plane normal.
        /// </param>
        /// <returns>
        /// The segments of the contour.
        /// </returns>
        public static IList <Point3D> GetContourSegments(MeshGeometry3D mesh, Point3D plane, Vector3D normal)
        {
            var segments      = new List <Point3D>();
            var contourHelper = new ContourHelper(plane, normal, mesh);

            for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                Point3D[]  positions;
                Vector3D[] normals;
                Point[]    textureCoordinates;
                int[]      triangleIndices;

                contourHelper.ContourFacet(
                    mesh.TriangleIndices[i],
                    mesh.TriangleIndices[i + 1],
                    mesh.TriangleIndices[i + 2],
                    out positions,
                    out normals,
                    out textureCoordinates,
                    out triangleIndices);
                segments.AddRange(positions);
            }

            return(segments);
        }
Exemple #2
0
        /// <summary>
        /// Cuts the mesh with the specified plane.
        /// </summary>
        /// <param name="mesh">
        /// The mesh.
        /// </param>
        /// <param name="plane">
        /// The plane origin.
        /// </param>
        /// <param name="normal">
        /// The plane normal.
        /// </param>
        /// <returns>
        /// The <see cref="MeshGeometry3D"/>.
        /// </returns>
        public static MeshGeometry3D Cut(MeshGeometry3D mesh, Point3D plane, Vector3D normal)
        {
            var hasTextureCoordinates = mesh.TextureCoordinates != null && mesh.TextureCoordinates.Count > 0;
            var meshBuilder           = new MeshBuilder(false, hasTextureCoordinates);
            var contourHelper         = new ContourHelper(plane, normal, mesh, hasTextureCoordinates);

            foreach (var position in mesh.Positions)
            {
                meshBuilder.Positions.Add(position);
            }

            if (hasTextureCoordinates)
            {
                foreach (var textureCoordinate in mesh.TextureCoordinates)
                {
                    meshBuilder.TextureCoordinates.Add(textureCoordinate);
                }
            }

            for (var i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                var index0 = mesh.TriangleIndices[i];
                var index1 = mesh.TriangleIndices[i + 1];
                var index2 = mesh.TriangleIndices[i + 2];

                Point3D[] positions;
                Point[]   textureCoordinates;
                int[]     triangleIndices;

                contourHelper.ContourFacet(index0, index1, index2, out positions, out textureCoordinates, out triangleIndices);

                foreach (var p in positions)
                {
                    meshBuilder.Positions.Add(p);
                }

                foreach (var tc in textureCoordinates)
                {
                    meshBuilder.TextureCoordinates.Add(tc);
                }

                foreach (var ti in triangleIndices)
                {
                    meshBuilder.TriangleIndices.Add(ti);
                }
            }

            return(meshBuilder.ToMesh());
        }
        /// <summary>
        /// Gets the contour segments.
        /// </summary>
        /// <param name="mesh">The mesh.</param>
        /// <param name="p">The plane origin.</param>
        /// <param name="n">The plane normal.</param>
        /// <returns></returns>
        public static IList <Point3D> GetContourSegments(MeshGeometry3D mesh, Point3D p, Vector3D n)
        {
            var segments = new List <Point3D>();
            var ch       = new ContourHelper(p, n);

            for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                var     p0 = mesh.Positions[mesh.TriangleIndices[i]];
                var     p1 = mesh.Positions[mesh.TriangleIndices[i + 1]];
                var     p2 = mesh.Positions[mesh.TriangleIndices[i + 2]];
                Point3D s0, s1;
                int     r = ch.ContourFacet(p0, p1, p2, out s0, out s1);
                if (r >= 0)
                {
                    segments.Add(s0);
                    segments.Add(s1);
                }
            }
            return(segments);
        }
        /// <summary>
        /// Gets the contour segments.
        /// </summary>
        /// <param name="mesh">
        /// The mesh.
        /// </param>
        /// <param name="plane">
        /// The plane origin.
        /// </param>
        /// <param name="normal">
        /// The plane normal.
        /// </param>
        /// <returns>
        /// The segments of the contour.
        /// </returns>
        public static IList<Point3D> GetContourSegments(MeshGeometry3D mesh, Point3D plane, Vector3D normal)
        {
            var segments = new List<Point3D>();
            var contourHelper = new ContourHelper(plane, normal, mesh);
            for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                Point3D[] positions;
                Point[] textureCoordinates;
                int[] triangleIndices;

                contourHelper.ContourFacet(
                    mesh.TriangleIndices[i],
                    mesh.TriangleIndices[i + 1],
                    mesh.TriangleIndices[i + 2],
                    out positions,
                    out textureCoordinates,
                    out triangleIndices);
                segments.AddRange(positions);
            }

            return segments;
        }
        /// <summary>
        /// Cuts the mesh with the specified plane.
        /// </summary>
        /// <param name="mesh">
        /// The mesh.
        /// </param>
        /// <param name="plane">
        /// The plane origin.
        /// </param>
        /// <param name="normal">
        /// The plane normal.
        /// </param>
        /// <returns>
        /// The <see cref="MeshGeometry3D"/>.
        /// </returns>
        public static MeshGeometry3D Cut(MeshGeometry3D mesh, Point3D plane, Vector3D normal)
        {
            var hasTextureCoordinates = mesh.TextureCoordinates != null && mesh.TextureCoordinates.Count > 0;
            var meshBuilder = new MeshBuilder(false, hasTextureCoordinates);
            var contourHelper = new ContourHelper(plane, normal, mesh, hasTextureCoordinates);
            foreach (var position in mesh.Positions)
            {
                meshBuilder.Positions.Add(position);
            }

            if (hasTextureCoordinates)
            {
                foreach (var textureCoordinate in mesh.TextureCoordinates)
                {
                    meshBuilder.TextureCoordinates.Add(textureCoordinate);
                }
            }

            for (var i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                var index0 = mesh.TriangleIndices[i];
                var index1 = mesh.TriangleIndices[i + 1];
                var index2 = mesh.TriangleIndices[i + 2];

                Point3D[] positions;
                Point[] textureCoordinates;
                int[] triangleIndices;

                contourHelper.ContourFacet(index0, index1, index2, out positions, out textureCoordinates, out triangleIndices);

                foreach (var p in positions)
                {
                    meshBuilder.Positions.Add(p);
                }

                foreach (var tc in textureCoordinates)
                {
                    meshBuilder.TextureCoordinates.Add(tc);
                }

                foreach (var ti in triangleIndices)
                {
                    meshBuilder.TriangleIndices.Add(ti);
                }
            }

            return meshBuilder.ToMesh();
        }
        /// <summary>
        /// Cuts the mesh with the specified plane.
        /// </summary>
        /// <param name="mesh">The mesh.</param>
        /// <param name="p">The plane origin.</param>
        /// <param name="n">The plane normal.</param>
        /// <returns></returns>
        public static MeshGeometry3D Cut(MeshGeometry3D mesh, Point3D p, Vector3D n)
        {
            var ch = new ContourHelper(p, n);
            var mb = new MeshBuilder(false, false);

            foreach (var pos in mesh.Positions)
            {
                mb.Positions.Add(pos);
            }
            int j = mb.Positions.Count;

            for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                int     i0 = mesh.TriangleIndices[i];
                int     i1 = mesh.TriangleIndices[i + 1];
                int     i2 = mesh.TriangleIndices[i + 2];
                var     p0 = mesh.Positions[i0];
                var     p1 = mesh.Positions[i1];
                var     p2 = mesh.Positions[i2];
                Point3D s0, s1;
                int     r = ch.ContourFacet(p0, p1, p2, out s0, out s1);
                switch (r)
                {
                case -1:
                    mb.TriangleIndices.Add(i0);
                    mb.TriangleIndices.Add(i1);
                    mb.TriangleIndices.Add(i2);
                    //mb.AddTriangle(p0, p1, p2);
                    break;

                case 0:
                    mb.Positions.Add(s1);
                    mb.Positions.Add(s0);
                    mb.TriangleIndices.Add(i0);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(j++);
                    //mb.AddTriangle(p0, s1, s0);
                    break;

                case 1:
                    mb.Positions.Add(s0);
                    mb.Positions.Add(s1);
                    mb.TriangleIndices.Add(i1);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(j++);
                    //mb.AddTriangle(p1, s0, s1);
                    break;

                case 2:
                    mb.Positions.Add(s0);
                    mb.Positions.Add(s1);
                    mb.TriangleIndices.Add(i2);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(j++);
                    // mb.AddTriangle(p2, s0, s1);
                    break;

                case 10:
                    mb.Positions.Add(s0);
                    mb.Positions.Add(s1);
                    mb.TriangleIndices.Add(i1);
                    mb.TriangleIndices.Add(i2);
                    mb.TriangleIndices.Add(j);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(i1);

                    // mb.AddTriangle(p1, p2, s0);
                    // mb.AddTriangle(s0, s1, p1);
                    break;

                case 11:
                    mb.Positions.Add(s1);
                    mb.Positions.Add(s0);
                    mb.TriangleIndices.Add(i2);
                    mb.TriangleIndices.Add(i0);
                    mb.TriangleIndices.Add(j);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(i2);

                    // mb.AddTriangle(p2, p0, s1);
                    // mb.AddTriangle(s1, s0, p2);
                    break;

                case 12:
                    mb.Positions.Add(s1);
                    mb.Positions.Add(s0);
                    mb.TriangleIndices.Add(i0);
                    mb.TriangleIndices.Add(i1);
                    mb.TriangleIndices.Add(j);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(j++);
                    mb.TriangleIndices.Add(i0);
                    // mb.AddTriangle(p0, p1, s1);
                    // mb.AddTriangle(s1, s0, p0);
                    break;
                }
            }
            return(mb.ToMesh());
        }
 /// <summary>
 /// Gets the contour segments.
 /// </summary>
 /// <param name="mesh">The mesh.</param>
 /// <param name="p">The plane origin.</param>
 /// <param name="n">The plane normal.</param>
 /// <returns></returns>
 public static IList<Point3D> GetContourSegments(MeshGeometry3D mesh, Point3D p, Vector3D n)
 {
     var segments = new List<Point3D>();
     var ch = new ContourHelper(p, n);
     for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
     {
         var p0 = mesh.Positions[mesh.TriangleIndices[i]];
         var p1 = mesh.Positions[mesh.TriangleIndices[i + 1]];
         var p2 = mesh.Positions[mesh.TriangleIndices[i + 2]];
         Point3D s0, s1;
         int r = ch.ContourFacet(p0, p1, p2, out s0, out s1);
         if (r >= 0)
         {
             segments.Add(s0);
             segments.Add(s1);
         }
     }
     return segments;
 }
        /// <summary>
        /// Cuts the mesh with the specified plane.
        /// </summary>
        /// <param name="mesh">The mesh.</param>
        /// <param name="p">The plane origin.</param>
        /// <param name="n">The plane normal.</param>
        /// <returns></returns>
        public static MeshGeometry3D Cut(MeshGeometry3D mesh, Point3D p, Vector3D n)
        {
            var ch = new ContourHelper(p, n);
            var mb = new MeshBuilder(false, false);
            foreach (var pos in mesh.Positions)
                mb.Positions.Add(pos);
            int j = mb.Positions.Count;
            for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                int i0 = mesh.TriangleIndices[i];
                int i1 = mesh.TriangleIndices[i + 1];
                int i2 = mesh.TriangleIndices[i + 2];
                var p0 = mesh.Positions[i0];
                var p1 = mesh.Positions[i1];
                var p2 = mesh.Positions[i2];
                Point3D s0, s1;
                int r = ch.ContourFacet(p0, p1, p2, out s0, out s1);
                switch (r)
                {
                    case -1:
                        mb.TriangleIndices.Add(i0);
                        mb.TriangleIndices.Add(i1);
                        mb.TriangleIndices.Add(i2);
                        //mb.AddTriangle(p0, p1, p2);
                        break;
                    case 0:
                        mb.Positions.Add(s1);
                        mb.Positions.Add(s0);
                        mb.TriangleIndices.Add(i0);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        //mb.AddTriangle(p0, s1, s0);
                        break;
                    case 1:
                        mb.Positions.Add(s0);
                        mb.Positions.Add(s1);
                        mb.TriangleIndices.Add(i1);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        //mb.AddTriangle(p1, s0, s1);
                        break;
                    case 2:
                        mb.Positions.Add(s0);
                        mb.Positions.Add(s1);
                        mb.TriangleIndices.Add(i2);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        // mb.AddTriangle(p2, s0, s1);
                        break;
                    case 10:
                        mb.Positions.Add(s0);
                        mb.Positions.Add(s1);
                        mb.TriangleIndices.Add(i1);
                        mb.TriangleIndices.Add(i2);
                        mb.TriangleIndices.Add(j);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(i1);

                        // mb.AddTriangle(p1, p2, s0);
                        // mb.AddTriangle(s0, s1, p1);
                        break;
                    case 11:
                        mb.Positions.Add(s1);
                        mb.Positions.Add(s0);
                        mb.TriangleIndices.Add(i2);
                        mb.TriangleIndices.Add(i0);
                        mb.TriangleIndices.Add(j);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(i2);

                        // mb.AddTriangle(p2, p0, s1);
                        // mb.AddTriangle(s1, s0, p2);
                        break;
                    case 12:
                        mb.Positions.Add(s1);
                        mb.Positions.Add(s0);
                        mb.TriangleIndices.Add(i0);
                        mb.TriangleIndices.Add(i1);
                        mb.TriangleIndices.Add(j);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(i0);
                        // mb.AddTriangle(p0, p1, s1);
                        // mb.AddTriangle(s1, s0, p0);
                        break;
                }
            }
            return mb.ToMesh();
        }
        /// <summary>
        /// Cuts the mesh with the specified plane.
        /// </summary>
        /// <param name="mesh">
        /// The mesh.
        /// </param>
        /// <param name="p">
        /// The plane origin.
        /// </param>
        /// <param name="n">
        /// The plane normal.
        /// </param>
        /// <returns>
        /// The <see cref="MeshGeometry3D"/>.
        /// </returns>
        public static MeshGeometry3D Cut(MeshGeometry3D mesh, Point3D p, Vector3D n)
        {
            var ch = new ContourHelper(p, n);
            var mb = new MeshBuilder(false, false);
            foreach (var pos in mesh.Positions)
            {
                mb.Positions.Add(pos);
            }

            int j = mb.Positions.Count;
            for (int i = 0; i < mesh.TriangleIndices.Count; i += 3)
            {
                int i0 = mesh.TriangleIndices[i];
                int i1 = mesh.TriangleIndices[i + 1];
                int i2 = mesh.TriangleIndices[i + 2];
                var p0 = mesh.Positions[i0];
                var p1 = mesh.Positions[i1];
                var p2 = mesh.Positions[i2];
                Point3D s0, s1;
                int r = ch.ContourFacet(p0, p1, p2, out s0, out s1);
                switch (r)
                {
                    case -1:
                        mb.TriangleIndices.Add(i0);
                        mb.TriangleIndices.Add(i1);
                        mb.TriangleIndices.Add(i2);
                        break;
                    case 0:
                        mb.Positions.Add(s1);
                        mb.Positions.Add(s0);
                        mb.TriangleIndices.Add(i0);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        break;
                    case 1:
                        mb.Positions.Add(s0);
                        mb.Positions.Add(s1);
                        mb.TriangleIndices.Add(i1);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        break;
                    case 2:
                        mb.Positions.Add(s0);
                        mb.Positions.Add(s1);
                        mb.TriangleIndices.Add(i2);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        break;
                    case 10:
                        mb.Positions.Add(s0);
                        mb.Positions.Add(s1);
                        mb.TriangleIndices.Add(i1);
                        mb.TriangleIndices.Add(i2);
                        mb.TriangleIndices.Add(j);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(i1);
                        break;
                    case 11:
                        mb.Positions.Add(s1);
                        mb.Positions.Add(s0);
                        mb.TriangleIndices.Add(i2);
                        mb.TriangleIndices.Add(i0);
                        mb.TriangleIndices.Add(j);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(i2);
                        break;
                    case 12:
                        mb.Positions.Add(s1);
                        mb.Positions.Add(s0);
                        mb.TriangleIndices.Add(i0);
                        mb.TriangleIndices.Add(i1);
                        mb.TriangleIndices.Add(j);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(j++);
                        mb.TriangleIndices.Add(i0);
                        break;
                }
            }
            // begin bonghi: this is different from the original HelixToolkit version
            if (mb.TriangleIndices.Count == 0)
                return new MeshGeometry3D();
            // end bonghi
            return mb.ToMesh();
        }