Salome HOME
0020139: EDF 944 SMESH : Get 2D/3D element with X, Y, Z coordinates
[modules/smesh.git] / src / SMDS / SMDS_VolumeTool.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File      : SMDS_VolumeTool.cxx
23 // Created   : Tue Jul 13 12:22:13 2004
24 // Author    : Edward AGAPOV (eap)
25 //
26 #ifdef _MSC_VER
27 #pragma warning(disable:4786)
28 #endif
29
30 #include "SMDS_VolumeTool.hxx"
31
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
35 #include "SMDS_Mesh.hxx"
36
37 #include "utilities.h"
38
39 #include <map>
40 #include <float.h>
41 #include <math.h>
42
43 using namespace std;
44
45 // ======================================================
46 // Node indices in faces depending on volume orientation
47 // making most faces normals external
48 // ======================================================
49
50 /*
51 //           N3
52 //           +
53 //          /|\
54 //         / | \
55 //        /  |  \
56 //    N0 +---|---+ N1                TETRAHEDRON
57 //       \   |   /
58 //        \  |  /
59 //         \ | /
60 //          \|/
61 //           +
62 //           N2
63 */
64 static int Tetra_F [4][4] = { // FORWARD == EXTERNAL
65   { 0, 1, 2, 0 },              // All faces have external normals
66   { 0, 3, 1, 0 },
67   { 1, 3, 2, 1 },
68   { 0, 2, 3, 0 }}; 
69 static int Tetra_R [4][4] = { // REVERSED
70   { 0, 1, 2, 0 },             // All faces but a bottom have external normals
71   { 0, 1, 3, 0 },
72   { 1, 2, 3, 1 },
73   { 0, 3, 2, 0 }};
74 static int Tetra_RE [4][4] = { // REVERSED -> FORWARD (EXTERNAL)
75   { 0, 2, 1, 0 },              // All faces have external normals
76   { 0, 1, 3, 0 },
77   { 1, 2, 3, 1 },
78   { 0, 3, 2, 0 }};
79 static int Tetra_nbN [] = { 3, 3, 3, 3 };
80
81 //
82 //     PYRAMID
83 //
84 static int Pyramid_F [5][5] = { // FORWARD == EXTERNAL
85   { 0, 1, 2, 3, 0 },            // All faces have external normals
86   { 0, 4, 1, 0, 4 },
87   { 1, 4, 2, 1, 4 },
88   { 2, 4, 3, 2, 4 },
89   { 3, 4, 0, 3, 4 }}; 
90 static int Pyramid_R [5][5] = { // REVERSED
91   { 0, 1, 2, 3, 0 },            // All faces but a bottom have external normals
92   { 0, 1, 4, 0, 4 },
93   { 1, 2, 4, 1, 4 },
94   { 2, 3, 4, 2, 4 },
95   { 3, 0, 4, 3, 4 }}; 
96 static int Pyramid_RE [5][5] = { // REVERSED -> FORWARD (EXTERNAL)
97   { 0, 3, 2, 1, 0 },             // All faces but a bottom have external normals
98   { 0, 1, 4, 0, 4 },
99   { 1, 2, 4, 1, 4 },
100   { 2, 3, 4, 2, 4 },
101   { 3, 0, 4, 3, 4 }}; 
102 static int Pyramid_nbN [] = { 4, 3, 3, 3, 3 };
103
104 /*   
105 //            + N4
106 //           /|\
107 //          / | \
108 //         /  |  \
109 //        /   |   \
110 //    N3 +---------+ N5
111 //       |    |    |
112 //       |    + N1 |
113 //       |   / \   |                PENTAHEDRON
114 //       |  /   \  |
115 //       | /     \ |
116 //       |/       \|
117 //    N0 +---------+ N2
118 */
119 static int Penta_F [5][5] = { // FORWARD
120   { 0, 1, 2, 0, 0 },          // Top face has an internal normal, other - external
121   { 3, 4, 5, 3, 3 },          // 0 is bottom, 1 is top face
122   { 0, 2, 5, 3, 0 },
123   { 1, 4, 5, 2, 1 },
124   { 0, 3, 4, 1, 0 }}; 
125 static int Penta_R [5][5] = { // REVERSED
126   { 0, 1, 2, 0, 0 },          // Bottom face has an internal normal, other - external
127   { 3, 4, 5, 3, 3 },          // 0 is bottom, 1 is top face
128   { 0, 3, 5, 2, 0 },
129   { 1, 2, 5, 4, 1 },
130   { 0, 1, 4, 3, 0 }}; 
131 static int Penta_FE [5][5] = { // FORWARD -> EXTERNAL
132   { 0, 1, 2, 0, 0 },
133   { 3, 5, 4, 3, 3 },
134   { 0, 2, 5, 3, 0 },
135   { 1, 4, 5, 2, 1 },
136   { 0, 3, 4, 1, 0 }}; 
137 static int Penta_RE [5][5] = { // REVERSED -> EXTERNAL
138   { 0, 2, 1, 0, 0 },
139   { 3, 4, 5, 3, 3 },
140   { 0, 3, 5, 2, 0 },
141   { 1, 2, 5, 4, 1 },
142   { 0, 1, 4, 3, 0 }}; 
143 static int Penta_nbN [] = { 3, 3, 4, 4, 4 };
144
145 /*
146 //         N5+----------+N6
147 //          /|         /|
148 //         / |        / |
149 //        /  |       /  |
150 //     N4+----------+N7 |
151 //       |   |      |   |           HEXAHEDRON
152 //       |   |      |   |
153 //       |   |      |   |
154 //       | N1+------|---+N2
155 //       |  /       |  /
156 //       | /        | /
157 //       |/         |/
158 //     N0+----------+N3
159 */
160 static int Hexa_F [6][5] = { // FORWARD
161   { 0, 1, 2, 3, 0 },         // opposite faces are neighbouring,
162   { 4, 5, 6, 7, 4 },         // odd face(1,3,5) normal is internal, even(0,2,4) - external
163   { 1, 0, 4, 5, 1 },         // same index nodes of opposite faces are linked
164   { 2, 3, 7, 6, 2 }, 
165   { 0, 3, 7, 4, 0 }, 
166   { 1, 2, 6, 5, 1 }};
167 // static int Hexa_R [6][5] = { // REVERSED
168 //   { 0, 3, 2, 1, 0 },         // opposite faces are neighbouring,
169 //   { 4, 7, 6, 5, 4 },         // odd face(1,3,5) normal is external, even(0,2,4) - internal
170 //   { 1, 5, 4, 0, 1 },         // same index nodes of opposite faces are linked
171 //   { 2, 6, 7, 3, 2 }, 
172 //   { 0, 4, 7, 3, 0 }, 
173 //   { 1, 5, 6, 2, 1 }};
174 static int Hexa_FE [6][5] = { // FORWARD -> EXTERNAL
175   { 0, 1, 2, 3, 0 } ,         // opposite faces are neighbouring,
176   { 4, 7, 6, 5, 4 },          // all face normals are external,
177   { 0, 4, 5, 1, 0 },          // links in opposite faces: 0-0, 1-3, 2-2, 3-1
178   { 3, 2, 6, 7, 3 }, 
179   { 0, 3, 7, 4, 0 },
180   { 1, 5, 6, 2, 1 }};
181 static int Hexa_RE [6][5] = { // REVERSED -> EXTERNAL
182   { 0, 3, 2, 1, 0 },          // opposite faces are neighbouring,
183   { 4, 5, 6, 7, 4 },          // all face normals are external,
184   { 0, 1, 5, 4, 0 },          // links in opposite faces: 0-0, 1-3, 2-2, 3-1
185   { 3, 7, 6, 2, 3 }, 
186   { 0, 4, 7, 3, 0 },
187   { 1, 2, 6, 5, 1 }};
188 static int Hexa_nbN [] = { 4, 4, 4, 4, 4, 4 };
189
190
191 /*
192 //           N3
193 //           +
194 //          /|\
195 //        7/ | \8
196 //        /  |4 \                    QUADRATIC
197 //    N0 +---|---+ N1                TETRAHEDRON
198 //       \   +9  /
199 //        \  |  /
200 //        6\ | /5
201 //          \|/
202 //           +
203 //           N2
204 */
205 static int QuadTetra_F [4][7] = { // FORWARD == EXTERNAL
206   { 0, 4, 1, 5, 2, 6, 0 },        // All faces have external normals
207   { 0, 7, 3, 8, 1, 4, 0 },
208   { 1, 8, 3, 9, 2, 5, 1 },
209   { 0, 6, 2, 9, 3, 7, 0 }}; 
210 static int QuadTetra_R [4][7] = { // REVERSED
211   { 0, 4, 1, 5, 2, 6, 0 },        // All faces but a bottom have external normals
212   { 0, 4, 1, 8, 3, 7, 0 },
213   { 1, 5, 2, 9, 3, 8, 1 },
214   { 0, 7, 3, 9, 2, 6, 0 }};
215 static int QuadTetra_RE [4][7] = { // REVERSED -> FORWARD (EXTERNAL)
216   { 0, 6, 2, 5, 1, 4, 0 },              // All faces have external normals
217   { 0, 4, 1, 8, 3, 7, 0 },
218   { 1, 5, 2, 9, 3, 8, 1 },
219   { 0, 7, 3, 9, 2, 6, 0 }};
220 static int QuadTetra_nbN [] = { 6, 6, 6, 6 };
221
222 //
223 //     QUADRATIC
224 //     PYRAMID
225 //
226 //            +4
227 //
228 //            
229 //       10+-----+11
230 //         |     |        9 - middle point for (0,4) etc.
231 //         |     |
232 //        9+-----+12
233 //
234 //            6
235 //      1+----+----+2
236 //       |         |
237 //       |         |
238 //      5+         +7
239 //       |         |
240 //       |         |
241 //      0+----+----+3
242 //            8
243 static int QuadPyram_F [5][9] = {  // FORWARD == EXTERNAL
244   { 0, 5, 1, 6, 2, 7, 3, 8, 0 },   // All faces have external normals
245   { 0, 9, 4, 10,1, 5, 0, 4, 4 },
246   { 1, 10,4, 11,2, 6, 1, 4, 4 },
247   { 2, 11,4, 12,3, 7, 2, 4, 4 },
248   { 3, 12,4, 9, 0, 8, 3, 4, 4 }}; 
249 static int QuadPyram_R [5][9] = {  // REVERSED
250   { 0, 5, 1, 6, 2, 7, 3, 8, 0 },   // All faces but a bottom have external normals
251   { 0, 5, 1, 10,4, 9, 0, 4, 4 },
252   { 1, 6, 2, 11,4, 10,1, 4, 4 },
253   { 2, 7, 3, 12,4, 11,2, 4, 4 },
254   { 3, 8, 0, 9, 4, 12,3, 4, 4 }}; 
255 static int QuadPyram_RE [5][9] = { // REVERSED -> FORWARD (EXTERNAL)
256   { 0, 8, 3, 7, 2, 6, 1, 5, 0 },   // All faces but a bottom have external normals
257   { 0, 5, 1, 10,4, 9, 0, 4, 4 },
258   { 1, 6, 2, 11,4, 10,1, 4, 4 },
259   { 2, 7, 3, 12,4, 11,2, 4, 4 },
260   { 3, 8, 0, 9, 4, 12,3, 4, 4 }}; 
261 static int QuadPyram_nbN [] = { 8, 6, 6, 6, 6 };
262
263 /*   
264 //            + N4
265 //           /|\
266 //         9/ | \10
267 //         /  |  \
268 //        /   |   \
269 //    N3 +----+----+ N5
270 //       |    |11  |
271 //       |    |    |
272 //       |    +13  |                QUADRATIC
273 //       |    |    |                PENTAHEDRON
274 //       |    |    |
275 //       |    |    |
276 //       |    |    |
277 //     12+    |    +14
278 //       |    |    |
279 //       |    |    |
280 //       |    + N1 |
281 //       |   / \   |               
282 //       | 6/   \7 |
283 //       | /     \ |
284 //       |/       \|
285 //    N0 +---------+ N2
286 //            8
287 */
288 static int QuadPenta_F [5][9] = {  // FORWARD
289   { 0, 6, 1, 7, 2, 8, 0, 0, 0 },   // Top face has an internal normal, other - external
290   { 3, 9, 4, 10,5, 11,3, 3, 3 },   // 0 is bottom, 1 is top face
291   { 0, 8, 2, 14,5, 11,3, 12,0 },
292   { 1, 13,4, 10,5, 14,2, 7, 1 },
293   { 0, 12,3, 9, 4, 13,1, 6, 0 }}; 
294 static int QuadPenta_R [5][9] = { // REVERSED
295   { 0, 6, 1, 7, 2, 8, 0, 0, 0 },  // Bottom face has an internal normal, other - external
296   { 3, 9, 4, 10,5, 11,3, 3, 3 },  // 0 is bottom, 1 is top face
297   { 0, 12,3, 11,5, 14,2, 8, 0 },
298   { 1, 7, 2, 14,5, 10,4, 13,1 },
299   { 0, 6, 1, 13,4, 9, 3, 12,0 }}; 
300 static int QuadPenta_FE [5][9] = { // FORWARD -> EXTERNAL
301   { 0, 6, 1, 7, 2, 8, 0, 0, 0 },
302   { 3,11, 5, 10,4, 9, 3, 3, 3 },
303   { 0, 8, 2, 14,5, 11,3, 12,0 },
304   { 1, 13,4, 10,5, 14,2, 7, 1 },
305   { 0, 12,3, 9, 4, 13,1, 6, 0 }}; 
306 static int QuadPenta_RE [5][9] = { // REVERSED -> EXTERNAL
307   { 0, 8, 2, 7, 1, 6, 0, 0, 0 },
308   { 3, 9, 4, 10,5, 11,3, 3, 3 },
309   { 0, 12,3, 11,5, 14,2, 8, 0 },
310   { 1, 7, 2, 14,5, 10,4, 13,1 },
311   { 0, 6, 1, 13,4, 9, 3, 12,0 }}; 
312 static int QuadPenta_nbN [] = { 6, 6, 8, 8, 8 };
313
314 /*
315 //                 13
316 //         N5+-----+-----+N6
317 //          /|          /|
318 //       12+ |       14+ |
319 //        /  |        /  |
320 //     N4+-----+-----+N7 |           QUADRATIC
321 //       |   | 15    |   |           HEXAHEDRON
322 //       |   |       |   |
323 //       | 17+       |   +18
324 //       |   |       |   |
325 //       |   |       |   |
326 //       |   |       |   |
327 //     16+   |       +19 |
328 //       |   |       |   |
329 //       |   |     9 |   |
330 //       | N1+-----+-|---+N2
331 //       |  /        |  /
332 //       | +8        | +10
333 //       |/          |/
334 //     N0+-----+-----+N3
335 //             11
336 */
337 static int QuadHexa_F [6][9] = {  // FORWARD
338   { 0, 8, 1, 9, 2, 10,3, 11,0 },  // opposite faces are neighbouring,
339   { 4, 12,5, 13,6, 14,7, 15,4 },  // odd face(1,3,5) normal is internal, even(0,2,4) - external
340   { 1, 8, 0, 16,4, 12,5, 17,1 },  // same index nodes of opposite faces are linked
341   { 2, 10,3, 19,7, 14,6, 18,2 }, 
342   { 0, 11,3, 19,7, 15,4, 16,0 }, 
343   { 1, 9, 2, 18,6, 13,5, 17,1 }};
344 // static int Hexa_R [6][5] = { // REVERSED
345 //   { 0, 3, 2, 1, 0 },         // opposite faces are neighbouring,
346 //   { 4, 7, 6, 5, 4 },         // odd face(1,3,5) normal is external, even(0,2,4) - internal
347 //   { 1, 5, 4, 0, 1 },         // same index nodes of opposite faces are linked
348 //   { 2, 6, 7, 3, 2 }, 
349 //   { 0, 4, 7, 3, 0 }, 
350 //   { 1, 5, 6, 2, 1 }};
351 static int QuadHexa_FE [6][9] = {  // FORWARD -> EXTERNAL
352   { 0, 8, 1, 9, 2, 10,3, 11,0 },   // opposite faces are neighbouring,
353   { 4, 15,7, 14,6, 13,5, 12,4 },   // all face normals are external,
354   { 0, 16,4, 12,5, 17,1, 8, 0 },   // links in opposite faces: 0-0, 1-3, 2-2, 3-1
355   { 3, 10,2, 18,6, 14,7, 19,3 }, 
356   { 0, 11,3, 19,7, 15,4, 16,0 },
357   { 1, 17,5, 13,6, 18,2, 9, 1 }};
358 static int QuadHexa_RE [6][9] = {  // REVERSED -> EXTERNAL
359   { 0, 11,3, 10,2, 9, 1, 8, 0 },   // opposite faces are neighbouring,
360   { 4, 12,5, 13,6, 14,7, 15,4 },   // all face normals are external,
361   { 0, 8, 1, 17,5, 12,4, 16,0 },   // links in opposite faces: 0-0, 1-3, 2-2, 3-1
362   { 3, 19,7, 14,6, 18,2, 10,3 }, 
363   { 0, 16,4, 15,7, 19,3, 11,0 },
364   { 1, 9, 2, 18,6, 13,5, 17,1 }};
365 static int QuadHexa_nbN [] = { 8, 8, 8, 8, 8, 8 };
366
367
368 // ========================================================
369 // to perform some calculations without linkage to CASCADE
370 // ========================================================
371 namespace
372 {
373 struct XYZ {
374   double x;
375   double y;
376   double z;
377   XYZ()                               { x = 0; y = 0; z = 0; }
378   XYZ( double X, double Y, double Z ) { x = X; y = Y; z = Z; }
379   XYZ( const XYZ& other )             { x = other.x; y = other.y; z = other.z; }
380   XYZ( const SMDS_MeshNode* n )       { x = n->X(); y = n->Y(); z = n->Z(); }
381   inline XYZ operator-( const XYZ& other );
382   inline XYZ Crossed( const XYZ& other );
383   inline double Dot( const XYZ& other );
384   inline double Magnitude();
385 };
386 inline XYZ XYZ::operator-( const XYZ& Right ) {
387   return XYZ(x - Right.x, y - Right.y, z - Right.z);
388 }
389 inline XYZ XYZ::Crossed( const XYZ& Right ) {
390   return XYZ (y * Right.z - z * Right.y,
391               z * Right.x - x * Right.z,
392               x * Right.y - y * Right.x);
393 }
394 inline double XYZ::Dot( const XYZ& Other ) {
395   return(x * Other.x + y * Other.y + z * Other.z);
396 }
397 inline double XYZ::Magnitude() {
398   return sqrt (x * x + y * y + z * z);
399 }
400 }
401
402 //=======================================================================
403 //function : SMDS_VolumeTool
404 //purpose  : 
405 //=======================================================================
406
407 SMDS_VolumeTool::SMDS_VolumeTool ()
408      : myVolume( 0 ),
409        myPolyedre( 0 ),
410        myVolForward( true ),
411        myNbFaces( 0 ),
412        myVolumeNbNodes( 0 ),
413        myVolumeNodes( NULL ),
414        myExternalFaces( false ),
415        myFaceNbNodes( 0 ),
416        myCurFace( -1 ),
417        myFaceNodeIndices( NULL ),
418        myFaceNodes( NULL )
419 {
420 }
421
422 //=======================================================================
423 //function : SMDS_VolumeTool
424 //purpose  : 
425 //=======================================================================
426
427 SMDS_VolumeTool::SMDS_VolumeTool (const SMDS_MeshElement* theVolume)
428      : myVolume( 0 ),
429        myPolyedre( 0 ),
430        myVolForward( true ),
431        myNbFaces( 0 ),
432        myVolumeNbNodes( 0 ),
433        myVolumeNodes( NULL ),
434        myExternalFaces( false ),
435        myFaceNbNodes( 0 ),
436        myCurFace( -1 ),
437        myFaceNodeIndices( NULL ),
438        myFaceNodes( NULL )
439 {
440   Set( theVolume );
441 }
442
443 //=======================================================================
444 //function : SMDS_VolumeTool
445 //purpose  : 
446 //=======================================================================
447
448 SMDS_VolumeTool::~SMDS_VolumeTool()
449 {
450   if (myVolumeNodes != NULL) {
451     delete [] myVolumeNodes;
452     myVolumeNodes = NULL;
453   }
454   if (myFaceNodes != NULL) {
455     delete [] myFaceNodes;
456     myFaceNodes = NULL;
457   }
458 }
459
460 //=======================================================================
461 //function : SetVolume
462 //purpose  : Set volume to iterate on
463 //=======================================================================
464
465 bool SMDS_VolumeTool::Set (const SMDS_MeshElement* theVolume)
466 {
467   myVolume = 0;
468   myPolyedre = 0;
469
470   myVolForward = true;
471   myNbFaces = 0;
472   myVolumeNbNodes = 0;
473   if (myVolumeNodes != NULL) {
474     delete [] myVolumeNodes;
475     myVolumeNodes = NULL;
476   }
477
478   myExternalFaces = false;
479   myFaceNbNodes = 0;
480
481   myCurFace = -1;
482   myFaceNodeIndices = NULL;
483   if (myFaceNodes != NULL) {
484     delete [] myFaceNodes;
485     myFaceNodes = NULL;
486   }
487
488   if ( theVolume && theVolume->GetType() == SMDSAbs_Volume )
489   {
490     myVolume = theVolume;
491
492     myNbFaces = theVolume->NbFaces();
493     myVolumeNbNodes = theVolume->NbNodes();
494
495     // set volume nodes
496     int iNode = 0;
497     myVolumeNodes = new const SMDS_MeshNode* [myVolumeNbNodes];
498     SMDS_ElemIteratorPtr nodeIt = myVolume->nodesIterator();
499     while ( nodeIt->more() ) {
500       myVolumeNodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
501     }
502
503     if (myVolume->IsPoly()) {
504       myPolyedre = static_cast<const SMDS_PolyhedralVolumeOfNodes*>( myVolume );
505       if (!myPolyedre) {
506         MESSAGE("Warning: bad volumic element");
507         return false;
508       }
509     }
510     else {
511       switch ( myVolumeNbNodes ) {
512       case 4:
513       case 5:
514       case 6:
515       case 8:
516       case 10:
517       case 13:
518       case 15:
519       case 20: {
520         // define volume orientation
521         XYZ botNormal;
522         GetFaceNormal( 0, botNormal.x, botNormal.y, botNormal.z );
523         const SMDS_MeshNode* topNode = myVolumeNodes[ myVolumeNbNodes - 1 ];
524         const SMDS_MeshNode* botNode = myVolumeNodes[ 0 ];
525         XYZ upDir (topNode->X() - botNode->X(),
526                    topNode->Y() - botNode->Y(),
527                    topNode->Z() - botNode->Z() );
528         myVolForward = ( botNormal.Dot( upDir ) < 0 );
529         break;
530       }
531       default:
532         break;
533       }
534     }
535   }
536   return ( myVolume != 0 );
537 }
538
539 //=======================================================================
540 //function : Inverse
541 //purpose  : Inverse volume
542 //=======================================================================
543
544 #define SWAP_NODES(nodes,i1,i2)           \
545 {                                         \
546   const SMDS_MeshNode* tmp = nodes[ i1 ]; \
547   nodes[ i1 ] = nodes[ i2 ];              \
548   nodes[ i2 ] = tmp;                      \
549 }
550 void SMDS_VolumeTool::Inverse ()
551 {
552   if ( !myVolume ) return;
553
554   if (myVolume->IsPoly()) {
555     MESSAGE("Warning: attempt to inverse polyhedral volume");
556     return;
557   }
558
559   myVolForward = !myVolForward;
560   myCurFace = -1;
561
562   // inverse top and bottom faces
563   switch ( myVolumeNbNodes ) {
564   case 4:
565     SWAP_NODES( myVolumeNodes, 1, 2 );
566     break;
567   case 5:
568     SWAP_NODES( myVolumeNodes, 1, 3 );
569     break;
570   case 6:
571     SWAP_NODES( myVolumeNodes, 1, 2 );
572     SWAP_NODES( myVolumeNodes, 4, 5 );
573     break;
574   case 8:
575     SWAP_NODES( myVolumeNodes, 1, 3 );
576     SWAP_NODES( myVolumeNodes, 5, 7 );
577     break;
578
579   case 10:
580     SWAP_NODES( myVolumeNodes, 1, 2 );
581     SWAP_NODES( myVolumeNodes, 4, 6 );
582     SWAP_NODES( myVolumeNodes, 8, 9 );
583     break;
584   case 13:
585     SWAP_NODES( myVolumeNodes, 1, 3 );
586     SWAP_NODES( myVolumeNodes, 5, 8 );
587     SWAP_NODES( myVolumeNodes, 6, 7 );
588     SWAP_NODES( myVolumeNodes, 10, 12 );
589     break;
590   case 15:
591     SWAP_NODES( myVolumeNodes, 1, 2 );
592     SWAP_NODES( myVolumeNodes, 4, 5 );
593     SWAP_NODES( myVolumeNodes, 6, 8 );
594     SWAP_NODES( myVolumeNodes, 9, 11 );
595     SWAP_NODES( myVolumeNodes, 13, 14 );
596     break;
597   case 20:
598     SWAP_NODES( myVolumeNodes, 1, 3 );
599     SWAP_NODES( myVolumeNodes, 5, 7 );
600     SWAP_NODES( myVolumeNodes, 8, 11 );
601     SWAP_NODES( myVolumeNodes, 9, 10 );
602     SWAP_NODES( myVolumeNodes, 12, 15 );
603     SWAP_NODES( myVolumeNodes, 13, 14 );
604     SWAP_NODES( myVolumeNodes, 17, 19 );
605     break;
606   default:;
607   }
608 }
609
610 //=======================================================================
611 //function : GetVolumeType
612 //purpose  : 
613 //=======================================================================
614
615 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetVolumeType() const
616 {
617   if ( myPolyedre )
618     return POLYHEDA;
619
620   if ( myVolume ) {
621 //    static const VolumeType types[] = {
622 //      TETRA,     // myVolumeNbNodes = 4
623 //      PYRAM,     // myVolumeNbNodes = 5
624 //      PENTA,     // myVolumeNbNodes = 6
625 //      UNKNOWN,   // myVolumeNbNodes = 7
626 //      HEXA       // myVolumeNbNodes = 8
627 //    };
628 //    return types[ myVolumeNbNodes - 4 ];
629     switch(myVolumeNbNodes) {
630     case 4: return TETRA; break;
631     case 5: return PYRAM; break;
632     case 6: return PENTA; break;
633     case 8: return HEXA; break;
634     case 10: return QUAD_TETRA; break;
635     case 13: return QUAD_PYRAM; break;
636     case 15: return QUAD_PENTA; break;
637     case 20: return QUAD_HEXA; break;
638     default: break;
639     }
640   }
641
642   return UNKNOWN;
643 }
644
645 //=======================================================================
646 //function : getTetraVolume
647 //purpose  : 
648 //=======================================================================
649
650 static double getTetraVolume(const SMDS_MeshNode* n1,
651                              const SMDS_MeshNode* n2,
652                              const SMDS_MeshNode* n3,
653                              const SMDS_MeshNode* n4)
654 {
655   double X1 = n1->X();
656   double Y1 = n1->Y();
657   double Z1 = n1->Z();
658
659   double X2 = n2->X();
660   double Y2 = n2->Y();
661   double Z2 = n2->Z();
662
663   double X3 = n3->X();
664   double Y3 = n3->Y();
665   double Z3 = n3->Z();
666
667   double X4 = n4->X();
668   double Y4 = n4->Y();
669   double Z4 = n4->Z();
670
671   double Q1 = -(X1-X2)*(Y3*Z4-Y4*Z3);
672   double Q2 =  (X1-X3)*(Y2*Z4-Y4*Z2);
673   double R1 = -(X1-X4)*(Y2*Z3-Y3*Z2);
674   double R2 = -(X2-X3)*(Y1*Z4-Y4*Z1);
675   double S1 =  (X2-X4)*(Y1*Z3-Y3*Z1);
676   double S2 = -(X3-X4)*(Y1*Z2-Y2*Z1);
677
678   return (Q1+Q2+R1+R2+S1+S2)/6.0;
679 }
680
681 //=======================================================================
682 //function : GetSize
683 //purpose  : Return element volume
684 //=======================================================================
685
686 double SMDS_VolumeTool::GetSize() const
687 {
688   double V = 0.;
689   if ( !myVolume )
690     return 0.;
691
692   if ( myVolume->IsPoly() )
693   {
694     if ( !myPolyedre )
695       return 0.;
696
697     // split a polyhedron into tetrahedrons
698
699     SMDS_VolumeTool* me = const_cast< SMDS_VolumeTool* > ( this );
700     XYZ baryCenter;
701     me->GetBaryCenter(baryCenter.x, baryCenter.y, baryCenter.z);
702     SMDS_MeshNode bcNode ( baryCenter.x, baryCenter.y, baryCenter.z );
703
704     for ( int f = 0; f < NbFaces(); ++f )
705     {
706       bool externalFace = me->IsFaceExternal( f ); // it calls setFace()
707       for ( int n = 2; n < myFaceNbNodes; ++n )
708       {
709         double Vn = getTetraVolume( myFaceNodes[ 0 ],
710                                     myFaceNodes[ n-1 ],
711                                     myFaceNodes[ n ],
712                                     & bcNode );
713 ///         cout <<"++++   " << Vn << "   nodes " <<myFaceNodes[ 0 ]->GetID() << " " <<myFaceNodes[ n-1 ]->GetID() << " " <<myFaceNodes[ n ]->GetID() << "        < " << V << endl;
714         V += externalFace ? -Vn : Vn;
715       }
716     }
717   }
718   else 
719   {
720     const static int ind[] = {
721       0, 1, 3, 6, 11, 19, 32, 46, 66};
722     const static int vtab[][4] = {
723       // tetrahedron
724       { 0, 1, 2, 3 },
725       // pyramid
726       { 0, 1, 3, 4 },
727       { 1, 2, 3, 4 },
728       // pentahedron
729       { 0, 1, 2, 3 },
730       { 1, 5, 3, 4 },
731       { 1, 5, 2, 3 },
732       // hexahedron
733       { 1, 4, 3, 0 },
734       { 4, 1, 6, 5 },
735       { 1, 3, 6, 2 },
736       { 4, 6, 3, 7 },
737       { 1, 4, 6, 3 },
738
739       // quadratic tetrahedron
740       { 0, 4, 6, 7 },
741       { 1, 5, 4, 8 },
742       { 2, 6, 5, 9 },
743       { 7, 8, 9, 3 },
744       { 4, 6, 7, 9 },
745       { 4, 5, 6, 9 },
746       { 4, 7, 8, 9 },
747       { 4, 5, 9, 8 },
748
749       // quadratic pyramid
750       { 0, 5, 8, 9 },
751       { 1, 5,10, 6 },
752       { 2, 6,11, 7 },
753       { 3, 7,12, 8 },
754       { 4, 9,11,10 },
755       { 4, 9,12,11 },
756       { 10, 5, 9, 8 },
757       { 10, 8, 9,12 },
758       { 10, 8,12, 7 },
759       { 10, 7,12,11 },
760       { 10, 7,11, 6 },
761       { 10, 5, 8, 6 },
762       { 10, 6, 8, 7 },
763
764       // quadratic pentahedron
765       { 12, 0, 8, 6 },
766       { 12, 8, 7, 6 },
767       { 12, 8, 2, 7 },
768       { 12, 6, 7, 1 },
769       { 12, 1, 7,13 },
770       { 12, 7, 2,13 },
771       { 12, 2,14,13 },
772
773       { 12, 3, 9,11 },
774       { 12,11, 9,10 },
775       { 12,11,10, 5 },
776       { 12, 9, 4,10 },
777       { 12,14, 5,10 },
778       { 12,14,10, 4 },
779       { 12,14, 4,13 },
780
781       // quadratic hexahedron
782       { 16, 0,11, 8 },
783       { 16,11, 9, 8 },
784       { 16, 8, 9, 1 },
785       { 16,11, 3,10 },
786       { 16,11,10, 9 },
787       { 16,10, 2, 9 },
788       { 16, 3,19, 2 },
789       { 16, 2,19,18 },
790       { 16, 2,18,17 },
791       { 16, 2,17, 1 },
792
793       { 16, 4,12,15 },
794       { 16,12, 5,13 },
795       { 16,12,13,15 },
796       { 16,13, 6,14 },
797       { 16,13,14,15 },
798       { 16,14, 7,15 },
799       { 16, 6, 5,17 },
800       { 16,18, 6,17 },
801       { 16,18, 7, 6 },
802       { 16,18,19, 7 },
803
804     };
805
806     int type = GetVolumeType();
807     int n1 = ind[type];
808     int n2 = ind[type+1];
809
810     for (int i = n1; i <  n2; i++) {
811       V -= getTetraVolume( myVolumeNodes[ vtab[i][0] ],
812                            myVolumeNodes[ vtab[i][1] ],
813                            myVolumeNodes[ vtab[i][2] ],
814                            myVolumeNodes[ vtab[i][3] ]);
815     }
816   }
817   return V;
818 }
819
820 //=======================================================================
821 //function : GetBaryCenter
822 //purpose  : 
823 //=======================================================================
824
825 bool SMDS_VolumeTool::GetBaryCenter(double & X, double & Y, double & Z) const
826 {
827   X = Y = Z = 0.;
828   if ( !myVolume )
829     return false;
830
831   for ( int i = 0; i < myVolumeNbNodes; i++ ) {
832     X += myVolumeNodes[ i ]->X();
833     Y += myVolumeNodes[ i ]->Y();
834     Z += myVolumeNodes[ i ]->Z();
835   }
836   X /= myVolumeNbNodes;
837   Y /= myVolumeNbNodes;
838   Z /= myVolumeNbNodes;
839
840   return true;
841 }
842
843 //================================================================================
844 /*!
845  * \brief Classify a point
846  *  \param tol - thickness of faces
847  */
848 //================================================================================
849
850 bool SMDS_VolumeTool::IsOut(double X, double Y, double Z, double tol)
851 {
852   // LIMITATION: for convex volumes only
853   XYZ p( X,Y,Z );
854   for ( int iF = 0; iF < myNbFaces; ++iF )
855   {
856     XYZ faceNormal;
857     if ( !GetFaceNormal( iF, faceNormal.x, faceNormal.y, faceNormal.z ))
858       continue;
859     if ( !IsFaceExternal( iF ))
860       faceNormal = XYZ() - faceNormal; // reverse
861
862     XYZ face2p( p - XYZ( myFaceNodes[0] ));
863     if ( face2p.Dot( faceNormal ) > tol )
864       return true;
865   }
866   return false;
867 }
868
869 //=======================================================================
870 //function : SetExternalNormal
871 //purpose  : Node order will be so that faces normals are external
872 //=======================================================================
873
874 void SMDS_VolumeTool::SetExternalNormal ()
875 {
876   myExternalFaces = true;
877   myCurFace = -1;
878 }
879
880 //=======================================================================
881 //function : NbFaceNodes
882 //purpose  : Return number of nodes in the array of face nodes
883 //=======================================================================
884
885 int SMDS_VolumeTool::NbFaceNodes( int faceIndex )
886 {
887     if ( !setFace( faceIndex ))
888       return 0;
889     return myFaceNbNodes;
890 }
891
892 //=======================================================================
893 //function : GetFaceNodes
894 //purpose  : Return pointer to the array of face nodes.
895 //           To comfort link iteration, the array
896 //           length == NbFaceNodes( faceIndex ) + 1 and
897 //           the last node == the first one.
898 //=======================================================================
899
900 const SMDS_MeshNode** SMDS_VolumeTool::GetFaceNodes( int faceIndex )
901 {
902   if ( !setFace( faceIndex ))
903     return 0;
904   return myFaceNodes;
905 }
906
907 //=======================================================================
908 //function : GetFaceNodesIndices
909 //purpose  : Return pointer to the array of face nodes indices
910 //           To comfort link iteration, the array
911 //           length == NbFaceNodes( faceIndex ) + 1 and
912 //           the last node index == the first one.
913 //=======================================================================
914
915 const int* SMDS_VolumeTool::GetFaceNodesIndices( int faceIndex )
916 {
917   if (myVolume->IsPoly()) {
918     MESSAGE("Warning: attempt to obtain FaceNodesIndices of polyhedral volume");
919     return NULL;
920   }
921   if ( !setFace( faceIndex ))
922     return 0;
923   return myFaceNodeIndices;
924 }
925
926 //=======================================================================
927 //function : GetFaceNodes
928 //purpose  : Return a set of face nodes.
929 //=======================================================================
930
931 bool SMDS_VolumeTool::GetFaceNodes (int                        faceIndex,
932                                     set<const SMDS_MeshNode*>& theFaceNodes )
933 {
934   if ( !setFace( faceIndex ))
935     return false;
936
937   theFaceNodes.clear();
938   int iNode, nbNode = myFaceNbNodes;
939   for ( iNode = 0; iNode < nbNode; iNode++ )
940     theFaceNodes.insert( myFaceNodes[ iNode ]);
941
942   return true;
943 }
944
945 //=======================================================================
946 //function : IsFaceExternal
947 //purpose  : Check normal orientation of a returned face
948 //=======================================================================
949
950 bool SMDS_VolumeTool::IsFaceExternal( int faceIndex )
951 {
952   if ( myExternalFaces || !myVolume )
953     return true;
954
955   if (myVolume->IsPoly()) {
956     XYZ aNormal, baryCenter, p0 (myPolyedre->GetFaceNode(faceIndex + 1, 1));
957     GetFaceNormal(faceIndex, aNormal.x, aNormal.y, aNormal.z);
958     GetBaryCenter(baryCenter.x, baryCenter.y, baryCenter.z);
959     XYZ insideVec (baryCenter - p0);
960     if (insideVec.Dot(aNormal) > 0)
961       return false;
962     return true;
963   }
964
965   switch ( myVolumeNbNodes ) {
966   case 4:
967   case 5:
968   case 10:
969   case 13:
970     // only the bottom of a reversed tetrahedron can be internal
971     return ( myVolForward || faceIndex != 0 );
972   case 6:
973   case 15:
974     // in a forward pentahedron, the top is internal, in a reversed one - bottom
975     return ( myVolForward ? faceIndex != 1 : faceIndex != 0 );
976   case 8:
977   case 20: {
978     // in a forward hexahedron, even face normal is external, odd - internal
979     bool odd = faceIndex % 2;
980     return ( myVolForward ? !odd : odd );
981   }
982   default:;
983   }
984   return false;
985 }
986
987 //=======================================================================
988 //function : GetFaceNormal
989 //purpose  : Return a normal to a face
990 //=======================================================================
991
992 bool SMDS_VolumeTool::GetFaceNormal (int faceIndex, double & X, double & Y, double & Z)
993 {
994   if ( !setFace( faceIndex ))
995     return false;
996
997   XYZ p1 ( myFaceNodes[0] );
998   XYZ p2 ( myFaceNodes[1] );
999   XYZ p3 ( myFaceNodes[2] );
1000   XYZ aVec12( p2 - p1 );
1001   XYZ aVec13( p3 - p1 );
1002   XYZ cross = aVec12.Crossed( aVec13 );
1003
1004   //if ( myFaceNbNodes == 4 ) {
1005   if ( myFaceNbNodes >3 ) {
1006     XYZ p4 ( myFaceNodes[3] );
1007     XYZ aVec14( p4 - p1 );
1008     XYZ cross2 = aVec13.Crossed( aVec14 );
1009     cross.x += cross2.x;
1010     cross.y += cross2.y;
1011     cross.z += cross2.z;    
1012   }
1013
1014   double size = cross.Magnitude();
1015   if ( size <= DBL_MIN )
1016     return false;
1017
1018   X = cross.x / size;
1019   Y = cross.y / size;
1020   Z = cross.z / size;
1021
1022   return true;
1023 }
1024
1025 //=======================================================================
1026 //function : GetFaceArea
1027 //purpose  : Return face area
1028 //=======================================================================
1029
1030 double SMDS_VolumeTool::GetFaceArea( int faceIndex )
1031 {
1032   if (myVolume->IsPoly()) {
1033     MESSAGE("Warning: attempt to obtain area of a face of polyhedral volume");
1034     return 0;
1035   }
1036
1037   if ( !setFace( faceIndex ))
1038     return 0;
1039
1040   XYZ p1 ( myFaceNodes[0] );
1041   XYZ p2 ( myFaceNodes[1] );
1042   XYZ p3 ( myFaceNodes[2] );
1043   XYZ aVec12( p2 - p1 );
1044   XYZ aVec13( p3 - p1 );
1045   double area = aVec12.Crossed( aVec13 ).Magnitude() * 0.5;
1046
1047   if ( myFaceNbNodes == 4 ) {
1048     XYZ p4 ( myFaceNodes[3] );
1049     XYZ aVec14( p4 - p1 );
1050     area += aVec14.Crossed( aVec13 ).Magnitude() * 0.5;
1051   }
1052   return area;
1053 }
1054
1055 //=======================================================================
1056 //function : GetOppFaceIndex
1057 //purpose  : Return index of the opposite face if it exists, else -1.
1058 //=======================================================================
1059
1060 int SMDS_VolumeTool::GetOppFaceIndex( int faceIndex ) const
1061 {
1062   int ind = -1;
1063   if (myVolume->IsPoly()) {
1064     MESSAGE("Warning: attempt to obtain opposite face on polyhedral volume");
1065     return ind;
1066   }
1067
1068   if ( faceIndex >= 0 && faceIndex < NbFaces() ) {
1069     switch ( myVolumeNbNodes ) {
1070     case 6:
1071       if ( faceIndex == 0 || faceIndex == 1 )
1072         ind = 1 - faceIndex;
1073         break;
1074     case 8:
1075       ind = faceIndex + ( faceIndex % 2 ? -1 : 1 );
1076       break;
1077     default:;
1078     }
1079   }
1080   return ind;
1081 }
1082
1083 //=======================================================================
1084 //function : IsLinked
1085 //purpose  : return true if theNode1 is linked with theNode2
1086 //=======================================================================
1087
1088 bool SMDS_VolumeTool::IsLinked (const SMDS_MeshNode* theNode1,
1089                                 const SMDS_MeshNode* theNode2) const
1090 {
1091   if ( !myVolume )
1092     return false;
1093
1094   if (myVolume->IsPoly()) {
1095     if (!myPolyedre) {
1096       MESSAGE("Warning: bad volumic element");
1097       return false;
1098     }
1099     bool isLinked = false;
1100     int iface;
1101     for (iface = 1; iface <= myNbFaces && !isLinked; iface++) {
1102       int inode, nbFaceNodes = myPolyedre->NbFaceNodes(iface);
1103
1104       for (inode = 1; inode <= nbFaceNodes && !isLinked; inode++) {
1105         const SMDS_MeshNode* curNode = myPolyedre->GetFaceNode(iface, inode);
1106
1107         if (curNode == theNode1 || curNode == theNode2) {
1108           int inextnode = (inode == nbFaceNodes) ? 1 : inode + 1;
1109           const SMDS_MeshNode* nextNode = myPolyedre->GetFaceNode(iface, inextnode);
1110
1111           if ((curNode == theNode1 && nextNode == theNode2) ||
1112               (curNode == theNode2 && nextNode == theNode1)) {
1113             isLinked = true;
1114           }
1115         }
1116       }
1117     }
1118     return isLinked;
1119   }
1120
1121   // find nodes indices
1122   int i1 = -1, i2 = -1;
1123   for ( int i = 0; i < myVolumeNbNodes; i++ ) {
1124     if ( myVolumeNodes[ i ] == theNode1 )
1125       i1 = i;
1126     else if ( myVolumeNodes[ i ] == theNode2 )
1127       i2 = i;
1128   }
1129   return IsLinked( i1, i2 );
1130 }
1131
1132 //=======================================================================
1133 //function : IsLinked
1134 //purpose  : return true if the node with theNode1Index is linked
1135 //           with the node with theNode2Index
1136 //=======================================================================
1137
1138 bool SMDS_VolumeTool::IsLinked (const int theNode1Index,
1139                                 const int theNode2Index) const
1140 {
1141   if ( myVolume->IsPoly() ) {
1142     return IsLinked(myVolumeNodes[theNode1Index], myVolumeNodes[theNode2Index]);
1143   }
1144
1145   int minInd = min( theNode1Index, theNode2Index );
1146   int maxInd = max( theNode1Index, theNode2Index );
1147
1148   if ( minInd < 0 || maxInd > myVolumeNbNodes - 1 || maxInd == minInd )
1149     return false;
1150
1151   switch ( myVolumeNbNodes ) {
1152   case 4:
1153     return true;
1154   case 5:
1155     if ( maxInd == 4 )
1156       return true;
1157     switch ( maxInd - minInd ) {
1158     case 1:
1159     case 3: return true;
1160     default:;
1161     }
1162     break;
1163   case 6:
1164     switch ( maxInd - minInd ) {
1165     case 1: return minInd != 2;
1166     case 2: return minInd == 0 || minInd == 3;
1167     case 3: return true;
1168     default:;
1169     }
1170     break;
1171   case 8:
1172     switch ( maxInd - minInd ) {
1173     case 1: return minInd != 3;
1174     case 3: return minInd == 0 || minInd == 4;
1175     case 4: return true;
1176     default:;
1177     }
1178     break;
1179   case 10:
1180     {
1181       switch ( minInd ) {
1182       case 0: if( maxInd==4 ||  maxInd==6 ||  maxInd==7 ) return true;
1183       case 1: if( maxInd==4 ||  maxInd==5 ||  maxInd==8 ) return true;
1184       case 2: if( maxInd==5 ||  maxInd==6 ||  maxInd==9 ) return true;
1185       case 3: if( maxInd==7 ||  maxInd==8 ||  maxInd==9 ) return true;
1186       default:;
1187       }
1188       break;
1189     }
1190   case 13:
1191     {
1192       switch ( minInd ) {
1193       case 0: if( maxInd==5 ||  maxInd==8 ||  maxInd==9 ) return true;
1194       case 1: if( maxInd==5 ||  maxInd==6 ||  maxInd==10 ) return true;
1195       case 2: if( maxInd==6 ||  maxInd==7 ||  maxInd==11 ) return true;
1196       case 3: if( maxInd==7 ||  maxInd==8 ||  maxInd==12 ) return true;
1197       case 4: if( maxInd==9 ||  maxInd==10 ||  maxInd==11 ||  maxInd==12 ) return true;
1198       default:;
1199       }
1200       break;
1201     }
1202   case 15:
1203     {
1204       switch ( minInd ) {
1205       case 0: if( maxInd==6 ||  maxInd==8 ||  maxInd==12 ) return true;
1206       case 1: if( maxInd==6 ||  maxInd==7 ||  maxInd==13 ) return true;
1207       case 2: if( maxInd==7 ||  maxInd==8 ||  maxInd==14 ) return true;
1208       case 3: if( maxInd==9 ||  maxInd==11 ||  maxInd==12 ) return true;
1209       case 4: if( maxInd==9 ||  maxInd==10 ||  maxInd==13 ) return true;
1210       case 5: if( maxInd==10 ||  maxInd==11 ||  maxInd==14 ) return true;
1211       default:;
1212       }
1213       break;
1214     }
1215   case 20:
1216     {
1217       switch ( minInd ) {
1218       case 0: if( maxInd==8 ||  maxInd==11 ||  maxInd==16 ) return true;
1219       case 1: if( maxInd==8 ||  maxInd==9 ||  maxInd==17 ) return true;
1220       case 2: if( maxInd==9 ||  maxInd==10 ||  maxInd==18 ) return true;
1221       case 3: if( maxInd==10 ||  maxInd==11 ||  maxInd==19 ) return true;
1222       case 4: if( maxInd==12 ||  maxInd==15 ||  maxInd==16 ) return true;
1223       case 5: if( maxInd==12 ||  maxInd==13 ||  maxInd==17 ) return true;
1224       case 6: if( maxInd==13 ||  maxInd==14 ||  maxInd==18 ) return true;
1225       case 7: if( maxInd==14 ||  maxInd==15 ||  maxInd==19 ) return true;
1226       default:;
1227       }
1228       break;
1229     }
1230   default:;
1231   }
1232   return false;
1233 }
1234
1235 //=======================================================================
1236 //function : GetNodeIndex
1237 //purpose  : Return an index of theNode
1238 //=======================================================================
1239
1240 int SMDS_VolumeTool::GetNodeIndex(const SMDS_MeshNode* theNode) const
1241 {
1242   if ( myVolume ) {
1243     for ( int i = 0; i < myVolumeNbNodes; i++ ) {
1244       if ( myVolumeNodes[ i ] == theNode )
1245         return i;
1246     }
1247   }
1248   return -1;
1249 }
1250
1251 //================================================================================
1252 /*!
1253  * \brief Fill vector with boundary faces existing in the mesh
1254   * \param faces - vector of found nodes
1255   * \retval int - nb of found faces
1256  */
1257 //================================================================================
1258
1259 int SMDS_VolumeTool::GetAllExistingFaces(vector<const SMDS_MeshElement*> & faces)
1260 {
1261   faces.clear();
1262   faces.reserve( NbFaces() );
1263   for ( int iF = 0; iF < NbFaces(); ++iF ) {
1264     const SMDS_MeshFace* face = 0;
1265     const SMDS_MeshNode** nodes = GetFaceNodes( iF );
1266     switch ( NbFaceNodes( iF )) {
1267     case 3:
1268       face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2] ); break;
1269     case 4:
1270       face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2], nodes[3] ); break;
1271     case 6:
1272       face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2],
1273                                   nodes[3], nodes[4], nodes[5]); break;
1274     case 8:
1275       face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2], nodes[3],
1276                                   nodes[4], nodes[5], nodes[6], nodes[7]); break;
1277     }
1278     if ( face )
1279       faces.push_back( face );
1280   }
1281   return faces.size();
1282 }
1283
1284
1285 //================================================================================
1286 /*!
1287  * \brief Fill vector with boundary edges existing in the mesh
1288   * \param edges - vector of found edges
1289   * \retval int - nb of found faces
1290  */
1291 //================================================================================
1292
1293 int SMDS_VolumeTool::GetAllExistingEdges(vector<const SMDS_MeshElement*> & edges) const
1294 {
1295   edges.clear();
1296   edges.reserve( myVolumeNbNodes * 2 );
1297   for ( int i = 0; i < myVolumeNbNodes; ++i ) {
1298     for ( int j = i + 1; j < myVolumeNbNodes; ++j ) {
1299       if ( IsLinked( i, j )) {
1300         const SMDS_MeshElement* edge =
1301           SMDS_Mesh::FindEdge( myVolumeNodes[i], myVolumeNodes[j] );
1302         if ( edge )
1303           edges.push_back( edge );
1304       }
1305     }
1306   }
1307   return edges.size();
1308 }
1309
1310 //=======================================================================
1311 //function : IsFreeFace
1312 //purpose  : check that only one volume is build on the face nodes
1313 //=======================================================================
1314
1315 bool SMDS_VolumeTool::IsFreeFace( int faceIndex )
1316 {
1317   const int free = true;
1318
1319   if (!setFace( faceIndex ))
1320     return !free;
1321
1322   const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
1323   int nbFaceNodes = myFaceNbNodes;
1324
1325   // evaluate nb of face nodes shared by other volume
1326   int maxNbShared = -1;
1327   typedef map< const SMDS_MeshElement*, int > TElemIntMap;
1328   TElemIntMap volNbShared;
1329   TElemIntMap::iterator vNbIt;
1330   for ( int iNode = 0; iNode < nbFaceNodes; iNode++ ) {
1331     const SMDS_MeshNode* n = nodes[ iNode ];
1332     SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator();
1333     while ( eIt->more() ) {
1334       const SMDS_MeshElement* elem = eIt->next();
1335       if ( elem != myVolume && elem->GetType() == SMDSAbs_Volume ) {
1336         int nbShared = 1;
1337         vNbIt = volNbShared.find( elem );
1338         if ( vNbIt == volNbShared.end() ) {
1339           volNbShared.insert ( TElemIntMap::value_type( elem, nbShared ));
1340         }
1341         else {
1342           nbShared = ++(*vNbIt).second;
1343         }
1344         if ( nbShared > maxNbShared )
1345           maxNbShared = nbShared;
1346       }
1347     }
1348   }
1349   if ( maxNbShared < 3 )
1350     return free; // is free
1351
1352   // find volumes laying on the opposite side of the face
1353   // and sharing all nodes
1354   XYZ intNormal; // internal normal
1355   GetFaceNormal( faceIndex, intNormal.x, intNormal.y, intNormal.z );
1356   if ( IsFaceExternal( faceIndex ))
1357     intNormal = XYZ( -intNormal.x, -intNormal.y, -intNormal.z );
1358   XYZ p0 ( nodes[0] ), baryCenter;
1359   for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ ) {
1360     int nbShared = (*vNbIt).second;
1361     if ( nbShared >= 3 ) {
1362       SMDS_VolumeTool volume( (*vNbIt).first );
1363       volume.GetBaryCenter( baryCenter.x, baryCenter.y, baryCenter.z );
1364       XYZ intNormal2( baryCenter - p0 );
1365       if ( intNormal.Dot( intNormal2 ) < 0 )
1366         continue; // opposite side
1367     }
1368     // remove a volume from volNbShared map
1369     volNbShared.erase( vNbIt-- );
1370   }
1371
1372   // here volNbShared contains only volumes laying on the
1373   // opposite side of the face
1374   if ( volNbShared.empty() ) {
1375     return free; // is free
1376   }
1377
1378   // check if the whole area of a face is shared
1379   bool isShared[] = { false, false, false, false }; // 4 triangle parts of a quadrangle
1380   for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ ) {
1381     SMDS_VolumeTool volume( (*vNbIt).first );
1382     bool prevLinkShared = false;
1383     int nbSharedLinks = 0;
1384     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ ) {
1385       bool linkShared = volume.IsLinked( nodes[ iNode ], nodes[ iNode + 1] );
1386       if ( linkShared )
1387         nbSharedLinks++;
1388       if ( linkShared && prevLinkShared &&
1389           volume.IsLinked( nodes[ iNode - 1 ], nodes[ iNode + 1] ))
1390         isShared[ iNode ] = true;
1391       prevLinkShared = linkShared;
1392     }
1393     if ( nbSharedLinks == nbFaceNodes )
1394       return !free; // is not free
1395     if ( nbFaceNodes == 4 ) {
1396       // check traingle parts 1 & 3
1397       if ( isShared[1] && isShared[3] )
1398         return !free; // is not free
1399       // check triangle parts 0 & 2;
1400       // 0 part could not be checked in the loop; check it here
1401       if ( isShared[2] && prevLinkShared &&
1402           volume.IsLinked( nodes[ 0 ], nodes[ 1 ] ) &&
1403           volume.IsLinked( nodes[ 1 ], nodes[ 3 ] ) )
1404         return !free; // is not free
1405     }
1406   }
1407   return free;
1408 }
1409
1410 //=======================================================================
1411 //function : GetFaceIndex
1412 //purpose  : Return index of a face formed by theFaceNodes
1413 //=======================================================================
1414
1415 int SMDS_VolumeTool::GetFaceIndex( const set<const SMDS_MeshNode*>& theFaceNodes )
1416 {
1417   for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
1418     const SMDS_MeshNode** nodes = GetFaceNodes( iFace );
1419     int nbFaceNodes = NbFaceNodes( iFace );
1420     set<const SMDS_MeshNode*> nodeSet;
1421     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1422       nodeSet.insert( nodes[ iNode ] );
1423     if ( theFaceNodes == nodeSet )
1424       return iFace;
1425   }
1426   return -1;
1427 }
1428
1429 //=======================================================================
1430 //function : GetFaceIndex
1431 //purpose  : Return index of a face formed by theFaceNodes
1432 //=======================================================================
1433
1434 /*int SMDS_VolumeTool::GetFaceIndex( const set<int>& theFaceNodesIndices )
1435 {
1436   for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
1437     const int* nodes = GetFaceNodesIndices( iFace );
1438     int nbFaceNodes = NbFaceNodes( iFace );
1439     set<int> nodeSet;
1440     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1441       nodeSet.insert( nodes[ iNode ] );
1442     if ( theFaceNodesIndices == nodeSet )
1443       return iFace;
1444   }
1445   return -1;
1446 }*/
1447
1448 //=======================================================================
1449 //function : setFace
1450 //purpose  : 
1451 //=======================================================================
1452
1453 bool SMDS_VolumeTool::setFace( int faceIndex )
1454 {
1455   if ( !myVolume )
1456     return false;
1457
1458   if ( myCurFace == faceIndex )
1459     return true;
1460
1461   myCurFace = -1;
1462
1463   if ( faceIndex < 0 || faceIndex >= NbFaces() )
1464     return false;
1465
1466   if (myFaceNodes != NULL) {
1467     delete [] myFaceNodes;
1468     myFaceNodes = NULL;
1469   }
1470
1471   if (myVolume->IsPoly()) {
1472     if (!myPolyedre) {
1473       MESSAGE("Warning: bad volumic element");
1474       return false;
1475     }
1476
1477     // check orientation
1478     bool isGoodOri = true;
1479     if (myExternalFaces)
1480       isGoodOri = IsFaceExternal( faceIndex );
1481
1482     // set face nodes
1483     int iNode;
1484     myFaceNbNodes = myPolyedre->NbFaceNodes(faceIndex + 1);
1485     myFaceNodes = new const SMDS_MeshNode* [myFaceNbNodes + 1];
1486     if (isGoodOri) {
1487       for ( iNode = 0; iNode < myFaceNbNodes; iNode++ )
1488         myFaceNodes[ iNode ] = myPolyedre->GetFaceNode(faceIndex + 1, iNode + 1);
1489     } else {
1490       for ( iNode = 0; iNode < myFaceNbNodes; iNode++ )
1491         myFaceNodes[ iNode ] = myPolyedre->GetFaceNode(faceIndex + 1, myFaceNbNodes - iNode);
1492     }
1493     myFaceNodes[ myFaceNbNodes ] = myFaceNodes[ 0 ]; // last = first
1494
1495   }
1496   else {
1497     // choose face node indices
1498     switch ( myVolumeNbNodes ) {
1499     case 4:
1500       myFaceNbNodes = Tetra_nbN[ faceIndex ];
1501       if ( myExternalFaces )
1502         myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_RE[ faceIndex ];
1503       else
1504         myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_R[ faceIndex ];
1505       break;
1506     case 5:
1507       myFaceNbNodes = Pyramid_nbN[ faceIndex ];
1508       if ( myExternalFaces )
1509         myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_RE[ faceIndex ];
1510       else
1511         myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_R[ faceIndex ];
1512       break;
1513     case 6:
1514       myFaceNbNodes = Penta_nbN[ faceIndex ];
1515       if ( myExternalFaces )
1516         myFaceNodeIndices = myVolForward ? Penta_FE[ faceIndex ] : Penta_RE[ faceIndex ];
1517       else
1518         myFaceNodeIndices = myVolForward ? Penta_F[ faceIndex ] : Penta_R[ faceIndex ];
1519       break;
1520     case 8:
1521       myFaceNbNodes = Hexa_nbN[ faceIndex ];
1522       if ( myExternalFaces )
1523         myFaceNodeIndices = myVolForward ? Hexa_FE[ faceIndex ] : Hexa_RE[ faceIndex ];
1524       else
1525         myFaceNodeIndices = Hexa_F[ faceIndex ];
1526       break;
1527     case 10:
1528       myFaceNbNodes = QuadTetra_nbN[ faceIndex ];
1529       if ( myExternalFaces )
1530         myFaceNodeIndices = myVolForward ? QuadTetra_F[ faceIndex ] : QuadTetra_RE[ faceIndex ];
1531       else
1532         myFaceNodeIndices = myVolForward ? QuadTetra_F[ faceIndex ] : QuadTetra_R[ faceIndex ];
1533       break;
1534     case 13:
1535       myFaceNbNodes = QuadPyram_nbN[ faceIndex ];
1536       if ( myExternalFaces )
1537         myFaceNodeIndices = myVolForward ? QuadPyram_F[ faceIndex ] : QuadPyram_RE[ faceIndex ];
1538       else
1539         myFaceNodeIndices = myVolForward ? QuadPyram_F[ faceIndex ] : QuadPyram_R[ faceIndex ];
1540       break;
1541     case 15:
1542       myFaceNbNodes = QuadPenta_nbN[ faceIndex ];
1543       if ( myExternalFaces )
1544         myFaceNodeIndices = myVolForward ? QuadPenta_FE[ faceIndex ] : QuadPenta_RE[ faceIndex ];
1545       else
1546         myFaceNodeIndices = myVolForward ? QuadPenta_F[ faceIndex ] : QuadPenta_R[ faceIndex ];
1547       break;
1548     case 20:
1549       myFaceNbNodes = QuadHexa_nbN[ faceIndex ];
1550       if ( myExternalFaces )
1551         myFaceNodeIndices = myVolForward ? QuadHexa_FE[ faceIndex ] : QuadHexa_RE[ faceIndex ];
1552       else
1553         myFaceNodeIndices = QuadHexa_F[ faceIndex ];
1554       break;
1555     default:
1556       return false;
1557     }
1558
1559     // set face nodes
1560     myFaceNodes = new const SMDS_MeshNode* [myFaceNbNodes + 1];
1561     for ( int iNode = 0; iNode < myFaceNbNodes; iNode++ )
1562       myFaceNodes[ iNode ] = myVolumeNodes[ myFaceNodeIndices[ iNode ]];
1563     myFaceNodes[ myFaceNbNodes ] = myFaceNodes[ 0 ];
1564   }
1565
1566   myCurFace = faceIndex;
1567
1568   return true;
1569 }
1570
1571 //=======================================================================
1572 //function : GetType
1573 //purpose  : return VolumeType by nb of nodes in a volume
1574 //=======================================================================
1575
1576 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetType(int nbNodes)
1577 {
1578   switch ( nbNodes ) {
1579   case 4: return TETRA;
1580   case 5: return PYRAM;
1581   case 6: return PENTA;
1582   case 8: return HEXA;
1583   case 10: return QUAD_TETRA;
1584   case 13: return QUAD_PYRAM;
1585   case 15: return QUAD_PENTA;
1586   case 20: return QUAD_HEXA;
1587   default:return UNKNOWN;
1588   }
1589 }
1590
1591 //=======================================================================
1592 //function : NbFaces
1593 //purpose  : return nb of faces by volume type
1594 //=======================================================================
1595
1596 int SMDS_VolumeTool::NbFaces( VolumeType type )
1597 {
1598   switch ( type ) {
1599   case TETRA     :
1600   case QUAD_TETRA: return 4;
1601   case PYRAM     :
1602   case QUAD_PYRAM: return 5;
1603   case PENTA     :
1604   case QUAD_PENTA: return 5;
1605   case HEXA      :
1606   case QUAD_HEXA : return 6;
1607   default:    return 0;
1608   }
1609 }
1610
1611 //================================================================================
1612 /*!
1613  * \brief Useful to know nb of corner nodes of a quadratic volume
1614   * \param type - volume type
1615   * \retval int - nb of corner nodes
1616  */
1617 //================================================================================
1618
1619 int SMDS_VolumeTool::NbCornerNodes(VolumeType type)
1620 {
1621   switch ( type ) {
1622   case TETRA     :
1623   case QUAD_TETRA: return 4;
1624   case PYRAM     :
1625   case QUAD_PYRAM: return 5;
1626   case PENTA     :
1627   case QUAD_PENTA: return 6;
1628   case HEXA      :
1629   case QUAD_HEXA : return 8;
1630   default:    return 0;
1631   }
1632   return 0;
1633 }
1634   // 
1635
1636 //=======================================================================
1637 //function : GetFaceNodesIndices
1638 //purpose  : Return the array of face nodes indices
1639 //           To comfort link iteration, the array
1640 //           length == NbFaceNodes( faceIndex ) + 1 and
1641 //           the last node index == the first one.
1642 //=======================================================================
1643
1644 const int* SMDS_VolumeTool::GetFaceNodesIndices(VolumeType type,
1645                                                 int        faceIndex,
1646                                                 bool       external)
1647 {
1648   switch ( type ) {
1649   case TETRA: return Tetra_F[ faceIndex ];
1650   case PYRAM: return Pyramid_F[ faceIndex ];
1651   case PENTA: return external ? Penta_FE[ faceIndex ] : Penta_F[ faceIndex ];
1652   case HEXA:  return external ? Hexa_FE[ faceIndex ] : Hexa_F[ faceIndex ];
1653   case QUAD_TETRA: return QuadTetra_F[ faceIndex ];
1654   case QUAD_PYRAM: return QuadPyram_F[ faceIndex ];
1655   case QUAD_PENTA: return external ? QuadPenta_FE[ faceIndex ] : QuadPenta_F[ faceIndex ];
1656   case QUAD_HEXA:  return external ? QuadHexa_FE[ faceIndex ] : QuadHexa_F[ faceIndex ];
1657   default:;
1658   }
1659   return 0;
1660 }
1661
1662 //=======================================================================
1663 //function : NbFaceNodes
1664 //purpose  : Return number of nodes in the array of face nodes
1665 //=======================================================================
1666
1667 int SMDS_VolumeTool::NbFaceNodes(VolumeType type,
1668                                  int        faceIndex )
1669 {
1670   switch ( type ) {
1671   case TETRA: return Tetra_nbN[ faceIndex ];
1672   case PYRAM: return Pyramid_nbN[ faceIndex ];
1673   case PENTA: return Penta_nbN[ faceIndex ];
1674   case HEXA:  return Hexa_nbN[ faceIndex ];
1675   case QUAD_TETRA: return QuadTetra_nbN[ faceIndex ];
1676   case QUAD_PYRAM: return QuadPyram_nbN[ faceIndex ];
1677   case QUAD_PENTA: return QuadPenta_nbN[ faceIndex ];
1678   case QUAD_HEXA:  return QuadHexa_nbN[ faceIndex ];
1679   default:;
1680   }
1681   return 0;
1682 }
1683
1684 //=======================================================================
1685 //function : Get
1686 //purpose  : return element
1687 //=======================================================================
1688
1689 const SMDS_MeshVolume* SMDS_VolumeTool::Get() const
1690 {
1691   return static_cast<const SMDS_MeshVolume*>( myVolume );
1692 }
1693
1694 //=======================================================================
1695 //function : ID
1696 //purpose  : return element ID
1697 //=======================================================================
1698
1699 int SMDS_VolumeTool::ID() const
1700 {
1701   return myVolume ? myVolume->GetID() : 0;
1702 }