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