summaryrefslogtreecommitdiff
path: root/src/Poly/Poly_Connect.cxx
blob: 49999fa9390d4d4f11ce9919d24cf3043693dfd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// File:	Poly_Connect.cxx
// Created:	Mon Mar  6 16:30:09 1995
// Author:	Laurent PAINNOT
//		<lpa@metrox>


#include <Poly_Connect.ixx>
#include <Poly_Triangle.hxx>

#include <Standard.hxx>


//=======================================================================
//function : Poly_Connect
//purpose  : 
//=======================================================================
// this structure records one of the edges starting from a node

//typedef struct polyedge {
struct polyedge {
    polyedge* next;             // the next edge in the list
    Standard_Integer nd;    // the second node of the edge
    Standard_Integer nt[2]; // the two adjacent triangles
    Standard_Integer nn[2]; // the two adjacent nodes
    void* operator new(size_t aSize) 
      {return (void*)(Standard::Allocate(aSize));}
//    void  operator delete(void* aNode, size_t aSize) {
    void  operator delete(void* aNode) {
      Standard_Address anAdress = (Standard_Address)aNode;
      Standard::Free(anAdress);
    }
  };

Poly_Connect::Poly_Connect(const Handle(Poly_Triangulation)& T) :
    myTriangulation(T),
    myTriangles(1,T->NbNodes()),
    myAdjacents(1,6*T->NbTriangles())
{
  myTriangles.Init(0);
  myAdjacents.Init(0);
  Standard_Integer nbNodes     = myTriangulation->NbNodes();
  Standard_Integer nbTriangles = myTriangulation->NbTriangles();

  // We first build an array of the list of edges connected to the nodes
 

  
  // create an array to store the edges starting from the vertices

  Standard_Integer i;
  // the last node is not used because edges are stored at the lower node index
  polyedge** edges = new polyedge*[nbNodes];
  for (i = 0; i < nbNodes; i++) edges[i] = 0;


  // loop on the triangles
  Standard_Integer j,k,n[3],n1,n2;
  const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles();

  for (i = 1; i <= nbTriangles; i++) {

    // get the nodes
    triangles(i).Get(n[0],n[1],n[2]);

    // Update the myTriangles array
    myTriangles(n[0]) = i;
    myTriangles(n[1]) = i;
    myTriangles(n[2]) = i;

    // update the edge lists
    for (j = 0; j < 3; j++) {
      k = (j+1) % 3;  // the following node of the edge
      if (n[j] <= n[k]) {
	n1 = n[j];
	n2 = n[k];
      }
      else {
	n1 = n[k];
	n2 = n[j];
      }

      // edge from n1 to n2 with n1 < n2
      // insert in the list of n1

      polyedge* ced = edges[n1];
      while (ced != 0) {
	// the edge already exists
	if (ced->nd == n2)
	  break;
	else
	  ced = ced->next;
      }

      if (ced == 0) {
	// create the edge if not found
	ced = new polyedge;
	ced->next = edges[n1];
	edges[n1] = ced;
	ced->nd = n2;
	ced->nt[0] = i;
	ced->nn[0] = n[3-j-k];  // the third node
	ced->nt[1] = 0;
	ced->nn[1] = 0;
      }
      else {
	// just mark the adjacency if found
	ced->nt[1] = i;
	ced->nn[1] = n[3-j-k];  // the third node
      }
    }
  }


  // now complete the myAdjacents array
  
  Standard_Integer index = 1;
  for (i = 1; i <= nbTriangles; i++) {
    
    // get the nodes
    triangles(i).Get(n[0],n[1],n[2]);

    // fore each edge
    for (j = 0; j < 3; j++) {
      k = (j+1) % 3;  // the following node of the edge
      if (n[j] <= n[k]) {
	n1 = n[j];
	n2 = n[k];
      }
      else {
	n1 = n[k];
	n2 = n[j];
      }

      // edge from n1 to n2 with n1 < n2
      // find in the list of n1

      polyedge* ced = edges[n1];
      while (ced->nd != n2)
	ced = ced->next;

      // Find the adjacent triangle
      Standard_Integer l = 0;
      if (ced->nt[0] == i) l = 1;
      
      myAdjacents(index)   = ced->nt[l];
      myAdjacents(index+3) = ced->nn[l];
      index++;
    }
    index += 3;
  }

  // destroy the edges array
  for (i = 0; i < nbNodes; i++) {
    polyedge* ced = edges[i];
    while (ced != 0) {
      polyedge* tmp = ced->next;
      delete ced;
      ced = tmp;
    }
  }
  delete [] edges;
}

