0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / Poly / Poly_CoherentTriangle.cxx
1 // Created on: 2007-11-25
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Poly_CoherentTriangle.hxx>
17 #include <Standard_ProgramError.hxx>
18
19 //=======================================================================
20 //function : Poly_CoherentTriangle()
21 //purpose  : Empty Constructor
22 //=======================================================================
23
24 Poly_CoherentTriangle::Poly_CoherentTriangle ()
25   : myNConnections (0)
26 {
27   myNodes[0] = -1;
28   myNodes[1] = -1;
29   myNodes[2] = -1;
30   myNodesOnConnected[0] = -1;
31   myNodesOnConnected[1] = -1;
32   myNodesOnConnected[2] = -1;
33   mypConnected[0] = 0L;
34   mypConnected[1] = 0L;
35   mypConnected[2] = 0L;
36   mypLink[0] = 0L;
37   mypLink[1] = 0L;
38   mypLink[2] = 0L;
39 }
40
41 //=======================================================================
42 //function : Poly_CoherentTriangle()
43 //purpose  : Constructor
44 //=======================================================================
45
46 Poly_CoherentTriangle::Poly_CoherentTriangle (const Standard_Integer iNode0,
47                                               const Standard_Integer iNode1,
48                                               const Standard_Integer iNode2)
49   : myNConnections (0)
50 {
51   myNodes[0] = iNode0;
52   myNodes[1] = iNode1;
53   myNodes[2] = iNode2;
54   myNodesOnConnected[0] = -1;
55   myNodesOnConnected[1] = -1;
56   myNodesOnConnected[2] = -1;
57   mypConnected[0] = 0L;
58   mypConnected[1] = 0L;
59   mypConnected[2] = 0L;
60   mypLink[0] = 0L;
61   mypLink[1] = 0L;
62   mypLink[2] = 0L;
63 }
64
65 //=======================================================================
66 //function : SetConnection
67 //purpose  : 
68 //=======================================================================
69
70 Standard_Boolean Poly_CoherentTriangle::SetConnection
71                                         (const Standard_Integer iConn,
72                                          Poly_CoherentTriangle& theTr)
73 {
74   Standard_Boolean aResult(Standard_False);
75   static const Standard_Integer II[] = { 2, 0, 1, 2, 0 };
76
77   if (theTr.Node(0) == myNodes[II[iConn+2]]) {
78     if (theTr.Node(2) == myNodes[II[iConn]]) {
79       RemoveConnection(iConn);
80       myNodesOnConnected[iConn] = theTr.Node(1);
81       mypConnected[iConn] = &theTr;
82       myNConnections++;
83       theTr.RemoveConnection(1);
84       theTr.myNodesOnConnected[1] = myNodes[iConn];
85       theTr.mypConnected[1] = this;
86       theTr.myNConnections++;
87       aResult = Standard_True;
88     }
89   } else if (theTr.Node(1) == myNodes[II[iConn+2]]) {
90     if (theTr.Node(0) == myNodes[II[iConn]]) {
91       RemoveConnection(iConn);
92       myNodesOnConnected[iConn] = theTr.Node(2);
93       mypConnected[iConn] = &theTr;
94       myNConnections++;
95       theTr.RemoveConnection(2);
96       theTr.myNodesOnConnected[2] = myNodes[iConn];
97       theTr.mypConnected[2] = this;
98       theTr.myNConnections++;
99       aResult = Standard_True;
100     }
101   } else if (theTr.Node(2) == myNodes[II[iConn+2]]) {
102     if (theTr.Node(1) == myNodes[II[iConn]]) {
103       RemoveConnection(iConn);
104       myNodesOnConnected[iConn] = theTr.Node(0);
105       mypConnected[iConn] = &theTr;
106       myNConnections++;
107       theTr.RemoveConnection(0);
108       theTr.myNodesOnConnected[0] = myNodes[iConn];
109       theTr.mypConnected[0] = this;
110       theTr.myNConnections++;
111       aResult = Standard_True;
112     }
113   }
114   return aResult;
115 }
116
117 //=======================================================================
118 //function : SetConnection
119 //purpose  : 
120 //=======================================================================
121
122 Standard_Boolean Poly_CoherentTriangle::SetConnection
123                                         (Poly_CoherentTriangle& theTr)
124 {
125   Standard_Boolean aResult(Standard_False);
126   if (myNodes[0] == theTr.Node(0)) {
127     if (myNodes[1] == theTr.Node(2) && mypConnected[2] != &theTr) {
128       RemoveConnection(2);
129       myNodesOnConnected[2] = theTr.Node(1);
130       mypConnected[2] = &theTr;
131       myNConnections++;
132       theTr.RemoveConnection(1);
133       theTr.myNodesOnConnected[1] = myNodes[2];
134       theTr.mypConnected[1] = this;
135       theTr.myNConnections++;
136       aResult = Standard_True;
137     } else if (myNodes[2] == theTr.Node(1) && mypConnected[1] != &theTr) {
138       RemoveConnection(1);
139       myNodesOnConnected[1] = theTr.Node(2);
140       mypConnected[1] = &theTr;
141       myNConnections++;
142       theTr.RemoveConnection(2);
143       theTr.myNodesOnConnected[2] = myNodes[1];
144       theTr.mypConnected[2] = this;
145       theTr.myNConnections++;
146       aResult = Standard_True;
147     }
148   } else if (myNodes[0] == theTr.Node(1)) {
149     if (myNodes[1] == theTr.Node(0) && mypConnected[2] != &theTr) {
150       RemoveConnection(2);
151       myNodesOnConnected[2] = theTr.Node(2);
152       mypConnected[2] = &theTr;
153       myNConnections++;
154       theTr.RemoveConnection(2);
155       theTr.myNodesOnConnected[2] = myNodes[2];
156       theTr.mypConnected[2] = this;
157       theTr.myNConnections++;
158       aResult = Standard_True;
159     } else if (myNodes[2] == theTr.Node(2) && mypConnected[1] != &theTr) {
160       RemoveConnection(1);
161       myNodesOnConnected[1] = theTr.Node(0);
162       mypConnected[1] = &theTr;
163       myNConnections++;
164       theTr.RemoveConnection(0);
165       theTr.myNodesOnConnected[0] = myNodes[1];
166       theTr.mypConnected[0] = this;
167       theTr.myNConnections++;
168       aResult = Standard_True;
169     }
170   } else if (myNodes[0] == theTr.Node(2)) {
171     if (myNodes[1] == theTr.Node(1) && mypConnected[2] != &theTr) {
172       RemoveConnection(2);
173       myNodesOnConnected[2] = theTr.Node(0);
174       mypConnected[2] = &theTr;
175       myNConnections++;
176       theTr.RemoveConnection(0);
177       theTr.myNodesOnConnected[0] = myNodes[2];
178       theTr.mypConnected[0] = this;
179       theTr.myNConnections++;
180       aResult = Standard_True;
181     } else if (myNodes[2] == theTr.Node(0) && mypConnected[1] != &theTr) {
182       RemoveConnection(1);
183       myNodesOnConnected[1] = theTr.Node(1);
184       mypConnected[1] = &theTr;
185       myNConnections++;
186       theTr.RemoveConnection(1);
187       theTr.myNodesOnConnected[1] = myNodes[1];
188       theTr.mypConnected[1] = this;
189       theTr.myNConnections++;
190       aResult = Standard_True;
191     }
192   } else if (mypConnected[0] != &theTr) {
193     if (myNodes[1] == theTr.Node(0) && myNodes[2] == theTr.Node(2)) {
194       RemoveConnection(0);
195       myNodesOnConnected[0] = theTr.Node(1);
196       mypConnected[0] = &theTr;
197       myNConnections++;
198       theTr.RemoveConnection(1);
199       theTr.myNodesOnConnected[1] = myNodes[0];
200       theTr.mypConnected[1] = this;
201       theTr.myNConnections++;
202       aResult = Standard_True;
203     } else if (myNodes[1] == theTr.Node(2) && myNodes[2] == theTr.Node(1)) {
204       RemoveConnection(0);
205       myNodesOnConnected[0] = theTr.Node(0);
206       mypConnected[0] = &theTr;
207       myNConnections++;
208       theTr.RemoveConnection(0);
209       theTr.myNodesOnConnected[0] = myNodes[0];
210       theTr.mypConnected[0] = this;
211       theTr.myNConnections++;
212       aResult = Standard_True;
213     } else if (myNodes[1] == theTr.Node(1) && myNodes[2] == theTr.Node(0)) {
214       RemoveConnection(0);
215       myNodesOnConnected[0] = theTr.Node(2);
216       mypConnected[0] = &theTr;
217       myNConnections++;
218       theTr.RemoveConnection(2);
219       theTr.myNodesOnConnected[2] = myNodes[0];
220       theTr.mypConnected[2] = this;
221       theTr.myNConnections++;
222       aResult = Standard_True;
223     }
224   }
225   return aResult;
226 }
227
228 //=======================================================================
229 //function : RemoveConnection
230 //purpose  : 
231 //=======================================================================
232
233 void Poly_CoherentTriangle::RemoveConnection(const Standard_Integer iConn)
234 {
235   Poly_CoherentTriangle * pConnectedTri = 
236     const_cast<Poly_CoherentTriangle *> (mypConnected[iConn]);
237   if (pConnectedTri) {
238     Standard_Integer iConn1(0);
239     if (pConnectedTri->mypConnected[0] != this) {      
240       if (pConnectedTri->mypConnected[1] == this)
241         iConn1 = 1;
242       else if (pConnectedTri->mypConnected[2] == this)
243         iConn1 = 2;
244       else
245         throw Standard_ProgramError("Poly_CoherentTriangle::RemoveConnection: "
246                                     "wrong connection between triangles");
247     }
248     pConnectedTri->mypConnected[iConn1] = 0L;
249     pConnectedTri->myNodesOnConnected[iConn1] = -1;
250     pConnectedTri->myNConnections--;
251     mypConnected[iConn] = 0L;
252     myNodesOnConnected[iConn] = -1;
253     myNConnections--;
254   }
255 }
256
257 //=======================================================================
258 //function : RemoveConnection
259 //purpose  : 
260 //=======================================================================
261
262 Standard_Boolean Poly_CoherentTriangle::RemoveConnection
263                                                 (Poly_CoherentTriangle& theTri)
264 {
265   const Standard_Integer iConn = FindConnection(theTri);
266   if (iConn >= 0)
267     RemoveConnection(iConn);
268   return (iConn >= 0);
269 }
270
271 //=======================================================================
272 //function : FindConnection
273 //purpose  : 
274 //=======================================================================
275
276 Standard_Integer Poly_CoherentTriangle::FindConnection
277                                 (const Poly_CoherentTriangle& theTri) const
278 {
279   Standard_Integer aResult;
280   if (mypConnected[0] == &theTri)
281     aResult = 0;
282   else if (mypConnected[1] == &theTri)
283     aResult = 1;
284   else if (mypConnected[2] == &theTri)
285     aResult = 2;
286   else
287     aResult = -1;
288   return aResult;
289 }