private bool _processBunch() { bool bModified = false; int iter = 0; com.epl.geometry.Point2D ptCenter = new com.epl.geometry.Point2D(); while (true) { m_dbgCounter++; // only for debugging iter++; // _ASSERT(iter < 10); if (m_bunchEdgeEndPoints == null) { m_bunchEdgeEndPoints = new com.epl.geometry.AttributeStreamOfInt32(0); m_bunchEdgeCenterPoints = new com.epl.geometry.AttributeStreamOfInt32(0); m_bunchEdgeIndices = new com.epl.geometry.AttributeStreamOfInt32(0); } else { m_bunchEdgeEndPoints.Clear(false); m_bunchEdgeCenterPoints.Clear(false); m_bunchEdgeIndices.Clear(false); } int currentVertex = m_firstCoincidentVertex; int index = 0; bool bFirst = true; while (currentVertex != m_nextVertexToProcess) { int v = m_sortedVertices.GetData(currentVertex); { // debug com.epl.geometry.Point2D pt = new com.epl.geometry.Point2D(); m_shape.GetXY(v, pt); double y = pt.x; } if (bFirst) { m_shape.GetXY(v, ptCenter); bFirst = false; } int vertP = m_shape.GetPrevVertex(v); int vertN = m_shape.GetNextVertex(v); // _ASSERT(vertP != vertN || m_shape.getPrevVertex(vertN) == v // && m_shape.getNextVertex(vertP) == v); int id = m_shape.GetUserIndex(vertP, m_userIndexSortedAngleIndexToVertex); if (id != unchecked ((int)(0xdeadbeef))) { // avoid adding a point twice // _ASSERT(id == -1); m_bunchEdgeEndPoints.Add(vertP); m_shape.SetUserIndex(vertP, m_userIndexSortedAngleIndexToVertex, unchecked ((int)(0xdeadbeef))); // mark // that // it // has // been // already // added m_bunchEdgeCenterPoints.Add(v); m_bunchEdgeIndices.Add(index++); } int id2 = m_shape.GetUserIndex(vertN, m_userIndexSortedAngleIndexToVertex); if (id2 != unchecked ((int)(0xdeadbeef))) { // avoid adding a point twice // _ASSERT(id2 == -1); m_bunchEdgeEndPoints.Add(vertN); m_shape.SetUserIndex(vertN, m_userIndexSortedAngleIndexToVertex, unchecked ((int)(0xdeadbeef))); // mark // that // it // has // been // already // added m_bunchEdgeCenterPoints.Add(v); m_bunchEdgeIndices.Add(index++); } currentVertex = m_sortedVertices.GetNext(currentVertex); } if (m_bunchEdgeEndPoints.Size() < 2) { break; } // Sort the bunch edpoints by angle (angle between the axis x and // the edge, connecting the endpoint with the bunch center) m_bunchEdgeIndices.Sort(0, m_bunchEdgeIndices.Size(), new com.epl.geometry.Simplificator.SimplificatorAngleComparer(this)); // SORTDYNAMICARRAYEX(m_bunchEdgeIndices, int, 0, // m_bunchEdgeIndices.size(), SimplificatorAngleComparer, this); for (int i = 0, n = m_bunchEdgeIndices.Size(); i < n; i++) { int indexL = m_bunchEdgeIndices.Get(i); int vertex = m_bunchEdgeEndPoints.Get(indexL); m_shape.SetUserIndex(vertex, m_userIndexSortedAngleIndexToVertex, i); { // rember the // sort by angle // order // debug com.epl.geometry.Point2D pt = new com.epl.geometry.Point2D(); m_shape.GetXY(vertex, pt); double y = pt.x; } } bool bCrossOverResolved = _processCrossOvers(ptCenter); // see of // there // are // crossing // over // edges. for (int i_1 = 0, n = m_bunchEdgeIndices.Size(); i_1 < n; i_1++) { int indexL = m_bunchEdgeIndices.Get(i_1); if (indexL == -1) { continue; } int vertex = m_bunchEdgeEndPoints.Get(indexL); m_shape.SetUserIndex(vertex, m_userIndexSortedAngleIndexToVertex, -1); } // remove // mapping if (bCrossOverResolved) { bModified = true; continue; } break; } return(bModified); }
private bool _simplify() { if (m_shape.GetGeometryType(m_geometry) == com.epl.geometry.Geometry.Type.Polygon.Value() && m_shape.GetFillRule(m_geometry) == com.epl.geometry.Polygon.FillRule.enumFillRuleWinding) { com.epl.geometry.TopologicalOperations ops = new com.epl.geometry.TopologicalOperations(); ops.PlanarSimplifyNoCrackingAndCluster(m_fixSelfTangency, m_shape, m_geometry, m_progressTracker); System.Diagnostics.Debug.Assert((m_shape.GetFillRule(m_geometry) == com.epl.geometry.Polygon.FillRule.enumFillRuleOddEven)); } bool bChanged = false; bool bNeedWindingRepeat = true; bool bWinding = false; m_userIndexSortedIndexToVertex = -1; m_userIndexSortedAngleIndexToVertex = -1; int pointCount = m_shape.GetPointCount(m_geometry); // Sort vertices lexicographically // Firstly copy allvertices to an array. com.epl.geometry.AttributeStreamOfInt32 verticesSorter = new com.epl.geometry.AttributeStreamOfInt32(0); verticesSorter.Reserve(pointCount); for (int path = m_shape.GetFirstPath(m_geometry); path != -1; path = m_shape.GetNextPath(path)) { int vertex = m_shape.GetFirstVertex(path); for (int index = 0, n = m_shape.GetPathSize(path); index < n; index++) { verticesSorter.Add(vertex); vertex = m_shape.GetNextVertex(vertex); } } // Sort verticesSorter.Sort(0, pointCount, new com.epl.geometry.Simplificator.SimplificatorVertexComparer(this)); // SORTDYNAMICARRAYEX(verticesSorter, int, 0, pointCount, // SimplificatorVertexComparer, this); // Copy sorted vertices to the m_sortedVertices list. Make a mapping // from the edit shape vertices to the sorted vertices. m_userIndexSortedIndexToVertex = m_shape.CreateUserIndex(); // this index // is used // to map // from edit // shape // vertex to // the // m_sortedVertices // list m_sortedVertices = new com.epl.geometry.IndexMultiDCList(); m_sortedVerticesListIndex = m_sortedVertices.CreateList(0); for (int i = 0; i < pointCount; i++) { int vertex = verticesSorter.Get(i); { // debug com.epl.geometry.Point2D pt = new com.epl.geometry.Point2D(); m_shape.GetXY(vertex, pt); // for debugging double y = pt.x; } int vertexlistIndex = m_sortedVertices.AddElement(m_sortedVerticesListIndex, vertex); m_shape.SetUserIndex(vertex, m_userIndexSortedIndexToVertex, vertexlistIndex); } // remember the sorted list element on the // vertex. // When we remove a vertex, we also remove associated sorted list // element. m_userIndexSortedAngleIndexToVertex = m_shape.CreateUserIndex(); // create // additional // list // to // store // angular // sort // mapping. m_nextVertexToProcess = -1; if (_cleanupSpikes()) { // cleanup any spikes on the polygon. bChanged = true; } // External iteration loop for the simplificator. // ST. I am not sure if it actually needs this loop. TODO: figure this // out. while (bNeedWindingRepeat) { bNeedWindingRepeat = false; int max_iter = m_shape.GetPointCount(m_geometry) + 10 > 30 ? 1000 : (m_shape.GetPointCount(m_geometry) + 10) * (m_shape.GetPointCount(m_geometry) + 10); // Simplify polygon int iRepeatNum = 0; bool bNeedRepeat = false; do { // Internal iteration loop for the simplificator. // ST. I am not sure if it actually needs this loop. TODO: figure // this out. // while (bNeedRepeat); bNeedRepeat = false; bool bVertexRecheck = false; m_firstCoincidentVertex = -1; int coincidentCount = 0; com.epl.geometry.Point2D ptFirst = new com.epl.geometry.Point2D(); com.epl.geometry.Point2D pt = new com.epl.geometry.Point2D(); // Main loop of the simplificator. Go through the vertices and // for those that have same coordinates, for (int vlistindex = m_sortedVertices.GetFirst(m_sortedVerticesListIndex); vlistindex != com.epl.geometry.IndexMultiDCList.NullNode();) { int vertex = m_sortedVertices.GetData(vlistindex); { // debug // Point2D pt = new Point2D(); m_shape.GetXY(vertex, pt); double d = pt.x; } if (m_firstCoincidentVertex != -1) { // Point2D pt = new Point2D(); m_shape.GetXY(vertex, pt); if (ptFirst.IsEqual(pt)) { coincidentCount++; } else { ptFirst.SetCoords(pt); m_nextVertexToProcess = vlistindex; // we remeber the // next index in // the member // variable to // allow it to // be updated if // a vertex is // removed // inside of the // _ProcessBunch. if (coincidentCount > 0) { bool result = _processBunch(); // process a // bunch of // coinciding // vertices if (result) { // something has changed. // Note that ProcessBunch may // change m_nextVertexToProcess // and m_firstCoincidentVertex. bNeedRepeat = true; if (m_nextVertexToProcess != com.epl.geometry.IndexMultiDCList.NullNode()) { int v = m_sortedVertices.GetData(m_nextVertexToProcess); m_shape.GetXY(v, ptFirst); } } } vlistindex = m_nextVertexToProcess; m_firstCoincidentVertex = vlistindex; coincidentCount = 0; } } else { m_firstCoincidentVertex = vlistindex; m_shape.GetXY(m_sortedVertices.GetData(vlistindex), ptFirst); coincidentCount = 0; } if (vlistindex != -1) { //vlistindex can be set to -1 after ProcessBunch call above vlistindex = m_sortedVertices.GetNext(vlistindex); } } m_nextVertexToProcess = -1; if (coincidentCount > 0) { bool result = _processBunch(); if (result) { bNeedRepeat = true; } } if (iRepeatNum++ > 10) { throw com.epl.geometry.GeometryException.GeometryInternalError(); } if (bNeedRepeat) { _fixOrphanVertices(); } // fix broken structure of the shape if (_cleanupSpikes()) { bNeedRepeat = true; } bNeedWindingRepeat |= bNeedRepeat && bWinding; bChanged |= bNeedRepeat; }while (bNeedRepeat); } // while (bNeedWindingRepeat) // Now process rings. Fix ring orientation and determine rings that need // to be deleted. m_shape.RemoveUserIndex(m_userIndexSortedIndexToVertex); m_shape.RemoveUserIndex(m_userIndexSortedAngleIndexToVertex); bChanged |= com.epl.geometry.RingOrientationFixer.Execute(m_shape, m_geometry, m_sortedVertices, m_fixSelfTangency); return(bChanged); }
internal virtual bool FixRingOrientation_() { bool bFound = false; if (m_fixSelfTangency) { bFound = FixRingSelfTangency_(); } if (m_shape.GetPathCount(m_geometry) == 1) { int path = m_shape.GetFirstPath(m_geometry); double area = m_shape.GetRingArea(path); m_shape.SetExterior(path, true); if (area < 0) { int first = m_shape.GetFirstVertex(path); m_shape.ReverseRingInternal_(first); m_shape.SetLastVertex_(path, m_shape.GetPrevVertex(first)); // fix // last // after // the // reverse return(true); } return(false); } m_path_orientation_index = m_shape.CreatePathUserIndex(); // used to // store // discovered // orientation // (3 - // extrior, // 2 - // interior) m_path_parentage_index = m_shape.CreatePathUserIndex(); // used to // resolve OGC // order for (int path_1 = m_shape.GetFirstPath(m_geometry); path_1 != -1; path_1 = m_shape.GetNextPath(path_1)) { m_shape.SetPathUserIndex(path_1, m_path_orientation_index, 0); m_shape.SetPathUserIndex(path_1, m_path_parentage_index, -1); } com.epl.geometry.AttributeStreamOfInt32 bunch = new com.epl.geometry.AttributeStreamOfInt32(0); m_y_scanline = com.epl.geometry.NumberUtils.TheNaN; com.epl.geometry.Point2D pt = new com.epl.geometry.Point2D(); m_unknown_ring_orientation_count = m_shape.GetPathCount(m_geometry); m_node_1_user_index = m_shape.CreateUserIndex(); m_node_2_user_index = m_shape.CreateUserIndex(); for (int ivertex = m_sorted_vertices.GetFirst(m_sorted_vertices.GetFirstList()); ivertex != -1; ivertex = m_sorted_vertices.GetNext(ivertex)) { int vertex = m_sorted_vertices.GetData(ivertex); m_shape.GetXY(vertex, pt); if (pt.y != m_y_scanline && bunch.Size() != 0) { bFound |= ProcessBunchForRingOrientationTest_(bunch); m_sweep_comparator.Reset(); bunch.Clear(false); } bunch.Add(vertex); // all vertices that have same y are added to the // bunch m_y_scanline = pt.y; if (m_unknown_ring_orientation_count == 0) { break; } } if (m_unknown_ring_orientation_count > 0) { bFound |= ProcessBunchForRingOrientationTest_(bunch); bunch.Clear(false); } m_shape.RemoveUserIndex(m_node_1_user_index); m_shape.RemoveUserIndex(m_node_2_user_index); // dbg_verify_ring_orientation_();//debug for (int path_2 = m_shape.GetFirstPath(m_geometry); path_2 != -1;) { if (m_shape.GetPathUserIndex(path_2, m_path_orientation_index) == 3) { // exterior m_shape.SetExterior(path_2, true); int afterPath = path_2; for (int nextHole = m_shape.GetPathUserIndex(path_2, m_path_parentage_index); nextHole != -1;) { int p = m_shape.GetPathUserIndex(nextHole, m_path_parentage_index); m_shape.MovePath(m_geometry, m_shape.GetNextPath(afterPath), nextHole); afterPath = nextHole; nextHole = p; } path_2 = m_shape.GetNextPath(afterPath); } else { m_shape.SetExterior(path_2, false); path_2 = m_shape.GetNextPath(path_2); } } m_shape.RemovePathUserIndex(m_path_orientation_index); m_shape.RemovePathUserIndex(m_path_parentage_index); return(bFound); }