1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESH : implementaion of SMESH idl descriptions
23 // File : StdMeshers_QuadToTriaAdaptor.cxx
25 // Created : Wen May 07 16:37:07 2008
26 // Author : Sergey KUUL (skl)
28 #include "StdMeshers_QuadToTriaAdaptor.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>
45 enum EQuadNature { NOT_QUAD, QUAD, DEGEN_QUAD };
47 //================================================================================
51 //================================================================================
53 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
55 // delete temporary faces
56 TQuad2Trias::iterator f_f = myResMap.begin(), ffEnd = myResMap.end();
57 for ( ; f_f != ffEnd; ++f_f )
59 TTriaList& fList = f_f->second;
60 TTriaList::iterator f = fList.begin(), fEnd = fList.end();
61 for ( ; f != fEnd; ++f )
66 // TF2PyramMap::iterator itp = myPyram2Trias.begin();
67 // for(; itp!=myPyram2Trias.end(); itp++)
68 // cout << itp->second << endl;
72 //=======================================================================
73 //function : FindBestPoint
74 //purpose : Return a point P laying on the line (PC,V) so that triangle
75 // (P, P1, P2) to be equilateral as much as possible
76 // V - normal to (P1,P2,PC)
77 //=======================================================================
78 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
79 const gp_Pnt& PC, const gp_Vec& V)
81 double a = P1.Distance(P2);
82 double b = P1.Distance(PC);
83 double c = P2.Distance(PC);
87 // find shift along V in order a to became equal to (b+c)/2
88 double shift = sqrt( a*a + (b*b-c*c)*(b*b-c*c)/16/a/a - (b*b+c*c)/2 );
90 gp_Pnt Pbest = PC.XYZ() + aDir.XYZ() * shift;
96 //=======================================================================
97 //function : HasIntersection3
98 //purpose : Auxilare for HasIntersection()
99 // find intersection point between triangle (P1,P2,P3)
100 // and segment [PC,P]
101 //=======================================================================
102 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
103 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
105 //cout<<"HasIntersection3"<<endl;
106 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
107 //cout<<" P("<<P.X()<<","<<P.Y()<<","<<P.Z()<<")"<<endl;
108 //cout<<" P1("<<P1.X()<<","<<P1.Y()<<","<<P1.Z()<<")"<<endl;
109 //cout<<" P2("<<P2.X()<<","<<P2.Y()<<","<<P2.Z()<<")"<<endl;
110 //cout<<" P3("<<P3.X()<<","<<P3.Y()<<","<<P3.Z()<<")"<<endl;
113 IntAna_Quadric IAQ(gp_Pln(P1,VP1.Crossed(VP2)));
114 IntAna_IntConicQuad IAICQ(gp_Lin(PC,gp_Dir(gp_Vec(PC,P))),IAQ);
116 if( IAICQ.IsInQuadric() )
118 if( IAICQ.NbPoints() == 1 ) {
119 gp_Pnt PIn = IAICQ.Point(1);
120 double preci = 1.e-6;
121 // check if this point is internal for segment [PC,P]
123 ( (PC.X()-PIn.X())*(P.X()-PIn.X()) > preci ) ||
124 ( (PC.Y()-PIn.Y())*(P.Y()-PIn.Y()) > preci ) ||
125 ( (PC.Z()-PIn.Z())*(P.Z()-PIn.Z()) > preci );
129 // check if this point is internal for triangle (P1,P2,P3)
133 if( V1.Magnitude()<preci || V2.Magnitude()<preci ||
134 V3.Magnitude()<preci ) {
138 gp_Vec VC1 = V1.Crossed(V2);
139 gp_Vec VC2 = V2.Crossed(V3);
140 gp_Vec VC3 = V3.Crossed(V1);
141 if(VC1.Magnitude()<preci) {
142 if(VC2.IsOpposite(VC3,preci)) {
146 else if(VC2.Magnitude()<preci) {
147 if(VC1.IsOpposite(VC3,preci)) {
151 else if(VC3.Magnitude()<preci) {
152 if(VC1.IsOpposite(VC2,preci)) {
157 if( VC1.IsOpposite(VC2,preci) || VC1.IsOpposite(VC3,preci) ||
158 VC2.IsOpposite(VC3,preci) ) {
171 //=======================================================================
172 //function : HasIntersection
173 //purpose : Auxilare for CheckIntersection()
174 //=======================================================================
176 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
177 Handle(TColgp_HSequenceOfPnt)& aContour)
179 if(aContour->Length()==3) {
180 return HasIntersection3( P, PC, Pint, aContour->Value(1),
181 aContour->Value(2), aContour->Value(3) );
185 if( (aContour->Value(1).Distance(aContour->Value(2)) > 1.e-6) &&
186 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
187 (aContour->Value(2).Distance(aContour->Value(3)) > 1.e-6) ) {
188 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
189 aContour->Value(2), aContour->Value(3) );
191 if(check) return true;
192 if( (aContour->Value(1).Distance(aContour->Value(4)) > 1.e-6) &&
193 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
194 (aContour->Value(4).Distance(aContour->Value(3)) > 1.e-6) ) {
195 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
196 aContour->Value(3), aContour->Value(4) );
198 if(check) return true;
205 //=======================================================================
206 //function : CheckIntersection
207 //purpose : Auxilare for Compute()
208 // NotCheckedFace - for optimization
209 //=======================================================================
210 bool StdMeshers_QuadToTriaAdaptor::CheckIntersection
211 (const gp_Pnt& P, const gp_Pnt& PC,
212 gp_Pnt& Pint, SMESH_Mesh& aMesh,
213 const TopoDS_Shape& aShape,
214 const TopoDS_Shape& NotCheckedFace)
216 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
217 //cout<<" CheckIntersection: meshDS->NbFaces() = "<<meshDS->NbFaces()<<endl;
219 double dist = RealLast();
221 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
222 const TopoDS_Shape& aShapeFace = exp.Current();
223 if(aShapeFace==NotCheckedFace)
225 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements(aShapeFace);
226 if ( aSubMeshDSFace ) {
227 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
228 while ( iteratorElem->more() ) { // loop on elements on a face
229 const SMDS_MeshElement* face = iteratorElem->next();
230 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
231 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
232 int nbN = face->NbNodes();
233 if( face->IsQuadratic() )
235 for ( int i = 0; i < nbN; ++i ) {
236 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
237 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
239 if( HasIntersection(P, PC, Pres, aContour) ) {
241 double tmp = PC.Distance(Pres);
254 //=======================================================================
255 //function : EqualTriangles
256 //purpose : Auxilare for Compute()
257 //=======================================================================
258 static bool EqualTriangles(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
261 ( F1->GetNode(1)==F2->GetNode(2) && F1->GetNode(2)==F2->GetNode(1) ) ||
262 ( F1->GetNode(1)==F2->GetNode(1) && F1->GetNode(2)==F2->GetNode(2) );
265 //================================================================================
267 * \brief Prepare data for the given face
268 * \param PN - coordinates of face nodes
269 * \param VN - cross products of vectors (PC-PN(i)) ^ (PC-PN(i+1))
270 * \param FNodes - face nodes
271 * \param PC - gravity center of nodes
272 * \param VNorm - face normal (sum of VN)
273 * \param volumes - two volumes sharing the given face, the first is in VNorm direction
274 * \retval int - 0 if given face is not quad,
275 * 1 if given face is quad,
276 * 2 if given face is degenerate quad (two nodes are coincided)
278 //================================================================================
280 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
281 Handle(TColgp_HArray1OfPnt)& PN,
282 Handle(TColgp_HArray1OfVec)& VN,
283 vector<const SMDS_MeshNode*>& FNodes,
286 const SMDS_MeshElement** volumes)
288 if( face->NbNodes() != ( face->IsQuadratic() ? 8 : 4 ))
289 if( face->NbNodes() != 4 )
293 gp_XYZ xyzC(0., 0., 0.);
294 for ( i = 0; i < 4; ++i )
296 gp_XYZ p = SMESH_MeshEditor::TNodeXYZ( FNodes[i] = face->GetNode(i) );
297 PN->SetValue( i+1, p );
301 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
309 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
314 //int deg_num = IsDegenarate(PN);
318 //cout<<"find degeneration"<<endl;
320 gp_Pnt Pdeg = PN->Value(i);
322 list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
323 const SMDS_MeshNode* DegNode = 0;
324 for(; itdg!=myDegNodes.end(); itdg++) {
325 const SMDS_MeshNode* N = (*itdg);
326 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
327 if(Pdeg.Distance(Ptmp)<1.e-6) {
329 //DegNode = const_cast<SMDS_MeshNode*>(N);
334 DegNode = FNodes[i-1];
335 myDegNodes.push_back(DegNode);
338 FNodes[i-1] = DegNode;
341 PN->SetValue(i,PN->Value(i+1));
342 FNodes[i-1] = FNodes[i];
347 PN->SetValue(nbp+1,PN->Value(1));
348 FNodes[nbp] = FNodes[0];
349 // find normal direction
350 gp_Vec V1(PC,PN->Value(nbp));
351 gp_Vec V2(PC,PN->Value(1));
352 VNorm = V1.Crossed(V2);
353 VN->SetValue(nbp,VNorm);
354 for(i=1; i<nbp; i++) {
355 V1 = gp_Vec(PC,PN->Value(i));
356 V2 = gp_Vec(PC,PN->Value(i+1));
357 gp_Vec Vtmp = V1.Crossed(V2);
358 VN->SetValue(i,Vtmp);
362 // find volumes sharing the face
365 volumes[0] = volumes[1] = 0;
366 SMDS_ElemIteratorPtr vIt = FNodes[0]->GetInverseElementIterator( SMDSAbs_Volume );
367 while ( vIt->more() )
369 const SMDS_MeshElement* vol = vIt->next();
370 bool volSharesAllNodes = true;
371 for ( int i = 1; i < face->NbNodes() && volSharesAllNodes; ++i )
372 volSharesAllNodes = ( vol->GetNodeIndex( FNodes[i] ) >= 0 );
373 if ( volSharesAllNodes )
374 volumes[ volumes[0] ? 1 : 0 ] = vol;
375 // we could additionally check that vol has all FNodes in its one face using SMDS_VolumeTool
377 // define volume position relating to the face normal
382 SMDS_ElemIteratorPtr nodeIt = volumes[0]->nodesIterator();
383 while ( nodeIt->more() )
384 volGC += SMESH_MeshEditor::TNodeXYZ( nodeIt->next() );
385 volGC /= volumes[0]->NbNodes();
387 if ( VNorm * gp_Vec( PC, volGC ) < 0 )
388 swap( volumes[0], volumes[1] );
392 //cout<<" VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
393 return hasdeg ? DEGEN_QUAD : QUAD;
397 //=======================================================================
400 //=======================================================================
402 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
405 myPyram2Trias.clear();
407 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
408 SMESH_MesherHelper helper(aMesh);
409 helper.IsQuadraticSubMesh(aShape);
410 helper.SetElementsOnShape( true );
412 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
413 const TopoDS_Shape& aShapeFace = exp.Current();
414 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
415 if ( aSubMeshDSFace ) {
416 bool isRev = SMESH_Algo::IsReversedSubMesh( TopoDS::Face(aShapeFace), meshDS );
418 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
419 while ( iteratorElem->more() ) { // loop on elements on a face
420 const SMDS_MeshElement* face = iteratorElem->next();
421 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
422 // preparation step using face info
423 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
424 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
425 vector<const SMDS_MeshNode*> FNodes(5);
428 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
434 // add triangles to result map
435 SMDS_FaceOfNodes* NewFace;
437 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
439 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
440 TTriaList aList( 1, NewFace );
441 myResMap.insert(make_pair(face,aList));
445 if(!isRev) VNorm.Reverse();
446 double xc = 0., yc = 0., zc = 0.;
451 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i).Reversed());
453 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
458 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
461 double height = PCbest.Distance(PC);
463 // create new PCbest using a bit shift along VNorm
464 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
467 // check possible intersection with other faces
469 bool check = CheckIntersection(PCbest, PC, Pint, aMesh, aShape, aShapeFace);
471 //cout<<"--PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
472 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
473 double dist = PC.Distance(Pint)/3.;
474 gp_Dir aDir(gp_Vec(PC,PCbest));
475 PCbest = PC.XYZ() + aDir.XYZ() * dist;
478 gp_Vec VB(PC,PCbest);
479 gp_Pnt PCbestTmp = PC.XYZ() + VB.XYZ() * 3.0;
480 bool check = CheckIntersection(PCbestTmp, PC, Pint, aMesh, aShape, aShapeFace);
482 double dist = PC.Distance(Pint)/3.;
484 gp_Dir aDir(gp_Vec(PC,PCbest));
485 PCbest = PC.XYZ() + aDir.XYZ() * dist;
490 // create node for PCbest
491 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
493 // add triangles to result map
494 TTriaList& triaList = myResMap.insert( make_pair( face, TTriaList() ))->second;
496 triaList.push_back( new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] ));
499 SMDS_MeshVolume* aPyram =
500 helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
501 myPyram2Trias.insert(make_pair(aPyram, & triaList));
502 } // end loop on elements on a face submesh
504 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
506 return Compute2ndPart(aMesh);
510 //=======================================================================
513 //=======================================================================
515 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
518 myPyram2Trias.clear();
519 SMESH_MesherHelper helper(aMesh);
520 helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
521 helper.SetElementsOnShape( true );
523 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
525 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator();
526 TIDSortedElemSet sortedFaces; // 0020279: control the "random" use when using mesh algorithms
527 while( fIt->more()) sortedFaces.insert( fIt->next() );
529 TIDSortedElemSet::iterator itFace = sortedFaces.begin(), fEnd = sortedFaces.end();
530 for ( ; itFace != fEnd; ++itFace )
532 const SMDS_MeshElement* face = *itFace;
533 if ( !face ) continue;
534 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
535 // retrieve needed information about a face
536 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
537 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
538 vector<const SMDS_MeshNode*> FNodes(5);
541 const SMDS_MeshElement* volumes[2];
542 int what = Preparation(face, PN, VN, FNodes, PC, VNorm, volumes);
543 if ( what == NOT_QUAD )
545 if ( volumes[0] && volumes[1] )
546 continue; // face is shared by two volumes - no space for a pyramid
548 if ( what == DEGEN_QUAD )
551 // add triangles to result map
553 SMDS_FaceOfNodes* NewFace;
556 double tmp = PN->Value(1).Distance(PN->Value(2)) + PN->Value(2).Distance(PN->Value(3));
557 // far points in VNorm direction
558 gp_Pnt Ptmp1 = PC.XYZ() + VNorm.XYZ() * tmp * 1.e6;
559 gp_Pnt Ptmp2 = PC.XYZ() - VNorm.XYZ() * tmp * 1.e6;
560 // check intersection for Ptmp1 and Ptmp2
564 double dist1 = RealLast();
565 double dist2 = RealLast();
567 for (TIDSortedElemSet::iterator itF = sortedFaces.begin(); itF != fEnd; ++itF ) {
568 const SMDS_MeshElement* F = *itF;
569 if(F==face) continue;
570 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
571 for ( int i = 0; i < 4; ++i )
572 aContour->Append( SMESH_MeshEditor::TNodeXYZ( F->GetNode(i) ));
574 if( !volumes[0] && HasIntersection(Ptmp1, PC, PPP, aContour) ) {
576 double tmp = PC.Distance(PPP);
582 if( !volumes[1] && HasIntersection(Ptmp2, PC, PPP, aContour) ) {
584 double tmp = PC.Distance(PPP);
592 if( IsOK1 && !IsOK2 ) {
593 // using existed direction
595 else if( !IsOK1 && IsOK2 ) {
596 // using opposite direction
599 else { // IsOK1 && IsOK2
600 double tmp1 = PC.Distance(Pres1);
601 double tmp2 = PC.Distance(Pres2);
603 // using existed direction
606 // using opposite direction
611 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
613 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
614 aList.push_back(NewFace);
615 myResMap.insert(make_pair(face,aList));
621 gp_XYZ PCbest(0., 0., 0.); // pyramid peak
624 gp_Pnt Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
625 PCbest += Pbest.XYZ();
629 double height = PC.Distance(PCbest); // pyramid height to precise
631 // create new PCbest using a bit shift along VNorm
632 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
633 height = PC.Distance(PCbest);
635 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
637 // Restrict pyramid height by intersection with other faces
638 gp_Vec tmpDir(PC,PCbest); tmpDir.Normalize();
639 double tmp = PN->Value(1).Distance(PN->Value(3)) + PN->Value(2).Distance(PN->Value(4));
640 // far points: in (PC, PCbest) direction and vice-versa
641 gp_Pnt farPnt[2] = { PC.XYZ() + tmpDir.XYZ() * tmp * 1.e6,
642 PC.XYZ() - tmpDir.XYZ() * tmp * 1.e6 };
643 // check intersection for farPnt1 and farPnt2
644 bool intersected[2] = { false, false };
645 double dist [2] = { RealLast(), RealLast() };
647 for (TIDSortedElemSet::iterator itF = sortedFaces.begin(); itF != fEnd; ++itF )
649 const SMDS_MeshElement* F = *itF;
650 if(F==face) continue;
651 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
652 int nbN = F->NbNodes() / ( F->IsQuadratic() ? 2 : 1 );
653 for ( i = 0; i < nbN; ++i )
654 aContour->Append( SMESH_MeshEditor::TNodeXYZ( F->GetNode(i) ));
656 for ( int isRev = 0; isRev < 2; ++isRev )
658 if( !volumes[isRev] && HasIntersection(farPnt[isRev], PC, intP, aContour) ) {
659 intersected[isRev] = true;
660 double d = PC.Distance( intP );
661 if( d < dist[isRev] )
663 intPnt[isRev] = intP;
670 // Create one or two pyramids
672 for ( int isRev = 0; isRev < 2; ++isRev )
674 if( !intersected[isRev] ) continue;
675 double pyramidH = Min( height, PC.Distance(intPnt[isRev])/3.);
676 PCbest = PC.XYZ() + tmpDir.XYZ() * (isRev ? -pyramidH : pyramidH);
678 // create node for PCbest
679 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
681 // add triangles to result map
682 TTriaList& aList = myResMap.insert( make_pair( face, TTriaList()))->second;
684 SMDS_FaceOfNodes* NewFace;
686 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
688 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i+1], FNodes[i] );
689 aList.push_back(NewFace);
692 SMDS_MeshVolume* aPyram;
694 aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
696 aPyram = helper.AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
697 myPyram2Trias.insert(make_pair(aPyram, & aList));
699 } // end loop on all faces
701 return Compute2ndPart(aMesh);
704 //=======================================================================
705 //function : Compute2ndPart
706 //purpose : Update created pyramids and faces to avoid their intersection
707 //=======================================================================
709 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh)
711 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
713 // check intersections between created pyramids
715 if(myPyram2Trias.empty())
718 // sdt-like iterator used to get coordinates of nodes of mesh element
719 typedef SMDS_StdIterator< SMESH_MeshEditor::TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
724 // for each pyramid store list of merged pyramids with their faces
725 typedef map< const SMDS_MeshElement*, list< TPyram2Trias::iterator > > TPyram2Merged;
726 TPyram2Merged MergesInfo;
728 // iterate on all pyramids
729 TPyram2Trias::iterator itPi = myPyram2Trias.begin(), itPEnd = myPyram2Trias.end();
730 for ( ; itPi != itPEnd; ++itPi )
732 const SMDS_MeshElement* PrmI = itPi->first;
733 TPyram2Merged::iterator pMergesI = MergesInfo.find( PrmI );
735 TXyzIterator xyzIt( PrmI->nodesIterator() );
736 vector<gp_Pnt> PsI( xyzIt, xyzEnd );
738 // compare PrmI with all the rest pyramids
739 bool NeedMove = false;
740 TPyram2Trias::iterator itPj = itPi;
741 for ( ++itPj; itPj != itPEnd; ++itPj )
743 const SMDS_MeshElement* PrmJ = itPj->first;
744 TPyram2Merged::iterator pMergesJ = MergesInfo.find( PrmJ );
746 // check if two pyramids already merged
747 if ( pMergesJ != MergesInfo.end() &&
748 find(pMergesJ->second.begin(),pMergesJ->second.end(), itPi )!=pMergesJ->second.end())
749 continue; // already merged
751 xyzIt = TXyzIterator( PrmJ->nodesIterator() );
752 vector<gp_Pnt> PsJ( xyzIt, xyzEnd );
756 for(k=0; k<4 && !hasInt; k++) {
757 gp_Vec Vtmp(PsI[k],PsI[4]);
758 gp_Pnt Pshift = PsI[k].XYZ() + Vtmp.XYZ() * 0.01;
760 ( HasIntersection3( Pshift, PsI[4], Pint, PsJ[0], PsJ[1], PsJ[4]) ||
761 HasIntersection3( Pshift, PsI[4], Pint, PsJ[1], PsJ[2], PsJ[4]) ||
762 HasIntersection3( Pshift, PsI[4], Pint, PsJ[2], PsJ[3], PsJ[4]) ||
763 HasIntersection3( Pshift, PsI[4], Pint, PsJ[3], PsJ[0], PsJ[4]) );
765 for(k=0; k<4 && !hasInt; k++) {
766 gp_Vec Vtmp(PsJ[k],PsJ[4]);
767 gp_Pnt Pshift = PsJ[k].XYZ() + Vtmp.XYZ() * 0.01;
769 ( HasIntersection3( Pshift, PsJ[4], Pint, PsI[0], PsI[1], PsI[4]) ||
770 HasIntersection3( Pshift, PsJ[4], Pint, PsI[1], PsI[2], PsI[4]) ||
771 HasIntersection3( Pshift, PsJ[4], Pint, PsI[2], PsI[3], PsI[4]) ||
772 HasIntersection3( Pshift, PsJ[4], Pint, PsI[3], PsI[0], PsI[4]) );
775 // count common nodes of base faces of two pyramids
778 nbc += int ( PrmI->GetNodeIndex( PrmJ->GetNode(k) ) >= 0 );
779 //cout<<" nbc = "<<nbc<<endl;
782 continue; // pyrams have a common base face
786 // Merge the two pyramids and others already merged with them
788 // initialize merge info of pyramids
789 if ( pMergesI == MergesInfo.end() ) // first merge of PrmI
791 pMergesI = MergesInfo.insert( make_pair( PrmI, list<TPyram2Trias::iterator >())).first;
792 pMergesI->second.push_back( itPi );
794 if ( pMergesJ == MergesInfo.end() ) // first merge of PrmJ
796 pMergesJ = MergesInfo.insert( make_pair( PrmJ, list<TPyram2Trias::iterator >())).first;
797 pMergesJ->second.push_back( itPj );
799 int nbI = pMergesI->second.size(), nbJ = pMergesJ->second.size();
801 // an apex node to make common to all merged pyramids
802 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
803 CommonNode->setXYZ( ( nbI*PsI[4].X() + nbJ*PsJ[4].X() ) / (nbI+nbJ),
804 ( nbI*PsI[4].Y() + nbJ*PsJ[4].Y() ) / (nbI+nbJ),
805 ( nbI*PsI[4].Z() + nbJ*PsJ[4].Z() ) / (nbI+nbJ) );
807 const SMDS_MeshNode* Nrem = PrmJ->GetNode(4); // node to remove
809 list< TPyram2Trias::iterator >& aMergesI = pMergesI->second;
810 list< TPyram2Trias::iterator >& aMergesJ = pMergesJ->second;
812 // find and remove coincided faces of merged pyramids
813 list< TPyram2Trias::iterator >::iterator itPttI, itPttJ;
814 TTriaList::iterator trI, trJ;
815 for ( itPttI = aMergesI.begin(); itPttI != aMergesI.end(); ++itPttI )
817 TTriaList* triaListI = (*itPttI)->second;
818 for ( trI = triaListI->begin(); trI != triaListI->end(); )
820 const SMDS_FaceOfNodes* FI = *trI;
822 for ( itPttJ = aMergesJ.begin(); itPttJ != aMergesJ.end() && FI; ++itPttJ )
824 TTriaList* triaListJ = (*itPttJ)->second;
825 for ( trJ = triaListJ->begin(); trJ != triaListJ->end(); )
827 const SMDS_FaceOfNodes* FJ = *trJ;
829 if( EqualTriangles(FI,FJ) )
834 trI = triaListI->erase( trI );
835 trJ = triaListJ->erase( trJ );
836 break; // only one triangle of a pyramid can coincide with another pyramid
841 if ( FI ) ++trI; // increament if triangle not deleted
845 // set the common apex node to pyramids and triangles merged with J
846 for ( itPttJ = aMergesJ.begin(); itPttJ != aMergesJ.end(); ++itPttJ )
848 const SMDS_MeshElement* Prm = (*itPttJ)->first;
849 TTriaList* triaList = (*itPttJ)->second;
851 vector< const SMDS_MeshNode* > nodes( Prm->begin_nodes(), Prm->end_nodes() );
852 nodes[4] = CommonNode;
853 meshDS->ChangeElementNodes( Prm, &nodes[0], nodes.size());
855 for ( TTriaList::iterator trIt = triaList->begin(); trIt != triaList->end(); ++trIt )
857 SMDS_FaceOfNodes* Ftria = const_cast< SMDS_FaceOfNodes*>( *trIt );
858 const SMDS_MeshNode* NF[3] = { CommonNode, Ftria->GetNode(1), Ftria->GetNode(2)};
859 Ftria->ChangeNodes(NF, 3);
863 // join MergesInfo of merged pyramids
864 for ( k = 0, itPttI = aMergesI.begin(); k < nbI; ++itPttI, ++k )
866 const SMDS_MeshElement* PrmI = (*itPttI)->first;
867 list< TPyram2Trias::iterator >& merges = MergesInfo[ PrmI ];
868 merges.insert( merges.end(), aMergesJ.begin(), aMergesJ.end() );
870 for ( k = 0, itPttJ = aMergesJ.begin(); k < nbJ; ++itPttJ, ++k )
872 const SMDS_MeshElement* PrmJ = (*itPttJ)->first;
873 list< TPyram2Trias::iterator >& merges = MergesInfo[ PrmJ ];
874 merges.insert( merges.end(), aMergesI.begin(), aMergesI.end() );
878 meshDS->RemoveNode(Nrem);
882 // decrease height of pyramids
883 gp_XYZ PC1(0,0,0), PC2(0,0,0);
889 gp_Vec VN1(PC1,PsI[4]);
890 gp_Vec VI1(PC1,Pint);
891 gp_Vec VN2(PC2,PsJ[4]);
892 gp_Vec VI2(PC2,Pint);
893 double ang1 = fabs(VN1.Angle(VI1));
894 double ang2 = fabs(VN2.Angle(VI2));
897 h1 = VI1.Magnitude()/2;
899 h1 = VI1.Magnitude()*cos(ang1);
901 h2 = VI2.Magnitude()/2;
903 h2 = VI2.Magnitude()*cos(ang2);
906 coef1 -= cos(ang1)*0.25;
909 coef2 -= cos(ang1)*0.25;
913 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
914 aNode1->setXYZ( PC1.X()+VN1.X(), PC1.Y()+VN1.Y(), PC1.Z()+VN1.Z() );
915 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(PrmJ->GetNode(4));
916 aNode2->setXYZ( PC2.X()+VN2.X(), PC2.Y()+VN2.Y(), PC2.Z()+VN2.Z() );
921 if( NeedMove && !meshDS->IsEmbeddedMode() )
923 const SMDS_MeshNode* apex = PrmI->GetNode( 4 );
924 meshDS->MoveNode( apex, apex->X(), apex->Y(), apex->Z() );
928 // rebind triangles of pyramids sharing the same base quadrangle to the first
929 // entrance of the base quadrangle
930 TQuad2Trias::iterator q2t = myResMap.begin(), q2tPrev = q2t;
931 for ( ++q2t; q2t != myResMap.end(); ++q2t, ++q2tPrev )
933 if ( q2t->first == q2tPrev->first )
934 q2tPrev->second.splice( q2tPrev->second.end(), q2t->second );
937 myPyram2Trias.clear(); // no more needed
943 //================================================================================
945 * \brief Return list of created triangles for given face
947 //================================================================================
949 const list<const SMDS_FaceOfNodes* >* StdMeshers_QuadToTriaAdaptor::GetTriangles (const SMDS_MeshElement* aQuad)
951 TQuad2Trias::iterator it = myResMap.find(aQuad);
952 if( it != myResMap.end() ) {