Exemplo n.º 1
0
        public static List <PointDirection3> Tesselate(IFace2[] faceList, ITessellation tess)
        {
            var r = new List <PointDirection3>();

            // performance improvement
            // ReSharper disable once ForCanBeConvertedToForeach
            for (int index = 0; index < faceList.Length; index++)
            {
                var face = faceList[index];
                foreach (var facet in tess.GetFaceFacets(face).CastArray <int>())
                {
                    var finIds    = tess.GetFacetFins(facet).CastArray <int>();
                    var vertexIds = Enumerable.ToList <int>(Enumerable.SelectMany <int, int>(finIds, finId => tess.GetFinVertices(finId).CastArray <int>())
                                                            .DistinctUntilChanged()
                                                            .SkipLast(1));

                    var vertexs = vertexIds
                                  .Select <int, double[]>(vId => tess.GetVertexPoint(vId).CastArray <double>())
                                  .ToList();

                    var normals = vertexIds
                                  .Select <int, double[]>(vId => tess.GetVertexNormal(vId).CastArray <double>())
                                  .ToList();

                    // Unroll loop for performance
                    r.Add(new PointDirection3(vertexs[0].ToVector3D(), normals[0].ToVector3D()));
                    r.Add(new PointDirection3(vertexs[1].ToVector3D(), normals[1].ToVector3D()));
                    r.Add(new PointDirection3(vertexs[2].ToVector3D(), normals[2].ToVector3D()));
                }
            }

            return(r);
        }
Exemplo n.º 2
0
        private static BodyTessellation GetBodyMesh(IBody2 body, IPartDoc partDoc, ITessellation tessellation, long?overrideColor)
        {
            var bodyTessellation = new BodyTessellation();

            if (overrideColor.HasValue)
            {
                bodyTessellation.FaceColors.Add(overrideColor.Value);
            }
            else
            {
                double[] bodyColorArray = null;
                var      features       = (object[])body.GetFeatures();
                if (features != null)
                {
                    foreach (IFeature feature in features.Reverse())
                    {
                        bodyColorArray =
                            (double[])feature.GetMaterialPropertyValues2(
                                (int)swInConfigurationOpts_e.swThisConfiguration, null);
                        if (bodyColorArray[0] == -1
                            ) // All -1s are returned by features that don't assign color.
                        {
                            bodyColorArray = null;
                        }

                        if (bodyColorArray != null)
                        {
                            break;
                        }
                    }
                }

                if (bodyColorArray == null)
                {
                    bodyColorArray = (double[])body.MaterialPropertyValues2;
                }

                if (bodyColorArray == null)
                {
                    bodyColorArray = (double[])partDoc.MaterialPropertyValues;
                }

                var bodyColor = ColorArrayToColor(bodyColorArray);

                bodyTessellation.FaceColors.Add(bodyColor);
            }

            var coloredFaces = new Dictionary <IFace2, long>();
            var faceCount    = 0;

            foreach (IFace2 face in (object[])body.GetFaces())
            {
                faceCount++;
                var colorArray = (double[])face.MaterialPropertyValues;
                if (colorArray != null)
                {
                    coloredFaces[face] = ColorArrayToColor(colorArray);
                }
            }

            if (coloredFaces.Count < faceCount)
            {
                for (var i = 0; i < tessellation.GetVertexCount(); i++)
                {
                    bodyTessellation.VertexPositions.Add((double[])tessellation.GetVertexPoint(i));
                    bodyTessellation.VertexNormals.Add((double[])tessellation.GetVertexNormal(i));
                }

                foreach (IFace2 face in (object[])body.GetFaces())
                {
                    if (coloredFaces.ContainsKey(face))
                    {
                        continue;
                    }

                    foreach (var facet in (int[])tessellation.GetFaceFacets(face))
                    {
                        var vertexIndices = new List <int>();
                        foreach (var fin in (int[])tessellation.GetFacetFins(facet))
                        {
                            vertexIndices.Add(((int[])tessellation.GetFinVertices(fin))[0]);
                        }

                        bodyTessellation.Faces.Add(new FaceStruct {
                            Color   = 0,
                            Vertex1 = vertexIndices[0],
                            Vertex2 = vertexIndices[1],
                            Vertex3 = vertexIndices[2],
                        });
                    }
                }
            }

            foreach (var pair in coloredFaces)
            {
                var colorIndex = bodyTessellation.FaceColors.IndexOf(pair.Value);
                if (colorIndex == -1)
                {
                    bodyTessellation.FaceColors.Add(pair.Value);
                    colorIndex = bodyTessellation.FaceColors.Count - 1;
                }

                foreach (var facet in (int[])tessellation.GetFaceFacets(pair.Key))
                {
                    var vertexIndices = new List <int>();
                    foreach (var fin in (int[])tessellation.GetFacetFins(facet))
                    {
                        vertexIndices.Add(((int[])tessellation.GetFinVertices(fin))[0]);
                    }

                    bodyTessellation.Faces.Add(
                        new FaceStruct {
                        Color   = colorIndex,
                        Vertex1 = bodyTessellation.VertexPositions.Count,
                        Vertex2 = bodyTessellation.VertexPositions.Count + 1,
                        Vertex3 = bodyTessellation.VertexPositions.Count + 2
                    });

                    bodyTessellation.VertexPositions.Add(
                        (double[])tessellation.GetVertexPoint(vertexIndices[0]));
                    bodyTessellation.VertexPositions.Add(
                        (double[])tessellation.GetVertexPoint(vertexIndices[1]));
                    bodyTessellation.VertexPositions.Add(
                        (double[])tessellation.GetVertexPoint(vertexIndices[2]));

                    bodyTessellation.VertexNormals.Add(
                        (double[])tessellation.GetVertexNormal(vertexIndices[0]));
                    bodyTessellation.VertexNormals.Add(
                        (double[])tessellation.GetVertexNormal(vertexIndices[1]));
                    bodyTessellation.VertexNormals.Add(
                        (double[])tessellation.GetVertexNormal(vertexIndices[2]));
                }
            }
            return(bodyTessellation);
        }