953555c5c38499fe9995adaa245b80b174dc79f7
[occt.git] / src / HLRAlgo / HLRAlgo_PolyInternalData.cxx
1 // Created on: 1995-07-11
2 // Created by: Christophe MARION
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <Standard_Stream.hxx>
23 #include <HLRAlgo_PolyInternalData.ixx>
24 #include <HLRAlgo_BiPoint.hxx>
25
26 #define Tri1Node1 ((Standard_Integer*)Tri1Indices)[0]
27 #define Tri1Node2 ((Standard_Integer*)Tri1Indices)[1]
28 #define Tri1Node3 ((Standard_Integer*)Tri1Indices)[2]
29 #define Tri1Flags ((Standard_Boolean*)Tri1Indices)[3]
30
31 #define Tri2Node1 ((Standard_Integer*)Tri2Indices)[0]
32 #define Tri2Node2 ((Standard_Integer*)Tri2Indices)[1]
33 #define Tri2Node3 ((Standard_Integer*)Tri2Indices)[2]
34 #define Tri2Flags ((Standard_Boolean*)Tri2Indices)[3]
35
36 #define Seg1LstSg1 ((Standard_Integer*)Seg1Indices)[0]
37 #define Seg1LstSg2 ((Standard_Integer*)Seg1Indices)[1]
38 #define Seg1NxtSg1 ((Standard_Integer*)Seg1Indices)[2]
39 #define Seg1NxtSg2 ((Standard_Integer*)Seg1Indices)[3]
40 #define Seg1Conex1 ((Standard_Integer*)Seg1Indices)[4]
41 #define Seg1Conex2 ((Standard_Integer*)Seg1Indices)[5]
42
43 #define Seg2LstSg1 ((Standard_Integer*)Seg2Indices)[0]
44 #define Seg2LstSg2 ((Standard_Integer*)Seg2Indices)[1]
45 #define Seg2NxtSg1 ((Standard_Integer*)Seg2Indices)[2]
46 #define Seg2NxtSg2 ((Standard_Integer*)Seg2Indices)[3]
47 #define Seg2Conex1 ((Standard_Integer*)Seg2Indices)[4]
48 #define Seg2Conex2 ((Standard_Integer*)Seg2Indices)[5]
49
50 #define Nod1NdSg ((Standard_Integer*)Nod1Indices)[0]
51 #define Nod1Flag ((Standard_Boolean*)Nod1Indices)[1]
52 #define Nod1Edg1 ((Standard_Boolean*)Nod1Indices)[2]
53 #define Nod1Edg2 ((Standard_Boolean*)Nod1Indices)[3]
54
55 #define Nod1PntX ((Standard_Real*)Nod1RValues)[ 0]
56 #define Nod1PntY ((Standard_Real*)Nod1RValues)[ 1]
57 #define Nod1PntZ ((Standard_Real*)Nod1RValues)[ 2]
58 #define Nod1PntU ((Standard_Real*)Nod1RValues)[ 3]
59 #define Nod1PntV ((Standard_Real*)Nod1RValues)[ 4]
60 #define Nod1NrmX ((Standard_Real*)Nod1RValues)[ 5]
61 #define Nod1NrmY ((Standard_Real*)Nod1RValues)[ 6]
62 #define Nod1NrmZ ((Standard_Real*)Nod1RValues)[ 7]
63 #define Nod1PCu1 ((Standard_Real*)Nod1RValues)[ 8]
64 #define Nod1PCu2 ((Standard_Real*)Nod1RValues)[ 9]
65 #define Nod1Scal ((Standard_Real*)Nod1RValues)[10]
66
67 #define Nod2NdSg ((Standard_Integer*)Nod2Indices)[0]
68 #define Nod2Flag ((Standard_Boolean*)Nod2Indices)[1]
69 #define Nod2Edg1 ((Standard_Boolean*)Nod2Indices)[2]
70 #define Nod2Edg2 ((Standard_Boolean*)Nod2Indices)[3]
71
72 #define Nod2PntX ((Standard_Real*)Nod2RValues)[ 0]
73 #define Nod2PntY ((Standard_Real*)Nod2RValues)[ 1]
74 #define Nod2PntZ ((Standard_Real*)Nod2RValues)[ 2]
75 #define Nod2PntU ((Standard_Real*)Nod2RValues)[ 3]
76 #define Nod2PntV ((Standard_Real*)Nod2RValues)[ 4]
77 #define Nod2NrmX ((Standard_Real*)Nod2RValues)[ 5]
78 #define Nod2NrmY ((Standard_Real*)Nod2RValues)[ 6]
79 #define Nod2NrmZ ((Standard_Real*)Nod2RValues)[ 7]
80 #define Nod2PCu1 ((Standard_Real*)Nod2RValues)[ 8]
81 #define Nod2PCu2 ((Standard_Real*)Nod2RValues)[ 9]
82 #define Nod2Scal ((Standard_Real*)Nod2RValues)[10]
83
84 #define Nod3NdSg ((Standard_Integer*)Nod3Indices)[0]
85 #define Nod3Flag ((Standard_Boolean*)Nod3Indices)[1]
86 #define Nod3Edg1 ((Standard_Boolean*)Nod3Indices)[2]
87 #define Nod3Edg2 ((Standard_Boolean*)Nod3Indices)[3]
88
89 #define Nod3PntX ((Standard_Real*)Nod3RValues)[ 0]
90 #define Nod3PntY ((Standard_Real*)Nod3RValues)[ 1]
91 #define Nod3PntZ ((Standard_Real*)Nod3RValues)[ 2]
92 #define Nod3PntU ((Standard_Real*)Nod3RValues)[ 3]
93 #define Nod3PntV ((Standard_Real*)Nod3RValues)[ 4]
94 #define Nod3NrmX ((Standard_Real*)Nod3RValues)[ 5]
95 #define Nod3NrmY ((Standard_Real*)Nod3RValues)[ 6]
96 #define Nod3NrmZ ((Standard_Real*)Nod3RValues)[ 7]
97 #define Nod3PCu1 ((Standard_Real*)Nod3RValues)[ 8]
98 #define Nod3PCu2 ((Standard_Real*)Nod3RValues)[ 9]
99 #define Nod3Scal ((Standard_Real*)Nod3RValues)[10]
100
101 #define Nod4NdSg ((Standard_Integer*)Nod4Indices)[0]
102 #define Nod4Flag ((Standard_Boolean*)Nod4Indices)[1]
103 #define Nod4Edg1 ((Standard_Boolean*)Nod4Indices)[2]
104 #define Nod4Edg2 ((Standard_Boolean*)Nod4Indices)[3]
105
106 #define Nod4PntX ((Standard_Real*)Nod4RValues)[ 0]
107 #define Nod4PntY ((Standard_Real*)Nod4RValues)[ 1]
108 #define Nod4PntZ ((Standard_Real*)Nod4RValues)[ 2]
109 #define Nod4PntU ((Standard_Real*)Nod4RValues)[ 3]
110 #define Nod4PntV ((Standard_Real*)Nod4RValues)[ 4]
111 #define Nod4NrmX ((Standard_Real*)Nod4RValues)[ 5]
112 #define Nod4NrmY ((Standard_Real*)Nod4RValues)[ 6]
113 #define Nod4NrmZ ((Standard_Real*)Nod4RValues)[ 7]
114 #define Nod4PCu1 ((Standard_Real*)Nod4RValues)[ 8]
115 #define Nod4PCu2 ((Standard_Real*)Nod4RValues)[ 9]
116 #define Nod4Scal ((Standard_Real*)Nod4RValues)[10]
117 #ifdef DEB
118 static Standard_Integer TRACE = Standard_False;
119 static Standard_Integer ERROR = Standard_False;
120 #endif
121 //=======================================================================
122 //function : PolyInternalData
123 //purpose  : 
124 //=======================================================================
125
126 HLRAlgo_PolyInternalData::HLRAlgo_PolyInternalData
127 (const Standard_Integer nbNod,
128  const Standard_Integer nbTri) :
129  myNbTData(nbTri),
130  myNbPISeg(0),
131  myNbPINod(nbNod),
132  myMxTData(nbTri),
133  myMxPINod(nbNod),
134  myIntOutL(Standard_False),
135  myPlanar(Standard_False)
136 {
137   Standard_Integer i;
138   myMxPISeg = 2 + (3 * nbTri + nbNod) / 2;
139   myTData = new HLRAlgo_HArray1OfTData(0,myMxTData);
140   myPISeg = new HLRAlgo_HArray1OfPISeg(0,myMxPISeg);
141   myPINod = new HLRAlgo_HArray1OfPINod(0,myMxPINod);
142   
143   HLRAlgo_Array1OfPINod& PINod = myPINod->ChangeArray1();
144   Handle(HLRAlgo_PolyInternalNode)* NN = &(PINod.ChangeValue(1));
145 //  Standard_Address Nod1Indices;
146   
147   for (i = 1; i <= myMxPINod; i++) {
148     *NN = new HLRAlgo_PolyInternalNode();
149     NN++;
150   }
151 }
152
153 //=======================================================================
154 //function : UpdateLinks
155 //purpose  : 
156 //=======================================================================
157
158 void HLRAlgo_PolyInternalData::UpdateLinks
159 (Standard_Address& TData,
160  Standard_Address& PISeg,
161  Standard_Address& PINod)
162 {
163   Standard_Integer i,n1,n2;
164   Standard_Integer find,iiii,icsv;
165   Standard_Address A1,A2,A3,Nod1Indices,Nod2Indices,Seg2Indices,Tri2Indices;
166   Standard_Boolean newSeg = Standard_False;
167   HLRAlgo_TriangleData* TD =
168     &(((HLRAlgo_Array1OfTData*)TData)->ChangeValue(1));
169   
170   for (i = 1; i <= myNbTData; i++) {
171     Tri2Indices = TD->Indices();
172     A1 = ((HLRAlgo_Array1OfPINod*)PINod)->ChangeValue(Tri2Node1)->Indices();
173     A2 = ((HLRAlgo_Array1OfPINod*)PINod)->ChangeValue(Tri2Node2)->Indices();
174     A3 = ((HLRAlgo_Array1OfPINod*)PINod)->ChangeValue(Tri2Node3)->Indices();
175     n1 = Tri2Node1;
176     n2 = Tri2Node2;
177     Nod1Indices = A1;
178     Nod2Indices = A2;
179     newSeg = Standard_False;
180     if (Nod1NdSg == 0 && Nod2NdSg == 0) {
181       newSeg = Standard_True;
182       myNbPISeg++;
183       Nod1NdSg = myNbPISeg;
184       Nod2NdSg = myNbPISeg;
185     }
186     else {
187       iiii = Nod1NdSg;
188       if (iiii != 0) {
189         find = 0;
190         
191         while (iiii != 0 && find == 0) {
192           Seg2Indices =
193             ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(iiii).Indices();
194           if (Seg2LstSg1 == n1) {
195             if (Seg2LstSg2 == n2) find = iiii;
196             else                  iiii = Seg2NxtSg1;
197             icsv = 1;
198           }
199           else {
200             if (Seg2LstSg1 == n2) find = iiii;
201             else                  iiii = Seg2NxtSg2;
202             icsv = 2;
203           }
204         }
205         if (find == 0) {
206           newSeg = Standard_True;
207           myNbPISeg++;
208           if (icsv == 1) Seg2NxtSg1 = myNbPISeg;
209           else           Seg2NxtSg2 = myNbPISeg;
210         }
211         else Seg2Conex2 = i;
212       }
213       else {
214         newSeg = Standard_True;
215         myNbPISeg++;
216         Nod1NdSg = myNbPISeg;
217       }
218       if (newSeg) {
219         iiii = Nod2NdSg;
220         if (iiii != 0) {
221           
222           while (iiii != 0) {
223             Seg2Indices =
224               ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(iiii).Indices();
225             if (Seg2LstSg1 == n2) { icsv = 1; iiii = Seg2NxtSg1; }
226             else                  { icsv = 2; iiii = Seg2NxtSg2; }
227           }
228           if (icsv == 1) Seg2NxtSg1 = myNbPISeg;
229           else           Seg2NxtSg2 = myNbPISeg;
230         }
231         else Nod2NdSg = myNbPISeg;
232       }
233     }
234     if (newSeg) {
235       Seg2Indices =
236         ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(myNbPISeg).Indices();
237       Seg2LstSg1 = n1;
238       Seg2LstSg2 = n2;
239       Seg2Conex1 = i;
240       Seg2Conex2 = 0;
241       Seg2NxtSg1 = 0;
242       Seg2NxtSg2 = 0;
243     }
244     
245     n1 = Tri2Node2;
246     n2 = Tri2Node3;
247     Nod1Indices = A2;
248     Nod2Indices = A3;
249     newSeg = Standard_False;
250     if (Nod1NdSg == 0 && Nod2NdSg == 0) {
251       newSeg = Standard_True;
252       myNbPISeg++;
253       Nod1NdSg = myNbPISeg;
254       Nod2NdSg = myNbPISeg;
255     }
256     else {
257       iiii = Nod1NdSg;
258       if (iiii != 0) {
259         find = 0;
260         
261         while (iiii != 0 && find == 0) {
262           Seg2Indices =
263             ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(iiii).Indices();
264           if (Seg2LstSg1 == n1) {
265             if (Seg2LstSg2 == n2) find = iiii;
266             else                  iiii = Seg2NxtSg1;
267             icsv = 1;
268           }
269           else {
270             if (Seg2LstSg1 == n2) find = iiii;
271             else                  iiii = Seg2NxtSg2;
272             icsv = 2;
273           }
274         }
275         if (find == 0) {
276           newSeg = Standard_True;
277           myNbPISeg++;
278           if (icsv == 1) Seg2NxtSg1 = myNbPISeg;
279           else           Seg2NxtSg2 = myNbPISeg;
280         }
281         else Seg2Conex2 = i;
282       }
283       else {
284         newSeg = Standard_True;
285         myNbPISeg++;
286         Nod1NdSg = myNbPISeg;
287       }
288       if (newSeg) {
289         iiii = Nod2NdSg;
290         if (iiii != 0) {
291           
292           while (iiii != 0) {
293             Seg2Indices =
294               ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(iiii).Indices();
295             if (Seg2LstSg1 == n2) { icsv = 1; iiii = Seg2NxtSg1; }
296             else                  { icsv = 2; iiii = Seg2NxtSg2; }
297           }
298           if (icsv == 1) Seg2NxtSg1 = myNbPISeg;
299           else           Seg2NxtSg2 = myNbPISeg;
300         }
301         else Nod2NdSg = myNbPISeg;
302       }
303     }
304     if (newSeg) {
305       Seg2Indices =
306         ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(myNbPISeg).Indices();
307       Seg2LstSg1 = n1;
308       Seg2LstSg2 = n2;
309       Seg2Conex1 = i;
310       Seg2Conex2 = 0;
311       Seg2NxtSg1 = 0;
312       Seg2NxtSg2 = 0;
313     }
314     
315     n1 = Tri2Node3;
316     n2 = Tri2Node1;
317     Nod1Indices = A3;
318     Nod2Indices = A1;
319     newSeg = Standard_False;
320     if (Nod1NdSg == 0 && Nod2NdSg == 0) {
321       newSeg = Standard_True;
322       myNbPISeg++;
323       Nod1NdSg = myNbPISeg;
324       Nod2NdSg = myNbPISeg;
325     }
326     else {
327       iiii = Nod1NdSg;
328       if (iiii != 0) {
329         find = 0;
330         
331         while (iiii != 0 && find == 0) {
332           Seg2Indices =
333             ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(iiii).Indices();
334           if (Seg2LstSg1 == n1) {
335             if (Seg2LstSg2 == n2) find = iiii;
336             else                  iiii = Seg2NxtSg1;
337             icsv = 1;
338           }
339           else {
340             if (Seg2LstSg1 == n2) find = iiii;
341             else                  iiii = Seg2NxtSg2;
342             icsv = 2;
343           }
344         }
345         if (find == 0) {
346           newSeg = Standard_True;
347           myNbPISeg++;
348           if (icsv == 1) Seg2NxtSg1 = myNbPISeg;
349           else           Seg2NxtSg2 = myNbPISeg;
350         }
351         else Seg2Conex2 = i;
352       }
353       else {
354         newSeg = Standard_True;
355         myNbPISeg++;
356         Nod1NdSg = myNbPISeg;
357       }
358       if (newSeg) {
359         iiii = Nod2NdSg;
360         if (iiii != 0) {
361           
362           while (iiii != 0) {
363             Seg2Indices =
364               ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(iiii).Indices();
365             if (Seg2LstSg1 == n2) { icsv = 1; iiii = Seg2NxtSg1; }
366             else                  { icsv = 2; iiii = Seg2NxtSg2; }
367           }
368           if (icsv == 1) Seg2NxtSg1 = myNbPISeg;
369           else           Seg2NxtSg2 = myNbPISeg;
370         }
371         else Nod2NdSg = myNbPISeg;
372       }
373     }
374     if (newSeg) {
375       Seg2Indices =
376         ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(myNbPISeg).Indices();
377       Seg2LstSg1 = n1;
378       Seg2LstSg2 = n2;
379       Seg2Conex1 = i;
380       Seg2Conex2 = 0;
381       Seg2NxtSg1 = 0;
382       Seg2NxtSg2 = 0;
383     }
384     TD++;
385   }
386 }
387
388 //=======================================================================
389 //function : AddNode
390 //purpose  : 
391 //=======================================================================
392
393 Standard_Integer
394 HLRAlgo_PolyInternalData::AddNode (const Standard_Address Nod1RValues,
395                                    const Standard_Address Nod2RValues,
396                                    Standard_Address& PINod1,
397                                    Standard_Address& PINod2,
398                                    const Standard_Real coef1,
399                                    const Standard_Real X3,
400                                    const Standard_Real Y3,
401                                    const Standard_Real Z3)
402 {
403   Standard_Real coef2 = 1 - coef1;
404   IncPINod(PINod1,PINod2);
405   Standard_Integer ip3 = myNbPINod;
406   Handle(HLRAlgo_PolyInternalNode)* pip3 = 
407     &(((HLRAlgo_Array1OfPINod*)PINod1)->ChangeValue(ip3));
408   *pip3 = new HLRAlgo_PolyInternalNode();
409   const Standard_Address Nod3RValues = (*pip3)->RValues();
410   const Standard_Address Nod3Indices = (*pip3)->Indices();
411   Nod3NdSg = 0;
412   Nod3Flag = 0;
413   Nod3PntX = X3;
414   Nod3PntY = Y3;
415   Nod3PntZ = Z3;
416   Nod3PntU = Nod1PntU * coef2 + Nod2PntU * coef1;
417   Nod3PntV = Nod1PntV * coef2 + Nod2PntV * coef1; 
418   Nod3Scal = Nod1Scal * coef2 + Nod2Scal * coef1;
419   Standard_Real x = Nod1NrmX * coef2 + Nod2NrmX * coef1;
420   Standard_Real y = Nod1NrmY * coef2 + Nod2NrmY * coef1;
421   Standard_Real z = Nod1NrmZ * coef2 + Nod2NrmZ * coef1;
422   Standard_Real d = sqrt(x * x + y * y + z * z);
423
424   if (d > 0) {
425     Nod3NrmX = x / d;
426     Nod3NrmY = y / d;
427     Nod3NrmZ = z / d;
428   }
429   else {
430     Nod3NrmX = 1;
431     Nod3NrmY = 0;
432     Nod3NrmZ = 0;
433 #ifdef DEB
434     if (ERROR)
435       cout << "HLRAlgo_PolyInternalData::AddNode" << endl;
436 #endif
437   }
438   return ip3;
439 }
440
441 //=======================================================================
442 //function : UpdateLinks
443 //purpose  : 
444 //=======================================================================
445
446 void
447 HLRAlgo_PolyInternalData::UpdateLinks (const Standard_Integer ip1,
448                                        const Standard_Integer ip2,
449                                        const Standard_Integer ip3,
450                                        Standard_Address& TData1,
451                                        Standard_Address& TData2,
452                                        Standard_Address& PISeg1,
453                                        Standard_Address& PISeg2,
454                                        Standard_Address& PINod1,
455                                        Standard_Address& )
456 {
457   Standard_Integer find,iiii,iisv,icsv,iip2 =0,cnx1 =0,cnx2 =0;
458   Standard_Address Seg1Indices,Seg2Indices;
459   Seg1Indices = 0;
460   find = 0;
461   iisv = 0;
462   icsv = 0;
463   IncPISeg(PISeg1,PISeg2);
464   IncPISeg(PISeg1,PISeg2);
465   IncPISeg(PISeg1,PISeg2);
466   myNbPISeg--;
467   myNbPISeg--;
468   IncTData(TData1,TData2);
469   IncTData(TData1,TData2);
470   myNbTData--;
471   myNbTData--;
472   Standard_Address Nod1Indices =
473     ((HLRAlgo_Array1OfPINod*)PINod1)->ChangeValue(ip1)->Indices();
474   Standard_Address Nod2Indices =
475     ((HLRAlgo_Array1OfPINod*)PINod1)->ChangeValue(ip2)->Indices();
476   Standard_Address Nod3Indices =
477     ((HLRAlgo_Array1OfPINod*)PINod1)->ChangeValue(ip3)->Indices();
478   iiii = Nod2NdSg;
479   
480   while (iiii != 0 && find == 0) {
481     Seg2Indices =
482       ((HLRAlgo_Array1OfPISeg*)PISeg1)->ChangeValue(iiii).Indices();
483     if (Seg2LstSg1 == ip2) {
484       if (Seg2LstSg2 == ip1) {
485         find = iiii;
486         cnx1 = Seg2Conex1;
487         cnx2 = Seg2Conex2;
488         Seg2LstSg1 = ip3;
489         iip2 = Seg2NxtSg1;
490         Seg2NxtSg1 = myNbPISeg;
491         if      (iisv == 0) Nod2NdSg   = myNbPISeg;
492         else if (icsv == 1) Seg1NxtSg1 = myNbPISeg;
493         else                Seg1NxtSg2 = myNbPISeg;
494       }
495       else { 
496         iisv = iiii;
497         icsv = 1;
498       }
499       iiii = Seg2NxtSg1;
500     }
501     else {
502       if (Seg2LstSg1 == ip1) {
503         find = iiii;
504         cnx1 = Seg2Conex1;
505         cnx2 = Seg2Conex2;
506         Seg2LstSg2 = ip3;
507         iip2 = Seg2NxtSg2;
508         Seg2NxtSg2 = myNbPISeg;
509         if      (iisv == 0) Nod2NdSg   = myNbPISeg;
510         else if (icsv == 1) Seg1NxtSg1 = myNbPISeg;
511         else                Seg1NxtSg2 = myNbPISeg;
512       }
513       else { 
514         iisv = iiii;
515         icsv = 2;
516       }
517       iiii = Seg2NxtSg2;
518     }
519     Seg1Indices = Seg2Indices;
520   }
521   if (find == 0) {
522     myNbPISeg--;
523 #ifdef DEB
524     if (ERROR) {
525       cout << "HLRAlgo_PolyInternalData::UpdateLinks : segment error";
526       cout << endl;
527     }
528 #endif
529   }
530   else {
531     Seg2Indices =
532       ((HLRAlgo_Array1OfPISeg*)PISeg1)->ChangeValue(myNbPISeg).Indices();
533     Seg2NxtSg1 = 0;
534     Seg2NxtSg2 = iip2;
535     Seg2LstSg1 = ip3;
536     Seg2LstSg2 = ip2;
537     Seg2Conex1 = cnx1;
538     Seg2Conex2 = cnx2;
539     Nod3NdSg   = find;
540
541     Standard_Integer iOld,iNew,iTr,skip,ip4,itpk[2];
542     Standard_Integer n1,n2,n3,nOld[3],nNew[3],New[4];
543     New[0] = cnx1;
544     New[2] = myNbTData + 1;
545     if (cnx2 == 0) {
546       New[1] = 0;
547       New[3] = 0;
548     }
549     else {
550       New[1] = cnx2;
551       New[3] = myNbTData + 2;
552     }
553     
554     for (skip = 0; skip <= 1; skip++) {
555       iOld = New[skip];
556       iNew = New[skip + 2];
557       if (iOld != 0) {
558         Standard_Address Tri1Indices =
559           ((HLRAlgo_Array1OfTData*)TData1)->ChangeValue(iOld).Indices();
560         Standard_Address Tri2Indices =
561           ((HLRAlgo_Array1OfTData*)TData1)->ChangeValue(iNew).Indices();
562         n1 = Tri1Node1;
563         n2 = Tri1Node2;
564         n3 = Tri1Node3;
565         nOld[0] = n1;
566         nOld[1] = n2;
567         nOld[2] = n3;
568         nNew[0] = n1;
569         nNew[1] = n2;
570         nNew[2] = n3;
571         Standard_Boolean found = Standard_False;
572         if      (n1 == ip1 && n2 == ip2) {
573           found = Standard_True;
574           nOld[1] = ip3;
575           nNew[0] = ip3;
576           itpk[skip] = n3;
577         }
578         else if (n1 == ip2 && n2 == ip1) {
579           found = Standard_True;
580           nOld[0] = ip3;
581           nNew[1] = ip3;
582           itpk[skip] = n3;
583         }
584         else if (n2 == ip1 && n3 == ip2) {
585           found = Standard_True;
586           nOld[2] = ip3;
587           nNew[1] = ip3;
588           itpk[skip] = n1;
589         }
590         else if (n2 == ip2 && n3 == ip1) {
591           found = Standard_True;
592           nOld[1] = ip3;
593           nNew[2] = ip3;
594           itpk[skip] = n1;
595         }
596         else if (n3 == ip1 && n1 == ip2) {
597           found = Standard_True;
598           nOld[0] = ip3;
599           nNew[2] = ip3;
600           itpk[skip] = n2;
601         }
602         else if (n3 == ip2 && n1 == ip1) {
603           found = Standard_True;
604           nOld[2] = ip3;
605           nNew[0] = ip3;
606           itpk[skip] = n2;
607         }
608         if (found) {
609           myNbTData++;
610           ip4 = itpk[skip];
611           Standard_Address Nod4Indices =
612             ((HLRAlgo_Array1OfPINod*)PINod1)->ChangeValue(ip4)->Indices();
613           Tri1Node1 = nOld[0];
614           Tri1Node2 = nOld[1];
615           Tri1Node3 = nOld[2];
616           Tri2Node1 = nNew[0];
617           Tri2Node2 = nNew[1];
618           Tri2Node3 = nNew[2];
619           Tri2Flags = Tri1Flags;
620           myNbPISeg++;
621           Seg2Indices =
622             ((HLRAlgo_Array1OfPISeg*)PISeg1)->ChangeValue(myNbPISeg).Indices();
623           Seg2LstSg1 = ip3;
624           Seg2LstSg2 = ip4;
625           Seg2NxtSg1 = Nod3NdSg;
626           Seg2NxtSg2 = Nod4NdSg;
627           Seg2Conex1 = iOld;
628           Seg2Conex2 = iNew;
629           Nod3NdSg   = myNbPISeg;
630           Nod4NdSg   = myNbPISeg;
631         }
632 #ifdef DEB
633         else if (ERROR) {
634           cout << "HLRAlgo_PolyInternalData::UpdateLinks : triangle error ";
635           cout << endl;
636         }
637 #endif
638       }
639     }
640     
641     for (iTr = 0; iTr <= 3; iTr++) {
642       iNew = New [iTr];
643       if (iTr < 2) skip = iTr;
644       else         skip = iTr - 2;
645       iOld = New [skip];
646       ip4  = itpk[skip];
647       if (iNew != 0) {
648         Standard_Address Tri2Indices =
649           ((HLRAlgo_Array1OfTData*)TData1)->ChangeValue(iNew).Indices();
650         n1 = Tri2Node1;
651         n2 = Tri2Node2;
652         n3 = Tri2Node3; 
653         
654         if (!((n1 == ip3 && n2 == ip4) ||
655               (n2 == ip3 && n1 == ip4))) {
656           Standard_Boolean found = Standard_False;
657           Nod1Indices =
658             ((HLRAlgo_Array1OfPINod*)PINod1)->ChangeValue(n1)->Indices();
659           iiii = Nod1NdSg;
660           
661           while (iiii != 0 && !found) {
662             Seg2Indices =
663               ((HLRAlgo_Array1OfPISeg*)PISeg1)->ChangeValue(iiii).Indices();
664             if (Seg2LstSg1 == n1) {
665               if (Seg2LstSg2 == n2) {
666                 found = Standard_True;
667                 if      (Seg2Conex1 == iOld) Seg2Conex1 = iNew;
668                 else if (Seg2Conex2 == iOld) Seg2Conex2 = iNew;
669               }
670               else iiii = Seg2NxtSg1;
671             }
672             else {
673               if (Seg2LstSg1 == n2) {
674                 found = Standard_True;
675                 if      (Seg2Conex1 == iOld) Seg2Conex1 = iNew;
676                 else if (Seg2Conex2 == iOld) Seg2Conex2 = iNew;
677               }
678               else iiii = Seg2NxtSg2;
679             }
680           }
681         }
682         
683         if (!((n2 == ip3 && n3 == ip4) ||
684               (n3 == ip3 && n2 == ip4))) {
685           Standard_Boolean found = Standard_False;
686           Nod1Indices =
687             ((HLRAlgo_Array1OfPINod*)PINod1)->ChangeValue(n2)->Indices();
688           iiii = Nod1NdSg;
689           
690           while (iiii != 0 && !found) {
691             Seg2Indices =
692               ((HLRAlgo_Array1OfPISeg*)PISeg1)->ChangeValue(iiii).Indices();
693             if (Seg2LstSg1 == n2) {
694               if (Seg2LstSg2 == n3) {
695                 found = Standard_True;
696                 if      (Seg2Conex1 == iOld) Seg2Conex1 = iNew;
697                 else if (Seg2Conex2 == iOld) Seg2Conex2 = iNew;
698               }
699               else iiii = Seg2NxtSg1;
700             }
701             else {
702               if (Seg2LstSg1 == n3) {
703                 found = Standard_True;
704                 if      (Seg2Conex1 == iOld) Seg2Conex1 = iNew;
705                 else if (Seg2Conex2 == iOld) Seg2Conex2 = iNew;
706               }
707               else iiii = Seg2NxtSg2;
708             }
709           }
710         }
711
712         if (!((n3 == ip3 && n1 == ip4) ||
713               (n1 == ip3 && n3 == ip4))) {
714           Standard_Boolean found = Standard_False;
715           Nod1Indices =
716             ((HLRAlgo_Array1OfPINod*)PINod1)->ChangeValue(n3)->Indices();
717           iiii = Nod1NdSg;
718           
719           while (iiii != 0 && !found) {
720             Seg2Indices =
721               ((HLRAlgo_Array1OfPISeg*)PISeg1)->ChangeValue(iiii).Indices();
722             if (Seg2LstSg1 == n3) {
723               if (Seg2LstSg2 == n1) {
724                 found = Standard_True;
725                 if      (Seg2Conex1 == iOld) Seg2Conex1 = iNew;
726                 else if (Seg2Conex2 == iOld) Seg2Conex2 = iNew;
727               }
728               else iiii = Seg2NxtSg1;
729             }
730             else {
731               if (Seg2LstSg1 == n1) {
732                 found = Standard_True;
733                 if      (Seg2Conex1 == iOld) Seg2Conex1 = iNew;
734                 else if (Seg2Conex2 == iOld) Seg2Conex2 = iNew;
735               }
736               else iiii = Seg2NxtSg2;
737             }
738           }
739         }
740       }
741     }
742   }
743 }
744
745 //=======================================================================
746 //function : Dump
747 //purpose  : 
748 //=======================================================================
749
750 void HLRAlgo_PolyInternalData::Dump () const
751 {
752   Standard_Integer i;//,i1,i2,i3;
753   Standard_Address Seg2Indices;
754   Standard_Address TData = &myTData->ChangeArray1();
755   Standard_Address PISeg = &myPISeg->ChangeArray1();
756   Standard_Address PINod = &myPINod->ChangeArray1();
757   
758   for (i = 1; i <= myNbPINod; i++) {
759     const Handle(HLRAlgo_PolyInternalNode)* pi =
760       &(((HLRAlgo_Array1OfPINod*)PINod)->ChangeValue(i));
761     Standard_Address Nod1Indices = (*pi)->Indices();
762     Standard_Address Nod1RValues = (*pi)->RValues();
763     cout << "Node " << setw(6) << i << " : ";
764     cout << setw(6) << Nod1NdSg;
765     cout << setw(20)<< Nod1PntX;
766     cout << setw(20)<< Nod1PntY;
767     cout << setw(20)<< Nod1PntZ;
768     cout << endl;
769   }
770
771   for (i = 1; i <= myNbPISeg; i++) {
772     Seg2Indices =
773       ((HLRAlgo_Array1OfPISeg*)PISeg)->ChangeValue(i).Indices();
774     cout << "Segment " << setw(6) << i << " : ";
775     cout << setw(6) << Seg2LstSg1;
776     cout << setw(6) << Seg2LstSg2;
777     cout << setw(6) << Seg2NxtSg1;
778     cout << setw(6) << Seg2NxtSg2;
779     cout << setw(6) << Seg2Conex1;
780     cout << setw(6) << Seg2Conex2;
781     cout << endl;
782   }
783
784   for (i = 1; i <= myNbTData; i++) {
785     Standard_Address Tri2Indices =
786       ((HLRAlgo_Array1OfTData*)TData)->ChangeValue(i).Indices();
787     cout << "Triangle " << setw(6) << i << " : ";
788     cout << setw(6) << Tri2Node1;
789     cout << setw(6) << Tri2Node2;
790     cout << setw(6) << Tri2Node3;
791     cout << endl;
792   }
793 }
794
795 //=======================================================================
796 //function : IncTData
797 //purpose  : 
798 //=======================================================================
799
800 void HLRAlgo_PolyInternalData::
801 IncTData (Standard_Address& TData1,
802           Standard_Address& TData2)
803 {
804   if (myNbTData >= myMxTData) {
805 #ifdef DEB
806     if (TRACE) 
807       cout << "HLRAlgo_PolyInternalData::IncTData : " << myMxTData << endl;
808 #endif
809     Standard_Integer i,j,k;
810     j = myMxTData;
811     k = 2 * j;
812
813     Handle(HLRAlgo_HArray1OfTData) NwTData =
814       new HLRAlgo_HArray1OfTData(0,k);
815     HLRAlgo_Array1OfTData& oTData = myTData->ChangeArray1();
816     HLRAlgo_Array1OfTData& nTData = NwTData->ChangeArray1();
817     HLRAlgo_TriangleData* OT = &(oTData.ChangeValue(1));
818     HLRAlgo_TriangleData* NT = &(nTData.ChangeValue(1));
819     Standard_Address Tri1Indices,Tri2Indices;
820
821     for (i = 1; i <= j; i++) {
822       Tri1Indices = OT->Indices();
823       Tri2Indices = NT->Indices();
824       Tri2Node1 = Tri1Node1;
825       Tri2Node2 = Tri1Node2;
826       Tri2Node3 = Tri1Node3;
827       Tri2Flags = Tri1Flags;
828       OT++;
829       NT++;
830     }
831     myMxTData = k;
832     myTData  = NwTData;
833     if (TData1 == TData2) {
834       TData1 = &nTData;
835       TData2 = TData1;
836     }
837     else {
838       TData1 = &nTData;
839     }
840   }
841   myNbTData++;
842 }
843
844 //=======================================================================
845 //function : IncPISeg
846 //purpose  : 
847 //=======================================================================
848
849 void HLRAlgo_PolyInternalData::
850 IncPISeg (Standard_Address& PISeg1,
851           Standard_Address& PISeg2)
852
853   if (myNbPISeg >= myMxPISeg) {
854 #ifdef DEB
855     if (TRACE) 
856       cout << "HLRAlgo_PolyInternalData::IncPISeg : " << myMxPISeg << endl;
857 #endif
858     Standard_Integer i,j,k;
859     j = myMxPISeg;
860     k = 2 * j;
861     Handle(HLRAlgo_HArray1OfPISeg) NwPISeg =
862       new HLRAlgo_HArray1OfPISeg(0,k);
863     HLRAlgo_Array1OfPISeg& oPISeg = myPISeg->ChangeArray1();
864     HLRAlgo_Array1OfPISeg& nPISeg = NwPISeg->ChangeArray1();
865     HLRAlgo_PolyInternalSegment* OS = &(oPISeg.ChangeValue(1));
866     HLRAlgo_PolyInternalSegment* NS = &(nPISeg.ChangeValue(1));
867     Standard_Address Seg1Indices,Seg2Indices;
868
869     for (i = 1; i <= j; i++) {
870       Seg1Indices = OS->Indices();
871       Seg2Indices = NS->Indices();
872       Seg2LstSg1 = Seg1LstSg1;
873       Seg2LstSg2 = Seg1LstSg2;
874       Seg2NxtSg1 = Seg1NxtSg1;
875       Seg2NxtSg2 = Seg1NxtSg2;
876       Seg2Conex1 = Seg1Conex1;
877       Seg2Conex2 = Seg1Conex2;
878       OS++;
879       NS++;
880     }
881     myMxPISeg = k;
882     myPISeg = NwPISeg;
883     if (PISeg1 == PISeg2) {
884       PISeg1 = &nPISeg;
885       PISeg2 = PISeg1;
886     }
887     else {
888       PISeg1 = &nPISeg;
889     }
890   }
891   myNbPISeg++;
892 }
893
894 //=======================================================================
895 //function : IncPINod
896 //purpose  : 
897 //=======================================================================
898
899 void HLRAlgo_PolyInternalData::
900 IncPINod (Standard_Address& PINod1,
901           Standard_Address& PINod2)
902 {
903   if (myNbPINod >= myMxPINod) {
904 #ifdef DEB
905     if (TRACE) 
906       cout << "HLRAlgo_PolyInternalData::IncPINod : " << myMxPINod << endl;
907 #endif
908     Standard_Integer i,j,k;
909 //    Standard_Address Nod1Indices;
910     j = myMxPINod;
911     k = 2 * j;
912     Handle(HLRAlgo_HArray1OfPINod) NwPINod =
913       new HLRAlgo_HArray1OfPINod(0,k);
914     HLRAlgo_Array1OfPINod& oPINod = myPINod->ChangeArray1();
915     HLRAlgo_Array1OfPINod& nPINod = NwPINod->ChangeArray1();
916     Handle(HLRAlgo_PolyInternalNode)* ON = &(oPINod.ChangeValue(1));
917     Handle(HLRAlgo_PolyInternalNode)* NN = &(nPINod.ChangeValue(1));
918
919     for (i = 1; i <= j; i++) {
920       *NN = *ON;
921       ON++;
922       NN++;
923     }
924     myMxPINod = k;
925     myPINod = NwPINod;
926     if (PINod1 == PINod2) {
927       PINod1 = &nPINod;
928       PINod2 = PINod1;
929     }
930     else {
931       PINod1 = &nPINod;
932     }
933   }
934   myNbPINod++;
935 }