0028470: Foundation Classes, NCollection_Array1 - add Resize() method for re-allocati...
[occt.git] / src / Poly / Poly_Connect.cxx
CommitLineData
b311480e 1// Created on: 1995-03-06
2// Created by: Laurent PAINNOT
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17
42cf5bc1 18#include <Poly_Connect.hxx>
19#include <Poly_Triangle.hxx>
20#include <Poly_Triangulation.hxx>
7fd59977 21#include <Standard.hxx>
22
7fd59977 23//=======================================================================
24//function : Poly_Connect
25//purpose :
26//=======================================================================
27// this structure records one of the edges starting from a node
7fd59977 28//typedef struct polyedge {
29struct polyedge {
30 polyedge* next; // the next edge in the list
31 Standard_Integer nd; // the second node of the edge
32 Standard_Integer nt[2]; // the two adjacent triangles
33 Standard_Integer nn[2]; // the two adjacent nodes
1c35b92f 34 DEFINE_STANDARD_ALLOC
7fd59977 35 };
36
37Poly_Connect::Poly_Connect(const Handle(Poly_Triangulation)& T) :
38 myTriangulation(T),
39 myTriangles(1,T->NbNodes()),
40 myAdjacents(1,6*T->NbTriangles())
41{
42 myTriangles.Init(0);
43 myAdjacents.Init(0);
44 Standard_Integer nbNodes = myTriangulation->NbNodes();
45 Standard_Integer nbTriangles = myTriangulation->NbTriangles();
46
47 // We first build an array of the list of edges connected to the nodes
48
49
50
51 // create an array to store the edges starting from the vertices
52
53 Standard_Integer i;
54 // the last node is not used because edges are stored at the lower node index
55 polyedge** edges = new polyedge*[nbNodes];
56 for (i = 0; i < nbNodes; i++) edges[i] = 0;
57
58
59 // loop on the triangles
60 Standard_Integer j,k,n[3],n1,n2;
61 const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles();
62
63 for (i = 1; i <= nbTriangles; i++) {
64
65 // get the nodes
66 triangles(i).Get(n[0],n[1],n[2]);
67
68 // Update the myTriangles array
69 myTriangles(n[0]) = i;
70 myTriangles(n[1]) = i;
71 myTriangles(n[2]) = i;
72
73 // update the edge lists
74 for (j = 0; j < 3; j++) {
75 k = (j+1) % 3; // the following node of the edge
76 if (n[j] <= n[k]) {
77 n1 = n[j];
78 n2 = n[k];
79 }
80 else {
81 n1 = n[k];
82 n2 = n[j];
83 }
84
85 // edge from n1 to n2 with n1 < n2
86 // insert in the list of n1
87
88 polyedge* ced = edges[n1];
89 while (ced != 0) {
90 // the edge already exists
91 if (ced->nd == n2)
92 break;
93 else
94 ced = ced->next;
95 }
96
97 if (ced == 0) {
98 // create the edge if not found
99 ced = new polyedge;
100 ced->next = edges[n1];
101 edges[n1] = ced;
102 ced->nd = n2;
103 ced->nt[0] = i;
104 ced->nn[0] = n[3-j-k]; // the third node
105 ced->nt[1] = 0;
106 ced->nn[1] = 0;
107 }
108 else {
109 // just mark the adjacency if found
110 ced->nt[1] = i;
111 ced->nn[1] = n[3-j-k]; // the third node
112 }
113 }
114 }
115
116
117 // now complete the myAdjacents array
118
119 Standard_Integer index = 1;
120 for (i = 1; i <= nbTriangles; i++) {
121
122 // get the nodes
123 triangles(i).Get(n[0],n[1],n[2]);
124
125 // fore each edge
126 for (j = 0; j < 3; j++) {
127 k = (j+1) % 3; // the following node of the edge
128 if (n[j] <= n[k]) {
129 n1 = n[j];
130 n2 = n[k];
131 }
132 else {
133 n1 = n[k];
134 n2 = n[j];
135 }
136
137 // edge from n1 to n2 with n1 < n2
138 // find in the list of n1
139
140 polyedge* ced = edges[n1];
141 while (ced->nd != n2)
142 ced = ced->next;
143
144 // Find the adjacent triangle
145 Standard_Integer l = 0;
146 if (ced->nt[0] == i) l = 1;
147
148 myAdjacents(index) = ced->nt[l];
149 myAdjacents(index+3) = ced->nn[l];
150 index++;
151 }
152 index += 3;
153 }
154
155 // destroy the edges array
156 for (i = 0; i < nbNodes; i++) {
157 polyedge* ced = edges[i];
158 while (ced != 0) {
159 polyedge* tmp = ced->next;
160 delete ced;
161 ced = tmp;
162 }
163 }
164 delete [] edges;
165}
166
167//=======================================================================
168//function : Triangles
169//purpose :
170//=======================================================================
171
172void Poly_Connect::Triangles(const Standard_Integer T,
173 Standard_Integer& t1,
174 Standard_Integer& t2,
175 Standard_Integer& t3) const
176{
177 Standard_Integer index = 6*(T-1);
178 t1 = myAdjacents(index+1);
179 t2 = myAdjacents(index+2);
180 t3 = myAdjacents(index+3);
181}
182
183//=======================================================================
184//function : Nodes
185//purpose :
186//=======================================================================
187
188void Poly_Connect::Nodes(const Standard_Integer T,
189 Standard_Integer& n1,
190 Standard_Integer& n2,
191 Standard_Integer& n3) const
192{
193 Standard_Integer index = 6*(T-1);
194 n1 = myAdjacents(index+4);
195 n2 = myAdjacents(index+5);
196 n3 = myAdjacents(index+6);
197}
198
199
200//=======================================================================
201//function : Initialize
202//purpose :
203//=======================================================================
204
205void Poly_Connect::Initialize(const Standard_Integer N)
206{
207 mynode = N;
208 myfirst = Triangle(N);
209 mytr = myfirst;
7fd59977 210 mysense = Standard_True;
446e11f3
A
211 mymore = (myfirst != 0);
212 if (mymore)
213 {
214 Standard_Integer i, no[3];
215 const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles();
216 triangles(myfirst).Get(no[0], no[1], no[2]);
217 for (i = 0; i < 3; i++)
218 if (no[i] == mynode) break;
219 myothernode = no[(i+2)%3];
220 }
7fd59977 221}
222
223//=======================================================================
224//function : Next
225//purpose :
226//=======================================================================
227
228void Poly_Connect::Next()
229{
230 Standard_Integer i, j;
9dfd7287
RK
231 Standard_Integer n[3];
232 Standard_Integer t[3];
7fd59977 233 const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles();
234 Triangles(mytr, t[0], t[1], t[2]);
235 if (mysense) {
236 for (i = 0; i < 3; i++) {
237 if (t[i] != 0) {
238 triangles(t[i]).Get(n[0], n[1], n[2]);
239 for (j = 0; j < 3; j++) {
240 if ((n[j] == mynode) && (n[(j+1)%3] == myothernode)) {
241 mytr = t[i];
242 myothernode = n[(j+2)%3];
243 mymore = (mytr != myfirst);
244 return;
245 }
246 }
247 }
248 }
249 // sinon, depart vers la gauche.
250 triangles(myfirst).Get(n[0], n[1], n[2]);
251 for (i = 0; i < 3; i++)
252 if (n[i] == mynode) break;
253 myothernode = n[(i+1)%3];
254 mysense = Standard_False;
255 mytr = myfirst;
256 Triangles(mytr, t[0], t[1], t[2]);
257 }
258 if (!mysense) {
259 for (i = 0; i < 3; i++) {
260 if (t[i] != 0) {
261 triangles(t[i]).Get(n[0], n[1], n[2]);
262 for (j = 0; j < 3; j++) {
263 if ((n[j] == mynode) && (n[(j+2)%3] == myothernode)) {
264 mytr = t[i];
265 myothernode = n[(j+1)%3];
266 mymore = Standard_True;
267 return;
268 }
269 }
270 }
271 }
272 }
273 mymore = Standard_False;
274}
275
276
277//=======================================================================
278//function : Value
279//purpose :
280//=======================================================================
281
282Standard_Integer Poly_Connect::Value() const
283{
284 return mytr;
285}