Salome HOME
Merge from BR_V5_DEV 16Feb09
[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 struct XYZ {
372   double x;
373   double y;
374   double z;
375   XYZ()                               { x = 0; y = 0; z = 0; }
376   XYZ( double X, double Y, double Z ) { x = X; y = Y; z = Z; }
377   XYZ( const XYZ& other )             { x = other.x; y = other.y; z = other.z; }
378   XYZ( const SMDS_MeshNode* n )       { x = n->X(); y = n->Y(); z = n->Z(); }
379   XYZ operator-( const XYZ& other );
380   XYZ Crossed( const XYZ& other );
381   double Dot( const XYZ& other );
382   double Magnitude();
383 };
384 XYZ XYZ::operator-( const XYZ& Right ) {
385   return XYZ(x - Right.x, y - Right.y, z - Right.z);
386 }
387 XYZ XYZ::Crossed( const XYZ& Right ) {
388   return XYZ (y * Right.z - z * Right.y,
389               z * Right.x - x * Right.z,
390               x * Right.y - y * Right.x);
391 }
392 double XYZ::Dot( const XYZ& Other ) {
393   return(x * Other.x + y * Other.y + z * Other.z);
394 }
395 double XYZ::Magnitude() {
396   return sqrt (x * x + y * y + z * z);
397 }
398
399 //=======================================================================
400 //function : SMDS_VolumeTool
401 //purpose  : 
402 //=======================================================================
403
404 SMDS_VolumeTool::SMDS_VolumeTool ()
405      : myVolume( 0 ),
406        myPolyedre( 0 ),
407        myVolForward( true ),
408        myNbFaces( 0 ),
409        myVolumeNbNodes( 0 ),
410        myVolumeNodes( NULL ),
411        myExternalFaces( false ),
412        myFaceNbNodes( 0 ),
413        myCurFace( -1 ),
414        myFaceNodeIndices( NULL ),
415        myFaceNodes( NULL )
416 {
417 }
418
419 //=======================================================================
420 //function : SMDS_VolumeTool
421 //purpose  : 
422 //=======================================================================
423
424 SMDS_VolumeTool::SMDS_VolumeTool (const SMDS_MeshElement* theVolume)
425      : myVolume( 0 ),
426        myPolyedre( 0 ),
427        myVolForward( true ),
428        myNbFaces( 0 ),
429        myVolumeNbNodes( 0 ),
430        myVolumeNodes( NULL ),
431        myExternalFaces( false ),
432        myFaceNbNodes( 0 ),
433        myCurFace( -1 ),
434        myFaceNodeIndices( NULL ),
435        myFaceNodes( NULL )
436 {
437   Set( theVolume );
438 }
439
440 //=======================================================================
441 //function : SMDS_VolumeTool
442 //purpose  : 
443 //=======================================================================
444
445 SMDS_VolumeTool::~SMDS_VolumeTool()
446 {
447   if (myVolumeNodes != NULL) {
448     delete [] myVolumeNodes;
449     myVolumeNodes = NULL;
450   }
451   if (myFaceNodes != NULL) {
452     delete [] myFaceNodes;
453     myFaceNodes = NULL;
454   }
455 }
456
457 //=======================================================================
458 //function : SetVolume
459 //purpose  : Set volume to iterate on
460 //=======================================================================
461
462 bool SMDS_VolumeTool::Set (const SMDS_MeshElement* theVolume)
463 {
464   myVolume = 0;
465   myPolyedre = 0;
466
467   myVolForward = true;
468   myNbFaces = 0;
469   myVolumeNbNodes = 0;
470   if (myVolumeNodes != NULL) {
471     delete [] myVolumeNodes;
472     myVolumeNodes = NULL;
473   }
474
475   myExternalFaces = false;
476   myFaceNbNodes = 0;
477
478   myCurFace = -1;
479   myFaceNodeIndices = NULL;
480   if (myFaceNodes != NULL) {
481     delete [] myFaceNodes;
482     myFaceNodes = NULL;
483   }
484
485   if ( theVolume && theVolume->GetType() == SMDSAbs_Volume )
486   {
487     myVolume = theVolume;
488
489     myNbFaces = theVolume->NbFaces();
490     myVolumeNbNodes = theVolume->NbNodes();
491
492     // set volume nodes
493     int iNode = 0;
494     myVolumeNodes = new const SMDS_MeshNode* [myVolumeNbNodes];
495     SMDS_ElemIteratorPtr nodeIt = myVolume->nodesIterator();
496     while ( nodeIt->more() ) {
497       myVolumeNodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
498     }
499
500     if (myVolume->IsPoly()) {
501       myPolyedre = static_cast<const SMDS_PolyhedralVolumeOfNodes*>( myVolume );
502       if (!myPolyedre) {
503         MESSAGE("Warning: bad volumic element");
504         return false;
505       }
506     }
507     else {
508       switch ( myVolumeNbNodes ) {
509       case 4:
510       case 5:
511       case 6:
512       case 8:
513       case 10:
514       case 13:
515       case 15:
516       case 20: {
517         // define volume orientation
518         XYZ botNormal;
519         GetFaceNormal( 0, botNormal.x, botNormal.y, botNormal.z );
520         const SMDS_MeshNode* topNode = myVolumeNodes[ myVolumeNbNodes - 1 ];
521         const SMDS_MeshNode* botNode = myVolumeNodes[ 0 ];
522         XYZ upDir (topNode->X() - botNode->X(),
523                    topNode->Y() - botNode->Y(),
524                    topNode->Z() - botNode->Z() );
525         myVolForward = ( botNormal.Dot( upDir ) < 0 );
526         break;
527       }
528       default:
529         break;
530       }
531     }
532   }
533   return ( myVolume != 0 );
534 }
535
536 //=======================================================================
537 //function : Inverse
538 //purpose  : Inverse volume
539 //=======================================================================
540
541 #define SWAP_NODES(nodes,i1,i2)           \
542 {                                         \
543   const SMDS_MeshNode* tmp = nodes[ i1 ]; \
544   nodes[ i1 ] = nodes[ i2 ];              \
545   nodes[ i2 ] = tmp;                      \
546 }
547 void SMDS_VolumeTool::Inverse ()
548 {
549   if ( !myVolume ) return;
550
551   if (myVolume->IsPoly()) {
552     MESSAGE("Warning: attempt to inverse polyhedral volume");
553     return;
554   }
555
556   myVolForward = !myVolForward;
557   myCurFace = -1;
558
559   // inverse top and bottom faces
560   switch ( myVolumeNbNodes ) {
561   case 4:
562     SWAP_NODES( myVolumeNodes, 1, 2 );
563     break;
564   case 5:
565     SWAP_NODES( myVolumeNodes, 1, 3 );
566     break;
567   case 6:
568     SWAP_NODES( myVolumeNodes, 1, 2 );
569     SWAP_NODES( myVolumeNodes, 4, 5 );
570     break;
571   case 8:
572     SWAP_NODES( myVolumeNodes, 1, 3 );
573     SWAP_NODES( myVolumeNodes, 5, 7 );
574     break;
575
576   case 10:
577     SWAP_NODES( myVolumeNodes, 1, 2 );
578     SWAP_NODES( myVolumeNodes, 4, 6 );
579     SWAP_NODES( myVolumeNodes, 8, 9 );
580     break;
581   case 13:
582     SWAP_NODES( myVolumeNodes, 1, 3 );
583     SWAP_NODES( myVolumeNodes, 5, 8 );
584     SWAP_NODES( myVolumeNodes, 6, 7 );
585     SWAP_NODES( myVolumeNodes, 10, 12 );
586     break;
587   case 15:
588     SWAP_NODES( myVolumeNodes, 1, 2 );
589     SWAP_NODES( myVolumeNodes, 4, 5 );
590     SWAP_NODES( myVolumeNodes, 6, 8 );
591     SWAP_NODES( myVolumeNodes, 9, 11 );
592     SWAP_NODES( myVolumeNodes, 13, 14 );
593     break;
594   case 20:
595     SWAP_NODES( myVolumeNodes, 1, 3 );
596     SWAP_NODES( myVolumeNodes, 5, 7 );
597     SWAP_NODES( myVolumeNodes, 8, 11 );
598     SWAP_NODES( myVolumeNodes, 9, 10 );
599     SWAP_NODES( myVolumeNodes, 12, 15 );
600     SWAP_NODES( myVolumeNodes, 13, 14 );
601     SWAP_NODES( myVolumeNodes, 17, 19 );
602     break;
603   default:;
604   }
605 }
606
607 //=======================================================================
608 //function : GetVolumeType
609 //purpose  : 
610 //=======================================================================
611
612 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetVolumeType() const
613 {
614   if ( myPolyedre )
615     return POLYHEDA;
616
617   if ( myVolume ) {
618 //    static const VolumeType types[] = {
619 //      TETRA,     // myVolumeNbNodes = 4
620 //      PYRAM,     // myVolumeNbNodes = 5
621 //      PENTA,     // myVolumeNbNodes = 6
622 //      UNKNOWN,   // myVolumeNbNodes = 7
623 //      HEXA       // myVolumeNbNodes = 8
624 //    };
625 //    return types[ myVolumeNbNodes - 4 ];
626     switch(myVolumeNbNodes) {
627     case 4: return TETRA; break;
628     case 5: return PYRAM; break;
629     case 6: return PENTA; break;
630     case 8: return HEXA; break;
631     case 10: return QUAD_TETRA; break;
632     case 13: return QUAD_PYRAM; break;
633     case 15: return QUAD_PENTA; break;
634     case 20: return QUAD_HEXA; break;
635     default: break;
636     }
637   }
638
639   return UNKNOWN;
640 }
641
642 //=======================================================================
643 //function : getTetraVolume
644 //purpose  : 
645 //=======================================================================
646
647 static double getTetraVolume(const SMDS_MeshNode* n1,
648                              const SMDS_MeshNode* n2,
649                              const SMDS_MeshNode* n3,
650                              const SMDS_MeshNode* n4)
651 {
652   double X1 = n1->X();
653   double Y1 = n1->Y();
654   double Z1 = n1->Z();
655
656   double X2 = n2->X();
657   double Y2 = n2->Y();
658   double Z2 = n2->Z();
659
660   double X3 = n3->X();
661   double Y3 = n3->Y();
662   double Z3 = n3->Z();
663
664   double X4 = n4->X();
665   double Y4 = n4->Y();
666   double Z4 = n4->Z();
667
668   double Q1 = -(X1-X2)*(Y3*Z4-Y4*Z3);
669   double Q2 =  (X1-X3)*(Y2*Z4-Y4*Z2);
670   double R1 = -(X1-X4)*(Y2*Z3-Y3*Z2);
671   double R2 = -(X2-X3)*(Y1*Z4-Y4*Z1);
672   double S1 =  (X2-X4)*(Y1*Z3-Y3*Z1);
673   double S2 = -(X3-X4)*(Y1*Z2-Y2*Z1);
674
675   return (Q1+Q2+R1+R2+S1+S2)/6.0;
676 }
677
678 //=======================================================================
679 //function : GetSize
680 //purpose  : Return element volume
681 //=======================================================================
682
683 double SMDS_VolumeTool::GetSize() const
684 {
685   double V = 0.;
686   if ( !myVolume )
687     return 0.;
688
689   if ( myVolume->IsPoly() )
690   {
691     if ( !myPolyedre )
692       return 0.;
693
694     // split a polyhedron into tetrahedrons
695
696     SMDS_VolumeTool* me = const_cast< SMDS_VolumeTool* > ( this );
697     XYZ baryCenter;
698     me->GetBaryCenter(baryCenter.x, baryCenter.y, baryCenter.z);
699     SMDS_MeshNode bcNode ( baryCenter.x, baryCenter.y, baryCenter.z );
700
701     for ( int f = 0; f < NbFaces(); ++f )
702     {
703       bool externalFace = me->IsFaceExternal( f ); // it calls setFace()
704       for ( int n = 2; n < myFaceNbNodes; ++n )
705       {
706         double Vn = getTetraVolume( myFaceNodes[ 0 ],
707                                     myFaceNodes[ n-1 ],
708                                     myFaceNodes[ n ],
709                                     & bcNode );
710 ///         cout <<"++++   " << Vn << "   nodes " <<myFaceNodes[ 0 ]->GetID() << " " <<myFaceNodes[ n-1 ]->GetID() << " " <<myFaceNodes[ n ]->GetID() << "        < " << V << endl;
711         V += externalFace ? -Vn : Vn;
712       }
713     }
714   }
715   else 
716   {
717     const static int ind[] = {
718       0, 1, 3, 6, 11, 19, 32, 46, 66};
719     const static int vtab[][4] = {
720       // tetrahedron
721       { 0, 1, 2, 3 },
722       // pyramid
723       { 0, 1, 3, 4 },
724       { 1, 2, 3, 4 },
725       // pentahedron
726       { 0, 1, 2, 3 },
727       { 1, 5, 3, 4 },
728       { 1, 5, 2, 3 },
729       // hexahedron
730       { 1, 4, 3, 0 },
731       { 4, 1, 6, 5 },
732       { 1, 3, 6, 2 },
733       { 4, 6, 3, 7 },
734       { 1, 4, 6, 3 },
735
736       // quadratic tetrahedron
737       { 0, 4, 6, 7 },
738       { 1, 5, 4, 8 },
739       { 2, 6, 5, 9 },
740       { 7, 8, 9, 3 },
741       { 4, 6, 7, 9 },
742       { 4, 5, 6, 9 },
743       { 4, 7, 8, 9 },
744       { 4, 5, 9, 8 },
745
746       // quadratic pyramid
747       { 0, 5, 8, 9 },
748       { 1, 5,10, 6 },
749       { 2, 6,11, 7 },
750       { 3, 7,12, 8 },
751       { 4, 9,11,10 },
752       { 4, 9,12,11 },
753       { 10, 5, 9, 8 },
754       { 10, 8, 9,12 },
755       { 10, 8,12, 7 },
756       { 10, 7,12,11 },
757       { 10, 7,11, 6 },
758       { 10, 5, 8, 6 },
759       { 10, 6, 8, 7 },
760
761       // quadratic pentahedron
762       { 12, 0, 8, 6 },
763       { 12, 8, 7, 6 },
764       { 12, 8, 2, 7 },
765       { 12, 6, 7, 1 },
766       { 12, 1, 7,13 },
767       { 12, 7, 2,13 },
768       { 12, 2,14,13 },
769
770       { 12, 3, 9,11 },
771       { 12,11, 9,10 },
772       { 12,11,10, 5 },
773       { 12, 9, 4,10 },
774       { 12,14, 5,10 },
775       { 12,14,10, 4 },
776       { 12,14, 4,13 },
777
778       // quadratic hexahedron
779       { 16, 0,11, 8 },
780       { 16,11, 9, 8 },
781       { 16, 8, 9, 1 },
782       { 16,11, 3,10 },
783       { 16,11,10, 9 },
784       { 16,10, 2, 9 },
785       { 16, 3,19, 2 },
786       { 16, 2,19,18 },
787       { 16, 2,18,17 },
788       { 16, 2,17, 1 },
789
790       { 16, 4,12,15 },
791       { 16,12, 5,13 },
792       { 16,12,13,15 },
793       { 16,13, 6,14 },
794       { 16,13,14,15 },
795       { 16,14, 7,15 },
796       { 16, 6, 5,17 },
797       { 16,18, 6,17 },
798       { 16,18, 7, 6 },
799       { 16,18,19, 7 },
800
801     };
802
803     int type = GetVolumeType();
804     int n1 = ind[type];
805     int n2 = ind[type+1];
806
807     for (int i = n1; i <  n2; i++) {
808       V -= getTetraVolume( myVolumeNodes[ vtab[i][0] ],
809                            myVolumeNodes[ vtab[i][1] ],
810                            myVolumeNodes[ vtab[i][2] ],
811                            myVolumeNodes[ vtab[i][3] ]);
812     }
813   }
814   return V;
815 }
816
817 //=======================================================================
818 //function : GetBaryCenter
819 //purpose  : 
820 //=======================================================================
821
822 bool SMDS_VolumeTool::GetBaryCenter(double & X, double & Y, double & Z) const
823 {
824   X = Y = Z = 0.;
825   if ( !myVolume )
826     return false;
827
828   for ( int i = 0; i < myVolumeNbNodes; i++ ) {
829     X += myVolumeNodes[ i ]->X();
830     Y += myVolumeNodes[ i ]->Y();
831     Z += myVolumeNodes[ i ]->Z();
832   }
833   X /= myVolumeNbNodes;
834   Y /= myVolumeNbNodes;
835   Z /= myVolumeNbNodes;
836
837   return true;
838 }
839
840 //=======================================================================
841 //function : SetExternalNormal
842 //purpose  : Node order will be so that faces normals are external
843 //=======================================================================
844
845 void SMDS_VolumeTool::SetExternalNormal ()
846 {
847   myExternalFaces = true;
848   myCurFace = -1;
849 }
850
851 //=======================================================================
852 //function : NbFaceNodes
853 //purpose  : Return number of nodes in the array of face nodes
854 //=======================================================================
855
856 int SMDS_VolumeTool::NbFaceNodes( int faceIndex )
857 {
858     if ( !setFace( faceIndex ))
859       return 0;
860     return myFaceNbNodes;
861 }
862
863 //=======================================================================
864 //function : GetFaceNodes
865 //purpose  : Return pointer to the array of face nodes.
866 //           To comfort link iteration, the array
867 //           length == NbFaceNodes( faceIndex ) + 1 and
868 //           the last node == the first one.
869 //=======================================================================
870
871 const SMDS_MeshNode** SMDS_VolumeTool::GetFaceNodes( int faceIndex )
872 {
873   if ( !setFace( faceIndex ))
874     return 0;
875   return myFaceNodes;
876 }
877
878 //=======================================================================
879 //function : GetFaceNodesIndices
880 //purpose  : Return pointer to the array of face nodes indices
881 //           To comfort link iteration, the array
882 //           length == NbFaceNodes( faceIndex ) + 1 and
883 //           the last node index == the first one.
884 //=======================================================================
885
886 const int* SMDS_VolumeTool::GetFaceNodesIndices( int faceIndex )
887 {
888   if (myVolume->IsPoly()) {
889     MESSAGE("Warning: attempt to obtain FaceNodesIndices of polyhedral volume");
890     return NULL;
891   }
892   if ( !setFace( faceIndex ))
893     return 0;
894   return myFaceNodeIndices;
895 }
896
897 //=======================================================================
898 //function : GetFaceNodes
899 //purpose  : Return a set of face nodes.
900 //=======================================================================
901
902 bool SMDS_VolumeTool::GetFaceNodes (int                        faceIndex,
903                                     set<const SMDS_MeshNode*>& theFaceNodes )
904 {
905   if ( !setFace( faceIndex ))
906     return false;
907
908   theFaceNodes.clear();
909   int iNode, nbNode = myFaceNbNodes;
910   for ( iNode = 0; iNode < nbNode; iNode++ )
911     theFaceNodes.insert( myFaceNodes[ iNode ]);
912
913   return true;
914 }
915
916 //=======================================================================
917 //function : IsFaceExternal
918 //purpose  : Check normal orientation of a returned face
919 //=======================================================================
920
921 bool SMDS_VolumeTool::IsFaceExternal( int faceIndex )
922 {
923   if ( myExternalFaces || !myVolume )
924     return true;
925
926   if (myVolume->IsPoly()) {
927     XYZ aNormal, baryCenter, p0 (myPolyedre->GetFaceNode(faceIndex + 1, 1));
928     GetFaceNormal(faceIndex, aNormal.x, aNormal.y, aNormal.z);
929     GetBaryCenter(baryCenter.x, baryCenter.y, baryCenter.z);
930     XYZ insideVec (baryCenter - p0);
931     if (insideVec.Dot(aNormal) > 0)
932       return false;
933     return true;
934   }
935
936   switch ( myVolumeNbNodes ) {
937   case 4:
938   case 5:
939   case 10:
940   case 13:
941     // only the bottom of a reversed tetrahedron can be internal
942     return ( myVolForward || faceIndex != 0 );
943   case 6:
944   case 15:
945     // in a forward pentahedron, the top is internal, in a reversed one - bottom
946     return ( myVolForward ? faceIndex != 1 : faceIndex != 0 );
947   case 8:
948   case 20: {
949     // in a forward hexahedron, even face normal is external, odd - internal
950     bool odd = faceIndex % 2;
951     return ( myVolForward ? !odd : odd );
952   }
953   default:;
954   }
955   return false;
956 }
957
958 //=======================================================================
959 //function : GetFaceNormal
960 //purpose  : Return a normal to a face
961 //=======================================================================
962
963 bool SMDS_VolumeTool::GetFaceNormal (int faceIndex, double & X, double & Y, double & Z)
964 {
965   if ( !setFace( faceIndex ))
966     return false;
967
968   XYZ p1 ( myFaceNodes[0] );
969   XYZ p2 ( myFaceNodes[1] );
970   XYZ p3 ( myFaceNodes[2] );
971   XYZ aVec12( p2 - p1 );
972   XYZ aVec13( p3 - p1 );
973   XYZ cross = aVec12.Crossed( aVec13 );
974
975   //if ( myFaceNbNodes == 4 ) {
976   if ( myFaceNbNodes >3 ) {
977     XYZ p4 ( myFaceNodes[3] );
978     XYZ aVec14( p4 - p1 );
979     XYZ cross2 = aVec13.Crossed( aVec14 );
980     cross.x += cross2.x;
981     cross.y += cross2.y;
982     cross.z += cross2.z;    
983   }
984
985   double size = cross.Magnitude();
986   if ( size <= DBL_MIN )
987     return false;
988
989   X = cross.x / size;
990   Y = cross.y / size;
991   Z = cross.z / size;
992
993   return true;
994 }
995
996 //=======================================================================
997 //function : GetFaceArea
998 //purpose  : Return face area
999 //=======================================================================
1000
1001 double SMDS_VolumeTool::GetFaceArea( int faceIndex )
1002 {
1003   if (myVolume->IsPoly()) {
1004     MESSAGE("Warning: attempt to obtain area of a face of polyhedral volume");
1005     return 0;
1006   }
1007
1008   if ( !setFace( faceIndex ))
1009     return 0;
1010
1011   XYZ p1 ( myFaceNodes[0] );
1012   XYZ p2 ( myFaceNodes[1] );
1013   XYZ p3 ( myFaceNodes[2] );
1014   XYZ aVec12( p2 - p1 );
1015   XYZ aVec13( p3 - p1 );
1016   double area = aVec12.Crossed( aVec13 ).Magnitude() * 0.5;
1017
1018   if ( myFaceNbNodes == 4 ) {
1019     XYZ p4 ( myFaceNodes[3] );
1020     XYZ aVec14( p4 - p1 );
1021     area += aVec14.Crossed( aVec13 ).Magnitude() * 0.5;
1022   }
1023   return area;
1024 }
1025
1026 //=======================================================================
1027 //function : GetOppFaceIndex
1028 //purpose  : Return index of the opposite face if it exists, else -1.
1029 //=======================================================================
1030
1031 int SMDS_VolumeTool::GetOppFaceIndex( int faceIndex ) const
1032 {
1033   int ind = -1;
1034   if (myVolume->IsPoly()) {
1035     MESSAGE("Warning: attempt to obtain opposite face on polyhedral volume");
1036     return ind;
1037   }
1038
1039   if ( faceIndex >= 0 && faceIndex < NbFaces() ) {
1040     switch ( myVolumeNbNodes ) {
1041     case 6:
1042       if ( faceIndex == 0 || faceIndex == 1 )
1043         ind = 1 - faceIndex;
1044         break;
1045     case 8:
1046       ind = faceIndex + ( faceIndex % 2 ? -1 : 1 );
1047       break;
1048     default:;
1049     }
1050   }
1051   return ind;
1052 }
1053
1054 //=======================================================================
1055 //function : IsLinked
1056 //purpose  : return true if theNode1 is linked with theNode2
1057 //=======================================================================
1058
1059 bool SMDS_VolumeTool::IsLinked (const SMDS_MeshNode* theNode1,
1060                                 const SMDS_MeshNode* theNode2) const
1061 {
1062   if ( !myVolume )
1063     return false;
1064
1065   if (myVolume->IsPoly()) {
1066     if (!myPolyedre) {
1067       MESSAGE("Warning: bad volumic element");
1068       return false;
1069     }
1070     bool isLinked = false;
1071     int iface;
1072     for (iface = 1; iface <= myNbFaces && !isLinked; iface++) {
1073       int inode, nbFaceNodes = myPolyedre->NbFaceNodes(iface);
1074
1075       for (inode = 1; inode <= nbFaceNodes && !isLinked; inode++) {
1076         const SMDS_MeshNode* curNode = myPolyedre->GetFaceNode(iface, inode);
1077
1078         if (curNode == theNode1 || curNode == theNode2) {
1079           int inextnode = (inode == nbFaceNodes) ? 1 : inode + 1;
1080           const SMDS_MeshNode* nextNode = myPolyedre->GetFaceNode(iface, inextnode);
1081
1082           if ((curNode == theNode1 && nextNode == theNode2) ||
1083               (curNode == theNode2 && nextNode == theNode1)) {
1084             isLinked = true;
1085           }
1086         }
1087       }
1088     }
1089     return isLinked;
1090   }
1091
1092   // find nodes indices
1093   int i1 = -1, i2 = -1;
1094   for ( int i = 0; i < myVolumeNbNodes; i++ ) {
1095     if ( myVolumeNodes[ i ] == theNode1 )
1096       i1 = i;
1097     else if ( myVolumeNodes[ i ] == theNode2 )
1098       i2 = i;
1099   }
1100   return IsLinked( i1, i2 );
1101 }
1102
1103 //=======================================================================
1104 //function : IsLinked
1105 //purpose  : return true if the node with theNode1Index is linked
1106 //           with the node with theNode2Index
1107 //=======================================================================
1108
1109 bool SMDS_VolumeTool::IsLinked (const int theNode1Index,
1110                                 const int theNode2Index) const
1111 {
1112   if ( myVolume->IsPoly() ) {
1113     return IsLinked(myVolumeNodes[theNode1Index], myVolumeNodes[theNode2Index]);
1114   }
1115
1116   int minInd = min( theNode1Index, theNode2Index );
1117   int maxInd = max( theNode1Index, theNode2Index );
1118
1119   if ( minInd < 0 || maxInd > myVolumeNbNodes - 1 || maxInd == minInd )
1120     return false;
1121
1122   switch ( myVolumeNbNodes ) {
1123   case 4:
1124     return true;
1125   case 5:
1126     if ( maxInd == 4 )
1127       return true;
1128     switch ( maxInd - minInd ) {
1129     case 1:
1130     case 3: return true;
1131     default:;
1132     }
1133     break;
1134   case 6:
1135     switch ( maxInd - minInd ) {
1136     case 1: return minInd != 2;
1137     case 2: return minInd == 0 || minInd == 3;
1138     case 3: return true;
1139     default:;
1140     }
1141     break;
1142   case 8:
1143     switch ( maxInd - minInd ) {
1144     case 1: return minInd != 3;
1145     case 3: return minInd == 0 || minInd == 4;
1146     case 4: return true;
1147     default:;
1148     }
1149     break;
1150   case 10:
1151     {
1152       switch ( minInd ) {
1153       case 0: if( maxInd==4 ||  maxInd==6 ||  maxInd==7 ) return true;
1154       case 1: if( maxInd==4 ||  maxInd==5 ||  maxInd==8 ) return true;
1155       case 2: if( maxInd==5 ||  maxInd==6 ||  maxInd==9 ) return true;
1156       case 3: if( maxInd==7 ||  maxInd==8 ||  maxInd==9 ) return true;
1157       default:;
1158       }
1159       break;
1160     }
1161   case 13:
1162     {
1163       switch ( minInd ) {
1164       case 0: if( maxInd==5 ||  maxInd==8 ||  maxInd==9 ) return true;
1165       case 1: if( maxInd==5 ||  maxInd==6 ||  maxInd==10 ) return true;
1166       case 2: if( maxInd==6 ||  maxInd==7 ||  maxInd==11 ) return true;
1167       case 3: if( maxInd==7 ||  maxInd==8 ||  maxInd==12 ) return true;
1168       case 4: if( maxInd==9 ||  maxInd==10 ||  maxInd==11 ||  maxInd==12 ) return true;
1169       default:;
1170       }
1171       break;
1172     }
1173   case 15:
1174     {
1175       switch ( minInd ) {
1176       case 0: if( maxInd==6 ||  maxInd==8 ||  maxInd==12 ) return true;
1177       case 1: if( maxInd==6 ||  maxInd==7 ||  maxInd==13 ) return true;
1178       case 2: if( maxInd==7 ||  maxInd==8 ||  maxInd==14 ) return true;
1179       case 3: if( maxInd==9 ||  maxInd==11 ||  maxInd==12 ) return true;
1180       case 4: if( maxInd==9 ||  maxInd==10 ||  maxInd==13 ) return true;
1181       case 5: if( maxInd==10 ||  maxInd==11 ||  maxInd==14 ) return true;
1182       default:;
1183       }
1184       break;
1185     }
1186   case 20:
1187     {
1188       switch ( minInd ) {
1189       case 0: if( maxInd==8 ||  maxInd==11 ||  maxInd==16 ) return true;
1190       case 1: if( maxInd==8 ||  maxInd==9 ||  maxInd==17 ) return true;
1191       case 2: if( maxInd==9 ||  maxInd==10 ||  maxInd==18 ) return true;
1192       case 3: if( maxInd==10 ||  maxInd==11 ||  maxInd==19 ) return true;
1193       case 4: if( maxInd==12 ||  maxInd==15 ||  maxInd==16 ) return true;
1194       case 5: if( maxInd==12 ||  maxInd==13 ||  maxInd==17 ) return true;
1195       case 6: if( maxInd==13 ||  maxInd==14 ||  maxInd==18 ) return true;
1196       case 7: if( maxInd==14 ||  maxInd==15 ||  maxInd==19 ) return true;
1197       default:;
1198       }
1199       break;
1200     }
1201   default:;
1202   }
1203   return false;
1204 }
1205
1206 //=======================================================================
1207 //function : GetNodeIndex
1208 //purpose  : Return an index of theNode
1209 //=======================================================================
1210
1211 int SMDS_VolumeTool::GetNodeIndex(const SMDS_MeshNode* theNode) const
1212 {
1213   if ( myVolume ) {
1214     for ( int i = 0; i < myVolumeNbNodes; i++ ) {
1215       if ( myVolumeNodes[ i ] == theNode )
1216         return i;
1217     }
1218   }
1219   return -1;
1220 }
1221
1222 //================================================================================
1223 /*!
1224  * \brief Fill vector with boundary faces existing in the mesh
1225   * \param faces - vector of found nodes
1226   * \retval int - nb of found faces
1227  */
1228 //================================================================================
1229
1230 int SMDS_VolumeTool::GetAllExistingFaces(vector<const SMDS_MeshElement*> & faces)
1231 {
1232   faces.clear();
1233   faces.reserve( NbFaces() );
1234   for ( int iF = 0; iF < NbFaces(); ++iF ) {
1235     const SMDS_MeshFace* face = 0;
1236     const SMDS_MeshNode** nodes = GetFaceNodes( iF );
1237     switch ( NbFaceNodes( iF )) {
1238     case 3:
1239       face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2] ); break;
1240     case 4:
1241       face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2], nodes[3] ); break;
1242     case 6:
1243       face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2],
1244                                   nodes[3], nodes[4], nodes[5]); break;
1245     case 8:
1246       face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2], nodes[3],
1247                                   nodes[4], nodes[5], nodes[6], nodes[7]); break;
1248     }
1249     if ( face )
1250       faces.push_back( face );
1251   }
1252   return faces.size();
1253 }
1254
1255
1256 //================================================================================
1257 /*!
1258  * \brief Fill vector with boundary edges existing in the mesh
1259   * \param edges - vector of found edges
1260   * \retval int - nb of found faces
1261  */
1262 //================================================================================
1263
1264 int SMDS_VolumeTool::GetAllExistingEdges(vector<const SMDS_MeshElement*> & edges) const
1265 {
1266   edges.clear();
1267   edges.reserve( myVolumeNbNodes * 2 );
1268   for ( int i = 0; i < myVolumeNbNodes; ++i ) {
1269     for ( int j = i + 1; j < myVolumeNbNodes; ++j ) {
1270       if ( IsLinked( i, j )) {
1271         const SMDS_MeshElement* edge =
1272           SMDS_Mesh::FindEdge( myVolumeNodes[i], myVolumeNodes[j] );
1273         if ( edge )
1274           edges.push_back( edge );
1275       }
1276     }
1277   }
1278   return edges.size();
1279 }
1280
1281 //=======================================================================
1282 //function : IsFreeFace
1283 //purpose  : check that only one volume is build on the face nodes
1284 //=======================================================================
1285
1286 bool SMDS_VolumeTool::IsFreeFace( int faceIndex )
1287 {
1288   const int free = true;
1289
1290   if (!setFace( faceIndex ))
1291     return !free;
1292
1293   const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
1294   int nbFaceNodes = myFaceNbNodes;
1295
1296   // evaluate nb of face nodes shared by other volume
1297   int maxNbShared = -1;
1298   typedef map< const SMDS_MeshElement*, int > TElemIntMap;
1299   TElemIntMap volNbShared;
1300   TElemIntMap::iterator vNbIt;
1301   for ( int iNode = 0; iNode < nbFaceNodes; iNode++ ) {
1302     const SMDS_MeshNode* n = nodes[ iNode ];
1303     SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator();
1304     while ( eIt->more() ) {
1305       const SMDS_MeshElement* elem = eIt->next();
1306       if ( elem != myVolume && elem->GetType() == SMDSAbs_Volume ) {
1307         int nbShared = 1;
1308         vNbIt = volNbShared.find( elem );
1309         if ( vNbIt == volNbShared.end() ) {
1310           volNbShared.insert ( TElemIntMap::value_type( elem, nbShared ));
1311         }
1312         else {
1313           nbShared = ++(*vNbIt).second;
1314         }
1315         if ( nbShared > maxNbShared )
1316           maxNbShared = nbShared;
1317       }
1318     }
1319   }
1320   if ( maxNbShared < 3 )
1321     return free; // is free
1322
1323   // find volumes laying on the opposite side of the face
1324   // and sharing all nodes
1325   XYZ intNormal; // internal normal
1326   GetFaceNormal( faceIndex, intNormal.x, intNormal.y, intNormal.z );
1327   if ( IsFaceExternal( faceIndex ))
1328     intNormal = XYZ( -intNormal.x, -intNormal.y, -intNormal.z );
1329   XYZ p0 ( nodes[0] ), baryCenter;
1330   for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ ) {
1331     int nbShared = (*vNbIt).second;
1332     if ( nbShared >= 3 ) {
1333       SMDS_VolumeTool volume( (*vNbIt).first );
1334       volume.GetBaryCenter( baryCenter.x, baryCenter.y, baryCenter.z );
1335       XYZ intNormal2( baryCenter - p0 );
1336       if ( intNormal.Dot( intNormal2 ) < 0 )
1337         continue; // opposite side
1338     }
1339     // remove a volume from volNbShared map
1340     volNbShared.erase( vNbIt-- );
1341   }
1342
1343   // here volNbShared contains only volumes laying on the
1344   // opposite side of the face
1345   if ( volNbShared.empty() ) {
1346     return free; // is free
1347   }
1348
1349   // check if the whole area of a face is shared
1350   bool isShared[] = { false, false, false, false }; // 4 triangle parts of a quadrangle
1351   for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ ) {
1352     SMDS_VolumeTool volume( (*vNbIt).first );
1353     bool prevLinkShared = false;
1354     int nbSharedLinks = 0;
1355     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ ) {
1356       bool linkShared = volume.IsLinked( nodes[ iNode ], nodes[ iNode + 1] );
1357       if ( linkShared )
1358         nbSharedLinks++;
1359       if ( linkShared && prevLinkShared &&
1360           volume.IsLinked( nodes[ iNode - 1 ], nodes[ iNode + 1] ))
1361         isShared[ iNode ] = true;
1362       prevLinkShared = linkShared;
1363     }
1364     if ( nbSharedLinks == nbFaceNodes )
1365       return !free; // is not free
1366     if ( nbFaceNodes == 4 ) {
1367       // check traingle parts 1 & 3
1368       if ( isShared[1] && isShared[3] )
1369         return !free; // is not free
1370       // check triangle parts 0 & 2;
1371       // 0 part could not be checked in the loop; check it here
1372       if ( isShared[2] && prevLinkShared &&
1373           volume.IsLinked( nodes[ 0 ], nodes[ 1 ] ) &&
1374           volume.IsLinked( nodes[ 1 ], nodes[ 3 ] ) )
1375         return !free; // is not free
1376     }
1377   }
1378   return free;
1379 }
1380
1381 //=======================================================================
1382 //function : GetFaceIndex
1383 //purpose  : Return index of a face formed by theFaceNodes
1384 //=======================================================================
1385
1386 int SMDS_VolumeTool::GetFaceIndex( const set<const SMDS_MeshNode*>& theFaceNodes )
1387 {
1388   for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
1389     const SMDS_MeshNode** nodes = GetFaceNodes( iFace );
1390     int nbFaceNodes = NbFaceNodes( iFace );
1391     set<const SMDS_MeshNode*> nodeSet;
1392     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1393       nodeSet.insert( nodes[ iNode ] );
1394     if ( theFaceNodes == nodeSet )
1395       return iFace;
1396   }
1397   return -1;
1398 }
1399
1400 //=======================================================================
1401 //function : GetFaceIndex
1402 //purpose  : Return index of a face formed by theFaceNodes
1403 //=======================================================================
1404
1405 /*int SMDS_VolumeTool::GetFaceIndex( const set<int>& theFaceNodesIndices )
1406 {
1407   for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
1408     const int* nodes = GetFaceNodesIndices( iFace );
1409     int nbFaceNodes = NbFaceNodes( iFace );
1410     set<int> nodeSet;
1411     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1412       nodeSet.insert( nodes[ iNode ] );
1413     if ( theFaceNodesIndices == nodeSet )
1414       return iFace;
1415   }
1416   return -1;
1417 }*/
1418
1419 //=======================================================================
1420 //function : setFace
1421 //purpose  : 
1422 //=======================================================================
1423
1424 bool SMDS_VolumeTool::setFace( int faceIndex )
1425 {
1426   if ( !myVolume )
1427     return false;
1428
1429   if ( myCurFace == faceIndex )
1430     return true;
1431
1432   myCurFace = -1;
1433
1434   if ( faceIndex < 0 || faceIndex >= NbFaces() )
1435     return false;
1436
1437   if (myFaceNodes != NULL) {
1438     delete [] myFaceNodes;
1439     myFaceNodes = NULL;
1440   }
1441
1442   if (myVolume->IsPoly()) {
1443     if (!myPolyedre) {
1444       MESSAGE("Warning: bad volumic element");
1445       return false;
1446     }
1447
1448     // check orientation
1449     bool isGoodOri = true;
1450     if (myExternalFaces)
1451       isGoodOri = IsFaceExternal( faceIndex );
1452
1453     // set face nodes
1454     int iNode;
1455     myFaceNbNodes = myPolyedre->NbFaceNodes(faceIndex + 1);
1456     myFaceNodes = new const SMDS_MeshNode* [myFaceNbNodes + 1];
1457     if (isGoodOri) {
1458       for ( iNode = 0; iNode < myFaceNbNodes; iNode++ )
1459         myFaceNodes[ iNode ] = myPolyedre->GetFaceNode(faceIndex + 1, iNode + 1);
1460     } else {
1461       for ( iNode = 0; iNode < myFaceNbNodes; iNode++ )
1462         myFaceNodes[ iNode ] = myPolyedre->GetFaceNode(faceIndex + 1, myFaceNbNodes - iNode);
1463     }
1464     myFaceNodes[ myFaceNbNodes ] = myFaceNodes[ 0 ]; // last = first
1465
1466   }
1467   else {
1468     // choose face node indices
1469     switch ( myVolumeNbNodes ) {
1470     case 4:
1471       myFaceNbNodes = Tetra_nbN[ faceIndex ];
1472       if ( myExternalFaces )
1473         myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_RE[ faceIndex ];
1474       else
1475         myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_R[ faceIndex ];
1476       break;
1477     case 5:
1478       myFaceNbNodes = Pyramid_nbN[ faceIndex ];
1479       if ( myExternalFaces )
1480         myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_RE[ faceIndex ];
1481       else
1482         myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_R[ faceIndex ];
1483       break;
1484     case 6:
1485       myFaceNbNodes = Penta_nbN[ faceIndex ];
1486       if ( myExternalFaces )
1487         myFaceNodeIndices = myVolForward ? Penta_FE[ faceIndex ] : Penta_RE[ faceIndex ];
1488       else
1489         myFaceNodeIndices = myVolForward ? Penta_F[ faceIndex ] : Penta_R[ faceIndex ];
1490       break;
1491     case 8:
1492       myFaceNbNodes = Hexa_nbN[ faceIndex ];
1493       if ( myExternalFaces )
1494         myFaceNodeIndices = myVolForward ? Hexa_FE[ faceIndex ] : Hexa_RE[ faceIndex ];
1495       else
1496         myFaceNodeIndices = Hexa_F[ faceIndex ];
1497       break;
1498     case 10:
1499       myFaceNbNodes = QuadTetra_nbN[ faceIndex ];
1500       if ( myExternalFaces )
1501         myFaceNodeIndices = myVolForward ? QuadTetra_F[ faceIndex ] : QuadTetra_RE[ faceIndex ];
1502       else
1503         myFaceNodeIndices = myVolForward ? QuadTetra_F[ faceIndex ] : QuadTetra_R[ faceIndex ];
1504       break;
1505     case 13:
1506       myFaceNbNodes = QuadPyram_nbN[ faceIndex ];
1507       if ( myExternalFaces )
1508         myFaceNodeIndices = myVolForward ? QuadPyram_F[ faceIndex ] : QuadPyram_RE[ faceIndex ];
1509       else
1510         myFaceNodeIndices = myVolForward ? QuadPyram_F[ faceIndex ] : QuadPyram_R[ faceIndex ];
1511       break;
1512     case 15:
1513       myFaceNbNodes = QuadPenta_nbN[ faceIndex ];
1514       if ( myExternalFaces )
1515         myFaceNodeIndices = myVolForward ? QuadPenta_FE[ faceIndex ] : QuadPenta_RE[ faceIndex ];
1516       else
1517         myFaceNodeIndices = myVolForward ? QuadPenta_F[ faceIndex ] : QuadPenta_R[ faceIndex ];
1518       break;
1519     case 20:
1520       myFaceNbNodes = QuadHexa_nbN[ faceIndex ];
1521       if ( myExternalFaces )
1522         myFaceNodeIndices = myVolForward ? QuadHexa_FE[ faceIndex ] : QuadHexa_RE[ faceIndex ];
1523       else
1524         myFaceNodeIndices = QuadHexa_F[ faceIndex ];
1525       break;
1526     default:
1527       return false;
1528     }
1529
1530     // set face nodes
1531     myFaceNodes = new const SMDS_MeshNode* [myFaceNbNodes + 1];
1532     for ( int iNode = 0; iNode < myFaceNbNodes; iNode++ )
1533       myFaceNodes[ iNode ] = myVolumeNodes[ myFaceNodeIndices[ iNode ]];
1534     myFaceNodes[ myFaceNbNodes ] = myFaceNodes[ 0 ];
1535   }
1536
1537   myCurFace = faceIndex;
1538
1539   return true;
1540 }
1541
1542 //=======================================================================
1543 //function : GetType
1544 //purpose  : return VolumeType by nb of nodes in a volume
1545 //=======================================================================
1546
1547 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetType(int nbNodes)
1548 {
1549   switch ( nbNodes ) {
1550   case 4: return TETRA;
1551   case 5: return PYRAM;
1552   case 6: return PENTA;
1553   case 8: return HEXA;
1554   case 10: return QUAD_TETRA;
1555   case 13: return QUAD_PYRAM;
1556   case 15: return QUAD_PENTA;
1557   case 20: return QUAD_HEXA;
1558   default:return UNKNOWN;
1559   }
1560 }
1561
1562 //=======================================================================
1563 //function : NbFaces
1564 //purpose  : return nb of faces by volume type
1565 //=======================================================================
1566
1567 int SMDS_VolumeTool::NbFaces( VolumeType type )
1568 {
1569   switch ( type ) {
1570   case TETRA     :
1571   case QUAD_TETRA: return 4;
1572   case PYRAM     :
1573   case QUAD_PYRAM: return 5;
1574   case PENTA     :
1575   case QUAD_PENTA: return 5;
1576   case HEXA      :
1577   case QUAD_HEXA : return 6;
1578   default:    return 0;
1579   }
1580 }
1581
1582 //================================================================================
1583 /*!
1584  * \brief Useful to know nb of corner nodes of a quadratic volume
1585   * \param type - volume type
1586   * \retval int - nb of corner nodes
1587  */
1588 //================================================================================
1589
1590 int SMDS_VolumeTool::NbCornerNodes(VolumeType type)
1591 {
1592   switch ( type ) {
1593   case TETRA     :
1594   case QUAD_TETRA: return 4;
1595   case PYRAM     :
1596   case QUAD_PYRAM: return 5;
1597   case PENTA     :
1598   case QUAD_PENTA: return 6;
1599   case HEXA      :
1600   case QUAD_HEXA : return 8;
1601   default:    return 0;
1602   }
1603   return 0;
1604 }
1605   // 
1606
1607 //=======================================================================
1608 //function : GetFaceNodesIndices
1609 //purpose  : Return the array of face nodes indices
1610 //           To comfort link iteration, the array
1611 //           length == NbFaceNodes( faceIndex ) + 1 and
1612 //           the last node index == the first one.
1613 //=======================================================================
1614
1615 const int* SMDS_VolumeTool::GetFaceNodesIndices(VolumeType type,
1616                                                 int        faceIndex,
1617                                                 bool       external)
1618 {
1619   switch ( type ) {
1620   case TETRA: return Tetra_F[ faceIndex ];
1621   case PYRAM: return Pyramid_F[ faceIndex ];
1622   case PENTA: return external ? Penta_FE[ faceIndex ] : Penta_F[ faceIndex ];
1623   case HEXA:  return external ? Hexa_FE[ faceIndex ] : Hexa_F[ faceIndex ];
1624   case QUAD_TETRA: return QuadTetra_F[ faceIndex ];
1625   case QUAD_PYRAM: return QuadPyram_F[ faceIndex ];
1626   case QUAD_PENTA: return external ? QuadPenta_FE[ faceIndex ] : QuadPenta_F[ faceIndex ];
1627   case QUAD_HEXA:  return external ? QuadHexa_FE[ faceIndex ] : QuadHexa_F[ faceIndex ];
1628   default:;
1629   }
1630   return 0;
1631 }
1632
1633 //=======================================================================
1634 //function : NbFaceNodes
1635 //purpose  : Return number of nodes in the array of face nodes
1636 //=======================================================================
1637
1638 int SMDS_VolumeTool::NbFaceNodes(VolumeType type,
1639                                  int        faceIndex )
1640 {
1641   switch ( type ) {
1642   case TETRA: return Tetra_nbN[ faceIndex ];
1643   case PYRAM: return Pyramid_nbN[ faceIndex ];
1644   case PENTA: return Penta_nbN[ faceIndex ];
1645   case HEXA:  return Hexa_nbN[ faceIndex ];
1646   case QUAD_TETRA: return QuadTetra_nbN[ faceIndex ];
1647   case QUAD_PYRAM: return QuadPyram_nbN[ faceIndex ];
1648   case QUAD_PENTA: return QuadPenta_nbN[ faceIndex ];
1649   case QUAD_HEXA:  return QuadHexa_nbN[ faceIndex ];
1650   default:;
1651   }
1652   return 0;
1653 }
1654
1655 //=======================================================================
1656 //function : Get
1657 //purpose  : return element
1658 //=======================================================================
1659
1660 const SMDS_MeshVolume* SMDS_VolumeTool::Get() const
1661 {
1662   return static_cast<const SMDS_MeshVolume*>( myVolume );
1663 }
1664
1665 //=======================================================================
1666 //function : ID
1667 //purpose  : return element ID
1668 //=======================================================================
1669
1670 int SMDS_VolumeTool::ID() const
1671 {
1672   return myVolume ? myVolume->GetID() : 0;
1673 }