1 // File : SMDS_VolumeTool.cxx
2 // Created : Tue Jul 13 12:22:13 2004
3 // Author : Edward AGAPOV (eap)
4 // Copyright : Open CASCADE
7 #pragma warning(disable:4786)
10 #include "SMDS_VolumeTool.hxx"
12 #include "SMDS_MeshElement.hxx"
13 #include "SMDS_MeshNode.hxx"
20 // ======================================================
21 // Node indices in faces depending on volume orientation
22 // making most faces normals external
23 // ======================================================
31 // N0 +---|---+ N1 TETRAHEDRON
39 static int Tetra_F [4][4] = { // FORWARD == EXTERNAL
40 { 0, 1, 2, 0 }, // All faces have external normals
44 static int Tetra_R [4][4] = { // REVERSED
45 { 0, 1, 2, 0 }, // All faces but a bottom have external normals
49 static int Tetra_RE [4][4] = { // REVERSED -> FORWARD (EXTERNAL)
50 { 0, 2, 1, 0 }, // All faces have external normals
54 static int Tetra_nbN [] = { 3, 3, 3, 3 };
59 static int Pyramid_F [5][5] = { // FORWARD == EXTERNAL
60 { 0, 1, 2, 3, 0 }, // All faces have external normals
65 static int Pyramid_R [5][5] = { // REVERSED
66 { 0, 1, 2, 3, 0 }, // All faces but a bottom have external normals
71 static int Pyramid_RE [5][5] = { // REVERSED -> FORWARD (EXTERNAL)
72 { 0, 3, 2, 1, 0 }, // All faces but a bottom have external normals
77 static int Pyramid_nbN [] = { 4, 3, 3, 3, 3 };
88 // | / \ | PENTAHEDRON
94 static int Penta_F [5][5] = { // FORWARD
95 { 0, 1, 2, 0, 0 }, // Top face has an internal normal, other - external
96 { 3, 4, 5, 3, 3 }, // 0 is bottom, 1 is top face
100 static int Penta_R [5][5] = { // REVERSED
101 { 0, 1, 2, 0, 0 }, // Bottom face has an internal normal, other - external
102 { 3, 4, 5, 3, 3 }, // 0 is bottom, 1 is top face
106 static int Penta_FE [5][5] = { // FORWARD -> EXTERNAL
112 static int Penta_RE [5][5] = { // REVERSED -> EXTERNAL
118 static int Penta_nbN [] = { 3, 3, 4, 4, 4 };
125 // N4+----------+N7 |
126 // | | | | HEXAHEDRON
129 // | N1+------|---+N2
135 static int Hexa_F [6][5] = { // FORWARD
136 { 0, 1, 2, 3, 0 }, // opposite faces are neighbouring,
137 { 4, 5, 6, 7, 4 }, // odd face(1,3,5) normal is internal, even(0,2,4) - external
138 { 1, 0, 4, 5, 1 }, // same index nodes of opposite faces are linked
142 // static int Hexa_R [6][5] = { // REVERSED
143 // { 0, 3, 2, 1, 0 }, // opposite faces are neighbouring,
144 // { 4, 7, 6, 5, 4 }, // odd face(1,3,5) normal is external, even(0,2,4) - internal
145 // { 1, 5, 4, 0, 1 }, // same index nodes of opposite faces are linked
146 // { 2, 6, 7, 3, 2 },
147 // { 0, 4, 7, 3, 0 },
148 // { 1, 5, 6, 2, 1 }};
149 static int Hexa_FE [6][5] = { // FORWARD -> EXTERNAL
150 { 0, 1, 2, 3, 0 } , // opposite faces are neighbouring,
151 { 4, 7, 6, 5, 4 }, // all face normals are external,
152 { 0, 4, 5, 1, 0 }, // links in opposite faces: 0-0, 1-3, 2-2, 3-1
156 static int Hexa_RE [6][5] = { // REVERSED -> EXTERNAL
157 { 0, 3, 2, 1, 0 }, // opposite faces are neighbouring,
158 { 4, 5, 6, 7, 4 }, // all face normals are external,
159 { 0, 1, 5, 4, 0 }, // links in opposite faces: 0-0, 1-3, 2-2, 3-1
163 static int Hexa_nbN [] = { 4, 4, 4, 4, 4, 4 };
165 // ========================================================
166 // to perform some calculations without linkage to CASCADE
167 // ========================================================
172 XYZ() { x = 0; y = 0; z = 0; }
173 XYZ( double X, double Y, double Z ) { x = X; y = Y; z = Z; }
174 XYZ( const XYZ& other ) { x = other.x; y = other.y; z = other.z; }
175 XYZ( const SMDS_MeshNode* n ) { x = n->X(); y = n->Y(); z = n->Z(); }
176 XYZ operator-( const XYZ& other );
177 XYZ Crossed( const XYZ& other );
178 double Dot( const XYZ& other );
181 XYZ XYZ::operator-( const XYZ& Right ) {
182 return XYZ(x - Right.x, y - Right.y, z - Right.z);
184 XYZ XYZ::Crossed( const XYZ& Right ) {
185 return XYZ (y * Right.z - z * Right.y,
186 z * Right.x - x * Right.z,
187 x * Right.y - y * Right.x);
189 double XYZ::Dot( const XYZ& Other ) {
190 return(x * Other.x + y * Other.y + z * Other.z);
192 double XYZ::Magnitude() {
193 return sqrt (x * x + y * y + z * z);
196 //=======================================================================
197 //function : SMDS_VolumeTool
199 //=======================================================================
201 SMDS_VolumeTool::SMDS_VolumeTool ()
203 myVolForward( true ),
205 myVolumeNbNodes( 0 ),
206 myExternalFaces( false )
209 //=======================================================================
210 //function : SMDS_VolumeTool
212 //=======================================================================
214 SMDS_VolumeTool::SMDS_VolumeTool (const SMDS_MeshElement* theVolume)
215 : myExternalFaces( false )
220 //=======================================================================
221 //function : SMDS_VolumeTool
223 //=======================================================================
225 SMDS_VolumeTool::~SMDS_VolumeTool()
229 //=======================================================================
230 //function : SetVolume
231 //purpose : Set volume to iterate on
232 //=======================================================================
234 bool SMDS_VolumeTool::Set (const SMDS_MeshElement* theVolume)
241 if ( theVolume && theVolume->GetType() == SMDSAbs_Volume )
243 myVolumeNbNodes = theVolume->NbNodes();
244 switch ( myVolumeNbNodes ) {
250 myVolume = theVolume;
251 myNbFaces = theVolume->NbFaces();
255 SMDS_ElemIteratorPtr nodeIt = myVolume->nodesIterator();
256 while ( nodeIt->more() )
257 myVolumeNodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
259 // nb nodes in each face
260 if ( myVolumeNbNodes == 4 )
261 myFaceNbNodes = Tetra_nbN;
262 else if ( myVolumeNbNodes == 5 )
263 myFaceNbNodes = Pyramid_nbN;
264 else if ( myVolumeNbNodes == 6 )
265 myFaceNbNodes = Penta_nbN;
267 myFaceNbNodes = Hexa_nbN;
269 // define volume orientation
271 GetFaceNormal( 0, botNormal.x, botNormal.y, botNormal.z );
272 const SMDS_MeshNode* topNode = myVolumeNodes[ myVolumeNbNodes - 1 ];
273 const SMDS_MeshNode* botNode = myVolumeNodes[ 0 ];
274 XYZ upDir (topNode->X() - botNode->X(),
275 topNode->Y() - botNode->Y(),
276 topNode->Z() - botNode->Z() );
277 myVolForward = ( botNormal.Dot( upDir ) < 0 );
280 default: myVolume = 0;
283 return ( myVolume != 0 );
286 //=======================================================================
287 //function : GetInverseNodes
288 //purpose : Return nodes vector of an inverse volume
289 //=======================================================================
291 #define SWAP_NODES(nodes,i1,i2) \
293 const SMDS_MeshNode* tmp = nodes[ i1 ]; \
294 nodes[ i1 ] = nodes[ i2 ]; \
297 void SMDS_VolumeTool::Inverse ()
299 if ( !myVolume ) return;
301 myVolForward = !myVolForward;
304 // inverse top and bottom faces
305 switch ( myVolumeNbNodes ) {
307 SWAP_NODES( myVolumeNodes, 1, 2 );
310 SWAP_NODES( myVolumeNodes, 1, 3 );
313 SWAP_NODES( myVolumeNodes, 1, 2 );
314 SWAP_NODES( myVolumeNodes, 4, 5 );
317 SWAP_NODES( myVolumeNodes, 1, 3 );
318 SWAP_NODES( myVolumeNodes, 5, 7 );
324 //=======================================================================
326 //purpose : Return element volume
327 //=======================================================================
329 double SMDS_VolumeTool::GetSize() const
334 //=======================================================================
335 //function : GetBaryCenter
337 //=======================================================================
339 bool SMDS_VolumeTool::GetBaryCenter(double & X, double & Y, double & Z) const
345 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
346 X += myVolumeNodes[ i ]->X();
347 Y += myVolumeNodes[ i ]->Y();
348 Z += myVolumeNodes[ i ]->Z();
350 X /= myVolumeNbNodes;
351 Y /= myVolumeNbNodes;
352 Z /= myVolumeNbNodes;
357 //=======================================================================
358 //function : SetExternalNormal
359 //purpose : Node order will be so that faces normals are external
360 //=======================================================================
362 void SMDS_VolumeTool::SetExternalNormal ()
364 myExternalFaces = true;
368 //=======================================================================
369 //function : NbFaceNodes
370 //purpose : Return number of nodes in the array of face nodes
371 //=======================================================================
373 int SMDS_VolumeTool::NbFaceNodes( int faceIndex )
375 if ( !setFace( faceIndex ))
377 return myFaceNbNodes[ faceIndex ];
380 //=======================================================================
381 //function : GetFaceNodes
382 //purpose : Return pointer to the array of face nodes.
383 // To comfort link iteration, the array
384 // length == NbFaceNodes( faceIndex ) + 1 and
385 // the last node == the first one.
386 //=======================================================================
388 const SMDS_MeshNode** SMDS_VolumeTool::GetFaceNodes( int faceIndex )
390 if ( !setFace( faceIndex ))
395 //=======================================================================
396 //function : GetFaceNodesIndices
397 //purpose : Return pointer to the array of face nodes indices
398 // To comfort link iteration, the array
399 // length == NbFaceNodes( faceIndex ) + 1 and
400 // the last node index == the first one.
401 //=======================================================================
403 const int* SMDS_VolumeTool::GetFaceNodesIndices( int faceIndex )
405 if ( !setFace( faceIndex ))
407 return myFaceNodeIndices;
410 //=======================================================================
411 //function : GetFaceNodes
412 //purpose : Return a set of face nodes.
413 //=======================================================================
415 bool SMDS_VolumeTool::GetFaceNodes (int faceIndex,
416 set<const SMDS_MeshNode*>& theFaceNodes )
418 if ( !setFace( faceIndex ))
421 theFaceNodes.clear();
422 int iNode, nbNode = myFaceNbNodes[ faceIndex ];
423 for ( iNode = 0; iNode < nbNode; iNode++ )
424 theFaceNodes.insert( myFaceNodes[ iNode ]);
429 //=======================================================================
430 //function : IsFaceExternal
431 //purpose : Check normal orientation of a returned face
432 //=======================================================================
434 bool SMDS_VolumeTool::IsFaceExternal( int faceIndex )
436 if ( myExternalFaces || !myVolume )
439 switch ( myVolumeNbNodes ) {
442 // only the bottom of a reversed tetrahedron can be internal
443 return ( myVolForward || faceIndex != 0 );
445 // in a forward pentahedron, the top is internal, in a reversed one - bottom
446 return ( myVolForward ? faceIndex != 1 : faceIndex != 0 );
448 // in a forward hexahedron, even face normal is external, odd - internal
449 bool odd = faceIndex % 2;
450 return ( myVolForward ? !odd : odd );
457 //=======================================================================
458 //function : GetFaceNormal
459 //purpose : Return a normal to a face
460 //=======================================================================
462 bool SMDS_VolumeTool::GetFaceNormal (int faceIndex, double & X, double & Y, double & Z)
464 if ( !setFace( faceIndex ))
467 XYZ p1 ( myFaceNodes[0] );
468 XYZ p2 ( myFaceNodes[1] );
469 XYZ p3 ( myFaceNodes[2] );
470 XYZ aVec12( p2 - p1 );
471 XYZ aVec13( p3 - p1 );
472 XYZ cross = aVec12.Crossed( aVec13 );
474 double size = cross.Magnitude();
475 if ( size <= DBL_MIN )
486 //=======================================================================
487 //function : GetFaceArea
488 //purpose : Return face area
489 //=======================================================================
491 double SMDS_VolumeTool::GetFaceArea( int faceIndex )
493 if ( !setFace( faceIndex ))
496 XYZ p1 ( myFaceNodes[0] );
497 XYZ p2 ( myFaceNodes[1] );
498 XYZ p3 ( myFaceNodes[2] );
499 XYZ aVec12( p2 - p1 );
500 XYZ aVec13( p3 - p1 );
501 double area = aVec12.Crossed( aVec13 ).Magnitude() * 0.5;
503 if ( myFaceNbNodes[ faceIndex ] == 4 ) {
504 XYZ p4 ( myFaceNodes[3] );
505 XYZ aVec14( p4 - p1 );
506 area += aVec14.Crossed( aVec13 ).Magnitude() * 0.5;
511 //=======================================================================
512 //function : GetOppFaceIndex
513 //purpose : Return index of the opposite face if it exists, else -1.
514 //=======================================================================
516 int SMDS_VolumeTool::GetOppFaceIndex( int faceIndex ) const
519 if ( faceIndex >= 0 && faceIndex < NbFaces() ) {
520 switch ( myVolumeNbNodes ) {
522 if ( faceIndex == 0 || faceIndex == 1 )
526 ind = faceIndex + ( faceIndex % 2 ? -1 : 1 );
534 //=======================================================================
535 //function : IsLinked
536 //purpose : return true if theNode1 is linked with theNode2
537 //=======================================================================
539 bool SMDS_VolumeTool::IsLinked (const SMDS_MeshNode* theNode1,
540 const SMDS_MeshNode* theNode2) const
545 // find nodes indices
546 int i1 = -1, i2 = -1;
547 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
548 if ( myVolumeNodes[ i ] == theNode1 )
550 else if ( myVolumeNodes[ i ] == theNode2 )
553 return IsLinked( i1, i2 );
556 //=======================================================================
557 //function : IsLinked
558 //purpose : return true if the node with theNode1Index is linked
559 // with the node with theNode2Index
560 //=======================================================================
562 bool SMDS_VolumeTool::IsLinked (const int theNode1Index,
563 const int theNode2Index) const
565 int minInd = theNode1Index < theNode2Index ? theNode1Index : theNode2Index;
566 int maxInd = theNode1Index < theNode2Index ? theNode2Index : theNode1Index;
568 if ( minInd < 0 || maxInd > myVolumeNbNodes - 1 || maxInd == minInd )
571 switch ( myVolumeNbNodes ) {
577 switch ( maxInd - minInd ) {
584 switch ( maxInd - minInd ) {
585 case 1: return minInd != 2;
586 case 2: return minInd == 0 || minInd == 3;
592 switch ( maxInd - minInd ) {
593 case 1: return minInd != 3;
594 case 3: return minInd == 0 || minInd == 4;
604 //=======================================================================
605 //function : GetNodeIndex
606 //purpose : Return an index of theNode
607 //=======================================================================
609 int SMDS_VolumeTool::GetNodeIndex(const SMDS_MeshNode* theNode) const
612 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
613 if ( myVolumeNodes[ i ] == theNode )
621 //=======================================================================
622 //function : IsFreeFace
623 //purpose : check that only one volume is build on the face nodes
624 //=======================================================================
626 bool SMDS_VolumeTool::IsFreeFace( int faceIndex )
628 const int free = true;
629 if ( !setFace( faceIndex ))
632 const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
633 int nbFaceNodes = NbFaceNodes( faceIndex );
635 // evaluate nb of face nodes shared by other volume
636 int maxNbShared = -1;
637 typedef map< const SMDS_MeshElement*, int > TElemIntMap;
638 TElemIntMap volNbShared;
639 TElemIntMap::iterator vNbIt;
640 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
642 const SMDS_MeshNode* n = nodes[ iNode ];
643 SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator();
644 while ( eIt->more() ) {
645 const SMDS_MeshElement* elem = eIt->next();
646 if ( elem != myVolume && elem->GetType() == SMDSAbs_Volume ) {
648 vNbIt = volNbShared.find( elem );
649 if ( vNbIt == volNbShared.end() )
650 volNbShared.insert ( TElemIntMap::value_type( elem, nbShared ));
652 nbShared = ++(*vNbIt).second;
653 if ( nbShared > maxNbShared )
654 maxNbShared = nbShared;
658 if ( maxNbShared < 3 )
659 return free; // is free
661 // find volumes laying on the opposite side of the face
662 // and sharing all nodes
663 XYZ intNormal; // internal normal
664 GetFaceNormal( faceIndex, intNormal.x, intNormal.y, intNormal.z );
665 if ( IsFaceExternal( faceIndex ))
666 intNormal = XYZ( -intNormal.x, -intNormal.y, -intNormal.z );
667 XYZ p0 ( nodes[0] ), baryCenter;
668 for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ )
670 int nbShared = (*vNbIt).second;
671 if ( nbShared >= 3 ) {
672 SMDS_VolumeTool volume( (*vNbIt).first );
673 volume.GetBaryCenter( baryCenter.x, baryCenter.y, baryCenter.z );
674 XYZ intNormal2( baryCenter - p0 );
675 if ( intNormal.Dot( intNormal2 ) < 0 )
676 continue; // opposite side
678 // remove a volume from volNbShared map
679 volNbShared.erase( vNbIt );
681 // here volNbShared contains only volumes laying on the
682 // opposite side of the face
683 if ( volNbShared.empty() )
684 return free; // is free
686 // check if the whole area of a face is shared
687 bool isShared[] = { false, false, false, false }; // 4 triangle parts of a quadrangle
688 for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ )
690 SMDS_VolumeTool volume( (*vNbIt).first );
691 bool prevLinkShared = false;
692 int nbSharedLinks = 0;
693 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
695 bool linkShared = volume.IsLinked( nodes[ iNode ], nodes[ iNode + 1] );
698 if ( linkShared && prevLinkShared &&
699 volume.IsLinked( nodes[ iNode - 1 ], nodes[ iNode + 1] ))
700 isShared[ iNode ] = true;
701 prevLinkShared = linkShared;
703 if ( nbSharedLinks == nbFaceNodes )
704 return !free; // is not free
705 if ( nbFaceNodes == 4 ) {
706 // check traingle parts 1 & 3
707 if ( isShared[1] && isShared[3] )
708 return !free; // is not free
709 // check traingle parts 0 & 2;
710 // 0 part could not be checked in the loop; check it here
711 if ( isShared[2] && prevLinkShared &&
712 volume.IsLinked( nodes[ 0 ], nodes[ 1 ] ) &&
713 volume.IsLinked( nodes[ 1 ], nodes[ 3 ] ) )
714 return !free; // is not free
720 //=======================================================================
721 //function : GetFaceIndex
722 //purpose : Return index of a face formed by theFaceNodes
723 //=======================================================================
725 int SMDS_VolumeTool::GetFaceIndex( const set<const SMDS_MeshNode*>& theFaceNodes )
727 for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
728 const SMDS_MeshNode** nodes = GetFaceNodes( iFace );
729 int nbFaceNodes = NbFaceNodes( iFace );
730 set<const SMDS_MeshNode*> nodeSet;
731 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
732 nodeSet.insert( nodes[ iNode ] );
733 if ( theFaceNodes == nodeSet )
739 //=======================================================================
740 //function : GetFaceIndex
741 //purpose : Return index of a face formed by theFaceNodes
742 //=======================================================================
744 int SMDS_VolumeTool::GetFaceIndex( const set<int>& theFaceNodesIndices )
746 for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
747 const int* nodes = GetFaceNodesIndices( iFace );
748 int nbFaceNodes = NbFaceNodes( iFace );
750 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
751 nodeSet.insert( nodes[ iNode ] );
752 if ( theFaceNodesIndices == nodeSet )
758 //=======================================================================
761 //=======================================================================
763 bool SMDS_VolumeTool::setFace( int faceIndex )
768 if ( myCurFace == faceIndex )
771 if ( faceIndex < 0 || faceIndex >= NbFaces() )
774 // choose face node indices
775 switch ( myVolumeNbNodes ) {
777 if ( myExternalFaces )
778 myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_RE[ faceIndex ];
780 myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_R[ faceIndex ];
783 if ( myExternalFaces )
784 myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_RE[ faceIndex ];
786 myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_R[ faceIndex ];
789 if ( myExternalFaces )
790 myFaceNodeIndices = myVolForward ? Penta_FE[ faceIndex ] : Penta_RE[ faceIndex ];
792 myFaceNodeIndices = myVolForward ? Penta_F[ faceIndex ] : Penta_R[ faceIndex ];
795 if ( myExternalFaces )
796 myFaceNodeIndices = myVolForward ? Hexa_FE[ faceIndex ] : Hexa_RE[ faceIndex ];
798 myFaceNodeIndices = Hexa_F[ faceIndex ];
800 default: return false;
804 int iNode, nbNode = myFaceNbNodes[ faceIndex ];
805 for ( iNode = 0; iNode <= nbNode; iNode++ )
806 myFaceNodes[ iNode ] = myVolumeNodes[ myFaceNodeIndices[ iNode ]];
808 myCurFace = faceIndex;