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, 35, 40};
717 const static int vtab[][4] = {
734 // quadratic tetrahedron
763 // quadratic pentahedron
768 // quadratic hexahedron
776 int type = GetVolumeType();
778 int n2 = ind[type+1];
780 //cout<<"n1="<<n1<<" n2="<<n2<<endl;
781 for (int i = n1; i < n2; i++) {
782 //cout<<"V="<<V<<endl;
783 V -= getTetraVolume( myVolumeNodes[ vtab[i][0] ],
784 myVolumeNodes[ vtab[i][1] ],
785 myVolumeNodes[ vtab[i][2] ],
786 myVolumeNodes[ vtab[i][3] ]);
788 //cout<<"V="<<V<<endl;
793 //=======================================================================
794 //function : GetBaryCenter
796 //=======================================================================
798 bool SMDS_VolumeTool::GetBaryCenter(double & X, double & Y, double & Z) const
804 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
805 X += myVolumeNodes[ i ]->X();
806 Y += myVolumeNodes[ i ]->Y();
807 Z += myVolumeNodes[ i ]->Z();
809 X /= myVolumeNbNodes;
810 Y /= myVolumeNbNodes;
811 Z /= myVolumeNbNodes;
816 //=======================================================================
817 //function : SetExternalNormal
818 //purpose : Node order will be so that faces normals are external
819 //=======================================================================
821 void SMDS_VolumeTool::SetExternalNormal ()
823 myExternalFaces = true;
827 //=======================================================================
828 //function : NbFaceNodes
829 //purpose : Return number of nodes in the array of face nodes
830 //=======================================================================
832 int SMDS_VolumeTool::NbFaceNodes( int faceIndex )
834 if ( !setFace( faceIndex ))
836 return myFaceNbNodes;
839 //=======================================================================
840 //function : GetFaceNodes
841 //purpose : Return pointer to the array of face nodes.
842 // To comfort link iteration, the array
843 // length == NbFaceNodes( faceIndex ) + 1 and
844 // the last node == the first one.
845 //=======================================================================
847 const SMDS_MeshNode** SMDS_VolumeTool::GetFaceNodes( int faceIndex )
849 if ( !setFace( faceIndex ))
854 //=======================================================================
855 //function : GetFaceNodesIndices
856 //purpose : Return pointer to the array of face nodes indices
857 // To comfort link iteration, the array
858 // length == NbFaceNodes( faceIndex ) + 1 and
859 // the last node index == the first one.
860 //=======================================================================
862 const int* SMDS_VolumeTool::GetFaceNodesIndices( int faceIndex )
864 if (myVolume->IsPoly()) {
865 MESSAGE("Warning: attempt to obtain FaceNodesIndices of polyhedral volume");
868 if ( !setFace( faceIndex ))
870 return myFaceNodeIndices;
873 //=======================================================================
874 //function : GetFaceNodes
875 //purpose : Return a set of face nodes.
876 //=======================================================================
878 bool SMDS_VolumeTool::GetFaceNodes (int faceIndex,
879 set<const SMDS_MeshNode*>& theFaceNodes )
881 if ( !setFace( faceIndex ))
884 theFaceNodes.clear();
885 int iNode, nbNode = myFaceNbNodes;
886 for ( iNode = 0; iNode < nbNode; iNode++ )
887 theFaceNodes.insert( myFaceNodes[ iNode ]);
892 //=======================================================================
893 //function : IsFaceExternal
894 //purpose : Check normal orientation of a returned face
895 //=======================================================================
897 bool SMDS_VolumeTool::IsFaceExternal( int faceIndex )
899 if ( myExternalFaces || !myVolume )
902 if (myVolume->IsPoly()) {
903 XYZ aNormal, baryCenter, p0 (myPolyedre->GetFaceNode(faceIndex + 1, 1));
904 GetFaceNormal(faceIndex, aNormal.x, aNormal.y, aNormal.z);
905 GetBaryCenter(baryCenter.x, baryCenter.y, baryCenter.z);
906 XYZ insideVec (baryCenter - p0);
907 if (insideVec.Dot(aNormal) > 0)
912 switch ( myVolumeNbNodes ) {
917 // only the bottom of a reversed tetrahedron can be internal
918 return ( myVolForward || faceIndex != 0 );
921 // in a forward pentahedron, the top is internal, in a reversed one - bottom
922 return ( myVolForward ? faceIndex != 1 : faceIndex != 0 );
925 // in a forward hexahedron, even face normal is external, odd - internal
926 bool odd = faceIndex % 2;
927 return ( myVolForward ? !odd : odd );
934 //=======================================================================
935 //function : GetFaceNormal
936 //purpose : Return a normal to a face
937 //=======================================================================
939 bool SMDS_VolumeTool::GetFaceNormal (int faceIndex, double & X, double & Y, double & Z)
941 if ( !setFace( faceIndex ))
944 XYZ p1 ( myFaceNodes[0] );
945 XYZ p2 ( myFaceNodes[1] );
946 XYZ p3 ( myFaceNodes[2] );
947 XYZ aVec12( p2 - p1 );
948 XYZ aVec13( p3 - p1 );
949 XYZ cross = aVec12.Crossed( aVec13 );
951 if ( myFaceNbNodes == 4 ) {
952 XYZ p4 ( myFaceNodes[3] );
953 XYZ aVec14( p4 - p1 );
954 XYZ cross2 = aVec13.Crossed( aVec14 );
960 double size = cross.Magnitude();
961 if ( size <= DBL_MIN )
971 //=======================================================================
972 //function : GetFaceArea
973 //purpose : Return face area
974 //=======================================================================
976 double SMDS_VolumeTool::GetFaceArea( int faceIndex )
978 if (myVolume->IsPoly()) {
979 MESSAGE("Warning: attempt to obtain area of a face of polyhedral volume");
983 if ( !setFace( faceIndex ))
986 XYZ p1 ( myFaceNodes[0] );
987 XYZ p2 ( myFaceNodes[1] );
988 XYZ p3 ( myFaceNodes[2] );
989 XYZ aVec12( p2 - p1 );
990 XYZ aVec13( p3 - p1 );
991 double area = aVec12.Crossed( aVec13 ).Magnitude() * 0.5;
993 if ( myFaceNbNodes == 4 ) {
994 XYZ p4 ( myFaceNodes[3] );
995 XYZ aVec14( p4 - p1 );
996 area += aVec14.Crossed( aVec13 ).Magnitude() * 0.5;
1001 //=======================================================================
1002 //function : GetOppFaceIndex
1003 //purpose : Return index of the opposite face if it exists, else -1.
1004 //=======================================================================
1006 int SMDS_VolumeTool::GetOppFaceIndex( int faceIndex ) const
1009 if (myVolume->IsPoly()) {
1010 MESSAGE("Warning: attempt to obtain opposite face on polyhedral volume");
1014 if ( faceIndex >= 0 && faceIndex < NbFaces() ) {
1015 switch ( myVolumeNbNodes ) {
1017 if ( faceIndex == 0 || faceIndex == 1 )
1018 ind = 1 - faceIndex;
1021 ind = faceIndex + ( faceIndex % 2 ? -1 : 1 );
1029 //=======================================================================
1030 //function : IsLinked
1031 //purpose : return true if theNode1 is linked with theNode2
1032 //=======================================================================
1034 bool SMDS_VolumeTool::IsLinked (const SMDS_MeshNode* theNode1,
1035 const SMDS_MeshNode* theNode2) const
1040 if (myVolume->IsPoly()) {
1042 MESSAGE("Warning: bad volumic element");
1045 bool isLinked = false;
1047 for (iface = 1; iface <= myNbFaces && !isLinked; iface++) {
1048 int inode, nbFaceNodes = myPolyedre->NbFaceNodes(iface);
1050 for (inode = 1; inode <= nbFaceNodes && !isLinked; inode++) {
1051 const SMDS_MeshNode* curNode = myPolyedre->GetFaceNode(iface, inode);
1053 if (curNode == theNode1 || curNode == theNode2) {
1054 int inextnode = (inode == nbFaceNodes) ? 1 : inode + 1;
1055 const SMDS_MeshNode* nextNode = myPolyedre->GetFaceNode(iface, inextnode);
1057 if ((curNode == theNode1 && nextNode == theNode2) ||
1058 (curNode == theNode2 && nextNode == theNode1)) {
1067 // find nodes indices
1068 int i1 = -1, i2 = -1;
1069 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
1070 if ( myVolumeNodes[ i ] == theNode1 )
1072 else if ( myVolumeNodes[ i ] == theNode2 )
1075 return IsLinked( i1, i2 );
1078 //=======================================================================
1079 //function : IsLinked
1080 //purpose : return true if the node with theNode1Index is linked
1081 // with the node with theNode2Index
1082 //=======================================================================
1084 bool SMDS_VolumeTool::IsLinked (const int theNode1Index,
1085 const int theNode2Index) const
1087 if (myVolume->IsPoly()) {
1088 return IsLinked(myVolumeNodes[theNode1Index], myVolumeNodes[theNode2Index]);
1091 int minInd = theNode1Index < theNode2Index ? theNode1Index : theNode2Index;
1092 int maxInd = theNode1Index < theNode2Index ? theNode2Index : theNode1Index;
1094 if ( minInd < 0 || maxInd > myVolumeNbNodes - 1 || maxInd == minInd )
1097 switch ( myVolumeNbNodes ) {
1103 switch ( maxInd - minInd ) {
1105 case 3: return true;
1110 switch ( maxInd - minInd ) {
1111 case 1: return minInd != 2;
1112 case 2: return minInd == 0 || minInd == 3;
1113 case 3: return true;
1118 switch ( maxInd - minInd ) {
1119 case 1: return minInd != 3;
1120 case 3: return minInd == 0 || minInd == 4;
1121 case 4: return true;
1130 //=======================================================================
1131 //function : GetNodeIndex
1132 //purpose : Return an index of theNode
1133 //=======================================================================
1135 int SMDS_VolumeTool::GetNodeIndex(const SMDS_MeshNode* theNode) const
1138 for ( int i = 0; i < myVolumeNbNodes; i++ ) {
1139 if ( myVolumeNodes[ i ] == theNode )
1146 //=======================================================================
1147 //function : IsFreeFace
1148 //purpose : check that only one volume is build on the face nodes
1149 //=======================================================================
1151 bool SMDS_VolumeTool::IsFreeFace( int faceIndex )
1153 const int free = true;
1155 if (!setFace( faceIndex ))
1158 const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
1159 int nbFaceNodes = myFaceNbNodes;
1161 // evaluate nb of face nodes shared by other volume
1162 int maxNbShared = -1;
1163 typedef map< const SMDS_MeshElement*, int > TElemIntMap;
1164 TElemIntMap volNbShared;
1165 TElemIntMap::iterator vNbIt;
1166 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1168 const SMDS_MeshNode* n = nodes[ iNode ];
1169 SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator();
1170 while ( eIt->more() ) {
1171 const SMDS_MeshElement* elem = eIt->next();
1172 if ( elem != myVolume && elem->GetType() == SMDSAbs_Volume ) {
1174 vNbIt = volNbShared.find( elem );
1175 if ( vNbIt == volNbShared.end() )
1176 volNbShared.insert ( TElemIntMap::value_type( elem, nbShared ));
1178 nbShared = ++(*vNbIt).second;
1179 if ( nbShared > maxNbShared )
1180 maxNbShared = nbShared;
1184 if ( maxNbShared < 3 )
1185 return free; // is free
1187 // find volumes laying on the opposite side of the face
1188 // and sharing all nodes
1189 XYZ intNormal; // internal normal
1190 GetFaceNormal( faceIndex, intNormal.x, intNormal.y, intNormal.z );
1191 if ( IsFaceExternal( faceIndex ))
1192 intNormal = XYZ( -intNormal.x, -intNormal.y, -intNormal.z );
1193 XYZ p0 ( nodes[0] ), baryCenter;
1194 for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ )
1196 int nbShared = (*vNbIt).second;
1197 if ( nbShared >= 3 ) {
1198 SMDS_VolumeTool volume( (*vNbIt).first );
1199 volume.GetBaryCenter( baryCenter.x, baryCenter.y, baryCenter.z );
1200 XYZ intNormal2( baryCenter - p0 );
1201 if ( intNormal.Dot( intNormal2 ) < 0 )
1202 continue; // opposite side
1204 // remove a volume from volNbShared map
1205 volNbShared.erase( vNbIt );
1207 // here volNbShared contains only volumes laying on the
1208 // opposite side of the face
1209 if ( volNbShared.empty() )
1210 return free; // is free
1212 // check if the whole area of a face is shared
1213 bool isShared[] = { false, false, false, false }; // 4 triangle parts of a quadrangle
1214 for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ )
1216 SMDS_VolumeTool volume( (*vNbIt).first );
1217 bool prevLinkShared = false;
1218 int nbSharedLinks = 0;
1219 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1221 bool linkShared = volume.IsLinked( nodes[ iNode ], nodes[ iNode + 1] );
1224 if ( linkShared && prevLinkShared &&
1225 volume.IsLinked( nodes[ iNode - 1 ], nodes[ iNode + 1] ))
1226 isShared[ iNode ] = true;
1227 prevLinkShared = linkShared;
1229 if ( nbSharedLinks == nbFaceNodes )
1230 return !free; // is not free
1231 if ( nbFaceNodes == 4 ) {
1232 // check traingle parts 1 & 3
1233 if ( isShared[1] && isShared[3] )
1234 return !free; // is not free
1235 // check triangle parts 0 & 2;
1236 // 0 part could not be checked in the loop; check it here
1237 if ( isShared[2] && prevLinkShared &&
1238 volume.IsLinked( nodes[ 0 ], nodes[ 1 ] ) &&
1239 volume.IsLinked( nodes[ 1 ], nodes[ 3 ] ) )
1240 return !free; // is not free
1246 //=======================================================================
1247 //function : GetFaceIndex
1248 //purpose : Return index of a face formed by theFaceNodes
1249 //=======================================================================
1251 int SMDS_VolumeTool::GetFaceIndex( const set<const SMDS_MeshNode*>& theFaceNodes )
1253 for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
1254 const SMDS_MeshNode** nodes = GetFaceNodes( iFace );
1255 int nbFaceNodes = NbFaceNodes( iFace );
1256 set<const SMDS_MeshNode*> nodeSet;
1257 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1258 nodeSet.insert( nodes[ iNode ] );
1259 if ( theFaceNodes == nodeSet )
1265 //=======================================================================
1266 //function : GetFaceIndex
1267 //purpose : Return index of a face formed by theFaceNodes
1268 //=======================================================================
1270 /*int SMDS_VolumeTool::GetFaceIndex( const set<int>& theFaceNodesIndices )
1272 for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
1273 const int* nodes = GetFaceNodesIndices( iFace );
1274 int nbFaceNodes = NbFaceNodes( iFace );
1276 for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
1277 nodeSet.insert( nodes[ iNode ] );
1278 if ( theFaceNodesIndices == nodeSet )
1284 //=======================================================================
1285 //function : setFace
1287 //=======================================================================
1289 bool SMDS_VolumeTool::setFace( int faceIndex )
1294 if ( myCurFace == faceIndex )
1299 if ( faceIndex < 0 || faceIndex >= NbFaces() )
1302 if (myFaceNodes != NULL) {
1303 delete [] myFaceNodes;
1307 if (myVolume->IsPoly()) {
1309 MESSAGE("Warning: bad volumic element");
1313 // check orientation
1314 bool isGoodOri = true;
1315 if (myExternalFaces)
1316 isGoodOri = IsFaceExternal( faceIndex );
1320 myFaceNbNodes = myPolyedre->NbFaceNodes(faceIndex + 1);
1321 myFaceNodes = new const SMDS_MeshNode* [myFaceNbNodes + 1];
1323 for ( iNode = 0; iNode < myFaceNbNodes; iNode++ )
1324 myFaceNodes[ iNode ] = myPolyedre->GetFaceNode(faceIndex + 1, iNode + 1);
1326 for ( iNode = 0; iNode < myFaceNbNodes; iNode++ )
1327 myFaceNodes[ iNode ] = myPolyedre->GetFaceNode(faceIndex + 1, myFaceNbNodes - iNode);
1329 myFaceNodes[ myFaceNbNodes ] = myFaceNodes[ 0 ]; // last = first
1333 // choose face node indices
1334 switch ( myVolumeNbNodes ) {
1336 myFaceNbNodes = Tetra_nbN[ faceIndex ];
1337 if ( myExternalFaces )
1338 myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_RE[ faceIndex ];
1340 myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_R[ faceIndex ];
1343 myFaceNbNodes = Pyramid_nbN[ faceIndex ];
1344 if ( myExternalFaces )
1345 myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_RE[ faceIndex ];
1347 myFaceNodeIndices = myVolForward ? Pyramid_F[ faceIndex ] : Pyramid_R[ faceIndex ];
1350 myFaceNbNodes = Penta_nbN[ faceIndex ];
1351 if ( myExternalFaces )
1352 myFaceNodeIndices = myVolForward ? Penta_FE[ faceIndex ] : Penta_RE[ faceIndex ];
1354 myFaceNodeIndices = myVolForward ? Penta_F[ faceIndex ] : Penta_R[ faceIndex ];
1357 myFaceNbNodes = Hexa_nbN[ faceIndex ];
1358 if ( myExternalFaces )
1359 myFaceNodeIndices = myVolForward ? Hexa_FE[ faceIndex ] : Hexa_RE[ faceIndex ];
1361 myFaceNodeIndices = Hexa_F[ faceIndex ];
1364 myFaceNbNodes = QuadTetra_nbN[ faceIndex ];
1365 if ( myExternalFaces )
1366 myFaceNodeIndices = myVolForward ? QuadTetra_F[ faceIndex ] : QuadTetra_RE[ faceIndex ];
1368 myFaceNodeIndices = myVolForward ? QuadTetra_F[ faceIndex ] : QuadTetra_R[ faceIndex ];
1371 myFaceNbNodes = QuadPyram_nbN[ faceIndex ];
1372 if ( myExternalFaces )
1373 myFaceNodeIndices = myVolForward ? QuadPyram_F[ faceIndex ] : QuadPyram_RE[ faceIndex ];
1375 myFaceNodeIndices = myVolForward ? QuadPyram_F[ faceIndex ] : QuadPyram_R[ faceIndex ];
1378 myFaceNbNodes = QuadPenta_nbN[ faceIndex ];
1379 if ( myExternalFaces )
1380 myFaceNodeIndices = myVolForward ? QuadPenta_FE[ faceIndex ] : QuadPenta_RE[ faceIndex ];
1382 myFaceNodeIndices = myVolForward ? QuadPenta_F[ faceIndex ] : QuadPenta_R[ faceIndex ];
1385 myFaceNbNodes = QuadHexa_nbN[ faceIndex ];
1386 if ( myExternalFaces )
1387 myFaceNodeIndices = myVolForward ? QuadHexa_FE[ faceIndex ] : QuadHexa_RE[ faceIndex ];
1389 myFaceNodeIndices = QuadHexa_F[ faceIndex ];
1396 myFaceNodes = new const SMDS_MeshNode* [myFaceNbNodes + 1];
1397 for ( int iNode = 0; iNode < myFaceNbNodes; iNode++ )
1398 myFaceNodes[ iNode ] = myVolumeNodes[ myFaceNodeIndices[ iNode ]];
1399 myFaceNodes[ myFaceNbNodes ] = myFaceNodes[ 0 ];
1402 myCurFace = faceIndex;
1407 //=======================================================================
1408 //function : GetType
1409 //purpose : return VolumeType by nb of nodes in a volume
1410 //=======================================================================
1412 SMDS_VolumeTool::VolumeType SMDS_VolumeTool::GetType(int nbNodes)
1414 switch ( nbNodes ) {
1415 case 4: return TETRA;
1416 case 5: return PYRAM;
1417 case 6: return PENTA;
1418 case 8: return HEXA;
1419 case 10: return QUAD_TETRA;
1420 case 13: return QUAD_PYRAM;
1421 case 15: return QUAD_PENTA;
1422 case 20: return QUAD_HEXA;
1423 default:return UNKNOWN;
1427 //=======================================================================
1428 //function : NbFaces
1429 //purpose : return nb of faces by volume type
1430 //=======================================================================
1432 int SMDS_VolumeTool::NbFaces( VolumeType type )
1436 case QUAD_TETRA: return 4;
1438 case QUAD_PYRAM: return 5;
1440 case QUAD_PENTA: return 5;
1442 case QUAD_HEXA : return 6;
1447 //=======================================================================
1448 //function : GetFaceNodesIndices
1449 //purpose : Return the array of face nodes indices
1450 // To comfort link iteration, the array
1451 // length == NbFaceNodes( faceIndex ) + 1 and
1452 // the last node index == the first one.
1453 //=======================================================================
1455 const int* SMDS_VolumeTool::GetFaceNodesIndices(VolumeType type,
1460 case TETRA: return Tetra_F[ faceIndex ];
1461 case PYRAM: return Pyramid_F[ faceIndex ];
1462 case PENTA: return external ? Penta_FE[ faceIndex ] : Penta_F[ faceIndex ];
1463 case HEXA: return external ? Hexa_FE[ faceIndex ] : Hexa_F[ faceIndex ];
1464 case QUAD_TETRA: return QuadTetra_F[ faceIndex ];
1465 case QUAD_PYRAM: return QuadPyram_F[ faceIndex ];
1466 case QUAD_PENTA: return external ? QuadPenta_FE[ faceIndex ] : QuadPenta_F[ faceIndex ];
1467 case QUAD_HEXA: return external ? QuadHexa_FE[ faceIndex ] : QuadHexa_F[ faceIndex ];
1473 //=======================================================================
1474 //function : NbFaceNodes
1475 //purpose : Return number of nodes in the array of face nodes
1476 //=======================================================================
1478 int SMDS_VolumeTool::NbFaceNodes(VolumeType type,
1482 case TETRA: return Tetra_nbN[ faceIndex ];
1483 case PYRAM: return Pyramid_nbN[ faceIndex ];
1484 case PENTA: return Penta_nbN[ faceIndex ];
1485 case HEXA: return Hexa_nbN[ faceIndex ];
1486 case QUAD_TETRA: return QuadTetra_nbN[ faceIndex ];
1487 case QUAD_PYRAM: return QuadPyram_nbN[ faceIndex ];
1488 case QUAD_PENTA: return QuadPenta_nbN[ faceIndex ];
1489 case QUAD_HEXA: return QuadHexa_nbN[ faceIndex ];