Salome HOME
Merge commit '6600bcec782fc8b6c72871fe6e08bd19a34a4e2b'
[modules/smesh.git] / src / SMDS / SMDS_VolumeTool.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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, or (at your option) any later version.
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_Mesh.hxx"
36
37 #include <utilities.h>
38
39 #include <map>
40 #include <limits>
41 #include <cmath>
42 #include <cstring>
43 #include <numeric>
44 #include <algorithm>
45 #include <array>
46
47 namespace
48 {
49 // ======================================================
50 // Node indices in faces depending on volume orientation
51 // making most faces normals external
52 // ======================================================
53 // For all elements, 0-th face is bottom based on the first nodes.
54 // For prismatic elements (tetra,hexa,prisms), 1-th face is a top one.
55 // For all elements, side faces follow order of bottom nodes
56 // ======================================================
57
58 /*
59 //           N3
60 //           +
61 //          /|\
62 //         / | \
63 //        /  |  \
64 //    N0 +---|---+ N1                TETRAHEDRON
65 //       \   |   /
66 //        \  |  /
67 //         \ | /
68 //          \|/
69 //           +
70 //           N2
71 */
72 static int Tetra_F [4][4] = { // FORWARD == EXTERNAL
73   { 0, 1, 2, 0 },              // All faces have external normals
74   { 0, 3, 1, 0 },
75   { 1, 3, 2, 1 },
76   { 0, 2, 3, 0 }}; 
77 static int Tetra_RE [4][4] = { // REVERSED -> FORWARD (EXTERNAL)
78   { 0, 2, 1, 0 },              // All faces have external normals
79   { 0, 1, 3, 0 },
80   { 1, 2, 3, 1 },
81   { 0, 3, 2, 0 }};
82 static int Tetra_nbN [] = { 3, 3, 3, 3 };
83
84 /*        
85 //    N1 +---------+ N2
86 //       | \     / | 
87 //       |  \   /  |
88 //       |   \ /   |
89 //       |   /+\   |     PYRAMID
90 //       |  / N4\  |
91 //       | /     \ |
92 //       |/       \|
93 //    N0 +---------+ N3
94 */
95 static int Pyramid_F [5][5] = { // FORWARD == EXTERNAL
96   { 0, 1, 2, 3, 0 },            // All faces have external normals
97   { 0, 4, 1, 0, 4 },
98   { 1, 4, 2, 1, 4 },
99   { 2, 4, 3, 2, 4 },
100   { 3, 4, 0, 3, 4 }
101 }; 
102 static int Pyramid_RE [5][5] = { // REVERSED -> FORWARD (EXTERNAL)
103   { 0, 3, 2, 1, 0 },             // All faces but a bottom have external normals
104   { 0, 1, 4, 0, 4 },
105   { 1, 2, 4, 1, 4 },
106   { 2, 3, 4, 2, 4 },
107   { 3, 0, 4, 3, 4 }}; 
108 static int Pyramid_nbN [] = { 4, 3, 3, 3, 3 };
109
110 /*   
111 //            + N4
112 //           /|\
113 //          / | \
114 //         /  |  \
115 //        /   |   \
116 //    N3 +---------+ N5
117 //       |    |    |
118 //       |    + N1 |
119 //       |   / \   |                PENTAHEDRON
120 //       |  /   \  |
121 //       | /     \ |
122 //       |/       \|
123 //    N0 +---------+ N2
124 */
125 static int Penta_F [5][5] = { // FORWARD
126   { 0, 1, 2, 0, 0 },          // All faces have external normals
127   { 3, 5, 4, 3, 3 },          // 0 is bottom, 1 is top face
128   { 0, 3, 4, 1, 0 },
129   { 1, 4, 5, 2, 1 },
130   { 0, 2, 5, 3, 0 }}; 
131 static int Penta_RE [5][5] = { // REVERSED -> EXTERNAL
132   { 0, 2, 1, 0, 0 },
133   { 3, 4, 5, 3, 3 },
134   { 0, 1, 4, 3, 0 },
135   { 1, 2, 5, 4, 1 },
136   { 0, 3, 5, 2, 0 }}; 
137 static int Penta_nbN [] = { 3, 3, 4, 4, 4 };
138
139 /*
140 //         N5+----------+N6
141 //          /|         /|
142 //         / |        / |
143 //        /  |       /  |
144 //     N4+----------+N7 |
145 //       |   |      |   |           HEXAHEDRON
146 //       | N1+------|---+N2
147 //       |  /       |  /
148 //       | /        | /
149 //       |/         |/
150 //     N0+----------+N3
151 */
152 static int Hexa_F [6][5] = { // FORWARD
153   { 0, 1, 2, 3, 0 },
154   { 4, 7, 6, 5, 4 },          // all face normals are external
155   { 0, 4, 5, 1, 0 },
156   { 1, 5, 6, 2, 1 },
157   { 3, 2, 6, 7, 3 }, 
158   { 0, 3, 7, 4, 0 }};
159 static int Hexa_RE [6][5] = { // REVERSED -> EXTERNAL
160   { 0, 3, 2, 1, 0 },
161   { 4, 5, 6, 7, 4 },          // all face normals are external
162   { 0, 1, 5, 4, 0 },
163   { 1, 2, 6, 5, 1 },
164   { 3, 7, 6, 2, 3 }, 
165   { 0, 4, 7, 3, 0 }};
166 static int Hexa_nbN [] = { 4, 4, 4, 4, 4, 4 };
167 static int Hexa_oppF[] = { 1, 0, 4, 5, 2, 3 }; // oppopsite facet indices
168
169 /*   
170 //      N8 +------+ N9
171 //        /        \
172 //       /          \
173 //   N7 +            + N10
174 //       \          /
175 //        \        /
176 //      N6 +------+ N11
177 //                             HEXAGONAL PRISM
178 //      N2 +------+ N3
179 //        /        \
180 //       /          \
181 //   N1 +            + N4
182 //       \          /
183 //        \        /
184 //      N0 +------+ N5
185 */
186 static int HexPrism_F [8][7] = { // FORWARD
187   { 0, 1, 2, 3, 4, 5, 0 },
188   { 6,11,10, 9, 8, 7, 6 },
189   { 0, 6, 7, 1, 0, 0, 0 },
190   { 1, 7, 8, 2, 1, 1, 1 },
191   { 2, 8, 9, 3, 2, 2, 2 },
192   { 3, 9,10, 4, 3, 3, 3 },
193   { 4,10,11, 5, 4, 4, 4 },
194   { 5,11, 6, 0, 5, 5, 5 }}; 
195 static int HexPrism_RE [8][7] = { // REVERSED -> EXTERNAL
196   { 0, 5, 4, 3, 2, 1, 0 },
197   { 6,11,10, 9, 8, 7, 6 },
198   { 0, 6, 7, 1, 0, 0, 0 },
199   { 1, 7, 8, 2, 1, 1, 1 },
200   { 2, 8, 9, 3, 2, 2, 2 },
201   { 3, 9,10, 4, 3, 3, 3 },
202   { 4,10,11, 5, 4, 4, 4 },
203   { 5,11, 6, 0, 5, 5, 5 }}; 
204 static int HexPrism_nbN [] = { 6, 6, 4, 4, 4, 4, 4, 4 };
205
206
207 /*
208 //           N3
209 //           +
210 //          /|\
211 //        7/ | \8
212 //        /  |4 \                    QUADRATIC
213 //    N0 +---|---+ N1                TETRAHEDRON
214 //       \   +9  /
215 //        \  |  /
216 //        6\ | /5
217 //          \|/
218 //           +
219 //           N2
220 */
221 static int QuadTetra_F [4][7] = { // FORWARD
222   { 0, 4, 1, 5, 2, 6, 0 },        // All faces have external normals
223   { 0, 7, 3, 8, 1, 4, 0 },
224   { 1, 8, 3, 9, 2, 5, 1 },
225   { 0, 6, 2, 9, 3, 7, 0 }}; 
226 static int QuadTetra_RE [4][7] = { // REVERSED -> FORWARD (EXTERNAL)
227   { 0, 6, 2, 5, 1, 4, 0 },         // All faces have external normals
228   { 0, 4, 1, 8, 3, 7, 0 },
229   { 1, 5, 2, 9, 3, 8, 1 },
230   { 0, 7, 3, 9, 2, 6, 0 }};
231 static int QuadTetra_nbN [] = { 6, 6, 6, 6 };
232
233 //
234 //     QUADRATIC
235 //     PYRAMID
236 //
237 //            +4
238 //
239 //            
240 //       10+-----+11
241 //         |     |        9 - middle point for (0,4) etc.
242 //         |     |
243 //        9+-----+12
244 //
245 //            6
246 //      1+----+----+2
247 //       |         |
248 //       |         |
249 //      5+         +7
250 //       |         |
251 //       |         |
252 //      0+----+----+3
253 //            8
254 static int QuadPyram_F [5][9] = {  // FORWARD
255   { 0, 5, 1, 6, 2, 7, 3, 8, 0 },   // All faces have external normals
256   { 0, 9, 4, 10,1, 5, 0, 4, 4 },
257   { 1, 10,4, 11,2, 6, 1, 4, 4 },
258   { 2, 11,4, 12,3, 7, 2, 4, 4 },
259   { 3, 12,4, 9, 0, 8, 3, 4, 4 }}; 
260 static int QuadPyram_RE [5][9] = { // REVERSED -> FORWARD (EXTERNAL)
261   { 0, 8, 3, 7, 2, 6, 1, 5, 0 },   // All faces but a bottom have external normals
262   { 0, 5, 1, 10,4, 9, 0, 4, 4 },
263   { 1, 6, 2, 11,4, 10,1, 4, 4 },
264   { 2, 7, 3, 12,4, 11,2, 4, 4 },
265   { 3, 8, 0, 9, 4, 12,3, 4, 4 }}; 
266 static int QuadPyram_nbN [] = { 8, 6, 6, 6, 6 };
267
268 /*   
269 //            + N4
270 //           /|\
271 //         9/ | \10
272 //         /  |  \
273 //        /   |   \
274 //    N3 +----+----+ N5
275 //       |    |11  |
276 //       |    |    |
277 //       |    +13  |                QUADRATIC
278 //       |    |    |                PENTAHEDRON
279 //     12+    |    +14
280 //       |    |    |
281 //       |    |    |
282 //       |    + N1 |
283 //       |   / \   |               
284 //       | 6/   \7 |
285 //       | /     \ |
286 //       |/       \|
287 //    N0 +---------+ N2
288 //            8
289 */
290 static int QuadPenta_F [5][9] = {  // FORWARD
291   { 0, 6, 1, 7, 2, 8, 0, 0, 0 },
292   { 3, 11,5, 10,4, 9, 3, 3, 3 },
293   { 0, 12,3, 9, 4, 13,1, 6, 0 },
294   { 1, 13,4, 10,5, 14,2, 7, 1 },
295   { 0, 8, 2, 14,5, 11,3, 12,0 }}; 
296 static int QuadPenta_RE [5][9] = { // REVERSED -> EXTERNAL
297   { 0, 8, 2, 7, 1, 6, 0, 0, 0 },
298   { 3, 9, 4, 10,5, 11,3, 3, 3 },
299   { 0, 6, 1, 13,4, 9, 3, 12,0 },
300   { 1, 7, 2, 14,5, 10,4, 13,1 },
301   { 0, 12,3, 11,5, 14,2, 8, 0 }}; 
302 static int QuadPenta_nbN [] = { 6, 6, 8, 8, 8 };
303
304 /*
305 //                 13                                                         
306 //         N5+-----+-----+N6                          +-----+-----+
307 //          /|          /|                           /|          /| 
308 //       12+ |       14+ |                          + |   +25   + |    
309 //        /  |        /  |                         /  |        /  |    
310 //     N4+-----+-----+N7 |       QUADRATIC        +-----+-----+   |  Central nodes
311 //       |   | 15    |   |       HEXAHEDRON       |   |       |   |  of tri-quadratic
312 //       |   |       |   |                        |   |       |   |  HEXAHEDRON
313 //       | 17+       |   +18                      |   +   22+ |   +  
314 //       |   |       |   |                        |21 |       |   | 
315 //       |   |       |   |                        | + | 26+   | + |    
316 //       |   |       |   |                        |   |       |23 |    
317 //     16+   |       +19 |                        +   | +24   +   |    
318 //       |   |       |   |                        |   |       |   |    
319 //       |   |     9 |   |                        |   |       |   |    
320 //       | N1+-----+-|---+N2                      |   +-----+-|---+    
321 //       |  /        |  /                         |  /        |  /  
322 //       | +8        | +10                        | +   20+   | +      
323 //       |/          |/                           |/          |/       
324 //     N0+-----+-----+N3                          +-----+-----+    
325 //             11                              
326 */
327 static int QuadHexa_F [6][9] = {  // FORWARD
328   { 0, 8, 1, 9, 2, 10,3, 11,0 },   // all face normals are external,
329   { 4, 15,7, 14,6, 13,5, 12,4 },
330   { 0, 16,4, 12,5, 17,1, 8, 0 },
331   { 1, 17,5, 13,6, 18,2, 9, 1 },
332   { 3, 10,2, 18,6, 14,7, 19,3 }, 
333   { 0, 11,3, 19,7, 15,4, 16,0 }};
334 static int QuadHexa_RE [6][9] = {  // REVERSED -> EXTERNAL
335   { 0, 11,3, 10,2, 9, 1, 8, 0 },   // all face normals are external
336   { 4, 12,5, 13,6, 14,7, 15,4 },
337   { 0, 8, 1, 17,5, 12,4, 16,0 },
338   { 1, 9, 2, 18,6, 13,5, 17,1 },
339   { 3, 19,7, 14,6, 18,2, 10,3 }, 
340   { 0, 16,4, 15,7, 19,3, 11,0 }};
341 static int QuadHexa_nbN [] = { 8, 8, 8, 8, 8, 8 };
342
343 static int TriQuadHexa_F [6][9] = {  // FORWARD
344   { 0, 8, 1, 9, 2, 10,3, 11, 20 },   // all face normals are external
345   { 4, 15,7, 14,6, 13,5, 12, 25 },
346   { 0, 16,4, 12,5, 17,1, 8,  21 },
347   { 1, 17,5, 13,6, 18,2, 9,  22 },
348   { 3, 10,2, 18,6, 14,7, 19, 23 }, 
349   { 0, 11,3, 19,7, 15,4, 16, 24 }};
350 static int TriQuadHexa_RE [6][9] = {  // REVERSED -> EXTERNAL
351   { 0, 11,3, 10,2, 9, 1, 8,  20 },   // opposite faces are neighbouring,
352   { 4, 12,5, 13,6, 14,7, 15, 25 },   // all face normals are external
353   { 0, 8, 1, 17,5, 12,4, 16, 21 },
354   { 1, 9, 2, 18,6, 13,5, 17, 22 },
355   { 3, 19,7, 14,6, 18,2, 10, 23 }, 
356   { 0, 16,4, 15,7, 19,3, 11, 24 }};
357 static int TriQuadHexa_nbN [] = { 9, 9, 9, 9, 9, 9 };
358
359
360 // ========================================================
361 // to perform some calculations without linkage to CASCADE
362 // ========================================================
363 struct XYZ {
364   double x;
365   double y;
366   double z;
367   XYZ()                               { x = 0; y = 0; z = 0; }
368   XYZ( double X, double Y, double Z ) { x = X; y = Y; z = Z; }
369   XYZ( const XYZ& other )             { x = other.x; y = other.y; z = other.z; }
370   XYZ( const SMDS_MeshNode* n )       { x = n->X(); y = n->Y(); z = n->Z(); }
371   double* data()                      { return &x; }
372   inline XYZ operator-( const XYZ& other );
373   inline XYZ operator+( const XYZ& other );
374   inline XYZ Crossed( const XYZ& other );
375   inline XYZ operator-();
376   inline double Dot( const XYZ& other );
377   inline double Magnitude();
378   inline double SquareMagnitude();
379   inline XYZ Normalize();
380 };
381 inline XYZ XYZ::operator-( const XYZ& Right ) {
382   return XYZ(x - Right.x, y - Right.y, z - Right.z);
383 }
384 inline XYZ XYZ::operator-() {
385   return XYZ(-x,-y,-z);
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 inline double XYZ::SquareMagnitude() {
402   return (x * x + y * y + z * z);
403 }
404 inline XYZ XYZ::Normalize() {
405   double magnitude = Magnitude();
406   if ( magnitude != 0.0 )
407     return XYZ(x /= magnitude,y /= magnitude,z /= magnitude );   
408   else
409     return XYZ(x,y,z);
410 }
411
412   //================================================================================
413   /*!
414    * \brief Return linear type corresponding to a quadratic one
415    */
416   //================================================================================
417
418   SMDS_VolumeTool::VolumeType quadToLinear(SMDS_VolumeTool::VolumeType quadType)
419   {
420     SMDS_VolumeTool::VolumeType linType = SMDS_VolumeTool::VolumeType( int(quadType)-4 );
421     const int           nbCornersByQuad = SMDS_VolumeTool::NbCornerNodes( quadType );
422     if ( SMDS_VolumeTool::NbCornerNodes( linType ) == nbCornersByQuad )
423       return linType;
424
425     int iLin = 0;
426     for ( ; iLin < SMDS_VolumeTool::NB_VOLUME_TYPES; ++iLin )
427       if ( SMDS_VolumeTool::NbCornerNodes( SMDS_VolumeTool::VolumeType( iLin )) == nbCornersByQuad)
428         return SMDS_VolumeTool::VolumeType( iLin );
429
430     return SMDS_VolumeTool::UNKNOWN;
431   }
432
433 } // namespace
434
435 //================================================================================
436 /*!
437  * \brief Saver/restorer of a SMDS_VolumeTool::myCurFace
438  */
439 //================================================================================
440
441 struct SMDS_VolumeTool::SaveFacet
442 {
443   SMDS_VolumeTool::Facet  mySaved;
444   SMDS_VolumeTool::Facet& myToRestore;
445   SaveFacet( SMDS_VolumeTool::Facet& facet ): myToRestore( facet )
446   {
447     mySaved = facet;
448     mySaved.myNodes.swap( facet.myNodes );
449   }
450   ~SaveFacet()
451   {
452     if ( myToRestore.myIndex != mySaved.myIndex )
453       myToRestore = mySaved;
454     myToRestore.myNodes.swap( mySaved.myNodes );
455   }
456 };
457
458 //=======================================================================
459 //function : SMDS_VolumeTool
460 //purpose  :
461 //=======================================================================
462
463 SMDS_VolumeTool::SMDS_VolumeTool ()
464 {
465   Set( 0 );
466 }
467
468 //=======================================================================
469 //function : SMDS_VolumeTool
470 //purpose  : 
471 //=======================================================================
472
473 SMDS_VolumeTool::SMDS_VolumeTool (const SMDS_MeshElement* theVolume,
474                                   const bool              ignoreCentralNodes)
475 {
476   Set( theVolume, ignoreCentralNodes );
477 }
478
479 //=======================================================================
480 //function : SMDS_VolumeTool
481 //purpose  : 
482 //=======================================================================
483
484 SMDS_VolumeTool::~SMDS_VolumeTool()
485 {
486   myCurFace.myNodeIndices = NULL;
487 }
488
489 //=======================================================================
490 //function : SetVolume
491 //purpose  : Set volume to iterate on
492 //=======================================================================
493
494 bool SMDS_VolumeTool::Set (const SMDS_MeshElement*                  theVolume,
495                            const bool                               ignoreCentralNodes,
496                            const std::vector<const SMDS_MeshNode*>* otherNodes)
497 {
498   // reset fields
499   myVolume = 0;
500   myPolyedre = 0;
501   myIgnoreCentralNodes = ignoreCentralNodes;
502
503   myVolForward = true;
504   myNbFaces = 0;
505   myVolumeNodes.clear();
506   myPolyIndices.clear();
507   myPolyQuantities.clear();
508   myPolyFacetOri.clear();
509   myFwdLinks.clear();
510
511   myExternalFaces = false;
512
513   myAllFacesNodeIndices_F  = 0;
514   myAllFacesNodeIndices_RE = 0;
515   myAllFacesNbNodes        = 0;
516
517   myCurFace.myIndex = -1;
518   myCurFace.myNodeIndices = NULL;
519   myCurFace.myNodes.clear();
520
521   // set volume data
522   if ( !theVolume || theVolume->GetType() != SMDSAbs_Volume )
523     return false;
524
525   myVolume = theVolume;
526   myNbFaces = theVolume->NbFaces();
527   if ( myVolume->IsPoly() )
528   {
529     myPolyedre = SMDS_Mesh::DownCast<SMDS_MeshVolume>( myVolume );
530     myPolyFacetOri.resize( myNbFaces, 0 );
531   }
532
533   // set nodes
534   myVolumeNodes.resize( myVolume->NbNodes() );
535   if ( otherNodes )
536   {
537     if ( otherNodes->size() != myVolumeNodes.size() )
538       return ( myVolume = 0 );
539     for ( size_t i = 0; i < otherNodes->size(); ++i )
540       if ( ! ( myVolumeNodes[i] = (*otherNodes)[0] ))
541         return ( myVolume = 0 );
542   }
543   else
544   {
545     myVolumeNodes.assign( myVolume->begin_nodes(), myVolume->end_nodes() );
546   }
547
548   // check validity
549   if ( !setFace(0) )
550     return ( myVolume = 0 );
551
552   if ( !myPolyedre )
553   {
554     // define volume orientation
555     XYZ botNormal;
556     if ( GetFaceNormal( 0, botNormal.x, botNormal.y, botNormal.z ))
557     {
558       const SMDS_MeshNode* botNode = myVolumeNodes[ 0 ];
559       int topNodeIndex = myVolume->NbCornerNodes() - 1;
560       while ( !IsLinked( 0, topNodeIndex, /*ignoreMediumNodes=*/true )) --topNodeIndex;
561       const SMDS_MeshNode* topNode = myVolumeNodes[ topNodeIndex ];
562       XYZ upDir (topNode->X() - botNode->X(),
563                  topNode->Y() - botNode->Y(),
564                  topNode->Z() - botNode->Z() );
565       myVolForward = ( botNormal.Dot( upDir ) < 0 );
566     }
567     if ( !myVolForward )
568       myCurFace.myIndex = -1; // previous setFace(0) didn't take myVolForward into account
569   }
570   return true;
571 }
572
573 //=======================================================================
574 //function : Inverse
575 //purpose  : Inverse volume
576 //=======================================================================
577
578 #define SWAP_NODES(nodes,i1,i2)           \
579 {                                         \
580   const SMDS_MeshNode* tmp = nodes[ i1 ]; \
581   nodes[ i1 ] = nodes[ i2 ];              \
582   nodes[ i2 ] = tmp;                      \
583 }
584 void SMDS_VolumeTool::Inverse ()
585 {
586   if ( !myVolume ) return;
587
588   if (myVolume->IsPoly()) {
589     MESSAGE("Warning: attempt to inverse polyhedral volume");
590     return;
591   }
592
593   myVolForward = !myVolForward;
594   myCurFace.myIndex = -1;
595
596   // inverse top and bottom faces
597   switch ( myVolumeNodes.size() ) {
598   case 4:
599     SWAP_NODES( myVolumeNodes, 1, 2 );
600     break;
601   case 5:
602     SWAP_NODES( myVolumeNodes, 1, 3 );
603     break;
604   case 6:
605     SWAP_NODES( myVolumeNodes, 1, 2 );
606     SWAP_NODES( myVolumeNodes, 4, 5 );
607     break;
608   case 8:
609     SWAP_NODES( myVolumeNodes, 1, 3 );
610     SWAP_NODES( myVolumeNodes, 5, 7 );
611     break;
612   case 12:
613     SWAP_NODES( myVolumeNodes, 1, 5 );
614     SWAP_NODES( myVolumeNodes, 2, 4 );
615     SWAP_NODES( myVolumeNodes, 7, 11 );
616     SWAP_NODES( myVolumeNodes, 8, 10 );
617     break;
618
619   case 10:
620     SWAP_NODES( myVolumeNodes, 1, 2 );
621     SWAP_NODES( myVolumeNodes, 4, 6 );
622     SWAP_NODES( myVolumeNodes, 8, 9 );
623     break;
624   case 13:
625     SWAP_NODES( myVolumeNodes, 1, 3 );
626     SWAP_NODES( myVolumeNodes, 5, 8 );
627     SWAP_NODES( myVolumeNodes, 6, 7 );
628     SWAP_NODES( myVolumeNodes, 10, 12 );
629     break;
630   case 15:
631     SWAP_NODES( myVolumeNodes, 1, 2 );
632     SWAP_NODES( myVolumeNodes, 4, 5 );
633     SWAP_NODES( myVolumeNodes, 6, 8 );
634     SWAP_NODES( myVolumeNodes, 9, 11 );
635     SWAP_NODES( myVolumeNodes, 13, 14 );
636     break;
637   case 20:
638     SWAP_NODES( myVolumeNodes, 1, 3 );
639     SWAP_NODES( myVolumeNodes, 5, 7 );
640     SWAP_NODES( myVolumeNodes, 8, 11 );
641     SWAP_NODES( myVolumeNodes, 9, 10 );
642     SWAP_NODES( myVolumeNodes, 12, 15 );
643     SWAP_NODES( myVolumeNodes, 13, 14 );
644     SWAP_NODES( myVolumeNodes, 17, 19 );
645     break;
646   case 27:
647     SWAP_NODES( myVolumeNodes, 1, 3 );
648     SWAP_NODES( myVolumeNodes, 5, 7 );
649     SWAP_NODES( myVolumeNodes, 8, 11 );
650     SWAP_NODES( myVolumeNodes, 9, 10 );
651     SWAP_NODES( myVolumeNodes, 12, 15 );
652     SWAP_NODES( myVolumeNodes, 13, 14 );
653     SWAP_NODES( myVolumeNodes, 17, 19 );
654     SWAP_NODES( myVolumeNodes, 21, 24 );
655     SWAP_NODES( myVolumeNodes, 22, 23 );
656     break;
657   default:;
658   }
659 }
660
661 //=======================================================================
662 //function : GetVolumeType
663 //purpose  : 
664 //=======================================================================
665
666 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetVolumeType() const
667 {
668   if ( myPolyedre )
669     return POLYHEDA;
670
671   switch( myVolumeNodes.size() ) {
672   case 4: return TETRA;
673   case 5: return PYRAM;
674   case 6: return PENTA;
675   case 8: return HEXA;
676   case 12: return HEX_PRISM;
677   case 10: return QUAD_TETRA;
678   case 13: return QUAD_PYRAM;
679   case 15: return QUAD_PENTA;
680   case 20: return QUAD_HEXA;
681   case 27: return QUAD_HEXA;
682   default: break;
683   }
684
685   return UNKNOWN;
686 }
687
688 //=======================================================================
689 //function : getTetraVolume
690 //purpose  : 
691 //=======================================================================
692
693 static double getTetraVolume(const SMDS_MeshNode* n1,
694                              const SMDS_MeshNode* n2,
695                              const SMDS_MeshNode* n3,
696                              const SMDS_MeshNode* n4)
697 {
698   double p1[3], p2[3], p3[3], p4[3];
699   n1->GetXYZ( p1 );
700   n2->GetXYZ( p2 );
701   n3->GetXYZ( p3 );
702   n4->GetXYZ( p4 );
703
704   double Q1 = -(p1[ 0 ]-p2[ 0 ])*(p3[ 1 ]*p4[ 2 ]-p4[ 1 ]*p3[ 2 ]);
705   double Q2 =  (p1[ 0 ]-p3[ 0 ])*(p2[ 1 ]*p4[ 2 ]-p4[ 1 ]*p2[ 2 ]);
706   double R1 = -(p1[ 0 ]-p4[ 0 ])*(p2[ 1 ]*p3[ 2 ]-p3[ 1 ]*p2[ 2 ]);
707   double R2 = -(p2[ 0 ]-p3[ 0 ])*(p1[ 1 ]*p4[ 2 ]-p4[ 1 ]*p1[ 2 ]);
708   double S1 =  (p2[ 0 ]-p4[ 0 ])*(p1[ 1 ]*p3[ 2 ]-p3[ 1 ]*p1[ 2 ]);
709   double S2 = -(p3[ 0 ]-p4[ 0 ])*(p1[ 1 ]*p2[ 2 ]-p2[ 1 ]*p1[ 2 ]);
710
711   return (Q1+Q2+R1+R2+S1+S2)/6.0;
712 }
713
714 //=======================================================================
715 //function : GetSize
716 //purpose  : Return element volume
717 //=======================================================================
718 double SMDS_VolumeTool::GetSize() const
719 {
720   double V = 0.;
721   if ( !myVolume )
722     return 0.;
723
724   if ( myVolume->IsPoly() )
725   {
726     if ( !myPolyedre )
727       return 0.;
728
729     SaveFacet savedFacet( myCurFace );
730
731     // split a polyhedron into tetrahedrons
732
733     bool oriOk = AllFacesSameOriented();
734     SMDS_VolumeTool* me = const_cast< SMDS_VolumeTool* > ( this );
735     for ( int f = 0; f < NbFaces(); ++f )
736     {
737       me->setFace( f );
738       XYZ area (0,0,0), p1( myCurFace.myNodes[0] );
739       for ( int n = 0; n < myCurFace.myNbNodes; ++n )
740       {
741         XYZ p2( myCurFace.myNodes[ n+1 ]);
742         area = area + p1.Crossed( p2 );
743         p1 = p2;
744       }
745       V += p1.Dot( area );
746     }
747     V /= 6;
748     if ( !oriOk && V > 0 )
749       V *= -1;
750   }
751   else 
752   {
753     const static int ind[] = {
754       0, 1, 3, 6, 11, 23, 31, 44, 58, 78 };
755     const static int vtab[][4] = { // decomposition into tetra in the order of enum VolumeType
756       // tetrahedron
757       { 0, 1, 2, 3 },
758       // pyramid
759       { 0, 1, 3, 4 },
760       { 1, 2, 3, 4 },
761       // pentahedron
762       { 0, 1, 2, 3 },
763       { 1, 5, 3, 4 },
764       { 1, 5, 2, 3 },
765       // hexahedron
766       { 1, 4, 3, 0 },
767       { 4, 1, 6, 5 },
768       { 1, 3, 6, 2 },
769       { 4, 6, 3, 7 },
770       { 1, 4, 6, 3 },
771       // hexagonal prism
772       { 0, 1, 2, 7 },
773       { 0, 7, 8, 6 },
774       { 2, 7, 8, 0 },
775
776       { 0, 3, 4, 9 },
777       { 0, 9, 10, 6 },
778       { 4, 9, 10, 0 },
779
780       { 0, 3, 4, 9 },
781       { 0, 9, 10, 6 },
782       { 4, 9, 10, 0 },
783
784       { 0, 4, 5, 10 },
785       { 0, 10, 11, 6 },
786       { 5, 10, 11, 0 },
787
788       // quadratic tetrahedron
789       { 0, 4, 6, 7 },
790       { 1, 5, 4, 8 },
791       { 2, 6, 5, 9 },
792       { 7, 8, 9, 3 },
793       { 4, 6, 7, 9 },
794       { 4, 5, 6, 9 },
795       { 4, 7, 8, 9 },
796       { 4, 5, 9, 8 },
797
798       // quadratic pyramid
799       { 0, 5, 8, 9 },
800       { 1, 5,10, 6 },
801       { 2, 6,11, 7 },
802       { 3, 7,12, 8 },
803       { 4, 9,11,10 },
804       { 4, 9,12,11 },
805       { 10, 5, 9, 8 },
806       { 10, 8, 9,12 },
807       { 10, 8,12, 7 },
808       { 10, 7,12,11 },
809       { 10, 7,11, 6 },
810       { 10, 5, 8, 6 },
811       { 10, 6, 8, 7 },
812
813       // quadratic pentahedron
814       { 12, 0, 8, 6 },
815       { 12, 8, 7, 6 },
816       { 12, 8, 2, 7 },
817       { 12, 6, 7, 1 },
818       { 12, 1, 7,13 },
819       { 12, 7, 2,13 },
820       { 12, 2,14,13 },
821
822       { 12, 3, 9,11 },
823       { 12,11, 9,10 },
824       { 12,11,10, 5 },
825       { 12, 9, 4,10 },
826       { 12,14, 5,10 },
827       { 12,14,10, 4 },
828       { 12,14, 4,13 },
829
830       // quadratic hexahedron
831       { 16, 0,11, 8 },
832       { 16,11, 9, 8 },
833       { 16, 8, 9, 1 },
834       { 16,11, 3,10 },
835       { 16,11,10, 9 },
836       { 16,10, 2, 9 },
837       { 16, 3,19, 2 },
838       { 16, 2,19,18 },
839       { 16, 2,18,17 },
840       { 16, 2,17, 1 },
841
842       { 16, 4,12,15 },
843       { 16,12, 5,13 },
844       { 16,12,13,15 },
845       { 16,13, 6,14 },
846       { 16,13,14,15 },
847       { 16,14, 7,15 },
848       { 16, 6, 5,17 },
849       { 16,18, 6,17 },
850       { 16,18, 7, 6 },
851       { 16,18,19, 7 },
852
853     };
854
855     int type = GetVolumeType();
856     int n1 = ind[type];
857     int n2 = ind[type+1];
858
859     for (int i = n1; i <  n2; i++) {
860       V -= getTetraVolume( myVolumeNodes[ vtab[i][0] ],
861                            myVolumeNodes[ vtab[i][1] ],
862                            myVolumeNodes[ vtab[i][2] ],
863                            myVolumeNodes[ vtab[i][3] ]);
864     }
865   }
866   return V;
867 }
868
869
870 //=======================================================================
871 //function : getTetraScaledJacobian
872 //purpose  : Given the smesh nodes in the canonical order of the tetrahedron, return the scaled jacobian
873 //=======================================================================
874 static double getTetraScaledJacobian(const SMDS_MeshNode* n0,
875                                      const SMDS_MeshNode* n1,
876                                      const SMDS_MeshNode* n2,
877                                      const SMDS_MeshNode* n3)
878 {
879   const double sqrt = std::sqrt(2.0);
880   // Get the coordinates
881   XYZ p0( n0 );
882   XYZ p1( n1 );
883   XYZ p2( n2 );
884   XYZ p3( n3 );
885   // Define the edges connecting the nodes
886   XYZ L0 = p1-p0;
887   XYZ L1 = p2-p1;
888   XYZ L2 = p2-p0; // invert the definition of doc to get the proper orientation of the crossed product
889   XYZ L3 = p3-p0;
890   XYZ L4 = p3-p1;
891   XYZ L5 = p3-p2;
892   double Jacobian = L2.Crossed( L0 ).Dot( L3 );
893   double norm0 = L0.Magnitude();
894   double norm1 = L1.Magnitude();
895   double norm2 = L2.Magnitude();
896   double norm3 = L3.Magnitude();
897   double norm4 = L4.Magnitude();
898   double norm5 = L5.Magnitude();
899
900   std::array<double, 5> norms{};
901   norms[0] = Jacobian;
902   norms[1] = norm3*norm4*norm5;
903   norms[2] = norm1*norm2*norm5;
904   norms[3] = norm0*norm1*norm4;
905   norms[4] = norm0*norm2*norm3;  
906
907   auto findMaxNorm = std::max_element(norms.begin(), norms.end());
908   double maxNorm = *findMaxNorm;
909
910   if ( std::fabs( maxNorm ) < std::numeric_limits<double>::min() )
911     maxNorm = std::numeric_limits<double>::max();
912
913   return Jacobian * sqrt / maxNorm;
914 }
915
916 //=======================================================================
917 //function : getPyramidScaledJacobian
918 //purpose  : Given the pyramid, compute the scaled jacobian of the four tetrahedrons and return the minimun value.
919 //=======================================================================
920 static double getPyramidScaledJacobian(const SMDS_MeshNode* n0,
921                                         const SMDS_MeshNode* n1,
922                                         const SMDS_MeshNode* n2,
923                                         const SMDS_MeshNode* n3,
924                                         const SMDS_MeshNode* n4)
925
926   const double sqrt = std::sqrt(2.0);
927   std::array<double, 4> tetScaledJacobian{};
928   tetScaledJacobian[0] = getTetraScaledJacobian(n0, n1, n3, n4);
929   tetScaledJacobian[1] = getTetraScaledJacobian(n1, n2, n0, n4);
930   tetScaledJacobian[2] = getTetraScaledJacobian(n2, n3, n1, n4);
931   tetScaledJacobian[3] = getTetraScaledJacobian(n3, n0, n2, n4);  
932
933   auto minEntry = std::min_element(tetScaledJacobian.begin(), tetScaledJacobian.end());
934
935   double scaledJacobian = (*minEntry) * 2.0/sqrt;
936   return scaledJacobian < 1.0 ? scaledJacobian : 1.0 - (scaledJacobian - 1.0);
937 }
938
939
940
941 //=======================================================================
942 //function : getHexaScaledJacobian
943 //purpose  : Evaluate the scaled jacobian on the eight vertices of the hexahedron and return the minimal registered value
944 //remark   : Follow the reference numeration described at the top of the class.
945 //=======================================================================
946 static double getHexaScaledJacobian(const SMDS_MeshNode* n0,
947                                     const SMDS_MeshNode* n1,
948                                     const SMDS_MeshNode* n2,
949                                     const SMDS_MeshNode* n3,
950                                     const SMDS_MeshNode* n4,
951                                     const SMDS_MeshNode* n5,
952                                     const SMDS_MeshNode* n6,
953                                     const SMDS_MeshNode* n7)
954
955   // Scaled jacobian is an scalar quantity measuring the deviation of the geometry from the perfect geometry
956   // Get the coordinates
957   XYZ p0( n0 );
958   XYZ p1( n1 );
959   XYZ p2( n2 );
960   XYZ p3( n3 );
961   XYZ p4( n4 );
962   XYZ p5( n5 );
963   XYZ p6( n6 );
964   XYZ p7( n7 );
965
966   // Define the edges connecting the nodes  
967   XYZ L0  = (p1-p0).Normalize();
968   XYZ L1  = (p2-p1).Normalize();
969   XYZ L2  = (p3-p2).Normalize();
970   XYZ L3  = (p3-p0).Normalize();
971   XYZ L4  = (p4-p0).Normalize();
972   XYZ L5  = (p5-p1).Normalize();
973   XYZ L6  = (p6-p2).Normalize();
974   XYZ L7  = (p7-p3).Normalize();
975   XYZ L8  = (p5-p4).Normalize();
976   XYZ L9  = (p6-p5).Normalize();
977   XYZ L10 = (p7-p6).Normalize();
978   XYZ L11 = (p7-p4).Normalize();
979   XYZ X0  = (p1-p0+p2-p3+p6-p7+p5-p4).Normalize();
980   XYZ X1  = (p3-p0+p2-p1+p7-p4+p6-p5).Normalize();
981   XYZ X2  = (p4-p0+p7-p3+p5-p1+p6-p2).Normalize();
982   
983   std::array<double, 9> scaledJacobian{};
984   //Scaled jacobian of nodes following their numeration
985   scaledJacobian[0] =  L4.Crossed( L3).Dot( L0 );   // For L0
986   scaledJacobian[1] =  L5.Crossed(-L0).Dot( L1 );   // For L1
987   scaledJacobian[2] =  L6.Crossed(-L1).Dot( L2 );   // For L2
988   scaledJacobian[3] =  L7.Crossed(-L2).Dot(-L3 );   // For L3
989   scaledJacobian[4] = -L4.Crossed( L8).Dot( L11 );  // For L11  
990   scaledJacobian[5] = -L5.Crossed( L9).Dot(-L8 );   // For L8
991   scaledJacobian[6] = -L6.Crossed(L10).Dot(-L9 );   // For L9
992   scaledJacobian[7] = -L7.Crossed(-L11).Dot(-L10 ); // For L10
993   scaledJacobian[8] =  X2.Crossed( X1).Dot( X0 );   // For principal axes
994
995   auto minScaledJacobian = std::min_element(scaledJacobian.begin(), scaledJacobian.end());
996   return *minScaledJacobian;
997 }
998
999
1000 //=======================================================================
1001 //function : getTetraNormalizedJacobian
1002 //purpose  : Return the jacobian of the tetrahedron based on normalized vectors
1003 //=======================================================================
1004 static double getTetraNormalizedJacobian(const SMDS_MeshNode* n0,
1005                                           const SMDS_MeshNode* n1,
1006                                           const SMDS_MeshNode* n2,
1007                                           const SMDS_MeshNode* n3)
1008 {
1009   const double sqrt = std::sqrt(2.0);
1010   // Get the coordinates
1011   XYZ p0( n0 );
1012   XYZ p1( n1 );
1013   XYZ p2( n2 );
1014   XYZ p3( n3 );
1015   // Define the normalized edges connecting the nodes
1016   XYZ L0 = (p1-p0).Normalize();
1017   XYZ L2 = (p2-p0).Normalize(); // invert the definition of doc to get the proper orientation of the crossed product
1018   XYZ L3 = (p3-p0).Normalize();
1019   return L2.Crossed( L0 ).Dot( L3 );
1020 }
1021
1022 //=======================================================================
1023 //function : getPentaScaledJacobian
1024 //purpose  : Evaluate the scaled jacobian on the pentahedron based on decomposed tetrahedrons
1025 //=======================================================================
1026 /*   
1027 //            + N1
1028 //           /|\
1029 //          / | \
1030 //         /  |  \
1031 //        /   |   \
1032 //    N0 +---------+ N2
1033 //       |    |    |               NUMERATION RERENCE FOLLOWING POSSITIVE RIGHT HAND RULE
1034 //       |    + N4 |               
1035 //       |   / \   |               PENTAHEDRON
1036 //       |  /   \  |
1037 //       | /     \ |
1038 //       |/       \|
1039 //    N3 +---------+ N5
1040 //
1041 //          N1
1042 //          +
1043 //          |\
1044 //         /| \
1045 //        / |  \
1046 //    N0 +--|---+ N2               TETRAHEDRON ASSOCIATED TO N0
1047 //       \  |   /                  Numeration passed to getTetraScaledJacobian 
1048 //        \ |  /                   N0=N0; N1=N2; N2=N3; N3=N1
1049 //         \| /                     
1050 //          |/
1051 //          +
1052 //          N3
1053 //
1054 //           N1
1055 //           +
1056 //          /|\
1057 //         / | \
1058 //        /  |  \
1059 //    N2 +---|---+ N5             TETRAHEDRON ASSOCIATED TO N2
1060 //       \   |  /                 Numeration passed to getTetraScaledJacobian 
1061 //        \  | /                  N0=N2; N1=N5; N2=N0; N3=N1
1062 //         \ |/
1063 //          \|
1064 //           +
1065 //           N0
1066 //
1067 //           N4
1068 //           +
1069 //          /|\
1070 //         / | \
1071 //        /  |  \
1072 //    N3 +---|---+ N0             TETRAHEDRON ASSOCIATED TO N3
1073 //       \   |   /                Numeration passed to getTetraScaledJacobian 
1074 //        \  |  /                 N0=N3; N1=N0; N2=N5; N3=N4
1075 //         \ | /
1076 //          \|/
1077 //           +
1078 //           N5
1079 //
1080 //           N3
1081 //           +
1082 //          /|\
1083 //         / | \
1084 //        /  |  \
1085 //    N1 +---|---+ N2             TETRAHEDRON ASSOCIATED TO N1
1086 //       \   |   /                Numeration passed to getTetraScaledJacobian 
1087 //        \  |  /                 N0=N1; N1=N2; N2=N0; N3=N3
1088 //         \ | /
1089 //          \|/
1090 //           +
1091 //           N0
1092 //
1093 //          ...
1094 */
1095 static double getPentaScaledJacobian(const SMDS_MeshNode* n0,
1096                                      const SMDS_MeshNode* n1,
1097                                      const SMDS_MeshNode* n2,
1098                                      const SMDS_MeshNode* n3,
1099                                      const SMDS_MeshNode* n4,
1100                                      const SMDS_MeshNode* n5)
1101
1102   std::array<double, 6> scaledJacobianOfReferenceTetra{};
1103   scaledJacobianOfReferenceTetra[0] = getTetraNormalizedJacobian(n0, n2, n3, n1);  // For n0
1104   scaledJacobianOfReferenceTetra[1] = getTetraNormalizedJacobian(n2, n5, n0, n1);  // For n2
1105   scaledJacobianOfReferenceTetra[2] = getTetraNormalizedJacobian(n3, n0, n5, n4);  // For n3
1106   scaledJacobianOfReferenceTetra[3] = getTetraNormalizedJacobian(n5, n3, n2, n4);  // For n5
1107   scaledJacobianOfReferenceTetra[4] = getTetraNormalizedJacobian(n1, n2, n0, n3);  // For n1  
1108   scaledJacobianOfReferenceTetra[5] = getTetraNormalizedJacobian(n4, n3, n5, n2);  // For n4
1109   
1110   auto minScaledJacobian = std::min_element(scaledJacobianOfReferenceTetra.begin(), scaledJacobianOfReferenceTetra.end());
1111   double minScalJac = (*minScaledJacobian)* 2.0 / std::sqrt(3.0);
1112
1113   if (minScalJac > 0)
1114     return std::min(minScalJac, std::numeric_limits<double>::max());
1115
1116   return std::max(minScalJac, -std::numeric_limits<double>::max());
1117 }
1118
1119 //=======================================================================
1120 //function : getHexaPrismScaledJacobian
1121 //purpose  : Evaluate the scaled jacobian on the hexaprism by decomposing the goemetry into three 1hexa + 2 pentahedrons
1122 //=======================================================================
1123 static double getHexaPrismScaledJacobian(const SMDS_MeshNode* n0,
1124                                           const SMDS_MeshNode* n1,
1125                                           const SMDS_MeshNode* n2,
1126                                           const SMDS_MeshNode* n3,
1127                                           const SMDS_MeshNode* n4,
1128                                           const SMDS_MeshNode* n5,
1129                                           const SMDS_MeshNode* n6,
1130                                           const SMDS_MeshNode* n7,
1131                                           const SMDS_MeshNode* n8,
1132                                           const SMDS_MeshNode* n9,
1133                                           const SMDS_MeshNode* n10, 
1134                                           const SMDS_MeshNode* n11)
1135
1136   // The Pentahedron from the left
1137   // n0=n0; n1=n1; n2=n2; n3=n6; n4=n7, n5=n8; 
1138   double scaledJacobianPentleft = getPentaScaledJacobian( n0, n1, n2, n6, n7, n8 );
1139   // The core Hexahedron
1140   // n0=n0; n1=n2, n2=n3; n3=n5; n4=n6; n5=n8; n6=n9; n7=n11
1141   double scaledJacobianHexa     = getHexaScaledJacobian( n0, n2, n3, n5, n6, n8, n9, n11 );
1142   // The Pentahedron from the right
1143   // n0=n5; n1=n4; n2=n3; n3=n11; n4=n10; n5=n9
1144   double scaledJacobianPentright = getPentaScaledJacobian( n5, n4, n3, n11, n10, n9 );
1145
1146   return std::min( scaledJacobianHexa, std::min( scaledJacobianPentleft, scaledJacobianPentright ) );
1147   
1148 }
1149
1150 //=======================================================================
1151 //function : GetScaledJacobian
1152 //purpose  : Return element Scaled Jacobian using the generic definition given 
1153 //            in https://gitlab.kitware.com/third-party/verdict/-/blob/master/SAND2007-2853p.pdf
1154 //=======================================================================
1155
1156 double SMDS_VolumeTool::GetScaledJacobian() const
1157 {
1158   
1159   // For Tetra, call directly the getTetraScaledJacobian 
1160   double scaledJacobian = 0.;
1161
1162   VolumeType type = GetVolumeType();    
1163   switch (type)
1164   {
1165   case TETRA:
1166   case QUAD_TETRA:
1167     scaledJacobian = getTetraScaledJacobian( myVolumeNodes[0], myVolumeNodes[1], myVolumeNodes[2], myVolumeNodes[3] );
1168     break;    
1169   case HEXA:
1170   case QUAD_HEXA: 
1171     scaledJacobian = getHexaScaledJacobian( myVolumeNodes[0], myVolumeNodes[1], myVolumeNodes[2], myVolumeNodes[3],
1172                                             myVolumeNodes[4], myVolumeNodes[5], myVolumeNodes[6], myVolumeNodes[7] );
1173     break;
1174   case PYRAM:
1175   case QUAD_PYRAM:  
1176     scaledJacobian = getPyramidScaledJacobian( myVolumeNodes[0], myVolumeNodes[1], myVolumeNodes[2], myVolumeNodes[3], myVolumeNodes[4] );
1177     break;
1178   case PENTA:
1179   case QUAD_PENTA:
1180     scaledJacobian = getPentaScaledJacobian( myVolumeNodes[0], myVolumeNodes[1], 
1181                                              myVolumeNodes[2], myVolumeNodes[3], 
1182                                              myVolumeNodes[4], myVolumeNodes[5] );
1183     break;
1184   case HEX_PRISM: 
1185     scaledJacobian = getHexaPrismScaledJacobian( myVolumeNodes[0], myVolumeNodes[1], myVolumeNodes[2], myVolumeNodes[3], 
1186                                                  myVolumeNodes[4], myVolumeNodes[5], myVolumeNodes[6], myVolumeNodes[7],
1187                                                  myVolumeNodes[8], myVolumeNodes[9], myVolumeNodes[10], myVolumeNodes[11]);
1188     break;
1189   default:
1190     break;
1191   }
1192
1193   return scaledJacobian;
1194 }
1195
1196
1197 //=======================================================================
1198 //function : GetBaryCenter
1199 //purpose  : 
1200 //=======================================================================
1201
1202 bool SMDS_VolumeTool::GetBaryCenter(double & X, double & Y, double & Z) const
1203 {
1204   X = Y = Z = 0.;
1205   if ( !myVolume )
1206     return false;
1207
1208   for ( size_t i = 0; i < myVolumeNodes.size(); i++ ) {
1209     X += myVolumeNodes[ i ]->X();
1210     Y += myVolumeNodes[ i ]->Y();
1211     Z += myVolumeNodes[ i ]->Z();
1212   }
1213   X /= myVolumeNodes.size();
1214   Y /= myVolumeNodes.size();
1215   Z /= myVolumeNodes.size();
1216
1217   return true;
1218 }
1219
1220 //================================================================================
1221 /*!
1222  * \brief Classify a point
1223  *  \param tol - thickness of faces
1224  */
1225 //================================================================================
1226
1227 bool SMDS_VolumeTool::IsOut(double X, double Y, double Z, double tol) const
1228 {
1229   // LIMITATION: for convex volumes only
1230   XYZ p( X,Y,Z );
1231   for ( int iF = 0; iF < myNbFaces; ++iF )
1232   {
1233     XYZ faceNormal;
1234     if ( !GetFaceNormal( iF, faceNormal.x, faceNormal.y, faceNormal.z ))
1235       continue;
1236     if ( !IsFaceExternal( iF ))
1237       faceNormal = XYZ() - faceNormal; // reverse
1238
1239     XYZ face2p( p - XYZ( myCurFace.myNodes[0] ));
1240     if ( face2p.Dot( faceNormal ) > tol )
1241       return true;
1242   }
1243   return false;
1244 }
1245
1246 //=======================================================================
1247 //function : SetExternalNormal
1248 //purpose  : Node order will be so that faces normals are external
1249 //=======================================================================
1250
1251 void SMDS_VolumeTool::SetExternalNormal ()
1252 {
1253   myExternalFaces = true;
1254   myCurFace.myIndex = -1;
1255 }
1256
1257 //=======================================================================
1258 //function : NbFaceNodes
1259 //purpose  : Return number of nodes in the array of face nodes
1260 //=======================================================================
1261
1262 int SMDS_VolumeTool::NbFaceNodes( int faceIndex ) const
1263 {
1264   if ( !setFace( faceIndex ))
1265     return 0;
1266   return myCurFace.myNbNodes;
1267 }
1268
1269 //=======================================================================
1270 //function : GetFaceNodes
1271 //purpose  : Return pointer to the array of face nodes.
1272 //           To comfort link iteration, the array
1273 //           length == NbFaceNodes( faceIndex ) + 1 and
1274 //           the last node == the first one.
1275 //=======================================================================
1276
1277 const SMDS_MeshNode** SMDS_VolumeTool::GetFaceNodes( int faceIndex ) const
1278 {
1279   if ( !setFace( faceIndex ))
1280     return 0;
1281   return &myCurFace.myNodes[0];
1282 }
1283
1284 //=======================================================================
1285 //function : GetFaceNodesIndices
1286 //purpose  : Return pointer to the array of face nodes indices
1287 //           To comfort link iteration, the array
1288 //           length == NbFaceNodes( faceIndex ) + 1 and
1289 //           the last node index == the first one.
1290 //=======================================================================
1291
1292 const int* SMDS_VolumeTool::GetFaceNodesIndices( int faceIndex ) const
1293 {
1294   if ( !setFace( faceIndex ))
1295     return 0;
1296
1297   return myCurFace.myNodeIndices;
1298 }
1299
1300 //=======================================================================
1301 //function : GetFaceNodes
1302 //purpose  : Return a set of face nodes.
1303 //=======================================================================
1304
1305 bool SMDS_VolumeTool::GetFaceNodes (int                             faceIndex,
1306                                     std::set<const SMDS_MeshNode*>& theFaceNodes ) const
1307 {
1308   if ( !setFace( faceIndex ))
1309     return false;
1310
1311   theFaceNodes.clear();
1312   theFaceNodes.insert( myCurFace.myNodes.begin(), myCurFace.myNodes.end() );
1313
1314   return true;
1315 }
1316
1317 namespace
1318 {
1319   struct NLink : public std::pair<smIdType,smIdType>
1320   {
1321     int myOri;
1322     NLink(const SMDS_MeshNode* n1=0, const SMDS_MeshNode* n2=0, int ori=1 )
1323     {
1324       if ( n1 )
1325       {
1326         if (( myOri = ( n1->GetID() < n2->GetID() )))
1327         {
1328           first  = n1->GetID();
1329           second = n2->GetID();
1330         }
1331         else
1332         {
1333           myOri  = -1;
1334           first  = n2->GetID();
1335           second = n1->GetID();
1336         }
1337         myOri *= ori;
1338       }
1339       else
1340       {
1341         myOri = first = second = 0;
1342       }
1343     }
1344     //int Node1() const { return myOri == -1 ? second : first; }
1345
1346     //bool IsSameOri( const std::pair<int,int>& link ) const { return link.first == Node1(); }
1347   };
1348 }
1349
1350 //=======================================================================
1351 //function : IsFaceExternal
1352 //purpose  : Check normal orientation of a given face
1353 //=======================================================================
1354
1355 bool SMDS_VolumeTool::IsFaceExternal( int faceIndex ) const
1356 {
1357   if ( myExternalFaces || !myVolume )
1358     return true;
1359
1360   if ( !myPolyedre ) // all classical volumes have external facet normals
1361     return true;
1362
1363   SMDS_VolumeTool* me = const_cast< SMDS_VolumeTool* >( this );
1364
1365   if ( myPolyFacetOri[ faceIndex ])
1366     return myPolyFacetOri[ faceIndex ] > 0;
1367
1368   int ori = 0; // -1-in, +1-out, 0-undef
1369   double minProj, maxProj;
1370   if ( projectNodesToNormal( faceIndex, minProj, maxProj ))
1371   {
1372     // all nodes are on the same side of the facet
1373     ori = ( minProj < 0 ? +1 : -1 );
1374     me->myPolyFacetOri[ faceIndex ] = ori;
1375
1376     if ( !myFwdLinks.empty() ) // concave polyhedron; collect oriented links
1377       for ( int i = 0; i < myCurFace.myNbNodes; ++i )
1378       {
1379         NLink link( myCurFace.myNodes[i], myCurFace.myNodes[i+1], ori );
1380         me->myFwdLinks.insert( make_pair( link, link.myOri ));
1381       }
1382     return ori > 0;
1383   }
1384
1385   SaveFacet savedFacet( myCurFace );
1386
1387   // concave polyhedron
1388
1389   if ( myFwdLinks.empty() ) // get links of the least ambiguously oriented facet
1390   {
1391     for ( size_t i = 0; i < myPolyFacetOri.size() && !ori; ++i )
1392       ori = myPolyFacetOri[ i ];
1393
1394     if ( !ori ) // none facet is oriented yet
1395     {
1396       // find the least ambiguously oriented facet
1397       int faceMostConvex = -1;
1398       std::map< double, int > convexity2face;
1399       for ( size_t iF = 0; iF < myPolyFacetOri.size() && faceMostConvex < 0; ++iF )
1400       {
1401         if ( projectNodesToNormal( iF, minProj, maxProj ))
1402         {
1403           // all nodes are on the same side of the facet
1404           me->myPolyFacetOri[ iF ] = ( minProj < 0 ? +1 : -1 );
1405           faceMostConvex = iF;
1406         }
1407         else
1408         {
1409           ori = ( -minProj < maxProj ? -1 : +1 );
1410           double convexity = std::min( -minProj, maxProj ) / std::max( -minProj, maxProj );
1411           convexity2face.insert( std::make_pair( convexity, iF * ori ));
1412         }
1413       }
1414       if ( faceMostConvex < 0 ) // none facet has nodes on the same side
1415       {
1416         // use the least ambiguous facet
1417         faceMostConvex = convexity2face.begin()->second;
1418         ori = ( faceMostConvex < 0 ? -1 : +1 );
1419         faceMostConvex = std::abs( faceMostConvex );
1420         me->myPolyFacetOri[ faceMostConvex ] = ori;
1421       }
1422     }
1423     // collect links of the oriented facets in myFwdLinks
1424     for ( size_t iF = 0; iF < myPolyFacetOri.size(); ++iF )
1425     {
1426       ori = myPolyFacetOri[ iF ];
1427       if ( !ori ) continue;
1428       setFace( iF );
1429       for ( int i = 0; i < myCurFace.myNbNodes; ++i )
1430       {
1431         NLink link( myCurFace.myNodes[i], myCurFace.myNodes[i+1], ori );
1432         me->myFwdLinks.insert( make_pair( link, link.myOri ));
1433       }
1434     }
1435   }
1436
1437   // compare orientation of links of the facet with myFwdLinks
1438   ori = 0;
1439   setFace( faceIndex );
1440   std::vector< NLink > links( myCurFace.myNbNodes ), links2;
1441   for ( int i = 0; i < myCurFace.myNbNodes && !ori; ++i )
1442   {
1443     NLink link( myCurFace.myNodes[i], myCurFace.myNodes[i+1] );
1444     std::map<Link, int>::const_iterator l2o = myFwdLinks.find( link );
1445     if ( l2o != myFwdLinks.end() )
1446       ori = link.myOri * l2o->second * -1;
1447     links[ i ] = link;
1448   }
1449   while ( !ori ) // the facet has no common links with already oriented facets
1450   {
1451     // orient and collect links of other non-oriented facets
1452     for ( size_t iF = 0; iF < myPolyFacetOri.size(); ++iF )
1453     {
1454       if ( myPolyFacetOri[ iF ] ) continue; // already oriented
1455       setFace( iF );
1456       links2.clear();
1457       ori = 0;
1458       for ( int i = 0; i < myCurFace.myNbNodes && !ori; ++i )
1459       {
1460         NLink link( myCurFace.myNodes[i], myCurFace.myNodes[i+1] );
1461         std::map<Link, int>::const_iterator l2o = myFwdLinks.find( link );
1462         if ( l2o != myFwdLinks.end() )
1463           ori = link.myOri * l2o->second * -1;
1464         links2.push_back( link );
1465       }
1466       if ( ori ) // one more facet oriented
1467       {
1468         me->myPolyFacetOri[ iF ] = ori;
1469         for ( size_t i = 0; i < links2.size(); ++i )
1470           me->myFwdLinks.insert( make_pair( links2[i], links2[i].myOri * ori ));
1471         break;
1472       }
1473     }
1474     if ( !ori )
1475       return false; // error in algorithm: infinite loop
1476
1477     // try to orient the facet again
1478     ori = 0;
1479     for ( size_t i = 0; i < links.size() && !ori; ++i )
1480     {
1481       std::map<Link, int>::const_iterator l2o = myFwdLinks.find( links[i] );
1482       if ( l2o != myFwdLinks.end() )
1483         ori = links[i].myOri * l2o->second * -1;
1484     }
1485     me->myPolyFacetOri[ faceIndex ] = ori;
1486   }
1487
1488   return ori > 0;
1489 }
1490
1491 //=======================================================================
1492 //function : projectNodesToNormal
1493 //purpose  : compute min and max projections of all nodes to normal of a facet.
1494 //=======================================================================
1495
1496 bool SMDS_VolumeTool::projectNodesToNormal( int     faceIndex,
1497                                             double& minProj,
1498                                             double& maxProj,
1499                                             double* normalXYZ ) const
1500 {
1501   minProj = std::numeric_limits<double>::max();
1502   maxProj = std::numeric_limits<double>::min();
1503
1504   XYZ normal;
1505   if ( !GetFaceNormal( faceIndex, normal.x, normal.y, normal.z ))
1506     return false;
1507   if ( normalXYZ )
1508     memcpy( normalXYZ, normal.data(), 3*sizeof(double));
1509
1510   XYZ p0 ( myCurFace.myNodes[0] );
1511   for ( size_t i = 0; i < myVolumeNodes.size(); ++i )
1512   {
1513     if ( std::find( myCurFace.myNodes.begin() + 1,
1514                     myCurFace.myNodes.end(),
1515                     myVolumeNodes[ i ] ) != myCurFace.myNodes.end() )
1516       continue; // node of the faceIndex-th facet
1517
1518     double proj = normal.Dot( XYZ( myVolumeNodes[ i ]) - p0 );
1519     if ( proj < minProj ) minProj = proj;
1520     if ( proj > maxProj ) maxProj = proj;
1521   }
1522   const double tol = 1e-7;
1523   minProj += tol;
1524   maxProj -= tol;
1525   bool diffSize = ( minProj * maxProj < 0 );
1526   // if ( diffSize )
1527   // {
1528   //   minProj = -minProj;
1529   // }
1530   // else if ( minProj < 0 )
1531   // {
1532   //   minProj = -minProj;
1533   //   maxProj = -maxProj;
1534   // }
1535
1536   return !diffSize; // ? 0 : (minProj >= 0);
1537 }
1538
1539 //=======================================================================
1540 //function : GetFaceNormal
1541 //purpose  : Return a normal to a face
1542 //=======================================================================
1543
1544 bool SMDS_VolumeTool::GetFaceNormal (int faceIndex, double & X, double & Y, double & Z) const
1545 {
1546   if ( !setFace( faceIndex ))
1547     return false;
1548
1549   const int iQuad = ( !myPolyedre && myCurFace.myNbNodes > 6 ) ? 2 : 1;
1550   XYZ p1 ( myCurFace.myNodes[0*iQuad] );
1551   XYZ p2 ( myCurFace.myNodes[1*iQuad] );
1552   XYZ p3 ( myCurFace.myNodes[2*iQuad] );
1553   XYZ aVec12( p2 - p1 );
1554   XYZ aVec13( p3 - p1 );
1555   XYZ cross = aVec12.Crossed( aVec13 );
1556
1557   for ( int i = 3*iQuad; i < myCurFace.myNbNodes; i += iQuad )
1558   {
1559     XYZ p4 ( myCurFace.myNodes[i] );
1560     XYZ aVec14( p4 - p1 );
1561     XYZ cross2 = aVec13.Crossed( aVec14 );
1562     cross = cross + cross2;
1563     aVec13 = aVec14;
1564   }
1565
1566   double size = cross.Magnitude();
1567   if ( size <= std::numeric_limits<double>::min() )
1568     return false;
1569
1570   X = cross.x / size;
1571   Y = cross.y / size;
1572   Z = cross.z / size;
1573
1574   return true;
1575 }
1576
1577 //================================================================================
1578 /*!
1579  * \brief Return barycenter of a face
1580  */
1581 //================================================================================
1582
1583 bool SMDS_VolumeTool::GetFaceBaryCenter (int faceIndex, double & X, double & Y, double & Z) const
1584 {
1585   if ( !setFace( faceIndex ))
1586     return false;
1587
1588   X = Y = Z = 0.0;
1589   for ( int i = 0; i < myCurFace.myNbNodes; ++i )
1590   {
1591     X += myCurFace.myNodes[i]->X() / myCurFace.myNbNodes;
1592     Y += myCurFace.myNodes[i]->Y() / myCurFace.myNbNodes;
1593     Z += myCurFace.myNodes[i]->Z() / myCurFace.myNbNodes;
1594   }
1595   return true;
1596 }
1597
1598 //================================================================================
1599 /*!
1600  * \brief Check that all the faces in a polyhedron follow the same orientation
1601  * \remark there is no differentiation for outward and inward face orientation.
1602  */
1603 //================================================================================
1604 bool SMDS_VolumeTool::AllFacesSameOriented() const
1605 {  
1606   SMDS_VolumeTool* me = const_cast< SMDS_VolumeTool* > ( this );
1607   bool validOrientation = true;
1608   std::map<Link, std::vector<int>> collectLinksOrientations;
1609   me->myFwdLinks.clear();
1610   for ( int faceId = 0; faceId < NbFaces(); ++faceId )
1611   {
1612     me->setFace( faceId );
1613     myExternalFaces = false;
1614
1615     // Build the links
1616     for ( int i = 0; i < myCurFace.myNbNodes; ++i )
1617     {
1618       NLink link( myCurFace.myNodes[i], myCurFace.myNodes[i+1] );
1619       std::map<Link, int>::const_iterator foundLink = myFwdLinks.find( link );
1620
1621       if ( foundLink == myFwdLinks.end() )
1622         me->myFwdLinks.insert( make_pair( link, link.myOri ));               
1623
1624       collectLinksOrientations[ link ].push_back( link.myOri );
1625     }     
1626   }
1627
1628   // Check duality of the orientations
1629   std::map<Link, std::vector<int>>::const_iterator theLinks;
1630   for ( theLinks = collectLinksOrientations.begin(); theLinks != collectLinksOrientations.end(); theLinks++ )
1631   {
1632     if ( theLinks->second.size() == 2 ) // 99% of the cases there are 2 faces per link
1633     {
1634       if ( 1 != -1*theLinks->second[0]*theLinks->second[1] ) 
1635         return false;
1636       continue;
1637     }
1638
1639     if ( theLinks->second.size() % 2 != 0 )// Dont treat uneven number of links 
1640       continue;
1641
1642     // In the other 1% of the cases we count the number occurrence and check that they match
1643     int minusOne  = std::count( theLinks->second.begin(), theLinks->second.end(), -1 );
1644     int plusOne   = std::count( theLinks->second.begin(), theLinks->second.end(),  1 );
1645     if ( minusOne != plusOne ) 
1646       return false;
1647   }
1648
1649   return validOrientation;
1650 }
1651
1652 //=======================================================================
1653 //function : GetFaceArea
1654 //purpose  : Return face area
1655 //=======================================================================
1656
1657 double SMDS_VolumeTool::GetFaceArea( int faceIndex ) const
1658 {
1659   double area = 0;
1660   if ( !setFace( faceIndex ))
1661     return area;
1662
1663   XYZ p1 ( myCurFace.myNodes[0] );
1664   XYZ p2 ( myCurFace.myNodes[1] );
1665   XYZ p3 ( myCurFace.myNodes[2] );
1666   XYZ aVec12( p2 - p1 );
1667   XYZ aVec13( p3 - p1 );
1668   area += aVec12.Crossed( aVec13 ).Magnitude();
1669
1670   if (myVolume->IsPoly())
1671   {
1672     for ( int i = 3; i < myCurFace.myNbNodes; ++i )
1673     {
1674       XYZ pI ( myCurFace.myNodes[i] );
1675       XYZ aVecI( pI - p1 );
1676       area += aVec13.Crossed( aVecI ).Magnitude();
1677       aVec13 = aVecI;
1678     }
1679   }
1680   else
1681   {
1682     if ( myCurFace.myNbNodes == 4 ) {
1683       XYZ p4 ( myCurFace.myNodes[3] );
1684       XYZ aVec14( p4 - p1 );
1685       area += aVec14.Crossed( aVec13 ).Magnitude();
1686     }
1687   }
1688   return area / 2;
1689 }
1690
1691 //================================================================================
1692 /*!
1693  * \brief Return index of the node located at face center of a quadratic element like HEX27
1694  */
1695 //================================================================================
1696
1697 int SMDS_VolumeTool::GetCenterNodeIndex( int faceIndex ) const
1698 {
1699   if ( myAllFacesNbNodes && myVolumeNodes.size() == 27 ) // classic element with 27 nodes
1700   {
1701     switch ( faceIndex ) {
1702     case 0: return 20;
1703     case 1: return 25;
1704     default:
1705       return faceIndex + 19;
1706     }
1707   }
1708   return -1;
1709 }
1710
1711 //=======================================================================
1712 //function : GetOppFaceIndex
1713 //purpose  : Return index of the opposite face if it exists, else -1.
1714 //=======================================================================
1715
1716 int SMDS_VolumeTool::GetOppFaceIndex( int faceIndex ) const
1717 {
1718   int ind = -1;
1719   if (myPolyedre) {
1720     MESSAGE("Warning: attempt to obtain opposite face on polyhedral volume");
1721     return ind;
1722   }
1723
1724   const int nbHoriFaces = 2;
1725
1726   if ( faceIndex >= 0 && faceIndex < NbFaces() ) {
1727     switch ( myVolumeNodes.size() ) {
1728     case 6:
1729     case 15:
1730       if ( faceIndex == 0 || faceIndex == 1 )
1731         ind = 1 - faceIndex;
1732       break;
1733     case 8:
1734     case 12:
1735       if ( faceIndex <= 1 ) // top or bottom
1736         ind = 1 - faceIndex;
1737       else {
1738         const int nbSideFaces = myAllFacesNbNodes[0];
1739         ind = ( faceIndex - nbHoriFaces + nbSideFaces/2 ) % nbSideFaces + nbHoriFaces;
1740       }
1741       break;
1742     case 20:
1743     case 27:
1744       ind = GetOppFaceIndexOfHex( faceIndex );
1745       break;
1746     default:;
1747     }
1748   }
1749   return ind;
1750 }
1751
1752 //=======================================================================
1753 //function : GetOppFaceIndexOfHex
1754 //purpose  : Return index of the opposite face of the hexahedron
1755 //=======================================================================
1756
1757 int SMDS_VolumeTool::GetOppFaceIndexOfHex( int faceIndex )
1758 {
1759   return Hexa_oppF[ faceIndex ];
1760 }
1761
1762 //=======================================================================
1763 //function : IsLinked
1764 //purpose  : return true if theNode1 is linked with theNode2
1765 // If theIgnoreMediumNodes then corner nodes of quadratic cell are considered linked as well
1766 //=======================================================================
1767
1768 bool SMDS_VolumeTool::IsLinked (const SMDS_MeshNode* theNode1,
1769                                 const SMDS_MeshNode* theNode2,
1770                                 const bool           theIgnoreMediumNodes) const
1771 {
1772   if ( !myVolume )
1773     return false;
1774
1775   if (myVolume->IsPoly()) {
1776     if (!myPolyedre) {
1777       MESSAGE("Warning: bad volumic element");
1778       return false;
1779     }
1780     if ( !myAllFacesNbNodes ) {
1781       SMDS_VolumeTool*  me = const_cast< SMDS_VolumeTool* >( this );
1782       me->myPolyQuantities = myPolyedre->GetQuantities();
1783       myAllFacesNbNodes    = &myPolyQuantities[0];
1784     }
1785     int from, to = 0, d1 = 1, d2 = 2;
1786     if ( myPolyedre->IsQuadratic() ) {
1787       if ( theIgnoreMediumNodes ) {
1788         d1 = 2; d2 = 0;
1789       }
1790     } else {
1791       d2 = 0;
1792     }
1793     std::vector<const SMDS_MeshNode*>::const_iterator i;
1794     for (int iface = 0; iface < myNbFaces; iface++)
1795     {
1796       from = to;
1797       to  += myPolyQuantities[iface];
1798       i    = std::find( myVolumeNodes.begin() + from, myVolumeNodes.begin() + to, theNode1 );
1799       if ( i != myVolumeNodes.end() )
1800       {
1801         if ((  theNode2 == *( i-d1 ) ||
1802                theNode2 == *( i+d1 )))
1803           return true;
1804         if (( d2 ) &&
1805             (( theNode2 == *( i-d2 ) ||
1806                theNode2 == *( i+d2 ))))
1807           return true;
1808       }
1809     }
1810     return false;
1811   }
1812
1813   // find nodes indices
1814   int i1 = -1, i2 = -1, nbFound = 0;
1815   for ( size_t i = 0; i < myVolumeNodes.size() && nbFound < 2; i++ )
1816   {
1817     if ( myVolumeNodes[ i ] == theNode1 )
1818       i1 = i, ++nbFound;
1819     else if ( myVolumeNodes[ i ] == theNode2 )
1820       i2 = i, ++nbFound;
1821   }
1822   return IsLinked( i1, i2 );
1823 }
1824
1825 //=======================================================================
1826 //function : IsLinked
1827 //purpose  : return true if the node with theNode1Index is linked
1828 //           with the node with theNode2Index
1829 // If theIgnoreMediumNodes then corner nodes of quadratic cell are considered linked as well
1830 //=======================================================================
1831
1832 bool SMDS_VolumeTool::IsLinked (const int theNode1Index,
1833                                 const int theNode2Index,
1834                                 bool      theIgnoreMediumNodes) const
1835 {
1836   if ( myVolume->IsPoly() ) {
1837     return IsLinked(myVolumeNodes[theNode1Index], myVolumeNodes[theNode2Index]);
1838   }
1839
1840   int minInd = std::min( theNode1Index, theNode2Index );
1841   int maxInd = std::max( theNode1Index, theNode2Index );
1842
1843   if ( minInd < 0 || maxInd > (int)myVolumeNodes.size() - 1 || maxInd == minInd )
1844     return false;
1845
1846   VolumeType type = GetVolumeType();
1847   if ( myVolume->IsQuadratic() )
1848   {
1849     int firstMediumInd = myVolume->NbCornerNodes();
1850     if ( minInd >= firstMediumInd )
1851       return false; // both nodes are medium - not linked
1852     if ( maxInd < firstMediumInd ) // both nodes are corners
1853     {
1854       if ( theIgnoreMediumNodes )
1855         type = quadToLinear(type); // to check linkage of corner nodes only
1856       else
1857         return false; // corner nodes are not linked directly in a quadratic cell
1858     }
1859   }
1860
1861   switch ( type ) {
1862   case TETRA:
1863     return true;
1864   case HEXA:
1865     switch ( maxInd - minInd ) {
1866     case 1: return minInd != 3;
1867     case 3: return minInd == 0 || minInd == 4;
1868     case 4: return true;
1869     default:;
1870     }
1871     break;
1872   case PYRAM:
1873     if ( maxInd == 4 )
1874       return true;
1875     switch ( maxInd - minInd ) {
1876     case 1:
1877     case 3: return true;
1878     default:;
1879     }
1880     break;
1881   case PENTA:
1882     switch ( maxInd - minInd ) {
1883     case 1: return minInd != 2;
1884     case 2: return minInd == 0 || minInd == 3;
1885     case 3: return true;
1886     default:;
1887     }
1888     break;
1889   case QUAD_TETRA:
1890     {
1891       switch ( minInd ) {
1892       case 0: return ( maxInd==4 ||  maxInd==6 ||  maxInd==7 );
1893       case 1: return ( maxInd==4 ||  maxInd==5 ||  maxInd==8 );
1894       case 2: return ( maxInd==5 ||  maxInd==6 ||  maxInd==9 );
1895       case 3: return ( maxInd==7 ||  maxInd==8 ||  maxInd==9 );
1896       default:;
1897       }
1898       break;
1899     }
1900   case QUAD_HEXA:
1901     {
1902       switch ( minInd ) {
1903       case 0: return ( maxInd==8  ||  maxInd==11 ||  maxInd==16 );
1904       case 1: return ( maxInd==8  ||  maxInd==9  ||  maxInd==17 );
1905       case 2: return ( maxInd==9  ||  maxInd==10 ||  maxInd==18 );
1906       case 3: return ( maxInd==10 ||  maxInd==11 ||  maxInd==19 );
1907       case 4: return ( maxInd==12 ||  maxInd==15 ||  maxInd==16 );
1908       case 5: return ( maxInd==12 ||  maxInd==13 ||  maxInd==17 );
1909       case 6: return ( maxInd==13 ||  maxInd==14 ||  maxInd==18 );
1910       case 7: return ( maxInd==14 ||  maxInd==15 ||  maxInd==19 );
1911       default:;
1912       }
1913       break;
1914     }
1915   case QUAD_PYRAM:
1916     {
1917       switch ( minInd ) {
1918       case 0: return ( maxInd==5 ||  maxInd==8  ||  maxInd==9  );
1919       case 1: return ( maxInd==5 ||  maxInd==6  ||  maxInd==10 );
1920       case 2: return ( maxInd==6 ||  maxInd==7  ||  maxInd==11 );
1921       case 3: return ( maxInd==7 ||  maxInd==8  ||  maxInd==12 );
1922       case 4: return ( maxInd==9 ||  maxInd==10 ||  maxInd==11 ||  maxInd==12 );
1923       default:;
1924       }
1925       break;
1926     }
1927   case QUAD_PENTA:
1928     {
1929       switch ( minInd ) {
1930       case 0: return ( maxInd==6  ||  maxInd==8  ||  maxInd==12 );
1931       case 1: return ( maxInd==6  ||  maxInd==7  ||  maxInd==13 );
1932       case 2: return ( maxInd==7  ||  maxInd==8  ||  maxInd==14 );
1933       case 3: return ( maxInd==9  ||  maxInd==11 ||  maxInd==12 );
1934       case 4: return ( maxInd==9  ||  maxInd==10 ||  maxInd==13 );
1935       case 5: return ( maxInd==10 ||  maxInd==11 ||  maxInd==14 );
1936       default:;
1937       }
1938       break;
1939     }
1940   case HEX_PRISM:
1941     {
1942       const int diff = maxInd-minInd;
1943       if ( diff > 6  ) return false;// not linked top and bottom
1944       if ( diff == 6 ) return true; // linked top and bottom
1945       return diff == 1 || diff == 7;
1946     }
1947   default:;
1948   }
1949   return false;
1950 }
1951
1952 //=======================================================================
1953 //function : GetNodeIndex
1954 //purpose  : Return an index of theNode
1955 //=======================================================================
1956
1957 int SMDS_VolumeTool::GetNodeIndex(const SMDS_MeshNode* theNode) const
1958 {
1959   if ( myVolume ) {
1960     for ( size_t i = 0; i < myVolumeNodes.size(); i++ ) {
1961       if ( myVolumeNodes[ i ] == theNode )
1962         return i;
1963     }
1964   }
1965   return -1;
1966 }
1967
1968 //================================================================================
1969 /*!
1970  * \brief Fill vector with boundary faces existing in the mesh
1971   * \param faces - vector of found nodes
1972   * \retval int - nb of found faces
1973  */
1974 //================================================================================
1975
1976 int SMDS_VolumeTool::GetAllExistingFaces(std::vector<const SMDS_MeshElement*> & faces) const
1977 {
1978   faces.clear();
1979   SaveFacet savedFacet( myCurFace );
1980   if ( IsPoly() )
1981     for ( int iF = 0; iF < NbFaces(); ++iF ) {
1982       if ( setFace( iF ))
1983         if ( const SMDS_MeshElement* face = SMDS_Mesh::FindFace( myCurFace.myNodes ))
1984           faces.push_back( face );
1985     }
1986   else
1987     for ( int iF = 0; iF < NbFaces(); ++iF ) {
1988       const SMDS_MeshFace* face = 0;
1989       const SMDS_MeshNode** nodes = GetFaceNodes( iF );
1990       switch ( NbFaceNodes( iF )) {
1991       case 3:
1992         face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2] ); break;
1993       case 4:
1994         face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2], nodes[3] ); break;
1995       case 6:
1996         face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2],
1997                                     nodes[3], nodes[4], nodes[5]); break;
1998       case 8:
1999         face = SMDS_Mesh::FindFace( nodes[0], nodes[1], nodes[2], nodes[3],
2000                                     nodes[4], nodes[5], nodes[6], nodes[7]); break;
2001       }
2002       if ( face )
2003         faces.push_back( face );
2004     }
2005   return faces.size();
2006 }
2007
2008
2009 //================================================================================
2010 /*!
2011  * \brief Fill vector with boundary edges existing in the mesh
2012  * \param edges - vector of found edges
2013  * \retval int - nb of found faces
2014  */
2015 //================================================================================
2016
2017 int SMDS_VolumeTool::GetAllExistingEdges(std::vector<const SMDS_MeshElement*> & edges) const
2018 {
2019   edges.clear();
2020   edges.reserve( myVolumeNodes.size() * 2 );
2021   for ( size_t i = 0; i < myVolumeNodes.size()-1; ++i ) {
2022     for ( size_t j = i + 1; j < myVolumeNodes.size(); ++j ) {
2023       if ( IsLinked( i, j )) {
2024         const SMDS_MeshElement* edge =
2025           SMDS_Mesh::FindEdge( myVolumeNodes[i], myVolumeNodes[j] );
2026         if ( edge )
2027           edges.push_back( edge );
2028       }
2029     }
2030   }
2031   return edges.size();
2032 }
2033
2034 //================================================================================
2035 /*!
2036  * \brief Return minimal square distance between connected corner nodes
2037  */
2038 //================================================================================
2039
2040 double SMDS_VolumeTool::MinLinearSize2() const
2041 {
2042   double minSize = 1e+100;
2043   int iQ = myVolume->IsQuadratic() ? 2 : 1;
2044
2045   SaveFacet savedFacet( myCurFace );
2046
2047   // it seems that compute distance twice is faster than organization of a sole computing
2048   myCurFace.myIndex = -1;
2049   for ( int iF = 0; iF < myNbFaces; ++iF )
2050   {
2051     setFace( iF );
2052     for ( int iN = 0; iN < myCurFace.myNbNodes; iN += iQ )
2053     {
2054       XYZ n1( myCurFace.myNodes[ iN ]);
2055       XYZ n2( myCurFace.myNodes[(iN + iQ) % myCurFace.myNbNodes]);
2056       minSize = std::min( minSize, (n1 - n2).SquareMagnitude());
2057     }
2058   }
2059
2060   return minSize;
2061 }
2062
2063 //================================================================================
2064 /*!
2065  * \brief Return maximal square distance between connected corner nodes
2066  */
2067 //================================================================================
2068
2069 double SMDS_VolumeTool::MaxLinearSize2() const
2070 {
2071   double maxSize = -1e+100;
2072   int iQ = myVolume->IsQuadratic() ? 2 : 1;
2073
2074   SaveFacet savedFacet( myCurFace );
2075   
2076   // it seems that compute distance twice is faster than organization of a sole computing
2077   myCurFace.myIndex = -1;
2078   for ( int iF = 0; iF < myNbFaces; ++iF )
2079   {
2080     setFace( iF );
2081     for ( int iN = 0; iN < myCurFace.myNbNodes; iN += iQ )
2082     {
2083       XYZ n1( myCurFace.myNodes[ iN ]);
2084       XYZ n2( myCurFace.myNodes[(iN + iQ) % myCurFace.myNbNodes]);
2085       maxSize = std::max( maxSize, (n1 - n2).SquareMagnitude());
2086     }
2087   }
2088
2089   return maxSize;
2090 }
2091
2092 //================================================================================
2093 /*!
2094  * \brief Fast quickly check that only one volume is built on the face nodes
2095  *        This check is valid for conformal meshes only
2096  */
2097 //================================================================================
2098
2099 bool SMDS_VolumeTool::IsFreeFace( int faceIndex, const SMDS_MeshElement** otherVol/*=0*/ ) const
2100 {
2101   const bool isFree = true;
2102
2103   if ( !setFace( faceIndex ))
2104     return !isFree;
2105
2106   const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
2107
2108   const int  di = myVolume->IsQuadratic() ? 2 : 1;
2109   const int nbN = ( myCurFace.myNbNodes/di <= 4 && !IsPoly()) ? 3 : myCurFace.myNbNodes/di; // nb nodes to check
2110
2111   SMDS_ElemIteratorPtr eIt = nodes[0]->GetInverseElementIterator( SMDSAbs_Volume );
2112   while ( eIt->more() )
2113   {
2114     const SMDS_MeshElement* vol = eIt->next();
2115     if ( vol == myVolume )
2116       continue;
2117     int iN;
2118     for ( iN = 1; iN < nbN; ++iN )
2119       if ( vol->GetNodeIndex( nodes[ iN*di ]) < 0 )
2120         break;
2121     if ( iN == nbN ) // nbN nodes are shared with vol
2122     {
2123       // if ( vol->IsPoly() || vol->NbFaces() > 6 ) // vol is polyhed or hex prism 
2124       // {
2125       //   int nb = myCurFace.myNbNodes;
2126       //   if ( myVolume->GetEntityType() != vol->GetEntityType() )
2127       //     nb -= ( GetCenterNodeIndex(0) > 0 );
2128       //   std::set<const SMDS_MeshNode*> faceNodes( nodes, nodes + nb );
2129       //   if ( SMDS_VolumeTool( vol ).GetFaceIndex( faceNodes ) < 0 )
2130       //     continue;
2131       // }
2132       if ( otherVol ) *otherVol = vol;
2133       return !isFree;
2134     }
2135   }
2136   if ( otherVol ) *otherVol = 0;
2137   return isFree;
2138 }
2139
2140 //================================================================================
2141 /*!
2142  * \brief Thorough check that only one volume is built on the face nodes
2143  */
2144 //================================================================================
2145
2146 bool SMDS_VolumeTool::IsFreeFaceAdv( int faceIndex, const SMDS_MeshElement** otherVol/*=0*/ ) const
2147 {
2148   const bool isFree = true;
2149
2150   if (!setFace( faceIndex ))
2151     return !isFree;
2152
2153   const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
2154   const int nbFaceNodes = myCurFace.myNbNodes;
2155
2156   // evaluate nb of face nodes shared by other volumes
2157   int maxNbShared = -1;
2158   typedef std::map< const SMDS_MeshElement*, int > TElemIntMap;
2159   TElemIntMap volNbShared;
2160   TElemIntMap::iterator vNbIt;
2161   for ( int iNode = 0; iNode < nbFaceNodes; iNode++ ) {
2162     const SMDS_MeshNode* n = nodes[ iNode ];
2163     SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator( SMDSAbs_Volume );
2164     while ( eIt->more() ) {
2165       const SMDS_MeshElement* elem = eIt->next();
2166       if ( elem != myVolume ) {
2167         vNbIt = volNbShared.insert( std::make_pair( elem, 0 )).first;
2168         (*vNbIt).second++;
2169         if ( vNbIt->second > maxNbShared )
2170           maxNbShared = vNbIt->second;
2171       }
2172     }
2173   }
2174   if ( maxNbShared < 3 )
2175     return isFree; // is free
2176
2177   // find volumes laying on the opposite side of the face
2178   // and sharing all nodes
2179   XYZ intNormal; // internal normal
2180   GetFaceNormal( faceIndex, intNormal.x, intNormal.y, intNormal.z );
2181   if ( IsFaceExternal( faceIndex ))
2182     intNormal = XYZ( -intNormal.x, -intNormal.y, -intNormal.z );
2183   XYZ p0 ( nodes[0] ), baryCenter;
2184   for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end();  ) {
2185     const int& nbShared = (*vNbIt).second;
2186     if ( nbShared >= 3 ) {
2187       SMDS_VolumeTool volume( (*vNbIt).first );
2188       volume.GetBaryCenter( baryCenter.x, baryCenter.y, baryCenter.z );
2189       XYZ intNormal2( baryCenter - p0 );
2190       if ( intNormal.Dot( intNormal2 ) < 0 ) {
2191         // opposite side
2192         if ( nbShared >= nbFaceNodes )
2193         {
2194           // a volume shares the whole facet
2195           if ( otherVol ) *otherVol = vNbIt->first;
2196           return !isFree; 
2197         }
2198         ++vNbIt;
2199         continue;
2200       }
2201     }
2202     // remove a volume from volNbShared map
2203     volNbShared.erase( vNbIt++ );
2204   }
2205
2206   // here volNbShared contains only volumes laying on the opposite side of
2207   // the face and sharing 3 or more but not all face nodes with myVolume
2208   if ( volNbShared.size() < 2 ) {
2209     return isFree; // is free
2210   }
2211
2212   // check if the whole area of a face is shared
2213   for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
2214   {
2215     const SMDS_MeshNode* n = nodes[ iNode ];
2216     // check if n is shared by one of volumes of volNbShared
2217     bool isShared = false;
2218     SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator( SMDSAbs_Volume );
2219     while ( eIt->more() && !isShared )
2220       isShared = volNbShared.count( eIt->next() );
2221     if ( !isShared )
2222       return isFree;
2223   }
2224   if ( otherVol ) *otherVol = volNbShared.begin()->first;
2225   return !isFree;
2226
2227 //   if ( !myVolume->IsPoly() )
2228 //   {
2229 //     bool isShared[] = { false, false, false, false }; // 4 triangle parts of a quadrangle
2230 //     for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ ) {
2231 //       SMDS_VolumeTool volume( (*vNbIt).first );
2232 //       bool prevLinkShared = false;
2233 //       int nbSharedLinks = 0;
2234 //       for ( int iNode = 0; iNode < nbFaceNodes; iNode++ ) {
2235 //         bool linkShared = volume.IsLinked( nodes[ iNode ], nodes[ iNode + 1] );
2236 //         if ( linkShared )
2237 //           nbSharedLinks++;
2238 //         if ( linkShared && prevLinkShared &&
2239 //              volume.IsLinked( nodes[ iNode - 1 ], nodes[ iNode + 1] ))
2240 //           isShared[ iNode ] = true;
2241 //         prevLinkShared = linkShared;
2242 //       }
2243 //       if ( nbSharedLinks == nbFaceNodes )
2244 //         return !free; // is not free
2245 //       if ( nbFaceNodes == 4 ) {
2246 //         // check traingle parts 1 & 3
2247 //         if ( isShared[1] && isShared[3] )
2248 //           return !free; // is not free
2249 //         // check triangle parts 0 & 2;
2250 //         // 0 part could not be checked in the loop; check it here
2251 //         if ( isShared[2] && prevLinkShared &&
2252 //              volume.IsLinked( nodes[ 0 ], nodes[ 1 ] ) &&
2253 //              volume.IsLinked( nodes[ 1 ], nodes[ 3 ] ) )
2254 //           return !free; // is not free
2255 //       }
2256 //     }
2257 //   }
2258 //  return free;
2259 }
2260
2261 //=======================================================================
2262 //function : GetFaceIndex
2263 //purpose  : Return index of a face formed by theFaceNodes
2264 //=======================================================================
2265
2266 int SMDS_VolumeTool::GetFaceIndex( const std::set<const SMDS_MeshNode*>& theFaceNodes,
2267                                    const int                        theFaceIndexHint ) const
2268 {
2269   if ( theFaceIndexHint >= 0 )
2270   {
2271     int nbNodes = NbFaceNodes( theFaceIndexHint );
2272     if ( nbNodes == (int) theFaceNodes.size() )
2273     {
2274       const SMDS_MeshNode** nodes = GetFaceNodes( theFaceIndexHint );
2275       while ( nbNodes )
2276         if ( theFaceNodes.count( nodes[ nbNodes-1 ]))
2277           --nbNodes;
2278         else
2279           break;
2280       if ( nbNodes == 0 )
2281         return theFaceIndexHint;
2282     }
2283   }
2284   for ( int iFace = 0; iFace < myNbFaces; iFace++ )
2285   {
2286     if ( iFace == theFaceIndexHint )
2287       continue;
2288     int nbNodes = NbFaceNodes( iFace );
2289     if ( nbNodes == (int) theFaceNodes.size() )
2290     {
2291       const SMDS_MeshNode** nodes = GetFaceNodes( iFace );
2292       while ( nbNodes )
2293         if ( theFaceNodes.count( nodes[ nbNodes-1 ]))
2294           --nbNodes;
2295         else
2296           break;
2297       if ( nbNodes == 0 )
2298         return iFace;
2299     }
2300   }
2301   return -1;
2302 }
2303
2304 //=======================================================================
2305 //function : GetFaceIndex
2306 //purpose  : Return index of a face formed by theFaceNodes
2307 //=======================================================================
2308
2309 /*int SMDS_VolumeTool::GetFaceIndex( const std::set<int>& theFaceNodesIndices )
2310 {
2311   for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
2312     const int* nodes = GetFaceNodesIndices( iFace );
2313     int nbFaceNodes = NbFaceNodes( iFace );
2314     std::set<int> nodeSet;
2315     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
2316       nodeSet.insert( nodes[ iNode ] );
2317     if ( theFaceNodesIndices == nodeSet )
2318       return iFace;
2319   }
2320   return -1;
2321 }*/
2322
2323 //=======================================================================
2324 //function : setFace
2325 //purpose  : 
2326 //=======================================================================
2327
2328 bool SMDS_VolumeTool::setFace( int faceIndex ) const
2329 {
2330   if ( !myVolume )
2331     return false;
2332
2333   if ( myCurFace.myIndex == faceIndex )
2334     return true;
2335
2336   myCurFace.myIndex = -1;
2337
2338   if ( faceIndex < 0 || faceIndex >= NbFaces() )
2339     return false;
2340
2341   if (myVolume->IsPoly())
2342   {
2343     if ( !myPolyedre ) {
2344       MESSAGE("Warning: bad volumic element");
2345       return false;
2346     }
2347
2348     // set face nodes
2349     SMDS_VolumeTool* me = const_cast< SMDS_VolumeTool* >( this );
2350     if ( !myAllFacesNbNodes ) {
2351       me->myPolyQuantities = myPolyedre->GetQuantities();
2352       myAllFacesNbNodes    = &myPolyQuantities[0];
2353     }
2354     myCurFace.myNbNodes = myAllFacesNbNodes[ faceIndex ];
2355     myCurFace.myNodes.resize( myCurFace.myNbNodes + 1 );
2356     me->myPolyIndices.resize( myCurFace.myNbNodes + 1 );
2357     myCurFace.myNodeIndices = & me->myPolyIndices[0];
2358     int shift = std::accumulate( myAllFacesNbNodes, myAllFacesNbNodes+faceIndex, 0 );
2359     for ( int iNode = 0; iNode < myCurFace.myNbNodes; iNode++ )
2360     {
2361       myCurFace.myNodes      [ iNode ] = myVolumeNodes[ shift + iNode ];
2362       myCurFace.myNodeIndices[ iNode ] = shift + iNode;
2363     }
2364     myCurFace.myNodes      [ myCurFace.myNbNodes ] = myCurFace.myNodes[ 0 ]; // last = first
2365     myCurFace.myNodeIndices[ myCurFace.myNbNodes ] = myCurFace.myNodeIndices[ 0 ];
2366
2367     // check orientation
2368     if (myExternalFaces)
2369     {
2370       myCurFace.myIndex = faceIndex; // avoid infinite recursion in IsFaceExternal()
2371       myExternalFaces = false; // force normal computation by IsFaceExternal()
2372       if ( !IsFaceExternal( faceIndex ))
2373         std::reverse( myCurFace.myNodes.begin(), myCurFace.myNodes.end() );
2374       myExternalFaces = true;
2375     }
2376   }
2377   else
2378   {
2379     if ( !myAllFacesNodeIndices_F )
2380     {
2381       // choose data for an element type
2382       switch ( myVolumeNodes.size() ) {
2383       case 4:
2384         myAllFacesNodeIndices_F  = &Tetra_F [0][0];
2385         //myAllFacesNodeIndices_FE = &Tetra_F [0][0];
2386         myAllFacesNodeIndices_RE = &Tetra_RE[0][0];
2387         myAllFacesNbNodes        = Tetra_nbN;
2388         myMaxFaceNbNodes         = sizeof(Tetra_F[0])/sizeof(Tetra_F[0][0]);
2389         break;
2390       case 5:
2391         myAllFacesNodeIndices_F  = &Pyramid_F [0][0];
2392         //myAllFacesNodeIndices_FE = &Pyramid_F [0][0];
2393         myAllFacesNodeIndices_RE = &Pyramid_RE[0][0];
2394         myAllFacesNbNodes        = Pyramid_nbN;
2395         myMaxFaceNbNodes         = sizeof(Pyramid_F[0])/sizeof(Pyramid_F[0][0]);
2396         break;
2397       case 6:
2398         myAllFacesNodeIndices_F  = &Penta_F [0][0];
2399         //myAllFacesNodeIndices_FE = &Penta_FE[0][0];
2400         myAllFacesNodeIndices_RE = &Penta_RE[0][0];
2401         myAllFacesNbNodes        = Penta_nbN;
2402         myMaxFaceNbNodes         = sizeof(Penta_F[0])/sizeof(Penta_F[0][0]);
2403         break;
2404       case 8:
2405         myAllFacesNodeIndices_F  = &Hexa_F [0][0];
2406         ///myAllFacesNodeIndices_FE = &Hexa_FE[0][0];
2407         myAllFacesNodeIndices_RE = &Hexa_RE[0][0];
2408         myAllFacesNbNodes        = Hexa_nbN;
2409         myMaxFaceNbNodes         = sizeof(Hexa_F[0])/sizeof(Hexa_F[0][0]);
2410         break;
2411       case 10:
2412         myAllFacesNodeIndices_F  = &QuadTetra_F [0][0];
2413         //myAllFacesNodeIndices_FE = &QuadTetra_F [0][0];
2414         myAllFacesNodeIndices_RE = &QuadTetra_RE[0][0];
2415         myAllFacesNbNodes        = QuadTetra_nbN;
2416         myMaxFaceNbNodes         = sizeof(QuadTetra_F[0])/sizeof(QuadTetra_F[0][0]);
2417         break;
2418       case 13:
2419         myAllFacesNodeIndices_F  = &QuadPyram_F [0][0];
2420         //myAllFacesNodeIndices_FE = &QuadPyram_F [0][0];
2421         myAllFacesNodeIndices_RE = &QuadPyram_RE[0][0];
2422         myAllFacesNbNodes        = QuadPyram_nbN;
2423         myMaxFaceNbNodes         = sizeof(QuadPyram_F[0])/sizeof(QuadPyram_F[0][0]);
2424         break;
2425       case 15:
2426         myAllFacesNodeIndices_F  = &QuadPenta_F [0][0];
2427         //myAllFacesNodeIndices_FE = &QuadPenta_FE[0][0];
2428         myAllFacesNodeIndices_RE = &QuadPenta_RE[0][0];
2429         myAllFacesNbNodes        = QuadPenta_nbN;
2430         myMaxFaceNbNodes         = sizeof(QuadPenta_F[0])/sizeof(QuadPenta_F[0][0]);
2431         break;
2432       case 20:
2433       case 27:
2434         myAllFacesNodeIndices_F  = &QuadHexa_F [0][0];
2435         //myAllFacesNodeIndices_FE = &QuadHexa_FE[0][0];
2436         myAllFacesNodeIndices_RE = &QuadHexa_RE[0][0];
2437         myAllFacesNbNodes        = QuadHexa_nbN;
2438         myMaxFaceNbNodes         = sizeof(QuadHexa_F[0])/sizeof(QuadHexa_F[0][0]);
2439         if ( !myIgnoreCentralNodes && myVolumeNodes.size() == 27 )
2440         {
2441           myAllFacesNodeIndices_F  = &TriQuadHexa_F [0][0];
2442           //myAllFacesNodeIndices_FE = &TriQuadHexa_FE[0][0];
2443           myAllFacesNodeIndices_RE = &TriQuadHexa_RE[0][0];
2444           myAllFacesNbNodes        = TriQuadHexa_nbN;
2445           myMaxFaceNbNodes         = sizeof(TriQuadHexa_F[0])/sizeof(TriQuadHexa_F[0][0]);
2446         }
2447         break;
2448       case 12:
2449         myAllFacesNodeIndices_F  = &HexPrism_F [0][0];
2450         //myAllFacesNodeIndices_FE = &HexPrism_FE[0][0];
2451         myAllFacesNodeIndices_RE = &HexPrism_RE[0][0];
2452         myAllFacesNbNodes        = HexPrism_nbN;
2453         myMaxFaceNbNodes         = sizeof(HexPrism_F[0])/sizeof(HexPrism_F[0][0]);
2454         break;
2455       default:
2456         return false;
2457       }
2458     }
2459     myCurFace.myNbNodes = myAllFacesNbNodes[ faceIndex ];
2460     // if ( myExternalFaces )
2461     //   myCurFace.myNodeIndices = (int*)( myVolForward ? myAllFacesNodeIndices_FE + faceIndex*myMaxFaceNbNodes : myAllFacesNodeIndices_RE + faceIndex*myMaxFaceNbNodes );
2462     // else
2463     //   myCurFace.myNodeIndices = (int*)( myAllFacesNodeIndices_F + faceIndex*myMaxFaceNbNodes );
2464     myCurFace.myNodeIndices = (int*)( myVolForward ? myAllFacesNodeIndices_F + faceIndex*myMaxFaceNbNodes : myAllFacesNodeIndices_RE + faceIndex*myMaxFaceNbNodes );
2465
2466     // set face nodes
2467     myCurFace.myNodes.resize( myCurFace.myNbNodes + 1 );
2468     for ( int iNode = 0; iNode < myCurFace.myNbNodes; iNode++ )
2469       myCurFace.myNodes[ iNode ] = myVolumeNodes[ myCurFace.myNodeIndices[ iNode ]];
2470     myCurFace.myNodes[ myCurFace.myNbNodes ] = myCurFace.myNodes[ 0 ];
2471   }
2472
2473   myCurFace.myIndex = faceIndex;
2474
2475   return true;
2476 }
2477
2478 //=======================================================================
2479 //function : GetType
2480 //purpose  : return VolumeType by nb of nodes in a volume
2481 //=======================================================================
2482
2483 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetType(int nbNodes)
2484 {
2485   switch ( nbNodes ) {
2486   case 4: return TETRA;
2487   case 5: return PYRAM;
2488   case 6: return PENTA;
2489   case 8: return HEXA;
2490   case 10: return QUAD_TETRA;
2491   case 13: return QUAD_PYRAM;
2492   case 15: return QUAD_PENTA;
2493   case 20:
2494   case 27: return QUAD_HEXA;
2495   case 12: return HEX_PRISM;
2496   default:return UNKNOWN;
2497   }
2498 }
2499
2500 //=======================================================================
2501 //function : NbFaces
2502 //purpose  : return nb of faces by volume type
2503 //=======================================================================
2504
2505 int SMDS_VolumeTool::NbFaces( VolumeType type )
2506 {
2507   switch ( type ) {
2508   case TETRA     :
2509   case QUAD_TETRA: return 4;
2510   case PYRAM     :
2511   case QUAD_PYRAM: return 5;
2512   case PENTA     :
2513   case QUAD_PENTA: return 5;
2514   case HEXA      :
2515   case QUAD_HEXA : return 6;
2516   case HEX_PRISM : return 8;
2517   default:         return 0;
2518   }
2519 }
2520
2521 //================================================================================
2522 /*!
2523  * \brief Useful to know nb of corner nodes of a quadratic volume
2524   * \param type - volume type
2525   * \retval int - nb of corner nodes
2526  */
2527 //================================================================================
2528
2529 int SMDS_VolumeTool::NbCornerNodes(VolumeType type)
2530 {
2531   switch ( type ) {
2532   case TETRA     :
2533   case QUAD_TETRA: return 4;
2534   case PYRAM     :
2535   case QUAD_PYRAM: return 5;
2536   case PENTA     :
2537   case QUAD_PENTA: return 6;
2538   case HEXA      :
2539   case QUAD_HEXA : return 8;
2540   case HEX_PRISM : return 12;
2541   default:         return 0;
2542   }
2543   return 0;
2544 }
2545   // 
2546
2547 //=======================================================================
2548 //function : GetFaceNodesIndices
2549 //purpose  : Return the array of face nodes indices
2550 //           To comfort link iteration, the array
2551 //           length == NbFaceNodes( faceIndex ) + 1 and
2552 //           the last node index == the first one.
2553 //=======================================================================
2554
2555 const int* SMDS_VolumeTool::GetFaceNodesIndices(VolumeType type,
2556                                                 int        faceIndex,
2557                                                 bool       external)
2558 {
2559   switch ( type ) {
2560   case TETRA: return Tetra_F[ faceIndex ];
2561   case PYRAM: return Pyramid_F[ faceIndex ];
2562   case PENTA: return external ? Penta_F[ faceIndex ] : Penta_F[ faceIndex ];
2563   case HEXA:  return external ? Hexa_F[ faceIndex ] : Hexa_F[ faceIndex ];
2564   case QUAD_TETRA: return QuadTetra_F[ faceIndex ];
2565   case QUAD_PYRAM: return QuadPyram_F[ faceIndex ];
2566   case QUAD_PENTA: return external ? QuadPenta_F[ faceIndex ] : QuadPenta_F[ faceIndex ];
2567     // what about SMDSEntity_TriQuad_Hexa?
2568   case QUAD_HEXA:  return external ? QuadHexa_F[ faceIndex ] : QuadHexa_F[ faceIndex ];
2569   case HEX_PRISM:  return external ? HexPrism_F[ faceIndex ] : HexPrism_F[ faceIndex ];
2570   default:;
2571   }
2572   return 0;
2573 }
2574
2575 //=======================================================================
2576 //function : NbFaceNodes
2577 //purpose  : Return number of nodes in the array of face nodes
2578 //=======================================================================
2579
2580 int SMDS_VolumeTool::NbFaceNodes(VolumeType type,
2581                                  int        faceIndex )
2582 {
2583   switch ( type ) {
2584   case TETRA: return Tetra_nbN[ faceIndex ];
2585   case PYRAM: return Pyramid_nbN[ faceIndex ];
2586   case PENTA: return Penta_nbN[ faceIndex ];
2587   case HEXA:  return Hexa_nbN[ faceIndex ];
2588   case QUAD_TETRA: return QuadTetra_nbN[ faceIndex ];
2589   case QUAD_PYRAM: return QuadPyram_nbN[ faceIndex ];
2590   case QUAD_PENTA: return QuadPenta_nbN[ faceIndex ];
2591     // what about SMDSEntity_TriQuad_Hexa?
2592   case QUAD_HEXA:  return QuadHexa_nbN[ faceIndex ];
2593   case HEX_PRISM:  return HexPrism_nbN[ faceIndex ];
2594   default:;
2595   }
2596   return 0;
2597 }
2598
2599 //=======================================================================
2600 //function : Element
2601 //purpose  : return element
2602 //=======================================================================
2603
2604 const SMDS_MeshVolume* SMDS_VolumeTool::Element() const
2605 {
2606   return static_cast<const SMDS_MeshVolume*>( myVolume );
2607 }
2608
2609 //=======================================================================
2610 //function : ID
2611 //purpose  : return element ID
2612 //=======================================================================
2613
2614 smIdType SMDS_VolumeTool::ID() const
2615 {
2616   return myVolume ? myVolume->GetID() : 0;
2617 }