1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : StdMeshers_QuadToTriaAdaptor.cxx
22 // Created : Wen May 07 16:37:07 2008
23 // Author : Sergey KUUL (skl)
25 #include "StdMeshers_QuadToTriaAdaptor.hxx"
27 #include "SMDS_IteratorOnIterators.hxx"
28 #include "SMDS_SetIterator.hxx"
29 #include "SMESHDS_GroupBase.hxx"
30 #include "SMESHDS_Mesh.hxx"
31 #include "SMESH_Algo.hxx"
32 #include "SMESH_Group.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_MeshAlgos.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_subMesh.hxx"
38 #include <IntAna_IntConicQuad.hxx>
39 #include <IntAna_Quadric.hxx>
40 #include <TColgp_Array1OfPnt.hxx>
41 #include <TColgp_Array1OfVec.hxx>
42 #include <TColgp_SequenceOfPnt.hxx>
43 #include <TopExp_Explorer.hxx>
45 #include <TopoDS_Iterator.hxx>
49 #include "utilities.h"
57 enum EQuadNature { NOT_QUAD, QUAD, DEGEN_QUAD, PYRAM_APEX = 4, TRIA_APEX = 0 };
59 // std-like iterator used to get coordinates of nodes of mesh element
60 typedef SMDS_StdIterator< SMESH_TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
62 //================================================================================
64 * \brief Return ID of pyramid base face, for debug
66 //================================================================================
68 int getFaceID(const SMDS_MeshElement* pyram)
71 if ( const SMDS_MeshElement* f = SMDS_Mesh::FindFace( pyram->GetNode(0),
81 //================================================================================
83 * \brief Return true if two nodes of triangles are equal
85 //================================================================================
87 bool EqualTriangles(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
90 ( F1->GetNode(1)==F2->GetNode(2) && F1->GetNode(2)==F2->GetNode(1) ) ||
91 ( F1->GetNode(1)==F2->GetNode(1) && F1->GetNode(2)==F2->GetNode(2) );
93 //================================================================================
95 * \brief Return true if two adjacent pyramids are too close one to another
96 * so that a tetrahedron to built between them would have too poor quality
98 //================================================================================
100 bool TooCloseAdjacent( const SMDS_MeshElement* PrmI,
101 const SMDS_MeshElement* PrmJ,
104 const SMDS_MeshNode* nApexI = PrmI->GetNode(4);
105 const SMDS_MeshNode* nApexJ = PrmJ->GetNode(4);
106 if ( nApexI == nApexJ ||
107 nApexI->getshapeId() != nApexJ->getshapeId() )
110 // Find two common base nodes and their indices within PrmI and PrmJ
111 const SMDS_MeshNode* baseNodes[2] = { 0,0 };
112 int baseNodesIndI[2], baseNodesIndJ[2];
113 for ( int i = 0; i < 4 ; ++i )
115 int j = PrmJ->GetNodeIndex( PrmI->GetNode(i));
118 int ind = baseNodes[0] ? 1:0;
119 if ( baseNodes[ ind ])
120 return false; // pyramids with a common base face
121 baseNodes [ ind ] = PrmI->GetNode(i);
122 baseNodesIndI[ ind ] = i;
123 baseNodesIndJ[ ind ] = j;
126 if ( !baseNodes[1] ) return false; // not adjacent
128 // Get normals of triangles sharing baseNodes
129 gp_XYZ apexI = SMESH_TNodeXYZ( nApexI );
130 gp_XYZ apexJ = SMESH_TNodeXYZ( nApexJ );
131 gp_XYZ base1 = SMESH_TNodeXYZ( baseNodes[0]);
132 gp_XYZ base2 = SMESH_TNodeXYZ( baseNodes[1]);
133 gp_Vec baseVec( base1, base2 );
134 gp_Vec baI( base1, apexI );
135 gp_Vec baJ( base1, apexJ );
136 gp_Vec nI = baseVec.Crossed( baI );
137 gp_Vec nJ = baseVec.Crossed( baJ );
139 // Check angle between normals
140 double angle = nI.Angle( nJ );
141 bool tooClose = ( angle < 15. * M_PI / 180. );
143 // Check if pyramids collide
144 if ( !tooClose && ( baI * baJ > 0 ) && ( nI * nJ > 0 ))
146 // find out if nI points outside of PrmI or inside
147 int dInd = baseNodesIndI[1] - baseNodesIndI[0];
148 bool isOutI = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
150 // find out sign of projection of baI to nJ
151 double proj = baI * nJ;
153 tooClose = ( isOutI ? proj > 0 : proj < 0 );
156 // Check if PrmI and PrmJ are in same domain
157 if ( tooClose && !hasShape )
159 // check order of baseNodes within pyramids, it must be opposite
161 dInd = baseNodesIndI[1] - baseNodesIndI[0];
162 bool isOutI = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
163 dInd = baseNodesIndJ[1] - baseNodesIndJ[0];
164 bool isOutJ = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
165 if ( isOutJ == isOutI )
166 return false; // other domain
168 // direct both normals outside pyramid
169 ( isOutI ? nJ : nI ).Reverse();
171 // check absence of a face separating domains between pyramids
172 TIDSortedElemSet emptySet, avoidSet;
174 while ( const SMDS_MeshElement* f =
175 SMESH_MeshAlgos::FindFaceInSet( baseNodes[0], baseNodes[1],
176 emptySet, avoidSet, &i1, &i2 ))
178 avoidSet.insert( f );
180 // face node other than baseNodes
181 int otherNodeInd = 0;
182 while ( otherNodeInd == i1 || otherNodeInd == i2 ) otherNodeInd++;
183 const SMDS_MeshNode* otherFaceNode = f->GetNode( otherNodeInd );
185 if ( otherFaceNode == nApexI || otherFaceNode == nApexJ )
186 continue; // f is a temporary triangle
188 // check if f is a base face of either of pyramids
189 if ( f->NbCornerNodes() == 4 &&
190 ( PrmI->GetNodeIndex( otherFaceNode ) >= 0 ||
191 PrmJ->GetNodeIndex( otherFaceNode ) >= 0 ))
192 continue; // f is a base quadrangle
194 // check projections of face direction (baOFN) to triangle normals (nI and nJ)
195 gp_Vec baOFN( base2, SMESH_TNodeXYZ( otherFaceNode ));
196 if ( nI * baOFN > 0 && nJ * baOFN > 0 &&
197 baI* baOFN > 0 && baJ* baOFN > 0 ) // issue 0023212
199 tooClose = false; // f is between pyramids
208 //================================================================================
210 * \brief Move medium nodes of merged quadratic pyramids
212 //================================================================================
214 void UpdateQuadraticPyramids(const set<const SMDS_MeshNode*>& commonApex,
215 SMESHDS_Mesh* meshDS)
217 typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TStdElemIterator;
218 TStdElemIterator itEnd;
220 // shift of node index to get medium nodes between the 4 base nodes and the apex
221 const int base2MediumShift = 9;
223 set<const SMDS_MeshNode*>::const_iterator nIt = commonApex.begin();
224 for ( ; nIt != commonApex.end(); ++nIt )
226 SMESH_TNodeXYZ apex( *nIt );
228 vector< const SMDS_MeshElement* > pyrams // pyramids sharing the apex node
229 ( TStdElemIterator( apex._node->GetInverseElementIterator( SMDSAbs_Volume )), itEnd );
231 // Select medium nodes to keep and medium nodes to remove
233 typedef map < const SMDS_MeshNode*, const SMDS_MeshNode*, TIDCompare > TN2NMap;
234 TN2NMap base2medium; // to keep
235 vector< const SMDS_MeshNode* > nodesToRemove;
237 for ( unsigned i = 0; i < pyrams.size(); ++i )
238 for ( int baseIndex = 0; baseIndex < PYRAM_APEX; ++baseIndex )
240 SMESH_TNodeXYZ base = pyrams[i]->GetNode( baseIndex );
241 const SMDS_MeshNode* medium = pyrams[i]->GetNode( baseIndex + base2MediumShift );
242 TN2NMap::iterator b2m = base2medium.insert( make_pair( base._node, medium )).first;
243 if ( b2m->second != medium )
245 nodesToRemove.push_back( medium );
249 // move the kept medium node
250 gp_XYZ newXYZ = 0.5 * ( apex + base );
251 meshDS->MoveNode( medium, newXYZ.X(), newXYZ.Y(), newXYZ.Z() );
255 // Within pyramids, replace nodes to remove by nodes to keep
257 for ( unsigned i = 0; i < pyrams.size(); ++i )
259 vector< const SMDS_MeshNode* > nodes( pyrams[i]->begin_nodes(),
260 pyrams[i]->end_nodes() );
261 for ( int baseIndex = 0; baseIndex < PYRAM_APEX; ++baseIndex )
263 const SMDS_MeshNode* base = pyrams[i]->GetNode( baseIndex );
264 nodes[ baseIndex + base2MediumShift ] = base2medium[ base ];
266 meshDS->ChangeElementNodes( pyrams[i], &nodes[0], nodes.size());
269 // Remove the replaced nodes
271 if ( !nodesToRemove.empty() )
273 SMESHDS_SubMesh * sm = meshDS->MeshElements( nodesToRemove[0]->getshapeId() );
274 for ( unsigned i = 0; i < nodesToRemove.size(); ++i )
275 meshDS->RemoveFreeNode( nodesToRemove[i], sm, /*fromGroups=*/false);
281 //================================================================================
283 * \brief Store an error about overlapping faces
285 //================================================================================
287 bool overlapError( SMESH_Mesh& mesh,
288 const SMDS_MeshElement* face1,
289 const SMDS_MeshElement* face2,
290 const TopoDS_Shape& shape = TopoDS_Shape())
292 if ( !face1 || !face2 ) return false;
295 msg << "face " << face1->GetID() << " overlaps face " << face2->GetID();
297 SMESH_subMesh * sm = 0;
298 if ( shape.IsNull() )
300 sm = mesh.GetSubMesh( mesh.GetShapeToMesh() );
302 else if ( shape.ShapeType() >= TopAbs_SOLID )
304 sm = mesh.GetSubMesh( shape );
308 TopoDS_Iterator it ( shape );
310 sm = mesh.GetSubMesh( it.Value() );
314 SMESH_ComputeErrorPtr& err = sm->GetComputeError();
315 if ( !err || err->IsOK() )
317 SMESH_BadInputElements* badElems =
318 new SMESH_BadInputElements( mesh.GetMeshDS(),COMPERR_BAD_INPUT_MESH, msg, sm->GetAlgo() );
319 badElems->add( face1 );
320 badElems->add( face2 );
321 err.reset( badElems );
325 return false; // == "algo fails"
328 //================================================================================
330 * \brief Check if a face is in a SOLID
332 //================================================================================
334 bool isInSolid( vector<const SMDS_MeshNode*> & faceNodes,
339 return true; // NOT_QUAD
340 for ( int i = 0; i < nbNodes; ++i )
342 int shapeID = faceNodes[i]->GetShapeID();
343 if ( shapeID == solidID )
346 faceNodes.resize( nbNodes );
347 std::vector<const SMDS_MeshElement*> vols;
348 SMDS_Mesh::GetElementsByNodes( faceNodes, vols, SMDSAbs_Volume );
349 bool inSolid = false;
350 for ( size_t i = 0; i < vols.size() && !inSolid; ++i )
352 int shapeID = vols[i]->GetShapeID();
353 inSolid = ( shapeID == solidID );
355 faceNodes.push_back( faceNodes[0] );
360 //================================================================================
362 * \brief Merge the two pyramids (i.e. fuse their apex) and others already merged with them
364 //================================================================================
366 void StdMeshers_QuadToTriaAdaptor::MergePiramids( const SMDS_MeshElement* PrmI,
367 const SMDS_MeshElement* PrmJ,
368 set<const SMDS_MeshNode*> & nodesToMove)
370 const SMDS_MeshNode* Nrem = PrmJ->GetNode(4); // node to remove
371 //int nbJ = Nrem->NbInverseElements( SMDSAbs_Volume );
372 SMESH_TNodeXYZ Pj( Nrem );
374 // an apex node to make common to all merged pyramids
375 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
376 if ( CommonNode == Nrem ) return; // already merged
377 //int nbI = CommonNode->NbInverseElements( SMDSAbs_Volume );
378 SMESH_TNodeXYZ Pi( CommonNode );
379 gp_XYZ Pnew = /*( nbI*Pi + nbJ*Pj ) / (nbI+nbJ);*/ 0.5 * ( Pi + Pj );
380 CommonNode->setXYZ( Pnew.X(), Pnew.Y(), Pnew.Z() );
382 nodesToMove.insert( CommonNode );
383 nodesToMove.erase ( Nrem );
385 //cout << "MergePiramids F" << getFaceID( PrmI ) << " - F" << getFaceID( PrmJ ) << endl;
387 typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TStdElemIterator;
388 TStdElemIterator itEnd;
390 typedef std::map< const SMDS_MeshNode*, const SMDS_MeshNode* > TNNMap;
391 TNNMap mediumReplaceMap;
393 // find and remove coincided faces of merged pyramids
394 vector< const SMDS_MeshElement* > inverseElems
395 // copy inverse elements to avoid iteration on changing container
396 ( TStdElemIterator( CommonNode->GetInverseElementIterator(SMDSAbs_Face)), itEnd);
397 for ( size_t i = 0; i < inverseElems.size(); ++i )
399 const SMDS_MeshElement* FI = inverseElems[i];
400 const SMDS_MeshElement* FJEqual = 0;
401 SMDS_ElemIteratorPtr triItJ = Nrem->GetInverseElementIterator(SMDSAbs_Face);
402 while ( !FJEqual && triItJ->more() )
404 const SMDS_MeshElement* FJ = triItJ->next();
405 if ( EqualTriangles( FJ, FI ))
410 if ( FJEqual->NbNodes() == 6 ) // find medium nodes to replace
412 mediumReplaceMap.insert( std::make_pair( FJEqual->GetNode(3), FI->GetNode(5) ));
413 mediumReplaceMap.insert( std::make_pair( FJEqual->GetNode(5), FI->GetNode(3) ));
415 removeTmpElement( FI );
416 removeTmpElement( FJEqual );
417 myRemovedTrias.insert( FI );
418 myRemovedTrias.insert( FJEqual );
422 // set the common apex node to pyramids and triangles merged with J
423 vector< const SMDS_MeshNode* > nodes;
424 inverseElems.assign( TStdElemIterator( Nrem->GetInverseElementIterator()), itEnd );
425 for ( size_t i = 0; i < inverseElems.size(); ++i )
427 const SMDS_MeshElement* elem = inverseElems[i];
428 nodes.assign( elem->begin_nodes(), elem->end_nodes() );
429 nodes[ elem->GetType() == SMDSAbs_Volume ? PYRAM_APEX : TRIA_APEX ] = CommonNode;
430 if ( !mediumReplaceMap.empty() )
431 for ( size_t iN = elem->NbCornerNodes(); iN < nodes.size(); ++iN )
433 TNNMap::iterator n2n = mediumReplaceMap.find( nodes[iN] );
434 if ( n2n != mediumReplaceMap.end() )
435 nodes[iN] = n2n->second;
437 GetMeshDS()->ChangeElementNodes( elem, &nodes[0], nodes.size());
439 ASSERT( Nrem->NbInverseElements() == 0 );
440 GetMeshDS()->RemoveFreeNode( Nrem,
441 GetMeshDS()->MeshElements( Nrem->getshapeId()),
442 /*fromGroups=*/false);
443 if ( !mediumReplaceMap.empty() )
444 for ( TNNMap::iterator n2n = mediumReplaceMap.begin(); n2n != mediumReplaceMap.end(); ++n2n )
446 const SMDS_MeshNode* remNode = n2n->first;
447 if ( !remNode->IsNull() && remNode->NbInverseElements() == 0 )
448 GetMeshDS()->RemoveFreeNode( remNode, 0, /*fromGroups=*/false);
453 //================================================================================
455 * \brief Merges adjacent pyramids
457 //================================================================================
459 void StdMeshers_QuadToTriaAdaptor::MergeAdjacent(const SMDS_MeshElement* PrmI,
460 set<const SMDS_MeshNode*>& nodesToMove,
461 const bool isRecursion)
463 TIDSortedElemSet adjacentPyrams;
464 bool mergedPyrams = false;
465 for ( int k = 0; k < 4; k++ ) // loop on 4 base nodes of PrmI
467 const SMDS_MeshNode* n = PrmI->GetNode(k);
468 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
469 while ( vIt->more() )
471 const SMDS_MeshElement* PrmJ = vIt->next();
472 if ( PrmJ == PrmI || PrmJ->NbCornerNodes() != 5 || !adjacentPyrams.insert( PrmJ ).second )
474 if ( TooCloseAdjacent( PrmI, PrmJ, GetMesh()->HasShapeToMesh() ))
476 MergePiramids( PrmI, PrmJ, nodesToMove );
478 // container of inverse elements can change
479 // vIt = n->GetInverseElementIterator( SMDSAbs_Volume ); -- iterator re-implemented
483 if ( mergedPyrams && !isRecursion )
485 TIDSortedElemSet::iterator prm;
486 for (prm = adjacentPyrams.begin(); prm != adjacentPyrams.end(); ++prm)
487 MergeAdjacent( *prm, nodesToMove, true );
492 //================================================================================
494 * \brief Decrease height of a given or adjacent pyramids if height difference
496 * \param [in] pyram - a pyramid to treat
497 * \param [inout] h2 - pyramid's square height
498 * \return bool - true if the height changes
500 //================================================================================
502 bool StdMeshers_QuadToTriaAdaptor::DecreaseHeightDifference( const SMDS_MeshElement* thePyram,
505 const double allowedFactor2 = 2. * 2.;
508 myNodes[0] = thePyram->GetNode( 3 );
509 for ( int i = 0; i < 4; ++i )
511 myNodes[1] = thePyram->GetNode( i );
512 SMDS_Mesh::GetElementsByNodes( myNodes, myAdjPyrams, SMDSAbs_Volume );
513 myNodes[0] = myNodes[1];
515 for ( const SMDS_MeshElement* pyramAdj : myAdjPyrams )
517 if ( pyramAdj == thePyram )
519 if ( !myPyramHeight2.IsBound( pyramAdj ))
521 double h2Adj = Abs( myPyramHeight2( pyramAdj ));
522 double h2 = Abs( theH2 );
525 if ( h2 * allowedFactor2 < h2Adj )
527 // bind negative value to allow finding pyramids whose height must change
528 myPyramHeight2.Bind( pyramAdj, - h2 * allowedFactor2 );
534 if ( h2Adj * allowedFactor2 < h2 )
536 // bind negative value to allow finding pyramids whose height must change
537 myPyramHeight2.Bind( thePyram, - h2Adj * allowedFactor2 );
547 //================================================================================
551 //================================================================================
553 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor():
558 //================================================================================
562 //================================================================================
564 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
566 // temporary faces are deleted by ~SMESH_ProxyMesh()
567 if ( myElemSearcher ) delete myElemSearcher;
571 //=======================================================================
572 //function : FindBestPoint
573 //purpose : Return a point P laying on the line (PC,V) so that triangle
574 // (P, P1, P2) to be equilateral as much as possible
575 // V - normal to (P1,P2,PC)
576 //=======================================================================
578 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
579 const gp_Pnt& PC, const gp_Vec& V,
584 const double a2 = P1.SquareDistance(P2);
585 const double b2 = P1.SquareDistance(PC);
586 const double c2 = P2.SquareDistance(PC);
587 if ( a2 < ( b2 + Sqrt( 4 * b2 * c2 ) + c2 ) / 4 ) // ( a < (b+c)/2 )
590 // find shift along V in order a to became equal to (b+c)/2
591 const double Vsize = V.Magnitude();
592 if ( fabs( Vsize ) > std::numeric_limits<double>::min() )
594 shift = sqrt( a2 + (b2-c2)*(b2-c2)/16/a2 - (b2+c2)/2 );
595 Pbest.ChangeCoord() += shift * V.XYZ() / Vsize;
601 //=======================================================================
602 //function : HasIntersection3
603 //purpose : Find intersection point between a triangle (P1,P2,P3)
604 // and a segment [PC,P]
605 //=======================================================================
607 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
608 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
610 const double EPSILON = 1e-6;
611 double segLen = P.Distance( PC );
613 gp_XYZ orig = PC.XYZ();
614 gp_XYZ dir = ( P.XYZ() - PC.XYZ() ) / segLen;
615 gp_XYZ vert0 = P1.XYZ();
616 gp_XYZ vert1 = P2.XYZ();
617 gp_XYZ vert2 = P3.XYZ();
619 gp_XYZ edge1 = vert1 - vert0;
620 gp_XYZ edge2 = vert2 - vert0;
622 /* begin calculating determinant - also used to calculate U parameter */
623 gp_XYZ pvec = dir ^ edge2;
625 /* if determinant is near zero, ray lies in plane of triangle */
626 double det = edge1 * pvec;
628 const double ANGL_EPSILON = 1e-12;
629 if ( det > -ANGL_EPSILON && det < ANGL_EPSILON )
632 /* calculate distance from vert0 to ray origin */
633 gp_XYZ tvec = orig - vert0;
635 /* calculate U parameter and test bounds */
636 double u = ( tvec * pvec ) / det;
637 //if (u < 0.0 || u > 1.0)
638 if (u < -EPSILON || u > 1.0 + EPSILON)
641 /* prepare to test V parameter */
642 gp_XYZ qvec = tvec ^ edge1;
644 /* calculate V parameter and test bounds */
645 double v = (dir * qvec) / det;
646 //if ( v < 0.0 || u + v > 1.0 )
647 if ( v < -EPSILON || u + v > 1.0 + EPSILON)
650 /* calculate t, ray intersects triangle */
651 double t = (edge2 * qvec) / det;
653 Pint = orig + dir * t;
655 bool hasInt = ( t > 0. && t < segLen );
657 if ( hasInt && det < EPSILON ) // t is inaccurate, additionally check
659 gp_XYZ triNorm = edge1 ^ edge2;
660 gp_XYZ int0vec = Pint.XYZ() - vert0;
661 gp_XYZ in = triNorm ^ edge1; // dir inside triangle from edge1
662 double dot = int0vec * in;
663 if ( dot < 0 && dot / triNorm.Modulus() < -EPSILON )
665 in = edge2 ^ triNorm;
667 if ( dot < 0 && dot / triNorm.Modulus() < -EPSILON )
669 gp_XYZ int1vec = Pint.XYZ() - vert1;
670 in = triNorm ^ ( vert2 - vert1 );
672 if ( dot < 0 && dot / triNorm.Modulus() < -EPSILON )
678 //=======================================================================
679 //function : HasIntersection
680 //purpose : Auxiliary for CheckIntersection()
681 //=======================================================================
683 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
684 TColgp_SequenceOfPnt& aContour)
686 if ( aContour.Length() == 3 ) {
687 return HasIntersection3( P, PC, Pint, aContour(1), aContour(2), aContour(3) );
691 if( (aContour(1).SquareDistance(aContour(2)) > 1.e-12) &&
692 (aContour(1).SquareDistance(aContour(3)) > 1.e-12) &&
693 (aContour(2).SquareDistance(aContour(3)) > 1.e-12) ) {
694 check = HasIntersection3( P, PC, Pint, aContour(1), aContour(2), aContour(3) );
696 if(check) return true;
697 if( (aContour(1).SquareDistance(aContour(4)) > 1.e-12) &&
698 (aContour(1).SquareDistance(aContour(3)) > 1.e-12) &&
699 (aContour(4).SquareDistance(aContour(3)) > 1.e-12) ) {
700 check = HasIntersection3( P, PC, Pint, aContour(1), aContour(3), aContour(4) );
702 if(check) return true;
708 //================================================================================
710 * \brief Return allowed height of a pyramid
711 * \param Papex - optimal pyramid apex
712 * \param PC - gravity center of a quadrangle
713 * \param PN - four nodes of the quadrangle
714 * \param aMesh - mesh
715 * \param NotCheckedFace - the quadrangle face
716 * \param Shape - the shape being meshed
717 * \retval false if mesh invalidity detected
719 //================================================================================
721 bool StdMeshers_QuadToTriaAdaptor::LimitHeight (gp_Pnt& Papex,
723 const TColgp_Array1OfPnt& PN,
724 const vector<const SMDS_MeshNode*>& FNodes,
726 const SMDS_MeshElement* NotCheckedFace,
727 const bool UseApexRay,
728 const TopoDS_Shape& Shape)
730 if ( !myElemSearcher )
731 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *aMesh.GetMeshDS() );
732 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
734 // Find intersection of faces with (P,PC) segment elongated 3 times
736 double height = Papex.Distance( PC );
737 gp_Ax1 line( PC, gp_Vec( PC, Papex ));
739 vector< const SMDS_MeshElement* > suspectFaces;
740 TColgp_SequenceOfPnt aContour;
744 double idealHeight = height;
745 const SMDS_MeshElement* intFace = 0;
747 // find intersection closest to PC
748 Ptest = PC.XYZ() + line.Direction().XYZ() * height * 3;
750 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces );
751 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
753 const SMDS_MeshElement* face = suspectFaces[iF];
754 if ( face == NotCheckedFace ) continue;
757 for ( int i = 0, nb = face->NbCornerNodes(); i < nb; ++i )
758 aContour.Append( SMESH_TNodeXYZ( face->GetNode(i) ));
760 if ( HasIntersection( Ptest, PC, Pint, aContour ))
762 double dInt = PC.Distance( Pint ) / 3.;
770 if ( height < 1e-2 * idealHeight && intFace )
771 return overlapError( aMesh, NotCheckedFace, intFace, Shape );
774 // Find faces intersecting triangular facets of the pyramid (issue 23212)
776 gp_XYZ center = PC.XYZ() + line.Direction().XYZ() * height * 0.5;
777 double diameter = Max( PN(1).Distance(PN(3)), PN(2).Distance(PN(4)));
778 suspectFaces.clear();
779 searcher->GetElementsInSphere( center, diameter * 0.6, SMDSAbs_Face, suspectFaces);
781 const double upShift = 1.5;
782 Ptest = PC.XYZ() + line.Direction().XYZ() * height * upShift; // tmp apex
784 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
786 const SMDS_MeshElement* face = suspectFaces[iF];
787 if ( face == NotCheckedFace ) continue;
788 if ( face->GetNodeIndex( FNodes[0] ) >= 0 ||
789 face->GetNodeIndex( FNodes[1] ) >= 0 ||
790 face->GetNodeIndex( FNodes[2] ) >= 0 ||
791 face->GetNodeIndex( FNodes[3] ) >= 0 )
792 continue; // neighbor face of the quadrangle
794 // limit height using points of intersection of face links with pyramid facets
795 int nbN = face->NbCornerNodes();
796 gp_Pnt P1 = SMESH_TNodeXYZ( face->GetNode( nbN-1 )); // 1st link end
797 for ( int i = 0; i < nbN; ++i )
799 gp_Pnt P2 = SMESH_TNodeXYZ( face->GetNode(i) ); // 2nd link end
801 for ( int iN = 1; iN <= 4; ++iN ) // loop on pyramid facets
803 if ( HasIntersection3( P1, P2, Pint, PN(iN), PN(iN+1), Ptest ))
805 height = Min( height, gp_Vec( PC, Pint ) * line.Direction() );
806 //Ptest = PC.XYZ() + line.Direction().XYZ() * height * upShift; // new tmp apex
813 Papex = PC.XYZ() + line.Direction().XYZ() * height;
818 //================================================================================
820 * \brief Retrieve data of the given face
821 * \param PN - coordinates of face nodes
822 * \param VN - cross products of vectors (PC-PN(i)) ^ (PC-PN(i+1))
823 * \param FNodes - face nodes
824 * \param PC - gravity center of nodes
825 * \param VNorm - face normal (sum of VN)
826 * \param volumes - two volumes sharing the given face, the first is in VNorm direction
827 * \retval int - 0 if given face is not quad,
828 * 1 if given face is quad,
829 * 2 if given face is degenerate quad (two nodes are coincided)
831 //================================================================================
833 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
834 TColgp_Array1OfPnt& PN,
835 TColgp_Array1OfVec& VN,
836 vector<const SMDS_MeshNode*>& FNodes,
839 const SMDS_MeshElement** volumes)
841 if( face->NbCornerNodes() != 4 )
847 gp_XYZ xyzC(0., 0., 0.);
848 for ( i = 0; i < 4; ++i )
850 gp_XYZ p = SMESH_TNodeXYZ( FNodes[i] = face->GetNode(i) );
851 PN.SetValue( i+1, p );
859 for ( i = 1; i < 4; i++ )
863 if( PN(i).Distance(PN(j)) < 1.e-6 )
875 list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
876 const SMDS_MeshNode* DegNode = 0;
877 for(; itdg!=myDegNodes.end(); itdg++) {
878 const SMDS_MeshNode* N = (*itdg);
879 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
880 if(Pdeg.Distance(Ptmp)<1.e-6) {
886 DegNode = FNodes[i-1];
887 myDegNodes.push_back(DegNode);
890 FNodes[i-1] = DegNode;
893 PN.SetValue(i,PN.Value(i+1));
894 FNodes[i-1] = FNodes[i];
899 PN.SetValue(nbp+1,PN(1));
900 FNodes[nbp] = FNodes[0];
902 // find normal direction
903 gp_Vec V1(PC,PN(nbp));
905 VNorm = V1.Crossed(V2);
906 VN.SetValue(nbp,VNorm);
907 for(i=1; i<nbp; i++) {
908 V1 = gp_Vec(PC,PN(i));
909 V2 = gp_Vec(PC,PN(i+1));
910 gp_Vec Vtmp = V1.Crossed(V2);
915 // find volumes sharing the face
918 volumes[0] = volumes[1] = 0;
919 SMDS_ElemIteratorPtr vIt = FNodes[0]->GetInverseElementIterator( SMDSAbs_Volume );
920 while ( vIt->more() )
922 const SMDS_MeshElement* vol = vIt->next();
923 bool volSharesAllNodes = true;
924 for ( int i = 1; i < face->NbNodes() && volSharesAllNodes; ++i )
925 volSharesAllNodes = ( vol->GetNodeIndex( FNodes[i] ) >= 0 );
926 if ( volSharesAllNodes )
927 volumes[ volumes[0] ? 1 : 0 ] = vol;
928 // we could additionally check that vol has all FNodes in its one face using SMDS_VolumeTool
930 // define volume position relating to the face normal
934 SMDS_ElemIteratorPtr nodeIt = volumes[0]->nodesIterator();
936 volGC = accumulate( TXyzIterator(nodeIt), TXyzIterator(), volGC ) / volumes[0]->NbNodes();
938 if ( VNorm * gp_Vec( PC, volGC ) < 0 )
939 swap( volumes[0], volumes[1] );
943 return hasdeg ? DEGEN_QUAD : QUAD;
947 //=======================================================================
950 //=======================================================================
952 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh,
953 const TopoDS_Shape& aShape,
954 SMESH_ProxyMesh* aProxyMesh)
956 SMESH_ProxyMesh::setMesh( aMesh );
958 if ( aShape.ShapeType() != TopAbs_SOLID )
963 vector<const SMDS_MeshElement*> myPyramids;
965 const SMESHDS_SubMesh * aSubMeshDSFace;
966 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
967 SMESH_MesherHelper helper1(aMesh);
968 helper1.IsQuadraticSubMesh(aShape);
970 if ( myElemSearcher ) delete myElemSearcher;
971 vector< SMDS_ElemIteratorPtr > itVec;
974 itVec.push_back( aProxyMesh->GetFaces( aShape ));
978 for ( TopExp_Explorer exp(aShape,TopAbs_FACE); exp.More(); exp.Next() )
979 if (( aSubMeshDSFace = meshDS->MeshElements( exp.Current() )))
980 itVec.push_back( aSubMeshDSFace->GetElements() );
983 SMDS_IteratorOnIterators< const SMDS_MeshElement*, vector< SMDS_ElemIteratorPtr > > TIter;
984 SMDS_ElemIteratorPtr faceIt( new TIter( itVec ));
985 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS, faceIt );
987 TColgp_Array1OfPnt PN(1,5);
988 TColgp_Array1OfVec VN(1,4);
989 vector<const SMDS_MeshNode*> FNodes(5);
992 const int solidID = meshDS->ShapeToIndex( aShape );
994 for ( TopExp_Explorer exp(aShape,TopAbs_FACE); exp.More(); exp.Next() )
996 const TopoDS_Shape& aShapeFace = exp.Current();
998 aSubMeshDSFace = aProxyMesh->GetSubMesh( aShapeFace );
1000 aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
1001 if ( !aSubMeshDSFace )
1004 vector<const SMDS_MeshElement*> trias, quads;
1005 bool hasNewTrias = false;
1007 const bool toCheckFaceInSolid =
1008 aProxyMesh ? aProxyMesh->HasPrismsOnTwoSides( meshDS->MeshElements( aShapeFace )) : false;
1009 if ( toCheckFaceInSolid && !dynamic_cast< const SMESH_ProxyMesh::SubMesh* >( aSubMeshDSFace ))
1010 continue; // no room for pyramids as prisms are on both sides
1013 bool isRevGlob = false;
1014 SMESH_MesherHelper helper2( aMesh );
1015 PShapeIteratorPtr sIt = helper2.GetAncestors( aShapeFace, aMesh, aShape.ShapeType() );
1016 while ( const TopoDS_Shape * solid = sIt->next() )
1017 if ( !solid->IsSame( aShape ))
1019 isRevGlob = helper2.IsReversedSubMesh( TopoDS::Face( aShapeFace ));
1020 if ( toCheckFaceInSolid )
1021 helper2.IsQuadraticSubMesh( *solid );
1025 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
1026 while ( iteratorElem->more() ) // loop on elements on a geometrical face
1028 const SMDS_MeshElement* face = iteratorElem->next();
1030 // preparation step to get face info
1031 int stat = Preparation( face, PN, VN, FNodes, PC, VNorm );
1033 bool isRev = isRevGlob;
1034 SMESH_MesherHelper* helper = &helper1;
1035 if ( toCheckFaceInSolid && !isInSolid( FNodes, face->NbCornerNodes(), solidID ))
1045 trias.push_back( face );
1051 // add triangles to result map
1052 SMDS_MeshFace* NewFace;
1053 helper->SetElementsOnShape( false );
1055 NewFace = helper->AddFace( FNodes[0], FNodes[1], FNodes[2] );
1057 NewFace = helper->AddFace( FNodes[0], FNodes[2], FNodes[1] );
1058 storeTmpElement( NewFace );
1059 trias.push_back ( NewFace );
1060 quads.push_back( face );
1067 if(!isRev) VNorm.Reverse();
1068 //double xc = 0., yc = 0., zc = 0.;
1069 double h, hMin = Precision::Infinite();
1075 Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i).Reversed(), h);
1077 Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i), h);
1078 if ( 0 < h && h < hMin )
1084 //gp_Pnt PCbest(xc/4., yc/4., zc/4.);
1087 double height = PCbest.Distance(PC);
1088 if ( height < 1.e-6 ) {
1089 // create new PCbest using a bit shift along VNorm
1090 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
1093 // check possible intersection with other faces
1094 if ( !LimitHeight( PCbest, PC, PN, FNodes, aMesh, face, /*UseApexRay=*/true, aShape ))
1097 // create node at PCbest
1098 helper->SetElementsOnShape( true );
1099 SMDS_MeshNode* NewNode = helper->AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
1102 SMDS_MeshVolume* aPyram;
1104 aPyram = helper->AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
1106 aPyram = helper->AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
1107 myPyramids.push_back(aPyram);
1108 //cout << "F" << face->GetID() << " - V" << aPyram->GetID() << endl;
1110 myPyramHeight2.Bind( aPyram, PCbest.SquareDistance( PC ));
1112 // add triangles to result map
1113 helper->SetElementsOnShape( false );
1114 for ( i = 0; i < 4; i++ )
1116 trias.push_back ( helper->AddFace( NewNode, FNodes[i], FNodes[i+1] ));
1117 storeTmpElement( trias.back() );
1120 quads.push_back( face );
1126 } // switch ( stat )
1127 } // end loop on elements on a face submesh
1129 bool sourceSubMeshIsProxy = false;
1132 // move proxy sub-mesh from other proxy mesh to this
1133 sourceSubMeshIsProxy = takeProxySubMesh( aShapeFace, aProxyMesh );
1134 // move also tmp elements added in mesh
1135 takeTmpElemsInMesh( aProxyMesh );
1139 SMESH_ProxyMesh::SubMesh* prxSubMesh = getProxySubMesh( aShapeFace );
1140 prxSubMesh->ChangeElements( trias.begin(), trias.end() );
1142 // delete tmp quadrangles removed from aProxyMesh
1143 if ( sourceSubMeshIsProxy )
1145 for ( unsigned i = 0; i < quads.size(); ++i )
1146 removeTmpElement( quads[i] );
1148 delete myElemSearcher;
1150 SMESH_MeshAlgos::GetElementSearcher( *meshDS, aProxyMesh->GetFaces(aShape));
1154 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
1156 return Compute2ndPart(aMesh, myPyramids);
1159 //================================================================================
1161 * \brief Computes pyramids in mesh with no shape
1163 //================================================================================
1165 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
1167 SMESH_ProxyMesh::setMesh( aMesh );
1168 SMESH_ProxyMesh::_allowedTypes.push_back( SMDSEntity_Triangle );
1169 SMESH_ProxyMesh::_allowedTypes.push_back( SMDSEntity_Quad_Triangle );
1170 if ( aMesh.NbQuadrangles() < 1 )
1173 // find if there is a group of faces identified as skin faces, with normal going outside the volume
1174 std::string groupName = "skinFaces";
1175 SMESHDS_GroupBase* groupDS = 0;
1176 SMESH_Mesh::GroupIteratorPtr groupIt = aMesh.GetGroups();
1177 while ( groupIt->more() )
1180 SMESH_Group * group = groupIt->next();
1181 if ( !group ) continue;
1182 groupDS = group->GetGroupDS();
1183 if ( !groupDS || groupDS->IsEmpty() )
1188 if (groupDS->GetType() != SMDSAbs_Face)
1193 std::string grpName = group->GetName();
1194 if (grpName == groupName)
1202 const bool toFindVolumes = aMesh.NbVolumes() > 0;
1204 vector<const SMDS_MeshElement*> myPyramids;
1205 SMESH_MesherHelper helper(aMesh);
1206 helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
1208 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1209 SMESH_ProxyMesh::SubMesh* prxSubMesh = getProxySubMesh();
1211 if ( !myElemSearcher )
1212 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS );
1213 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>( myElemSearcher );
1214 SMESHUtils::Deleter<SMESH_ElementSearcher>
1215 volSearcher( SMESH_MeshAlgos::GetElementSearcher( *meshDS ));
1216 vector< const SMDS_MeshElement* > suspectFaces, foundVolumes;
1218 TColgp_Array1OfPnt PN(1,5);
1219 TColgp_Array1OfVec VN(1,4);
1220 vector<const SMDS_MeshNode*> FNodes(5);
1221 TColgp_SequenceOfPnt aContour;
1223 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator();
1226 const SMDS_MeshElement* face = fIt->next();
1227 if ( !face ) continue;
1228 // retrieve needed information about a face
1231 const SMDS_MeshElement* volumes[2];
1232 int what = Preparation( face, PN, VN, FNodes, PC, VNorm, volumes );
1233 if ( what == NOT_QUAD )
1235 if ( volumes[0] && volumes[1] )
1236 continue; // face is shared by two volumes - no room for a pyramid
1238 if ( what == DEGEN_QUAD )
1241 // add a triangle to the proxy mesh
1242 SMDS_MeshFace* NewFace;
1244 // check orientation
1245 double tmp = PN(1).Distance(PN(2)) + PN(2).Distance(PN(3));
1246 // far points in VNorm direction
1247 gp_Pnt Ptmp1 = PC.XYZ() + VNorm.XYZ() * tmp * 1.e6;
1248 gp_Pnt Ptmp2 = PC.XYZ() - VNorm.XYZ() * tmp * 1.e6;
1249 // check intersection for Ptmp1 and Ptmp2
1253 double dist1 = RealLast();
1254 double dist2 = RealLast();
1257 gp_Ax1 line( PC, VNorm );
1258 vector< const SMDS_MeshElement* > suspectFaces;
1259 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces);
1261 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF ) {
1262 const SMDS_MeshElement* F = suspectFaces[iF];
1263 if ( F == face ) continue;
1265 for ( int i = 0; i < 4; ++i )
1266 aContour.Append( SMESH_TNodeXYZ( F->GetNode(i) ));
1268 if ( !volumes[0] && HasIntersection( Ptmp1, PC, PPP, aContour )) {
1270 double tmp = PC.Distance(PPP);
1271 if ( tmp < dist1 ) {
1276 if ( !volumes[1] && HasIntersection( Ptmp2, PC, PPP, aContour )) {
1278 double tmp = PC.Distance(PPP);
1279 if ( tmp < dist2 ) {
1286 if( IsOK1 && !IsOK2 ) {
1287 // using existed direction
1289 else if( !IsOK1 && IsOK2 ) {
1290 // using opposite direction
1293 else { // IsOK1 && IsOK2
1294 double tmp1 = PC.Distance(Pres1);
1295 double tmp2 = PC.Distance(Pres2);
1297 // using existed direction
1300 // using opposite direction
1304 helper.SetElementsOnShape( false );
1306 NewFace = helper.AddFace( FNodes[0], FNodes[1], FNodes[2] );
1308 NewFace = helper.AddFace( FNodes[0], FNodes[2], FNodes[1] );
1309 storeTmpElement( NewFace );
1310 prxSubMesh->AddElement( NewFace );
1314 // -----------------------------------
1315 // Case of non-degenerated quadrangle
1316 // -----------------------------------
1318 // Find pyramid peak
1320 gp_XYZ PCbest = PC.XYZ();//(0., 0., 0.); // pyramid peak
1321 double h, hMin = Precision::Infinite();
1323 for ( ; i <= 4; i++ ) {
1324 gp_Pnt Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i), h);
1325 if ( 0 < h && h < hMin )
1327 PCbest = Pbest.XYZ();
1330 //PCbest += Pbest.XYZ();
1334 double height = PC.Distance(PCbest); // pyramid height to precise
1335 if ( height < 1.e-6 ) {
1336 // create new PCbest using a bit shift along VNorm
1337 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
1338 height = PC.Distance(PCbest);
1339 if ( height < std::numeric_limits<double>::min() )
1340 return false; // batterfly element
1343 // Restrict pyramid height by intersection with other faces
1345 gp_Vec tmpDir(PC,PCbest); tmpDir.Normalize();
1346 double tmp = PN(1).Distance(PN(3)) + PN(2).Distance(PN(4));
1347 // far points: in (PC, PCbest) direction and vice-versa
1348 gp_Pnt farPnt[2] = { PC.XYZ() + tmpDir.XYZ() * tmp * 1.e6,
1349 PC.XYZ() - tmpDir.XYZ() * tmp * 1.e6 };
1350 // check intersection for farPnt1 and farPnt2
1351 bool intersected[2] = { false, false };
1352 double dist2int [2] = { RealLast(), RealLast() };
1354 int intFaceInd [2] = { 0, 0 };
1356 if ( toFindVolumes && 0 ) // non-conformal mesh is not suitable for any mesher so far
1358 // there are volumes in the mesh, in a non-conformal mesh a neighbor
1359 // volume can be not found yet
1360 for ( int isRev = 0; isRev < 2; ++isRev )
1362 if ( volumes[isRev] ) continue;
1363 gp_Pnt testPnt = PC.XYZ() + tmpDir.XYZ() * height * ( isRev ? -0.1: 0.1 );
1364 foundVolumes.clear();
1365 if ( volSearcher->FindElementsByPoint( testPnt, SMDSAbs_Volume, foundVolumes ))
1366 volumes[isRev] = foundVolumes[0];
1368 if ( volumes[0] && volumes[1] )
1369 continue; // no room for a pyramid
1372 gp_Ax1 line( PC, tmpDir );
1373 suspectFaces.clear();
1374 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces);
1376 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
1378 const SMDS_MeshElement* F = suspectFaces[iF];
1379 if ( F == face ) continue;
1381 int nbN = F->NbCornerNodes();
1382 for ( i = 0; i < nbN; ++i )
1383 aContour.Append( SMESH_TNodeXYZ( F->GetNode(i) ));
1385 for ( int isRev = 0; isRev < 2; ++isRev )
1387 if( !volumes[isRev] && HasIntersection( farPnt[isRev], PC, intP, aContour ))
1389 double d = PC.Distance( intP );
1390 if ( d < dist2int[isRev] )
1392 intersected[isRev] = true;
1393 intPnt [isRev] = intP;
1394 dist2int [isRev] = d;
1395 intFaceInd [isRev] = iF;
1401 // if the face belong to the group of skinFaces, do not build a pyramid outside
1402 if ( groupDS && groupDS->Contains(face) )
1404 intersected[0] = false;
1406 else if ( intersected[0] && intersected[1] ) // check if one of pyramids is in a hole
1408 gp_Pnt P ( PC.XYZ() + tmpDir.XYZ() * 0.5 * dist2int[0] );
1409 if ( searcher->GetPointState( P ) == TopAbs_OUT )
1410 intersected[0] = false;
1413 P = ( PC.XYZ() - tmpDir.XYZ() * 0.5 * dist2int[1] );
1414 if ( searcher->GetPointState( P ) == TopAbs_OUT )
1415 intersected[1] = false;
1419 // Create one or two pyramids
1421 for ( int isRev = 0; isRev < 2; ++isRev )
1423 if ( !intersected[isRev] ) continue;
1424 double pyramidH = Min( height, dist2int[isRev]/3. );
1425 gp_Pnt Papex = PC.XYZ() + tmpDir.XYZ() * (isRev ? -pyramidH : pyramidH);
1426 if ( pyramidH < 1e-2 * height )
1427 return overlapError( aMesh, face, suspectFaces[ intFaceInd[isRev] ] );
1429 if ( !LimitHeight( Papex, PC, PN, FNodes, aMesh, face, /*UseApexRay=*/false ))
1432 // create node for Papex
1433 helper.SetElementsOnShape( true );
1434 SMDS_MeshNode* NewNode = helper.AddNode( Papex.X(), Papex.Y(), Papex.Z() );
1437 SMDS_MeshVolume* aPyram;
1439 aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
1441 aPyram = helper.AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
1442 myPyramids.push_back(aPyram);
1444 //myPyramHeight2.Bind( aPyram, Papex.SquareDistance( PC ));
1446 // add triangles to result map
1447 helper.SetElementsOnShape( false );
1448 for ( i = 0; i < 4; i++) {
1449 SMDS_MeshFace* NewFace;
1451 NewFace = helper.AddFace( NewNode, FNodes[i], FNodes[i+1] );
1453 NewFace = helper.AddFace( NewNode, FNodes[i+1], FNodes[i] );
1454 storeTmpElement( NewFace );
1455 prxSubMesh->AddElement( NewFace );
1458 } // end loop on all faces
1460 return Compute2ndPart(aMesh, myPyramids);
1463 //================================================================================
1465 * \brief Update created pyramids and faces to avoid their intersection
1467 //================================================================================
1469 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh,
1470 const vector<const SMDS_MeshElement*>& myPyramids)
1472 if ( myPyramids.empty() )
1475 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1477 //int myShapeID = myPyramids[0]->GetNode(4)->getshapeId();
1479 SMDS_ElemIteratorPtr
1480 pyramIt( new SMDS_ElementVectorIterator( myPyramids.begin(), myPyramids.end() ));
1481 if ( myElemSearcher ) delete myElemSearcher;
1482 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS, pyramIt );
1484 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>( myElemSearcher );
1486 set<const SMDS_MeshNode*> nodesToMove;
1488 // check adjacent pyramids
1490 // for ( i = 0; i < myPyramids.size(); ++i )
1492 // const SMDS_MeshElement* PrmI = myPyramids[i];
1493 // MergeAdjacent( PrmI, nodesToMove );
1496 // Fix adjacent pyramids whose heights differ too much
1499 bool modifHeight = true;
1500 typedef NCollection_DataMap< const SMDS_MeshElement*, double >::Iterator TPyramToH2Iter;
1501 while ( modifHeight )
1503 modifHeight = false;
1504 for ( TPyramToH2Iter pyramToH2( myPyramHeight2 ); pyramToH2.More(); pyramToH2.Next() )
1505 modifHeight |= DecreaseHeightDifference( pyramToH2.Key(), pyramToH2.Value() );
1507 for ( TPyramToH2Iter pyramToH2( myPyramHeight2 ); pyramToH2.More(); pyramToH2.Next() )
1509 if ( pyramToH2.Value() > 0 )
1510 continue; // not changed
1511 const double h = Sqrt( - pyramToH2.Value() );
1512 const SMDS_MeshElement* pyram = pyramToH2.Key();
1513 SMESH_NodeXYZ Papex = pyram->GetNode( PYRAM_APEX );
1515 for ( int i = 0; i < 4; ++i )
1516 PC += SMESH_NodeXYZ( pyram->GetNode( i ));
1518 gp_Vec V( PC, Papex );
1519 gp_Pnt newApex = gp_Pnt( PC ).Translated( h * V.Normalized() );
1520 meshDS->MoveNode( Papex.Node(), newApex.X(), newApex.Y(), newApex.Z() );
1523 // iterate on all new pyramids
1524 vector< const SMDS_MeshElement* > suspectPyrams;
1525 for ( i = 0; i < myPyramids.size(); ++i )
1527 const SMDS_MeshElement* PrmI = myPyramids[i];
1528 const SMDS_MeshNode* apexI = PrmI->GetNode( PYRAM_APEX );
1530 // compare PrmI with all the rest pyramids
1532 // collect adjacent pyramids and nodes coordinates of PrmI
1533 set<const SMDS_MeshElement*> checkedPyrams;
1535 for ( k = 0; k < 5; k++ )
1537 const SMDS_MeshNode* n = PrmI->GetNode(k);
1538 PsI[k] = SMESH_TNodeXYZ( n );
1539 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
1540 while ( vIt->more() )
1542 const SMDS_MeshElement* PrmJ = vIt->next();
1543 if ( SMESH_MeshAlgos::NbCommonNodes( PrmI, PrmJ ) > 1 )
1544 checkedPyrams.insert( PrmJ );
1548 // get pyramids to check
1549 gp_XYZ PC = ( PsI[0].XYZ() + PsI[1].XYZ() + PsI[2].XYZ() + PsI[3].XYZ() ) / 4.;
1550 gp_XYZ ray = PsI[4].XYZ() - PC;
1551 gp_XYZ center = PC + 0.5 * ray;
1552 double diameter = Max( PsI[0].Distance(PsI[2]), PsI[1].Distance(PsI[3]));
1553 suspectPyrams.clear();
1554 searcher->GetElementsInSphere( center, diameter * 0.6, SMDSAbs_Volume, suspectPyrams);
1556 // check intersection with distant pyramids
1557 for ( j = 0; j < suspectPyrams.size(); ++j )
1559 const SMDS_MeshElement* PrmJ = suspectPyrams[j];
1562 if ( apexI == PrmJ->GetNode( PYRAM_APEX ))
1563 continue; // pyramids PrmI and PrmJ already merged
1564 if ( !checkedPyrams.insert( PrmJ ).second )
1565 continue; // already checked
1568 for ( k = 0; k < 5; k++ )
1569 PsJ[k] = SMESH_TNodeXYZ( PrmJ->GetNode(k) );
1571 if ( ray * ( PsJ[4].XYZ() - PC ) < 0. )
1572 continue; // PrmJ is below PrmI
1574 for ( k = 0; k < 4; k++ ) // loop on 4 base nodes of PrmI
1578 for ( k = 0; k < 4 && !hasInt; k++ )
1580 gp_Vec Vtmp( PsI[k], PsI[ PYRAM_APEX ]);
1581 gp_Pnt Pshift = PsI[k].XYZ() + Vtmp.XYZ() * 0.01; // base node moved a bit to apex
1583 ( HasIntersection3( Pshift, PsI[4], Pint, PsJ[0], PsJ[1], PsJ[PYRAM_APEX]) ||
1584 HasIntersection3( Pshift, PsI[4], Pint, PsJ[1], PsJ[2], PsJ[PYRAM_APEX]) ||
1585 HasIntersection3( Pshift, PsI[4], Pint, PsJ[2], PsJ[3], PsJ[PYRAM_APEX]) ||
1586 HasIntersection3( Pshift, PsI[4], Pint, PsJ[3], PsJ[0], PsJ[PYRAM_APEX]) );
1588 for ( k = 0; k < 4 && !hasInt; k++ )
1590 gp_Vec Vtmp( PsJ[k], PsJ[ PYRAM_APEX ]);
1591 gp_Pnt Pshift = PsJ[k].XYZ() + Vtmp.XYZ() * 0.01;
1593 ( HasIntersection3( Pshift, PsJ[4], Pint, PsI[0], PsI[1], PsI[PYRAM_APEX]) ||
1594 HasIntersection3( Pshift, PsJ[4], Pint, PsI[1], PsI[2], PsI[PYRAM_APEX]) ||
1595 HasIntersection3( Pshift, PsJ[4], Pint, PsI[2], PsI[3], PsI[PYRAM_APEX]) ||
1596 HasIntersection3( Pshift, PsJ[4], Pint, PsI[3], PsI[0], PsI[PYRAM_APEX]) );
1601 // count common nodes of base faces of two pyramids
1603 for ( k = 0; k < 4; k++ )
1604 nbc += int ( PrmI->GetNodeIndex( PrmJ->GetNode(k) ) >= 0 );
1607 continue; // pyrams have a common base face
1611 // Merge the two pyramids and others already merged with them
1612 MergePiramids( PrmI, PrmJ, nodesToMove );
1616 // decrease height of pyramids
1617 gp_XYZ PCi(0,0,0), PCj(0,0,0);
1618 for ( k = 0; k < 4; k++ ) {
1619 PCi += PsI[k].XYZ();
1620 PCj += PsJ[k].XYZ();
1623 gp_Vec VN1(PCi,PsI[4]);
1624 gp_Vec VN2(PCj,PsJ[4]);
1625 gp_Vec VI1(PCi,Pint);
1626 gp_Vec VI2(PCj,Pint);
1627 double ang1 = fabs(VN1.Angle(VI1));
1628 double ang2 = fabs(VN2.Angle(VI2));
1629 double coef1 = 0.5 - (( ang1 < M_PI/3. ) ? cos(ang1)*0.25 : 0 );
1630 double coef2 = 0.5 - (( ang2 < M_PI/3. ) ? cos(ang2)*0.25 : 0 ); // cos(ang2) ?
1631 // double coef2 = 0.5;
1633 // coef2 -= cos(ang1)*0.25;
1637 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>( apexI );
1638 aNode1->setXYZ( PCi.X()+VN1.X(), PCi.Y()+VN1.Y(), PCi.Z()+VN1.Z() );
1639 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(PrmJ->GetNode( PYRAM_APEX ));
1640 aNode2->setXYZ( PCj.X()+VN2.X(), PCj.Y()+VN2.Y(), PCj.Z()+VN2.Z() );
1641 nodesToMove.insert( aNode1 );
1642 nodesToMove.insert( aNode2 );
1643 //cout << "Limit H F" << getFaceID( PrmI ) << " - F" << getFaceID( PrmJ ) << endl;
1645 // fix intersections that can appear after apex movement
1646 //MergeAdjacent( PrmI, nodesToMove );
1647 //MergeAdjacent( PrmJ, nodesToMove );
1649 apexI = PrmI->GetNode( PYRAM_APEX ); // apexI can be removed by merge
1652 } // loop on 4 base nodes of PrmI
1653 } // loop on suspectPyrams
1655 } // loop on all pyramids
1657 //smIdType nbNodes = aMesh.NbNodes();
1658 for ( i = 0; i < myPyramids.size(); ++i )
1660 const SMDS_MeshElement* PrmI = myPyramids[i];
1661 MergeAdjacent( PrmI, nodesToMove );
1664 if( !nodesToMove.empty() && !meshDS->IsEmbeddedMode() )
1666 set<const SMDS_MeshNode*>::iterator n = nodesToMove.begin();
1667 for ( ; n != nodesToMove.end(); ++n )
1668 meshDS->MoveNode( *n, (*n)->X(), (*n)->Y(), (*n)->Z() );
1671 // move medium nodes of merged quadratic pyramids
1672 if ( myPyramids[0]->IsQuadratic() )
1673 UpdateQuadraticPyramids( nodesToMove, GetMeshDS() );
1675 // erase removed triangles from the proxy mesh
1676 if ( !myRemovedTrias.empty() )
1678 for ( int i = 0; i <= meshDS->MaxShapeIndex(); ++i )
1679 if ( SMESH_ProxyMesh::SubMesh* sm = findProxySubMesh(i))
1681 vector<const SMDS_MeshElement *> faces;
1682 faces.reserve( sm->NbElements() );
1683 SMDS_ElemIteratorPtr fIt = sm->GetElements();
1684 while ( fIt->more() )
1686 const SMDS_MeshElement* tria = fIt->next();
1687 set<const SMDS_MeshElement*>::iterator rmTria = myRemovedTrias.find( tria );
1688 if ( rmTria != myRemovedTrias.end() )
1689 myRemovedTrias.erase( rmTria );
1691 faces.push_back( tria );
1693 sm->ChangeElements( faces.begin(), faces.end() );
1699 delete myElemSearcher;