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