Esempio 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 = finIds
                                    .SelectMany(finId => tess.GetFinVertices(finId).CastArray <int>())
                                    .DistinctUntilChanged()
                                    .SkipLast(1)
                                    .ToList();

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

                    var normals = vertexIds
                                  .Select(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);
        }
Esempio n. 2
0
        private static List <int> GetEdgePolyline(ITessellation tessellation, object edge)
        {
            var fins = (int[])tessellation.GetEdgeFins(edge);

            var finVertices   = new Dictionary <int, List <int> >();
            int minOpenVertex = -1;
            int minVertex     = int.MaxValue;

            foreach (var fin in fins)
            {
                var finEnds = (int[])tessellation.GetFinVertices(fin);
                var min     = Math.Min(finEnds[0], finEnds[1]);
                var max     = Math.Max(finEnds[0], finEnds[1]);

                if (min < minVertex)
                {
                    minVertex = min;
                }

                List <int> list;
                if (!finVertices.TryGetValue(min, out list))
                {
                    list = new List <int> {
                        max
                    };
                    finVertices[min] = list;

                    if (minOpenVertex == -1 || minOpenVertex > min)
                    {
                        minOpenVertex = min;
                    }
                }
                else
                {
                    list.Add(max);
                    if (minOpenVertex == min)
                    {
                        minOpenVertex = -1;
                    }
                }

                if (!finVertices.TryGetValue(max, out list))
                {
                    list = new List <int> {
                        min
                    };
                    finVertices[max] = list;

                    if (minOpenVertex == -1 || minOpenVertex > max)
                    {
                        minOpenVertex = max;
                    }
                }
                else
                {
                    list.Add(min);
                    if (minOpenVertex == max)
                    {
                        minOpenVertex = -1;
                    }
                }
            }

            var polyline = new List <int>();

            var visited = new HashSet <int>();

            var startVertex = minOpenVertex == -1 ? minVertex : minOpenVertex;

            while (visited.Count < finVertices.Count && startVertex != -1)
            {
                polyline.Add(startVertex);
                visited.Add(startVertex);

                var next = finVertices[startVertex];

                startVertex = -1;
                for (var i = 0; i < next.Count; i++)
                {
                    if (!visited.Contains(next[i]))
                    {
                        startVertex = next[i];
                    }
                }
            }

            if (minOpenVertex == -1)
            {
                polyline.Add(polyline[0]);
            }

            return(polyline);
        }
Esempio n. 3
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);
        }