4f9da150f08675d0a58827233035a356b3e88096
[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             RejetLigne = Standard_False;
1376             Point3dDebut = PW.Value(1).Value();
1377             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
1378             Point3dFin   = PointFin.Value();
1379             for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
1380               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
1381
1382               // Check end point if it is on existing line.
1383               // Start point is checked before.
1384               if (IsPointOnLine(PointFin, verwline, Deflection)) {
1385                 RejetLigne = Standard_True; 
1386                 break;
1387               }
1388
1389               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
1390               const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
1391               if(Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) { 
1392                 if(Point3dFin.Distance(verPointFin.Value()) <= TolTangency)
1393                   RejetLigne = Standard_True; 
1394               }
1395             }
1396             
1397             if(!RejetLigne) {
1398               IntSurf_TypeTrans trans1,trans2;
1399               Standard_Real locu,locv;
1400               gp_Vec norm1,norm2,d1u,d1v;
1401               gp_Pnt ptbid;
1402               Standard_Integer indextg;
1403               gp_Vec tgline(PW.TangentAtLine(indextg));
1404               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1405               Surf1->D1(locu,locv,ptbid,d1u,d1v);
1406               norm1 = d1u.Crossed(d1v);
1407               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1408               Surf2->D1(locu,locv,ptbid,d1u,d1v);
1409               norm2 = d1u.Crossed(d1v);
1410               if( tgline.DotCross(norm2,norm1) >= 0. ) {
1411                 trans1 = IntSurf_Out;
1412                 trans2 = IntSurf_In;
1413               }
1414               else {
1415                 trans1 = IntSurf_In;
1416                 trans2 = IntSurf_Out;
1417               }
1418               
1419               Standard_Real TolTang = TolTangency;
1420               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1421               if (RestrictLine){
1422                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1423                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1424               }
1425               
1426               if(wline->NbVertex() == 0) {
1427                 IntPatch_Point vtx;
1428                 IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1429                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1430                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1431                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1432                 vtx.SetParameter(1);
1433                 wline->AddVertex(vtx);
1434                 
1435                 POn2S = PW.Line()->Value(wline->NbPnts());
1436                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1437                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
1438                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1439                 vtx.SetParameter(wline->NbPnts());
1440                 wline->AddVertex(vtx);
1441               }              
1442
1443               Standard_Integer slinlen = SLin.Length();
1444               if( slinlen > 0 ) {
1445                 Standard_Integer cnbV = wline->NbVertex();
1446                 Standard_Integer ciV;
1447                 for( ciV = 1; ciV <= cnbV; ciV++ ) {
1448                   Standard_Real pntDMin = 1.e+100;
1449                   Standard_Integer VDMin = 0;
1450                   Standard_Integer WLDMin = 0;
1451                   gp_Pnt cPV = wline->Vertex(ciV).Value();
1452                   Standard_Integer iL;
1453                   for( iL = 1; iL <= slinlen; iL++) {
1454                     const Handle(IntPatch_Line)& aSLine = SLin.Value(iL);
1455                     IntPatch_IType aType = aSLine->ArcType();
1456                     if( aType != IntPatch_Walking)
1457                       continue;
1458                     const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
1459                     Standard_Integer tnbV = aWLine->NbVertex();
1460                     Standard_Integer tiV;
1461                     for( tiV = 1; tiV <= tnbV; tiV++ ) {
1462                       gp_Pnt tPV = aWLine->Vertex(tiV).Value();
1463                       Standard_Real tDistance = cPV.Distance(tPV);
1464                       Standard_Real uRs1 = Surf1->Surface().UResolution(tDistance);
1465                       Standard_Real vRs1 = Surf1->Surface().VResolution(tDistance);
1466                       Standard_Real uRs2 = Surf2->Surface().UResolution(tDistance);
1467                       Standard_Real vRs2 = Surf2->Surface().VResolution(tDistance);
1468                       Standard_Real RmaxS1 = Max(uRs1,vRs1);
1469                       Standard_Real RmaxS2 = Max(uRs2,vRs2);
1470                       if(RmaxS1 < 1.e-4 && RmaxS2 < 1.e-4) {
1471                         if( pntDMin > tDistance && tDistance > 1.e-9) {
1472                           pntDMin = tDistance;
1473                           VDMin = tiV;
1474                           WLDMin = iL;
1475                         }
1476                       }
1477                     }
1478                   }
1479                   
1480                   if( VDMin != 0 ) {
1481                     const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
1482                     const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
1483                     Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
1484                     Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
1485                     Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
1486                     Standard_Real u21 = 0., u22 = 0., v21 = 0., v22 = 0.;
1487                     wline->Point(ciVpar).Parameters(u11,v11,u12,v12);
1488                     aWLine->Point(tiVpar).Parameters(u21,v21,u22,v22);
1489                     
1490                     Handle(IntSurf_LineOn2S) newL2s = new IntSurf_LineOn2S();
1491                     IntSurf_PntOn2S replacePnt = aWLine->Point(tiVpar);
1492                     Standard_Integer cNbP = wline->NbPnts();
1493                     
1494                     TColStd_SequenceOfInteger VPold;
1495                     Standard_Integer iPo;
1496                     for( iPo = 1; iPo <= cnbV; iPo++ ) {
1497                       Standard_Real Po = wline->Vertex(iPo).ParameterOnLine();
1498                       Standard_Integer IPo = (Standard_Integer) Po;
1499                       VPold.Append(IPo);
1500                     }
1501                     
1502                     Standard_Boolean removeNext = Standard_False;
1503                     Standard_Boolean removePrev = Standard_False;
1504                     if( ciV == 1) {
1505                       Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
1506                       if(dPar > 10) {
1507                                 removeNext = Standard_True;
1508                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
1509                                   VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1510                               }
1511                     }
1512                     else if( ciV == cnbV) {
1513                       Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
1514                       if(dPar > 10) {
1515                         removePrev = Standard_True;
1516                         VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
1517                       }
1518                     }
1519                     else {
1520                       Standard_Integer dParMi = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
1521                       Standard_Integer dParMa = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
1522                       if(dParMi > 10) {
1523                         removePrev = Standard_True;
1524                         VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
1525                       }
1526                       if(dParMa > 10) {
1527                         removeNext = Standard_True;
1528                         for( iPo = (ciV+1); iPo <= cnbV; iPo++ ) {
1529                           if(dParMi > 10)
1530                             VPold.SetValue(iPo, VPold.Value(iPo) - 2 );
1531                           else
1532                             VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1533                         }
1534                       }
1535                       else {
1536                         if(dParMi > 10)
1537                           for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
1538                             VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1539                       } 
1540                     }
1541                     Standard_Integer pI = (Standard_Integer) ciVpar;
1542                     
1543                     Standard_Integer iP;
1544                     for( iP = 1; iP <= cNbP; iP++) {
1545                       if( pI == iP )
1546                       {
1547                         IntSurf_PntOn2S newPnt = MakeNewPoint(replacePnt, wline->Point(iP), Periods);
1548                         newL2s->Add(newPnt);
1549                       }
1550                       else if(removeNext && iP == (pI + 1))
1551                         continue;
1552                       else if(removePrev && iP == (pI - 1))
1553                         continue;
1554                       else
1555                         newL2s->Add(wline->Point(iP));
1556                     }
1557                     
1558                     IntPatch_Point newVtx;
1559                     gp_Pnt Pnt3dV = aWLine->Vertex(VDMin).Value();
1560                     newVtx.SetValue(Pnt3dV,TolTang,Standard_False);
1561                     newVtx.SetParameters(u21,v21,u22,v22);
1562                     newVtx.SetParameter(VPold.Value(ciV));
1563                     
1564                     Handle(IntPatch_WLine) NWLine = new IntPatch_WLine(newL2s,Standard_False,trans1,trans2);
1565                     
1566                     Standard_Integer iV;
1567                     for( iV = 1; iV <= cnbV; iV++ ) {
1568                       if( iV == ciV )
1569                         NWLine->AddVertex(newVtx);
1570                       else {
1571                         IntPatch_Point theVtx = wline->Vertex(iV);
1572                         theVtx.SetParameter(VPold.Value(iV));
1573                         NWLine->AddVertex(theVtx);
1574                       }
1575                     }
1576                     
1577                     wline = NWLine;
1578                   }
1579                 }
1580               }// SLin.Length > 0
1581               
1582               AddWLine(SLin, wline, Deflection);
1583               empt = Standard_False;
1584             }// !RejetLigne
1585           }// PW points > 2
1586         }// done is True
1587       }// dminiPointLigne > SeuildPointLigne
1588     }// HasStartPoint  
1589   }// for( IterLOP ...
1590   done = Standard_True;
1591   return;      
1592 }
1593 //=======================================================================
1594 //function : Perform
1595 //purpose  : 
1596 //=======================================================================
1597 void IntPatch_PrmPrmIntersection::Perform(const Handle(Adaptor3d_HSurface)&    Surf1,
1598                                           const Handle(Adaptor3d_TopolTool)& D1,
1599                                           const Handle(Adaptor3d_HSurface)&    Surf2,
1600                                           const Handle(Adaptor3d_TopolTool)& D2,
1601                                           const Standard_Real   U1Depart,
1602                                           const Standard_Real   V1Depart,
1603                                           const Standard_Real   U2Depart,
1604                                           const Standard_Real   V2Depart,
1605                                           const Standard_Real   TolTangency,
1606                                           const Standard_Real   Epsilon,
1607                                           const Standard_Real   Deflection,
1608                                           const Standard_Real   Increment) { 
1609   
1610   
1611   
1612 //    Standard_Integer NbU1 = D1->NbSamplesU();
1613 //    Standard_Integer NbV1 = D1->NbSamplesV();
1614 //    Standard_Integer NbU2 = D2->NbSamplesU();
1615 //    Standard_Integer NbV2 = D2->NbSamplesV();
1616
1617   //-- Traitement des Lignes de sections
1618   empt = Standard_True;
1619   done = Standard_True;
1620   SLin.Clear();  
1621   
1622   //------------------------------------------------------------
1623
1624   Standard_Real pu1,pu2,pv1,pv2;
1625   
1626   TColStd_Array1OfReal StartParams(1,4);
1627   
1628 //    Standard_Integer MaxOscill = NbU1;
1629 //    if(MaxOscill < NbU2) MaxOscill=NbU2;
1630 //    if(MaxOscill < NbV1) MaxOscill=NbV1;
1631 //    if(MaxOscill < NbV2) MaxOscill=NbV2;
1632   
1633 //    Standard_Real nIncrement=Increment;
1634 //    if(MaxOscill>10) { 
1635 //  #ifdef DEB 
1636 //      cout<<"\n IntPatch_PrmPrmIntersection.gxx : Increment:"<<Increment<<" -> "<<Increment/(0.5*MaxOscill)<<endl;
1637 //  #endif
1638 //      nIncrement/=0.5*MaxOscill;
1639 //    }
1640   
1641   IntPatch_ThePWalkingInter PW(Surf1,Surf2,
1642                                TolTangency,
1643                                Epsilon,
1644                                Deflection,
1645                                Increment); //nIncrement);
1646   
1647   
1648   //Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
1649   //Standard_Real    incidence;
1650   //Standard_Real    dminiPointLigne;
1651   
1652   Standard_Boolean HasStartPoint;//,RejetLigne;
1653   
1654   IntSurf_PntOn2S StartPOn2S;
1655   
1656   //Standard_Integer ver;
1657   
1658   gp_Pnt Point3dDebut,Point3dFin;
1659   
1660   //------------------------------------------------------------
1661   
1662   StartParams(1) = U1Depart;
1663   StartParams(2) = V1Depart;
1664   StartParams(3) = U2Depart;
1665   StartParams(4) = V2Depart;
1666   
1667   //-----------------------------------------------------------------------
1668   //-- Calcul du premier point de cheminement a partir du point approche --
1669   //-----------------------------------------------------------------------
1670   HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S); 
1671   if(HasStartPoint) { 
1672     //-------------------------------------------------
1673     //-- Un point a ete trouve                       --
1674     //-- On verifie qu il n appartient pas           --
1675     //--  a une ligne de cheminement deja calculee.  --
1676     //-------------------------------------------------
1677     
1678     PW.Perform(StartParams);
1679     if(PW.IsDone()) {
1680       
1681       Point3dDebut = PW.Value(1).Value();
1682       Point3dFin   = PW.Value(PW.NbPoints()).Value();
1683
1684       IntSurf_TypeTrans trans1,trans2;
1685       Standard_Real locu,locv;
1686       gp_Vec norm1,norm2,d1u,d1v;
1687       gp_Pnt ptbid;
1688       Standard_Integer indextg;
1689       gp_Vec tgline(PW.TangentAtLine(indextg));
1690       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1691       Surf1->D1(locu,locv,ptbid,d1u,d1v);
1692       norm1 = d1u.Crossed(d1v);
1693       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1694       Surf2->D1(locu,locv,ptbid,d1u,d1v);
1695       norm2 = d1u.Crossed(d1v);
1696       if (tgline.DotCross(norm2,norm1)>0.) {
1697         trans1 = IntSurf_Out;
1698         trans2 = IntSurf_In;
1699       }
1700       else {
1701         trans1 = IntSurf_In;
1702         trans2 = IntSurf_Out;
1703       }
1704       
1705       
1706       
1707       Standard_Real TolTang = TolTangency;
1708       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1709       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1710       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1711       
1712       //---------------
1713       if(wline->NbVertex() == 0) {
1714         IntPatch_Point vtx;
1715         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1716         POn2S.Parameters(pu1,pv1,pu2,pv2);
1717         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1718         vtx.SetParameters(pu1,pv1,pu2,pv2);
1719         vtx.SetParameter(1);
1720         wline->AddVertex(vtx);
1721         
1722         POn2S = PW.Line()->Value(wline->NbPnts());
1723         POn2S.Parameters(pu1,pv1,pu2,pv2);
1724         vtx.SetValue(Point3dFin,TolTang,Standard_False);
1725         vtx.SetParameters(pu1,pv1,pu2,pv2);
1726         vtx.SetParameter(wline->NbPnts());
1727         wline->AddVertex(vtx);
1728       }
1729       
1730       //---------------
1731       SLin.Append(wline);
1732       empt = Standard_False;
1733       
1734     }
1735   }
1736 }
1737 //==================================================================================
1738 // function : AdjustOnPeriodic
1739 // purpose  : 
1740 //==================================================================================
1741 void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
1742                       const Handle(Adaptor3d_HSurface)& Surf2,
1743                       IntPatch_SequenceOfLine& aSLin)
1744 {
1745   Standard_Boolean bIsPeriodic[4], bModified, bIsNull, bIsPeriod;
1746   Standard_Integer i, j, k, aNbLines, aNbPx, aIndx, aIndq;
1747   Standard_Real aPeriod[4], dPeriod[4], ux[4], uq[4], aEps, du;
1748   //
1749   aEps=Precision::Confusion();
1750   //
1751   for (k=0; k<4; ++k) {
1752     aPeriod[k]=0.;
1753   }
1754   //
1755   bIsPeriodic[0]=Surf1->IsUPeriodic();
1756   bIsPeriodic[1]=Surf1->IsVPeriodic();
1757   bIsPeriodic[2]=Surf2->IsUPeriodic();
1758   bIsPeriodic[3]=Surf2->IsVPeriodic();
1759   //
1760   if (bIsPeriodic[0]){
1761     aPeriod[0]=Surf1->UPeriod();
1762   }
1763   if (bIsPeriodic[1]){
1764     aPeriod[1]=Surf1->VPeriod();
1765   }
1766   if (bIsPeriodic[2]){
1767     aPeriod[2]=Surf2->UPeriod();
1768   }
1769   if (bIsPeriodic[3]){
1770     aPeriod[3]=Surf2->VPeriod();
1771   }
1772   //
1773   for (k=0; k<4; ++k) {
1774     dPeriod[k]=0.25*aPeriod[k];
1775   }
1776   //
1777   aNbLines=aSLin.Length();
1778   for (i=1; i<=aNbLines; ++i) {
1779     Handle(IntPatch_WLine) aIL=Handle(IntPatch_WLine)::DownCast(aSLin.Value(i));
1780     Handle(IntSurf_LineOn2S) aL=aIL->Curve();
1781
1782     aNbPx=aL->NbPoints();
1783     if (aNbPx<10) {
1784       continue;
1785     }
1786     //
1787     for (j=0; j<2; ++j) {
1788       bModified=Standard_False;
1789       aIndx=1;
1790       aIndq=2;
1791       if (j) {
1792         aIndx=aNbPx;
1793         aIndq=aNbPx-1;
1794       }
1795       //
1796       const IntSurf_PntOn2S& aPSx=aL->Value(aIndx);
1797       const IntSurf_PntOn2S& aPSq=aL->Value(aIndq);
1798       //
1799       aPSx.Parameters(ux[0], ux[1], ux[2], ux[3]);
1800       aPSq.Parameters(uq[0], uq[1], uq[2], uq[3]);
1801       //
1802       for (k=0; k<4; ++k) {
1803         bIsNull=Standard_False;
1804         bIsPeriod=Standard_False;
1805         //
1806         if (!bIsPeriodic[k]) {
1807           continue;
1808         }
1809         //
1810         if (fabs(ux[k])<aEps) {
1811           bModified=Standard_True;
1812           bIsNull=Standard_True;
1813         }
1814         //
1815         else if (fabs(ux[k]-aPeriod[k])<aEps) {
1816           bModified=Standard_True;
1817           bIsPeriod=Standard_True;
1818         }
1819         //
1820         if (bModified) {
1821           du=fabs(ux[k]-uq[k]);
1822           if (du > dPeriod[k]) {
1823             if(bIsNull){
1824               ux[k]=aPeriod[k];
1825             }
1826             if(bIsPeriod) {
1827               ux[k]=0.;
1828             }
1829           }
1830         }
1831       }//for (k=0; k<4; ++k) 
1832       if (bModified) {
1833         IntSurf_PntOn2S aPntOn2S;
1834         //
1835         aPntOn2S=aPSx;  
1836         aPntOn2S.SetValue(ux[0], ux[1], ux[2], ux[3]);
1837         aL->Value(aIndx, aPntOn2S);
1838       }
1839     }//for (j=0; j<1; ++j) {
1840   }//for (i=1; i<=aNbLines; ++i)
1841 }
1842
1843 //==================================================================================
1844 // function : MakeNewPoint
1845 // purpose  : 
1846 //==================================================================================
1847 IntSurf_PntOn2S MakeNewPoint(const IntSurf_PntOn2S& replacePnt,
1848                              const IntSurf_PntOn2S& oldPnt,
1849                              const Standard_Real* Periods)
1850 {
1851   IntSurf_PntOn2S NewPoint;
1852   NewPoint.SetValue(replacePnt.Value());
1853   
1854   Standard_Real OldParams[4], NewParams[4];
1855   oldPnt.Parameters(OldParams[0], OldParams[1], OldParams[2], OldParams[3]);
1856   replacePnt.Parameters(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
1857
1858   Standard_Integer i;
1859   for (i = 0; i < 4; i++)
1860     if (Periods[i] != 0.)
1861     {
1862       if (Abs(NewParams[i] - OldParams[i]) >= 0.5*Periods[i])
1863       {
1864         if (NewParams[i] < OldParams[i])
1865           NewParams[i] += Periods[i];
1866         else
1867           NewParams[i] -= Periods[i];
1868       }
1869     }
1870
1871   NewPoint.SetValue(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
1872   return NewPoint;
1873 }
1874
1875 //==================================================================================
1876 // function : Perform
1877 // purpose  : base SS Int. function
1878 //==================================================================================
1879 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
1880                                            const Handle(Adaptor3d_TopolTool)& D1,
1881                                            const Handle(Adaptor3d_HSurface)& Surf2,
1882                                            const Handle(Adaptor3d_TopolTool)& D2,
1883                                            const Standard_Real   TolTangency,
1884                                            const Standard_Real   Epsilon,
1885                                            const Standard_Real   Deflection,
1886                                            const Standard_Real   Increment,
1887                                            const Standard_Boolean ClearFlag) 
1888 {
1889   Standard_Integer NbU1, NbV1, NbU2, NbV2, Limit; 
1890   //
1891   D1->SamplePnts(Deflection, 10, 10);
1892   D2->SamplePnts(Deflection, 10, 10);
1893   //
1894   NbU1 = D1->NbSamplesU();
1895   NbV1 = D1->NbSamplesV();
1896   NbU2 = D2->NbSamplesU();
1897   NbV2 = D2->NbSamplesV();
1898   TColStd_Array1OfReal anUpars1(1, NbU1), aVpars1(1, NbV1);
1899   TColStd_Array1OfReal anUpars2(1, NbU2), aVpars2(1, NbV2);
1900   //
1901   D1->UParameters(anUpars1); 
1902   D1->VParameters(aVpars1);
1903   D2->UParameters(anUpars2); 
1904   D2->VParameters(aVpars2);
1905
1906   Standard_Real Periods [4];
1907   Periods[0] = (Surf1->IsUPeriodic())? Surf1->UPeriod() : 0.;
1908   Periods[1] = (Surf1->IsVPeriodic())? Surf1->VPeriod() : 0.;
1909   Periods[2] = (Surf2->IsUPeriodic())? Surf2->UPeriod() : 0.;
1910   Periods[3] = (Surf2->IsVPeriodic())? Surf2->VPeriod() : 0.;
1911   
1912   //---------------------------------------------
1913   Limit = 2500;
1914   if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit)) {  
1915     empt = Standard_True;
1916     if (ClearFlag){
1917       SLin.Clear();
1918     }
1919     //
1920     IntPolyh_Intersection* pInterference = NULL;
1921
1922     if ( D1->IsUniformSampling() || D2->IsUniformSampling() ) {
1923       pInterference = new IntPolyh_Intersection(Surf1,NbU1,NbV1,Surf2,NbU2,NbV2);
1924     }
1925     else {
1926       pInterference = new IntPolyh_Intersection(Surf1, anUpars1, aVpars1, 
1927                                                 Surf2, anUpars2, aVpars2 );
1928     }
1929     if ( !pInterference ) {
1930       done = Standard_False;
1931       return;
1932     }
1933     //
1934     IntPolyh_Intersection& Interference = *pInterference;
1935     //
1936     done = Interference.IsDone();
1937     if( !done ) {
1938       if (pInterference) {
1939         delete pInterference;
1940         pInterference = NULL;
1941       }
1942       return;
1943     }
1944     
1945     Standard_Integer nbLigSec = Interference.NbSectionLines();
1946     Standard_Integer nbTanZon = Interference.NbTangentZones();
1947     Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
1948
1949     Standard_Integer NbLigCalculee = 0, ver;
1950     Standard_Real U1,U2,V1,V2, pu1,pu2,pv1,pv2, incidence, dminiPointLigne;
1951     Standard_Boolean HasStartPoint,RejetLigne;
1952     IntSurf_PntOn2S StartPOn2S;
1953     gp_Pnt Point3dDebut,Point3dFin;
1954
1955     TColStd_Array1OfReal StartParams(1,4);
1956     IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);
1957
1958     if(nbLigSec>=1) {  
1959       Standard_Integer *TabL = new Standard_Integer [nbLigSec+1];
1960       Standard_Integer ls;
1961       for(ls=1; ls<=nbLigSec; ++ls){
1962         TabL[ls]=ls;
1963       }
1964       //----------------------------------------1.1
1965       Standard_Boolean triok;
1966       do { 
1967         Standard_Integer b, nb_B, nb_A, tyu;
1968         //
1969         triok=Standard_True;
1970         for( b = 2; b <= nbLigSec; ++b ) { 
1971           nb_B = Interference.NbPointsInLine(TabL[b]);
1972           nb_A = Interference.NbPointsInLine(TabL[b-1]);
1973           if( nb_B > nb_A ) { 
1974             tyu=TabL[b]; 
1975             TabL[b]=TabL[b-1];
1976             TabL[b-1]=tyu;
1977             triok=Standard_False;
1978           }
1979         }
1980       } while(triok==Standard_False);
1981       //----------------------------------------
1982       // 1.2 For the line "ls" get 2D-bounds U,V for surfaces 1,2
1983       //
1984       for( ls = 1; ls <= nbLigSec; ++ls) {
1985         Standard_Integer nbp, ilig, *TabPtDep;
1986         //
1987         nbp = Interference.NbPointsInLine(TabL[ls]);
1988         if (!nbp) {
1989           continue;
1990         }
1991         //
1992         TabPtDep = new Standard_Integer [nbp+1];
1993         for( ilig = 1; ilig <= nbp; ++ilig ) {
1994           TabPtDep[ilig]=0;
1995         }
1996         //
1997         Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
1998         Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
1999         Standard_Real _x,_y,_z;
2000         //
2001         Interference.GetLinePoint(TabL[ls], 1, 
2002                                   _x,_y,_z, 
2003                                   UminLig1, VminLig1, UminLig2, VminLig2, 
2004                                   incidence);
2005         
2006         UmaxLig1=UminLig1;
2007         VmaxLig1=VminLig1;
2008         UmaxLig2=UminLig2;
2009         VmaxLig2=VminLig2;
2010         //
2011         for( ilig = 2; ilig <= nbp; ilig++ ) { 
2012           Interference.GetLinePoint(TabL[ls],ilig,_x,_y,_z,U1,V1,U2,V2,incidence);
2013           //
2014           if(U1>UmaxLig1) UmaxLig1=U1;
2015           if(V1>VmaxLig1) VmaxLig1=V1;
2016           if(U2>UmaxLig2) UmaxLig2=U2;
2017           if(V2>VmaxLig2) VmaxLig2=V2;
2018           //
2019           if(U1<UminLig1) UminLig1=U1;
2020           if(V1<VminLig1) VminLig1=V1;
2021           if(U2<UminLig2) UminLig2=U2;
2022           if(V2<VminLig2) VminLig2=V2;
2023         }//for( ilig = 2; ilig <= nbp; ilig++ ) { 
2024         //
2025         //----------------------------------------
2026         // 1.3
2027         Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
2028         Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
2029         Standard_Boolean lignetrouvee=Standard_False;
2030         const Standard_Integer NbDePointsDeDepartDuChmLimit = 5;
2031         //
2032         do { 
2033           NombreDePointsDeDepartDuCheminement++;
2034           switch (NombreDePointsDeDepartDuCheminement) {
2035           case 1:
2036             nbps2 = (nbp > 1) ? nbp/2 : 1;
2037             if(nbp<3) 
2038               NombreDePointsDeDepartDuCheminement = NbDePointsDeDepartDuChmLimit;
2039             break;
2040           case 2:
2041             nbps2 = 1;
2042             break;
2043           case 3:
2044             nbps2 = nbp-1;
2045             break;
2046           case 4:
2047             nbps2 = 3 * nbp / 4;
2048             break;
2049           case 5:
2050             nbps2 = nbp / 4;
2051             break;
2052           default:
2053             nbps2 = NombreDePointsDeDepartDuCheminement-3;
2054             NombreDePointsDeDepartDuCheminement++;
2055           }
2056           //
2057           if(TabPtDep[nbps2] == 0) {
2058             TabPtDep[nbps2] = 1;
2059             Interference.GetLinePoint(TabL[ls],nbps2,_x,_y,_z,U1,V1,U2,V2,incidence);
2060                       
2061             StartParams(1) = U1;
2062             StartParams(2) = V1;
2063             StartParams(3) = U2;
2064             StartParams(4) = V2;
2065
2066             HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
2067             dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
2068             if(HasStartPoint) {
2069               StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2070               NbLigCalculee = SLin.Length();
2071               Standard_Integer l;
2072               for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
2073                 const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2074
2075                 if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
2076                   dminiPointLigne = 0.0;
2077                 }
2078               }// for( l ...
2079
2080               if(dminiPointLigne > SeuildPointLigne) {
2081                 PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
2082                 //
2083                 Standard_Boolean bPWIsDone;
2084                 Standard_Integer iPWNbPoints, aNbPointsVer;
2085                 Standard_Real aD11, aD12, aD21, aD22, aDx;
2086                 //
2087                 bPWIsDone=PW.IsDone();
2088                 if(bPWIsDone) {
2089                   iPWNbPoints=PW.NbPoints();
2090                   //
2091                   if( iPWNbPoints > 2 ) {
2092                     RejetLigne = Standard_False;
2093                     Point3dDebut = PW.Value(1).Value();
2094                     Point3dFin   = PW.Value(iPWNbPoints).Value();
2095                     for( ver = 1; (!RejetLigne) && (ver<= NbLigCalculee); ++ver) { 
2096                       const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2097                       //
2098                       aNbPointsVer=verwline->NbPnts();
2099                       if (aNbPointsVer<3) {
2100                         continue;
2101                       }
2102                       //
2103                       const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2104                       const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
2105                       //xf
2106                       const gp_Pnt& aP21=verPointDebut.Value();
2107                       const gp_Pnt& aP22=verPointFin.Value();
2108                       //
2109                       aD11=Point3dDebut.Distance(aP21);
2110                       aD12=Point3dDebut.Distance(aP22);
2111                       aD21=Point3dFin.Distance(aP21);
2112                       aD22=Point3dFin.Distance(aP22);
2113                       //
2114                       if((aD11<=TolTangency && aD22<=TolTangency) ||
2115                          (aD12<=TolTangency && aD21<=TolTangency)) {
2116                         Standard_Integer m, mx;
2117                         //
2118                         mx=aNbPointsVer/2;
2119                         if (aNbPointsVer%2) {
2120                           ++mx; 
2121                         }
2122                         //
2123                         const gp_Pnt& aPx=verwline->Point(mx).Value();
2124                         for(m=1; m<iPWNbPoints; ++m){
2125                           const gp_Pnt& aP1=PW.Value(m).Value();
2126                           const gp_Pnt& aP2=PW.Value(m+1).Value();
2127                           gp_Vec aVec12(aP1, aP2);
2128                           if (aVec12.SquareMagnitude()<1.e-20){
2129                             continue;
2130                           }
2131                           //
2132                           gp_Dir aDir12(aVec12);
2133                           gp_Lin aLin12(aP1, aDir12);
2134                           aDx=aLin12.Distance(aPx);
2135                           //modified by NIZNHY-PKV Tue May 10 11:08:07 2011f
2136                           if (aDx<=2.*Epsilon) {
2137                           //if (aDx<=TolTangency) {
2138                           //modified by NIZNHY-PKV Tue May 10 11:08:13 2011t  
2139                             RejetLigne = Standard_True;
2140                             break;
2141                           }
2142                         }//for(m=1; m<iPWNbPoints; ++m){
2143                       }//if((aD11<=TolTangency && aD22<=TolTangency) ||...
2144                     }// for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2145                     //
2146                     if(!RejetLigne) {
2147                       IntSurf_TypeTrans trans1,trans2;
2148                       Standard_Real locu,locv;
2149                       gp_Vec norm1,norm2,d1u,d1v;
2150                       gp_Pnt ptbid;
2151                       Standard_Integer indextg;
2152                       gp_Vec tgline(PW.TangentAtLine(indextg));
2153                       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2154                       Surf1->D1(locu,locv,ptbid,d1u,d1v);
2155                       norm1 = d1u.Crossed(d1v);
2156                       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2157                       Surf2->D1(locu,locv,ptbid,d1u,d1v);
2158                       norm2 = d1u.Crossed(d1v);
2159                       if( tgline.DotCross(norm2,norm1) >= 0. ) {
2160                         trans1 = IntSurf_Out;
2161                         trans2 = IntSurf_In;
2162                       }
2163                       else {
2164                         trans1 = IntSurf_In;
2165                         trans2 = IntSurf_Out;
2166                       }
2167
2168                       Standard_Real TolTang = TolTangency;
2169                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2170                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2171                       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2172
2173                       if(wline->NbVertex() == 0) {
2174                         IntPatch_Point vtx;
2175                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2176                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2177                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2178                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2179                         vtx.SetParameter(1);
2180                         wline->AddVertex(vtx);
2181                         
2182                         POn2S = PW.Line()->Value(wline->NbPnts());
2183                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2184                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
2185                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2186                         vtx.SetParameter(wline->NbPnts());
2187                         wline->AddVertex(vtx);
2188                       }
2189                       
2190                       lignetrouvee = Standard_True;
2191
2192                       Standard_Integer slinlen = SLin.Length();
2193                       if( slinlen > 0 ) {
2194                         Standard_Integer cnbV = wline->NbVertex();
2195                         Standard_Integer ciV;
2196                         for( ciV = 1; ciV <= cnbV; ciV++ ) {
2197                           Standard_Real pntDMin = 1.e+100;
2198                           Standard_Integer VDMin = 0;
2199                           Standard_Integer WLDMin = 0;
2200                           gp_Pnt cPV = wline->Vertex(ciV).Value();
2201                           Standard_Integer iL;
2202                           for( iL = 1; iL <= slinlen; iL++) {
2203                             const Handle(IntPatch_Line)& aSLine = SLin.Value(iL);
2204                             IntPatch_IType aType = aSLine->ArcType();
2205                             if( aType != IntPatch_Walking)
2206                               continue;
2207                             const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
2208                             Standard_Integer tnbV = aWLine->NbVertex();
2209                             Standard_Integer tiV;
2210                             for( tiV = 1; tiV <= tnbV; tiV++ ) {
2211                               gp_Pnt tPV = aWLine->Vertex(tiV).Value();
2212                               Standard_Real tDistance = cPV.Distance(tPV);
2213                               Standard_Real uRs1 = Surf1->Surface().UResolution(tDistance);
2214                               Standard_Real vRs1 = Surf1->Surface().VResolution(tDistance);
2215                               Standard_Real uRs2 = Surf2->Surface().UResolution(tDistance);
2216                               Standard_Real vRs2 = Surf2->Surface().VResolution(tDistance);
2217                               Standard_Real RmaxS1 = Max(uRs1,vRs1);
2218                               Standard_Real RmaxS2 = Max(uRs2,vRs2);
2219                               if(RmaxS1 < 1.e-4 && RmaxS2 < 1.e-4) {
2220                                 if( pntDMin > tDistance && tDistance > 1.e-9) {
2221                                   pntDMin = tDistance;
2222                                   VDMin = tiV;
2223                                   WLDMin = iL;
2224                                 }
2225                               }
2226                             }
2227                           }
2228                           
2229                           if( VDMin != 0 ) {
2230                             const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
2231                             const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
2232                             Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
2233                             Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
2234                             Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
2235                             Standard_Real u21 = 0., u22 = 0., v21 = 0., v22 = 0.;
2236                             wline->Point(ciVpar).Parameters(u11,v11,u12,v12);
2237                             aWLine->Point(tiVpar).Parameters(u21,v21,u22,v22);
2238
2239                             Handle(IntSurf_LineOn2S) newL2s = new IntSurf_LineOn2S();
2240                             IntSurf_PntOn2S replacePnt = aWLine->Point(tiVpar);
2241                             Standard_Integer cNbP = wline->NbPnts();
2242
2243                             TColStd_SequenceOfInteger VPold;
2244                             Standard_Integer iPo;
2245                             for( iPo = 1; iPo <= cnbV; iPo++ ) {
2246                               Standard_Real Po = wline->Vertex(iPo).ParameterOnLine();
2247                               Standard_Integer IPo = (Standard_Integer) Po;
2248                               VPold.Append(IPo);
2249                             }
2250                             
2251                             Standard_Boolean removeNext = Standard_False;
2252                             Standard_Boolean removePrev = Standard_False;
2253                             if( ciV == 1) {
2254                               Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
2255                               if(dPar > 10) {
2256                                 removeNext = Standard_True;
2257                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2258                                   VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2259                               }
2260                             }
2261                             else if( ciV == cnbV) {
2262                               Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
2263                               if(dPar > 10) {
2264                                 removePrev = Standard_True;
2265                                 VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
2266                               }
2267                             }
2268                             else {
2269                               Standard_Integer dParMi = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
2270                               Standard_Integer dParMa = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
2271                               if(dParMi > 10) {
2272                                 removePrev = Standard_True;
2273                                 VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
2274                               }
2275                               if(dParMa > 10) {
2276                                 removeNext = Standard_True;
2277                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ ) {
2278                                   if(dParMi > 10)
2279                                     VPold.SetValue(iPo, VPold.Value(iPo) - 2 );
2280                                   else
2281                                     VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2282                                 }
2283                               }
2284                               else {
2285                                 if(dParMi > 10)
2286                                   for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2287                                     VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2288                               } 
2289                             }
2290                             Standard_Integer pI = (Standard_Integer) ciVpar;
2291
2292                             Standard_Integer iP;
2293                             for( iP = 1; iP <= cNbP; iP++) {
2294                               if( pI == iP )
2295                               {
2296                                 IntSurf_PntOn2S newPnt = MakeNewPoint(replacePnt, wline->Point(iP), Periods);
2297                                 newL2s->Add(newPnt);
2298                               }
2299                               else if(removeNext && iP == (pI + 1))
2300                                 continue;
2301                               else if(removePrev && iP == (pI - 1))
2302                                 continue;
2303                               else
2304                                 newL2s->Add(wline->Point(iP));
2305                             }
2306
2307                             IntPatch_Point newVtx;
2308                             gp_Pnt Pnt3dV = aWLine->Vertex(VDMin).Value();
2309                             newVtx.SetValue(Pnt3dV,TolTang,Standard_False);
2310                             newVtx.SetParameters(u21,v21,u22,v22);
2311                             newVtx.SetParameter(VPold.Value(ciV));
2312
2313                             Handle(IntPatch_WLine) NWLine = new IntPatch_WLine(newL2s,Standard_False,trans1,trans2);
2314
2315                             Standard_Integer iV;
2316                             for( iV = 1; iV <= cnbV; iV++ ) {
2317                               if( iV == ciV )
2318                                 NWLine->AddVertex(newVtx);
2319                               else {
2320                                 IntPatch_Point theVtx = wline->Vertex(iV);
2321                                 theVtx.SetParameter(VPold.Value(iV));
2322                                 NWLine->AddVertex(theVtx);
2323                               }
2324                             }
2325
2326                             wline = NWLine;
2327                           }
2328                         }
2329                       }// SLin.Length > 0
2330
2331                       AddWLine(SLin, wline, Deflection);
2332                       empt = Standard_False;
2333                     }// !RejetLigne
2334                   }// PW points > 2
2335                 }// done is True
2336               }// dminiPointLigne > SeuildPointLigne
2337             }// HasStartPoint
2338           }// if TabPtDep[nbps2] == 0
2339         } while(nbp>5 && !( (NombreDePointsDeDepartDuCheminement >= NbDePointsDeDepartDuChmLimit && lignetrouvee) || 
2340                             (NombreDePointsDeDepartDuCheminement-3 >= nbp && (!lignetrouvee))));
2341         delete [] TabPtDep;
2342       }// for( ls ...
2343
2344       delete [] TabL;
2345
2346     }// if nbLigSec >= 1
2347     //
2348     AdjustOnPeriodic(Surf1, Surf2, SLin);
2349     //
2350
2351     //--------------------------------------------------------------------
2352     //-- Calcul des parametres approches a partir des Zones De Tangence --
2353     //--------------------------------------------------------------------
2354     Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2355     Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2356     
2357     UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
2358     UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
2359
2360     // NbPointsInTangentZone always == 1 (eap)
2361     
2362     Standard_Integer z;
2363     for( z=1; z <= nbTanZon; z++) { 
2364       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2365       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) {
2366         Standard_Integer pz=1;
2367         Standard_Real _x,_y,_z;
2368         Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2369
2370         if(U1>UmaxLig1) UmaxLig1=U1;
2371         if(V1>VmaxLig1) VmaxLig1=V1;
2372         if(U2>UmaxLig2) UmaxLig2=U2;
2373         if(V2>VmaxLig2) VmaxLig2=V2;
2374         
2375         if(U1<UminLig1) UminLig1=U1;
2376         if(V1<VminLig1) VminLig1=V1;
2377         if(U2<UminLig2) UminLig2=U2;
2378         if(V2<VminLig2) VminLig2=V2;
2379       //}
2380     }
2381     for(z=1; z <= nbTanZon; z++) {    
2382
2383       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2384       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) { 
2385         Standard_Integer pz=1;
2386         Standard_Real _x,_y,_z;
2387         Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2388
2389         StartParams(1) = U1;
2390         StartParams(2) = V1;
2391         StartParams(3) = U2;
2392         StartParams(4) = V2;
2393         
2394         //-----------------------------------------------------------------------
2395         //-- Calcul du premier point de cheminement a partir du point approche --
2396         //-----------------------------------------------------------------------
2397         HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);   
2398         if(HasStartPoint) { 
2399           //-------------------------------------------------
2400           //-- Un point a ete trouve                       --
2401           //-- On verifie qu il n appartient pas           --
2402           //--  a une ligne de cheminement deja calculee.  --
2403           //-------------------------------------------------
2404           StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2405           
2406           NbLigCalculee = SLin.Length();
2407           dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2408
2409           for(Standard_Integer l=1; 
2410               (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2411               l++) { 
2412             const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2413
2414             if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
2415               dminiPointLigne = 0.0;
2416             }
2417           }
2418
2419           //-- Fin d exploration des lignes
2420           if(dminiPointLigne > SeuildPointLigne) { 
2421             //---------------------------------------------------
2422             //-- Le point de depart du nouveau cheminement     --
2423             //-- n est present dans aucune ligne deja calculee.--
2424             //---------------------------------------------------
2425             PW.Perform(StartParams,
2426                          UminLig1,VminLig1,UminLig2,VminLig2,
2427                          UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
2428             if(PW.IsDone()) {
2429               if(PW.NbPoints()>2) { 
2430                 //-----------------------------------------------
2431                 //-- Verification a posteriori : 
2432                 //-- On teste si le point de depart et de fin de 
2433                 //-- la ligne de cheminement est present dans une 
2434                 //-- autre ligne . 
2435                 //-----------------------------------------------
2436                 RejetLigne = Standard_False;
2437                 Point3dDebut = PW.Value(1).Value();
2438                 const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2439                 Point3dFin   = PointFin.Value();
2440                 
2441                 for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2442                   const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2443                   //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2444
2445                   // Check end point if it is on existing line.
2446                   // Start point is checked before.
2447                   if (IsPointOnLine(PointFin, verwline, Deflection)) {
2448                     RejetLigne = Standard_True; 
2449                     break;
2450                   }
2451
2452                   const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2453                   const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2454                   if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) { 
2455                     RejetLigne = Standard_True; 
2456                   }
2457                   else { 
2458                     if(Point3dFin.Distance(verPointFin.Value()) < TolTangency) { 
2459                       RejetLigne = Standard_True; 
2460                     }
2461                   }
2462                 }
2463                 
2464                 if(!RejetLigne) { 
2465
2466                   IntSurf_TypeTrans trans1,trans2;
2467                   Standard_Real locu,locv;
2468                   gp_Vec norm1,norm2,d1u,d1v;
2469                   gp_Pnt ptbid;
2470                   Standard_Integer indextg;
2471                   gp_Vec tgline(PW.TangentAtLine(indextg));
2472                   PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2473                   Surf1->D1(locu,locv,ptbid,d1u,d1v);
2474                   norm1 = d1u.Crossed(d1v);
2475                   PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2476                   Surf2->D1(locu,locv,ptbid,d1u,d1v);
2477                   norm2 = d1u.Crossed(d1v);
2478                   if (tgline.DotCross(norm2,norm1)>0.) {
2479                     trans1 = IntSurf_Out;
2480                     trans2 = IntSurf_In;
2481                   }
2482                   else {
2483                     trans1 = IntSurf_In;
2484                     trans2 = IntSurf_Out;
2485                   }
2486                   
2487                   
2488                   
2489                   Standard_Real TolTang = TolTangency;
2490                   Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2491                   IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2492                   IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2493                   
2494                   //---------------
2495                     if(wline->NbVertex() == 0) {
2496                       IntPatch_Point vtx;
2497                       IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2498                       POn2S.Parameters(pu1,pv1,pu2,pv2);
2499                       vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2500                       vtx.SetParameters(pu1,pv1,pu2,pv2);
2501                       vtx.SetParameter(1);
2502                       wline->AddVertex(vtx);
2503                       
2504                       POn2S = PW.Line()->Value(wline->NbPnts());
2505                       POn2S.Parameters(pu1,pv1,pu2,pv2);
2506                       vtx.SetValue(Point3dFin,TolTang,Standard_False);
2507                       vtx.SetParameters(pu1,pv1,pu2,pv2);
2508                       vtx.SetParameter(wline->NbPnts());
2509                       wline->AddVertex(vtx);
2510                     }
2511                     
2512                   //---------------
2513                   AddWLine(SLin, wline, Deflection);
2514                   empt = Standard_False;
2515                   
2516                 }
2517                 else { 
2518                   //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2519                 }
2520                 //------------------------------------------------------------          
2521               }
2522             }  //--  le cheminement a reussi (done a True)
2523           }  //--  le point approche ne renvoie pas sur une ligne existante
2524         } //-- Si HasStartPoint
2525       //} //-- Boucle Sur les Points de la Tangent Zone
2526     } //-- Boucle sur Les Tangent Zones
2527
2528     if ( pInterference ) {
2529       delete pInterference;
2530       pInterference = NULL;
2531     }
2532     return;
2533   }// if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit)) {  
2534
2535   Handle(IntSurf_LineOn2S) LOn2S = new IntSurf_LineOn2S();
2536   PointDepart( LOn2S, Surf1, NbU1, NbV1, Surf2, NbU2, NbV2 );
2537   empt = Standard_True;
2538   done = Standard_True;
2539   SLin.Clear();  
2540   
2541   Standard_Integer NbLigCalculee = 0;
2542   Standard_Real U1,U2,V1,V2;
2543   Standard_Real pu1,pu2,pv1,pv2;
2544   
2545   TColStd_Array1OfReal StartParams(1,4);
2546   Standard_Integer MaxOscill = NbU1;
2547   if(MaxOscill < NbU2) MaxOscill=NbU2;
2548   if(MaxOscill < NbV1) MaxOscill=NbV1;
2549   if(MaxOscill < NbV2) MaxOscill=NbV2;
2550   
2551   Standard_Real nIncrement=Increment;
2552   //if(MaxOscill>10)
2553     //nIncrement/=0.5*MaxOscill;
2554
2555   IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,nIncrement);
2556   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
2557   Standard_Real    dminiPointLigne;
2558   Standard_Boolean HasStartPoint,RejetLigne;
2559   IntSurf_PntOn2S StartPOn2S;
2560   Standard_Integer ver;
2561   gp_Pnt Point3dDebut,Point3dFin;
2562
2563   //------------------------------------------------------------
2564   //-- Calcul des parametres approches a partir des Zones De Tangence --
2565   //--------------------------------------------------------------------
2566   Standard_Integer nbTanZon = LOn2S->NbPoints();
2567   for(Standard_Integer z=1; z <= nbTanZon; z++) { 
2568     const IntSurf_PntOn2S& POn2S = LOn2S->Value(z);
2569     POn2S.Parameters(U1,V1,U2,V2);
2570     StartParams(1) = U1;
2571     StartParams(2) = V1;
2572     StartParams(3) = U2;
2573     StartParams(4) = V2;
2574     
2575     //-----------------------------------------------------------------------
2576     //-- Calcul du premier point de cheminement a partir du point approche --
2577     //-----------------------------------------------------------------------
2578     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);       
2579     if(HasStartPoint) { 
2580       //-------------------------------------------------
2581       //-- Un point a ete trouve                       --
2582       //-- On verifie qu il n appartient pas           --
2583       //--  a une ligne de cheminement deja calculee.  --
2584       //-------------------------------------------------
2585       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2586       
2587       NbLigCalculee = SLin.Length();
2588       dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2589       
2590       for(Standard_Integer l=1; 
2591           (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2592           l++) { 
2593         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2594
2595         if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
2596           dminiPointLigne = 0.0;
2597         }
2598       }
2599
2600       //-- Fin d exploration des lignes
2601       if(dminiPointLigne > SeuildPointLigne) { 
2602         //---------------------------------------------------
2603         //-- Le point de depart du nouveau cheminement     --
2604         //-- n est present dans aucune ligne deja calculee.--
2605         //---------------------------------------------------
2606         PW.Perform(StartParams);
2607         if(PW.IsDone()) {
2608           if(PW.NbPoints()>2) { 
2609             //-----------------------------------------------
2610             //-- Verification a posteriori : 
2611             //-- On teste si le point de depart et de fin de 
2612             //-- la ligne de cheminement est present dans une 
2613             //-- autre ligne . 
2614             //-----------------------------------------------
2615             RejetLigne = Standard_False;
2616             Point3dDebut = PW.Value(1).Value();
2617             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2618             Point3dFin   = PointFin.Value();
2619             
2620             for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2621               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2622               //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2623
2624               // Check end point if it is on existing line.
2625               // Start point is checked before.
2626               if (IsPointOnLine(PointFin, verwline, Deflection)) {
2627                 RejetLigne = Standard_True; 
2628                 break;
2629               }
2630
2631               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2632               const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2633               if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) { 
2634                 RejetLigne = Standard_True; 
2635               }
2636               else { 
2637                 if(Point3dFin.Distance(verPointFin.Value()) < TolTangency) { 
2638                   RejetLigne = Standard_True; 
2639                 }
2640               }
2641             }
2642             
2643             if(!RejetLigne) { 
2644               
2645               IntSurf_TypeTrans trans1,trans2;
2646               Standard_Real locu,locv;
2647               gp_Vec norm1,norm2,d1u,d1v;
2648               gp_Pnt ptbid;
2649               Standard_Integer indextg;
2650               gp_Vec tgline(PW.TangentAtLine(indextg));
2651               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2652               Surf1->D1(locu,locv,ptbid,d1u,d1v);
2653               norm1 = d1u.Crossed(d1v);
2654               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2655               Surf2->D1(locu,locv,ptbid,d1u,d1v);
2656               norm2 = d1u.Crossed(d1v);
2657               if (tgline.DotCross(norm2,norm1)>0.) {
2658                 trans1 = IntSurf_Out;
2659                 trans2 = IntSurf_In;
2660               }
2661               else {
2662                 trans1 = IntSurf_In;
2663                 trans2 = IntSurf_Out;
2664               }
2665               
2666               
2667               
2668               Standard_Real TolTang = TolTangency;
2669               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2670               IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2671               IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2672               
2673               //---------------
2674               if(wline->NbVertex() == 0) {
2675                 IntPatch_Point vtx;
2676                 const IntSurf_PntOn2S& POn2Sf = PW.Line()->Value(1);
2677                 POn2Sf.Parameters(pu1,pv1,pu2,pv2);
2678                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2679                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2680                 vtx.SetParameter(1);
2681                 wline->AddVertex(vtx);
2682                 
2683                 const IntSurf_PntOn2S& POn2Sl = PW.Line()->Value(wline->NbPnts());
2684                 POn2Sl.Parameters(pu1,pv1,pu2,pv2);
2685                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
2686                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2687                 vtx.SetParameter(wline->NbPnts());
2688                 wline->AddVertex(vtx);
2689               }
2690               
2691               //---------------
2692               AddWLine(SLin, wline, Deflection);
2693               empt = Standard_False;
2694               
2695             }
2696             else { 
2697               //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2698             }
2699             //------------------------------------------------------------              
2700           }
2701         }  //--  le cheminement a reussi (done a True)
2702       }  //--  le point approche ne renvoie pas sur une ligne existante
2703     } //-- Si HasStartPoint
2704   } //-- Boucle sur Les Tangent Zones
2705     
2706 }
2707 //modified by NIZNHY-PKV Wed May 25 09:39:07 2011f
2708 //=======================================================================
2709 //class : IntPatch_InfoPD
2710 //purpose  : 
2711 //=======================================================================
2712 class IntPatch_InfoPD {
2713  public:
2714   //----------------------------------------C-tor
2715   IntPatch_InfoPD(const Standard_Integer aNBI) {
2716     Standard_Integer aNBI2, i, j;
2717     myNBI=aNBI;
2718     //
2719     aNBI2=aNBI*aNBI;
2720     myP1DS2=new char[aNBI2];
2721     myP2DS1=new char[aNBI2];
2722     myIP1=new Standard_Integer[aNBI2]; 
2723     myIP2=new Standard_Integer[aNBI2];
2724     myP1=new gp_Pnt[aNBI2];
2725     myP2=new gp_Pnt[aNBI2];
2726     //
2727     for (i=0; i<myNBI; ++i) {
2728       for (j=0; j<myNBI; ++j) {
2729         xP1DS2(i, j)=0;
2730         xP2DS1(i, j)=0;
2731         xIP1(i, j)=0;
2732         xIP2(i, j)=0;
2733         xP1(i, j).SetCoord(0., 0., 0.);
2734         xP2(i, j).SetCoord(0., 0., 0.);
2735       }
2736     }
2737   };
2738   //---------------------------------------- D-tor
2739   ~IntPatch_InfoPD() {
2740     delete [] (char*) myP1DS2;
2741     delete [] (char*) myP2DS1;
2742     delete [] (Standard_Integer*) myIP1;
2743     delete [] (Standard_Integer*) myIP2; 
2744     delete [] (gp_Pnt*)myP1;
2745     delete [] (gp_Pnt*)myP2;
2746   };
2747   //---------------------------------------- Index
2748   Standard_Integer Index(const Standard_Integer i,
2749                          const Standard_Integer j) const { 
2750     return i*myNBI+j;
2751   };
2752   //---------------------------------------- NBI
2753   Standard_Integer NBI() const { 
2754     return myNBI;
2755   };
2756   //----------------------------------------xP1DS2
2757   char& xP1DS2(const Standard_Integer i,
2758                const Standard_Integer j) { 
2759     return myP1DS2[Index(i,j)];
2760   };
2761   //----------------------------------------xP2DS1
2762   char& xP2DS1(const Standard_Integer i,
2763                const Standard_Integer j) { 
2764     return myP2DS1[Index(i,j)];
2765   };
2766   //----------------------------------------xIP1
2767   Standard_Integer& xIP1(const Standard_Integer i,
2768                          const Standard_Integer j) { 
2769     return myIP1[Index(i,j)];
2770   };
2771   //----------------------------------------xIP2
2772   Standard_Integer& xIP2(const Standard_Integer i,
2773                          const Standard_Integer j) { 
2774     return myIP2[Index(i,j)];
2775   };
2776   //----------------------------------------xP1
2777   gp_Pnt& xP1(const Standard_Integer i,
2778               const Standard_Integer j) { 
2779     return myP1[Index(i,j)];
2780   };
2781   //----------------------------------------xP1
2782   gp_Pnt& xP2(const Standard_Integer i,
2783               const Standard_Integer j) { 
2784     return myP2[Index(i,j)];
2785   };
2786  protected:
2787   Standard_Integer myNBI;
2788   char *myP1DS2;
2789   char *myP2DS1;
2790   Standard_Integer *myIP1;
2791   Standard_Integer *myIP2;
2792   gp_Pnt *myP1;
2793   gp_Pnt *myP2;
2794 }; 
2795 //modified by NIZNHY-PKV Tue May 24 11:38:55 2011t
2796 //==================================================================================
2797 // function : PointDepart
2798 // purpose  : 
2799 //==================================================================================
2800 void IntPatch_PrmPrmIntersection::PointDepart(Handle(IntSurf_LineOn2S)& LineOn2S,
2801                                               const Handle(Adaptor3d_HSurface)& S1,
2802                                               const Standard_Integer SU_1,
2803                                               const Standard_Integer SV_1,
2804                                               const Handle(Adaptor3d_HSurface)& S2,
2805                                               const Standard_Integer SU_2,
2806                                               const Standard_Integer SV_2) const 
2807
2808   Standard_Integer i, j, xNBI;
2809   //modified by NIZNHY-PKV Tue May 24 11:37:38 2011f
2810   xNBI=200;
2811   IntPatch_InfoPD aIPD(xNBI);
2812   //modified by NIZNHY-PKV Wed May 25 06:47:12 2011t
2813   Standard_Integer iC15, SU1, SV1, SU2, SV2;
2814   Standard_Real U0, U1, V0, V1, U, V;
2815   Standard_Real resu0,resu1,resv0,resv1;
2816   Standard_Real  du1,du2,dv1,dv2, dmaxOn1, dmaxOn2;
2817   Standard_Real x0,y0,z0, x1,y1,z1,d;
2818   Bnd_Box Box1, Box2;
2819   //
2820   iC15=15;
2821   SU1 =iC15*SU_1 ;
2822   SV1 =iC15*SV_1 ;
2823   SU2 =iC15*SU_2 ;
2824   SV2 =iC15*SV_2 ;
2825   //
2826   if(xNBI<SU1) {
2827     SU1 = xNBI;
2828   }
2829   if(xNBI<SV1){
2830     SV1 = xNBI;
2831   }
2832   if(xNBI<SU2){
2833     SU2 = xNBI;
2834   }
2835   if(xNBI<SV2){
2836     SV2 = xNBI;
2837   }
2838   //
2839   U0 = S1->FirstUParameter();
2840   U1 = S1->LastUParameter();
2841   V0 = S1->FirstVParameter();
2842   V1 = S1->LastVParameter();
2843   //
2844   resu0=U0;
2845   resu1=U1;
2846   resv1=V1;
2847   resv0=V0;
2848   //
2849   dmaxOn1 = 0.0;
2850   dmaxOn2 = 0.0;
2851   //-----
2852   du1 = (U1-U0)/(SU1-1);
2853   dv1 = (V1-V0)/(SV1-1);
2854   for(U=U0,i=0; i<SU1; i++,U+=du1) { 
2855     for(V=V0,j=0; j<SV1; V+=dv1,j++) { 
2856       aIPD.xP1(i, j)= S1->Value(U,V);
2857       Box1.Add(aIPD.xP1(i, j));
2858       if(i>0 && j>0) { 
2859         aIPD.xP1(i, j)    .Coord(x0,y0,z0);
2860         aIPD.xP1(i-1, j-1).Coord(x1,y1,z1);
2861         //
2862         d=Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
2863         if(d>dmaxOn1) {
2864           dmaxOn1 = d;
2865         }
2866       }
2867     }
2868   }
2869   Box1.Enlarge(1.e-8);
2870   //
2871   U0 = S2->FirstUParameter();
2872   U1 = S2->LastUParameter();
2873   V0 = S2->FirstVParameter();
2874   V1 = S2->LastVParameter();
2875   //
2876   du2 = (U1-U0)/(SU2-1);  
2877   dv2 = (V1-V0)/(SV2-1);
2878   for(U=U0,i=0; i<SU2; i++,U+=du2) { 
2879     for(V=V0,j=0; j<SV2; V+=dv2,j++) { 
2880       aIPD.xP2(i, j) = S2->Value(U,V);
2881       Box2.Add(aIPD.xP2(i, j));
2882       if(i>0 && j>0) { 
2883         aIPD.xP2(i, j)    .Coord(x0,y0,z0);
2884         aIPD.xP2(i-1, j-1).Coord(x1,y1,z1);
2885         d =  Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
2886         if(d>dmaxOn2) {
2887           dmaxOn2 = d;
2888         }
2889       }
2890     }
2891   }
2892   Box2.Enlarge(1.e-8);
2893   //--------
2894   //
2895   if(Box1.IsOut(Box2)) {
2896     
2897     return;
2898   }
2899   //
2900   Standard_Integer aNbPG;
2901   Standard_Real x10,y10,z10,x11,y11,z11;
2902   Standard_Real x20,y20,z20,x21,y21,z21;
2903   Standard_Real dx, dy, dz, dmax;
2904   Standard_Real dx2, dy2, dz2;
2905   //
2906   Box1.Get(x10,y10,z10,x11,y11,z11);
2907   Box2.Get(x20,y20,z20,x21,y21,z21);
2908   //
2909   x0 = (x10>x20)? x10 : x20;
2910   y0 = (y10>y20)? y10 : y20;
2911   z0 = (z10>z20)? z10 : z20;
2912   //
2913   x1 = (x11<x21)? x11 : x21;
2914   y1 = (y11<y21)? y11 : y21;
2915   z1 = (z11<z21)? z11 : z21;
2916   //
2917   if(dmaxOn2 > dmaxOn1) {
2918     dmaxOn1 = dmaxOn2;
2919   }
2920   //
2921   dmaxOn1+=dmaxOn1;
2922   x0-=dmaxOn1;
2923   y0-=dmaxOn1;
2924   z0-=dmaxOn1;
2925   x1+=dmaxOn1;
2926   y1+=dmaxOn1;
2927   z1+=dmaxOn1;
2928   //
2929   x10-=dmaxOn1;   y10-=dmaxOn1;   z10-=dmaxOn1; 
2930   x11+=dmaxOn1;   y11+=dmaxOn1;   z11+=dmaxOn1; 
2931
2932   x20-=dmaxOn1;   y20-=dmaxOn1;   z20-=dmaxOn1; 
2933   x21+=dmaxOn1;   y21+=dmaxOn1;   z21+=dmaxOn1; 
2934
2935   aNbPG=NbPointsGrille();
2936   dx = (x1-x0)/aNbPG;
2937   dy = (y1-y0)/aNbPG;
2938   dz = (z1-z0)/aNbPG;
2939   //
2940   dmax = dx;
2941   if(dy>dmax) {
2942     dmax = dy;
2943   }
2944   if(dz>dmax){
2945     dmax = dz;
2946   }
2947   //
2948   if(dx<dmax*0.01) {
2949     dx = dmax*0.01;
2950   }
2951   if(dy<dmax*0.01) {
2952     dy = dmax*0.01;
2953   }
2954   if(dz<dmax*0.01) {
2955     dz = dmax*0.01;
2956   }
2957   //
2958   dx2 = dx*0.5;
2959   dy2 = dy*0.5;
2960   dz2 = dz*0.5 ;
2961   //
2962   IntPatch_PrmPrmIntersection_T3Bits M1(_BASE);
2963   IntPatch_PrmPrmIntersection_T3Bits M2(_BASE);
2964   //
2965   for(i=0;i<SU1;i++) { 
2966     for(j=0;j<SV1;j++) { 
2967       aIPD.xIP1(i, j)=-1;
2968       const gp_Pnt& P=aIPD.xP1(i, j);
2969       aIPD.xP1DS2(i, j) = (char)CodeReject(x20,y20,z20,x21,y21,z21,P.X(),P.Y(),P.Z());
2970       int ix = (int)((P.X()-x0  + dx2 )/dx);
2971       if(DansGrille(ix)) { 
2972         int iy = (int)((P.Y()-y0 + dy2)/dy);
2973         if(DansGrille(iy)) {
2974           int iz = (int)((P.Z()-z0 + dz2)/dz);
2975           if(DansGrille(iz)) {
2976             aIPD.xIP1(i, j) = GrilleInteger(ix,iy,iz);
2977           }
2978         }
2979       }
2980     }
2981   }
2982   //-- cout<<" Grille  du 1 fini "<<endl;
2983   for(i=0;i<SU2;i++) { 
2984     for(j=0;j<SV2;j++) { 
2985       aIPD.xIP2(i, j)=-1;
2986       const gp_Pnt& P=aIPD.xP2(i, j);
2987       aIPD.xP2DS1(i, j) = (char)CodeReject(x10,y10,z10,x11,y11,z11,P.X(),P.Y(),P.Z());
2988       int ix = (int)((P.X()-x0 + dx2)/dx);
2989       if(DansGrille(ix)) { 
2990         int iy = (int)((P.Y()-y0 + dy2)/dy);
2991         if(DansGrille(iy)) {
2992           int iz = (int)((P.Z()-z0 + dz2)/dz);
2993           if(DansGrille(iz)) {
2994             aIPD.xIP2(i, j) =  GrilleInteger(ix,iy,iz);
2995           }
2996         }
2997       }
2998     }
2999   }
3000   //
3001   for(i=0;i<SU1-1;i+=1) {
3002     for(j=0;j<SV1-1;j+=1) { 
3003       if(!((aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j)) || 
3004            (aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j+1)))){
3005         Remplit(aIPD.xIP1(i, j),
3006                 aIPD.xIP1(i+1, j),
3007                 aIPD.xIP1(i+1, j+1),
3008                 M1);
3009       }
3010       if(!((aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i, j+1)) || 
3011            (aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j+1)))) {
3012         Remplit(aIPD.xIP1(i, j),
3013                 aIPD.xIP1(i, j+1),
3014                 aIPD.xIP1(i+1, j+1),
3015                 M1);    
3016       }
3017     }
3018   }     
3019   //
3020   for(i=0;i<SU2-1;i+=1) {
3021     for(j=0;j<SV2-1;j+=1) { 
3022       if(!((aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j)) ||
3023            (aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j+1)))){
3024         Remplit(aIPD.xIP2(i, j),
3025                 aIPD.xIP2(i+1, j),
3026                 aIPD.xIP2(i+1, j+1),
3027                 M2);
3028       }
3029       if(!((aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i, j+1)) || 
3030            (aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j+1)))){
3031         Remplit(aIPD.xIP2(i, j),
3032                 aIPD.xIP2(i, j+1),
3033                 aIPD.xIP2(i+1, j+1),
3034                 M2);    
3035       }
3036     }
3037   }     
3038   //
3039   M1.ResetAnd();
3040   M2.ResetAnd();
3041   //
3042   int newind=0;
3043   long unsigned Compt=0;
3044   int ok=0;
3045   int indicepointtraite = 0;
3046   Standard_Integer k,nu,nv;
3047   //
3048   do { 
3049     indicepointtraite--;
3050     ok = M1.And(M2,newind);
3051     if(ok) { 
3052       IntegerGrille(newind,i,j,k);
3053       int nb=0;
3054       int LIM=3;
3055       if(   DansGrille(i-1) && DansGrille(j-1) && DansGrille(k-1) 
3056          && DansGrille(i+1) && DansGrille(j+1) && DansGrille(k+1)) { 
3057         int si,sj,sk;
3058         for(si=-1; si<= 1 && nb<LIM; si++) { 
3059           for(sj=-1; sj<= 1 && nb<LIM; sj++) { 
3060             for(sk=-1; sk<= 1 && nb<LIM; sk++) { 
3061               long unsigned lu=GrilleInteger(i+si,j+sj,k+sk);
3062               if(M1.Val(lu) && M2.Val(lu)) { 
3063                 nb++;
3064               }
3065             }
3066           }
3067         }
3068         if(nb>=LIM) { 
3069           for(si=-1; si<= 1; si++) { 
3070             for(sj=-1; sj<= 1; sj++) { 
3071               for(sk=-1; sk<= 1; sk++) { 
3072                 if(si || sj || sk) { 
3073                   long unsigned lu=GrilleInteger(i+si,j+sj,k+sk);
3074                   M1.Raz(lu);
3075                 }
3076               }
3077             }
3078           }
3079         }
3080       }
3081       //
3082       gp_Pnt P(dx*i + x0, dy*j + y0, dz*k+z0);
3083       //
3084       Standard_Integer nu1=-1,nu2=-1;
3085       Standard_Integer nv1=0, nv2=0;
3086       int nbsur1 = 0;
3087       for(nu=0;nu1<0 && nu<SU1;nu++) { 
3088         for(nv=0;nu1<0 && nv<SV1;nv++) { 
3089           if( aIPD.xIP1(nu, nv) ==(Standard_Integer) newind )  { 
3090             nbsur1++;
3091             aIPD.xIP1(nu, nv)=indicepointtraite;
3092             nu1=nu; nv1=nv;
3093           }
3094         }
3095       }
3096       if(nu1>=0) { 
3097         int nbsur2 = 0;
3098         for(nu=0;nu2<0 && nu<SU2;nu++) { 
3099           for(nv=0;nu2<0 && nv<SV2;nv++) { 
3100             if( aIPD.xIP2(nu, nv)==(Standard_Integer) newind )  { 
3101               nbsur2++;
3102               aIPD.xIP2(nu, nv)=indicepointtraite;
3103               nu2=nu; nv2=nv;
3104             }
3105           }
3106         }
3107       }
3108       if(nu1>=0 && nu2>=0) { 
3109         IntSurf_PntOn2S POn2S;
3110         POn2S.SetValue(P, 
3111                        S1->FirstUParameter()+nu1*du1,
3112                        S1->FirstVParameter()+nv1*dv1,
3113                        S2->FirstUParameter()+nu2*du2,
3114                        S2->FirstVParameter()+nv2*dv2);
3115         LineOn2S->Add(POn2S);
3116         Compt++;
3117       }
3118       else { 
3119         //-- aucun point du triangle n a ete trouve assez proche
3120         //-- on recherche les 3 points les plus proches de P 
3121         //-- dans chacun des tableaux 
3122                   Standard_Real Dist3[3],u3[3] = { 0.0, 0.0, 0.0 },v3[3] = { 0.0, 0.0, 0.0 };
3123         Dist3[0]=Dist3[1]=Dist3[2]=RealLast();
3124         for(U=resu0,i=0; i<SU1; i++,U+=du1) { 
3125           for(V=resv0,j=0; j<SV1; V+=dv1,j++) {       
3126             //-- On place les 3 meilleures valeurs dans Dist1,Dist2,Dist3
3127             Standard_Real t = aIPD.xP1(i, j).SquareDistance(P);
3128             //-- On remplace la plus grande valeur ds Dist[.] par la val courante
3129             if(Dist3[0]<Dist3[1]) { 
3130               Standard_Real z;
3131               z=Dist3[0]; Dist3[0]=Dist3[1]; Dist3[1]=z;
3132               z=u3[0]; u3[0]=u3[1]; u3[1]=z;
3133               z=v3[0]; v3[0]=v3[1]; v3[1]=z;
3134             }
3135             if(Dist3[1]<Dist3[2]) { 
3136               Standard_Real z;
3137               z=Dist3[1]; Dist3[1]=Dist3[2]; Dist3[2]=z;
3138               z=u3[1]; u3[1]=u3[2]; u3[2]=z;
3139               z=v3[1]; v3[1]=v3[2]; v3[2]=z;
3140             }
3141             if(Dist3[0]<Dist3[1]) { 
3142               Standard_Real z;
3143               z=Dist3[0]; Dist3[0]=Dist3[1]; Dist3[1]=z;
3144               z=u3[0]; u3[0]=u3[1]; u3[1]=z;
3145               z=v3[0]; v3[0]=v3[1]; v3[1]=z;
3146             }
3147             //-- la plus grande valeur est dans Dist3[0]
3148             if(t<Dist3[0]) { 
3149               Dist3[0]=t; u3[0]=U; v3[0]=V;
3150             }
3151           }
3152         }
3153         //
3154         Standard_Real U1_3 = (u3[0]+u3[1]+u3[2])/3.0;
3155         Standard_Real V1_3 = (v3[0]+v3[1]+v3[2])/3.0;
3156         
3157         Dist3[0]=Dist3[1]=Dist3[2]=RealLast();
3158         for(U=U0,i=0; i<SU2; i++,U+=du2) { 
3159           for(V=V0,j=0; j<SV2; V+=dv2,j++) {       
3160             //-- On place les 3 meilleures valeurs dans Dist1,Dist2,Dist3
3161             Standard_Real t = aIPD.xP2(i, j).SquareDistance(P);
3162             //-- On remplace la plus grande valeur ds Dist3[.] par la val courante
3163             if(Dist3[0]<Dist3[1]) { 
3164               Standard_Real z;
3165               z=Dist3[0]; Dist3[0]=Dist3[1]; Dist3[1]=z;
3166               z=u3[0]; u3[0]=u3[1]; u3[1]=z;
3167               z=v3[0]; v3[0]=v3[1]; v3[1]=z;
3168             }
3169             if(Dist3[1]<Dist3[2]) { 
3170               Standard_Real z;
3171               z=Dist3[1]; Dist3[1]=Dist3[2]; Dist3[2]=z;
3172               z=u3[1]; u3[1]=u3[2]; u3[2]=z;
3173               z=v3[1]; v3[1]=v3[2]; v3[2]=z;
3174             }
3175             if(Dist3[0]<Dist3[1]) { 
3176               Standard_Real z;
3177               z=Dist3[0]; Dist3[0]=Dist3[1]; Dist3[1]=z;
3178               z=u3[0]; u3[0]=u3[1]; u3[1]=z;
3179               z=v3[0]; v3[0]=v3[1]; v3[1]=z;
3180             }
3181             //-- la plus grande valeur est dans Dist3[0]
3182             if(t<Dist3[0]) { 
3183               Dist3[0]=t; u3[0]=U; v3[0]=V;
3184             }
3185           }
3186         }
3187         //
3188         Standard_Real U2_3 = (u3[0]+u3[1]+u3[2])/3.0;
3189         Standard_Real V2_3 = (v3[0]+v3[1]+v3[2])/3.0;
3190         //
3191         IntSurf_PntOn2S POn2S;
3192         POn2S.SetValue(P,U1_3,V1_3,U2_3,V2_3);
3193         LineOn2S->Add(POn2S);
3194         Compt++;        
3195       }
3196     }
3197   }
3198   while(ok);
3199 }
3200
3201 //==================================================================================
3202 // function : IsPointOnLine
3203 // purpose  : 
3204 //==================================================================================
3205
3206 Standard_Boolean IsPointOnLine(const IntSurf_PntOn2S        &thePOn2S,
3207                                const Handle(IntPatch_WLine) &theWLine,
3208                                const Standard_Real           Deflection)
3209 {
3210   Standard_Boolean isOnLine = Standard_False;
3211   Standard_Real Deflection2 = Deflection*Deflection;
3212   Standard_Real pu1, pu2, pv1, pv2;
3213
3214   thePOn2S.Parameters(pu1, pv1, pu2, pv2);
3215
3216   if ((theWLine->IsOutSurf1Box(gp_Pnt2d(pu1, pv1)) == Standard_False) &&
3217       (theWLine->IsOutSurf2Box(gp_Pnt2d(pu2, pv2)) == Standard_False) &&
3218       (theWLine->IsOutBox(thePOn2S.Value())        == Standard_False)) {
3219     const Standard_Integer NbPntOn2SOnLine = theWLine->NbPnts();
3220     Standard_Integer ll;
3221
3222     for (ll=1; ll < NbPntOn2SOnLine && !isOnLine; ll++) {
3223       const gp_Pnt &Pa     = theWLine->Point(ll).Value();
3224       const gp_Pnt &Pb     = theWLine->Point(ll+1).Value();
3225       const gp_Pnt &PStart = thePOn2S.Value();
3226       const gp_Vec  AM(Pa, PStart);
3227       const gp_Vec  MB(PStart,Pb);
3228       const Standard_Real AMMB = AM.Dot(MB);
3229
3230       if(AMMB > 0.0) {
3231         gp_Dir ABN(Pb.X() - Pa.X(), Pb.Y() - Pa.Y(), Pb.Z() - Pa.Z());
3232         Standard_Real lan =  ABN.X()*AM.X() + ABN.Y()*AM.Y() + ABN.Z()*AM.Z();
3233         gp_Vec AH(lan*ABN.X(), lan*ABN.Y(), lan*ABN.Z());
3234         gp_Vec HM(AM.X() - AH.X(), AM.Y() - AH.Y(), AM.Z() - AH.Z());
3235         Standard_Real d = 0.0;
3236   
3237         if(HM.X() < Deflection) {
3238           d += HM.X()*HM.X();
3239
3240           if(HM.Y() < Deflection) {
3241             d += HM.Y()*HM.Y();
3242
3243             if(HM.Z() < Deflection) {
3244               d += HM.Z()*HM.Z();
3245             } else {
3246               d = Deflection2;
3247             }
3248           } else {
3249             d = Deflection2;
3250           }
3251         } else {
3252           d = Deflection2;
3253         }
3254
3255         if(d < Deflection2) {
3256           isOnLine = Standard_True;
3257         }
3258       } else {
3259         Standard_Real dab = Pa.SquareDistance(Pb);
3260         Standard_Real dap = Pa.SquareDistance(PStart);
3261
3262         if(dap < dab) {
3263           isOnLine = Standard_True;
3264         } else {
3265           Standard_Real dbp = Pb.SquareDistance(PStart);
3266
3267           if(dbp < dab) {
3268             isOnLine = Standard_True;
3269           }
3270         }
3271       }
3272     }
3273   }
3274
3275   return isOnLine;
3276 }
3277
3278 //==================================================================================
3279 // function : AddWLine
3280 // purpose  : 
3281 //==================================================================================
3282
3283 void AddWLine(IntPatch_SequenceOfLine      &theLines,
3284               const Handle(IntPatch_WLine) &theWLine,
3285               const Standard_Real           Deflection)
3286 {
3287   Standard_Integer i = 1;
3288   Standard_Integer aNbLines = theLines.Length();
3289   Standard_Boolean isToRemove;
3290
3291   // Check each line of theLines if it is on theWLine.
3292   while (i <= aNbLines) {
3293     Handle(IntPatch_WLine) aWLine =
3294       Handle(IntPatch_WLine)::DownCast(theLines.Value(i));
3295     
3296     isToRemove = Standard_False;
3297
3298     if (aWLine.IsNull() == Standard_False) {
3299       // Check the middle point.
3300       Standard_Integer aMidIndex = (aWLine->NbPnts() + 1)/2;
3301
3302       if (aMidIndex > 0) {
3303         const IntSurf_PntOn2S &aPnt = aWLine->Point(aMidIndex);
3304
3305         if (IsPointOnLine(aPnt, theWLine, Deflection)) {
3306           // Middle point is on theWLine. Check vertices.
3307           isToRemove = Standard_True;
3308
3309           Standard_Integer j;
3310           const Standard_Integer aNbVtx = aWLine->NbVertex();
3311
3312           for (j = 1; j <= aNbVtx; j++) {
3313             const IntPatch_Point &aPoint = aWLine->Vertex(j);
3314
3315             if (!IsPointOnLine(aPoint.PntOn2S(), theWLine, Deflection)) {
3316               isToRemove = Standard_False;
3317               break;
3318             }
3319           }
3320         }
3321       }
3322     }
3323
3324     if (isToRemove) {
3325       theLines.Remove(i);
3326       aNbLines--;
3327     } else {
3328       i++;
3329     }
3330   }
3331
3332   // Add theWLine to the sequence of lines.
3333   theLines.Append(theWLine);
3334 }