//=======================================================================
//function : Triangles
//purpose  : 
//=======================================================================

void Poly_Connect::Triangles(const Standard_Integer T, 
			     Standard_Integer& t1, 
			     Standard_Integer& t2, 
			     Standard_Integer& t3) const 
{
  Standard_Integer index = 6*(T-1);
  t1 = myAdjacents(index+1);
  t2 = myAdjacents(index+2);
  t3 = myAdjacents(index+3);
}

//=======================================================================
//function : Nodes
//purpose  : 
//=======================================================================

void Poly_Connect::Nodes(const Standard_Integer T, 
			 Standard_Integer& n1, 
			 Standard_Integer& n2, 
			 Standard_Integer& n3) const 
{
  Standard_Integer index = 6*(T-1);
  n1 = myAdjacents(index+4);
  n2 = myAdjacents(index+5);
  n3 = myAdjacents(index+6);
}


//=======================================================================
//function : Initialize
//purpose  : 
//=======================================================================

void Poly_Connect::Initialize(const Standard_Integer N)
{
  mynode = N;
  myfirst = Triangle(N);
  mytr = myfirst;
  mysense = Standard_True;
  mymore = (myfirst != 0);
  if (mymore)
  {
    Standard_Integer i, no[3];
    const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles();
    triangles(myfirst).Get(no[0], no[1], no[2]);
    for (i = 0; i < 3; i++)
      if (no[i] == mynode) break;
    myothernode = no[(i+2)%3];
  }
}

//=======================================================================
//function : Next
//purpose  : 
//=======================================================================

void Poly_Connect::Next()
{
  Standard_Integer i, j;
  Standard_Integer n[3];
  Standard_Integer t[3];
  const Poly_Array1OfTriangle& triangles = myTriangulation->Triangles();
  Triangles(mytr, t[0], t[1], t[2]);
  if (mysense) {
    for (i = 0; i < 3; i++) {
      if (t[i] != 0) {
	triangles(t[i]).Get(n[0], n[1], n[2]);
	for (j = 0; j < 3; j++) {
	  if ((n[j] == mynode) && (n[(j+1)%3] == myothernode)) {
	    mytr = t[i];
	    myothernode = n[(j+2)%3];
	    mymore = (mytr != myfirst);
	    return;
	  }
	}
      }
    }
    // sinon, depart vers la gauche.
    triangles(myfirst).Get(n[0], n[1], n[2]);
    for (i = 0; i < 3; i++)
      if (n[i] == mynode) break;
    myothernode = n[(i+1)%3];
    mysense = Standard_False;
    mytr = myfirst;
    Triangles(mytr, t[0], t[1], t[2]);
  }
  if (!mysense) {
    for (i = 0; i < 3; i++) {
      if (t[i] != 0) {
	triangles(t[i]).Get(n[0], n[1], n[2]);
	for (j = 0; j < 3; j++) {
	  if ((n[j] == mynode) && (n[(j+2)%3] == myothernode)) {
	    mytr = t[i];
	    myothernode = n[(j+1)%3];
	    mymore = Standard_True;
	    return;
	  }
	}
      }
    }
  }
  mymore = Standard_False;
}


//=======================================================================
//function : Value
//purpose  : 
//=======================================================================

Standard_Integer Poly_Connect::Value() const
{
  return mytr;
}