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 <SMESH_Algo.hxx>
29 #include <SMESH_MesherHelper.hxx>
31 #include <IntAna_IntConicQuad.hxx>
32 #include <IntAna_Quadric.hxx>
33 #include <TColgp_HArray1OfPnt.hxx>
34 #include <TColgp_HArray1OfVec.hxx>
35 #include <TColgp_HSequenceOfPnt.hxx>
36 #include <TopExp_Explorer.hxx>
45 enum EQuadNature { NOT_QUAD, QUAD, DEGEN_QUAD };
47 // sdt-like iterator used to get coordinates of nodes of mesh element
48 typedef SMDS_StdIterator< SMESH_MeshEditor::TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
50 //================================================================================
54 //================================================================================
56 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
58 // delete temporary faces
59 TQuad2Trias::iterator f_f = myResMap.begin(), ffEnd = myResMap.end();
60 for ( ; f_f != ffEnd; ++f_f )
62 TTriaList& fList = f_f->second;
63 TTriaList::iterator f = fList.begin(), fEnd = fList.end();
64 for ( ; f != fEnd; ++f )
69 // TF2PyramMap::iterator itp = myPyram2Trias.begin();
70 // for(; itp!=myPyram2Trias.end(); itp++)
71 // cout << itp->second << endl;
75 //=======================================================================
76 //function : FindBestPoint
77 //purpose : Return a point P laying on the line (PC,V) so that triangle
78 // (P, P1, P2) to be equilateral as much as possible
79 // V - normal to (P1,P2,PC)
80 //=======================================================================
81 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
82 const gp_Pnt& PC, const gp_Vec& V)
84 double a = P1.Distance(P2);
85 double b = P1.Distance(PC);
86 double c = P2.Distance(PC);
90 // find shift along V in order a to became equal to (b+c)/2
91 double shift = sqrt( a*a + (b*b-c*c)*(b*b-c*c)/16/a/a - (b*b+c*c)/2 );
93 gp_Pnt Pbest = PC.XYZ() + aDir.XYZ() * shift;
99 //=======================================================================
100 //function : HasIntersection3
101 //purpose : Auxilare for HasIntersection()
102 // find intersection point between triangle (P1,P2,P3)
103 // and segment [PC,P]
104 //=======================================================================
105 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
106 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
108 //cout<<"HasIntersection3"<<endl;
109 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
110 //cout<<" P("<<P.X()<<","<<P.Y()<<","<<P.Z()<<")"<<endl;
111 //cout<<" P1("<<P1.X()<<","<<P1.Y()<<","<<P1.Z()<<")"<<endl;
112 //cout<<" P2("<<P2.X()<<","<<P2.Y()<<","<<P2.Z()<<")"<<endl;
113 //cout<<" P3("<<P3.X()<<","<<P3.Y()<<","<<P3.Z()<<")"<<endl;
116 IntAna_Quadric IAQ(gp_Pln(P1,VP1.Crossed(VP2)));
117 IntAna_IntConicQuad IAICQ(gp_Lin(PC,gp_Dir(gp_Vec(PC,P))),IAQ);
119 if( IAICQ.IsInQuadric() )
121 if( IAICQ.NbPoints() == 1 ) {
122 gp_Pnt PIn = IAICQ.Point(1);
123 double preci = 1.e-6;
124 // check if this point is internal for segment [PC,P]
126 ( (PC.X()-PIn.X())*(P.X()-PIn.X()) > preci ) ||
127 ( (PC.Y()-PIn.Y())*(P.Y()-PIn.Y()) > preci ) ||
128 ( (PC.Z()-PIn.Z())*(P.Z()-PIn.Z()) > preci );
132 // check if this point is internal for triangle (P1,P2,P3)
136 if( V1.Magnitude()<preci || V2.Magnitude()<preci ||
137 V3.Magnitude()<preci ) {
141 gp_Vec VC1 = V1.Crossed(V2);
142 gp_Vec VC2 = V2.Crossed(V3);
143 gp_Vec VC3 = V3.Crossed(V1);
144 if(VC1.Magnitude()<preci) {
145 if(VC2.IsOpposite(VC3,preci)) {
149 else if(VC2.Magnitude()<preci) {
150 if(VC1.IsOpposite(VC3,preci)) {
154 else if(VC3.Magnitude()<preci) {
155 if(VC1.IsOpposite(VC2,preci)) {
160 if( VC1.IsOpposite(VC2,preci) || VC1.IsOpposite(VC3,preci) ||
161 VC2.IsOpposite(VC3,preci) ) {
174 //=======================================================================
175 //function : HasIntersection
176 //purpose : Auxilare for CheckIntersection()
177 //=======================================================================
179 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
180 Handle(TColgp_HSequenceOfPnt)& aContour)
182 if(aContour->Length()==3) {
183 return HasIntersection3( P, PC, Pint, aContour->Value(1),
184 aContour->Value(2), aContour->Value(3) );
188 if( (aContour->Value(1).Distance(aContour->Value(2)) > 1.e-6) &&
189 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
190 (aContour->Value(2).Distance(aContour->Value(3)) > 1.e-6) ) {
191 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
192 aContour->Value(2), aContour->Value(3) );
194 if(check) return true;
195 if( (aContour->Value(1).Distance(aContour->Value(4)) > 1.e-6) &&
196 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
197 (aContour->Value(4).Distance(aContour->Value(3)) > 1.e-6) ) {
198 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
199 aContour->Value(3), aContour->Value(4) );
201 if(check) return true;
208 //=======================================================================
209 //function : CheckIntersection
210 //purpose : Auxilare for Compute()
211 // NotCheckedFace - for optimization
212 //=======================================================================
213 bool StdMeshers_QuadToTriaAdaptor::CheckIntersection
214 (const gp_Pnt& P, const gp_Pnt& PC,
215 gp_Pnt& Pint, SMESH_Mesh& aMesh,
216 const TopoDS_Shape& aShape,
217 const TopoDS_Shape& NotCheckedFace)
219 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
220 //cout<<" CheckIntersection: meshDS->NbFaces() = "<<meshDS->NbFaces()<<endl;
222 double dist = RealLast();
224 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
225 const TopoDS_Shape& aShapeFace = exp.Current();
226 if(aShapeFace==NotCheckedFace)
228 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements(aShapeFace);
229 if ( aSubMeshDSFace ) {
230 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
231 while ( iteratorElem->more() ) { // loop on elements on a face
232 const SMDS_MeshElement* face = iteratorElem->next();
233 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
234 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
235 int nbN = face->NbNodes();
236 if( face->IsQuadratic() )
238 for ( int i = 0; i < nbN; ++i ) {
239 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
240 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
242 if( HasIntersection(P, PC, Pres, aContour) ) {
244 double tmp = PC.Distance(Pres);
257 //=======================================================================
258 //function : EqualTriangles
259 //purpose : Auxilare for Compute()
260 //=======================================================================
261 static bool EqualTriangles(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
264 ( F1->GetNode(1)==F2->GetNode(2) && F1->GetNode(2)==F2->GetNode(1) ) ||
265 ( F1->GetNode(1)==F2->GetNode(1) && F1->GetNode(2)==F2->GetNode(2) );
268 //================================================================================
270 * \brief Prepare data for the given face
271 * \param PN - coordinates of face nodes
272 * \param VN - cross products of vectors (PC-PN(i)) ^ (PC-PN(i+1))
273 * \param FNodes - face nodes
274 * \param PC - gravity center of nodes
275 * \param VNorm - face normal (sum of VN)
276 * \param volumes - two volumes sharing the given face, the first is in VNorm direction
277 * \retval int - 0 if given face is not quad,
278 * 1 if given face is quad,
279 * 2 if given face is degenerate quad (two nodes are coincided)
281 //================================================================================
283 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
284 Handle(TColgp_HArray1OfPnt)& PN,
285 Handle(TColgp_HArray1OfVec)& VN,
286 vector<const SMDS_MeshNode*>& FNodes,
289 const SMDS_MeshElement** volumes)
291 if( face->NbNodes() != ( face->IsQuadratic() ? 8 : 4 ))
292 if( face->NbNodes() != 4 )
296 gp_XYZ xyzC(0., 0., 0.);
297 for ( i = 0; i < 4; ++i )
299 gp_XYZ p = SMESH_MeshEditor::TNodeXYZ( FNodes[i] = face->GetNode(i) );
300 PN->SetValue( i+1, p );
304 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
312 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
317 //int deg_num = IsDegenarate(PN);
321 //cout<<"find degeneration"<<endl;
323 gp_Pnt Pdeg = PN->Value(i);
325 list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
326 const SMDS_MeshNode* DegNode = 0;
327 for(; itdg!=myDegNodes.end(); itdg++) {
328 const SMDS_MeshNode* N = (*itdg);
329 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
330 if(Pdeg.Distance(Ptmp)<1.e-6) {
332 //DegNode = const_cast<SMDS_MeshNode*>(N);
337 DegNode = FNodes[i-1];
338 myDegNodes.push_back(DegNode);
341 FNodes[i-1] = DegNode;
344 PN->SetValue(i,PN->Value(i+1));
345 FNodes[i-1] = FNodes[i];
350 PN->SetValue(nbp+1,PN->Value(1));
351 FNodes[nbp] = FNodes[0];
352 // find normal direction
353 gp_Vec V1(PC,PN->Value(nbp));
354 gp_Vec V2(PC,PN->Value(1));
355 VNorm = V1.Crossed(V2);
356 VN->SetValue(nbp,VNorm);
357 for(i=1; i<nbp; i++) {
358 V1 = gp_Vec(PC,PN->Value(i));
359 V2 = gp_Vec(PC,PN->Value(i+1));
360 gp_Vec Vtmp = V1.Crossed(V2);
361 VN->SetValue(i,Vtmp);
365 // find volumes sharing the face
368 volumes[0] = volumes[1] = 0;
369 SMDS_ElemIteratorPtr vIt = FNodes[0]->GetInverseElementIterator( SMDSAbs_Volume );
370 while ( vIt->more() )
372 const SMDS_MeshElement* vol = vIt->next();
373 bool volSharesAllNodes = true;
374 for ( int i = 1; i < face->NbNodes() && volSharesAllNodes; ++i )
375 volSharesAllNodes = ( vol->GetNodeIndex( FNodes[i] ) >= 0 );
376 if ( volSharesAllNodes )
377 volumes[ volumes[0] ? 1 : 0 ] = vol;
378 // we could additionally check that vol has all FNodes in its one face using SMDS_VolumeTool
380 // define volume position relating to the face normal
384 SMDS_ElemIteratorPtr nodeIt = volumes[0]->nodesIterator();
386 volGC = accumulate( TXyzIterator(nodeIt), TXyzIterator(), volGC ) / volumes[0]->NbNodes();
388 if ( VNorm * gp_Vec( PC, volGC ) < 0 )
389 swap( volumes[0], volumes[1] );
393 //cout<<" VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
394 return hasdeg ? DEGEN_QUAD : QUAD;
398 //=======================================================================
401 //=======================================================================
403 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
406 myPyram2Trias.clear();
408 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
409 SMESH_MesherHelper helper(aMesh);
410 helper.IsQuadraticSubMesh(aShape);
411 helper.SetElementsOnShape( true );
413 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
414 const TopoDS_Shape& aShapeFace = exp.Current();
415 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
416 if ( aSubMeshDSFace ) {
417 bool isRev = SMESH_Algo::IsReversedSubMesh( TopoDS::Face(aShapeFace), meshDS );
419 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
420 while ( iteratorElem->more() ) { // loop on elements on a face
421 const SMDS_MeshElement* face = iteratorElem->next();
422 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
423 // preparation step using face info
424 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
425 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
426 vector<const SMDS_MeshNode*> FNodes(5);
429 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
435 // add triangles to result map
436 SMDS_FaceOfNodes* NewFace;
438 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
440 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
441 TTriaList aList( 1, NewFace );
442 myResMap.insert(make_pair(face,aList));
446 if(!isRev) VNorm.Reverse();
447 double xc = 0., yc = 0., zc = 0.;
452 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i).Reversed());
454 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
459 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
462 double height = PCbest.Distance(PC);
464 // create new PCbest using a bit shift along VNorm
465 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
468 // check possible intersection with other faces
470 bool check = CheckIntersection(PCbest, PC, Pint, aMesh, aShape, aShapeFace);
472 //cout<<"--PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
473 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
474 double dist = PC.Distance(Pint)/3.;
475 gp_Dir aDir(gp_Vec(PC,PCbest));
476 PCbest = PC.XYZ() + aDir.XYZ() * dist;
479 gp_Vec VB(PC,PCbest);
480 gp_Pnt PCbestTmp = PC.XYZ() + VB.XYZ() * 3.0;
481 bool check = CheckIntersection(PCbestTmp, PC, Pint, aMesh, aShape, aShapeFace);
483 double dist = PC.Distance(Pint)/3.;
485 gp_Dir aDir(gp_Vec(PC,PCbest));
486 PCbest = PC.XYZ() + aDir.XYZ() * dist;
491 // create node for PCbest
492 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
494 // add triangles to result map
495 TTriaList& triaList = myResMap.insert( make_pair( face, TTriaList() ))->second;
497 triaList.push_back( new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] ));
500 if ( isRev ) swap( FNodes[1], FNodes[3]);
501 SMDS_MeshVolume* aPyram =
502 helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
503 myPyram2Trias.insert(make_pair(aPyram, & triaList));
504 } // end loop on elements on a face submesh
506 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
508 return Compute2ndPart(aMesh);
512 //=======================================================================
515 //=======================================================================
517 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
520 myPyram2Trias.clear();
521 SMESH_MesherHelper helper(aMesh);
522 helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
523 helper.SetElementsOnShape( true );
525 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
527 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator();
528 TIDSortedElemSet sortedFaces; // 0020279: control the "random" use when using mesh algorithms
529 while( fIt->more()) sortedFaces.insert( fIt->next() );
531 TIDSortedElemSet::iterator itFace = sortedFaces.begin(), fEnd = sortedFaces.end();
532 for ( ; itFace != fEnd; ++itFace )
534 const SMDS_MeshElement* face = *itFace;
535 if ( !face ) continue;
536 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
537 // retrieve needed information about a face
538 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
539 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
540 vector<const SMDS_MeshNode*> FNodes(5);
543 const SMDS_MeshElement* volumes[2];
544 int what = Preparation(face, PN, VN, FNodes, PC, VNorm, volumes);
545 if ( what == NOT_QUAD )
547 if ( volumes[0] && volumes[1] )
548 continue; // face is shared by two volumes - no space for a pyramid
550 if ( what == DEGEN_QUAD )
553 // add triangles to result map
555 SMDS_FaceOfNodes* NewFace;
558 double tmp = PN->Value(1).Distance(PN->Value(2)) + PN->Value(2).Distance(PN->Value(3));
559 // far points in VNorm direction
560 gp_Pnt Ptmp1 = PC.XYZ() + VNorm.XYZ() * tmp * 1.e6;
561 gp_Pnt Ptmp2 = PC.XYZ() - VNorm.XYZ() * tmp * 1.e6;
562 // check intersection for Ptmp1 and Ptmp2
566 double dist1 = RealLast();
567 double dist2 = RealLast();
569 for (TIDSortedElemSet::iterator itF = sortedFaces.begin(); itF != fEnd; ++itF ) {
570 const SMDS_MeshElement* F = *itF;
571 if(F==face) continue;
572 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
573 for ( int i = 0; i < 4; ++i )
574 aContour->Append( SMESH_MeshEditor::TNodeXYZ( F->GetNode(i) ));
576 if( !volumes[0] && HasIntersection(Ptmp1, PC, PPP, aContour) ) {
578 double tmp = PC.Distance(PPP);
584 if( !volumes[1] && HasIntersection(Ptmp2, PC, PPP, aContour) ) {
586 double tmp = PC.Distance(PPP);
594 if( IsOK1 && !IsOK2 ) {
595 // using existed direction
597 else if( !IsOK1 && IsOK2 ) {
598 // using opposite direction
601 else { // IsOK1 && IsOK2
602 double tmp1 = PC.Distance(Pres1);
603 double tmp2 = PC.Distance(Pres2);
605 // using existed direction
608 // using opposite direction
613 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
615 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
616 aList.push_back(NewFace);
617 myResMap.insert(make_pair(face,aList));
623 gp_XYZ PCbest(0., 0., 0.); // pyramid peak
626 gp_Pnt Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
627 PCbest += Pbest.XYZ();
631 double height = PC.Distance(PCbest); // pyramid height to precise
633 // create new PCbest using a bit shift along VNorm
634 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
635 height = PC.Distance(PCbest);
637 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
639 // Restrict pyramid height by intersection with other faces
640 gp_Vec tmpDir(PC,PCbest); tmpDir.Normalize();
641 double tmp = PN->Value(1).Distance(PN->Value(3)) + PN->Value(2).Distance(PN->Value(4));
642 // far points: in (PC, PCbest) direction and vice-versa
643 gp_Pnt farPnt[2] = { PC.XYZ() + tmpDir.XYZ() * tmp * 1.e6,
644 PC.XYZ() - tmpDir.XYZ() * tmp * 1.e6 };
645 // check intersection for farPnt1 and farPnt2
646 bool intersected[2] = { false, false };
647 double dist [2] = { RealLast(), RealLast() };
649 for (TIDSortedElemSet::iterator itF = sortedFaces.begin(); itF != fEnd; ++itF )
651 const SMDS_MeshElement* F = *itF;
652 if(F==face) continue;
653 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
654 int nbN = F->NbNodes() / ( F->IsQuadratic() ? 2 : 1 );
655 for ( i = 0; i < nbN; ++i )
656 aContour->Append( SMESH_MeshEditor::TNodeXYZ( F->GetNode(i) ));
658 for ( int isRev = 0; isRev < 2; ++isRev )
660 if( !volumes[isRev] && HasIntersection(farPnt[isRev], PC, intP, aContour) ) {
661 intersected[isRev] = true;
662 double d = PC.Distance( intP );
663 if( d < dist[isRev] )
665 intPnt[isRev] = intP;
672 // Create one or two pyramids
674 for ( int isRev = 0; isRev < 2; ++isRev )
676 if( !intersected[isRev] ) continue;
677 double pyramidH = Min( height, PC.Distance(intPnt[isRev])/3.);
678 PCbest = PC.XYZ() + tmpDir.XYZ() * (isRev ? -pyramidH : pyramidH);
680 // create node for PCbest
681 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
683 // add triangles to result map
684 TTriaList& aList = myResMap.insert( make_pair( face, TTriaList()))->second;
686 SMDS_FaceOfNodes* NewFace;
688 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
690 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i+1], FNodes[i] );
691 aList.push_back(NewFace);
694 SMDS_MeshVolume* aPyram;
696 aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
698 aPyram = helper.AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
699 myPyram2Trias.insert(make_pair(aPyram, & aList));
701 } // end loop on all faces
703 return Compute2ndPart(aMesh);
706 //=======================================================================
707 //function : Compute2ndPart
708 //purpose : Update created pyramids and faces to avoid their intersection
709 //=======================================================================
711 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh)
713 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
715 // check intersections between created pyramids
717 if(myPyram2Trias.empty())
722 // for each pyramid store list of merged pyramids with their faces
723 typedef map< const SMDS_MeshElement*, list< TPyram2Trias::iterator > > TPyram2Merged;
724 TPyram2Merged MergesInfo;
726 // iterate on all pyramids
727 TPyram2Trias::iterator itPi = myPyram2Trias.begin(), itPEnd = myPyram2Trias.end();
728 for ( ; itPi != itPEnd; ++itPi )
730 const SMDS_MeshElement* PrmI = itPi->first;
731 TPyram2Merged::iterator pMergesI = MergesInfo.find( PrmI );
733 TXyzIterator xyzIt( PrmI->nodesIterator() );
734 vector<gp_Pnt> PsI( xyzIt, TXyzIterator() );
736 // compare PrmI with all the rest pyramids
737 bool NeedMove = false;
738 TPyram2Trias::iterator itPj = itPi;
739 for ( ++itPj; itPj != itPEnd; ++itPj )
741 const SMDS_MeshElement* PrmJ = itPj->first;
742 TPyram2Merged::iterator pMergesJ = MergesInfo.find( PrmJ );
744 // check if two pyramids already merged
745 if ( pMergesJ != MergesInfo.end() &&
746 find(pMergesJ->second.begin(),pMergesJ->second.end(), itPi )!=pMergesJ->second.end())
747 continue; // already merged
749 xyzIt = TXyzIterator( PrmJ->nodesIterator() );
750 vector<gp_Pnt> PsJ( xyzIt, TXyzIterator() );
754 for(k=0; k<4 && !hasInt; k++) {
755 gp_Vec Vtmp(PsI[k],PsI[4]);
756 gp_Pnt Pshift = PsI[k].XYZ() + Vtmp.XYZ() * 0.01;
758 ( HasIntersection3( Pshift, PsI[4], Pint, PsJ[0], PsJ[1], PsJ[4]) ||
759 HasIntersection3( Pshift, PsI[4], Pint, PsJ[1], PsJ[2], PsJ[4]) ||
760 HasIntersection3( Pshift, PsI[4], Pint, PsJ[2], PsJ[3], PsJ[4]) ||
761 HasIntersection3( Pshift, PsI[4], Pint, PsJ[3], PsJ[0], PsJ[4]) );
763 for(k=0; k<4 && !hasInt; k++) {
764 gp_Vec Vtmp(PsJ[k],PsJ[4]);
765 gp_Pnt Pshift = PsJ[k].XYZ() + Vtmp.XYZ() * 0.01;
767 ( HasIntersection3( Pshift, PsJ[4], Pint, PsI[0], PsI[1], PsI[4]) ||
768 HasIntersection3( Pshift, PsJ[4], Pint, PsI[1], PsI[2], PsI[4]) ||
769 HasIntersection3( Pshift, PsJ[4], Pint, PsI[2], PsI[3], PsI[4]) ||
770 HasIntersection3( Pshift, PsJ[4], Pint, PsI[3], PsI[0], PsI[4]) );
773 // count common nodes of base faces of two pyramids
776 nbc += int ( PrmI->GetNodeIndex( PrmJ->GetNode(k) ) >= 0 );
777 //cout<<" nbc = "<<nbc<<endl;
780 continue; // pyrams have a common base face
784 // Merge the two pyramids and others already merged with them
786 // initialize merge info of pyramids
787 if ( pMergesI == MergesInfo.end() ) // first merge of PrmI
789 pMergesI = MergesInfo.insert( make_pair( PrmI, list<TPyram2Trias::iterator >())).first;
790 pMergesI->second.push_back( itPi );
792 if ( pMergesJ == MergesInfo.end() ) // first merge of PrmJ
794 pMergesJ = MergesInfo.insert( make_pair( PrmJ, list<TPyram2Trias::iterator >())).first;
795 pMergesJ->second.push_back( itPj );
797 int nbI = pMergesI->second.size(), nbJ = pMergesJ->second.size();
799 // an apex node to make common to all merged pyramids
800 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
801 CommonNode->setXYZ( ( nbI*PsI[4].X() + nbJ*PsJ[4].X() ) / (nbI+nbJ),
802 ( nbI*PsI[4].Y() + nbJ*PsJ[4].Y() ) / (nbI+nbJ),
803 ( nbI*PsI[4].Z() + nbJ*PsJ[4].Z() ) / (nbI+nbJ) );
805 const SMDS_MeshNode* Nrem = PrmJ->GetNode(4); // node to remove
807 list< TPyram2Trias::iterator >& aMergesI = pMergesI->second;
808 list< TPyram2Trias::iterator >& aMergesJ = pMergesJ->second;
810 // find and remove coincided faces of merged pyramids
811 list< TPyram2Trias::iterator >::iterator itPttI, itPttJ;
812 TTriaList::iterator trI, trJ;
813 for ( itPttI = aMergesI.begin(); itPttI != aMergesI.end(); ++itPttI )
815 TTriaList* triaListI = (*itPttI)->second;
816 for ( trI = triaListI->begin(); trI != triaListI->end(); )
818 const SMDS_FaceOfNodes* FI = *trI;
820 for ( itPttJ = aMergesJ.begin(); itPttJ != aMergesJ.end() && FI; ++itPttJ )
822 TTriaList* triaListJ = (*itPttJ)->second;
823 for ( trJ = triaListJ->begin(); trJ != triaListJ->end(); )
825 const SMDS_FaceOfNodes* FJ = *trJ;
827 if( EqualTriangles(FI,FJ) )
832 trI = triaListI->erase( trI );
833 trJ = triaListJ->erase( trJ );
834 break; // only one triangle of a pyramid can coincide with another pyramid
839 if ( FI ) ++trI; // increament if triangle not deleted
843 // set the common apex node to pyramids and triangles merged with J
844 for ( itPttJ = aMergesJ.begin(); itPttJ != aMergesJ.end(); ++itPttJ )
846 const SMDS_MeshElement* Prm = (*itPttJ)->first;
847 TTriaList* triaList = (*itPttJ)->second;
849 vector< const SMDS_MeshNode* > nodes( Prm->begin_nodes(), Prm->end_nodes() );
850 nodes[4] = CommonNode;
851 meshDS->ChangeElementNodes( Prm, &nodes[0], nodes.size());
853 for ( TTriaList::iterator trIt = triaList->begin(); trIt != triaList->end(); ++trIt )
855 SMDS_FaceOfNodes* Ftria = const_cast< SMDS_FaceOfNodes*>( *trIt );
856 const SMDS_MeshNode* NF[3] = { CommonNode, Ftria->GetNode(1), Ftria->GetNode(2)};
857 Ftria->ChangeNodes(NF, 3);
861 // join MergesInfo of merged pyramids
862 for ( k = 0, itPttI = aMergesI.begin(); k < nbI; ++itPttI, ++k )
864 const SMDS_MeshElement* PrmI = (*itPttI)->first;
865 list< TPyram2Trias::iterator >& merges = MergesInfo[ PrmI ];
866 merges.insert( merges.end(), aMergesJ.begin(), aMergesJ.end() );
868 for ( k = 0, itPttJ = aMergesJ.begin(); k < nbJ; ++itPttJ, ++k )
870 const SMDS_MeshElement* PrmJ = (*itPttJ)->first;
871 list< TPyram2Trias::iterator >& merges = MergesInfo[ PrmJ ];
872 merges.insert( merges.end(), aMergesI.begin(), aMergesI.end() );
876 meshDS->RemoveNode(Nrem);
880 // decrease height of pyramids
881 gp_XYZ PC1(0,0,0), PC2(0,0,0);
887 gp_Vec VN1(PC1,PsI[4]);
888 gp_Vec VI1(PC1,Pint);
889 gp_Vec VN2(PC2,PsJ[4]);
890 gp_Vec VI2(PC2,Pint);
891 double ang1 = fabs(VN1.Angle(VI1));
892 double ang2 = fabs(VN2.Angle(VI2));
895 h1 = VI1.Magnitude()/2;
897 h1 = VI1.Magnitude()*cos(ang1);
899 h2 = VI2.Magnitude()/2;
901 h2 = VI2.Magnitude()*cos(ang2);
904 coef1 -= cos(ang1)*0.25;
907 coef2 -= cos(ang1)*0.25;
911 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>(PrmI->GetNode(4));
912 aNode1->setXYZ( PC1.X()+VN1.X(), PC1.Y()+VN1.Y(), PC1.Z()+VN1.Z() );
913 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(PrmJ->GetNode(4));
914 aNode2->setXYZ( PC2.X()+VN2.X(), PC2.Y()+VN2.Y(), PC2.Z()+VN2.Z() );
919 if( NeedMove && !meshDS->IsEmbeddedMode() )
921 const SMDS_MeshNode* apex = PrmI->GetNode( 4 );
922 meshDS->MoveNode( apex, apex->X(), apex->Y(), apex->Z() );
926 // rebind triangles of pyramids sharing the same base quadrangle to the first
927 // entrance of the base quadrangle
928 TQuad2Trias::iterator q2t = myResMap.begin(), q2tPrev = q2t;
929 for ( ++q2t; q2t != myResMap.end(); ++q2t, ++q2tPrev )
931 if ( q2t->first == q2tPrev->first )
932 q2tPrev->second.splice( q2tPrev->second.end(), q2t->second );
935 myPyram2Trias.clear(); // no more needed
941 //================================================================================
943 * \brief Return list of created triangles for given face
945 //================================================================================
947 const list<const SMDS_FaceOfNodes* >* StdMeshers_QuadToTriaAdaptor::GetTriangles (const SMDS_MeshElement* aQuad)
949 TQuad2Trias::iterator it = myResMap.find(aQuad);
950 if( it != myResMap.end() ) {