Exemplo n.º 1
0
        internal static ThreeMfMesh ParseMesh(XElement element, Dictionary <int, ThreeMfResource> resourceMap)
        {
            if (element == null)
            {
                throw new ThreeMfParseException("Missing element <mesh>.");
            }

            var vertices = new List <ThreeMfVertex>();

            foreach (var vertexElement in element.Element(VerticesName).Elements(ThreeMfVertex.VertexName))
            {
                var vertex = ThreeMfVertex.ParseVertex(vertexElement);
                vertices.Add(vertex);
            }

            var mesh = new ThreeMfMesh();

            foreach (var triangleElement in element.Element(TrianglesName).Elements(ThreeMfTriangle.TriangleName))
            {
                var triangle = ThreeMfTriangle.ParseTriangle(triangleElement, vertices, resourceMap);
                mesh.Triangles.Add(triangle);
            }

            return(mesh);
        }
Exemplo n.º 2
0
        internal static ThreeMfTriangle ParseTriangle(XElement triangleElement, IList <ThreeMfVertex> vertices, Dictionary <int, ThreeMfResource> resourceMap)
        {
            var v1Index = triangleElement.AttributeIntValueOrThrow(V1AttributeName);
            var v2Index = triangleElement.AttributeIntValueOrThrow(V2AttributeName);
            var v3Index = triangleElement.AttributeIntValueOrThrow(V3AttributeName);

            if (v1Index == v2Index || v1Index == v3Index || v2Index == v3Index)
            {
                throw new ThreeMfParseException("Triangle must specify distinct indices.");
            }

            if (v1Index < 0 || v1Index >= vertices.Count ||
                v2Index < 0 || v2Index >= vertices.Count ||
                v3Index < 0 || v3Index >= vertices.Count)
            {
                throw new ThreeMfParseException("Triangle vertex index does not exist.");
            }

            var triangle = new ThreeMfTriangle(vertices[v1Index], vertices[v2Index], vertices[v3Index]);

            if (resourceMap.TryGetPropertyResource(triangleElement, PropertyIndexAttributeName, out var propertyResource))
            {
                triangle.PropertyResource = propertyResource;
                triangle.V1PropertyIndex  = propertyResource.ParseAndValidateOptionalResourceIndex(triangleElement, V1PropertyAttributeName);
                triangle.V2PropertyIndex  = propertyResource.ParseAndValidateOptionalResourceIndex(triangleElement, V2PropertyAttributeName);
                triangle.V3PropertyIndex  = propertyResource.ParseAndValidateOptionalResourceIndex(triangleElement, V3PropertyAttributeName);
            }

            return(triangle);
        }