1 // Copyright (C) 2007-2020 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;
64 //================================================================================
66 * \brief Return true if two nodes of triangles are equal
68 //================================================================================
70 bool EqualTriangles(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
73 ( F1->GetNode(1)==F2->GetNode(2) && F1->GetNode(2)==F2->GetNode(1) ) ||
74 ( F1->GetNode(1)==F2->GetNode(1) && F1->GetNode(2)==F2->GetNode(2) );
76 //================================================================================
78 * \brief Return true if two adjacent pyramids are too close one to another
79 * so that a tetrahedron to built between them would have too poor quality
81 //================================================================================
83 bool TooCloseAdjacent( const SMDS_MeshElement* PrmI,
84 const SMDS_MeshElement* PrmJ,
87 const SMDS_MeshNode* nApexI = PrmI->GetNode(4);
88 const SMDS_MeshNode* nApexJ = PrmJ->GetNode(4);
89 if ( nApexI == nApexJ ||
90 nApexI->getshapeId() != nApexJ->getshapeId() )
93 // Find two common base nodes and their indices within PrmI and PrmJ
94 const SMDS_MeshNode* baseNodes[2] = { 0,0 };
95 int baseNodesIndI[2], baseNodesIndJ[2];
96 for ( int i = 0; i < 4 ; ++i )
98 int j = PrmJ->GetNodeIndex( PrmI->GetNode(i));
101 int ind = baseNodes[0] ? 1:0;
102 if ( baseNodes[ ind ])
103 return false; // pyramids with a common base face
104 baseNodes [ ind ] = PrmI->GetNode(i);
105 baseNodesIndI[ ind ] = i;
106 baseNodesIndJ[ ind ] = j;
109 if ( !baseNodes[1] ) return false; // not adjacent
111 // Get normals of triangles sharing baseNodes
112 gp_XYZ apexI = SMESH_TNodeXYZ( nApexI );
113 gp_XYZ apexJ = SMESH_TNodeXYZ( nApexJ );
114 gp_XYZ base1 = SMESH_TNodeXYZ( baseNodes[0]);
115 gp_XYZ base2 = SMESH_TNodeXYZ( baseNodes[1]);
116 gp_Vec baseVec( base1, base2 );
117 gp_Vec baI( base1, apexI );
118 gp_Vec baJ( base1, apexJ );
119 gp_Vec nI = baseVec.Crossed( baI );
120 gp_Vec nJ = baseVec.Crossed( baJ );
122 // Check angle between normals
123 double angle = nI.Angle( nJ );
124 bool tooClose = ( angle < 15. * M_PI / 180. );
126 // Check if pyramids collide
127 if ( !tooClose && ( baI * baJ > 0 ) && ( nI * nJ > 0 ))
129 // find out if nI points outside of PrmI or inside
130 int dInd = baseNodesIndI[1] - baseNodesIndI[0];
131 bool isOutI = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
133 // find out sign of projection of baI to nJ
134 double proj = baI * nJ;
136 tooClose = ( isOutI ? proj > 0 : proj < 0 );
139 // Check if PrmI and PrmJ are in same domain
140 if ( tooClose && !hasShape )
142 // check order of baseNodes within pyramids, it must be opposite
144 dInd = baseNodesIndI[1] - baseNodesIndI[0];
145 bool isOutI = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
146 dInd = baseNodesIndJ[1] - baseNodesIndJ[0];
147 bool isOutJ = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
148 if ( isOutJ == isOutI )
149 return false; // other domain
151 // direct both normals outside pyramid
152 ( isOutI ? nJ : nI ).Reverse();
154 // check absence of a face separating domains between pyramids
155 TIDSortedElemSet emptySet, avoidSet;
157 while ( const SMDS_MeshElement* f =
158 SMESH_MeshAlgos::FindFaceInSet( baseNodes[0], baseNodes[1],
159 emptySet, avoidSet, &i1, &i2 ))
161 avoidSet.insert( f );
163 // face node other than baseNodes
164 int otherNodeInd = 0;
165 while ( otherNodeInd == i1 || otherNodeInd == i2 ) otherNodeInd++;
166 const SMDS_MeshNode* otherFaceNode = f->GetNode( otherNodeInd );
168 if ( otherFaceNode == nApexI || otherFaceNode == nApexJ )
169 continue; // f is a temporary triangle
171 // check if f is a base face of either of pyramids
172 if ( f->NbCornerNodes() == 4 &&
173 ( PrmI->GetNodeIndex( otherFaceNode ) >= 0 ||
174 PrmJ->GetNodeIndex( otherFaceNode ) >= 0 ))
175 continue; // f is a base quadrangle
177 // check projections of face direction (baOFN) to triangle normals (nI and nJ)
178 gp_Vec baOFN( base2, SMESH_TNodeXYZ( otherFaceNode ));
179 if ( nI * baOFN > 0 && nJ * baOFN > 0 &&
180 baI* baOFN > 0 && baJ* baOFN > 0 ) // issue 0023212
182 tooClose = false; // f is between pyramids
191 //================================================================================
193 * \brief Move medium nodes of merged quadratic pyramids
195 //================================================================================
197 void UpdateQuadraticPyramids(const set<const SMDS_MeshNode*>& commonApex,
198 SMESHDS_Mesh* meshDS)
200 typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TStdElemIterator;
201 TStdElemIterator itEnd;
203 // shift of node index to get medium nodes between the 4 base nodes and the apex
204 const int base2MediumShift = 9;
206 set<const SMDS_MeshNode*>::const_iterator nIt = commonApex.begin();
207 for ( ; nIt != commonApex.end(); ++nIt )
209 SMESH_TNodeXYZ apex( *nIt );
211 vector< const SMDS_MeshElement* > pyrams // pyramids sharing the apex node
212 ( TStdElemIterator( apex._node->GetInverseElementIterator( SMDSAbs_Volume )), itEnd );
214 // Select medium nodes to keep and medium nodes to remove
216 typedef map < const SMDS_MeshNode*, const SMDS_MeshNode*, TIDCompare > TN2NMap;
217 TN2NMap base2medium; // to keep
218 vector< const SMDS_MeshNode* > nodesToRemove;
220 for ( unsigned i = 0; i < pyrams.size(); ++i )
221 for ( int baseIndex = 0; baseIndex < PYRAM_APEX; ++baseIndex )
223 SMESH_TNodeXYZ base = pyrams[i]->GetNode( baseIndex );
224 const SMDS_MeshNode* medium = pyrams[i]->GetNode( baseIndex + base2MediumShift );
225 TN2NMap::iterator b2m = base2medium.insert( make_pair( base._node, medium )).first;
226 if ( b2m->second != medium )
228 nodesToRemove.push_back( medium );
232 // move the kept medium node
233 gp_XYZ newXYZ = 0.5 * ( apex + base );
234 meshDS->MoveNode( medium, newXYZ.X(), newXYZ.Y(), newXYZ.Z() );
238 // Within pyramids, replace nodes to remove by nodes to keep
240 for ( unsigned i = 0; i < pyrams.size(); ++i )
242 vector< const SMDS_MeshNode* > nodes( pyrams[i]->begin_nodes(),
243 pyrams[i]->end_nodes() );
244 for ( int baseIndex = 0; baseIndex < PYRAM_APEX; ++baseIndex )
246 const SMDS_MeshNode* base = pyrams[i]->GetNode( baseIndex );
247 nodes[ baseIndex + base2MediumShift ] = base2medium[ base ];
249 meshDS->ChangeElementNodes( pyrams[i], &nodes[0], nodes.size());
252 // Remove the replaced nodes
254 if ( !nodesToRemove.empty() )
256 SMESHDS_SubMesh * sm = meshDS->MeshElements( nodesToRemove[0]->getshapeId() );
257 for ( unsigned i = 0; i < nodesToRemove.size(); ++i )
258 meshDS->RemoveFreeNode( nodesToRemove[i], sm, /*fromGroups=*/false);
264 //================================================================================
266 * \brief Store an error about overlapping faces
268 //================================================================================
270 bool overlapError( SMESH_Mesh& mesh,
271 const SMDS_MeshElement* face1,
272 const SMDS_MeshElement* face2,
273 const TopoDS_Shape& shape = TopoDS_Shape())
275 if ( !face1 || !face2 ) return false;
278 msg << "face " << face1->GetID() << " overlaps face " << face2->GetID();
280 SMESH_subMesh * sm = 0;
281 if ( shape.IsNull() )
283 sm = mesh.GetSubMesh( mesh.GetShapeToMesh() );
285 else if ( shape.ShapeType() >= TopAbs_SOLID )
287 sm = mesh.GetSubMesh( shape );
291 TopoDS_Iterator it ( shape );
293 sm = mesh.GetSubMesh( it.Value() );
297 SMESH_ComputeErrorPtr& err = sm->GetComputeError();
298 if ( !err || err->IsOK() )
300 SMESH_BadInputElements* badElems =
301 new SMESH_BadInputElements( mesh.GetMeshDS(),COMPERR_BAD_INPUT_MESH, msg, sm->GetAlgo() );
302 badElems->add( face1 );
303 badElems->add( face2 );
304 err.reset( badElems );
308 return false; // == "algo fails"
311 //================================================================================
313 * \brief Check if a face is in a SOLID
315 //================================================================================
317 bool isInSolid( vector<const SMDS_MeshNode*> & faceNodes,
322 return true; // NOT_QUAD
323 for ( int i = 0; i < nbNodes; ++i )
325 int shapeID = faceNodes[i]->GetShapeID();
326 if ( shapeID == solidID )
329 faceNodes.resize( nbNodes );
330 std::vector<const SMDS_MeshElement*> vols;
331 SMDS_Mesh::GetElementsByNodes( faceNodes, vols, SMDSAbs_Volume );
332 bool inSolid = false;
333 for ( size_t i = 0; i < vols.size() && !inSolid; ++i )
335 int shapeID = vols[i]->GetShapeID();
336 inSolid = ( shapeID == solidID );
338 faceNodes.push_back( faceNodes[0] );
343 //================================================================================
345 * \brief Merge the two pyramids (i.e. fuse their apex) and others already merged with them
347 //================================================================================
349 void StdMeshers_QuadToTriaAdaptor::MergePiramids( const SMDS_MeshElement* PrmI,
350 const SMDS_MeshElement* PrmJ,
351 set<const SMDS_MeshNode*> & nodesToMove)
353 // cout << endl << "Merge " << PrmI->GetID() << " " << PrmJ->GetID() << " "
354 // << PrmI->GetNode(4)->GetID() << " " << PrmJ->GetNode(4)->GetID() << endl;
355 const SMDS_MeshNode* Nrem = PrmJ->GetNode(4); // node to remove
356 //int nbJ = Nrem->NbInverseElements( SMDSAbs_Volume );
357 SMESH_TNodeXYZ Pj( Nrem );
359 // an apex node to make common to all merged pyramids
360 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
361 if ( CommonNode == Nrem ) return; // already merged
362 //int nbI = CommonNode->NbInverseElements( SMDSAbs_Volume );
363 SMESH_TNodeXYZ Pi( CommonNode );
364 gp_XYZ Pnew = /*( nbI*Pi + nbJ*Pj ) / (nbI+nbJ);*/ 0.5 * ( Pi + Pj );
365 CommonNode->setXYZ( Pnew.X(), Pnew.Y(), Pnew.Z() );
367 nodesToMove.insert( CommonNode );
368 nodesToMove.erase ( Nrem );
370 typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TStdElemIterator;
371 TStdElemIterator itEnd;
373 typedef std::map< const SMDS_MeshNode*, const SMDS_MeshNode* > TNNMap;
374 TNNMap mediumReplaceMap;
376 // find and remove coincided faces of merged pyramids
377 vector< const SMDS_MeshElement* > inverseElems
378 // copy inverse elements to avoid iteration on changing container
379 ( TStdElemIterator( CommonNode->GetInverseElementIterator(SMDSAbs_Face)), itEnd);
380 for ( size_t i = 0; i < inverseElems.size(); ++i )
382 const SMDS_MeshElement* FI = inverseElems[i];
383 const SMDS_MeshElement* FJEqual = 0;
384 SMDS_ElemIteratorPtr triItJ = Nrem->GetInverseElementIterator(SMDSAbs_Face);
385 while ( !FJEqual && triItJ->more() )
387 const SMDS_MeshElement* FJ = triItJ->next();
388 if ( EqualTriangles( FJ, FI ))
393 if ( FJEqual->NbNodes() == 6 ) // find medium nodes to replace
395 mediumReplaceMap.insert( std::make_pair( FJEqual->GetNode(3), FI->GetNode(5) ));
396 mediumReplaceMap.insert( std::make_pair( FJEqual->GetNode(5), FI->GetNode(3) ));
398 removeTmpElement( FI );
399 removeTmpElement( FJEqual );
400 myRemovedTrias.insert( FI );
401 myRemovedTrias.insert( FJEqual );
405 // set the common apex node to pyramids and triangles merged with J
406 vector< const SMDS_MeshNode* > nodes;
407 inverseElems.assign( TStdElemIterator( Nrem->GetInverseElementIterator()), itEnd );
408 for ( size_t i = 0; i < inverseElems.size(); ++i )
410 const SMDS_MeshElement* elem = inverseElems[i];
411 nodes.assign( elem->begin_nodes(), elem->end_nodes() );
412 nodes[ elem->GetType() == SMDSAbs_Volume ? PYRAM_APEX : TRIA_APEX ] = CommonNode;
413 if ( !mediumReplaceMap.empty() )
414 for ( size_t iN = elem->NbCornerNodes(); iN < nodes.size(); ++iN )
416 TNNMap::iterator n2n = mediumReplaceMap.find( nodes[iN] );
417 if ( n2n != mediumReplaceMap.end() )
418 nodes[iN] = n2n->second;
420 GetMeshDS()->ChangeElementNodes( elem, &nodes[0], nodes.size());
422 ASSERT( Nrem->NbInverseElements() == 0 );
423 GetMeshDS()->RemoveFreeNode( Nrem,
424 GetMeshDS()->MeshElements( Nrem->getshapeId()),
425 /*fromGroups=*/false);
426 if ( !mediumReplaceMap.empty() )
427 for ( TNNMap::iterator n2n = mediumReplaceMap.begin(); n2n != mediumReplaceMap.end(); ++n2n )
429 const SMDS_MeshNode* remNode = n2n->first;
430 if ( !remNode->IsNull() && remNode->NbInverseElements() == 0 )
431 GetMeshDS()->RemoveFreeNode( remNode, 0, /*fromGroups=*/false);
436 //================================================================================
438 * \brief Merges adjacent pyramids
440 //================================================================================
442 void StdMeshers_QuadToTriaAdaptor::MergeAdjacent(const SMDS_MeshElement* PrmI,
443 set<const SMDS_MeshNode*>& nodesToMove,
444 const bool isRecursion)
446 TIDSortedElemSet adjacentPyrams;
447 bool mergedPyrams = false;
448 for ( int k = 0; k < 4; k++ ) // loop on 4 base nodes of PrmI
450 const SMDS_MeshNode* n = PrmI->GetNode(k);
451 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
452 while ( vIt->more() )
454 const SMDS_MeshElement* PrmJ = vIt->next();
455 if ( PrmJ == PrmI || PrmJ->NbCornerNodes() != 5 || !adjacentPyrams.insert( PrmJ ).second )
457 if ( TooCloseAdjacent( PrmI, PrmJ, GetMesh()->HasShapeToMesh() ))
459 MergePiramids( PrmI, PrmJ, nodesToMove );
461 // container of inverse elements can change
462 // vIt = n->GetInverseElementIterator( SMDSAbs_Volume ); -- iterator re-implemented
466 if ( mergedPyrams && !isRecursion )
468 TIDSortedElemSet::iterator prm;
469 for (prm = adjacentPyrams.begin(); prm != adjacentPyrams.end(); ++prm)
470 MergeAdjacent( *prm, nodesToMove, true );
475 //================================================================================
479 //================================================================================
481 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor():
486 //================================================================================
490 //================================================================================
492 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
494 // temporary faces are deleted by ~SMESH_ProxyMesh()
495 if ( myElemSearcher ) delete myElemSearcher;
499 //=======================================================================
500 //function : FindBestPoint
501 //purpose : Return a point P laying on the line (PC,V) so that triangle
502 // (P, P1, P2) to be equilateral as much as possible
503 // V - normal to (P1,P2,PC)
504 //=======================================================================
506 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
507 const gp_Pnt& PC, const gp_Vec& V)
510 const double a2 = P1.SquareDistance(P2);
511 const double b2 = P1.SquareDistance(PC);
512 const double c2 = P2.SquareDistance(PC);
513 if ( a2 < ( b2 + Sqrt( 4 * b2 * c2 ) + c2 ) / 4 ) // ( a < (b+c)/2 )
516 // find shift along V in order a to became equal to (b+c)/2
517 const double Vsize = V.Magnitude();
518 if ( fabs( Vsize ) > std::numeric_limits<double>::min() )
520 const double shift = sqrt( a2 + (b2-c2)*(b2-c2)/16/a2 - (b2+c2)/2 );
521 Pbest.ChangeCoord() += shift * V.XYZ() / Vsize;
527 //=======================================================================
528 //function : HasIntersection3
529 //purpose : Find intersection point between a triangle (P1,P2,P3)
530 // and a segment [PC,P]
531 //=======================================================================
533 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
534 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
536 const double EPSILON = 1e-6;
537 double segLen = P.Distance( PC );
539 gp_XYZ orig = PC.XYZ();
540 gp_XYZ dir = ( P.XYZ() - PC.XYZ() ) / segLen;
541 gp_XYZ vert0 = P1.XYZ();
542 gp_XYZ vert1 = P2.XYZ();
543 gp_XYZ vert2 = P3.XYZ();
545 gp_XYZ edge1 = vert1 - vert0;
546 gp_XYZ edge2 = vert2 - vert0;
548 /* begin calculating determinant - also used to calculate U parameter */
549 gp_XYZ pvec = dir ^ edge2;
551 /* if determinant is near zero, ray lies in plane of triangle */
552 double det = edge1 * pvec;
554 const double ANGL_EPSILON = 1e-12;
555 if ( det > -ANGL_EPSILON && det < ANGL_EPSILON )
558 /* calculate distance from vert0 to ray origin */
559 gp_XYZ tvec = orig - vert0;
561 /* calculate U parameter and test bounds */
562 double u = ( tvec * pvec ) / det;
563 //if (u < 0.0 || u > 1.0)
564 if (u < -EPSILON || u > 1.0 + EPSILON)
567 /* prepare to test V parameter */
568 gp_XYZ qvec = tvec ^ edge1;
570 /* calculate V parameter and test bounds */
571 double v = (dir * qvec) / det;
572 //if ( v < 0.0 || u + v > 1.0 )
573 if ( v < -EPSILON || u + v > 1.0 + EPSILON)
576 /* calculate t, ray intersects triangle */
577 double t = (edge2 * qvec) / det;
579 Pint = orig + dir * t;
581 bool hasInt = ( t > 0. && t < segLen );
583 if ( hasInt && det < EPSILON ) // t is inaccurate, additionally check
585 gp_XYZ triNorm = edge1 ^ edge2;
586 gp_XYZ int0vec = Pint.XYZ() - vert0;
587 gp_XYZ in = triNorm ^ edge1; // dir inside triangle from edge1
588 double dot = int0vec * in;
589 if ( dot < 0 && dot / triNorm.Modulus() < -EPSILON )
591 in = edge2 ^ triNorm;
593 if ( dot < 0 && dot / triNorm.Modulus() < -EPSILON )
595 gp_XYZ int1vec = Pint.XYZ() - vert1;
596 in = triNorm ^ ( vert2 - vert1 );
598 if ( dot < 0 && dot / triNorm.Modulus() < -EPSILON )
604 //=======================================================================
605 //function : HasIntersection
606 //purpose : Auxilare for CheckIntersection()
607 //=======================================================================
609 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
610 TColgp_SequenceOfPnt& aContour)
612 if ( aContour.Length() == 3 ) {
613 return HasIntersection3( P, PC, Pint, aContour(1), aContour(2), aContour(3) );
617 if( (aContour(1).SquareDistance(aContour(2)) > 1.e-12) &&
618 (aContour(1).SquareDistance(aContour(3)) > 1.e-12) &&
619 (aContour(2).SquareDistance(aContour(3)) > 1.e-12) ) {
620 check = HasIntersection3( P, PC, Pint, aContour(1), aContour(2), aContour(3) );
622 if(check) return true;
623 if( (aContour(1).SquareDistance(aContour(4)) > 1.e-12) &&
624 (aContour(1).SquareDistance(aContour(3)) > 1.e-12) &&
625 (aContour(4).SquareDistance(aContour(3)) > 1.e-12) ) {
626 check = HasIntersection3( P, PC, Pint, aContour(1), aContour(3), aContour(4) );
628 if(check) return true;
634 //================================================================================
636 * \brief Return allowed height of a pyramid
637 * \param Papex - optimal pyramid apex
638 * \param PC - gravity center of a quadrangle
639 * \param PN - four nodes of the quadrangle
640 * \param aMesh - mesh
641 * \param NotCheckedFace - the quadrangle face
642 * \param Shape - the shape being meshed
643 * \retval false if mesh invalidity detected
645 //================================================================================
647 bool StdMeshers_QuadToTriaAdaptor::LimitHeight (gp_Pnt& Papex,
649 const TColgp_Array1OfPnt& PN,
650 const vector<const SMDS_MeshNode*>& FNodes,
652 const SMDS_MeshElement* NotCheckedFace,
653 const bool UseApexRay,
654 const TopoDS_Shape& Shape)
656 if ( !myElemSearcher )
657 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *aMesh.GetMeshDS() );
658 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
660 // Find intersection of faces with (P,PC) segment elongated 3 times
662 double height = Papex.Distance( PC );
663 gp_Ax1 line( PC, gp_Vec( PC, Papex ));
665 vector< const SMDS_MeshElement* > suspectFaces;
666 TColgp_SequenceOfPnt aContour;
670 double idealHeight = height;
671 const SMDS_MeshElement* intFace = 0;
673 // find intersection closest to PC
674 Ptest = PC.XYZ() + line.Direction().XYZ() * height * 3;
676 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces );
677 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
679 const SMDS_MeshElement* face = suspectFaces[iF];
680 if ( face == NotCheckedFace ) continue;
683 for ( int i = 0, nb = face->NbCornerNodes(); i < nb; ++i )
684 aContour.Append( SMESH_TNodeXYZ( face->GetNode(i) ));
686 if ( HasIntersection( Ptest, PC, Pint, aContour ))
688 double dInt = PC.Distance( Pint ) / 3.;
696 if ( height < 1e-2 * idealHeight && intFace )
697 return overlapError( aMesh, NotCheckedFace, intFace, Shape );
700 // Find faces intersecting triangular facets of the pyramid (issue 23212)
702 gp_XYZ center = PC.XYZ() + line.Direction().XYZ() * height * 0.5;
703 double diameter = Max( PN(1).Distance(PN(3)), PN(2).Distance(PN(4)));
704 suspectFaces.clear();
705 searcher->GetElementsInSphere( center, diameter * 0.6, SMDSAbs_Face, suspectFaces);
707 const double upShift = 1.5;
708 Ptest = PC.XYZ() + line.Direction().XYZ() * height * upShift; // tmp apex
710 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
712 const SMDS_MeshElement* face = suspectFaces[iF];
713 if ( face == NotCheckedFace ) continue;
714 if ( face->GetNodeIndex( FNodes[0] ) >= 0 ||
715 face->GetNodeIndex( FNodes[1] ) >= 0 ||
716 face->GetNodeIndex( FNodes[2] ) >= 0 ||
717 face->GetNodeIndex( FNodes[3] ) >= 0 )
718 continue; // neighbor face of the quadrangle
720 // limit height using points of intersection of face links with pyramid facets
721 int nbN = face->NbCornerNodes();
722 gp_Pnt P1 = SMESH_TNodeXYZ( face->GetNode( nbN-1 )); // 1st link end
723 for ( int i = 0; i < nbN; ++i )
725 gp_Pnt P2 = SMESH_TNodeXYZ( face->GetNode(i) ); // 2nd link end
727 for ( int iN = 1; iN <= 4; ++iN ) // loop on pyramid facets
729 if ( HasIntersection3( P1, P2, Pint, PN(iN), PN(iN+1), Ptest ))
731 height = Min( height, gp_Vec( PC, Pint ) * line.Direction() );
732 //Ptest = PC.XYZ() + line.Direction().XYZ() * height * upShift; // new tmp apex
739 Papex = PC.XYZ() + line.Direction().XYZ() * height;
744 //================================================================================
746 * \brief Retrieve data of the given face
747 * \param PN - coordinates of face nodes
748 * \param VN - cross products of vectors (PC-PN(i)) ^ (PC-PN(i+1))
749 * \param FNodes - face nodes
750 * \param PC - gravity center of nodes
751 * \param VNorm - face normal (sum of VN)
752 * \param volumes - two volumes sharing the given face, the first is in VNorm direction
753 * \retval int - 0 if given face is not quad,
754 * 1 if given face is quad,
755 * 2 if given face is degenerate quad (two nodes are coincided)
757 //================================================================================
759 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
760 TColgp_Array1OfPnt& PN,
761 TColgp_Array1OfVec& VN,
762 vector<const SMDS_MeshNode*>& FNodes,
765 const SMDS_MeshElement** volumes)
767 if( face->NbCornerNodes() != 4 )
773 gp_XYZ xyzC(0., 0., 0.);
774 for ( i = 0; i < 4; ++i )
776 gp_XYZ p = SMESH_TNodeXYZ( FNodes[i] = face->GetNode(i) );
777 PN.SetValue( i+1, p );
785 for ( i = 1; i < 4; i++ )
789 if( PN(i).Distance(PN(j)) < 1.e-6 )
801 list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
802 const SMDS_MeshNode* DegNode = 0;
803 for(; itdg!=myDegNodes.end(); itdg++) {
804 const SMDS_MeshNode* N = (*itdg);
805 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
806 if(Pdeg.Distance(Ptmp)<1.e-6) {
812 DegNode = FNodes[i-1];
813 myDegNodes.push_back(DegNode);
816 FNodes[i-1] = DegNode;
819 PN.SetValue(i,PN.Value(i+1));
820 FNodes[i-1] = FNodes[i];
825 PN.SetValue(nbp+1,PN(1));
826 FNodes[nbp] = FNodes[0];
828 // find normal direction
829 gp_Vec V1(PC,PN(nbp));
831 VNorm = V1.Crossed(V2);
832 VN.SetValue(nbp,VNorm);
833 for(i=1; i<nbp; i++) {
834 V1 = gp_Vec(PC,PN(i));
835 V2 = gp_Vec(PC,PN(i+1));
836 gp_Vec Vtmp = V1.Crossed(V2);
841 // find volumes sharing the face
844 volumes[0] = volumes[1] = 0;
845 SMDS_ElemIteratorPtr vIt = FNodes[0]->GetInverseElementIterator( SMDSAbs_Volume );
846 while ( vIt->more() )
848 const SMDS_MeshElement* vol = vIt->next();
849 bool volSharesAllNodes = true;
850 for ( int i = 1; i < face->NbNodes() && volSharesAllNodes; ++i )
851 volSharesAllNodes = ( vol->GetNodeIndex( FNodes[i] ) >= 0 );
852 if ( volSharesAllNodes )
853 volumes[ volumes[0] ? 1 : 0 ] = vol;
854 // we could additionally check that vol has all FNodes in its one face using SMDS_VolumeTool
856 // define volume position relating to the face normal
860 SMDS_ElemIteratorPtr nodeIt = volumes[0]->nodesIterator();
862 volGC = accumulate( TXyzIterator(nodeIt), TXyzIterator(), volGC ) / volumes[0]->NbNodes();
864 if ( VNorm * gp_Vec( PC, volGC ) < 0 )
865 swap( volumes[0], volumes[1] );
869 return hasdeg ? DEGEN_QUAD : QUAD;
873 //=======================================================================
876 //=======================================================================
878 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh,
879 const TopoDS_Shape& aShape,
880 SMESH_ProxyMesh* aProxyMesh)
882 SMESH_ProxyMesh::setMesh( aMesh );
884 if ( aShape.ShapeType() != TopAbs_SOLID )
889 vector<const SMDS_MeshElement*> myPyramids;
891 const SMESHDS_SubMesh * aSubMeshDSFace;
892 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
893 SMESH_MesherHelper helper1(aMesh);
894 helper1.IsQuadraticSubMesh(aShape);
896 if ( myElemSearcher ) delete myElemSearcher;
897 vector< SMDS_ElemIteratorPtr > itVec;
900 itVec.push_back( aProxyMesh->GetFaces( aShape ));
904 for ( TopExp_Explorer exp(aShape,TopAbs_FACE); exp.More(); exp.Next() )
905 if (( aSubMeshDSFace = meshDS->MeshElements( exp.Current() )))
906 itVec.push_back( aSubMeshDSFace->GetElements() );
909 SMDS_IteratorOnIterators< const SMDS_MeshElement*, vector< SMDS_ElemIteratorPtr > > TIter;
910 SMDS_ElemIteratorPtr faceIt( new TIter( itVec ));
911 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS, faceIt );
913 TColgp_Array1OfPnt PN(1,5);
914 TColgp_Array1OfVec VN(1,4);
915 vector<const SMDS_MeshNode*> FNodes(5);
918 const int solidID = meshDS->ShapeToIndex( aShape );
920 for ( TopExp_Explorer exp(aShape,TopAbs_FACE); exp.More(); exp.Next() )
922 const TopoDS_Shape& aShapeFace = exp.Current();
924 aSubMeshDSFace = aProxyMesh->GetSubMesh( aShapeFace );
926 aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
927 if ( !aSubMeshDSFace )
930 vector<const SMDS_MeshElement*> trias, quads;
931 bool hasNewTrias = false;
933 const bool toCheckFaceInSolid =
934 aProxyMesh ? aProxyMesh->HasPrismsOnTwoSides( meshDS->MeshElements( aShapeFace )) : false;
935 if ( toCheckFaceInSolid && !dynamic_cast< const SMESH_ProxyMesh::SubMesh* >( aSubMeshDSFace ))
936 continue; // no room for pyramids as prisms are on both sides
939 bool isRevGlob = false;
940 SMESH_MesherHelper helper2( aMesh );
941 PShapeIteratorPtr sIt = helper2.GetAncestors( aShapeFace, aMesh, aShape.ShapeType() );
942 while ( const TopoDS_Shape * solid = sIt->next() )
943 if ( !solid->IsSame( aShape ))
945 isRevGlob = helper2.IsReversedSubMesh( TopoDS::Face( aShapeFace ));
946 if ( toCheckFaceInSolid )
947 helper2.IsQuadraticSubMesh( *solid );
951 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
952 while ( iteratorElem->more() ) // loop on elements on a geometrical face
954 const SMDS_MeshElement* face = iteratorElem->next();
956 // preparation step to get face info
957 int stat = Preparation( face, PN, VN, FNodes, PC, VNorm );
959 bool isRev = isRevGlob;
960 SMESH_MesherHelper* helper = &helper1;
961 if ( toCheckFaceInSolid && !isInSolid( FNodes, face->NbCornerNodes(), solidID ))
971 trias.push_back( face );
977 // add triangles to result map
978 SMDS_MeshFace* NewFace;
979 helper->SetElementsOnShape( false );
981 NewFace = helper->AddFace( FNodes[0], FNodes[1], FNodes[2] );
983 NewFace = helper->AddFace( FNodes[0], FNodes[2], FNodes[1] );
984 storeTmpElement( NewFace );
985 trias.push_back ( NewFace );
986 quads.push_back( face );
993 if(!isRev) VNorm.Reverse();
994 double xc = 0., yc = 0., zc = 0.;
999 Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i).Reversed());
1001 Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i));
1006 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
1009 double height = PCbest.Distance(PC);
1010 if ( height < 1.e-6 ) {
1011 // create new PCbest using a bit shift along VNorm
1012 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
1015 // check possible intersection with other faces
1016 if ( !LimitHeight( PCbest, PC, PN, FNodes, aMesh, face, /*UseApexRay=*/true, aShape ))
1019 // create node at PCbest
1020 helper->SetElementsOnShape( true );
1021 SMDS_MeshNode* NewNode = helper->AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
1024 SMDS_MeshVolume* aPyram;
1026 aPyram = helper->AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
1028 aPyram = helper->AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
1029 myPyramids.push_back(aPyram);
1031 // add triangles to result map
1032 helper->SetElementsOnShape( false );
1033 for ( i = 0; i < 4; i++ )
1035 trias.push_back ( helper->AddFace( NewNode, FNodes[i], FNodes[i+1] ));
1036 storeTmpElement( trias.back() );
1039 quads.push_back( face );
1045 } // switch ( stat )
1046 } // end loop on elements on a face submesh
1048 bool sourceSubMeshIsProxy = false;
1051 // move proxy sub-mesh from other proxy mesh to this
1052 sourceSubMeshIsProxy = takeProxySubMesh( aShapeFace, aProxyMesh );
1053 // move also tmp elements added in mesh
1054 takeTmpElemsInMesh( aProxyMesh );
1058 SMESH_ProxyMesh::SubMesh* prxSubMesh = getProxySubMesh( aShapeFace );
1059 prxSubMesh->ChangeElements( trias.begin(), trias.end() );
1061 // delete tmp quadrangles removed from aProxyMesh
1062 if ( sourceSubMeshIsProxy )
1064 for ( unsigned i = 0; i < quads.size(); ++i )
1065 removeTmpElement( quads[i] );
1067 delete myElemSearcher;
1069 SMESH_MeshAlgos::GetElementSearcher( *meshDS, aProxyMesh->GetFaces(aShape));
1073 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
1075 return Compute2ndPart(aMesh, myPyramids);
1078 //================================================================================
1080 * \brief Computes pyramids in mesh with no shape
1082 //================================================================================
1084 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
1086 SMESH_ProxyMesh::setMesh( aMesh );
1087 SMESH_ProxyMesh::_allowedTypes.push_back( SMDSEntity_Triangle );
1088 SMESH_ProxyMesh::_allowedTypes.push_back( SMDSEntity_Quad_Triangle );
1089 if ( aMesh.NbQuadrangles() < 1 )
1092 // find if there is a group of faces identified as skin faces, with normal going outside the volume
1093 std::string groupName = "skinFaces";
1094 SMESHDS_GroupBase* groupDS = 0;
1095 SMESH_Mesh::GroupIteratorPtr groupIt = aMesh.GetGroups();
1096 while ( groupIt->more() )
1099 SMESH_Group * group = groupIt->next();
1100 if ( !group ) continue;
1101 groupDS = group->GetGroupDS();
1102 if ( !groupDS || groupDS->IsEmpty() )
1107 if (groupDS->GetType() != SMDSAbs_Face)
1112 std::string grpName = group->GetName();
1113 if (grpName == groupName)
1121 const bool toFindVolumes = aMesh.NbVolumes() > 0;
1123 vector<const SMDS_MeshElement*> myPyramids;
1124 SMESH_MesherHelper helper(aMesh);
1125 helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
1127 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1128 SMESH_ProxyMesh::SubMesh* prxSubMesh = getProxySubMesh();
1130 if ( !myElemSearcher )
1131 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS );
1132 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>( myElemSearcher );
1133 SMESHUtils::Deleter<SMESH_ElementSearcher>
1134 volSearcher( SMESH_MeshAlgos::GetElementSearcher( *meshDS ));
1135 vector< const SMDS_MeshElement* > suspectFaces, foundVolumes;
1137 TColgp_Array1OfPnt PN(1,5);
1138 TColgp_Array1OfVec VN(1,4);
1139 vector<const SMDS_MeshNode*> FNodes(5);
1140 TColgp_SequenceOfPnt aContour;
1142 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator();
1145 const SMDS_MeshElement* face = fIt->next();
1146 if ( !face ) continue;
1147 // retrieve needed information about a face
1150 const SMDS_MeshElement* volumes[2];
1151 int what = Preparation( face, PN, VN, FNodes, PC, VNorm, volumes );
1152 if ( what == NOT_QUAD )
1154 if ( volumes[0] && volumes[1] )
1155 continue; // face is shared by two volumes - no room for a pyramid
1157 if ( what == DEGEN_QUAD )
1160 // add a triangle to the proxy mesh
1161 SMDS_MeshFace* NewFace;
1163 // check orientation
1164 double tmp = PN(1).Distance(PN(2)) + PN(2).Distance(PN(3));
1165 // far points in VNorm direction
1166 gp_Pnt Ptmp1 = PC.XYZ() + VNorm.XYZ() * tmp * 1.e6;
1167 gp_Pnt Ptmp2 = PC.XYZ() - VNorm.XYZ() * tmp * 1.e6;
1168 // check intersection for Ptmp1 and Ptmp2
1172 double dist1 = RealLast();
1173 double dist2 = RealLast();
1176 gp_Ax1 line( PC, VNorm );
1177 vector< const SMDS_MeshElement* > suspectFaces;
1178 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces);
1180 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF ) {
1181 const SMDS_MeshElement* F = suspectFaces[iF];
1182 if ( F == face ) continue;
1184 for ( int i = 0; i < 4; ++i )
1185 aContour.Append( SMESH_TNodeXYZ( F->GetNode(i) ));
1187 if ( !volumes[0] && HasIntersection( Ptmp1, PC, PPP, aContour )) {
1189 double tmp = PC.Distance(PPP);
1190 if ( tmp < dist1 ) {
1195 if ( !volumes[1] && HasIntersection( Ptmp2, PC, PPP, aContour )) {
1197 double tmp = PC.Distance(PPP);
1198 if ( tmp < dist2 ) {
1205 if( IsOK1 && !IsOK2 ) {
1206 // using existed direction
1208 else if( !IsOK1 && IsOK2 ) {
1209 // using opposite direction
1212 else { // IsOK1 && IsOK2
1213 double tmp1 = PC.Distance(Pres1);
1214 double tmp2 = PC.Distance(Pres2);
1216 // using existed direction
1219 // using opposite direction
1223 helper.SetElementsOnShape( false );
1225 NewFace = helper.AddFace( FNodes[0], FNodes[1], FNodes[2] );
1227 NewFace = helper.AddFace( FNodes[0], FNodes[2], FNodes[1] );
1228 storeTmpElement( NewFace );
1229 prxSubMesh->AddElement( NewFace );
1233 // -----------------------------------
1234 // Case of non-degenerated quadrangle
1235 // -----------------------------------
1237 // Find pyramid peak
1239 gp_XYZ PCbest(0., 0., 0.); // pyramid peak
1241 for ( ; i <= 4; i++ ) {
1242 gp_Pnt Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i));
1243 PCbest += Pbest.XYZ();
1247 double height = PC.Distance(PCbest); // pyramid height to precise
1248 if ( height < 1.e-6 ) {
1249 // create new PCbest using a bit shift along VNorm
1250 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
1251 height = PC.Distance(PCbest);
1252 if ( height < std::numeric_limits<double>::min() )
1253 return false; // batterfly element
1256 // Restrict pyramid height by intersection with other faces
1258 gp_Vec tmpDir(PC,PCbest); tmpDir.Normalize();
1259 double tmp = PN(1).Distance(PN(3)) + PN(2).Distance(PN(4));
1260 // far points: in (PC, PCbest) direction and vice-versa
1261 gp_Pnt farPnt[2] = { PC.XYZ() + tmpDir.XYZ() * tmp * 1.e6,
1262 PC.XYZ() - tmpDir.XYZ() * tmp * 1.e6 };
1263 // check intersection for farPnt1 and farPnt2
1264 bool intersected[2] = { false, false };
1265 double dist2int [2] = { RealLast(), RealLast() };
1267 int intFaceInd [2] = { 0, 0 };
1269 if ( toFindVolumes && 0 ) // non-conformal mesh is not suitable for any mesher so far
1271 // there are volumes in the mesh, in a non-conformal mesh a neighbor
1272 // volume can be not found yet
1273 for ( int isRev = 0; isRev < 2; ++isRev )
1275 if ( volumes[isRev] ) continue;
1276 gp_Pnt testPnt = PC.XYZ() + tmpDir.XYZ() * height * ( isRev ? -0.1: 0.1 );
1277 foundVolumes.clear();
1278 if ( volSearcher->FindElementsByPoint( testPnt, SMDSAbs_Volume, foundVolumes ))
1279 volumes[isRev] = foundVolumes[0];
1281 if ( volumes[0] && volumes[1] )
1282 continue; // no room for a pyramid
1285 gp_Ax1 line( PC, tmpDir );
1286 suspectFaces.clear();
1287 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces);
1289 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
1291 const SMDS_MeshElement* F = suspectFaces[iF];
1292 if ( F == face ) continue;
1294 int nbN = F->NbCornerNodes();
1295 for ( i = 0; i < nbN; ++i )
1296 aContour.Append( SMESH_TNodeXYZ( F->GetNode(i) ));
1298 for ( int isRev = 0; isRev < 2; ++isRev )
1300 if( !volumes[isRev] && HasIntersection( farPnt[isRev], PC, intP, aContour ))
1302 double d = PC.Distance( intP );
1303 if ( d < dist2int[isRev] )
1305 intersected[isRev] = true;
1306 intPnt [isRev] = intP;
1307 dist2int [isRev] = d;
1308 intFaceInd [isRev] = iF;
1314 // if the face belong to the group of skinFaces, do not build a pyramid outside
1315 if ( groupDS && groupDS->Contains(face) )
1317 intersected[0] = false;
1319 else if ( intersected[0] && intersected[1] ) // check if one of pyramids is in a hole
1321 gp_Pnt P ( PC.XYZ() + tmpDir.XYZ() * 0.5 * dist2int[0] );
1322 if ( searcher->GetPointState( P ) == TopAbs_OUT )
1323 intersected[0] = false;
1326 P = ( PC.XYZ() - tmpDir.XYZ() * 0.5 * dist2int[1] );
1327 if ( searcher->GetPointState( P ) == TopAbs_OUT )
1328 intersected[1] = false;
1332 // Create one or two pyramids
1334 for ( int isRev = 0; isRev < 2; ++isRev )
1336 if ( !intersected[isRev] ) continue;
1337 double pyramidH = Min( height, dist2int[isRev]/3. );
1338 gp_Pnt Papex = PC.XYZ() + tmpDir.XYZ() * (isRev ? -pyramidH : pyramidH);
1339 if ( pyramidH < 1e-2 * height )
1340 return overlapError( aMesh, face, suspectFaces[ intFaceInd[isRev] ] );
1342 if ( !LimitHeight( Papex, PC, PN, FNodes, aMesh, face, /*UseApexRay=*/false ))
1345 // create node for Papex
1346 helper.SetElementsOnShape( true );
1347 SMDS_MeshNode* NewNode = helper.AddNode( Papex.X(), Papex.Y(), Papex.Z() );
1350 SMDS_MeshVolume* aPyram;
1352 aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
1354 aPyram = helper.AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
1355 myPyramids.push_back(aPyram);
1357 // add triangles to result map
1358 helper.SetElementsOnShape( false );
1359 for ( i = 0; i < 4; i++) {
1360 SMDS_MeshFace* NewFace;
1362 NewFace = helper.AddFace( NewNode, FNodes[i], FNodes[i+1] );
1364 NewFace = helper.AddFace( NewNode, FNodes[i+1], FNodes[i] );
1365 storeTmpElement( NewFace );
1366 prxSubMesh->AddElement( NewFace );
1369 } // end loop on all faces
1371 return Compute2ndPart(aMesh, myPyramids);
1374 //================================================================================
1376 * \brief Update created pyramids and faces to avoid their intersection
1378 //================================================================================
1380 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh,
1381 const vector<const SMDS_MeshElement*>& myPyramids)
1383 if ( myPyramids.empty() )
1386 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1388 //int myShapeID = myPyramids[0]->GetNode(4)->getshapeId();
1390 SMDS_ElemIteratorPtr
1391 pyramIt( new SMDS_ElementVectorIterator( myPyramids.begin(), myPyramids.end() ));
1392 if ( myElemSearcher ) delete myElemSearcher;
1393 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS, pyramIt );
1395 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>( myElemSearcher );
1397 set<const SMDS_MeshNode*> nodesToMove;
1399 // check adjacent pyramids
1401 for ( i = 0; i < myPyramids.size(); ++i )
1403 const SMDS_MeshElement* PrmI = myPyramids[i];
1404 MergeAdjacent( PrmI, nodesToMove );
1407 // iterate on all new pyramids
1408 vector< const SMDS_MeshElement* > suspectPyrams;
1409 for ( i = 0; i < myPyramids.size(); ++i )
1411 const SMDS_MeshElement* PrmI = myPyramids[i];
1412 const SMDS_MeshNode* apexI = PrmI->GetNode( PYRAM_APEX );
1414 // compare PrmI with all the rest pyramids
1416 // collect adjacent pyramids and nodes coordinates of PrmI
1417 set<const SMDS_MeshElement*> checkedPyrams;
1419 for ( k = 0; k < 5; k++ )
1421 const SMDS_MeshNode* n = PrmI->GetNode(k);
1422 PsI[k] = SMESH_TNodeXYZ( n );
1423 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
1424 while ( vIt->more() )
1426 const SMDS_MeshElement* PrmJ = vIt->next();
1427 if ( SMESH_MeshAlgos::NbCommonNodes( PrmI, PrmJ ) > 1 )
1428 checkedPyrams.insert( PrmJ );
1432 // get pyramids to check
1433 gp_XYZ PC = ( PsI[0].XYZ() + PsI[1].XYZ() + PsI[2].XYZ() + PsI[3].XYZ() ) / 4.;
1434 gp_XYZ ray = PsI[4].XYZ() - PC;
1435 gp_XYZ center = PC + 0.5 * ray;
1436 double diameter = Max( PsI[0].Distance(PsI[2]), PsI[1].Distance(PsI[3]));
1437 suspectPyrams.clear();
1438 searcher->GetElementsInSphere( center, diameter * 0.6, SMDSAbs_Volume, suspectPyrams);
1440 // check intersection with distant pyramids
1441 for ( j = 0; j < suspectPyrams.size(); ++j )
1443 const SMDS_MeshElement* PrmJ = suspectPyrams[j];
1446 if ( apexI == PrmJ->GetNode( PYRAM_APEX ))
1447 continue; // pyramids PrmI and PrmJ already merged
1448 if ( !checkedPyrams.insert( PrmJ ).second )
1449 continue; // already checked
1452 for ( k = 0; k < 5; k++ )
1453 PsJ[k] = SMESH_TNodeXYZ( PrmJ->GetNode(k) );
1455 if ( ray * ( PsJ[4].XYZ() - PC ) < 0. )
1456 continue; // PrmJ is below PrmI
1458 for ( k = 0; k < 4; k++ ) // loop on 4 base nodes of PrmI
1462 for ( k = 0; k < 4 && !hasInt; k++ )
1464 gp_Vec Vtmp( PsI[k], PsI[ PYRAM_APEX ]);
1465 gp_Pnt Pshift = PsI[k].XYZ() + Vtmp.XYZ() * 0.01; // base node moved a bit to apex
1467 ( HasIntersection3( Pshift, PsI[4], Pint, PsJ[0], PsJ[1], PsJ[PYRAM_APEX]) ||
1468 HasIntersection3( Pshift, PsI[4], Pint, PsJ[1], PsJ[2], PsJ[PYRAM_APEX]) ||
1469 HasIntersection3( Pshift, PsI[4], Pint, PsJ[2], PsJ[3], PsJ[PYRAM_APEX]) ||
1470 HasIntersection3( Pshift, PsI[4], Pint, PsJ[3], PsJ[0], PsJ[PYRAM_APEX]) );
1472 for ( k = 0; k < 4 && !hasInt; k++ )
1474 gp_Vec Vtmp( PsJ[k], PsJ[ PYRAM_APEX ]);
1475 gp_Pnt Pshift = PsJ[k].XYZ() + Vtmp.XYZ() * 0.01;
1477 ( HasIntersection3( Pshift, PsJ[4], Pint, PsI[0], PsI[1], PsI[PYRAM_APEX]) ||
1478 HasIntersection3( Pshift, PsJ[4], Pint, PsI[1], PsI[2], PsI[PYRAM_APEX]) ||
1479 HasIntersection3( Pshift, PsJ[4], Pint, PsI[2], PsI[3], PsI[PYRAM_APEX]) ||
1480 HasIntersection3( Pshift, PsJ[4], Pint, PsI[3], PsI[0], PsI[PYRAM_APEX]) );
1485 // count common nodes of base faces of two pyramids
1487 for ( k = 0; k < 4; k++ )
1488 nbc += int ( PrmI->GetNodeIndex( PrmJ->GetNode(k) ) >= 0 );
1491 continue; // pyrams have a common base face
1495 // Merge the two pyramids and others already merged with them
1496 MergePiramids( PrmI, PrmJ, nodesToMove );
1500 // decrease height of pyramids
1501 gp_XYZ PCi(0,0,0), PCj(0,0,0);
1502 for ( k = 0; k < 4; k++ ) {
1503 PCi += PsI[k].XYZ();
1504 PCj += PsJ[k].XYZ();
1507 gp_Vec VN1(PCi,PsI[4]);
1508 gp_Vec VN2(PCj,PsJ[4]);
1509 gp_Vec VI1(PCi,Pint);
1510 gp_Vec VI2(PCj,Pint);
1511 double ang1 = fabs(VN1.Angle(VI1));
1512 double ang2 = fabs(VN2.Angle(VI2));
1513 double coef1 = 0.5 - (( ang1 < M_PI/3. ) ? cos(ang1)*0.25 : 0 );
1514 double coef2 = 0.5 - (( ang2 < M_PI/3. ) ? cos(ang2)*0.25 : 0 ); // cos(ang2) ?
1515 // double coef2 = 0.5;
1517 // coef2 -= cos(ang1)*0.25;
1521 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>( apexI );
1522 aNode1->setXYZ( PCi.X()+VN1.X(), PCi.Y()+VN1.Y(), PCi.Z()+VN1.Z() );
1523 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(PrmJ->GetNode( PYRAM_APEX ));
1524 aNode2->setXYZ( PCj.X()+VN2.X(), PCj.Y()+VN2.Y(), PCj.Z()+VN2.Z() );
1525 nodesToMove.insert( aNode1 );
1526 nodesToMove.insert( aNode2 );
1528 // fix intersections that can appear after apex movement
1529 MergeAdjacent( PrmI, nodesToMove );
1530 MergeAdjacent( PrmJ, nodesToMove );
1533 } // loop on suspectPyrams
1534 } // loop on 4 base nodes of PrmI
1536 } // loop on all pyramids
1538 if( !nodesToMove.empty() && !meshDS->IsEmbeddedMode() )
1540 set<const SMDS_MeshNode*>::iterator n = nodesToMove.begin();
1541 for ( ; n != nodesToMove.end(); ++n )
1542 meshDS->MoveNode( *n, (*n)->X(), (*n)->Y(), (*n)->Z() );
1545 // move medium nodes of merged quadratic pyramids
1546 if ( myPyramids[0]->IsQuadratic() )
1547 UpdateQuadraticPyramids( nodesToMove, GetMeshDS() );
1549 // erase removed triangles from the proxy mesh
1550 if ( !myRemovedTrias.empty() )
1552 for ( int i = 0; i <= meshDS->MaxShapeIndex(); ++i )
1553 if ( SMESH_ProxyMesh::SubMesh* sm = findProxySubMesh(i))
1555 vector<const SMDS_MeshElement *> faces;
1556 faces.reserve( sm->NbElements() );
1557 SMDS_ElemIteratorPtr fIt = sm->GetElements();
1558 while ( fIt->more() )
1560 const SMDS_MeshElement* tria = fIt->next();
1561 set<const SMDS_MeshElement*>::iterator rmTria = myRemovedTrias.find( tria );
1562 if ( rmTria != myRemovedTrias.end() )
1563 myRemovedTrias.erase( rmTria );
1565 faces.push_back( tria );
1567 sm->ChangeElements( faces.begin(), faces.end() );
1573 delete myElemSearcher;