// private AttributeStreamOfInt32 m_orphanVertices; private void _beforeRemoveVertex(int vertex, bool bChangePathFirst) { int vertexlistIndex = m_shape.GetUserIndex(vertex, m_userIndexSortedIndexToVertex); if (m_nextVertexToProcess == vertexlistIndex) { m_nextVertexToProcess = m_sortedVertices.GetNext(m_nextVertexToProcess); } if (m_firstCoincidentVertex == vertexlistIndex) { m_firstCoincidentVertex = m_sortedVertices.GetNext(m_firstCoincidentVertex); } m_sortedVertices.DeleteElement(m_sortedVerticesListIndex, vertexlistIndex); _removeAngleSortInfo(vertex); if (bChangePathFirst) { int path = m_shape.GetPathFromVertex(vertex); if (path != -1) { int first = m_shape.GetFirstVertex(path); if (first == vertex) { int next = m_shape.GetNextVertex(vertex); if (next != vertex) { int p = m_shape.GetPathFromVertex(next); if (p == path) { m_shape.SetFirstVertex_(path, next); return; } else { int prev = m_shape.GetPrevVertex(vertex); if (prev != vertex) { p = m_shape.GetPathFromVertex(prev); if (p == path) { m_shape.SetFirstVertex_(path, prev); return; } } } } m_shape.SetFirstVertex_(path, -1); m_shape.SetLastVertex_(path, -1); } } } }
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); }