Пример #1
0
        //    *
        //	 * Executes the Constrained Delaunay Triangulation algorithm
        //	 * @param output A vector of index where is outputed the resulting triangle indexes
        //	 * @param outputVertices A vector of vertices where is outputed the resulting triangle vertices
        //	 * @exception Ogre::InvalidStateException Either shape or multishape or segment list must be defined
        //
        //void triangulate(ref List<int>& output, ref List<Vector2>& outputVertices) const;
        public void triangulate(std_vector <int> output, PointList outputVertices)
        {
            if (mShapeToTriangulate == null && mMultiShapeToTriangulate == null && mSegmentListToTriangulate == null)
            {
                throw new NullReferenceException("Ogre::Exception::ERR_INVALID_STATE," + "Either shape or multishape or segment list must be defined!" + ",  Procedural::Triangulator::triangulate(std::vector<int>&, PointList&)");
            }
            //Ogre::Timer mTimer;
            //Mogre.Timer mTimer = new Timer();
            //mTimer.Reset();
            DelaunayTriangleBuffer dtb = new std_list <Triangle>();
            // Do the Delaunay triangulation
            std_vector <int> segmentListIndices = new std_vector <int>();

            if (mShapeToTriangulate != null)
            {
                outputVertices = new std_vector <Vector2>(mShapeToTriangulate.getPoints());
                for (int i = 0; i < mShapeToTriangulate.getSegCount(); ++i)
                {
                    segmentListIndices.push_back(i);
                    segmentListIndices.push_back(mShapeToTriangulate.getBoundedIndex(i + 1));
                }
            }
            else if (mMultiShapeToTriangulate != null)
            {
                outputVertices = new std_vector <Vector2>(mMultiShapeToTriangulate.getPoints());
                int index = 0;
                for (int i = 0; i < mMultiShapeToTriangulate.getShapeCount(); ++i)
                {
                    Shape shape = mMultiShapeToTriangulate.getShape((uint)i);
                    for (int j = 0; j < shape.getSegCount(); j++)
                    {
                        segmentListIndices.push_back(index + j);
                        segmentListIndices.push_back(index + shape.getBoundedIndex(j + 1));
                    }
                    index += shape.getSegCount();
                }
            }
            else if (mSegmentListToTriangulate != null)
            {
                //std_map<Vector2, int, Vector2Comparator> backMap;
                std_map <Vector2, int> backMap = new std_map <Vector2, int>(new Vector2Comparator());
                //for (std::vector<Segment2D>::iterator it = mSegmentListToTriangulate->begin(); it!= mSegmentListToTriangulate->end(); it++)
                foreach (var it in mSegmentListToTriangulate)
                {
                    if ((it.mA - it.mB).SquaredLength < 1e-6)
                    {
                        continue;
                    }

                    //std::map<Vector2, int, Vector2Comparator>::iterator it2 = backMap.find(it->mA);
                    int it2_pos = backMap.find(it.mA);
                    //if (it2 != backMap.end())
                    if (it2_pos != -1)
                    {
                        //segmentListIndices.push_back(it2->second);
                        segmentListIndices.push_back(backMap[it.mA]);
                    }
                    else
                    {
                        //backMap[it->mA] = outputVertices.size();
                        backMap.insert(it.mA, outputVertices.size());
                        segmentListIndices.push_back(outputVertices.size());
                        outputVertices.push_back(it.mA);
                    }

                    //it2 = backMap.find(it.mB);
                    it2_pos = backMap.find(it.mB);
                    //if (it2 != backMap.end())
                    if (it2_pos != -1)
                    {
                        //segmentListIndices.push_back(it2.second);
                        segmentListIndices.push_back(backMap[it.mB]);
                    }
                    else
                    {
                        //backMap[it->mB] = outputVertices.size();
                        backMap.insert(it.mB, outputVertices.size());
                        segmentListIndices.push_back(outputVertices.size());
                        outputVertices.push_back(it.mB);
                    }
                }

                if (mManualSuperTriangle != null)
                {
                    Triangle superTriangle = new Triangle(outputVertices);
                    for (int i = 0; i < 3; i++)
                    {
                        //std::map<Vector2, int, Vector2Comparator>::iterator it = backMap.find(mManualSuperTriangle->mPoints[i]);
                        int it_pos = backMap.find(mManualSuperTriangle.mPoints[i]);
                        //if (it != backMap.end())
                        if (it_pos != -1)
                        {
                            //segmentListIndices.push_back(it->second);
                            //superTriangle.i[i] = it->second;
                            superTriangle.i[i] = backMap[mManualSuperTriangle.mPoints[i]];
                        }
                        else
                        {
                            //backMap[mManualSuperTriangle->mPoints[i]] = outputVertices.size();
                            backMap.insert(mManualSuperTriangle.mPoints[i], outputVertices.size());
                            //segmentListIndices.push_back(outputVertices.size());
                            superTriangle.i[i] = outputVertices.size();
                            outputVertices.push_back(mManualSuperTriangle.mPoints[i]);
                        }
                    }

                    dtb.push_back(superTriangle);
                }
            }
            //Utils::log("Triangulator preparation : " + StringConverter::toString(mTimer.getMicroseconds() / 1000.0f) + " ms");
            delaunay(outputVertices, ref dtb);
            //Utils::log("Triangulator delaunay : " + StringConverter::toString(mTimer.getMicroseconds() / 1000.0f) + " ms");
            // Add contraints
            _addConstraints(ref dtb, outputVertices, segmentListIndices);
            //Utils::log("Triangulator constraints : " + StringConverter::toString(mTimer.getMicroseconds() / 1000.0f) + " ms");
            //Outputs index buffer
            //for (DelaunayTriangleBuffer::iterator it = dtb.begin(); it!=dtb.end(); ++it)
            foreach (var it in dtb)
            {
                if (!it.isDegenerate())
                {
                    output.push_back(it.i[0]);
                    output.push_back(it.i[1]);
                    output.push_back(it.i[2]);
                }
            }
            // Remove super triangle
            if (mRemoveOutside)
            {
                outputVertices.pop_back();
                outputVertices.pop_back();
                outputVertices.pop_back();
            }
            //Utils::log("Triangulator output : " + StringConverter::toString(mTimer.getMicroseconds() / 1000.0f) + " ms");
        }