0024472: Wrong section curves
[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   //    Standard_Integer NbU1 = D1->NbSamplesU();
1611   //    Standard_Integer NbV1 = D1->NbSamplesV();
1612   //    Standard_Integer NbU2 = D2->NbSamplesU();
1613   //    Standard_Integer NbV2 = D2->NbSamplesV();
1614
1615   //-- Traitement des Lignes de sections
1616   empt = Standard_True;
1617   done = Standard_True;
1618   SLin.Clear();  
1619
1620   //------------------------------------------------------------
1621
1622   Standard_Real pu1,pu2,pv1,pv2;
1623
1624   TColStd_Array1OfReal StartParams(1,4);
1625
1626   //    Standard_Integer MaxOscill = NbU1;
1627   //    if(MaxOscill < NbU2) MaxOscill=NbU2;
1628   //    if(MaxOscill < NbV1) MaxOscill=NbV1;
1629   //    if(MaxOscill < NbV2) MaxOscill=NbV2;
1630
1631   //    Standard_Real nIncrement=Increment;
1632   //    if(MaxOscill>10) { 
1633   //  #ifdef DEB 
1634   //      cout<<"\n IntPatch_PrmPrmIntersection.gxx : Increment:"<<Increment<<" -> "<<Increment/(0.5*MaxOscill)<<endl;
1635   //  #endif
1636   //      nIncrement/=0.5*MaxOscill;
1637   //    }
1638
1639   IntPatch_ThePWalkingInter PW(Surf1,Surf2,
1640     TolTangency,
1641     Epsilon,
1642     Deflection,
1643     Increment); //nIncrement);
1644
1645
1646   //Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
1647   //Standard_Real    incidence;
1648   //Standard_Real    dminiPointLigne;
1649
1650   Standard_Boolean HasStartPoint;//,RejetLigne;
1651
1652   IntSurf_PntOn2S StartPOn2S;
1653
1654   //Standard_Integer ver;
1655
1656   gp_Pnt Point3dDebut,Point3dFin;
1657
1658   //------------------------------------------------------------
1659
1660   StartParams(1) = U1Depart;
1661   StartParams(2) = V1Depart;
1662   StartParams(3) = U2Depart;
1663   StartParams(4) = V2Depart;
1664
1665   //-----------------------------------------------------------------------
1666   //-- Calcul du premier point de cheminement a partir du point approche --
1667   //-----------------------------------------------------------------------
1668   HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S); 
1669   if(HasStartPoint) { 
1670     //-------------------------------------------------
1671     //-- Un point a ete trouve                       --
1672     //-- On verifie qu il n appartient pas           --
1673     //--  a une ligne de cheminement deja calculee.  --
1674     //-------------------------------------------------
1675
1676     PW.Perform(StartParams);
1677     if(PW.IsDone()) {
1678
1679       Point3dDebut = PW.Value(1).Value();
1680       Point3dFin   = PW.Value(PW.NbPoints()).Value();
1681
1682       IntSurf_TypeTrans trans1,trans2;
1683       Standard_Real locu,locv;
1684       gp_Vec norm1,norm2,d1u,d1v;
1685       gp_Pnt ptbid;
1686       Standard_Integer indextg;
1687       gp_Vec tgline(PW.TangentAtLine(indextg));
1688       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1689       Surf1->D1(locu,locv,ptbid,d1u,d1v);
1690       norm1 = d1u.Crossed(d1v);
1691       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1692       Surf2->D1(locu,locv,ptbid,d1u,d1v);
1693       norm2 = d1u.Crossed(d1v);
1694       if (tgline.DotCross(norm2,norm1)>0.) {
1695         trans1 = IntSurf_Out;
1696         trans2 = IntSurf_In;
1697       }
1698       else {
1699         trans1 = IntSurf_In;
1700         trans2 = IntSurf_Out;
1701       }
1702
1703
1704
1705       Standard_Real TolTang = TolTangency;
1706       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1707       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1708       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1709
1710       //---------------
1711       if(wline->NbVertex() == 0) {
1712         IntPatch_Point vtx;
1713         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1714         POn2S.Parameters(pu1,pv1,pu2,pv2);
1715         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1716         vtx.SetParameters(pu1,pv1,pu2,pv2);
1717         vtx.SetParameter(1);
1718         wline->AddVertex(vtx);
1719
1720         POn2S = PW.Line()->Value(wline->NbPnts());
1721         POn2S.Parameters(pu1,pv1,pu2,pv2);
1722         vtx.SetValue(Point3dFin,TolTang,Standard_False);
1723         vtx.SetParameters(pu1,pv1,pu2,pv2);
1724         vtx.SetParameter(wline->NbPnts());
1725         wline->AddVertex(vtx);
1726       }
1727
1728       //---------------
1729       SLin.Append(wline);
1730       empt = Standard_False;
1731
1732     }
1733   }
1734 }
1735 //==================================================================================
1736 // function : AdjustOnPeriodic
1737 // purpose  : 
1738 //==================================================================================
1739 void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
1740                       const Handle(Adaptor3d_HSurface)& Surf2,
1741                       IntPatch_SequenceOfLine& aSLin)
1742 {
1743   Standard_Boolean bIsPeriodic[4], bModified, bIsNull, bIsPeriod;
1744   Standard_Integer i, j, k, aNbLines, aNbPx, aIndx, aIndq;
1745   Standard_Real aPeriod[4], dPeriod[4], ux[4], uq[4], aEps, du;
1746   //
1747   aEps=Precision::Confusion();
1748   //
1749   for (k=0; k<4; ++k) {
1750     aPeriod[k]=0.;
1751   }
1752   //
1753   bIsPeriodic[0]=Surf1->IsUPeriodic();
1754   bIsPeriodic[1]=Surf1->IsVPeriodic();
1755   bIsPeriodic[2]=Surf2->IsUPeriodic();
1756   bIsPeriodic[3]=Surf2->IsVPeriodic();
1757   //
1758   if (bIsPeriodic[0]){
1759     aPeriod[0]=Surf1->UPeriod();
1760   }
1761   if (bIsPeriodic[1]){
1762     aPeriod[1]=Surf1->VPeriod();
1763   }
1764   if (bIsPeriodic[2]){
1765     aPeriod[2]=Surf2->UPeriod();
1766   }
1767   if (bIsPeriodic[3]){
1768     aPeriod[3]=Surf2->VPeriod();
1769   }
1770   //
1771   for (k=0; k<4; ++k) {
1772     dPeriod[k]=0.25*aPeriod[k];
1773   }
1774   //
1775   aNbLines=aSLin.Length();
1776   for (i=1; i<=aNbLines; ++i) {
1777     Handle(IntPatch_WLine) aIL=Handle(IntPatch_WLine)::DownCast(aSLin.Value(i));
1778     Handle(IntSurf_LineOn2S) aL=aIL->Curve();
1779
1780     aNbPx=aL->NbPoints();
1781     if (aNbPx<10) {
1782       continue;
1783     }
1784     //
1785     for (j=0; j<2; ++j) {
1786       bModified=Standard_False;
1787       aIndx=1;
1788       aIndq=2;
1789       if (j) {
1790         aIndx=aNbPx;
1791         aIndq=aNbPx-1;
1792       }
1793       //
1794       const IntSurf_PntOn2S& aPSx=aL->Value(aIndx);
1795       const IntSurf_PntOn2S& aPSq=aL->Value(aIndq);
1796       //
1797       aPSx.Parameters(ux[0], ux[1], ux[2], ux[3]);
1798       aPSq.Parameters(uq[0], uq[1], uq[2], uq[3]);
1799       //
1800       for (k=0; k<4; ++k) {
1801         bIsNull=Standard_False;
1802         bIsPeriod=Standard_False;
1803         //
1804         if (!bIsPeriodic[k]) {
1805           continue;
1806         }
1807         //
1808         if (fabs(ux[k])<aEps) {
1809           bModified=Standard_True;
1810           bIsNull=Standard_True;
1811         }
1812         //
1813         else if (fabs(ux[k]-aPeriod[k])<aEps) {
1814           bModified=Standard_True;
1815           bIsPeriod=Standard_True;
1816         }
1817         //
1818         if (bModified) {
1819           du=fabs(ux[k]-uq[k]);
1820           if (du > dPeriod[k]) {
1821             if(bIsNull){
1822               ux[k]=aPeriod[k];
1823             }
1824             if(bIsPeriod) {
1825               ux[k]=0.;
1826             }
1827           }
1828         }
1829       }//for (k=0; k<4; ++k) 
1830       if (bModified) {
1831         IntSurf_PntOn2S aPntOn2S;
1832         //
1833         aPntOn2S=aPSx;  
1834         aPntOn2S.SetValue(ux[0], ux[1], ux[2], ux[3]);
1835         aL->Value(aIndx, aPntOn2S);
1836       }
1837     }//for (j=0; j<1; ++j) {
1838   }//for (i=1; i<=aNbLines; ++i)
1839 }
1840
1841 //==================================================================================
1842 // function : MakeNewPoint
1843 // purpose  : 
1844 //==================================================================================
1845 IntSurf_PntOn2S MakeNewPoint(const IntSurf_PntOn2S& replacePnt,
1846                              const IntSurf_PntOn2S& oldPnt,
1847                              const Standard_Real* Periods)
1848 {
1849   IntSurf_PntOn2S NewPoint;
1850   NewPoint.SetValue(replacePnt.Value());
1851
1852   Standard_Real OldParams[4], NewParams[4];
1853   oldPnt.Parameters(OldParams[0], OldParams[1], OldParams[2], OldParams[3]);
1854   replacePnt.Parameters(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
1855
1856   Standard_Integer i;
1857   for (i = 0; i < 4; i++)
1858     if (Periods[i] != 0.)
1859     {
1860       if (Abs(NewParams[i] - OldParams[i]) >= 0.5*Periods[i])
1861       {
1862         if (NewParams[i] < OldParams[i])
1863           NewParams[i] += Periods[i];
1864         else
1865           NewParams[i] -= Periods[i];
1866       }
1867     }
1868
1869     NewPoint.SetValue(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
1870     return NewPoint;
1871 }
1872
1873 //==================================================================================
1874 // function : Perform
1875 // purpose  : base SS Int. function
1876 //==================================================================================
1877 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
1878                                            const Handle(Adaptor3d_TopolTool)& D1,
1879                                            const Handle(Adaptor3d_HSurface)& Surf2,
1880                                            const Handle(Adaptor3d_TopolTool)& D2,
1881                                            const Standard_Real   TolTangency,
1882                                            const Standard_Real   Epsilon,
1883                                            const Standard_Real   Deflection,
1884                                            const Standard_Real   Increment,
1885                                            const Standard_Boolean ClearFlag) 
1886 {
1887   Standard_Integer Limit = 2500;
1888   Standard_Integer NbU1 = 10, NbV1 = 10, NbU2 = 10, NbV2 = 10;
1889   //
1890   D1->SamplePnts(Deflection, NbU1, NbV1);
1891   D2->SamplePnts(Deflection, NbU2, NbV2);
1892   //
1893   NbU1 = D1->NbSamplesU();
1894   NbV1 = D1->NbSamplesV();
1895   NbU2 = D2->NbSamplesU();
1896   NbV2 = D2->NbSamplesV();
1897
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   if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit))
1914   {
1915     empt = Standard_True;
1916     if (ClearFlag)
1917     {
1918       SLin.Clear();
1919     }
1920     //
1921     IntPolyh_Intersection* pInterference = NULL;
1922
1923     if ( D1->IsUniformSampling() || D2->IsUniformSampling() )
1924     {
1925       pInterference = new IntPolyh_Intersection(Surf1,NbU1,NbV1,Surf2,NbU2,NbV2);
1926     }
1927     else
1928     {
1929       pInterference = new IntPolyh_Intersection(Surf1, anUpars1, aVpars1, 
1930         Surf2, anUpars2, aVpars2 );
1931     }
1932
1933     if ( !pInterference )
1934     {
1935       done = Standard_False;
1936       return;
1937     }
1938     //
1939     IntPolyh_Intersection& Interference = *pInterference;
1940     //
1941     done = Interference.IsDone();
1942     if( !done )
1943     {
1944       if (pInterference)
1945       {
1946         delete pInterference;
1947         pInterference = NULL;
1948       }
1949
1950       return;
1951     }
1952
1953     Standard_Integer nbLigSec = Interference.NbSectionLines();
1954     Standard_Integer nbTanZon = Interference.NbTangentZones();
1955     Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
1956
1957     Standard_Integer NbLigCalculee = 0, ver;
1958     Standard_Real pu1,pu2,pv1,pv2, incidence, dminiPointLigne;
1959     Standard_Boolean HasStartPoint = Standard_False, RejectLine = Standard_False;
1960     IntSurf_PntOn2S StartPOn2S;
1961     gp_Pnt Point3dDebut,Point3dFin;
1962
1963     TColStd_Array1OfReal StartParams(1,4);
1964     IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);
1965
1966     if(nbLigSec>=1)
1967     {
1968       Standard_Integer *TabL = new Standard_Integer [nbLigSec+1];
1969       for(Standard_Integer ls=1; ls<=nbLigSec; ++ls)
1970       {
1971         TabL[ls]=ls;
1972       }
1973       //----------------------------------------1.1
1974       {
1975         Standard_Boolean triok;
1976         Standard_Integer nb_A, nb_B, tyu;
1977         do
1978         {
1979           triok=Standard_True;
1980           for(Standard_Integer b = 2; b <= nbLigSec; ++b )
1981           {
1982             nb_B = Interference.NbPointsInLine(TabL[b]);
1983             nb_A = Interference.NbPointsInLine(TabL[b-1]);
1984
1985             if( nb_B > nb_A )
1986             {
1987               tyu=TabL[b];
1988               TabL[b]=TabL[b-1];
1989               TabL[b-1]=tyu;
1990               triok=Standard_False;
1991             }
1992           }
1993         }
1994         while(triok==Standard_False);
1995       }
1996
1997       //----------------------------------------
1998       // 1.2 For the line "ls" get 2D-bounds U,V for surfaces 1,2
1999       //
2000       for(Standard_Integer ls = 1; ls <= nbLigSec; ++ls)
2001       {
2002         Standard_Integer nbp = Interference.NbPointsInLine(TabL[ls]);
2003         if (!nbp)
2004         {
2005           continue;
2006         }
2007         //
2008         Standard_Integer *TabPtDep = new Standard_Integer [nbp+1];
2009         for(Standard_Integer ilig = 1; ilig <= nbp; ++ilig )
2010         {
2011           TabPtDep[ilig]=0;
2012         }
2013         //
2014         Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2015         Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2016         Standard_Real _x,_y,_z;
2017         //
2018         Interference.GetLinePoint(TabL[ls], 1, _x, _y, _z,
2019           UminLig1, VminLig1, UminLig2, VminLig2, 
2020           incidence);
2021
2022         UmaxLig1=UminLig1;
2023         VmaxLig1=VminLig1;
2024         UmaxLig2=UminLig2;
2025         VmaxLig2=VminLig2;
2026         //
2027         for(Standard_Integer ilig = 2; ilig <= nbp; ilig++ )
2028         {
2029           Standard_Real U1, U2, V1, V2;
2030           Interference.GetLinePoint(TabL[ls],ilig,_x,_y,_z,U1,V1,U2,V2,incidence);
2031           //
2032           if(U1>UmaxLig1) UmaxLig1=U1;
2033           if(V1>VmaxLig1) VmaxLig1=V1;
2034           if(U2>UmaxLig2) UmaxLig2=U2;
2035           if(V2>VmaxLig2) VmaxLig2=V2;
2036           //
2037           if(U1<UminLig1) UminLig1=U1;
2038           if(V1<VminLig1) VminLig1=V1;
2039           if(U2<UminLig2) UminLig2=U2;
2040           if(V2<VminLig2) VminLig2=V2;
2041         }//for( ilig = 2; ilig <= nbp; ilig++ ) { 
2042         //
2043         //----------------------------------------
2044         // 1.3
2045         Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
2046         Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
2047         Standard_Boolean lignetrouvee=Standard_False;
2048         const Standard_Integer NbDePointsDeDepartDuChmLimit = 5;
2049         //
2050         do
2051         {
2052           NombreDePointsDeDepartDuCheminement++;
2053           switch (NombreDePointsDeDepartDuCheminement)
2054           {
2055           case 1:
2056             nbps2 = (nbp > 1) ? nbp/2 : 1;
2057             if(nbp<3) 
2058               NombreDePointsDeDepartDuCheminement = NbDePointsDeDepartDuChmLimit;
2059
2060             break;
2061           case 2:
2062             nbps2 = 1;
2063             break;
2064           case 3:
2065             nbps2 = nbp-1;
2066             break;
2067
2068           case 4:
2069             nbps2 = 3 * nbp / 4;
2070             break;
2071
2072           case 5:
2073             nbps2 = nbp / 4;
2074             break;
2075           default:
2076             nbps2 = NombreDePointsDeDepartDuCheminement-3;
2077             NombreDePointsDeDepartDuCheminement++;
2078           }
2079
2080           //
2081           if(TabPtDep[nbps2] == 0)
2082           {
2083             Standard_Real U1, U2, V1, V2;
2084
2085             TabPtDep[nbps2] = 1;
2086             Interference.GetLinePoint(TabL[ls],nbps2,_x,_y,_z,U1,V1,U2,V2,incidence);
2087
2088             StartParams(1) = U1;
2089             StartParams(2) = V1;
2090             StartParams(3) = U2;
2091             StartParams(4) = V2;
2092
2093             HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
2094             dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
2095             if(HasStartPoint)
2096             {
2097               StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2098               NbLigCalculee = SLin.Length();
2099               Standard_Integer l;
2100               for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++)
2101               {
2102                 const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2103
2104                 if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2105                 {
2106                   dminiPointLigne = 0.0;
2107                 }
2108               }// for( l ...
2109
2110               if(dminiPointLigne > SeuildPointLigne)
2111               {
2112                 PW.Perform(StartParams, UminLig1, VminLig1, UminLig2, VminLig2,
2113                   UmaxLig1, VmaxLig1, UmaxLig2, VmaxLig2);
2114
2115                 //
2116                 Standard_Boolean bPWIsDone;
2117                 Standard_Integer iPWNbPoints, aNbPointsVer;
2118                 Standard_Real aD11, aD12, aD21, aD22, aDx;
2119                 //
2120                 bPWIsDone=PW.IsDone();
2121
2122                 if(bPWIsDone)
2123                 {
2124                   iPWNbPoints=PW.NbPoints();
2125                   //
2126                   if( iPWNbPoints > 2 )
2127                   {
2128                     const Standard_Integer aMinNbPoints = 40;
2129                     if(iPWNbPoints < aMinNbPoints)
2130                     {
2131                       PW.SeekAdditionalPoints(Surf1, Surf2, aMinNbPoints);
2132                       iPWNbPoints = PW.NbPoints();
2133                     }
2134                     
2135                     RejectLine = Standard_False;
2136                     Point3dDebut = PW.Value(1).Value();
2137                     Point3dFin   = PW.Value(iPWNbPoints).Value();
2138                     for( ver = 1; (!RejectLine) && (ver<= NbLigCalculee); ++ver)
2139                     {
2140                       const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2141                       //
2142                       aNbPointsVer=verwline->NbPnts();
2143                       if (aNbPointsVer<3)
2144                       {
2145                         continue;
2146                       }
2147                       //
2148                       const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2149                       const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
2150                       //xf
2151                       const gp_Pnt& aP21=verPointDebut.Value();
2152                       const gp_Pnt& aP22=verPointFin.Value();
2153                       //
2154                       aD11=Point3dDebut.Distance(aP21);
2155                       aD12=Point3dDebut.Distance(aP22);
2156                       aD21=Point3dFin.Distance(aP21);
2157                       aD22=Point3dFin.Distance(aP22);
2158                       //
2159                       if((aD11<=TolTangency && aD22<=TolTangency) ||
2160                         (aD12<=TolTangency && aD21<=TolTangency))
2161                       {
2162                         Standard_Integer m, mx;
2163                         //
2164                         mx=aNbPointsVer/2;
2165                         if (aNbPointsVer%2)
2166                         {
2167                           ++mx; 
2168                         }
2169                         //
2170                         const gp_Pnt& aPx=verwline->Point(mx).Value();
2171                         for(m=1; m<iPWNbPoints; ++m)
2172                         {
2173                           const gp_Pnt& aP1=PW.Value(m).Value();
2174                           const gp_Pnt& aP2=PW.Value(m+1).Value();
2175                           gp_Vec aVec12(aP1, aP2);
2176                           if (aVec12.SquareMagnitude()<1.e-20)
2177                           {
2178                             continue;
2179                           }
2180
2181                           //
2182                           gp_Dir aDir12(aVec12);
2183                           gp_Lin aLin12(aP1, aDir12);
2184                           aDx=aLin12.Distance(aPx);
2185
2186                           //modified by NIZNHY-PKV Tue May 10 11:08:07 2011f
2187                           if (aDx<=2.*Epsilon)
2188                           {
2189                             //if (aDx<=TolTangency) {
2190                             //modified by NIZNHY-PKV Tue May 10 11:08:13 2011t
2191
2192                             RejectLine = Standard_True;
2193                             break;
2194                           }
2195                         }//for(m=1; m<iPWNbPoints; ++m){
2196                       }//if((aD11<=TolTangency && aD22<=TolTangency) ||...
2197                     }// for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2198                     //
2199
2200                     if(!RejectLine)
2201                     {
2202                       IntSurf_TypeTrans trans1,trans2;
2203                       Standard_Real locu,locv;
2204                       gp_Vec norm1,norm2,d1u,d1v;
2205                       gp_Pnt ptbid;
2206                       Standard_Integer indextg;
2207                       gp_Vec tgline(PW.TangentAtLine(indextg));
2208                       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2209                       Surf1->D1(locu,locv,ptbid,d1u,d1v);
2210                       norm1 = d1u.Crossed(d1v);
2211                       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2212                       Surf2->D1(locu,locv,ptbid,d1u,d1v);
2213                       norm2 = d1u.Crossed(d1v);
2214                       if( tgline.DotCross(norm2,norm1) >= 0. )
2215                       {
2216                         trans1 = IntSurf_Out;
2217                         trans2 = IntSurf_In;
2218                       }
2219                       else
2220                       {
2221                         trans1 = IntSurf_In;
2222                         trans2 = IntSurf_Out;
2223                       }
2224
2225                       Standard_Real TolTang = TolTangency;
2226                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2227                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2228                       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2229
2230                       if(wline->NbVertex() == 0)
2231                       {
2232                         IntPatch_Point vtx;
2233                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2234                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2235                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2236                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2237                         vtx.SetParameter(1);
2238                         wline->AddVertex(vtx);
2239
2240                         POn2S = PW.Line()->Value(wline->NbPnts());
2241                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2242                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
2243                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2244                         vtx.SetParameter(wline->NbPnts());
2245                         wline->AddVertex(vtx);
2246                       }
2247
2248                       lignetrouvee = Standard_True;
2249
2250                       Standard_Integer slinlen = SLin.Length();
2251                       if( slinlen > 0 )
2252                       {
2253                         Standard_Integer cnbV = wline->NbVertex();
2254                         Standard_Integer ciV;
2255                         for( ciV = 1; ciV <= cnbV; ciV++ )
2256                         {
2257                           Standard_Real pntDMin = 1.e+100;
2258                           Standard_Integer VDMin = 0;
2259                           Standard_Integer WLDMin = 0;
2260                           gp_Pnt cPV = wline->Vertex(ciV).Value();
2261                           Standard_Integer iL;
2262                           for( iL = 1; iL <= slinlen; iL++)
2263                           {
2264                             const Handle(IntPatch_Line)& aSLine = SLin.Value(iL);
2265                             IntPatch_IType aType = aSLine->ArcType();
2266                             if( aType != IntPatch_Walking)
2267                               continue;
2268                             const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
2269                             Standard_Integer tnbV = aWLine->NbVertex();
2270                             Standard_Integer tiV;
2271                             for( tiV = 1; tiV <= tnbV; tiV++ )
2272                             {
2273                               gp_Pnt tPV = aWLine->Vertex(tiV).Value();
2274                               Standard_Real tDistance = cPV.Distance(tPV);
2275                               Standard_Real uRs1 = Surf1->Surface().UResolution(tDistance);
2276                               Standard_Real vRs1 = Surf1->Surface().VResolution(tDistance);
2277                               Standard_Real uRs2 = Surf2->Surface().UResolution(tDistance);
2278                               Standard_Real vRs2 = Surf2->Surface().VResolution(tDistance);
2279                               Standard_Real RmaxS1 = Max(uRs1,vRs1);
2280                               Standard_Real RmaxS2 = Max(uRs2,vRs2);
2281                               if(RmaxS1 < 1.e-4 && RmaxS2 < 1.e-4)
2282                               {
2283                                 if( pntDMin > tDistance && tDistance > 1.e-9)
2284                                 {
2285                                   pntDMin = tDistance;
2286                                   VDMin = tiV;
2287                                   WLDMin = iL;
2288                                 }
2289                               }
2290                             }
2291                           }
2292
2293                           if( VDMin != 0 )
2294                           {
2295                             const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
2296                             const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
2297                             Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
2298                             Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
2299                             Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
2300                             Standard_Real u21 = 0., u22 = 0., v21 = 0., v22 = 0.;
2301                             wline->Point(ciVpar).Parameters(u11,v11,u12,v12);
2302                             aWLine->Point(tiVpar).Parameters(u21,v21,u22,v22);
2303
2304                             Handle(IntSurf_LineOn2S) newL2s = new IntSurf_LineOn2S();
2305                             IntSurf_PntOn2S replacePnt = aWLine->Point(tiVpar);
2306                             Standard_Integer cNbP = wline->NbPnts();
2307
2308                             TColStd_SequenceOfInteger VPold;
2309                             Standard_Integer iPo;
2310                             for( iPo = 1; iPo <= cnbV; iPo++ )
2311                             {
2312                               Standard_Real Po = wline->Vertex(iPo).ParameterOnLine();
2313                               Standard_Integer IPo = (Standard_Integer) Po;
2314                               VPold.Append(IPo);
2315                             }
2316
2317                             Standard_Boolean removeNext = Standard_False;
2318                             Standard_Boolean removePrev = Standard_False;
2319                             if( ciV == 1)
2320                             {
2321                               Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
2322                               if(dPar > 10)
2323                               {
2324                                 removeNext = Standard_True;
2325                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2326                                   VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2327                               }
2328                             }
2329                             else if( ciV == cnbV)
2330                             {
2331                               Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
2332                               if(dPar > 10)
2333                               {
2334                                 removePrev = Standard_True;
2335                                 VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
2336                               }
2337                             }
2338                             else
2339                             {
2340                               Standard_Integer dParMi = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
2341                               Standard_Integer dParMa = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
2342                               if(dParMi > 10)
2343                               {
2344                                 removePrev = Standard_True;
2345                                 VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
2346                               }
2347
2348                               if(dParMa > 10)
2349                               {
2350                                 removeNext = Standard_True;
2351                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2352                                 {
2353                                   if(dParMi > 10)
2354                                     VPold.SetValue(iPo, VPold.Value(iPo) - 2 );
2355                                   else
2356                                     VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2357                                 }
2358                               }
2359                               else
2360                               {
2361                                 if(dParMi > 10)
2362                                   for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2363                                     VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2364                               } 
2365                             }
2366
2367                             Standard_Integer pI = ciVpar;
2368
2369                             Standard_Integer iP;
2370                             for( iP = 1; iP <= cNbP; iP++)
2371                             {
2372                               if( pI == iP )
2373                               {
2374                                 IntSurf_PntOn2S newPnt = MakeNewPoint(replacePnt, wline->Point(iP), Periods);
2375                                 newL2s->Add(newPnt);
2376                               }
2377                               else if(removeNext && iP == (pI + 1))
2378                                 continue;
2379                               else if(removePrev && iP == (pI - 1))
2380                                 continue;
2381                               else
2382                                 newL2s->Add(wline->Point(iP));
2383                             }
2384
2385                             IntPatch_Point newVtx;
2386                             gp_Pnt Pnt3dV = aWLine->Vertex(VDMin).Value();
2387                             newVtx.SetValue(Pnt3dV,TolTang,Standard_False);
2388                             newVtx.SetParameters(u21,v21,u22,v22);
2389                             newVtx.SetParameter(VPold.Value(ciV));
2390
2391                             Handle(IntPatch_WLine) NWLine = new IntPatch_WLine(newL2s,Standard_False,trans1,trans2);
2392
2393                             Standard_Integer iV;
2394                             for( iV = 1; iV <= cnbV; iV++ )
2395                             {
2396                               if( iV == ciV )
2397                                 NWLine->AddVertex(newVtx);
2398                               else
2399                               {
2400                                 IntPatch_Point theVtx = wline->Vertex(iV);
2401                                 theVtx.SetParameter(VPold.Value(iV));
2402                                 NWLine->AddVertex(theVtx);
2403                               }
2404                             }
2405
2406                             wline = NWLine;
2407                           }//if( VDMin != 0 )
2408                         }//for( ciV = 1; ciV <= cnbV; ciV++ )
2409                       }// SLin.Length > 0
2410
2411                       AddWLine(SLin, wline, Deflection);
2412                       empt = Standard_False;
2413                     }// !RejetLigne
2414                   }// PW points > 2
2415                 }// done is True
2416               }// dminiPointLigne > SeuildPointLigne
2417             }// HasStartPoint
2418           }// if TabPtDep[nbps2] == 0
2419         } while(nbp>5 && !( (NombreDePointsDeDepartDuCheminement >= NbDePointsDeDepartDuChmLimit && lignetrouvee) || 
2420           (NombreDePointsDeDepartDuCheminement-3 >= nbp && (!lignetrouvee))));
2421         delete [] TabPtDep;
2422       }// for( ls ...
2423
2424       delete [] TabL;
2425
2426     }// if nbLigSec >= 1
2427     //
2428     AdjustOnPeriodic(Surf1, Surf2, SLin);
2429     //
2430
2431     //--------------------------------------------------------------------
2432     //-- Calcul des parametres approches a partir des Zones De Tangence --
2433     //--------------------------------------------------------------------
2434     Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2435     Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2436
2437     UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
2438     UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
2439
2440     // NbPointsInTangentZone always == 1 (eap)
2441
2442     Standard_Integer z;
2443     for( z=1; z <= nbTanZon; z++)
2444     { 
2445       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2446       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) {
2447       Standard_Integer pz=1;
2448       Standard_Real _x,_y,_z;
2449       Standard_Real U1, U2, V1, V2;
2450       Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2451
2452       if(U1>UmaxLig1) UmaxLig1=U1;
2453       if(V1>VmaxLig1) VmaxLig1=V1;
2454       if(U2>UmaxLig2) UmaxLig2=U2;
2455       if(V2>VmaxLig2) VmaxLig2=V2;
2456
2457       if(U1<UminLig1) UminLig1=U1;
2458       if(V1<VminLig1) VminLig1=V1;
2459       if(U2<UminLig2) UminLig2=U2;
2460       if(V2<VminLig2) VminLig2=V2;
2461       //}
2462     }
2463
2464     for(z=1; z <= nbTanZon; z++)
2465     {
2466       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2467       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) { 
2468       Standard_Integer pz=1;
2469       Standard_Real _x,_y,_z;
2470       Standard_Real U1, U2, V1, V2;
2471       Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2472
2473       StartParams(1) = U1;
2474       StartParams(2) = V1;
2475       StartParams(3) = U2;
2476       StartParams(4) = V2;
2477
2478       //-----------------------------------------------------------------------
2479       //-- Calcul du premier point de cheminement a partir du point approche --
2480       //-----------------------------------------------------------------------
2481       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);     
2482       if(HasStartPoint)
2483       {
2484         //-------------------------------------------------
2485         //-- Un point a ete trouve                       --
2486         //-- On verifie qu il n appartient pas           --
2487         //--  a une ligne de cheminement deja calculee.  --
2488         //-------------------------------------------------
2489         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2490
2491         NbLigCalculee = SLin.Length();
2492         dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2493
2494         for(Standard_Integer l=1; 
2495           (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2496           l++)
2497         {
2498           const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2499
2500           if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2501           {
2502             dminiPointLigne = 0.0;
2503           }
2504         }
2505
2506         //-- Fin d exploration des lignes
2507         if(dminiPointLigne > SeuildPointLigne)
2508         {
2509           //---------------------------------------------------
2510           //-- Le point de depart du nouveau cheminement     --
2511           //-- n est present dans aucune ligne deja calculee.--
2512           //---------------------------------------------------
2513           PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,
2514             UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
2515
2516           if(PW.IsDone())
2517           {
2518             if(PW.NbPoints()>2)
2519             { 
2520               const Standard_Integer aMinNbPoints = 40;
2521               if(PW.NbPoints() < aMinNbPoints)
2522               {
2523                 PW.SeekAdditionalPoints(Surf1, Surf2, aMinNbPoints);
2524               }
2525
2526               //-----------------------------------------------
2527               //-- Verification a posteriori : 
2528               //-- On teste si le point de depart et de fin de 
2529               //-- la ligne de cheminement est present dans une 
2530               //-- autre ligne . 
2531               //-----------------------------------------------
2532               RejectLine = Standard_False;
2533               Point3dDebut = PW.Value(1).Value();
2534               const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2535               Point3dFin   = PointFin.Value();
2536
2537               for(ver=1 ; (!RejectLine) && (ver<= NbLigCalculee) ; ver++)
2538               {
2539                 const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2540                 //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2541
2542                 // Check end point if it is on existing line.
2543                 // Start point is checked before.
2544                 if (IsPointOnLine(PointFin, verwline, Deflection))
2545                 {
2546                   RejectLine = Standard_True; 
2547                   break;
2548                 }
2549
2550                 const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2551                 const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2552                 if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
2553                 {
2554                   RejectLine = Standard_True; 
2555                 }
2556                 else
2557                 {
2558                   if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
2559                   {
2560                     RejectLine = Standard_True; 
2561                   }
2562                 }
2563               }
2564
2565               if(!RejectLine)
2566               { 
2567                 IntSurf_TypeTrans trans1,trans2;
2568                 Standard_Real locu,locv;
2569                 gp_Vec norm1,norm2,d1u,d1v;
2570                 gp_Pnt ptbid;
2571                 Standard_Integer indextg;
2572                 gp_Vec tgline(PW.TangentAtLine(indextg));
2573                 PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2574                 Surf1->D1(locu,locv,ptbid,d1u,d1v);
2575                 norm1 = d1u.Crossed(d1v);
2576                 PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2577                 Surf2->D1(locu,locv,ptbid,d1u,d1v);
2578                 norm2 = d1u.Crossed(d1v);
2579                 if (tgline.DotCross(norm2,norm1)>0.)
2580                 {
2581                   trans1 = IntSurf_Out;
2582                   trans2 = IntSurf_In;
2583                 }
2584                 else
2585                 {
2586                   trans1 = IntSurf_In;
2587                   trans2 = IntSurf_Out;
2588                 }
2589
2590                 Standard_Real TolTang = TolTangency;
2591                 Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2592                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2593                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2594
2595                 //---------------
2596                 if(wline->NbVertex() == 0)
2597                 {
2598                   IntPatch_Point vtx;
2599                   IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2600                   POn2S.Parameters(pu1,pv1,pu2,pv2);
2601                   vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2602                   vtx.SetParameters(pu1,pv1,pu2,pv2);
2603                   vtx.SetParameter(1);
2604                   wline->AddVertex(vtx);
2605
2606                   POn2S = PW.Line()->Value(wline->NbPnts());
2607                   POn2S.Parameters(pu1,pv1,pu2,pv2);
2608                   vtx.SetValue(Point3dFin,TolTang,Standard_False);
2609                   vtx.SetParameters(pu1,pv1,pu2,pv2);
2610                   vtx.SetParameter(wline->NbPnts());
2611                   wline->AddVertex(vtx);
2612                 }
2613
2614                 //---------------
2615                 AddWLine(SLin, wline, Deflection);
2616                 empt = Standard_False;
2617               }
2618               else
2619               {
2620                 //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2621               }
2622               //------------------------------------------------------------            
2623             }
2624           }  //--  le cheminement a reussi (done a True)
2625         }  //--  le point approche ne renvoie pas sur une ligne existante
2626       } //-- Si HasStartPoint
2627       //} //-- Boucle Sur les Points de la Tangent Zone
2628     } //-- Boucle sur Les Tangent Zones
2629
2630     if ( pInterference )
2631     {
2632       delete pInterference;
2633       pInterference = NULL;
2634     }
2635
2636     return;
2637   }// if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit)) {  
2638
2639   Handle(IntSurf_LineOn2S) LOn2S = new IntSurf_LineOn2S();
2640   PointDepart( LOn2S, Surf1, NbU1, NbV1, Surf2, NbU2, NbV2 );
2641   empt = Standard_True;
2642   done = Standard_True;
2643   SLin.Clear();  
2644
2645   Standard_Integer NbLigCalculee = 0;
2646   Standard_Real U1,U2,V1,V2;
2647   Standard_Real pu1,pu2,pv1,pv2;
2648
2649   TColStd_Array1OfReal StartParams(1,4);
2650   Standard_Integer MaxOscill = NbU1;
2651   if(MaxOscill < NbU2) MaxOscill=NbU2;
2652   if(MaxOscill < NbV1) MaxOscill=NbV1;
2653   if(MaxOscill < NbV2) MaxOscill=NbV2;
2654
2655   Standard_Real nIncrement=Increment;
2656   //if(MaxOscill>10)
2657   //nIncrement/=0.5*MaxOscill;
2658
2659   IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,nIncrement);
2660   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
2661   Standard_Real    dminiPointLigne;
2662   Standard_Boolean HasStartPoint,RejetLigne;
2663   IntSurf_PntOn2S StartPOn2S;
2664   Standard_Integer ver;
2665   gp_Pnt Point3dDebut,Point3dFin;
2666
2667   //------------------------------------------------------------
2668   //-- Calcul des parametres approches a partir des Zones De Tangence --
2669   //--------------------------------------------------------------------
2670   Standard_Integer nbTanZon = LOn2S->NbPoints();
2671   for(Standard_Integer z=1; z <= nbTanZon; z++) { 
2672     const IntSurf_PntOn2S& POn2S = LOn2S->Value(z);
2673     POn2S.Parameters(U1,V1,U2,V2);
2674     StartParams(1) = U1;
2675     StartParams(2) = V1;
2676     StartParams(3) = U2;
2677     StartParams(4) = V2;
2678
2679     //-----------------------------------------------------------------------
2680     //-- Calcul du premier point de cheminement a partir du point approche --
2681     //-----------------------------------------------------------------------
2682     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);       
2683     if(HasStartPoint)
2684     { 
2685       //-------------------------------------------------
2686       //-- Un point a ete trouve                       --
2687       //-- On verifie qu il n appartient pas           --
2688       //--  a une ligne de cheminement deja calculee.  --
2689       //-------------------------------------------------
2690       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2691
2692       NbLigCalculee = SLin.Length();
2693       dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2694
2695       for(Standard_Integer l=1; 
2696         (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2697         l++)
2698       {
2699         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2700
2701         if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2702         {
2703           dminiPointLigne = 0.0;
2704         }
2705       }
2706
2707       //-- Fin d exploration des lignes
2708       if(dminiPointLigne > SeuildPointLigne)
2709       { 
2710         //---------------------------------------------------
2711         //-- Le point de depart du nouveau cheminement     --
2712         //-- n est present dans aucune ligne deja calculee.--
2713         //---------------------------------------------------
2714         PW.Perform(StartParams);
2715         if(PW.IsDone())
2716         {
2717           if(PW.NbPoints()>2)
2718           {
2719             //-----------------------------------------------
2720             //-- Verification a posteriori : 
2721             //-- On teste si le point de depart et de fin de 
2722             //-- la ligne de cheminement est present dans une 
2723             //-- autre ligne . 
2724             //-----------------------------------------------
2725             RejetLigne = Standard_False;
2726             Point3dDebut = PW.Value(1).Value();
2727             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2728             Point3dFin   = PointFin.Value();
2729
2730             for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++)
2731             {
2732               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2733               //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2734
2735               // Check end point if it is on existing line.
2736               // Start point is checked before.
2737               if (IsPointOnLine(PointFin, verwline, Deflection))
2738               {
2739                 RejetLigne = Standard_True; 
2740                 break;
2741               }
2742
2743               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2744               const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2745               if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
2746               {
2747                 RejetLigne = Standard_True; 
2748               }
2749               else
2750               {
2751                 if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
2752                 {
2753                   RejetLigne = Standard_True; 
2754                 }
2755               }
2756             }
2757
2758             if(!RejetLigne)
2759             {
2760               IntSurf_TypeTrans trans1,trans2;
2761               Standard_Real locu,locv;
2762               gp_Vec norm1,norm2,d1u,d1v;
2763               gp_Pnt ptbid;
2764               Standard_Integer indextg;
2765               gp_Vec tgline(PW.TangentAtLine(indextg));
2766               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2767               Surf1->D1(locu,locv,ptbid,d1u,d1v);
2768               norm1 = d1u.Crossed(d1v);
2769               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2770               Surf2->D1(locu,locv,ptbid,d1u,d1v);
2771               norm2 = d1u.Crossed(d1v);
2772               if (tgline.DotCross(norm2,norm1)>0.)
2773               {
2774                 trans1 = IntSurf_Out;
2775                 trans2 = IntSurf_In;
2776               }
2777               else {
2778                 trans1 = IntSurf_In;
2779                 trans2 = IntSurf_Out;
2780               }
2781
2782               Standard_Real TolTang = TolTangency;
2783               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2784               IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2785               IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2786
2787               //---------------
2788               if(wline->NbVertex() == 0)
2789               {
2790                 IntPatch_Point vtx;
2791                 const IntSurf_PntOn2S& POn2Sf = PW.Line()->Value(1);
2792                 POn2Sf.Parameters(pu1,pv1,pu2,pv2);
2793                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2794                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2795                 vtx.SetParameter(1);
2796                 wline->AddVertex(vtx);
2797
2798                 const IntSurf_PntOn2S& POn2Sl = PW.Line()->Value(wline->NbPnts());
2799                 POn2Sl.Parameters(pu1,pv1,pu2,pv2);
2800                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
2801                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2802                 vtx.SetParameter(wline->NbPnts());
2803                 wline->AddVertex(vtx);
2804               }
2805
2806               //---------------
2807               AddWLine(SLin, wline, Deflection);
2808               empt = Standard_False;
2809             }
2810             else
2811             { 
2812               //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2813             }
2814             //------------------------------------------------------------              
2815           }
2816         }  //--  le cheminement a reussi (done a True)
2817       }  //--  le point approche ne renvoie pas sur une ligne existante
2818     } //-- Si HasStartPoint
2819   } //-- Boucle sur Les Tangent Zones
2820 }
2821 //modified by NIZNHY-PKV Wed May 25 09:39:07 2011f
2822 //=======================================================================
2823 //class : IntPatch_InfoPD
2824 //purpose  : 
2825 //=======================================================================
2826 class IntPatch_InfoPD {
2827 public:
2828   //----------------------------------------C-tor
2829   IntPatch_InfoPD(const Standard_Integer aNBI) {
2830     Standard_Integer aNBI2, i, j;
2831     myNBI=aNBI;
2832     //
2833     aNBI2=aNBI*aNBI;
2834     myP1DS2=new char[aNBI2];
2835     myP2DS1=new char[aNBI2];
2836     myIP1=new Standard_Integer[aNBI2]; 
2837     myIP2=new Standard_Integer[aNBI2];
2838     myP1=new gp_Pnt[aNBI2];
2839     myP2=new gp_Pnt[aNBI2];
2840     //
2841     for (i=0; i<myNBI; ++i) {
2842       for (j=0; j<myNBI; ++j) {
2843         xP1DS2(i, j)=0;
2844         xP2DS1(i, j)=0;
2845         xIP1(i, j)=0;
2846         xIP2(i, j)=0;
2847         xP1(i, j).SetCoord(0., 0., 0.);
2848         xP2(i, j).SetCoord(0., 0., 0.);
2849       }
2850     }
2851   };
2852   //---------------------------------------- D-tor
2853   ~IntPatch_InfoPD() {
2854     delete [] (char*) myP1DS2;
2855     delete [] (char*) myP2DS1;
2856     delete [] (Standard_Integer*) myIP1;
2857     delete [] (Standard_Integer*) myIP2; 
2858     delete [] (gp_Pnt*)myP1;
2859     delete [] (gp_Pnt*)myP2;
2860   };
2861   //---------------------------------------- Index
2862   Standard_Integer Index(const Standard_Integer i,
2863     const Standard_Integer j) const { 
2864       return i*myNBI+j;
2865   };
2866   //---------------------------------------- NBI
2867   Standard_Integer NBI() const { 
2868     return myNBI;
2869   };
2870   //----------------------------------------xP1DS2
2871   char& xP1DS2(const Standard_Integer i,
2872     const Standard_Integer j) { 
2873       return myP1DS2[Index(i,j)];
2874   };
2875   //----------------------------------------xP2DS1
2876   char& xP2DS1(const Standard_Integer i,
2877     const Standard_Integer j) { 
2878       return myP2DS1[Index(i,j)];
2879   };
2880   //----------------------------------------xIP1
2881   Standard_Integer& xIP1(const Standard_Integer i,
2882     const Standard_Integer j) { 
2883       return myIP1[Index(i,j)];
2884   };
2885   //----------------------------------------xIP2
2886   Standard_Integer& xIP2(const Standard_Integer i,
2887     const Standard_Integer j) { 
2888       return myIP2[Index(i,j)];
2889   };
2890   //----------------------------------------xP1
2891   gp_Pnt& xP1(const Standard_Integer i,
2892     const Standard_Integer j) { 
2893       return myP1[Index(i,j)];
2894   };
2895   //----------------------------------------xP1
2896   gp_Pnt& xP2(const Standard_Integer i,
2897     const Standard_Integer j) { 
2898       return myP2[Index(i,j)];
2899   };
2900 protected:
2901   Standard_Integer myNBI;
2902   char *myP1DS2;
2903   char *myP2DS1;
2904   Standard_Integer *myIP1;
2905   Standard_Integer *myIP2;
2906   gp_Pnt *myP1;
2907   gp_Pnt *myP2;
2908 }; 
2909 //modified by NIZNHY-PKV Tue May 24 11:38:55 2011t
2910 //==================================================================================
2911 // function : PointDepart
2912 // purpose  : 
2913 //==================================================================================
2914 void IntPatch_PrmPrmIntersection::PointDepart(Handle(IntSurf_LineOn2S)& LineOn2S,
2915                                               const Handle(Adaptor3d_HSurface)& S1,
2916                                               const Standard_Integer SU_1,
2917                                               const Standard_Integer SV_1,
2918                                               const Handle(Adaptor3d_HSurface)& S2,
2919                                               const Standard_Integer SU_2,
2920                                               const Standard_Integer SV_2) const 
2921
2922   Standard_Integer i, j, xNBI;
2923   //modified by NIZNHY-PKV Tue May 24 11:37:38 2011f
2924   xNBI=200;
2925   IntPatch_InfoPD aIPD(xNBI);
2926   //modified by NIZNHY-PKV Wed May 25 06:47:12 2011t
2927   Standard_Integer iC15, SU1, SV1, SU2, SV2;
2928   Standard_Real U0, U1, V0, V1, U, V;
2929   Standard_Real resu0,resv0;
2930   Standard_Real  du1,du2,dv1,dv2, dmaxOn1, dmaxOn2;
2931   Standard_Real x0,y0,z0, x1,y1,z1,d;
2932   Bnd_Box Box1, Box2;
2933   //
2934   iC15=15;
2935   SU1 =iC15*SU_1 ;
2936   SV1 =iC15*SV_1 ;
2937   SU2 =iC15*SU_2 ;
2938   SV2 =iC15*SV_2 ;
2939   //
2940   if(xNBI<SU1) {
2941     SU1 = xNBI;
2942   }
2943   if(xNBI<SV1){
2944     SV1 = xNBI;
2945   }
2946   if(xNBI<SU2){
2947     SU2 = xNBI;
2948   }
2949   if(xNBI<SV2){
2950     SV2 = xNBI;
2951   }
2952   //
2953   U0 = S1->FirstUParameter();
2954   U1 = S1->LastUParameter();
2955   V0 = S1->FirstVParameter();
2956   V1 = S1->LastVParameter();
2957   //
2958   resu0=U0;
2959   resv0=V0;
2960   //
2961   dmaxOn1 = 0.0;
2962   dmaxOn2 = 0.0;
2963   //-----
2964   du1 = (U1-U0)/(SU1-1);
2965   dv1 = (V1-V0)/(SV1-1);
2966   for(U=U0,i=0; i<SU1; i++,U+=du1) { 
2967     for(V=V0,j=0; j<SV1; V+=dv1,j++) { 
2968       aIPD.xP1(i, j)= S1->Value(U,V);
2969       Box1.Add(aIPD.xP1(i, j));
2970       if(i>0 && j>0) { 
2971         aIPD.xP1(i, j)    .Coord(x0,y0,z0);
2972         aIPD.xP1(i-1, j-1).Coord(x1,y1,z1);
2973         //
2974         d=Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
2975         if(d>dmaxOn1) {
2976           dmaxOn1 = d;
2977         }
2978       }
2979     }
2980   }
2981   Box1.Enlarge(1.e-8);
2982   //
2983   U0 = S2->FirstUParameter();
2984   U1 = S2->LastUParameter();
2985   V0 = S2->FirstVParameter();
2986   V1 = S2->LastVParameter();
2987   //
2988   du2 = (U1-U0)/(SU2-1);  
2989   dv2 = (V1-V0)/(SV2-1);
2990   for(U=U0,i=0; i<SU2; i++,U+=du2) { 
2991     for(V=V0,j=0; j<SV2; V+=dv2,j++) { 
2992       aIPD.xP2(i, j) = S2->Value(U,V);
2993       Box2.Add(aIPD.xP2(i, j));
2994       if(i>0 && j>0) { 
2995         aIPD.xP2(i, j)    .Coord(x0,y0,z0);
2996         aIPD.xP2(i-1, j-1).Coord(x1,y1,z1);
2997         d =  Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
2998         if(d>dmaxOn2) {
2999           dmaxOn2 = d;
3000         }
3001       }
3002     }
3003   }
3004   Box2.Enlarge(1.e-8);
3005   //--------
3006   //
3007   if(Box1.IsOut(Box2)) {
3008
3009     return;
3010   }
3011   //
3012   Standard_Integer aNbPG;
3013   Standard_Real x10,y10,z10,x11,y11,z11;
3014   Standard_Real x20,y20,z20,x21,y21,z21;
3015   Standard_Real dx, dy, dz, dmax;
3016   Standard_Real dx2, dy2, dz2;
3017   //
3018   Box1.Get(x10,y10,z10,x11,y11,z11);
3019   Box2.Get(x20,y20,z20,x21,y21,z21);
3020   //
3021   x0 = (x10>x20)? x10 : x20;
3022   y0 = (y10>y20)? y10 : y20;
3023   z0 = (z10>z20)? z10 : z20;
3024   //
3025   x1 = (x11<x21)? x11 : x21;
3026   y1 = (y11<y21)? y11 : y21;
3027   z1 = (z11<z21)? z11 : z21;
3028   //
3029   if(dmaxOn2 > dmaxOn1) {
3030     dmaxOn1 = dmaxOn2;
3031   }
3032   //
3033   dmaxOn1+=dmaxOn1;
3034   x0-=dmaxOn1;
3035   y0-=dmaxOn1;
3036   z0-=dmaxOn1;
3037   x1+=dmaxOn1;
3038   y1+=dmaxOn1;
3039   z1+=dmaxOn1;
3040   //
3041   x10-=dmaxOn1;   y10-=dmaxOn1;   z10-=dmaxOn1; 
3042   x11+=dmaxOn1;   y11+=dmaxOn1;   z11+=dmaxOn1; 
3043
3044   x20-=dmaxOn1;   y20-=dmaxOn1;   z20-=dmaxOn1; 
3045   x21+=dmaxOn1;   y21+=dmaxOn1;   z21+=dmaxOn1; 
3046
3047   aNbPG=NbPointsGrille();
3048   dx = (x1-x0)/aNbPG;
3049   dy = (y1-y0)/aNbPG;
3050   dz = (z1-z0)/aNbPG;
3051   //
3052   dmax = dx;
3053   if(dy>dmax) {
3054     dmax = dy;
3055   }
3056   if(dz>dmax){
3057     dmax = dz;
3058   }
3059   //
3060   if(dx<dmax*0.01) {
3061     dx = dmax*0.01;
3062   }
3063   if(dy<dmax*0.01) {
3064     dy = dmax*0.01;
3065   }
3066   if(dz<dmax*0.01) {
3067     dz = dmax*0.01;
3068   }
3069   //
3070   dx2 = dx*0.5;