1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/
20 // File : SMDS_VolumeTool.cxx
21 // Created : Tue Jul 13 12:22:13 2004
22 // Author : Edward AGAPOV (eap)
23 // Copyright : Open CASCADE
26 #pragma warning(disable:4786)
29 #include "SMDS_VolumeTool.hxx"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
35 #include "utilities.h"
43 // ======================================================
44 // Node indices in faces depending on volume orientation
45 // making most faces normals external
46 // ======================================================
54 // N0 +---|---+ N1 TETRAHEDRON
62 static int Tetra_F [4][4] = { // FORWARD == EXTERNAL
63 { 0, 1, 2, 0 }, // All faces have external normals
67 static int Tetra_R [4][4] = { // REVERSED
68 { 0, 1, 2, 0 }, // All faces but a bottom have external normals
72 static int Tetra_RE [4][4] = { // REVERSED -> FORWARD (EXTERNAL)
73 { 0, 2, 1, 0 }, // All faces have external normals
77 static int Tetra_nbN [] = { 3, 3, 3, 3 };
82 static int Pyramid_F [5][5] = { // FORWARD == EXTERNAL
83 { 0, 1, 2, 3, 0 }, // All faces have external normals
88 static int Pyramid_R [5][5] = { // REVERSED
89 { 0, 1, 2, 3, 0 }, // All faces but a bottom have external normals
94 static int Pyramid_RE [5][5] = { // REVERSED -> FORWARD (EXTERNAL)
95 { 0, 3, 2, 1, 0 }, // All faces but a bottom have external normals
100 static int Pyramid_nbN [] = { 4, 3, 3, 3, 3 };
111 // | / \ | PENTAHEDRON
117 static int Penta_F [5][5] = { // FORWARD
118 { 0, 1, 2, 0, 0 }, // Top face has an internal normal, other - external
119 { 3, 4, 5, 3, 3 }, // 0 is bottom, 1 is top face
123 static int Penta_R [5][5] = { // REVERSED
124 { 0, 1, 2, 0, 0 }, // Bottom face has an internal normal, other - external
125 { 3, 4, 5, 3, 3 }, // 0 is bottom, 1 is top face
129 static int Penta_FE [5][5] = { // FORWARD -> EXTERNAL
135 static int Penta_RE [5][5] = { // REVERSED -> EXTERNAL
141 static int Penta_nbN [] = { 3, 3, 4, 4, 4 };
148 // N4+----------+N7 |
149 // | | | | HEXAHEDRON
152 // | N1+------|---+N2
158 static int Hexa_F [6][5] = { // FORWARD
159 { 0, 1, 2, 3, 0 }, // opposite faces are neighbouring,
160 { 4, 5, 6, 7, 4 }, // odd face(1,3,5) normal is internal, even(0,2,4) - external
161 { 1, 0, 4, 5, 1 }, // same index nodes of opposite faces are linked
165 // static int Hexa_R [6][5] = { // REVERSED
166 // { 0, 3, 2, 1, 0 }, // opposite faces are neighbouring,
167 // { 4, 7, 6, 5, 4 }, // odd face(1,3,5) normal is external, even(0,2,4) - internal
168 // { 1, 5, 4, 0, 1 }, // same index nodes of opposite faces are linked
169 // { 2, 6, 7, 3, 2 },
170 // { 0, 4, 7, 3, 0 },
171 // { 1, 5, 6, 2, 1 }};
172 static int Hexa_FE [6][5] = { // FORWARD -> EXTERNAL
173 { 0, 1, 2, 3, 0 } , // opposite faces are neighbouring,
174 { 4, 7, 6, 5, 4 }, // all face normals are external,
175 { 0, 4, 5, 1, 0 }, // links in opposite faces: 0-0, 1-3, 2-2, 3-1
179 static int Hexa_RE [6][5] = { // REVERSED -> EXTERNAL
180 { 0, 3, 2, 1, 0 }, // opposite faces are neighbouring,
181 { 4, 5, 6, 7, 4 }, // all face normals are external,
182 { 0, 1, 5, 4, 0 }, // links in opposite faces: 0-0, 1-3, 2-2, 3-1
186 static int Hexa_nbN [] = { 4, 4, 4, 4, 4, 4 };
195 // N0 +---|---+ N1 TETRAHEDRON
203 static int QuadTetra_F [4][7] = { // FORWARD == EXTERNAL
204 { 0, 4, 1, 5, 2, 6, 0 }, // All faces have external normals
205 { 0, 7, 3, 8, 1, 4, 0 },
206 { 1, 8, 3, 9, 2, 5, 1 },
207 { 0, 6, 2, 9, 3, 7, 0 }};
208 static int QuadTetra_R [4][7] = { // REVERSED
209 { 0, 4, 1, 5, 2, 6, 0 }, // All faces but a bottom have external normals
210 { 0, 4, 1, 8, 3, 7, 0 },
211 { 1, 5, 2, 9, 3, 8, 1 },
212 { 0, 7, 3, 9, 2, 6, 0 }};
213 static int QuadTetra_RE [4][7] = { // REVERSED -> FORWARD (EXTERNAL)
214 { 0, 6, 2, 5, 1, 4, 0 }, // All faces have external normals
215 { 0, 4, 1, 8, 3, 7, 0 },
216 { 1, 5, 2, 9, 3, 8, 1 },
217 { 0, 7, 3, 9, 2, 6, 0 }};
218 static int QuadTetra_nbN [] = { 6, 6, 6, 6 };
228 // | | 9 - middle point for (0,4) etc.
241 static int QuadPyram_F [5][9] = { // FORWARD == EXTERNAL
242 { 0, 5, 1, 6, 2, 7, 3, 8, 0 }, // All faces have external normals
243 { 0, 9, 4, 10,1, 5, 0, 4, 4 },
244 { 1, 10,4, 11,2, 6, 1, 4, 4 },
245 { 2, 11,4, 12,3, 7, 2, 4, 4 },
246 { 3, 12,4, 9, 0, 8, 3, 4, 4 }};
247 static int QuadPyram_R [5][9] = { // REVERSED
248 { 0, 5, 1, 6, 2, 7, 3, 8, 0 }, // All faces but a bottom have external normals
249 { 0, 5, 1, 10,4, 9, 0, 4, 4 },
250 { 1, 6, 2, 11,4, 10,1, 4, 4 },
251 { 2, 7, 3, 12,4, 11,2, 4, 4 },
252 { 3, 8, 0, 9, 4, 12,3, 4, 4 }};
253 static int QuadPyram_RE [5][9] = { // REVERSED -> FORWARD (EXTERNAL)
254 { 0, 8, 3, 7, 2, 6, 1, 5, 0 }, // All faces but a bottom have external normals
255 { 0, 5, 1, 10,4, 9, 0, 4, 4 },
256 { 1, 6, 2, 11,4, 10,1, 4, 4 },
257 { 2, 7, 3, 12,4, 11,2, 4, 4 },
258 { 3, 8, 0, 9, 4, 12,3, 4, 4 }};
259 static int QuadPyram_nbN [] = { 8, 6, 6, 6, 6 };
286 static int QuadPenta_F [5][9] = { // FORWARD
287 { 0, 6, 1, 7, 2, 8, 0, 0, 0 }, // Top face has an internal normal, other - external
288 { 3, 9, 4, 10,5, 11,3, 3, 3 }, // 0 is bottom, 1 is top face
289 { 0, 8, 2, 14,5, 11,3, 12,0 },
290 { 1, 13,4, 10,5, 14,2, 7, 1 },
291 { 0, 12,3, 9, 4, 13,1, 6, 0 }};
292 static int QuadPenta_R [5][9] = { // REVERSED
293 { 0, 6, 1, 7, 2, 8, 0, 0, 0 }, // Bottom face has an internal normal, other - external
294 { 3, 9, 4, 10,5, 11,3, 3, 3 }, // 0 is bottom, 1 is top face
295 { 0, 12,3, 11,5, 14,2, 8, 0 },
296 { 1, 7, 2, 14,5, 10,4, 13,1 },
297 { 0, 6, 1, 13,4, 9, 3, 12,0 }};
298 static int QuadPenta_FE [5][9] = { // FORWARD -> EXTERNAL
299 { 0, 6, 1, 7, 2, 8, 0, 0, 0 },
300 { 3,11, 5, 10,4, 9, 3, 3, 3 },
301 { 0, 8, 2, 14,5, 11,3, 12,0 },
302 { 1, 13,4, 10,5, 14,2, 7, 1 },
303 { 0, 12,3, 9, 4, 13,1, 6, 0 }};
304 static int QuadPenta_RE [5][9] = { // REVERSED -> EXTERNAL
305 { 0, 8, 2, 7, 1, 6, 0, 0, 0 },
306 { 3, 9, 4, 10,5, 11,3, 3, 3 },
307 { 0, 12,3, 11,5, 14,2, 8, 0 },
308 { 1, 7, 2, 14,5, 10,4, 13,1 },
309 { 0, 6, 1, 13,4, 9, 3, 12,0 }};
310 static int QuadPenta_nbN [] = { 6, 6, 8, 8, 8 };
318 // N4+-----+-----+N7 | QUADRATIC
319 // | | 15 | | HEXAHEDRON
328 // | N1+-----+-|---+N2
335 static int QuadHexa_F [6][9] = { // FORWARD
336 { 0, 8, 1, 9, 2, 10,3, 11,0 }, // opposite faces are neighbouring,
337 { 4, 12,5, 13,6, 14,7, 15,4 }, // odd face(1,3,5) normal is internal, even(0,2,4) - external
338 { 1, 8, 0, 16,4, 12,5, 17,1 }, // same index nodes of opposite faces are linked
339 { 2, 10,3, 19,7, 14,6, 18,2 },
340 { 0, 11,3, 19,7, 15,4, 16,0 },
341 { 1, 9, 2, 18,6, 13,5, 17,1 }};
342 // static int Hexa_R [6][5] = { // REVERSED
343 // { 0, 3, 2, 1, 0 }, // opposite faces are neighbouring,
344 // { 4, 7, 6, 5, 4 }, // odd face(1,3,5) normal is external, even(0,2,4) - internal
345 // { 1, 5, 4, 0, 1 }, // same index nodes of opposite faces are linked
346 // { 2, 6, 7, 3, 2 },
347 // { 0, 4, 7, 3, 0 },
348 // { 1, 5, 6, 2, 1 }};
349 static int QuadHexa_FE [6][9] = { // FORWARD -> EXTERNAL
350 { 0, 8, 1, 9, 2, 10,3, 11,0 }, // opposite faces are neighbouring,
351 { 4, 15,7, 14,6, 13,5, 12,4 }, // all face normals are external,
352 { 0, 16,4, 12,5, 17,1, 8, 0 }, // links in opposite faces: 0-0, 1-3, 2-2, 3-1
353 { 3, 10,2, 18,6, 14,7, 19,3 },
354 { 0, 11,3, 19,7, 15,4, 16,0 },
355 { 1, 17,5, 13,6, 18,2, 9, 1 }};
356 static int QuadHexa_RE [6][9] = { // REVERSED -> EXTERNAL
357 { 0, 11,3, 10,2, 9, 1, 8, 0 }, // opposite faces are neighbouring,
358 { 4, 12,5, 13,6, 14,7, 15,4 }, // all face normals are external,
359 { 0, 8, 1, 17,5, 12,4, 16,0 }, // links in opposite faces: 0-0, 1-3, 2-2, 3-1
360 { 3, 19,7, 14,6, 18,2, 10,3 },
361 { 0, 16,4, 15,7, 19,3, 11,0 },
362 { 1, 9, 2, 18,6, 13,5, 17,1 }};
363 static int QuadHexa_nbN [] = { 8, 8, 8, 8, 8, 8 };
366 // ========================================================
367 // to perform some calculations without linkage to CASCADE
368 // ========================================================
373 XYZ() { x = 0; y = 0; z = 0; }
374 XYZ( double X, double Y, double Z ) { x = X; y = Y; z = Z; }
375 XYZ( const XYZ& other ) { x = other.x; y = other.y; z = other.z; }
376 XYZ( const SMDS_MeshNode* n ) { x = n->X(); y = n->Y(); z = n->Z(); }
377 XYZ operator-( const XYZ& other );
378 XYZ Crossed( const XYZ& other );
379 double Dot( const XYZ& other );
382 XYZ XYZ::operator-( const XYZ& Right ) {
383 return XYZ(x - Right.x, y - Right.y, z - Right.z);
385 XYZ XYZ::Crossed( const XYZ& Right ) {
386 return XYZ (y * Right.z - z * Right.y,
387 z * Right.x - x * Right.z,
388 x * Right.y - y * Right.x);
390 double XYZ::Dot( const XYZ& Other ) {
391 return(x * Other.x + y * Other.y + z * Other.z);
393 double XYZ::Magnitude() {
394 return sqrt (x * x + y * y + z * z);
397 //=======================================================================
398 //function : SMDS_VolumeTool
400 //=======================================================================
402 SMDS_VolumeTool::SMDS_VolumeTool ()
405 myVolForward( true ),
407 myVolumeNbNodes( 0 ),
408 myVolumeNodes( NULL ),
409 myExternalFaces( false ),
412 myFaceNodeIndices( NULL ),
417 //=======================================================================
418 //function : SMDS_VolumeTool
420 //=======================================================================
422 SMDS_VolumeTool::SMDS_VolumeTool (const SMDS_MeshElement* theVolume)
425 myVolForward( true ),
427 myVolumeNbNodes( 0 ),
428 myVolumeNodes( NULL ),
429 myExternalFaces( false ),
432 myFaceNodeIndices( NULL ),
438 //=======================================================================
439 //function : SMDS_VolumeTool
441 //=======================================================================
443 SMDS_VolumeTool::~SMDS_VolumeTool()
445 if (myVolumeNodes != NULL) {
446 delete [] myVolumeNodes;
447 myVolumeNodes = NULL;
449 if (myFaceNodes != NULL) {
450 delete [] myFaceNodes;
455 //=======================================================================
456 //function : SetVolume
457 //purpose : Set volume to iterate on
458 //=======================================================================
460 bool SMDS_VolumeTool::Set (const SMDS_MeshElement* theVolume)
468 if (myVolumeNodes != NULL) {
469 delete [] myVolumeNodes;
470 myVolumeNodes = NULL;
473 myExternalFaces = false;
477 myFaceNodeIndices = NULL;
478 if (myFaceNodes != NULL) {
479 delete [] myFaceNodes;
483 if ( theVolume && theVolume->GetType() == SMDSAbs_Volume )
485 myVolume = theVolume;
487 myNbFaces = theVolume->NbFaces();
488 myVolumeNbNodes = theVolume->NbNodes();
492 myVolumeNodes = new const SMDS_MeshNode* [myVolumeNbNodes];
493 SMDS_ElemIteratorPtr nodeIt = myVolume->nodesIterator();
494 while ( nodeIt->more() ) {
495 myVolumeNodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
498 if (myVolume->IsPoly()) {
499 myPolyedre = static_cast<const SMDS_PolyhedralVolumeOfNodes*>( myVolume );
501 MESSAGE("Warning: bad volumic element");
506 switch ( myVolumeNbNodes ) {
515 // define volume orientation
517 GetFaceNormal( 0, botNormal.x, botNormal.y, botNormal.z );
518 const SMDS_MeshNode* topNode = myVolumeNodes[ myVolumeNbNodes - 1 ];
519 const SMDS_MeshNode* botNode = myVolumeNodes[ 0 ];
520 XYZ upDir (topNode->X() - botNode->X(),
521 topNode->Y() - botNode->Y(),
522 topNode->Z() - botNode->Z() );
523 myVolForward = ( botNormal.Dot( upDir ) < 0 );
531 return ( myVolume != 0 );
534 //=======================================================================
536 //purpose : Inverse volume
537 //=======================================================================
539 #define SWAP_NODES(nodes,i1,i2) \
541 const SMDS_MeshNode* tmp = nodes[ i1 ]; \
542 nodes[ i1 ] = nodes[ i2 ]; \
545 void SMDS_VolumeTool::Inverse ()
547 if ( !myVolume ) return;
549 if (myVolume->IsPoly()) {
550 MESSAGE("Warning: attempt to inverse polyhedral volume");
554 myVolForward = !myVolForward;
557 // inverse top and bottom faces
558 switch ( myVolumeNbNodes ) {
560 SWAP_NODES( myVolumeNodes, 1, 2 );
563 SWAP_NODES( myVolumeNodes, 1, 3 );
566 SWAP_NODES( myVolumeNodes, 1, 2 );
567 SWAP_NODES( myVolumeNodes, 4, 5 );
570 SWAP_NODES( myVolumeNodes, 1, 3 );
571 SWAP_NODES( myVolumeNodes, 5, 7 );
575 SWAP_NODES( myVolumeNodes, 1, 2 );
576 SWAP_NODES( myVolumeNodes, 4, 6 );
577 SWAP_NODES( myVolumeNodes, 8, 9 );
580 SWAP_NODES( myVolumeNodes, 1, 3 );
581 SWAP_NODES( myVolumeNodes, 5, 8 );
582 SWAP_NODES( myVolumeNodes, 6, 7 );
583 SWAP_NODES( myVolumeNodes, 10, 12 );
586 SWAP_NODES( myVolumeNodes, 1, 2 );
587 SWAP_NODES( myVolumeNodes, 4, 5 );
588 SWAP_NODES( myVolumeNodes, 6, 8 );
589 SWAP_NODES( myVolumeNodes, 9, 11 );
590 SWAP_NODES( myVolumeNodes, 13, 14 );
593 SWAP_NODES( myVolumeNodes, 1, 3 );
594 SWAP_NODES( myVolumeNodes, 5, 7 );
595 SWAP_NODES( myVolumeNodes, 8, 11 );
596 SWAP_NODES( myVolumeNodes, 9, 10 );
597 SWAP_NODES( myVolumeNodes, 12, 15 );
598 SWAP_NODES( myVolumeNodes, 13, 14 );
599 SWAP_NODES( myVolumeNodes, 17, 19 );
605 //=======================================================================
606 //function : GetVolumeType
608 //=======================================================================
610 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetVolumeType() const
616 // static const VolumeType types[] = {
617 // TETRA, // myVolumeNbNodes = 4
618 // PYRAM, // myVolumeNbNodes = 5
619 // PENTA, // myVolumeNbNodes = 6
620 // UNKNOWN, // myVolumeNbNodes = 7
621 // HEXA // myVolumeNbNodes = 8
623 // return types[ myVolumeNbNodes - 4 ];
624 switch(myVolumeNbNodes) {
625 case 4: return TETRA; break;
626 case 5: return PYRAM; break;
627 case 6: return PENTA; break;
628 case 8: return HEXA; break;
629 case 10: return QUAD_TETRA; break;
630 case 13: return QUAD_PYRAM; break;
631 case 15: return QUAD_PENTA; break;
632 case 20: return QUAD_HEXA; break;
640 //=======================================================================
641 //function : getTetraVolume
643 //=======================================================================
645 static double getTetraVolume(const SMDS_MeshNode* n1,
646 const SMDS_MeshNode* n2,
647 const SMDS_MeshNode* n3,
648 const SMDS_MeshNode* n4)
666 double Q1 = -(X1-X2)*(Y3*Z4-Y4*Z3);
667 double Q2 = (X1-X3)*(Y2*Z4-Y4*Z2);
668 double R1 = -(X1-X4)*(Y2*Z3-Y3*Z2);
669 double R2 = -(X2-X3)*(Y1*Z4-Y4*Z1);
670 double S1 = (X2-X4)*(Y1*Z3-Y3*Z1);
671 double S2 = -(X3-X4)*(Y1*Z2-Y2*Z1);
673 return (Q1+Q2+R1+R2+S1+S2)/6.0;
676 //=======================================================================
678 //purpose : Return element volume
679 //=======================================================================
681 double SMDS_VolumeTool::GetSize() const
687 if ( myVolume->IsPoly() )
692 // split a polyhedron into tetrahedrons
694 SMDS_VolumeTool* me = const_cast< SMDS_VolumeTool* > ( this );
696 me->GetBaryCenter(baryCenter.x, baryCenter.y, baryCenter.z);
697 SMDS_MeshNode bcNode ( baryCenter.x, baryCenter.y, baryCenter.z );
699 for ( int f = 0; f < NbFaces(); ++f )
701 bool externalFace = me->IsFaceExternal( f ); // it calls setFace()
702 for ( int n = 2; n < myFaceNbNodes; ++n )
704 double Vn = getTetraVolume( myFaceNodes[ 0 ],
708 /// cout <<"++++ " << Vn << " nodes " <<myFaceNodes[ 0 ]->GetID() << " " <<myFaceNodes[ n-1 ]->GetID() << " " <<myFaceNodes[ n ]->GetID() << " < " << V << endl;
709 V += externalFace ? -Vn : Vn;
715 const static int ind[] = {
716 0, 1, 3, 6, 11, 19, 32, 46, 66};
717 const static int vtab[][4] = {
734 // quadratic tetrahedron
759 // quadratic pentahedron
776 // quadratic hexahedron
801 int type = GetVolumeType();
803 int n2 = ind[type+1];
805 for (int i = n1; i < n2; i++) {
806 V -= getTetraVolume( myVolumeNodes[ vtab[i][0] ],
807 myVolumeNodes[ vtab[i][1] ],
808 myVolumeNodes[ vtab[i][2] ],
809 myVolumeNodes[ vtab[i][3] ]);
815 //=======================================================================
816 //function : GetBaryCenter
818 //=======================================================================
820 bool SMDS_VolumeTool::GetBaryCenter(double & X, double & Y, double & Z) const
826 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
827 X += myVolumeNodes[ i ]->X();
828 Y += myVolumeNodes[ i ]->Y();
829 Z += myVolumeNodes[ i ]->Z();
831 X /= myVolumeNbNodes;
832 Y /= myVolumeNbNodes;
833 Z /= myVolumeNbNodes;
838 //=======================================================================
839 //function : SetExternalNormal
840 //purpose : Node order will be so that faces normals are external
841 //=======================================================================
843 void SMDS_VolumeTool::SetExternalNormal ()
845 myExternalFaces = true;
849 //=======================================================================
850 //function : NbFaceNodes
851 //purpose : Return number of nodes in the array of face nodes
852 //=======================================================================
854 int SMDS_VolumeTool::NbFaceNodes( int faceIndex )
856 if ( !setFace( faceIndex ))
858 return myFaceNbNodes;
861 //=======================================================================
862 //function : GetFaceNodes
863 //purpose : Return pointer to the array of face nodes.
864 // To comfort link iteration, the array
865 // length == NbFaceNodes( faceIndex ) + 1 and
866 // the last node == the first one.
867 //=======================================================================
869 const SMDS_MeshNode** SMDS_VolumeTool::GetFaceNodes( int faceIndex )
871 if ( !setFace( faceIndex ))
876 //=======================================================================
877 //function : GetFaceNodesIndices
878 //purpose : Return pointer to the array of face nodes indices
879 // To comfort link iteration, the array
880 // length == NbFaceNodes( faceIndex ) + 1 and
881 // the last node index == the first one.
882 //=======================================================================
884 const int* SMDS_VolumeTool::GetFaceNodesIndices( int faceIndex )
886 if (myVolume->IsPoly()) {
887 MESSAGE("Warning: attempt to obtain FaceNodesIndices of polyhedral volume");
890 if ( !setFace( faceIndex ))
892 return myFaceNodeIndices;
895 //=======================================================================
896 //function : GetFaceNodes
897 //purpose : Return a set of face nodes.
898 //=======================================================================
900 bool SMDS_VolumeTool::GetFaceNodes (int faceIndex,
901 set<const SMDS_MeshNode*>& theFaceNodes )
903 if ( !setFace( faceIndex ))
906 theFaceNodes.clear();
907 int iNode, nbNode = myFaceNbNodes;
908 for ( iNode = 0; iNode < nbNode; iNode++ )
909 theFaceNodes.insert( myFaceNodes[ iNode ]);
914 //=======================================================================
915 //function : IsFaceExternal
916 //purpose : Check normal orientation of a returned face
917 //=======================================================================
919 bool SMDS_VolumeTool::IsFaceExternal( int faceIndex )
921 if ( myExternalFaces || !myVolume )
924 if (myVolume->IsPoly()) {
925 XYZ aNormal, baryCenter, p0 (myPolyedre->GetFaceNode(faceIndex + 1, 1));
926 GetFaceNormal(faceIndex, aNormal.x, aNormal.y, aNormal.z);
927 GetBaryCenter(baryCenter.x, baryCenter.y, baryCenter.z);
928 XYZ insideVec (baryCenter - p0);
929 if (insideVec.Dot(aNormal) > 0)
934 switch ( myVolumeNbNodes ) {
939 // only the bottom of a reversed tetrahedron can be internal
940 return ( myVolForward || faceIndex != 0 );
943 // in a forward pentahedron, the top is internal, in a reversed one - bottom
944 return ( myVolForward ? faceIndex != 1 : faceIndex != 0 );
947 // in a forward hexahedron, even face normal is external, odd - internal
948 bool odd = faceIndex % 2;
949 return ( myVolForward ? !odd : odd );
956 //=======================================================================
957 //function : GetFaceNormal
958 //purpose : Return a normal to a face
959 //=======================================================================
961 bool SMDS_VolumeTool::GetFaceNormal (int faceIndex, double & X, double & Y, double & Z)
963 if ( !setFace( faceIndex ))
966 XYZ p1 ( myFaceNodes[0] );
967 XYZ p2 ( myFaceNodes[1] );
968 XYZ p3 ( myFaceNodes[2] );
969 XYZ aVec12( p2 - p1 );
970 XYZ aVec13( p3 - p1 );
971 XYZ cross = aVec12.Crossed( aVec13 );
973 //if ( myFaceNbNodes == 4 ) {
974 if ( myFaceNbNodes >3 ) {
975 XYZ p4 ( myFaceNodes[3] );
976 XYZ aVec14( p4 - p1 );
977 XYZ cross2 = aVec13.Crossed( aVec14 );
983 double size = cross.Magnitude();
984 if ( size <= DBL_MIN )
994 //=======================================================================
995 //function : GetFaceArea
996 //purpose : Return face area
997 //=======================================================================
999 double SMDS_VolumeTool::GetFaceArea( int faceIndex )
1001 if (myVolume->IsPoly()) {
1002 MESSAGE("Warning: attempt to obtain area of a face of polyhedral volume");
1006 if ( !setFace( faceIndex ))
1009 XYZ p1 ( myFaceNodes[0] );
1010 XYZ p2 ( myFaceNodes[1] );
1011 XYZ p3 ( myFaceNodes[2] );
1012 XYZ aVec12( p2 - p1 );
1013 XYZ aVec13( p3 - p1 );
1014 double area = aVec12.Crossed( aVec13 ).Magnitude() * 0.5;
1016 if ( myFaceNbNodes == 4 ) {
1017 XYZ p4 ( myFaceNodes[3] );
1018 XYZ aVec14( p4 - p1 );
1019 area += aVec14.Crossed( aVec13 ).Magnitude() * 0.5;
1024 //=======================================================================
1025 //function : GetOppFaceIndex
1026 //purpose : Return index of the opposite face if it exists, else -1.
1027 //=======================================================================
1029 int SMDS_VolumeTool::GetOppFaceIndex( int faceIndex ) const
1032 if (myVolume->IsPoly()) {
1033 MESSAGE("Warning: attempt to obtain opposite face on polyhedral volume");
1037 if ( faceIndex >= 0 && faceIndex < NbFaces() ) {
1038 switch ( myVolumeNbNodes ) {
1040 if ( faceIndex == 0 || faceIndex == 1 )
1041 ind = 1 - faceIndex;
1044 ind = faceIndex + ( faceIndex % 2 ? -1 : 1 );
1052 //=======================================================================
1053 //function : IsLinked
1054 //purpose : return true if theNode1 is linked with theNode2
1055 //=======================================================================
1057 bool SMDS_VolumeTool::IsLinked (const SMDS_MeshNode* theNode1,
1058 const SMDS_MeshNode* theNode2) const
1063 if (myVolume->IsPoly()) {
1065 MESSAGE("Warning: bad volumic element");
1068 bool isLinked = false;
1070 for (iface = 1; iface <= myNbFaces && !isLinked; iface++) {
1071 int inode, nbFaceNodes = myPolyedre->NbFaceNodes(iface);
1073 for (inode = 1; inode <= nbFaceNodes && !isLinked; inode++) {
1074 const SMDS_MeshNode* curNode = myPolyedre->GetFaceNode(iface, inode);
1076 if (curNode == theNode1 || curNode == theNode2) {
1077 int inextnode = (inode == nbFaceNodes) ? 1 : inode + 1;
1078 const SMDS_MeshNode* nextNode = myPolyedre->GetFaceNode(iface, inextnode);
1080 if ((curNode == theNode1 && nextNode == theNode2) ||
1081 (curNode == theNode2 && nextNode == theNode1)) {
1090 // find nodes indices
1091 int i1 = -1, i2 = -1;
1092 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
1093 if ( myVolumeNodes[ i ] == theNode1 )
1095 else if ( myVolumeNodes[ i ] == theNode2 )
1098 return IsLinked( i1, i2 );
1101 //=======================================================================
1102 //function : IsLinked
1103 //purpose : return true if the node with theNode1Index is linked
1104 // with the node with theNode2Index
1105 //=======================================================================
1107 bool SMDS_VolumeTool::IsLinked (const int theNode1Index,
1108 const int theNode2Index) const
1110 if ( myVolume->IsPoly() ) {
1111 return IsLinked(myVolumeNodes[theNode1Index], myVolumeNodes[theNode2Index]);
1114 int minInd = theNode1Index < theNode2Index ? theNode1Index : theNode2Index;
1115 int maxInd = theNode1Index < theNode2Index ? theNode2Index : theNode1Index;
1117 if ( minInd < 0 || maxInd > myVolumeNbNodes - 1 || maxInd == minInd )
1120 switch ( myVolumeNbNodes ) {
1126 switch ( maxInd - minInd ) {
1128 case 3: return true;
1133 switch ( maxInd - minInd ) {
1134 case 1: return minInd != 2;
1135 case 2: return minInd == 0 || minInd == 3;
1136 case 3: return true;
1141 switch ( maxInd - minInd ) {
1142 case 1: return minInd != 3;
1143 case 3: return minInd == 0 || minInd == 4;
1144 case 4: return true;
1151 case 0: if( maxInd==4 || maxInd==6 || maxInd==7 ) return true;
1152 case 1: if( maxInd==4 || maxInd==5 || maxInd==8 ) return true;
1153 case 2: if( maxInd==5 || maxInd==6 || maxInd==9 ) return true;
1154 case 3: if( maxInd==7 || maxInd==8 || maxInd==9 ) return true;
1162 case 0: if( maxInd==5 || maxInd==8 || maxInd==9 ) return true;
1163 case 1: if( maxInd==5 || maxInd==6 || maxInd==10 ) return true;
1164 case 2: if( maxInd==6 || maxInd==7 || maxInd==11 ) return true;
1165 case 3: if( maxInd==7 || maxInd==8 || maxInd==12 ) return true;
1166 case 4: if( maxInd==9 || maxInd==10 || maxInd==11 || maxInd==12 ) return true;
1174 case 0: if( maxInd==6 || maxInd==8 || maxInd==12 ) return true;
1175 case 1: if( maxInd==6 || maxInd==7 || maxInd==13 ) return true;
1176 case 2: if( maxInd==7 || maxInd==8 || maxInd==14 ) return true;
1177 case 3: if( maxInd==9 || maxInd==11 || maxInd==12 ) return true;
1178 case 4: if( maxInd==9 || maxInd==10 || maxInd==13 ) return true;
1179 case 5: if( maxInd==10 || maxInd==11 || maxInd==14 ) return true;
1187 case 0: if( maxInd==8 || maxInd==11 || maxInd==16 ) return true;
1188 case 1: if( maxInd==8 || maxInd==9 || maxInd==17 ) return true;
1189 case 2: if( maxInd==9 || maxInd==10 || maxInd==18 ) return true;
1190 case 3: if( maxInd==10 || maxInd==11 || maxInd==19 ) return true;
1191 case 4: if( maxInd==12 || maxInd==15 || maxInd==16 ) return true;
1192 case 5: if( maxInd==12 || maxInd==13 || maxInd==17 ) return true;
1193 case 6: if( maxInd==13 || maxInd==14 || maxInd==18 ) return true;
1194 case 7: if( maxInd==14 || maxInd==15 || maxInd==19 ) return true;
1204 //=======================================================================
1205 //function : GetNodeIndex
1206 //purpose : Return an index of theNode
1207 //=======================================================================
1209 int SMDS_VolumeTool::GetNodeIndex(const SMDS_MeshNode* theNode) const
1212 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
1213 if ( myVolumeNodes[ i ] == theNode )
1220 //=======================================================================
1221 //function : IsFreeFace
1222 //purpose : check that only one volume is build on the face nodes
1223 //=======================================================================
1225 bool SMDS_VolumeTool::IsFreeFace( int faceIndex )
1227 const int free = true;
1229 if (!setFace( faceIndex ))
1232 const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
1233 int nbFaceNodes = myFaceNbNodes;
1235 // evaluate nb of face nodes shared by other volume
1236 int maxNbShared = -1;
1237 typedef map< const SMDS_MeshElement*, int > TElemIntMap;
1238 TElemIntMap volNbShared;
1239 TElemIntMap::iterator vNbIt;
1240 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ ) {
1241 const SMDS_MeshNode* n = nodes[ iNode ];
1242 SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator();
1243 while ( eIt->more() ) {
1244 const SMDS_MeshElement* elem = eIt->next();
1245 if ( elem != myVolume && elem->GetType() == SMDSAbs_Volume ) {
1247 vNbIt = volNbShared.find( elem );
1248 if ( vNbIt == volNbShared.end() ) {
1249 volNbShared.insert ( TElemIntMap::value_type( elem, nbShared ));
1252 nbShared = ++(*vNbIt).second;
1254 if ( nbShared > maxNbShared )
1255 maxNbShared = nbShared;
1259 if ( maxNbShared < 3 )
1260 return free; // is free
1262 // find volumes laying on the opposite side of the face
1263 // and sharing all nodes
1264 XYZ intNormal; // internal normal
1265 GetFaceNormal( faceIndex, intNormal.x, intNormal.y, intNormal.z );
1266 if ( IsFaceExternal( faceIndex ))
1267 intNormal = XYZ( -intNormal.x, -intNormal.y, -intNormal.z );
1268 XYZ p0 ( nodes[0] ), baryCenter;
1269 for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ ) {
1270 int nbShared = (*vNbIt).second;
1271 if ( nbShared >= 3 ) {
1272 SMDS_VolumeTool volume( (*vNbIt).first );
1273 volume.GetBaryCenter( baryCenter.x, baryCenter.y, baryCenter.z );
1274 XYZ intNormal2( baryCenter - p0 );
1275 if ( intNormal.Dot( intNormal2 ) < 0 )
1276 continue; // opposite side
1278 // remove a volume from volNbShared map
1279 volNbShared.erase( vNbIt );
1282 // here volNbShared contains only volumes laying on the
1283 // opposite side of the face
1284 if ( volNbShared.empty() ) {
1285 return free; // is free
1288 // check if the whole area of a face is shared
1289 bool isShared[] = { false, false, false, false }; // 4 triangle parts of a quadrangle
1290 for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ ) {
1291 SMDS_VolumeTool volume( (*vNbIt).first );
1292 bool prevLinkShared = false;
1293 int nbSharedLinks = 0;
1294 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ ) {
1295 bool linkShared = volume.IsLinked( nodes[ iNode ], nodes[ iNode + 1] );
1298 if ( linkShared && prevLinkShared &&
1299 volume.IsLinked( nodes[ iNode - 1 ], nodes[ iNode + 1] ))
1300 isShared[ iNode ] = true;
1301 prevLinkShared = linkShared;
1303 if ( nbSharedLinks == nbFaceNodes )
1304 return !free; // is not free
1305 if ( nbFaceNodes == 4 ) {
1306 // check traingle parts 1 & 3
1307 if ( isShared[1] && isShared[3] )
1308 return !free; // is not free
1309 // check triangle parts 0 & 2;
1310 // 0 part could not be checked in the loop; check it here
1311 if ( isShared[2] && prevLinkShared &&
1312 volume.IsLinked( nodes[ 0 ], nodes[ 1 ] ) &&
1313 volume.IsLinked( nodes[ 1 ], nodes[ 3 ] ) )
1314 return !free; // is not free
1320 //=======================================================================
1321 //function : GetFaceIndex
1322 //purpose : Return index of a face formed by theFaceNodes
1323 //=======================================================================
1325 int SMDS_VolumeTool::GetFaceIndex( const set<const SMDS_MeshNode*>& theFaceNodes )
1327 for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
1328 const SMDS_MeshNode** nodes = GetFaceNodes( iFace );
1329 int nbFaceNodes = NbFaceNodes( iFace );
1330 set<const SMDS_MeshNode*> nodeSet;
1331 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1332 nodeSet.insert( nodes[ iNode ] );
1333 if ( theFaceNodes == nodeSet )
1339 //=======================================================================
1340 //function : GetFaceIndex
1341 //purpose : Return index of a face formed by theFaceNodes
1342 //=======================================================================
1344 /*int SMDS_VolumeTool::GetFaceIndex( const set<int>& theFaceNodesIndices )
1346 for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
1347 const int* nodes = GetFaceNodesIndices( iFace );
1348 int nbFaceNodes = NbFaceNodes( iFace );
1350 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1351 nodeSet.insert( nodes[ iNode ] );
1352 if ( theFaceNodesIndices == nodeSet )
1358 //=======================================================================
1359 //function : setFace
1361 //=======================================================================
1363 bool SMDS_VolumeTool::setFace( int faceIndex )
1368 if ( myCurFace == faceIndex )
1373 if ( faceIndex < 0 || faceIndex >= NbFaces() )
1376 if (myFaceNodes != NULL) {
1377 delete [] myFaceNodes;
1381 if (myVolume->IsPoly()) {
1383 MESSAGE("Warning: bad volumic element");
1387 // check orientation
1388 bool isGoodOri = true;
1389 if (myExternalFaces)
1390 isGoodOri = IsFaceExternal( faceIndex );
1394 myFaceNbNodes = myPolyedre->NbFaceNodes(faceIndex + 1);
1395 myFaceNodes = new const SMDS_MeshNode* [myFaceNbNodes + 1];
1397 for ( iNode = 0; iNode < myFaceNbNodes; iNode++ )
1398 myFaceNodes[ iNode ] = myPolyedre->GetFaceNode(faceIndex + 1, iNode + 1);
1400 for ( iNode = 0; iNode < myFaceNbNodes; iNode++ )
1401 myFaceNodes[ iNode ] = myPolyedre->GetFaceNode(faceIndex + 1, myFaceNbNodes - iNode);
1403 myFaceNodes[ myFaceNbNodes ] = myFaceNodes[ 0 ]; // last = first
1407 // choose face node indices
1408 switch ( myVolumeNbNodes ) {
1410 myFaceNbNodes = Tetra_nbN[ faceIndex ];
1411 if ( myExternalFaces )
1412 myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_RE[ faceIndex ];
1414 myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_R[ faceIndex ];
1417 myFaceNbNodes = Pyramid_nbN[ faceIndex ];
1418 if ( myExternalFaces )
1419 myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_RE[ faceIndex ];
1421 myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_R[ faceIndex ];
1424 myFaceNbNodes = Penta_nbN[ faceIndex ];
1425 if ( myExternalFaces )
1426 myFaceNodeIndices = myVolForward ? Penta_FE[ faceIndex ] : Penta_RE[ faceIndex ];
1428 myFaceNodeIndices = myVolForward ? Penta_F[ faceIndex ] : Penta_R[ faceIndex ];
1431 myFaceNbNodes = Hexa_nbN[ faceIndex ];
1432 if ( myExternalFaces )
1433 myFaceNodeIndices = myVolForward ? Hexa_FE[ faceIndex ] : Hexa_RE[ faceIndex ];
1435 myFaceNodeIndices = Hexa_F[ faceIndex ];
1438 myFaceNbNodes = QuadTetra_nbN[ faceIndex ];
1439 if ( myExternalFaces )
1440 myFaceNodeIndices = myVolForward ? QuadTetra_F[ faceIndex ] : QuadTetra_RE[ faceIndex ];
1442 myFaceNodeIndices = myVolForward ? QuadTetra_F[ faceIndex ] : QuadTetra_R[ faceIndex ];
1445 myFaceNbNodes = QuadPyram_nbN[ faceIndex ];
1446 if ( myExternalFaces )
1447 myFaceNodeIndices = myVolForward ? QuadPyram_F[ faceIndex ] : QuadPyram_RE[ faceIndex ];
1449 myFaceNodeIndices = myVolForward ? QuadPyram_F[ faceIndex ] : QuadPyram_R[ faceIndex ];
1452 myFaceNbNodes = QuadPenta_nbN[ faceIndex ];
1453 if ( myExternalFaces )
1454 myFaceNodeIndices = myVolForward ? QuadPenta_FE[ faceIndex ] : QuadPenta_RE[ faceIndex ];
1456 myFaceNodeIndices = myVolForward ? QuadPenta_F[ faceIndex ] : QuadPenta_R[ faceIndex ];
1459 myFaceNbNodes = QuadHexa_nbN[ faceIndex ];
1460 if ( myExternalFaces )
1461 myFaceNodeIndices = myVolForward ? QuadHexa_FE[ faceIndex ] : QuadHexa_RE[ faceIndex ];
1463 myFaceNodeIndices = QuadHexa_F[ faceIndex ];
1470 myFaceNodes = new const SMDS_MeshNode* [myFaceNbNodes + 1];
1471 for ( int iNode = 0; iNode < myFaceNbNodes; iNode++ )
1472 myFaceNodes[ iNode ] = myVolumeNodes[ myFaceNodeIndices[ iNode ]];
1473 myFaceNodes[ myFaceNbNodes ] = myFaceNodes[ 0 ];
1476 myCurFace = faceIndex;
1481 //=======================================================================
1482 //function : GetType
1483 //purpose : return VolumeType by nb of nodes in a volume
1484 //=======================================================================
1486 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetType(int nbNodes)
1488 switch ( nbNodes ) {
1489 case 4: return TETRA;
1490 case 5: return PYRAM;
1491 case 6: return PENTA;
1492 case 8: return HEXA;
1493 case 10: return QUAD_TETRA;
1494 case 13: return QUAD_PYRAM;
1495 case 15: return QUAD_PENTA;
1496 case 20: return QUAD_HEXA;
1497 default:return UNKNOWN;
1501 //=======================================================================
1502 //function : NbFaces
1503 //purpose : return nb of faces by volume type
1504 //=======================================================================
1506 int SMDS_VolumeTool::NbFaces( VolumeType type )
1510 case QUAD_TETRA: return 4;
1512 case QUAD_PYRAM: return 5;
1514 case QUAD_PENTA: return 5;
1516 case QUAD_HEXA : return 6;
1521 //=======================================================================
1522 //function : GetFaceNodesIndices
1523 //purpose : Return the array of face nodes indices
1524 // To comfort link iteration, the array
1525 // length == NbFaceNodes( faceIndex ) + 1 and
1526 // the last node index == the first one.
1527 //=======================================================================
1529 const int* SMDS_VolumeTool::GetFaceNodesIndices(VolumeType type,
1534 case TETRA: return Tetra_F[ faceIndex ];
1535 case PYRAM: return Pyramid_F[ faceIndex ];
1536 case PENTA: return external ? Penta_FE[ faceIndex ] : Penta_F[ faceIndex ];
1537 case HEXA: return external ? Hexa_FE[ faceIndex ] : Hexa_F[ faceIndex ];
1538 case QUAD_TETRA: return QuadTetra_F[ faceIndex ];
1539 case QUAD_PYRAM: return QuadPyram_F[ faceIndex ];
1540 case QUAD_PENTA: return external ? QuadPenta_FE[ faceIndex ] : QuadPenta_F[ faceIndex ];
1541 case QUAD_HEXA: return external ? QuadHexa_FE[ faceIndex ] : QuadHexa_F[ faceIndex ];
1547 //=======================================================================
1548 //function : NbFaceNodes
1549 //purpose : Return number of nodes in the array of face nodes
1550 //=======================================================================
1552 int SMDS_VolumeTool::NbFaceNodes(VolumeType type,
1556 case TETRA: return Tetra_nbN[ faceIndex ];
1557 case PYRAM: return Pyramid_nbN[ faceIndex ];
1558 case PENTA: return Penta_nbN[ faceIndex ];
1559 case HEXA: return Hexa_nbN[ faceIndex ];
1560 case QUAD_TETRA: return QuadTetra_nbN[ faceIndex ];
1561 case QUAD_PYRAM: return QuadPyram_nbN[ faceIndex ];
1562 case QUAD_PENTA: return QuadPenta_nbN[ faceIndex ];
1563 case QUAD_HEXA: return QuadHexa_nbN[ faceIndex ];