1 // File : SMDS_VolumeTool.cxx
2 // Created : Tue Jul 13 12:22:13 2004
3 // Author : Edward AGAPOV (eap)
4 // Copyright : Open CASCADE
7 #include "SMDS_VolumeTool.hxx"
9 #include "SMDS_MeshElement.hxx"
10 #include "SMDS_MeshNode.hxx"
22 // N0 +---|---+ N2 TETRAHEDRON
30 static int Tetra_F [4][4] = { // FORWARD == REVERSED EXTERNAL
31 { 0, 1, 2, 0 }, // Bottom face has an internal normal, other - external
35 static int Tetra_R [4][4] = { // REVERSED == FORWARD EXTERNAL
36 { 0, 2, 1, 0 }, // All faces have external normals
40 static int Tetra_nbN [] = { 3, 3, 3, 3 };
51 // | / \ | PENTAHEDRON
57 static int Penta_F [5][5] = { // FORWARD
58 { 0, 1, 2, 0, 0 }, // Top face has an internal normal, other - external
59 { 3, 4, 5, 3, 3 }, // 0 is bottom, 1 is top face
63 static int Penta_R [5][5] = { // REVERSED
64 { 0, 2, 1, 0, 0 }, // Bottom face has an internal normal, other - external
65 { 3, 5, 4, 3, 3 }, // 0 is bottom, 1 is top face
69 static int Penta_FE [5][5] = { // EXTERNAL
75 static int Penta_RE [5][5] = { // REVERSED EXTERNAL
81 static int Penta_nbN [] = { 3, 3, 4, 4, 4 };
98 static int Hexa_F [6][5] = { // FORWARD
99 { 0, 1, 2, 3, 0 }, // opposite faces are neighbouring,
100 { 4, 5, 6, 7, 4 }, // even face normal is internal, odd - external
101 { 1, 0, 4, 5, 1 }, // same index nodes of opposite faces are linked
105 static int Hexa_R [6][5] = { // REVERSED
106 { 0, 3, 2, 1, 0 }, // opposite faces are neighbouring,
107 { 4, 7, 6, 5, 4 }, // even face normal is external, odd - internal
108 { 1, 5, 4, 0, 1 }, // same index nodes of opposite faces are linked
112 static int Hexa_FE [6][5] = { // EXTERNAL
113 { 0, 3, 2, 1, 0 }, // opposite faces are neighbouring,
114 { 4, 5, 6, 7, 4 }, // all face normals are external,
115 { 0, 1, 5, 4, 0 }, // links in opposite faces: 0-0, 1-3, 2-2, 3-1
119 static int Hexa_RE [6][5] = { // REVERSED EXTERNAL
120 { 0, 1, 2, 3, 0 }, // opposite faces are neighbouring,
121 { 4, 7, 6, 5, 4 }, // all face normals are external,
122 { 0, 1, 5, 4, 0 }, // links in opposite faces: 0-0, 1-3, 2-2, 3-1
126 static int Hexa_nbN [] = { 4, 4, 4, 4, 4, 4 };
128 // ========================================================
129 // to perform some calculations without linkage to CASCADE
130 // ========================================================
135 XYZ() { x = 0; y = 0; z = 0; }
136 XYZ( double X, double Y, double Z ) { x = X; y = Y; z = Z; }
137 XYZ( const XYZ& other ) { x = other.x; y = other.y; z = other.z; }
138 XYZ( const SMDS_MeshNode* n ) { x = n->X(); y = n->Y(); z = n->Z(); }
139 XYZ operator-( const XYZ& other );
140 XYZ Crossed( const XYZ& other );
141 double Dot( const XYZ& other );
144 XYZ XYZ::operator-( const XYZ& Right ) {
145 return XYZ(x - Right.x, y - Right.y, z - Right.z);
147 XYZ XYZ::Crossed( const XYZ& Right ) {
148 return XYZ (y * Right.z - z * Right.y,
149 z * Right.x - x * Right.z,
150 x * Right.y - y * Right.x);
152 double XYZ::Dot( const XYZ& Other ) {
153 return(x * Other.x + y * Other.y + z * Other.z);
155 double XYZ::Magnitude() {
156 return sqrt (x * x + y * y + z * z);
159 //=======================================================================
160 //function : SMDS_VolumeTool
162 //=======================================================================
164 SMDS_VolumeTool::SMDS_VolumeTool ()
166 myVolForward( true ),
168 myVolumeNbNodes( 0 ),
169 myForwardFaces( false ),
170 myExternalFaces( false )
173 //=======================================================================
174 //function : SMDS_VolumeTool
176 //=======================================================================
178 SMDS_VolumeTool::SMDS_VolumeTool (const SMDS_MeshElement* theVolume)
179 : myForwardFaces( false ),
180 myExternalFaces( false )
185 //=======================================================================
186 //function : SMDS_VolumeTool
188 //=======================================================================
190 SMDS_VolumeTool::~SMDS_VolumeTool()
194 //=======================================================================
195 //function : SetVolume
196 //purpose : Set volume to iterate on
197 //=======================================================================
199 bool SMDS_VolumeTool::Set (const SMDS_MeshElement* theVolume)
206 if ( theVolume && theVolume->GetType() == SMDSAbs_Volume )
208 myVolumeNbNodes = theVolume->NbNodes();
209 switch ( myVolumeNbNodes ) {
214 myVolume = theVolume;
215 myNbFaces = theVolume->NbFaces();
219 SMDS_ElemIteratorPtr nodeIt = myVolume->nodesIterator();
220 while ( nodeIt->more() )
221 myVolumeNodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
223 // nb nodes in each face
224 if ( myVolumeNbNodes == 4 )
225 myFaceNbNodes = Tetra_nbN;
226 else if ( myVolumeNbNodes == 6 )
227 myFaceNbNodes = Penta_nbN;
229 myFaceNbNodes = Hexa_nbN;
231 // define volume orientation
233 GetFaceNormal( 0, botNormal.x, botNormal.y, botNormal.z );
234 const SMDS_MeshNode* topNode = myVolumeNodes[ myVolumeNbNodes - 1 ];
235 const SMDS_MeshNode* botNode = myVolumeNodes[ 0 ];
236 XYZ upDir (topNode->X() - botNode->X(),
237 topNode->Y() - botNode->Y(),
238 topNode->Z() - botNode->Z() );
239 bool diffDir = ( botNormal.Dot( upDir ) < 0 );
240 myVolForward = ( myVolumeNbNodes == 6 ? diffDir : !diffDir );
243 default: myVolume = 0;
246 return ( myVolume != 0 );
249 //=======================================================================
250 //function : GetInverseNodes
251 //purpose : Return nodes vector of an inverse volume
252 //=======================================================================
254 #define SWAP_NODES(nodes,i1,i2) \
256 const SMDS_MeshNode* tmp = nodes[ i1 ]; \
257 nodes[ i1 ] = nodes[ i2 ]; \
260 void SMDS_VolumeTool::Inverse ()
262 if ( !myVolume ) return;
264 myVolForward = !myVolForward;
267 // inverse top and bottom faces
268 switch ( myVolumeNbNodes ) {
270 SWAP_NODES( myVolumeNodes, 1, 2 );
273 SWAP_NODES( myVolumeNodes, 1, 2 );
274 SWAP_NODES( myVolumeNodes, 4, 5 );
277 SWAP_NODES( myVolumeNodes, 1, 3 );
278 SWAP_NODES( myVolumeNodes, 5, 7 );
284 //=======================================================================
286 //purpose : Return element volume
287 //=======================================================================
289 double SMDS_VolumeTool::GetSize() const
294 //=======================================================================
295 //function : GetBaryCenter
297 //=======================================================================
299 bool SMDS_VolumeTool::GetBaryCenter(double & X, double & Y, double & Z) const
305 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
306 X += myVolumeNodes[ i ]->X();
307 Y += myVolumeNodes[ i ]->Y();
308 Z += myVolumeNodes[ i ]->Z();
310 X /= myVolumeNbNodes;
311 Y /= myVolumeNbNodes;
312 Z /= myVolumeNbNodes;
317 //=======================================================================
318 //function : SetForwardOrientation
319 //purpose : Node order will be as for forward orientation
320 //=======================================================================
322 void SMDS_VolumeTool::SetForwardOrientation ()
324 myForwardFaces = true;
327 //=======================================================================
328 //function : SetExternalNormal
329 //purpose : Node order will be so that faces normals are external
330 //=======================================================================
332 void SMDS_VolumeTool::SetExternalNormal ()
334 myExternalFaces = true;
337 //=======================================================================
338 //function : NbFaceNodes
339 //purpose : Return number of nodes in the array of face nodes
340 //=======================================================================
342 int SMDS_VolumeTool::NbFaceNodes( int faceIndex )
344 if ( !setFace( faceIndex ))
346 return myFaceNbNodes[ faceIndex ];
349 //=======================================================================
350 //function : GetFaceNodes
351 //purpose : Return pointer to the array of face nodes.
352 // To comfort link iteration, the array
353 // length == NbFaceNodes( faceIndex ) + 1 and
354 // the last node == the first one.
355 //=======================================================================
357 const SMDS_MeshNode** SMDS_VolumeTool::GetFaceNodes( int faceIndex )
359 if ( !setFace( faceIndex ))
364 //=======================================================================
365 //function : GetFaceNodesIndices
366 //purpose : Return pointer to the array of face nodes indices
367 // To comfort link iteration, the array
368 // length == NbFaceNodes( faceIndex ) + 1 and
369 // the last node index == the first one.
370 //=======================================================================
372 const int* SMDS_VolumeTool::GetFaceNodesIndices( int faceIndex )
374 if ( !setFace( faceIndex ))
376 return myFaceNodeIndices;
379 //=======================================================================
380 //function : GetFaceNodes
381 //purpose : Return a set of face nodes.
382 //=======================================================================
384 bool SMDS_VolumeTool::GetFaceNodes (int faceIndex,
385 std::set<const SMDS_MeshNode*>& theFaceNodes )
387 if ( !setFace( faceIndex ))
390 theFaceNodes.clear();
391 int iNode, nbNode = myFaceNbNodes[ faceIndex ];
392 for ( int iNode = 0; iNode < nbNode; iNode++ )
393 theFaceNodes.insert( myFaceNodes[ iNode ]);
398 //=======================================================================
399 //function : IsFaceExternal
400 //purpose : Check normal orientation of a returned face
401 //=======================================================================
403 bool SMDS_VolumeTool::IsFaceExternal( int faceIndex )
405 if ( myExternalFaces || !myVolume )
408 bool reversed = ( !myForwardFaces && !myVolForward );
409 switch ( myVolumeNbNodes ) {
411 // only the bottom of a forward tetrahedron can be internal
412 return ( reversed || faceIndex != 0 );
414 // in a forward pentahedron, the top is internal, in a reversed one - bottom
415 return ( reversed ? faceIndex != 0 : faceIndex != 1 );
417 // in a forward hexahedron, odd face normal is external, else vice versa
418 bool odd = faceIndex % 2;
419 return ( reversed ? !odd : odd );
426 //=======================================================================
427 //function : GetFaceNormal
428 //purpose : Return a normal to a face
429 //=======================================================================
431 bool SMDS_VolumeTool::GetFaceNormal (int faceIndex, double & X, double & Y, double & Z)
433 if ( !setFace( faceIndex ))
436 XYZ p1 ( myFaceNodes[0] );
437 XYZ p2 ( myFaceNodes[1] );
438 XYZ p3 ( myFaceNodes[2] );
439 XYZ aVec12( p2 - p1 );
440 XYZ aVec13( p3 - p1 );
441 XYZ cross = aVec12.Crossed( aVec13 );
443 double size = cross.Magnitude();
444 if ( size <= DBL_MIN )
455 //=======================================================================
456 //function : GetFaceArea
457 //purpose : Return face area
458 //=======================================================================
460 double SMDS_VolumeTool::GetFaceArea( int faceIndex )
462 if ( !setFace( faceIndex ))
465 XYZ p1 ( myFaceNodes[0] );
466 XYZ p2 ( myFaceNodes[1] );
467 XYZ p3 ( myFaceNodes[2] );
468 XYZ aVec12( p2 - p1 );
469 XYZ aVec13( p3 - p1 );
470 double area = aVec12.Crossed( aVec13 ).Magnitude() * 0.5;
472 if ( myFaceNbNodes[ faceIndex ] == 4 ) {
473 XYZ p4 ( myFaceNodes[3] );
474 XYZ aVec14( p4 - p1 );
475 area += aVec14.Crossed( aVec13 ).Magnitude() * 0.5;
480 //=======================================================================
481 //function : GetOppFaceIndex
482 //purpose : Return index of the opposite face if it exists, else -1.
483 //=======================================================================
485 int SMDS_VolumeTool::GetOppFaceIndex( int faceIndex ) const
488 if ( faceIndex >= 0 && faceIndex < NbFaces() ) {
489 switch ( myVolumeNbNodes ) {
491 if ( faceIndex == 0 || faceIndex == 1 )
495 ind = faceIndex + ( faceIndex % 2 ? -1 : 1 );
503 //=======================================================================
504 //function : IsLinked
505 //purpose : return true if theNode1 is linked with theNode2
506 //=======================================================================
508 bool SMDS_VolumeTool::IsLinked (const SMDS_MeshNode* theNode1,
509 const SMDS_MeshNode* theNode2) const
514 // find nodes indices
515 int i1 = -1, i2 = -1;
516 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
517 if ( myVolumeNodes[ i ] == theNode1 )
519 else if ( myVolumeNodes[ i ] == theNode2 )
522 return IsLinked( i1, i2 );
525 //=======================================================================
526 //function : IsLinked
527 //purpose : return true if the node with theNode1Index is linked
528 // with the node with theNode2Index
529 //=======================================================================
531 bool SMDS_VolumeTool::IsLinked (const int theNode1Index,
532 const int theNode2Index) const
534 int minInd = theNode1Index < theNode2Index ? theNode1Index : theNode2Index;
535 int maxInd = theNode1Index < theNode2Index ? theNode2Index : theNode1Index;
537 if ( minInd < 0 || maxInd > myVolumeNbNodes - 1 || maxInd == minInd )
540 switch ( myVolumeNbNodes ) {
544 switch ( maxInd - minInd ) {
545 case 1: return minInd != 2;
546 case 2: return minInd == 0 || minInd == 3;
552 switch ( maxInd - minInd ) {
553 case 1: return minInd != 3;
554 case 3: return minInd == 0 || minInd == 4;
564 //=======================================================================
565 //function : GetNodeIndex
566 //purpose : Return an index of theNode
567 //=======================================================================
569 int SMDS_VolumeTool::GetNodeIndex(const SMDS_MeshNode* theNode) const
572 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
573 if ( myVolumeNodes[ i ] == theNode )
581 //=======================================================================
582 //function : IsFreeFace
583 //purpose : check that only one volume is build on the face nodes
584 //=======================================================================
586 bool SMDS_VolumeTool::IsFreeFace( int faceIndex )
588 const int free = true;
589 if ( !setFace( faceIndex ))
592 const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
593 int nbFaceNodes = NbFaceNodes( faceIndex );
595 // evaluate nb of face nodes shared by other volume
596 int maxNbShared = -1;
597 typedef map< const SMDS_MeshElement*, int > TElemIntMap;
598 TElemIntMap volNbShared;
599 TElemIntMap::iterator vNbIt;
600 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
602 const SMDS_MeshNode* n = nodes[ iNode ];
603 SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator();
604 while ( eIt->more() ) {
605 const SMDS_MeshElement* elem = eIt->next();
606 if ( elem != myVolume && elem->GetType() == SMDSAbs_Volume ) {
608 vNbIt = volNbShared.find( elem );
609 if ( vNbIt == volNbShared.end() )
610 volNbShared.insert ( TElemIntMap::value_type( elem, nbShared ));
612 nbShared = ++(*vNbIt).second;
613 if ( nbShared > maxNbShared )
614 maxNbShared = nbShared;
618 if ( maxNbShared < 3 )
619 return free; // is free
621 // find volumes laying on the opposite side of the face
622 // and sharing all nodes
623 XYZ intNormal; // internal normal
624 GetFaceNormal( faceIndex, intNormal.x, intNormal.y, intNormal.z );
625 if ( IsFaceExternal( faceIndex ))
626 intNormal = XYZ( -intNormal.x, -intNormal.y, -intNormal.z );
627 XYZ p0 ( nodes[0] ), baryCenter;
628 for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ )
630 int nbShared = (*vNbIt).second;
631 if ( nbShared >= 3 ) {
632 SMDS_VolumeTool volume( (*vNbIt).first );
633 volume.GetBaryCenter( baryCenter.x, baryCenter.y, baryCenter.z );
634 XYZ intNormal2( baryCenter - p0 );
635 if ( intNormal.Dot( intNormal2 ) < 0 )
636 continue; // opposite side
638 // remove a volume from volNbShared map
639 volNbShared.erase( vNbIt );
641 // here volNbShared contains only volumes laying on the
642 // opposite side of the face
643 if ( volNbShared.empty() )
644 return free; // is free
646 // check if the whole area of a face is shared
647 bool isShared[] = { false, false, false, false }; // 4 triangle parts of a quadrangle
648 for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ )
650 SMDS_VolumeTool volume( (*vNbIt).first );
651 bool prevLinkShared = false;
652 int nbSharedLinks = 0;
653 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
655 bool linkShared = volume.IsLinked( nodes[ iNode ], nodes[ iNode + 1] );
658 if ( linkShared && prevLinkShared &&
659 volume.IsLinked( nodes[ iNode - 1 ], nodes[ iNode + 1] ))
660 isShared[ iNode ] = true;
661 prevLinkShared = linkShared;
663 if ( nbSharedLinks == nbFaceNodes )
664 return !free; // is not free
665 if ( nbFaceNodes == 4 ) {
666 // check traingle parts 1 & 3
667 if ( isShared[1] && isShared[3] )
668 return !free; // is not free
669 // check traingle parts 0 & 2;
670 // 0 part could not be checked in the loop; check it here
671 if ( isShared[2] && prevLinkShared &&
672 volume.IsLinked( nodes[ 0 ], nodes[ 1 ] ) &&
673 volume.IsLinked( nodes[ 1 ], nodes[ 3 ] ) )
674 return !free; // is not free
680 //=======================================================================
681 //function : GetFaceIndex
682 //purpose : Return index of a face formed by theFaceNodes
683 //=======================================================================
685 int SMDS_VolumeTool::GetFaceIndex( const set<const SMDS_MeshNode*>& theFaceNodes )
687 for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
688 const SMDS_MeshNode** nodes = GetFaceNodes( iFace );
689 int nbFaceNodes = NbFaceNodes( iFace );
690 set<const SMDS_MeshNode*> nodeSet;
691 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
692 nodeSet.insert( nodes[ iNode ] );
693 if ( theFaceNodes == nodeSet )
699 //=======================================================================
700 //function : GetFaceIndex
701 //purpose : Return index of a face formed by theFaceNodes
702 //=======================================================================
704 int SMDS_VolumeTool::GetFaceIndex( const set<int>& theFaceNodesIndices )
706 for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
707 const int* nodes = GetFaceNodesIndices( iFace );
708 int nbFaceNodes = NbFaceNodes( iFace );
710 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
711 nodeSet.insert( nodes[ iNode ] );
712 if ( theFaceNodesIndices == nodeSet )
718 //=======================================================================
721 //=======================================================================
723 bool SMDS_VolumeTool::setFace( int faceIndex )
728 if ( myCurFace == faceIndex )
731 if ( faceIndex < 0 || faceIndex >= NbFaces() )
734 // choose face node indices
735 switch ( myVolumeNbNodes ) {
737 if ( myExternalFaces )
738 myFaceNodeIndices = myVolForward ? Tetra_R[ faceIndex ] : Tetra_F[ faceIndex ];
739 else if ( myForwardFaces )
740 myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_R[ faceIndex ];
742 myFaceNodeIndices = Tetra_F[ faceIndex ];
745 if ( myExternalFaces )
746 myFaceNodeIndices = myVolForward ? Penta_FE[ faceIndex ] : Penta_RE[ faceIndex ];
747 else if ( myForwardFaces )
748 myFaceNodeIndices = myVolForward ? Penta_F[ faceIndex ] : Penta_R[ faceIndex ];
750 myFaceNodeIndices = Penta_F[ faceIndex ];
753 if ( myExternalFaces )
754 myFaceNodeIndices = myVolForward ? Hexa_FE[ faceIndex ] : Hexa_RE[ faceIndex ];
755 else if ( myForwardFaces )
756 myFaceNodeIndices = myVolForward ? Hexa_F[ faceIndex ] : Hexa_R[ faceIndex ];
758 myFaceNodeIndices = Hexa_F[ faceIndex ];
760 default: return false;
764 int iNode, nbNode = myFaceNbNodes[ faceIndex ];
765 for ( iNode = 0; iNode <= nbNode; iNode++ )
766 myFaceNodes[ iNode ] = myVolumeNodes[ myFaceNodeIndices[ iNode ]];
768 myCurFace = faceIndex;