Salome HOME
correct previous integration (Porting to Python 2.6)
[modules/smesh.git] / src / StdMeshers / StdMeshers_QuadToTriaAdaptor.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESH : implementaion of SMESH idl descriptions
23 // File      : StdMeshers_QuadToTriaAdaptor.cxx
24 // Module    : SMESH
25 // Created   : Wen May 07 16:37:07 2008
26 // Author    : Sergey KUUL (skl)
27 //
28 #include "StdMeshers_QuadToTriaAdaptor.hxx"
29
30 #include <SMDS_FaceOfNodes.hxx>
31 #include <SMESH_Algo.hxx>
32 #include <SMESH_MesherHelper.hxx>
33
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>
39 #include <TopoDS.hxx>
40 #include <gp_Lin.hxx>
41 #include <gp_Pln.hxx>
42
43 #include <NCollection_Array1.hxx>
44 typedef NCollection_Array1<TColStd_SequenceOfInteger> StdMeshers_Array1OfSequenceOfInteger;
45
46
47 //=======================================================================
48 //function : StdMeshers_QuadToTriaAdaptor
49 //purpose  : 
50 //=======================================================================
51
52 StdMeshers_QuadToTriaAdaptor::StdMeshers_QuadToTriaAdaptor()
53 {
54 }
55
56
57 //================================================================================
58 /*!
59  * \brief Destructor
60  */
61 //================================================================================
62
63 StdMeshers_QuadToTriaAdaptor::~StdMeshers_QuadToTriaAdaptor()
64 {
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 )
69   {
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 )
73       delete *f;
74   }
75   myResMap.clear();
76
77 //   TF2PyramMap::iterator itp = myMapFPyram.begin();
78 //   for(; itp!=myMapFPyram.end(); itp++)
79 //     cout << itp->second << endl;
80 }
81
82
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)
90 {
91   double a = P1.Distance(P2);
92   double b = P1.Distance(PC);
93   double c = P2.Distance(PC);
94   if( a < (b+c)/2 )
95     return PC;
96   else {
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 );
99     gp_Dir aDir(V);
100     gp_Pnt Pbest( PC.X() + aDir.X()*shift,  PC.Y() + aDir.Y()*shift,
101                   PC.Z() + aDir.Z()*shift );
102     return Pbest;
103   }
104 }
105
106
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)
115 {
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;
122   gp_Vec VP1(P1,P2);
123   gp_Vec VP2(P1,P3);
124   IntAna_Quadric IAQ(gp_Pln(P1,VP1.Crossed(VP2)));
125   IntAna_IntConicQuad IAICQ(gp_Lin(PC,gp_Dir(gp_Vec(PC,P))),IAQ);
126   if(IAICQ.IsDone()) {
127     if( IAICQ.IsInQuadric() )
128       return false;
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]
133       bool IsExternal =
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 );
137       if(IsExternal) {
138         return false;
139       }
140       // check if this point is internal for triangle (P1,P2,P3)
141       gp_Vec V1(PIn,P1);
142       gp_Vec V2(PIn,P2);
143       gp_Vec V3(PIn,P3);
144       if( V1.Magnitude()<preci || V2.Magnitude()<preci ||
145           V3.Magnitude()<preci ) {
146         Pint = PIn;
147         return true;
148       }
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)) {
154           return false;
155         }
156       }
157       else if(VC2.Magnitude()<preci) {
158         if(VC1.IsOpposite(VC3,preci)) {
159           return false;
160         }
161       }
162       else if(VC3.Magnitude()<preci) {
163         if(VC1.IsOpposite(VC2,preci)) {
164           return false;
165         }
166       }
167       else {
168         if( VC1.IsOpposite(VC2,preci) || VC1.IsOpposite(VC3,preci) ||
169             VC2.IsOpposite(VC3,preci) ) {
170           return false;
171         }
172       }
173       Pint = PIn;
174       return true;
175     }
176   }
177
178   return false;
179 }
180
181
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)
188 {
189   if(aContour->Length()==3) {
190     return HasIntersection3( P, PC, Pint, aContour->Value(1),
191                              aContour->Value(2), aContour->Value(3) );
192   }
193   else {
194     bool check = false;
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) );
200     }
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) );
207     }
208     if(check) return true;
209   }
210
211   return false;
212 }
213
214
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)
225 {
226   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
227   //cout<<"    CheckIntersection: meshDS->NbFaces() = "<<meshDS->NbFaces()<<endl;
228   bool res = false;
229   double dist = RealLast();
230   gp_Pnt Pres;
231   for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
232     const TopoDS_Shape& aShapeFace = exp.Current();
233     if(aShapeFace==NotCheckedFace)
234       continue;
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() )
244           nbN /= 2;
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()));
248         }
249         if( HasIntersection(P, PC, Pres, aContour) ) {
250           res = true;
251           double tmp = PC.Distance(Pres);
252           if(tmp<dist) {
253             Pint = Pres;
254             dist = tmp;
255           }
256         }
257       }
258     }
259   }
260   return res;
261 }
262
263
264 //=======================================================================
265 //function : CompareTrias
266 //purpose  : Auxilare for Compute()
267 //=======================================================================
268 static bool CompareTrias(const SMDS_MeshElement* F1,const SMDS_MeshElement* F2)
269 {
270   return
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) );
273 }
274
275
276 //=======================================================================
277 //function : IsDegenarate
278 //purpose  : Auxilare for Preparation()
279 //=======================================================================
280 // static int IsDegenarate(const Handle(TColgp_HArray1OfPnt)& PN)
281 // {
282 //   int i = 1;
283 //   for(; i<4; i++) {
284 //     int j = i+1;
285 //     for(; j<=4; j++) {
286 //       if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
287 //         return j;
288 //     }
289 //   }
290 //   return 0;
291 // }
292
293
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)
306 {
307   int i = 0;
308   double xc=0., yc=0., zc=0.;
309   SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
310   if( !face->IsQuadratic() ) {
311     if( face->NbNodes() != 4 )
312       return 0;
313     while ( nodeIt->more() ) {
314       i++;
315       const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
316       FNodes[i-1] = node;
317       PN->SetValue( i, gp_Pnt(node->X(), node->Y(), node->Z()) );
318       xc += node->X();
319       yc += node->Y();
320       zc += node->Z();
321     }
322   }
323   else {
324     if( face->NbNodes() != 8)
325       return 0;
326     while ( nodeIt->more() ) {
327       i++;
328       const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
329       FNodes[i-1] = node;
330       PN->SetValue( i, gp_Pnt(node->X(), node->Y(), node->Z()) );
331       xc += node->X();
332       yc += node->Y();
333       zc += node->Z();
334       if(i==4) break;
335     }
336   }
337
338   int nbp = 4;
339
340   int j = 0;
341   for(i=1; i<4; i++) {
342     j = i+1;
343     for(; j<=4; j++) {
344       if( PN->Value(i).Distance(PN->Value(j)) < 1.e-6 )
345         break;
346     }
347     if(j<=4) break;
348   }
349   //int deg_num = IsDegenarate(PN);
350   //if(deg_num>0) {
351   bool hasdeg = false;
352   if(i<4) {
353     //cout<<"find degeneration"<<endl;
354     hasdeg = true;
355     gp_Pnt Pdeg = PN->Value(i);
356
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) {
363         DegNode = N;
364         //DegNode = const_cast<SMDS_MeshNode*>(N);
365         break;
366       }
367     }
368     if(!DegNode) {
369       DegNode = FNodes[i-1];
370       myDegNodes.push_back(DegNode);
371     }
372     else {
373       FNodes[i-1] = DegNode;
374     }
375     for(i=j; i<4; i++) {
376       PN->SetValue(i,PN->Value(i+1));
377       FNodes[i-1] = FNodes[i];
378     }
379     nbp = 3;
380     //PC = gp_Pnt( PN->Value(1).X() + PN.Value
381   }
382
383   PC = gp_Pnt(xc/4., yc/4., zc/4.);
384   //cout<<"  PC("<<PC.X()<<","<<PC.Y()<<","<<PC.Z()<<")"<<endl;
385
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);
403     VNorm += Vtmp;
404   }
405   //cout<<"  VNorm("<<VNorm.X()<<","<<VNorm.Y()<<","<<VNorm.Z()<<")"<<endl;
406   if(hasdeg) return 2;
407   return 1;
408 }
409
410
411 //=======================================================================
412 //function : Compute
413 //purpose  : 
414 //=======================================================================
415
416 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
417 {
418   myResMap.clear();
419   myMapFPyram.clear();
420
421   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
422   SMESH_MesherHelper helper(aMesh);
423   helper.IsQuadraticSubMesh(aShape);
424   helper.SetElementsOnShape( true );
425
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 );
431
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);
440         gp_Pnt PC;
441         gp_Vec VNorm;
442         int stat =  Preparation(face, PN, VN, FNodes, PC, VNorm);
443         if(stat==0)
444           continue;
445
446         if(stat==2) {
447           // degenerate face
448           // add triangles to result map
449           std::list<const SMDS_FaceOfNodes*> aList;
450           SMDS_FaceOfNodes* NewFace;
451           if(!isRev)
452             NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
453           else
454             NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
455           aList.push_back(NewFace);
456           myResMap.insert(make_pair(face,aList));
457           continue;
458         }
459
460         if(!isRev) VNorm.Reverse();
461         double xc = 0., yc = 0., zc = 0.;
462         int i = 1;
463         for(; i<=4; i++) {
464           gp_Pnt Pbest;
465           if(!isRev)
466             Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i).Reversed());
467           else
468             Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
469           xc += Pbest.X();
470           yc += Pbest.Y();
471           zc += Pbest.Z();
472         }
473         gp_Pnt PCbest(xc/4., yc/4., zc/4.);
474
475         // check PCbest
476         double height = PCbest.Distance(PC);
477         if(height<1.e-6) {
478           // create new PCbest using a bit shift along VNorm
479           PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
480         }
481         else {
482           // check possible intersection with other faces
483           gp_Pnt Pint;
484           bool check = CheckIntersection(PCbest, PC, Pint, aMesh, aShape, aShapeFace);
485           if(check) {
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;
491           }
492           else {
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);
496             if(check) {
497               double dist = PC.Distance(Pint)/3.;
498               if(dist<height) {
499                 gp_Dir aDir(gp_Vec(PC,PCbest));
500                 PCbest = PC.XYZ() + aDir.XYZ() * dist;
501               }
502             }
503           }
504         }
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;
509         for(i=0; i<4; i++) {
510           SMDS_FaceOfNodes* NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
511           aList.push_back(NewFace);
512         }
513         myResMap.insert(make_pair(face,aList));
514         // create pyramid
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
519     }
520   } // end for(TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next()) {
521
522   return Compute2ndPart(aMesh);
523 }
524
525
526 //=======================================================================
527 //function : Compute
528 //purpose  : 
529 //=======================================================================
530
531 bool StdMeshers_QuadToTriaAdaptor::Compute(SMESH_Mesh& aMesh)
532 {
533   myResMap.clear();
534   myMapFPyram.clear();
535   SMESH_MesherHelper helper(aMesh);
536   helper.IsQuadraticSubMesh(aMesh.GetShapeToMesh());
537   helper.SetElementsOnShape( true );
538
539   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
540
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() );
544
545   TIDSortedElemSet::iterator itFace = sortedFaces.begin(), fEnd = sortedFaces.end();
546   for ( ; itFace != fEnd; ++itFace )
547   {
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);
555     gp_Pnt PC;
556     gp_Vec VNorm;
557
558     int stat =  Preparation(face, PN, VN, FNodes, PC, VNorm);
559     if(stat==0)
560       continue;
561
562     if(stat==2) {
563       // degenerate face
564       // add triangles to result map
565       std::list<const SMDS_FaceOfNodes*> aList;
566       SMDS_FaceOfNodes* NewFace;
567       // check orientation
568
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
574       bool IsRev = false;
575       bool IsOK1 = false;
576       bool IsOK2 = false;
577       double dist1 = RealLast();
578       double dist2 = RealLast();
579       gp_Pnt Pres1,Pres2;
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()));
589           }
590         }
591         else {
592           int nn = 0;
593           while ( nodeIt->more() ) {
594             nn++;
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;
598           }
599         }
600         gp_Pnt PPP;
601         if( HasIntersection(Ptmp1, PC, PPP, aContour) ) {
602           IsOK1 = true;
603           double tmp = PC.Distance(PPP);
604           if(tmp<dist1) {
605             Pres1 = PPP;
606             dist1 = tmp;
607           }
608         }
609         if( HasIntersection(Ptmp2, PC, PPP, aContour) ) {
610           IsOK2 = true;
611           double tmp = PC.Distance(PPP);
612           if(tmp<dist2) {
613             Pres2 = PPP;
614             dist2 = tmp;
615           }
616         }
617       }
618
619       if( IsOK1 && !IsOK2 ) {
620         // using existed direction
621       }
622       else if( !IsOK1 && IsOK2 ) {
623         // using opposite direction
624         IsRev = true;
625       }
626       else { // IsOK1 && IsOK2
627         double tmp1 = PC.Distance(Pres1)/3.;
628         double tmp2 = PC.Distance(Pres2)/3.;
629         if(tmp1<tmp2) {
630           // using existed direction
631         }
632         else {
633           // using opposite direction
634           IsRev = true;
635         }
636       }
637       if(!IsRev)
638         NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[1], FNodes[2] );
639       else
640         NewFace = new SMDS_FaceOfNodes( FNodes[0], FNodes[2], FNodes[1] );
641       aList.push_back(NewFace);
642       myResMap.insert(make_pair(face,aList));
643       continue;
644     }
645     
646     double xc = 0., yc = 0., zc = 0.;
647     int i = 1;
648     for(; i<=4; i++) {
649       gp_Pnt Pbest = FindBestPoint(PN->Value(i), PN->Value(i+1), PC, VN->Value(i));
650       xc += Pbest.X();
651       yc += Pbest.Y();
652       zc += Pbest.Z();
653     }
654     gp_Pnt PCbest(xc/4., yc/4., zc/4.);
655     double height = PCbest.Distance(PC);
656     if(height<1.e-6) {
657       // create new PCbest using a bit shift along VNorm
658       PCbest = PC.XYZ() + VNorm.XYZ() * 0.001;
659       height = PCbest.Distance(PC);
660     }
661     //cout<<"  PCbest("<<PCbest.X()<<","<<PCbest.Y()<<","<<PCbest.Z()<<")"<<endl;
662
663     gp_Vec V1(PC,PCbest);
664     double tmp = PN->Value(1).Distance(PN->Value(3)) +
665       PN->Value(2).Distance(PN->Value(4));
666     gp_Dir tmpDir(V1);
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
670     bool IsRev = false;
671     bool IsOK1 = false;
672     bool IsOK2 = false;
673     double dist1 = RealLast();
674     double dist2 = RealLast();
675     gp_Pnt Pres1,Pres2;
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()));
685         }
686       }
687       else {
688         int nn = 0;
689         while ( nodeIt->more() ) {
690           nn++;
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;
694         }
695       }
696       gp_Pnt PPP;
697       if( HasIntersection(Ptmp1, PC, PPP, aContour) ) {
698         IsOK1 = true;
699         double tmp = PC.Distance(PPP);
700         if(tmp<dist1) {
701           Pres1 = PPP;
702           dist1 = tmp;
703         }
704       }
705       if( HasIntersection(Ptmp2, PC, PPP, aContour) ) {
706         IsOK2 = true;
707         double tmp = PC.Distance(PPP);
708         if(tmp<dist2) {
709           Pres2 = PPP;
710           dist2 = tmp;
711         }
712       }
713     }
714
715     if( IsOK1 && !IsOK2 ) {
716       // using existed direction
717       double tmp = PC.Distance(Pres1)/3.;
718       if( height > tmp ) {
719         height = tmp;
720         PCbest = PC.XYZ() + tmpDir.XYZ() * height;
721       }
722     }
723     else if( !IsOK1 && IsOK2 ) {
724       // using opposite direction
725       IsRev = true;
726       double tmp = PC.Distance(Pres2)/3.;
727       if( height > tmp ) height = tmp;
728       PCbest = PC.XYZ() - tmpDir.XYZ() * height;
729     }
730     else { // IsOK1 && IsOK2
731       double tmp1 = PC.Distance(Pres1)/3.;
732       double tmp2 = PC.Distance(Pres2)/3.;
733       if(tmp1<tmp2) {
734         // using existed direction
735         if( height > tmp1 ) {
736           height = tmp1;
737           PCbest = PC.XYZ() + tmpDir.XYZ() * height;
738         }
739       }
740       else {
741         // using opposite direction
742         IsRev = true;
743         if( height > tmp2 ) height = tmp2;
744         PCbest = PC.XYZ() - tmpDir.XYZ() * height;
745       }
746     }
747
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;
752     for(i=0; i<4; i++) {
753       SMDS_FaceOfNodes* NewFace;
754       if(IsRev)
755         NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i], FNodes[i+1] );
756       else
757         NewFace = new SMDS_FaceOfNodes( NewNode, FNodes[i+1], FNodes[i] );
758       aList.push_back(NewFace);
759     }
760     myResMap.insert(make_pair(face,aList));
761     // create pyramid
762     SMDS_MeshVolume* aPyram;
763     if(IsRev)
764      aPyram = helper.AddVolume( FNodes[0], FNodes[1], FNodes[2], FNodes[3], NewNode );
765     else
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
769
770   return Compute2ndPart(aMesh);
771 }
772
773
774 //=======================================================================
775 //function : Compute2ndPart
776 //purpose  : 
777 //=======================================================================
778
779 bool StdMeshers_QuadToTriaAdaptor::Compute2ndPart(SMESH_Mesh& aMesh)
780 {
781   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
782
783   // check intersections between created pyramids
784   int NbPyram = myMapFPyram.size();
785   //cout<<"NbPyram = "<<NbPyram<<endl;
786   if(NbPyram==0)
787     return true;
788
789   vector< const SMDS_MeshElement* > Pyrams(NbPyram);
790   vector< const SMDS_MeshElement* > Faces(NbPyram);
791   TF2PyramMap::iterator itp = myMapFPyram.begin();
792   int i = 0;
793   for(; itp!=myMapFPyram.end(); itp++, i++) {
794     Faces[i] = (*itp).first;
795     Pyrams[i] = (*itp).second;
796   }
797   StdMeshers_Array1OfSequenceOfInteger MergesInfo(0,NbPyram-1);
798   for(i=0; i<NbPyram; i++) {
799     TColStd_SequenceOfInteger aMerges;
800     aMerges.Append(i);
801     MergesInfo.SetValue(i,aMerges);
802   }
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() );
808     int k = 0;
809     for ( ; k < Ns1.size(); ++k ) {
810       const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nIt->next() );
811       Ns1[k] = node;
812       Ps1[k] = gp_Pnt(node->X(), node->Y(), node->Z());
813     }
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) {
825           NeedCont = true;
826           break;
827         }
828       }
829       if(NeedCont) continue; // already merged
830
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() );
837         Ns2[k] = node;
838         Ps2[k] = gp_Pnt(node->X(), node->Y(), node->Z());
839       }
840
841       bool hasInt = false;
842       gp_Pnt Pint;
843       for(k=0; k<4; k++) {
844         gp_Vec Vtmp(Ps1[k],Ps1[4]);
845         gp_Pnt Pshift = Ps1[k].XYZ() + Vtmp.XYZ() * 0.01;
846         int m=0;
847         for(; m<3; m++) {
848           if( HasIntersection3( Pshift, Ps1[4], Pint, Ps2[m], Ps2[m+1], Ps2[4]) ) {
849             hasInt = true;
850             break;
851           }
852         }
853         if( HasIntersection3( Pshift, Ps1[4], Pint, Ps2[3], Ps2[0], Ps2[4]) ) {
854           hasInt = true;
855         }
856         if(hasInt) break;
857       }
858       if(!hasInt) {
859         for(k=0; k<4; k++) {
860           gp_Vec Vtmp(Ps2[k],Ps2[4]);
861           gp_Pnt Pshift = Ps2[k].XYZ() + Vtmp.XYZ() * 0.01;
862           int m=0;
863           for(; m<3; m++) {
864             if( HasIntersection3( Pshift, Ps2[4], Pint, Ps1[m], Ps1[m+1], Ps1[4]) ) {
865               hasInt = true;
866               break;
867             }
868           }
869           if( HasIntersection3( Pshift, Ps2[4], Pint, Ps1[3], Ps1[0], Ps1[4]) ) {
870             hasInt = true;
871           }
872           if(hasInt) break;
873         }
874       }
875
876       if(hasInt) {
877         //cout<<"    has intersec for i="<<i<<" j="<<j<<endl;
878         // check if MeshFaces have 2 common node
879         int nbc = 0;
880         for(k=0; k<4; k++) {
881           for(int m=0; m<4; m++) {
882             if( Ns1[k]==Ns2[m] ) nbc++;
883           }
884         }
885         //cout<<"      nbc = "<<nbc<<endl;
886         if(nbc>0) {
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) );
892           NeedMove = true;
893           //cout<<"       CommonNode: "<<CommonNode;
894           const SMDS_MeshNode* Nrem = Ns2[4];
895           Ns2[4] = CommonNode;
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() );
904             Ns[4] = CommonNode;
905             meshDS->ChangeElementNodes(tmpPrm, &Ns[0], Ns.size());
906           }
907
908           // update MergesInfo
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));
914           }
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));
920           }
921
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];
934               int nn = 0;
935               while ( nodeIt->more() )
936                 NF[nn++] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
937               NF[0] = CommonNode;
938               SMDS_FaceOfNodes* Ftria = const_cast< SMDS_FaceOfNodes*>( (*itt) );
939               Ftria->ChangeNodes(NF, 3);
940             }
941           }
942
943           // check and remove coincided faces
944           //TColStd_SequenceOfInteger IdRemovedTrias;
945           int i1 = 1;
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);
954             k = 0;
955             for(; ittI!=triasI.end(); ittI++) {
956               FsI[k]  = (*ittI);
957               k++;
958             }
959             int i2 = 0;
960             for(; i2<nbfI; i2++) {
961               const SMDS_FaceOfNodes* FI = FsI[i2];
962               if(FI==0) continue;
963               int j1 = 1;
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);
972                 k = 0;
973                 for(; ittJ!=triasJ.end(); ittJ++) {
974                   FsJ[k]  = (*ittJ);
975                   k++;
976                 }
977                 int j2 = 0;
978                 for(; j2<nbfJ; j2++) {
979                   const SMDS_FaceOfNodes* FJ = FsJ[j2];
980                   // compare triangles
981                   if( CompareTrias(FI,FJ) ) {
982                     //IdRemovedTrias.Append( FI->GetID() );
983                     //IdRemovedTrias.Append( FJ->GetID() );
984                     FsI[i2] = 0;
985                     FsJ[j2] = 0;
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] );
990                     }
991                     (*itrmI).second = new_triasI;
992                     triasI = 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] );
997                     }
998                     (*itrmJ).second = new_triasJ;
999                     triasJ = new_triasJ;
1000                     // remove faces
1001                     delete FI;
1002                     delete FJ;
1003                     // close for j2 and j1
1004                     j1 = nbJ;
1005                     break;
1006                   }
1007                 } // j2
1008               } // j1
1009             } // i2
1010           } // i1
1011           // removing node
1012           meshDS->RemoveNode(Nrem);
1013         }
1014         else { // nbc==0
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++) {
1020             xc1 += Ps1[k].X();
1021             yc1 += Ps1[k].Y();
1022             zc1 += Ps1[k].Z();
1023             xc2 += Ps2[k].X();
1024             yc2 += Ps2[k].Y();
1025             zc2 += Ps2[k].Z();
1026           }
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));
1035           double h1,h2;
1036           if(ang1>PI/3.)
1037             h1 = VI1.Magnitude()/2;
1038           else
1039             h1 = VI1.Magnitude()*cos(ang1);
1040           if(ang2>PI/3.)
1041             h2 = VI2.Magnitude()/2;
1042           else
1043             h2 = VI2.Magnitude()*cos(ang2);
1044           double coef1 = 0.5;
1045           if(ang1<PI/3)
1046             coef1 -= cos(ang1)*0.25;
1047           double coef2 = 0.5;
1048           if(ang2<PI/3)
1049             coef2 -= cos(ang1)*0.25;
1050
1051           SMDS_MeshNode* aNode1 = const_cast<SMDS_MeshNode*>(Ns1[4]);
1052           VN1.Scale(coef1);
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]);
1055           VN2.Scale(coef2);
1056           aNode2->setXYZ( PC2.X()+VN2.X(), PC2.Y()+VN2.Y(), PC2.Z()+VN2.Z() );
1057           NeedMove = true;
1058         }
1059       } // end if(hasInt)
1060       else {
1061         //cout<<"    no intersec for i="<<i<<" j="<<j<<endl;
1062       }
1063
1064     }
1065     if( NeedMove && !meshDS->IsEmbeddedMode() ) {
1066       meshDS->MoveNode( Ns1[4], Ns1[4]->X(), Ns1[4]->Y(), Ns1[4]->Z() );
1067     }
1068   }
1069
1070   return true;
1071 }
1072
1073
1074 //================================================================================
1075 /*!
1076  * \brief Return list of created triangles for given face
1077  */
1078 //================================================================================
1079 const list<const SMDS_FaceOfNodes*>* StdMeshers_QuadToTriaAdaptor::GetTriangles
1080                                                    (const SMDS_MeshElement* aFace)
1081 {
1082   map< const SMDS_MeshElement*,
1083     list<const SMDS_FaceOfNodes*> >::iterator it = myResMap.find(aFace);
1084   if( it != myResMap.end() ) {
1085     return & it->second;
1086   }
1087   return 0;
1088 }
1089
1090
1091 //================================================================================
1092 /*!
1093  * \brief Remove all create auxilary faces
1094  */
1095 //================================================================================
1096 //void StdMeshers_QuadToTriaAdaptor::RemoveFaces(SMESH_Mesh& aMesh)
1097 //{
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) );
1106 //    }
1107 //  }
1108 //}