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 <TColgp_HArray1OfPnt.hxx>
31 //#include <TColgp_HArray1OfVec.hxx>
32 #include <TopExp_Explorer.hxx>
34 #include <SMESH_Algo.hxx>
35 #include <TColgp_HSequenceOfPnt.hxx>
36 #include <TColStd_MapOfInteger.hxx>
37 #include <TColStd_HSequenceOfInteger.hxx>
38 #include <IntAna_Quadric.hxx>
39 #include <IntAna_IntConicQuad.hxx>
42 #include <SMDS_FaceOfNodes.hxx>
44 #include <NCollection_Array1.hxx>
45 typedef NCollection_Array1<TColStd_SequenceOfInteger> StdMeshers_Array1OfSequenceOfInteger;
48 //=======================================================================
49 //function : StdMeshers_QuadToTriaAdaptor
51 //=======================================================================
53 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor()
58 //================================================================================
62 //================================================================================
64 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
68 //=======================================================================
69 //function : FindBestPoint
70 //purpose : Auxilare for Compute()
71 // V - normal to (P1,P2,PC)
72 //=======================================================================
73 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
74 const gp_Pnt& PC, const gp_Vec& V)
76 double a = P1.Distance(P2);
77 double b = P1.Distance(PC);
78 double c = P2.Distance(PC);
82 // find shift along V in order to a became equal to (b+c)/2
83 double shift = sqrt( a*a + (b*b-c*c)*(b*b-c*c)/16/a/a - (b*b+c*c)/2 );
85 gp_Pnt Pbest( PC.X() + aDir.X()*shift, PC.Y() + aDir.Y()*shift,
86 PC.Z() + aDir.Z()*shift );
92 //=======================================================================
93 //function : HasIntersection3
94 //purpose : Auxilare for HasIntersection()
95 // find intersection point between triangle (P1,P2,P3)
97 //=======================================================================
98 static bool HasIntersection3(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
99 const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3)
101 //cout<<"HasIntersection3"<<endl;
102 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
103 //cout<<" P("<<P.X()<<","<<P.Y()<<","<<P.Z()<<")"<<endl;
104 //cout<<" P1("<<P1.X()<<","<<P1.Y()<<","<<P1.Z()<<")"<<endl;
105 //cout<<" P2("<<P2.X()<<","<<P2.Y()<<","<<P2.Z()<<")"<<endl;
106 //cout<<" P3("<<P3.X()<<","<<P3.Y()<<","<<P3.Z()<<")"<<endl;
109 IntAna_Quadric IAQ(gp_Pln(P1,VP1.Crossed(VP2)));
110 IntAna_IntConicQuad IAICQ(gp_Lin(PC,gp_Dir(gp_Vec(PC,P))),IAQ);
112 if( IAICQ.IsInQuadric() )
114 if( IAICQ.NbPoints() == 1 ) {
115 gp_Pnt PIn = IAICQ.Point(1);
116 double preci = 1.e-6;
117 // check if this point is internal for segment [PC,P]
119 ( (PC.X()-PIn.X())*(P.X()-PIn.X()) > preci ) ||
120 ( (PC.Y()-PIn.Y())*(P.Y()-PIn.Y()) > preci ) ||
121 ( (PC.Z()-PIn.Z())*(P.Z()-PIn.Z()) > preci );
125 // check if this point is internal for triangle (P1,P2,P3)
129 if( V1.Magnitude()<preci || V2.Magnitude()<preci ||
130 V3.Magnitude()<preci ) {
134 gp_Vec VC1 = V1.Crossed(V2);
135 gp_Vec VC2 = V2.Crossed(V3);
136 gp_Vec VC3 = V3.Crossed(V1);
137 if(VC1.Magnitude()<preci) {
138 if(VC2.IsOpposite(VC3,preci)) {
142 else if(VC2.Magnitude()<preci) {
143 if(VC1.IsOpposite(VC3,preci)) {
147 else if(VC3.Magnitude()<preci) {
148 if(VC1.IsOpposite(VC2,preci)) {
153 if( VC1.IsOpposite(VC2,preci) || VC1.IsOpposite(VC3,preci) ||
154 VC2.IsOpposite(VC3,preci) ) {
167 //=======================================================================
168 //function : HasIntersection
169 //purpose : Auxilare for CheckIntersection()
170 //=======================================================================
171 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
172 Handle(TColgp_HSequenceOfPnt)& aContour)
174 if(aContour->Length()==3) {
175 return HasIntersection3( P, PC, Pint, aContour->Value(1),
176 aContour->Value(2), aContour->Value(3) );
180 if( (aContour->Value(1).Distance(aContour->Value(2)) > 1.e-6) &&
181 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
182 (aContour->Value(2).Distance(aContour->Value(3)) > 1.e-6) ) {
183 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
184 aContour->Value(2), aContour->Value(3) );
186 if(check) return true;
187 if( (aContour->Value(1).Distance(aContour->Value(4)) > 1.e-6) &&
188 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
189 (aContour->Value(4).Distance(aContour->Value(3)) > 1.e-6) ) {
190 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
191 aContour->Value(3), aContour->Value(4) );
193 if(check) return true;
200 //=======================================================================
201 //function : CheckIntersection
202 //purpose : Auxilare for Compute()
203 // NotCheckedFace - for optimization
204 //=======================================================================
205 bool StdMeshers_QuadToTriaAdaptor::CheckIntersection
206 (const gp_Pnt& P, const gp_Pnt& PC,
207 gp_Pnt& Pint, SMESH_Mesh& aMesh,
208 const TopoDS_Shape& aShape,
209 const TopoDS_Shape& NotCheckedFace)
211 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
212 //cout<<" CheckIntersection: meshDS->NbFaces() = "<<meshDS->NbFaces()<<endl;
214 double dist = RealLast();
216 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
217 const TopoDS_Shape& aShapeFace = exp.Current();
218 if(aShapeFace==NotCheckedFace)
220 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements(aShapeFace);
221 if ( aSubMeshDSFace ) {
222 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
223 while ( iteratorElem->more() ) { // loop on elements on a face
224 const SMDS_MeshElement* face = iteratorElem->next();
225 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
226 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
227 if( !face->IsQuadratic() ) {
228 while ( nodeIt->more() ) {
229 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
230 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
235 while ( nodeIt->more() ) {
237 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
238 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
239 if(nn==face->NbNodes()/2) break;
242 if( HasIntersection(P, PC, Pres, aContour) ) {
244 double tmp = PC.Distance(Pres);
257 //=======================================================================
258 //function : CompareTrias
259 //purpose : Auxilare for Compute()
260 //=======================================================================
261 static bool CompareTrias(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
263 SMDS_ElemIteratorPtr nIt = F1->nodesIterator();
264 const SMDS_MeshNode* Ns1[3];
266 while( nIt->more() ) {
267 Ns1[k] = static_cast<const SMDS_MeshNode*>( nIt->next() );
270 nIt = F2->nodesIterator();
271 const SMDS_MeshNode* Ns2[3];
273 while( nIt->more() ) {
274 Ns2[k] = static_cast<const SMDS_MeshNode*>( nIt->next() );
277 if( ( Ns1[1]==Ns2[1] && Ns1[2]==Ns2[2] ) ||
278 ( Ns1[1]==Ns2[2] && Ns1[2]==Ns2[1] ) )
284 //=======================================================================
285 //function : IsDegenarate
286 //purpose : Auxilare for Preparation()
287 //=======================================================================
288 static int IsDegenarate(const Handle(TColgp_HArray1OfPnt)& PN)
294 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
302 //=======================================================================
303 //function : Preparation
304 //purpose : Auxilare for Compute()
305 // : Return 0 if given face is not quad,
306 // 1 if given face is quad,
307 // 2 if given face is degenerate quad (two nodes are coincided)
308 //=======================================================================
309 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
310 Handle(TColgp_HArray1OfPnt) PN,
311 Handle(TColgp_HArray1OfVec) VN,
312 std::vector<const SMDS_MeshNode*>& FNodes,
313 gp_Pnt& PC, gp_Vec& VNorm)
316 double xc=0., yc=0., zc=0.;
317 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
318 if( !face->IsQuadratic() ) {
319 if( face->NbNodes() != 4 )
321 while ( nodeIt->more() ) {
323 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
325 PN->SetValue( i, gp_Pnt(node->X(), node->Y(), node->Z()) );
332 if( face->NbNodes() != 8)
334 while ( nodeIt->more() ) {
336 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
338 PN->SetValue( i, gp_Pnt(node->X(), node->Y(), node->Z()) );
352 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
357 //int deg_num = IsDegenarate(PN);
361 //cout<<"find degeneration"<<endl;
363 gp_Pnt Pdeg = PN->Value(i);
365 std::list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
366 const SMDS_MeshNode* DegNode = 0;
367 for(; itdg!=myDegNodes.end(); itdg++) {
368 const SMDS_MeshNode* N = (*itdg);
369 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
370 if(Pdeg.Distance(Ptmp)<1.e-6) {
372 //DegNode = const_cast<SMDS_MeshNode*>(N);
377 DegNode = FNodes[i-1];
378 myDegNodes.push_back(DegNode);
381 FNodes[i-1] = DegNode;
384 PN->SetValue(i,PN->Value(i+1));
385 FNodes[i-1] = FNodes[i];
388 //PC = gp_Pnt( PN->Value(1).X() + PN.Value
391 PC = gp_Pnt(xc/4., yc/4., zc/4.);
392 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
394 //PN->SetValue(5,PN->Value(1));
395 PN->SetValue(nbp+1,PN->Value(1));
396 //FNodes[4] = FNodes[0];
397 FNodes[nbp] = FNodes[0];
398 // find normal direction
399 //gp_Vec V1(PC,PN->Value(4));
400 gp_Vec V1(PC,PN->Value(nbp));
401 gp_Vec V2(PC,PN->Value(1));
402 VNorm = V1.Crossed(V2);
403 //VN->SetValue(4,VNorm);
404 VN->SetValue(nbp,VNorm);
405 //for(i=1; i<4; i++) {
406 for(i=1; i<nbp; i++) {
407 V1 = gp_Vec(PC,PN->Value(i));
408 V2 = gp_Vec(PC,PN->Value(i+1));
409 gp_Vec Vtmp = V1.Crossed(V2);
410 VN->SetValue(i,Vtmp);
413 //cout<<" VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
419 //=======================================================================
422 //=======================================================================
424 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
429 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
431 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
432 const TopoDS_Shape& aShapeFace = exp.Current();
433 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
434 if ( aSubMeshDSFace ) {
435 bool isRev = SMESH_Algo::IsReversedSubMesh( TopoDS::Face(aShapeFace), meshDS );
437 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
438 while ( iteratorElem->more() ) { // loop on elements on a face
439 const SMDS_MeshElement* face = iteratorElem->next();
440 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
441 // preparation step using face info
442 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
443 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
444 std::vector<const SMDS_MeshNode*> FNodes(5);
447 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
453 // add triangles to result map
454 std::list<const SMDS_FaceOfNodes*> aList;
455 SMDS_FaceOfNodes* NewFace;
457 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
459 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
460 aList.push_back(NewFace);
461 myResMap.insert(make_pair(face,aList));
465 if(!isRev) VNorm.Reverse();
466 double xc = 0., yc = 0., zc = 0.;
471 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i).Reversed());
473 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
478 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
481 double height = PCbest.Distance(PC);
483 // create new PCbest using a bit shift along VNorm
484 PCbest = gp_Pnt( PC.X() + VNorm.X()*0.001,
485 PC.Y() + VNorm.Y()*0.001,
486 PC.Z() + VNorm.Z()*0.001);
489 // check possible intersection with other faces
491 bool check = CheckIntersection(PCbest, PC, Pint, aMesh, aShape, aShapeFace);
493 //cout<<"--PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
494 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
495 double dist = PC.Distance(Pint)/3.;
496 gp_Dir aDir(gp_Vec(PC,PCbest));
497 PCbest = gp_Pnt( PC.X() + aDir.X()*dist,
498 PC.Y() + aDir.Y()*dist,
499 PC.Z() + aDir.Z()*dist );
502 gp_Vec VB(PC,PCbest);
503 gp_Pnt PCbestTmp(PC.X()+VB.X()*3, PC.X()+VB.X()*3, PC.X()+VB.X()*3);
504 bool check = CheckIntersection(PCbestTmp, PC, Pint, aMesh, aShape, aShapeFace);
506 double dist = PC.Distance(Pint)/3.;
508 gp_Dir aDir(gp_Vec(PC,PCbest));
509 PCbest = gp_Pnt( PC.X() + aDir.X()*dist,
510 PC.Y() + aDir.Y()*dist,
511 PC.Z() + aDir.Z()*dist );
516 // create node for PCbest
517 SMDS_MeshNode* NewNode = meshDS->AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
518 // add triangles to result map
519 std::list<const SMDS_FaceOfNodes*> aList;
521 SMDS_FaceOfNodes* NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
522 aList.push_back(NewFace);
524 myResMap.insert(make_pair(face,aList));
526 SMDS_MeshVolume* aPyram =
527 meshDS->AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
528 myMapFPyram.insert(make_pair(face,aPyram));
529 } // end loop on elements on a face
531 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
533 return Compute2ndPart(aMesh);
537 //=======================================================================
540 //=======================================================================
542 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
547 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
549 SMDS_FaceIteratorPtr itFace = meshDS->facesIterator();
551 while(itFace->more()) {
552 const SMDS_MeshElement* face = itFace->next();
553 if ( !face ) continue;
554 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
555 // preparation step using face info
556 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
557 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
558 std::vector<const SMDS_MeshNode*> FNodes(5);
562 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
568 // add triangles to result map
569 std::list<const SMDS_FaceOfNodes*> aList;
570 SMDS_FaceOfNodes* NewFace;
573 double tmp = PN->Value(1).Distance(PN->Value(2)) +
574 PN->Value(2).Distance(PN->Value(3));
575 gp_Dir tmpDir(VNorm);
576 gp_Pnt Ptmp1( PC.X() + tmpDir.X()*tmp*1.e6,
577 PC.Y() + tmpDir.Y()*tmp*1.e6,
578 PC.Z() + tmpDir.Z()*tmp*1.e6 );
579 gp_Pnt Ptmp2( PC.X() + tmpDir.Reversed().X()*tmp*1.e6,
580 PC.Y() + tmpDir.Reversed().Y()*tmp*1.e6,
581 PC.Z() + tmpDir.Reversed().Z()*tmp*1.e6 );
582 // check intersection for Ptmp1 and Ptmp2
586 double dist1 = RealLast();
587 double dist2 = RealLast();
589 SMDS_FaceIteratorPtr itf = meshDS->facesIterator();
591 const SMDS_MeshElement* F = itf->next();
592 if(F==face) continue;
593 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
594 SMDS_ElemIteratorPtr nodeIt = F->nodesIterator();
595 if( !F->IsQuadratic() ) {
596 while ( nodeIt->more() ) {
597 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
598 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
603 while ( nodeIt->more() ) {
605 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
606 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
607 if(nn==face->NbNodes()/2) break;
611 if( HasIntersection(Ptmp1, PC, PPP, aContour) ) {
613 double tmp = PC.Distance(PPP);
619 if( HasIntersection(Ptmp2, PC, PPP, aContour) ) {
621 double tmp = PC.Distance(PPP);
629 if( IsOK1 && !IsOK2 ) {
630 // using existed direction
632 else if( !IsOK1 && IsOK2 ) {
633 // using opposite direction
636 else { // IsOK1 && IsOK2
637 double tmp1 = PC.Distance(Pres1)/3.;
638 double tmp2 = PC.Distance(Pres2)/3.;
640 // using existed direction
643 // using opposite direction
648 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
650 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
651 aList.push_back(NewFace);
652 myResMap.insert(make_pair(face,aList));
656 double xc = 0., yc = 0., zc = 0.;
659 gp_Pnt Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
664 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
665 double height = PCbest.Distance(PC);
667 // create new PCbest using a bit shift along VNorm
668 PCbest = gp_Pnt( PC.X() + VNorm.X()*0.001,
669 PC.Y() + VNorm.Y()*0.001,
670 PC.Z() + VNorm.Z()*0.001);
671 height = PCbest.Distance(PC);
673 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
675 gp_Vec V1(PC,PCbest);
676 double tmp = PN->Value(1).Distance(PN->Value(3)) +
677 PN->Value(2).Distance(PN->Value(4));
679 gp_Pnt Ptmp1( PC.X() + tmpDir.X()*tmp*1.e6,
680 PC.Y() + tmpDir.Y()*tmp*1.e6,
681 PC.Z() + tmpDir.Z()*tmp*1.e6 );
682 gp_Pnt Ptmp2( PC.X() + tmpDir.Reversed().X()*tmp*1.e6,
683 PC.Y() + tmpDir.Reversed().Y()*tmp*1.e6,
684 PC.Z() + tmpDir.Reversed().Z()*tmp*1.e6 );
685 // check intersection for Ptmp1 and Ptmp2
689 double dist1 = RealLast();
690 double dist2 = RealLast();
692 SMDS_FaceIteratorPtr itf = meshDS->facesIterator();
694 const SMDS_MeshElement* F = itf->next();
695 if(F==face) continue;
696 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
697 SMDS_ElemIteratorPtr nodeIt = F->nodesIterator();
698 if( !F->IsQuadratic() ) {
699 while ( nodeIt->more() ) {
700 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
701 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
706 while ( nodeIt->more() ) {
708 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
709 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
710 if(nn==face->NbNodes()/2) break;
714 if( HasIntersection(Ptmp1, PC, PPP, aContour) ) {
716 double tmp = PC.Distance(PPP);
722 if( HasIntersection(Ptmp2, PC, PPP, aContour) ) {
724 double tmp = PC.Distance(PPP);
732 if( IsOK1 && !IsOK2 ) {
733 // using existed direction
734 double tmp = PC.Distance(Pres1)/3.;
737 PCbest = gp_Pnt( PC.X() + tmpDir.X()*height,
738 PC.Y() + tmpDir.Y()*height,
739 PC.Z() + tmpDir.Z()*height );
742 else if( !IsOK1 && IsOK2 ) {
743 // using opposite direction
745 double tmp = PC.Distance(Pres2)/3.;
746 if( height > tmp ) height = tmp;
747 PCbest = gp_Pnt( PC.X() + tmpDir.Reversed().X()*height,
748 PC.Y() + tmpDir.Reversed().Y()*height,
749 PC.Z() + tmpDir.Reversed().Z()*height );
751 else { // IsOK1 && IsOK2
752 double tmp1 = PC.Distance(Pres1)/3.;
753 double tmp2 = PC.Distance(Pres2)/3.;
755 // using existed direction
756 if( height > tmp1 ) {
758 PCbest = gp_Pnt( PC.X() + tmpDir.X()*height,
759 PC.Y() + tmpDir.Y()*height,
760 PC.Z() + tmpDir.Z()*height );
764 // using opposite direction
766 if( height > tmp2 ) height = tmp2;
767 PCbest = gp_Pnt( PC.X() + tmpDir.Reversed().X()*height,
768 PC.Y() + tmpDir.Reversed().Y()*height,
769 PC.Z() + tmpDir.Reversed().Z()*height );
773 // create node for PCbest
774 SMDS_MeshNode* NewNode = meshDS->AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
775 // add triangles to result map
776 std::list<const SMDS_FaceOfNodes*> aList;
778 SMDS_FaceOfNodes* NewFace;
780 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
782 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i+1], FNodes[i] );
783 aList.push_back(NewFace);
785 myResMap.insert(make_pair(face,aList));
787 SMDS_MeshVolume* aPyram;
789 aPyram = meshDS->AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
791 aPyram = meshDS->AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
792 myMapFPyram.insert(make_pair(face,aPyram));
793 } // end loop on elements on a face
795 return Compute2ndPart(aMesh);
799 //=======================================================================
800 //function : Compute2ndPart
802 //=======================================================================
804 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh)
806 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
808 // check intersections between created pyramids
809 int NbPyram = myMapFPyram.size();
810 //cout<<"NbPyram = "<<NbPyram<<endl;
814 std::vector< const SMDS_MeshElement* > Pyrams(NbPyram);
815 std::vector< const SMDS_MeshElement* > Faces(NbPyram);
816 std::map< const SMDS_MeshElement*,
817 const SMDS_MeshElement* >::iterator itp = myMapFPyram.begin();
819 for(; itp!=myMapFPyram.end(); itp++, i++) {
820 Faces[i] = (*itp).first;
821 Pyrams[i] = (*itp).second;
823 StdMeshers_Array1OfSequenceOfInteger MergesInfo(0,NbPyram-1);
824 for(i=0; i<NbPyram; i++) {
825 TColStd_SequenceOfInteger aMerges;
827 MergesInfo.SetValue(i,aMerges);
829 for(i=0; i<NbPyram-1; i++) {
830 const SMDS_MeshElement* Prm1 = Pyrams[i];
831 SMDS_ElemIteratorPtr nIt = Prm1->nodesIterator();
832 std::vector<gp_Pnt> Ps1(5);
833 const SMDS_MeshNode* Ns1[5];
835 while( nIt->more() ) {
836 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nIt->next() );
838 Ps1[k] = gp_Pnt(node->X(), node->Y(), node->Z());
841 bool NeedMove = false;
842 for(int j=i+1; j<NbPyram; j++) {
843 //cout<<" i="<<i<<" j="<<j<<endl;
844 const TColStd_SequenceOfInteger& aMergesI = MergesInfo.Value(i);
845 int nbI = aMergesI.Length();
846 const TColStd_SequenceOfInteger& aMergesJ = MergesInfo.Value(j);
847 int nbJ = aMergesJ.Length();
850 bool NeedCont = false;
852 if(aMergesI.Value(k)==j) {
857 if(NeedCont) continue;
859 const SMDS_MeshElement* Prm2 = Pyrams[j];
860 nIt = Prm2->nodesIterator();
861 std::vector<gp_Pnt> Ps2(5);
862 const SMDS_MeshNode* Ns2[5];
864 while( nIt->more() ) {
865 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nIt->next() );
867 Ps2[k] = gp_Pnt(node->X(), node->Y(), node->Z());
874 gp_Vec Vtmp(Ps1[k],Ps1[4]);
875 gp_Pnt Pshift( Ps1[k].X() + Vtmp.X()*0.01,
876 Ps1[k].Y() + Vtmp.Y()*0.01,
877 Ps1[k].Z() + Vtmp.Z()*0.01 );
880 if( HasIntersection3( Pshift, Ps1[4], Pint, Ps2[m], Ps2[m+1], Ps2[4]) ) {
885 if( HasIntersection3( Pshift, Ps1[4], Pint, Ps2[3], Ps2[0], Ps2[4]) ) {
892 gp_Vec Vtmp(Ps2[k],Ps2[4]);
893 gp_Pnt Pshift( Ps2[k].X() + Vtmp.X()*0.01,
894 Ps2[k].Y() + Vtmp.Y()*0.01,
895 Ps2[k].Z() + Vtmp.Z()*0.01 );
898 if( HasIntersection3( Pshift, Ps2[4], Pint, Ps1[m], Ps1[m+1], Ps1[4]) ) {
903 if( HasIntersection3( Pshift, Ps2[4], Pint, Ps1[3], Ps1[0], Ps1[4]) ) {
911 //cout<<" has intersec for i="<<i<<" j="<<j<<endl;
912 // check if MeshFaces have 2 common node
915 for(int m=0; m<4; m++) {
916 if( Ns1[k]==Ns2[m] ) nbc++;
919 //cout<<" nbc = "<<nbc<<endl;
921 // create common node
922 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(Ns1[4]);
923 CommonNode->setXYZ( ( nbI*Ps1[4].X() + nbJ*Ps2[4].X() ) / (nbI+nbJ),
924 ( nbI*Ps1[4].Y() + nbJ*Ps2[4].Y() ) / (nbI+nbJ),
925 ( nbI*Ps1[4].Z() + nbJ*Ps2[4].Z() ) / (nbI+nbJ) );
927 //cout<<" CommonNode: "<<CommonNode;
928 const SMDS_MeshNode* Nrem = Ns2[4];
930 meshDS->ChangeElementNodes(Prm2, Ns2, 5);
931 // update pyramids for J
932 for(k=2; k<=nbJ; k++) {
933 const SMDS_MeshElement* tmpPrm = Pyrams[aMergesJ.Value(k)];
934 SMDS_ElemIteratorPtr tmpIt = tmpPrm->nodesIterator();
935 const SMDS_MeshNode* Ns[5];
937 while( tmpIt->more() ) {
938 Ns[m] = static_cast<const SMDS_MeshNode*>( tmpIt->next() );
942 meshDS->ChangeElementNodes(tmpPrm, Ns, 5);
946 for(k=1; k<=nbI; k++) {
947 int num = aMergesI.Value(k);
948 const TColStd_SequenceOfInteger& aSeq = MergesInfo.Value(num);
949 TColStd_SequenceOfInteger tmpSeq;
951 for(; m<=aSeq.Length(); m++) {
952 tmpSeq.Append(aSeq.Value(m));
954 for(m=1; m<=nbJ; m++) {
955 tmpSeq.Append(aMergesJ.Value(m));
957 MergesInfo.SetValue(num,tmpSeq);
959 for(k=1; k<=nbJ; k++) {
960 int num = aMergesJ.Value(k);
961 const TColStd_SequenceOfInteger& aSeq = MergesInfo.Value(num);
962 TColStd_SequenceOfInteger tmpSeq;
964 for(; m<=aSeq.Length(); m++) {
965 tmpSeq.Append(aSeq.Value(m));
967 for(m=1; m<=nbI; m++) {
968 tmpSeq.Append(aMergesI.Value(m));
970 MergesInfo.SetValue(num,tmpSeq);
973 // update triangles for aMergesJ
974 for(k=1; k<=nbJ; k++) {
975 std::list< std::list< const SMDS_MeshNode* > > aFNodes;
976 std::list< const SMDS_MeshElement* > aFFaces;
977 int num = aMergesJ.Value(k);
978 std::map< const SMDS_MeshElement*,
979 std::list<const SMDS_FaceOfNodes*> >::iterator itrm = myResMap.find(Faces[num]);
980 std::list<const SMDS_FaceOfNodes*> trias = (*itrm).second;
981 std::list<const SMDS_FaceOfNodes*>::iterator itt = trias.begin();
982 for(; itt!=trias.end(); itt++) {
984 SMDS_ElemIteratorPtr nodeIt = (*itt)->nodesIterator();
985 const SMDS_MeshNode* NF[3];
986 while ( nodeIt->more() ) {
988 NF[nn] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
991 SMDS_FaceOfNodes* Ftria = const_cast< SMDS_FaceOfNodes*>( (*itt) );
992 Ftria->ChangeNodes(NF, 3);
996 // check and remove coincided faces
997 TColStd_SequenceOfInteger IdRemovedTrias;
999 for(; i1<=nbI; i1++) {
1000 int numI = aMergesI.Value(i1);
1001 std::map< const SMDS_MeshElement*,
1002 std::list<const SMDS_FaceOfNodes*> >::iterator itrmI = myResMap.find(Faces[numI]);
1003 std::list<const SMDS_FaceOfNodes*> triasI = (*itrmI).second;
1004 std::list<const SMDS_FaceOfNodes*>::iterator ittI = triasI.begin();
1005 int nbfI = triasI.size();
1006 std::vector<const SMDS_FaceOfNodes*> FsI(nbfI);
1008 for(; ittI!=triasI.end(); ittI++) {
1013 for(; i2<nbfI; i2++) {
1014 const SMDS_FaceOfNodes* FI = FsI[i2];
1017 for(; j1<=nbJ; j1++) {
1018 int numJ = aMergesJ.Value(j1);
1019 std::map< const SMDS_MeshElement*,
1020 std::list<const SMDS_FaceOfNodes*> >::iterator itrmJ = myResMap.find(Faces[numJ]);
1021 std::list<const SMDS_FaceOfNodes*> triasJ = (*itrmJ).second;
1022 std::list<const SMDS_FaceOfNodes*>::iterator ittJ = triasJ.begin();
1023 int nbfJ = triasJ.size();
1024 std::vector<const SMDS_FaceOfNodes*> FsJ(nbfJ);
1026 for(; ittJ!=triasJ.end(); ittJ++) {
1031 for(; j2<nbfJ; j2++) {
1032 const SMDS_FaceOfNodes* FJ = FsJ[j2];
1033 // compare triangles
1034 if( CompareTrias(FI,FJ) ) {
1035 IdRemovedTrias.Append( FI->GetID() );
1036 IdRemovedTrias.Append( FJ->GetID() );
1039 std::list<const SMDS_FaceOfNodes*> new_triasI;
1040 for(k=0; k<nbfI; k++) {
1041 if( FsI[k]==0 ) continue;
1042 new_triasI.push_back( FsI[k] );
1044 (*itrmI).second = new_triasI;
1045 triasI = new_triasI;
1046 std::list<const SMDS_FaceOfNodes*> new_triasJ;
1047 for(k=0; k<nbfJ; k++) {
1048 if( FsJ[k]==0 ) continue;
1049 new_triasJ.push_back( FsJ[k] );
1051 (*itrmJ).second = new_triasJ;
1052 triasJ = new_triasJ;
1056 // close for j2 and j1
1065 meshDS->RemoveNode(Nrem);
1068 //cout<<"decrease height of pyramids"<<endl;
1069 // decrease height of pyramids
1070 double xc1 = 0., yc1 = 0., zc1 = 0.;
1071 double xc2 = 0., yc2 = 0., zc2 = 0.;
1072 for(k=0; k<4; k++) {
1080 gp_Pnt PC1(xc1/4.,yc1/4.,zc1/4.);
1081 gp_Pnt PC2(xc2/4.,yc2/4.,zc2/4.);
1082 gp_Vec VN1(PC1,Ps1[4]);
1083 gp_Vec VI1(PC1,Pint);
1084 gp_Vec VN2(PC2,Ps2[4]);
1085 gp_Vec VI2(PC2,Pint);
1086 double ang1 = fabs(VN1.Angle(VI1));
1087 double ang2 = fabs(VN2.Angle(VI2));
1090 h1 = VI1.Magnitude()/2;
1092 h1 = VI1.Magnitude()*cos(ang1);
1094 h2 = VI2.Magnitude()/2;
1096 h2 = VI2.Magnitude()*cos(ang2);
1099 coef1 -= cos(ang1)*0.25;
1102 coef2 -= cos(ang1)*0.25;
1104 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>(Ns1[4]);
1106 aNode1->setXYZ( PC1.X()+VN1.X(), PC1.Y()+VN1.Y(), PC1.Z()+VN1.Z() );
1107 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(Ns2[4]);
1109 aNode2->setXYZ( PC2.X()+VN2.X(), PC2.Y()+VN2.Y(), PC2.Z()+VN2.Z() );
1114 //cout<<" no intersec for i="<<i<<" j="<<j<<endl;
1118 if( NeedMove && !meshDS->IsEmbeddedMode() ) {
1119 meshDS->MoveNode( Ns1[4], Ns1[4]->X(), Ns1[4]->Y(), Ns1[4]->Z() );
1127 //================================================================================
1129 * \brief Return list of created triangles for given face
1131 //================================================================================
1132 std::list<const SMDS_FaceOfNodes*> StdMeshers_QuadToTriaAdaptor::GetTriangles
1133 (const SMDS_MeshElement* aFace)
1135 std::list<const SMDS_FaceOfNodes*> aRes;
1136 std::map< const SMDS_MeshElement*,
1137 std::list<const SMDS_FaceOfNodes*> >::iterator it = myResMap.find(aFace);
1138 if( it != myResMap.end() ) {
1145 //================================================================================
1147 * \brief Remove all create auxilary faces
1149 //================================================================================
1150 //void StdMeshers_QuadToTriaAdaptor::RemoveFaces(SMESH_Mesh& aMesh)
1152 // SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1153 // std::map< const SMDS_MeshElement*,
1154 // std::list<const SMDS_MeshElement*> >::iterator it = myResMap.begin();
1155 // for(; it != myResMap.end(); it++ ) {
1156 // std::list<const SMDS_MeshElement*> aFaces = (*it).second;
1157 // std::list<const SMDS_MeshElement*>::iterator itf = aFaces.begin();
1158 // for(; itf!=aFaces.end(); itf++ ) {
1159 // meshDS->RemoveElement( (*itf) );