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 <SMDS_FaceOfNodes.hxx>
31 #include <SMESH_Algo.hxx>
32 #include <SMESH_MesherHelper.hxx>
34 #include <IntAna_IntConicQuad.hxx>
35 #include <IntAna_Quadric.hxx>
36 #include <TColStd_SequenceOfInteger.hxx>
37 #include <TColgp_HSequenceOfPnt.hxx>
38 #include <TopExp_Explorer.hxx>
43 #include <NCollection_Array1.hxx>
44 typedef NCollection_Array1<TColStd_SequenceOfInteger> StdMeshers_Array1OfSequenceOfInteger;
47 //=======================================================================
48 //function : StdMeshers_QuadToTriaAdaptor
50 //=======================================================================
52 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor()
57 //================================================================================
61 //================================================================================
63 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
65 // delete temporary faces
66 map< const SMDS_MeshElement*, list<const SMDS_FaceOfNodes*> >::iterator
67 f_f = myResMap.begin(), ffEnd = myResMap.end();
68 for ( ; f_f != ffEnd; ++f_f )
70 list<const SMDS_FaceOfNodes*>& fList = f_f->second;
71 list<const SMDS_FaceOfNodes*>::iterator f = fList.begin(), fEnd = fList.end();
72 for ( ; f != fEnd; ++f )
77 // TF2PyramMap::iterator itp = myMapFPyram.begin();
78 // for(; itp!=myMapFPyram.end(); itp++)
79 // cout << itp->second << endl;
83 //=======================================================================
84 //function : FindBestPoint
85 //purpose : Auxilare for Compute()
86 // V - normal to (P1,P2,PC)
87 //=======================================================================
88 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
89 const gp_Pnt& PC, const gp_Vec& V)
91 double a = P1.Distance(P2);
92 double b = P1.Distance(PC);
93 double c = P2.Distance(PC);
97 // find shift along V in order to a became equal to (b+c)/2
98 double shift = sqrt( a*a + (b*b-c*c)*(b*b-c*c)/16/a/a - (b*b+c*c)/2 );
100 gp_Pnt Pbest( PC.X() + aDir.X()*shift, PC.Y() + aDir.Y()*shift,
101 PC.Z() + aDir.Z()*shift );
107 //=======================================================================
108 //function : HasIntersection3
109 //purpose : Auxilare for HasIntersection()
110 // find intersection point between triangle (P1,P2,P3)
111 // and segment [PC,P]
112 //=======================================================================
113 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
114 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
116 //cout<<"HasIntersection3"<<endl;
117 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
118 //cout<<" P("<<P.X()<<","<<P.Y()<<","<<P.Z()<<")"<<endl;
119 //cout<<" P1("<<P1.X()<<","<<P1.Y()<<","<<P1.Z()<<")"<<endl;
120 //cout<<" P2("<<P2.X()<<","<<P2.Y()<<","<<P2.Z()<<")"<<endl;
121 //cout<<" P3("<<P3.X()<<","<<P3.Y()<<","<<P3.Z()<<")"<<endl;
124 IntAna_Quadric IAQ(gp_Pln(P1,VP1.Crossed(VP2)));
125 IntAna_IntConicQuad IAICQ(gp_Lin(PC,gp_Dir(gp_Vec(PC,P))),IAQ);
127 if( IAICQ.IsInQuadric() )
129 if( IAICQ.NbPoints() == 1 ) {
130 gp_Pnt PIn = IAICQ.Point(1);
131 double preci = 1.e-6;
132 // check if this point is internal for segment [PC,P]
134 ( (PC.X()-PIn.X())*(P.X()-PIn.X()) > preci ) ||
135 ( (PC.Y()-PIn.Y())*(P.Y()-PIn.Y()) > preci ) ||
136 ( (PC.Z()-PIn.Z())*(P.Z()-PIn.Z()) > preci );
140 // check if this point is internal for triangle (P1,P2,P3)
144 if( V1.Magnitude()<preci || V2.Magnitude()<preci ||
145 V3.Magnitude()<preci ) {
149 gp_Vec VC1 = V1.Crossed(V2);
150 gp_Vec VC2 = V2.Crossed(V3);
151 gp_Vec VC3 = V3.Crossed(V1);
152 if(VC1.Magnitude()<preci) {
153 if(VC2.IsOpposite(VC3,preci)) {
157 else if(VC2.Magnitude()<preci) {
158 if(VC1.IsOpposite(VC3,preci)) {
162 else if(VC3.Magnitude()<preci) {
163 if(VC1.IsOpposite(VC2,preci)) {
168 if( VC1.IsOpposite(VC2,preci) || VC1.IsOpposite(VC3,preci) ||
169 VC2.IsOpposite(VC3,preci) ) {
182 //=======================================================================
183 //function : HasIntersection
184 //purpose : Auxilare for CheckIntersection()
185 //=======================================================================
186 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
187 Handle(TColgp_HSequenceOfPnt)& aContour)
189 if(aContour->Length()==3) {
190 return HasIntersection3( P, PC, Pint, aContour->Value(1),
191 aContour->Value(2), aContour->Value(3) );
195 if( (aContour->Value(1).Distance(aContour->Value(2)) > 1.e-6) &&
196 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
197 (aContour->Value(2).Distance(aContour->Value(3)) > 1.e-6) ) {
198 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
199 aContour->Value(2), aContour->Value(3) );
201 if(check) return true;
202 if( (aContour->Value(1).Distance(aContour->Value(4)) > 1.e-6) &&
203 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
204 (aContour->Value(4).Distance(aContour->Value(3)) > 1.e-6) ) {
205 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
206 aContour->Value(3), aContour->Value(4) );
208 if(check) return true;
215 //=======================================================================
216 //function : CheckIntersection
217 //purpose : Auxilare for Compute()
218 // NotCheckedFace - for optimization
219 //=======================================================================
220 bool StdMeshers_QuadToTriaAdaptor::CheckIntersection
221 (const gp_Pnt& P, const gp_Pnt& PC,
222 gp_Pnt& Pint, SMESH_Mesh& aMesh,
223 const TopoDS_Shape& aShape,
224 const TopoDS_Shape& NotCheckedFace)
226 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
227 //cout<<" CheckIntersection: meshDS->NbFaces() = "<<meshDS->NbFaces()<<endl;
229 double dist = RealLast();
231 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
232 const TopoDS_Shape& aShapeFace = exp.Current();
233 if(aShapeFace==NotCheckedFace)
235 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements(aShapeFace);
236 if ( aSubMeshDSFace ) {
237 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
238 while ( iteratorElem->more() ) { // loop on elements on a face
239 const SMDS_MeshElement* face = iteratorElem->next();
240 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
241 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
242 int nbN = face->NbNodes();
243 if( face->IsQuadratic() )
245 for ( int i = 0; i < nbN; ++i ) {
246 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
247 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
249 if( HasIntersection(P, PC, Pres, aContour) ) {
251 double tmp = PC.Distance(Pres);
264 //=======================================================================
265 //function : CompareTrias
266 //purpose : Auxilare for Compute()
267 //=======================================================================
268 static bool CompareTrias(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
271 ( F1->GetNode(1)==F2->GetNode(2) && F1->GetNode(2)==F2->GetNode(1) ) ||
272 ( F1->GetNode(1)==F2->GetNode(1) && F1->GetNode(2)==F2->GetNode(2) );
276 //=======================================================================
277 //function : IsDegenarate
278 //purpose : Auxilare for Preparation()
279 //=======================================================================
280 // static int IsDegenarate(const Handle(TColgp_HArray1OfPnt)& PN)
285 // for(; j<=4; j++) {
286 // if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
294 //=======================================================================
295 //function : Preparation
296 //purpose : Auxilare for Compute()
297 // : Return 0 if given face is not quad,
298 // 1 if given face is quad,
299 // 2 if given face is degenerate quad (two nodes are coincided)
300 //=======================================================================
301 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
302 Handle(TColgp_HArray1OfPnt)& PN,
303 Handle(TColgp_HArray1OfVec)& VN,
304 std::vector<const SMDS_MeshNode*>& FNodes,
305 gp_Pnt& PC, gp_Vec& VNorm)
308 double xc=0., yc=0., zc=0.;
309 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
310 if( !face->IsQuadratic() ) {
311 if( face->NbNodes() != 4 )
313 while ( nodeIt->more() ) {
315 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
317 PN->SetValue( i, gp_Pnt(node->X(), node->Y(), node->Z()) );
324 if( face->NbNodes() != 8)
326 while ( nodeIt->more() ) {
328 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
330 PN->SetValue( i, gp_Pnt(node->X(), node->Y(), node->Z()) );
344 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
349 //int deg_num = IsDegenarate(PN);
353 //cout<<"find degeneration"<<endl;
355 gp_Pnt Pdeg = PN->Value(i);
357 std::list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
358 const SMDS_MeshNode* DegNode = 0;
359 for(; itdg!=myDegNodes.end(); itdg++) {
360 const SMDS_MeshNode* N = (*itdg);
361 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
362 if(Pdeg.Distance(Ptmp)<1.e-6) {
364 //DegNode = const_cast<SMDS_MeshNode*>(N);
369 DegNode = FNodes[i-1];
370 myDegNodes.push_back(DegNode);
373 FNodes[i-1] = DegNode;
376 PN->SetValue(i,PN->Value(i+1));
377 FNodes[i-1] = FNodes[i];
380 //PC = gp_Pnt( PN->Value(1).X() + PN.Value
383 PC = gp_Pnt(xc/4., yc/4., zc/4.);
384 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
386 //PN->SetValue(5,PN->Value(1));
387 PN->SetValue(nbp+1,PN->Value(1));
388 //FNodes[4] = FNodes[0];
389 FNodes[nbp] = FNodes[0];
390 // find normal direction
391 //gp_Vec V1(PC,PN->Value(4));
392 gp_Vec V1(PC,PN->Value(nbp));
393 gp_Vec V2(PC,PN->Value(1));
394 VNorm = V1.Crossed(V2);
395 //VN->SetValue(4,VNorm);
396 VN->SetValue(nbp,VNorm);
397 //for(i=1; i<4; i++) {
398 for(i=1; i<nbp; i++) {
399 V1 = gp_Vec(PC,PN->Value(i));
400 V2 = gp_Vec(PC,PN->Value(i+1));
401 gp_Vec Vtmp = V1.Crossed(V2);
402 VN->SetValue(i,Vtmp);
405 //cout<<" VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
411 //=======================================================================
414 //=======================================================================
416 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
421 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
422 SMESH_MesherHelper helper(aMesh);
423 helper.IsQuadraticSubMesh(aShape);
424 helper.SetElementsOnShape( true );
426 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
427 const TopoDS_Shape& aShapeFace = exp.Current();
428 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
429 if ( aSubMeshDSFace ) {
430 bool isRev = SMESH_Algo::IsReversedSubMesh( TopoDS::Face(aShapeFace), meshDS );
432 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
433 while ( iteratorElem->more() ) { // loop on elements on a face
434 const SMDS_MeshElement* face = iteratorElem->next();
435 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
436 // preparation step using face info
437 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
438 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
439 std::vector<const SMDS_MeshNode*> FNodes(5);
442 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
448 // add triangles to result map
449 std::list<const SMDS_FaceOfNodes*> aList;
450 SMDS_FaceOfNodes* NewFace;
452 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
454 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
455 aList.push_back(NewFace);
456 myResMap.insert(make_pair(face,aList));
460 if(!isRev) VNorm.Reverse();
461 double xc = 0., yc = 0., zc = 0.;
466 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i).Reversed());
468 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
473 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
476 double height = PCbest.Distance(PC);
478 // create new PCbest using a bit shift along VNorm
479 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
482 // check possible intersection with other faces
484 bool check = CheckIntersection(PCbest, PC, Pint, aMesh, aShape, aShapeFace);
486 //cout<<"--PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
487 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
488 double dist = PC.Distance(Pint)/3.;
489 gp_Dir aDir(gp_Vec(PC,PCbest));
490 PCbest = PC.XYZ() + aDir.XYZ() * dist;
493 gp_Vec VB(PC,PCbest);
494 gp_Pnt PCbestTmp = PC.XYZ() + VB.XYZ() * 3.0;
495 bool check = CheckIntersection(PCbestTmp, PC, Pint, aMesh, aShape, aShapeFace);
497 double dist = PC.Distance(Pint)/3.;
499 gp_Dir aDir(gp_Vec(PC,PCbest));
500 PCbest = PC.XYZ() + aDir.XYZ() * dist;
505 // create node for PCbest
506 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
507 // add triangles to result map
508 std::list<const SMDS_FaceOfNodes*> aList;
510 SMDS_FaceOfNodes* NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
511 aList.push_back(NewFace);
513 myResMap.insert(make_pair(face,aList));
515 SMDS_MeshVolume* aPyram =
516 helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
517 myMapFPyram.insert(make_pair(face,aPyram));
518 } // end loop on elements on a face
520 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
522 return Compute2ndPart(aMesh);
526 //=======================================================================
529 //=======================================================================
531 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
535 SMESH_MesherHelper helper(aMesh);
536 helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
537 helper.SetElementsOnShape( true );
539 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
541 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator();
542 TIDSortedElemSet sortedFaces; // 0020279: control the "random" use when using mesh algorithms
543 while( fIt->more()) sortedFaces.insert( fIt->next() );
545 TIDSortedElemSet::iterator itFace = sortedFaces.begin(), fEnd = sortedFaces.end();
546 for ( ; itFace != fEnd; ++itFace )
548 const SMDS_MeshElement* face = *itFace;
549 if ( !face ) continue;
550 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
551 // preparation step using face info
552 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
553 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
554 std::vector<const SMDS_MeshNode*> FNodes(5);
558 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
564 // add triangles to result map
565 std::list<const SMDS_FaceOfNodes*> aList;
566 SMDS_FaceOfNodes* NewFace;
569 double tmp = PN->Value(1).Distance(PN->Value(2)) +
570 PN->Value(2).Distance(PN->Value(3));
571 gp_Pnt Ptmp1 = PC.XYZ() + VNorm.XYZ() * tmp * 1.e6;
572 gp_Pnt Ptmp2 = PC.XYZ() - VNorm.XYZ() * tmp * 1.e6;
573 // check intersection for Ptmp1 and Ptmp2
577 double dist1 = RealLast();
578 double dist2 = RealLast();
580 for (TIDSortedElemSet::iterator itF = sortedFaces.begin(); itF != fEnd; ++itF ) {
581 const SMDS_MeshElement* F = *itF;
582 if(F==face) continue;
583 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
584 SMDS_ElemIteratorPtr nodeIt = F->nodesIterator();
585 if( !F->IsQuadratic() ) {
586 while ( nodeIt->more() ) {
587 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
588 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
593 while ( nodeIt->more() ) {
595 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
596 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
597 if(nn==face->NbNodes()/2) break;
601 if( HasIntersection(Ptmp1, PC, PPP, aContour) ) {
603 double tmp = PC.Distance(PPP);
609 if( HasIntersection(Ptmp2, PC, PPP, aContour) ) {
611 double tmp = PC.Distance(PPP);
619 if( IsOK1 && !IsOK2 ) {
620 // using existed direction
622 else if( !IsOK1 && IsOK2 ) {
623 // using opposite direction
626 else { // IsOK1 && IsOK2
627 double tmp1 = PC.Distance(Pres1)/3.;
628 double tmp2 = PC.Distance(Pres2)/3.;
630 // using existed direction
633 // using opposite direction
638 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
640 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
641 aList.push_back(NewFace);
642 myResMap.insert(make_pair(face,aList));
646 double xc = 0., yc = 0., zc = 0.;
649 gp_Pnt Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
654 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
655 double height = PCbest.Distance(PC);
657 // create new PCbest using a bit shift along VNorm
658 PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
659 height = PCbest.Distance(PC);
661 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
663 gp_Vec V1(PC,PCbest);
664 double tmp = PN->Value(1).Distance(PN->Value(3)) +
665 PN->Value(2).Distance(PN->Value(4));
667 gp_Pnt Ptmp1 = PC.XYZ() + tmpDir.XYZ() * tmp * 1.e6;
668 gp_Pnt Ptmp2 = PC.XYZ() - tmpDir.XYZ() * tmp * 1.e6;
669 // check intersection for Ptmp1 and Ptmp2
673 double dist1 = RealLast();
674 double dist2 = RealLast();
676 for (TIDSortedElemSet::iterator itF = sortedFaces.begin(); itF != fEnd; ++itF ) {
677 const SMDS_MeshElement* F = *itF;
678 if(F==face) continue;
679 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
680 SMDS_ElemIteratorPtr nodeIt = F->nodesIterator();
681 if( !F->IsQuadratic() ) {
682 while ( nodeIt->more() ) {
683 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
684 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
689 while ( nodeIt->more() ) {
691 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
692 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
693 if(nn==face->NbNodes()/2) break;
697 if( HasIntersection(Ptmp1, PC, PPP, aContour) ) {
699 double tmp = PC.Distance(PPP);
705 if( HasIntersection(Ptmp2, PC, PPP, aContour) ) {
707 double tmp = PC.Distance(PPP);
715 if( IsOK1 && !IsOK2 ) {
716 // using existed direction
717 double tmp = PC.Distance(Pres1)/3.;
720 PCbest = PC.XYZ() + tmpDir.XYZ() * height;
723 else if( !IsOK1 && IsOK2 ) {
724 // using opposite direction
726 double tmp = PC.Distance(Pres2)/3.;
727 if( height > tmp ) height = tmp;
728 PCbest = PC.XYZ() - tmpDir.XYZ() * height;
730 else { // IsOK1 && IsOK2
731 double tmp1 = PC.Distance(Pres1)/3.;
732 double tmp2 = PC.Distance(Pres2)/3.;
734 // using existed direction
735 if( height > tmp1 ) {
737 PCbest = PC.XYZ() + tmpDir.XYZ() * height;
741 // using opposite direction
743 if( height > tmp2 ) height = tmp2;
744 PCbest = PC.XYZ() - tmpDir.XYZ() * height;
748 // create node for PCbest
749 SMDS_MeshNode* NewNode = helper.AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
750 // add triangles to result map
751 std::list<const SMDS_FaceOfNodes*> aList;
753 SMDS_FaceOfNodes* NewFace;
755 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
757 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i+1], FNodes[i] );
758 aList.push_back(NewFace);
760 myResMap.insert(make_pair(face,aList));
762 SMDS_MeshVolume* aPyram;
764 aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
766 aPyram = helper.AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
767 myMapFPyram.insert(make_pair(face,aPyram));
768 } // end loop on elements on a face
770 return Compute2ndPart(aMesh);
774 //=======================================================================
775 //function : Compute2ndPart
777 //=======================================================================
779 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh)
781 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
783 // check intersections between created pyramids
784 int NbPyram = myMapFPyram.size();
785 //cout<<"NbPyram = "<<NbPyram<<endl;
789 vector< const SMDS_MeshElement* > Pyrams(NbPyram);
790 vector< const SMDS_MeshElement* > Faces(NbPyram);
791 TF2PyramMap::iterator itp = myMapFPyram.begin();
793 for(; itp!=myMapFPyram.end(); itp++, i++) {
794 Faces[i] = (*itp).first;
795 Pyrams[i] = (*itp).second;
797 StdMeshers_Array1OfSequenceOfInteger MergesInfo(0,NbPyram-1);
798 for(i=0; i<NbPyram; i++) {
799 TColStd_SequenceOfInteger aMerges;
801 MergesInfo.SetValue(i,aMerges);
803 for(i=0; i<NbPyram-1; i++) {
804 const SMDS_MeshElement* Prm1 = Pyrams[i];
805 SMDS_ElemIteratorPtr nIt = Prm1->nodesIterator();
806 std::vector<gp_Pnt> Ps1( Prm1->NbNodes() );
807 vector< const SMDS_MeshNode* > Ns1( Prm1->NbNodes() );
809 for ( ; k < Ns1.size(); ++k ) {
810 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nIt->next() );
812 Ps1[k] = gp_Pnt(node->X(), node->Y(), node->Z());
814 bool NeedMove = false;
815 for(int j=i+1; j<NbPyram; j++) {
816 //cout<<" i="<<i<<" j="<<j<<endl;
817 const TColStd_SequenceOfInteger& aMergesI = MergesInfo.Value(i);
818 int nbI = aMergesI.Length();
819 const TColStd_SequenceOfInteger& aMergesJ = MergesInfo.Value(j);
820 int nbJ = aMergesJ.Length();
821 // check if two pyramids already merged
822 bool NeedCont = false;
823 for( k = 2; k<=nbI; k++) {
824 if(aMergesI.Value(k)==j) {
829 if(NeedCont) continue; // already merged
831 const SMDS_MeshElement* Prm2 = Pyrams[j];
832 nIt = Prm2->nodesIterator();
833 vector<gp_Pnt> Ps2( Prm2->NbNodes() );
834 vector<const SMDS_MeshNode*> Ns2( Prm2->NbNodes() );
835 for ( k = 0; k < Ns2.size(); ++k ) {
836 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nIt->next() );
838 Ps2[k] = gp_Pnt(node->X(), node->Y(), node->Z());
844 gp_Vec Vtmp(Ps1[k],Ps1[4]);
845 gp_Pnt Pshift = Ps1[k].XYZ() + Vtmp.XYZ() * 0.01;
848 if( HasIntersection3( Pshift, Ps1[4], Pint, Ps2[m], Ps2[m+1], Ps2[4]) ) {
853 if( HasIntersection3( Pshift, Ps1[4], Pint, Ps2[3], Ps2[0], Ps2[4]) ) {
860 gp_Vec Vtmp(Ps2[k],Ps2[4]);
861 gp_Pnt Pshift = Ps2[k].XYZ() + Vtmp.XYZ() * 0.01;
864 if( HasIntersection3( Pshift, Ps2[4], Pint, Ps1[m], Ps1[m+1], Ps1[4]) ) {
869 if( HasIntersection3( Pshift, Ps2[4], Pint, Ps1[3], Ps1[0], Ps1[4]) ) {
877 //cout<<" has intersec for i="<<i<<" j="<<j<<endl;
878 // check if MeshFaces have 2 common node
881 for(int m=0; m<4; m++) {
882 if( Ns1[k]==Ns2[m] ) nbc++;
885 //cout<<" nbc = "<<nbc<<endl;
887 // create common node
888 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(Ns1[4]);
889 CommonNode->setXYZ( ( nbI*Ps1[4].X() + nbJ*Ps2[4].X() ) / (nbI+nbJ),
890 ( nbI*Ps1[4].Y() + nbJ*Ps2[4].Y() ) / (nbI+nbJ),
891 ( nbI*Ps1[4].Z() + nbJ*Ps2[4].Z() ) / (nbI+nbJ) );
893 //cout<<" CommonNode: "<<CommonNode;
894 const SMDS_MeshNode* Nrem = Ns2[4];
896 meshDS->ChangeElementNodes(Prm2, &Ns2[0], Ns2.size());
897 // update pyramids for J
898 for(k=2; k<=nbJ; k++) {
899 const SMDS_MeshElement* tmpPrm = Pyrams[aMergesJ.Value(k)];
900 SMDS_ElemIteratorPtr tmpIt = tmpPrm->nodesIterator();
901 vector< const SMDS_MeshNode* > Ns( tmpPrm->NbNodes() );
902 for ( int m = 0; m < Ns.size(); ++m )
903 Ns[m] = static_cast<const SMDS_MeshNode*>( tmpIt->next() );
905 meshDS->ChangeElementNodes(tmpPrm, &Ns[0], Ns.size());
909 for(k=1; k<=nbI; k++) {
910 int num = aMergesI.Value(k);
911 TColStd_SequenceOfInteger& aSeq = MergesInfo.ChangeValue(num);
912 for(int m=1; m<=nbJ; m++)
913 aSeq.Append(aMergesJ.Value(m));
915 for(k=1; k<=nbJ; k++) {
916 int num = aMergesJ.Value(k);
917 TColStd_SequenceOfInteger& aSeq = MergesInfo.ChangeValue(num);
918 for(int m=1; m<=nbI; m++)
919 aSeq.Append(aMergesI.Value(m));
922 // update triangles for aMergesJ
923 for(k=1; k<=nbJ; k++) {
924 list< list< const SMDS_MeshNode* > > aFNodes;
925 list< const SMDS_MeshElement* > aFFaces;
926 int num = aMergesJ.Value(k);
927 map< const SMDS_MeshElement*,
928 list<const SMDS_FaceOfNodes*> >::iterator itrm = myResMap.find(Faces[num]);
929 list<const SMDS_FaceOfNodes*>& trias = itrm->second;
930 list<const SMDS_FaceOfNodes*>::iterator itt = trias.begin();
931 for(; itt!=trias.end(); itt++) {
932 SMDS_ElemIteratorPtr nodeIt = (*itt)->nodesIterator();
933 const SMDS_MeshNode* NF[3];
935 while ( nodeIt->more() )
936 NF[nn++] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
938 SMDS_FaceOfNodes* Ftria = const_cast< SMDS_FaceOfNodes*>( (*itt) );
939 Ftria->ChangeNodes(NF, 3);
943 // check and remove coincided faces
944 //TColStd_SequenceOfInteger IdRemovedTrias;
946 for(; i1<=nbI; i1++) {
947 int numI = aMergesI.Value(i1);
948 map< const SMDS_MeshElement*,
949 list<const SMDS_FaceOfNodes*> >::iterator itrmI = myResMap.find(Faces[numI]);
950 list<const SMDS_FaceOfNodes*>& triasI = (*itrmI).second;
951 list<const SMDS_FaceOfNodes*>::iterator ittI = triasI.begin();
952 int nbfI = triasI.size();
953 vector<const SMDS_FaceOfNodes*> FsI(nbfI);
955 for(; ittI!=triasI.end(); ittI++) {
960 for(; i2<nbfI; i2++) {
961 const SMDS_FaceOfNodes* FI = FsI[i2];
964 for(; j1<=nbJ; j1++) {
965 int numJ = aMergesJ.Value(j1);
966 map< const SMDS_MeshElement*,
967 list<const SMDS_FaceOfNodes*> >::iterator itrmJ = myResMap.find(Faces[numJ]);
968 list<const SMDS_FaceOfNodes*>& triasJ = (*itrmJ).second;
969 list<const SMDS_FaceOfNodes*>::iterator ittJ = triasJ.begin();
970 int nbfJ = triasJ.size();
971 vector<const SMDS_FaceOfNodes*> FsJ(nbfJ);
973 for(; ittJ!=triasJ.end(); ittJ++) {
978 for(; j2<nbfJ; j2++) {
979 const SMDS_FaceOfNodes* FJ = FsJ[j2];
981 if( CompareTrias(FI,FJ) ) {
982 //IdRemovedTrias.Append( FI->GetID() );
983 //IdRemovedTrias.Append( FJ->GetID() );
986 list<const SMDS_FaceOfNodes*> new_triasI;
987 for(k=0; k<nbfI; k++) {
988 if( FsI[k]==0 ) continue;
989 new_triasI.push_back( FsI[k] );
991 (*itrmI).second = new_triasI;
993 list<const SMDS_FaceOfNodes*> new_triasJ;
994 for(k=0; k<nbfJ; k++) {
995 if( FsJ[k]==0 ) continue;
996 new_triasJ.push_back( FsJ[k] );
998 (*itrmJ).second = new_triasJ;
1003 // close for j2 and j1
1012 meshDS->RemoveNode(Nrem);
1015 //cout<<"decrease height of pyramids"<<endl;
1016 // decrease height of pyramids
1017 double xc1 = 0., yc1 = 0., zc1 = 0.;
1018 double xc2 = 0., yc2 = 0., zc2 = 0.;
1019 for(k=0; k<4; k++) {
1027 gp_Pnt PC1(xc1/4.,yc1/4.,zc1/4.);
1028 gp_Pnt PC2(xc2/4.,yc2/4.,zc2/4.);
1029 gp_Vec VN1(PC1,Ps1[4]);
1030 gp_Vec VI1(PC1,Pint);
1031 gp_Vec VN2(PC2,Ps2[4]);
1032 gp_Vec VI2(PC2,Pint);
1033 double ang1 = fabs(VN1.Angle(VI1));
1034 double ang2 = fabs(VN2.Angle(VI2));
1037 h1 = VI1.Magnitude()/2;
1039 h1 = VI1.Magnitude()*cos(ang1);
1041 h2 = VI2.Magnitude()/2;
1043 h2 = VI2.Magnitude()*cos(ang2);
1046 coef1 -= cos(ang1)*0.25;
1049 coef2 -= cos(ang1)*0.25;
1051 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>(Ns1[4]);
1053 aNode1->setXYZ( PC1.X()+VN1.X(), PC1.Y()+VN1.Y(), PC1.Z()+VN1.Z() );
1054 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(Ns2[4]);
1056 aNode2->setXYZ( PC2.X()+VN2.X(), PC2.Y()+VN2.Y(), PC2.Z()+VN2.Z() );
1061 //cout<<" no intersec for i="<<i<<" j="<<j<<endl;
1065 if( NeedMove && !meshDS->IsEmbeddedMode() ) {
1066 meshDS->MoveNode( Ns1[4], Ns1[4]->X(), Ns1[4]->Y(), Ns1[4]->Z() );
1074 //================================================================================
1076 * \brief Return list of created triangles for given face
1078 //================================================================================
1079 const list<const SMDS_FaceOfNodes*>* StdMeshers_QuadToTriaAdaptor::GetTriangles
1080 (const SMDS_MeshElement* aFace)
1082 map< const SMDS_MeshElement*,
1083 list<const SMDS_FaceOfNodes*> >::iterator it = myResMap.find(aFace);
1084 if( it != myResMap.end() ) {
1085 return & it->second;
1091 //================================================================================
1093 * \brief Remove all create auxilary faces
1095 //================================================================================
1096 //void StdMeshers_QuadToTriaAdaptor::RemoveFaces(SMESH_Mesh& aMesh)
1098 // SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1099 // map< const SMDS_MeshElement*,
1100 // list<const SMDS_MeshElement*> >::iterator it = myResMap.begin();
1101 // for(; it != myResMap.end(); it++ ) {
1102 // list<const SMDS_MeshElement*> aFaces = (*it).second;
1103 // list<const SMDS_MeshElement*>::iterator itf = aFaces.begin();
1104 // for(; itf!=aFaces.end(); itf++ ) {
1105 // meshDS->RemoveElement( (*itf) );