1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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
24 // File : StdMeshers_QuadToTriaAdaptor.cxx
26 // Created : Wen May 07 16:37:07 2008
27 // Author : Sergey KUUL (skl)
30 #include "StdMeshers_QuadToTriaAdaptor.hxx"
32 //#include <TColgp_HArray1OfPnt.hxx>
33 //#include <TColgp_HArray1OfVec.hxx>
34 #include <TopExp_Explorer.hxx>
36 #include <SMESH_Algo.hxx>
37 #include <TColgp_HSequenceOfPnt.hxx>
38 #include <TColStd_MapOfInteger.hxx>
39 #include <TColStd_HSequenceOfInteger.hxx>
40 #include <IntAna_Quadric.hxx>
41 #include <IntAna_IntConicQuad.hxx>
44 #include <SMDS_FaceOfNodes.hxx>
46 #include <NCollection_Array1.hxx>
47 typedef NCollection_Array1<TColStd_SequenceOfInteger> StdMeshers_Array1OfSequenceOfInteger;
52 //=======================================================================
53 //function : StdMeshers_QuadToTriaAdaptor
55 //=======================================================================
57 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor()
62 //================================================================================
66 //================================================================================
68 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
72 //=======================================================================
73 //function : FindBestPoint
74 //purpose : Auxilare for Compute()
75 // V - normal to (P1,P2,PC)
76 //=======================================================================
77 static gp_Pnt FindBestPoint(const gp_Pnt& P1, const gp_Pnt& P2,
78 const gp_Pnt& PC, const gp_Vec& V)
80 double a = P1.Distance(P2);
81 double b = P1.Distance(PC);
82 double c = P2.Distance(PC);
86 // find shift along V in order to a became equal to (b+c)/2
87 double shift = sqrt( a*a + (b*b-c*c)*(b*b-c*c)/16/a/a - (b*b+c*c)/2 );
89 gp_Pnt Pbest( PC.X() + aDir.X()*shift, PC.Y() + aDir.Y()*shift,
90 PC.Z() + aDir.Z()*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 //=======================================================================
175 static bool HasIntersection(const gp_Pnt& P, const gp_Pnt& PC, gp_Pnt& Pint,
176 Handle(TColgp_HSequenceOfPnt)& aContour)
178 if(aContour->Length()==3) {
179 return HasIntersection3( P, PC, Pint, aContour->Value(1),
180 aContour->Value(2), aContour->Value(3) );
184 if( (aContour->Value(1).Distance(aContour->Value(2)) > 1.e-6) &&
185 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
186 (aContour->Value(2).Distance(aContour->Value(3)) > 1.e-6) ) {
187 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
188 aContour->Value(2), aContour->Value(3) );
190 if(check) return true;
191 if( (aContour->Value(1).Distance(aContour->Value(4)) > 1.e-6) &&
192 (aContour->Value(1).Distance(aContour->Value(3)) > 1.e-6) &&
193 (aContour->Value(4).Distance(aContour->Value(3)) > 1.e-6) ) {
194 check = HasIntersection3( P, PC, Pint, aContour->Value(1),
195 aContour->Value(3), aContour->Value(4) );
197 if(check) return true;
204 //=======================================================================
205 //function : CheckIntersection
206 //purpose : Auxilare for Compute()
207 // NotCheckedFace - for optimization
208 //=======================================================================
209 bool StdMeshers_QuadToTriaAdaptor::CheckIntersection
210 (const gp_Pnt& P, const gp_Pnt& PC,
211 gp_Pnt& Pint, SMESH_Mesh& aMesh,
212 const TopoDS_Shape& aShape,
213 const TopoDS_Shape& NotCheckedFace)
215 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
216 //cout<<" CheckIntersection: meshDS->NbFaces() = "<<meshDS->NbFaces()<<endl;
218 double dist = RealLast();
220 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
221 const TopoDS_Shape& aShapeFace = exp.Current();
222 if(aShapeFace==NotCheckedFace)
224 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements(aShapeFace);
225 if ( aSubMeshDSFace ) {
226 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
227 while ( iteratorElem->more() ) { // loop on elements on a face
228 const SMDS_MeshElement* face = iteratorElem->next();
229 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
230 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
231 if( !face->IsQuadratic() ) {
232 while ( nodeIt->more() ) {
233 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
234 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
239 while ( nodeIt->more() ) {
241 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
242 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
243 if(nn==face->NbNodes()/2) break;
246 if( HasIntersection(P, PC, Pres, aContour) ) {
248 double tmp = PC.Distance(Pres);
261 //=======================================================================
262 //function : CompareTrias
263 //purpose : Auxilare for Compute()
264 //=======================================================================
265 static bool CompareTrias(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
267 SMDS_ElemIteratorPtr nIt = F1->nodesIterator();
268 const SMDS_MeshNode* Ns1[3];
270 while( nIt->more() ) {
271 Ns1[k] = static_cast<const SMDS_MeshNode*>( nIt->next() );
274 nIt = F2->nodesIterator();
275 const SMDS_MeshNode* Ns2[3];
277 while( nIt->more() ) {
278 Ns2[k] = static_cast<const SMDS_MeshNode*>( nIt->next() );
281 if( ( Ns1[1]==Ns2[1] && Ns1[2]==Ns2[2] ) ||
282 ( Ns1[1]==Ns2[2] && Ns1[2]==Ns2[1] ) )
288 //=======================================================================
289 //function : IsDegenarate
290 //purpose : Auxilare for Preparation()
291 //=======================================================================
292 static int IsDegenarate(const Handle(TColgp_HArray1OfPnt)& PN)
298 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
306 //=======================================================================
307 //function : Preparation
308 //purpose : Auxilare for Compute()
309 // : Return 0 if given face is not quad,
310 // 1 if given face is quad,
311 // 2 if given face is degenerate quad (two nodes are coincided)
312 //=======================================================================
313 int StdMeshers_QuadToTriaAdaptor::Preparation(const SMDS_MeshElement* face,
314 Handle(TColgp_HArray1OfPnt) PN,
315 Handle(TColgp_HArray1OfVec) VN,
316 std::vector<const SMDS_MeshNode*>& FNodes,
317 gp_Pnt& PC, gp_Vec& VNorm)
320 double xc=0., yc=0., zc=0.;
321 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
322 if( !face->IsQuadratic() ) {
323 if( face->NbNodes() != 4 )
325 while ( nodeIt->more() ) {
327 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
329 PN->SetValue( i, gp_Pnt(node->X(), node->Y(), node->Z()) );
336 if( face->NbNodes() != 8)
338 while ( nodeIt->more() ) {
340 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
342 PN->SetValue( i, gp_Pnt(node->X(), node->Y(), node->Z()) );
356 if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
361 //int deg_num = IsDegenarate(PN);
365 //cout<<"find degeneration"<<endl;
367 gp_Pnt Pdeg = PN->Value(i);
369 std::list< const SMDS_MeshNode* >::iterator itdg = myDegNodes.begin();
370 const SMDS_MeshNode* DegNode = 0;
371 for(; itdg!=myDegNodes.end(); itdg++) {
372 const SMDS_MeshNode* N = (*itdg);
373 gp_Pnt Ptmp(N->X(),N->Y(),N->Z());
374 if(Pdeg.Distance(Ptmp)<1.e-6) {
376 //DegNode = const_cast<SMDS_MeshNode*>(N);
381 DegNode = FNodes[i-1];
382 myDegNodes.push_back(DegNode);
385 FNodes[i-1] = DegNode;
388 PN->SetValue(i,PN->Value(i+1));
389 FNodes[i-1] = FNodes[i];
392 //PC = gp_Pnt( PN->Value(1).X() + PN.Value
395 PC = gp_Pnt(xc/4., yc/4., zc/4.);
396 //cout<<" PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
398 //PN->SetValue(5,PN->Value(1));
399 PN->SetValue(nbp+1,PN->Value(1));
400 //FNodes[4] = FNodes[0];
401 FNodes[nbp] = FNodes[0];
402 // find normal direction
403 //gp_Vec V1(PC,PN->Value(4));
404 gp_Vec V1(PC,PN->Value(nbp));
405 gp_Vec V2(PC,PN->Value(1));
406 VNorm = V1.Crossed(V2);
407 //VN->SetValue(4,VNorm);
408 VN->SetValue(nbp,VNorm);
409 //for(i=1; i<4; i++) {
410 for(i=1; i<nbp; i++) {
411 V1 = gp_Vec(PC,PN->Value(i));
412 V2 = gp_Vec(PC,PN->Value(i+1));
413 gp_Vec Vtmp = V1.Crossed(V2);
414 VN->SetValue(i,Vtmp);
417 //cout<<" VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
423 //=======================================================================
426 //=======================================================================
428 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
433 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
435 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
436 const TopoDS_Shape& aShapeFace = exp.Current();
437 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements( aShapeFace );
438 if ( aSubMeshDSFace ) {
439 bool isRev = SMESH_Algo::IsReversedSubMesh( TopoDS::Face(aShapeFace), meshDS );
441 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
442 while ( iteratorElem->more() ) { // loop on elements on a face
443 const SMDS_MeshElement* face = iteratorElem->next();
444 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
445 // preparation step using face info
446 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
447 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
448 std::vector<const SMDS_MeshNode*> FNodes(5);
451 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
457 // add triangles to result map
458 std::list<const SMDS_FaceOfNodes*> aList;
459 SMDS_FaceOfNodes* NewFace;
461 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
463 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
464 aList.push_back(NewFace);
465 myResMap.insert(make_pair(face,aList));
469 if(!isRev) VNorm.Reverse();
470 double xc = 0., yc = 0., zc = 0.;
475 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i).Reversed());
477 Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
482 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
485 double height = PCbest.Distance(PC);
487 // create new PCbest using a bit shift along VNorm
488 PCbest = gp_Pnt( PC.X() + VNorm.X()*0.001,
489 PC.Y() + VNorm.Y()*0.001,
490 PC.Z() + VNorm.Z()*0.001);
493 // check possible intersection with other faces
495 bool check = CheckIntersection(PCbest, PC, Pint, aMesh, aShape, aShapeFace);
497 //cout<<"--PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
498 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
499 double dist = PC.Distance(Pint)/3.;
500 gp_Dir aDir(gp_Vec(PC,PCbest));
501 PCbest = gp_Pnt( PC.X() + aDir.X()*dist,
502 PC.Y() + aDir.Y()*dist,
503 PC.Z() + aDir.Z()*dist );
506 gp_Vec VB(PC,PCbest);
507 gp_Pnt PCbestTmp(PC.X()+VB.X()*3, PC.X()+VB.X()*3, PC.X()+VB.X()*3);
508 bool check = CheckIntersection(PCbestTmp, PC, Pint, aMesh, aShape, aShapeFace);
510 double dist = PC.Distance(Pint)/3.;
512 gp_Dir aDir(gp_Vec(PC,PCbest));
513 PCbest = gp_Pnt( PC.X() + aDir.X()*dist,
514 PC.Y() + aDir.Y()*dist,
515 PC.Z() + aDir.Z()*dist );
520 // create node for PCbest
521 SMDS_MeshNode* NewNode = meshDS->AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
522 // add triangles to result map
523 std::list<const SMDS_FaceOfNodes*> aList;
525 SMDS_FaceOfNodes* NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
526 aList.push_back(NewFace);
528 myResMap.insert(make_pair(face,aList));
530 SMDS_MeshVolume* aPyram =
531 meshDS->AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
532 myMapFPyram.insert(make_pair(face,aPyram));
533 } // end loop on elements on a face
535 } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
537 return Compute2ndPart(aMesh);
541 //=======================================================================
544 //=======================================================================
546 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
551 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
553 SMDS_FaceIteratorPtr itFace = meshDS->facesIterator();
555 while(itFace->more()) {
556 const SMDS_MeshElement* face = itFace->next();
557 if ( !face ) continue;
558 //cout<<endl<<"================= face->GetID() = "<<face->GetID()<<endl;
559 // preparation step using face info
560 Handle(TColgp_HArray1OfPnt) PN = new TColgp_HArray1OfPnt(1,5);
561 Handle(TColgp_HArray1OfVec) VN = new TColgp_HArray1OfVec(1,4);
562 std::vector<const SMDS_MeshNode*> FNodes(5);
566 int stat = Preparation(face, PN, VN, FNodes, PC, VNorm);
572 // add triangles to result map
573 std::list<const SMDS_FaceOfNodes*> aList;
574 SMDS_FaceOfNodes* NewFace;
577 double tmp = PN->Value(1).Distance(PN->Value(2)) +
578 PN->Value(2).Distance(PN->Value(3));
579 gp_Dir tmpDir(VNorm);
580 gp_Pnt Ptmp1( PC.X() + tmpDir.X()*tmp*1.e6,
581 PC.Y() + tmpDir.Y()*tmp*1.e6,
582 PC.Z() + tmpDir.Z()*tmp*1.e6 );
583 gp_Pnt Ptmp2( PC.X() + tmpDir.Reversed().X()*tmp*1.e6,
584 PC.Y() + tmpDir.Reversed().Y()*tmp*1.e6,
585 PC.Z() + tmpDir.Reversed().Z()*tmp*1.e6 );
586 // check intersection for Ptmp1 and Ptmp2
590 double dist1 = RealLast();
591 double dist2 = RealLast();
593 SMDS_FaceIteratorPtr itf = meshDS->facesIterator();
595 const SMDS_MeshElement* F = itf->next();
596 if(F==face) continue;
597 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
598 SMDS_ElemIteratorPtr nodeIt = F->nodesIterator();
599 if( !F->IsQuadratic() ) {
600 while ( nodeIt->more() ) {
601 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
602 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
607 while ( nodeIt->more() ) {
609 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
610 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
611 if(nn==face->NbNodes()/2) break;
615 if( HasIntersection(Ptmp1, PC, PPP, aContour) ) {
617 double tmp = PC.Distance(PPP);
623 if( HasIntersection(Ptmp2, PC, PPP, aContour) ) {
625 double tmp = PC.Distance(PPP);
633 if( IsOK1 && !IsOK2 ) {
634 // using existed direction
636 else if( !IsOK1 && IsOK2 ) {
637 // using opposite direction
640 else { // IsOK1 && IsOK2
641 double tmp1 = PC.Distance(Pres1)/3.;
642 double tmp2 = PC.Distance(Pres2)/3.;
644 // using existed direction
647 // using opposite direction
652 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
654 NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
655 aList.push_back(NewFace);
656 myResMap.insert(make_pair(face,aList));
660 double xc = 0., yc = 0., zc = 0.;
663 gp_Pnt Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
668 gp_Pnt PCbest(xc/4., yc/4., zc/4.);
669 double height = PCbest.Distance(PC);
671 // create new PCbest using a bit shift along VNorm
672 PCbest = gp_Pnt( PC.X() + VNorm.X()*0.001,
673 PC.Y() + VNorm.Y()*0.001,
674 PC.Z() + VNorm.Z()*0.001);
675 height = PCbest.Distance(PC);
677 //cout<<" PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
679 gp_Vec V1(PC,PCbest);
680 double tmp = PN->Value(1).Distance(PN->Value(3)) +
681 PN->Value(2).Distance(PN->Value(4));
683 gp_Pnt Ptmp1( PC.X() + tmpDir.X()*tmp*1.e6,
684 PC.Y() + tmpDir.Y()*tmp*1.e6,
685 PC.Z() + tmpDir.Z()*tmp*1.e6 );
686 gp_Pnt Ptmp2( PC.X() + tmpDir.Reversed().X()*tmp*1.e6,
687 PC.Y() + tmpDir.Reversed().Y()*tmp*1.e6,
688 PC.Z() + tmpDir.Reversed().Z()*tmp*1.e6 );
689 // check intersection for Ptmp1 and Ptmp2
693 double dist1 = RealLast();
694 double dist2 = RealLast();
696 SMDS_FaceIteratorPtr itf = meshDS->facesIterator();
698 const SMDS_MeshElement* F = itf->next();
699 if(F==face) continue;
700 Handle(TColgp_HSequenceOfPnt) aContour = new TColgp_HSequenceOfPnt;
701 SMDS_ElemIteratorPtr nodeIt = F->nodesIterator();
702 if( !F->IsQuadratic() ) {
703 while ( nodeIt->more() ) {
704 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
705 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
710 while ( nodeIt->more() ) {
712 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
713 aContour->Append(gp_Pnt(node->X(), node->Y(), node->Z()));
714 if(nn==face->NbNodes()/2) break;
718 if( HasIntersection(Ptmp1, PC, PPP, aContour) ) {
720 double tmp = PC.Distance(PPP);
726 if( HasIntersection(Ptmp2, PC, PPP, aContour) ) {
728 double tmp = PC.Distance(PPP);
736 if( IsOK1 && !IsOK2 ) {
737 // using existed direction
738 double tmp = PC.Distance(Pres1)/3.;
741 PCbest = gp_Pnt( PC.X() + tmpDir.X()*height,
742 PC.Y() + tmpDir.Y()*height,
743 PC.Z() + tmpDir.Z()*height );
746 else if( !IsOK1 && IsOK2 ) {
747 // using opposite direction
749 double tmp = PC.Distance(Pres2)/3.;
750 if( height > tmp ) height = tmp;
751 PCbest = gp_Pnt( PC.X() + tmpDir.Reversed().X()*height,
752 PC.Y() + tmpDir.Reversed().Y()*height,
753 PC.Z() + tmpDir.Reversed().Z()*height );
755 else { // IsOK1 && IsOK2
756 double tmp1 = PC.Distance(Pres1)/3.;
757 double tmp2 = PC.Distance(Pres2)/3.;
759 // using existed direction
760 if( height > tmp1 ) {
762 PCbest = gp_Pnt( PC.X() + tmpDir.X()*height,
763 PC.Y() + tmpDir.Y()*height,
764 PC.Z() + tmpDir.Z()*height );
768 // using opposite direction
770 if( height > tmp2 ) height = tmp2;
771 PCbest = gp_Pnt( PC.X() + tmpDir.Reversed().X()*height,
772 PC.Y() + tmpDir.Reversed().Y()*height,
773 PC.Z() + tmpDir.Reversed().Z()*height );
777 // create node for PCbest
778 SMDS_MeshNode* NewNode = meshDS->AddNode( PCbest.X(), PCbest.Y(), PCbest.Z() );
779 // add triangles to result map
780 std::list<const SMDS_FaceOfNodes*> aList;
782 SMDS_FaceOfNodes* NewFace;
784 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
786 NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i+1], FNodes[i] );
787 aList.push_back(NewFace);
789 myResMap.insert(make_pair(face,aList));
791 SMDS_MeshVolume* aPyram;
793 aPyram = meshDS->AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
795 aPyram = meshDS->AddVolume( FNodes[0], FNodes[3], FNodes[2], FNodes[1], NewNode );
796 myMapFPyram.insert(make_pair(face,aPyram));
797 } // end loop on elements on a face
799 return Compute2ndPart(aMesh);
803 //=======================================================================
804 //function : Compute2ndPart
806 //=======================================================================
808 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh)
810 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
812 // check intersections between created pyramids
813 int NbPyram = myMapFPyram.size();
814 //cout<<"NbPyram = "<<NbPyram<<endl;
818 std::vector< const SMDS_MeshElement* > Pyrams(NbPyram);
819 std::vector< const SMDS_MeshElement* > Faces(NbPyram);
820 std::map< const SMDS_MeshElement*,
821 const SMDS_MeshElement* >::iterator itp = myMapFPyram.begin();
823 for(; itp!=myMapFPyram.end(); itp++, i++) {
824 Faces[i] = (*itp).first;
825 Pyrams[i] = (*itp).second;
827 StdMeshers_Array1OfSequenceOfInteger MergesInfo(0,NbPyram-1);
828 for(i=0; i<NbPyram; i++) {
829 TColStd_SequenceOfInteger aMerges;
831 MergesInfo.SetValue(i,aMerges);
833 for(i=0; i<NbPyram-1; i++) {
834 const SMDS_MeshElement* Prm1 = Pyrams[i];
835 SMDS_ElemIteratorPtr nIt = Prm1->nodesIterator();
836 std::vector<gp_Pnt> Ps1(5);
837 const SMDS_MeshNode* Ns1[5];
839 while( nIt->more() ) {
840 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nIt->next() );
842 Ps1[k] = gp_Pnt(node->X(), node->Y(), node->Z());
845 bool NeedMove = false;
846 for(int j=i+1; j<NbPyram; j++) {
847 //cout<<" i="<<i<<" j="<<j<<endl;
848 const TColStd_SequenceOfInteger& aMergesI = MergesInfo.Value(i);
849 int nbI = aMergesI.Length();
850 const TColStd_SequenceOfInteger& aMergesJ = MergesInfo.Value(j);
851 int nbJ = aMergesJ.Length();
854 bool NeedCont = false;
856 if(aMergesI.Value(k)==j) {
861 if(NeedCont) continue;
863 const SMDS_MeshElement* Prm2 = Pyrams[j];
864 nIt = Prm2->nodesIterator();
865 std::vector<gp_Pnt> Ps2(5);
866 const SMDS_MeshNode* Ns2[5];
868 while( nIt->more() ) {
869 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nIt->next() );
871 Ps2[k] = gp_Pnt(node->X(), node->Y(), node->Z());
878 gp_Vec Vtmp(Ps1[k],Ps1[4]);
879 gp_Pnt Pshift( Ps1[k].X() + Vtmp.X()*0.01,
880 Ps1[k].Y() + Vtmp.Y()*0.01,
881 Ps1[k].Z() + Vtmp.Z()*0.01 );
884 if( HasIntersection3( Pshift, Ps1[4], Pint, Ps2[m], Ps2[m+1], Ps2[4]) ) {
889 if( HasIntersection3( Pshift, Ps1[4], Pint, Ps2[3], Ps2[0], Ps2[4]) ) {
896 gp_Vec Vtmp(Ps2[k],Ps2[4]);
897 gp_Pnt Pshift( Ps2[k].X() + Vtmp.X()*0.01,
898 Ps2[k].Y() + Vtmp.Y()*0.01,
899 Ps2[k].Z() + Vtmp.Z()*0.01 );
902 if( HasIntersection3( Pshift, Ps2[4], Pint, Ps1[m], Ps1[m+1], Ps1[4]) ) {
907 if( HasIntersection3( Pshift, Ps2[4], Pint, Ps1[3], Ps1[0], Ps1[4]) ) {
915 //cout<<" has intersec for i="<<i<<" j="<<j<<endl;
916 // check if MeshFaces have 2 common node
919 for(int m=0; m<4; m++) {
920 if( Ns1[k]==Ns2[m] ) nbc++;
923 //cout<<" nbc = "<<nbc<<endl;
925 // create common node
926 SMDS_MeshNode* CommonNode = const_cast<SMDS_MeshNode*>(Ns1[4]);
927 CommonNode->setXYZ( ( nbI*Ps1[4].X() + nbJ*Ps2[4].X() ) / (nbI+nbJ),
928 ( nbI*Ps1[4].Y() + nbJ*Ps2[4].Y() ) / (nbI+nbJ),
929 ( nbI*Ps1[4].Z() + nbJ*Ps2[4].Z() ) / (nbI+nbJ) );
931 //cout<<" CommonNode: "<<CommonNode;
932 const SMDS_MeshNode* Nrem = Ns2[4];
934 meshDS->ChangeElementNodes(Prm2, Ns2, 5);
935 // update pyramids for J
936 for(k=2; k<=nbJ; k++) {
937 const SMDS_MeshElement* tmpPrm = Pyrams[aMergesJ.Value(k)];
938 SMDS_ElemIteratorPtr tmpIt = tmpPrm->nodesIterator();
939 const SMDS_MeshNode* Ns[5];
941 while( tmpIt->more() ) {
942 Ns[m] = static_cast<const SMDS_MeshNode*>( tmpIt->next() );
946 meshDS->ChangeElementNodes(tmpPrm, Ns, 5);
950 for(k=1; k<=nbI; k++) {
951 int num = aMergesI.Value(k);
952 const TColStd_SequenceOfInteger& aSeq = MergesInfo.Value(num);
953 TColStd_SequenceOfInteger tmpSeq;
955 for(; m<=aSeq.Length(); m++) {
956 tmpSeq.Append(aSeq.Value(m));
958 for(m=1; m<=nbJ; m++) {
959 tmpSeq.Append(aMergesJ.Value(m));
961 MergesInfo.SetValue(num,tmpSeq);
963 for(k=1; k<=nbJ; k++) {
964 int num = aMergesJ.Value(k);
965 const TColStd_SequenceOfInteger& aSeq = MergesInfo.Value(num);
966 TColStd_SequenceOfInteger tmpSeq;
968 for(; m<=aSeq.Length(); m++) {
969 tmpSeq.Append(aSeq.Value(m));
971 for(m=1; m<=nbI; m++) {
972 tmpSeq.Append(aMergesI.Value(m));
974 MergesInfo.SetValue(num,tmpSeq);
977 // update triangles for aMergesJ
978 for(k=1; k<=nbJ; k++) {
979 std::list< std::list< const SMDS_MeshNode* > > aFNodes;
980 std::list< const SMDS_MeshElement* > aFFaces;
981 int num = aMergesJ.Value(k);
982 std::map< const SMDS_MeshElement*,
983 std::list<const SMDS_FaceOfNodes*> >::iterator itrm = myResMap.find(Faces[num]);
984 std::list<const SMDS_FaceOfNodes*> trias = (*itrm).second;
985 std::list<const SMDS_FaceOfNodes*>::iterator itt = trias.begin();
986 for(; itt!=trias.end(); itt++) {
988 SMDS_ElemIteratorPtr nodeIt = (*itt)->nodesIterator();
989 const SMDS_MeshNode* NF[3];
990 while ( nodeIt->more() ) {
992 NF[nn] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
995 SMDS_FaceOfNodes* Ftria = const_cast< SMDS_FaceOfNodes*>( (*itt) );
996 Ftria->ChangeNodes(NF, 3);
1000 // check and remove coincided faces
1001 TColStd_SequenceOfInteger IdRemovedTrias;
1003 for(; i1<=nbI; i1++) {
1004 int numI = aMergesI.Value(i1);
1005 std::map< const SMDS_MeshElement*,
1006 std::list<const SMDS_FaceOfNodes*> >::iterator itrmI = myResMap.find(Faces[numI]);
1007 std::list<const SMDS_FaceOfNodes*> triasI = (*itrmI).second;
1008 std::list<const SMDS_FaceOfNodes*>::iterator ittI = triasI.begin();
1009 int nbfI = triasI.size();
1010 const SMDS_FaceOfNodes* FsI[nbfI];
1012 for(; ittI!=triasI.end(); ittI++) {
1017 for(; i2<nbfI; i2++) {
1018 const SMDS_FaceOfNodes* FI = FsI[i2];
1021 for(; j1<=nbJ; j1++) {
1022 int numJ = aMergesJ.Value(j1);
1023 std::map< const SMDS_MeshElement*,
1024 std::list<const SMDS_FaceOfNodes*> >::iterator itrmJ = myResMap.find(Faces[numJ]);
1025 std::list<const SMDS_FaceOfNodes*> triasJ = (*itrmJ).second;
1026 std::list<const SMDS_FaceOfNodes*>::iterator ittJ = triasJ.begin();
1027 int nbfJ = triasJ.size();
1028 const SMDS_FaceOfNodes* FsJ[nbfJ];
1030 for(; ittJ!=triasJ.end(); ittJ++) {
1035 for(; j2<nbfJ; j2++) {
1036 const SMDS_FaceOfNodes* FJ = FsJ[j2];
1037 // compare triangles
1038 if( CompareTrias(FI,FJ) ) {
1039 IdRemovedTrias.Append( FI->GetID() );
1040 IdRemovedTrias.Append( FJ->GetID() );
1043 std::list<const SMDS_FaceOfNodes*> new_triasI;
1044 for(k=0; k<nbfI; k++) {
1045 if( FsI[k]==0 ) continue;
1046 new_triasI.push_back( FsI[k] );
1048 (*itrmI).second = new_triasI;
1049 triasI = new_triasI;
1050 std::list<const SMDS_FaceOfNodes*> new_triasJ;
1051 for(k=0; k<nbfJ; k++) {
1052 if( FsJ[k]==0 ) continue;
1053 new_triasJ.push_back( FsJ[k] );
1055 (*itrmJ).second = new_triasJ;
1056 triasJ = new_triasJ;
1060 // close for j2 and j1
1069 meshDS->RemoveNode(Nrem);
1072 //cout<<"decrease height of pyramids"<<endl;
1073 // decrease height of pyramids
1074 double xc1 = 0., yc1 = 0., zc1 = 0.;
1075 double xc2 = 0., yc2 = 0., zc2 = 0.;
1076 for(k=0; k<4; k++) {
1084 gp_Pnt PC1(xc1/4.,yc1/4.,zc1/4.);
1085 gp_Pnt PC2(xc2/4.,yc2/4.,zc2/4.);
1086 gp_Vec VN1(PC1,Ps1[4]);
1087 gp_Vec VI1(PC1,Pint);
1088 gp_Vec VN2(PC2,Ps2[4]);
1089 gp_Vec VI2(PC2,Pint);
1090 double ang1 = fabs(VN1.Angle(VI1));
1091 double ang2 = fabs(VN2.Angle(VI2));
1094 h1 = VI1.Magnitude()/2;
1096 h1 = VI1.Magnitude()*cos(ang1);
1098 h2 = VI2.Magnitude()/2;
1100 h2 = VI2.Magnitude()*cos(ang2);
1103 coef1 -= cos(ang1)*0.25;
1106 coef2 -= cos(ang1)*0.25;
1108 SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>(Ns1[4]);
1110 aNode1->setXYZ( PC1.X()+VN1.X(), PC1.Y()+VN1.Y(), PC1.Z()+VN1.Z() );
1111 SMDS_MeshNode* aNode2 = const_cast<SMDS_MeshNode*>(Ns2[4]);
1113 aNode2->setXYZ( PC2.X()+VN2.X(), PC2.Y()+VN2.Y(), PC2.Z()+VN2.Z() );
1118 //cout<<" no intersec for i="<<i<<" j="<<j<<endl;
1122 if( NeedMove && !meshDS->IsEmbeddedMode() ) {
1123 meshDS->MoveNode( Ns1[4], Ns1[4]->X(), Ns1[4]->Y(), Ns1[4]->Z() );
1131 //================================================================================
1133 * \brief Return list of created triangles for given face
1135 //================================================================================
1136 std::list<const SMDS_FaceOfNodes*> StdMeshers_QuadToTriaAdaptor::GetTriangles
1137 (const SMDS_MeshElement* aFace)
1139 std::list<const SMDS_FaceOfNodes*> aRes;
1140 std::map< const SMDS_MeshElement*,
1141 std::list<const SMDS_FaceOfNodes*> >::iterator it = myResMap.find(aFace);
1142 if( it != myResMap.end() ) {
1143 aRes = (*it).second;
1149 //================================================================================
1151 * \brief Remove all create auxilary faces
1153 //================================================================================
1154 //void StdMeshers_QuadToTriaAdaptor::RemoveFaces(SMESH_Mesh& aMesh)
1156 // SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
1157 // std::map< const SMDS_MeshElement*,
1158 // std::list<const SMDS_MeshElement*> >::iterator it = myResMap.begin();
1159 // for(; it != myResMap.end(); it++ ) {
1160 // std::list<const SMDS_MeshElement*> aFaces = (*it).second;
1161 // std::list<const SMDS_MeshElement*>::iterator itf = aFaces.begin();
1162 // for(; itf!=aFaces.end(); itf++ ) {
1163 // meshDS->RemoveElement( (*itf) );