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;
54 const int Q2TAbs_TmpTriangle = SMDSEntity_Last + 1;
56 //================================================================================
58 * \brief Temporary face. It's key feature is that it adds itself to an apex node
59 * as inverse element, so that tmp triangles of a piramid can be easily found
61 //================================================================================
63 class SMDS_EXPORT Q2TAdaptor_Triangle : public SMDS_MeshFace
65 const SMDS_MeshNode* _nodes[3];
67 Q2TAdaptor_Triangle(const SMDS_MeshNode* apexNode,
68 const SMDS_MeshNode* node2,
69 const SMDS_MeshNode* node3)
71 _nodes[0]=0; ChangeApex(apexNode);
75 ~Q2TAdaptor_Triangle() { MarkAsRemoved(); }
76 void ChangeApex(const SMDS_MeshNode* node)
80 const_cast<SMDS_MeshNode*>(node)->AddInverseElement(this);
85 const_cast<SMDS_MeshNode*>(_nodes[0])->RemoveInverseElement(this), _nodes[0] = 0;
87 bool IsRemoved() const { return !_nodes[0]; }
88 virtual int NbNodes() const { return 3; }
89 virtual const SMDS_MeshNode* GetNode(const int ind) const { return _nodes[ind]; }
90 virtual SMDSAbs_EntityType GetEntityType() const
92 return SMDSAbs_EntityType( Q2TAbs_TmpTriangle );
94 virtual SMDS_ElemIteratorPtr elementsIterator(SMDSAbs_ElementType type) const
96 if ( type == SMDSAbs_Node )
97 return SMDS_ElemIteratorPtr( new SMDS_NodeArrayElemIterator( _nodes, _nodes+3 ));
98 throw SALOME_Exception(LOCALIZED("Not implemented"));
102 //================================================================================
104 * \brief Return true if two nodes of triangles are equal
106 //================================================================================
108 bool EqualTriangles(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
111 ( F1->GetNode(1)==F2->GetNode(2) && F1->GetNode(2)==F2->GetNode(1) ) ||
112 ( F1->GetNode(1)==F2->GetNode(1) && F1->GetNode(2)==F2->GetNode(2) );
115 //================================================================================
117 * \brief Merge the two pyramids (i.e. fuse their apex) and others already merged with them
119 //================================================================================
121 void MergePiramids( const SMDS_MeshElement* PrmI,
122 const SMDS_MeshElement* PrmJ,
123 SMESHDS_Mesh* meshDS,
124 set<const SMDS_MeshNode*> & nodesToMove)
126 const SMDS_MeshNode* Nrem = PrmJ->GetNode(4); // node to remove
127 int nbJ = Nrem->NbInverseElements( SMDSAbs_Volume );
128 SMESH_MeshEditor::TNodeXYZ Pj( Nrem );
130 // an apex node to make common to all merged pyramids
131 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
132 if ( CommonNode == Nrem ) return; // already merged
133 int nbI = CommonNode->NbInverseElements( SMDSAbs_Volume );
134 SMESH_MeshEditor::TNodeXYZ Pi( CommonNode );
135 gp_XYZ Pnew = ( nbI*Pi + nbJ*Pj ) / (nbI+nbJ);
136 CommonNode->setXYZ( Pnew.X(), Pnew.Y(), Pnew.Z() );
138 nodesToMove.insert( CommonNode );
139 nodesToMove.erase ( Nrem );
141 // find and remove coincided faces of merged pyramids
142 SMDS_ElemIteratorPtr triItI = CommonNode->GetInverseElementIterator(SMDSAbs_Face);
143 while ( triItI->more() )
145 const SMDS_MeshElement* FI = triItI->next();
146 const SMDS_MeshElement* FJEqual = 0;
147 SMDS_ElemIteratorPtr triItJ = Nrem->GetInverseElementIterator(SMDSAbs_Face);
148 while ( !FJEqual && triItJ->more() )
150 const SMDS_MeshElement* FJ = triItJ->next();
151 if ( EqualTriangles( FJ, FI ))
156 ((Q2TAdaptor_Triangle*) FI)->MarkAsRemoved();
157 ((Q2TAdaptor_Triangle*) FJEqual)->MarkAsRemoved();
161 // set the common apex node to pyramids and triangles merged with J
162 SMDS_ElemIteratorPtr itJ = Nrem->GetInverseElementIterator();
163 while ( itJ->more() )
165 const SMDS_MeshElement* elem = itJ->next();
166 if ( elem->GetType() == SMDSAbs_Volume ) // pyramid
168 vector< const SMDS_MeshNode* > nodes( elem->begin_nodes(), elem->end_nodes() );
169 nodes[4] = CommonNode;
170 meshDS->ChangeElementNodes( elem, &nodes[0], nodes.size());
172 else if ( elem->GetEntityType() == Q2TAbs_TmpTriangle ) // tmp triangle
174 ((Q2TAdaptor_Triangle*) elem )->ChangeApex( CommonNode );
177 ASSERT( Nrem->NbInverseElements() == 0 );
178 meshDS->RemoveFreeNode( Nrem,
179 meshDS->MeshElements( Nrem->GetPosition()->GetShapeId()),
180 /*fromGroups=*/false);
183 //================================================================================
185 * \brief Return true if two adjacent pyramids are too close one to another
186 * so that a tetrahedron to built between them whoul have too poor quality
188 //================================================================================
190 bool TooCloseAdjacent( const SMDS_MeshElement* PrmI,
191 const SMDS_MeshElement* PrmJ,
194 const SMDS_MeshNode* nApexI = PrmI->GetNode(4);
195 const SMDS_MeshNode* nApexJ = PrmJ->GetNode(4);
196 if ( nApexI == nApexJ ||
197 nApexI->GetPosition()->GetShapeId() != nApexJ->GetPosition()->GetShapeId() )
200 // Find two common base nodes and their indices within PrmI and PrmJ
201 const SMDS_MeshNode* baseNodes[2] = { 0,0 };
202 int baseNodesIndI[2], baseNodesIndJ[2];
203 for ( int i = 0; i < 4 ; ++i )
205 int j = PrmJ->GetNodeIndex( PrmI->GetNode(i));
208 int ind = baseNodes[0] ? 1:0;
209 if ( baseNodes[ ind ])
210 return false; // pyramids with a common base face
211 baseNodes [ ind ] = PrmI->GetNode(i);
212 baseNodesIndI[ ind ] = i;
213 baseNodesIndJ[ ind ] = j;
216 if ( !baseNodes[1] ) return false; // not adjacent
218 // Get normals of triangles sharing baseNodes
219 gp_XYZ apexI = SMESH_MeshEditor::TNodeXYZ( nApexI );
220 gp_XYZ apexJ = SMESH_MeshEditor::TNodeXYZ( nApexJ );
221 gp_XYZ base1 = SMESH_MeshEditor::TNodeXYZ( baseNodes[0]);
222 gp_XYZ base2 = SMESH_MeshEditor::TNodeXYZ( baseNodes[1]);
223 gp_Vec baseVec( base1, base2 );
224 gp_Vec baI( base1, apexI );
225 gp_Vec baJ( base1, apexJ );
226 gp_Vec nI = baseVec.Crossed( baI );
227 gp_Vec nJ = baseVec.Crossed( baJ );
229 // Check angle between normals
230 double angle = nI.Angle( nJ );
231 bool tooClose = ( angle < 10 * PI180 );
233 // Check if pyramids collide
235 if ( !tooClose && baI * baJ > 0 )
237 // find out if nI points outside of PrmI or inside
238 int dInd = baseNodesIndI[1] - baseNodesIndI[0];
239 isOutI = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
241 // find out sign of projection of nJ to baI
242 double proj = baI * nJ;
244 tooClose = isOutI ? proj > 0 : proj < 0;
247 // Check if PrmI and PrmJ are in same domain
248 if ( tooClose && !hasShape )
250 // check order of baseNodes within pyramids, it must be opposite
251 int dInd = baseNodesIndJ[1] - baseNodesIndJ[0];
252 isOutJ = ( abs(dInd)==1 ) ? dInd < 0 : dInd > 0;
253 if ( isOutJ == isOutI )
254 return false; // other domain
256 // check absence of a face separating domains between pyramids
257 TIDSortedElemSet emptySet, avoidSet;
259 while ( const SMDS_MeshElement* f =
260 SMESH_MeshEditor::FindFaceInSet( baseNodes[0], baseNodes[1],
261 emptySet, avoidSet, &i1, &i2 ))
263 avoidSet.insert( f );
265 // face node other than baseNodes
266 int otherNodeInd = 0;
267 while ( otherNodeInd == i1 || otherNodeInd == i2 ) otherNodeInd++;
268 const SMDS_MeshNode* otherFaceNode = f->GetNode( otherNodeInd );
270 // check if f is a base face of either of pyramids
271 if ( f->NbCornerNodes() == 4 &&
272 ( PrmI->GetNodeIndex( otherFaceNode ) >= 0 ||
273 PrmJ->GetNodeIndex( otherFaceNode ) >= 0 ))
274 continue; // f is a base quadrangle
276 // check projections of face direction (baOFN) to triange normals (nI and nJ)
277 gp_Vec baOFN( base1, SMESH_MeshEditor::TNodeXYZ( otherFaceNode ));
278 ( isOutI ? nJ : nI ).Reverse();
279 if ( nI * baOFN > 0 && nJ * baOFN > 0 )
281 tooClose = false; // f is between pyramids
290 //================================================================================
292 * \brief Merges adjacent pyramids
294 //================================================================================
296 void MergeAdjacent(const SMDS_MeshElement* PrmI,
298 set<const SMDS_MeshNode*>& nodesToMove)
300 TIDSortedElemSet adjacentPyrams, mergedPyrams;
301 for(int k=0; k<4; k++) // loop on 4 base nodes of PrmI
303 const SMDS_MeshNode* n = PrmI->GetNode(k);
304 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
305 while ( vIt->more() )
307 const SMDS_MeshElement* PrmJ = vIt->next();
308 if ( PrmJ->NbCornerNodes() != 5 || !adjacentPyrams.insert( PrmJ ).second )
310 if ( PrmI != PrmJ && TooCloseAdjacent( PrmI, PrmJ, mesh.HasShapeToMesh() ))
312 MergePiramids( PrmI, PrmJ, mesh.GetMeshDS(), nodesToMove );
313 mergedPyrams.insert( PrmJ );
317 if ( !mergedPyrams.empty() )
318 for (TIDSortedElemSet::iterator prm = mergedPyrams.begin(); prm != mergedPyrams.end(); ++prm)
319 MergeAdjacent( *prm, mesh, nodesToMove );
323 //================================================================================
327 //================================================================================
329 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor():
334 //================================================================================
338 //================================================================================
340 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
342 // delete temporary faces
343 TQuad2Trias::iterator f_f = myResMap.begin(), ffEnd = myResMap.end();
344 for ( ; f_f != ffEnd; ++f_f )
346 TTriaList& fList = f_f->second;
347 TTriaList::iterator f = fList.begin(), fEnd = fList.end();
348 for ( ; f != fEnd; ++f )
353 if ( myElemSearcher ) delete myElemSearcher;
358 //=======================================================================
359 //function : FindBestPoint
360 //purpose : Return a point P laying on the line (PC,V) so that triangle
361 // (P, P1, P2) to be equilateral as much as possible
362 // V - normal to (P1,P2,PC)
363 //=======================================================================
364 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
365 const gp_Pnt& PC, const gp_Vec& V)
367 double a = P1.Distance(P2);
368 double b = P1.Distance(PC);
369 double c = P2.Distance(PC);
373 // find shift along V in order a to became equal to (b+c)/2
374 double shift = sqrt( a*a + (b*b-c*c)*(b*b-c*c)/16/a/a - (b*b+c*c)/2 );
376 gp_Pnt Pbest = PC.XYZ() + aDir.XYZ() * shift;
382 //=======================================================================
383 //function : HasIntersection3
384 //purpose : Auxilare for HasIntersection()
385 // find intersection point between triangle (P1,P2,P3)
386 // and segment [PC,P]
387 //=======================================================================
388 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
389 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
391 //cout<<"HasIntersection3"<<endl;
392 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
393 //cout<<" P("<<P.X()<<","<<P.Y()<<","<<P.Z()<<")"<<endl;
394 //cout<<" P1("<<P1.X()<<","<<P1.Y()<<","<<P1.Z()<<")"<<endl;
395 //cout<<" P2("<<P2.X()<<","<<P2.Y()<<","<<P2.Z()<<")"<<endl;
396 //cout<<" P3("<<P3.X()<<","<<P3.Y()<<","<<P3.Z()<<")"<<endl;
399 IntAna_Quadric IAQ(gp_Pln(P1,VP1.Crossed(VP2)));
400 IntAna_IntConicQuad IAICQ(gp_Lin(PC,gp_Dir(gp_Vec(PC,P))),IAQ);
402 if( IAICQ.IsInQuadric() )
404 if( IAICQ.NbPoints() == 1 ) {
405 gp_Pnt PIn = IAICQ.Point(1);
406 const double preci = 1.e-10 * P.Distance(PC);
407 // check if this point is internal for segment [PC,P]
409 ( (PC.X()-PIn.X())*(P.X()-PIn.X()) > preci ) ||
410 ( (PC.Y()-PIn.Y())*(P.Y()-PIn.Y()) > preci ) ||
411 ( (PC.Z()-PIn.Z())*(P.Z()-PIn.Z()) > preci );
415 // check if this point is internal for triangle (P1,P2,P3)
419 if( V1.Magnitude()<preci ||
420 V2.Magnitude()<preci ||
421 V3.Magnitude()<preci ) {
425 const double angularTol = 1e-6;
426 gp_Vec VC1 = V1.Crossed(V2);
427 gp_Vec VC2 = V2.Crossed(V3);
428 gp_Vec VC3 = V3.Crossed(V1);
429 if(VC1.Magnitude()<gp::Resolution()) {
430 if(VC2.IsOpposite(VC3,angularTol)) {
434 else if(VC2.Magnitude()<gp::Resolution()) {
435 if(VC1.IsOpposite(VC3,angularTol)) {
439 else if(VC3.Magnitude()<gp::Resolution()) {
440 if(VC1.IsOpposite(VC2,angularTol)) {
445 if( VC1.IsOpposite(VC2,angularTol) || VC1.IsOpposite(VC3,angularTol) ||
446 VC2.IsOpposite(VC3,angularTol) ) {
459 //=======================================================================
460 //function : HasIntersection
461 //purpose : Auxilare for CheckIntersection()
462 //=======================================================================
464 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
465 Handle(TColgp_HSequenceOfPnt)& aContour)
467 if(aContour->Length()==3) {
468 return HasIntersection3( P, PC, Pint, aContour->Value(1),
469 aContour->Value(2), aContour->Value(3) );
473 if( (aContour->Value(1).Distance(aContour->Value(2)) > 1.e-6) &&
474 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
475 (aContour->Value(2).Distance(aContour->Value(3)) > 1.e-6) ) {
476 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
477 aContour->Value(2), aContour->Value(3) );
479 if(check) return true;
480 if( (aContour->Value(1).Distance(aContour->Value(4)) > 1.e-6) &&
481 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
482 (aContour->Value(4).Distance(aContour->Value(3)) > 1.e-6) ) {
483 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
484 aContour->Value(3), aContour->Value(4) );
486 if(check) return true;
492 //================================================================================
494 * \brief Checks if a line segment (P,PC) intersects any mesh face.
495 * \param P - first segment end
496 * \param PC - second segment end (it is a gravity center of quadrangle)
497 * \param Pint - (out) intersection point
498 * \param aMesh - mesh
499 * \param aShape - shape to check faces on
500 * \param NotCheckedFace - mesh face not to check
501 * \retval bool - true if there is an intersection
503 //================================================================================
505 bool StdMeshers_QuadToTriaAdaptor::CheckIntersection (const gp_Pnt& P,
509 const TopoDS_Shape& aShape,
510 const SMDS_MeshElement* NotCheckedFace)
512 if ( !myElemSearcher )
513 myElemSearcher = SMESH_MeshEditor(&aMesh).GetElementSearcher();
514 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
516 //SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
517 //cout<<" CheckIntersection: meshDS->NbFaces() = "<<meshDS->NbFaces()<<endl;
519 double dist = RealLast(); // find intersection closest to the segment
522 gp_Ax1 line( P, gp_Vec(P,PC));
523 vector< const SMDS_MeshElement* > suspectElems;
524 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectElems);
526 // for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
527 // const TopoDS_Shape& aShapeFace = exp.Current();
528 // if(aShapeFace==NotCheckedFace)
530 // const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements(aShapeFace);
531 // if ( aSubMeshDSFace ) {
532 // SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
533 // while ( iteratorElem->more() ) { // loop on elements on a face
534 // const SMDS_MeshElement* face = iteratorElem->next();
535 for ( int i = 0; i < suspectElems.size(); ++i )
537 const SMDS_MeshElement* face = suspectElems[i];
538 if ( face == NotCheckedFace ) continue;
539 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
540 for ( int i = 0; i < face->NbCornerNodes(); ++i )
541 aContour->Append( SMESH_MeshEditor::TNodeXYZ( face->GetNode(i) ));
542 if( HasIntersection(P, PC, Pres, aContour) ) {
544 double tmp = PC.Distance(Pres);
554 //================================================================================
556 * \brief Prepare data for the given face
557 * \param PN - coordinates of face nodes
558 * \param VN - cross products of vectors (PC-PN(i)) ^ (PC-PN(i+1))
559 * \param FNodes - face nodes
560 * \param PC - gravity center of nodes
561 * \param VNorm - face normal (sum of VN)
562 * \param volumes - two volumes sharing the given face, the first is in VNorm direction
563 * \retval int - 0 if given face is not quad,
564 * 1 if given face is quad,
565 * 2 if given face is degenerate quad (two nodes are coincided)
567 //================================================================================
569 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
570 Handle(TColgp_HArray1OfPnt)& PN,
571 Handle(TColgp_HArray1OfVec)& VN,
572 vector<const SMDS_MeshNode*>& FNodes,
575 const SMDS_MeshElement** volumes)
577 if( face->NbNodes() != ( face->IsQuadratic() ? 8 : 4 ))
578 if( face->NbNodes() != 4 )
582 gp_XYZ xyzC(0., 0., 0.);
583 for ( i = 0; i < 4; ++i )
585 gp_XYZ p = SMESH_MeshEditor::TNodeXYZ( FNodes[i] = face->GetNode(i) );
586 PN->SetValue( i+1, p );
590 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
598 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
603 //int deg_num = IsDegenarate(PN);
607 //cout<<"find degeneration"<<endl;
609 gp_Pnt Pdeg = PN->Value(i);
611 list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
612 const SMDS_MeshNode* DegNode = 0;
613 for(; itdg!=myDegNodes.end(); itdg++) {
614 const SMDS_MeshNode* N = (*itdg);
615 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
616 if(Pdeg.Distance(Ptmp)<1.e-6) {
618 //DegNode = const_cast<SMDS_MeshNode*>(N);
623 DegNode = FNodes[i-1];
624 myDegNodes.push_back(DegNode);
627 FNodes[i-1] = DegNode;
630 PN->SetValue(i,PN->Value(i+1));
631 FNodes[i-1] = FNodes[i];
636 PN->SetValue(nbp+1,PN->Value(1));
637 FNodes[nbp] = FNodes[0];
638 // find normal direction
639 gp_Vec V1(PC,PN->Value(nbp));
640 gp_Vec V2(PC,PN->Value(1));
641 VNorm = V1.Crossed(V2);
642 VN->SetValue(nbp,VNorm);
643 for(i=1; i<nbp; i++) {
644 V1 = gp_Vec(PC,PN->Value(i));
645 V2 = gp_Vec(PC,PN->Value(i+1));
646 gp_Vec Vtmp = V1.Crossed(V2);
647 VN->SetValue(i,Vtmp);
651 // find volumes sharing the face
654 volumes[0] = volumes[1] = 0;
655 SMDS_ElemIteratorPtr vIt = FNodes[0]->GetInverseElementIterator( SMDSAbs_Volume );
656 while ( vIt->more() )
658 const SMDS_MeshElement* vol = vIt->next();
659 bool volSharesAllNodes = true;
660 for ( int i = 1; i < face->NbNodes() && volSharesAllNodes; ++i )
661 volSharesAllNodes = ( vol->GetNodeIndex( FNodes[i] ) >= 0 );
662 if ( volSharesAllNodes )
663 volumes[ volumes[0] ? 1 : 0 ] = vol;
664 // we could additionally check that vol has all FNodes in its one face using SMDS_VolumeTool
666 // define volume position relating to the face normal
670 SMDS_ElemIteratorPtr nodeIt = volumes[0]->nodesIterator();
672 volGC = accumulate( TXyzIterator(nodeIt), TXyzIterator(), volGC ) / volumes[0]->NbNodes();
674 if ( VNorm * gp_Vec( PC, volGC ) < 0 )
675 swap( volumes[0], volumes[1] );
679 //cout<<" VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
680 return hasdeg ? DEGEN_QUAD : QUAD;
684 //=======================================================================
687 //=======================================================================
689 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
694 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
695 SMESH_MesherHelper helper(aMesh);
696 helper.IsQuadraticSubMesh(aShape);
697 helper.SetElementsOnShape( true );
699 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next())
701 const TopoDS_Shape& aShapeFace = exp.Current();
702 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
703 if ( aSubMeshDSFace )
705 bool isRev = SMESH_Algo::IsReversedSubMesh( TopoDS::Face(aShapeFace), meshDS );
707 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
708 while ( iteratorElem->more() ) // loop on elements on a geometrical face
710 const SMDS_MeshElement* face = iteratorElem->next();
711 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
712 // preparation step using face info
713 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
714 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
715 vector<const SMDS_MeshNode*> FNodes(5);
718 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
725 // add triangles to result map
726 SMDS_MeshFace* NewFace;
728 NewFace = new Q2TAdaptor_Triangle( FNodes[0], FNodes[1], FNodes[2] );
730 NewFace = new Q2TAdaptor_Triangle( FNodes[0], FNodes[2], FNodes[1] );
731 TTriaList aList( 1, NewFace );
732 myResMap.insert(make_pair(face,aList));
736 if(!isRev) VNorm.Reverse();
737 double xc = 0., yc = 0., zc = 0.;
742 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i).Reversed());
744 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
749 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
752 double height = PCbest.Distance(PC);
754 // create new PCbest using a bit shift along VNorm
755 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
758 // check possible intersection with other faces
760 bool check = CheckIntersection(PCbest, PC, Pint, aMesh, aShape, face);
762 //cout<<"--PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
763 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
764 double dist = PC.Distance(Pint)/3.;
765 gp_Dir aDir(gp_Vec(PC,PCbest));
766 PCbest = PC.XYZ() + aDir.XYZ() * dist;
769 gp_Vec VB(PC,PCbest);
770 gp_Pnt PCbestTmp = PC.XYZ() + VB.XYZ() * 3.0;
771 check = CheckIntersection(PCbestTmp, PC, Pint, aMesh, aShape, face);
773 double dist = PC.Distance(Pint)/3.;
775 gp_Dir aDir(gp_Vec(PC,PCbest));
776 PCbest = PC.XYZ() + aDir.XYZ() * dist;
781 // create node for PCbest
782 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
784 // add triangles to result map
785 TTriaList& triaList = myResMap.insert( make_pair( face, TTriaList() ))->second;
787 triaList.push_back( new Q2TAdaptor_Triangle( NewNode, FNodes[i], FNodes[i+1] ));
790 if ( isRev ) swap( FNodes[1], FNodes[3]);
791 SMDS_MeshVolume* aPyram =
792 helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
793 myPyramids.push_back(aPyram);
795 } // end loop on elements on a face submesh
797 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
799 return Compute2ndPart(aMesh);
802 //================================================================================
804 * \brief Computes pyramids in mesh with no shape
806 //================================================================================
808 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
812 SMESH_MesherHelper helper(aMesh);
813 helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
814 helper.SetElementsOnShape( true );
816 if ( !myElemSearcher )
817 myElemSearcher = SMESH_MeshEditor(&aMesh).GetElementSearcher();
818 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
820 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
822 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator(/*idInceasingOrder=*/true);
825 const SMDS_MeshElement* face = fIt->next();
826 if ( !face ) continue;
827 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
828 // retrieve needed information about a face
829 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
830 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
831 vector<const SMDS_MeshNode*> FNodes(5);
834 const SMDS_MeshElement* volumes[2];
835 int what = Preparation(face, PN, VN, FNodes, PC, VNorm, volumes);
836 if ( what == NOT_QUAD )
838 if ( volumes[0] && volumes[1] )
839 continue; // face is shared by two volumes - no space for a pyramid
841 if ( what == DEGEN_QUAD )
844 // add triangles to result map
846 SMDS_MeshFace* NewFace;
849 double tmp = PN->Value(1).Distance(PN->Value(2)) + PN->Value(2).Distance(PN->Value(3));
850 // far points in VNorm direction
851 gp_Pnt Ptmp1 = PC.XYZ() + VNorm.XYZ() * tmp * 1.e6;
852 gp_Pnt Ptmp2 = PC.XYZ() - VNorm.XYZ() * tmp * 1.e6;
853 // check intersection for Ptmp1 and Ptmp2
857 double dist1 = RealLast();
858 double dist2 = RealLast();
861 gp_Ax1 line( PC, VNorm );
862 vector< const SMDS_MeshElement* > suspectElems;
863 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectElems);
865 for ( int iF = 0; iF < suspectElems.size(); ++iF ) {
866 const SMDS_MeshElement* F = suspectElems[iF];
867 if(F==face) continue;
868 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
869 for ( int i = 0; i < 4; ++i )
870 aContour->Append( SMESH_MeshEditor::TNodeXYZ( F->GetNode(i) ));
872 if( !volumes[0] && HasIntersection(Ptmp1, PC, PPP, aContour) ) {
874 double tmp = PC.Distance(PPP);
880 if( !volumes[1] && HasIntersection(Ptmp2, PC, PPP, aContour) ) {
882 double tmp = PC.Distance(PPP);
890 if( IsOK1 && !IsOK2 ) {
891 // using existed direction
893 else if( !IsOK1 && IsOK2 ) {
894 // using opposite direction
897 else { // IsOK1 && IsOK2
898 double tmp1 = PC.Distance(Pres1);
899 double tmp2 = PC.Distance(Pres2);
901 // using existed direction
904 // using opposite direction
909 NewFace = new Q2TAdaptor_Triangle( FNodes[0], FNodes[1], FNodes[2] );
911 NewFace = new Q2TAdaptor_Triangle( FNodes[0], FNodes[2], FNodes[1] );
912 aList.push_back(NewFace);
913 myResMap.insert(make_pair(face,aList));
919 gp_XYZ PCbest(0., 0., 0.); // pyramid peak
922 gp_Pnt Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
923 PCbest += Pbest.XYZ();
927 double height = PC.Distance(PCbest); // pyramid height to precise
929 // create new PCbest using a bit shift along VNorm
930 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
931 height = PC.Distance(PCbest);
933 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
935 // Restrict pyramid height by intersection with other faces
936 gp_Vec tmpDir(PC,PCbest); tmpDir.Normalize();
937 double tmp = PN->Value(1).Distance(PN->Value(3)) + PN->Value(2).Distance(PN->Value(4));
938 // far points: in (PC, PCbest) direction and vice-versa
939 gp_Pnt farPnt[2] = { PC.XYZ() + tmpDir.XYZ() * tmp * 1.e6,
940 PC.XYZ() - tmpDir.XYZ() * tmp * 1.e6 };
941 // check intersection for farPnt1 and farPnt2
942 bool intersected[2] = { false, false };
943 double dist [2] = { RealLast(), RealLast() };
946 gp_Ax1 line( PC, tmpDir );
947 vector< const SMDS_MeshElement* > suspectElems;
948 searcher->GetElementsNearLine( line, SMDSAbs_Face, suspectElems);
950 for ( int iF = 0; iF < suspectElems.size(); ++iF )
952 const SMDS_MeshElement* F = suspectElems[iF];
953 if(F==face) continue;
954 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
955 int nbN = F->NbNodes() / ( F->IsQuadratic() ? 2 : 1 );
956 for ( i = 0; i < nbN; ++i )
957 aContour->Append( SMESH_MeshEditor::TNodeXYZ( F->GetNode(i) ));
959 for ( int isRev = 0; isRev < 2; ++isRev )
961 if( !volumes[isRev] && HasIntersection(farPnt[isRev], PC, intP, aContour) ) {
962 intersected[isRev] = true;
963 double d = PC.Distance( intP );
964 if( d < dist[isRev] )
966 intPnt[isRev] = intP;
973 // Create one or two pyramids
975 for ( int isRev = 0; isRev < 2; ++isRev )
977 if( !intersected[isRev] ) continue;
978 double pyramidH = Min( height, PC.Distance(intPnt[isRev])/3.);
979 PCbest = PC.XYZ() + tmpDir.XYZ() * (isRev ? -pyramidH : pyramidH);
981 // create node for PCbest
982 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
984 // add triangles to result map
985 TTriaList& aList = myResMap.insert( make_pair( face, TTriaList()))->second;
987 SMDS_MeshFace* NewFace;
989 NewFace = new Q2TAdaptor_Triangle( NewNode, FNodes[i], FNodes[i+1] );
991 NewFace = new Q2TAdaptor_Triangle( NewNode, FNodes[i+1], FNodes[i] );
992 aList.push_back(NewFace);
995 SMDS_MeshVolume* aPyram;
997 aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
999 aPyram = helper.AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
1000 myPyramids.push_back(aPyram);
1002 } // end loop on all faces
1004 return Compute2ndPart(aMesh);
1007 //================================================================================
1009 * \brief Update created pyramids and faces to avoid their intersection
1011 //================================================================================
1013 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh)
1015 if(myPyramids.empty())
1018 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1019 int i, j, k, myShapeID = myPyramids[0]->GetNode(4)->GetPosition()->GetShapeId();
1021 if ( !myElemSearcher )
1022 myElemSearcher = SMESH_MeshEditor(&aMesh).GetElementSearcher();
1023 SMESH_ElementSearcher* searcher = const_cast<SMESH_ElementSearcher*>(myElemSearcher);
1025 set<const SMDS_MeshNode*> nodesToMove;
1027 // check adjacent pyramids
1029 for ( i = 0; i < myPyramids.size(); ++i )
1031 const SMDS_MeshElement* PrmI = myPyramids[i];
1032 MergeAdjacent( PrmI, aMesh, nodesToMove );
1035 // iterate on all pyramids
1036 for ( i = 0; i < myPyramids.size(); ++i )
1038 const SMDS_MeshElement* PrmI = myPyramids[i];
1040 // compare PrmI with all the rest pyramids
1042 // collect adjacent pyramids and nodes coordinates of PrmI
1043 set<const SMDS_MeshElement*> checkedPyrams;
1044 vector<gp_Pnt> PsI(5);
1045 for(k=0; k<5; k++) // loop on 4 base nodes of PrmI
1047 const SMDS_MeshNode* n = PrmI->GetNode(k);
1048 PsI[k] = SMESH_MeshEditor::TNodeXYZ( n );
1049 SMDS_ElemIteratorPtr vIt = n->GetInverseElementIterator( SMDSAbs_Volume );
1050 while ( vIt->more() )
1051 checkedPyrams.insert( vIt->next() );
1054 // check intersection with distant pyramids
1055 for(k=0; k<4; k++) // loop on 4 base nodes of PrmI
1057 gp_Vec Vtmp(PsI[k],PsI[4]);
1058 gp_Pnt Pshift = PsI[k].XYZ() + Vtmp.XYZ() * 0.01; // base node moved a bit to apex
1060 gp_Ax1 line( PsI[k], Vtmp );
1061 vector< const SMDS_MeshElement* > suspectPyrams;
1062 searcher->GetElementsNearLine( line, SMDSAbs_Volume, suspectPyrams);
1064 for ( j = 0; j < suspectPyrams.size(); ++j )
1066 const SMDS_MeshElement* PrmJ = suspectPyrams[j];
1067 if ( PrmJ == PrmI || PrmJ->NbCornerNodes() != 5 )
1069 if ( myShapeID != PrmJ->GetNode(4)->GetPosition()->GetShapeId())
1070 continue; // pyramid from other SOLID
1071 if ( PrmI->GetNode(4) == PrmJ->GetNode(4) )
1072 continue; // pyramids PrmI and PrmJ already merged
1073 if ( !checkedPyrams.insert( PrmJ ).second )
1074 continue; // already checked
1076 TXyzIterator xyzIt( PrmJ->nodesIterator() );
1077 vector<gp_Pnt> PsJ( xyzIt, TXyzIterator() );
1081 ( HasIntersection3( Pshift, PsI[4], Pint, PsJ[0], PsJ[1], PsJ[4]) ||
1082 HasIntersection3( Pshift, PsI[4], Pint, PsJ[1], PsJ[2], PsJ[4]) ||
1083 HasIntersection3( Pshift, PsI[4], Pint, PsJ[2], PsJ[3], PsJ[4]) ||
1084 HasIntersection3( Pshift, PsI[4], Pint, PsJ[3], PsJ[0], PsJ[4]) );
1086 for(k=0; k<4 && !hasInt; k++) {
1087 gp_Vec Vtmp(PsJ[k],PsJ[4]);
1088 gp_Pnt Pshift = PsJ[k].XYZ() + Vtmp.XYZ() * 0.01;
1090 ( HasIntersection3( Pshift, PsJ[4], Pint, PsI[0], PsI[1], PsI[4]) ||
1091 HasIntersection3( Pshift, PsJ[4], Pint, PsI[1], PsI[2], PsI[4]) ||
1092 HasIntersection3( Pshift, PsJ[4], Pint, PsI[2], PsI[3], PsI[4]) ||
1093 HasIntersection3( Pshift, PsJ[4], Pint, PsI[3], PsI[0], PsI[4]) );
1098 // count common nodes of base faces of two pyramids
1101 nbc += int ( PrmI->GetNodeIndex( PrmJ->GetNode(k) ) >= 0 );
1104 continue; // pyrams have a common base face
1108 // Merge the two pyramids and others already merged with them
1109 MergePiramids( PrmI, PrmJ, meshDS, nodesToMove );
1113 // decrease height of pyramids
1114 gp_XYZ PCi(0,0,0), PCj(0,0,0);
1115 for(k=0; k<4; k++) {
1116 PCi += PsI[k].XYZ();
1117 PCj += PsJ[k].XYZ();
1120 gp_Vec VN1(PCi,PsI[4]);
1121 gp_Vec VN2(PCj,PsJ[4]);
1122 gp_Vec VI1(PCi,Pint);
1123 gp_Vec VI2(PCj,Pint);
1124 double ang1 = fabs(VN1.Angle(VI1));
1125 double ang2 = fabs(VN2.Angle(VI2));
1126 double coef1 = 0.5 - (( ang1<PI/3 ) ? cos(ang1)*0.25 : 0 );
1127 double coef2 = 0.5 - (( ang2<PI/3 ) ? cos(ang2)*0.25 : 0 ); // cos(ang2) ?
1128 // double coef2 = 0.5;
1130 // coef2 -= cos(ang1)*0.25;
1134 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
1135 aNode1->setXYZ( PCi.X()+VN1.X(), PCi.Y()+VN1.Y(), PCi.Z()+VN1.Z() );
1136 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(PrmJ->GetNode(4));
1137 aNode2->setXYZ( PCj.X()+VN2.X(), PCj.Y()+VN2.Y(), PCj.Z()+VN2.Z() );
1138 nodesToMove.insert( aNode1 );
1139 nodesToMove.insert( aNode2 );
1142 } // loop on suspectPyrams
1143 } // loop on 4 base nodes of PrmI
1145 } // loop on all pyramids
1147 if( !nodesToMove.empty() && !meshDS->IsEmbeddedMode() )
1149 set<const SMDS_MeshNode*>::iterator n = nodesToMove.begin();
1150 for ( ; n != nodesToMove.end(); ++n )
1151 meshDS->MoveNode( *n, (*n)->X(), (*n)->Y(), (*n)->Z() );
1154 // rebind triangles of pyramids sharing the same base quadrangle to the first
1155 // entrance of the base quadrangle
1156 TQuad2Trias::iterator q2t = myResMap.begin(), q2tPrev = q2t;
1157 for ( ++q2t; q2t != myResMap.end(); ++q2t, ++q2tPrev )
1159 if ( q2t->first == q2tPrev->first )
1160 q2tPrev->second.splice( q2tPrev->second.end(), q2t->second );
1162 // delete removed triangles
1163 for ( q2t = myResMap.begin(); q2t != myResMap.end(); ++q2t )
1165 TTriaList & trias = q2t->second;
1166 for ( TTriaList::iterator tri = trias.begin(); tri != trias.end(); )
1167 if ( ((const Q2TAdaptor_Triangle*) *tri)->IsRemoved() )
1168 delete *tri, trias.erase( tri++ );
1173 myPyramids.clear(); // no more needed
1176 delete myElemSearcher;
1182 //================================================================================
1184 * \brief Return list of created triangles for given face
1186 //================================================================================
1188 const list<const SMDS_MeshFace* >* StdMeshers_QuadToTriaAdaptor::GetTriangles (const SMDS_MeshElement* aQuad)
1190 TQuad2Trias::iterator it = myResMap.find(aQuad);
1191 return ( it != myResMap.end() ? & it->second : 0 );