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