0024585: Wrong pcurve of the section curve
[occt.git] / src / IntPatch / IntPatch_PrmPrmIntersection.cxx
1 // Created on: 1993-02-02
2 // Created by: Laurent BUCHARD
3 // Copyright (c) 1993-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
9 // under the terms of the GNU Lesser General Public 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 // modified by Edward AGAPOV (eap) Tue Jan 22 12:29:55 2002
18 // modified by Oleg FEDYAED  (ofv) Fri Nov 29 16:08:02 2002
19
20 #include <IntPatch_PrmPrmIntersection.ixx>
21
22 #include <IntPatch_TheInterfPolyhedron.hxx>
23 #include <IntPatch_ThePWalkingInter.hxx>
24 #include <IntPatch_WLine.hxx>
25 #include <IntPatch_RstInt.hxx>
26
27 #include <IntPatch_Line.hxx>
28
29 #include <Handle_IntSurf_LineOn2S.hxx>
30 #include <IntSurf_PntOn2S.hxx>
31 #include <IntSurf_LineOn2S.hxx>
32 #include <TColStd_Array1OfReal.hxx>
33 #include <Intf_PIType.hxx>
34 #include <Intf_SectionLine.hxx>
35 #include <Intf_TangentZone.hxx>
36 #include <Intf_SectionPoint.hxx>
37 #include <gp_Pnt.hxx>
38 #include <gp_Dir.hxx>
39 #include <IntPolyh_Intersection.hxx>
40
41 #include <TColStd_SequenceOfInteger.hxx>
42 #include <IntSurf_ListIteratorOfListOfPntOn2S.hxx>
43 #include <TColStd_HArray1OfReal.hxx>
44
45 static void SectionPointToParameters(const Intf_SectionPoint& Sp,
46                                      const IntPatch_Polyhedron& Surf1,
47                                      const IntPatch_Polyhedron& Surf2,
48                                      Standard_Real& u1,
49                                      Standard_Real& v1,
50                                      Standard_Real& u2,
51                                      Standard_Real& v2);
52
53
54 static 
55 void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
56                       const Handle(Adaptor3d_HSurface)& Surf2,
57                       IntPatch_SequenceOfLine& aSLin);
58
59 static
60 IntSurf_PntOn2S MakeNewPoint(const IntSurf_PntOn2S& replacePnt,
61                              const IntSurf_PntOn2S& oldPnt,
62                              const Standard_Real* Periods);
63
64 static Standard_Boolean IsPointOnLine(const IntSurf_PntOn2S        &thePOn2S,
65                                       const Handle(IntPatch_WLine) &theWLine,
66                                       const Standard_Real           Deflection);
67
68 static void AddWLine(IntPatch_SequenceOfLine      &theLines,
69                      const Handle(IntPatch_WLine) &theWLine,
70                      const Standard_Real           Deflection);
71
72
73 //==================================================================================
74 // function : 
75 // purpose  : 
76 //==================================================================================
77 IntPatch_PrmPrmIntersection::IntPatch_PrmPrmIntersection(): done(Standard_False)
78 {
79 }
80
81 //==================================================================================
82 // function : Perform
83 // purpose  : 
84 //==================================================================================
85 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&   Surf1,
86                                            const Handle(Adaptor3d_TopolTool)& D1,
87                                            const Standard_Real  TolTangency,
88                                            const Standard_Real  Epsilon,
89                                            const Standard_Real  Deflection,
90                                            const Standard_Real  Increment)
91
92   IntPatch_Polyhedron Poly1( Surf1, D1->NbSamplesU(), D1->NbSamplesV() );
93   Perform( Surf1, Poly1, D1, TolTangency, Epsilon, Deflection, Increment );
94 }
95
96 //==================================================================================
97 // function : Perform
98 // purpose  : 
99 //==================================================================================
100 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&   Surf1,
101                                            const IntPatch_Polyhedron& Poly1,
102                                            const Handle(Adaptor3d_TopolTool)& D1,
103                                            const Handle(Adaptor3d_HSurface)&   Surf2,
104                                            const Handle(Adaptor3d_TopolTool)& D2,
105                                            const Standard_Real  TolTangency,
106                                            const Standard_Real  Epsilon,
107                                            const Standard_Real  Deflection,
108                                            const Standard_Real  Increment)
109
110   IntPatch_Polyhedron Poly2( Surf2 );
111   Perform( Surf1, Poly1, D1, Surf2, Poly2, D2, TolTangency, Epsilon, Deflection, Increment);
112 }
113
114 //==================================================================================
115 // function : Perform
116 // purpose  : 
117 //==================================================================================
118 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&   Surf1,
119                                            const Handle(Adaptor3d_TopolTool)& D1,
120                                            const Handle(Adaptor3d_HSurface)&   Surf2,
121                                            const IntPatch_Polyhedron& Poly2,
122                                            const Handle(Adaptor3d_TopolTool)& D2,
123                                            const Standard_Real  TolTangency,
124                                            const Standard_Real  Epsilon,
125                                            const Standard_Real  Deflection,
126                                            const Standard_Real  Increment)
127
128   IntPatch_Polyhedron Poly1( Surf1 );    
129   Perform( Surf1, Poly1, D1, Surf2, Poly2, D2, TolTangency, Epsilon, Deflection, Increment );
130 }
131
132 //==================================================================================
133 // function : Perform
134 // purpose  : 
135 //==================================================================================
136 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&    Surf1,
137                                            const IntPatch_Polyhedron&  Poly1,
138                                            const Handle(Adaptor3d_TopolTool)& D1,
139                                            const Handle(Adaptor3d_HSurface)&    Surf2,
140                                            const IntPatch_Polyhedron&  Poly2,
141                                            const Handle(Adaptor3d_TopolTool)& D2,
142                                            const Standard_Real   TolTangency,
143                                            const Standard_Real   Epsilon,
144                                            const Standard_Real   Deflection,
145                                            const Standard_Real   Increment)
146
147   IntPatch_TheInterfPolyhedron Interference(Poly1,Poly2);
148   empt = Standard_True;
149   done = Standard_True;
150   SLin.Clear();  
151
152   Standard_Integer nbLigSec = Interference.NbSectionLines();
153   Standard_Integer nbTanZon = Interference.NbTangentZones();
154
155   Standard_Integer NbLigCalculee = 0;
156
157   Standard_Real U1,U2,V1,V2;
158   Standard_Real pu1,pu2,pv1,pv2;
159
160   TColStd_Array1OfReal StartParams(1,4);
161
162   IntPatch_ThePWalkingInter PW( Surf1, Surf2, TolTangency, Epsilon, Deflection, Increment );
163
164   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
165   Standard_Real    incidence;
166   Standard_Real    dminiPointLigne;
167
168   Standard_Boolean HasStartPoint,RejetLigne;
169
170   IntSurf_PntOn2S StartPOn2S;
171
172   Standard_Integer ver;
173
174   gp_Pnt Point3dDebut,Point3dFin;
175
176   if( nbLigSec >= 1 ) {
177     Standard_Integer *TabL = new Standard_Integer [nbLigSec+1];
178     Standard_Integer ls;
179     for( ls = 1; ls <= nbLigSec; ls++ )
180       TabL[ls]=ls;
181
182     Standard_Boolean triok;
183     do { 
184       triok=Standard_True;
185       for(Standard_Integer b=2; b<=nbLigSec; b++) {
186         Standard_Integer nb_B = Interference.LineValue(TabL[b]).NumberOfPoints();
187         Standard_Integer nb_A = Interference.LineValue(TabL[b-1]).NumberOfPoints();
188         if( nb_B > nb_A ) { 
189           Standard_Integer tyu=TabL[b]; 
190           TabL[b]=TabL[b-1];
191           TabL[b-1]=tyu;
192           triok=Standard_False;
193         }
194       }
195     } while( triok==Standard_False );
196
197     for( ls = 1; ls <= nbLigSec; ls++) {
198       const Intf_SectionLine& LineSec=Interference.LineValue(TabL[ls]);
199       Standard_Integer nbp = LineSec.NumberOfPoints();
200
201       Standard_Integer *TabPtDep = new Standard_Integer [nbp+1];
202       Standard_Integer ilig;
203       for( ilig = 1; ilig <= nbp; ilig++) 
204         TabPtDep[ilig]=0;
205
206       Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
207       Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
208
209       SectionPointToParameters(LineSec.GetPoint(1),Poly1,Poly2,UminLig1,VminLig1,UminLig2,VminLig2);
210
211       UmaxLig1=UminLig1;
212       VmaxLig1=VminLig1;
213       UmaxLig2=UminLig2;
214       VmaxLig2=VminLig2;
215
216       for( ilig = 2; ilig <= nbp; ilig++ ) { 
217         SectionPointToParameters(LineSec.GetPoint(ilig),Poly1,Poly2,U1,V1,U2,V2);
218
219         if(U1>UmaxLig1) UmaxLig1=U1;
220         if(V1>VmaxLig1) VmaxLig1=V1;
221         if(U2>UmaxLig2) UmaxLig2=U2;
222         if(V2>VmaxLig2) VmaxLig2=V2;
223
224         if(U1<UminLig1) UminLig1=U1;
225         if(V1<VminLig1) VminLig1=V1;
226         if(U2<UminLig2) UminLig2=U2;
227         if(V2<VminLig2) VminLig2=V2;
228       }
229
230       Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
231       Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
232       Standard_Integer IndicePointdeDepart1 = 0,IndicePointdeDepart2 = 0;
233       Standard_Boolean lignetrouvee=Standard_False;
234
235       do { 
236         NombreDePointsDeDepartDuCheminement++;
237         if(NombreDePointsDeDepartDuCheminement == 1) { 
238           incidence=3.0;
239           Standard_Integer nbp1_4=nbp/4;
240           Standard_Integer nbp3_4=nbp-nbp1_4;
241
242           Standard_Integer nsp;
243           for(nsp=nbp/2; nsp<nbp3_4; nsp++) { 
244             Standard_Real CurrentIncidence =  LineSec.GetPoint(nsp).Incidence();
245             if(CurrentIncidence < incidence) { 
246               nbps2 = nsp;
247               incidence = 0.9*CurrentIncidence;
248             }
249           }
250
251           for(nsp=nbp/2; nsp>nbp1_4; nsp--) { 
252             Standard_Real CurrentIncidence =  LineSec.GetPoint(nsp).Incidence();
253             if(CurrentIncidence < incidence) { 
254               nbps2 = nsp;
255               incidence = 0.9*CurrentIncidence;
256             }
257           }
258
259           if(nbp<3) 
260             NombreDePointsDeDepartDuCheminement=3;
261
262           IndicePointdeDepart1 = nbps2;
263         }
264         else if(NombreDePointsDeDepartDuCheminement == 2) { 
265           if(IndicePointdeDepart1 == 1) { 
266             nbps2 = nbp/2;
267             IndicePointdeDepart2 = nbps2;
268           }
269           else { 
270             nbps2 = 1;
271             IndicePointdeDepart2 = 1;
272           }
273         }
274         else if(NombreDePointsDeDepartDuCheminement == 3) {
275           if(IndicePointdeDepart1 == nbp)
276             nbps2 = (IndicePointdeDepart1+IndicePointdeDepart2)/2;
277           else
278             nbps2 = nbp;
279         }
280         else { 
281           nbps2 = NombreDePointsDeDepartDuCheminement-3;
282           NombreDePointsDeDepartDuCheminement++;
283         } 
284
285         if(TabPtDep[nbps2]==0) { 
286           TabPtDep[nbps2]=1;
287           SectionPointToParameters(LineSec.GetPoint(nbps2),Poly1,Poly2,U1,V1,U2,V2);
288
289           StartParams(1) = U1;
290           StartParams(2) = V1;
291           StartParams(3) = U2;
292           StartParams(4) = V2;
293
294           HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
295           dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
296
297           if(HasStartPoint) { 
298             StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
299             NbLigCalculee = SLin.Length();
300             Standard_Integer l;
301             for( l=1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
302               const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
303
304               if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
305                 dminiPointLigne = 0.0;
306               }
307             } // for ( l ...
308
309             if(dminiPointLigne > SeuildPointLigne) { 
310               PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
311               if(PW.IsDone()) {
312                 if(PW.NbPoints()>2) { 
313                   RejetLigne = Standard_False;
314                   Point3dDebut = PW.Value(1).Value();
315                   const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
316                   Point3dFin   = PointFin.Value();
317                   for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
318                     const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
319
320                     // Check end point if it is on existing line.
321                     // Start point is checked before.
322                     if (IsPointOnLine(PointFin, verwline, Deflection)) {
323                       RejetLigne = Standard_True; 
324                       break;
325                     }
326
327                     const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
328                     const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
329                     if( Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency ) { 
330                       if(Point3dFin.Distance(verPointFin.Value()) <= TolTangency)
331                         RejetLigne = Standard_True; 
332                     }
333                   }
334
335                   if(!RejetLigne) { 
336                     // Calculation transition
337                     IntSurf_TypeTrans trans1,trans2;
338                     Standard_Real locu,locv;
339                     gp_Vec norm1,norm2,d1u,d1v;
340                     gp_Pnt ptbid;
341                     Standard_Integer indextg;
342                     gp_Vec tgline(PW.TangentAtLine(indextg));
343                     PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
344                     Surf1->D1(locu,locv,ptbid,d1u,d1v);
345                     norm1 = d1u.Crossed(d1v);
346                     PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
347                     Surf2->D1(locu,locv,ptbid,d1u,d1v);
348                     norm2 = d1u.Crossed(d1v);
349                     if(tgline.DotCross(norm2,norm1)>0.) {
350                       trans1 = IntSurf_Out;
351                       trans2 = IntSurf_In;
352                     }
353                     else {
354                       trans1 = IntSurf_In;
355                       trans2 = IntSurf_Out;
356                     }
357
358                     Standard_Real TolTang = TolTangency;
359                     Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
360                     IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
361                     IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
362
363                     if(wline->NbVertex() == 0) {
364                       IntPatch_Point vtx;
365                       IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
366                       POn2S.Parameters(pu1,pv1,pu2,pv2);
367                       vtx.SetValue(Point3dDebut,TolTang,Standard_False);
368                       vtx.SetParameters(pu1,pv1,pu2,pv2);
369                       vtx.SetParameter(1);
370                       wline->AddVertex(vtx);
371
372                       POn2S = PW.Line()->Value(wline->NbPnts());
373                       POn2S.Parameters(pu1,pv1,pu2,pv2);
374                       vtx.SetValue(Point3dFin,TolTang,Standard_False);
375                       vtx.SetParameters(pu1,pv1,pu2,pv2);
376                       vtx.SetParameter(wline->NbPnts());
377                       wline->AddVertex(vtx);
378                     }
379
380                     lignetrouvee = Standard_True;
381                     AddWLine(SLin, wline, Deflection);
382                     empt = Standard_False;
383                   }// !RejetLigne
384                 }// PW.NbPoints()>2
385               }// done is True
386             }// dminiPointLigne > SeuildPointLigne
387           }// HasStartPoint
388         }// TabPtDep[nbps2]==0
389       } while( nbp>5 && ( !( ( (NombreDePointsDeDepartDuCheminement >= 3) && lignetrouvee ) || 
390         ( (NombreDePointsDeDepartDuCheminement-3>=nbp) && !lignetrouvee ) ) ) );
391
392       delete [] TabPtDep;
393     }// for( ls ...
394
395     delete [] TabL;
396   }// nbLigSec >= 1
397
398   Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
399   Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
400
401   UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
402   UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
403
404   Standard_Integer z;
405   for(z=1; z <= nbTanZon; z++) { 
406     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
407     for(Standard_Integer pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
408       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly2,U1,V1,U2,V2);
409
410       if(U1>UmaxLig1) UmaxLig1=U1;
411       if(V1>VmaxLig1) VmaxLig1=V1;
412       if(U2>UmaxLig2) UmaxLig2=U2;
413       if(V2>VmaxLig2) VmaxLig2=V2;
414
415       if(U1<UminLig1) UminLig1=U1;
416       if(V1<VminLig1) VminLig1=V1;
417       if(U2<UminLig2) UminLig2=U2;
418       if(V2<VminLig2) VminLig2=V2;
419     }
420   }
421
422   for(z=1; z <= nbTanZon; z++) {
423     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
424     Standard_Integer pz;
425     for( pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
426       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly2,U1,V1,U2,V2);
427
428       StartParams(1) = U1;
429       StartParams(2) = V1;
430       StartParams(3) = U2;
431       StartParams(4) = V2;
432
433       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
434       if(HasStartPoint) { 
435         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
436         NbLigCalculee = SLin.Length();
437         dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
438         Standard_Integer l;
439         for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
440           const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
441
442           if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
443             dminiPointLigne = 0.0;
444           }
445         }// for( l ...
446
447         if(dminiPointLigne > SeuildPointLigne) { 
448           PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
449           if(PW.IsDone()) {
450             if(PW.NbPoints()>2) { 
451               RejetLigne = Standard_False;
452               Point3dDebut = PW.Value(1).Value();
453               const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
454               Point3dFin   = PointFin.Value();
455               for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
456                 const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
457
458                 // Check end point if it is on existing line.
459                 // Start point is checked before.
460                 if (IsPointOnLine(PointFin, verwline, Deflection)) {
461                   RejetLigne = Standard_True; 
462                   break;
463                 }
464
465                 const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
466                 const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
467                 if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
468                   RejetLigne = Standard_True; 
469                 else { 
470                   if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
471                     RejetLigne = Standard_True; 
472                 }
473               }
474
475               if(!RejetLigne) { 
476                 IntSurf_TypeTrans trans1,trans2;
477                 Standard_Real locu,locv;
478                 gp_Vec norm1,norm2,d1u,d1v;
479                 gp_Pnt ptbid;
480                 Standard_Integer indextg;
481                 gp_Vec tgline(PW.TangentAtLine(indextg));
482                 PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
483                 Surf1->D1(locu,locv,ptbid,d1u,d1v);
484                 norm1 = d1u.Crossed(d1v);
485                 PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
486                 Surf2->D1(locu,locv,ptbid,d1u,d1v);
487                 norm2 = d1u.Crossed(d1v);
488                 if(tgline.DotCross(norm2,norm1)>0.) {
489                   trans1 = IntSurf_Out;
490                   trans2 = IntSurf_In;
491                 }
492                 else {
493                   trans1 = IntSurf_In;
494                   trans2 = IntSurf_Out;
495                 }
496
497                 Standard_Real TolTang = TolTangency;
498                 Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
499                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
500                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
501
502                 if(wline->NbVertex() == 0) {
503                   IntPatch_Point vtx;
504                   IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
505                   POn2S.Parameters(pu1,pv1,pu2,pv2);
506                   vtx.SetValue(Point3dDebut,TolTang,Standard_False);
507                   vtx.SetParameters(pu1,pv1,pu2,pv2);
508                   vtx.SetParameter(1);
509                   wline->AddVertex(vtx);
510
511                   POn2S = PW.Line()->Value(wline->NbPnts());
512                   POn2S.Parameters(pu1,pv1,pu2,pv2);
513                   vtx.SetValue(Point3dFin,TolTang,Standard_False);
514                   vtx.SetParameters(pu1,pv1,pu2,pv2);
515                   vtx.SetParameter(wline->NbPnts());
516                   wline->AddVertex(vtx);
517                 }
518
519                 AddWLine(SLin, wline, Deflection);
520                 empt = Standard_False;
521               }// if !RejetLigne
522             }// PW.NbPoints()>2
523           }// done is True
524         }// dminiPointLigne > SeuildPointLigne
525       }// HasStartPoint
526     }// for( pz ...
527   }// for( z ...
528 }
529
530 //==================================================================================
531 // function : Perform
532 // purpose  : 
533 //==================================================================================
534 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&    Surf1,
535                                            const IntPatch_Polyhedron&  Poly1,
536                                            const Handle(Adaptor3d_TopolTool)& D1,
537                                            const Standard_Real   TolTangency,
538                                            const Standard_Real   Epsilon,
539                                            const Standard_Real   Deflection,
540                                            const Standard_Real   Increment)
541
542   IntPatch_TheInterfPolyhedron Interference(Poly1);
543   empt = Standard_True;
544   done = Standard_True;
545   SLin.Clear();  
546
547   Standard_Integer nbLigSec = Interference.NbSectionLines();
548   Standard_Integer nbTanZon = Interference.NbTangentZones();
549
550   Standard_Integer NbPntOn2SOnLine;
551   Standard_Integer NbLigCalculee = 0;
552
553   Standard_Real U1,U2,V1,V2;
554   Standard_Real pu1,pu2,pv1,pv2;
555
556   TColStd_Array1OfReal StartParams(1,4);
557   IntPatch_ThePWalkingInter PW(Surf1,Surf1,TolTangency,Epsilon,Deflection,Increment);
558
559   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
560   Standard_Real    incidence;
561   Standard_Real    dminiPointLigne;
562
563   Standard_Boolean HasStartPoint,RejetLigne;
564
565   IntSurf_PntOn2S StartPOn2S;
566
567   Standard_Integer ver;
568
569   gp_Pnt Point3dDebut,Point3dFin;
570
571   if(nbLigSec>=1) {
572     Standard_Integer ls;
573     for( ls = 1; ls <= nbLigSec; ls++) {
574       const Intf_SectionLine& LineSec=Interference.LineValue(ls);
575       Standard_Integer nbp = LineSec.NumberOfPoints();
576       Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
577       Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
578       Standard_Integer IndicePointdeDepart1 = 0, IndicePointdeDepart2 = 0;
579       do { 
580         NombreDePointsDeDepartDuCheminement++;
581         if(NombreDePointsDeDepartDuCheminement == 1) { 
582           incidence = 0.0;
583           Standard_Integer nsp1;
584           for( nsp1= nbp/2; nsp1 >= 1; nsp1--) { 
585             SectionPointToParameters(LineSec.GetPoint(nsp1),Poly1,Poly1,U1,V1,U2,V2);
586             Standard_Real CurrentIncidence =  Abs(U1-U2)+Abs(V1-V2);
587             if(CurrentIncidence > incidence) { 
588               nbps2 = nsp1;
589               incidence = CurrentIncidence;
590             }
591           }
592           for( nsp1 = nbp/2; nsp1 <= nbp; nsp1++) { 
593             SectionPointToParameters(LineSec.GetPoint(nsp1),Poly1,Poly1,U1,V1,U2,V2);
594             Standard_Real CurrentIncidence =  Abs(U1-U2)+Abs(V1-V2);
595             if(CurrentIncidence > incidence) { 
596               nbps2 = nsp1;
597               incidence = CurrentIncidence;
598             }
599           }
600
601           if(nbp<3) 
602             NombreDePointsDeDepartDuCheminement=3;
603
604           IndicePointdeDepart1 = nbps2;
605         }
606         else if(NombreDePointsDeDepartDuCheminement == 2) { 
607           if(IndicePointdeDepart1 == 1) { 
608             nbps2 = nbp/2;
609             IndicePointdeDepart2 = nbps2;
610           }
611           else { 
612             nbps2 = 1;
613             IndicePointdeDepart2 = 1;
614           }
615         }
616         else {
617           if(IndicePointdeDepart1 == nbp)
618             nbps2 = (IndicePointdeDepart1+IndicePointdeDepart2)/2;
619           else
620             nbps2 = nbp;
621         }
622
623         SectionPointToParameters(LineSec.GetPoint(nbps2),Poly1,Poly1,U1,V1,U2,V2);
624
625         StartParams(1) = U1;
626         StartParams(2) = V1;
627         StartParams(3) = U2;
628         StartParams(4) = V2;
629
630         HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
631         dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
632         if(HasStartPoint) { 
633           StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
634           if(Abs(pu1-pu2)>1e-7 || Abs(pv1-pv2)>1e-7) { 
635             NbLigCalculee = SLin.Length();
636             Standard_Integer l;
637             for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
638               const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
639               if( (testwline->IsOutSurf1Box(gp_Pnt2d(pu1,pv1))==Standard_False) &&
640                 (testwline->IsOutSurf2Box(gp_Pnt2d(pu2,pv2))==Standard_False) &&
641                 (testwline->IsOutBox(StartPOn2S.Value())==Standard_False) ) { 
642                   NbPntOn2SOnLine = testwline->NbPnts();
643                   Standard_Integer ll;
644                   for( ll=1; (ll < NbPntOn2SOnLine) && (dminiPointLigne >= SeuildPointLigne); ll++) { 
645                     Standard_Real t,Au1,Av1,Au2,Av2,Bu1,Bv1,Bu2,Bv2;
646                     testwline->Point(ll).Parameters(Au1,Av1,Au2,Av2);
647                     testwline->Point(ll+1).Parameters(Bu1,Bv1,Bu2,Bv2);
648                     if(Au1>Bu1) {
649                       t=Au1;
650                       Au1=Bu1;
651                       Bu1=t;
652                     } 
653                     if(Av1>Bv1) {
654                       t=Av1;
655                       Av1=Bv1;
656                       Bv1=t;
657                     } 
658                     Au1-=1.0e-7;
659                     Av1-=1.0e-7;
660                     Bu1+=1.0e-7;
661                     Bv1+=1.0e-7;
662
663                     if((pu1>=Au1) && (pu1<=Bu1) && (pv1>=Av1) && (pv1<=Bv1))
664                       dminiPointLigne = 0.0; 
665                     else { 
666                       if((pu2>=Au1) && (pu2<=Bu1) && (pv2>=Av1) && (pv2<=Bv1))
667                         dminiPointLigne = 0.0;
668                     }
669                   }// for( ll ...
670               }// if ...
671             }// for( l ...
672
673             if(dminiPointLigne > SeuildPointLigne) { 
674               PW.Perform(StartParams);
675               if(PW.IsDone()) {
676                 if(PW.NbPoints()>2) { 
677                   RejetLigne = Standard_False;
678                   Point3dDebut = PW.Value(1).Value();
679                   Point3dFin   = PW.Value(PW.NbPoints()).Value();
680                   for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
681                     const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
682                     const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
683                     const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
684                     if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
685                       RejetLigne = Standard_True; 
686                     else { 
687                       if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
688                         RejetLigne = Standard_True; 
689                     }
690                   }
691
692                   if(!RejetLigne) { 
693                     IntSurf_TypeTrans trans1,trans2;
694                     Standard_Real locu,locv;
695                     gp_Vec norm1,norm2,d1u,d1v;
696                     gp_Pnt ptbid;
697                     Standard_Integer indextg;
698                     gp_Vec tgline(PW.TangentAtLine(indextg));
699                     PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
700                     Surf1->D1(locu,locv,ptbid,d1u,d1v);
701                     norm1 = d1u.Crossed(d1v);
702                     PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
703                     Surf1->D1(locu,locv,ptbid,d1u,d1v);
704                     norm2 = d1u.Crossed(d1v);
705                     if (tgline.DotCross(norm2,norm1)>0.) {
706                       trans1 = IntSurf_Out;
707                       trans2 = IntSurf_In;
708                     }
709                     else {
710                       trans1 = IntSurf_In;
711                       trans2 = IntSurf_Out;
712                     }
713
714                     IntSurf_LineOn2S LineOn2S;
715                     Standard_Integer nbpw,imin,imax,i;
716                     nbpw = PW.Line()->NbPoints();
717                     Standard_Real u1,v1,u2,v2;
718                     i=0;
719                     do { 
720                       i++;
721                       imin=i;
722                       const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
723                       Pi.Parameters(u1,v1,u2,v2);
724                     } while((i<nbpw)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
725
726                     if(imin>2)
727                       imin--;
728
729                     i=nbpw+1;
730                     do { 
731                       i--;
732                       imax=i;
733                       const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
734                       Pi.Parameters(u1,v1,u2,v2);
735                     } while((i>2)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
736
737                     if(imax<nbpw)
738                       imax++;
739
740                     if(imin<imax) { 
741                       Handle_IntSurf_LineOn2S PWLine = new IntSurf_LineOn2S();
742                       for(i=imin;i<=imax;i++) 
743                         PWLine->Add(PW.Line()->Value(i));
744
745                       Standard_Real TolTang = TolTangency;
746                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PWLine,Standard_False,trans1,trans2);
747                       const IntSurf_PntOn2S& POn2SDeb = wline->Point(1);
748                       const IntSurf_PntOn2S& POn2SFin = wline->Point(wline->NbPnts());
749                       if((POn2SDeb.Value()).Distance(POn2SFin.Value()) <= TolTangency) { 
750                         Standard_Real u1t,v1t,u2t,v2t; 
751                         POn2SDeb.Parameters(u1t,v1t,u2t,v2t);
752                         IntPatch_Point vtx;
753                         vtx.SetValue(POn2SDeb.Value(),TolTang,Standard_False);
754                         vtx.SetParameters(u2t,v2t,u1t,v1t);
755                         vtx.SetParameter(wline->NbPnts());
756                         wline->SetPoint(wline->NbPnts(),vtx);
757                       }
758                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf1,Standard_True,TolTang);
759                       if(wline->NbVertex() == 0) {
760                         IntPatch_Point vtx;
761                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
762                         POn2S.Parameters(pu1,pv1,pu2,pv2);
763                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
764                         vtx.SetParameters(pu1,pv1,pu2,pv2);
765                         vtx.SetParameter(1);
766                         wline->AddVertex(vtx);
767
768                         POn2S = PW.Line()->Value(wline->NbPnts());
769                         POn2S.Parameters(pu1,pv1,pu2,pv2);
770                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
771                         vtx.SetParameters(pu1,pv1,pu2,pv2);
772                         vtx.SetParameter(wline->NbPnts());
773                         wline->AddVertex(vtx);
774                       }
775                       SLin.Append(wline);
776                       empt = Standard_False;
777                     }// imin<imax
778                   }// !RejetLigne
779                 }// PW.NbPoints()>2
780               }// done is True
781             }// dminiPointLigne > SeuildPointLigne
782           }// Abs || Abs
783         }// HasStartPoint
784       } while(nbp>5 && NombreDePointsDeDepartDuCheminement<3);
785     }// for( ls ...
786   }// nbLigSec>=1
787
788   Standard_Integer z;
789   for( z = 1; z <= nbTanZon; z++) { 
790     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
791     for(Standard_Integer pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
792       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly1,U1,V1,U2,V2);
793
794       StartParams(1) = U1;
795       StartParams(2) = V1;
796       StartParams(3) = U2;
797       StartParams(4) = V2;
798
799       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);     
800       if(HasStartPoint) { 
801         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
802         if(Abs(pu1-pu2)>1e-7 || Abs(pv1-pv2)>1e-7) { 
803           NbLigCalculee = SLin.Length();
804           dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
805           Standard_Integer l;
806           for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
807             const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
808             if( (testwline->IsOutSurf1Box(gp_Pnt2d(pu1,pv1))==Standard_False) &&
809               (testwline->IsOutSurf2Box(gp_Pnt2d(pu2,pv2))==Standard_False) &&
810               (testwline->IsOutBox(StartPOn2S.Value())==Standard_False) ) { 
811                 NbPntOn2SOnLine = testwline->NbPnts();
812                 Standard_Integer ll;
813                 for( ll = 1; (ll < NbPntOn2SOnLine) && (dminiPointLigne >= SeuildPointLigne); ll++) { 
814                   Standard_Real t,Au1,Av1,Au2,Av2,Bu1,Bv1,Bu2,Bv2;
815                   testwline->Point(ll).Parameters(Au1,Av1,Au2,Av2);
816                   testwline->Point(ll+1).Parameters(Bu1,Bv1,Bu2,Bv2);
817                   if(Au1>Bu1) {
818                     t=Au1;
819                     Au1=Bu1;
820                     Bu1=t;
821                   } 
822                   if(Av1>Bv1) {
823                     t=Av1;
824                     Av1=Bv1;
825                     Bv1=t;
826                   } 
827                   Au1-=1.0e-7;
828                   Av1-=1.0e-7;
829                   Bu1+=1.0e-7;
830                   Bv1+=1.0e-7;
831                   if((pu1>=Au1) && (pu1<=Bu1) && (pv1>=Av1) && (pv1<=Bv1))
832                     dminiPointLigne = 0.0; 
833                   else { 
834                     if((pu2>=Au1) && (pu2<=Bu1) && (pv2>=Av1) && (pv2<=Bv1))
835                       dminiPointLigne = 0.0;
836                   }
837                 }// for( ll ...
838             }// if ...
839           }// for( l ...
840
841           if(dminiPointLigne > SeuildPointLigne) { 
842             PW.Perform(StartParams);
843             if(PW.IsDone()) {
844               if(PW.NbPoints()>2) { 
845                 RejetLigne = Standard_False;
846                 Point3dDebut = PW.Value(1).Value();
847                 Point3dFin   = PW.Value(PW.NbPoints()).Value();
848                 for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
849                   const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
850                   const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
851                   const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
852                   if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
853                     RejetLigne = Standard_True; 
854                   else { 
855                     if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
856                       RejetLigne = Standard_True; 
857                   }
858                 }
859
860                 if(!RejetLigne) { 
861                   IntSurf_TypeTrans trans1,trans2;
862                   Standard_Real locu,locv;
863                   gp_Vec norm1,norm2,d1u,d1v;
864                   gp_Pnt ptbid;
865                   Standard_Integer indextg;
866                   gp_Vec tgline(PW.TangentAtLine(indextg));
867                   PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
868                   Surf1->D1(locu,locv,ptbid,d1u,d1v);
869                   norm1 = d1u.Crossed(d1v);
870                   PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
871                   Surf1->D1(locu,locv,ptbid,d1u,d1v);
872                   norm2 = d1u.Crossed(d1v);
873                   if(tgline.DotCross(norm2,norm1)>0.) {
874                     trans1 = IntSurf_Out;
875                     trans2 = IntSurf_In;
876                   }
877                   else {
878                     trans1 = IntSurf_In;
879                     trans2 = IntSurf_Out;
880                   }
881
882                   IntSurf_LineOn2S LineOn2S;
883                   Standard_Integer nbp,imin,imax,i;
884                   nbp = PW.Line()->NbPoints();
885                   Standard_Real u1,v1,u2,v2;
886                   i=0;
887                   do { 
888                     i++;
889                     imin=i;
890                     const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
891                     Pi.Parameters(u1,v1,u2,v2);
892                   } while((i<nbp)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
893
894                   if(imin>2)
895                     imin--;
896
897                   i=nbp+1;
898                   do { 
899                     i--;
900                     imax=i;
901                     const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
902                     Pi.Parameters(u1,v1,u2,v2);
903                   } while((i>2)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
904
905                   if(imax<nbp)
906                     imax++;
907
908                   if(imin<imax) { 
909                     Handle_IntSurf_LineOn2S PWLine = new IntSurf_LineOn2S();
910                     for(i=imin;i<=imax;i++)
911                       PWLine->Add(PW.Line()->Value(i));
912
913                     Standard_Real TolTang = TolTangency;
914                     Handle(IntPatch_WLine) wline = new IntPatch_WLine(PWLine,Standard_False,trans1,trans2);
915                     const IntSurf_PntOn2S& POn2SDeb = wline->Point(1);
916                     const IntSurf_PntOn2S& POn2SFin = wline->Point(wline->NbPnts());
917                     if((POn2SDeb.Value()).Distance(POn2SFin.Value()) <= TolTangency) { 
918                       Standard_Real u1t,v1t,u2t,v2t; 
919                       POn2SDeb.Parameters(u1t,v1t,u2t,v2t);
920                       IntPatch_Point vtx;
921                       vtx.SetValue(POn2SDeb.Value(),TolTang,Standard_False);
922                       vtx.SetParameters(u2t,v2t,u1t,v1t);
923                       vtx.SetParameter(wline->NbPnts());
924                       wline->SetPoint(wline->NbPnts(),vtx);
925                     }
926
927                     IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf1,Standard_True,TolTang);
928
929                     if(wline->NbVertex() == 0) {
930                       IntPatch_Point vtx;
931                       IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
932                       POn2S.Parameters(pu1,pv1,pu2,pv2);
933                       vtx.SetValue(Point3dDebut,TolTang,Standard_False);
934                       vtx.SetParameters(pu1,pv1,pu2,pv2);
935                       vtx.SetParameter(1);
936                       wline->AddVertex(vtx);
937
938                       POn2S = PW.Line()->Value(wline->NbPnts());
939                       POn2S.Parameters(pu1,pv1,pu2,pv2);
940                       vtx.SetValue(Point3dFin,TolTang,Standard_False);
941                       vtx.SetParameters(pu1,pv1,pu2,pv2);
942                       vtx.SetParameter(wline->NbPnts());
943                       wline->AddVertex(vtx);
944                     }
945
946                     SLin.Append(wline);
947                     empt = Standard_False;
948                   }// imin<imax
949                 }// !RejetLigne
950               }// PW.NbPoints()>2
951             }// done a True
952           }// dminiPointLigne > SeuildPointLigne
953         }// Abs || Abs
954       }// HasStartPoint
955     }// for ( pz ...
956   }// for( z ...
957 }
958
959 //==================================================================================
960 // function : NewLine
961 // purpose  : 
962 //==================================================================================
963 Handle_IntPatch_Line IntPatch_PrmPrmIntersection::NewLine (const Handle(Adaptor3d_HSurface)&    Surf1,
964                                                            const Handle(Adaptor3d_HSurface)&    Surf2,
965                                                            const Standard_Integer NumLine,
966                                                            const Standard_Integer Low,
967                                                            const Standard_Integer High,
968                                                            const Standard_Integer NbPntsToInsert) const 
969
970   Standard_Integer NbPnts = NbPntsToInsert + High - Low;
971   if(NumLine>NbLines() || NumLine<1  || Low>=High ) 
972     Standard_OutOfRange::Raise(" IntPatch_PrmPrmIntersection NewLine "); 
973   //------------------------------------------------------------------
974   //--  Indice     :   Low       Low+1     I    I+1         High    --
975   //--                                                              --
976   //--  Abs.Curv.  :  S(Low)              S(I)  S(I+1)      S(High) --
977   //--                                                              --
978   //--                On echantillonne a abcisse curviligne         --
979   //--                constante.                                    --
980   //--                L abcisse est calculee sur les params U1,V1   --
981   //------------------------------------------------------------------
982   TColStd_Array1OfReal U1(Low,High);
983   TColStd_Array1OfReal V1(Low,High);
984   TColStd_Array1OfReal U2(Low,High);
985   TColStd_Array1OfReal V2(Low,High);
986   TColStd_Array1OfReal AC(Low,High);
987
988   Standard_Real s,ds;
989   Handle(IntPatch_WLine) TheLine = Handle(IntPatch_WLine)::DownCast(Line(NumLine));
990   const IntSurf_PntOn2S& Point=TheLine->Point(Low);
991   Standard_Real u1,v1,u2,v2;
992   Point.Parameters(u1,v1,u2,v2);
993   U1(Low) = u1;
994   V1(Low) = v1;
995   U2(Low) = u2;
996   V2(Low) = v2;
997   AC(Low) =0.0;
998
999   IntPatch_ThePWalkingInter PW(Surf1,Surf2,0.000001,0.000001,0.001,0.001);
1000
1001   Standard_Integer i;
1002   for(i=Low+1; i<=High; i++)
1003   {
1004     const IntSurf_PntOn2S& Pointi=TheLine->Point(i);
1005     Pointi.Parameters(u1,v1,u2,v2);
1006     U1(i) = u1;
1007     V1(i) = v1;
1008     U2(i) = u2;
1009     V2(i) = v2;
1010
1011     Standard_Real du1=u1-U1(i-1);
1012     Standard_Real dv1=v1-V1(i-1);
1013
1014     AC(i) = AC(i-1) + Sqrt((du1*du1)+(dv1*dv1));
1015   }
1016
1017   Handle(IntSurf_LineOn2S) ResultPntOn2SLine = new IntSurf_LineOn2S();
1018
1019   IntSurf_PntOn2S StartPOn2S;  
1020   TColStd_Array1OfReal StartParams(1,4);
1021
1022   ResultPntOn2SLine->Add(TheLine->Point(Low));
1023
1024   ds = AC(High) / (NbPnts-1);
1025   Standard_Integer Indice = Low;
1026
1027   Standard_Real dsmin = ds*0.3;
1028   Standard_Real smax  = AC(High);
1029
1030   for(i=2,s=ds; (i<NbPnts)&&(s<smax); i++,s+=ds)
1031   { 
1032     while(AC(Indice+1) <= s)
1033     { 
1034       ResultPntOn2SLine->Add(TheLine->Point(Indice));
1035       Indice++;
1036     }
1037     Standard_Real a = s - AC(Indice);
1038     Standard_Real b = AC(Indice+1) - s;
1039     Standard_Real nab = 1.0/(a+b);
1040     //----------------------------------------------------------
1041     //-- Verification :  Si Dist au prochain  point < dsmin   --
1042     //--                 Si Dist au precedent point < dsmin   --
1043     //--                                                      --
1044     //----------------------------------------------------------
1045     if((nab > ds)&&(a>dsmin)&&(b>dsmin))
1046     {
1047       StartParams(1) = (U1(Indice) * b   +  U1(Indice+1) * a) * nab;
1048       StartParams(2) = (V1(Indice) * b   +  V1(Indice+1) * a) * nab;
1049       StartParams(3) = (U2(Indice) * b   +  U2(Indice+1) * a) * nab;
1050       StartParams(4) = (V2(Indice) * b   +  V2(Indice+1) * a) * nab;
1051
1052       Standard_Boolean HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
1053       if(HasStartPoint)
1054         ResultPntOn2SLine->Add(StartPOn2S);
1055     }
1056     else
1057       s+=dsmin; 
1058   }
1059
1060   ResultPntOn2SLine->Add(TheLine->Point(High));
1061
1062   return(new IntPatch_WLine(ResultPntOn2SLine,Standard_False));
1063 }
1064
1065 //==================================================================================
1066 // function : SectionPointToParameters
1067 // purpose  : 
1068 //==================================================================================
1069 void SectionPointToParameters(const Intf_SectionPoint& Sp,
1070                               const IntPatch_Polyhedron& Poly1,
1071                               const IntPatch_Polyhedron& Poly2,
1072                               Standard_Real& u1,
1073                               Standard_Real& v1,
1074                               Standard_Real& u2,
1075                               Standard_Real& v2)
1076 {
1077   Intf_PIType       typ;
1078   Standard_Integer  Adr1,Adr2;
1079   Standard_Real     Param,u,v;
1080   gp_Pnt P(Sp.Pnt());
1081
1082   Standard_Integer Pt1,Pt2,Pt3;
1083
1084   Sp.InfoFirst(typ,Adr1,Adr2,Param);
1085   switch(typ) { 
1086   case Intf_VERTEX:   //-- Adr1 est le numero du vertex
1087     {
1088       Poly1.Parameters(Adr1,u1,v1);
1089       break;
1090     }
1091   case Intf_EDGE:
1092     {
1093       Poly1.Parameters(Adr1,u1,v1);    
1094       Poly1.Parameters(Adr2,u,v);
1095       u1+= Param * (u-u1);
1096       v1+= Param * (v-v1);
1097       break;
1098     }
1099   case Intf_FACE:
1100     {
1101       Standard_Real ua,va,ub,vb,uc,vc,ca,cb,cc,cabc;
1102       Poly1.Triangle(Adr1,Pt1,Pt2,Pt3);
1103       gp_Pnt PA(Poly1.Point(Pt1));
1104       gp_Pnt PB(Poly1.Point(Pt2));
1105       gp_Pnt PC(Poly1.Point(Pt3));
1106       Poly1.Parameters(Pt1,ua,va);
1107       Poly1.Parameters(Pt2,ub,vb);
1108       Poly1.Parameters(Pt3,uc,vc);
1109       gp_Vec Normale(gp_Vec(PA,PB).Crossed(gp_Vec(PA,PC)));
1110       cc = (gp_Vec(PA,PB).Crossed(gp_Vec(PA,P))).Dot(Normale);
1111       ca = (gp_Vec(PB,PC).Crossed(gp_Vec(PB,P))).Dot(Normale);
1112       cb = (gp_Vec(PC,PA).Crossed(gp_Vec(PC,P))).Dot(Normale);
1113       cabc = ca + cb + cc;
1114
1115       ca/=cabc;     cb/=cabc;       cc/=cabc;
1116
1117       u1 = ca * ua + cb * ub + cc * uc;
1118       v1 = ca * va + cb * vb + cc * vc;
1119       break;
1120     }
1121   default: 
1122     {
1123       //-- cout<<" Default dans SectionPointToParameters "<<endl;
1124       break;
1125     }
1126   }
1127
1128
1129   Sp.InfoSecond(typ,Adr1,Adr2,Param);
1130   switch(typ) { 
1131   case Intf_VERTEX:   //-- Adr1 est le numero du vertex
1132     {
1133       Poly2.Parameters(Adr1,u2,v2);
1134       break;
1135     }
1136   case Intf_EDGE:
1137     {
1138       Poly2.Parameters(Adr1,u2,v2);    
1139       Poly2.Parameters(Adr2,u,v);
1140       u2+= Param * (u-u2);
1141       v2+= Param * (v-v2);
1142       break;
1143     }
1144   case Intf_FACE:
1145     {
1146       Standard_Real ua,va,ub,vb,uc,vc,ca,cb,cc,cabc;
1147       Poly2.Triangle(Adr1,Pt1,Pt2,Pt3);
1148       gp_Pnt PA(Poly2.Point(Pt1));
1149       gp_Pnt PB(Poly2.Point(Pt2));
1150       gp_Pnt PC(Poly2.Point(Pt3));
1151       Poly2.Parameters(Pt1,ua,va);
1152       Poly2.Parameters(Pt2,ub,vb);
1153       Poly2.Parameters(Pt3,uc,vc);
1154       gp_Vec Normale(gp_Vec(PA,PB).Crossed(gp_Vec(PA,PC)));
1155       cc = (gp_Vec(PA,PB).Crossed(gp_Vec(PA,P))).Dot(Normale);
1156       ca = (gp_Vec(PB,PC).Crossed(gp_Vec(PB,P))).Dot(Normale);
1157       cb = (gp_Vec(PC,PA).Crossed(gp_Vec(PC,P))).Dot(Normale);
1158       cabc = ca + cb + cc;
1159
1160       ca/=cabc;     cb/=cabc;       cc/=cabc;
1161
1162       u2 = ca * ua + cb * ub + cc * uc;
1163       v2 = ca * va + cb * vb + cc * vc;
1164       break;
1165     }
1166   default: 
1167     {
1168       //-- cout<<" Default dans SectionPointToParameters "<<endl;
1169       break;
1170     }
1171   }
1172
1173
1174 //==================================================================================
1175 // function : RemplitLin
1176 // purpose  : 
1177 //==================================================================================
1178 void IntPatch_PrmPrmIntersection::RemplitLin(const Standard_Integer x1,
1179                                              const Standard_Integer y1,
1180                                              const Standard_Integer z1,
1181                                              const Standard_Integer x2,
1182                                              const Standard_Integer y2,
1183                                              const Standard_Integer z2,
1184                                              IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1185 {
1186   int xg,yg,zg;
1187   xg=x1-x2; if(xg<0) xg=-xg; 
1188   yg=y1-y2; if(yg<0) yg=-yg; 
1189   zg=z1-z2; if(zg<0) zg=-zg; 
1190   if(DansGrille(x1) && DansGrille(y1) && DansGrille(z1)) { 
1191     Standard_Integer t = GrilleInteger(x1,y1,z1); 
1192     Map.Add(t);
1193   }
1194   if(xg<=1 && yg<=1 && zg<=1) return;
1195   xg = (x1+x2)>>1;
1196   yg = (y1+y2)>>1;
1197   zg = (z1+z2)>>1;
1198   RemplitLin(x1,y1,z1,xg,yg,zg,Map);
1199   RemplitLin(x2,y2,z2,xg,yg,zg,Map);
1200 }
1201
1202 //==================================================================================
1203 // function : RemplitTri
1204 // purpose  : 
1205 //==================================================================================
1206 void IntPatch_PrmPrmIntersection::RemplitTri(const Standard_Integer x1,
1207                                              const Standard_Integer y1,
1208                                              const Standard_Integer z1,
1209                                              const Standard_Integer x2,
1210                                              const Standard_Integer y2,
1211                                              const Standard_Integer z2,
1212                                              const Standard_Integer x3,
1213                                              const Standard_Integer y3,
1214                                              const Standard_Integer z3,
1215                                              IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1216
1217   if(x1==x2 && x1==x3 && y1==y2 && y1==y3 && z1==z2 && z1==z3) {
1218     if(DansGrille(x1) && DansGrille(y1) && DansGrille(z1)) { 
1219       Standard_Integer t = GrilleInteger(x1,y1,z1); 
1220       Map.Add(t);
1221     }
1222     return;
1223   }
1224   else { 
1225     Standard_Integer xg=(x1+x2+x3)/3;
1226     Standard_Integer yg=(y1+y2+y3)/3;
1227     Standard_Integer zg=(z1+z2+z3)/3;
1228     if(xg==x1 && yg==y1 && zg==z1) { 
1229       RemplitLin(x1,y1,z1,x2,y2,z2,Map);
1230       RemplitLin(x1,y1,z1,x3,y3,z3,Map);
1231       return;
1232     }
1233     if(xg==x2 && yg==y2 && zg==z2) { 
1234       RemplitLin(x2,y2,z2,x1,y1,z1,Map);
1235       RemplitLin(x2,y2,z2,x3,y3,z3,Map);
1236       return;
1237     }
1238     if(xg==x3 && yg==y3 && zg==z3) { 
1239       RemplitLin(x3,y3,z3,x2,y2,z2,Map);
1240       RemplitLin(x3,y3,z3,x1,y1,z1,Map);
1241       return;
1242     }
1243     if(DansGrille(xg) && DansGrille(yg) && DansGrille(zg)) {
1244       Standard_Integer t = GrilleInteger(xg,yg,zg); 
1245       Map.Add(t);
1246     }
1247     if(xg!=x3 || yg!=y3 || zg!=z3) RemplitTri(x1,y1,z1, x2,y2,z2, xg,yg,zg, Map);
1248     if(xg!=x1 || yg!=y1 || zg!=z1) RemplitTri(xg,yg,zg, x2,y2,z2, x3,y3,z3, Map);
1249     if(xg!=x2 || yg!=y2 || zg!=z2) RemplitTri(x1,y1,z1, xg,yg,zg, x3,y3,z3, Map);
1250   }
1251 }
1252
1253 //==================================================================================
1254 // function : Remplit
1255 // purpose  : 
1256 //==================================================================================
1257 void IntPatch_PrmPrmIntersection::Remplit(const Standard_Integer a,
1258                                           const Standard_Integer b,
1259                                           const Standard_Integer c,
1260                                           IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1261
1262   int iax,iay,iaz,ibx,iby,ibz,icx,icy,icz;
1263   if(a!=-1) Map.Add(a);
1264   if(b!=-1) Map.Add(b);
1265   if(c!=-1) Map.Add(c);
1266
1267   if(a!=-1 && b!=-1 && c!=-1 ) { 
1268     IntegerGrille(a,iax,iay,iaz);
1269     IntegerGrille(b,ibx,iby,ibz);
1270     IntegerGrille(c,icx,icy,icz);
1271     RemplitTri(iax,iay,iaz,ibx,iby,ibz,icx,icy,icz,Map);
1272   }
1273 }
1274
1275
1276
1277 //=======================================================================
1278 //function : Perform
1279 //purpose  : 
1280 //=======================================================================
1281 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&    Surf1,
1282                                            const Handle(Adaptor3d_TopolTool)& D1,
1283                                            const Handle(Adaptor3d_HSurface)&    Surf2,
1284                                            const Handle(Adaptor3d_TopolTool)& D2,
1285                                            const Standard_Real   TolTangency,
1286                                            const Standard_Real   Epsilon,
1287                                            const Standard_Real   Deflection,
1288                                            const Standard_Real   Increment,
1289                                            IntSurf_ListOfPntOn2S& LOfPnts,
1290                                            const Standard_Boolean RestrictLine)
1291 {
1292   if (LOfPnts.IsEmpty()){
1293     done = Standard_True;
1294     return;
1295   }
1296
1297   empt = Standard_True;
1298   SLin.Clear();  
1299
1300   Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
1301   Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
1302   Standard_Real U1,U2,V1,V2;
1303
1304   UminLig1 = Surf1->FirstUParameter();
1305   VminLig1 = Surf1->FirstVParameter();
1306   UmaxLig1 = Surf1->LastUParameter();
1307   VmaxLig1 = Surf1->LastVParameter();
1308   UminLig2 = Surf2->FirstUParameter();
1309   VminLig2 = Surf2->FirstVParameter();
1310   UmaxLig2 = Surf2->LastUParameter();
1311   VmaxLig2 = Surf2->LastVParameter();
1312
1313   Standard_Real Periods [4];
1314   Periods[0] = (Surf1->IsUPeriodic())? Surf1->UPeriod() : 0.;
1315   Periods[1] = (Surf1->IsVPeriodic())? Surf1->VPeriod() : 0.;
1316   Periods[2] = (Surf2->IsUPeriodic())? Surf2->UPeriod() : 0.;
1317   Periods[3] = (Surf2->IsVPeriodic())? Surf2->VPeriod() : 0.;
1318
1319   IntSurf_ListIteratorOfListOfPntOn2S IterLOP1(LOfPnts);
1320
1321   for(; IterLOP1.More(); IterLOP1.Next()){
1322     IntSurf_PntOn2S Pnt = IterLOP1.Value();
1323     Pnt.Parameters(U1, V1, U2, V2);
1324     if(U1>UmaxLig1) UmaxLig1=U1;
1325     if(V1>VmaxLig1) VmaxLig1=V1;
1326     if(U2>UmaxLig2) UmaxLig2=U2;
1327     if(V2>VmaxLig2) VmaxLig2=V2;
1328
1329     if(U1<UminLig1) UminLig1=U1;
1330     if(V1<VminLig1) VminLig1=V1;
1331     if(U2<UminLig2) UminLig2=U2;
1332     if(V2<VminLig2) VminLig2=V2; 
1333   }
1334
1335   Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
1336
1337   Standard_Integer NbLigCalculee = 0, ver;
1338   Standard_Real pu1,pu2,pv1,pv2, dminiPointLigne;
1339   Standard_Boolean HasStartPoint,RejetLigne;
1340   IntSurf_PntOn2S StartPOn2S;
1341   gp_Pnt Point3dDebut,Point3dFin;
1342
1343   TColStd_Array1OfReal StartParams(1,4);
1344   IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);  
1345
1346   IntSurf_ListIteratorOfListOfPntOn2S IterLOP2(LOfPnts);
1347   for(; IterLOP2.More(); IterLOP2.Next() ){
1348
1349     IntSurf_PntOn2S cPnt = IterLOP2.Value();
1350     cPnt.Parameters(U1, V1, U2, V2);
1351
1352     StartParams(1) = U1;
1353     StartParams(2) = V1;
1354     StartParams(3) = U2;
1355     StartParams(4) = V2;
1356
1357     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
1358     dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
1359     if(HasStartPoint) {
1360       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
1361       NbLigCalculee = SLin.Length();
1362       Standard_Integer l;
1363       for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
1364         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
1365
1366         if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
1367           dminiPointLigne = 0.0;
1368         }
1369       }// for( l ...
1370
1371       if(dminiPointLigne > SeuildPointLigne) {
1372         PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
1373         if(PW.IsDone()) {
1374           if(PW.NbPoints()>2)
1375           {
1376             //Try to extend the intersection line to boundary, if it is possibly
1377             Standard_Boolean hasBeenAdded = PW.PutToBoundary(Surf1, Surf2);
1378
1379             RejetLigne = Standard_False;
1380             Point3dDebut = PW.Value(1).Value();
1381             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
1382             Point3dFin   = PointFin.Value();
1383             for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
1384               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
1385
1386               // Check end point if it is on existing line.
1387               // Start point is checked before.
1388               if (IsPointOnLine(PointFin, verwline, Deflection)) {
1389                 RejetLigne = Standard_True; 
1390                 break;
1391               }
1392
1393               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
1394               const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
1395               if(Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) { 
1396                 if(Point3dFin.Distance(verPointFin.Value()) <= TolTangency)
1397                   RejetLigne = Standard_True; 
1398               }
1399             }
1400
1401             if(!RejetLigne) {
1402               IntSurf_TypeTrans trans1,trans2;
1403               Standard_Real locu,locv;
1404               gp_Vec norm1,norm2,d1u,d1v;
1405               gp_Pnt ptbid;
1406               Standard_Integer indextg;
1407               gp_Vec tgline(PW.TangentAtLine(indextg));
1408               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1409               Surf1->D1(locu,locv,ptbid,d1u,d1v);
1410               norm1 = d1u.Crossed(d1v);
1411               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1412               Surf2->D1(locu,locv,ptbid,d1u,d1v);
1413               norm2 = d1u.Crossed(d1v);
1414               if( tgline.DotCross(norm2,norm1) >= 0. ) {
1415                 trans1 = IntSurf_Out;
1416                 trans2 = IntSurf_In;
1417               }
1418               else {
1419                 trans1 = IntSurf_In;
1420                 trans2 = IntSurf_Out;
1421               }
1422
1423               Standard_Real TolTang = TolTangency;
1424               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1425               if (RestrictLine){
1426                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang,hasBeenAdded);
1427                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang,hasBeenAdded);
1428               }
1429
1430               if(wline->NbVertex() == 0) {
1431                 IntPatch_Point vtx;
1432                 IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1433                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1434                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1435                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1436                 vtx.SetParameter(1);
1437                 wline->AddVertex(vtx);
1438
1439                 POn2S = PW.Line()->Value(wline->NbPnts());
1440                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1441                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
1442                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1443                 vtx.SetParameter(wline->NbPnts());
1444                 wline->AddVertex(vtx);
1445               }              
1446
1447               Standard_Integer slinlen = SLin.Length();
1448               if( slinlen > 0 ) {
1449                 Standard_Integer cnbV = wline->NbVertex();
1450                 Standard_Integer ciV;
1451                 for( ciV = 1; ciV <= cnbV; ciV++ ) {
1452                   Standard_Real pntDMin = 1.e+100;
1453                   Standard_Integer VDMin = 0;
1454                   Standard_Integer WLDMin = 0;
1455                   gp_Pnt cPV = wline->Vertex(ciV).Value();
1456                   Standard_Integer iL;
1457                   for( iL = 1; iL <= slinlen; iL++) {
1458                     const Handle(IntPatch_Line)& aSLine = SLin.Value(iL);
1459                     IntPatch_IType aType = aSLine->ArcType();
1460                     if( aType != IntPatch_Walking)
1461                       continue;
1462                     const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
1463                     Standard_Integer tnbV = aWLine->NbVertex();
1464                     Standard_Integer tiV;
1465                     for( tiV = 1; tiV <= tnbV; tiV++ ) {
1466                       gp_Pnt tPV = aWLine->Vertex(tiV).Value();
1467                       Standard_Real tDistance = cPV.Distance(tPV);
1468                       Standard_Real uRs1 = Surf1->Surface().UResolution(tDistance);
1469                       Standard_Real vRs1 = Surf1->Surface().VResolution(tDistance);
1470                       Standard_Real uRs2 = Surf2->Surface().UResolution(tDistance);
1471                       Standard_Real vRs2 = Surf2->Surface().VResolution(tDistance);
1472                       Standard_Real RmaxS1 = Max(uRs1,vRs1);
1473                       Standard_Real RmaxS2 = Max(uRs2,vRs2);
1474                       if(RmaxS1 < 1.e-4 && RmaxS2 < 1.e-4) {
1475                         if( pntDMin > tDistance && tDistance > 1.e-9) {
1476                           pntDMin = tDistance;
1477                           VDMin = tiV;
1478                           WLDMin = iL;
1479                         }
1480                       }
1481                     }
1482                   }
1483
1484                   if( VDMin != 0 ) {
1485                     const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
1486                     const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
1487                     Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
1488                     Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
1489                     Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
1490                     Standard_Real u21 = 0., u22 = 0., v21 = 0., v22 = 0.;
1491                     wline->Point(ciVpar).Parameters(u11,v11,u12,v12);
1492                     aWLine->Point(tiVpar).Parameters(u21,v21,u22,v22);
1493
1494                     Handle(IntSurf_LineOn2S) newL2s = new IntSurf_LineOn2S();
1495                     IntSurf_PntOn2S replacePnt = aWLine->Point(tiVpar);
1496                     Standard_Integer cNbP = wline->NbPnts();
1497
1498                     TColStd_SequenceOfInteger VPold;
1499                     Standard_Integer iPo;
1500                     for( iPo = 1; iPo <= cnbV; iPo++ ) {
1501                       Standard_Real Po = wline->Vertex(iPo).ParameterOnLine();
1502                       Standard_Integer IPo = (Standard_Integer) Po;
1503                       VPold.Append(IPo);
1504                     }
1505
1506                     Standard_Boolean removeNext = Standard_False;
1507                     Standard_Boolean removePrev = Standard_False;
1508                     if( ciV == 1) {
1509                       Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
1510                       if(dPar > 10) {
1511                         removeNext = Standard_True;
1512                         for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
1513                           VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1514                       }
1515                     }
1516                     else if( ciV == cnbV) {
1517                       Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
1518                       if(dPar > 10) {
1519                         removePrev = Standard_True;
1520                         VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
1521                       }
1522                     }
1523                     else {
1524                       Standard_Integer dParMi = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
1525                       Standard_Integer dParMa = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
1526                       if(dParMi > 10) {
1527                         removePrev = Standard_True;
1528                         VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
1529                       }
1530                       if(dParMa > 10) {
1531                         removeNext = Standard_True;
1532                         for( iPo = (ciV+1); iPo <= cnbV; iPo++ ) {
1533                           if(dParMi > 10)
1534                             VPold.SetValue(iPo, VPold.Value(iPo) - 2 );
1535                           else
1536                             VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1537                         }
1538                       }
1539                       else {
1540                         if(dParMi > 10)
1541                           for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
1542                             VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1543                       } 
1544                     }
1545                     Standard_Integer pI = (Standard_Integer) ciVpar;
1546
1547                     Standard_Integer iP;
1548                     for( iP = 1; iP <= cNbP; iP++) {
1549                       if( pI == iP )
1550                       {
1551                         IntSurf_PntOn2S newPnt = MakeNewPoint(replacePnt, wline->Point(iP), Periods);
1552                         newL2s->Add(newPnt);
1553                       }
1554                       else if(removeNext && iP == (pI + 1))
1555                         continue;
1556                       else if(removePrev && iP == (pI - 1))
1557                         continue;
1558                       else
1559                         newL2s->Add(wline->Point(iP));
1560                     }
1561
1562                     IntPatch_Point newVtx;
1563                     gp_Pnt Pnt3dV = aWLine->Vertex(VDMin).Value();
1564                     newVtx.SetValue(Pnt3dV,TolTang,Standard_False);
1565                     newVtx.SetParameters(u21,v21,u22,v22);
1566                     newVtx.SetParameter(VPold.Value(ciV));
1567
1568                     Handle(IntPatch_WLine) NWLine = new IntPatch_WLine(newL2s,Standard_False,trans1,trans2);
1569
1570                     Standard_Integer iV;
1571                     for( iV = 1; iV <= cnbV; iV++ ) {
1572                       if( iV == ciV )
1573                         NWLine->AddVertex(newVtx);
1574                       else {
1575                         IntPatch_Point theVtx = wline->Vertex(iV);
1576                         theVtx.SetParameter(VPold.Value(iV));
1577                         NWLine->AddVertex(theVtx);
1578                       }
1579                     }
1580
1581                     wline = NWLine;
1582                   }
1583                 }
1584               }// SLin.Length > 0
1585
1586               AddWLine(SLin, wline, Deflection);
1587               empt = Standard_False;
1588             }// !RejetLigne
1589           }// PW points > 2
1590         }// done is True
1591       }// dminiPointLigne > SeuildPointLigne
1592     }// HasStartPoint  
1593   }// for( IterLOP ...
1594   done = Standard_True;
1595   return;      
1596 }
1597 //=======================================================================
1598 //function : Perform
1599 //purpose  : 
1600 //=======================================================================
1601 void IntPatch_PrmPrmIntersection::Perform(const Handle(Adaptor3d_HSurface)&    Surf1,
1602                                           const Handle(Adaptor3d_TopolTool)& D1,
1603                                           const Handle(Adaptor3d_HSurface)&    Surf2,
1604                                           const Handle(Adaptor3d_TopolTool)& D2,
1605                                           const Standard_Real   U1Depart,
1606                                           const Standard_Real   V1Depart,
1607                                           const Standard_Real   U2Depart,
1608                                           const Standard_Real   V2Depart,
1609                                           const Standard_Real   TolTangency,
1610                                           const Standard_Real   Epsilon,
1611                                           const Standard_Real   Deflection,
1612                                           const Standard_Real   Increment)
1613 {
1614   //    Standard_Integer NbU1 = D1->NbSamplesU();
1615   //    Standard_Integer NbV1 = D1->NbSamplesV();
1616   //    Standard_Integer NbU2 = D2->NbSamplesU();
1617   //    Standard_Integer NbV2 = D2->NbSamplesV();
1618
1619   //-- Traitement des Lignes de sections
1620   empt = Standard_True;
1621   done = Standard_True;
1622   SLin.Clear();  
1623
1624   //------------------------------------------------------------
1625
1626   Standard_Real pu1,pu2,pv1,pv2;
1627
1628   TColStd_Array1OfReal StartParams(1,4);
1629
1630   //    Standard_Integer MaxOscill = NbU1;
1631   //    if(MaxOscill < NbU2) MaxOscill=NbU2;
1632   //    if(MaxOscill < NbV1) MaxOscill=NbV1;
1633   //    if(MaxOscill < NbV2) MaxOscill=NbV2;
1634
1635   //    Standard_Real nIncrement=Increment;
1636   //    if(MaxOscill>10) { 
1637   //  #ifdef DEB 
1638   //      cout<<"\n IntPatch_PrmPrmIntersection.gxx : Increment:"<<Increment<<" -> "<<Increment/(0.5*MaxOscill)<<endl;
1639   //  #endif
1640   //      nIncrement/=0.5*MaxOscill;
1641   //    }
1642
1643   IntPatch_ThePWalkingInter PW(Surf1,Surf2,
1644     TolTangency,
1645     Epsilon,
1646     Deflection,
1647     Increment); //nIncrement);
1648
1649
1650   //Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
1651   //Standard_Real    incidence;
1652   //Standard_Real    dminiPointLigne;
1653
1654   Standard_Boolean HasStartPoint;//,RejetLigne;
1655
1656   IntSurf_PntOn2S StartPOn2S;
1657
1658   //Standard_Integer ver;
1659
1660   gp_Pnt Point3dDebut,Point3dFin;
1661
1662   //------------------------------------------------------------
1663
1664   StartParams(1) = U1Depart;
1665   StartParams(2) = V1Depart;
1666   StartParams(3) = U2Depart;
1667   StartParams(4) = V2Depart;
1668
1669   //-----------------------------------------------------------------------
1670   //-- Calcul du premier point de cheminement a partir du point approche --
1671   //-----------------------------------------------------------------------
1672   HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S); 
1673   if(HasStartPoint) { 
1674     //-------------------------------------------------
1675     //-- Un point a ete trouve                       --
1676     //-- On verifie qu il n appartient pas           --
1677     //--  a une ligne de cheminement deja calculee.  --
1678     //-------------------------------------------------
1679
1680     PW.Perform(StartParams);
1681     if(PW.IsDone()) {
1682
1683       Point3dDebut = PW.Value(1).Value();
1684       Point3dFin   = PW.Value(PW.NbPoints()).Value();
1685
1686       IntSurf_TypeTrans trans1,trans2;
1687       Standard_Real locu,locv;
1688       gp_Vec norm1,norm2,d1u,d1v;
1689       gp_Pnt ptbid;
1690       Standard_Integer indextg;
1691       gp_Vec tgline(PW.TangentAtLine(indextg));
1692       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1693       Surf1->D1(locu,locv,ptbid,d1u,d1v);
1694       norm1 = d1u.Crossed(d1v);
1695       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1696       Surf2->D1(locu,locv,ptbid,d1u,d1v);
1697       norm2 = d1u.Crossed(d1v);
1698       if (tgline.DotCross(norm2,norm1)>0.) {
1699         trans1 = IntSurf_Out;
1700         trans2 = IntSurf_In;
1701       }
1702       else {
1703         trans1 = IntSurf_In;
1704         trans2 = IntSurf_Out;
1705       }
1706
1707
1708
1709       Standard_Real TolTang = TolTangency;
1710       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1711       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1712       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1713
1714       //---------------
1715       if(wline->NbVertex() == 0) {
1716         IntPatch_Point vtx;
1717         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1718         POn2S.Parameters(pu1,pv1,pu2,pv2);
1719         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1720         vtx.SetParameters(pu1,pv1,pu2,pv2);
1721         vtx.SetParameter(1);
1722         wline->AddVertex(vtx);
1723
1724         POn2S = PW.Line()->Value(wline->NbPnts());
1725         POn2S.Parameters(pu1,pv1,pu2,pv2);
1726         vtx.SetValue(Point3dFin,TolTang,Standard_False);
1727         vtx.SetParameters(pu1,pv1,pu2,pv2);
1728         vtx.SetParameter(wline->NbPnts());
1729         wline->AddVertex(vtx);
1730       }
1731
1732       //---------------
1733       SLin.Append(wline);
1734       empt = Standard_False;
1735
1736     }
1737   }
1738 }
1739 //==================================================================================
1740 // function : AdjustOnPeriodic
1741 // purpose  : 
1742 //==================================================================================
1743 void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
1744                       const Handle(Adaptor3d_HSurface)& Surf2,
1745                       IntPatch_SequenceOfLine& aSLin)
1746 {
1747   Standard_Boolean bIsPeriodic[4], bModified, bIsNull, bIsPeriod;
1748   Standard_Integer i, j, k, aNbLines, aNbPx, aIndx, aIndq;
1749   Standard_Real aPeriod[4], dPeriod[4], ux[4], uq[4], aEps, du;
1750   //
1751   aEps=Precision::Confusion();
1752   //
1753   for (k=0; k<4; ++k) {
1754     aPeriod[k]=0.;
1755   }
1756   //
1757   bIsPeriodic[0]=Surf1->IsUPeriodic();
1758   bIsPeriodic[1]=Surf1->IsVPeriodic();
1759   bIsPeriodic[2]=Surf2->IsUPeriodic();
1760   bIsPeriodic[3]=Surf2->IsVPeriodic();
1761   //
1762   if (bIsPeriodic[0]){
1763     aPeriod[0]=Surf1->UPeriod();
1764   }
1765   if (bIsPeriodic[1]){
1766     aPeriod[1]=Surf1->VPeriod();
1767   }
1768   if (bIsPeriodic[2]){
1769     aPeriod[2]=Surf2->UPeriod();
1770   }
1771   if (bIsPeriodic[3]){
1772     aPeriod[3]=Surf2->VPeriod();
1773   }
1774   //
1775   for (k=0; k<4; ++k) {
1776     dPeriod[k]=0.25*aPeriod[k];
1777   }
1778   //
1779   aNbLines=aSLin.Length();
1780   for (i=1; i<=aNbLines; ++i) {
1781     Handle(IntPatch_WLine) aIL=Handle(IntPatch_WLine)::DownCast(aSLin.Value(i));
1782     Handle(IntSurf_LineOn2S) aL=aIL->Curve();
1783
1784     aNbPx=aL->NbPoints();
1785     if (aNbPx<10) {
1786       continue;
1787     }
1788     //
1789     for (j=0; j<2; ++j) {
1790       bModified=Standard_False;
1791       aIndx=1;
1792       aIndq=2;
1793       if (j) {
1794         aIndx=aNbPx;
1795         aIndq=aNbPx-1;
1796       }
1797       //
1798       const IntSurf_PntOn2S& aPSx=aL->Value(aIndx);
1799       const IntSurf_PntOn2S& aPSq=aL->Value(aIndq);
1800       //
1801       aPSx.Parameters(ux[0], ux[1], ux[2], ux[3]);
1802       aPSq.Parameters(uq[0], uq[1], uq[2], uq[3]);
1803       //
1804       for (k=0; k<4; ++k) {
1805         bIsNull=Standard_False;
1806         bIsPeriod=Standard_False;
1807         //
1808         if (!bIsPeriodic[k]) {
1809           continue;
1810         }
1811         //
1812         if (fabs(ux[k])<aEps) {
1813           bModified=Standard_True;
1814           bIsNull=Standard_True;
1815         }
1816         //
1817         else if (fabs(ux[k]-aPeriod[k])<aEps) {
1818           bModified=Standard_True;
1819           bIsPeriod=Standard_True;
1820         }
1821         //
1822         if (bModified) {
1823           du=fabs(ux[k]-uq[k]);
1824           if (du > dPeriod[k]) {
1825             if(bIsNull){
1826               ux[k]=aPeriod[k];
1827             }
1828             if(bIsPeriod) {
1829               ux[k]=0.;
1830             }
1831           }
1832         }
1833       }//for (k=0; k<4; ++k) 
1834       if (bModified) {
1835         IntSurf_PntOn2S aPntOn2S;
1836         //
1837         aPntOn2S=aPSx;  
1838         aPntOn2S.SetValue(ux[0], ux[1], ux[2], ux[3]);
1839         aL->Value(aIndx, aPntOn2S);
1840       }
1841     }//for (j=0; j<1; ++j) {
1842   }//for (i=1; i<=aNbLines; ++i)
1843 }
1844
1845 //==================================================================================
1846 // function : MakeNewPoint
1847 // purpose  : 
1848 //==================================================================================
1849 IntSurf_PntOn2S MakeNewPoint(const IntSurf_PntOn2S& replacePnt,
1850                              const IntSurf_PntOn2S& oldPnt,
1851                              const Standard_Real* Periods)
1852 {
1853   IntSurf_PntOn2S NewPoint;
1854   NewPoint.SetValue(replacePnt.Value());
1855
1856   Standard_Real OldParams[4], NewParams[4];
1857   oldPnt.Parameters(OldParams[0], OldParams[1], OldParams[2], OldParams[3]);
1858   replacePnt.Parameters(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
1859
1860   Standard_Integer i;
1861   for (i = 0; i < 4; i++)
1862     if (Periods[i] != 0.)
1863     {
1864       if (Abs(NewParams[i] - OldParams[i]) >= 0.5*Periods[i])
1865       {
1866         if (NewParams[i] < OldParams[i])
1867           NewParams[i] += Periods[i];
1868         else
1869           NewParams[i] -= Periods[i];
1870       }
1871     }
1872
1873     NewPoint.SetValue(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
1874     return NewPoint;
1875 }
1876
1877 //==================================================================================
1878 // function : Perform
1879 // purpose  : base SS Int. function
1880 //==================================================================================
1881 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
1882                                            const Handle(Adaptor3d_TopolTool)& D1,
1883                                            const Handle(Adaptor3d_HSurface)& Surf2,
1884                                            const Handle(Adaptor3d_TopolTool)& D2,
1885                                            const Standard_Real   TolTangency,
1886                                            const Standard_Real   Epsilon,
1887                                            const Standard_Real   Deflection,
1888                                            const Standard_Real   Increment,
1889                                            const Standard_Boolean ClearFlag) 
1890 {
1891   Standard_Integer Limit = 2500;
1892   Standard_Integer NbU1 = 10, NbV1 = 10, NbU2 = 10, NbV2 = 10;
1893   //
1894   D1->SamplePnts(Deflection, NbU1, NbV1);
1895   D2->SamplePnts(Deflection, NbU2, NbV2);
1896   //
1897   NbU1 = D1->NbSamplesU();
1898   NbV1 = D1->NbSamplesV();
1899   NbU2 = D2->NbSamplesU();
1900   NbV2 = D2->NbSamplesV();
1901
1902   TColStd_Array1OfReal anUpars1(1, NbU1), aVpars1(1, NbV1);
1903   TColStd_Array1OfReal anUpars2(1, NbU2), aVpars2(1, NbV2);
1904   //
1905   D1->UParameters(anUpars1); 
1906   D1->VParameters(aVpars1);
1907   D2->UParameters(anUpars2); 
1908   D2->VParameters(aVpars2);
1909
1910   Standard_Real Periods [4];
1911   Periods[0] = (Surf1->IsUPeriodic())? Surf1->UPeriod() : 0.;
1912   Periods[1] = (Surf1->IsVPeriodic())? Surf1->VPeriod() : 0.;
1913   Periods[2] = (Surf2->IsUPeriodic())? Surf2->UPeriod() : 0.;
1914   Periods[3] = (Surf2->IsVPeriodic())? Surf2->VPeriod() : 0.;
1915
1916   //---------------------------------------------
1917   if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit))
1918   {
1919     empt = Standard_True;
1920     if (ClearFlag)
1921     {
1922       SLin.Clear();
1923     }
1924     //
1925     IntPolyh_Intersection* pInterference = NULL;
1926
1927     if ( D1->IsUniformSampling() || D2->IsUniformSampling() )
1928     {
1929       pInterference = new IntPolyh_Intersection(Surf1,NbU1,NbV1,Surf2,NbU2,NbV2);
1930     }
1931     else
1932     {
1933       pInterference = new IntPolyh_Intersection(Surf1, anUpars1, aVpars1, 
1934         Surf2, anUpars2, aVpars2 );
1935     }
1936
1937     if ( !pInterference )
1938     {
1939       done = Standard_False;
1940       return;
1941     }
1942     //
1943     IntPolyh_Intersection& Interference = *pInterference;
1944     //
1945     done = Interference.IsDone();
1946     if( !done )
1947     {
1948       if (pInterference)
1949       {
1950         delete pInterference;
1951         pInterference = NULL;
1952       }
1953
1954       return;
1955     }
1956
1957     Standard_Integer nbLigSec = Interference.NbSectionLines();
1958     Standard_Integer nbTanZon = Interference.NbTangentZones();
1959     Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
1960
1961     Standard_Integer NbLigCalculee = 0, ver;
1962     Standard_Real pu1,pu2,pv1,pv2, incidence, dminiPointLigne;
1963     Standard_Boolean HasStartPoint = Standard_False, RejectLine = Standard_False;
1964     IntSurf_PntOn2S StartPOn2S;
1965     gp_Pnt Point3dDebut,Point3dFin;
1966
1967     TColStd_Array1OfReal StartParams(1,4);
1968     IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);
1969
1970     if(nbLigSec>=1)
1971     {
1972       Standard_Integer *TabL = new Standard_Integer [nbLigSec+1];
1973       for(Standard_Integer ls=1; ls<=nbLigSec; ++ls)
1974       {
1975         TabL[ls]=ls;
1976       }
1977       //----------------------------------------1.1
1978       {
1979         Standard_Boolean triok;
1980         Standard_Integer nb_A, nb_B, tyu;
1981         do
1982         {
1983           triok=Standard_True;
1984           for(Standard_Integer b = 2; b <= nbLigSec; ++b )
1985           {
1986             nb_B = Interference.NbPointsInLine(TabL[b]);
1987             nb_A = Interference.NbPointsInLine(TabL[b-1]);
1988
1989             if( nb_B > nb_A )
1990             {
1991               tyu=TabL[b];
1992               TabL[b]=TabL[b-1];
1993               TabL[b-1]=tyu;
1994               triok=Standard_False;
1995             }
1996           }
1997         }
1998         while(triok==Standard_False);
1999       }
2000
2001       //----------------------------------------
2002       // 1.2 For the line "ls" get 2D-bounds U,V for surfaces 1,2
2003       //
2004       for(Standard_Integer ls = 1; ls <= nbLigSec; ++ls)
2005       {
2006         Standard_Integer nbp = Interference.NbPointsInLine(TabL[ls]);
2007         if (!nbp)
2008         {
2009           continue;
2010         }
2011         //
2012         Standard_Integer *TabPtDep = new Standard_Integer [nbp+1];
2013         for(Standard_Integer ilig = 1; ilig <= nbp; ++ilig )
2014         {
2015           TabPtDep[ilig]=0;
2016         }
2017         //
2018         Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2019         Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2020         Standard_Real _x,_y,_z;
2021         //
2022         Interference.GetLinePoint(TabL[ls], 1, _x, _y, _z,
2023           UminLig1, VminLig1, UminLig2, VminLig2, 
2024           incidence);
2025
2026         UmaxLig1=UminLig1;
2027         VmaxLig1=VminLig1;
2028         UmaxLig2=UminLig2;
2029         VmaxLig2=VminLig2;
2030         //
2031         for(Standard_Integer ilig = 2; ilig <= nbp; ilig++ )
2032         {
2033           Standard_Real U1, U2, V1, V2;
2034           Interference.GetLinePoint(TabL[ls],ilig,_x,_y,_z,U1,V1,U2,V2,incidence);
2035           //
2036           if(U1>UmaxLig1) UmaxLig1=U1;
2037           if(V1>VmaxLig1) VmaxLig1=V1;
2038           if(U2>UmaxLig2) UmaxLig2=U2;
2039           if(V2>VmaxLig2) VmaxLig2=V2;
2040           //
2041           if(U1<UminLig1) UminLig1=U1;
2042           if(V1<VminLig1) VminLig1=V1;
2043           if(U2<UminLig2) UminLig2=U2;
2044           if(V2<VminLig2) VminLig2=V2;
2045         }//for( ilig = 2; ilig <= nbp; ilig++ ) { 
2046         //
2047         //----------------------------------------
2048         // 1.3
2049         Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
2050         Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
2051         Standard_Boolean lignetrouvee=Standard_False;
2052         const Standard_Integer NbDePointsDeDepartDuChmLimit = 5;
2053         //
2054         do
2055         {
2056           NombreDePointsDeDepartDuCheminement++;
2057           switch (NombreDePointsDeDepartDuCheminement)
2058           {
2059           case 1:
2060             nbps2 = (nbp > 1) ? nbp/2 : 1;
2061             if(nbp<3) 
2062               NombreDePointsDeDepartDuCheminement = NbDePointsDeDepartDuChmLimit;
2063
2064             break;
2065           case 2:
2066             nbps2 = 1;
2067             break;
2068           case 3:
2069             nbps2 = nbp-1;
2070             break;
2071
2072           case 4:
2073             nbps2 = 3 * nbp / 4;
2074             break;
2075
2076           case 5:
2077             nbps2 = nbp / 4;
2078             break;
2079           default:
2080             nbps2 = NombreDePointsDeDepartDuCheminement-3;
2081             NombreDePointsDeDepartDuCheminement++;
2082           }
2083
2084           //
2085           if(TabPtDep[nbps2] == 0)
2086           {
2087             Standard_Real U1, U2, V1, V2;
2088
2089             TabPtDep[nbps2] = 1;
2090             Interference.GetLinePoint(TabL[ls],nbps2,_x,_y,_z,U1,V1,U2,V2,incidence);
2091
2092             StartParams(1) = U1;
2093             StartParams(2) = V1;
2094             StartParams(3) = U2;
2095             StartParams(4) = V2;
2096
2097             HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
2098             dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
2099             if(HasStartPoint)
2100             {
2101               StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2102               NbLigCalculee = SLin.Length();
2103               Standard_Integer l;
2104               for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++)
2105               {
2106                 const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2107
2108                 if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2109                 {
2110                   dminiPointLigne = 0.0;
2111                 }
2112               }// for( l ...
2113
2114               if(dminiPointLigne > SeuildPointLigne)
2115               {
2116                 PW.Perform(StartParams, UminLig1, VminLig1, UminLig2, VminLig2,
2117                   UmaxLig1, VmaxLig1, UmaxLig2, VmaxLig2);
2118
2119                 //
2120                 Standard_Boolean bPWIsDone;
2121                 Standard_Integer iPWNbPoints, aNbPointsVer;
2122                 Standard_Real aD11, aD12, aD21, aD22, aDx;
2123                 //
2124                 bPWIsDone=PW.IsDone();
2125
2126                 if(bPWIsDone)
2127                 {
2128                   iPWNbPoints=PW.NbPoints();
2129                   //
2130                   if( iPWNbPoints > 2 )
2131                   {
2132                     //Try to extend the intersection line to boundary, if it is possibly
2133                     Standard_Boolean hasBeenAdded = PW.PutToBoundary(Surf1, Surf2);
2134
2135                     const Standard_Integer aMinNbPoints = 40;
2136                     if(iPWNbPoints < aMinNbPoints)
2137                     {
2138                       hasBeenAdded = 
2139                         PW.SeekAdditionalPoints(Surf1, Surf2, aMinNbPoints) || hasBeenAdded;
2140                       iPWNbPoints = PW.NbPoints();
2141                     }
2142                     
2143                     RejectLine = Standard_False;
2144                     Point3dDebut = PW.Value(1).Value();
2145                     Point3dFin   = PW.Value(iPWNbPoints).Value();
2146                     for( ver = 1; (!RejectLine) && (ver<= NbLigCalculee); ++ver)
2147                     {
2148                       const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2149                       //
2150                       aNbPointsVer=verwline->NbPnts();
2151                       if (aNbPointsVer<3)
2152                       {
2153                         continue;
2154                       }
2155                       //
2156                       const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2157                       const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
2158                       //xf
2159                       const gp_Pnt& aP21=verPointDebut.Value();
2160                       const gp_Pnt& aP22=verPointFin.Value();
2161                       //
2162                       aD11=Point3dDebut.Distance(aP21);
2163                       aD12=Point3dDebut.Distance(aP22);
2164                       aD21=Point3dFin.Distance(aP21);
2165                       aD22=Point3dFin.Distance(aP22);
2166                       //
2167                       if((aD11<=TolTangency && aD22<=TolTangency) ||
2168                         (aD12<=TolTangency && aD21<=TolTangency))
2169                       {
2170                         Standard_Integer m, mx;
2171                         //
2172                         mx=aNbPointsVer/2;
2173                         if (aNbPointsVer%2)
2174                         {
2175                           ++mx; 
2176                         }
2177                         //
2178                         const gp_Pnt& aPx=verwline->Point(mx).Value();
2179                         for(m=1; m<iPWNbPoints; ++m)
2180                         {
2181                           const gp_Pnt& aP1=PW.Value(m).Value();
2182                           const gp_Pnt& aP2=PW.Value(m+1).Value();
2183                           gp_Vec aVec12(aP1, aP2);
2184                           if (aVec12.SquareMagnitude()<1.e-20)
2185                           {
2186                             continue;
2187                           }
2188
2189                           //
2190                           gp_Dir aDir12(aVec12);
2191                           gp_Lin aLin12(aP1, aDir12);
2192                           aDx=aLin12.Distance(aPx);
2193
2194                           //modified by NIZNHY-PKV Tue May 10 11:08:07 2011f
2195                           if (aDx<=2.*Epsilon)
2196                           {
2197                             //if (aDx<=TolTangency) {
2198                             //modified by NIZNHY-PKV Tue May 10 11:08:13 2011t
2199
2200                             RejectLine = Standard_True;
2201                             break;
2202                           }
2203                         }//for(m=1; m<iPWNbPoints; ++m){
2204                       }//if((aD11<=TolTangency && aD22<=TolTangency) ||...
2205                     }// for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2206                     //
2207
2208                     if(!RejectLine)
2209                     {
2210                       IntSurf_TypeTrans trans1,trans2;
2211                       Standard_Real locu,locv;
2212                       gp_Vec norm1,norm2,d1u,d1v;
2213                       gp_Pnt ptbid;
2214                       Standard_Integer indextg;
2215                       gp_Vec tgline(PW.TangentAtLine(indextg));
2216                       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2217                       Surf1->D1(locu,locv,ptbid,d1u,d1v);
2218                       norm1 = d1u.Crossed(d1v);
2219                       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2220                       Surf2->D1(locu,locv,ptbid,d1u,d1v);
2221                       norm2 = d1u.Crossed(d1v);
2222                       if( tgline.DotCross(norm2,norm1) >= 0. )
2223                       {
2224                         trans1 = IntSurf_Out;
2225                         trans2 = IntSurf_In;
2226                       }
2227                       else
2228                       {
2229                         trans1 = IntSurf_In;
2230                         trans2 = IntSurf_Out;
2231                       }
2232
2233                       Standard_Real TolTang = TolTangency;
2234                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2235                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang,hasBeenAdded);
2236                       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang,hasBeenAdded);
2237
2238                       if(wline->NbVertex() == 0)
2239                       {
2240                         IntPatch_Point vtx;
2241                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2242                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2243                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2244                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2245                         vtx.SetParameter(1);
2246                         wline->AddVertex(vtx);
2247
2248                         POn2S = PW.Line()->Value(wline->NbPnts());
2249                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2250                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
2251                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2252                         vtx.SetParameter(wline->NbPnts());
2253                         wline->AddVertex(vtx);
2254                       }
2255
2256                       lignetrouvee = Standard_True;
2257
2258                       Standard_Integer slinlen = SLin.Length();
2259                       if( slinlen > 0 )
2260                       {
2261                         Standard_Integer cnbV = wline->NbVertex();
2262                         Standard_Integer ciV;
2263                         for( ciV = 1; ciV <= cnbV; ciV++ )
2264                         {
2265                           Standard_Real pntDMin = 1.e+100;
2266                           Standard_Integer VDMin = 0;
2267                           Standard_Integer WLDMin = 0;
2268                           gp_Pnt cPV = wline->Vertex(ciV).Value();
2269                           Standard_Integer iL;
2270                           for( iL = 1; iL <= slinlen; iL++)
2271                           {
2272                             const Handle(IntPatch_Line)& aSLine = SLin.Value(iL);
2273                             IntPatch_IType aType = aSLine->ArcType();
2274                             if( aType != IntPatch_Walking)
2275                               continue;
2276                             const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
2277                             Standard_Integer tnbV = aWLine->NbVertex();
2278                             Standard_Integer tiV;
2279                             for( tiV = 1; tiV <= tnbV; tiV++ )
2280                             {
2281                               gp_Pnt tPV = aWLine->Vertex(tiV).Value();
2282                               Standard_Real tDistance = cPV.Distance(tPV);
2283                               Standard_Real uRs1 = Surf1->Surface().UResolution(tDistance);
2284                               Standard_Real vRs1 = Surf1->Surface().VResolution(tDistance);
2285                               Standard_Real uRs2 = Surf2->Surface().UResolution(tDistance);
2286                               Standard_Real vRs2 = Surf2->Surface().VResolution(tDistance);
2287                               Standard_Real RmaxS1 = Max(uRs1,vRs1);
2288                               Standard_Real RmaxS2 = Max(uRs2,vRs2);
2289                               if(RmaxS1 < 1.e-4 && RmaxS2 < 1.e-4)
2290                               {
2291                                 if( pntDMin > tDistance && tDistance > 1.e-9)
2292                                 {
2293                                   pntDMin = tDistance;
2294                                   VDMin = tiV;
2295                                   WLDMin = iL;
2296                                 }
2297                               }
2298                             }
2299                           }
2300
2301                           if( VDMin != 0 )
2302                           {
2303                             const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
2304                             const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
2305                             Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
2306                             Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
2307                             Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
2308                             Standard_Real u21 = 0., u22 = 0., v21 = 0., v22 = 0.;
2309                             wline->Point(ciVpar).Parameters(u11,v11,u12,v12);
2310                             aWLine->Point(tiVpar).Parameters(u21,v21,u22,v22);
2311
2312                             Handle(IntSurf_LineOn2S) newL2s = new IntSurf_LineOn2S();
2313                             IntSurf_PntOn2S replacePnt = aWLine->Point(tiVpar);
2314                             Standard_Integer cNbP = wline->NbPnts();
2315
2316                             TColStd_SequenceOfInteger VPold;
2317                             Standard_Integer iPo;
2318                             for( iPo = 1; iPo <= cnbV; iPo++ )
2319                             {
2320                               Standard_Real Po = wline->Vertex(iPo).ParameterOnLine();
2321                               Standard_Integer IPo = (Standard_Integer) Po;
2322                               VPold.Append(IPo);
2323                             }
2324
2325                             Standard_Boolean removeNext = Standard_False;
2326                             Standard_Boolean removePrev = Standard_False;
2327                             if( ciV == 1)
2328                             {
2329                               Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
2330                               if(dPar > 10)
2331                               {
2332                                 removeNext = Standard_True;
2333                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2334                                   VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2335                               }
2336                             }
2337                             else if( ciV == cnbV)
2338                             {
2339                               Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
2340                               if(dPar > 10)
2341                               {
2342                                 removePrev = Standard_True;
2343                                 VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
2344                               }
2345                             }
2346                             else
2347                             {
2348                               Standard_Integer dParMi = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
2349                               Standard_Integer dParMa = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
2350                               if(dParMi > 10)
2351                               {
2352                                 removePrev = Standard_True;
2353                                 VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
2354                               }
2355
2356                               if(dParMa > 10)
2357                               {
2358                                 removeNext = Standard_True;
2359                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2360                                 {
2361                                   if(dParMi > 10)
2362                                     VPold.SetValue(iPo, VPold.Value(iPo) - 2 );
2363                                   else
2364                                     VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2365                                 }
2366                               }
2367                               else
2368                               {
2369                                 if(dParMi > 10)
2370                                   for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2371                                     VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2372                               } 
2373                             }
2374
2375                             Standard_Integer pI = ciVpar;
2376
2377                             Standard_Integer iP;
2378                             for( iP = 1; iP <= cNbP; iP++)
2379                             {
2380                               if( pI == iP )
2381                               {
2382                                 IntSurf_PntOn2S newPnt = MakeNewPoint(replacePnt, wline->Point(iP), Periods);
2383                                 newL2s->Add(newPnt);
2384                               }
2385                               else if(removeNext && iP == (pI + 1))
2386                                 continue;
2387                               else if(removePrev && iP == (pI - 1))
2388                                 continue;
2389                               else
2390                                 newL2s->Add(wline->Point(iP));
2391                             }
2392
2393                             IntPatch_Point newVtx;
2394                             gp_Pnt Pnt3dV = aWLine->Vertex(VDMin).Value();
2395                             newVtx.SetValue(Pnt3dV,TolTang,Standard_False);
2396                             newVtx.SetParameters(u21,v21,u22,v22);
2397                             newVtx.SetParameter(VPold.Value(ciV));
2398
2399                             Handle(IntPatch_WLine) NWLine = new IntPatch_WLine(newL2s,Standard_False,trans1,trans2);
2400
2401                             Standard_Integer iV;
2402                             for( iV = 1; iV <= cnbV; iV++ )
2403                             {
2404                               if( iV == ciV )
2405                                 NWLine->AddVertex(newVtx);
2406                               else
2407                               {
2408                                 IntPatch_Point theVtx = wline->Vertex(iV);
2409                                 theVtx.SetParameter(VPold.Value(iV));
2410                                 NWLine->AddVertex(theVtx);
2411                               }
2412                             }
2413
2414                             wline = NWLine;
2415                           }//if( VDMin != 0 )
2416                         }//for( ciV = 1; ciV <= cnbV; ciV++ )
2417                       }// SLin.Length > 0
2418
2419                       AddWLine(SLin, wline, Deflection);
2420                       empt = Standard_False;
2421                     }// !RejetLigne
2422                   }// PW points > 2
2423                 }// done is True
2424               }// dminiPointLigne > SeuildPointLigne
2425             }// HasStartPoint
2426           }// if TabPtDep[nbps2] == 0
2427         } while(nbp>5 && !( (NombreDePointsDeDepartDuCheminement >= NbDePointsDeDepartDuChmLimit && lignetrouvee) || 
2428           (NombreDePointsDeDepartDuCheminement-3 >= nbp && (!lignetrouvee))));
2429         delete [] TabPtDep;
2430       }// for( ls ...
2431
2432       delete [] TabL;
2433
2434     }// if nbLigSec >= 1
2435     //
2436     AdjustOnPeriodic(Surf1, Surf2, SLin);
2437     //
2438
2439     //--------------------------------------------------------------------
2440     //-- Calcul des parametres approches a partir des Zones De Tangence --
2441     //--------------------------------------------------------------------
2442     Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2443     Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2444
2445     UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
2446     UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
2447
2448     // NbPointsInTangentZone always == 1 (eap)
2449
2450     Standard_Integer z;
2451     for( z=1; z <= nbTanZon; z++)
2452     { 
2453       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2454       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) {
2455       Standard_Integer pz=1;
2456       Standard_Real _x,_y,_z;
2457       Standard_Real U1, U2, V1, V2;
2458       Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2459
2460       if(U1>UmaxLig1) UmaxLig1=U1;
2461       if(V1>VmaxLig1) VmaxLig1=V1;
2462       if(U2>UmaxLig2) UmaxLig2=U2;
2463       if(V2>VmaxLig2) VmaxLig2=V2;
2464
2465       if(U1<UminLig1) UminLig1=U1;
2466       if(V1<VminLig1) VminLig1=V1;
2467       if(U2<UminLig2) UminLig2=U2;
2468       if(V2<VminLig2) VminLig2=V2;
2469       //}
2470     }
2471
2472     for(z=1; z <= nbTanZon; z++)
2473     {
2474       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2475       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) { 
2476       Standard_Integer pz=1;
2477       Standard_Real _x,_y,_z;
2478       Standard_Real U1, U2, V1, V2;
2479       Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2480
2481       StartParams(1) = U1;
2482       StartParams(2) = V1;
2483       StartParams(3) = U2;
2484       StartParams(4) = V2;
2485
2486       //-----------------------------------------------------------------------
2487       //-- Calcul du premier point de cheminement a partir du point approche --
2488       //-----------------------------------------------------------------------
2489       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);     
2490       if(HasStartPoint)
2491       {
2492         //-------------------------------------------------
2493         //-- Un point a ete trouve                       --
2494         //-- On verifie qu il n appartient pas           --
2495         //--  a une ligne de cheminement deja calculee.  --
2496         //-------------------------------------------------
2497         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2498
2499         NbLigCalculee = SLin.Length();
2500         dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2501
2502         for(Standard_Integer l=1; 
2503           (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2504           l++)
2505         {
2506           const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2507
2508           if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2509           {
2510             dminiPointLigne = 0.0;
2511           }
2512         }
2513
2514         //-- Fin d exploration des lignes
2515         if(dminiPointLigne > SeuildPointLigne)
2516         {
2517           //---------------------------------------------------
2518           //-- Le point de depart du nouveau cheminement     --
2519           //-- n est present dans aucune ligne deja calculee.--
2520           //---------------------------------------------------
2521           PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,
2522             UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
2523
2524           if(PW.IsDone())
2525           {
2526             if(PW.NbPoints()>2)
2527             { 
2528               const Standard_Integer aMinNbPoints = 40;
2529               if(PW.NbPoints() < aMinNbPoints)
2530               {
2531                 PW.SeekAdditionalPoints(Surf1, Surf2, aMinNbPoints);
2532               }
2533
2534               //-----------------------------------------------
2535               //-- Verification a posteriori : 
2536               //-- On teste si le point de depart et de fin de 
2537               //-- la ligne de cheminement est present dans une 
2538               //-- autre ligne . 
2539               //-----------------------------------------------
2540               RejectLine = Standard_False;
2541               Point3dDebut = PW.Value(1).Value();
2542               const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2543               Point3dFin   = PointFin.Value();
2544
2545               for(ver=1 ; (!RejectLine) && (ver<= NbLigCalculee) ; ver++)
2546               {
2547                 const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2548                 //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2549
2550                 // Check end point if it is on existing line.
2551                 // Start point is checked before.
2552                 if (IsPointOnLine(PointFin, verwline, Deflection))
2553                 {
2554                   RejectLine = Standard_True; 
2555                   break;
2556                 }
2557
2558                 const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2559                 const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2560                 if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
2561                 {
2562                   RejectLine = Standard_True; 
2563                 }
2564                 else
2565                 {
2566                   if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
2567                   {
2568                     RejectLine = Standard_True; 
2569                   }
2570                 }
2571               }
2572
2573               if(!RejectLine)
2574               { 
2575                 IntSurf_TypeTrans trans1,trans2;
2576                 Standard_Real locu,locv;
2577                 gp_Vec norm1,norm2,d1u,d1v;
2578                 gp_Pnt ptbid;
2579                 Standard_Integer indextg;
2580                 gp_Vec tgline(PW.TangentAtLine(indextg));
2581                 PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2582                 Surf1->D1(locu,locv,ptbid,d1u,d1v);
2583                 norm1 = d1u.Crossed(d1v);
2584                 PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2585                 Surf2->D1(locu,locv,ptbid,d1u,d1v);
2586                 norm2 = d1u.Crossed(d1v);
2587                 if (tgline.DotCross(norm2,norm1)>0.)
2588                 {
2589                   trans1 = IntSurf_Out;
2590                   trans2 = IntSurf_In;
2591                 }
2592                 else
2593                 {
2594                   trans1 = IntSurf_In;
2595                   trans2 = IntSurf_Out;
2596                 }
2597
2598                 Standard_Real TolTang = TolTangency;
2599                 Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2600                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2601                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2602
2603                 //---------------
2604                 if(wline->NbVertex() == 0)
2605                 {
2606                   IntPatch_Point vtx;
2607                   IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2608                   POn2S.Parameters(pu1,pv1,pu2,pv2);
2609                   vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2610                   vtx.SetParameters(pu1,pv1,pu2,pv2);
2611                   vtx.SetParameter(1);
2612                   wline->AddVertex(vtx);
2613
2614                   POn2S = PW.Line()->Value(wline->NbPnts());
2615                   POn2S.Parameters(pu1,pv1,pu2,pv2);
2616                   vtx.SetValue(Point3dFin,TolTang,Standard_False);
2617                   vtx.SetParameters(pu1,pv1,pu2,pv2);
2618                   vtx.SetParameter(wline->NbPnts());
2619                   wline->AddVertex(vtx);
2620                 }
2621
2622                 //---------------
2623                 AddWLine(SLin, wline, Deflection);
2624                 empt = Standard_False;
2625               }
2626               else
2627               {
2628                 //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2629               }
2630               //------------------------------------------------------------            
2631             }
2632           }  //--  le cheminement a reussi (done a True)
2633         }  //--  le point approche ne renvoie pas sur une ligne existante
2634       } //-- Si HasStartPoint
2635       //} //-- Boucle Sur les Points de la Tangent Zone
2636     } //-- Boucle sur Les Tangent Zones
2637
2638     if ( pInterference )
2639     {
2640       delete pInterference;
2641       pInterference = NULL;
2642     }
2643
2644     return;
2645   }// if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit)) {  
2646
2647   Handle(IntSurf_LineOn2S) LOn2S = new IntSurf_LineOn2S();
2648   PointDepart( LOn2S, Surf1, NbU1, NbV1, Surf2, NbU2, NbV2 );
2649   empt = Standard_True;
2650   done = Standard_True;
2651   SLin.Clear();  
2652
2653   Standard_Integer NbLigCalculee = 0;
2654   Standard_Real U1,U2,V1,V2;
2655   Standard_Real pu1,pu2,pv1,pv2;
2656
2657   TColStd_Array1OfReal StartParams(1,4);
2658   Standard_Integer MaxOscill = NbU1;
2659   if(MaxOscill < NbU2) MaxOscill=NbU2;
2660   if(MaxOscill < NbV1) MaxOscill=NbV1;
2661   if(MaxOscill < NbV2) MaxOscill=NbV2;
2662
2663   Standard_Real nIncrement=Increment;
2664   //if(MaxOscill>10)
2665   //nIncrement/=0.5*MaxOscill;
2666
2667   IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,nIncrement);
2668   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
2669   Standard_Real    dminiPointLigne;
2670   Standard_Boolean HasStartPoint,RejetLigne;
2671   IntSurf_PntOn2S StartPOn2S;
2672   Standard_Integer ver;
2673   gp_Pnt Point3dDebut,Point3dFin;
2674
2675   //------------------------------------------------------------
2676   //-- Calcul des parametres approches a partir des Zones De Tangence --
2677   //--------------------------------------------------------------------
2678   Standard_Integer nbTanZon = LOn2S->NbPoints();
2679   for(Standard_Integer z=1; z <= nbTanZon; z++) { 
2680     const IntSurf_PntOn2S& POn2S = LOn2S->Value(z);
2681     POn2S.Parameters(U1,V1,U2,V2);
2682     StartParams(1) = U1;
2683     StartParams(2) = V1;
2684     StartParams(3) = U2;
2685     StartParams(4) = V2;
2686
2687     //-----------------------------------------------------------------------
2688     //-- Calcul du premier point de cheminement a partir du point approche --
2689     //-----------------------------------------------------------------------
2690     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);       
2691     if(HasStartPoint)
2692     { 
2693       //-------------------------------------------------
2694       //-- Un point a ete trouve                       --
2695       //-- On verifie qu il n appartient pas           --
2696       //--  a une ligne de cheminement deja calculee.  --
2697       //-------------------------------------------------
2698       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2699
2700       NbLigCalculee = SLin.Length();
2701       dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2702
2703       for(Standard_Integer l=1; 
2704         (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2705         l++)
2706       {
2707         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2708
2709         if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2710         {
2711           dminiPointLigne = 0.0;
2712         }
2713       }
2714
2715       //-- Fin d exploration des lignes
2716       if(dminiPointLigne > SeuildPointLigne)
2717       { 
2718         //---------------------------------------------------
2719         //-- Le point de depart du nouveau cheminement     --
2720         //-- n est present dans aucune ligne deja calculee.--
2721         //---------------------------------------------------
2722         PW.Perform(StartParams);
2723         if(PW.IsDone())
2724         {
2725           if(PW.NbPoints()>2)
2726           {
2727             //-----------------------------------------------
2728             //-- Verification a posteriori : 
2729             //-- On teste si le point de depart et de fin de 
2730             //-- la ligne de cheminement est present dans une 
2731             //-- autre ligne . 
2732             //-----------------------------------------------
2733             RejetLigne = Standard_False;
2734             Point3dDebut = PW.Value(1).Value();
2735             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2736             Point3dFin   = PointFin.Value();
2737
2738             for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++)
2739             {
2740               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2741               //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2742
2743               // Check end point if it is on existing line.
2744               // Start point is checked before.
2745               if (IsPointOnLine(PointFin, verwline, Deflection))
2746               {
2747                 RejetLigne = Standard_True; 
2748                 break;
2749               }
2750
2751               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2752               const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2753               if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
2754               {
2755                 RejetLigne = Standard_True; 
2756               }
2757               else
2758               {
2759                 if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
2760                 {
2761                   RejetLigne = Standard_True; 
2762                 }
2763               }
2764             }
2765
2766             if(!RejetLigne)
2767             {
2768               IntSurf_TypeTrans trans1,trans2;
2769               Standard_Real locu,locv;
2770               gp_Vec norm1,norm2,d1u,d1v;
2771               gp_Pnt ptbid;
2772               Standard_Integer indextg;
2773               gp_Vec tgline(PW.TangentAtLine(indextg));
2774               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2775               Surf1->D1(locu,locv,ptbid,d1u,d1v);
2776               norm1 = d1u.Crossed(d1v);
2777               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2778               Surf2->D1(locu,locv,ptbid,d1u,d1v);
2779               norm2 = d1u.Crossed(d1v);
2780               if (tgline.DotCross(norm2,norm1)>0.)
2781               {
2782                 trans1 = IntSurf_Out;
2783                 trans2 = IntSurf_In;
2784               }
2785               else {
2786                 trans1 = IntSurf_In;
2787                 trans2 = IntSurf_Out;
2788               }
2789
2790               Standard_Real TolTang = TolTangency;
2791               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2792               IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2793               IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2794
2795               //---------------
2796               if(wline->NbVertex() == 0)
2797               {
2798                 IntPatch_Point vtx;
2799                 const IntSurf_PntOn2S& POn2Sf = PW.Line()->Value(1);
2800                 POn2Sf.Parameters(pu1,pv1,pu2,pv2);
2801                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2802                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2803                 vtx.SetParameter(1);
2804                 wline->AddVertex(vtx);
2805
2806                 const IntSurf_PntOn2S& POn2Sl = PW.Line()->Value(wline->NbPnts());
2807                 POn2Sl.Parameters(pu1,pv1,pu2,pv2);
2808                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
2809                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2810                 vtx.SetParameter(wline->NbPnts());
2811                 wline->AddVertex(vtx);
2812               }
2813
2814               //---------------
2815               AddWLine(SLin, wline, Deflection);
2816               empt = Standard_False;
2817             }
2818             else
2819             { 
2820               //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2821             }
2822             //------------------------------------------------------------              
2823           }
2824         }  //--  le cheminement a reussi (done a True)
2825       }  //--  le point approche ne renvoie pas sur une ligne existante
2826     } //-- Si HasStartPoint
2827   } //-- Boucle sur Les Tangent Zones
2828 }
2829 //modified by NIZNHY-PKV Wed May 25 09:39:07 2011f
2830 //=======================================================================
2831 //class : IntPatch_InfoPD
2832 //purpose  : 
2833 //=======================================================================
2834 class IntPatch_InfoPD {
2835 public:
2836   //----------------------------------------C-tor
2837   IntPatch_InfoPD(const Standard_Integer aNBI) {
2838     Standard_Integer aNBI2, i, j;
2839     myNBI=aNBI;
2840     //
2841     aNBI2=aNBI*aNBI;
2842     myP1DS2=new char[aNBI2];
2843     myP2DS1=new char[aNBI2];
2844     myIP1=new Standard_Integer[aNBI2]; 
2845     myIP2=new Standard_Integer[aNBI2];
2846     myP1=new gp_Pnt[aNBI2];
2847     myP2=new gp_Pnt[aNBI2];
2848     //
2849     for (i=0; i<myNBI; ++i) {
2850       for (j=0; j<myNBI; ++j) {
2851         xP1DS2(i, j)=0;
2852         xP2DS1(i, j)=0;
2853         xIP1(i, j)=0;
2854         xIP2(i, j)=0;
2855         xP1(i, j).SetCoord(0., 0., 0.);
2856         xP2(i, j).SetCoord(0., 0., 0.);
2857       }
2858     }
2859   };
2860   //---------------------------------------- D-tor
2861   ~IntPatch_InfoPD() {
2862     delete [] (char*) myP1DS2;
2863     delete [] (char*) myP2DS1;
2864     delete [] (Standard_Integer*) myIP1;
2865     delete [] (Standard_Integer*) myIP2; 
2866     delete [] (gp_Pnt*)myP1;
2867     delete [] (gp_Pnt*)myP2;
2868   };
2869   //---------------------------------------- Index
2870   Standard_Integer Index(const Standard_Integer i,
2871     const Standard_Integer j) const { 
2872       return i*myNBI+j;
2873   };
2874   //---------------------------------------- NBI
2875   Standard_Integer NBI() const { 
2876     return myNBI;
2877   };
2878   //----------------------------------------xP1DS2
2879   char& xP1DS2(const Standard_Integer i,
2880     const Standard_Integer j) { 
2881       return myP1DS2[Index(i,j)];
2882   };
2883   //----------------------------------------xP2DS1
2884   char& xP2DS1(const Standard_Integer i,
2885     const Standard_Integer j) { 
2886       return myP2DS1[Index(i,j)];
2887   };
2888   //----------------------------------------xIP1
2889   Standard_Integer& xIP1(const Standard_Integer i,
2890     const Standard_Integer j) { 
2891       return myIP1[Index(i,j)];
2892   };
2893   //----------------------------------------xIP2
2894   Standard_Integer& xIP2(const Standard_Integer i,
2895     const Standard_Integer j) { 
2896       return myIP2[Index(i,j)];
2897   };
2898   //----------------------------------------xP1
2899   gp_Pnt& xP1(const Standard_Integer i,
2900     const Standard_Integer j) { 
2901       return myP1[Index(i,j)];
2902   };
2903   //----------------------------------------xP1
2904   gp_Pnt& xP2(const Standard_Integer i,
2905     const Standard_Integer j) { 
2906       return myP2[Index(i,j)];
2907   };
2908 protected:
2909   Standard_Integer myNBI;
2910   char *myP1DS2;
2911   char *myP2DS1;
2912   Standard_Integer *myIP1;
2913   Standard_Integer *myIP2;
2914   gp_Pnt *myP1;
2915   gp_Pnt *myP2;
2916 }; 
2917 //modified by NIZNHY-PKV Tue May 24 11:38:55 2011t
2918 //==================================================================================
2919 // function : PointDepart
2920 // purpose  : 
2921 //==================================================================================
2922 void IntPatch_PrmPrmIntersection::PointDepart(Handle(IntSurf_LineOn2S)& LineOn2S,
2923                                               const Handle(Adaptor3d_HSurface)& S1,
2924                                               const Standard_Integer SU_1,
2925                                               const Standard_Integer SV_1,
2926                                               const Handle(Adaptor3d_HSurface)& S2,
2927                                               const Standard_Integer SU_2,
2928                                               const Standard_Integer SV_2) const 
2929
2930   Standard_Integer i, j, xNBI;
2931   //modified by NIZNHY-PKV Tue May 24 11:37:38 2011f
2932   xNBI=200;
2933   IntPatch_InfoPD aIPD(xNBI);
2934   //modified by NIZNHY-PKV Wed May 25 06:47:12 2011t
2935   Standard_Integer iC15, SU1, SV1, SU2, SV2;
2936   Standard_Real U0, U1, V0, V1, U, V;
2937   Standard_Real resu0,resv0;
2938   Standard_Real  du1,du2,dv1,dv2, dmaxOn1, dmaxOn2;
2939   Standard_Real x0,y0,z0, x1,y1,z1,d;
2940   Bnd_Box Box1, Box2;
2941   //
2942   iC15=15;
2943   SU1 =iC15*SU_1 ;
2944   SV1 =iC15*SV_1 ;
2945   SU2 =iC15*SU_2 ;
2946   SV2 =iC15*SV_2 ;
2947   //
2948   if(xNBI<SU1) {
2949     SU1 = xNBI;
2950   }
2951   if(xNBI<SV1){
2952     SV1 = xNBI;
2953   }
2954   if(xNBI<SU2){
2955     SU2 = xNBI;
2956   }
2957   if(xNBI<SV2){
2958     SV2 = xNBI;
2959   }
2960   //
2961   U0 = S1->FirstUParameter();
2962   U1 = S1->LastUParameter();
2963   V0 = S1->FirstVParameter();
2964   V1 = S1->LastVParameter();
2965   //
2966   resu0=U0;
2967   resv0=V0;
2968   //
2969   dmaxOn1 = 0.0;
2970   dmaxOn2 = 0.0;
2971   //-----
2972   du1 = (U1-U0)/(SU1-1);
2973   dv1 = (V1-V0)/(SV1-1);
2974   for(U=U0,i=0; i<SU1; i++,U+=du1) { 
2975     for(V=V0,j=0; j<SV1; V+=dv1,j++) { 
2976       aIPD.xP1(i, j)= S1->Value(U,V);
2977       Box1.Add(aIPD.xP1(i, j));
2978       if(i>0 && j>0) { 
2979         aIPD.xP1(i, j)    .Coord(x0,y0,z0);
2980         aIPD.xP1(i-1, j-1).Coord(x1,y1,z1);
2981         //
2982         d=Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
2983         if(d>dmaxOn1) {
2984           dmaxOn1 = d;
2985         }
2986       }
2987     }
2988   }
2989   Box1.Enlarge(1.e-8);
2990   //
2991   U0 = S2->FirstUParameter();
2992   U1 = S2->LastUParameter();
2993   V0 = S2->FirstVParameter();
2994   V1 = S2->LastVParameter();
2995   //
2996   du2 = (U1-U0)/(SU2-1);  
2997   dv2 = (V1-V0)/(SV2-1);
2998   for(U=U0,i=0; i<SU2; i++,U+=du2) { 
2999     for(V=V0,j=0; j<SV2; V+=dv2,j++) { 
3000       aIPD.xP2(i, j) = S2->Value(U,V);
3001       Box2.Add(aIPD.xP2(i, j));
3002       if(i>0 && j>0) { 
3003         aIPD.xP2(i, j)    .Coord(x0,y0,z0);
3004         aIPD.xP2(i-1, j-1).Coord(x1,y1,z1);
3005         d =  Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
3006         if(d>dmaxOn2) {
3007           dmaxOn2 = d;
3008         }
3009       }
3010     }
3011   }
3012   Box2.Enlarge(1.e-8);
3013   //--------
3014   //
3015   if(Box1.IsOut(Box2)) {
3016
3017     return;
3018   }
3019   //
3020   Standard_Integer aNbPG;
3021   Standard_Real x10,y10,z10,x11,y11,z11;
3022   Standard_Real x20,y20,z20,x21,y21,z21;
3023   Standard_Real dx, dy, dz, dmax;
3024   Standard_Real dx2, dy2, dz2;
3025   //
3026   Box1.Get(x10,y10,z10,x11,y11,z11);
3027   Box2.Get(x20,y20,z20,x21,y21,z21);
3028   //
3029   x0 = (x10>x20)? x10 : x20;
3030   y0 = (y10>y20)? y10 : y20;
3031   z0 = (z10>z20)? z10 : z20;
3032   //
3033   x1 = (x11<x21)? x11 : x21;
3034   y1 = (y11<y21)? y11 : y21;
3035   z1 = (z11<z21)? z11 : z21;
3036   //
3037   if(dmaxOn2 > dmaxOn1) {
3038     dmaxOn1 = dmaxOn2;
3039   }
3040   //
3041   dmaxOn1+=dmaxOn1;
3042   x0-=dmaxOn1;
3043   y0-=dmaxOn1;
3044   z0-=dmaxOn1;
3045   x1+=dmaxOn1;
3046   y1+=dmaxOn1;
3047   z1+=dmaxOn1;
3048   //
3049   x10-=dmaxOn1;   y10-=dmaxOn1;   z10-=dmaxOn1; 
3050   x11+=dmaxOn1;   y11+=dmaxOn1;   z11+=dmaxOn1; 
3051
3052   x20-=dmaxOn1;   y20-=dmaxOn1;   z20-=dmaxOn1; 
3053   x21+=dmaxOn1;   y21+=dmaxOn1;   z21+=dmaxOn1; 
3054
3055   aNbPG=NbPointsGrille();
3056   dx = (x1-x0)/aNbPG;
3057   dy = (y1-y0)/aNbPG;
3058   dz = (z1-z0)/aNbPG;
3059   //
3060   dmax = dx;
3061   if(dy>dmax) {
3062     dmax = dy;
3063   }
3064   if(dz>dmax){
3065     dmax = dz;
3066   }
3067   //
3068   if(dx<dmax*0.01) {
3069     dx = dmax*0.01;
3070   }
3071   if(dy<dmax*0.01) {
3072     dy = dmax*0.01;
3073   }
3074   if(dz<dmax*0.01) {
3075     dz = dmax*0.01;
3076   }
3077   //
3078   dx2 = dx*0.5;
3079   dy2 = dy*0.5;
3080   dz2 = dz*0.5 ;
3081   //
3082   IntPatch_PrmPrmIntersection_T3Bits M1(_BASE);
3083   IntPatch_PrmPrmIntersection_T3Bits M2(_BASE);
3084   //
3085   for(i=0;i<SU1;i++) { 
3086     for(j=0;j<SV1;j++) { 
3087       aIPD.xIP1(i, j)=-1;
3088       const gp_Pnt& P=aIPD.xP1(i, j);
3089       aIPD.xP1DS2(i, j) = (char)CodeReject(x20,y20,z20,x21,y21,z21,P.X(),P.Y(),P.Z());
3090       int ix = (int)((P.X()-x0  + dx2 )/dx);
3091       if(DansGrille(ix)) { 
3092         int iy = (int)((P.Y()-y0 + dy2)/dy);
3093         if(DansGrille(iy)) {
3094           int iz = (int)((P.Z()-z0 + dz2)/dz);
3095           if(DansGrille(iz)) {
3096             aIPD.xIP1(i, j) = GrilleInteger(ix,iy,iz);
3097           }
3098         }
3099       }
3100     }
3101   }
3102   //-- cout<<" Grille  du 1 fini "<<endl;
3103   for(i=0;i<SU2;i++) { 
3104     for(j=0;j<SV2;j++) { 
3105       aIPD.xIP2(i, j)=-1;
3106       const gp_Pnt& P=aIPD.xP2(i, j);
3107       aIPD.xP2DS1(i, j) = (char)CodeReject(x10,y10,z10,x11,y11,z11,P.X(),P.Y(),P.Z());
3108       int ix = (int)((P.X()-x0 + dx2)/dx);
3109       if(DansGrille(ix)) { 
3110         int iy = (int)((P.Y()-y0 + dy2)/dy);
3111         if(DansGrille(iy)) {
3112           int iz = (int)((P.Z()-z0 + dz2)/dz);
3113           if(DansGrille(iz)) {
3114             aIPD.xIP2(i, j) =  GrilleInteger(ix,iy,iz);
3115           }
3116         }
3117       }
3118     }
3119   }
3120   //
3121   for(i=0;i<SU1-1;i+=1) {
3122     for(j=0;j<SV1-1;j+=1) { 
3123       if(!((aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j)) || 
3124         (aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j+1)))){
3125           Remplit(aIPD.xIP1(i, j),
3126             aIPD.xIP1(i+1, j),
3127             aIPD.xIP1(i+1, j+1),
3128             M1);
3129       }
3130       if(!((aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i, j+1)) || 
3131         (aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j+1)))) {
3132           Remplit(aIPD.xIP1(i, j),
3133             aIPD.xIP1(i, j+1),
3134             aIPD.xIP1(i+1, j+1),
3135             M1);        
3136       }
3137     }
3138   }     
3139   //
3140   for(i=0;i<SU2-1;i+=1) {
3141     for(j=0;j<SV2-1;j+=1) { 
3142       if(!((aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j)) ||
3143         (aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j+1)))){
3144           Remplit(aIPD.xIP2(i, j),
3145             aIPD.xIP2(i+1, j),
3146             aIPD.xIP2(i+1, j+1),
3147             M2);
3148       }
3149       if(!((aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i, j+1)) || 
3150         (aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j+1)))){
3151           Remplit(aIPD.xIP2(i, j),
3152             aIPD.xIP2(i, j+1),
3153             aIPD.xIP2(i+1, j+1),
3154             M2);        
3155       }
3156     }
3157   }     
3158   //
3159   M1.ResetAnd();
3160   M2.ResetAnd();
3161   //
3162   int newind=0;
3163   long unsigned Compt=0;
3164   int ok=0;
3165   int indicepointtraite = 0;
3166   Standard_Integer k,nu,nv;
3167   //
3168   do { 
3169     indicepointtraite--;
3170     ok = M1.And(M2,newind);
3171     if(ok) { 
3172       IntegerGrille(newind,i,j,k);
3173       int nb=0;
3174       int LIM=3;
3175       if(   DansGrille(i-1) && DansGrille(j-1) && DansGrille(k-1) 
3176         && DansGrille(i+1) && DansGrille(j+1) && DansGrille(k+1)) { 
3177           int si,sj,sk;
3178           for(si=-1; si<= 1 && nb<LIM; si++) { 
3179             for(sj=-1; sj<= 1 && nb<LIM; sj++) { 
3180               for(sk=-1; sk<= 1 && nb<LIM; sk++) { 
3181                 long unsigned lu=GrilleInteger(i+si,j+sj,k+sk);
3182                 if(M1.Val(lu) && M2.Val(lu)) { 
3183                   nb++;
3184                 }
3185               }
3186             }
3187           }
3188           if(nb>=LIM) { 
3189             for(si=-1; si<= 1; si++) { 
3190               for(sj=-1; sj<= 1; sj++) { 
3191                 for(sk=-1; sk<= 1; sk++) { 
3192                   if(si || sj || sk) { 
3193                     long unsigned lu=GrilleInteger(i+si,j+sj,k+sk);
3194                     M1.Raz(lu);
3195                   }
3196                 }
3197               }
3198             }
3199           }
3200       }
3201       //
3202       gp_Pnt P(dx*i + x0, dy*j + y0, dz*k+z0);
3203       //
3204       Standard_Integer nu1=-1,nu2=-1;
3205       Standard_Integer nv1=0, nv2=0;
3206       int nbsur1 = 0;
3207       for(nu=0;nu1<0 && nu<SU1;nu++) { 
3208         for(nv=0;nu1<0 && nv<SV1;nv++) { 
3209           if( aIPD.xIP1(nu, nv) ==(Standard_Integer) newind )  { 
3210             nbsur1++;
3211             aIPD.xIP1(nu, nv)=indicepointtraite;
3212             nu1=nu; nv1=nv;
3213           }
3214         }
3215       }
3216       if(nu1>=0) { 
3217         int nbsur2 = 0;
3218         for(nu=0;nu2<0 && nu<SU2;nu++) { 
3219           for(nv=0;nu2<0 && nv<SV2;nv++) { 
3220             if( aIPD.xIP2(nu, nv)==(Standard_Integer) newind )  { 
3221               nbsur2++;
3222               aIPD.xIP2(nu, nv)=indicepointtraite;
3223               nu2=nu; nv2=nv;
3224             }
3225           }
3226         }
3227       }
3228       if(nu1>=0 && nu2>=0) { 
3229         IntSurf_PntOn2S POn2S;
3230         POn2S.SetValue(P, 
3231           S1->FirstUParameter()+nu1*du1,
3232           S1->FirstVParameter()+nv1*dv1,
3233           S2->FirstUParameter()+nu2*du2,
3234           S2->FirstVParameter()+nv2*dv2);
3235         LineOn2S->Add(POn2S);
3236         Compt++;
3237       }
3238       else { 
3239         //-- aucun point du triangle n a ete trouve assez proche
3240         //-- on recherche les 3 points les plus proches de P 
3241         //-- dans chacun des tableaux 
3242         Standard_Real Dist3[3],u3[3] = { 0.0, 0.0, 0.0 },v3[3] = { 0.0, 0.0, 0.0 };
3243         Dist3[0]=Dist3[1]=Dist3[2]=RealLast();
3244         for(U=resu0,i=0; i<SU1; i++,U+=du1) { 
3245           for(V=resv0,j=0; j<SV1; V+=dv1,j++) {       
3246             //-- On place les 3 meilleures valeurs dans Dist1,Dist2,Dist3
3247             Standard_Real t = aIPD.xP1(i, j).SquareDistance(P);
3248             //-- On remplace la plus grande valeur ds Dist[.] par la val courante
3249             if(Dist3[0]<Dist3[1]) { 
3250               Standard_Real z;
3251               z=Dist3[0]; Dist3[0]=Dist3[1]; Dist3[1]=z;
3252               z=u3[0]; u3[0]=u3[1]; u3[1]=z;
3253               z=v3[0]; v3[0]=v3[1]; v3[1]=z;
3254             }
3255             if(Dist3[1]<Dist3[2]) { 
3256               Standard_Real z;
3257               z=Dist3[1]; Dist3[1]=Dist3[2]; Dist3[2]=z;
3258               z=u3[1]; u3[1]=u3[2]; u3[2]=z;
3259               z=v3[1]; v3[1]=v3[2]; v3[2]=z;
3260             }
3261             if(Dist3[0]<Dist3[1]) { 
3262               Standard_Real z;
3263               z=Dist3[0]; Dist3[0]=Dist3[1]; Dist3[1]=z;
3264               z=u3[0]; u3[0]=u3[1]; u3[1]=z;
3265               z=v3[0]; v3[0]=v3[1]; v3[1]=z;
3266             }
3267             //-- la plus grande valeur est dans Dist3[0]
3268             if(t<Dist3[0]) { 
3269               Dist3[0]=t; u3[0]=U; v3[0]=V;
3270             }
3271           }
3272         }
3273         //
3274         Standard_Real U1_3 = (u3[0]+u3[1]+u3[2])/3.0;
3275         Standard_Real V1_3 = (v3[0]+v3[1]+v3[2])/3.0;
3276
3277         Dist3[0]=Dist3[1]=Dist3[2]=RealLast();
3278         for(U=U0,i=0; i<SU2; i++,U+=du2) { 
3279           for(V=V0,j=0; j<SV2; V+=dv2,j++) {       
3280             //-- On place les 3 meilleures valeurs dans Dist1,Dist2,Dist3
3281             Standard_Real t = aIPD.xP2(i, j).SquareDistance(P);
3282             //-- On remplace la plus grande valeur ds Dist3[.] par la val courante
3283             if(Dist3[0]<Dist3[1]) { 
3284               Standard_Real z;
3285               z=Dist3[0]; Dist3[0]=Dist3[1]; Dist3[1]=z;
3286               z=u3[0]; u3[0]=u3[1]; u3[1]=z;
3287               z=v3[0]; v3[0]=v3[1]; v3[1]=z;
3288             }
3289             if(Dist3[1]<Dist3[2]) { 
3290               Standard_Real z;
3291               z=Dist3[1]; Dist3[1]=Dist3[2]; Dist3[2]=z;
3292               z=u3[1]; u3[1]=u3[2]; u3[2]=z;
3293               z=v3[1]; v3[1]=v3[2]; v3[2]=z;
3294             }
3295             if(Dist3[0]<Dist3[1]) { 
3296               Standard_Real z;
3297               z=Dist3[0]; Dist3[0]=Dist3[1]; Dist3[1]=z;
3298               z=u3[0]; u3[0]=u3[1]; u3[1]=z;
3299               z=v3[0]; v3[0]=v3[1]; v3[1]=z;
3300             }
3301             //-- la plus grande valeur est dans Dist3[0]
3302             if(t<Dist3[0]) { 
3303               Dist3[0]=t; u3[0]=U; v3[0]=V;
3304             }
3305           }
3306         }
3307         //
3308         Standard_Real U2_3 = (u3[0]+u3[1]+u3[2])/3.0;
3309         Standard_Real V2_3 = (v3[0]+v3[1]+v3[2])/3.0;
3310         //
3311         IntSurf_PntOn2S POn2S;
3312         POn2S.SetValue(P,U1_3,V1_3,U2_3,V2_3);
3313         LineOn2S->Add(POn2S);
3314         Compt++;        
3315       }
3316     }
3317   }
3318   while(ok);
3319 }
3320
3321 //==================================================================================
3322 // function : IsPointOnLine
3323 // purpose  : 
3324 //==================================================================================
3325
3326 Standard_Boolean IsPointOnLine(const IntSurf_PntOn2S        &thePOn2S,
3327                                const Handle(IntPatch_WLine) &theWLine,
3328                                const Standard_Real           Deflection)
3329 {
3330   Standard_Boolean isOnLine = Standard_False;
3331   Standard_Real Deflection2 = Deflection*Deflection;
3332   Standard_Real pu1, pu2, pv1, pv2;
3333
3334   thePOn2S.Parameters(pu1, pv1, pu2, pv2);
3335
3336   if ((theWLine->IsOutSurf1Box(gp_Pnt2d(pu1, pv1)) == Standard_False) &&
3337     (theWLine->IsOutSurf2Box(gp_Pnt2d(pu2, pv2)) == Standard_False) &&
3338     (theWLine->IsOutBox(thePOn2S.Value())        == Standard_False)) {
3339       const Standard_Integer NbPntOn2SOnLine = theWLine->NbPnts();
3340       Standard_Integer ll;
3341
3342       for (ll=1; ll < NbPntOn2SOnLine && !isOnLine; ll++) {
3343         const gp_Pnt &Pa     = theWLine->Point(ll).Value();
3344         const gp_Pnt &Pb     = theWLine->Point(ll+1).Value();
3345         const gp_Pnt &PStart = thePOn2S.Value();
3346         const gp_Vec  AM(Pa, PStart);
3347         const gp_Vec  MB(PStart,Pb);
3348         const Standard_Real AMMB = AM.Dot(MB);
3349
3350         if(AMMB > 0.0) {
3351           gp_Dir ABN(Pb.X() - Pa.X(), Pb.Y() - Pa.Y(), Pb.Z() - Pa.Z());
3352           Standard_Real lan =  ABN.X()*AM.X() + ABN.Y()*AM.Y() + ABN.Z()*AM.Z();
3353           gp_Vec AH(lan*ABN.X(), lan*ABN.Y(), lan*ABN.Z());
3354           gp_Vec HM(AM.X() - AH.X(), AM.Y() - AH.Y(), AM.Z() - AH.Z());
3355           Standard_Real d = 0.0;
3356
3357           if(HM.X() < Deflection) {
3358             d += HM.X()*HM.X();
3359
3360             if(HM.Y() < Deflection) {
3361               d += HM.Y()*HM.Y();
3362
3363               if(HM.Z() < Deflection) {
3364                 d += HM.Z()*HM.Z();
3365               } else {
3366                 d = Deflection2;
3367               }
3368             } else {
3369               d = Deflection2;
3370             }
3371           } else {
3372             d = Deflection2;
3373           }
3374
3375           if(d < Deflection2) {
3376             isOnLine = Standard_True;
3377           }
3378         } else {
3379           Standard_Real dab = Pa.SquareDistance(Pb);
3380           Standard_Real dap = Pa.SquareDistance(PStart);
3381
3382           if(dap < dab) {
3383             isOnLine = Standard_True;
3384           } else {
3385             Standard_Real dbp = Pb.SquareDistance(PStart);
3386
3387             if(dbp < dab) {
3388               isOnLine = Standard_True;
3389             }
3390           }
3391         }
3392       }
3393   }
3394
3395   return isOnLine;
3396 }
3397
3398 //==================================================================================
3399 // function : AddWLine
3400 // purpose  : 
3401 //==================================================================================
3402
3403 void AddWLine(IntPatch_SequenceOfLine      &theLines,
3404               const Handle(IntPatch_WLine) &theWLine,
3405               const Standard_Real           Deflection)
3406 {
3407   Standard_Integer i = 1;
3408   Standard_Integer aNbLines = theLines.Length();
3409   Standard_Boolean isToRemove;
3410
3411   // Check each line of theLines if it is on theWLine.
3412   while (i <= aNbLines) {
3413     Handle(IntPatch_WLine) aWLine =
3414       Handle(IntPatch_WLine)::DownCast(theLines.Value(i));
3415
3416     isToRemove = Standard_False;
3417
3418     if (aWLine.IsNull() == Standard_False) {
3419       // Check the middle point.
3420       Standard_Integer aMidIndex = (aWLine->NbPnts() + 1)/2;
3421
3422       if (aMidIndex > 0) {
3423         const IntSurf_PntOn2S &aPnt = aWLine->Point(aMidIndex);
3424
3425         if (IsPointOnLine(aPnt, theWLine, Deflection)) {
3426           // Middle point is on theWLine. Check vertices.
3427           isToRemove = Standard_True;
3428
3429           Standard_Integer j;
3430           const Standard_Integer aNbVtx = aWLine->NbVertex();
3431
3432           for (j = 1; j <= aNbVtx; j++) {
3433             const IntPatch_Point &aPoint = aWLine->Vertex(j);
3434
3435             if (!IsPointOnLine(aPoint.PntOn2S(), theWLine, Deflection)) {
3436               isToRemove = Standard_False;
3437               break;
3438             }
3439           }
3440         }
3441       }
3442     }
3443
3444     if (isToRemove) {
3445       theLines.Remove(i);
3446       aNbLines--;
3447     } else {
3448       i++;
3449     }
3450   }
3451
3452   // Add theWLine to the sequence of lines.
3453   theLines.Append(theWLine);
3454 }