0028643: Coding rules - eliminate GCC compiler warnings -Wmisleading-indentation
[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                     //the method PutVertexOnLine can reduce the number of points in <wline>
595                     IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
596                     if (wline->NbPnts() < 2)
597                       continue;
598                     IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
599                     if (wline->NbPnts() < 2)
600                       continue;
601
602                     if(wline->NbVertex() == 0) {
603                       IntPatch_Point vtx;
604                       IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
605                       POn2S.Parameters(pu1,pv1,pu2,pv2);
606                       vtx.SetValue(Point3dDebut,TolTang,Standard_False);
607                       vtx.SetParameters(pu1,pv1,pu2,pv2);
608                       vtx.SetParameter(1);
609                       wline->AddVertex(vtx);
610
611                       POn2S = PW.Line()->Value(wline->NbPnts());
612                       POn2S.Parameters(pu1,pv1,pu2,pv2);
613                       vtx.SetValue(Point3dFin,TolTang,Standard_False);
614                       vtx.SetParameters(pu1,pv1,pu2,pv2);
615                       vtx.SetParameter(wline->NbPnts());
616                       wline->AddVertex(vtx);
617                     }
618
619                     lignetrouvee = Standard_True;
620                     AddWLine(SLin, wline, Deflection);
621                     empt = Standard_False;
622                   }// !RejetLigne
623                 }// PW.NbPoints()>2
624               }// done is True
625             }// dminiPointLigne > SeuildPointLigne
626           }// HasStartPoint
627         }// TabPtDep[nbps2]==0
628       } while( nbp>5 && ( !( ( (NombreDePointsDeDepartDuCheminement >= 3) && lignetrouvee ) || 
629         ( (NombreDePointsDeDepartDuCheminement-3>=nbp) && !lignetrouvee ) ) ) );
630
631       delete [] TabPtDep;
632     }// for( ls ...
633
634     delete [] TabL;
635   }// nbLigSec >= 1
636
637   Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
638   Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
639
640   UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
641   UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
642
643   Standard_Integer z;
644   for(z=1; z <= nbTanZon; z++) { 
645     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
646     for(Standard_Integer pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
647       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly2,U1,V1,U2,V2);
648
649       if(U1>UmaxLig1) UmaxLig1=U1;
650       if(V1>VmaxLig1) VmaxLig1=V1;
651       if(U2>UmaxLig2) UmaxLig2=U2;
652       if(V2>VmaxLig2) VmaxLig2=V2;
653
654       if(U1<UminLig1) UminLig1=U1;
655       if(V1<VminLig1) VminLig1=V1;
656       if(U2<UminLig2) UminLig2=U2;
657       if(V2<VminLig2) VminLig2=V2;
658     }
659   }
660
661   for(z=1; z <= nbTanZon; z++) {
662     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
663     Standard_Integer pz;
664     for( pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
665       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly2,U1,V1,U2,V2);
666
667       StartParams(1) = U1;
668       StartParams(2) = V1;
669       StartParams(3) = U2;
670       StartParams(4) = V2;
671
672       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
673       if(HasStartPoint) { 
674         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
675         NbLigCalculee = SLin.Length();
676         dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
677         Standard_Integer l;
678         for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
679           const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
680
681           if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
682             dminiPointLigne = 0.0;
683           }
684         }// for( l ...
685
686         if(dminiPointLigne > SeuildPointLigne) { 
687           PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
688           if(PW.IsDone()) {
689             if(PW.NbPoints()>2) { 
690               RejetLigne = Standard_False;
691               Point3dDebut = PW.Value(1).Value();
692               const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
693               Point3dFin   = PointFin.Value();
694               for(ver=1 ; ver<= NbLigCalculee ; ver++) { 
695                 const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
696
697                 // Check end point if it is on existing line.
698                 // Start point is checked before.
699                 if (IsPointOnLine(PointFin, verwline, Deflection)) {
700                   RejetLigne = Standard_True; 
701                   break;
702                 }
703
704                 const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
705                 const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
706                 if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
707                     (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
708                 {
709                   RejetLigne = Standard_True; 
710                   break;
711                 }
712               }
713
714               if(RejetLigne)
715               {
716                 DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
717               }
718
719               if(!RejetLigne) { 
720                 IntSurf_TypeTrans trans1,trans2;
721                 Standard_Real locu,locv;
722                 gp_Vec norm1,norm2,d1u,d1v;
723                 gp_Pnt ptbid;
724                 Standard_Integer indextg;
725                 gp_Vec tgline(PW.TangentAtLine(indextg));
726                 PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
727                 Surf1->D1(locu,locv,ptbid,d1u,d1v);
728                 norm1 = d1u.Crossed(d1v);
729                 PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
730                 Surf2->D1(locu,locv,ptbid,d1u,d1v);
731                 norm2 = d1u.Crossed(d1v);
732                 if(tgline.DotCross(norm2,norm1)>0.) {
733                   trans1 = IntSurf_Out;
734                   trans2 = IntSurf_In;
735                 }
736                 else {
737                   trans1 = IntSurf_In;
738                   trans2 = IntSurf_Out;
739                 }
740
741                 Standard_Real TolTang = TolTangency;
742                 Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
743                 //the method PutVertexOnLine can reduce the number of points in <wline>
744                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
745                 if (wline->NbPnts() < 2)
746                   continue;
747                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
748                 if (wline->NbPnts() < 2)
749                   continue;
750
751                 if(wline->NbVertex() == 0) {
752                   IntPatch_Point vtx;
753                   IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
754                   POn2S.Parameters(pu1,pv1,pu2,pv2);
755                   vtx.SetValue(Point3dDebut,TolTang,Standard_False);
756                   vtx.SetParameters(pu1,pv1,pu2,pv2);
757                   vtx.SetParameter(1);
758                   wline->AddVertex(vtx);
759
760                   POn2S = PW.Line()->Value(wline->NbPnts());
761                   POn2S.Parameters(pu1,pv1,pu2,pv2);
762                   vtx.SetValue(Point3dFin,TolTang,Standard_False);
763                   vtx.SetParameters(pu1,pv1,pu2,pv2);
764                   vtx.SetParameter(wline->NbPnts());
765                   wline->AddVertex(vtx);
766                 }
767
768                 AddWLine(SLin, wline, Deflection);
769                 empt = Standard_False;
770               }// if !RejetLigne
771             }// PW.NbPoints()>2
772           }// done is True
773         }// dminiPointLigne > SeuildPointLigne
774       }// HasStartPoint
775     }// for( pz ...
776   }// for( z ...
777 }
778
779 //==================================================================================
780 // function : Perform
781 // purpose  : 
782 //==================================================================================
783 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&    Surf1,
784                                            const IntPatch_Polyhedron&  Poly1,
785                                            const Handle(Adaptor3d_TopolTool)& D1,
786                                            const Standard_Real   TolTangency,
787                                            const Standard_Real   Epsilon,
788                                            const Standard_Real   Deflection,
789                                            const Standard_Real   Increment)
790
791   IntPatch_InterferencePolyhedron Interference(Poly1);
792   empt = Standard_True;
793   done = Standard_True;
794   SLin.Clear();  
795
796   Standard_Integer nbLigSec = Interference.NbSectionLines();
797   Standard_Integer nbTanZon = Interference.NbTangentZones();
798
799   Standard_Integer NbPntOn2SOnLine;
800   Standard_Integer NbLigCalculee = 0;
801
802   Standard_Real U1,U2,V1,V2;
803   Standard_Real pu1,pu2,pv1,pv2;
804
805   TColStd_Array1OfReal StartParams(1,4);
806   IntWalk_PWalking PW(Surf1,Surf1,TolTangency,Epsilon,Deflection,Increment);
807
808   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
809   Standard_Real    incidence;
810   Standard_Real    dminiPointLigne;
811
812   Standard_Boolean HasStartPoint,RejetLigne;
813
814   IntSurf_PntOn2S StartPOn2S;
815
816   Standard_Integer ver;
817
818   gp_Pnt Point3dDebut,Point3dFin;
819
820   if(nbLigSec>=1) {
821     Standard_Integer ls;
822     for( ls = 1; ls <= nbLigSec; ls++) {
823       const Intf_SectionLine& LineSec=Interference.LineValue(ls);
824       Standard_Integer nbp = LineSec.NumberOfPoints();
825       Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
826       Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
827       Standard_Integer IndicePointdeDepart1 = 0, IndicePointdeDepart2 = 0;
828       do { 
829         NombreDePointsDeDepartDuCheminement++;
830         if(NombreDePointsDeDepartDuCheminement == 1) { 
831           incidence = 0.0;
832           Standard_Integer nsp1;
833           for( nsp1= nbp/2; nsp1 >= 1; nsp1--) { 
834             SectionPointToParameters(LineSec.GetPoint(nsp1),Poly1,Poly1,U1,V1,U2,V2);
835             Standard_Real CurrentIncidence =  Abs(U1-U2)+Abs(V1-V2);
836             if(CurrentIncidence > incidence) { 
837               nbps2 = nsp1;
838               incidence = CurrentIncidence;
839             }
840           }
841           for( nsp1 = nbp/2; nsp1 <= nbp; nsp1++) { 
842             SectionPointToParameters(LineSec.GetPoint(nsp1),Poly1,Poly1,U1,V1,U2,V2);
843             Standard_Real CurrentIncidence =  Abs(U1-U2)+Abs(V1-V2);
844             if(CurrentIncidence > incidence) { 
845               nbps2 = nsp1;
846               incidence = CurrentIncidence;
847             }
848           }
849
850           if(nbp<3) 
851             NombreDePointsDeDepartDuCheminement=3;
852
853           IndicePointdeDepart1 = nbps2;
854         }
855         else if(NombreDePointsDeDepartDuCheminement == 2) { 
856           if(IndicePointdeDepart1 == 1) { 
857             nbps2 = nbp/2;
858             IndicePointdeDepart2 = nbps2;
859           }
860           else { 
861             nbps2 = 1;
862             IndicePointdeDepart2 = 1;
863           }
864         }
865         else {
866           if(IndicePointdeDepart1 == nbp)
867             nbps2 = (IndicePointdeDepart1+IndicePointdeDepart2)/2;
868           else
869             nbps2 = nbp;
870         }
871
872         SectionPointToParameters(LineSec.GetPoint(nbps2),Poly1,Poly1,U1,V1,U2,V2);
873
874         StartParams(1) = U1;
875         StartParams(2) = V1;
876         StartParams(3) = U2;
877         StartParams(4) = V2;
878
879         HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
880         dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
881         if(HasStartPoint) { 
882           StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
883           if(Abs(pu1-pu2)>1e-7 || Abs(pv1-pv2)>1e-7) { 
884             NbLigCalculee = SLin.Length();
885             Standard_Integer l;
886             for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
887               const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
888               if( (testwline->IsOutSurf1Box(gp_Pnt2d(pu1,pv1))==Standard_False) &&
889                 (testwline->IsOutSurf2Box(gp_Pnt2d(pu2,pv2))==Standard_False) &&
890                 (testwline->IsOutBox(StartPOn2S.Value())==Standard_False) ) { 
891                   NbPntOn2SOnLine = testwline->NbPnts();
892                   Standard_Integer ll;
893                   for( ll=1; (ll < NbPntOn2SOnLine) && (dminiPointLigne >= SeuildPointLigne); ll++) { 
894                     Standard_Real t,Au1,Av1,Au2,Av2,Bu1,Bv1,Bu2,Bv2;
895                     testwline->Point(ll).Parameters(Au1,Av1,Au2,Av2);
896                     testwline->Point(ll+1).Parameters(Bu1,Bv1,Bu2,Bv2);
897                     if(Au1>Bu1) {
898                       t=Au1;
899                       Au1=Bu1;
900                       Bu1=t;
901                     } 
902                     if(Av1>Bv1) {
903                       t=Av1;
904                       Av1=Bv1;
905                       Bv1=t;
906                     } 
907                     Au1-=1.0e-7;
908                     Av1-=1.0e-7;
909                     Bu1+=1.0e-7;
910                     Bv1+=1.0e-7;
911
912                     if((pu1>=Au1) && (pu1<=Bu1) && (pv1>=Av1) && (pv1<=Bv1))
913                       dminiPointLigne = 0.0; 
914                     else { 
915                       if((pu2>=Au1) && (pu2<=Bu1) && (pv2>=Av1) && (pv2<=Bv1))
916                         dminiPointLigne = 0.0;
917                     }
918                   }// for( ll ...
919               }// if ...
920             }// for( l ...
921
922             if(dminiPointLigne > SeuildPointLigne) { 
923               PW.Perform(StartParams);
924               if(PW.IsDone()) {
925                 if(PW.NbPoints()>2) { 
926                   RejetLigne = Standard_False;
927                   Point3dDebut = PW.Value(1).Value();
928                   Point3dFin   = PW.Value(PW.NbPoints()).Value();
929                   for(ver=1 ; ver<= NbLigCalculee ; ver++) { 
930                     const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
931                     const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
932                     const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
933                     if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
934                         (Point3dFin.Distance(verPointFin.Value()) < TolTangency)) 
935                     {
936                       RejetLigne = Standard_True; 
937                       break;
938                     }
939                   }
940
941                   if(RejetLigne)
942                   {
943                     DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
944                   }
945
946                   if(!RejetLigne) { 
947                     IntSurf_TypeTrans trans1,trans2;
948                     Standard_Real locu,locv;
949                     gp_Vec norm1,norm2,d1u,d1v;
950                     gp_Pnt ptbid;
951                     Standard_Integer indextg;
952                     gp_Vec tgline(PW.TangentAtLine(indextg));
953                     PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
954                     Surf1->D1(locu,locv,ptbid,d1u,d1v);
955                     norm1 = d1u.Crossed(d1v);
956                     PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
957                     Surf1->D1(locu,locv,ptbid,d1u,d1v);
958                     norm2 = d1u.Crossed(d1v);
959                     if (tgline.DotCross(norm2,norm1)>0.) {
960                       trans1 = IntSurf_Out;
961                       trans2 = IntSurf_In;
962                     }
963                     else {
964                       trans1 = IntSurf_In;
965                       trans2 = IntSurf_Out;
966                     }
967
968                     IntSurf_LineOn2S LineOn2S;
969                     Standard_Integer nbpw,imin,imax,i;
970                     nbpw = PW.Line()->NbPoints();
971                     Standard_Real u1,v1,u2,v2;
972                     i=0;
973                     do { 
974                       i++;
975                       imin=i;
976                       const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
977                       Pi.Parameters(u1,v1,u2,v2);
978                     } while((i<nbpw)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
979
980                     if(imin>2)
981                       imin--;
982
983                     i=nbpw+1;
984                     do { 
985                       i--;
986                       imax=i;
987                       const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
988                       Pi.Parameters(u1,v1,u2,v2);
989                     } while((i>2)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
990
991                     if(imax<nbpw)
992                       imax++;
993
994                     if(imin<imax) { 
995                       Handle(IntSurf_LineOn2S) PWLine = new IntSurf_LineOn2S();
996                       for(i=imin;i<=imax;i++) 
997                         PWLine->Add(PW.Line()->Value(i));
998
999                       Standard_Real TolTang = TolTangency;
1000                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PWLine,Standard_False,trans1,trans2);
1001                       const IntSurf_PntOn2S& POn2SDeb = wline->Point(1);
1002                       const IntSurf_PntOn2S& POn2SFin = wline->Point(wline->NbPnts());
1003                       if((POn2SDeb.Value()).Distance(POn2SFin.Value()) <= TolTangency) { 
1004                         Standard_Real u1t,v1t,u2t,v2t; 
1005                         POn2SDeb.Parameters(u1t,v1t,u2t,v2t);
1006                         IntPatch_Point vtx;
1007                         vtx.SetValue(POn2SDeb.Value(),TolTang,Standard_False);
1008                         vtx.SetParameters(u2t,v2t,u1t,v1t);
1009                         vtx.SetParameter(wline->NbPnts());
1010                         wline->SetPoint(wline->NbPnts(),vtx);
1011                       }
1012                       //the method PutVertexOnLine can reduce the number of points in <wline>
1013                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf1,Standard_True,TolTang);
1014                       if (wline->NbPnts() < 2)
1015                         continue;
1016                       if(wline->NbVertex() == 0) {
1017                         IntPatch_Point vtx;
1018                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1019                         POn2S.Parameters(pu1,pv1,pu2,pv2);
1020                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1021                         vtx.SetParameters(pu1,pv1,pu2,pv2);
1022                         vtx.SetParameter(1);
1023                         wline->AddVertex(vtx);
1024
1025                         POn2S = PW.Line()->Value(wline->NbPnts());
1026                         POn2S.Parameters(pu1,pv1,pu2,pv2);
1027                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
1028                         vtx.SetParameters(pu1,pv1,pu2,pv2);
1029                         vtx.SetParameter(wline->NbPnts());
1030                         wline->AddVertex(vtx);
1031                       }
1032                       SLin.Append(wline);
1033                       empt = Standard_False;
1034                     }// imin<imax
1035                   }// !RejetLigne
1036                 }// PW.NbPoints()>2
1037               }// done is True
1038             }// dminiPointLigne > SeuildPointLigne
1039           }// Abs || Abs
1040         }// HasStartPoint
1041       } while(nbp>5 && NombreDePointsDeDepartDuCheminement<3);
1042     }// for( ls ...
1043   }// nbLigSec>=1
1044
1045   Standard_Integer z;
1046   for( z = 1; z <= nbTanZon; z++) { 
1047     const Intf_TangentZone& TangentZone=Interference.ZoneValue(z);
1048     for(Standard_Integer pz=1; pz<=TangentZone.NumberOfPoints(); pz++) { 
1049       SectionPointToParameters(TangentZone.GetPoint(pz),Poly1,Poly1,U1,V1,U2,V2);
1050
1051       StartParams(1) = U1;
1052       StartParams(2) = V1;
1053       StartParams(3) = U2;
1054       StartParams(4) = V2;
1055
1056       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);     
1057       if(HasStartPoint) { 
1058         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
1059         if(Abs(pu1-pu2)>1e-7 || Abs(pv1-pv2)>1e-7) { 
1060           NbLigCalculee = SLin.Length();
1061           dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
1062           Standard_Integer l;
1063           for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
1064             const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
1065             if( (testwline->IsOutSurf1Box(gp_Pnt2d(pu1,pv1))==Standard_False) &&
1066               (testwline->IsOutSurf2Box(gp_Pnt2d(pu2,pv2))==Standard_False) &&
1067               (testwline->IsOutBox(StartPOn2S.Value())==Standard_False) ) { 
1068                 NbPntOn2SOnLine = testwline->NbPnts();
1069                 Standard_Integer ll;
1070                 for( ll = 1; (ll < NbPntOn2SOnLine) && (dminiPointLigne >= SeuildPointLigne); ll++) { 
1071                   Standard_Real t,Au1,Av1,Au2,Av2,Bu1,Bv1,Bu2,Bv2;
1072                   testwline->Point(ll).Parameters(Au1,Av1,Au2,Av2);
1073                   testwline->Point(ll+1).Parameters(Bu1,Bv1,Bu2,Bv2);
1074                   if(Au1>Bu1) {
1075                     t=Au1;
1076                     Au1=Bu1;
1077                     Bu1=t;
1078                   } 
1079                   if(Av1>Bv1) {
1080                     t=Av1;
1081                     Av1=Bv1;
1082                     Bv1=t;
1083                   } 
1084                   Au1-=1.0e-7;
1085                   Av1-=1.0e-7;
1086                   Bu1+=1.0e-7;
1087                   Bv1+=1.0e-7;
1088                   if((pu1>=Au1) && (pu1<=Bu1) && (pv1>=Av1) && (pv1<=Bv1))
1089                     dminiPointLigne = 0.0; 
1090                   else { 
1091                     if((pu2>=Au1) && (pu2<=Bu1) && (pv2>=Av1) && (pv2<=Bv1))
1092                       dminiPointLigne = 0.0;
1093                   }
1094                 }// for( ll ...
1095             }// if ...
1096           }// for( l ...
1097
1098           if(dminiPointLigne > SeuildPointLigne) { 
1099             PW.Perform(StartParams);
1100             if(PW.IsDone()) {
1101               if(PW.NbPoints()>2) { 
1102                 RejetLigne = Standard_False;
1103                 Point3dDebut = PW.Value(1).Value();
1104                 Point3dFin   = PW.Value(PW.NbPoints()).Value();
1105                 for( ver = 1 ; ver<= NbLigCalculee ; ver++) { 
1106                   const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
1107                   const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
1108                   const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
1109                   if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
1110                       (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
1111                   {
1112                     RejetLigne = Standard_True; 
1113                     break;
1114                   }
1115                 }
1116
1117                 if(RejetLigne)
1118                 {
1119                   DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
1120                 }
1121
1122                 if(!RejetLigne) { 
1123                   IntSurf_TypeTrans trans1,trans2;
1124                   Standard_Real locu,locv;
1125                   gp_Vec norm1,norm2,d1u,d1v;
1126                   gp_Pnt ptbid;
1127                   Standard_Integer indextg;
1128                   gp_Vec tgline(PW.TangentAtLine(indextg));
1129                   PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1130                   Surf1->D1(locu,locv,ptbid,d1u,d1v);
1131                   norm1 = d1u.Crossed(d1v);
1132                   PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1133                   Surf1->D1(locu,locv,ptbid,d1u,d1v);
1134                   norm2 = d1u.Crossed(d1v);
1135                   if(tgline.DotCross(norm2,norm1)>0.) {
1136                     trans1 = IntSurf_Out;
1137                     trans2 = IntSurf_In;
1138                   }
1139                   else {
1140                     trans1 = IntSurf_In;
1141                     trans2 = IntSurf_Out;
1142                   }
1143
1144                   IntSurf_LineOn2S LineOn2S;
1145                   Standard_Integer nbp,imin,imax,i;
1146                   nbp = PW.Line()->NbPoints();
1147                   Standard_Real u1,v1,u2,v2;
1148                   i=0;
1149                   do { 
1150                     i++;
1151                     imin=i;
1152                     const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
1153                     Pi.Parameters(u1,v1,u2,v2);
1154                   } while((i<nbp)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
1155
1156                   if(imin>2)
1157                     imin--;
1158
1159                   i=nbp+1;
1160                   do { 
1161                     i--;
1162                     imax=i;
1163                     const IntSurf_PntOn2S& Pi   = PW.Line()->Value(i);
1164                     Pi.Parameters(u1,v1,u2,v2);
1165                   } while((i>2)&&(Abs(u1-u2)<=1e-6 && Abs(v1-v2)<=1e-6));
1166
1167                   if(imax<nbp)
1168                     imax++;
1169
1170                   if(imin<imax) { 
1171                     Handle(IntSurf_LineOn2S) PWLine = new IntSurf_LineOn2S();
1172                     for(i=imin;i<=imax;i++)
1173                       PWLine->Add(PW.Line()->Value(i));
1174
1175                     Standard_Real TolTang = TolTangency;
1176                     Handle(IntPatch_WLine) wline = new IntPatch_WLine(PWLine,Standard_False,trans1,trans2);
1177                     const IntSurf_PntOn2S& POn2SDeb = wline->Point(1);
1178                     const IntSurf_PntOn2S& POn2SFin = wline->Point(wline->NbPnts());
1179                     if((POn2SDeb.Value()).Distance(POn2SFin.Value()) <= TolTangency) { 
1180                       Standard_Real u1t,v1t,u2t,v2t; 
1181                       POn2SDeb.Parameters(u1t,v1t,u2t,v2t);
1182                       IntPatch_Point vtx;
1183                       vtx.SetValue(POn2SDeb.Value(),TolTang,Standard_False);
1184                       vtx.SetParameters(u2t,v2t,u1t,v1t);
1185                       vtx.SetParameter(wline->NbPnts());
1186                       wline->SetPoint(wline->NbPnts(),vtx);
1187                     }
1188
1189                     //the method PutVertexOnLine can reduce the number of points in <wline>
1190                     IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf1,Standard_True,TolTang);
1191                     if (wline->NbPnts() < 2)
1192                       continue;
1193
1194                     if(wline->NbVertex() == 0) {
1195                       IntPatch_Point vtx;
1196                       IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1197                       POn2S.Parameters(pu1,pv1,pu2,pv2);
1198                       vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1199                       vtx.SetParameters(pu1,pv1,pu2,pv2);
1200                       vtx.SetParameter(1);
1201                       wline->AddVertex(vtx);
1202
1203                       POn2S = PW.Line()->Value(wline->NbPnts());
1204                       POn2S.Parameters(pu1,pv1,pu2,pv2);
1205                       vtx.SetValue(Point3dFin,TolTang,Standard_False);
1206                       vtx.SetParameters(pu1,pv1,pu2,pv2);
1207                       vtx.SetParameter(wline->NbPnts());
1208                       wline->AddVertex(vtx);
1209                     }
1210
1211                     SLin.Append(wline);
1212                     empt = Standard_False;
1213                   }// imin<imax
1214                 }// !RejetLigne
1215               }// PW.NbPoints()>2
1216             }// done a True
1217           }// dminiPointLigne > SeuildPointLigne
1218         }// Abs || Abs
1219       }// HasStartPoint
1220     }// for ( pz ...
1221   }// for( z ...
1222 }
1223
1224 //==================================================================================
1225 // function : NewLine
1226 // purpose  : 
1227 //==================================================================================
1228 Handle(IntPatch_Line) IntPatch_PrmPrmIntersection::NewLine (const Handle(Adaptor3d_HSurface)&    Surf1,
1229                                                            const Handle(Adaptor3d_HSurface)&    Surf2,
1230                                                            const Standard_Integer NumLine,
1231                                                            const Standard_Integer Low,
1232                                                            const Standard_Integer High,
1233                                                            const Standard_Integer NbPntsToInsert) const 
1234
1235   Standard_Integer NbPnts = NbPntsToInsert + High - Low;
1236   if(NumLine>NbLines() || NumLine<1  || Low>=High ) 
1237     throw Standard_OutOfRange(" IntPatch_PrmPrmIntersection NewLine ");
1238   //------------------------------------------------------------------
1239   //--  Indice     :   Low       Low+1     I    I+1         High    --
1240   //--                                                              --
1241   //--  Abs.Curv.  :  S(Low)              S(I)  S(I+1)      S(High) --
1242   //--                                                              --
1243   //--                On echantillonne a abcisse curviligne         --
1244   //--                constante.                                    --
1245   //--                L abcisse est calculee sur les params U1,V1   --
1246   //------------------------------------------------------------------
1247   TColStd_Array1OfReal U1(Low,High);
1248   TColStd_Array1OfReal V1(Low,High);
1249   TColStd_Array1OfReal U2(Low,High);
1250   TColStd_Array1OfReal V2(Low,High);
1251   TColStd_Array1OfReal AC(Low,High);
1252
1253   Standard_Real s,ds;
1254   Handle(IntPatch_WLine) TheLine = Handle(IntPatch_WLine)::DownCast(Line(NumLine));
1255   const IntSurf_PntOn2S& Point=TheLine->Point(Low);
1256   Standard_Real u1,v1,u2,v2;
1257   Point.Parameters(u1,v1,u2,v2);
1258   U1(Low) = u1;
1259   V1(Low) = v1;
1260   U2(Low) = u2;
1261   V2(Low) = v2;
1262   AC(Low) =0.0;
1263
1264   IntWalk_PWalking PW(Surf1,Surf2,0.000001,0.000001,0.001,0.001);
1265
1266   Standard_Integer i;
1267   for(i=Low+1; i<=High; i++)
1268   {
1269     const IntSurf_PntOn2S& Pointi=TheLine->Point(i);
1270     Pointi.Parameters(u1,v1,u2,v2);
1271     U1(i) = u1;
1272     V1(i) = v1;
1273     U2(i) = u2;
1274     V2(i) = v2;
1275
1276     Standard_Real du1=u1-U1(i-1);
1277     Standard_Real dv1=v1-V1(i-1);
1278
1279     AC(i) = AC(i-1) + Sqrt((du1*du1)+(dv1*dv1));
1280   }
1281
1282   Handle(IntSurf_LineOn2S) ResultPntOn2SLine = new IntSurf_LineOn2S();
1283
1284   IntSurf_PntOn2S StartPOn2S;  
1285   TColStd_Array1OfReal StartParams(1,4);
1286
1287   ResultPntOn2SLine->Add(TheLine->Point(Low));
1288
1289   ds = AC(High) / (NbPnts-1);
1290   Standard_Integer Indice = Low;
1291
1292   Standard_Real dsmin = ds*0.3;
1293   Standard_Real smax  = AC(High);
1294
1295   for(i=2,s=ds; (i<NbPnts)&&(s<smax); i++,s+=ds)
1296   { 
1297     while(AC(Indice+1) <= s)
1298     { 
1299       ResultPntOn2SLine->Add(TheLine->Point(Indice));
1300       Indice++;
1301     }
1302     Standard_Real a = s - AC(Indice);
1303     Standard_Real b = AC(Indice+1) - s;
1304     Standard_Real nab = 1.0/(a+b);
1305     //----------------------------------------------------------
1306     //-- Verification :  Si Dist au prochain  point < dsmin   --
1307     //--                 Si Dist au precedent point < dsmin   --
1308     //--                                                      --
1309     //----------------------------------------------------------
1310     if((nab > ds)&&(a>dsmin)&&(b>dsmin))
1311     {
1312       StartParams(1) = (U1(Indice) * b   +  U1(Indice+1) * a) * nab;
1313       StartParams(2) = (V1(Indice) * b   +  V1(Indice+1) * a) * nab;
1314       StartParams(3) = (U2(Indice) * b   +  U2(Indice+1) * a) * nab;
1315       StartParams(4) = (V2(Indice) * b   +  V2(Indice+1) * a) * nab;
1316
1317       Standard_Boolean HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
1318       if(HasStartPoint)
1319         ResultPntOn2SLine->Add(StartPOn2S);
1320     }
1321     else
1322       s+=dsmin; 
1323   }
1324
1325   ResultPntOn2SLine->Add(TheLine->Point(High));
1326
1327   return(new IntPatch_WLine(ResultPntOn2SLine,Standard_False));
1328 }
1329
1330 //==================================================================================
1331 // function : SectionPointToParameters
1332 // purpose  : 
1333 //==================================================================================
1334 void SectionPointToParameters(const Intf_SectionPoint& Sp,
1335                               const IntPatch_Polyhedron& Poly1,
1336                               const IntPatch_Polyhedron& Poly2,
1337                               Standard_Real& u1,
1338                               Standard_Real& v1,
1339                               Standard_Real& u2,
1340                               Standard_Real& v2)
1341 {
1342   Intf_PIType       typ;
1343   Standard_Integer  Adr1,Adr2;
1344   Standard_Real     Param,u,v;
1345   gp_Pnt P(Sp.Pnt());
1346
1347   Standard_Integer Pt1,Pt2,Pt3;
1348
1349   Sp.InfoFirst(typ,Adr1,Adr2,Param);
1350   switch(typ) { 
1351   case Intf_VERTEX:   //-- Adr1 est le numero du vertex
1352     {
1353       Poly1.Parameters(Adr1,u1,v1);
1354       break;
1355     }
1356   case Intf_EDGE:
1357     {
1358       Poly1.Parameters(Adr1,u1,v1);    
1359       Poly1.Parameters(Adr2,u,v);
1360       u1+= Param * (u-u1);
1361       v1+= Param * (v-v1);
1362       break;
1363     }
1364   case Intf_FACE:
1365     {
1366       Standard_Real ua,va,ub,vb,uc,vc,ca,cb,cc,cabc;
1367       Poly1.Triangle(Adr1,Pt1,Pt2,Pt3);
1368       gp_Pnt PA(Poly1.Point(Pt1));
1369       gp_Pnt PB(Poly1.Point(Pt2));
1370       gp_Pnt PC(Poly1.Point(Pt3));
1371       Poly1.Parameters(Pt1,ua,va);
1372       Poly1.Parameters(Pt2,ub,vb);
1373       Poly1.Parameters(Pt3,uc,vc);
1374       gp_Vec Normale(gp_Vec(PA,PB).Crossed(gp_Vec(PA,PC)));
1375       cc = (gp_Vec(PA,PB).Crossed(gp_Vec(PA,P))).Dot(Normale);
1376       ca = (gp_Vec(PB,PC).Crossed(gp_Vec(PB,P))).Dot(Normale);
1377       cb = (gp_Vec(PC,PA).Crossed(gp_Vec(PC,P))).Dot(Normale);
1378       cabc = ca + cb + cc;
1379
1380       ca/=cabc;     cb/=cabc;       cc/=cabc;
1381
1382       u1 = ca * ua + cb * ub + cc * uc;
1383       v1 = ca * va + cb * vb + cc * vc;
1384       break;
1385     }
1386   default: 
1387     {
1388       //-- cout<<" Default dans SectionPointToParameters "<<endl;
1389       break;
1390     }
1391   }
1392
1393
1394   Sp.InfoSecond(typ,Adr1,Adr2,Param);
1395   switch(typ) { 
1396   case Intf_VERTEX:   //-- Adr1 est le numero du vertex
1397     {
1398       Poly2.Parameters(Adr1,u2,v2);
1399       break;
1400     }
1401   case Intf_EDGE:
1402     {
1403       Poly2.Parameters(Adr1,u2,v2);    
1404       Poly2.Parameters(Adr2,u,v);
1405       u2+= Param * (u-u2);
1406       v2+= Param * (v-v2);
1407       break;
1408     }
1409   case Intf_FACE:
1410     {
1411       Standard_Real ua,va,ub,vb,uc,vc,ca,cb,cc,cabc;
1412       Poly2.Triangle(Adr1,Pt1,Pt2,Pt3);
1413       gp_Pnt PA(Poly2.Point(Pt1));
1414       gp_Pnt PB(Poly2.Point(Pt2));
1415       gp_Pnt PC(Poly2.Point(Pt3));
1416       Poly2.Parameters(Pt1,ua,va);
1417       Poly2.Parameters(Pt2,ub,vb);
1418       Poly2.Parameters(Pt3,uc,vc);
1419       gp_Vec Normale(gp_Vec(PA,PB).Crossed(gp_Vec(PA,PC)));
1420       cc = (gp_Vec(PA,PB).Crossed(gp_Vec(PA,P))).Dot(Normale);
1421       ca = (gp_Vec(PB,PC).Crossed(gp_Vec(PB,P))).Dot(Normale);
1422       cb = (gp_Vec(PC,PA).Crossed(gp_Vec(PC,P))).Dot(Normale);
1423       cabc = ca + cb + cc;
1424
1425       ca/=cabc;     cb/=cabc;       cc/=cabc;
1426
1427       u2 = ca * ua + cb * ub + cc * uc;
1428       v2 = ca * va + cb * vb + cc * vc;
1429       break;
1430     }
1431   default: 
1432     {
1433       //-- cout<<" Default dans SectionPointToParameters "<<endl;
1434       break;
1435     }
1436   }
1437
1438
1439 //==================================================================================
1440 // function : RemplitLin
1441 // purpose  : 
1442 //==================================================================================
1443 void IntPatch_PrmPrmIntersection::RemplitLin(const Standard_Integer x1,
1444                                              const Standard_Integer y1,
1445                                              const Standard_Integer z1,
1446                                              const Standard_Integer x2,
1447                                              const Standard_Integer y2,
1448                                              const Standard_Integer z2,
1449                                              IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1450 {
1451   int xg,yg,zg;
1452   xg=x1-x2; if(xg<0) xg=-xg; 
1453   yg=y1-y2; if(yg<0) yg=-yg; 
1454   zg=z1-z2; if(zg<0) zg=-zg; 
1455   if(DansGrille(x1) && DansGrille(y1) && DansGrille(z1)) { 
1456     Standard_Integer t = GrilleInteger(x1,y1,z1); 
1457     Map.Add(t);
1458   }
1459   if(xg<=1 && yg<=1 && zg<=1) return;
1460   xg = (x1+x2)>>1;
1461   yg = (y1+y2)>>1;
1462   zg = (z1+z2)>>1;
1463   RemplitLin(x1,y1,z1,xg,yg,zg,Map);
1464   RemplitLin(x2,y2,z2,xg,yg,zg,Map);
1465 }
1466
1467 //==================================================================================
1468 // function : RemplitTri
1469 // purpose  : 
1470 //==================================================================================
1471 void IntPatch_PrmPrmIntersection::RemplitTri(const Standard_Integer x1,
1472                                              const Standard_Integer y1,
1473                                              const Standard_Integer z1,
1474                                              const Standard_Integer x2,
1475                                              const Standard_Integer y2,
1476                                              const Standard_Integer z2,
1477                                              const Standard_Integer x3,
1478                                              const Standard_Integer y3,
1479                                              const Standard_Integer z3,
1480                                              IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1481
1482   if(x1==x2 && x1==x3 && y1==y2 && y1==y3 && z1==z2 && z1==z3) {
1483     if(DansGrille(x1) && DansGrille(y1) && DansGrille(z1)) { 
1484       Standard_Integer t = GrilleInteger(x1,y1,z1); 
1485       Map.Add(t);
1486     }
1487     return;
1488   }
1489   else { 
1490     Standard_Integer xg=(x1+x2+x3)/3;
1491     Standard_Integer yg=(y1+y2+y3)/3;
1492     Standard_Integer zg=(z1+z2+z3)/3;
1493     if(xg==x1 && yg==y1 && zg==z1) { 
1494       RemplitLin(x1,y1,z1,x2,y2,z2,Map);
1495       RemplitLin(x1,y1,z1,x3,y3,z3,Map);
1496       return;
1497     }
1498     if(xg==x2 && yg==y2 && zg==z2) { 
1499       RemplitLin(x2,y2,z2,x1,y1,z1,Map);
1500       RemplitLin(x2,y2,z2,x3,y3,z3,Map);
1501       return;
1502     }
1503     if(xg==x3 && yg==y3 && zg==z3) { 
1504       RemplitLin(x3,y3,z3,x2,y2,z2,Map);
1505       RemplitLin(x3,y3,z3,x1,y1,z1,Map);
1506       return;
1507     }
1508     if(DansGrille(xg) && DansGrille(yg) && DansGrille(zg)) {
1509       Standard_Integer t = GrilleInteger(xg,yg,zg); 
1510       Map.Add(t);
1511     }
1512     if(xg!=x3 || yg!=y3 || zg!=z3) RemplitTri(x1,y1,z1, x2,y2,z2, xg,yg,zg, Map);
1513     if(xg!=x1 || yg!=y1 || zg!=z1) RemplitTri(xg,yg,zg, x2,y2,z2, x3,y3,z3, Map);
1514     if(xg!=x2 || yg!=y2 || zg!=z2) RemplitTri(x1,y1,z1, xg,yg,zg, x3,y3,z3, Map);
1515   }
1516 }
1517
1518 //==================================================================================
1519 // function : Remplit
1520 // purpose  : 
1521 //==================================================================================
1522 void IntPatch_PrmPrmIntersection::Remplit(const Standard_Integer a,
1523                                           const Standard_Integer b,
1524                                           const Standard_Integer c,
1525                                           IntPatch_PrmPrmIntersection_T3Bits& Map) const 
1526
1527   int iax,iay,iaz,ibx,iby,ibz,icx,icy,icz;
1528   if(a!=-1) Map.Add(a);
1529   if(b!=-1) Map.Add(b);
1530   if(c!=-1) Map.Add(c);
1531
1532   if(a!=-1 && b!=-1 && c!=-1 ) { 
1533     IntegerGrille(a,iax,iay,iaz);
1534     IntegerGrille(b,ibx,iby,ibz);
1535     IntegerGrille(c,icx,icy,icz);
1536     RemplitTri(iax,iay,iaz,ibx,iby,ibz,icx,icy,icz,Map);
1537   }
1538 }
1539
1540
1541
1542 //=======================================================================
1543 //function : Perform
1544 //purpose  : 
1545 //=======================================================================
1546 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)&    Surf1,
1547                                            const Handle(Adaptor3d_TopolTool)& D1,
1548                                            const Handle(Adaptor3d_HSurface)&    Surf2,
1549                                            const Handle(Adaptor3d_TopolTool)& D2,
1550                                            const Standard_Real   TolTangency,
1551                                            const Standard_Real   Epsilon,
1552                                            const Standard_Real   Deflection,
1553                                            const Standard_Real   Increment,
1554                                            IntSurf_ListOfPntOn2S& LOfPnts,
1555                                            const Standard_Boolean RestrictLine)
1556 {
1557   if (LOfPnts.IsEmpty()){
1558     done = Standard_True;
1559     return;
1560   }
1561
1562   empt = Standard_True;
1563   SLin.Clear();  
1564
1565   Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
1566   Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
1567   Standard_Real U1,U2,V1,V2;
1568
1569   UminLig1 = Surf1->FirstUParameter();
1570   VminLig1 = Surf1->FirstVParameter();
1571   UmaxLig1 = Surf1->LastUParameter();
1572   VmaxLig1 = Surf1->LastVParameter();
1573   UminLig2 = Surf2->FirstUParameter();
1574   VminLig2 = Surf2->FirstVParameter();
1575   UmaxLig2 = Surf2->LastUParameter();
1576   VmaxLig2 = Surf2->LastVParameter();
1577
1578   Standard_Real Periods [4];
1579   Periods[0] = (Surf1->IsUPeriodic())? Surf1->UPeriod() : 0.;
1580   Periods[1] = (Surf1->IsVPeriodic())? Surf1->VPeriod() : 0.;
1581   Periods[2] = (Surf2->IsUPeriodic())? Surf2->UPeriod() : 0.;
1582   Periods[3] = (Surf2->IsVPeriodic())? Surf2->VPeriod() : 0.;
1583
1584   IntSurf_ListIteratorOfListOfPntOn2S IterLOP1(LOfPnts);
1585   if (Surf1->IsUClosed() || Surf1->IsVClosed() ||
1586       Surf2->IsUClosed() || Surf2->IsVClosed())
1587   {
1588     Standard_Real TolPar = Precision::PConfusion();
1589     IntSurf_ListOfPntOn2S AdditionalPnts;
1590     Standard_Real NewU1, NewV1, NewU2, NewV2;
1591     for(; IterLOP1.More(); IterLOP1.Next())
1592     {
1593       IntSurf_PntOn2S Pnt = IterLOP1.Value();
1594       Pnt.Parameters(U1, V1, U2, V2);
1595       IntSurf_PntOn2S NewPnt;
1596       if (Surf1->IsUClosed())
1597       {
1598         if (Abs(U1 - Surf1->FirstUParameter()) <= TolPar)
1599         {
1600           NewU1 = Surf1->LastUParameter();
1601           NewPnt.SetValue( NewU1, V1, U2, V2 );
1602           AdditionalPnts.Append(NewPnt);
1603         }
1604         else if (Abs(U1 - Surf1->LastUParameter()) <= TolPar)
1605         {
1606           NewU1 = Surf1->FirstUParameter();
1607           NewPnt.SetValue( NewU1, V1, U2, V2 );
1608           AdditionalPnts.Append(NewPnt);
1609         }
1610       }
1611       if (Surf1->IsVClosed())
1612       {
1613         if (Abs(V1 - Surf1->FirstVParameter()) <= TolPar)
1614         {
1615           NewV1 = Surf1->LastVParameter();
1616           NewPnt.SetValue( U1, NewV1, U2, V2 );
1617           AdditionalPnts.Append(NewPnt);
1618         }
1619         else if (Abs(V1 - Surf1->LastVParameter()) <= TolPar)
1620         {
1621           NewV1 = Surf1->FirstVParameter();
1622           NewPnt.SetValue( U1, NewV1, U2, V2 );
1623           AdditionalPnts.Append(NewPnt);
1624         }
1625       }
1626       if (Surf2->IsUClosed())
1627       {
1628         if (Abs(U2 - Surf2->FirstUParameter()) <= TolPar)
1629         {
1630           NewU2 = Surf2->LastUParameter();
1631           NewPnt.SetValue( U1, V1, NewU2, V2);
1632           AdditionalPnts.Append(NewPnt);
1633         }
1634         else if (Abs(U2 - Surf2->LastUParameter()) <= TolPar)
1635         {
1636           NewU2 = Surf2->FirstUParameter();
1637           NewPnt.SetValue( U1, V1, NewU2, V2);
1638           AdditionalPnts.Append(NewPnt);
1639         }
1640       }
1641       if (Surf2->IsVClosed())
1642       {
1643         if (Abs(V2 - Surf2->FirstVParameter()) <= TolPar)
1644         {
1645           NewV2 = Surf2->LastVParameter();
1646           NewPnt.SetValue( U1, V1, U2, NewV2 );
1647           AdditionalPnts.Append(NewPnt);
1648         }
1649         else if (Abs(V2 - Surf2->LastVParameter()) <= TolPar)
1650         {
1651           NewV2 = Surf2->FirstVParameter();
1652           NewPnt.SetValue( U1, V1, U2, NewV2 );
1653           AdditionalPnts.Append(NewPnt);
1654         }
1655       }
1656     }
1657     //Cut repeated points
1658     for (IterLOP1.Initialize(LOfPnts); IterLOP1.More(); IterLOP1.Next())
1659     {
1660       IntSurf_PntOn2S aPnt = IterLOP1.Value();
1661       aPnt.Parameters(U1, V1, U2, V2);
1662       IntSurf_ListIteratorOfListOfPntOn2S iter2(AdditionalPnts);
1663       while (iter2.More())
1664       {
1665         IntSurf_PntOn2S aNewPnt = iter2.Value();
1666         aNewPnt.Parameters(NewU1, NewV1, NewU2, NewV2);
1667         if (Abs(U1 - NewU1) <= TolPar &&
1668             Abs(V1 - NewV1) <= TolPar &&
1669             Abs(U2 - NewU2) <= TolPar &&
1670             Abs(V2 - NewV2) <= TolPar)
1671           AdditionalPnts.Remove(iter2);
1672         else
1673           iter2.Next();
1674       }
1675     }
1676
1677     LOfPnts.Append(AdditionalPnts);
1678   }
1679
1680   for(IterLOP1.Initialize(LOfPnts); IterLOP1.More(); IterLOP1.Next()){
1681     IntSurf_PntOn2S Pnt = IterLOP1.Value();
1682     Pnt.Parameters(U1, V1, U2, V2);
1683     if(U1>UmaxLig1) UmaxLig1=U1;
1684     if(V1>VmaxLig1) VmaxLig1=V1;
1685     if(U2>UmaxLig2) UmaxLig2=U2;
1686     if(V2>VmaxLig2) VmaxLig2=V2;
1687
1688     if(U1<UminLig1) UminLig1=U1;
1689     if(V1<VminLig1) VminLig1=V1;
1690     if(U2<UminLig2) UminLig2=U2;
1691     if(V2<VminLig2) VminLig2=V2; 
1692   }
1693
1694   Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
1695
1696   Standard_Integer NbLigCalculee = 0, ver;
1697   Standard_Real pu1,pu2,pv1,pv2, dminiPointLigne;
1698   Standard_Boolean HasStartPoint,RejetLigne;
1699   IntSurf_PntOn2S StartPOn2S;
1700   gp_Pnt Point3dDebut,Point3dFin;
1701
1702   TColStd_Array1OfReal StartParams(1,4);
1703   IntWalk_PWalking PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);  
1704
1705   IntSurf_ListIteratorOfListOfPntOn2S IterLOP2(LOfPnts);
1706   for(; IterLOP2.More(); IterLOP2.Next() ){
1707
1708     IntSurf_PntOn2S cPnt = IterLOP2.Value();
1709     cPnt.Parameters(U1, V1, U2, V2);
1710
1711     StartParams(1) = U1;
1712     StartParams(2) = V1;
1713     StartParams(3) = U2;
1714     StartParams(4) = V2;
1715
1716     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
1717     dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
1718     if(HasStartPoint) {
1719       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
1720       NbLigCalculee = SLin.Length();
1721       Standard_Integer l;
1722       for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++) { 
1723         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
1724
1725         if (IsPointOnLine(StartPOn2S, testwline, Deflection)) {
1726           dminiPointLigne = 0.0;
1727         }
1728       }// for( l ...
1729
1730       if(dminiPointLigne > SeuildPointLigne) {
1731         PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
1732         if(PW.IsDone()) {
1733           if(PW.NbPoints()>2)
1734           {
1735             //Try to extend the intersection line to boundary, if it is possible.
1736             PW.PutToBoundary(Surf1, Surf2);
1737
1738             RejetLigne = Standard_False;
1739             Point3dDebut = PW.Value(1).Value();
1740             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
1741             Point3dFin   = PointFin.Value();
1742             for( ver = 1 ; ver<= NbLigCalculee ; ver++) { 
1743               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
1744
1745               // Check end point if it is on existing line.
1746               // Start point is checked before.
1747               if (IsPointOnLine(PointFin, verwline, Deflection)) {
1748                 RejetLigne = Standard_True; 
1749                 break;
1750               }
1751
1752               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
1753               const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
1754               if( (Point3dDebut.Distance(verPointDebut.Value()) <= TolTangency) &&
1755                   (Point3dFin.Distance(verPointFin.Value()) <= TolTangency)) 
1756               {
1757                 RejetLigne = Standard_True; 
1758                 break;
1759               }
1760             }
1761
1762             if(RejetLigne)
1763             {
1764               DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
1765             }
1766
1767             if(!RejetLigne) {
1768               IntSurf_TypeTrans trans1,trans2;
1769               Standard_Real locu,locv;
1770               gp_Vec norm1,norm2,d1u,d1v;
1771               gp_Pnt ptbid;
1772               Standard_Integer indextg;
1773               gp_Vec tgline(PW.TangentAtLine(indextg));
1774               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1775               Surf1->D1(locu,locv,ptbid,d1u,d1v);
1776               norm1 = d1u.Crossed(d1v);
1777               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1778               Surf2->D1(locu,locv,ptbid,d1u,d1v);
1779               norm2 = d1u.Crossed(d1v);
1780               if( tgline.DotCross(norm2,norm1) >= 0. ) {
1781                 trans1 = IntSurf_Out;
1782                 trans2 = IntSurf_In;
1783               }
1784               else {
1785                 trans1 = IntSurf_In;
1786                 trans2 = IntSurf_Out;
1787               }
1788
1789               Standard_Real TolTang = TolTangency;
1790               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1791               if (RestrictLine){
1792                 //the method PutVertexOnLine can reduce the number of points in <wline>
1793                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1794                 if (wline->NbPnts() < 2)
1795                   continue;
1796                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1797                 if (wline->NbPnts() < 2)
1798                   continue;
1799               }
1800
1801               if(wline->NbVertex() == 0) {
1802                 IntPatch_Point vtx;
1803                 IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1804                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1805                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1806                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1807                 vtx.SetParameter(1);
1808                 wline->AddVertex(vtx);
1809
1810                 POn2S = PW.Line()->Value(wline->NbPnts());
1811                 POn2S.Parameters(pu1,pv1,pu2,pv2);
1812                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
1813                 vtx.SetParameters(pu1,pv1,pu2,pv2);
1814                 vtx.SetParameter(wline->NbPnts());
1815                 wline->AddVertex(vtx);
1816               }              
1817
1818               SeveralWlinesProcessing(Surf1, Surf2, SLin, Periods, trans1, trans2,
1819                                       TolTang, Max(PW.MaxStep(0), PW.MaxStep(1)),
1820                                       Max(PW.MaxStep(2), PW.MaxStep(3)), wline);
1821
1822               AddWLine(SLin, wline, Deflection);
1823               empt = Standard_False;
1824             }// !RejetLigne
1825           }// PW points > 2
1826         }// done is True
1827       }// dminiPointLigne > SeuildPointLigne
1828     }// HasStartPoint  
1829   }// for( IterLOP ...
1830   done = Standard_True;
1831   return;      
1832 }
1833 //=======================================================================
1834 //function : Perform
1835 //purpose  : 
1836 //=======================================================================
1837 void IntPatch_PrmPrmIntersection::Perform(const Handle(Adaptor3d_HSurface)&    Surf1,
1838                                           const Handle(Adaptor3d_TopolTool)& D1,
1839                                           const Handle(Adaptor3d_HSurface)&    Surf2,
1840                                           const Handle(Adaptor3d_TopolTool)& D2,
1841                                           const Standard_Real   U1Depart,
1842                                           const Standard_Real   V1Depart,
1843                                           const Standard_Real   U2Depart,
1844                                           const Standard_Real   V2Depart,
1845                                           const Standard_Real   TolTangency,
1846                                           const Standard_Real   Epsilon,
1847                                           const Standard_Real   Deflection,
1848                                           const Standard_Real   Increment)
1849 {
1850   //    Standard_Integer NbU1 = D1->NbSamplesU();
1851   //    Standard_Integer NbV1 = D1->NbSamplesV();
1852   //    Standard_Integer NbU2 = D2->NbSamplesU();
1853   //    Standard_Integer NbV2 = D2->NbSamplesV();
1854
1855   //-- Traitement des Lignes de sections
1856   empt = Standard_True;
1857   done = Standard_True;
1858   SLin.Clear();  
1859
1860   //------------------------------------------------------------
1861
1862   Standard_Real pu1,pu2,pv1,pv2;
1863
1864   TColStd_Array1OfReal StartParams(1,4);
1865
1866   //    Standard_Integer MaxOscill = NbU1;
1867   //    if(MaxOscill < NbU2) MaxOscill=NbU2;
1868   //    if(MaxOscill < NbV1) MaxOscill=NbV1;
1869   //    if(MaxOscill < NbV2) MaxOscill=NbV2;
1870
1871   //    Standard_Real nIncrement=Increment;
1872   //    if(MaxOscill>10) { 
1873   //  #ifdef OCCT_DEBUG
1874   //      cout<<"\n IntPatch_PrmPrmIntersection.gxx : Increment:"<<Increment<<" -> "<<Increment/(0.5*MaxOscill)<<endl;
1875   //  #endif
1876   //      nIncrement/=0.5*MaxOscill;
1877   //    }
1878
1879   IntWalk_PWalking PW(Surf1,Surf2,
1880     TolTangency,
1881     Epsilon,
1882     Deflection,
1883     Increment); //nIncrement);
1884
1885
1886   //Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
1887   //Standard_Real    incidence;
1888   //Standard_Real    dminiPointLigne;
1889
1890   Standard_Boolean HasStartPoint;//,RejetLigne;
1891
1892   IntSurf_PntOn2S StartPOn2S;
1893
1894   //Standard_Integer ver;
1895
1896   gp_Pnt Point3dDebut,Point3dFin;
1897
1898   //------------------------------------------------------------
1899
1900   StartParams(1) = U1Depart;
1901   StartParams(2) = V1Depart;
1902   StartParams(3) = U2Depart;
1903   StartParams(4) = V2Depart;
1904
1905   //-----------------------------------------------------------------------
1906   //-- Calcul du premier point de cheminement a partir du point approche --
1907   //-----------------------------------------------------------------------
1908   HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S); 
1909   if(HasStartPoint) { 
1910     //-------------------------------------------------
1911     //-- Un point a ete trouve                       --
1912     //-- On verifie qu il n appartient pas           --
1913     //--  a une ligne de cheminement deja calculee.  --
1914     //-------------------------------------------------
1915
1916     PW.Perform(StartParams);
1917     if(PW.IsDone()) {
1918       if(PW.NbPoints()>2) {
1919         Point3dDebut = PW.Value(1).Value();
1920         Point3dFin   = PW.Value(PW.NbPoints()).Value();
1921
1922         IntSurf_TypeTrans trans1,trans2;
1923         Standard_Real locu,locv;
1924         gp_Vec norm1,norm2,d1u,d1v;
1925         gp_Pnt ptbid;
1926         Standard_Integer indextg;
1927         gp_Vec tgline(PW.TangentAtLine(indextg));
1928         PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
1929         Surf1->D1(locu,locv,ptbid,d1u,d1v);
1930         norm1 = d1u.Crossed(d1v);
1931         PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
1932         Surf2->D1(locu,locv,ptbid,d1u,d1v);
1933         norm2 = d1u.Crossed(d1v);
1934         if (tgline.DotCross(norm2,norm1)>0.) {
1935           trans1 = IntSurf_Out;
1936           trans2 = IntSurf_In;
1937         }
1938         else {
1939           trans1 = IntSurf_In;
1940           trans2 = IntSurf_Out;
1941         }
1942
1943         Standard_Real TolTang = TolTangency;
1944         Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
1945         //the method PutVertexOnLine can reduce the number of points in <wline>
1946         IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
1947         if (wline->NbPnts() < 2)
1948           return;
1949         IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
1950         if (wline->NbPnts() < 2)
1951           return;
1952
1953         //---------------
1954         if(wline->NbVertex() == 0) {
1955           IntPatch_Point vtx;
1956           IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
1957           POn2S.Parameters(pu1,pv1,pu2,pv2);
1958           vtx.SetValue(Point3dDebut,TolTang,Standard_False);
1959           vtx.SetParameters(pu1,pv1,pu2,pv2);
1960           vtx.SetParameter(1);
1961           wline->AddVertex(vtx);
1962
1963           POn2S = PW.Line()->Value(wline->NbPnts());
1964           POn2S.Parameters(pu1,pv1,pu2,pv2);
1965           vtx.SetValue(Point3dFin,TolTang,Standard_False);
1966           vtx.SetParameters(pu1,pv1,pu2,pv2);
1967           vtx.SetParameter(wline->NbPnts());
1968           wline->AddVertex(vtx);
1969         }
1970
1971         //---------------
1972         SLin.Append(wline);
1973         empt = Standard_False;
1974       } //if(PW.NbPoints()>2)
1975     } //if(PW.IsDone())
1976   } //if(HasStartPoint)
1977 }
1978 //==================================================================================
1979 // function : AdjustOnPeriodic
1980 // purpose  : 
1981 //==================================================================================
1982 void AdjustOnPeriodic(const Handle(Adaptor3d_HSurface)& Surf1,
1983                       const Handle(Adaptor3d_HSurface)& Surf2,
1984                       IntPatch_SequenceOfLine& aSLin)
1985 {
1986   Standard_Boolean bIsPeriodic[4], bModified, bIsNull, bIsPeriod;
1987   Standard_Integer i, j, k, aNbLines, aNbPx, aIndx, aIndq;
1988   Standard_Real aPeriod[4], dPeriod[4], ux[4], uq[4], aEps, du;
1989   //
1990   aEps=Precision::Confusion();
1991   //
1992   for (k=0; k<4; ++k) {
1993     aPeriod[k]=0.;
1994   }
1995   //
1996   bIsPeriodic[0]=Surf1->IsUPeriodic();
1997   bIsPeriodic[1]=Surf1->IsVPeriodic();
1998   bIsPeriodic[2]=Surf2->IsUPeriodic();
1999   bIsPeriodic[3]=Surf2->IsVPeriodic();
2000   //
2001   if (bIsPeriodic[0]){
2002     aPeriod[0]=Surf1->UPeriod();
2003   }
2004   if (bIsPeriodic[1]){
2005     aPeriod[1]=Surf1->VPeriod();
2006   }
2007   if (bIsPeriodic[2]){
2008     aPeriod[2]=Surf2->UPeriod();
2009   }
2010   if (bIsPeriodic[3]){
2011     aPeriod[3]=Surf2->VPeriod();
2012   }
2013   //
2014   for (k=0; k<4; ++k) {
2015     dPeriod[k]=0.25*aPeriod[k];
2016   }
2017   //
2018   aNbLines=aSLin.Length();
2019   for (i=1; i<=aNbLines; ++i) {
2020     Handle(IntPatch_WLine) aIL=Handle(IntPatch_WLine)::DownCast(aSLin.Value(i));
2021     Handle(IntSurf_LineOn2S) aL=aIL->Curve();
2022
2023     aNbPx=aL->NbPoints();
2024     if (aNbPx<10) {
2025       continue;
2026     }
2027     //
2028     for (j=0; j<2; ++j) {
2029       bModified=Standard_False;
2030       aIndx=1;
2031       aIndq=2;
2032       if (j) {
2033         aIndx=aNbPx;
2034         aIndq=aNbPx-1;
2035       }
2036       //
2037       const IntSurf_PntOn2S& aPSx=aL->Value(aIndx);
2038       const IntSurf_PntOn2S& aPSq=aL->Value(aIndq);
2039       //
2040       aPSx.Parameters(ux[0], ux[1], ux[2], ux[3]);
2041       aPSq.Parameters(uq[0], uq[1], uq[2], uq[3]);
2042       //
2043       for (k=0; k<4; ++k) {
2044         bIsNull=Standard_False;
2045         bIsPeriod=Standard_False;
2046         //
2047         if (!bIsPeriodic[k]) {
2048           continue;
2049         }
2050         //
2051         if (fabs(ux[k])<aEps) {
2052           bModified=Standard_True;
2053           bIsNull=Standard_True;
2054         }
2055         //
2056         else if (fabs(ux[k]-aPeriod[k])<aEps) {
2057           bModified=Standard_True;
2058           bIsPeriod=Standard_True;
2059         }
2060         //
2061         if (bModified) {
2062           du=fabs(ux[k]-uq[k]);
2063           if (du > dPeriod[k]) {
2064             if(bIsNull){
2065               ux[k]=aPeriod[k];
2066             }
2067             if(bIsPeriod) {
2068               ux[k]=0.;
2069             }
2070           }
2071         }
2072       }//for (k=0; k<4; ++k) 
2073       if (bModified) {
2074         IntSurf_PntOn2S aPntOn2S;
2075         //
2076         aPntOn2S=aPSx;  
2077         aPntOn2S.SetValue(ux[0], ux[1], ux[2], ux[3]);
2078         aL->Value(aIndx, aPntOn2S);
2079       }
2080     }//for (j=0; j<1; ++j) {
2081   }//for (i=1; i<=aNbLines; ++i)
2082 }
2083
2084 //==================================================================================
2085 // function : MakeNewPoint
2086 // purpose  : 
2087 //==================================================================================
2088 IntSurf_PntOn2S MakeNewPoint(const IntSurf_PntOn2S& replacePnt,
2089                              const IntSurf_PntOn2S& oldPnt,
2090                              const Standard_Real* Periods)
2091 {
2092   IntSurf_PntOn2S NewPoint;
2093   NewPoint.SetValue(replacePnt.Value());
2094
2095   Standard_Real OldParams[4], NewParams[4];
2096   oldPnt.Parameters(OldParams[0], OldParams[1], OldParams[2], OldParams[3]);
2097   replacePnt.Parameters(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
2098
2099   Standard_Integer i;
2100   for (i = 0; i < 4; i++)
2101   {
2102     if (Periods[i] != 0.)
2103     {
2104       if (Abs(NewParams[i] - OldParams[i]) >= 0.5*Periods[i])
2105       {
2106         if (NewParams[i] < OldParams[i])
2107           NewParams[i] += Periods[i];
2108         else
2109           NewParams[i] -= Periods[i];
2110       }
2111     }
2112   }
2113
2114   NewPoint.SetValue(NewParams[0], NewParams[1], NewParams[2], NewParams[3]);
2115   return NewPoint;
2116 }
2117
2118 //==================================================================================
2119 // function : Perform
2120 // purpose  : base SS Int. function
2121 //==================================================================================
2122 void IntPatch_PrmPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
2123                                            const Handle(Adaptor3d_TopolTool)& D1,
2124                                            const Handle(Adaptor3d_HSurface)& Surf2,
2125                                            const Handle(Adaptor3d_TopolTool)& D2,
2126                                            const Standard_Real   TolTangency,
2127                                            const Standard_Real   Epsilon,
2128                                            const Standard_Real   Deflection,
2129                                            const Standard_Real   Increment,
2130                                            const Standard_Boolean ClearFlag) 
2131 {
2132   Standard_Integer Limit = 2500;
2133   Standard_Integer NbU1 = 10, NbV1 = 10, NbU2 = 10, NbV2 = 10;
2134   //
2135   D1->SamplePnts(Deflection, NbU1, NbV1);
2136   D2->SamplePnts(Deflection, NbU2, NbV2);
2137   //
2138   NbU1 = D1->NbSamplesU();
2139   NbV1 = D1->NbSamplesV();
2140   NbU2 = D2->NbSamplesU();
2141   NbV2 = D2->NbSamplesV();
2142
2143   TColStd_Array1OfReal anUpars1(1, NbU1), aVpars1(1, NbV1);
2144   TColStd_Array1OfReal anUpars2(1, NbU2), aVpars2(1, NbV2);
2145   //
2146   D1->UParameters(anUpars1); 
2147   D1->VParameters(aVpars1);
2148   D2->UParameters(anUpars2); 
2149   D2->VParameters(aVpars2);
2150
2151   Standard_Real Periods [4];
2152   Periods[0] = (Surf1->IsUPeriodic())? Surf1->UPeriod() : 0.;
2153   Periods[1] = (Surf1->IsVPeriodic())? Surf1->VPeriod() : 0.;
2154   Periods[2] = (Surf2->IsUPeriodic())? Surf2->UPeriod() : 0.;
2155   Periods[3] = (Surf2->IsVPeriodic())? Surf2->VPeriod() : 0.;
2156
2157   //---------------------------------------------
2158   if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit))
2159   {
2160     empt = Standard_True;
2161     if (ClearFlag)
2162     {
2163       SLin.Clear();
2164     }
2165     //
2166     IntPolyh_Intersection* pInterference = NULL;
2167
2168     if ( D1->IsUniformSampling() || D2->IsUniformSampling() )
2169     {
2170       pInterference = new IntPolyh_Intersection(Surf1,NbU1,NbV1,Surf2,NbU2,NbV2);
2171     }
2172     else
2173     {
2174       pInterference = new IntPolyh_Intersection(Surf1, anUpars1, aVpars1, 
2175         Surf2, anUpars2, aVpars2 );
2176     }
2177
2178     if ( !pInterference )
2179     {
2180       done = Standard_False;
2181       return;
2182     }
2183     //
2184     IntPolyh_Intersection& Interference = *pInterference;
2185     //
2186     done = Interference.IsDone();
2187     if( !done )
2188     {
2189       if (pInterference)
2190       {
2191         delete pInterference;
2192         pInterference = NULL;
2193       }
2194
2195       return;
2196     }
2197
2198     Standard_Integer nbLigSec = Interference.NbSectionLines();
2199     Standard_Integer nbTanZon = Interference.NbTangentZones();
2200     Standard_Real SeuildPointLigne = 15.0 * Increment * Increment;
2201
2202     Standard_Integer NbLigCalculee = 0, ver;
2203     Standard_Real pu1,pu2,pv1,pv2, incidence, dminiPointLigne;
2204     Standard_Boolean HasStartPoint = Standard_False, RejectLine = Standard_False;
2205     IntSurf_PntOn2S StartPOn2S;
2206     gp_Pnt Point3dDebut,Point3dFin;
2207
2208     TColStd_Array1OfReal StartParams(1,4);
2209     IntWalk_PWalking PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,Increment);
2210
2211     if(nbLigSec>=1)
2212     {
2213       Standard_Integer *TabL = new Standard_Integer [nbLigSec+1];
2214       for(Standard_Integer ls=1; ls<=nbLigSec; ++ls)
2215       {
2216         TabL[ls]=ls;
2217       }
2218       //----------------------------------------1.1
2219       {
2220         Standard_Boolean triok;
2221         Standard_Integer nb_A, nb_B, tyu;
2222         do
2223         {
2224           triok=Standard_True;
2225           for(Standard_Integer b = 2; b <= nbLigSec; ++b )
2226           {
2227             nb_B = Interference.NbPointsInLine(TabL[b]);
2228             nb_A = Interference.NbPointsInLine(TabL[b-1]);
2229
2230             if( nb_B > nb_A )
2231             {
2232               tyu=TabL[b];
2233               TabL[b]=TabL[b-1];
2234               TabL[b-1]=tyu;
2235               triok=Standard_False;
2236             }
2237           }
2238         }
2239         while(triok==Standard_False);
2240       }
2241
2242       //----------------------------------------
2243       // 1.2 For the line "ls" get 2D-bounds U,V for surfaces 1,2
2244       //
2245       for(Standard_Integer ls = 1; ls <= nbLigSec; ++ls)
2246       {
2247         Standard_Integer nbp = Interference.NbPointsInLine(TabL[ls]);
2248         if (!nbp)
2249         {
2250           continue;
2251         }
2252         //
2253         Standard_Integer *TabPtDep = new Standard_Integer [nbp+1];
2254         for(Standard_Integer ilig = 1; ilig <= nbp; ++ilig )
2255         {
2256           TabPtDep[ilig]=0;
2257         }
2258         //
2259         Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2260         Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2261         Standard_Real _x,_y,_z;
2262         //
2263         Interference.GetLinePoint(TabL[ls], 1, _x, _y, _z,
2264           UminLig1, VminLig1, UminLig2, VminLig2, 
2265           incidence);
2266
2267         UmaxLig1=UminLig1;
2268         VmaxLig1=VminLig1;
2269         UmaxLig2=UminLig2;
2270         VmaxLig2=VminLig2;
2271         //
2272         for(Standard_Integer ilig = 2; ilig <= nbp; ilig++ )
2273         {
2274           Standard_Real U1, U2, V1, V2;
2275           Interference.GetLinePoint(TabL[ls],ilig,_x,_y,_z,U1,V1,U2,V2,incidence);
2276           //
2277           if(U1>UmaxLig1) UmaxLig1=U1;
2278           if(V1>VmaxLig1) VmaxLig1=V1;
2279           if(U2>UmaxLig2) UmaxLig2=U2;
2280           if(V2>VmaxLig2) VmaxLig2=V2;
2281           //
2282           if(U1<UminLig1) UminLig1=U1;
2283           if(V1<VminLig1) VminLig1=V1;
2284           if(U2<UminLig2) UminLig2=U2;
2285           if(V2<VminLig2) VminLig2=V2;
2286         }//for( ilig = 2; ilig <= nbp; ilig++ ) { 
2287         //
2288         //----------------------------------------
2289         // 1.3
2290         Standard_Integer nbps2 = (nbp>3)? (nbp/2) :  1;
2291         Standard_Integer NombreDePointsDeDepartDuCheminement = 0;
2292         Standard_Boolean lignetrouvee=Standard_False;
2293         const Standard_Integer NbDePointsDeDepartDuChmLimit = 5;
2294         //
2295         do
2296         {
2297           NombreDePointsDeDepartDuCheminement++;
2298           switch (NombreDePointsDeDepartDuCheminement)
2299           {
2300           case 1:
2301             nbps2 = (nbp > 1) ? nbp/2 : 1;
2302             if(nbp<3) 
2303               NombreDePointsDeDepartDuCheminement = NbDePointsDeDepartDuChmLimit;
2304
2305             break;
2306           case 2:
2307             nbps2 = 1;
2308             break;
2309           case 3:
2310             nbps2 = nbp-1;
2311             break;
2312
2313           case 4:
2314             nbps2 = 3 * nbp / 4;
2315             break;
2316
2317           case 5:
2318             nbps2 = nbp / 4;
2319             break;
2320           default:
2321             nbps2 = NombreDePointsDeDepartDuCheminement-3;
2322             NombreDePointsDeDepartDuCheminement++;
2323           }
2324
2325           //
2326           if(TabPtDep[nbps2] == 0)
2327           {
2328             Standard_Real U1, U2, V1, V2;
2329
2330             TabPtDep[nbps2] = 1;
2331             Interference.GetLinePoint(TabL[ls],nbps2,_x,_y,_z,U1,V1,U2,V2,incidence);
2332
2333             StartParams(1) = U1;
2334             StartParams(2) = V1;
2335             StartParams(3) = U2;
2336             StartParams(4) = V2;
2337
2338             HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);
2339             dminiPointLigne = SeuildPointLigne + SeuildPointLigne;
2340             if(HasStartPoint)
2341             {
2342               StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2343               NbLigCalculee = SLin.Length();
2344               Standard_Integer l;
2345               for( l = 1; (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); l++)
2346               {
2347                 const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2348
2349                 if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2350                 {
2351                   dminiPointLigne = 0.0;
2352                 }
2353               }// for( l ...
2354
2355               if(dminiPointLigne > SeuildPointLigne)
2356               {
2357                 PW.Perform(StartParams, UminLig1, VminLig1, UminLig2, VminLig2,
2358                   UmaxLig1, VmaxLig1, UmaxLig2, VmaxLig2);
2359
2360                 //
2361                 Standard_Boolean bPWIsDone;
2362                 Standard_Real aD11, aD12, aD21, aD22, aDx;
2363                 //
2364                 bPWIsDone=PW.IsDone();
2365
2366                 if(bPWIsDone)
2367                 {
2368                   Standard_Boolean hasBeenAdded = Standard_False;
2369                   if(PW.NbPoints() > 2 )
2370                   {
2371                     //Try to extend the intersection line to the boundary,
2372                     //if it is possibly
2373                     PW.PutToBoundary(Surf1, Surf2);
2374
2375                     const Standard_Integer aMinNbPoints = 40;
2376                     if(PW.NbPoints() < aMinNbPoints)
2377                     {
2378                       hasBeenAdded = PW.SeekAdditionalPoints(Surf1, Surf2, aMinNbPoints);
2379                     }
2380                     
2381                     Standard_Integer iPWNbPoints = PW.NbPoints(), aNbPointsVer = 0;
2382                     RejectLine = Standard_False;
2383                     Point3dDebut = PW.Value(1).Value();
2384                     Point3dFin = PW.Value(iPWNbPoints).Value();
2385                     for( ver = 1; (!RejectLine) && (ver<= NbLigCalculee); ++ver)
2386                     {
2387                       const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2388                       //
2389                       aNbPointsVer=verwline->NbPnts();
2390                       if (aNbPointsVer<3)
2391                       {
2392                         continue;
2393                       }
2394                       //
2395                       const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2396                       const IntSurf_PntOn2S& verPointFin = verwline->Point(verwline->NbPnts());
2397                       //xf
2398                       const gp_Pnt& aP21=verPointDebut.Value();
2399                       const gp_Pnt& aP22=verPointFin.Value();
2400                       //
2401                       aD11=Point3dDebut.Distance(aP21);
2402                       aD12=Point3dDebut.Distance(aP22);
2403                       aD21=Point3dFin.Distance(aP21);
2404                       aD22=Point3dFin.Distance(aP22);
2405                       //
2406                       if((aD11<=TolTangency && aD22<=TolTangency) ||
2407                         (aD12<=TolTangency && aD21<=TolTangency))
2408                       {
2409                         Standard_Integer m, mx;
2410                         //
2411                         mx=aNbPointsVer/2;
2412                         if (aNbPointsVer%2)
2413                         {
2414                           ++mx; 
2415                         }
2416                         //
2417                         const gp_Pnt& aPx=verwline->Point(mx).Value();
2418                         for(m=1; m<iPWNbPoints; ++m)
2419                         {
2420                           const gp_Pnt& aP1=PW.Value(m).Value();
2421                           const gp_Pnt& aP2=PW.Value(m+1).Value();
2422                           gp_Vec aVec12(aP1, aP2);
2423                           if (aVec12.SquareMagnitude()<1.e-20)
2424                           {
2425                             continue;
2426                           }
2427
2428                           //
2429                           gp_Dir aDir12(aVec12);
2430                           gp_Lin aLin12(aP1, aDir12);
2431                           aDx=aLin12.Distance(aPx);
2432
2433                           //modified by NIZNHY-PKV Tue May 10 11:08:07 2011f
2434                           if (aDx<=2.*Epsilon)
2435                           {
2436                             //if (aDx<=TolTangency) {
2437                             //modified by NIZNHY-PKV Tue May 10 11:08:13 2011t
2438
2439                             RejectLine = Standard_True;
2440                             ver--;
2441                             break;
2442                           }
2443                         }//for(m=1; m<iPWNbPoints; ++m){
2444                       }//if((aD11<=TolTangency && aD22<=TolTangency) ||...
2445                     }// for( ver = 1 ; (!RejetLigne) && (ver<= NbLigCalculee) ; ver++) { 
2446                     //
2447
2448                     if(RejectLine)
2449                     {
2450                       DublicateOfLinesProcessing(PW, ver, SLin, RejectLine);
2451                     }
2452
2453                     if(!RejectLine)
2454                     {
2455                       IntSurf_TypeTrans trans1,trans2;
2456                       Standard_Real locu,locv;
2457                       gp_Vec norm1,norm2,d1u,d1v;
2458                       gp_Pnt ptbid;
2459                       Standard_Integer indextg;
2460                       gp_Vec tgline(PW.TangentAtLine(indextg));
2461                       PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2462                       Surf1->D1(locu,locv,ptbid,d1u,d1v);
2463                       norm1 = d1u.Crossed(d1v);
2464                       PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2465                       Surf2->D1(locu,locv,ptbid,d1u,d1v);
2466                       norm2 = d1u.Crossed(d1v);
2467                       if( tgline.DotCross(norm2,norm1) >= 0. )
2468                       {
2469                         trans1 = IntSurf_Out;
2470                         trans2 = IntSurf_In;
2471                       }
2472                       else
2473                       {
2474                         trans1 = IntSurf_In;
2475                         trans2 = IntSurf_Out;
2476                       }
2477
2478                       Standard_Real TolTang = TolTangency;
2479                       Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2480                       wline->EnablePurging(!hasBeenAdded);
2481                       //the method PutVertexOnLine can reduce the number of points in <wline>
2482                       IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2483                       if (wline->NbPnts() < 2)
2484                         continue;
2485                       IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2486                       if (wline->NbPnts() < 2)
2487                         continue;
2488                       if(wline->NbVertex() == 0)
2489                       {
2490                         IntPatch_Point vtx;
2491                         IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2492                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2493                         vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2494                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2495                         vtx.SetParameter(1);
2496                         wline->AddVertex(vtx);
2497
2498                         POn2S = PW.Line()->Value(wline->NbPnts());
2499                         POn2S.Parameters(pu1,pv1,pu2,pv2);
2500                         vtx.SetValue(Point3dFin,TolTang,Standard_False);
2501                         vtx.SetParameters(pu1,pv1,pu2,pv2);
2502                         vtx.SetParameter(wline->NbPnts());
2503                         wline->AddVertex(vtx);
2504                       }
2505
2506                       lignetrouvee = Standard_True;
2507
2508                       SeveralWlinesProcessing(Surf1, Surf2, SLin, Periods, trans1, trans2,
2509                                               TolTang, Max(PW.MaxStep(0), PW.MaxStep(1)),
2510                                               Max(PW.MaxStep(2), PW.MaxStep(3)), wline);
2511
2512                       AddWLine(SLin, wline, Deflection);
2513                       empt = Standard_False;
2514                     }// !RejetLigne
2515                   }// PW points > 2
2516                 }// done is True
2517               }// dminiPointLigne > SeuildPointLigne
2518             }// HasStartPoint
2519           }// if TabPtDep[nbps2] == 0
2520         } while(nbp>5 && !( (NombreDePointsDeDepartDuCheminement >= NbDePointsDeDepartDuChmLimit && lignetrouvee) || 
2521           (NombreDePointsDeDepartDuCheminement-3 >= nbp && (!lignetrouvee))));
2522         delete [] TabPtDep;
2523       }// for( ls ...
2524
2525       delete [] TabL;
2526
2527     }// if nbLigSec >= 1
2528     //
2529     AdjustOnPeriodic(Surf1, Surf2, SLin);
2530     //
2531
2532     //--------------------------------------------------------------------
2533     //-- Calcul des parametres approches a partir des Zones De Tangence --
2534     //--------------------------------------------------------------------
2535     Standard_Real UminLig1,VminLig1,UmaxLig1,VmaxLig1;
2536     Standard_Real UminLig2,VminLig2,UmaxLig2,VmaxLig2;
2537
2538     UminLig1=VminLig1=UminLig2=VminLig2=RealLast();
2539     UmaxLig1=VmaxLig1=UmaxLig2=VmaxLig2=-UminLig1;
2540
2541     // NbPointsInTangentZone always == 1 (eap)
2542
2543     Standard_Integer z;
2544     for( z=1; z <= nbTanZon; z++)
2545     { 
2546       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2547       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) {
2548       Standard_Integer pz=1;
2549       Standard_Real _x,_y,_z;
2550       Standard_Real U1, U2, V1, V2;
2551       Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2552
2553       if(U1>UmaxLig1) UmaxLig1=U1;
2554       if(V1>VmaxLig1) VmaxLig1=V1;
2555       if(U2>UmaxLig2) UmaxLig2=U2;
2556       if(V2>VmaxLig2) VmaxLig2=V2;
2557
2558       if(U1<UminLig1) UminLig1=U1;
2559       if(V1<VminLig1) VminLig1=V1;
2560       if(U2<UminLig2) UminLig2=U2;
2561       if(V2<VminLig2) VminLig2=V2;
2562       //}
2563     }
2564
2565     for(z=1; z <= nbTanZon; z++)
2566     {
2567       //Standard_Integer NbPointsInTangentZone=Interference.NbPointsInTangentZone(z);
2568       //for(Standard_Integer pz=1; pz<=NbPointsInTangentZone; pz++) { 
2569       Standard_Integer pz=1;
2570       Standard_Real _x,_y,_z;
2571       Standard_Real U1, U2, V1, V2;
2572       Interference.GetTangentZonePoint(z,pz,_x,_y,_z,U1,V1,U2,V2);
2573
2574       StartParams(1) = U1;
2575       StartParams(2) = V1;
2576       StartParams(3) = U2;
2577       StartParams(4) = V2;
2578
2579       //-----------------------------------------------------------------------
2580       //-- Calcul du premier point de cheminement a partir du point approche --
2581       //-----------------------------------------------------------------------
2582       HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);     
2583       if(HasStartPoint)
2584       {
2585         //-------------------------------------------------
2586         //-- Un point a ete trouve                       --
2587         //-- On verifie qu il n appartient pas           --
2588         //--  a une ligne de cheminement deja calculee.  --
2589         //-------------------------------------------------
2590         StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2591
2592         NbLigCalculee = SLin.Length();
2593         dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2594
2595         for(Standard_Integer l=1; 
2596           (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2597           l++)
2598         {
2599           const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2600
2601           if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2602           {
2603             dminiPointLigne = 0.0;
2604           }
2605         }
2606
2607         //-- Fin d exploration des lignes
2608         if(dminiPointLigne > SeuildPointLigne)
2609         {
2610           //---------------------------------------------------
2611           //-- Le point de depart du nouveau cheminement     --
2612           //-- n est present dans aucune ligne deja calculee.--
2613           //---------------------------------------------------
2614           PW.Perform(StartParams,UminLig1,VminLig1,UminLig2,VminLig2,
2615             UmaxLig1,VmaxLig1,UmaxLig2,VmaxLig2);
2616
2617           if(PW.IsDone())
2618           {
2619             Standard_Boolean hasBeenAdded = Standard_False;
2620             if(PW.NbPoints()>2)
2621             { 
2622               const Standard_Integer aMinNbPoints = 40;
2623               if(PW.NbPoints() < aMinNbPoints)
2624               {
2625                 hasBeenAdded = PW.SeekAdditionalPoints(Surf1, Surf2, aMinNbPoints);
2626               }
2627
2628               //-----------------------------------------------
2629               //-- Verification a posteriori : 
2630               //-- On teste si le point de depart et de fin de 
2631               //-- la ligne de cheminement est present dans une 
2632               //-- autre ligne . 
2633               //-----------------------------------------------
2634               RejectLine = Standard_False;
2635               Point3dDebut = PW.Value(1).Value();
2636               const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2637               Point3dFin   = PointFin.Value();
2638
2639               for(ver=1 ; (!RejectLine) && (ver<= NbLigCalculee) ; ver++)
2640               {
2641                 const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2642                 //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2643
2644                 // Check end point if it is on existing line.
2645                 // Start point is checked before.
2646                 if (IsPointOnLine(PointFin, verwline, Deflection))
2647                 {
2648                   RejectLine = Standard_True; 
2649                   break;
2650                 }
2651
2652                 const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2653                 const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2654                 if(Point3dDebut.Distance(verPointDebut.Value()) < TolTangency)
2655                 {
2656                   RejectLine = Standard_True; 
2657                 }
2658                 else
2659                 {
2660                   if(Point3dFin.Distance(verPointFin.Value()) < TolTangency)
2661                   {
2662                     RejectLine = Standard_True; 
2663                   }
2664                 }
2665               }
2666
2667               if(!RejectLine)
2668               { 
2669                 IntSurf_TypeTrans trans1,trans2;
2670                 Standard_Real locu,locv;
2671                 gp_Vec norm1,norm2,d1u,d1v;
2672                 gp_Pnt ptbid;
2673                 Standard_Integer indextg;
2674                 gp_Vec tgline(PW.TangentAtLine(indextg));
2675                 PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2676                 Surf1->D1(locu,locv,ptbid,d1u,d1v);
2677                 norm1 = d1u.Crossed(d1v);
2678                 PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2679                 Surf2->D1(locu,locv,ptbid,d1u,d1v);
2680                 norm2 = d1u.Crossed(d1v);
2681                 if (tgline.DotCross(norm2,norm1)>0.)
2682                 {
2683                   trans1 = IntSurf_Out;
2684                   trans2 = IntSurf_In;
2685                 }
2686                 else
2687                 {
2688                   trans1 = IntSurf_In;
2689                   trans2 = IntSurf_Out;
2690                 }
2691
2692                 Standard_Real TolTang = TolTangency;
2693                 Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2694                 wline->EnablePurging(!hasBeenAdded);
2695                 //the method PutVertexOnLine can reduce the number of points in <wline>
2696                 IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2697                 if (wline->NbPnts() < 2)
2698                   continue;
2699                 IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2700                 if (wline->NbPnts() < 2)
2701                   continue;
2702
2703                 //---------------
2704                 if(wline->NbVertex() == 0)
2705                 {
2706                   IntPatch_Point vtx;
2707                   IntSurf_PntOn2S POn2S = PW.Line()->Value(1);
2708                   POn2S.Parameters(pu1,pv1,pu2,pv2);
2709                   vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2710                   vtx.SetParameters(pu1,pv1,pu2,pv2);
2711                   vtx.SetParameter(1);
2712                   wline->AddVertex(vtx);
2713
2714                   POn2S = PW.Line()->Value(wline->NbPnts());
2715                   POn2S.Parameters(pu1,pv1,pu2,pv2);
2716                   vtx.SetValue(Point3dFin,TolTang,Standard_False);
2717                   vtx.SetParameters(pu1,pv1,pu2,pv2);
2718                   vtx.SetParameter(wline->NbPnts());
2719                   wline->AddVertex(vtx);
2720                 }
2721
2722                 //---------------
2723                 AddWLine(SLin, wline, Deflection);
2724                 empt = Standard_False;
2725               }
2726               else
2727               {
2728                 //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2729               }
2730               //------------------------------------------------------------            
2731             }
2732           }  //--  le cheminement a reussi (done a True)
2733         }  //--  le point approche ne renvoie pas sur une ligne existante
2734       } //-- Si HasStartPoint
2735       //} //-- Boucle Sur les Points de la Tangent Zone
2736     } //-- Boucle sur Les Tangent Zones
2737
2738     if ( pInterference )
2739     {
2740       delete pInterference;
2741       pInterference = NULL;
2742     }
2743
2744     return;
2745   }// if((NbU1*NbV1<=Limit && NbV2*NbU2<=Limit)) {  
2746
2747   Handle(IntSurf_LineOn2S) LOn2S = new IntSurf_LineOn2S();
2748   PointDepart( LOn2S, Surf1, NbU1, NbV1, Surf2, NbU2, NbV2 );
2749   empt = Standard_True;
2750   done = Standard_True;
2751
2752   Standard_Integer NbLigCalculee = 0;
2753   Standard_Real U1,U2,V1,V2;
2754   Standard_Real pu1,pu2,pv1,pv2;
2755
2756   TColStd_Array1OfReal StartParams(1,4);
2757   Standard_Integer MaxOscill = NbU1;
2758   if(MaxOscill < NbU2) MaxOscill=NbU2;
2759   if(MaxOscill < NbV1) MaxOscill=NbV1;
2760   if(MaxOscill < NbV2) MaxOscill=NbV2;
2761
2762   Standard_Real nIncrement=Increment;
2763   //if(MaxOscill>10)
2764   //nIncrement/=0.5*MaxOscill;
2765
2766   IntWalk_PWalking PW(Surf1,Surf2,TolTangency,Epsilon,Deflection,nIncrement);
2767   Standard_Real    SeuildPointLigne = 15.0 * Increment * Increment; //-- 10 est insuffisant
2768   Standard_Real    dminiPointLigne;
2769   Standard_Boolean HasStartPoint,RejetLigne;
2770   IntSurf_PntOn2S StartPOn2S;
2771   Standard_Integer ver;
2772   gp_Pnt Point3dDebut,Point3dFin;
2773
2774   //------------------------------------------------------------
2775   //-- Calcul des parametres approches a partir des Zones De Tangence --
2776   //--------------------------------------------------------------------
2777   Standard_Integer nbTanZon = LOn2S->NbPoints();
2778   for(Standard_Integer z=1; z <= nbTanZon; z++) { 
2779     const IntSurf_PntOn2S& POn2S = LOn2S->Value(z);
2780     POn2S.Parameters(U1,V1,U2,V2);
2781     StartParams(1) = U1;
2782     StartParams(2) = V1;
2783     StartParams(3) = U2;
2784     StartParams(4) = V2;
2785
2786     //-----------------------------------------------------------------------
2787     //-- Calcul du premier point de cheminement a partir du point approche --
2788     //-----------------------------------------------------------------------
2789     HasStartPoint = PW.PerformFirstPoint(StartParams,StartPOn2S);       
2790     if(HasStartPoint)
2791     { 
2792       //-------------------------------------------------
2793       //-- Un point a ete trouve                       --
2794       //-- On verifie qu il n appartient pas           --
2795       //--  a une ligne de cheminement deja calculee.  --
2796       //-------------------------------------------------
2797       StartPOn2S.Parameters(pu1,pv1,pu2,pv2);
2798
2799       NbLigCalculee = SLin.Length();
2800       dminiPointLigne = SeuildPointLigne + SeuildPointLigne; 
2801
2802       for(Standard_Integer l=1; 
2803         (l <= NbLigCalculee) && (dminiPointLigne >= SeuildPointLigne); 
2804         l++)
2805       {
2806         const Handle(IntPatch_WLine)& testwline = *((Handle(IntPatch_WLine)*)&SLin.Value(l));
2807
2808         if (IsPointOnLine(StartPOn2S, testwline, Deflection))
2809         {
2810           dminiPointLigne = 0.0;
2811         }
2812       }
2813
2814       //-- Fin d exploration des lignes
2815       if(dminiPointLigne > SeuildPointLigne)
2816       { 
2817         //---------------------------------------------------
2818         //-- Le point de depart du nouveau cheminement     --
2819         //-- n est present dans aucune ligne deja calculee.--
2820         //---------------------------------------------------
2821         PW.Perform(StartParams);
2822         if(PW.IsDone())
2823         {
2824           if(PW.NbPoints()>2)
2825           {
2826             //-----------------------------------------------
2827             //-- Verification a posteriori : 
2828             //-- On teste si le point de depart et de fin de 
2829             //-- la ligne de cheminement est present dans une 
2830             //-- autre ligne . 
2831             //-----------------------------------------------
2832             RejetLigne = Standard_False;
2833             Point3dDebut = PW.Value(1).Value();
2834             const IntSurf_PntOn2S& PointFin = PW.Value(PW.NbPoints());
2835             Point3dFin   = PointFin.Value();
2836
2837             for(ver=1 ; ver<= NbLigCalculee ; ver++)
2838             {
2839               const Handle(IntPatch_WLine)& verwline = *((Handle(IntPatch_WLine)*)&SLin.Value(ver));
2840               //-- Handle(IntPatch_WLine) verwline=Handle(IntPatch_WLine)::DownCast(SLin.Value(ver));
2841
2842               // Check end point if it is on existing line.
2843               // Start point is checked before.
2844               if (IsPointOnLine(PointFin, verwline, Deflection))
2845               {
2846                 RejetLigne = Standard_True; 
2847                 break;
2848               }
2849
2850               const IntSurf_PntOn2S& verPointDebut = verwline->Point(1);
2851               const IntSurf_PntOn2S& verPointFin   = verwline->Point(verwline->NbPnts());
2852               if( (Point3dDebut.Distance(verPointDebut.Value()) < TolTangency) ||
2853                   (Point3dFin.Distance(verPointFin.Value()) < TolTangency))
2854               {
2855                 RejetLigne = Standard_True; 
2856                 break;
2857               }
2858             }
2859
2860             if(RejetLigne)
2861             {
2862               DublicateOfLinesProcessing(PW, ver, SLin, RejetLigne);
2863             }
2864
2865             if(!RejetLigne)
2866             {
2867               IntSurf_TypeTrans trans1,trans2;
2868               Standard_Real locu,locv;
2869               gp_Vec norm1,norm2,d1u,d1v;
2870               gp_Pnt ptbid;
2871               Standard_Integer indextg;
2872               gp_Vec tgline(PW.TangentAtLine(indextg));
2873               PW.Line()->Value(indextg).ParametersOnS1(locu,locv);
2874               Surf1->D1(locu,locv,ptbid,d1u,d1v);
2875               norm1 = d1u.Crossed(d1v);
2876               PW.Line()->Value(indextg).ParametersOnS2(locu,locv);
2877               Surf2->D1(locu,locv,ptbid,d1u,d1v);
2878               norm2 = d1u.Crossed(d1v);
2879               if (tgline.DotCross(norm2,norm1)>0.)
2880               {
2881                 trans1 = IntSurf_Out;
2882                 trans2 = IntSurf_In;
2883               }
2884               else {
2885                 trans1 = IntSurf_In;
2886                 trans2 = IntSurf_Out;
2887               }
2888
2889               Standard_Real TolTang = TolTangency;
2890               Handle(IntPatch_WLine) wline = new IntPatch_WLine(PW.Line(),Standard_False,trans1,trans2);
2891               //the method PutVertexOnLine can reduce the number of points in <wline>
2892               IntPatch_RstInt::PutVertexOnLine(wline,Surf1,D1,Surf2,Standard_True,TolTang);
2893               if (wline->NbPnts() < 2)
2894                 continue;
2895               IntPatch_RstInt::PutVertexOnLine(wline,Surf2,D2,Surf1,Standard_False,TolTang);
2896               if (wline->NbPnts() < 2)
2897                 continue;
2898
2899               //---------------
2900               if(wline->NbVertex() == 0)
2901               {
2902                 IntPatch_Point vtx;
2903                 const IntSurf_PntOn2S& POn2Sf = PW.Line()->Value(1);
2904                 POn2Sf.Parameters(pu1,pv1,pu2,pv2);
2905                 vtx.SetValue(Point3dDebut,TolTang,Standard_False);
2906                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2907                 vtx.SetParameter(1);
2908                 wline->AddVertex(vtx);
2909
2910                 const IntSurf_PntOn2S& POn2Sl = PW.Line()->Value(wline->NbPnts());
2911                 POn2Sl.Parameters(pu1,pv1,pu2,pv2);
2912                 vtx.SetValue(Point3dFin,TolTang,Standard_False);
2913                 vtx.SetParameters(pu1,pv1,pu2,pv2);
2914                 vtx.SetParameter(wline->NbPnts());
2915                 wline->AddVertex(vtx);
2916               }
2917
2918               //---------------
2919               AddWLine(SLin, wline, Deflection);
2920               empt = Standard_False;
2921             }
2922             else
2923             { 
2924               //-- cout<<" ----- REJET DE LIGNE (POINT DE DEPART) ----- "<<endl;
2925             }
2926             //------------------------------------------------------------              
2927           }
2928         }  //--  le cheminement a reussi (done a True)
2929       }  //--  le point approche ne renvoie pas sur une ligne existante
2930     } //-- Si HasStartPoint
2931   } //-- Boucle sur Les Tangent Zones
2932 }
2933 //modified by NIZNHY-PKV Wed May 25 09:39:07 2011f
2934 //=======================================================================
2935 //class : IntPatch_InfoPD
2936 //purpose  : 
2937 //=======================================================================
2938 class IntPatch_InfoPD {
2939 public:
2940   //----------------------------------------C-tor
2941   IntPatch_InfoPD(const Standard_Integer aNBI) {
2942     Standard_Integer aNBI2, i, j;
2943     myNBI=aNBI;
2944     //
2945     aNBI2=aNBI*aNBI;
2946     myP1DS2=new char[aNBI2];
2947     myP2DS1=new char[aNBI2];
2948     myIP1=new Standard_Integer[aNBI2]; 
2949     myIP2=new Standard_Integer[aNBI2];
2950     myP1=new gp_Pnt[aNBI2];
2951     myP2=new gp_Pnt[aNBI2];
2952     //
2953     for (i=0; i<myNBI; ++i) {
2954       for (j=0; j<myNBI; ++j) {
2955         xP1DS2(i, j)=0;
2956         xP2DS1(i, j)=0;
2957         xIP1(i, j)=0;
2958         xIP2(i, j)=0;
2959         xP1(i, j).SetCoord(0., 0., 0.);
2960         xP2(i, j).SetCoord(0., 0., 0.);
2961       }
2962     }
2963   };
2964   //---------------------------------------- D-tor
2965   ~IntPatch_InfoPD() {
2966     delete [] (char*) myP1DS2;
2967     delete [] (char*) myP2DS1;
2968     delete [] (Standard_Integer*) myIP1;
2969     delete [] (Standard_Integer*) myIP2; 
2970     delete [] (gp_Pnt*)myP1;
2971     delete [] (gp_Pnt*)myP2;
2972   };
2973   //---------------------------------------- Index
2974   Standard_Integer Index(const Standard_Integer i,
2975     const Standard_Integer j) const { 
2976       return i*myNBI+j;
2977   };
2978   //---------------------------------------- NBI
2979   Standard_Integer NBI() const { 
2980     return myNBI;
2981   };
2982   //----------------------------------------xP1DS2
2983   char& xP1DS2(const Standard_Integer i,
2984     const Standard_Integer j) { 
2985       return myP1DS2[Index(i,j)];
2986   };
2987   //----------------------------------------xP2DS1
2988   char& xP2DS1(const Standard_Integer i,
2989     const Standard_Integer j) { 
2990       return myP2DS1[Index(i,j)];
2991   };
2992   //----------------------------------------xIP1
2993   Standard_Integer& xIP1(const Standard_Integer i,
2994     const Standard_Integer j) { 
2995       return myIP1[Index(i,j)];
2996   };
2997   //----------------------------------------xIP2
2998   Standard_Integer& xIP2(const Standard_Integer i,
2999     const Standard_Integer j) { 
3000       return myIP2[Index(i,j)];
3001   };
3002   //----------------------------------------xP1
3003   gp_Pnt& xP1(const Standard_Integer i,
3004     const Standard_Integer j) { 
3005       return myP1[Index(i,j)];
3006   };
3007   //----------------------------------------xP1
3008   gp_Pnt& xP2(const Standard_Integer i,
3009     const Standard_Integer j) { 
3010       return myP2[Index(i,j)];
3011   };
3012
3013 private:
3014
3015   IntPatch_InfoPD (const IntPatch_InfoPD&);
3016   IntPatch_InfoPD& operator=(const IntPatch_InfoPD&);
3017
3018 private:
3019
3020   Standard_Integer myNBI;
3021   char *myP1DS2;
3022   char *myP2DS1;
3023   Standard_Integer *myIP1;
3024   Standard_Integer *myIP2;
3025   gp_Pnt *myP1;
3026   gp_Pnt *myP2;
3027 }; 
3028 //modified by NIZNHY-PKV Tue May 24 11:38:55 2011t
3029 //==================================================================================
3030 // function : PointDepart
3031 // purpose  : 
3032 //==================================================================================
3033 void IntPatch_PrmPrmIntersection::PointDepart(Handle(IntSurf_LineOn2S)& LineOn2S,
3034                                               const Handle(Adaptor3d_HSurface)& S1,
3035                                               const Standard_Integer SU_1,
3036                                               const Standard_Integer SV_1,
3037                                               const Handle(Adaptor3d_HSurface)& S2,
3038                                               const Standard_Integer SU_2,
3039                                               const Standard_Integer SV_2) const 
3040
3041   Standard_Integer i, j, xNBI;
3042   //modified by NIZNHY-PKV Tue May 24 11:37:38 2011f
3043   xNBI=200;
3044   IntPatch_InfoPD aIPD(xNBI);
3045   //modified by NIZNHY-PKV Wed May 25 06:47:12 2011t
3046   Standard_Integer iC15, SU1, SV1, SU2, SV2;
3047   Standard_Real U0, U1, V0, V1, U, V;
3048   Standard_Real resu0,resv0;
3049   Standard_Real  du1,du2,dv1,dv2, dmaxOn1, dmaxOn2;
3050   Standard_Real x0,y0,z0, x1,y1,z1,d;
3051   Bnd_Box Box1, Box2;
3052   //
3053   iC15=15;
3054   SU1 =iC15*SU_1 ;
3055   SV1 =iC15*SV_1 ;
3056   SU2 =iC15*SU_2 ;
3057   SV2 =iC15*SV_2 ;
3058   //
3059   if(xNBI<SU1) {
3060     SU1 = xNBI;
3061   }
3062   if(xNBI<SV1){
3063     SV1 = xNBI;
3064   }
3065   if(xNBI<SU2){
3066     SU2 = xNBI;
3067   }
3068   if(xNBI<SV2){
3069     SV2 = xNBI;
3070   }
3071   //
3072   U0 = S1->FirstUParameter();
3073   U1 = S1->LastUParameter();
3074   V0 = S1->FirstVParameter();
3075   V1 = S1->LastVParameter();
3076   //
3077   resu0=U0;
3078   resv0=V0;
3079   //
3080   dmaxOn1 = 0.0;
3081   dmaxOn2 = 0.0;
3082   //-----
3083   du1 = (U1-U0)/(SU1-1);
3084   dv1 = (V1-V0)/(SV1-1);
3085   for(U=U0,i=0; i<SU1; i++,U+=du1) { 
3086     for(V=V0,j=0; j<SV1; V+=dv1,j++) { 
3087       aIPD.xP1(i, j)= S1->Value(U,V);
3088       Box1.Add(aIPD.xP1(i, j));
3089       if(i>0 && j>0) { 
3090         aIPD.xP1(i, j)    .Coord(x0,y0,z0);
3091         aIPD.xP1(i-1, j-1).Coord(x1,y1,z1);
3092         //
3093         d=Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
3094         if(d>dmaxOn1) {
3095           dmaxOn1 = d;
3096         }
3097       }
3098     }
3099   }
3100   Box1.Enlarge(1.e-8);
3101   //
3102   U0 = S2->FirstUParameter();
3103   U1 = S2->LastUParameter();
3104   V0 = S2->FirstVParameter();
3105   V1 = S2->LastVParameter();
3106   //
3107   du2 = (U1-U0)/(SU2-1);  
3108   dv2 = (V1-V0)/(SV2-1);
3109   for(U=U0,i=0; i<SU2; i++,U+=du2) { 
3110     for(V=V0,j=0; j<SV2; V+=dv2,j++) { 
3111       aIPD.xP2(i, j) = S2->Value(U,V);
3112       Box2.Add(aIPD.xP2(i, j));
3113       if(i>0 && j>0) { 
3114         aIPD.xP2(i, j)    .Coord(x0,y0,z0);
3115         aIPD.xP2(i-1, j-1).Coord(x1,y1,z1);
3116         d =  Abs(x1-x0)+Abs(y1-y0)+Abs(z1-z0);
3117         if(d>dmaxOn2) {
3118           dmaxOn2 = d;
3119         }
3120       }
3121     }
3122   }
3123   Box2.Enlarge(1.e-8);
3124   //--------
3125   //
3126   if(Box1.IsOut(Box2)) {
3127
3128     return;
3129   }
3130   //
3131   Standard_Integer aNbPG;
3132   Standard_Real x10,y10,z10,x11,y11,z11;
3133   Standard_Real x20,y20,z20,x21,y21,z21;
3134   Standard_Real dx, dy, dz, dmax;
3135   Standard_Real dx2, dy2, dz2;
3136   //
3137   Box1.Get(x10,y10,z10,x11,y11,z11);
3138   Box2.Get(x20,y20,z20,x21,y21,z21);
3139   //