0024087: Eliminate compiler warning C4244 in MSVC++ with warning level 4
[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-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 // modified by Edward AGAPOV (eap) Tue Jan 22 12:29:55 2002
23 // modified by Oleg FEDYAED  (ofv) Fri Nov 29 16:08:02 2002
24
25 #include <IntPatch_PrmPrmIntersection.ixx>
26
27 #include <IntPatch_TheInterfPolyhedron.hxx>
28 #include <IntPatch_ThePWalkingInter.hxx>
29 #include <IntPatch_WLine.hxx>
30 #include <IntPatch_RstInt.hxx>
31
32 #include <IntPatch_Line.hxx>
33
34 #include <Handle_IntSurf_LineOn2S.hxx>
35 #include <IntSurf_PntOn2S.hxx>
36 #include <IntSurf_LineOn2S.hxx>
37 #include <TColStd_Array1OfReal.hxx>
38 #include <Intf_PIType.hxx>
39 #include <Intf_SectionLine.hxx>
40 #include <Intf_TangentZone.hxx>
41 #include <Intf_SectionPoint.hxx>
42 #include <gp_Pnt.hxx>
43 #include <gp_Dir.hxx>
44 #include <IntPolyh_Intersection.hxx>
45
46 #include <TColStd_SequenceOfInteger.hxx>
47 #include <IntSurf_ListIteratorOfListOfPntOn2S.hxx>
48 #include <TColStd_HArray1OfReal.hxx>
49
50 static void SectionPointToParameters(const Intf_SectionPoint& Sp,
51                                      const IntPatch_Polyhedron& Surf1,
52                                      const IntPatch_Polyhedron& Surf2,
53                                      Standard_Real& u1,
54                                      Standard_Real& v1,
55                                      Standard_Real& u2,
56                                      Standard_Real& v2);
57
58
59 static 
60   void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
61                         const Handle(Adaptor3d_HSurface)& Surf2,
62                         IntPatch_SequenceOfLine& aSLin);
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   IntSurf_ListIteratorOfListOfPntOn2S IterLOP1(LOfPnts);
1314
1315   for(; IterLOP1.More(); IterLOP1.Next()){
1316     IntSurf_PntOn2S Pnt = IterLOP1.Value();
1317     Pnt.Parameters(U1, V1, U2, V2);
1318     if(U1>UmaxLig1) UmaxLig1=U1;
1319     if(V1>VmaxLig1) VmaxLig1=V1;
1320     if(U2>UmaxLig2) UmaxLig2=U2;
1321     if(V2>VmaxLig2) VmaxLig2=V2;
1322     
1323     if(U1<UminLig1) UminLig1=U1;
1324     if(V1<VminLig1) VminLig1=V1;
1325     if(U2<UminLig2) UminLig2=U2;
1326     if(V2<VminLig2) VminLig2=V2; 
1327   }
1328   
1329   Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
1330   
1331   Standard_Integer NbLigCalculee = 0, ver;
1332   Standard_Real pu1,pu2,pv1,pv2, dminiPointLigne;
1333   Standard_Boolean HasStartPoint,RejetLigne;
1334   IntSurf_PntOn2S StartPOn2S;
1335   gp_Pnt Point3dDebut,Point3dFin;
1336
1337   TColStd_Array1OfReal StartParams(1,4);
1338   IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);  
1339     
1340   IntSurf_ListIteratorOfListOfPntOn2S IterLOP2(LOfPnts);
1341   for(; IterLOP2.More(); IterLOP2.Next() ){
1342     
1343     IntSurf_PntOn2S cPnt = IterLOP2.Value();
1344     cPnt.Parameters(U1, V1, U2, V2);
1345     
1346     StartParams(1) = U1;
1347     StartParams(2) = V1;
1348     StartParams(3) = U2;
1349     StartParams(4) = V2;
1350     
1351     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
1352     dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
1353     if(HasStartPoint) {
1354       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
1355       NbLigCalculee = SLin.Length();
1356       Standard_Integer l;
1357       for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
1358         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
1359
1360         if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
1361           dminiPointLigne = 0.0;
1362         }
1363       }// for( l ...
1364       
1365       if(dminiPointLigne > SeuildPointLigne) {
1366         PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
1367         if(PW.IsDone()) {
1368           if(PW.NbPoints()>2) {
1369             RejetLigne = Standard_False;
1370             Point3dDebut = PW.Value(1).Value();
1371             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
1372             Point3dFin   = PointFin.Value();
1373             for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
1374               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
1375
1376               // Check end point if it is on existing line.
1377               // Start point is checked before.
1378               if (IsPointOnLine(PointFin, verwline, Deflection)) {
1379                 RejetLigne = Standard_True; 
1380                 break;
1381               }
1382
1383               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
1384               const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
1385               if(Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) { 
1386                 if(Point3dFin.Distance(verPointFin.Value()) <= TolTangency)
1387                   RejetLigne = Standard_True; 
1388               }
1389             }
1390             
1391             if(!RejetLigne) {
1392               IntSurf_TypeTrans trans1,trans2;
1393               Standard_Real locu,locv;
1394               gp_Vec norm1,norm2,d1u,d1v;
1395               gp_Pnt ptbid;
1396               Standard_Integer indextg;
1397               gp_Vec tgline(PW.TangentAtLine(indextg));
1398               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1399               Surf1->D1(locu,locv,ptbid,d1u,d1v);
1400               norm1 = d1u.Crossed(d1v);
1401               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1402               Surf2->D1(locu,locv,ptbid,d1u,d1v);
1403               norm2 = d1u.Crossed(d1v);
1404               if( tgline.DotCross(norm2,norm1) >= 0. ) {
1405                 trans1 = IntSurf_Out;
1406                 trans2 = IntSurf_In;
1407               }
1408               else {
1409                 trans1 = IntSurf_In;
1410                 trans2 = IntSurf_Out;
1411               }
1412               
1413               Standard_Real TolTang = TolTangency;
1414               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1415               if (RestrictLine){
1416                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1417                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1418               }
1419               
1420               if(wline->NbVertex() == 0) {
1421                 IntPatch_Point vtx;
1422                 IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1423                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1424                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1425                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1426                 vtx.SetParameter(1);
1427                 wline->AddVertex(vtx);
1428                 
1429                 POn2S = PW.Line()->Value(wline->NbPnts());
1430                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1431                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
1432                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1433                 vtx.SetParameter(wline->NbPnts());
1434                 wline->AddVertex(vtx);
1435               }              
1436
1437               Standard_Integer slinlen = SLin.Length();
1438               if( slinlen > 0 ) {
1439                 Standard_Integer cnbV = wline->NbVertex();
1440                 Standard_Integer ciV;
1441                 for( ciV = 1; ciV <= cnbV; ciV++ ) {
1442                   Standard_Real pntDMin = 1.e+100;
1443                   Standard_Integer VDMin = 0;
1444                   Standard_Integer WLDMin = 0;
1445                   gp_Pnt cPV = wline->Vertex(ciV).Value();
1446                   Standard_Integer iL;
1447                   for( iL = 1; iL <= slinlen; iL++) {
1448                     const Handle(IntPatch_Line)& aSLine = SLin.Value(iL);
1449                     IntPatch_IType aType = aSLine->ArcType();
1450                     if( aType != IntPatch_Walking)
1451                       continue;
1452                     const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
1453                     Standard_Integer tnbV = aWLine->NbVertex();
1454                     Standard_Integer tiV;
1455                     for( tiV = 1; tiV <= tnbV; tiV++ ) {
1456                       gp_Pnt tPV = aWLine->Vertex(tiV).Value();
1457                       Standard_Real tDistance = cPV.Distance(tPV);
1458                       Standard_Real uRs1 = Surf1->Surface().UResolution(tDistance);
1459                       Standard_Real vRs1 = Surf1->Surface().VResolution(tDistance);
1460                       Standard_Real uRs2 = Surf2->Surface().UResolution(tDistance);
1461                       Standard_Real vRs2 = Surf2->Surface().VResolution(tDistance);
1462                       Standard_Real RmaxS1 = Max(uRs1,vRs1);
1463                       Standard_Real RmaxS2 = Max(uRs2,vRs2);
1464                       if(RmaxS1 < 1.e-4 && RmaxS2 < 1.e-4) {
1465                         if( pntDMin > tDistance && tDistance > 1.e-9) {
1466                           pntDMin = tDistance;
1467                           VDMin = tiV;
1468                           WLDMin = iL;
1469                         }
1470                       }
1471                     }
1472                   }
1473                   
1474                   if( VDMin != 0 ) {
1475                     const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
1476                     const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
1477                     Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
1478                     Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
1479                     Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
1480                     Standard_Real u21 = 0., u22 = 0., v21 = 0., v22 = 0.;
1481                     wline->Point(ciVpar).Parameters(u11,v11,u12,v12);
1482                     aWLine->Point(tiVpar).Parameters(u21,v21,u22,v22);
1483                     
1484                     Handle(IntSurf_LineOn2S) newL2s = new IntSurf_LineOn2S();
1485                     IntSurf_PntOn2S replacePnt = aWLine->Point(tiVpar);
1486                     Standard_Integer cNbP = wline->NbPnts();
1487                     
1488                     TColStd_SequenceOfInteger VPold;
1489                     Standard_Integer iPo;
1490                     for( iPo = 1; iPo <= cnbV; iPo++ ) {
1491                       Standard_Real Po = wline->Vertex(iPo).ParameterOnLine();
1492                       Standard_Integer IPo = (Standard_Integer) Po;
1493                       VPold.Append(IPo);
1494                     }
1495                     
1496                     Standard_Boolean removeNext = Standard_False;
1497                     Standard_Boolean removePrev = Standard_False;
1498                     if( ciV == 1) {
1499                       Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
1500                       if(dPar > 10) {
1501                                 removeNext = Standard_True;
1502                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
1503                                   VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1504                               }
1505                     }
1506                     else if( ciV == cnbV) {
1507                       Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
1508                       if(dPar > 10) {
1509                         removePrev = Standard_True;
1510                         VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
1511                       }
1512                     }
1513                     else {
1514                       Standard_Integer dParMi = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
1515                       Standard_Integer dParMa = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
1516                       if(dParMi > 10) {
1517                         removePrev = Standard_True;
1518                         VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
1519                       }
1520                       if(dParMa > 10) {
1521                         removeNext = Standard_True;
1522                         for( iPo = (ciV+1); iPo <= cnbV; iPo++ ) {
1523                           if(dParMi > 10)
1524                             VPold.SetValue(iPo, VPold.Value(iPo) - 2 );
1525                           else
1526                             VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1527                         }
1528                       }
1529                       else {
1530                         if(dParMi > 10)
1531                           for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
1532                             VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
1533                       } 
1534                     }
1535                     Standard_Integer pI = (Standard_Integer) ciVpar;
1536                     
1537                     Standard_Integer iP;
1538                     for( iP = 1; iP <= cNbP; iP++) {
1539                       if( pI == iP )
1540                         newL2s->Add(replacePnt);
1541                       else if(removeNext && iP == (pI + 1))
1542                         continue;
1543                       else if(removePrev && iP == (pI - 1))
1544                         continue;
1545                       else
1546                         newL2s->Add(wline->Point(iP));
1547                     }
1548                     
1549                     IntPatch_Point newVtx;
1550                     gp_Pnt Pnt3dV = aWLine->Vertex(VDMin).Value();
1551                     newVtx.SetValue(Pnt3dV,TolTang,Standard_False);
1552                     newVtx.SetParameters(u21,v21,u22,v22);
1553                     newVtx.SetParameter(VPold.Value(ciV));
1554                     
1555                     Handle(IntPatch_WLine) NWLine = new IntPatch_WLine(newL2s,Standard_False,trans1,trans2);
1556                     
1557                     Standard_Integer iV;
1558                     for( iV = 1; iV <= cnbV; iV++ ) {
1559                       if( iV == ciV )
1560                         NWLine->AddVertex(newVtx);
1561                       else {
1562                         IntPatch_Point theVtx = wline->Vertex(iV);
1563                         theVtx.SetParameter(VPold.Value(iV));
1564                         NWLine->AddVertex(theVtx);
1565                       }
1566                     }
1567                     
1568                     wline = NWLine;
1569                   }
1570                 }
1571               }// SLin.Length > 0
1572               
1573               AddWLine(SLin, wline, Deflection);
1574               empt = Standard_False;
1575             }// !RejetLigne
1576           }// PW points > 2
1577         }// done is True
1578       }// dminiPointLigne > SeuildPointLigne
1579     }// HasStartPoint  
1580   }// for( IterLOP ...
1581   done = Standard_True;
1582   return;      
1583 }
1584 //=======================================================================
1585 //function : Perform
1586 //purpose  : 
1587 //=======================================================================
1588 void IntPatch_PrmPrmIntersection::Perform(const Handle(Adaptor3d_HSurface)&    Surf1,
1589                                           const Handle(Adaptor3d_TopolTool)& D1,
1590                                           const Handle(Adaptor3d_HSurface)&    Surf2,
1591                                           const Handle(Adaptor3d_TopolTool)& D2,
1592                                           const Standard_Real   U1Depart,
1593                                           const Standard_Real   V1Depart,
1594                                           const Standard_Real   U2Depart,
1595                                           const Standard_Real   V2Depart,
1596                                           const Standard_Real   TolTangency,
1597                                           const Standard_Real   Epsilon,
1598                                           const Standard_Real   Deflection,
1599                                           const Standard_Real   Increment) { 
1600   
1601   
1602   
1603 //    Standard_Integer NbU1 = D1->NbSamplesU();
1604 //    Standard_Integer NbV1 = D1->NbSamplesV();
1605 //    Standard_Integer NbU2 = D2->NbSamplesU();
1606 //    Standard_Integer NbV2 = D2->NbSamplesV();
1607
1608   //-- Traitement des Lignes de sections
1609   empt = Standard_True;
1610   done = Standard_True;
1611   SLin.Clear();  
1612   
1613   //------------------------------------------------------------
1614
1615   Standard_Real pu1,pu2,pv1,pv2;
1616   
1617   TColStd_Array1OfReal StartParams(1,4);
1618   
1619 //    Standard_Integer MaxOscill = NbU1;
1620 //    if(MaxOscill < NbU2) MaxOscill=NbU2;
1621 //    if(MaxOscill < NbV1) MaxOscill=NbV1;
1622 //    if(MaxOscill < NbV2) MaxOscill=NbV2;
1623   
1624 //    Standard_Real nIncrement=Increment;
1625 //    if(MaxOscill>10) { 
1626 //  #ifdef DEB 
1627 //      cout<<"\n IntPatch_PrmPrmIntersection.gxx : Increment:"<<Increment<<" -> "<<Increment/(0.5*MaxOscill)<<endl;
1628 //  #endif
1629 //      nIncrement/=0.5*MaxOscill;
1630 //    }
1631   
1632   IntPatch_ThePWalkingInter PW(Surf1,Surf2,
1633                                TolTangency,
1634                                Epsilon,
1635                                Deflection,
1636                                Increment); //nIncrement);
1637   
1638   
1639   //Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
1640   //Standard_Real    incidence;
1641   //Standard_Real    dminiPointLigne;
1642   
1643   Standard_Boolean HasStartPoint;//,RejetLigne;
1644   
1645   IntSurf_PntOn2S StartPOn2S;
1646   
1647   //Standard_Integer ver;
1648   
1649   gp_Pnt Point3dDebut,Point3dFin;
1650   
1651   //------------------------------------------------------------
1652   
1653   StartParams(1) = U1Depart;
1654   StartParams(2) = V1Depart;
1655   StartParams(3) = U2Depart;
1656   StartParams(4) = V2Depart;
1657   
1658   //-----------------------------------------------------------------------
1659   //-- Calcul du premier point de cheminement a partir du point approche --
1660   //-----------------------------------------------------------------------
1661   HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S); 
1662   if(HasStartPoint) { 
1663     //-------------------------------------------------
1664     //-- Un point a ete trouve                       --
1665     //-- On verifie qu il n appartient pas           --
1666     //--  a une ligne de cheminement deja calculee.  --
1667     //-------------------------------------------------
1668     
1669     PW.Perform(StartParams);
1670     if(PW.IsDone()) {
1671       
1672       Point3dDebut = PW.Value(1).Value();
1673       Point3dFin   = PW.Value(PW.NbPoints()).Value();
1674
1675       IntSurf_TypeTrans trans1,trans2;
1676       Standard_Real locu,locv;
1677       gp_Vec norm1,norm2,d1u,d1v;
1678       gp_Pnt ptbid;
1679       Standard_Integer indextg;
1680       gp_Vec tgline(PW.TangentAtLine(indextg));
1681       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1682       Surf1->D1(locu,locv,ptbid,d1u,d1v);
1683       norm1 = d1u.Crossed(d1v);
1684       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1685       Surf2->D1(locu,locv,ptbid,d1u,d1v);
1686       norm2 = d1u.Crossed(d1v);
1687       if (tgline.DotCross(norm2,norm1)>0.) {
1688         trans1 = IntSurf_Out;
1689         trans2 = IntSurf_In;
1690       }
1691       else {
1692         trans1 = IntSurf_In;
1693         trans2 = IntSurf_Out;
1694       }
1695       
1696       
1697       
1698       Standard_Real TolTang = TolTangency;
1699       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1700       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1701       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1702       
1703       //---------------
1704       if(wline->NbVertex() == 0) {
1705         IntPatch_Point vtx;
1706         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1707         POn2S.Parameters(pu1,pv1,pu2,pv2);
1708         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1709         vtx.SetParameters(pu1,pv1,pu2,pv2);
1710         vtx.SetParameter(1);
1711         wline->AddVertex(vtx);
1712         
1713         POn2S = PW.Line()->Value(wline->NbPnts());
1714         POn2S.Parameters(pu1,pv1,pu2,pv2);
1715         vtx.SetValue(Point3dFin,TolTang,Standard_False);
1716         vtx.SetParameters(pu1,pv1,pu2,pv2);
1717         vtx.SetParameter(wline->NbPnts());
1718         wline->AddVertex(vtx);
1719       }
1720       
1721       //---------------
1722       SLin.Append(wline);
1723       empt = Standard_False;
1724       
1725     }
1726   }
1727 }
1728 //==================================================================================
1729 // function : AdjustOnPeriodic
1730 // purpose  : 
1731 //==================================================================================
1732 void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
1733                       const Handle(Adaptor3d_HSurface)& Surf2,
1734                       IntPatch_SequenceOfLine& aSLin)
1735 {
1736   Standard_Boolean bIsPeriodic[4], bModified, bIsNull, bIsPeriod;
1737   Standard_Integer i, j, k, aNbLines, aNbPx, aIndx, aIndq;
1738   Standard_Real aPeriod[4], dPeriod[4], ux[4], uq[4], aEps, du;
1739   //
1740   aEps=Precision::Confusion();
1741   //
1742   for (k=0; k<4; ++k) {
1743     aPeriod[k]=0.;
1744   }
1745   //
1746   bIsPeriodic[0]=Surf1->IsUPeriodic();
1747   bIsPeriodic[1]=Surf1->IsVPeriodic();
1748   bIsPeriodic[2]=Surf2->IsUPeriodic();
1749   bIsPeriodic[3]=Surf2->IsVPeriodic();
1750   //
1751   if (bIsPeriodic[0]){
1752     aPeriod[0]=Surf1->UPeriod();
1753   }
1754   if (bIsPeriodic[1]){
1755     aPeriod[1]=Surf1->VPeriod();
1756   }
1757   if (bIsPeriodic[2]){
1758     aPeriod[2]=Surf2->UPeriod();
1759   }
1760   if (bIsPeriodic[3]){
1761     aPeriod[3]=Surf2->VPeriod();
1762   }
1763   //
1764   for (k=0; k<4; ++k) {
1765     dPeriod[k]=0.25*aPeriod[k];
1766   }
1767   //
1768   aNbLines=aSLin.Length();
1769   for (i=1; i<=aNbLines; ++i) {
1770     Handle(IntPatch_WLine) aIL=Handle(IntPatch_WLine)::DownCast(aSLin.Value(i));
1771     Handle(IntSurf_LineOn2S) aL=aIL->Curve();
1772
1773     aNbPx=aL->NbPoints();
1774     if (aNbPx<10) {
1775       continue;
1776     }
1777     //
1778     for (j=0; j<2; ++j) {
1779       bModified=Standard_False;
1780       aIndx=1;
1781       aIndq=2;
1782       if (j) {
1783         aIndx=aNbPx;
1784         aIndq=aNbPx-1;
1785       }
1786       //
1787       const IntSurf_PntOn2S& aPSx=aL->Value(aIndx);
1788       const IntSurf_PntOn2S& aPSq=aL->Value(aIndq);
1789       //
1790       aPSx.Parameters(ux[0], ux[1], ux[2], ux[3]);
1791       aPSq.Parameters(uq[0], uq[1], uq[2], uq[3]);
1792       //
1793       for (k=0; k<4; ++k) {
1794         bIsNull=Standard_False;
1795         bIsPeriod=Standard_False;
1796         //
1797         if (!bIsPeriodic[k]) {
1798           continue;
1799         }
1800         //
1801         if (fabs(ux[k])<aEps) {
1802           bModified=Standard_True;
1803           bIsNull=Standard_True;
1804         }
1805         //
1806         else if (fabs(ux[k]-aPeriod[k])<aEps) {
1807           bModified=Standard_True;
1808           bIsPeriod=Standard_True;
1809         }
1810         //
1811         if (bModified) {
1812           du=fabs(ux[k]-uq[k]);
1813           if (du > dPeriod[k]) {
1814             if(bIsNull){
1815               ux[k]=aPeriod[k];
1816             }
1817             if(bIsPeriod) {
1818               ux[k]=0.;
1819             }
1820           }
1821         }
1822       }//for (k=0; k<4; ++k) 
1823       if (bModified) {
1824         IntSurf_PntOn2S aPntOn2S;
1825         //
1826         aPntOn2S=aPSx;  
1827         aPntOn2S.SetValue(ux[0], ux[1], ux[2], ux[3]);
1828         aL->Value(aIndx, aPntOn2S);
1829       }
1830     }//for (j=0; j<1; ++j) {
1831   }//for (i=1; i<=aNbLines; ++i)
1832 }
1833 //==================================================================================
1834 // function : Perform
1835 // purpose  : base SS Int. function
1836 //==================================================================================
1837 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
1838                                            const Handle(Adaptor3d_TopolTool)& D1,
1839                                            const Handle(Adaptor3d_HSurface)& Surf2,
1840                                            const Handle(Adaptor3d_TopolTool)& D2,
1841                                            const Standard_Real   TolTangency,
1842                                            const Standard_Real   Epsilon,
1843                                            const Standard_Real   Deflection,
1844                                            const Standard_Real   Increment,
1845                                            const Standard_Boolean ClearFlag) 
1846 {
1847   Standard_Integer NbU1, NbV1, NbU2, NbV2, Limit; 
1848   //
1849   D1->SamplePnts(Deflection, 10, 10);
1850   D2->SamplePnts(Deflection, 10, 10);
1851   //
1852   NbU1 = D1->NbSamplesU();
1853   NbV1 = D1->NbSamplesV();
1854   NbU2 = D2->NbSamplesU();
1855   NbV2 = D2->NbSamplesV();
1856   TColStd_Array1OfReal anUpars1(1, NbU1), aVpars1(1, NbV1);
1857   TColStd_Array1OfReal anUpars2(1, NbU2), aVpars2(1, NbV2);
1858   //
1859   D1->UParameters(anUpars1); 
1860   D1->VParameters(aVpars1);
1861   D2->UParameters(anUpars2); 
1862   D2->VParameters(aVpars2);
1863   //---------------------------------------------
1864   Limit = 2500;
1865   if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit)) {  
1866     empt = Standard_True;
1867     if (ClearFlag){
1868       SLin.Clear();
1869     }
1870     //
1871     IntPolyh_Intersection* pInterference = NULL;
1872
1873     if ( D1->IsUniformSampling() || D2->IsUniformSampling() ) {
1874       pInterference = new IntPolyh_Intersection(Surf1,NbU1,NbV1,Surf2,NbU2,NbV2);
1875     }
1876     else {
1877       pInterference = new IntPolyh_Intersection(Surf1, anUpars1, aVpars1, 
1878                                                 Surf2, anUpars2, aVpars2 );
1879     }
1880     if ( !pInterference ) {
1881       done = Standard_False;
1882       return;
1883     }
1884     //
1885     IntPolyh_Intersection& Interference = *pInterference;
1886     //
1887     done = Interference.IsDone();
1888     if( !done ) {
1889       if (pInterference) {
1890         delete pInterference;
1891         pInterference = NULL;
1892       }
1893       return;
1894     }
1895     
1896     Standard_Integer nbLigSec = Interference.NbSectionLines();
1897     Standard_Integer nbTanZon = Interference.NbTangentZones();
1898     Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
1899
1900     Standard_Integer NbLigCalculee = 0, ver;
1901     Standard_Real U1,U2,V1,V2, pu1,pu2,pv1,pv2, incidence, dminiPointLigne;
1902     Standard_Boolean HasStartPoint,RejetLigne;
1903     IntSurf_PntOn2S StartPOn2S;
1904     gp_Pnt Point3dDebut,Point3dFin;
1905
1906     TColStd_Array1OfReal StartParams(1,4);
1907     IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);
1908
1909     if(nbLigSec>=1) {  
1910       Standard_Integer *TabL = new Standard_Integer [nbLigSec+1];
1911       Standard_Integer ls;
1912       for(ls=1; ls<=nbLigSec; ++ls){
1913         TabL[ls]=ls;
1914       }
1915       //----------------------------------------1.1
1916       Standard_Boolean triok;
1917       do { 
1918         Standard_Integer b, nb_B, nb_A, tyu;
1919         //
1920         triok=Standard_True;
1921         for( b = 2; b <= nbLigSec; ++b ) { 
1922           nb_B = Interference.NbPointsInLine(TabL[b]);
1923           nb_A = Interference.NbPointsInLine(TabL[b-1]);
1924           if( nb_B > nb_A ) { 
1925             tyu=TabL[b]; 
1926             TabL[b]=TabL[b-1];
1927             TabL[b-1]=tyu;
1928             triok=Standard_False;
1929           }
1930         }
1931       } while(triok==Standard_False);
1932       //----------------------------------------
1933       // 1.2 For the line "ls" get 2D-bounds U,V for surfaces 1,2
1934       //
1935       for( ls = 1; ls <= nbLigSec; ++ls) {
1936         Standard_Integer nbp, ilig, *TabPtDep;
1937         //
1938         nbp = Interference.NbPointsInLine(TabL[ls]);
1939         if (!nbp) {
1940           continue;
1941         }
1942         //
1943         TabPtDep = new Standard_Integer [nbp+1];
1944         for( ilig = 1; ilig <= nbp; ++ilig ) {
1945           TabPtDep[ilig]=0;
1946         }
1947         //
1948         Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
1949         Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
1950         Standard_Real _x,_y,_z;
1951         //
1952         Interference.GetLinePoint(TabL[ls], 1, 
1953                                   _x,_y,_z, 
1954                                   UminLig1, VminLig1, UminLig2, VminLig2, 
1955                                   incidence);
1956         
1957         UmaxLig1=UminLig1;
1958         VmaxLig1=VminLig1;
1959         UmaxLig2=UminLig2;
1960         VmaxLig2=VminLig2;
1961         //
1962         for( ilig = 2; ilig <= nbp; ilig++ ) { 
1963           Interference.GetLinePoint(TabL[ls],ilig,_x,_y,_z,U1,V1,U2,V2,incidence);
1964           //
1965           if(U1>UmaxLig1) UmaxLig1=U1;
1966           if(V1>VmaxLig1) VmaxLig1=V1;
1967           if(U2>UmaxLig2) UmaxLig2=U2;
1968           if(V2>VmaxLig2) VmaxLig2=V2;
1969           //
1970           if(U1<UminLig1) UminLig1=U1;
1971           if(V1<VminLig1) VminLig1=V1;
1972           if(U2<UminLig2) UminLig2=U2;
1973           if(V2<VminLig2) VminLig2=V2;
1974         }//for( ilig = 2; ilig <= nbp; ilig++ ) { 
1975         //
1976         //----------------------------------------
1977         // 1.3
1978         Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
1979         Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
1980         Standard_Boolean lignetrouvee=Standard_False;
1981         const Standard_Integer NbDePointsDeDepartDuChmLimit = 5;
1982         //
1983         do { 
1984           NombreDePointsDeDepartDuCheminement++;
1985           switch (NombreDePointsDeDepartDuCheminement) {
1986           case 1:
1987             nbps2 = (nbp > 1) ? nbp/2 : 1;
1988             if(nbp<3) 
1989               NombreDePointsDeDepartDuCheminement = NbDePointsDeDepartDuChmLimit;
1990             break;
1991           case 2:
1992             nbps2 = 1;
1993             break;
1994           case 3:
1995             nbps2 = nbp-1;
1996             break;
1997           case 4:
1998             nbps2 = 3 * nbp / 4;
1999             break;
2000           case 5:
2001             nbps2 = nbp / 4;
2002             break;
2003           default:
2004             nbps2 = NombreDePointsDeDepartDuCheminement-3;
2005             NombreDePointsDeDepartDuCheminement++;
2006           }
2007           //
2008           if(TabPtDep[nbps2] == 0) {
2009             TabPtDep[nbps2] = 1;
2010             Interference.GetLinePoint(TabL[ls],nbps2,_x,_y,_z,U1,V1,U2,V2,incidence);
2011                       
2012             StartParams(1) = U1;
2013             StartParams(2) = V1;
2014             StartParams(3) = U2;
2015             StartParams(4) = V2;
2016
2017             HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
2018             dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
2019             if(HasStartPoint) {
2020               StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2021               NbLigCalculee = SLin.Length();
2022               Standard_Integer l;
2023               for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
2024                 const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2025
2026                 if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
2027                   dminiPointLigne = 0.0;
2028                 }
2029               }// for( l ...
2030
2031               if(dminiPointLigne > SeuildPointLigne) {
2032                 PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
2033                 //
2034                 Standard_Boolean bPWIsDone;
2035                 Standard_Integer iPWNbPoints, aNbPointsVer;
2036                 Standard_Real aD11, aD12, aD21, aD22, aDx;
2037                 //
2038                 bPWIsDone=PW.IsDone();
2039                 if(bPWIsDone) {
2040                   iPWNbPoints=PW.NbPoints();
2041                   //
2042                   if( iPWNbPoints > 2 ) {
2043                     RejetLigne = Standard_False;
2044                     Point3dDebut = PW.Value(1).Value();
2045                     Point3dFin   = PW.Value(iPWNbPoints).Value();
2046                     for( ver = 1; (!RejetLigne) && (ver<= NbLigCalculee); ++ver) { 
2047                       const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2048                       //
2049                       aNbPointsVer=verwline->NbPnts();
2050                       if (aNbPointsVer<3) {
2051                         continue;
2052                       }
2053                       //
2054                       const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2055                       const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
2056                       //xf
2057                       const gp_Pnt& aP21=verPointDebut.Value();
2058                       const gp_Pnt& aP22=verPointFin.Value();
2059                       //
2060                       aD11=Point3dDebut.Distance(aP21);
2061                       aD12=Point3dDebut.Distance(aP22);
2062                       aD21=Point3dFin.Distance(aP21);
2063                       aD22=Point3dFin.Distance(aP22);
2064                       //
2065                       if((aD11<=TolTangency && aD22<=TolTangency) ||
2066                          (aD12<=TolTangency && aD21<=TolTangency)) {
2067                         Standard_Integer m, mx;
2068                         //
2069                         mx=aNbPointsVer/2;
2070                         if (aNbPointsVer%2) {
2071                           ++mx; 
2072                         }
2073                         //
2074                         const gp_Pnt& aPx=verwline->Point(mx).Value();
2075                         for(m=1; m<iPWNbPoints; ++m){
2076                           const gp_Pnt& aP1=PW.Value(m).Value();
2077                           const gp_Pnt& aP2=PW.Value(m+1).Value();
2078                           gp_Vec aVec12(aP1, aP2);
2079                           if (aVec12.SquareMagnitude()<1.e-20){
2080                             continue;
2081                           }
2082                           //
2083                           gp_Dir aDir12(aVec12);
2084                           gp_Lin aLin12(aP1, aDir12);
2085                           aDx=aLin12.Distance(aPx);
2086                           //modified by NIZNHY-PKV Tue May 10 11:08:07 2011f
2087                           if (aDx<=2.*Epsilon) {
2088                           //if (aDx<=TolTangency) {
2089                           //modified by NIZNHY-PKV Tue May 10 11:08:13 2011t  
2090                             RejetLigne = Standard_True;
2091                             break;
2092                           }
2093                         }//for(m=1; m<iPWNbPoints; ++m){
2094                       }//if((aD11<=TolTangency && aD22<=TolTangency) ||...
2095                     }// for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2096                     //
2097                     if(!RejetLigne) {
2098                       IntSurf_TypeTrans trans1,trans2;
2099                       Standard_Real locu,locv;
2100                       gp_Vec norm1,norm2,d1u,d1v;
2101                       gp_Pnt ptbid;
2102                       Standard_Integer indextg;
2103                       gp_Vec tgline(PW.TangentAtLine(indextg));
2104                       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2105                       Surf1->D1(locu,locv,ptbid,d1u,d1v);
2106                       norm1 = d1u.Crossed(d1v);
2107                       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2108                       Surf2->D1(locu,locv,ptbid,d1u,d1v);
2109                       norm2 = d1u.Crossed(d1v);
2110                       if( tgline.DotCross(norm2,norm1) >= 0. ) {
2111                         trans1 = IntSurf_Out;
2112                         trans2 = IntSurf_In;
2113                       }
2114                       else {
2115                         trans1 = IntSurf_In;
2116                         trans2 = IntSurf_Out;
2117                       }
2118
2119                       Standard_Real TolTang = TolTangency;
2120                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2121                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2122                       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2123
2124                       if(wline->NbVertex() == 0) {
2125                         IntPatch_Point vtx;
2126                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2127                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2128                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2129                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2130                         vtx.SetParameter(1);
2131                         wline->AddVertex(vtx);
2132                         
2133                         POn2S = PW.Line()->Value(wline->NbPnts());
2134                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2135                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
2136                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2137                         vtx.SetParameter(wline->NbPnts());
2138                         wline->AddVertex(vtx);
2139                       }
2140                       
2141                       lignetrouvee = Standard_True;
2142
2143                       Standard_Integer slinlen = SLin.Length();
2144                       if( slinlen > 0 ) {
2145                         Standard_Integer cnbV = wline->NbVertex();
2146                         Standard_Integer ciV;
2147                         for( ciV = 1; ciV <= cnbV; ciV++ ) {
2148                           Standard_Real pntDMin = 1.e+100;
2149                           Standard_Integer VDMin = 0;
2150                           Standard_Integer WLDMin = 0;
2151                           gp_Pnt cPV = wline->Vertex(ciV).Value();
2152                           Standard_Integer iL;
2153                           for( iL = 1; iL <= slinlen; iL++) {
2154                             const Handle(IntPatch_Line)& aSLine = SLin.Value(iL);
2155                             IntPatch_IType aType = aSLine->ArcType();
2156                             if( aType != IntPatch_Walking)
2157                               continue;
2158                             const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
2159                             Standard_Integer tnbV = aWLine->NbVertex();
2160                             Standard_Integer tiV;
2161                             for( tiV = 1; tiV <= tnbV; tiV++ ) {
2162                               gp_Pnt tPV = aWLine->Vertex(tiV).Value();
2163                               Standard_Real tDistance = cPV.Distance(tPV);
2164                               Standard_Real uRs1 = Surf1->Surface().UResolution(tDistance);
2165                               Standard_Real vRs1 = Surf1->Surface().VResolution(tDistance);
2166                               Standard_Real uRs2 = Surf2->Surface().UResolution(tDistance);
2167                               Standard_Real vRs2 = Surf2->Surface().VResolution(tDistance);
2168                               Standard_Real RmaxS1 = Max(uRs1,vRs1);
2169                               Standard_Real RmaxS2 = Max(uRs2,vRs2);
2170                               if(RmaxS1 < 1.e-4 && RmaxS2 < 1.e-4) {
2171                                 if( pntDMin > tDistance && tDistance > 1.e-9) {
2172                                   pntDMin = tDistance;
2173                                   VDMin = tiV;
2174                                   WLDMin = iL;
2175                                 }
2176                               }
2177                             }
2178                           }
2179                           
2180                           if( VDMin != 0 ) {
2181                             const Handle(IntPatch_Line)& aSLine = SLin.Value(WLDMin);
2182                             const Handle(IntPatch_WLine)&  aWLine = (*((Handle(IntPatch_WLine)*)&aSLine));
2183                             Standard_Integer tiVpar = (Standard_Integer)aWLine->Vertex(VDMin).ParameterOnLine();
2184                             Standard_Integer ciVpar = (Standard_Integer)wline->Vertex(ciV).ParameterOnLine();
2185                             Standard_Real u11 = 0., u12 = 0., v11 = 0., v12 = 0.;
2186                             Standard_Real u21 = 0., u22 = 0., v21 = 0., v22 = 0.;
2187                             wline->Point(ciVpar).Parameters(u11,v11,u12,v12);
2188                             aWLine->Point(tiVpar).Parameters(u21,v21,u22,v22);
2189
2190                             Handle(IntSurf_LineOn2S) newL2s = new IntSurf_LineOn2S();
2191                             IntSurf_PntOn2S replacePnt = aWLine->Point(tiVpar);
2192                             Standard_Integer cNbP = wline->NbPnts();
2193
2194                             TColStd_SequenceOfInteger VPold;
2195                             Standard_Integer iPo;
2196                             for( iPo = 1; iPo <= cnbV; iPo++ ) {
2197                               Standard_Real Po = wline->Vertex(iPo).ParameterOnLine();
2198                               Standard_Integer IPo = (Standard_Integer) Po;
2199                               VPold.Append(IPo);
2200                             }
2201                             
2202                             Standard_Boolean removeNext = Standard_False;
2203                             Standard_Boolean removePrev = Standard_False;
2204                             if( ciV == 1) {
2205                               Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
2206                               if(dPar > 10) {
2207                                 removeNext = Standard_True;
2208                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2209                                   VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2210                               }
2211                             }
2212                             else if( ciV == cnbV) {
2213                               Standard_Integer dPar = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
2214                               if(dPar > 10) {
2215                                 removePrev = Standard_True;
2216                                 VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
2217                               }
2218                             }
2219                             else {
2220                               Standard_Integer dParMi = Abs( VPold.Value(ciV) - VPold.Value(ciV-1));
2221                               Standard_Integer dParMa = Abs( VPold.Value(ciV) - VPold.Value(ciV+1));
2222                               if(dParMi > 10) {
2223                                 removePrev = Standard_True;
2224                                 VPold.SetValue(ciV, VPold.Value(ciV) - 1 );
2225                               }
2226                               if(dParMa > 10) {
2227                                 removeNext = Standard_True;
2228                                 for( iPo = (ciV+1); iPo <= cnbV; iPo++ ) {
2229                                   if(dParMi > 10)
2230                                     VPold.SetValue(iPo, VPold.Value(iPo) - 2 );
2231                                   else
2232                                     VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2233                                 }
2234                               }
2235                               else {
2236                                 if(dParMi > 10)
2237                                   for( iPo = (ciV+1); iPo <= cnbV; iPo++ )
2238                                     VPold.SetValue(iPo, VPold.Value(iPo) - 1 );
2239                               } 
2240                             }
2241                             Standard_Integer pI = (Standard_Integer) ciVpar;
2242
2243                             Standard_Integer iP;
2244                             for( iP = 1; iP <= cNbP; iP++) {
2245                               if( pI == iP )
2246                                 newL2s->Add(replacePnt);
2247                               else if(removeNext && iP == (pI + 1))
2248                                 continue;
2249                               else if(removePrev && iP == (pI - 1))
2250                                 continue;
2251                               else
2252                                 newL2s->Add(wline->Point(iP));
2253                             }
2254
2255                             IntPatch_Point newVtx;
2256                             gp_Pnt Pnt3dV = aWLine->Vertex(VDMin).Value();
2257                             newVtx.SetValue(Pnt3dV,TolTang,Standard_False);
2258                             newVtx.SetParameters(u21,v21,u22,v22);
2259                             newVtx.SetParameter(VPold.Value(ciV));
2260
2261                             Handle(IntPatch_WLine) NWLine = new IntPatch_WLine(newL2s,Standard_False,trans1,trans2);
2262
2263                             Standard_Integer iV;
2264                             for( iV = 1; iV <= cnbV; iV++ ) {
2265                               if( iV == ciV )
2266                                 NWLine->AddVertex(newVtx);
2267                               else {
2268                                 IntPatch_Point theVtx = wline->Vertex(iV);
2269                                 theVtx.SetParameter(VPold.Value(iV));
2270                                 NWLine->AddVertex(theVtx);
2271                               }
2272                             }
2273
2274                             wline = NWLine;
2275                           }
2276                         }
2277                       }// SLin.Length > 0
2278
2279                       AddWLine(SLin, wline, Deflection);
2280                       empt = Standard_False;
2281                     }// !RejetLigne
2282                   }// PW points > 2
2283                 }// done is True
2284               }// dminiPointLigne > SeuildPointLigne
2285             }// HasStartPoint
2286           }// if TabPtDep[nbps2] == 0
2287         } while(nbp>5 && !( (NombreDePointsDeDepartDuCheminement >= NbDePointsDeDepartDuChmLimit && lignetrouvee) || 
2288                             (NombreDePointsDeDepartDuCheminement-3 >= nbp && (!lignetrouvee))));
2289         delete [] TabPtDep;
2290       }// for( ls ...
2291
2292       delete [] TabL;
2293
2294     }// if nbLigSec >= 1
2295     //
2296     AdjustOnPeriodic(Surf1, Surf2, SLin);
2297     //
2298
2299     //--------------------------------------------------------------------
2300     //-- Calcul des parametres approches a partir des Zones De Tangence --
2301     //--------------------------------------------------------------------
2302     Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2303     Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2304     
2305     UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
2306     UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
2307
2308     // NbPointsInTangentZone always == 1 (eap)
2309     
2310     Standard_Integer z;
2311     for( z=1; z <= nbTanZon; z++) { 
2312       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2313       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) {
2314         Standard_Integer pz=1;
2315         Standard_Real _x,_y,_z;
2316         Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2317
2318         if(U1>UmaxLig1) UmaxLig1=U1;
2319         if(V1>VmaxLig1) VmaxLig1=V1;
2320         if(U2>UmaxLig2) UmaxLig2=U2;
2321         if(V2>VmaxLig2) VmaxLig2=V2;
2322         
2323         if(U1<UminLig1) UminLig1=U1;
2324         if(V1<VminLig1) VminLig1=V1;
2325         if(U2<UminLig2) UminLig2=U2;
2326         if(V2<VminLig2) VminLig2=V2;
2327       //}
2328     }
2329     for(z=1; z <= nbTanZon; z++) {    
2330
2331       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2332       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) { 
2333         Standard_Integer pz=1;
2334         Standard_Real _x,_y,_z;
2335         Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2336
2337         StartParams(1) = U1;
2338         StartParams(2) = V1;
2339         StartParams(3) = U2;
2340         StartParams(4) = V2;
2341         
2342         //-----------------------------------------------------------------------
2343         //-- Calcul du premier point de cheminement a partir du point approche --
2344         //-----------------------------------------------------------------------
2345         HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);   
2346         if(HasStartPoint) { 
2347           //-------------------------------------------------
2348           //-- Un point a ete trouve                       --
2349           //-- On verifie qu il n appartient pas           --
2350           //--  a une ligne de cheminement deja calculee.  --
2351           //-------------------------------------------------
2352           StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2353           
2354           NbLigCalculee = SLin.Length();
2355           dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2356
2357           for(Standard_Integer l=1; 
2358               (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2359               l++) { 
2360             const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2361
2362             if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
2363               dminiPointLigne = 0.0;
2364             }
2365           }
2366
2367           //-- Fin d exploration des lignes
2368           if(dminiPointLigne > SeuildPointLigne) { 
2369             //---------------------------------------------------
2370             //-- Le point de depart du nouveau cheminement     --
2371             //-- n est present dans aucune ligne deja calculee.--
2372             //---------------------------------------------------
2373             PW.Perform(StartParams,
2374                          UminLig1,VminLig1,UminLig2,VminLig2,
2375                          UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
2376             if(PW.IsDone()) {
2377               if(PW.NbPoints()>2) { 
2378                 //-----------------------------------------------
2379                 //-- Verification a posteriori : 
2380                 //-- On teste si le point de depart et de fin de 
2381                 //-- la ligne de cheminement est present dans une 
2382                 //-- autre ligne . 
2383                 //-----------------------------------------------
2384                 RejetLigne = Standard_False;
2385                 Point3dDebut = PW.Value(1).Value();
2386                 const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2387                 Point3dFin   = PointFin.Value();
2388                 
2389                 for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2390                   const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2391                   //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2392
2393                   // Check end point if it is on existing line.
2394                   // Start point is checked before.
2395                   if (IsPointOnLine(PointFin, verwline, Deflection)) {
2396                     RejetLigne = Standard_True; 
2397                     break;
2398                   }
2399
2400                   const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2401                   const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2402                   if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) { 
2403                     RejetLigne = Standard_True; 
2404                   }
2405                   else { 
2406                     if(Point3dFin.Distance(verPointFin.Value()) < TolTangency) { 
2407                       RejetLigne = Standard_True; 
2408                     }
2409                   }
2410                 }
2411                 
2412                 if(!RejetLigne) { 
2413
2414                   IntSurf_TypeTrans trans1,trans2;
2415                   Standard_Real locu,locv;
2416                   gp_Vec norm1,norm2,d1u,d1v;
2417                   gp_Pnt ptbid;
2418                   Standard_Integer indextg;
2419                   gp_Vec tgline(PW.TangentAtLine(indextg));
2420                   PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2421                   Surf1->D1(locu,locv,ptbid,d1u,d1v);
2422                   norm1 = d1u.Crossed(d1v);
2423                   PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2424                   Surf2->D1(locu,locv,ptbid,d1u,d1v);
2425                   norm2 = d1u.Crossed(d1v);
2426                   if (tgline.DotCross(norm2,norm1)>0.) {
2427                     trans1 = IntSurf_Out;
2428                     trans2 = IntSurf_In;
2429                   }
2430                   else {
2431                     trans1 = IntSurf_In;
2432                     trans2 = IntSurf_Out;
2433                   }
2434                   
2435                   
2436                   
2437                   Standard_Real TolTang = TolTangency;
2438                   Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2439                   IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2440                   IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2441                   
2442                   //---------------
2443                     if(wline->NbVertex() == 0) {
2444                       IntPatch_Point vtx;
2445                       IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2446                       POn2S.Parameters(pu1,pv1,pu2,pv2);
2447                       vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2448                       vtx.SetParameters(pu1,pv1,pu2,pv2);
2449                       vtx.SetParameter(1);
2450                       wline->AddVertex(vtx);
2451                       
2452                       POn2S = PW.Line()->Value(wline->NbPnts());
2453                       POn2S.Parameters(pu1,pv1,pu2,pv2);
2454                       vtx.SetValue(Point3dFin,TolTang,Standard_False);
2455                       vtx.SetParameters(pu1,pv1,pu2,pv2);
2456                       vtx.SetParameter(wline->NbPnts());
2457                       wline->AddVertex(vtx);
2458                     }
2459                     
2460                   //---------------
2461                   AddWLine(SLin, wline, Deflection);
2462                   empt = Standard_False;
2463                   
2464                 }
2465                 else { 
2466                   //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2467                 }
2468                 //------------------------------------------------------------          
2469               }
2470             }  //--  le cheminement a reussi (done a True)
2471           }  //--  le point approche ne renvoie pas sur une ligne existante
2472         } //-- Si HasStartPoint
2473       //} //-- Boucle Sur les Points de la Tangent Zone
2474     } //-- Boucle sur Les Tangent Zones
2475
2476     if ( pInterference ) {
2477       delete pInterference;
2478       pInterference = NULL;
2479     }
2480     return;
2481   }// if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit)) {  
2482
2483   Handle(IntSurf_LineOn2S) LOn2S = new IntSurf_LineOn2S();
2484   PointDepart( LOn2S, Surf1, NbU1, NbV1, Surf2, NbU2, NbV2 );
2485   empt = Standard_True;
2486   done = Standard_True;
2487   SLin.Clear();  
2488   
2489   Standard_Integer NbLigCalculee = 0;
2490   Standard_Real U1,U2,V1,V2;
2491   Standard_Real pu1,pu2,pv1,pv2;
2492   
2493   TColStd_Array1OfReal StartParams(1,4);
2494   Standard_Integer MaxOscill = NbU1;
2495   if(MaxOscill < NbU2) MaxOscill=NbU2;
2496   if(MaxOscill < NbV1) MaxOscill=NbV1;
2497   if(MaxOscill < NbV2) MaxOscill=NbV2;
2498   
2499   Standard_Real nIncrement=Increment;
2500   //if(MaxOscill>10)
2501     //nIncrement/=0.5*MaxOscill;
2502
2503   IntPatch_ThePWalkingInter PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,nIncrement);
2504   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
2505   Standard_Real    dminiPointLigne;
2506   Standard_Boolean HasStartPoint,RejetLigne;
2507   IntSurf_PntOn2S StartPOn2S;
2508   Standard_Integer ver;
2509   gp_Pnt Point3dDebut,Point3dFin;
2510
2511   //------------------------------------------------------------
2512   //-- Calcul des parametres approches a partir des Zones De Tangence --
2513   //--------------------------------------------------------------------
2514   Standard_Integer nbTanZon = LOn2S->NbPoints();
2515   for(Standard_Integer z=1; z <= nbTanZon; z++) { 
2516     const IntSurf_PntOn2S& POn2S = LOn2S->Value(z);
2517     POn2S.Parameters(U1,V1,U2,V2);
2518     StartParams(1) = U1;
2519     StartParams(2) = V1;
2520     StartParams(3) = U2;
2521     StartParams(4) = V2;
2522     
2523     //-----------------------------------------------------------------------
2524     //-- Calcul du premier point de cheminement a partir du point approche --
2525     //-----------------------------------------------------------------------
2526     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);       
2527     if(HasStartPoint) { 
2528       //-------------------------------------------------
2529       //-- Un point a ete trouve                       --
2530       //-- On verifie qu il n appartient pas           --
2531       //--  a une ligne de cheminement deja calculee.  --
2532       //-------------------------------------------------
2533       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2534       
2535       NbLigCalculee = SLin.Length();
2536       dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2537       
2538       for(Standard_Integer l=1; 
2539           (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2540           l++) { 
2541         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2542
2543         if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
2544           dminiPointLigne = 0.0;
2545         }
2546       }
2547
2548       //-- Fin d exploration des lignes
2549       if(dminiPointLigne > SeuildPointLigne) { 
2550         //---------------------------------------------------
2551         //-- Le point de depart du nouveau cheminement     --
2552         //-- n est present dans aucune ligne deja calculee.--
2553         //---------------------------------------------------
2554         PW.Perform(StartParams);
2555         if(PW.IsDone()) {
2556           if(PW.NbPoints()>2) { 
2557             //-----------------------------------------------
2558             //-- Verification a posteriori : 
2559             //-- On teste si le point de depart et de fin de 
2560             //-- la ligne de cheminement est present dans une 
2561             //-- autre ligne . 
2562             //-----------------------------------------------
2563             RejetLigne = Standard_False;
2564             Point3dDebut = PW.Value(1).Value();
2565             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2566             Point3dFin   = PointFin.Value();
2567             
2568             for(ver=1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2569               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2570               //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2571
2572               // Check end point if it is on existing line.
2573               // Start point is checked before.
2574               if (IsPointOnLine(PointFin, verwline, Deflection)) {
2575                 RejetLigne = Standard_True; 
2576                 break;
2577               }
2578
2579               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2580               const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2581               if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) { 
2582                 RejetLigne = Standard_True; 
2583               }
2584               else { 
2585                 if(Point3dFin.Distance(verPointFin.Value()) < TolTangency) { 
2586                   RejetLigne = Standard_True; 
2587                 }
2588               }
2589             }
2590             
2591             if(!RejetLigne) { 
2592               
2593               IntSurf_TypeTrans trans1,trans2;
2594               Standard_Real locu,locv;
2595               gp_Vec norm1,norm2,d1u,d1v;
2596               gp_Pnt ptbid;
2597               Standard_Integer indextg;
2598               gp_Vec tgline(PW.TangentAtLine(indextg));
2599               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2600               Surf1->D1(locu,locv,ptbid,d1u,d1v);
2601               norm1 = d1u.Crossed(d1v);
2602               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2603               Surf2->D1(locu,locv,ptbid,d1u,d1v);
2604               norm2 = d1u.Crossed(d1v);
2605               if (tgline.DotCross(norm2,norm1)>0.) {
2606                 trans1 = IntSurf_Out;
2607                 trans2 = IntSurf_In;
2608               }
2609               else {
2610                 trans1 = IntSurf_In;
2611                 trans2 = IntSurf_Out;
2612               }
2613               
2614               
2615               
2616               Standard_Real TolTang = TolTangency;
2617               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2618               IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2619               IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2620               
2621               //---------------
2622               if(wline->NbVertex() == 0) {
2623                 IntPatch_Point vtx;
2624                 const IntSurf_PntOn2S& POn2Sf = PW.Line()->Value(1);
2625                 POn2Sf.Parameters(pu1,pv1,pu2,pv2);
2626                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2627                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2628                 vtx.SetParameter(1);
2629                 wline->AddVertex(vtx);
2630                 
2631                 const IntSurf_PntOn2S& POn2Sl = PW.Line()->Value(wline->NbPnts());
2632                 POn2Sl.Parameters(pu1,pv1,pu2,pv2);
2633                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
2634                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2635                 vtx.SetParameter(wline->NbPnts());
2636                 wline->AddVertex(vtx);
2637               }
2638               
2639               //---------------
2640               AddWLine(SLin, wline, Deflection);
2641               empt = Standard_False;
2642               
2643             }
2644             else { 
2645               //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2646             }
2647             //------------------------------------------------------------              
2648           }
2649         }  //--  le cheminement a reussi (done a True)
2650       }  //--  le point approche ne renvoie pas sur une ligne existante
2651     } //-- Si HasStartPoint
2652   } //-- Boucle sur Les Tangent Zones
2653     
2654 }
2655 //modified by NIZNHY-PKV Wed May 25 09:39:07 2011f
2656 //=======================================================================
2657 //class : IntPatch_InfoPD
2658 //purpose  : 
2659 //=======================================================================
2660 class IntPatch_InfoPD {
2661  public:
2662   //----------------------------------------C-tor
2663   IntPatch_InfoPD(const Standard_Integer aNBI) {
2664     Standard_Integer aNBI2, i, j;
2665     myNBI=aNBI;
2666     //
2667     aNBI2=aNBI*aNBI;
2668     myP1DS2=new char[aNBI2];
2669     myP2DS1=new char[aNBI2];
2670     myIP1=new Standard_Integer[aNBI2]; 
2671     myIP2=new Standard_Integer[aNBI2];
2672     myP1=new gp_Pnt[aNBI2];
2673     myP2=new gp_Pnt[aNBI2];
2674     //
2675     for (i=0; i<myNBI; ++i) {
2676       for (j=0; j<myNBI; ++j) {
2677         xP1DS2(i, j)=0;
2678         xP2DS1(i, j)=0;
2679         xIP1(i, j)=0;
2680         xIP2(i, j)=0;
2681         xP1(i, j).SetCoord(0., 0., 0.);
2682         xP2(i, j).SetCoord(0., 0., 0.);
2683       }
2684     }
2685   };
2686   //---------------------------------------- D-tor
2687   ~IntPatch_InfoPD() {
2688     delete [] (char*) myP1DS2;
2689     delete [] (char*) myP2DS1;
2690     delete [] (Standard_Integer*) myIP1;
2691     delete [] (Standard_Integer*) myIP2; 
2692     delete [] (gp_Pnt*)myP1;
2693     delete [] (gp_Pnt*)myP2;
2694   };
2695   //---------------------------------------- Index
2696   Standard_Integer Index(const Standard_Integer i,
2697                          const Standard_Integer j) const { 
2698     return i*myNBI+j;
2699   };
2700   //---------------------------------------- NBI
2701   Standard_Integer NBI() const { 
2702     return myNBI;
2703   };
2704   //----------------------------------------xP1DS2
2705   char& xP1DS2(const Standard_Integer i,
2706                const Standard_Integer j) { 
2707     return myP1DS2[Index(i,j)];
2708   };
2709   //----------------------------------------xP2DS1
2710   char& xP2DS1(const Standard_Integer i,
2711                const Standard_Integer j) { 
2712     return myP2DS1[Index(i,j)];
2713   };
2714   //----------------------------------------xIP1
2715   Standard_Integer& xIP1(const Standard_Integer i,
2716                          const Standard_Integer j) { 
2717     return myIP1[Index(i,j)];
2718   };
2719   //----------------------------------------xIP2
2720   Standard_Integer& xIP2(const Standard_Integer i,
2721                          const Standard_Integer j) { 
2722     return myIP2[Index(i,j)];
2723   };
2724   //----------------------------------------xP1
2725   gp_Pnt& xP1(const Standard_Integer i,
2726               const Standard_Integer j) { 
2727     return myP1[Index(i,j)];
2728   };
2729   //----------------------------------------xP1
2730   gp_Pnt& xP2(const Standard_Integer i,
2731               const Standard_Integer j) { 
2732     return myP2[Index(i,j)];
2733   };
2734  protected:
2735   Standard_Integer myNBI;
2736   char *myP1DS2;
2737   char *myP2DS1;
2738   Standard_Integer *myIP1;
2739   Standard_Integer *myIP2;
2740   gp_Pnt *myP1;
2741   gp_Pnt *myP2;
2742 }; 
2743 //modified by NIZNHY-PKV Tue May 24 11:38:55 2011t
2744 //==================================================================================
2745 // function : PointDepart
2746 // purpose  : 
2747 //==================================================================================
2748 void IntPatch_PrmPrmIntersection::PointDepart(Handle(IntSurf_LineOn2S)& LineOn2S,
2749                                               const Handle(Adaptor3d_HSurface)& S1,
2750                                               const Standard_Integer SU_1,
2751                                               const Standard_Integer SV_1,
2752                                               const Handle(Adaptor3d_HSurface)& S2,
2753                                               const Standard_Integer SU_2,
2754                                               const Standard_Integer SV_2) const 
2755
2756   Standard_Integer i, j, xNBI;
2757   //modified by NIZNHY-PKV Tue May 24 11:37:38 2011f
2758   xNBI=200;
2759   IntPatch_InfoPD aIPD(xNBI);
2760   //modified by NIZNHY-PKV Wed May 25 06:47:12 2011t
2761   Standard_Integer iC15, SU1, SV1, SU2, SV2;
2762   Standard_Real U0, U1, V0, V1, U, V;
2763   Standard_Real resu0,resu1,resv0,resv1;
2764   Standard_Real  du1,du2,dv1,dv2, dmaxOn1, dmaxOn2;
2765   Standard_Real x0,y0,z0, x1,y1,z1,d;
2766   Bnd_Box Box1, Box2;
2767   //
2768   iC15=15;
2769   SU1 =iC15*SU_1 ;
2770   SV1 =iC15*SV_1 ;
2771   SU2 =iC15*SU_2 ;
2772   SV2 =iC15*SV_2 ;
2773   //
2774   if(xNBI<SU1) {
2775     SU1 = xNBI;
2776   }
2777   if(xNBI<SV1){
2778     SV1 = xNBI;
2779   }
2780   if(xNBI<SU2){
2781     SU2 = xNBI;
2782   }
2783   if(xNBI<SV2){
2784     SV2 = xNBI;
2785   }
2786   //
2787   U0 = S1->FirstUParameter();
2788   U1 = S1->LastUParameter();
2789   V0 = S1->FirstVParameter();
2790   V1 = S1->LastVParameter();
2791   //
2792   resu0=U0;
2793   resu1=U1;
2794   resv1=V1;
2795   resv0=V0;
2796   //
2797   dmaxOn1 = 0.0;
2798   dmaxOn2 = 0.0;
2799   //-----
2800   du1 = (U1-U0)/(SU1-1);
2801   dv1 = (V1-V0)/(SV1-1);
2802   for(U=U0,i=0; i<SU1; i++,U+=du1) { 
2803     for(V=V0,j=0; j<SV1; V+=dv1,j++) { 
2804       aIPD.xP1(i, j)= S1->Value(U,V);
2805       Box1.Add(aIPD.xP1(i, j));
2806       if(i>0 && j>0) { 
2807         aIPD.xP1(i, j)    .Coord(x0,y0,z0);
2808         aIPD.xP1(i-1, j-1).Coord(x1,y1,z1);
2809         //
2810         d=Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
2811         if(d>dmaxOn1) {
2812           dmaxOn1 = d;
2813         }
2814       }
2815     }
2816   }
2817   Box1.Enlarge(1.e-8);
2818   //
2819   U0 = S2->FirstUParameter();
2820   U1 = S2->LastUParameter();
2821   V0 = S2->FirstVParameter();
2822   V1 = S2->LastVParameter();
2823   //
2824   du2 = (U1-U0)/(SU2-1);  
2825   dv2 = (V1-V0)/(SV2-1);
2826   for(U=U0,i=0; i<SU2; i++,U+=du2) { 
2827     for(V=V0,j=0; j<SV2; V+=dv2,j++) { 
2828       aIPD.xP2(i, j) = S2->Value(U,V);
2829       Box2.Add(aIPD.xP2(i, j));
2830       if(i>0 && j>0) { 
2831         aIPD.xP2(i, j)    .Coord(x0,y0,z0);
2832         aIPD.xP2(i-1, j-1).Coord(x1,y1,z1);
2833         d =  Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
2834         if(d>dmaxOn2) {
2835           dmaxOn2 = d;
2836         }
2837       }
2838     }
2839   }
2840   Box2.Enlarge(1.e-8);
2841   //--------
2842   //
2843   if(Box1.IsOut(Box2)) {
2844     
2845     return;
2846   }
2847   //
2848   Standard_Integer aNbPG;
2849   Standard_Real x10,y10,z10,x11,y11,z11;
2850   Standard_Real x20,y20,z20,x21,y21,z21;
2851   Standard_Real dx, dy, dz, dmax;
2852   Standard_Real dx2, dy2, dz2;
2853   //
2854   Box1.Get(x10,y10,z10,x11,y11,z11);
2855   Box2.Get(x20,y20,z20,x21,y21,z21);
2856   //
2857   x0 = (x10>x20)? x10 : x20;
2858   y0 = (y10>y20)? y10 : y20;
2859   z0 = (z10>z20)? z10 : z20;
2860   //
2861   x1 = (x11<x21)? x11 : x21;
2862   y1 = (y11<y21)? y11 : y21;
2863   z1 = (z11<z21)? z11 : z21;
2864   //
2865   if(dmaxOn2 > dmaxOn1) {
2866     dmaxOn1 = dmaxOn2;
2867   }
2868   //
2869   dmaxOn1+=dmaxOn1;
2870   x0-=dmaxOn1;
2871   y0-=dmaxOn1;
2872   z0-=dmaxOn1;
2873   x1+=dmaxOn1;
2874   y1+=dmaxOn1;
2875   z1+=dmaxOn1;
2876   //
2877   x10-=dmaxOn1;   y10-=dmaxOn1;   z10-=dmaxOn1; 
2878   x11+=dmaxOn1;   y11+=dmaxOn1;   z11+=dmaxOn1; 
2879
2880   x20-=dmaxOn1;   y20-=dmaxOn1;   z20-=dmaxOn1; 
2881   x21+=dmaxOn1;   y21+=dmaxOn1;   z21+=dmaxOn1; 
2882
2883   aNbPG=NbPointsGrille();
2884   dx = (x1-x0)/aNbPG;
2885   dy = (y1-y0)/aNbPG;
2886   dz = (z1-z0)/aNbPG;
2887   //
2888   dmax = dx;
2889   if(dy>dmax) {
2890     dmax = dy;
2891   }
2892   if(dz>dmax){
2893     dmax = dz;
2894   }
2895   //
2896   if(dx<dmax*0.01) {
2897     dx = dmax*0.01;
2898   }
2899   if(dy<dmax*0.01) {
2900     dy = dmax*0.01;
2901   }
2902   if(dz<dmax*0.01) {
2903     dz = dmax*0.01;
2904   }
2905   //
2906   dx2 = dx*0.5;
2907   dy2 = dy*0.5;
2908   dz2 = dz*0.5 ;
2909   //
2910   IntPatch_PrmPrmIntersection_T3Bits M1(_BASE);
2911   IntPatch_PrmPrmIntersection_T3Bits M2(_BASE);
2912   //
2913   for(i=0;i<SU1;i++) { 
2914     for(j=0;j<SV1;j++) { 
2915       aIPD.xIP1(i, j)=-1;
2916       const gp_Pnt& P=aIPD.xP1(i, j);
2917       aIPD.xP1DS2(i, j) = (char)CodeReject(x20,y20,z20,x21,y21,z21,P.X(),P.Y(),P.Z());
2918       int ix = (int)((P.X()-x0  + dx2 )/dx);
2919       if(DansGrille(ix)) { 
2920         int iy = (int)((P.Y()-y0 + dy2)/dy);
2921         if(DansGrille(iy)) {
2922           int iz = (int)((P.Z()-z0 + dz2)/dz);
2923           if(DansGrille(iz)) {
2924             aIPD.xIP1(i, j) = GrilleInteger(ix,iy,iz);
2925           }
2926         }
2927       }
2928     }
2929   }
2930   //-- cout<<" Grille  du 1 fini "<<endl;
2931   for(i=0;i<SU2;i++) { 
2932     for(j=0;j<SV2;j++) { 
2933       aIPD.xIP2(i, j)=-1;
2934       const gp_Pnt& P=aIPD.xP2(i, j);
2935       aIPD.xP2DS1(i, j) = (char)CodeReject(x10,y10,z10,x11,y11,z11,P.X(),P.Y(),P.Z());
2936       int ix = (int)((P.X()-x0 + dx2)/dx);
2937       if(DansGrille(ix)) { 
2938         int iy = (int)((P.Y()-y0 + dy2)/dy);
2939         if(DansGrille(iy)) {
2940           int iz = (int)((P.Z()-z0 + dz2)/dz);
2941           if(DansGrille(iz)) {
2942             aIPD.xIP2(i, j) =  GrilleInteger(ix,iy,iz);
2943           }
2944         }
2945       }
2946     }
2947   }
2948   //
2949   for(i=0;i<SU1-1;i+=1) {
2950     for(j=0;j<SV1-1;j+=1) { 
2951       if(!((aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j)) || 
2952            (aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j+1)))){
2953         Remplit(aIPD.xIP1(i, j),
2954                 aIPD.xIP1(i+1, j),
2955                 aIPD.xIP1(i+1, j+1),
2956                 M1);
2957       }
2958       if(!((aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i, j+1)) || 
2959            (aIPD.xP1DS2(i, j) & aIPD.xP1DS2(i+1, j+1)))) {
2960         Remplit(aIPD.xIP1(i, j),
2961                 aIPD.xIP1(i, j+1),
2962                 aIPD.xIP1(i+1, j+1),
2963                 M1);    
2964       }
2965     }
2966   }     
2967   //
2968   for(i=0;i<SU2-1;i+=1) {
2969     for(j=0;j<SV2-1;j+=1) { 
2970       if(!((aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j)) ||
2971            (aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j+1)))){
2972         Remplit(aIPD.xIP2(i, j),
2973                 aIPD.xIP2(i+1, j),
2974                 aIPD.xIP2(i+1, j+1),
2975                 M2);
2976       }
2977       if(!((aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i, j+1)) || 
2978            (aIPD.xP2DS1(i, j) & aIPD.xP2DS1(i+1, j+1)))){
2979         Remplit(aIPD.xIP2(i, j),
2980                 aIPD.xIP2(i, j+1),
2981                 aIPD.xIP2(i+1, j+1),
2982                 M2);    
2983       }
2984     }
2985   }     
2986   //
2987   M1.ResetAnd();
2988   M2.ResetAnd();
2989   //
2990   int newind=0;
2991   long unsigned Compt=0;
2992   int ok=0;
2993   int indicepointtraite = 0;
2994   Standard_Integer k,nu,nv;
2995   //
2996   do { 
2997     indicepointtraite--;
2998     ok = M1.And(M2,newind);
2999     if(ok) { 
3000       IntegerGrille(newind,i,j,k);
3001       int nb=0;
3002       int LIM=3;
3003       if(   DansGrille(i-1) && DansGrille(j-1) && DansGrille(k-1) 
3004          && DansGrille(i+1) && DansGrille(j+1) && DansGrille(k+1)) { 
3005         int si,sj,sk;
3006         for(si=-1; si<= 1 && nb<LIM; si++) { 
3007           for(sj=-1; sj<= 1 && nb<LIM; sj++) { 
3008             for(sk=-1; sk<= 1 && nb<LIM; sk++) { 
3009               long unsigned lu=GrilleInteger(i+si,j+sj,k+sk);
3010               if(M1.Val(lu) && M2.Val(lu)) { 
3011                 nb++;
3012               }
3013             }
3014           }
3015         }
3016         if(nb>=LIM) { 
3017           for(si=-1; si<= 1; si++) { 
3018             for(sj=-1; sj<= 1; sj++) { 
3019               for(sk=-1; sk<= 1; sk++) { 
3020                 if(si || sj || sk) { 
3021                   long unsigned lu=GrilleInteger(i+si,j+sj,k+sk);
3022                   M1.Raz(lu);
3023                 }
3024               }
3025             }
3026           }
3027         }
3028       }
3029       //
3030       gp_Pnt P(dx*i + x0, dy*j + y0, dz*k+z0);
3031       //
3032       Standard_Integer nu1=-1,nu2=-1;
3033       Standard_Integer nv1=0, nv2=0;
3034       int nbsur1 = 0;
3035       for(nu=0;nu1<0 && nu<SU1;nu++) { 
3036         for(nv=0;nu1<0 && nv<SV1;nv++) { 
3037           if( aIPD.xIP1(nu, nv) ==(Standard_Integer) newind )  { 
3038             nbsur1++;