0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / ChFi3d / ChFi3d_FilBuilder_C3.cxx
1 // Created on: 1995-04-26
2 // Created by: Modelistation
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <ChFi3d_FilBuilder.jxx>
18 #include <ChFi3d_Builder_0.hxx>
19 #include <ChFi3d.hxx>
20
21 #include <Precision.hxx>
22
23 #include <Standard_Failure.hxx>
24 #include <Standard_NotImplemented.hxx>
25 #include <TColStd_ListOfInteger.hxx>
26
27 #include <math_Vector.hxx>
28
29 #include <gp_Pnt.hxx>
30 #include <gp_Vec.hxx>
31 #include <gp_Dir.hxx>
32 #include <gp_Pnt2d.hxx>
33 #include <gp_Ax2.hxx>
34 #include <gp_Ax3.hxx>
35 #include <gp_Lin.hxx>
36 #include <ElCLib.hxx>
37 #include <ElSLib.hxx>
38
39 #include <TColgp_Array1OfPnt2d.hxx>
40
41 #include <Geom_Plane.hxx>
42 #include <Geom_Circle.hxx>
43 #include <Geom_BezierCurve.hxx>
44 #include <Geom2d_BezierCurve.hxx>
45 #include <Geom2d_Curve.hxx>
46 #include <Geom2d_TrimmedCurve.hxx>
47 #include <Geom2d_Line.hxx>
48
49 #include <IntAna_QuadQuadGeo.hxx>
50 #include <IntCurveSurface_IntersectionPoint.hxx>
51
52 #include <TopoDS.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <TopoDS_Face.hxx>
55 #include <TopoDS_Edge.hxx>
56 #include <TopoDS_Vertex.hxx>
57
58 #include <Adaptor3d_HSurface.hxx>
59 #include <Adaptor3d_CurveOnSurface.hxx>
60 #include <Adaptor3d_TopolTool.hxx>
61 #include <Geom2dAdaptor_Curve.hxx>
62 #include <Geom2dAdaptor_HCurve.hxx>
63 #include <GeomAdaptor_Curve.hxx>
64 #include <GeomAdaptor_HCurve.hxx>
65 #include <GeomAdaptor_Surface.hxx>
66 #include <GeomAdaptor_HSurface.hxx>
67 #include <BRepAdaptor_Curve.hxx>
68 #include <BRepAdaptor_HCurve.hxx>
69 #include <BRepAdaptor_Surface.hxx>
70 #include <BRepAdaptor_HSurface.hxx>
71 #include <BRepTopAdaptor_TopolTool.hxx>
72
73 #include <TopAbs.hxx>
74 #include <TopAbs_Orientation.hxx>
75
76 #include <ChFiDS_SurfData.hxx>
77 #include <ChFiDS_CommonPoint.hxx>
78 #include <ChFiDS_FaceInterference.hxx>
79 #include <ChFiDS_Spine.hxx>
80 #include <ChFiDS_SequenceOfSurfData.hxx>
81 #include <ChFiDS_Stripe.hxx>
82 #include <ChFiDS_HData.hxx>
83 #include <ChFiDS_ListIteratorOfListOfStripe.hxx>
84 #include <ChFiDS_Regul.hxx>
85
86 #include <TopOpeBRepDS_HDataStructure.hxx>
87 #include <TopOpeBRepDS_DataStructure.hxx>
88 #include <TopOpeBRepDS_Curve.hxx>
89 #include <TopOpeBRepDS_Surface.hxx>
90
91 #include <ChFiKPart_ComputeData.hxx>
92 #include <BRepBlend_Line.hxx>
93 #include <BRepBlend_ConstRad.hxx>
94 #include <BRepBlend_ConstRadInv.hxx>
95 #include <BRepBlend_EvolRad.hxx>
96 #include <BRepBlend_EvolRadInv.hxx>
97 #include <Law_S.hxx>
98
99 #ifdef DRAW
100 #include <DrawTrSurf.hxx>
101 #endif
102 #ifdef DEB
103 #include <Geom_TrimmedCurve.hxx>
104 extern Standard_Boolean ChFi3d_GettraceDRAWSPINE();
105 extern Standard_Boolean ChFi3d_GetcontextSPINEBEZIER();
106 extern Standard_Boolean ChFi3d_GetcontextSPINECIRCLE();
107 extern Standard_Boolean ChFi3d_GetcontextSPINECE();
108 extern Standard_Boolean ChFi3d_GetcontextFORCEFILLING();
109 #include <OSD_Chronometer.hxx>
110
111 extern Standard_Real  t_t3cornerinit ,t_spherique,t_torique,
112 t_notfilling,t_filling,t_t3cornerDS;
113 extern void ChFi3d_InitChron(OSD_Chronometer& ch);
114 extern void ChFi3d_ResultChron(OSD_Chronometer & ch,Standard_Real& time);
115 #endif
116
117 //=======================================================================
118 //function : SearchPivot
119 //purpose  : 
120 //=======================================================================
121 static Standard_Integer SearchPivot(Standard_Integer* s,
122                                     Standard_Real u[3][3],
123                                     const Standard_Real t)
124 {
125   Standard_Boolean bondeb,bonfin;
126   for(Standard_Integer i = 0; i <= 2; i++){
127     if(s[(i+1)%3] == 1){bondeb = (u[(i+1)%3][i]-u[(i+1)%3][(i+2)%3] >= -t);}
128     else {bondeb = (u[(i+1)%3][i]-u[(i+1)%3][(i+2)%3] <= t);}
129     if(s[(i+2)%3] == 1){bonfin = (u[(i+2)%3][i]-u[(i+2)%3][(i+1)%3] >= -t);}
130     else {bonfin = (u[(i+2)%3][i]-u[(i+2)%3][(i+1)%3] <= t);}
131     if (bondeb && bonfin){ return i; }
132   }
133   return -1;
134 }
135
136
137 //=======================================================================
138 //function : SearchFD
139 //purpose  : 
140 //=======================================================================
141 static Standard_Boolean SearchFD(TopOpeBRepDS_DataStructure& DStr,
142                                  const Handle(ChFiDS_Stripe)& cd1, 
143                                  const Handle(ChFiDS_Stripe)& cd2,
144                                  const Standard_Integer sens1,
145                                  const Standard_Integer sens2,
146                                  Standard_Integer& i1,
147                                  Standard_Integer& i2,
148                                  Standard_Real& p1,
149                                  Standard_Real& p2,
150                                  const Standard_Integer ind1,
151                                  const Standard_Integer ind2,
152                                  TopoDS_Face& face,
153                                  Standard_Boolean& sameside,
154                                  Standard_Integer& jf1,
155                                  Standard_Integer& jf2)
156 {
157   Standard_Boolean found = Standard_False;
158   Standard_Integer id1 = ind1, id2 = ind2;
159   Standard_Integer if1 = ind1, if2 = ind2;
160   Standard_Integer l1 = cd1->SetOfSurfData()->Length();
161   Standard_Integer l2 = cd2->SetOfSurfData()->Length();
162   Standard_Integer i;
163   Standard_Boolean fini1 = Standard_False, fini2 = Standard_False;
164   Standard_Boolean visavis;
165   TopoDS_Vertex Vtx;
166   while( !found ){
167     for(i = id1; (i*sens1) <= (if1*sens1) && !found && !fini2; i = i+sens1 ){ 
168       if(ChFi3d_IsInFront(DStr,cd1,cd2,i,if2,sens1,sens2,p1,p2,face,sameside,jf1,jf2,visavis,Vtx,Standard_False,0)){
169         i1 = i;
170         i2 = if2;
171         found = Standard_True;
172       }
173     }
174     if(!fini1){
175       if1 = if1 + sens1;
176       if(if1 < 1 || if1 > l1){ if1 = if1 - sens1; fini1 = Standard_True; }
177     }
178     
179     for(i = id2; (i*sens2) <= (if2*sens2) && !found && !fini1; i = i+sens2 ){ 
180       if(ChFi3d_IsInFront(DStr,cd1,cd2,if1,i,sens1,sens2,p1,p2,face,sameside,jf1,jf2,visavis,Vtx,Standard_False,0)){
181         i1 = if1;
182         i2 = i;
183         found = Standard_True;
184       }
185     }
186     if(!fini2){
187       if2 = if2 + sens2;
188       if(if2 < 1 || if2 > l2){ if2 = if2 - sens2; fini2 = Standard_True; }
189     }
190     if(fini1 && fini2) break;
191   }
192   return found;
193 }
194
195
196 //=======================================================================
197 //function : ToricCorner
198 //purpose  : Test if this is a paricular cas of a torus corner 
199 //           (or spherical limited by isos).
200 //=======================================================================
201
202 static Standard_Boolean ToricCorner(const TopoDS_Face& F,
203                                     const Standard_Real rd,
204                                     const Standard_Real rf,
205                                     const gp_Vec& v)
206 {
207   if(Abs(rd-rf) > Precision::Confusion()){ return Standard_False; }
208   BRepAdaptor_Surface bs(F);
209   if(bs.GetType() != GeomAbs_Plane){ return Standard_False; }
210   Standard_Real scal1 = Abs(bs.Plane().Position().XDirection().Dot(v));
211   Standard_Real scal2 = Abs(bs.Plane().Position().YDirection().Dot(v));
212   return (scal1 <= Precision::Confusion() && 
213           scal2 <= Precision::Confusion());
214 }
215
216 //=======================================================================
217 //function : PerformThreeCorner
218 //purpose  : Calculate fillet on a top with three edges 
219 //           incident carrying each edge.
220 //=======================================================================
221
222 void ChFi3d_FilBuilder::PerformThreeCorner(const Standard_Integer Jndex)
223 {
224   
225 #ifdef DEB 
226   OSD_Chronometer ch;
227   ChFi3d_InitChron(ch); // init perf initialisation 
228 #endif 
229   
230   TopOpeBRepDS_DataStructure& DStr = myDS->ChangeDS();
231   const TopoDS_Vertex& Vtx = myVDataMap.FindKey(Jndex);
232   ChFiDS_ListIteratorOfListOfStripe It;
233   Standard_Integer Index[3],pivot,deb,fin,ii,jj,kk;
234   //Standard_Real R = 0.;
235   Standard_Boolean pivdif = Standard_True;
236   Standard_Boolean c1pointu = Standard_False;
237   Standard_Boolean c1toric = Standard_False;
238   Standard_Boolean c1spheric = Standard_False;
239   Handle(ChFiDS_Stripe) CD[3];
240   TopoDS_Face face[3];
241   Standard_Integer jf[3][3];
242   Standard_Boolean sameside[3], oksea[3];
243   for(Standard_Integer g = 0; g <= 2; g++){oksea[g] = Standard_False;}
244   Standard_Integer i[3][3];
245   Standard_Integer sens[3];
246   Standard_Real p[3][3];
247   
248   Standard_Boolean filling = 0;
249   
250   for (It.Initialize(myVDataMap(Jndex)),ii=0;It.More() && ii<3;It.Next(),ii++){
251     Index[ii] = ChFi3d_IndexOfSurfData(Vtx,It.Value(),sens[ii]);
252     CD[ii] = It.Value();
253   }
254   // It is checked if one of CD is not present twice in which  
255   // case it is necessary to modify the return of IndexOfSurfData  
256   // that takes the first solution.
257   if(CD[0] == CD[1]){ 
258     Index[1] = CD[1]->SetOfSurfData()->Length();
259     sens[1] = -1;
260   }
261   else if(CD[1] == CD[2]){ 
262     Index[2] = CD[2]->SetOfSurfData()->Length();
263     sens[2] = -1;
264   }
265   else if(CD[0] == CD[2]){ 
266     Index[2] = CD[2]->SetOfSurfData()->Length();
267     sens[2] = -1;
268   }
269   oksea[2] = SearchFD(DStr,CD[0],CD[1],sens[0],sens[1],i[0][1],i[1][0],
270                       p[0][1],p[1][0],Index[0],Index[1],face[2],sameside[2],
271                       jf[0][1],jf[1][0]);
272   oksea[1] = SearchFD(DStr,CD[0],CD[2],sens[0],sens[2],i[0][2],i[2][0],
273                       p[0][2],p[2][0],Index[0],Index[2],face[1],sameside[1],
274                       jf[0][2],jf[2][0]);
275   oksea[0] = SearchFD(DStr,CD[1],CD[2],sens[1],sens[2],i[1][2],i[2][1],
276                       p[1][2],p[2][1],Index[1],Index[2],face[0],sameside[0],
277                       jf[1][2],jf[2][1]);
278   //
279   // Analyze concavities of 3 fillets :
280   //        - 2 concavities identic and 1 inverted.
281   //        - 3 concavities identic
282   //
283   if(oksea[2] && oksea[1] && !sameside[2] && !sameside[1])
284     { pivot = 0; deb = 1; fin = 2;}
285   else if(oksea[2] && oksea[0] && !sameside[2] && !sameside[0])
286     { pivot = 1; deb = 2; fin = 0;}
287   else if(oksea[1] && oksea[0] && !sameside[1] && !sameside[0])
288     { pivot = 2; deb = 0; fin = 1;}
289   else if(oksea[0] && oksea[1] && oksea[2]){ 
290     // 3 concavities identic.
291     pivot = SearchPivot(sens,p,tol2d);
292     if(pivot < 0){ 
293 #ifdef CHFI3D_DEB
294       cout<<"pivot not found, plate is called"<<endl;
295 #endif
296       PerformMoreThreeCorner(Jndex, 3);
297       return;
298     }
299     else{deb = (pivot+1)%3 ; fin = (pivot+2)%3;}
300     pivdif = Standard_False;
301     if(Abs(p[0][1]-p[0][2]) <= tol2d &&
302        Abs(p[1][0]-p[1][2]) <= tol2d &&
303        Abs(p[2][0]-p[2][1]) <= tol2d){
304       c1pointu = Standard_True;
305     }
306   }
307   else {
308     PerformMoreThreeCorner(Jndex, 3);
309     return;
310   }
311   Standard_Integer ifacdeb, ifacfin;
312   ifacdeb = CD[deb]->ChangeSetOfSurfData()->Value(i[deb][pivot])->Index(3-jf[deb][pivot]);
313   ifacfin = CD[fin]->ChangeSetOfSurfData()->Value(i[fin][pivot])->Index(3-jf[fin][pivot]);
314   if(ifacfin != ifacdeb){
315 #ifdef CHFI3D_DEB
316     cout<<"several base faces, plate is called"<<endl;
317 #endif
318     PerformMoreThreeCorner(Jndex, 3);
319     return;
320   }
321   if(i[pivot][deb] != i[pivot][fin]){
322 #ifdef CHFI3D_DEB
323     cout<<"load surfdata on the pivot, plate is called"<<endl;
324 #endif
325     PerformMoreThreeCorner(Jndex, 3);
326     return;
327   }
328   
329   Standard_Real Rdeb,Rfin,Rdp,Rfp;
330   gp_Pnt Pdeb,Pfin,Pdp,Pfp;
331   gp_Vec Vdeb,Vfin,Vdp,Vfp;
332   if(c1pointu){
333     gp_Pnt pbid;
334     gp_Vec qv[3];
335     Standard_Real qr[3];
336     for(ii = 0; ii<=2; ii++){
337       jj = (ii+1)%3 ; kk = (ii+2)%3;
338       ChFi3d_ExtrSpineCarac(DStr,CD[jj],i[jj][ii],p[jj][ii],1,
339                             sens[jj],pbid,qv[jj],qr[jj]);
340     }
341     for(ii = 0; ii<=2 && !c1toric; ii++){
342       jj = (ii+1)%3 ; kk = (ii+2)%3;
343       if(ToricCorner(face[ii],qr[jj],qr[kk],qv[ii])){
344         c1toric = Standard_True;
345         pivot = ii; deb = jj; fin = kk;
346       }
347     }
348     if(!c1toric)c1spheric=(Abs(qr[0]-qr[1])<tolesp && Abs(qr[0]-qr[2])<tolesp);
349   }
350   
351   //  Previously to avoid loops the points were always located 
352   //  inside, which could impede the construction of the
353   //  guideline of the corner which now is a circle.
354   //  Standard_Integer jjjd = jf[deb][fin], jjjf = jf[fin][deb];
355   //  if (pivdif) jjjd = jf[deb][pivot], jjjf = jf[fin][pivot];
356   Standard_Integer jjjd = jf[deb][pivot], jjjf = jf[fin][pivot];
357   ChFi3d_ExtrSpineCarac(DStr,CD[deb],i[deb][pivot],p[deb][pivot],
358                         jjjd,sens[deb],Pdeb,Vdeb,Rdeb);
359   ChFi3d_ExtrSpineCarac(DStr,CD[fin],i[fin][pivot],p[fin][pivot],
360                         jjjf,sens[fin],Pfin,Vfin,Rfin);
361   ChFi3d_ExtrSpineCarac(DStr,CD[pivot],i[pivot][deb],p[pivot][deb],
362                         0,sens[pivot],Pdp,Vdp,Rdp);
363   ChFi3d_ExtrSpineCarac(DStr,CD[pivot],i[pivot][fin],p[pivot][fin],
364                         0,sens[pivot],Pfp,Vfp,Rfp);
365   //in cas of allsame it is checked that points on the face are not 
366   //too close, which can stop the work.
367   if(!pivdif) {
368     gp_Pnt ptestdeb,ptestfin; gp_Vec bidvec; Standard_Real bidr;
369     ChFi3d_ExtrSpineCarac(DStr,CD[deb],i[deb][pivot],p[deb][pivot],
370                           jf[deb][fin],sens[deb],ptestdeb,bidvec,bidr);
371     ChFi3d_ExtrSpineCarac(DStr,CD[fin],i[fin][pivot],p[fin][pivot],
372                           jf[fin][deb],sens[fin],ptestfin,bidvec,bidr);
373     Standard_Real distest = ptestdeb.Distance(ptestfin);
374     if(distest < (Rdp+Rfp)*0.05) filling = 1;
375     if(distest < (Rdp+Rfp)*0.005) c1pointu = 1;
376   }
377   
378   if(!c1pointu){
379     if (!pivdif) c1pointu = (Abs(p[deb][pivot]-p[deb][fin]) <=tol2d &&
380                              Abs(p[fin][pivot]-p[fin][deb]) <=tol2d);
381     if(Abs(p[pivot][deb] - p[pivot][fin]) <= tol2d)
382       c1toric = ToricCorner(face[pivot],Rdeb,Rfin,Vdp);
383   }
384   // there is a pivot, the start and the end CD (finally !?!) :
385   // -------------------------------------------------------------
386   // the criterions determining if the corner is a torus or a sphere
387   // are based only on the configuration of sections at end and the 
388   // nature of faces, it is necessary to make tests to 
389   // determine if a more detailed analysis of incident fillets
390   // is necessare to provide tangency between them and 
391   // the corner (in particular in the case with variable radius).
392   
393   
394   
395   Handle(ChFiDS_SurfData)& 
396     fddeb = CD[deb]->ChangeSetOfSurfData()->ChangeValue(i[deb][pivot]);
397   Handle(ChFiDS_SurfData)& 
398     fdfin = CD[fin]->ChangeSetOfSurfData()->ChangeValue(i[fin][pivot]);
399   Handle(ChFiDS_SurfData)& 
400     fdpiv = CD[pivot]->ChangeSetOfSurfData()->ChangeValue(i[pivot][deb]);
401   
402   
403   // HSurfaces and other suitable tools are constructed.
404   // ----------------------------------------------------------
405   
406   TopAbs_Orientation OFac = face[pivot].Orientation();
407   Handle(BRepAdaptor_HSurface) Fac = new BRepAdaptor_HSurface(face[pivot]);
408   gp_Pnt2d ppp1,ppp2;
409   const ChFiDS_FaceInterference& bid1 = CD[pivot]->SetOfSurfData()->
410     Value(i[pivot][deb])->InterferenceOnS1();
411   ppp1 = bid1.PCurveOnSurf()->Value(bid1.FirstParameter());
412   const ChFiDS_FaceInterference& bid2 = CD[pivot]->SetOfSurfData()->
413     Value(i[pivot][deb])->InterferenceOnS2();
414   ppp2 = bid2.PCurveOnSurf()->Value(bid2.LastParameter());
415   Standard_Real uu1 = ppp1.X(), uu2 = ppp2.X(), vv1 =  ppp1.Y(), vv2 =  ppp2.Y(); 
416   GeomAdaptor_Surface 
417     gasurf((DStr.Surface(CD[pivot]->SetOfSurfData()->
418                          Value(i[pivot][deb])->Surf())).Surface(),
419            uu1, uu2, vv1, vv2);
420   GeomAbs_SurfaceType styp = gasurf.GetType();
421   if(styp == GeomAbs_Cylinder){
422     Standard_Real h = vv2 - vv1;
423     vv1 -= 0.5*h;
424     vv2 += 0.5*h;
425     gasurf.Load((DStr.Surface(CD[pivot]->SetOfSurfData()->
426                               Value(i[pivot][deb])->Surf())).Surface(),
427                 uu1, uu2, vv1, vv2);
428   }
429   else if(styp == GeomAbs_Torus){
430     Standard_Real h = uu2 - uu1;
431     uu1 -= 0.1*h;
432     uu2 += 0.1*h;
433     gasurf.Load((DStr.Surface(CD[pivot]->SetOfSurfData()->
434                               Value(i[pivot][deb])->Surf())).Surface(),
435                 uu1, uu2, vv1, vv2);
436   }
437   else if(styp == GeomAbs_BezierSurface || styp == GeomAbs_BSplineSurface){
438     gasurf.Load((DStr.Surface(CD[pivot]->SetOfSurfData()->
439                               Value(i[pivot][deb])->Surf())).Surface());
440   }
441   
442   Handle(GeomAdaptor_HSurface) Surf = new GeomAdaptor_HSurface(gasurf);
443   //  Handle(BRepTopAdaptor_TopolTool) IFac = new BRepTopAdaptor_TopolTool(Fac);
444   // Try to not classify on the face for cases of reentering fillets which naturally depass 
445   // the border.  
446   Handle(GeomAdaptor_HSurface) 
447     bidsurf = new GeomAdaptor_HSurface(Fac->ChangeSurface().Surface());
448   Handle(Adaptor3d_TopolTool) 
449     IFac = new Adaptor3d_TopolTool(bidsurf);
450   // end of the attempt.
451   Handle(Adaptor3d_TopolTool) ISurf = new Adaptor3d_TopolTool(Surf);
452   Handle(ChFiDS_Stripe) corner = new ChFiDS_Stripe();
453   Handle(ChFiDS_HData)& cornerset = corner->ChangeSetOfSurfData();
454   cornerset = new ChFiDS_HData();
455   Handle(ChFiDS_SurfData) coin = new ChFiDS_SurfData();
456   cornerset->Append(coin);
457   TopAbs_Orientation o1,o2,os1,os2,oo1,oo2;
458   Standard_Integer choix = CD[deb]->Choix();
459   o1 = face[pivot].Orientation();
460   o2 = fdpiv->Orientation();
461   oo1 = o1; oo2 = o2;
462   os1 = CD[deb]->OrientationOnFace1();
463   os2 = CD[deb]->OrientationOnFace2();
464   if(jf[deb][fin] == 1){
465     choix = ChFi3d::NextSide(o1,o2,os1,os2,choix);
466     if(sens[deb] == 1){
467       if(choix%2 == 1) choix++;
468       else choix--;
469     }
470   }
471   else {
472     choix = ChFi3d::NextSide(o2,o1,os1,os2,-choix);
473     if(sens[deb] == -1){
474       if(choix%2 == 1) choix++;
475       else choix--;
476     }
477   }
478   
479   gp_Pnt2d pfac1,pfac2,psurf1,psurf2;
480   gp_Vec2d vfac1,vfac2;
481   CD[deb]->SetOfSurfData()->Value(i[deb][pivot])->
482     Interference(jf[deb][fin]).PCurveOnFace()->D1(p[deb][pivot],pfac1,vfac1);
483   CD[fin]->SetOfSurfData()->Value(i[fin][pivot])->
484     Interference(jf[fin][deb]).PCurveOnFace()->D1(p[fin][pivot],pfac2,vfac2);
485   psurf1 = CD[pivot]->SetOfSurfData()->Value(i[pivot][deb])->
486     Interference(jf[pivot][deb]).PCurveOnSurf()->Value(p[pivot][deb]);
487   psurf2 = CD[pivot]->SetOfSurfData()->Value(i[pivot][fin])->
488     Interference(jf[pivot][fin]).PCurveOnSurf()->Value(p[pivot][fin]);
489   
490   done = Standard_False;
491 #ifdef DEB
492   if(ChFi3d_GetcontextFORCEFILLING()) c1spheric = c1toric = 0;
493 #endif
494   
495 #ifdef DEB 
496   ChFi3d_ResultChron(ch , t_t3cornerinit); // result perf initialisations 
497 #endif 
498   
499   if (c1toric){
500     
501 #ifdef DEB 
502     ChFi3d_InitChron(ch); // init perf case torus 
503 #endif 
504     
505     // Direct Construction.
506     // ---------------------
507     done = ChFiKPart_ComputeData::ComputeCorner
508       (DStr,coin,Fac,Surf,oo1,oo2,o1,o2,Rdeb,Rdp,pfac1,pfac2,psurf1,psurf2);
509     
510 #ifdef DEB 
511     ChFi3d_ResultChron(ch , t_torique); // result perf case torus 
512 #endif 
513     
514   }
515   else if(c1spheric){
516     
517 #ifdef DEB   
518     ChFi3d_InitChron(ch); //init perf case sphere 
519 #endif 
520     
521     done = ChFiKPart_ComputeData::ComputeCorner
522       (DStr,coin,Fac,Surf,oo1,oo2,o1,o2,Rdp,pfac1,psurf1,psurf2);
523     
524 #ifdef DEB  
525     ChFi3d_ResultChron(ch , t_spherique);// result perf cas sphere 
526 #endif 
527     
528   }
529   else if(c1pointu){
530     filling = 1;
531   }
532   if(!done){
533     if(!filling) {
534       
535 #ifdef DEB   
536       ChFi3d_InitChron(ch);// init perf not filling 
537 #endif
538       
539       //Calculate a guideline,
540       //------------------------------
541       //Numerous problems with loops and half-turns connected to 
542       //the curvature of the guideline !!!!!! 
543       //FOR CIRCLE.
544       //If the nature of guideline is changed it is necessary to 
545       //reset points Pdeb and Pfin at the inside (see the
546       //comments about it in the calculation of Pdeb and Pfin).
547       
548       Standard_Real radpondere = (Rdp+Rfp)/2.;
549       Standard_Real locfleche = fleche;
550       
551       Standard_Real WFirst,WLast;
552       Handle(Geom_Curve) spinecoin = ChFi3d_CircularSpine(WFirst,WLast,
553                                                           Pdeb,Vdeb,
554                                                           Pfin,Vfin,radpondere);
555       if(spinecoin.IsNull()){
556         // This is a bad case when the intersection of 
557         // section planes is done out of the sector.
558         spinecoin = ChFi3d_Spine(Pdeb,Vdeb,
559                                  Pfin,Vfin,radpondere);
560         WFirst = 0.; WLast = 1.;
561       }
562       else locfleche = radpondere * (WLast - WFirst) * fleche;
563       Standard_Real pasmax = (WLast-WFirst)*0.05;
564       Handle(ChFiDS_HElSpine) cornerspine = new ChFiDS_HElSpine();
565       cornerspine->ChangeCurve().SetCurve(spinecoin);
566       cornerspine->ChangeCurve().FirstParameter(WFirst - pasmax);
567       cornerspine->ChangeCurve().LastParameter(WLast + pasmax);
568       // Just to confuse Compute that should not require this 
569       // in this exact case ...
570       Handle(ChFiDS_Spine) NullSpine;
571       // The fillet is calculated - from beginning to end 
572       //                       - from the face to the surface
573       //
574       math_Vector Soldep(1,4);
575       Soldep(1) = pfac1.X();
576       Soldep(2) = pfac1.Y();
577       Soldep(3) = psurf1.X();
578       Soldep(4) = psurf1.Y();
579       
580       Standard_Boolean Gd1,Gd2,Gf1,Gf2;
581       Handle(BRepBlend_Line) lin;
582       Standard_Real ffi = WFirst, lla = WLast + pasmax;
583       
584       if (Abs(Rdeb-Rfin)<=tolesp){
585         
586         BRepBlend_ConstRad func(Fac,Surf,cornerspine);
587         BRepBlend_ConstRadInv finv(Fac,Surf,cornerspine);
588         func.Set(Rdeb,choix);
589         func.Set(myShape);
590         finv.Set(Rdeb,choix);
591         Standard_Real TolGuide = cornerspine->Resolution(tolesp); 
592         
593         Standard_Boolean intf = 3, intl = 3;
594         done = ComputeData(coin,cornerspine,NullSpine,lin,Fac,IFac,Surf,ISurf,
595                            func,finv,ffi,pasmax,locfleche,TolGuide,ffi,lla,
596                            0,0,1,Soldep,intf,intl,Gd1,Gd2,Gf1,Gf2,0,1);
597 #ifdef DEB
598         if(ChFi3d_GetcontextFORCEFILLING()) done = 0;
599 #endif
600         if(done && Gf2){
601           done = CompleteData(coin,func,lin,Fac,Surf,OFac,Gd1,0,Gf1,0);
602           filling = !done;
603         }
604         else filling = 1;
605       }
606       else{
607         Handle(Law_S) law = new Law_S();
608         law->Set(WFirst,Rdeb,WLast,Rfin);
609         BRepBlend_EvolRad func(Fac,Surf,cornerspine,law);
610         BRepBlend_EvolRadInv finv(Fac,Surf,cornerspine,law);
611         func.Set(choix);
612         func.Set(myShape);
613         finv.Set(choix);
614         Standard_Real TolGuide = cornerspine->Resolution(tolesp);
615         Standard_Boolean intf = 3, intl = 3;
616         done = ComputeData(coin,cornerspine,NullSpine,lin,Fac,IFac,Surf,ISurf,
617                            func,finv,ffi,pasmax,locfleche,TolGuide,ffi,lla,
618                            0,0,1,Soldep,intf,intl,Gd1,Gd2,Gf1,Gf2,0,1);
619 #ifdef DEB
620         if(ChFi3d_GetcontextFORCEFILLING()) done = 0;
621 #endif
622         if(done && Gf2){
623           done = CompleteData(coin,func,lin,Fac,Surf,OFac,Gd1,0,Gf1,0);
624           filling = !done;
625         }
626         else filling = 1;
627       }
628       
629 #ifdef DEB  
630       ChFi3d_ResultChron(ch , t_notfilling);// result perf not filling 
631 #endif
632       
633     }
634     
635     if(filling) {
636       
637 #ifdef DEB    
638       ChFi3d_InitChron(ch); // init perf filling
639 #endif
640       
641       // the contour to be fillet consists of straight lines uv in beginning and end 
642       // of two pcurves (only one if c1pointu) calculted as possible
643       // on piv and the opposite face.
644       Handle(GeomFill_Boundary) Bdeb,Bfin,Bpiv,Bfac;
645       Handle(Geom2d_Curve) PCurveOnFace;
646       if(!c1pointu) 
647         Bfac = ChFi3d_mkbound(Fac,PCurveOnFace,sens[deb],pfac1,vfac1,
648                               sens[fin],pfac2,vfac2,tolesp,2.e-4);
649       Standard_Integer kkk;
650       gp_Pnt ppbid;
651       gp_Vec vp1,vp2;
652       kkk = CD[deb]->SetOfSurfData()->Value(i[deb][pivot])->
653         Interference(jf[deb][pivot]).LineIndex();
654       DStr.Curve(kkk).Curve()->D1(p[deb][pivot],ppbid,vp1);
655       kkk = CD[fin]->SetOfSurfData()->Value(i[fin][pivot])->
656         Interference(jf[fin][pivot]).LineIndex();
657       DStr.Curve(kkk).Curve()->D1(p[fin][pivot],ppbid,vp2);
658       Handle(Geom2d_Curve) PCurveOnPiv;
659 //      Bpiv = ChFi3d_mkbound(Surf,PCurveOnPiv,sens[deb],psurf1,vp1,
660 //                          sens[fin],psurf2,vp2,tolesp,2.e-4);
661       Bpiv = ChFi3d_mkbound(Surf,PCurveOnPiv,psurf1,psurf2,tolesp,2.e-4,0);
662       Standard_Real pardeb2 = p[deb][pivot];
663       Standard_Real parfin2 = p[fin][pivot];
664       if(c1pointu){
665         pardeb2 = p[deb][fin];
666         parfin2 = p[fin][deb];
667       }
668       gp_Pnt2d pdeb1 = CD[deb]->SetOfSurfData()->Value(i[deb][pivot])->
669         Interference(jf[deb][pivot]).PCurveOnSurf()->Value(p[deb][pivot]);
670       gp_Pnt2d pdeb2 = CD[deb]->SetOfSurfData()->Value(i[deb][pivot])->
671         Interference(jf[deb][fin]).PCurveOnSurf()->Value(pardeb2);
672       gp_Pnt2d pfin1 = CD[fin]->SetOfSurfData()->Value(i[fin][pivot])->
673         Interference(jf[fin][pivot]).PCurveOnSurf()->Value(p[fin][pivot]);
674       gp_Pnt2d pfin2 = CD[fin]->SetOfSurfData()->Value(i[fin][pivot])->
675         Interference(jf[fin][deb]).PCurveOnSurf()->Value(parfin2);
676       Handle(Geom_Surface) sdeb = 
677         DStr.Surface(CD[deb]->SetOfSurfData()->
678                      Value(i[deb][pivot])->Surf()).Surface();
679       Handle(Geom_Surface) sfin = 
680         DStr.Surface(CD[fin]->SetOfSurfData()->
681                      Value(i[fin][pivot])->Surf()).Surface();
682       
683       Bdeb = ChFi3d_mkbound(sdeb,pdeb1,pdeb2,tolesp,2.e-4);
684       Bfin = ChFi3d_mkbound(sfin,pfin1,pfin2,tolesp,2.e-4);
685       
686       GeomFill_ConstrainedFilling fil(11,20);
687       if(c1pointu) fil.Init(Bpiv,Bfin,Bdeb,1);
688       else fil.Init(Bpiv,Bfin,Bfac,Bdeb,1);
689       
690       Handle(Geom_Surface) Surfcoin = fil.Surface();
691       Surfcoin->VReverse(); // revert to direction face surface;
692       done = CompleteData(coin,Surfcoin,
693                           Fac,PCurveOnFace,
694                           Surf,PCurveOnPiv,fdpiv->Orientation(),0,
695                           0,0,0,0);
696       
697 #ifdef DEB 
698       ChFi3d_ResultChron(ch , t_filling);// result perf filling 
699 #endif 
700       
701     }
702   }
703   Standard_Real P1deb,P2deb,P1fin,P2fin;
704   if(!c1pointu){
705     p[deb][fin] = p[deb][pivot];
706     p[fin][deb] = p[fin][pivot];
707   }
708   
709   if (done){    
710     // Update of 4 Stripes and the DS
711     // -------------------------------------
712     
713 #ifdef DEB  
714     ChFi3d_InitChron(ch);// init perf update DS
715 #endif 
716     
717     gp_Pnt2d pp1,pp2;
718     Standard_Real parpp1,parpp2;
719     Standard_Integer If1,If2,Il1,Il2,Icf,Icl;
720     const ChFiDS_CommonPoint& Pf1 = coin->VertexFirstOnS1();
721     const ChFiDS_CommonPoint& Pf2 = coin->VertexFirstOnS2();
722     ChFiDS_CommonPoint& Pl1 = coin->ChangeVertexLastOnS1();
723     if(c1pointu) Pl1 = coin->ChangeVertexFirstOnS1();
724     const ChFiDS_CommonPoint& Pl2 = coin->VertexLastOnS2();
725     
726     Bnd_Box bf1,bl1,bf2,bl2;
727     Bnd_Box *pbf1 = &bf1,*pbl1 = &bl1,*pbf2 = &bf2,*pbl2 = &bl2;
728     if(c1pointu) pbl1 = pbf1;
729     pbf1->Add(Pf1.Point());pbf2->Add(Pf2.Point());
730     pbl1->Add(Pl1.Point());pbl2->Add(Pl2.Point());
731     
732     // the start corner,
733     // -----------------------
734     ChFiDS_Regul regdeb, regfin;
735     If1 = ChFi3d_IndexPointInDS(Pf1,DStr);
736     If2 = ChFi3d_IndexPointInDS(Pf2,DStr);
737     if(c1pointu) Il1 = If1;
738     else Il1 = ChFi3d_IndexPointInDS(Pl1,DStr);
739     Il2 = ChFi3d_IndexPointInDS(Pl2,DStr);
740     pp1 = coin->InterferenceOnS1().PCurveOnSurf()->
741       Value(coin->InterferenceOnS1().FirstParameter());
742     pp2 = coin->InterferenceOnS2().PCurveOnSurf()->
743       Value(coin->InterferenceOnS2().FirstParameter());
744     if(c1pointu) coin->ChangeIndexOfS1(0);
745     else coin->ChangeIndexOfS1(DStr.AddShape(face[pivot]));
746     coin->ChangeIndexOfS2(-fdpiv->Surf());
747     
748     Handle(Geom_Curve) C3d;
749     Standard_Real tolreached;
750     ChFi3d_ComputeArete(Pf1,pp1,Pf2,pp2,
751                         DStr.Surface(coin->Surf()).Surface(),C3d,
752                         corner->ChangeFirstPCurve(),P1deb,P2deb,
753                         tolesp,tol2d,tolreached,0);
754     TopOpeBRepDS_Curve Tcurv1(C3d,tolreached);
755     Icf = DStr.AddCurve(Tcurv1);
756     regdeb.SetCurve(Icf);
757     regdeb.SetS1(coin->Surf(),0);
758     regdeb.SetS2(fddeb->Surf(),0);
759     myRegul.Append(regdeb);
760     corner->ChangeFirstCurve(Icf);
761     corner->ChangeFirstParameters(P1deb,P2deb);
762     corner->ChangeIndexFirstPointOnS1(If1);
763     corner->ChangeIndexFirstPointOnS2(If2);
764     ChFi3d_EnlargeBox(DStr,corner,coin,*pbf1,*pbf2,1);
765     
766     pp1 = coin->InterferenceOnS1().PCurveOnSurf()->
767       Value(coin->InterferenceOnS1().LastParameter());
768     pp2 = coin->InterferenceOnS2().PCurveOnSurf()->
769       Value(coin->InterferenceOnS2().LastParameter());
770     ChFi3d_ComputeArete(Pl1,pp1,Pl2,pp2,
771                         DStr.Surface(coin->Surf()).Surface(),C3d,
772                         corner->ChangeLastPCurve(),P1fin,P2fin,
773                         tolesp,tol2d,tolreached,0);
774     TopOpeBRepDS_Curve Tcurv2(C3d,tolreached);
775     Icl = DStr.AddCurve(Tcurv2);
776     regfin.SetCurve(Icl);
777     regfin.SetS1(coin->Surf(),0);
778     regfin.SetS2(fdfin->Surf(),0);
779     myRegul.Append(regfin);
780     corner->ChangeLastCurve(Icl);
781     corner->ChangeLastParameters(P1fin,P2fin);
782     corner->ChangeIndexLastPointOnS1(Il1);
783     corner->ChangeIndexLastPointOnS2(Il2);
784     ChFi3d_EnlargeBox(DStr,corner,coin,*pbl1,*pbl2,0);
785     
786     // then CornerData of the beginning,
787     // --------------------------------
788     Standard_Boolean isfirst = (sens[deb] == 1), rev = (jf[deb][fin] == 2);
789     Standard_Integer isurf1 = 1, isurf2 = 2;
790     parpp1 = p[deb][fin]; parpp2 = p[deb][pivot]; 
791     if (rev) { 
792       isurf1 = 2; isurf2 = 1; 
793       parpp1 = p[deb][pivot]; parpp2 = p[deb][fin]; 
794       CD[deb]->SetOrientation(TopAbs_REVERSED,isfirst);
795     }
796     pp1 = fddeb->InterferenceOnS1().PCurveOnSurf()->Value(parpp1);
797     pp2 = fddeb->InterferenceOnS2().PCurveOnSurf()->Value(parpp2);
798     CD[deb]->SetCurve(Icf,isfirst);
799     CD[deb]->SetIndexPoint(If1,isfirst,isurf1);
800     CD[deb]->SetIndexPoint(If2,isfirst,isurf2);
801     CD[deb]->SetParameters(isfirst,P1deb,P2deb);
802     fddeb->ChangeVertex(isfirst,isurf1) = Pf1;
803     fddeb->ChangeVertex(isfirst,isurf2) = Pf2;
804     fddeb->ChangeInterferenceOnS1().SetParameter(parpp1,isfirst);
805     fddeb->ChangeInterferenceOnS2().SetParameter(parpp2,isfirst);
806     TopOpeBRepDS_Curve& tcdeb = DStr.ChangeCurve(Icf);
807     Handle(Geom_Curve) crefdeb = tcdeb.Curve();
808     Standard_Real tolrdeb;
809     ChFi3d_ComputePCurv(crefdeb,pp1,pp2,CD[deb]->ChangePCurve(isfirst),
810                         DStr.Surface(fddeb->Surf()).Surface(),
811                         P1deb,P2deb,tolesp,tolrdeb,rev);
812     tcdeb.Tolerance(Max(tolrdeb,tcdeb.Tolerance()));
813     if(rev) ChFi3d_EnlargeBox(DStr,CD[deb],fddeb,*pbf2,*pbf1,isfirst);
814     else ChFi3d_EnlargeBox(DStr,CD[deb],fddeb,*pbf1,*pbf2,isfirst);
815     
816     // then the end CornerData,
817     // ------------------------
818     isfirst = (sens[fin] == 1); rev = (jf[fin][deb] == 2);
819     isurf1 = 1; isurf2 = 2;
820     parpp1 = p[fin][deb]; parpp2 = p[fin][pivot]; 
821     if (rev) { 
822       isurf1 = 2; isurf2 = 1; 
823       parpp1 = p[fin][pivot]; parpp2 = p[fin][deb]; 
824       CD[fin]->SetOrientation(TopAbs_REVERSED,isfirst);
825     }
826     pp1 = fdfin->InterferenceOnS1().PCurveOnSurf()->Value(parpp1);
827     pp2 = fdfin->InterferenceOnS2().PCurveOnSurf()->Value(parpp2);
828     CD[fin]->SetCurve(Icl,isfirst);
829     CD[fin]->SetIndexPoint(Il1,isfirst,isurf1);
830     CD[fin]->SetIndexPoint(Il2,isfirst,isurf2);
831     CD[fin]->SetParameters(isfirst,P1fin,P2fin);
832     fdfin->ChangeVertex(isfirst,isurf1) = Pl1;
833     fdfin->ChangeVertex(isfirst,isurf2) = Pl2;
834     fdfin->ChangeInterferenceOnS1().SetParameter(parpp1,isfirst);
835     fdfin->ChangeInterferenceOnS2().SetParameter(parpp2,isfirst);
836     TopOpeBRepDS_Curve& tcfin = DStr.ChangeCurve(Icl);
837     Handle(Geom_Curve) creffin = tcfin.Curve();
838     Standard_Real tolrfin;
839     ChFi3d_ComputePCurv(creffin,pp1,pp2,CD[fin]->ChangePCurve(isfirst),
840                         DStr.Surface(fdfin->Surf()).Surface(),
841                         P1fin,P2fin,tolesp,tolrfin,rev);
842     tcfin.Tolerance(Max(tolrfin,tcfin.Tolerance()));
843     if(rev) ChFi3d_EnlargeBox(DStr,CD[fin],fdfin,*pbl2,*pbl1,isfirst);
844     else ChFi3d_EnlargeBox(DStr,CD[fin],fdfin,*pbl1,*pbl2,isfirst);
845     
846     // anf finally the pivot.
847     // ------------------
848     ChFiDS_FaceInterference& fi = coin->ChangeInterferenceOnS2();
849     isfirst = (sens[pivot] == 1); rev = (jf[pivot][deb] == 2);
850     isurf1 = 1; isurf2 = 2;
851     if (rev) { 
852       isurf1 = 2; isurf2 = 1; 
853       CD[pivot]->SetOrientation(TopAbs_REVERSED,isfirst);
854     }
855     Standard_Integer ICcoinpiv = fi.LineIndex();
856     TopOpeBRepDS_Curve& TCcoinpiv = DStr.ChangeCurve(ICcoinpiv);
857     CD[pivot]->SetCurve(ICcoinpiv,isfirst);
858     Handle(Geom_Curve) Ccoinpiv = DStr.Curve(ICcoinpiv).Curve();
859     Handle(Geom2d_Curve)& C2dOnPiv = fi.ChangePCurveOnFace();
860     Handle(Geom_Surface) Spiv = DStr.Surface(fdpiv->Surf()).Surface();
861     Standard_Real tolr;
862     ChFi3d_SameParameter(Ccoinpiv,C2dOnPiv,Spiv,
863                          fi.FirstParameter(),fi.LastParameter(),
864                          tolesp,tolr);
865     TCcoinpiv.Tolerance(Max(TCcoinpiv.Tolerance(),tolr));
866     CD[pivot]->ChangePCurve(isfirst) = C2dOnPiv;
867     CD[pivot]->SetIndexPoint(If2,isfirst,isurf1);
868     CD[pivot]->SetIndexPoint(Il2,isfirst,isurf2);
869     CD[pivot]->SetParameters(isfirst,fi.FirstParameter(),fi.LastParameter());
870     fdpiv->ChangeVertex(isfirst,isurf1) = Pf2;
871     fdpiv->ChangeVertex(isfirst,isurf2) = Pl2;
872     fdpiv->ChangeInterference(isurf1).SetParameter(p[pivot][deb],isfirst);
873     fdpiv->ChangeInterference(isurf2).SetParameter(p[pivot][fin],isfirst);
874     CD[pivot]->InDS(isfirst); // filDS already does it from the corner.
875     if(rev) ChFi3d_EnlargeBox(DStr,CD[pivot],fdpiv,*pbl2,*pbf2,isfirst);
876     else ChFi3d_EnlargeBox(DStr,CD[pivot],fdpiv,*pbf2,*pbl2,isfirst);
877     
878     //To end the tolerances of points are rescaled.
879     ChFi3d_SetPointTolerance(DStr,*pbf1,If1);
880     ChFi3d_SetPointTolerance(DStr,*pbf2,If2);
881     ChFi3d_SetPointTolerance(DStr,*pbl1,Il1);
882     ChFi3d_SetPointTolerance(DStr,*pbl2,Il2);
883   }
884   
885   //The data corners are truncated and index is updated.
886   //----------------------------------------------------
887   
888   if(i[deb][pivot] < Index[deb]){
889     CD[deb]->ChangeSetOfSurfData()->Remove(i[deb][pivot]+1,Index[deb]);
890     Index[deb] = i[deb][pivot];
891   }
892   else if(i[deb][pivot] > Index[deb]) {
893     CD[deb]->ChangeSetOfSurfData()->Remove(Index[deb],i[deb][pivot]-1);
894     i[deb][pivot] = Index[deb]; 
895   }
896   if(i[fin][pivot] < Index[fin]) {
897     CD[fin]->ChangeSetOfSurfData()->Remove(i[fin][pivot]+1,Index[fin]);
898     Index[fin] = i[fin][pivot];
899   }
900   else if(i[fin][pivot] > Index[fin]) {
901     CD[fin]->ChangeSetOfSurfData()->Remove(Index[fin],i[fin][pivot]-1);
902     i[fin][pivot] = Index[fin]; 
903   }
904   // it is necessary to take into account mutant corners.
905   if(i[pivot][deb] < Index[pivot]) {
906     CD[pivot]->ChangeSetOfSurfData()->Remove(i[pivot][deb]+1,Index[pivot]);
907     Index[pivot] = i[pivot][deb];
908   }
909   else if(i[pivot][deb] > Index[pivot]) {
910     CD[pivot]->ChangeSetOfSurfData()->Remove(Index[pivot],i[pivot][deb]-1);
911     i[pivot][deb] = Index[pivot]; 
912   }
913   if(!myEVIMap.IsBound(Vtx)){
914     TColStd_ListOfInteger li;
915     myEVIMap.Bind(Vtx,li);
916   }
917   myEVIMap.ChangeFind(Vtx).Append(coin->Surf());
918   corner->SetSolidIndex(CD[pivot]->SolidIndex());
919   myListStripe.Append(corner);
920   
921 #ifdef DEB  
922   ChFi3d_ResultChron(ch , t_t3cornerDS);// result perf update DS
923 #endif 
924 }