1 // Copyright (C) 2007-2016 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 triange 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);
263 //================================================================================
265 * \brief Store an error about overlapping faces
267 //================================================================================
269 bool overlapError( SMESH_Mesh& mesh,
270 const SMDS_MeshElement* face1,
271 const SMDS_MeshElement* face2,
272 const TopoDS_Shape& shape = TopoDS_Shape())
274 if ( !face1 || !face2 ) return false;
277 msg << "face " << face1->GetID() << " overlaps face " << face2->GetID();
279 SMESH_subMesh * sm = 0;
280 if ( shape.IsNull() )
282 sm = mesh.GetSubMesh( mesh.GetShapeToMesh() );
284 else if ( shape.ShapeType() >= TopAbs_SOLID )
286 sm = mesh.GetSubMesh( shape );
290 TopoDS_Iterator it ( shape );
292 sm = mesh.GetSubMesh( it.Value() );
296 SMESH_ComputeErrorPtr& err = sm->GetComputeError();
297 if ( !err || err->IsOK() )
299 err = SMESH_ComputeError::New( COMPERR_BAD_INPUT_MESH, msg, sm->GetAlgo() );
300 err->myBadElements.push_back( face1 );
301 err->myBadElements.push_back( face2 );
304 //throw SALOME_Exception( msg.c_str() );
306 return false; // == "algo fails"
310 //================================================================================
312 * \brief Merge the two pyramids (i.e. fuse their apex) and others already merged with them
314 //================================================================================
316 void StdMeshers_QuadToTriaAdaptor::MergePiramids( const SMDS_MeshElement* PrmI,
317 const SMDS_MeshElement* PrmJ,
318 set<const SMDS_MeshNode*> & nodesToMove)
320 // cout << endl << "Merge " << PrmI->GetID() << " " << PrmJ->GetID() << " "
321 // << PrmI->GetNode(4) << PrmJ->GetNode(4) << endl;
322 const SMDS_MeshNode* Nrem = PrmJ->GetNode(4); // node to remove
323 //int nbJ = Nrem->NbInverseElements( SMDSAbs_Volume );
324 SMESH_TNodeXYZ Pj( Nrem );
326 // an apex node to make common to all merged pyramids
327 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
328 if ( CommonNode == Nrem ) return; // already merged
329 //int nbI = CommonNode->NbInverseElements( SMDSAbs_Volume );
330 SMESH_TNodeXYZ Pi( CommonNode );
331 gp_XYZ Pnew = /*( nbI*Pi + nbJ*Pj ) / (nbI+nbJ);*/ 0.5 * ( Pi + Pj );
332 CommonNode->setXYZ( Pnew.X(), Pnew.Y(), Pnew.Z() );
334 nodesToMove.insert( CommonNode );
335 nodesToMove.erase ( Nrem );
337 typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TStdElemIterator;
338 TStdElemIterator itEnd;
340 // find and remove coincided faces of merged pyramids
341 vector< const SMDS_MeshElement* > inverseElems
342 // copy inverse elements to avoid iteration on changing container
343 ( TStdElemIterator( CommonNode->GetInverseElementIterator(SMDSAbs_Face)), itEnd);
344 for ( size_t i = 0; i < inverseElems.size(); ++i )
346 const SMDS_MeshElement* FI = inverseElems[i];
347 const SMDS_MeshElement* FJEqual = 0;
348 SMDS_ElemIteratorPtr triItJ = Nrem->GetInverseElementIterator(SMDSAbs_Face);
349 while ( !FJEqual && triItJ->more() )
351 const SMDS_MeshElement* FJ = triItJ->next();
352 if ( EqualTriangles( FJ, FI ))
357 removeTmpElement( FI );
358 removeTmpElement( FJEqual );
359 myRemovedTrias.insert( FI );
360 myRemovedTrias.insert( FJEqual );
364 // set the common apex node to pyramids and triangles merged with J
365 vector< const SMDS_MeshNode* > nodes;
366 inverseElems.assign( TStdElemIterator( Nrem->GetInverseElementIterator()), itEnd );
367 for ( size_t i = 0; i < inverseElems.size(); ++i )
369 const SMDS_MeshElement* elem = inverseElems[i];
370 nodes.assign( elem->begin_nodes(), elem->end_nodes() );
371 nodes[ elem->GetType() == SMDSAbs_Volume ? PYRAM_APEX : TRIA_APEX ] = CommonNode;
372 GetMeshDS()->ChangeElementNodes( elem, &nodes[0], nodes.size());
374 ASSERT( Nrem->NbInverseElements() == 0 );
375 GetMeshDS()->RemoveFreeNode( Nrem,
376 GetMeshDS()->MeshElements( Nrem->getshapeId()),
377 /*fromGroups=*/false);
380 //================================================================================
382 * \brief Merges adjacent pyramids
384 //================================================================================
386 void StdMeshers_QuadToTriaAdaptor::MergeAdjacent(const SMDS_MeshElement* PrmI,
387 set<const SMDS_MeshNode*>& nodesToMove,
388 const bool isRecursion)
390 TIDSortedElemSet adjacentPyrams;
391 bool mergedPyrams = false;
392 for ( int k=0; k<4; k++ ) // loop on 4 base nodes of PrmI
394 const SMDS_MeshNode* n = PrmI->GetNode(k);
395 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
396 while ( vIt->more() )
398 const SMDS_MeshElement* PrmJ = vIt->next();
399 if ( PrmJ == PrmI || PrmJ->NbCornerNodes() != 5 || !adjacentPyrams.insert( PrmJ ).second )
401 if ( TooCloseAdjacent( PrmI, PrmJ, GetMesh()->HasShapeToMesh() ))
403 MergePiramids( PrmI, PrmJ, nodesToMove );
405 // container of inverse elements can change
406 // vIt = n->GetInverseElementIterator( SMDSAbs_Volume ); -- iterator re-implemented
410 if ( mergedPyrams && !isRecursion )
412 TIDSortedElemSet::iterator prm;
413 for (prm = adjacentPyrams.begin(); prm != adjacentPyrams.end(); ++prm)
414 MergeAdjacent( *prm, nodesToMove, true );
418 //================================================================================
422 //================================================================================
424 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor():
429 //================================================================================
433 //================================================================================
435 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
437 // temporary faces are deleted by ~SMESH_ProxyMesh()
438 if ( myElemSearcher ) delete myElemSearcher;
442 //=======================================================================
443 //function : FindBestPoint
444 //purpose : Return a point P laying on the line (PC,V) so that triangle
445 // (P, P1, P2) to be equilateral as much as possible
446 // V - normal to (P1,P2,PC)
447 //=======================================================================
449 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
450 const gp_Pnt& PC, const gp_Vec& V)
453 const double a2 = P1.SquareDistance(P2);
454 const double b2 = P1.SquareDistance(PC);
455 const double c2 = P2.SquareDistance(PC);
456 if ( a2 < ( b2 + Sqrt( 4 * b2 * c2 ) + c2 ) / 4 ) // ( a < (b+c)/2 )
459 // find shift along V in order a to became equal to (b+c)/2
460 const double Vsize = V.Magnitude();
461 if ( fabs( Vsize ) > std::numeric_limits<double>::min() )
463 const double shift = sqrt( a2 + (b2-c2)*(b2-c2)/16/a2 - (b2+c2)/2 );
464 Pbest.ChangeCoord() += shift * V.XYZ() / Vsize;
470 //=======================================================================
471 //function : HasIntersection3
472 //purpose : Find intersection point between a triangle (P1,P2,P3)
473 // and a segment [PC,P]
474 //=======================================================================
476 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
477 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
479 const double EPSILON = 1e-6;
480 double segLen = P.Distance( PC );
482 gp_XYZ orig = PC.XYZ();
483 gp_XYZ dir = ( P.XYZ() - PC.XYZ() ) / segLen;
484 gp_XYZ vert0 = P1.XYZ();
485 gp_XYZ vert1 = P2.XYZ();
486 gp_XYZ vert2 = P3.XYZ();
488 gp_XYZ edge1 = vert1 - vert0;
489 gp_XYZ edge2 = vert2 - vert0;
491 /* begin calculating determinant - also used to calculate U parameter */
492 gp_XYZ pvec = dir ^ edge2;
494 /* if determinant is near zero, ray lies in plane of triangle */
495 double det = edge1 * pvec;
497 const double ANGL_EPSILON = 1e-12;
498 if ( det > -ANGL_EPSILON && det < ANGL_EPSILON )
501 /* calculate distance from vert0 to ray origin */
502 gp_XYZ tvec = orig - vert0;
504 /* calculate U parameter and test bounds */
505 double u = ( tvec * pvec ) / det;
506 //if (u < 0.0 || u > 1.0)
507 if (u < -EPSILON || u > 1.0 + EPSILON)
510 /* prepare to test V parameter */
511 gp_XYZ qvec = tvec ^ edge1;
513 /* calculate V parameter and test bounds */
514 double v = (dir * qvec) / det;
515 //if ( v < 0.0 || u + v > 1.0 )
516 if ( v < -EPSILON || u + v > 1.0 + EPSILON)
519 /* calculate t, ray intersects triangle */
520 double t = (edge2 * qvec) / det;
522 Pint = orig + dir * t;
524 return ( t > 0. && t < segLen );
527 //=======================================================================
528 //function : HasIntersection
529 //purpose : Auxilare for CheckIntersection()
530 //=======================================================================
532 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
533 TColgp_SequenceOfPnt& aContour)
535 if ( aContour.Length() == 3 ) {
536 return HasIntersection3( P, PC, Pint, aContour(1), aContour(2), aContour(3) );
540 if( (aContour(1).SquareDistance(aContour(2)) > 1.e-12) &&
541 (aContour(1).SquareDistance(aContour(3)) > 1.e-12) &&
542 (aContour(2).SquareDistance(aContour(3)) > 1.e-12) ) {
543 check = HasIntersection3( P, PC, Pint, aContour(1), aContour(2), aContour(3) );
545 if(check) return true;
546 if( (aContour(1).SquareDistance(aContour(4)) > 1.e-12) &&
547 (aContour(1).SquareDistance(aContour(3)) > 1.e-12) &&
548 (aContour(4).SquareDistance(aContour(3)) > 1.e-12) ) {
549 check = HasIntersection3( P, PC, Pint, aContour(1), aContour(3), aContour(4) );
551 if(check) return true;
557 //================================================================================
559 * \brief Return allowed height of a pyramid
560 * \param Papex - optimal pyramid apex
561 * \param PC - gravity center of a quadrangle
562 * \param PN - four nodes of the quadrangle
563 * \param aMesh - mesh
564 * \param NotCheckedFace - the quadrangle face
565 * \param Shape - the shape being meshed
566 * \retval false if mesh invalidity detected
568 //================================================================================
570 bool StdMeshers_QuadToTriaAdaptor::LimitHeight (gp_Pnt& Papex,
572 const TColgp_Array1OfPnt& PN,
573 const vector<const SMDS_MeshNode*>& FNodes,
575 const SMDS_MeshElement* NotCheckedFace,
576 const bool UseApexRay,
577 const TopoDS_Shape& Shape)
579 if ( !myElemSearcher )
580 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *aMesh.GetMeshDS() );
581 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
583 // Find intersection of faces with (P,PC) segment elongated 3 times
585 double height = Papex.Distance( PC );
586 gp_Ax1 line( PC, gp_Vec( PC, Papex ));
588 vector< const SMDS_MeshElement* > suspectFaces;
589 TColgp_SequenceOfPnt aContour;
593 double idealHeight = height;
594 const SMDS_MeshElement* intFace = 0;
596 // find intersection closest to PC
597 Ptest = PC.XYZ() + line.Direction().XYZ() * height * 3;
599 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces );
600 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
602 const SMDS_MeshElement* face = suspectFaces[iF];
603 if ( face == NotCheckedFace ) continue;
606 for ( int i = 0, nb = face->NbCornerNodes(); i < nb; ++i )
607 aContour.Append( SMESH_TNodeXYZ( face->GetNode(i) ));
609 if ( HasIntersection( Ptest, PC, Pint, aContour ))
611 double dInt = PC.Distance( Pint ) / 3.;
619 if ( height < 1e-2 * idealHeight && intFace )
620 return overlapError( aMesh, NotCheckedFace, intFace, Shape );
623 // Find faces intersecting triangular facets of the pyramid (issue 23212)
625 gp_XYZ center = PC.XYZ() + line.Direction().XYZ() * height * 0.5;
626 double diameter = Max( PN(1).Distance(PN(3)), PN(2).Distance(PN(4)));
627 suspectFaces.clear();
628 searcher->GetElementsInSphere( center, diameter * 0.6, SMDSAbs_Face, suspectFaces);
630 const double upShift = 1.5;
631 Ptest = PC.XYZ() + line.Direction().XYZ() * height * upShift; // tmp apex
633 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
635 const SMDS_MeshElement* face = suspectFaces[iF];
636 if ( face == NotCheckedFace ) continue;
637 if ( face->GetNodeIndex( FNodes[0] ) >= 0 ||
638 face->GetNodeIndex( FNodes[1] ) >= 0 ||
639 face->GetNodeIndex( FNodes[2] ) >= 0 ||
640 face->GetNodeIndex( FNodes[3] ) >= 0 )
641 continue; // neighbor face of the quadrangle
643 // limit height using points of intersection of face links with pyramid facets
644 int nbN = face->NbCornerNodes();
645 gp_Pnt P1 = SMESH_TNodeXYZ( face->GetNode( nbN-1 )); // 1st link end
646 for ( int i = 0; i < nbN; ++i )
648 gp_Pnt P2 = SMESH_TNodeXYZ( face->GetNode(i) ); // 2nd link end
650 for ( int iN = 1; iN <= 4; ++iN ) // loop on pyramid facets
652 if ( HasIntersection3( P1, P2, Pint, PN(iN), PN(iN+1), Ptest ))
654 height = Min( height, gp_Vec( PC, Pint ) * line.Direction() );
655 //Ptest = PC.XYZ() + line.Direction().XYZ() * height * upShift; // new tmp apex
662 Papex = PC.XYZ() + line.Direction().XYZ() * height;
667 //================================================================================
669 * \brief Prepare data for the given face
670 * \param PN - coordinates of face nodes
671 * \param VN - cross products of vectors (PC-PN(i)) ^ (PC-PN(i+1))
672 * \param FNodes - face nodes
673 * \param PC - gravity center of nodes
674 * \param VNorm - face normal (sum of VN)
675 * \param volumes - two volumes sharing the given face, the first is in VNorm direction
676 * \retval int - 0 if given face is not quad,
677 * 1 if given face is quad,
678 * 2 if given face is degenerate quad (two nodes are coincided)
680 //================================================================================
682 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
683 TColgp_Array1OfPnt& PN,
684 TColgp_Array1OfVec& VN,
685 vector<const SMDS_MeshNode*>& FNodes,
688 const SMDS_MeshElement** volumes)
690 if( face->NbCornerNodes() != 4 )
696 gp_XYZ xyzC(0., 0., 0.);
697 for ( i = 0; i < 4; ++i )
699 gp_XYZ p = SMESH_TNodeXYZ( FNodes[i] = face->GetNode(i) );
700 PN.SetValue( i+1, p );
711 if( PN(i).Distance(PN(j)) < 1.e-6 )
716 //int deg_num = IsDegenarate(PN);
720 //cout<<"find degeneration"<<endl;
724 list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
725 const SMDS_MeshNode* DegNode = 0;
726 for(; itdg!=myDegNodes.end(); itdg++) {
727 const SMDS_MeshNode* N = (*itdg);
728 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
729 if(Pdeg.Distance(Ptmp)<1.e-6) {
731 //DegNode = const_cast<SMDS_MeshNode*>(N);
736 DegNode = FNodes[i-1];
737 myDegNodes.push_back(DegNode);
740 FNodes[i-1] = DegNode;
743 PN.SetValue(i,PN.Value(i+1));
744 FNodes[i-1] = FNodes[i];
749 PN.SetValue(nbp+1,PN(1));
750 FNodes[nbp] = FNodes[0];
751 // find normal direction
752 gp_Vec V1(PC,PN(nbp));
754 VNorm = V1.Crossed(V2);
755 VN.SetValue(nbp,VNorm);
756 for(i=1; i<nbp; i++) {
757 V1 = gp_Vec(PC,PN(i));
758 V2 = gp_Vec(PC,PN(i+1));
759 gp_Vec Vtmp = V1.Crossed(V2);
764 // find volumes sharing the face
767 volumes[0] = volumes[1] = 0;
768 SMDS_ElemIteratorPtr vIt = FNodes[0]->GetInverseElementIterator( SMDSAbs_Volume );
769 while ( vIt->more() )
771 const SMDS_MeshElement* vol = vIt->next();
772 bool volSharesAllNodes = true;
773 for ( int i = 1; i < face->NbNodes() && volSharesAllNodes; ++i )
774 volSharesAllNodes = ( vol->GetNodeIndex( FNodes[i] ) >= 0 );
775 if ( volSharesAllNodes )
776 volumes[ volumes[0] ? 1 : 0 ] = vol;
777 // we could additionally check that vol has all FNodes in its one face using SMDS_VolumeTool
779 // define volume position relating to the face normal
783 SMDS_ElemIteratorPtr nodeIt = volumes[0]->nodesIterator();
785 volGC = accumulate( TXyzIterator(nodeIt), TXyzIterator(), volGC ) / volumes[0]->NbNodes();
787 if ( VNorm * gp_Vec( PC, volGC ) < 0 )
788 swap( volumes[0], volumes[1] );
792 //cout<<" VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
793 return hasdeg ? DEGEN_QUAD : QUAD;
797 //=======================================================================
800 //=======================================================================
802 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh,
803 const TopoDS_Shape& aShape,
804 SMESH_ProxyMesh* aProxyMesh)
806 SMESH_ProxyMesh::setMesh( aMesh );
808 if ( aShape.ShapeType() != TopAbs_SOLID &&
809 aShape.ShapeType() != TopAbs_SHELL )
814 vector<const SMDS_MeshElement*> myPyramids;
816 const SMESHDS_SubMesh * aSubMeshDSFace;
817 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
818 SMESH_MesherHelper helper(aMesh);
819 helper.IsQuadraticSubMesh(aShape);
820 helper.SetElementsOnShape( true );
822 if ( myElemSearcher ) delete myElemSearcher;
823 vector< SMDS_ElemIteratorPtr > itVec;
826 itVec.push_back( aProxyMesh->GetFaces( aShape ));
830 for ( TopExp_Explorer exp(aShape,TopAbs_FACE); exp.More(); exp.Next() )
831 if (( aSubMeshDSFace = meshDS->MeshElements( exp.Current() )))
832 itVec.push_back( aSubMeshDSFace->GetElements() );
835 SMDS_IteratorOnIterators< const SMDS_MeshElement*, vector< SMDS_ElemIteratorPtr > > TIter;
836 SMDS_ElemIteratorPtr faceIt( new TIter( itVec ));
837 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS, faceIt );
839 TColgp_Array1OfPnt PN(1,5);
840 TColgp_Array1OfVec VN(1,4);
841 vector<const SMDS_MeshNode*> FNodes(5);
845 for ( TopExp_Explorer exp(aShape,TopAbs_FACE); exp.More(); exp.Next() )
847 const TopoDS_Shape& aShapeFace = exp.Current();
849 aSubMeshDSFace = aProxyMesh->GetSubMesh( aShapeFace );
851 aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
853 vector<const SMDS_MeshElement*> trias, quads;
854 bool hasNewTrias = false;
856 if ( aSubMeshDSFace )
859 if ( helper.NbAncestors( aShapeFace, aMesh, aShape.ShapeType() ) > 1 )
860 isRev = helper.IsReversedSubMesh( TopoDS::Face(aShapeFace) );
862 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
863 while ( iteratorElem->more() ) // loop on elements on a geometrical face
865 const SMDS_MeshElement* face = iteratorElem->next();
866 // preparation step to get face info
867 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
872 trias.push_back( face );
878 // add triangles to result map
879 SMDS_MeshFace* NewFace;
881 NewFace = meshDS->AddFace( FNodes[0], FNodes[1], FNodes[2] );
883 NewFace = meshDS->AddFace( FNodes[0], FNodes[2], FNodes[1] );
884 storeTmpElement( NewFace );
885 trias.push_back ( NewFace );
886 quads.push_back( face );
893 if(!isRev) VNorm.Reverse();
894 double xc = 0., yc = 0., zc = 0.;
899 Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i).Reversed());
901 Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i));
906 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
909 double height = PCbest.Distance(PC);
910 if ( height < 1.e-6 ) {
911 // create new PCbest using a bit shift along VNorm
912 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
915 // check possible intersection with other faces
916 if ( !LimitHeight( PCbest, PC, PN, FNodes, aMesh, face, /*UseApexRay=*/true, aShape ))
919 // create node for PCbest
920 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
922 // add triangles to result map
925 trias.push_back ( meshDS->AddFace( NewNode, FNodes[i], FNodes[i+1] ));
926 storeTmpElement( trias.back() );
929 if ( isRev ) swap( FNodes[1], FNodes[3]);
930 SMDS_MeshVolume* aPyram =
931 helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
932 myPyramids.push_back(aPyram);
934 quads.push_back( face );
941 } // end loop on elements on a face submesh
943 bool sourceSubMeshIsProxy = false;
946 // move proxy sub-mesh from other proxy mesh to this
947 sourceSubMeshIsProxy = takeProxySubMesh( aShapeFace, aProxyMesh );
948 // move also tmp elements added in mesh
949 takeTmpElemsInMesh( aProxyMesh );
953 SMESH_ProxyMesh::SubMesh* prxSubMesh = getProxySubMesh( aShapeFace );
954 prxSubMesh->ChangeElements( trias.begin(), trias.end() );
956 // delete tmp quadrangles removed from aProxyMesh
957 if ( sourceSubMeshIsProxy )
959 for ( unsigned i = 0; i < quads.size(); ++i )
960 removeTmpElement( quads[i] );
962 delete myElemSearcher;
964 SMESH_MeshAlgos::GetElementSearcher( *meshDS, aProxyMesh->GetFaces(aShape));
968 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
970 return Compute2ndPart(aMesh, myPyramids);
973 //================================================================================
975 * \brief Computes pyramids in mesh with no shape
977 //================================================================================
979 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
981 SMESH_ProxyMesh::setMesh( aMesh );
982 SMESH_ProxyMesh::_allowedTypes.push_back( SMDSEntity_Triangle );
983 SMESH_ProxyMesh::_allowedTypes.push_back( SMDSEntity_Quad_Triangle );
984 if ( aMesh.NbQuadrangles() < 1 )
987 // find if there is a group of faces identified as skin faces, with normal going outside the volume
988 std::string groupName = "skinFaces";
989 SMESHDS_GroupBase* groupDS = 0;
990 SMESH_Mesh::GroupIteratorPtr groupIt = aMesh.GetGroups();
991 while ( groupIt->more() )
994 SMESH_Group * group = groupIt->next();
995 if ( !group ) continue;
996 groupDS = group->GetGroupDS();
997 if ( !groupDS || groupDS->IsEmpty() )
1002 if (groupDS->GetType() != SMDSAbs_Face)
1007 std::string grpName = group->GetName();
1008 if (grpName == groupName)
1016 const bool toFindVolumes = aMesh.NbVolumes() > 0;
1018 vector<const SMDS_MeshElement*> myPyramids;
1019 SMESH_MesherHelper helper(aMesh);
1020 helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
1021 helper.SetElementsOnShape( true );
1023 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1024 SMESH_ProxyMesh::SubMesh* prxSubMesh = getProxySubMesh();
1026 if ( !myElemSearcher )
1027 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS );
1028 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
1029 SMESHUtils::Deleter<SMESH_ElementSearcher>
1030 volSearcher( SMESH_MeshAlgos::GetElementSearcher( *meshDS ));
1031 vector< const SMDS_MeshElement* > suspectFaces, foundVolumes;
1033 TColgp_Array1OfPnt PN(1,5);
1034 TColgp_Array1OfVec VN(1,4);
1035 vector<const SMDS_MeshNode*> FNodes(5);
1036 TColgp_SequenceOfPnt aContour;
1038 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator(/*idInceasingOrder=*/true);
1041 const SMDS_MeshElement* face = fIt->next();
1042 if ( !face ) continue;
1043 // retrieve needed information about a face
1046 const SMDS_MeshElement* volumes[2];
1047 int what = Preparation(face, PN, VN, FNodes, PC, VNorm, volumes);
1048 if ( what == NOT_QUAD )
1050 if ( volumes[0] && volumes[1] )
1051 continue; // face is shared by two volumes - no room for a pyramid
1053 if ( what == DEGEN_QUAD )
1056 // add a triangle to the proxy mesh
1057 SMDS_MeshFace* NewFace;
1059 // check orientation
1060 double tmp = PN(1).Distance(PN(2)) + PN(2).Distance(PN(3));
1061 // far points in VNorm direction
1062 gp_Pnt Ptmp1 = PC.XYZ() + VNorm.XYZ() * tmp * 1.e6;
1063 gp_Pnt Ptmp2 = PC.XYZ() - VNorm.XYZ() * tmp * 1.e6;
1064 // check intersection for Ptmp1 and Ptmp2
1068 double dist1 = RealLast();
1069 double dist2 = RealLast();
1072 gp_Ax1 line( PC, VNorm );
1073 vector< const SMDS_MeshElement* > suspectFaces;
1074 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces);
1076 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF ) {
1077 const SMDS_MeshElement* F = suspectFaces[iF];
1078 if ( F == face ) continue;
1080 for ( int i = 0; i < 4; ++i )
1081 aContour.Append( SMESH_TNodeXYZ( F->GetNode(i) ));
1083 if ( !volumes[0] && HasIntersection( Ptmp1, PC, PPP, aContour )) {
1085 double tmp = PC.Distance(PPP);
1086 if ( tmp < dist1 ) {
1091 if ( !volumes[1] && HasIntersection( Ptmp2, PC, PPP, aContour )) {
1093 double tmp = PC.Distance(PPP);
1094 if ( tmp < dist2 ) {
1101 if( IsOK1 && !IsOK2 ) {
1102 // using existed direction
1104 else if( !IsOK1 && IsOK2 ) {
1105 // using opposite direction
1108 else { // IsOK1 && IsOK2
1109 double tmp1 = PC.Distance(Pres1);
1110 double tmp2 = PC.Distance(Pres2);
1112 // using existed direction
1115 // using opposite direction
1120 NewFace = meshDS->AddFace( FNodes[0], FNodes[1], FNodes[2] );
1122 NewFace = meshDS->AddFace( FNodes[0], FNodes[2], FNodes[1] );
1123 storeTmpElement( NewFace );
1124 prxSubMesh->AddElement( NewFace );
1128 // -----------------------------------
1129 // Case of non-degenerated quadrangle
1130 // -----------------------------------
1132 // Find pyramid peak
1134 gp_XYZ PCbest(0., 0., 0.); // pyramid peak
1136 for ( ; i <= 4; i++ ) {
1137 gp_Pnt Pbest = FindBestPoint(PN(i), PN(i+1), PC, VN(i));
1138 PCbest += Pbest.XYZ();
1142 double height = PC.Distance(PCbest); // pyramid height to precise
1143 if ( height < 1.e-6 ) {
1144 // create new PCbest using a bit shift along VNorm
1145 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
1146 height = PC.Distance(PCbest);
1147 if ( height < std::numeric_limits<double>::min() )
1148 return false; // batterfly element
1151 // Restrict pyramid height by intersection with other faces
1153 gp_Vec tmpDir(PC,PCbest); tmpDir.Normalize();
1154 double tmp = PN(1).Distance(PN(3)) + PN(2).Distance(PN(4));
1155 // far points: in (PC, PCbest) direction and vice-versa
1156 gp_Pnt farPnt[2] = { PC.XYZ() + tmpDir.XYZ() * tmp * 1.e6,
1157 PC.XYZ() - tmpDir.XYZ() * tmp * 1.e6 };
1158 // check intersection for farPnt1 and farPnt2
1159 bool intersected[2] = { false, false };
1160 double dist2int [2] = { RealLast(), RealLast() };
1162 int intFaceInd [2] = { 0, 0 };
1164 if ( toFindVolumes && 0 ) // non-conformal mesh is not suitable for any mesher so far
1166 // there are volumes in the mesh, in a non-conformal mesh an neighbor
1167 // volume can be not found yet
1168 for ( int isRev = 0; isRev < 2; ++isRev )
1170 if ( volumes[isRev] ) continue;
1171 gp_Pnt testPnt = PC.XYZ() + tmpDir.XYZ() * height * ( isRev ? -0.1: 0.1 );
1172 foundVolumes.clear();
1173 if ( volSearcher->FindElementsByPoint( testPnt, SMDSAbs_Volume, foundVolumes ))
1174 volumes[isRev] = foundVolumes[0];
1176 if ( volumes[0] && volumes[1] )
1177 continue; // no room for a pyramid
1180 gp_Ax1 line( PC, tmpDir );
1181 suspectFaces.clear();
1182 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectFaces);
1184 for ( size_t iF = 0; iF < suspectFaces.size(); ++iF )
1186 const SMDS_MeshElement* F = suspectFaces[iF];
1187 if ( F == face ) continue;
1189 int nbN = F->NbCornerNodes();
1190 for ( i = 0; i < nbN; ++i )
1191 aContour.Append( SMESH_TNodeXYZ( F->GetNode(i) ));
1193 for ( int isRev = 0; isRev < 2; ++isRev )
1195 if( !volumes[isRev] && HasIntersection(farPnt[isRev], PC, intP, aContour) )
1197 double d = PC.Distance( intP );
1198 if ( d < dist2int[isRev] )
1200 intersected[isRev] = true;
1201 intPnt [isRev] = intP;
1202 dist2int [isRev] = d;
1203 intFaceInd [isRev] = iF;
1209 // if the face belong to the group of skinFaces, do not build a pyramid outside
1210 if ( groupDS && groupDS->Contains(face) )
1212 intersected[0] = false;
1214 else if ( intersected[0] && intersected[1] ) // check if one of pyramids is in a hole
1216 gp_Pnt P ( PC.XYZ() + tmpDir.XYZ() * 0.5 * dist2int[0] );
1217 if ( searcher->GetPointState( P ) == TopAbs_OUT )
1218 intersected[0] = false;
1221 P = ( PC.XYZ() - tmpDir.XYZ() * 0.5 * dist2int[1] );
1222 if ( searcher->GetPointState( P ) == TopAbs_OUT )
1223 intersected[1] = false;
1227 // Create one or two pyramids
1229 for ( int isRev = 0; isRev < 2; ++isRev )
1231 if ( !intersected[isRev] ) continue;
1232 double pyramidH = Min( height, dist2int[isRev]/3. );
1233 gp_Pnt Papex = PC.XYZ() + tmpDir.XYZ() * (isRev ? -pyramidH : pyramidH);
1234 if ( pyramidH < 1e-2 * height )
1235 return overlapError( aMesh, face, suspectFaces[ intFaceInd[isRev] ] );
1237 if ( !LimitHeight( Papex, PC, PN, FNodes, aMesh, face, /*UseApexRay=*/false ))
1240 // create node for Papex
1241 SMDS_MeshNode* NewNode = helper.AddNode( Papex.X(), Papex.Y(), Papex.Z() );
1243 // add triangles to result map
1244 for ( i = 0; i < 4; i++) {
1245 SMDS_MeshFace* NewFace;
1247 NewFace = meshDS->AddFace( NewNode, FNodes[i], FNodes[i+1] );
1249 NewFace = meshDS->AddFace( NewNode, FNodes[i+1], FNodes[i] );
1250 storeTmpElement( NewFace );
1251 prxSubMesh->AddElement( NewFace );
1254 SMDS_MeshVolume* aPyram;
1256 aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
1258 aPyram = helper.AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
1259 myPyramids.push_back(aPyram);
1261 } // end loop on all faces
1263 return Compute2ndPart(aMesh, myPyramids);
1266 //================================================================================
1268 * \brief Update created pyramids and faces to avoid their intersection
1270 //================================================================================
1272 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh,
1273 const vector<const SMDS_MeshElement*>& myPyramids)
1275 if ( myPyramids.empty() )
1278 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1280 //int myShapeID = myPyramids[0]->GetNode(4)->getshapeId();
1282 SMDS_ElemIteratorPtr
1283 pyramIt( new SMDS_ElementVectorIterator( myPyramids.begin(), myPyramids.end() ));
1284 if ( myElemSearcher ) delete myElemSearcher;
1285 myElemSearcher = SMESH_MeshAlgos::GetElementSearcher( *meshDS, pyramIt );
1287 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>( myElemSearcher );
1289 set<const SMDS_MeshNode*> nodesToMove;
1291 // check adjacent pyramids
1293 for ( i = 0; i < myPyramids.size(); ++i )
1295 const SMDS_MeshElement* PrmI = myPyramids[i];
1296 MergeAdjacent( PrmI, nodesToMove );
1299 // iterate on all new pyramids
1300 vector< const SMDS_MeshElement* > suspectPyrams;
1301 for ( i = 0; i < myPyramids.size(); ++i )
1303 const SMDS_MeshElement* PrmI = myPyramids[i];
1304 const SMDS_MeshNode* apexI = PrmI->GetNode( PYRAM_APEX );
1306 // compare PrmI with all the rest pyramids
1308 // collect adjacent pyramids and nodes coordinates of PrmI
1309 set<const SMDS_MeshElement*> checkedPyrams;
1311 for ( k = 0; k < 5; k++ )
1313 const SMDS_MeshNode* n = PrmI->GetNode(k);
1314 PsI[k] = SMESH_TNodeXYZ( n );
1315 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
1316 while ( vIt->more() )
1318 const SMDS_MeshElement* PrmJ = vIt->next();
1319 if ( SMESH_MeshAlgos::GetCommonNodes( PrmI, PrmJ ).size() > 1 )
1320 checkedPyrams.insert( PrmJ );
1324 // get pyramids to check
1325 gp_XYZ PC = ( PsI[0].XYZ() + PsI[1].XYZ() + PsI[2].XYZ() + PsI[3].XYZ() ) / 4.;
1326 gp_XYZ ray = PsI[4].XYZ() - PC;
1327 gp_XYZ center = PC + 0.5 * ray;
1328 double diameter = Max( PsI[0].Distance(PsI[2]), PsI[1].Distance(PsI[3]));
1329 suspectPyrams.clear();
1330 searcher->GetElementsInSphere( center, diameter * 0.6, SMDSAbs_Volume, suspectPyrams);
1332 // check intersection with distant pyramids
1333 for ( j = 0; j < suspectPyrams.size(); ++j )
1335 const SMDS_MeshElement* PrmJ = suspectPyrams[j];
1338 if ( apexI == PrmJ->GetNode( PYRAM_APEX ))
1339 continue; // pyramids PrmI and PrmJ already merged
1340 if ( !checkedPyrams.insert( PrmJ ).second )
1341 continue; // already checked
1344 for ( k = 0; k < 5; k++ )
1345 PsJ[k] = SMESH_TNodeXYZ( PrmJ->GetNode(k) );
1347 if ( ray * ( PsJ[4].XYZ() - PC ) < 0. )
1348 continue; // PrmJ is below PrmI
1350 for ( k = 0; k < 4; k++ ) // loop on 4 base nodes of PrmI
1354 for ( k = 0; k < 4 && !hasInt; k++ )
1356 gp_Vec Vtmp( PsI[k], PsI[ PYRAM_APEX ]);
1357 gp_Pnt Pshift = PsI[k].XYZ() + Vtmp.XYZ() * 0.01; // base node moved a bit to apex
1359 ( HasIntersection3( Pshift, PsI[4], Pint, PsJ[0], PsJ[1], PsJ[PYRAM_APEX]) ||
1360 HasIntersection3( Pshift, PsI[4], Pint, PsJ[1], PsJ[2], PsJ[PYRAM_APEX]) ||
1361 HasIntersection3( Pshift, PsI[4], Pint, PsJ[2], PsJ[3], PsJ[PYRAM_APEX]) ||
1362 HasIntersection3( Pshift, PsI[4], Pint, PsJ[3], PsJ[0], PsJ[PYRAM_APEX]) );
1364 for ( k = 0; k < 4 && !hasInt; k++ )
1366 gp_Vec Vtmp( PsJ[k], PsJ[ PYRAM_APEX ]);
1367 gp_Pnt Pshift = PsJ[k].XYZ() + Vtmp.XYZ() * 0.01;
1369 ( HasIntersection3( Pshift, PsJ[4], Pint, PsI[0], PsI[1], PsI[PYRAM_APEX]) ||
1370 HasIntersection3( Pshift, PsJ[4], Pint, PsI[1], PsI[2], PsI[PYRAM_APEX]) ||
1371 HasIntersection3( Pshift, PsJ[4], Pint, PsI[2], PsI[3], PsI[PYRAM_APEX]) ||
1372 HasIntersection3( Pshift, PsJ[4], Pint, PsI[3], PsI[0], PsI[PYRAM_APEX]) );
1377 // count common nodes of base faces of two pyramids
1379 for ( k = 0; k < 4; k++ )
1380 nbc += int ( PrmI->GetNodeIndex( PrmJ->GetNode(k) ) >= 0 );
1383 continue; // pyrams have a common base face
1387 // Merge the two pyramids and others already merged with them
1388 MergePiramids( PrmI, PrmJ, nodesToMove );
1392 // decrease height of pyramids
1393 gp_XYZ PCi(0,0,0), PCj(0,0,0);
1394 for ( k = 0; k < 4; k++ ) {
1395 PCi += PsI[k].XYZ();
1396 PCj += PsJ[k].XYZ();
1399 gp_Vec VN1(PCi,PsI[4]);
1400 gp_Vec VN2(PCj,PsJ[4]);
1401 gp_Vec VI1(PCi,Pint);
1402 gp_Vec VI2(PCj,Pint);
1403 double ang1 = fabs(VN1.Angle(VI1));
1404 double ang2 = fabs(VN2.Angle(VI2));
1405 double coef1 = 0.5 - (( ang1 < M_PI/3. ) ? cos(ang1)*0.25 : 0 );
1406 double coef2 = 0.5 - (( ang2 < M_PI/3. ) ? cos(ang2)*0.25 : 0 ); // cos(ang2) ?
1407 // double coef2 = 0.5;
1409 // coef2 -= cos(ang1)*0.25;
1413 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>( apexI );
1414 aNode1->setXYZ( PCi.X()+VN1.X(), PCi.Y()+VN1.Y(), PCi.Z()+VN1.Z() );
1415 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(PrmJ->GetNode( PYRAM_APEX ));
1416 aNode2->setXYZ( PCj.X()+VN2.X(), PCj.Y()+VN2.Y(), PCj.Z()+VN2.Z() );
1417 nodesToMove.insert( aNode1 );
1418 nodesToMove.insert( aNode2 );
1420 // fix intersections that can appear after apex movement
1421 MergeAdjacent( PrmI, nodesToMove );
1422 MergeAdjacent( PrmJ, nodesToMove );
1425 } // loop on suspectPyrams
1426 } // loop on 4 base nodes of PrmI
1428 } // loop on all pyramids
1430 if( !nodesToMove.empty() && !meshDS->IsEmbeddedMode() )
1432 set<const SMDS_MeshNode*>::iterator n = nodesToMove.begin();
1433 for ( ; n != nodesToMove.end(); ++n )
1434 meshDS->MoveNode( *n, (*n)->X(), (*n)->Y(), (*n)->Z() );
1437 // move medium nodes of merged quadratic pyramids
1438 if ( myPyramids[0]->IsQuadratic() )
1439 UpdateQuadraticPyramids( nodesToMove, GetMeshDS() );
1441 // erase removed triangles from the proxy mesh
1442 if ( !myRemovedTrias.empty() )
1444 for ( int i = 0; i <= meshDS->MaxShapeIndex(); ++i )
1445 if ( SMESH_ProxyMesh::SubMesh* sm = findProxySubMesh(i))
1447 vector<const SMDS_MeshElement *> faces;
1448 faces.reserve( sm->NbElements() );
1449 SMDS_ElemIteratorPtr fIt = sm->GetElements();
1450 while ( fIt->more() )
1452 const SMDS_MeshElement* tria = fIt->next();
1453 set<const SMDS_MeshElement*>::iterator rmTria = myRemovedTrias.find( tria );
1454 if ( rmTria != myRemovedTrias.end() )
1455 myRemovedTrias.erase( rmTria );
1457 faces.push_back( tria );
1459 sm->ChangeElements( faces.begin(), faces.end() );
1465 delete myElemSearcher;