1 // Copyright (C) 2007-2010 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.
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 // SMESH SMESH : implementaion of SMESH idl descriptions
21 // File : StdMeshers_QuadToTriaAdaptor.cxx
23 // Created : Wen May 07 16:37:07 2008
24 // Author : Sergey KUUL (skl)
26 #include "StdMeshers_QuadToTriaAdaptor.hxx"
28 #include "SMDS_SetIterator.hxx"
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_MesherHelper.hxx"
33 #include <IntAna_IntConicQuad.hxx>
34 #include <IntAna_Quadric.hxx>
35 #include <TColgp_HArray1OfPnt.hxx>
36 #include <TColgp_HArray1OfVec.hxx>
37 #include <TColgp_HSequenceOfPnt.hxx>
38 #include <TopExp_Explorer.hxx>
47 enum EQuadNature { NOT_QUAD, QUAD, DEGEN_QUAD };
49 // std-like iterator used to get coordinates of nodes of mesh element
50 typedef SMDS_StdIterator< SMESH_MeshEditor::TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
55 //================================================================================
57 * \brief Return true if two nodes of triangles are equal
59 //================================================================================
61 bool EqualTriangles(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
64 ( F1->GetNode(1)==F2->GetNode(2) && F1->GetNode(2)==F2->GetNode(1) ) ||
65 ( F1->GetNode(1)==F2->GetNode(1) && F1->GetNode(2)==F2->GetNode(2) );
68 //================================================================================
70 * \brief Merge the two pyramids (i.e. fuse their apex) and others already merged with them
72 //================================================================================
74 void MergePyramids( const SMDS_MeshElement* PrmI,
75 const SMDS_MeshElement* PrmJ,
78 set<const SMDS_MeshNode*> & nodesToMove)
80 const SMDS_MeshNode* Nrem = PrmJ->GetNode(4); // node to remove
81 int nbJ = Nrem->NbInverseElements( SMDSAbs_Volume );
82 SMESH_MeshEditor::TNodeXYZ Pj( Nrem );
84 // an apex node to make common to all merged pyramids
85 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
86 if ( CommonNode == Nrem ) return; // already merged
87 int nbI = CommonNode->NbInverseElements( SMDSAbs_Volume );
88 //cerr << __LINE__ << " " << nbI << " " << nbJ << endl;
89 SMESH_MeshEditor::TNodeXYZ Pi( CommonNode );
90 gp_XYZ Pnew = ( nbI*Pi + nbJ*Pj ) / (nbI+nbJ);
91 CommonNode->setXYZ( Pnew.X(), Pnew.Y(), Pnew.Z() );
93 nodesToMove.insert( CommonNode );
94 nodesToMove.erase ( Nrem );
96 // find and remove coincided faces of merged pyramids
97 SMDS_ElemIteratorPtr triItI = CommonNode->GetInverseElementIterator(SMDSAbs_Face);
98 while ( triItI->more() )
100 const SMDS_MeshElement* FI = triItI->next();
101 const SMDS_MeshElement* FJEqual = 0;
102 SMDS_ElemIteratorPtr triItJ = Nrem->GetInverseElementIterator(SMDSAbs_Face);
103 while ( !FJEqual && triItJ->more() )
105 const SMDS_MeshElement* FJ = triItJ->next();
106 if ( EqualTriangles( FJ, FI ))
111 //meshDS->RemoveFreeElement(FI, 0, false);
112 //meshDS->RemoveFreeElement(FJEqual, 0, false);
113 tempTrias[FI] = false;
114 tempTrias[FJEqual] = false;
118 // set the common apex node to pyramids and triangles merged with J
119 //cerr << __LINE__ << " NbInverseElements " << Nrem->NbInverseElements() << endl;
120 SMDS_ElemIteratorPtr itJ = Nrem->GetInverseElementIterator();
121 while ( itJ->more() )
123 const SMDS_MeshElement* elem = itJ->next();
124 if ( elem->GetType() == SMDSAbs_Volume ) // pyramid
126 vector< const SMDS_MeshNode* > nodes( elem->begin_nodes(), elem->end_nodes() );
127 //cerr << __LINE__ << " volId " << elem->GetID() << " nbNodes " << nodes.size() << endl;
128 nodes[4] = CommonNode;
129 MESSAGE("ChangeElementNodes");
130 meshDS->ChangeElementNodes( elem, &nodes[0], nodes.size());
132 else if ( tempTrias.count(elem) ) // tmp triangles
134 //cerr << __LINE__ << " triaId " << elem->GetID() << endl;
135 ((SMDS_VtkFace*) elem )->ChangeApex( CommonNode );
139 // cerr << __LINE__ << " other " << elem->GetVtkType() << endl;
142 //cerr << __LINE__ << " NbInverseElements " << Nrem->NbInverseElements() << endl;
143 ASSERT( Nrem->NbInverseElements() == 0 );
144 meshDS->RemoveFreeNode( Nrem,
145 meshDS->MeshElements( Nrem->getshapeId()),
146 /*fromGroups=*/false);
149 //================================================================================
151 * \brief Return true if two adjacent pyramids are too close one to another
152 * so that a tetrahedron to built between them would have too poor quality
154 //================================================================================
156 bool TooCloseAdjacent( const SMDS_MeshElement* PrmI,
157 const SMDS_MeshElement* PrmJ,
160 const SMDS_MeshNode* nApexI = PrmI->GetNode(4);
161 const SMDS_MeshNode* nApexJ = PrmJ->GetNode(4);
162 if ( nApexI == nApexJ ||
163 nApexI->getshapeId() != nApexJ->getshapeId() )
166 // Find two common base nodes and their indices within PrmI and PrmJ
167 const SMDS_MeshNode* baseNodes[2] = { 0,0 };
168 int baseNodesIndI[2], baseNodesIndJ[2];
169 for ( int i = 0; i < 4 ; ++i )
171 int j = PrmJ->GetNodeIndex( PrmI->GetNode(i));
174 int ind = baseNodes[0] ? 1:0;
175 if ( baseNodes[ ind ])
176 return false; // pyramids with a common base face
177 baseNodes [ ind ] = PrmI->GetNode(i);
178 baseNodesIndI[ ind ] = i;
179 baseNodesIndJ[ ind ] = j;
182 if ( !baseNodes[1] ) return false; // not adjacent
184 // Get normals of triangles sharing baseNodes
185 gp_XYZ apexI = SMESH_MeshEditor::TNodeXYZ( nApexI );
186 gp_XYZ apexJ = SMESH_MeshEditor::TNodeXYZ( nApexJ );
187 gp_XYZ base1 = SMESH_MeshEditor::TNodeXYZ( baseNodes[0]);
188 gp_XYZ base2 = SMESH_MeshEditor::TNodeXYZ( baseNodes[1]);
189 gp_Vec baseVec( base1, base2 );
190 gp_Vec baI( base1, apexI );
191 gp_Vec baJ( base1, apexJ );
192 gp_Vec nI = baseVec.Crossed( baI );
193 gp_Vec nJ = baseVec.Crossed( baJ );
195 // Check angle between normals
196 double angle = nI.Angle( nJ );
197 bool tooClose = ( angle < 15 * PI180 );
199 // Check if pyramids collide
201 if ( !tooClose && baI * baJ > 0 )
203 // find out if nI points outside of PrmI or inside
204 int dInd = baseNodesIndI[1] - baseNodesIndI[0];
205 isOutI = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
207 // find out sign of projection of nJ to baI
208 double proj = baI * nJ;
210 tooClose = isOutI ? proj > 0 : proj < 0;
213 // Check if PrmI and PrmJ are in same domain
214 if ( tooClose && !hasShape )
216 // check order of baseNodes within pyramids, it must be opposite
217 int dInd = baseNodesIndJ[1] - baseNodesIndJ[0];
218 isOutJ = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
219 if ( isOutJ == isOutI )
220 return false; // other domain
222 // check absence of a face separating domains between pyramids
223 TIDSortedElemSet emptySet, avoidSet;
225 while ( const SMDS_MeshElement* f =
226 SMESH_MeshEditor::FindFaceInSet( baseNodes[0], baseNodes[1],
227 emptySet, avoidSet, &i1, &i2 ))
229 avoidSet.insert( f );
231 // face node other than baseNodes
232 int otherNodeInd = 0;
233 while ( otherNodeInd == i1 || otherNodeInd == i2 ) otherNodeInd++;
234 const SMDS_MeshNode* otherFaceNode = f->GetNode( otherNodeInd );
236 // check if f is a base face of either of pyramids
237 if ( f->NbCornerNodes() == 4 &&
238 ( PrmI->GetNodeIndex( otherFaceNode ) >= 0 ||
239 PrmJ->GetNodeIndex( otherFaceNode ) >= 0 ))
240 continue; // f is a base quadrangle
242 // check projections of face direction (baOFN) to triange normals (nI and nJ)
243 gp_Vec baOFN( base1, SMESH_MeshEditor::TNodeXYZ( otherFaceNode ));
244 ( isOutI ? nJ : nI ).Reverse();
245 if ( nI * baOFN > 0 && nJ * baOFN > 0 )
247 tooClose = false; // f is between pyramids
256 //================================================================================
258 * \brief Merges adjacent pyramids
260 //================================================================================
262 void MergeAdjacent(const SMDS_MeshElement* PrmI,
264 TRemTrias & tempTrias,
265 set<const SMDS_MeshNode*>& nodesToMove)
267 TIDSortedElemSet adjacentPyrams, mergedPyrams;
268 for(int k=0; k<4; k++) // loop on 4 base nodes of PrmI
270 const SMDS_MeshNode* n = PrmI->GetNode(k);
271 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
272 while ( vIt->more() )
274 const SMDS_MeshElement* PrmJ = vIt->next();
275 if ( PrmJ->NbCornerNodes() != 5 || !adjacentPyrams.insert( PrmJ ).second )
277 if ( PrmI != PrmJ && TooCloseAdjacent( PrmI, PrmJ, mesh.HasShapeToMesh() ))
279 MergePyramids( PrmI, PrmJ, mesh.GetMeshDS(), tempTrias, nodesToMove );
280 mergedPyrams.insert( PrmJ );
284 if ( !mergedPyrams.empty() )
286 TIDSortedElemSet::iterator prm;
287 // for (prm = mergedPyrams.begin(); prm != mergedPyrams.end(); ++prm)
288 // MergeAdjacent( *prm, mesh, tempTrias, nodesToMove );
290 for (prm = adjacentPyrams.begin(); prm != adjacentPyrams.end(); ++prm)
291 MergeAdjacent( *prm, mesh, tempTrias, nodesToMove );
296 //================================================================================
300 //================================================================================
302 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor():
303 myElemSearcher(0), myNbTriangles(0)
307 //================================================================================
311 //================================================================================
313 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
315 // delete temporary faces
316 TQuad2Trias::iterator f_f = myResMap.begin(), ffEnd = myResMap.end();
317 for ( ; f_f != ffEnd; ++f_f )
319 TTriaList& fList = f_f->second;
320 TTriaList::iterator f = fList.begin(), fEnd = fList.end();
321 for ( ; f != fEnd; ++f )
323 const SMDS_MeshElement *elem = *f;
324 SMDS_Mesh::_meshList[elem->getMeshId()]->RemoveFreeElement(elem);
329 if ( myElemSearcher ) delete myElemSearcher;
334 //=======================================================================
335 //function : FindBestPoint
336 //purpose : Return a point P laying on the line (PC,V) so that triangle
337 // (P, P1, P2) to be equilateral as much as possible
338 // V - normal to (P1,P2,PC)
339 //=======================================================================
340 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
341 const gp_Pnt& PC, const gp_Vec& V)
343 double a = P1.Distance(P2);
344 double b = P1.Distance(PC);
345 double c = P2.Distance(PC);
349 // find shift along V in order a to became equal to (b+c)/2
350 double shift = sqrt( a*a + (b*b-c*c)*(b*b-c*c)/16/a/a - (b*b+c*c)/2 );
352 gp_Pnt Pbest = PC.XYZ() + aDir.XYZ() * shift;
358 //=======================================================================
359 //function : HasIntersection3
360 //purpose : Auxilare for HasIntersection()
361 // find intersection point between triangle (P1,P2,P3)
362 // and segment [PC,P]
363 //=======================================================================
364 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
365 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
367 //cout<<"HasIntersection3"<<endl;
368 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
369 //cout<<" P("<<P.X()<<","<<P.Y()<<","<<P.Z()<<")"<<endl;
370 //cout<<" P1("<<P1.X()<<","<<P1.Y()<<","<<P1.Z()<<")"<<endl;
371 //cout<<" P2("<<P2.X()<<","<<P2.Y()<<","<<P2.Z()<<")"<<endl;
372 //cout<<" P3("<<P3.X()<<","<<P3.Y()<<","<<P3.Z()<<")"<<endl;
375 IntAna_Quadric IAQ(gp_Pln(P1,VP1.Crossed(VP2)));
376 IntAna_IntConicQuad IAICQ(gp_Lin(PC,gp_Dir(gp_Vec(PC,P))),IAQ);
378 if( IAICQ.IsInQuadric() )
380 if( IAICQ.NbPoints() == 1 ) {
381 gp_Pnt PIn = IAICQ.Point(1);
382 const double preci = 1.e-10 * P.Distance(PC);
383 // check if this point is internal for segment [PC,P]
385 ( (PC.X()-PIn.X())*(P.X()-PIn.X()) > preci ) ||
386 ( (PC.Y()-PIn.Y())*(P.Y()-PIn.Y()) > preci ) ||
387 ( (PC.Z()-PIn.Z())*(P.Z()-PIn.Z()) > preci );
391 // check if this point is internal for triangle (P1,P2,P3)
395 if( V1.Magnitude()<preci ||
396 V2.Magnitude()<preci ||
397 V3.Magnitude()<preci ) {
401 const double angularTol = 1e-6;
402 gp_Vec VC1 = V1.Crossed(V2);
403 gp_Vec VC2 = V2.Crossed(V3);
404 gp_Vec VC3 = V3.Crossed(V1);
405 if(VC1.Magnitude()<gp::Resolution()) {
406 if(VC2.IsOpposite(VC3,angularTol)) {
410 else if(VC2.Magnitude()<gp::Resolution()) {
411 if(VC1.IsOpposite(VC3,angularTol)) {
415 else if(VC3.Magnitude()<gp::Resolution()) {
416 if(VC1.IsOpposite(VC2,angularTol)) {
421 if( VC1.IsOpposite(VC2,angularTol) || VC1.IsOpposite(VC3,angularTol) ||
422 VC2.IsOpposite(VC3,angularTol) ) {
435 //=======================================================================
436 //function : HasIntersection
437 //purpose : Auxilare for CheckIntersection()
438 //=======================================================================
440 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
441 Handle(TColgp_HSequenceOfPnt)& aContour)
443 if(aContour->Length()==3) {
444 return HasIntersection3( P, PC, Pint, aContour->Value(1),
445 aContour->Value(2), aContour->Value(3) );
449 if( (aContour->Value(1).Distance(aContour->Value(2)) > 1.e-6) &&
450 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
451 (aContour->Value(2).Distance(aContour->Value(3)) > 1.e-6) ) {
452 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
453 aContour->Value(2), aContour->Value(3) );
455 if(check) return true;
456 if( (aContour->Value(1).Distance(aContour->Value(4)) > 1.e-6) &&
457 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
458 (aContour->Value(4).Distance(aContour->Value(3)) > 1.e-6) ) {
459 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
460 aContour->Value(3), aContour->Value(4) );
462 if(check) return true;
468 //================================================================================
470 * \brief Checks if a line segment (P,PC) intersects any mesh face.
471 * \param P - first segment end
472 * \param PC - second segment end (it is a gravity center of quadrangle)
473 * \param Pint - (out) intersection point
474 * \param aMesh - mesh
475 * \param aShape - shape to check faces on
476 * \param NotCheckedFace - mesh face not to check
477 * \retval bool - true if there is an intersection
479 //================================================================================
481 bool StdMeshers_QuadToTriaAdaptor::CheckIntersection (const gp_Pnt& P,
485 const TopoDS_Shape& aShape,
486 const SMDS_MeshElement* NotCheckedFace)
488 if ( !myElemSearcher )
489 myElemSearcher = SMESH_MeshEditor(&aMesh).GetElementSearcher();
490 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
492 //SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
493 //cout<<" CheckIntersection: meshDS->NbFaces() = "<<meshDS->NbFaces()<<endl;
495 double dist = RealLast(); // find intersection closest to the segment
498 gp_Ax1 line( P, gp_Vec(P,PC));
499 vector< const SMDS_MeshElement* > suspectElems;
500 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectElems);
502 // for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
503 // const TopoDS_Shape& aShapeFace = exp.Current();
504 // if(aShapeFace==NotCheckedFace)
506 // const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements(aShapeFace);
507 // if ( aSubMeshDSFace ) {
508 // SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
509 // while ( iteratorElem->more() ) { // loop on elements on a face
510 // const SMDS_MeshElement* face = iteratorElem->next();
511 for ( int i = 0; i < suspectElems.size(); ++i )
513 const SMDS_MeshElement* face = suspectElems[i];
514 if ( face == NotCheckedFace ) continue;
515 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
516 for ( int i = 0; i < face->NbCornerNodes(); ++i )
517 aContour->Append( SMESH_MeshEditor::TNodeXYZ( face->GetNode(i) ));
518 if( HasIntersection(P, PC, Pres, aContour) ) {
520 double tmp = PC.Distance(Pres);
530 //================================================================================
532 * \brief Prepare data for the given face
533 * \param PN - coordinates of face nodes
534 * \param VN - cross products of vectors (PC-PN(i)) ^ (PC-PN(i+1))
535 * \param FNodes - face nodes
536 * \param PC - gravity center of nodes
537 * \param VNorm - face normal (sum of VN)
538 * \param volumes - two volumes sharing the given face, the first is in VNorm direction
539 * \retval int - 0 if given face is not quad,
540 * 1 if given face is quad,
541 * 2 if given face is degenerate quad (two nodes are coincided)
543 //================================================================================
545 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
546 Handle(TColgp_HArray1OfPnt)& PN,
547 Handle(TColgp_HArray1OfVec)& VN,
548 vector<const SMDS_MeshNode*>& FNodes,
551 const SMDS_MeshElement** volumes)
553 if( face->NbCornerNodes() != 4 )
555 myNbTriangles += int( face->NbCornerNodes() == 3 );
560 gp_XYZ xyzC(0., 0., 0.);
561 for ( i = 0; i < 4; ++i )
563 gp_XYZ p = SMESH_MeshEditor::TNodeXYZ( FNodes[i] = face->GetNode(i) );
564 PN->SetValue( i+1, p );
568 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
576 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
581 //int deg_num = IsDegenarate(PN);
585 //cout<<"find degeneration"<<endl;
587 gp_Pnt Pdeg = PN->Value(i);
589 list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
590 const SMDS_MeshNode* DegNode = 0;
591 for(; itdg!=myDegNodes.end(); itdg++) {
592 const SMDS_MeshNode* N = (*itdg);
593 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
594 if(Pdeg.Distance(Ptmp)<1.e-6) {
596 //DegNode = const_cast<SMDS_MeshNode*>(N);
601 DegNode = FNodes[i-1];
602 myDegNodes.push_back(DegNode);
605 FNodes[i-1] = DegNode;
608 PN->SetValue(i,PN->Value(i+1));
609 FNodes[i-1] = FNodes[i];
614 PN->SetValue(nbp+1,PN->Value(1));
615 FNodes[nbp] = FNodes[0];
616 // find normal direction
617 gp_Vec V1(PC,PN->Value(nbp));
618 gp_Vec V2(PC,PN->Value(1));
619 VNorm = V1.Crossed(V2);
620 VN->SetValue(nbp,VNorm);
621 for(i=1; i<nbp; i++) {
622 V1 = gp_Vec(PC,PN->Value(i));
623 V2 = gp_Vec(PC,PN->Value(i+1));
624 gp_Vec Vtmp = V1.Crossed(V2);
625 VN->SetValue(i,Vtmp);
629 // find volumes sharing the face
632 volumes[0] = volumes[1] = 0;
633 SMDS_ElemIteratorPtr vIt = FNodes[0]->GetInverseElementIterator( SMDSAbs_Volume );
634 while ( vIt->more() )
636 const SMDS_MeshElement* vol = vIt->next();
637 bool volSharesAllNodes = true;
638 for ( int i = 1; i < face->NbNodes() && volSharesAllNodes; ++i )
639 volSharesAllNodes = ( vol->GetNodeIndex( FNodes[i] ) >= 0 );
640 if ( volSharesAllNodes )
641 volumes[ volumes[0] ? 1 : 0 ] = vol;
642 // we could additionally check that vol has all FNodes in its one face using SMDS_VolumeTool
644 // define volume position relating to the face normal
648 SMDS_ElemIteratorPtr nodeIt = volumes[0]->nodesIterator();
650 volGC = accumulate( TXyzIterator(nodeIt), TXyzIterator(), volGC ) / volumes[0]->NbNodes();
652 if ( VNorm * gp_Vec( PC, volGC ) < 0 )
653 swap( volumes[0], volumes[1] );
657 //cout<<" VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
658 return hasdeg ? DEGEN_QUAD : QUAD;
662 //=======================================================================
665 //=======================================================================
667 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
674 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
675 SMESH_MesherHelper helper(aMesh);
676 helper.IsQuadraticSubMesh(aShape);
677 helper.SetElementsOnShape( true );
679 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next())
681 const TopoDS_Shape& aShapeFace = exp.Current();
682 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
683 if ( aSubMeshDSFace )
685 bool isRev = SMESH_Algo::IsReversedSubMesh( TopoDS::Face(aShapeFace), meshDS );
687 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
688 while ( iteratorElem->more() ) // loop on elements on a geometrical face
690 const SMDS_MeshElement* face = iteratorElem->next();
691 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
692 // preparation step using face info
693 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
694 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
695 vector<const SMDS_MeshNode*> FNodes(5);
698 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
705 // add triangles to result map
706 SMDS_MeshFace* NewFace;
708 NewFace = meshDS->AddFace( FNodes[0], FNodes[1], FNodes[2] );
710 NewFace = meshDS->AddFace( FNodes[0], FNodes[2], FNodes[1] );
711 myTempTriangles[NewFace] =true;
712 TTriaList aList( 1, NewFace );
713 myResMap.insert(make_pair(face,aList));
717 if(!isRev) VNorm.Reverse();
718 double xc = 0., yc = 0., zc = 0.;
723 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i).Reversed());
725 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
730 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
733 double height = PCbest.Distance(PC);
735 // create new PCbest using a bit shift along VNorm
736 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
739 // check possible intersection with other faces
741 bool check = CheckIntersection(PCbest, PC, Pint, aMesh, aShape, face);
743 //cout<<"--PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
744 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
745 double dist = PC.Distance(Pint)/3.;
746 gp_Dir aDir(gp_Vec(PC,PCbest));
747 PCbest = PC.XYZ() + aDir.XYZ() * dist;
750 gp_Vec VB(PC,PCbest);
751 gp_Pnt PCbestTmp = PC.XYZ() + VB.XYZ() * 3.0;
752 check = CheckIntersection(PCbestTmp, PC, Pint, aMesh, aShape, face);
754 double dist = PC.Distance(Pint)/3.;
756 gp_Dir aDir(gp_Vec(PC,PCbest));
757 PCbest = PC.XYZ() + aDir.XYZ() * dist;
762 // create node for PCbest
763 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
765 // add triangles to result map
766 TTriaList& triaList = myResMap.insert( make_pair( face, TTriaList() ))->second;
769 SMDS_MeshFace* newFace =meshDS->AddFace( NewNode, FNodes[i], FNodes[i+1] );
770 triaList.push_back( newFace );
771 myTempTriangles[newFace] = true;
775 if ( isRev ) swap( FNodes[1], FNodes[3]);
776 SMDS_MeshVolume* aPyram =
777 helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
778 myPyramids.push_back(aPyram);
780 } // end loop on elements on a face submesh
782 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
784 return Compute2ndPart(aMesh);
787 //================================================================================
789 * \brief Computes pyramids in mesh with no shape
791 //================================================================================
793 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
797 SMESH_MesherHelper helper(aMesh);
798 helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
799 helper.SetElementsOnShape( true );
801 if ( !myElemSearcher )
802 myElemSearcher = SMESH_MeshEditor(&aMesh).GetElementSearcher();
803 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
805 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
807 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator(/*idInceasingOrder=*/true);
810 const SMDS_MeshElement* face = fIt->next();
811 if ( !face ) continue;
812 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
813 // retrieve needed information about a face
814 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
815 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
816 vector<const SMDS_MeshNode*> FNodes(5);
819 const SMDS_MeshElement* volumes[2];
820 int what = Preparation(face, PN, VN, FNodes, PC, VNorm, volumes);
821 if ( what == NOT_QUAD )
823 if ( volumes[0] && volumes[1] )
824 continue; // face is shared by two volumes - no space for a pyramid
826 if ( what == DEGEN_QUAD )
829 // add triangles to result map
831 SMDS_MeshFace* NewFace;
834 double tmp = PN->Value(1).Distance(PN->Value(2)) + PN->Value(2).Distance(PN->Value(3));
835 // far points in VNorm direction
836 gp_Pnt Ptmp1 = PC.XYZ() + VNorm.XYZ() * tmp * 1.e6;
837 gp_Pnt Ptmp2 = PC.XYZ() - VNorm.XYZ() * tmp * 1.e6;
838 // check intersection for Ptmp1 and Ptmp2
842 double dist1 = RealLast();
843 double dist2 = RealLast();
846 gp_Ax1 line( PC, VNorm );
847 vector< const SMDS_MeshElement* > suspectElems;
848 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectElems);
850 for ( int iF = 0; iF < suspectElems.size(); ++iF ) {
851 const SMDS_MeshElement* F = suspectElems[iF];
852 if(F==face) continue;
853 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
854 for ( int i = 0; i < 4; ++i )
855 aContour->Append( SMESH_MeshEditor::TNodeXYZ( F->GetNode(i) ));
857 if( !volumes[0] && HasIntersection(Ptmp1, PC, PPP, aContour) ) {
859 double tmp = PC.Distance(PPP);
865 if( !volumes[1] && HasIntersection(Ptmp2, PC, PPP, aContour) ) {
867 double tmp = PC.Distance(PPP);
875 if( IsOK1 && !IsOK2 ) {
876 // using existed direction
878 else if( !IsOK1 && IsOK2 ) {
879 // using opposite direction
882 else { // IsOK1 && IsOK2
883 double tmp1 = PC.Distance(Pres1);
884 double tmp2 = PC.Distance(Pres2);
886 // using existed direction
889 // using opposite direction
894 NewFace = meshDS->AddFace( FNodes[0], FNodes[1], FNodes[2] );
896 NewFace = meshDS->AddFace( FNodes[0], FNodes[2], FNodes[1] );
897 myTempTriangles[NewFace] = true;
898 aList.push_back(NewFace);
899 myResMap.insert(make_pair(face,aList));
905 gp_XYZ PCbest(0., 0., 0.); // pyramid peak
908 gp_Pnt Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
909 PCbest += Pbest.XYZ();
913 double height = PC.Distance(PCbest); // pyramid height to precise
915 // create new PCbest using a bit shift along VNorm
916 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
917 height = PC.Distance(PCbest);
919 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
921 // Restrict pyramid height by intersection with other faces
922 gp_Vec tmpDir(PC,PCbest); tmpDir.Normalize();
923 double tmp = PN->Value(1).Distance(PN->Value(3)) + PN->Value(2).Distance(PN->Value(4));
924 // far points: in (PC, PCbest) direction and vice-versa
925 gp_Pnt farPnt[2] = { PC.XYZ() + tmpDir.XYZ() * tmp * 1.e6,
926 PC.XYZ() - tmpDir.XYZ() * tmp * 1.e6 };
927 // check intersection for farPnt1 and farPnt2
928 bool intersected[2] = { false, false };
929 double dist [2] = { RealLast(), RealLast() };
932 gp_Ax1 line( PC, tmpDir );
933 vector< const SMDS_MeshElement* > suspectElems;
934 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectElems);
936 for ( int iF = 0; iF < suspectElems.size(); ++iF )
938 const SMDS_MeshElement* F = suspectElems[iF];
939 if(F==face) continue;
940 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
941 int nbN = F->NbNodes() / ( F->IsQuadratic() ? 2 : 1 );
942 for ( i = 0; i < nbN; ++i )
943 aContour->Append( SMESH_MeshEditor::TNodeXYZ( F->GetNode(i) ));
945 for ( int isRev = 0; isRev < 2; ++isRev )
947 if( !volumes[isRev] && HasIntersection(farPnt[isRev], PC, intP, aContour) ) {
948 intersected[isRev] = true;
949 double d = PC.Distance( intP );
950 if( d < dist[isRev] )
952 intPnt[isRev] = intP;
959 // Create one or two pyramids
961 for ( int isRev = 0; isRev < 2; ++isRev )
963 if( !intersected[isRev] ) continue;
964 double pyramidH = Min( height, PC.Distance(intPnt[isRev])/3.);
965 PCbest = PC.XYZ() + tmpDir.XYZ() * (isRev ? -pyramidH : pyramidH);
967 // create node for PCbest
968 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
970 // add triangles to result map
971 TTriaList& aList = myResMap.insert( make_pair( face, TTriaList()))->second;
973 SMDS_MeshFace* NewFace;
975 NewFace = meshDS->AddFace( NewNode, FNodes[i], FNodes[i+1] );
977 NewFace = meshDS->AddFace( NewNode, FNodes[i+1], FNodes[i] );
978 myTempTriangles[NewFace] = true;
979 aList.push_back(NewFace);
982 SMDS_MeshVolume* aPyram;
984 aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
986 aPyram = helper.AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
987 myPyramids.push_back(aPyram);
989 } // end loop on all faces
991 return Compute2ndPart(aMesh);
994 //================================================================================
996 * \brief Update created pyramids and faces to avoid their intersection
998 //================================================================================
1000 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh)
1002 if(myPyramids.empty())
1005 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1006 int i, j, k, myShapeID = myPyramids[0]->GetNode(4)->getshapeId();
1008 if ( !myElemSearcher )
1009 myElemSearcher = SMESH_MeshEditor(&aMesh).GetElementSearcher();
1010 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
1012 set<const SMDS_MeshNode*> nodesToMove;
1014 // check adjacent pyramids
1016 for ( i = 0; i < myPyramids.size(); ++i )
1018 const SMDS_MeshElement* PrmI = myPyramids[i];
1019 MergeAdjacent( PrmI, aMesh, myTempTriangles, nodesToMove );
1022 // iterate on all pyramids
1023 for ( i = 0; i < myPyramids.size(); ++i )
1025 const SMDS_MeshElement* PrmI = myPyramids[i];
1027 // compare PrmI with all the rest pyramids
1029 // collect adjacent pyramids and nodes coordinates of PrmI
1030 set<const SMDS_MeshElement*> checkedPyrams;
1031 vector<gp_Pnt> PsI(5);
1032 for(k=0; k<5; k++) // loop on 4 base nodes of PrmI
1034 const SMDS_MeshNode* n = PrmI->GetNode(k);
1035 PsI[k] = SMESH_MeshEditor::TNodeXYZ( n );
1036 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
1037 while ( vIt->more() )
1038 checkedPyrams.insert( vIt->next() );
1041 // check intersection with distant pyramids
1042 for(k=0; k<4; k++) // loop on 4 base nodes of PrmI
1044 gp_Vec Vtmp(PsI[k],PsI[4]);
1045 gp_Pnt Pshift = PsI[k].XYZ() + Vtmp.XYZ() * 0.01; // base node moved a bit to apex
1047 gp_Ax1 line( PsI[k], Vtmp );
1048 vector< const SMDS_MeshElement* > suspectPyrams;
1049 searcher->GetElementsNearLine( line, SMDSAbs_Volume, suspectPyrams);
1051 for ( j = 0; j < suspectPyrams.size(); ++j )
1053 const SMDS_MeshElement* PrmJ = suspectPyrams[j];
1054 if ( PrmJ == PrmI || PrmJ->NbCornerNodes() != 5 )
1056 if ( myShapeID != PrmJ->GetNode(4)->getshapeId())
1057 continue; // pyramid from other SOLID
1058 if ( PrmI->GetNode(4) == PrmJ->GetNode(4) )
1059 continue; // pyramids PrmI and PrmJ already merged
1060 if ( !checkedPyrams.insert( PrmJ ).second )
1061 continue; // already checked
1063 TXyzIterator xyzIt( PrmJ->nodesIterator() );
1064 vector<gp_Pnt> PsJ( xyzIt, TXyzIterator() );
1068 ( HasIntersection3( Pshift, PsI[4], Pint, PsJ[0], PsJ[1], PsJ[4]) ||
1069 HasIntersection3( Pshift, PsI[4], Pint, PsJ[1], PsJ[2], PsJ[4]) ||
1070 HasIntersection3( Pshift, PsI[4], Pint, PsJ[2], PsJ[3], PsJ[4]) ||
1071 HasIntersection3( Pshift, PsI[4], Pint, PsJ[3], PsJ[0], PsJ[4]) );
1073 for(k=0; k<4 && !hasInt; k++) {
1074 gp_Vec Vtmp(PsJ[k],PsJ[4]);
1075 gp_Pnt Pshift = PsJ[k].XYZ() + Vtmp.XYZ() * 0.01;
1077 ( HasIntersection3( Pshift, PsJ[4], Pint, PsI[0], PsI[1], PsI[4]) ||
1078 HasIntersection3( Pshift, PsJ[4], Pint, PsI[1], PsI[2], PsI[4]) ||
1079 HasIntersection3( Pshift, PsJ[4], Pint, PsI[2], PsI[3], PsI[4]) ||
1080 HasIntersection3( Pshift, PsJ[4], Pint, PsI[3], PsI[0], PsI[4]) );
1085 // count common nodes of base faces of two pyramids
1088 nbc += int ( PrmI->GetNodeIndex( PrmJ->GetNode(k) ) >= 0 );
1091 continue; // pyrams have a common base face
1095 // Merge the two pyramids and others already merged with them
1096 MergePyramids( PrmI, PrmJ, meshDS, myTempTriangles, nodesToMove );
1100 // decrease height of pyramids
1101 gp_XYZ PCi(0,0,0), PCj(0,0,0);
1102 for(k=0; k<4; k++) {
1103 PCi += PsI[k].XYZ();
1104 PCj += PsJ[k].XYZ();
1107 gp_Vec VN1(PCi,PsI[4]);
1108 gp_Vec VN2(PCj,PsJ[4]);
1109 gp_Vec VI1(PCi,Pint);
1110 gp_Vec VI2(PCj,Pint);
1111 double ang1 = fabs(VN1.Angle(VI1));
1112 double ang2 = fabs(VN2.Angle(VI2));
1113 double coef1 = 0.5 - (( ang1<PI/3 ) ? cos(ang1)*0.25 : 0 );
1114 double coef2 = 0.5 - (( ang2<PI/3 ) ? cos(ang2)*0.25 : 0 ); // cos(ang2) ?
1115 // double coef2 = 0.5;
1117 // coef2 -= cos(ang1)*0.25;
1121 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
1122 aNode1->setXYZ( PCi.X()+VN1.X(), PCi.Y()+VN1.Y(), PCi.Z()+VN1.Z() );
1123 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(PrmJ->GetNode(4));
1124 aNode2->setXYZ( PCj.X()+VN2.X(), PCj.Y()+VN2.Y(), PCj.Z()+VN2.Z() );
1125 nodesToMove.insert( aNode1 );
1126 nodesToMove.insert( aNode2 );
1128 // fix intersections that could appear after apex movement
1129 MergeAdjacent( PrmI, aMesh, myTempTriangles, nodesToMove );
1130 MergeAdjacent( PrmJ, aMesh, myTempTriangles, nodesToMove );
1133 } // loop on suspectPyrams
1134 } // loop on 4 base nodes of PrmI
1136 } // loop on all pyramids
1138 if( !nodesToMove.empty() && !meshDS->IsEmbeddedMode() )
1140 set<const SMDS_MeshNode*>::iterator n = nodesToMove.begin();
1141 for ( ; n != nodesToMove.end(); ++n )
1142 meshDS->MoveNode( *n, (*n)->X(), (*n)->Y(), (*n)->Z() );
1145 // rebind triangles of pyramids sharing the same base quadrangle to the first
1146 // entrance of the base quadrangle
1147 TQuad2Trias::iterator q2t = myResMap.begin(), q2tPrev = q2t;
1148 for ( ++q2t; q2t != myResMap.end(); ++q2t, ++q2tPrev )
1150 if ( q2t->first == q2tPrev->first )
1152 //cerr << __LINE__ << " splice" << endl;
1153 q2tPrev->second.splice( q2tPrev->second.end(), q2t->second );
1156 // delete removed triangles and count resulting nb of triangles
1157 for (q2t = myResMap.begin(); q2t != myResMap.end(); ++q2t)
1159 TTriaList & trias = q2t->second;
1160 vector<const SMDS_MeshFace*> faceToErase;
1161 faceToErase.clear();
1162 //cerr << __LINE__ << " " << trias.size() << endl;
1163 for (TTriaList::iterator tri = trias.begin(); tri != trias.end(); ++tri)
1165 //cerr << " " << __LINE__ << endl;
1166 const SMDS_MeshFace* face = *tri;
1167 if (myTempTriangles.count(face) && (myTempTriangles[face] == false))
1168 faceToErase.push_back(face);
1172 for (vector<const SMDS_MeshFace*>::iterator it = faceToErase.begin(); it != faceToErase.end(); ++it)
1174 const SMDS_MeshFace *face = dynamic_cast<const SMDS_MeshFace*>(*it);
1175 if (face) trias.remove(face);
1176 meshDS->RemoveFreeElement(face, 0, false);
1180 myPyramids.clear(); // no more needed
1183 delete myElemSearcher;
1189 //================================================================================
1191 * \brief Return list of created triangles for given face
1193 //================================================================================
1195 const list<const SMDS_MeshFace* >* StdMeshers_QuadToTriaAdaptor::GetTriangles (const SMDS_MeshElement* aQuad)
1197 TQuad2Trias::iterator it = myResMap.find(aQuad);
1198 return ( it != myResMap.end() ? & it->second : 0 );