Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a subdivision mesh by vertex shading, given an input 
        /// array of vertex points and an input array of faces defined by a set 
        /// of numbers, which are the indices of the vertices in the 'vertices' 
        /// array making up the face. 
        /// </summary>
        /// <param name="vertices">Input array of vertex points</param>
        /// <param name="faceIndices">Input array of faces indices for each 
        /// vertex </param>
        /// <param name="vertexNormals">Input array of vertex normals</param>
        /// <param name="vertexColors">Input array of color assigned to each
        /// vertex </param>
        /// <param name="subDivisionLevel">Initial smoothness level, subDivisionLevel must 
        /// have a value greater than or equal to 0 and less than 5.</param>
        /// <returns>SubDivisionMesh</returns>
        public static DSSubDivisionMesh ByVerticesFaceIndices(DSPoint[] vertices, 
            int[][] faceIndices, DSVector[] vertexNormals, DSColor[] vertexColors, int subDivisionLevel)
        {
            string kMethodName = "DSSubDivisionMesh.ByVerticesFaceIndices";
            if (subDivisionLevel < 0 || subDivisionLevel >= 5)
                throw new System.ArgumentException(string.Format(Properties.Resources.InvalidInput, "subDivisionLevel", kMethodName));

            IPointEntity[] points = vertices.ConvertAll(DSGeometryExtension.ToEntity<DSPoint, IPointEntity>);
            if (points.Length < 3 || points.ArePointsColinear())
                throw new System.ArgumentException(string.Format(Properties.Resources.InvalidInput, "vertices", kMethodName));

            ISubDMeshEntity entity = HostFactory.Factory.SubDMeshByVerticesFaceIndices(points, faceIndices, subDivisionLevel);
            if (null == entity)
                throw new System.InvalidOperationException(string.Format(Properties.Resources.OperationFailed, kMethodName));

            DSSubDivisionMesh mesh = new DSSubDivisionMesh(entity, true);
            try
            {
                if (null != vertexNormals && vertexNormals.Length > 0)
                {
                    if (vertexNormals.Length != entity.GetNumVertices())
                        throw new System.ArgumentException(string.Format(Properties.Resources.NotEqual, "size of vertexNormals", "number of vertices"), "vertexNormals");

                    if (!entity.UpdateSubDMeshNormals(vertexNormals.ConvertAll((DSVector v)=>v.IVector)))
                        throw new System.InvalidOperationException(string.Format(Properties.Resources.OperationFailed, kMethodName));
                }

                if (null != vertexColors && vertexColors.Length > 0)
                {
                    if (subDivisionLevel > 0)
                        throw new System.InvalidOperationException(Properties.Resources.VertexColorNotSupported);

                    if (vertexColors.Length != entity.GetNumVertices())
                        throw new System.ArgumentException(string.Format(Properties.Resources.NotEqual, "size of vertexColors", "number of vertices"), "vertexColors");

                    if (!entity.UpdateSubDMeshColors(vertexColors.ConvertAll((DSColor c)=>c.IColor)))
                        throw new System.InvalidOperationException(string.Format(Properties.Resources.OperationFailed, kMethodName));
                }
            }
            catch (System.Exception ex)
            {
                mesh.Dispose();
                throw ex;
            }

            mesh.SubDivisionLevel = subDivisionLevel;
            return mesh;
        }