f65b202f5055fc9283b19aecf9c0857ae9b2307a
[occt.git] / src / IntWalk / IntWalk_PWalking_1.gxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19 //-----------------------------
20 //--  IntWalk_PWalking_1.gxx
21 //-- 
22
23 #include <Precision.hxx>
24 #include <math_FunctionSetRoot.hxx>
25
26 //#define KELARG 20.0
27
28 //==================================================================================
29 // function : IntWalk_PWalking::IntWalk_PWalking
30 // purpose  :
31 // estimation des max pas : Pour eviter des changenets 
32 // trop brusques lors des changements d isos 
33 //==================================================================================
34 void ComputePasInit(Standard_Real *pasuv,
35                     Standard_Real Um1,Standard_Real UM1,
36                     Standard_Real Vm1,Standard_Real VM1,
37                     Standard_Real Um2,Standard_Real UM2,
38                     Standard_Real Vm2,Standard_Real VM2,
39                     Standard_Real _Um1,Standard_Real _UM1,
40                     Standard_Real _Vm1,Standard_Real _VM1,
41                     Standard_Real _Um2,Standard_Real _UM2,
42                     Standard_Real _Vm2,Standard_Real _VM2,
43                     const ThePSurface& ,
44                     const ThePSurface& ,
45                     const Standard_Real Increment) 
46
47   Standard_Real du1=Abs(UM1-Um1);
48   Standard_Real dv1=Abs(VM1-Vm1);
49   Standard_Real du2=Abs(UM2-Um2);
50   Standard_Real dv2=Abs(VM2-Vm2);
51  
52   Standard_Real _du1=Abs(_UM1-_Um1);
53   Standard_Real _dv1=Abs(_VM1-_Vm1);
54   Standard_Real _du2=Abs(_UM2-_Um2);
55   Standard_Real _dv2=Abs(_VM2-_Vm2);
56  
57   //-- On limite la reduction de l estimation de la boite uv a 0.01 la boite naturelle
58   //--  du1 : Sur boite des Inter
59   //-- _du1 : Sur espace parametrique
60   if(_du1<1e50 && du1<0.01*_du1) du1=0.01*_du1;
61   if(_dv1<1e50 && dv1<0.01*_dv1) dv1=0.01*_dv1;
62   if(_du2<1e50 && du2<0.01*_du2) du2=0.01*_du2;
63   if(_dv2<1e50 && dv2<0.01*_dv2) dv2=0.01*_dv2;
64   
65   pasuv[0]=Increment*du1;
66   pasuv[1]=Increment*dv1;
67   pasuv[2]=Increment*du2;
68   pasuv[3]=Increment*dv2;
69 }
70 //==================================================================================
71 // function : IntWalk_PWalking::IntWalk_PWalking
72 // purpose  : 
73 //==================================================================================
74 IntWalk_PWalking::IntWalk_PWalking(const ThePSurface& Caro1,
75                                    const ThePSurface& Caro2,
76                                    const Standard_Real TolTangency,
77                                    const Standard_Real Epsilon,
78                                    const Standard_Real Deflection,
79                                    const Standard_Real Increment ) 
80      :
81        
82        done(Standard_True),
83        close(Standard_False),
84        fleche(Deflection),
85        tolconf(Epsilon),
86        sensCheminement(1),
87        myIntersectionOn2S(Caro1,Caro2,TolTangency),
88        STATIC_BLOCAGE_SUR_PAS_TROP_GRAND(0),
89        STATIC_PRECEDENT_INFLEXION(0)
90 {
91   Standard_Real KELARG=20.;
92   //
93   pasMax=Increment*0.2; //-- le 25 juin 99 suite a des pbs de precision 
94   Um1 = ThePSurfaceTool::FirstUParameter(Caro1);
95   Vm1 = ThePSurfaceTool::FirstVParameter(Caro1);
96   UM1 = ThePSurfaceTool::LastUParameter(Caro1);
97   VM1 = ThePSurfaceTool::LastVParameter(Caro1);
98
99   Um2 = ThePSurfaceTool::FirstUParameter(Caro2);
100   Vm2 = ThePSurfaceTool::FirstVParameter(Caro2);
101   UM2 = ThePSurfaceTool::LastUParameter(Caro2);
102   VM2 = ThePSurfaceTool::LastVParameter(Caro2);
103
104   ResoU1 = ThePSurfaceTool::UResolution(Caro1,Precision::Confusion());
105   ResoV1 = ThePSurfaceTool::VResolution(Caro1,Precision::Confusion());
106
107   ResoU2 = ThePSurfaceTool::UResolution(Caro2,Precision::Confusion());
108   ResoV2 = ThePSurfaceTool::VResolution(Caro2,Precision::Confusion());
109
110   Standard_Real NEWRESO;
111   Standard_Real MAXVAL;
112   Standard_Real MAXVAL2;
113   //
114   MAXVAL  = Abs(Um1);  MAXVAL2 = Abs(UM1);
115   if(MAXVAL2 > MAXVAL) MAXVAL = MAXVAL2;
116   NEWRESO = ResoU1 * MAXVAL ;
117   if(NEWRESO > ResoU1 &&NEWRESO<10) {    ResoU1 = NEWRESO;  }
118
119
120   MAXVAL  = Abs(Um2);   MAXVAL2 = Abs(UM2);
121   if(MAXVAL2 > MAXVAL) MAXVAL = MAXVAL2;
122   NEWRESO = ResoU2 * MAXVAL ;
123   if(NEWRESO > ResoU2 && NEWRESO<10) {     ResoU2 = NEWRESO;  }
124
125
126   MAXVAL  = Abs(Vm1);  MAXVAL2 = Abs(VM1);
127   if(MAXVAL2 > MAXVAL) MAXVAL = MAXVAL2;
128   NEWRESO = ResoV1 * MAXVAL ;
129   if(NEWRESO > ResoV1 && NEWRESO<10) {     ResoV1 = NEWRESO;  }
130
131
132   MAXVAL  = Abs(Vm2);  MAXVAL2 = Abs(VM2);
133   if(MAXVAL2 > MAXVAL) MAXVAL = MAXVAL2;
134   NEWRESO = ResoV2 * MAXVAL ;
135   if(NEWRESO > ResoV2 && NEWRESO<10) {     ResoV2 = NEWRESO;  }
136
137   pasuv[0]=pasMax*Abs(UM1-Um1);
138   pasuv[1]=pasMax*Abs(VM1-Vm1);
139   pasuv[2]=pasMax*Abs(UM2-Um2);
140   pasuv[3]=pasMax*Abs(VM2-Vm2);
141
142   if(ResoU1>0.0001*pasuv[0]) ResoU1=0.00001*pasuv[0];
143   if(ResoV1>0.0001*pasuv[1]) ResoV1=0.00001*pasuv[1];
144   if(ResoU2>0.0001*pasuv[2]) ResoU2=0.00001*pasuv[2];
145   if(ResoV2>0.0001*pasuv[3]) ResoV2=0.00001*pasuv[3];
146
147
148   if(ThePSurfaceTool::IsUPeriodic(Caro1)==Standard_False) { 
149     //UM1+=KELARG*pasuv[0];  Um1-=KELARG*pasuv[0];
150   }
151   else { 
152     Standard_Real t = UM1-Um1; 
153     if(t<ThePSurfaceTool::UPeriod(Caro1)) { 
154       t=0.5*(ThePSurfaceTool::UPeriod(Caro1)-t);
155       t=(t>KELARG*pasuv[0])? KELARG*pasuv[0] : t;
156       UM1+=t;  Um1-=t;
157     }
158   }
159       
160   if(ThePSurfaceTool::IsVPeriodic(Caro1)==Standard_False) { 
161     //VM1+=KELARG*pasuv[1];  Vm1-=KELARG*pasuv[1];
162   }
163   else { 
164     Standard_Real t = VM1-Vm1; 
165     if(t<ThePSurfaceTool::VPeriod(Caro1)) { 
166       t=0.5*(ThePSurfaceTool::VPeriod(Caro1)-t);
167       t=(t>KELARG*pasuv[1])? KELARG*pasuv[1] : t;
168       VM1+=t;  Vm1-=t;
169     }
170   }
171    
172   if(ThePSurfaceTool::IsUPeriodic(Caro2)==Standard_False) { 
173     //UM2+=KELARG*pasuv[2];  Um2-=KELARG*pasuv[2];
174   }
175   else { 
176     Standard_Real t = UM2-Um2; 
177     if(t<ThePSurfaceTool::UPeriod(Caro2)) { 
178       t=0.5*(ThePSurfaceTool::UPeriod(Caro2)-t);
179       t=(t>KELARG*pasuv[2])? KELARG*pasuv[2] : t;
180       UM2+=t;  Um2-=t;
181     }
182   }
183    
184   if(ThePSurfaceTool::IsVPeriodic(Caro2)==Standard_False) {   
185     //VM2+=KELARG*pasuv[3];  Vm2-=KELARG*pasuv[3];
186   }
187   else { 
188     Standard_Real t = VM2-Vm2; 
189     if(t<ThePSurfaceTool::VPeriod(Caro2)) { 
190       t=0.5*(ThePSurfaceTool::VPeriod(Caro2)-t);
191       t=(t>KELARG*pasuv[3])? KELARG*pasuv[3] : t;
192       VM2+=t;  Vm2-=t;
193     }
194   }
195
196   //-- ComputePasInit(pasuv,Um1,UM1,Vm1,VM1,Um2,UM2,Vm2,VM2,Caro1,Caro2);
197
198   for (Standard_Integer i = 0; i<=3;i++) {
199     if(pasuv[i]>10) 
200       pasuv[i] = 10; 
201     pasInit[i] = pasSav[i] = pasuv[i]; 
202   }
203
204
205 }
206 //==================================================================================
207 // function : IntWalk_PWalking
208 // purpose  : 
209 //==================================================================================
210 IntWalk_PWalking::IntWalk_PWalking(const ThePSurface& Caro1,
211                                    const ThePSurface& Caro2,
212                                    const Standard_Real TolTangency,
213                                    const Standard_Real Epsilon,
214                                    const Standard_Real Deflection,
215                                    const Standard_Real Increment, 
216                                    const Standard_Real U1,
217                                    const Standard_Real V1,
218                                    const Standard_Real U2, 
219                                    const Standard_Real V2)
220      :
221        
222        done(Standard_True),
223        close(Standard_False),
224        fleche(Deflection),
225        tolconf(Epsilon),
226        sensCheminement(1),       
227        myIntersectionOn2S(Caro1,Caro2,TolTangency),
228        STATIC_BLOCAGE_SUR_PAS_TROP_GRAND(0),
229        STATIC_PRECEDENT_INFLEXION(0)
230 {
231   Standard_Real KELARG=20.;
232   //
233   pasMax=Increment*0.2; //-- le 25 juin 99 suite a des pbs de precision 
234   //
235   Um1 = ThePSurfaceTool::FirstUParameter(Caro1);
236   Vm1 = ThePSurfaceTool::FirstVParameter(Caro1);
237   UM1 = ThePSurfaceTool::LastUParameter(Caro1);
238   VM1 = ThePSurfaceTool::LastVParameter(Caro1);
239
240   Um2 = ThePSurfaceTool::FirstUParameter(Caro2);
241   Vm2 = ThePSurfaceTool::FirstVParameter(Caro2);
242   UM2 = ThePSurfaceTool::LastUParameter(Caro2);
243   VM2 = ThePSurfaceTool::LastVParameter(Caro2);
244
245   ResoU1 = ThePSurfaceTool::UResolution(Caro1,Precision::Confusion());
246   ResoV1 = ThePSurfaceTool::VResolution(Caro1,Precision::Confusion());
247
248   ResoU2 = ThePSurfaceTool::UResolution(Caro2,Precision::Confusion());
249   ResoV2 = ThePSurfaceTool::VResolution(Caro2,Precision::Confusion());
250   //
251   Standard_Real NEWRESO, MAXVAL, MAXVAL2;
252   //
253   MAXVAL  = Abs(Um1);  
254   MAXVAL2 = Abs(UM1);
255   if(MAXVAL2 > MAXVAL) {
256     MAXVAL = MAXVAL2;
257   }
258   NEWRESO = ResoU1 * MAXVAL ;
259   if(NEWRESO > ResoU1) {
260     ResoU1 = NEWRESO;  
261   }
262   //
263   MAXVAL  = Abs(Um2);   
264   MAXVAL2 = Abs(UM2);
265   if(MAXVAL2 > MAXVAL){
266     MAXVAL = MAXVAL2;
267   }  
268   NEWRESO = ResoU2 * MAXVAL ;
269   if(NEWRESO > ResoU2) {
270     ResoU2 = NEWRESO;  
271   }
272   //
273   MAXVAL  = Abs(Vm1);  
274   MAXVAL2 = Abs(VM1);
275   if(MAXVAL2 > MAXVAL) {
276     MAXVAL = MAXVAL2;
277   }
278   NEWRESO = ResoV1 * MAXVAL ;
279   if(NEWRESO > ResoV1) {    
280     ResoV1 = NEWRESO; 
281   }
282   //
283   MAXVAL  = Abs(Vm2);  
284   MAXVAL2 = Abs(VM2);
285   if(MAXVAL2 > MAXVAL){
286     MAXVAL = MAXVAL2;
287   }  
288   NEWRESO = ResoV2 * MAXVAL ;
289   if(NEWRESO > ResoV2) {  
290     ResoV2 = NEWRESO;
291   }
292   //
293   pasuv[0]=pasMax*Abs(UM1-Um1);
294   pasuv[1]=pasMax*Abs(VM1-Vm1);
295   pasuv[2]=pasMax*Abs(UM2-Um2);
296   pasuv[3]=pasMax*Abs(VM2-Vm2);
297   //
298   if(ThePSurfaceTool::IsUPeriodic(Caro1)==Standard_False) { 
299     UM1+=KELARG*pasuv[0];  
300     Um1-=KELARG*pasuv[0];
301   }
302   else { 
303     Standard_Real t = UM1-Um1; 
304     if(t<ThePSurfaceTool::UPeriod(Caro1)) { 
305       t=0.5*(ThePSurfaceTool::UPeriod(Caro1)-t);
306       t=(t>KELARG*pasuv[0])? KELARG*pasuv[0] : t;
307       UM1+=t;  
308       Um1-=t;
309     }
310   }
311   //
312   if(ThePSurfaceTool::IsVPeriodic(Caro1)==Standard_False) { 
313     VM1+=KELARG*pasuv[1];
314     Vm1-=KELARG*pasuv[1];
315   }
316   else { 
317     Standard_Real t = VM1-Vm1; 
318     if(t<ThePSurfaceTool::VPeriod(Caro1)) { 
319       t=0.5*(ThePSurfaceTool::VPeriod(Caro1)-t);
320       t=(t>KELARG*pasuv[1])? KELARG*pasuv[1] : t;
321       VM1+=t;  Vm1-=t;
322     }
323   }
324   //
325   if(ThePSurfaceTool::IsUPeriodic(Caro2)==Standard_False) { 
326     UM2+=KELARG*pasuv[2];  
327     Um2-=KELARG*pasuv[2];
328   }
329   else { 
330     Standard_Real t = UM2-Um2; 
331     if(t<ThePSurfaceTool::UPeriod(Caro2)) { 
332       t=0.5*(ThePSurfaceTool::UPeriod(Caro2)-t);
333       t=(t>KELARG*pasuv[2])? KELARG*pasuv[2] : t;
334       UM2+=t;  
335       Um2-=t;
336     }
337   }
338    
339   if(ThePSurfaceTool::IsVPeriodic(Caro2)==Standard_False) {   
340     VM2+=KELARG*pasuv[3];  
341     Vm2-=KELARG*pasuv[3];
342   }
343   else { 
344     Standard_Real t = VM2-Vm2; 
345     if(t<ThePSurfaceTool::UPeriod(Caro2)) { 
346       t=0.5*(ThePSurfaceTool::VPeriod(Caro2)-t);
347       t=(t>KELARG*pasuv[3])? KELARG*pasuv[3] : t;
348       VM2+=t;  
349       Vm2-=t;
350     }
351   }
352   //-- ComputePasInit(pasuv,Um1,UM1,Vm1,VM1,Um2,UM2,Vm2,VM2,Caro1,Caro2);
353
354   for (Standard_Integer i = 0; i<=3;i++) {
355     pasInit[i] = pasSav[i] = pasuv[i]; 
356   }  
357
358   if(ResoU1>0.0001*pasuv[0]) ResoU1=0.00001*pasuv[0];
359   if(ResoV1>0.0001*pasuv[1]) ResoV1=0.00001*pasuv[1];
360   if(ResoU2>0.0001*pasuv[2]) ResoU2=0.00001*pasuv[2];
361   if(ResoV2>0.0001*pasuv[3]) ResoV2=0.00001*pasuv[3];
362   //
363   TColStd_Array1OfReal Par(1,4);
364   Par(1) = U1;
365   Par(2) = V1;
366   Par(3) = U2;
367   Par(4) = V2;
368   Perform(Par);
369 }
370
371 //==================================================================================
372 // function : PerformFirstPoint
373 // purpose  : 
374 //==================================================================================
375 Standard_Boolean IntWalk_PWalking::PerformFirstPoint  (const TColStd_Array1OfReal& ParDep,
376                                                        IntSurf_PntOn2S& FirstPoint)   
377 {
378   sensCheminement = 1;
379   close = Standard_False;
380   //
381   Standard_Integer i;
382   Standard_Real aTmp;
383   TColStd_Array1OfReal Param(1,4);
384   //
385   for (i=1; i<=4; ++i) {
386     aTmp = ParDep(i);
387     Param(i) = ParDep(i);
388   }
389   //-- calcul du premier point solution
390   math_FunctionSetRoot  Rsnld(myIntersectionOn2S.Function());
391   //
392   myIntersectionOn2S.Perform(Param,Rsnld);
393   if (!myIntersectionOn2S.IsDone())  { 
394     return Standard_False;
395   }
396   if (myIntersectionOn2S.IsEmpty()) {
397     return Standard_False;
398   }
399   FirstPoint = myIntersectionOn2S.Point();
400   return Standard_True;
401 }
402 //==================================================================================
403 // function : Perform
404 // purpose  : 
405 //==================================================================================
406 void IntWalk_PWalking::Perform(const TColStd_Array1OfReal& ParDep)    
407 {
408   Perform(ParDep,Um1,Vm1,Um2,Vm2,UM1,VM1,UM2,VM2);
409 }
410 //==================================================================================
411 // function : Perform
412 // purpose  : 
413 //==================================================================================
414 void IntWalk_PWalking::Perform(const TColStd_Array1OfReal& ParDep,
415                                const Standard_Real u1min,
416                                const Standard_Real v1min,
417                                const Standard_Real u2min,
418                                const Standard_Real v2min,
419                                const Standard_Real u1max,
420                                const Standard_Real v1max,
421                                const Standard_Real u2max,
422                                const Standard_Real v2max)
423 {
424   //xf
425   Standard_Integer i, NbPasOKConseq;
426   Standard_Real UFirst1, VFirst1, ULast1, VLast1, UFirst2, VFirst2, ULast2, VLast2;
427   Standard_Real pasMaxSV[4], aTmp;
428   TColStd_Array1OfReal Param(1,4);
429   IntImp_ConstIsoparametric ChoixIso;
430   //xt
431   //
432   done = Standard_False;
433   NbPasOKConseq=0;
434   //
435   // Caro1 and Caro2
436   const ThePSurface& Caro1 =myIntersectionOn2S.Function().AuxillarSurface1();
437   const ThePSurface& Caro2 =myIntersectionOn2S.Function().AuxillarSurface2();
438   //
439   UFirst1 = ThePSurfaceTool::FirstUParameter(Caro1);
440   VFirst1 = ThePSurfaceTool::FirstVParameter(Caro1);
441   ULast1  = ThePSurfaceTool::LastUParameter (Caro1);
442   VLast1  = ThePSurfaceTool::LastVParameter (Caro1);
443   //
444   UFirst2 = ThePSurfaceTool::FirstUParameter(Caro2);
445   VFirst2 = ThePSurfaceTool::FirstVParameter(Caro2);
446   ULast2  = ThePSurfaceTool::LastUParameter (Caro2);
447   VLast2  = ThePSurfaceTool::LastVParameter (Caro2);
448   //
449   ComputePasInit(pasuv,u1min,u1max,v1min,v1max,u2min,u2max,v2min,v2max,
450                  Um1,UM1,Vm1,VM1,Um2,UM2,Vm2,VM2,Caro1,Caro2,pasMax+pasMax);
451   //
452   if(pasuv[0]<100*ResoU1) {
453     pasuv[0]=100*ResoU1; 
454   }
455   if(pasuv[1]<100*ResoV1) {
456     pasuv[1]=100*ResoV1; 
457   }
458   if(pasuv[2]<100*ResoU2) {
459     pasuv[2]=100*ResoU2;
460   }
461   if(pasuv[3]<100*ResoV2) {
462     pasuv[3]=100*ResoV2;
463   }
464   //
465   for (i=0; i<4; ++i) {
466     if(pasuv[i]>10) {
467       pasuv[i] = 10;
468     }
469     pasInit[i] = pasSav[i] = pasuv[i]; 
470   }
471   //
472   line = new IntSurf_LineOn2S ();
473   //
474   for (i=1; i<=4; ++i) {
475     aTmp=ParDep(i);
476     Param(i)=ParDep(i);
477   }
478   //-- reprise des pas uv lies aux surfaces Caro1 et Caro2
479   //-- pasuv[] et pasSav[] sont modifies lors du cheminement
480   for(i = 0; i < 4; ++i) { 
481     pasMaxSV[i] = pasSav[i] = pasuv[i] = pasInit[i]; 
482   }
483
484   //-- calcul du premier point solution
485   math_FunctionSetRoot  Rsnld(myIntersectionOn2S.Function());
486   //
487   ChoixIso = myIntersectionOn2S.Perform(Param,Rsnld);
488   if (!myIntersectionOn2S.IsDone())   {
489     return;
490   }
491   //
492   if (myIntersectionOn2S.IsEmpty()) {
493     return;
494   }
495   //
496   if(myIntersectionOn2S.IsTangent()) {
497     return;
498   }
499   //
500   Standard_Boolean Arrive, DejaReparti;
501   Standard_Integer IncKey, RejectIndex;
502   gp_Pnt pf,pl;
503   //
504   DejaReparti = Standard_False;
505   IncKey = 0;
506   RejectIndex = 0;
507   //
508   previousPoint = myIntersectionOn2S.Point();
509   previoustg = Standard_False;
510   previousd  = myIntersectionOn2S.Direction();
511   previousd1 = myIntersectionOn2S.DirectionOnS1();
512   previousd2 = myIntersectionOn2S.DirectionOnS2();
513   indextg = 1;
514   tgdir   = previousd;
515   firstd1 = previousd1;
516   firstd2 = previousd2;
517   tgfirst = tglast = Standard_False;
518   choixIsoSav  =  ChoixIso;
519   //------------------------------------------------------------
520   //-- On Teste si le premier point de cheminement correspond 
521   //-- a un point sur frontiere. 
522   //-- Dans ce cas, DejaReparti est initialise a True
523   //-- 
524   pf = previousPoint.Value();
525   Standard_Boolean bTestFirstPoint = Standard_True;
526   
527   previousPoint.Parameters(Param(1),Param(2),Param(3),Param(4));  
528   AddAPoint(line,previousPoint);
529   //
530   IntWalk_StatusDeflection Status = IntWalk_OK;
531   Standard_Boolean NoTestDeflection = Standard_False;
532   Standard_Real SvParam[4], f;
533   Standard_Integer LevelOfEmptyInmyIntersectionOn2S=0;
534   Standard_Integer LevelOfPointConfondu = 0; 
535   Standard_Integer LevelOfIterWithoutAppend = -1;
536   //
537   Arrive = Standard_False;
538   while(!Arrive) {//010
539     LevelOfIterWithoutAppend++;
540     if(LevelOfIterWithoutAppend>20) { 
541       Arrive = Standard_True; 
542       if(DejaReparti) {
543         break;
544       }
545       RepartirOuDiviser(DejaReparti,ChoixIso,Arrive);
546       LevelOfIterWithoutAppend = 0;
547     }
548     //
549     // compute f
550     f = 0.;
551     switch (ChoixIso) { 
552       case IntImp_UIsoparametricOnCaro1: f = Abs(previousd1.X()); break;
553       case IntImp_VIsoparametricOnCaro1: f = Abs(previousd1.Y()); break;
554       case IntImp_UIsoparametricOnCaro2: f = Abs(previousd2.X()); break;
555       case IntImp_VIsoparametricOnCaro2: f = Abs(previousd2.Y()); break;
556       default:break;
557     }
558     //
559     if(f<0.1) {
560       f=0.1;
561     }
562     //
563     previousPoint.Parameters(Param(1),Param(2),Param(3),Param(4));
564     //
565     //--ofv.begin
566     Standard_Real aIncKey, aEps, dP1, dP2, dP3, dP4;
567     //
568     dP1 = sensCheminement * pasuv[0] * previousd1.X() /f;
569     dP2 = sensCheminement * pasuv[1] * previousd1.Y() /f;
570     dP3 = sensCheminement * pasuv[2] * previousd2.X() /f; 
571     dP4 = sensCheminement * pasuv[3] * previousd2.Y() /f;
572     //
573     aIncKey=5.*(Standard_Real)IncKey;
574     aEps=1.e-7;
575     if(ChoixIso == IntImp_UIsoparametricOnCaro1 && Abs(dP1) < aEps) {
576       dP1 *= aIncKey;
577     }
578     if(ChoixIso == IntImp_VIsoparametricOnCaro1 && Abs(dP2) < aEps) {
579       dP2 *= aIncKey;
580     }
581     if(ChoixIso == IntImp_UIsoparametricOnCaro2 && Abs(dP3) < aEps) {
582       dP3 *= aIncKey;
583     }
584     if(ChoixIso == IntImp_VIsoparametricOnCaro2 && Abs(dP4) < aEps) {
585       dP4 *= aIncKey;
586     }
587     //--ofv.end
588     //
589     Param(1) += dP1;
590     Param(2) += dP2;
591     Param(3) += dP3; 
592     Param(4) += dP4;
593     //==========================
594     SvParam[0]=Param(1); 
595     SvParam[1]=Param(2);
596     SvParam[2]=Param(3);
597     SvParam[3]=Param(4);
598     //
599     ChoixIso= myIntersectionOn2S.Perform(Param, Rsnld, ChoixIso);                  
600     //
601     if (!myIntersectionOn2S.IsDone())   {
602       //arret de la ligne,division
603       Arrive = Standard_False;
604       Param(1)=SvParam[0]; 
605       Param(2)=SvParam[1]; 
606       Param(3)=SvParam[2];
607       Param(4)=SvParam[3];
608       RepartirOuDiviser(DejaReparti, ChoixIso, Arrive);
609     }
610     else  {//009 
611       //== Le calcul du point exact a partir de Param(.) est possible
612       if (myIntersectionOn2S.IsEmpty()) {
613         Standard_Real u1,v1,u2,v2;
614         previousPoint.Parameters(u1,v1,u2,v2);
615         //
616         Arrive = Standard_False;
617         if(u1<UFirst1 || u1>ULast1) {
618           Arrive=Standard_True;
619         }       
620         if(u2<UFirst2 || u2>ULast2) {
621           Arrive=Standard_True;
622         }
623         if(v1<VFirst1 || v1>VLast1) {
624           Arrive=Standard_True;
625         }
626         if(v2<VFirst2 || v2>VLast2) {
627           Arrive=Standard_True;
628         }       
629         RepartirOuDiviser(DejaReparti,ChoixIso,Arrive);
630         LevelOfEmptyInmyIntersectionOn2S++;
631         //
632         if(LevelOfEmptyInmyIntersectionOn2S>10)    {
633           pasuv[0]=pasSav[0]; 
634           pasuv[1]=pasSav[1]; 
635           pasuv[2]=pasSav[2]; 
636           pasuv[3]=pasSav[3];
637         }           
638       }
639       else {//008
640         //============================================================
641         //== Un point a ete trouve :  T E S T   D E F L E C T I O N 
642         //============================================================
643         if(NoTestDeflection) {
644           NoTestDeflection = Standard_False;
645         }                 
646         else { 
647           if(--LevelOfEmptyInmyIntersectionOn2S<=0)     { 
648             LevelOfEmptyInmyIntersectionOn2S=0;
649             if(LevelOfIterWithoutAppend < 10) {
650               Status = TestDeflection();
651             }                   
652             else   { 
653               pasuv[0]*=0.5; 
654               pasuv[1]*=0.5; 
655               pasuv[2]*=0.5; 
656               pasuv[3]*=0.5;
657             }
658           }
659         }
660         //============================================================
661         //==       T r a i t e m e n t   s u r   S t a t u s        ==
662         //============================================================
663         if(LevelOfPointConfondu > 5) { 
664           Status = IntWalk_ArretSurPoint; 
665           LevelOfPointConfondu = 0;  
666         }
667         //
668         if(Status==IntWalk_OK) { 
669           NbPasOKConseq++;
670           if(NbPasOKConseq >= 5) { 
671             NbPasOKConseq=0;
672             Standard_Boolean pastroppetit;
673             Standard_Real t;
674             //
675             do { 
676               pastroppetit=Standard_True;
677               //
678               if(pasuv[0]<pasInit[0])     { 
679                 t = (pasInit[0]-pasuv[0])*0.25;
680                 if(t>0.1*pasInit[0]) {
681                   t=0.1*pasuv[0];
682                 }
683                 pasuv[0]+=t; 
684                 pastroppetit=Standard_False;
685               } 
686               if(pasuv[1]<pasInit[1])   { 
687                 t = (pasInit[1]-pasuv[1])*0.25;
688                 if(t>0.1*pasInit[1]) {
689                   t=0.1*pasuv[1];
690                 }               
691                 pasuv[1]+=t; 
692                 pastroppetit=Standard_False;
693               } 
694               if(pasuv[2]<pasInit[2]){
695                 t = (pasInit[2]-pasuv[2])*0.25;
696                 if(t>0.1*pasInit[2]) {
697                   t=0.1*pasuv[2];
698                 }
699                 pasuv[2]+=t; 
700                 pastroppetit=Standard_False;
701               } 
702               if(pasuv[3]<pasInit[3])   { 
703                 t = (pasInit[3]-pasuv[3])*0.25;
704                 if(t>0.1*pasInit[3]) {
705                   t=0.1*pasuv[3];
706                 }
707                 pasuv[3]+=t; 
708                 pastroppetit=Standard_False;
709               }
710               if(pastroppetit) { 
711                 if(pasMax<0.1){ 
712                   pasMax*=1.1; 
713                   pasInit[0]*=1.1; 
714                   pasInit[1]*=1.1; 
715                   pasInit[2]*=1.1; 
716                   pasInit[3]*=1.1; 
717                 }
718                 else {
719                   pastroppetit=Standard_False;
720                 }
721               }
722             } while(pastroppetit);
723           }
724         }//Status==IntWalk_OK
725         else NbPasOKConseq=0;
726         //
727         switch(Status) {//007 
728           case IntWalk_ArretSurPointPrecedent:  {                     
729             Arrive = Standard_False;                        
730             RepartirOuDiviser(DejaReparti, ChoixIso, Arrive);
731             break;
732           }
733           case IntWalk_PasTropGrand:  {
734             Param(1)=SvParam[0]; 
735             Param(2)=SvParam[1]; 
736             Param(3)=SvParam[2]; 
737             Param(4)=SvParam[3];
738             if(LevelOfIterWithoutAppend > 5) { 
739               if(pasSav[0]<pasInit[0]) { 
740                 pasInit[0]-=(pasInit[0]-pasSav[0])*0.25; 
741                 LevelOfIterWithoutAppend=0;
742               }
743               if(pasSav[1]<pasInit[1]) { 
744                 pasInit[1]-=(pasInit[1]-pasSav[1])*0.25;  
745                 LevelOfIterWithoutAppend=0; 
746               }
747               if(pasSav[2]<pasInit[2]) { 
748                 pasInit[2]-=(pasInit[2]-pasSav[2])*0.25;  
749                 LevelOfIterWithoutAppend=0; 
750               }
751               if(pasSav[3]<pasInit[3]) {
752                 pasInit[3]-=(pasInit[3]-pasSav[3])*0.25; 
753                 LevelOfIterWithoutAppend=0; 
754               }
755             }
756             break;
757           }
758           case IntWalk_PointConfondu:   {
759             LevelOfPointConfondu++;
760             if(LevelOfPointConfondu>5)   { 
761               Standard_Boolean pastroppetit;
762               //
763               do { 
764                 pastroppetit=Standard_True;
765                 if(pasuv[0]<pasInit[0]) { 
766                   pasuv[0]+=(pasInit[0]-pasuv[0])*0.25;
767                   pastroppetit=Standard_False; 
768                 } 
769                 if(pasuv[1]<pasInit[1]) { 
770                   pasuv[1]+=(pasInit[1]-pasuv[1])*0.25;
771                   pastroppetit=Standard_False; 
772                 } 
773                 if(pasuv[2]<pasInit[2]) { 
774                   pasuv[2]+=(pasInit[2]-pasuv[2])*0.25;
775                   pastroppetit=Standard_False; 
776                 } 
777                 if(pasuv[3]<pasInit[3]) { 
778                   pasuv[3]+=(pasInit[3]-pasuv[3])*0.25; 
779                   pastroppetit=Standard_False; 
780                 }
781                 if(pastroppetit)  { 
782                   if(pasMax<0.1){ 
783                     pasMax*=1.1; 
784                     pasInit[0]*=1.1; 
785                     pasInit[1]*=1.1; 
786                     pasInit[2]*=1.1; 
787                     pasInit[3]*=1.1; 
788                   }
789                   else{
790                     pastroppetit=Standard_False; 
791                   }             
792                 }
793               } while(pastroppetit);
794             }
795             break;
796           }
797           case IntWalk_OK:
798           case IntWalk_ArretSurPoint:  {//006
799             //=======================================================
800             //== T e s t   A r r e t   :  Cadrage sur Param(.)     ==
801             //=======================================================
802             //xft arrive here
803             Arrive = TestArret(DejaReparti,Param,ChoixIso); 
804             // JMB 30th December 1999. 
805             // Some statement below should not be put in comment because they are useful.
806             // See grid CTO 909 A1 which infinitely loops 
807             if(Arrive==Standard_False && Status==IntWalk_ArretSurPoint) { 
808               Arrive=Standard_True;
809 #ifdef DEB
810               cout << "Compile avec option DEB :Si Pb d intersection : ";
811               cout << "IntWalk_PWalking_1.gxx (lbr le 1erdec98)"<<endl;
812 #endif
813             }
814             if(Arrive) {
815               NbPasOKConseq = -10;
816             }
817             if(!Arrive)  {//005
818               //=====================================================
819               //== Param(.)  est dans les bornes                   ==
820               //==  et ne finit pas une ligne fermee               ==
821               //=====================================================
822               //== Verification sur Le Point Courant de myInters
823               Standard_Boolean pointisvalid = Standard_False;
824               {
825                 Standard_Real u1,v1,u2,v2; 
826                 myIntersectionOn2S.Point().Parameters(u1,v1,u2,v2); 
827                 //
828                 if(u1 <= UM1  && u2 <= UM2 && v1 <= VM1 && 
829                    v2 <= VM2  && u1 >= Um1 && u2 >= Um2 &&
830                    v1 >= Vm1  && v2 >= Vm2) {
831                   pointisvalid=Standard_True;
832                 }
833               }
834               //
835               if(pointisvalid) { 
836                 previousPoint = myIntersectionOn2S.Point();
837                 previoustg = myIntersectionOn2S.IsTangent();
838                 if(!previoustg) {
839                   previousd  = myIntersectionOn2S.Direction();
840                   previousd1 = myIntersectionOn2S.DirectionOnS1();
841                   previousd2 = myIntersectionOn2S.DirectionOnS2();
842                 }
843                 //=====================================================
844                 //== Verification sur Previous Point
845                 {
846                   Standard_Real u1,v1,u2,v2; 
847                   previousPoint.Parameters(u1,v1,u2,v2); 
848                   if( u1 <= UM1  && u2 <= UM2 && v1 <= VM1 &&
849                       v2 <= VM2  && u1 >= Um1 && u2 >= Um2 &&
850                       v1 >= Vm1  && v2 >= Vm2) { 
851                     pl = previousPoint.Value();
852                     if(bTestFirstPoint) {
853                       if(pf.Distance(pl) < 1.e-7){ 
854                         IncKey++; 
855                         if(IncKey == 5000) 
856                           return; 
857                         else 
858                           continue; 
859                       }
860                       else {
861                         bTestFirstPoint = Standard_False;
862                       }
863                     }
864                     //
865                     AddAPoint(line,previousPoint);
866                     RejectIndex++; 
867                     if(RejectIndex >= 250000) {
868                       break; 
869                     };
870                     //
871                     LevelOfIterWithoutAppend = 0;
872                   } 
873                 }
874               }//pointisvalid
875               //====================================================
876               if(Status == IntWalk_ArretSurPoint)  {
877                 RepartirOuDiviser(DejaReparti,ChoixIso,Arrive);
878               }
879               else{ 
880                 if (line->NbPoints() == 2) {
881                   pasSav[0] = pasuv[0]; 
882                   pasSav[1] = pasuv[1]; 
883                   pasSav[2] = pasuv[2]; 
884                   pasSav[3] = pasuv[3];
885                 }
886               }
887             }//005 if(!Arrive)
888             //
889             else {//004
890               if(close) { 
891                 //================= la ligne est fermee ===============
892                 AddAPoint(line,line->Value(1)); //ligne fermee
893                 LevelOfIterWithoutAppend=0;
894               }
895               else {//$$$
896                 //====================================================
897                 //== Param n etait pas dans les bornes (a ete recadre)
898                 //====================================================
899                 Standard_Boolean bPrevNotTangent = !previoustg || !myIntersectionOn2S.IsTangent();
900                 
901                 IntImp_ConstIsoparametric SauvChoixIso = ChoixIso;
902                 ChoixIso = myIntersectionOn2S.Perform(Param,Rsnld,ChoixIso);    
903                 //
904                 if(!myIntersectionOn2S.IsEmpty()) { //002
905                   // debordement sur le carreau reciproque ou intersection en coin
906                   if(TestArret(Standard_True,Param,ChoixIso))  {
907                     NbPasOKConseq = -10;
908                     ChoixIso = myIntersectionOn2S.Perform(Param,Rsnld,ChoixIso); 
909                     if(!myIntersectionOn2S.IsEmpty())     {
910                       previousPoint = myIntersectionOn2S.Point();         
911                       previoustg = myIntersectionOn2S.IsTangent();
912                       if (!previoustg)     {
913                         previousd  = myIntersectionOn2S.Direction();
914                         previousd1 = myIntersectionOn2S.DirectionOnS1();
915                         previousd2 = myIntersectionOn2S.DirectionOnS2();
916                       }
917                       pl = previousPoint.Value();
918                       if(bTestFirstPoint) {
919                         if(pf.Distance(pl) < 1.e-7){ 
920                           IncKey++; 
921                           if(IncKey == 5000) 
922                             return; 
923                           else 
924                             continue; 
925                         }
926                         else {
927                           bTestFirstPoint = Standard_False;
928                         }
929                       }
930                       //
931                       AddAPoint(line,previousPoint);
932                       RejectIndex++;
933                       if(RejectIndex >= 250000) {
934                         break;
935                       };
936                       //
937                       LevelOfIterWithoutAppend=0;
938                       RepartirOuDiviser(DejaReparti,ChoixIso,Arrive);
939                     }
940                     else  {
941                       //echec cadrage diviser le pas
942                       Arrive = Standard_False;
943                       RepartirOuDiviser(DejaReparti,ChoixIso,Arrive);
944                       NoTestDeflection = Standard_True;
945                       ChoixIso = SauvChoixIso;
946                     }
947                   }//if(TestArret())
948                   else {
949                     // save the last point
950                     // to revert to it if the current point is out of bounds
951                     IntSurf_PntOn2S previousPointSave = previousPoint;
952                     Standard_Boolean previoustgSave   = previoustg;
953                     gp_Dir previousdSave              = previousd;
954                     gp_Dir2d previousd1Save           = previousd1;
955                     gp_Dir2d previousd2Save           = previousd2;
956                     
957                     previousPoint = myIntersectionOn2S.Point();         
958                     previoustg = myIntersectionOn2S.IsTangent();
959                     Arrive = Standard_False; 
960                     if(!previoustg)  {
961                       previousd  = myIntersectionOn2S.Direction();
962                       previousd1 = myIntersectionOn2S.DirectionOnS1();
963                       previousd2 = myIntersectionOn2S.DirectionOnS2();
964                     }
965                     //========================================
966                     //== Verification sur PreviousPoint @@
967                     {
968                       Standard_Real u1,v1,u2,v2; 
969                       previousPoint.Parameters(u1,v1,u2,v2); 
970                       Param(1) = u1; 
971                       Param(2) = v1;    
972                       Param(3) = u2; 
973                       Param(4) = v2;
974                       //
975                       //xf
976                       Standard_Boolean bFlag1, bFlag2;
977                       Standard_Real aTol2D=1.e-11;
978                       //
979                       bFlag1=u1 >= Um1-aTol2D && v1 >= Vm1-aTol2D && u1 <= UM1+aTol2D && v1 <= VM1+aTol2D;
980                       bFlag2=u2 >= Um2-aTol2D && v2 >= Vm2-aTol2D && u2 <= UM2+aTol2D && v2 <= VM2+aTol2D;
981                       if (bFlag1 && bFlag2) {
982                       /*
983                       if(u1 <= UM1  && u2 <= UM2 && v1 <= VM1 &&
984                          v2 <= VM2  && u1 >= Um1 && u2 >= Um2 &&
985                          v1 >= Vm1  && v2 >= Vm2)  {
986                          */                     
987                       //xt
988                         pl = previousPoint.Value();
989                         if(bTestFirstPoint) {
990                           if(pf.Distance(pl) < 1.e-7) {
991                             IncKey++;
992                             if(IncKey == 5000)
993                               return; 
994                             else 
995                               continue;
996                           }
997                           else {
998                             bTestFirstPoint = Standard_False;
999                           }
1000                         }
1001                         AddAPoint(line,previousPoint);
1002                         RejectIndex++;
1003                         if(RejectIndex >= 250000) {
1004                           break;
1005                         }
1006                         //
1007                         LevelOfIterWithoutAppend=0;
1008                         Arrive = Standard_True;
1009                       }
1010                       else {
1011                         // revert to the last correctly calculated point
1012                         previousPoint = previousPointSave;
1013                         previoustg    = previoustgSave;
1014                         previousd     = previousdSave;
1015                         previousd1    = previousd1Save;
1016                         previousd2    = previousd2Save;
1017                       }
1018                     }
1019                     //
1020                     Standard_Boolean wasExtended = Standard_False;
1021                     
1022                     if(Arrive && myIntersectionOn2S.IsTangent() && bPrevNotTangent) {
1023                       if(ExtendLineInCommonZone(SauvChoixIso, DejaReparti)) {
1024                         wasExtended = Standard_True;
1025                         Arrive = Standard_False;
1026                         ChoixIso = SauvChoixIso;
1027                       }
1028                     }
1029                     
1030                     RepartirOuDiviser(DejaReparti,ChoixIso,Arrive);
1031                     if(Arrive && 
1032                        myIntersectionOn2S.IsDone() && !myIntersectionOn2S.IsEmpty() &&
1033                        myIntersectionOn2S.IsTangent() && bPrevNotTangent &&
1034                        !wasExtended) {
1035                       
1036                       if(ExtendLineInCommonZone(SauvChoixIso, DejaReparti)) {
1037                         wasExtended = Standard_True;
1038                         Arrive = Standard_False;
1039                         ChoixIso = SauvChoixIso;
1040                       }
1041                     }
1042                   }//else !TestArret() $
1043                 } //$$ fin succes cadrage sur frontiere (!myIntersectionOn2S.IsEmpty())
1044                 else  {
1045                   //echec cadrage  sur frontiere;division du pas 
1046                   Arrive = Standard_False;
1047                   NoTestDeflection = Standard_True;
1048                   RepartirOuDiviser(DejaReparti,ChoixIso,Arrive);
1049                 } 
1050               }//$$$ fin cadrage sur frontiere (!close)
1051             } //004 fin TestArret retourne Arrive = True
1052           } // 006case IntWalk_ArretSurPoint:  fin Traitement Status = OK  ou ArretSurPoint 
1053         } //007  switch(Status) 
1054       } //008 fin traitement point en court (TEST DEFLECTION)
1055     } //009 fin traitement ligne (else if myIntersectionOn2S.IsDone())
1056   }  //010 fin si premier point de depart a permis un cheminement while(!Arrive)
1057   done = Standard_True;
1058 }
1059 // ===========================================================================================================
1060 // function: ExtendLineInCommonZone
1061 // purpose:  Extends already computed line inside tangent zone in the direction given by theChoixIso.
1062 //           Returns Standard_True if the line was extended through tangent zone and the last computed point 
1063 //           is outside the tangent zone (but it is not put into the line). Otherwise returns Standard_False.
1064 // ===========================================================================================================
1065 Standard_Boolean IntWalk_PWalking::ExtendLineInCommonZone(const IntImp_ConstIsoparametric theChoixIso,
1066                                                           const Standard_Boolean          theDirectionFlag) 
1067 {
1068   Standard_Boolean bOutOfTangentZone = Standard_False;
1069   Standard_Boolean bStop = !myIntersectionOn2S.IsTangent();
1070   Standard_Integer dIncKey = 1;
1071   TColStd_Array1OfReal Param(1,4);
1072   IntWalk_StatusDeflection Status = IntWalk_OK;
1073   Standard_Integer nbIterWithoutAppend = 0;
1074   Standard_Integer nbEqualPoints = 0;
1075   Standard_Integer parit = 0;
1076   Standard_Integer uvit = 0;
1077   IntSurf_SequenceOfPntOn2S aSeqOfNewPoint;
1078
1079   while (!bStop) {
1080     nbIterWithoutAppend++;
1081
1082     if((nbIterWithoutAppend > 20) || (nbEqualPoints > 20)) {
1083 #ifdef DEB
1084       cout<<"Compile with option DEB:";
1085       cout<<"Infinite loop has detected. Stop iterations (IntWalk_PWalking_1.gxx)" << endl;
1086 #endif
1087       bStop = Standard_True;
1088       break;
1089     }
1090     Standard_Real f = 0.;
1091
1092     switch (theChoixIso)
1093       { 
1094       case IntImp_UIsoparametricOnCaro1: f = Abs(previousd1.X()); break;
1095       case IntImp_VIsoparametricOnCaro1: f = Abs(previousd1.Y()); break;
1096       case IntImp_UIsoparametricOnCaro2: f = Abs(previousd2.X()); break;
1097       case IntImp_VIsoparametricOnCaro2: f = Abs(previousd2.Y()); break;
1098       }
1099
1100     if(f<0.1) f=0.1;
1101     
1102     previousPoint.Parameters(Param(1),Param(2),Param(3),Param(4));
1103
1104     Standard_Real dP1 = sensCheminement * pasuv[0] * previousd1.X() /f;
1105     Standard_Real dP2 = sensCheminement * pasuv[1] * previousd1.Y() /f;
1106     Standard_Real dP3 = sensCheminement * pasuv[2] * previousd2.X() /f; 
1107     Standard_Real dP4 = sensCheminement * pasuv[3] * previousd2.Y() /f;
1108
1109     if(theChoixIso == IntImp_UIsoparametricOnCaro1 && Abs(dP1) < 1.e-7) dP1 *= (5. * (Standard_Real)dIncKey);
1110     if(theChoixIso == IntImp_VIsoparametricOnCaro1 && Abs(dP2) < 1.e-7) dP2 *= (5. * (Standard_Real)dIncKey);
1111     if(theChoixIso == IntImp_UIsoparametricOnCaro2 && Abs(dP3) < 1.e-7) dP3 *= (5. * (Standard_Real)dIncKey);
1112     if(theChoixIso == IntImp_VIsoparametricOnCaro2 && Abs(dP4) < 1.e-7) dP4 *= (5. * (Standard_Real)dIncKey);
1113    
1114     Param(1) += dP1;
1115     Param(2) += dP2;
1116     Param(3) += dP3; 
1117     Param(4) += dP4;
1118     Standard_Real SvParam[4];
1119     IntImp_ConstIsoparametric ChoixIso = theChoixIso;
1120
1121     for(parit = 0; parit < 4; parit++) {
1122       SvParam[parit] = Param(parit+1);
1123     }
1124     math_FunctionSetRoot  Rsnld(myIntersectionOn2S.Function());
1125     ChoixIso = myIntersectionOn2S.Perform(Param,Rsnld, theChoixIso);
1126
1127     if (!myIntersectionOn2S.IsDone()) {
1128       return bOutOfTangentZone;
1129     }
1130     else {
1131       if (myIntersectionOn2S.IsEmpty()) {
1132         return bOutOfTangentZone;
1133       }
1134
1135       Status = TestDeflection();
1136
1137       if(Status == IntWalk_OK) {
1138
1139         for(uvit = 0; uvit < 4; uvit++) {
1140           if(pasuv[uvit] < pasInit[uvit]) {
1141             pasuv[uvit] = pasInit[uvit];
1142           }
1143         }
1144       }
1145
1146       switch(Status) {
1147       case  IntWalk_ArretSurPointPrecedent:
1148         {
1149           bStop = Standard_True;
1150           bOutOfTangentZone = !myIntersectionOn2S.IsTangent();
1151           break;
1152         }
1153       case IntWalk_PasTropGrand:
1154         {
1155           for(parit = 0; parit < 4; parit++) {
1156             Param(parit+1) = SvParam[parit];
1157           }
1158           Standard_Boolean bDecrease = Standard_False;
1159
1160           for(uvit = 0; uvit < 4; uvit++) {
1161             if(pasSav[uvit] < pasInit[uvit]) { 
1162               pasInit[uvit] -= (pasInit[uvit] - pasSav[uvit]) * 0.1;
1163               bDecrease = Standard_True;
1164             }
1165           }
1166
1167           if(bDecrease) nbIterWithoutAppend--;
1168           break;
1169         }
1170       case IntWalk_PointConfondu:
1171         {
1172           for(uvit = 0; uvit < 4; uvit++) {
1173             if(pasuv[uvit] < pasInit[uvit]) {
1174               pasuv[uvit] += (pasInit[uvit] - pasuv[uvit]) * 0.1;
1175             }
1176           }
1177           break;
1178         }
1179       case IntWalk_OK:
1180       case IntWalk_ArretSurPoint:
1181         {
1182           //
1183           bStop = TestArret(theDirectionFlag, Param, ChoixIso);
1184           //
1185
1186           //
1187           if(!bStop) {
1188             Standard_Real u11,v11,u12,v12; 
1189             myIntersectionOn2S.Point().Parameters(u11,v11,u12,v12); 
1190             Standard_Real u21,v21,u22,v22;
1191             previousPoint.Parameters(u21,v21,u22,v22); 
1192
1193             if(((fabs(u11-u21) < ResoU1) && (fabs(v11-v21) < ResoV1)) ||
1194                ((fabs(u12-u22) < ResoU2) && (fabs(v12-v22) < ResoV2))) {
1195               nbEqualPoints++;
1196             }
1197             else {
1198               nbEqualPoints = 0;
1199             }
1200           }
1201           //
1202
1203           bStop = bStop || !myIntersectionOn2S.IsTangent();
1204           bOutOfTangentZone = !myIntersectionOn2S.IsTangent();
1205
1206           if(!bStop) {
1207             Standard_Boolean pointisvalid = Standard_False;
1208             Standard_Real u1,v1,u2,v2; 
1209             myIntersectionOn2S.Point().Parameters(u1,v1,u2,v2); 
1210
1211             if(u1 <= UM1  && u2 <= UM2 && v1 <= VM1 && 
1212                v2 <= VM2  && u1 >= Um1 && u2 >= Um2 &&
1213                v1 >= Vm1  && v2 >= Vm2) 
1214               pointisvalid = Standard_True;
1215
1216             if(pointisvalid) {
1217               previousPoint = myIntersectionOn2S.Point();
1218               previoustg = myIntersectionOn2S.IsTangent();
1219
1220               if(!previoustg) {
1221                 previousd  = myIntersectionOn2S.Direction();
1222                 previousd1 = myIntersectionOn2S.DirectionOnS1();
1223                 previousd2 = myIntersectionOn2S.DirectionOnS2();
1224               }
1225               Standard_Boolean bAddPoint = Standard_True;
1226
1227               if(line->NbPoints() >= 1) {
1228                 gp_Pnt pf = line->Value(1).Value();
1229                 gp_Pnt pl = previousPoint.Value(); 
1230
1231                 if(pf.Distance(pl) < Precision::Confusion()) { 
1232                   dIncKey++; 
1233                   if(dIncKey == 5000) return bOutOfTangentZone; 
1234                   else bAddPoint = Standard_False;
1235                 }
1236               }
1237
1238               if(bAddPoint) {
1239                 aSeqOfNewPoint.Append(previousPoint);
1240                 nbIterWithoutAppend = 0;
1241               }
1242             }
1243
1244             if (line->NbPoints() == 2) {
1245               for(uvit = 0; uvit < 4; uvit++) {
1246                 pasSav[uvit] = pasuv[uvit]; 
1247               }
1248             }
1249
1250             if ( !pointisvalid ) {
1251               // decrease step if out of bounds
1252               // otherwise the same calculations will be 
1253               // repeated several times
1254               if ( ( u1 > UM1 ) || ( u1 < Um1 ) )
1255                 pasuv[0] *= 0.5;
1256
1257               if ( ( v1 > VM1 ) || ( v1 < Vm1 ) ) 
1258                 pasuv[1] *= 0.5;
1259
1260               if ( ( u2 > UM2 ) || ( u2 < Um2 ) )
1261                 pasuv[2] *= 0.5;
1262
1263               if ( ( v2 > VM2 ) || ( v2 < Vm2 ) )
1264                 pasuv[3] *= 0.5;
1265             }
1266           } // end if(!bStop)
1267           else { //if(bStop)
1268             if(close && (line->NbPoints() >= 1)) { 
1269
1270               if(!bOutOfTangentZone) {
1271                 aSeqOfNewPoint.Append(line->Value(1)); // line end
1272               }
1273               nbIterWithoutAppend = 0;
1274             }
1275             else {
1276               ChoixIso = myIntersectionOn2S.Perform(Param, Rsnld, theChoixIso);
1277
1278               if(myIntersectionOn2S.IsEmpty()) { 
1279                 bStop = !myIntersectionOn2S.IsTangent();
1280                 bOutOfTangentZone = !myIntersectionOn2S.IsTangent();
1281               }
1282               else {
1283                 Standard_Boolean bAddPoint = Standard_True;
1284                 Standard_Boolean pointisvalid = Standard_False;
1285
1286                 previousPoint = myIntersectionOn2S.Point();
1287                 Standard_Real u1,v1,u2,v2; 
1288                 previousPoint.Parameters(u1,v1,u2,v2); 
1289
1290                 if(u1 <= UM1  && u2 <= UM2 && v1 <= VM1 && 
1291                    v2 <= VM2  && u1 >= Um1 && u2 >= Um2 &&
1292                    v1 >= Vm1  && v2 >= Vm2) 
1293                   pointisvalid = Standard_True;
1294
1295                 if(pointisvalid) {
1296
1297                   if(line->NbPoints() >= 1) {
1298                     gp_Pnt pf = line->Value(1).Value();
1299                     gp_Pnt pl = previousPoint.Value(); 
1300
1301                     if(pf.Distance(pl) < Precision::Confusion()) { 
1302                       dIncKey++; 
1303                       if(dIncKey == 5000) return bOutOfTangentZone; 
1304                       else bAddPoint = Standard_False;
1305                     }
1306                   }
1307
1308                   if(bAddPoint && !bOutOfTangentZone) {
1309                     aSeqOfNewPoint.Append(previousPoint);
1310                     nbIterWithoutAppend = 0;
1311                   }
1312                 }
1313               }
1314             }
1315           }
1316           break;
1317         }
1318       default:
1319         {
1320           break;
1321         }
1322       }
1323     }
1324   }
1325   Standard_Boolean bExtendLine = Standard_False;
1326   Standard_Real u1 = 0., v1 = 0., u2 = 0., v2 = 0.; 
1327
1328   Standard_Integer pit = 0;
1329
1330   for(pit = 0; !bExtendLine && (pit < 2); pit++) {
1331     if(pit == 0)
1332       previousPoint.Parameters(u1,v1,u2,v2); 
1333     else {
1334       if(aSeqOfNewPoint.Length() > 0)
1335         aSeqOfNewPoint.Value(aSeqOfNewPoint.Length()).Parameters(u1,v1,u2,v2); 
1336       else
1337         break;
1338     }
1339
1340     if(((u1 - Um1) < ResoU1) ||
1341        ((UM1 - u1) < ResoU1) ||
1342        ((u2 - Um2) < ResoU2) ||
1343        ((UM2 - u2) < ResoU2) ||
1344        ((v1 - Vm1) < ResoV1) ||
1345        ((VM1 - v1) < ResoV1) ||
1346        ((v2 - Vm2) < ResoV2) ||
1347        ((VM2 - v2) < ResoV2))
1348       bExtendLine = Standard_True;
1349   }
1350
1351   if(!bExtendLine) {
1352     //    if(Status == IntWalk_OK || Status == IntWalk_ArretSurPoint) {
1353     if(Status == IntWalk_OK) {
1354       bExtendLine = Standard_True;
1355
1356       if(aSeqOfNewPoint.Length() > 1) {
1357         TColStd_Array1OfReal FirstParams(0, 3), LastParams(0, 3), Resolutions(0, 3);
1358         Resolutions(0) = ResoU1; Resolutions(1) = ResoV1; Resolutions(2) = ResoU2; Resolutions(3) = ResoV2;
1359
1360         aSeqOfNewPoint(1).Parameters(FirstParams.ChangeValue(0), FirstParams.ChangeValue(1),
1361                                      FirstParams.ChangeValue(2), FirstParams.ChangeValue(3));
1362         aSeqOfNewPoint(aSeqOfNewPoint.Length()).Parameters(LastParams.ChangeValue(0), 
1363                                                            LastParams.ChangeValue(1),
1364                                                            LastParams.ChangeValue(2), 
1365                                                            LastParams.ChangeValue(3)); 
1366         Standard_Integer indexofiso = 0;
1367
1368         if(theChoixIso == IntImp_UIsoparametricOnCaro1) indexofiso = 0;
1369         if(theChoixIso == IntImp_VIsoparametricOnCaro1) indexofiso = 1;
1370         if(theChoixIso == IntImp_UIsoparametricOnCaro2) indexofiso = 2;
1371         if(theChoixIso == IntImp_VIsoparametricOnCaro2) indexofiso = 3;
1372
1373         Standard_Integer afirstindex = (indexofiso < 2) ? 0 : 2;
1374         gp_Vec2d aTangentZoneDir(gp_Pnt2d(FirstParams.Value(afirstindex), FirstParams.Value(afirstindex + 1)),
1375                                  gp_Pnt2d(LastParams.Value(afirstindex), LastParams.Value(afirstindex + 1)));
1376
1377         gp_Dir2d anIsoDir(0, 1);
1378
1379         if((indexofiso == 1) || (indexofiso == 3))
1380           anIsoDir = gp_Dir2d(1, 0);
1381
1382         if(aTangentZoneDir.SquareMagnitude() > gp::Resolution()) {
1383           Standard_Real piquota = M_PI*0.25;
1384
1385           if(fabs(aTangentZoneDir.Angle(anIsoDir)) > piquota) {
1386             Standard_Integer ii = 1, nextii = 2;
1387             gp_Vec2d d1(0, 0);
1388             Standard_Real asqresol = gp::Resolution();
1389             asqresol *= asqresol;
1390
1391             do {
1392               aSeqOfNewPoint(ii).Parameters(FirstParams.ChangeValue(0), FirstParams.ChangeValue(1),
1393                                             FirstParams.ChangeValue(2), FirstParams.ChangeValue(3));
1394               aSeqOfNewPoint(ii + 1).Parameters(LastParams.ChangeValue(0), LastParams.ChangeValue(1),
1395                                                   LastParams.ChangeValue(2), LastParams.ChangeValue(3));
1396               d1 = gp_Vec2d(gp_Pnt2d(FirstParams.Value(afirstindex),
1397                                      FirstParams.Value(afirstindex + 1)),
1398                             gp_Pnt2d(LastParams.Value(afirstindex),
1399                                      LastParams.Value(afirstindex + 1)));
1400               ii++;
1401             }
1402             while((d1.SquareMagnitude() < asqresol) &&
1403                   (ii < aSeqOfNewPoint.Length()));
1404
1405             nextii = ii;
1406
1407             while(nextii < aSeqOfNewPoint.Length()) {
1408
1409               gp_Vec2d nextd1(0, 0);
1410               Standard_Integer jj = nextii;
1411
1412               do {
1413                 aSeqOfNewPoint(jj).Parameters(FirstParams.ChangeValue(0), FirstParams.ChangeValue(1),
1414                                               FirstParams.ChangeValue(2), FirstParams.ChangeValue(3));
1415                 aSeqOfNewPoint(jj + 1).Parameters(LastParams.ChangeValue(0), LastParams.ChangeValue(1),
1416                                                   LastParams.ChangeValue(2), LastParams.ChangeValue(3));
1417                 nextd1 = gp_Vec2d(gp_Pnt2d(FirstParams.Value(afirstindex),
1418                                            FirstParams.Value(afirstindex + 1)),
1419                                   gp_Pnt2d(LastParams.Value(afirstindex),
1420                                            LastParams.Value(afirstindex + 1)));
1421                 jj++;
1422                 
1423               }
1424               while((nextd1.SquareMagnitude() < asqresol) &&
1425                     (jj < aSeqOfNewPoint.Length()));
1426               nextii = jj;
1427               
1428               if(fabs(d1.Angle(nextd1)) > piquota) {
1429                 bExtendLine = Standard_False;
1430                 break;
1431               }
1432               d1 = nextd1;
1433             }
1434           }
1435           // end if(fabs(aTangentZoneDir.Angle(anIsoDir)
1436         }
1437       }
1438     }
1439   }
1440
1441   if(!bExtendLine) {
1442     return Standard_False;
1443   }
1444   Standard_Integer i = 0;
1445
1446   for(i = 1; i <= aSeqOfNewPoint.Length(); i++) {
1447     AddAPoint(line, aSeqOfNewPoint.Value(i));
1448   }
1449
1450   return bOutOfTangentZone;
1451 }