public static void Test1() { com.epl.geometry.Point2D[] pts = new com.epl.geometry.Point2D[10]; for (int i = 0; i < 10; i++) { com.epl.geometry.Point2D pt = new com.epl.geometry.Point2D(); pt.x = i; pt.y = 0; pts[i] = pt; } com.epl.geometry.TreapComparatorForTesting c = new com.epl.geometry.TreapComparatorForTesting(pts); com.epl.geometry.Treap treap = new com.epl.geometry.Treap(); treap.SetComparator(c); int[] nodes = new int[10]; for (int i_1 = 0; i_1 < 10; i_1++) { nodes[i_1] = treap.AddElement(i_1, -1); } for (int i_2 = 1; i_2 < 10; i_2++) { NUnit.Framework.Assert.IsTrue(treap.GetPrev(nodes[i_2]) == nodes[i_2 - 1]); } for (int i_3 = 0; i_3 < 9; i_3++) { NUnit.Framework.Assert.IsTrue(treap.GetNext(nodes[i_3]) == nodes[i_3 + 1]); } treap.DeleteNode(nodes[0], -1); treap.DeleteNode(nodes[2], -1); treap.DeleteNode(nodes[4], -1); treap.DeleteNode(nodes[6], -1); treap.DeleteNode(nodes[8], -1); NUnit.Framework.Assert.IsTrue(treap.GetPrev(nodes[3]) == nodes[1]); NUnit.Framework.Assert.IsTrue(treap.GetPrev(nodes[5]) == nodes[3]); NUnit.Framework.Assert.IsTrue(treap.GetPrev(nodes[7]) == nodes[5]); NUnit.Framework.Assert.IsTrue(treap.GetPrev(nodes[9]) == nodes[7]); NUnit.Framework.Assert.IsTrue(treap.GetNext(nodes[1]) == nodes[3]); NUnit.Framework.Assert.IsTrue(treap.GetNext(nodes[3]) == nodes[5]); NUnit.Framework.Assert.IsTrue(treap.GetNext(nodes[5]) == nodes[7]); NUnit.Framework.Assert.IsTrue(treap.GetNext(nodes[7]) == nodes[9]); }
internal bool NeedsCrackingImpl_() { bool b_needs_cracking = false; if (m_sweep_structure == null) { m_sweep_structure = new com.epl.geometry.Treap(); } com.epl.geometry.AttributeStreamOfInt32 event_q = new com.epl.geometry.AttributeStreamOfInt32(0); event_q.Reserve(m_shape.GetTotalPointCount() + 1); com.epl.geometry.EditShape.VertexIterator iter = m_shape.QueryVertexIterator(); for (int vert = iter.Next(); vert != -1; vert = iter.Next()) { event_q.Add(vert); } System.Diagnostics.Debug.Assert((m_shape.GetTotalPointCount() == event_q.Size())); m_shape.SortVerticesSimpleByY_(event_q, 0, event_q.Size()); event_q.Add(-1); // for termination; // create user indices to store edges that end at vertices. int edge_index_1 = m_shape.CreateUserIndex(); int edge_index_2 = m_shape.CreateUserIndex(); m_sweep_comparator = new com.epl.geometry.SweepComparator(m_shape, m_tolerance, !m_bAllowCoincident); m_sweep_structure.SetComparator(m_sweep_comparator); com.epl.geometry.AttributeStreamOfInt32 swept_edges_to_delete = new com.epl.geometry.AttributeStreamOfInt32(0); com.epl.geometry.AttributeStreamOfInt32 edges_to_insert = new com.epl.geometry.AttributeStreamOfInt32(0); // Go throught the sorted vertices int event_q_index = 0; com.epl.geometry.Point2D cluster_pt = new com.epl.geometry.Point2D(); // sweep-line algorithm: for (int vertex = event_q.Get(event_q_index++); vertex != -1;) { m_shape.GetXY(vertex, cluster_pt); do { int next_vertex = m_shape.GetNextVertex(vertex); int prev_vertex = m_shape.GetPrevVertex(vertex); if (next_vertex != -1 && m_shape.CompareVerticesSimpleY_(vertex, next_vertex) < 0) { edges_to_insert.Add(vertex); edges_to_insert.Add(next_vertex); } if (prev_vertex != -1 && m_shape.CompareVerticesSimpleY_(vertex, prev_vertex) < 0) { edges_to_insert.Add(prev_vertex); edges_to_insert.Add(prev_vertex); } // Continue accumulating current cluster int attached_edge_1 = m_shape.GetUserIndex(vertex, edge_index_1); if (attached_edge_1 != -1) { swept_edges_to_delete.Add(attached_edge_1); m_shape.SetUserIndex(vertex, edge_index_1, -1); } int attached_edge_2 = m_shape.GetUserIndex(vertex, edge_index_2); if (attached_edge_2 != -1) { swept_edges_to_delete.Add(attached_edge_2); m_shape.SetUserIndex(vertex, edge_index_2, -1); } vertex = event_q.Get(event_q_index++); }while (vertex != -1 && m_shape.IsEqualXY(vertex, cluster_pt)); bool b_continuing_segment_chain_optimization = swept_edges_to_delete.Size() == 1 && edges_to_insert.Size() == 2; int new_left = -1; int new_right = -1; // Process the cluster for (int i = 0, n = swept_edges_to_delete.Size(); i < n; i++) { // Find left and right neighbour of the edges that terminate at // the cluster (there will be atmost only one left and one // right). int edge = swept_edges_to_delete.Get(i); int left = m_sweep_structure.GetPrev(edge); if (left != -1 && !swept_edges_to_delete.HasElement(left)) { // Note: // for // some // heavy // cases, // it // could // be // better // to // use // binary // search. System.Diagnostics.Debug.Assert((new_left == -1)); new_left = left; } int right = m_sweep_structure.GetNext(edge); if (right != -1 && !swept_edges_to_delete.HasElement(right)) { System.Diagnostics.Debug.Assert((new_right == -1)); new_right = right; } //#ifdef NDEBUG if (new_left != -1 && new_right != -1) { break; } } //#endif System.Diagnostics.Debug.Assert((new_left == -1 || new_left != new_right)); m_sweep_comparator.SetSweepY(cluster_pt.y, cluster_pt.x); // Delete the edges that terminate at the cluster. for (int i_1 = 0, n = swept_edges_to_delete.Size(); i_1 < n; i_1++) { int edge = swept_edges_to_delete.Get(i_1); m_sweep_structure.DeleteNode(edge, -1); } swept_edges_to_delete.Clear(false); if (!b_continuing_segment_chain_optimization && new_left != -1 && new_right != -1) { if (CheckForIntersections_(new_left, new_right)) { b_needs_cracking = true; m_non_simple_result = m_sweep_comparator.GetResult(); break; } } for (int i_2 = 0, n = edges_to_insert.Size(); i_2 < n; i_2 += 2) { int v = edges_to_insert.Get(i_2); int otherv = edges_to_insert.Get(i_2 + 1); int new_edge_1 = -1; if (b_continuing_segment_chain_optimization) { new_edge_1 = m_sweep_structure.AddElementAtPosition(new_left, new_right, v, true, true, -1); b_continuing_segment_chain_optimization = false; } else { new_edge_1 = m_sweep_structure.AddElement(v, -1); } // the // sweep // structure // consist // of // the // origin // vertices // for // edges. // One // can // always // get // the // other // endpoint // as // the // next // vertex. if (m_sweep_comparator.IntersectionDetected()) { m_non_simple_result = m_sweep_comparator.GetResult(); b_needs_cracking = true; break; } int e_1 = m_shape.GetUserIndex(otherv, edge_index_1); if (e_1 == -1) { m_shape.SetUserIndex(otherv, edge_index_1, new_edge_1); } else { System.Diagnostics.Debug.Assert((m_shape.GetUserIndex(otherv, edge_index_2) == -1)); m_shape.SetUserIndex(otherv, edge_index_2, new_edge_1); } } if (b_needs_cracking) { break; } // Start accumulating new cluster edges_to_insert.ResizePreserveCapacity(0); } m_shape.RemoveUserIndex(edge_index_1); m_shape.RemoveUserIndex(edge_index_2); return(b_needs_cracking); }
internal virtual bool InsertEdge_(int vertex, int reused_node) { com.epl.geometry.Point2D pt_1 = new com.epl.geometry.Point2D(); com.epl.geometry.Point2D pt_2 = new com.epl.geometry.Point2D(); m_shape.GetXY(vertex, pt_1); int next = m_shape.GetNextVertex(vertex); m_shape.GetXY(next, pt_2); bool b_res = false; if (pt_1.y < pt_2.y) { b_res = true; int edge = m_edges.NewEdge(vertex); int aetNode; if (reused_node == -1) { aetNode = m_AET.AddElement(edge, -1); } else { aetNode = reused_node; m_AET.SetElement(aetNode, edge); } int node = m_shape.GetUserIndex(next, m_node_1_user_index); if (node == -1) { m_shape.SetUserIndex(next, m_node_1_user_index, aetNode); } else { m_shape.SetUserIndex(next, m_node_2_user_index, aetNode); } int path = m_shape.GetPathFromVertex(vertex); if (m_shape.GetPathUserIndex(path, m_path_orientation_index) == 0) { m_unknown_nodes.Add(aetNode); } } int prev = m_shape.GetPrevVertex(vertex); m_shape.GetXY(prev, pt_2); if (pt_1.y < pt_2.y) { b_res = true; int edge = m_edges.NewEdge(prev); int aetNode; if (reused_node == -1) { aetNode = m_AET.AddElement(edge, -1); } else { aetNode = reused_node; m_AET.SetElement(aetNode, edge); } int node = m_shape.GetUserIndex(prev, m_node_1_user_index); if (node == -1) { m_shape.SetUserIndex(prev, m_node_1_user_index, aetNode); } else { m_shape.SetUserIndex(prev, m_node_2_user_index, aetNode); } int path = m_shape.GetPathFromVertex(vertex); if (m_shape.GetPathUserIndex(path, m_path_orientation_index) == 0) { m_unknown_nodes.Add(aetNode); } } return(b_res); }