Example #1
0
        public void Execute()
        {
            int vCount = inputVertices.Length, sCount = inputSitesVertices.Length, totalCount = vCount + sCount,
                A, B, C, tCount, nIndex;
            Triad                       triad;
            float3                      center, pt;
            NativeList <int>            neighbors     = new NativeList <int>(10, Allocator.Temp);
            NativeArray <IndexedVertex> neighborsList = new NativeArray <IndexedVertex>(0, Allocator.Temp);
            UIntPair                    edge;
            bool check = false;

            outputVertices.Clear();
            outputVertices.Capacity = totalCount;

            outputTriangles.Clear();
            outputTriangles.Capacity = sCount * 3;

            //Merge base vertices and voronoi sites in a single list
            for (int i = 0; i < vCount; i++)
            {
                outputVertices.Add(inputVertices[i]);
            }
            for (int i = 0; i < sCount; i++)
            {
                outputVertices.Add(inputSitesVertices[i]);
            }

            for (int i = 0; i < vCount; i++)
            {
                //A being the base vertice
                A = i;

                check = inputUnorderedHullEdges.TryGetValue(A, out edge);
                if (check)
                {
                    continue;
                }

                neighbors.Clear();
                tCount = inputSites.PushValues(ref i, ref neighbors);

                neighborsList.Release();
                neighborsList = new NativeArray <IndexedVertex>(tCount, Allocator.Temp);

                center = inputVertices[A];
                for (int v = 0; v < tCount; v++)
                {
                    nIndex = neighbors[v];
                    pt     = inputSitesVertices[nIndex] - center;
                    IndexedVertex iv = new IndexedVertex()
                    {
                        index = vCount + nIndex,
                        angle = plane == AxisPair.XY ? atan2(pt.y, pt.x) : atan2(pt.z, pt.x)
                    };
                    neighborsList[v] = iv;
                }

                neighborsList.Sort(new SortIndexedVertex());

                for (int v = 0; v < tCount; v++)
                {
                    B = neighborsList[v].index;
                    C = v + 1;

                    if (C >= tCount)
                    {
                        C = neighborsList[0].index;
                    }
                    else
                    {
                        C = neighborsList[v + 1].index;
                    }

                    if (check)
                    {
                        if (inputUnorderedHullEdges.TryGetValue(B - vCount, out edge) ||
                            inputUnorderedHullEdges.TryGetValue(C - vCount, out edge))
                        {
                            continue;
                        }
                    }


                    triad = new Triad(A, B, C, float3(0f), 0f);
                    outputTriangles.Add(triad);
                }
            }
        }
Example #2
0
        public void Execute()
        {
            int      A, B, C, siteCount = inputTriangles.Length, edgeIndex = -1, Ti;
            float3   centroid, circumcenter;
            UIntPair edge;
            Triad    triad;
            NativeHashMap <UIntPair, int>      uniqueEdges        = new NativeHashMap <UIntPair, int>(siteCount * 3, Allocator.Temp);
            NativeMultiHashMap <UIntPair, int> connectedTriangles = new NativeMultiHashMap <UIntPair, int>(siteCount * 3, Allocator.Temp);
            NativeList <int> neighbors = new NativeList <int>(0, Allocator.Temp);
            float            weight    = clamp(centroidWeight, 0f, 1f);

            if (weight == 0f)
            {
                for (int i = 0; i < siteCount; i++)
                {
                    triad = inputTriangles[i];
                    A     = triad.A; B = triad.B; C = triad.C;
                    outputVertices.Add(Circumcenter(ref triad, ref inputVertices));

                    connectedTriangles.Add(new UIntPair(A, B), i);
                    connectedTriangles.Add(new UIntPair(B, C), i);
                    connectedTriangles.Add(new UIntPair(C, A), i);
                }
            }
            else if (weight == 1f)
            {
                for (int i = 0; i < siteCount; i++)
                {
                    triad = inputTriangles[i];
                    A     = triad.A; B = triad.B; C = triad.C;
                    outputVertices.Add((inputVertices[A] + inputVertices[B] + inputVertices[C]) / 3f);

                    connectedTriangles.Add(new UIntPair(A, B), i);
                    connectedTriangles.Add(new UIntPair(B, C), i);
                    connectedTriangles.Add(new UIntPair(C, A), i);
                }
            }
            else
            {
                for (int i = 0; i < siteCount; i++)
                {
                    triad = inputTriangles[i];
                    A     = triad.A; B = triad.B; C = triad.C;

                    centroid     = (inputVertices[A] + inputVertices[B] + inputVertices[C]) / 3f;
                    circumcenter = Circumcenter(ref triad, ref inputVertices);

                    outputVertices.Add(lerp(circumcenter, centroid, weight));

                    connectedTriangles.Add(new UIntPair(A, B), i);
                    connectedTriangles.Add(new UIntPair(B, C), i);
                    connectedTriangles.Add(new UIntPair(C, A), i);
                }
            }

            for (int i = 0; i < siteCount; i++)
            {
                triad = inputTriangles[i];
                A     = triad.A; B = triad.B; C = triad.C;

                outputSites.Add(A, i);
                outputSites.Add(B, i);
                outputSites.Add(C, i);

                neighbors.Clear();

                edge = new UIntPair(A, B);
                connectedTriangles.PushValues(ref edge, ref neighbors);
                edge = new UIntPair(B, C);
                connectedTriangles.PushValues(ref edge, ref neighbors);
                edge = new UIntPair(C, A);
                connectedTriangles.PushValues(ref edge, ref neighbors);

                for (int t = 0; t < neighbors.Length; t++)
                {
                    Ti = neighbors[t];
                    if (Ti == i)
                    {
                        continue;
                    }

                    edge = new UIntPair(Ti, i);

                    if (!uniqueEdges.TryGetValue(edge, out edgeIndex))
                    {
                        uniqueEdges.TryAdd(edge, outputEdges.Length);
                        outputEdges.Add(edge);
                    }
                }
            }
        }