Пример #1
0
    public static RefinementResult Make(QuadTopology controlTopology, int[] controlSurfaceMap, int refinementLevel, bool derivativesOnly)
    {
        if (controlTopology.Faces.Length == 0 && controlTopology.VertexCount == 0)
        {
            return(RefinementResult.Empty);
        }

        PackedLists <WeightedIndex> limitStencils, limitDuStencils, limitDvStencils;
        QuadTopology            refinedTopology;
        SubdivisionTopologyInfo refinedTopologyInfo;

        int[] controlFaceMap;
        using (var refinement = new Refinement(controlTopology, refinementLevel)) {
            limitStencils   = refinement.GetStencils(StencilKind.LimitStencils);
            limitDuStencils = refinement.GetStencils(StencilKind.LimitDuStencils);
            limitDvStencils = refinement.GetStencils(StencilKind.LimitDvStencils);
            refinedTopology = refinement.GetTopology();

            var adjacentVertices = refinement.GetAdjacentVertices();
            var rules            = refinement.GetVertexRules();
            refinedTopologyInfo = new SubdivisionTopologyInfo(adjacentVertices, rules);

            controlFaceMap = refinement.GetFaceMap();
        }

        if (derivativesOnly)
        {
            if (refinementLevel != 0)
            {
                throw new InvalidOperationException("derivatives-only mode can only be used at refinement level 0");
            }

            limitStencils = PackedLists <WeightedIndex> .Pack(Enumerable.Range(0, controlTopology.VertexCount)
                                                              .Select(vertexIdx => {
                var selfWeight = new WeightedIndex(vertexIdx, 1);
                return(new List <WeightedIndex> {
                    selfWeight
                });
            }).ToList());
        }

        PackedLists <WeightedIndexWithDerivatives> stencils = WeightedIndexWithDerivatives.Merge(limitStencils, limitDuStencils, limitDvStencils);
        var refinedMesh = new SubdivisionMesh(controlTopology.VertexCount, refinedTopology, stencils);

        return(new RefinementResult(refinedMesh, refinedTopologyInfo, controlFaceMap));
    }
    public void Run()
    {
        int controlVertexCount = 6;

        Quad[] controlFaces = new [] {
            new Quad(0, 1, 2, 3),
            new Quad(1, 4, 5, 2)
        };
        QuadTopology controlTopology = new QuadTopology(controlVertexCount, controlFaces);

        int refinementLevel = 1;

        using (Refinement refinement = new Refinement(controlTopology, refinementLevel)) {
            QuadTopology topology = refinement.GetTopology();
            int[]        faceMap  = refinement.GetFaceMap();

            for (int faceIdx = 0; faceIdx < topology.Faces.Length; ++faceIdx)
            {
                Console.WriteLine(topology.Faces[faceIdx] + " -> " + faceMap[faceIdx]);
            }
            Console.WriteLine();

            PackedLists <int> adjacentVertices = refinement.GetAdjacentVertices();
            VertexRule[]      vertexRules      = refinement.GetVertexRules();
            Console.WriteLine("adjacent vertices and rules: ");
            for (int vertexIdx = 0; vertexIdx < topology.VertexCount; ++vertexIdx)
            {
                VertexRule vertexRule = vertexRules[vertexIdx];

                Console.WriteLine("\t" + vertexIdx + $": [{vertexRule}] " + String.Join(", ", adjacentVertices.GetElements(vertexIdx)));
            }
            Console.WriteLine();

            PackedLists <WeightedIndex> stencils = refinement.GetStencils(StencilKind.LimitStencils);
            Console.WriteLine("stencils: ");
            for (int vertexIdx = 0; vertexIdx < stencils.Count; ++vertexIdx)
            {
                Console.WriteLine(vertexIdx + ":");
                foreach (WeightedIndex weightedIndex in stencils.GetElements(vertexIdx))
                {
                    Console.WriteLine("\t" + weightedIndex.Index + " -> " + weightedIndex.Weight);
                }
            }
        }
    }