public static Vector CalculateNormal(IModel model, Face f)
        {
            var normal = new Vector(0, 0, 0);

            for (var idx0 = 0; idx0 < f.VertexIndices.Length; idx0++)
            {
                var idx1 = new CircularIterator(f.VertexIndices.Length, idx0 + 1).Current;
                var idx2 = new CircularIterator(f.VertexIndices.Length, idx1 + 1).Current;

                normal = normal + CalculateNormal(model, f.VertexIndices[idx0], f.VertexIndices[idx1], f.VertexIndices[idx2]);
            }

            return(normal.Normalize());
        }
示例#2
0
        private static bool CanClip(IModel model, Face f, IList <int> vertices, int i0, int i1, int i2)
        {
            if (IsConcave(model, f, vertices[i0], vertices[i1], vertices[i2]))
            {
                return(false);
            }

            var ip = new CircularIterator(vertices.Count, i2 + 1);

            do
            {
                if (!IsOutSideTriangle(model, f, vertices[i0], vertices[i1], vertices[i2], vertices[ip.Current]))
                {
                    return(false);
                }
                ip.MoveNext();
            } while (ip.Current != i0);
            return(true);
        }
示例#3
0
        private static List <int[]> Triangulate(IModel model, Face f, List <int> vertices, List <int[]> results, int startIndex = 0)
        {
            var i0 = new CircularIterator(vertices.Count, startIndex).Current;
            var i1 = new CircularIterator(vertices.Count, startIndex + 1).Current;
            var i2 = new CircularIterator(vertices.Count, startIndex + 2).Current;

            if (vertices.Count == 3)
            {
                results.Add(vertices.ToArray());
                return(results);
            }

            if (!CanClip(model, f, vertices, i0, i1, i2))
            {
                return(Triangulate(model, f, vertices, results, i1));
            }

            results.Add(new[] { vertices[i0], vertices[i1], vertices[i2] });
            Debug.Assert(vertices.Remove(vertices[i1]));

            Triangulate(model, f, vertices, results, i2);
            return(results);
        }