ebc4cbe82a4209307d0f361553dc242b0c55dbe5
[occt.git] / src / IntPatch / IntPatch_ImpPrmIntersection.cxx
1 // Created on: 1992-05-07
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1992-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
18 #include <Adaptor2d_HCurve2d.hxx>
19 #include <Adaptor3d_HSurface.hxx>
20 #include <Adaptor3d_TopolTool.hxx>
21 #include <IntPatch_ArcFunction.hxx>
22 #include <IntPatch_ImpPrmIntersection.hxx>
23 #include <IntPatch_Line.hxx>
24 #include <IntPatch_Point.hxx>
25 #include <IntPatch_RLine.hxx>
26 #include <IntPatch_RstInt.hxx>
27 #include <IntPatch_SequenceOfLine.hxx>
28 #include <IntPatch_TheIWalking.hxx>
29 #include <IntPatch_TheIWLineOfTheIWalking.hxx>
30 #include <IntPatch_ThePathPointOfTheSOnBounds.hxx>
31 #include <IntPatch_TheSegmentOfTheSOnBounds.hxx>
32 #include <IntPatch_TheSurfFunction.hxx>
33 #include <IntPatch_WLine.hxx>
34 #include <IntSurf.hxx>
35 #include <IntSurf_InteriorPoint.hxx>
36 #include <IntSurf_LineOn2S.hxx>
37 #include <IntSurf_PathPoint.hxx>
38 #include <IntSurf_PntOn2S.hxx>
39 #include <IntSurf_SequenceOfPathPoint.hxx>
40 #include <Standard_ConstructionError.hxx>
41 #include <Standard_DomainError.hxx>
42 #include <Standard_OutOfRange.hxx>
43 #include <StdFail_NotDone.hxx>
44 #include <TColStd_Array1OfInteger.hxx>
45
46 #ifndef OCCT_DEBUG
47 #define No_Standard_RangeError
48 #define No_Standard_OutOfRange
49 #endif
50
51 #include <math_Vector.hxx>
52 #include <math_Matrix.hxx>
53 #include <TopTrans_CurveTransition.hxx>
54 #include <TopAbs_State.hxx>
55 #include <TopAbs_Orientation.hxx>
56 #include <TColStd_Array1OfInteger.hxx>
57 #include <TColStd_Array1OfReal.hxx>
58
59 #include <IntSurf_SequenceOfInteriorPoint.hxx>
60 #include <IntSurf_QuadricTool.hxx>
61 #include <GeomAbs_SurfaceType.hxx>
62 #include <IntAna2d_AnaIntersection.hxx>
63 #include <gp_Lin2d.hxx>
64 #include <ElCLib.hxx>
65
66 #include <Bnd_Box2d.hxx>
67 #include <IntPatch_PointLine.hxx>
68
69 static Standard_Boolean DecomposeResult(const Handle(IntPatch_Line)&   Line,
70   const Standard_Boolean         IsReversed,
71   const IntSurf_Quadric&         Quad,
72   const Handle(Adaptor3d_TopolTool)&    PDomain,
73   const Handle(Adaptor3d_HSurface)&              QSurf,
74   const Standard_Real            ArcTol,
75   IntPatch_SequenceOfLine& Lines);
76 static 
77   void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
78   IntSurf_SequenceOfPathPoint& seqpdep,
79   const Handle(Adaptor3d_TopolTool)& Domain,
80   IntPatch_TheSurfFunction& Func,
81   const Handle(Adaptor3d_HSurface)& PSurf,
82   TColStd_Array1OfInteger& Destination);
83 static 
84   void Recadre(const Standard_Boolean ,
85   GeomAbs_SurfaceType typeS1,
86   GeomAbs_SurfaceType typeS2,
87   IntPatch_Point&  pt,
88   const Handle(IntPatch_TheIWLineOfTheIWalking)& iwline,
89   Standard_Integer Param,
90   Standard_Real U1,
91   Standard_Real V1,
92   Standard_Real U2,
93   Standard_Real V2);
94
95 static Standard_Boolean IsIn2DBox(const Bnd_Box2d& theBox,
96                                   const Handle(IntPatch_PointLine)& theLine,
97                                   const Standard_Real theUPeriod,
98                                   const Standard_Real theVPeriod,
99                                   const Standard_Boolean isTheSurface1Using);
100
101 //=======================================================================
102 //function : IntPatch_ImpPrmIntersection
103 //purpose  : 
104 //=======================================================================
105 IntPatch_ImpPrmIntersection::IntPatch_ImpPrmIntersection ()
106   : done(Standard_False),
107   empt(Standard_False),
108   myIsStartPnt(Standard_False),
109   myUStart(0.0),
110   myVStart(0.0)
111 { }
112
113
114 //=======================================================================
115 //function : IntPatch_ImpPrmIntersection
116 //purpose  : 
117 //=======================================================================
118
119 IntPatch_ImpPrmIntersection::IntPatch_ImpPrmIntersection
120   (const Handle(Adaptor3d_HSurface)&    Surf1,
121   const Handle(Adaptor3d_TopolTool)&   D1,
122   const Handle(Adaptor3d_HSurface)&    Surf2,
123   const Handle(Adaptor3d_TopolTool)&   D2,
124   const Standard_Real    TolArc,
125   const Standard_Real    TolTang,
126   const Standard_Real    Fleche,
127   const Standard_Real    Pas)
128   : done(Standard_False),
129   empt(Standard_False),
130   myIsStartPnt(Standard_False),
131   myUStart(0.0),
132   myVStart(0.0)
133 {
134   Perform(Surf1,D1,Surf2,D2,TolArc,TolTang,Fleche,Pas);
135 }
136
137
138 //=======================================================================
139 //function : SetStartPoint
140 //purpose  : 
141 //=======================================================================
142
143 void IntPatch_ImpPrmIntersection::SetStartPoint(const Standard_Real U,
144   const Standard_Real V)
145 {
146   myIsStartPnt = Standard_True;
147   myUStart = U; myVStart = V;
148 }
149
150 //=======================================================================
151 //function : ComputeTangency
152 //purpose  : 
153 //=======================================================================
154 void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
155   IntSurf_SequenceOfPathPoint& seqpdep,
156   const Handle(Adaptor3d_TopolTool)& Domain,
157   IntPatch_TheSurfFunction& Func,
158   const Handle(Adaptor3d_HSurface)& PSurf,
159   TColStd_Array1OfInteger& Destination)
160 {
161   Standard_Integer i,k, NbPoints, seqlength;
162   Standard_Real theparam,test;
163   Standard_Boolean fairpt, ispassing;
164   TopAbs_Orientation arcorien,vtxorien;
165   Handle(Adaptor2d_HCurve2d) thearc;
166   Handle(Adaptor3d_HVertex) vtx,vtxbis;
167   //Standard_Boolean ispassing;
168   IntPatch_ThePathPointOfTheSOnBounds PStart;
169   IntSurf_PathPoint PPoint;
170   gp_Vec vectg;
171   gp_Dir2d dirtg;
172   gp_Pnt ptbid;
173   gp_Vec d1u,d1v,v1,v2;
174   gp_Pnt2d p2d;
175   gp_Vec2d d2d;
176   //
177   double aX[2], aF[1], aD[1][2];
178   math_Vector X(aX, 1, 2);
179   math_Vector F(aF, 1, 1);
180   math_Matrix D(aD, 1, 1, 1, 2); 
181   //
182   seqlength = 0;
183   NbPoints = solrst.NbPoints();
184   for (i=1; i<= NbPoints; i++) {
185     if (Destination(i) == 0) {
186       PStart = solrst.Point(i);
187       thearc   = PStart.Arc();
188       theparam = PStart.Parameter();
189       arcorien = Domain->Orientation(thearc);
190       ispassing = (arcorien == TopAbs_INTERNAL || 
191         arcorien == TopAbs_EXTERNAL);
192
193       thearc->D0(theparam,p2d);
194       X(1) = p2d.X(); 
195       X(2) = p2d.Y();
196       PPoint.SetValue(PStart.Value(),X(1),X(2));
197
198       Func.Values(X,F,D);
199       if (Func.IsTangent()) {
200         PPoint.SetTangency(Standard_True);
201         Destination(i) = seqlength+1;
202         if (!PStart.IsNew()) {
203           vtx = PStart.Vertex();
204           for (k=i+1; k<=NbPoints; k++) {
205             if (Destination(k) ==0) {
206               PStart = solrst.Point(k);
207               if (!PStart.IsNew()) {
208                 vtxbis = PStart.Vertex();
209                 if (Domain->Identical(vtx,vtxbis)) {
210                   thearc   = PStart.Arc();
211                   theparam = PStart.Parameter();
212                   arcorien = Domain->Orientation(thearc);
213                   ispassing = ispassing && (arcorien == TopAbs_INTERNAL ||
214                     arcorien == TopAbs_EXTERNAL);
215
216                   thearc->D0(theparam,p2d);
217                   PPoint.AddUV(p2d.X(),p2d.Y());
218                   Destination(k) = seqlength+1;
219                 }
220               }
221             }
222           }
223         }
224         PPoint.SetPassing(ispassing);
225         seqpdep.Append(PPoint);
226         seqlength++;
227       }
228       else { // on a un point de depart potentiel
229
230         vectg = Func.Direction3d();
231         dirtg = Func.Direction2d();
232
233         PSurf->D1(X(1),X(2),ptbid,d1u,d1v);
234         thearc->D1(theparam,p2d,d2d);
235         v2.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
236         v1 = d1u.Crossed(d1v);
237
238         test = vectg.Dot(v1.Crossed(v2));
239         if (PStart.IsNew()) {
240           if ((test < 0. && arcorien == TopAbs_FORWARD) ||
241             (test > 0. && arcorien == TopAbs_REVERSED)) {
242               vectg.Reverse();
243               dirtg.Reverse();
244           }
245           PPoint.SetDirections(vectg,dirtg);
246           PPoint.SetPassing(ispassing);
247           Destination(i) = seqlength+1;
248           seqpdep.Append(PPoint);
249           seqlength++;
250         }
251         else { // traiter la transition complexe
252           gp_Dir bidnorm(1.,1.,1.);
253           Standard_Real tole = 1.e-8;
254           TopAbs_Orientation LocTrans;
255           TopTrans_CurveTransition comptrans;
256           comptrans.Reset(vectg,bidnorm,0.);
257           if (arcorien == TopAbs_FORWARD || 
258             arcorien == TopAbs_REVERSED) {
259               // pour essai
260
261               vtx = PStart.Vertex();
262               vtxorien = Domain->Orientation(vtx);
263               if (Abs(test) <= tole) {
264                 LocTrans = TopAbs_EXTERNAL; // et pourquoi pas INTERNAL
265               }
266               else {
267                 if (((test > 0.)&& arcorien == TopAbs_FORWARD) ||
268                   ((test < 0.)&& arcorien == TopAbs_REVERSED)){
269                     LocTrans = TopAbs_FORWARD;
270                 }
271                 else {
272                   LocTrans = TopAbs_REVERSED;
273                 }
274                 if (arcorien == TopAbs_REVERSED) {v2.Reverse();}
275               }
276
277               comptrans.Compare(tole,v2,bidnorm,0.,LocTrans,vtxorien);
278           }
279           Destination(i) = seqlength+1;
280           for (k= i+1; k<=NbPoints; k++) {
281             if (Destination(k) == 0) {
282               PStart = solrst.Point(k);
283               if (!PStart.IsNew()) {
284                 vtxbis = PStart.Vertex();
285                 if (Domain->Identical(vtx,vtxbis)) {
286                   thearc   = PStart.Arc();
287                   theparam = PStart.Parameter();
288                   arcorien = Domain->Orientation(thearc);
289
290                   PPoint.AddUV(X(1),X(2));
291
292                   thearc->D1(theparam,p2d,d2d);
293                   PPoint.AddUV(p2d.X(),p2d.Y());
294
295                   if (arcorien == TopAbs_FORWARD || 
296                     arcorien == TopAbs_REVERSED) {
297                       ispassing = Standard_False;
298                       v2.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
299
300                       test = vectg.Dot(v1.Crossed(v2));
301                       vtxorien = Domain->Orientation(PStart.Vertex());
302                       if (Abs(test) <= tole) {
303                         LocTrans = TopAbs_EXTERNAL; // et pourquoi pas INTERNAL
304                       }
305                       else {
306                         if (((test > 0.)&& arcorien == TopAbs_FORWARD) ||
307                           ((test < 0.)&& arcorien == TopAbs_REVERSED)){
308                             LocTrans = TopAbs_FORWARD;
309                         }
310                         else {
311                           LocTrans = TopAbs_REVERSED;
312                         }
313                         if (arcorien == TopAbs_REVERSED) {v2.Reverse();}
314                       }
315
316                       comptrans.Compare(tole,v2,bidnorm,0.,LocTrans,vtxorien);
317                   }
318                   Destination(k) = seqlength+1;
319                 }
320               }
321             }
322           }
323           fairpt = Standard_True;
324           if (!ispassing) {
325             TopAbs_State Before = comptrans.StateBefore();
326             TopAbs_State After  = comptrans.StateAfter();
327             if ((Before == TopAbs_UNKNOWN)||(After == TopAbs_UNKNOWN)) {
328               fairpt = Standard_False;
329             }
330             else if (Before == TopAbs_IN) {
331               if (After == TopAbs_IN) {
332                 ispassing = Standard_True;
333               }
334               else {
335                 vectg.Reverse();
336                 dirtg.Reverse();
337               }
338             }
339             else {
340               if (After !=TopAbs_IN) {
341                 fairpt = Standard_False;
342               }
343             }
344           }
345           if (fairpt) {
346             PPoint.SetDirections(vectg,dirtg);
347             PPoint.SetPassing(ispassing);
348             seqpdep.Append(PPoint);
349             seqlength++;
350           }
351           else { // il faut remettre en "ordre" si on ne garde pas le point.
352             for (k=i; k <=NbPoints ; k++) {
353               if (Destination(k)==seqlength + 1) {
354                 Destination(k) = -Destination(k);
355               }
356             }
357           }
358         }
359       }
360     }
361   }
362 }
363 //=======================================================================
364 //function : Recadre
365 //purpose  : 
366 //=======================================================================
367 void Recadre(const Standard_Boolean ,
368   GeomAbs_SurfaceType typeS1,
369   GeomAbs_SurfaceType typeS2,
370   IntPatch_Point&  pt,
371   const Handle(IntPatch_TheIWLineOfTheIWalking)& iwline,
372   Standard_Integer Param,
373   Standard_Real U1,
374   Standard_Real V1,
375   Standard_Real U2,
376   Standard_Real V2)
377 {
378   Standard_Real U1p,V1p,U2p,V2p;
379   iwline->Line()->Value(Param).Parameters(U1p,V1p,U2p,V2p);
380   switch(typeS1)
381   {
382   case GeomAbs_Torus:
383     while(V1<(V1p-1.5*M_PI)) V1+=M_PI+M_PI;
384     while(V1>(V1p+1.5*M_PI)) V1-=M_PI+M_PI;
385   case GeomAbs_Cylinder:
386   case GeomAbs_Cone:
387   case GeomAbs_Sphere:
388     while(U1<(U1p-1.5*M_PI)) U1+=M_PI+M_PI;
389     while(U1>(U1p+1.5*M_PI)) U1-=M_PI+M_PI;
390   default:
391     break;
392   }
393   switch(typeS2)
394   { 
395   case GeomAbs_Torus:
396     while(V2<(V2p-1.5*M_PI)) V2+=M_PI+M_PI;
397     while(V2>(V2p+1.5*M_PI)) V2-=M_PI+M_PI;
398   case GeomAbs_Cylinder:
399   case GeomAbs_Cone:
400   case GeomAbs_Sphere:
401     while(U2<(U2p-1.5*M_PI)) U2+=M_PI+M_PI;
402     while(U2>(U2p+1.5*M_PI)) U2-=M_PI+M_PI;
403   default:
404     break;
405   }
406   pt.SetParameters(U1,V1,U2,V2);
407 }
408
409 //=======================================================================
410 //function : Perform
411 //purpose  : 
412 //=======================================================================
413 void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
414   const Handle(Adaptor3d_TopolTool)& D1,
415   const Handle(Adaptor3d_HSurface)& Surf2,
416   const Handle(Adaptor3d_TopolTool)& D2,
417   const Standard_Real TolArc,
418   const Standard_Real TolTang,
419   const Standard_Real Fleche,
420   const Standard_Real Pas)
421 {
422   Standard_Boolean reversed, procf, procl, dofirst, dolast;
423   Standard_Integer indfirst = 0, indlast = 0, ind2, NbSegm;
424   Standard_Integer NbPointIns, NbPointRst, Nblines, Nbpts, NbPointDep;
425   Standard_Real U1,V1,U2,V2,paramf,paraml,currentparam;
426
427   IntPatch_TheSegmentOfTheSOnBounds thesegm;
428   IntSurf_PathPoint PPoint;
429
430   Handle(IntPatch_RLine) rline;
431   Handle(IntPatch_WLine) wline;
432   IntPatch_ThePathPointOfTheSOnBounds PStart,PStartf,PStartl;
433   IntPatch_Point ptdeb,ptfin,ptbis;
434
435   IntPatch_IType typ;
436   IntSurf_Transition TLine,TArc;
437   IntSurf_TypeTrans trans1,trans2;
438   gp_Pnt valpt,ptbid;
439   gp_Vec tgline,tgrst,norm1,norm2,d1u,d1v;
440   gp_Dir DirNormale;
441   gp_Vec VecNormale;
442
443   gp_Pnt2d p2d;
444   gp_Vec2d d2d;
445
446   Handle(Adaptor2d_HCurve2d) currentarc;
447   GeomAbs_SurfaceType typeS1, typeS2;
448   IntSurf_Quadric Quad;
449   IntPatch_TheSurfFunction Func;
450   IntPatch_ArcFunction AFunc;
451   //
452   typeS1 = Surf1->GetType();
453   typeS2 = Surf2->GetType();
454
455   paramf =0.;
456   paraml =0.;
457   trans1 = IntSurf_Undecided;
458   trans2 = IntSurf_Undecided;
459   //
460   done = Standard_False;
461   empt = Standard_True;
462   slin.Clear();
463   spnt.Clear();
464   //
465   reversed = Standard_False;
466   switch (typeS1)
467   {
468   case GeomAbs_Plane:
469     Quad.SetValue(Surf1->Plane());
470     break;
471
472   case GeomAbs_Cylinder:
473     Quad.SetValue(Surf1->Cylinder());
474     break;
475
476   case GeomAbs_Sphere:
477     Quad.SetValue(Surf1->Sphere());
478     break;
479
480   case GeomAbs_Cone:
481     Quad.SetValue(Surf1->Cone());
482     break;
483
484   default:
485     {
486       reversed = Standard_True;
487       switch (typeS2)
488       {
489       case GeomAbs_Plane:
490         Quad.SetValue(Surf2->Plane());
491         break;
492
493       case GeomAbs_Cylinder:
494         Quad.SetValue(Surf2->Cylinder());
495         break;
496
497       case GeomAbs_Sphere:
498         Quad.SetValue(Surf2->Sphere());
499         break;
500
501       case GeomAbs_Cone:
502         Quad.SetValue(Surf2->Cone());
503         break;
504       default:
505         {
506           Standard_ConstructionError::Raise();
507           break;
508         }
509       } 
510     }
511     break;
512   }
513   //
514   Func.SetImplicitSurface(Quad);
515   Func.Set(IntSurf_QuadricTool::Tolerance(Quad));
516   AFunc.SetQuadric(Quad);
517   //
518   if (!reversed) {
519     Func.Set(Surf2);
520     AFunc.Set(Surf2);
521   }
522   else {
523     Func.Set(Surf1);
524     AFunc.Set(Surf1);
525   }
526   //
527   if (!reversed) {
528     solrst.Perform(AFunc,D2,TolArc,TolTang);
529   }
530   else {
531     solrst.Perform(AFunc,D1,TolArc,TolTang);
532   }
533   if (!solrst.IsDone()) {
534     return;
535   }
536   //
537   IntSurf_SequenceOfPathPoint seqpdep;
538   IntSurf_SequenceOfInteriorPoint seqpins;
539   //
540   NbPointRst = solrst.NbPoints();
541   TColStd_Array1OfInteger Destination(1,NbPointRst+1); Destination.Init(0);
542   if (NbPointRst) {
543     if (!reversed) {
544       ComputeTangency(solrst,seqpdep,D2,Func,Surf2,Destination);
545     }
546     else {
547       ComputeTangency(solrst,seqpdep,D1,Func,Surf1,Destination);
548     }
549   }
550   //
551   Standard_Boolean SearchIns = Standard_True;
552   if(Quad.TypeQuadric() == GeomAbs_Plane && solrst.NbSegments() > 0)
553   {
554     //For such kind of cases it is possible that whole surface is on one side of plane,
555     //plane only touches surface and does not cross it,
556     //so no inner points exist.
557     SearchIns = Standard_False;
558     Handle(Adaptor3d_TopolTool) T;
559     if(reversed)
560     {
561       T = D1;
562     }
563     else
564     {
565       T = D2;
566     }
567     Standard_Integer aNbSamples = 0;
568     aNbSamples = T->NbSamples();
569     gp_Pnt2d s2d;
570     gp_Pnt s3d;
571     Standard_Real aValf[1], aUVap[2];
572     math_Vector Valf(aValf,1,1), UVap(aUVap,1,2);
573     T->SamplePoint(1,s2d, s3d);
574     UVap(1)=s2d.X(); 
575     UVap(2)=s2d.Y();
576     Func.Value(UVap,Valf);
577     Standard_Real rvalf = Sign(1.,Valf(1));
578     for(Standard_Integer i = 2; i <= aNbSamples; ++i)
579     {
580       T->SamplePoint(i,s2d, s3d);
581       UVap(1)=s2d.X(); 
582       UVap(2)=s2d.Y();
583       Func.Value(UVap,Valf);
584       if(rvalf * Valf(1) < 0.)
585       {
586         SearchIns = Standard_True;
587         break;
588       }   
589     }
590   }
591   // Recherche des points interieurs
592   NbPointIns = 0;
593   if(SearchIns) {
594     if (!reversed) {
595       if (myIsStartPnt)
596         solins.Perform(Func,Surf2,myUStart,myVStart);
597       else
598         solins.Perform(Func,Surf2,D2,TolTang);
599     }
600     else {
601       if (myIsStartPnt)
602         solins.Perform(Func,Surf1,myUStart,myVStart);
603       else
604         solins.Perform(Func,Surf1,D1,TolTang);
605     }
606     NbPointIns = solins.NbPoints();
607     for (Standard_Integer i=1; i <= NbPointIns; i++) {
608       seqpins.Append(solins.Value(i));
609     }
610   }
611   //
612   NbPointDep=seqpdep.Length();
613   //
614   if (NbPointDep || NbPointIns) {
615     IntPatch_TheIWalking iwalk(TolTang,Fleche,Pas);
616     if (!reversed) {
617       iwalk.Perform(seqpdep,seqpins,Func,Surf2);
618     }
619     else {
620       iwalk.Perform(seqpdep,seqpins,Func,Surf1,Standard_True);
621     }
622     if(!iwalk.IsDone()) {
623       return;
624     }
625
626     Standard_Real Vmin, Vmax, TolV = 1.e-14;
627     if (!reversed) { //Surf1 is quadric
628       Vmin = Surf1->FirstVParameter();
629       Vmax = Surf1->LastVParameter();
630     }
631     else { //Surf2 is quadric
632       Vmin = Surf2->FirstVParameter();
633       Vmax = Surf2->LastVParameter();
634     }
635     //
636     Nblines = iwalk.NbLines();
637     for (Standard_Integer j=1; j<=Nblines; j++) {
638       const Handle(IntPatch_TheIWLineOfTheIWalking)&  iwline  = iwalk.Value(j);
639       const Handle(IntSurf_LineOn2S)&                 thelin  = iwline->Line();
640
641       Nbpts = thelin->NbPoints();
642       if(Nbpts>=2) { 
643         Standard_Integer k = 0;
644         tgline = iwline->TangentVector(k);      
645         if(k>=1 && k<=Nbpts) { } else { k=Nbpts>>1; } 
646         valpt = thelin->Value(k).Value();       
647
648         if (!reversed) {
649           thelin->Value(k).ParametersOnS2(U2,V2);
650           norm1 = Quad.Normale(valpt);
651           Surf2->D1(U2,V2,ptbid,d1u,d1v);
652           norm2 = d1u.Crossed(d1v);
653         }
654         else {
655           thelin->Value(k).ParametersOnS1(U2,V2);
656           norm2 = Quad.Normale(valpt);
657           Surf1->D1(U2,V2,ptbid,d1u,d1v);
658           norm1 = d1u.Crossed(d1v);
659         }
660         if (tgline.DotCross(norm2,norm1) > 0.) {
661           trans1 = IntSurf_Out;
662           trans2 = IntSurf_In;
663         }
664         else {
665           trans1 = IntSurf_In;
666           trans2 = IntSurf_Out;
667         }
668
669         //
670         Standard_Real AnU1,AnU2,AnV2;
671
672         GeomAbs_SurfaceType typQuad = Quad.TypeQuadric();
673         Standard_Boolean arecadr=Standard_False;
674         valpt = thelin->Value(1).Value();
675         Quad.Parameters(valpt,AnU1,V1);
676
677         if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
678         if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
679
680         if(reversed) { 
681           thelin->SetUV(1,Standard_False,AnU1,V1); //-- on va lire u2,v2
682           thelin->Value(1).ParametersOnS1(AnU2,AnV2);
683         }
684         else { 
685           thelin->SetUV(1,Standard_True,AnU1,V1);  //-- on va lire u1,v1 
686           thelin->Value(1).ParametersOnS2(AnU2,AnV2);
687         }
688
689         if(typQuad==GeomAbs_Cylinder || 
690           typQuad==GeomAbs_Cone || 
691           typQuad==GeomAbs_Sphere) { 
692             arecadr=Standard_True; 
693         } 
694         //
695         for (k=2; k<=Nbpts; ++k) {
696           valpt = thelin->Value(k).Value();
697           Quad.Parameters(valpt,U1,V1);
698           //
699           if((V1 < Vmin) && (Vmin-V1 < TolV)) {
700             V1 = Vmin;
701           }
702           if((V1 > Vmax) && (V1-Vmax < TolV)) {
703             V1 = Vmax;
704           }
705           //
706           if(arecadr) {
707             //modified by NIZNHY-PKV Fri Mar 28 15:06:01 2008f
708             Standard_Real aCf, aTwoPI;
709             //
710             aCf=0.;
711             aTwoPI=M_PI+M_PI;
712             if ((U1-AnU1) >  1.5*M_PI) { 
713               while ((U1-AnU1) > (1.5*M_PI+aCf*aTwoPI)) {
714                 aCf=aCf+1.;
715               }
716               U1=U1-aCf*aTwoPI;
717             } 
718             //
719             else {
720               while ((U1-AnU1) < (-1.5*M_PI-aCf*aTwoPI)) {
721                 aCf=aCf+1.;
722               }
723               U1=U1+aCf*aTwoPI;
724             }
725             // was:
726             //if ((U1-AnU1) >  1.5*M_PI) { 
727             //  U1-=M_PI+M_PI;
728             //}
729             //else if ((U1-AnU1) < -1.5*M_PI) { 
730             //  U1+=M_PI+M_PI; 
731             //}
732             //modified by NIZNHY-PKV Fri Mar 28 15:06:11 2008t
733           }
734           //
735           if(reversed) { 
736             thelin->SetUV(k,Standard_False,U1,V1);
737
738             thelin->Value(k).ParametersOnS1(U2,V2);
739             switch(typeS1) { 
740             case GeomAbs_Cylinder:
741             case GeomAbs_Cone:
742             case GeomAbs_Sphere:
743             case GeomAbs_Torus:
744               while(U2<(AnU2-1.5*M_PI)) U2+=M_PI+M_PI;
745               while(U2>(AnU2+1.5*M_PI)) U2-=M_PI+M_PI;
746               break;
747             default: 
748               break;
749             }
750             if(typeS2==GeomAbs_Torus) { 
751               while(V2<(AnV2-1.5*M_PI)) V2+=M_PI+M_PI;
752               while(V2>(AnV2+1.5*M_PI)) V2-=M_PI+M_PI;
753             }
754             thelin->SetUV(k,Standard_True,U2,V2);
755           }
756           else { 
757             thelin->SetUV(k,Standard_True,U1,V1);
758
759             thelin->Value(k).ParametersOnS2(U2,V2);
760             switch(typeS2) { 
761             case GeomAbs_Cylinder:
762             case GeomAbs_Cone:
763             case GeomAbs_Sphere:
764             case GeomAbs_Torus:
765               while(U2<(AnU2-1.5*M_PI)) U2+=M_PI+M_PI;
766               while(U2>(AnU2+1.5*M_PI)) U2-=M_PI+M_PI;
767               break;
768             default: 
769               break;
770             }
771             if(typeS2==GeomAbs_Torus) { 
772               while(V2<(AnV2-1.5*M_PI)) V2+=M_PI+M_PI;
773               while(V2>(AnV2+1.5*M_PI)) V2-=M_PI+M_PI;
774             }
775             thelin->SetUV(k,Standard_False,U2,V2);
776
777           }
778
779           AnU1=U1;
780           AnU2=U2;
781           AnV2=V2;
782         }
783         // <-A
784         wline = new IntPatch_WLine(thelin,Standard_False,trans1,trans2);
785
786         if (   iwline->HasFirstPoint() 
787           && iwline->IsTangentAtBegining() == Standard_False) 
788         {
789           indfirst = iwline->FirstPointIndex();
790           PPoint = seqpdep(indfirst);
791           tgline = PPoint.Direction3d();
792           Standard_Integer themult = PPoint.Multiplicity();
793           for (Standard_Integer i=NbPointRst; i>=1; i--) {
794             if (Destination(i) == indfirst) {
795               if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
796                 Quad.Parameters(PPoint.Value(),U1,V1);
797
798                 if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
799                 if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
800
801                 PPoint.Parameters(themult,U2,V2);
802                 Surf2->D1(U2,V2,ptbid,d1u,d1v); //-- @@@@
803               }
804               else {  //-- typeS1 != Pln && Cyl && Sph && Cone
805                 Quad.Parameters(PPoint.Value(),U2,V2);
806
807                 if((V2 < Vmin) && (Vmin-V2 < TolV)) V2 = Vmin;
808                 if((V2 > Vmax) && (V2-Vmax < TolV)) V2 = Vmax;
809
810                 PPoint.Parameters(themult,U1,V1);
811                 Surf1->D1(U1,V1,ptbid,d1u,d1v); //-- @@@@
812               }
813
814               VecNormale = d1u.Crossed(d1v);                      
815               //-- Modif du 27 Septembre 94 (Recadrage des pts U,V) 
816               ptdeb.SetValue(PPoint.Value(),TolArc,Standard_False);
817               ptdeb.SetParameters(U1,V1,U2,V2);
818               ptdeb.SetParameter(1.);
819
820               Recadre(reversed,typeS1,typeS2,ptdeb,iwline,1,U1,V1,U2,V2);
821
822               currentarc = solrst.Point(i).Arc();
823               currentparam = solrst.Point(i).Parameter();
824               currentarc->D1(currentparam,p2d,d2d);
825               tgrst.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
826
827               Standard_Real squaremagnitudeVecNormale = VecNormale.SquareMagnitude();
828               if(squaremagnitudeVecNormale > 1e-13) { 
829                 DirNormale=VecNormale;
830                 IntSurf::MakeTransition(tgline,tgrst,DirNormale,TLine,TArc);
831               }
832               else { 
833                 TLine.SetValue(Standard_True,IntSurf_Undecided);
834                 TArc.SetValue(Standard_True,IntSurf_Undecided);
835               }
836
837               ptdeb.SetArc(reversed,currentarc,currentparam,TLine,TArc);
838               if (!solrst.Point(i).IsNew()) {
839                 ptdeb.SetVertex(reversed,solrst.Point(i).Vertex());
840               }
841               wline->AddVertex(ptdeb);
842               if (themult == 0) {
843                 wline->SetFirstPoint(wline->NbVertex());
844               }
845
846               themult--;
847             }
848           }
849         }
850         else if (iwline->IsTangentAtBegining()) 
851         {
852           gp_Pnt psol = thelin->Value(1).Value();
853           thelin->Value(1).ParametersOnS1(U1,V1);
854           thelin->Value(1).ParametersOnS2(U2,V2);
855           ptdeb.SetValue(psol,TolArc,Standard_True);
856           ptdeb.SetParameters(U1,V1,U2,V2);
857           ptdeb.SetParameter(1.);
858           wline->AddVertex(ptdeb);
859           wline->SetFirstPoint(wline->NbVertex());
860         }
861         else 
862         { 
863           gp_Pnt psol = thelin->Value(1).Value();
864           thelin->Value(1).ParametersOnS1(U1,V1);
865           thelin->Value(1).ParametersOnS2(U2,V2);
866           ptdeb.SetValue(psol,TolArc,Standard_False);
867           ptdeb.SetParameters(U1,V1,U2,V2);
868           ptdeb.SetParameter(1.);
869           wline->AddVertex(ptdeb);
870           wline->SetFirstPoint(wline->NbVertex());
871         }
872
873
874         if (   iwline->HasLastPoint() 
875           && iwline->IsTangentAtEnd() == Standard_False) 
876         {
877           indlast = iwline->LastPointIndex();
878           PPoint = seqpdep(indlast);
879           tgline = PPoint.Direction3d().Reversed();
880           Standard_Integer themult = PPoint.Multiplicity();
881           for (Standard_Integer i=NbPointRst; i >=1; i--) {
882             if (Destination(i) == indlast) {
883               if (!reversed) {
884                 Quad.Parameters(PPoint.Value(),U1,V1);
885
886                 if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
887                 if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
888
889                 PPoint.Parameters(themult,U2,V2);
890                 Surf2->D1(U2,V2,ptbid,d1u,d1v); //-- @@@@
891                 VecNormale = d1u.Crossed(d1v);                    //-- @@@@
892               }
893               else {
894                 Quad.Parameters(PPoint.Value(),U2,V2);
895
896                 if((V2 < Vmin) && (Vmin-V2 < TolV)) V2 = Vmin;
897                 if((V2 > Vmax) && (V2-Vmax < TolV)) V2 = Vmax;
898
899                 PPoint.Parameters(themult,U1,V1);
900                 Surf1->D1(U1,V1,ptbid,d1u,d1v); //-- @@@@
901                 VecNormale = d1u.Crossed(d1v);                    //-- @@@@
902               }
903
904               ptfin.SetValue(PPoint.Value(),TolArc,Standard_False);
905               ptfin.SetParameters(U1,V1,U2,V2);
906               ptfin.SetParameter(Nbpts);
907
908               Recadre(reversed,typeS1,typeS2,ptfin,iwline,Nbpts-1,U1,V1,U2,V2);
909
910               currentarc = solrst.Point(i).Arc();
911               currentparam = solrst.Point(i).Parameter();
912               currentarc->D1(currentparam,p2d,d2d);
913               tgrst.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
914
915
916               Standard_Real squaremagnitudeVecNormale = VecNormale.SquareMagnitude();
917               if(squaremagnitudeVecNormale > 1e-13) { 
918                 DirNormale=VecNormale;
919                 IntSurf::MakeTransition(tgline,tgrst,DirNormale,TLine,TArc);
920               }
921               else { 
922                 TLine.SetValue(Standard_True,IntSurf_Undecided);
923                 TArc.SetValue(Standard_True,IntSurf_Undecided);
924               }
925
926
927               ptfin.SetArc(reversed,currentarc,currentparam,TLine,TArc);
928               if (!solrst.Point(i).IsNew()) {
929                 ptfin.SetVertex(reversed,solrst.Point(i).Vertex());
930               }
931               wline->AddVertex(ptfin);
932               if (themult == 0) {
933                 wline->SetLastPoint(wline->NbVertex());
934               }
935
936               themult--;
937             }
938           }
939         }
940         else if (iwline->IsTangentAtEnd()) 
941         {
942           gp_Pnt psol = thelin->Value(Nbpts).Value();
943           thelin->Value(Nbpts).ParametersOnS1(U1,V1);
944           thelin->Value(Nbpts).ParametersOnS2(U2,V2);
945           ptfin.SetValue(psol,TolArc,Standard_True);
946           ptfin.SetParameters(U1,V1,U2,V2);
947           ptfin.SetParameter(Nbpts);
948           wline->AddVertex(ptfin);
949           wline->SetLastPoint(wline->NbVertex());
950         }
951         else 
952         { 
953           gp_Pnt psol = thelin->Value(Nbpts).Value();
954           thelin->Value(Nbpts).ParametersOnS1(U1,V1);
955           thelin->Value(Nbpts).ParametersOnS2(U2,V2);
956           ptfin.SetValue(psol,TolArc,Standard_False);
957           ptfin.SetParameters(U1,V1,U2,V2);
958           ptfin.SetParameter(Nbpts);
959           wline->AddVertex(ptfin);
960           wline->SetLastPoint(wline->NbVertex());
961         }
962         //
963         // Il faut traiter les points de passage.
964         slin.Append(wline);
965       }// if(Nbpts>=2) {
966     }// for (j=1; j<=Nblines; j++) {
967
968     // ON GERE LES RACCORDS ENTRE LIGNES. ELLE NE PEUVENT SE RACCORDER
969     // QUE SUR DES POINTS DE TANGENCE
970
971
972     Nblines = slin.Length();
973     for (Standard_Integer j=1; j<=Nblines-1; j++) {
974       dofirst = dolast = Standard_False;
975       const  Handle(IntPatch_Line)& slinj = slin(j);
976       Handle(IntPatch_WLine) wlin1 (Handle(IntPatch_WLine)::DownCast (slinj));
977       if (wlin1->HasFirstPoint()) {
978         ptdeb = wlin1->FirstPoint(indfirst);
979         if (ptdeb.IsTangencyPoint()) {
980           dofirst = Standard_True;
981         }
982       }
983       if (wlin1->HasLastPoint()) {
984         ptfin = wlin1->LastPoint(indlast);
985         if (ptfin.IsTangencyPoint()) {
986           dolast = Standard_True;
987         }
988       }
989
990       if (dofirst || dolast) {
991         for (Standard_Integer k=j+1; k<=Nblines;k++) {
992           const  Handle(IntPatch_Line)& slink = slin(k);
993           Handle(IntPatch_WLine) wlin2 (Handle(IntPatch_WLine)::DownCast (slink));
994           if (wlin2->HasFirstPoint()) {
995             ptbis = wlin2->FirstPoint(ind2);
996             if (ptbis.IsTangencyPoint()) {
997               if (dofirst ) {
998                 if (ptdeb.Value().Distance(ptbis.Value()) <= TolArc) {
999                   ptdeb.SetMultiple(Standard_True);
1000                   if (!ptbis.IsMultiple()) {
1001                     ptbis.SetMultiple(Standard_True);
1002                     wlin2->Replace(ind2,ptbis);
1003                   }
1004                 }
1005               }
1006               if (dolast ) {
1007                 if (ptfin.Value().Distance(ptbis.Value()) <= TolArc) {
1008                   ptfin.SetMultiple(Standard_True);
1009                   if (!ptbis.IsMultiple()) {
1010                     ptbis.SetMultiple(Standard_True);
1011                     wlin2->Replace(ind2,ptbis);
1012                   }
1013                 }
1014               }
1015             }
1016           }
1017           if (wlin2->HasLastPoint()) {
1018             ptbis = wlin2->LastPoint(ind2);
1019             if (ptbis.IsTangencyPoint()) {
1020               if (dofirst ) {
1021                 if (ptdeb.Value().Distance(ptbis.Value()) <= TolArc) {
1022                   ptdeb.SetMultiple(Standard_True);
1023                   if (!ptbis.IsMultiple()) {
1024                     ptbis.SetMultiple(Standard_True);
1025                     wlin2->Replace(ind2,ptbis);
1026                   }
1027                 }
1028               }
1029               if (dolast ) {
1030                 if (ptfin.Value().Distance(ptbis.Value()) <= TolArc) {
1031                   ptfin.SetMultiple(Standard_True);
1032                   if (!ptbis.IsMultiple()) {
1033                     ptbis.SetMultiple(Standard_True);
1034                     wlin2->Replace(ind2,ptbis);
1035                   }
1036                 }
1037               }
1038             }
1039           }
1040         }
1041         if(dofirst) 
1042           wlin1->Replace(indfirst,ptdeb);
1043         if(dolast) 
1044           wlin1->Replace(indlast,ptfin);
1045       }
1046     }
1047   }// if (seqpdep.Length() != 0 || seqpins.Length() != 0) {
1048   //
1049   // Treatment the segments
1050   NbSegm = solrst.NbSegments();
1051   if (NbSegm) {
1052     for(Standard_Integer i=1; i<=NbSegm; i++) {
1053       thesegm = solrst.Segment(i);  
1054       //Check if segment is degenerated
1055       if(thesegm.HasFirstPoint() && thesegm.HasLastPoint())
1056       {
1057         Standard_Real tol2 = Precision::Confusion();
1058         tol2 *= tol2;
1059         const gp_Pnt& aPf = thesegm.FirstPoint().Value();
1060         const gp_Pnt& aPl = thesegm.LastPoint().Value();
1061         if(aPf.SquareDistance(aPl) <= tol2)
1062         {
1063           //segment can be degenerated - check inner point
1064           paramf = thesegm.FirstPoint().Parameter();
1065           paraml = thesegm.LastPoint().Parameter();
1066           gp_Pnt2d _p2d = 
1067             thesegm.Curve()->Value(.57735 * paramf + 0.42265 * paraml);
1068           gp_Pnt aPm;
1069           if(reversed)
1070           {
1071             Surf1->D0(_p2d.X(), _p2d.Y(), aPm);
1072           }
1073           else
1074           {
1075             Surf2->D0(_p2d.X(), _p2d.Y(), aPm);
1076           }
1077           if(aPm.SquareDistance(aPf) <= tol2)
1078           {
1079             //Degenerated
1080             continue;
1081           }
1082         }
1083       }
1084
1085
1086       //----------------------------------------------------------------------      
1087       // on cree une ligne d intersection contenant uniquement le segment.
1088       // VOIR POUR LA TRANSITION DE LA LIGNE
1089       // On ajoute aussi un polygone pour le traitement des intersections
1090       // entre ligne et restrictions de la surface implicite (PutVertexOnLine)
1091       //----------------------------------------------------------------------
1092       //-- Calcul de la transition sur la rline (12 fev 97)
1093       //-- reversed a le sens de OnFirst
1094       //--
1095       dofirst = dolast  = Standard_False;
1096       procf = Standard_False;
1097       procl = Standard_False;
1098       IntSurf_Transition TLineUnk,TArcUnk;
1099
1100       IntPatch_Point _thepointAtBeg;
1101       IntPatch_Point _thepointAtEnd;
1102
1103       Standard_Boolean TransitionOK=Standard_False;
1104
1105       if(thesegm.HasFirstPoint()) { 
1106         Standard_Real _u1,_v1,_u2,_v2;
1107
1108         dofirst = Standard_True;
1109         PStartf = thesegm.FirstPoint();
1110         paramf = PStartf.Parameter();
1111
1112         gp_Pnt2d _p2d     = thesegm.Curve()->Value(paramf);
1113         Handle(Adaptor3d_HVertex) _vtx;
1114         if(PStartf.IsNew()==Standard_False) 
1115           _vtx= PStartf.Vertex();
1116         const gp_Pnt& _Pp = PStartf.Value();
1117         _thepointAtBeg.SetValue(_Pp,PStartf.Tolerance(),Standard_False);
1118         if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
1119           Quad.Parameters(_Pp,_u1,_v1);
1120           _u2=_p2d.X(); _v2=_p2d.Y();
1121         }
1122         else {  //-- typeS1 != Pln && Cyl && Sph && Cone
1123           Quad.Parameters(_Pp,_u2,_v2);
1124           _u1=_p2d.X(); _v1=_p2d.Y();
1125         }
1126         _thepointAtBeg.SetParameters(_u1,_v1,_u2,_v2);
1127         _thepointAtBeg.SetParameter(paramf);
1128         if(PStartf.IsNew()==Standard_False) 
1129           _thepointAtBeg.SetVertex(reversed,_vtx);
1130         _thepointAtBeg.SetArc(reversed,thesegm.Curve(),paramf,TLineUnk,TArcUnk);
1131
1132
1133         gp_Vec d1u1,d1v1,d1u2,d1v2; gp_Vec2d _d2d;
1134         Surf1->D1(_u1,_v1,ptbid,d1u1,d1v1);
1135         norm1 = d1u1.Crossed(d1v1);
1136         Surf2->D1(_u2,_v2,ptbid,d1u2,d1v2);
1137         norm2 = d1u2.Crossed(d1v2);
1138
1139         thesegm.Curve()->D1(paramf,_p2d,_d2d);
1140         if(reversed) { 
1141           tgline.SetLinearForm(_d2d.X(),d1u1,_d2d.Y(),d1v1);
1142         }
1143         else { 
1144           tgline.SetLinearForm(_d2d.X(),d1u2,_d2d.Y(),d1v2);
1145         }
1146         _u1=tgline.DotCross(norm2,norm1);
1147         TransitionOK=Standard_True;
1148         if (_u1 > 0.00000001) {
1149           trans1 = IntSurf_Out;
1150           trans2 = IntSurf_In;
1151         }
1152         else if(_u1 < -0.00000001) { 
1153           trans1 = IntSurf_In;
1154           trans2 = IntSurf_Out;
1155         }
1156         else { 
1157           TransitionOK=Standard_False;
1158         }
1159       }
1160       if(thesegm.HasLastPoint()) {  
1161         Standard_Real _u1,_v1,_u2,_v2;
1162
1163         dolast = Standard_True;
1164         PStartl = thesegm.LastPoint();
1165         paraml = PStartl.Parameter();
1166
1167         gp_Pnt2d _p2d = thesegm.Curve()->Value(paraml);
1168         Handle(Adaptor3d_HVertex) _vtx;
1169         if(PStartl.IsNew()==Standard_False) 
1170           _vtx = PStartl.Vertex();
1171         const gp_Pnt& _Pp = PStartl.Value();
1172         IntPatch_Point _thepoint;
1173         _thepointAtEnd.SetValue(_Pp,PStartl.Tolerance(),Standard_False);
1174         if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
1175           Quad.Parameters(_Pp,_u1,_v1);
1176           _u2=_p2d.X(); _v2=_p2d.Y();
1177         }
1178         else {  //-- typeS1 != Pln && Cyl && Sph && Cone
1179           Quad.Parameters(_Pp,_u2,_v2);
1180           _u1=_p2d.X(); _v1=_p2d.Y();
1181         }
1182         _thepointAtEnd.SetParameters(_u1,_v1,_u2,_v2);
1183         _thepointAtEnd.SetParameter(paraml);
1184         if(PStartl.IsNew()==Standard_False)
1185           _thepointAtEnd.SetVertex(reversed,_vtx);
1186         _thepointAtEnd.SetArc(reversed,thesegm.Curve(),paraml,TLineUnk,TArcUnk);
1187
1188
1189
1190         gp_Vec d1u1,d1v1,d1u2,d1v2; gp_Vec2d _d2d;
1191         Surf1->D1(_u1,_v1,ptbid,d1u1,d1v1);
1192         norm1 = d1u1.Crossed(d1v1);
1193         Surf2->D1(_u2,_v2,ptbid,d1u2,d1v2);
1194         norm2 = d1u2.Crossed(d1v2);
1195
1196         thesegm.Curve()->D1(paraml,_p2d,_d2d);
1197         if(reversed) { 
1198           tgline.SetLinearForm(_d2d.X(),d1u1,_d2d.Y(),d1v1);
1199         }
1200         else { 
1201           tgline.SetLinearForm(_d2d.X(),d1u2,_d2d.Y(),d1v2);
1202         }
1203         _u1=tgline.DotCross(norm2,norm1);
1204         TransitionOK=Standard_True;
1205         if (_u1 > 0.00000001) {
1206           trans1 = IntSurf_Out;
1207           trans2 = IntSurf_In;
1208         }
1209         else if(_u1 < -0.00000001) { 
1210           trans1 = IntSurf_In;
1211           trans2 = IntSurf_Out;
1212         }
1213         else { 
1214           TransitionOK=Standard_False;
1215         }       
1216       }
1217       if(TransitionOK==Standard_False) { 
1218         //-- rline = new IntPatch_RLine (thesegm.Curve(),reversed,Standard_False);
1219         rline =  new IntPatch_RLine (Standard_False);
1220         if(reversed) { 
1221           rline->SetArcOnS1(thesegm.Curve());
1222         }
1223         else { 
1224           rline->SetArcOnS2(thesegm.Curve());
1225         }
1226       }
1227       else { 
1228         //-- rline = new IntPatch_RLine (thesegm.Curve(),reversed,Standard_False,trans1,trans2);
1229         rline =  new IntPatch_RLine (Standard_False,trans1,trans2);
1230         if(reversed) { 
1231           rline->SetArcOnS1(thesegm.Curve());
1232         }
1233         else { 
1234           rline->SetArcOnS2(thesegm.Curve());
1235         }
1236       }
1237
1238       //------------------------------
1239       //-- Ajout des points 
1240       //--
1241       if (thesegm.HasFirstPoint()) {
1242         rline->AddVertex(_thepointAtBeg);
1243         rline->SetFirstPoint(rline->NbVertex());
1244       }
1245
1246       if (thesegm.HasLastPoint()) {
1247         rline->AddVertex(_thepointAtEnd);
1248         rline->SetLastPoint(rline->NbVertex());
1249       }
1250
1251       // Polygone sur restriction solution
1252       if (dofirst && dolast) {
1253         Standard_Real prm;
1254         gp_Pnt ptpoly;
1255         IntSurf_PntOn2S p2s;
1256         Handle(IntSurf_LineOn2S) Thelin = new IntSurf_LineOn2S ();
1257         Handle(Adaptor2d_HCurve2d) arcsegm = thesegm.Curve();
1258         Standard_Integer nbsample = 100;
1259
1260         if (!reversed) {
1261           for (Standard_Integer j=1; j<=nbsample; j++) {
1262             prm = paramf + (j-1)*(paraml-paramf)/(nbsample-1);
1263             arcsegm->D0(prm,p2d);
1264             Surf2->D0(p2d.X(),p2d.Y(),ptpoly);
1265
1266             Quad.Parameters(ptpoly,U1,V1);
1267             p2s.SetValue(ptpoly,U1,V1,p2d.X(),p2d.Y());
1268             Thelin->Add(p2s);
1269           }
1270         }
1271         else {
1272           for (Standard_Integer j=1; j<=nbsample; j++) {
1273             prm = paramf + (j-1)*(paraml-paramf)/(nbsample-1);
1274             arcsegm->D0(prm,p2d);
1275             Surf1->D0(p2d.X(),p2d.Y(),ptpoly);
1276
1277             Quad.Parameters(ptpoly,U2,V2);
1278             p2s.SetValue(ptpoly,p2d.X(),p2d.Y(),U2,V2);
1279             Thelin->Add(p2s);
1280           }
1281         }
1282         rline->Add(Thelin);
1283       }
1284
1285       if (dofirst || dolast) {
1286         Nblines = slin.Length();
1287         for (Standard_Integer j=1; j<=Nblines; j++) {
1288           const Handle(IntPatch_Line)& slinj = slin(j);
1289           typ = slinj->ArcType();
1290           if (typ == IntPatch_Walking) {
1291             Nbpts = Handle(IntPatch_WLine)::DownCast (slinj)->NbVertex();
1292           }
1293           else {
1294             Nbpts = Handle(IntPatch_RLine)::DownCast (slinj)->NbVertex();
1295           }
1296           for (Standard_Integer k=1; k<=Nbpts;k++) {
1297             if (typ == IntPatch_Walking) {
1298               ptdeb = Handle(IntPatch_WLine)::DownCast (slinj)->Vertex(k);
1299             }
1300             else {
1301               ptdeb = Handle(IntPatch_RLine)::DownCast (slinj)->Vertex(k);
1302             }
1303             if (dofirst) {
1304
1305               if (ptdeb.Value().Distance(PStartf.Value()) <=TolArc) {
1306                 ptdeb.SetMultiple(Standard_True);
1307                 if (typ == IntPatch_Walking) {
1308                   Handle(IntPatch_WLine)::DownCast (slinj)->Replace(k,ptdeb);
1309                 }
1310                 else {
1311                   Handle(IntPatch_RLine)::DownCast (slinj)->Replace(k,ptdeb);
1312                 }
1313                 ptdeb.SetParameter(paramf);
1314                 rline->AddVertex(ptdeb);
1315                 if (!procf){
1316                   procf=Standard_True;
1317                   rline->SetFirstPoint(rline->NbVertex());
1318                 }
1319               }
1320             }
1321             if (dolast) {
1322               if(dofirst) { //-- on recharge le ptdeb
1323                 if (typ == IntPatch_Walking) {
1324                   ptdeb = Handle(IntPatch_WLine)::DownCast (slinj)->Vertex(k);
1325                 }
1326                 else {
1327                   ptdeb = Handle(IntPatch_RLine)::DownCast (slinj)->Vertex(k);
1328                 }
1329               }
1330               if (ptdeb.Value().Distance(PStartl.Value()) <=TolArc) {
1331                 ptdeb.SetMultiple(Standard_True);
1332                 if (typ == IntPatch_Walking) {
1333                   Handle(IntPatch_WLine)::DownCast (slinj)->Replace(k,ptdeb);
1334                 }
1335                 else {
1336                   Handle(IntPatch_RLine)::DownCast (slinj)->Replace(k,ptdeb);
1337                 }
1338                 ptdeb.SetParameter(paraml);
1339                 rline->AddVertex(ptdeb);
1340                 if (!procl){
1341                   procl=Standard_True;
1342                   rline->SetLastPoint(rline->NbVertex());
1343                 }
1344               }
1345             }
1346           }
1347         }
1348       }
1349       slin.Append(rline);
1350     }
1351   }// if (NbSegm) 
1352   //
1353   // on traite les restrictions de la surface implicite
1354
1355   for (Standard_Integer i=1, aNbLin = slin.Length(); i<=aNbLin; i++)
1356   {
1357     Handle(IntPatch_Line)& aL = slin(i);
1358     
1359     if (!reversed)
1360       IntPatch_RstInt::PutVertexOnLine(aL,Surf1,D1,Surf2,Standard_True,TolTang);
1361     else
1362       IntPatch_RstInt::PutVertexOnLine(aL,Surf2,D2,Surf1,Standard_False,TolTang);
1363
1364     if(aL->ArcType() == IntPatch_Walking)
1365     {
1366       const Handle(IntPatch_WLine) aWL = Handle(IntPatch_WLine)::DownCast(aL);
1367       slin.Append(aWL);
1368       slin.Remove(i);
1369       i--;
1370       aNbLin--;
1371     }
1372   }
1373
1374   // Now slin is filled as follows: lower indices correspond to Restriction line,
1375   // after (higher indices) - only Walking-line.
1376
1377   const Standard_Real aUPeriodOfSurf1 = Surf1->IsUPeriodic() ? Surf1->UPeriod() : 0.0,
1378                       aUPeriodOfSurf2 = Surf2->IsUPeriodic() ? Surf2->UPeriod() : 0.0,
1379                       aVPeriodOfSurf1 = Surf1->IsVPeriodic() ? Surf1->VPeriod() : 0.0,
1380                       aVPeriodOfSurf2 = Surf2->IsVPeriodic() ? Surf2->VPeriod() : 0.0;
1381
1382   for (Standard_Integer i = 1; i <= slin.Length(); i++)
1383   {
1384     //BndBox of the points in Restriction line
1385     Bnd_Box2d aBRL;
1386     for(Standard_Integer j = i + 1; j <= slin.Length(); j++)
1387     {
1388       Handle(IntPatch_PointLine) aL1 = Handle(IntPatch_PointLine)::DownCast(slin(i));
1389       Handle(IntPatch_PointLine) aL2 = Handle(IntPatch_PointLine)::DownCast(slin(j));
1390
1391       Handle(IntPatch_RLine) aRL1 = Handle(IntPatch_RLine)::DownCast(aL1);
1392       Handle(IntPatch_RLine) aRL2 = Handle(IntPatch_RLine)::DownCast(aL2);
1393
1394       if(aRL1.IsNull())
1395       {//If Walking-Walking
1396         continue;
1397       }
1398
1399       //Here aL1 (i-th line) is Restriction-line and aL2 (j-th line) is
1400       //Restriction or Walking
1401
1402       if(aBRL.IsVoid())
1403       {//Fill aBRL
1404         for(Standard_Integer aPRID = 1; aPRID <= aRL1->NbPnts(); aPRID++)
1405         {
1406           Standard_Real u = 0.0, v = 0.0;
1407           if(reversed)
1408             aRL1->Point(aPRID).ParametersOnS1(u, v);
1409           else
1410             aRL1->Point(aPRID).ParametersOnS2(u, v);
1411
1412           aBRL.Add(gp_Pnt2d(u, v));
1413         }
1414
1415         Standard_Real aXmin = 0.0, aYmin = 0.0, aXMax = 0.0, aYMax = 0.0;
1416         aBRL.Get(aXmin, aYmin, aXMax, aYMax);
1417         const Standard_Real aDX = aXMax - aXmin,
1418                             aDY = aYMax - aYmin;
1419
1420         const Standard_Real aTolU = reversed? Surf1->UResolution(TolArc) : Surf2->UResolution(TolArc);
1421         const Standard_Real aTolV = reversed? Surf1->VResolution(TolArc) : Surf2->VResolution(TolArc);
1422
1423         if((aDX > aTolU) && (aDY > aTolV))
1424         {//Delete restriction line because it is not isoline.
1425           slin.Remove(i);
1426           i--;
1427           break;
1428         }
1429
1430         aXmin -= aTolU;
1431         aXMax += aTolU;
1432         aYmin -= aTolV;
1433         aYMax += aTolV;
1434         aBRL.SetVoid();
1435         aBRL.Update(aXmin, aYmin, aXMax, aYMax);
1436       }
1437
1438       const Standard_Boolean isCoincide = IsIn2DBox(aBRL, aL2,
1439                         (reversed? aUPeriodOfSurf1 : aUPeriodOfSurf2),
1440                         (reversed? aVPeriodOfSurf1 : aVPeriodOfSurf2), reversed);
1441
1442       if(isCoincide)
1443       {//Delete Walking-line
1444         slin.Remove(j);
1445         j--;
1446       }
1447     }
1448   }
1449
1450   empt = (slin.Length() == 0 && spnt.Length() == 0);
1451   done = Standard_True;
1452
1453
1454   if(slin.Length() == 0)
1455     return;
1456
1457   Standard_Boolean isDecomposeRequired =  (Quad.TypeQuadric() == GeomAbs_Cone) || 
1458                                           (Quad.TypeQuadric() == GeomAbs_Sphere);
1459
1460   if(!isDecomposeRequired)
1461     return;
1462
1463   // post processing for cones and spheres
1464
1465   const Handle(Adaptor3d_TopolTool)& PDomain = (reversed) ? D1 : D2;
1466   const Handle(Adaptor3d_HSurface)& aQSurf = (reversed) ? Surf2 : Surf1;
1467
1468   IntPatch_SequenceOfLine dslin;
1469   Standard_Boolean isDecompose = Standard_False;
1470   for(Standard_Integer i = 1; i <= slin.Length(); i++ )
1471   {
1472     if(DecomposeResult(slin(i),reversed,Quad,PDomain,aQSurf,TolArc,dslin))
1473     {
1474       isDecompose = Standard_True;
1475     }
1476   }
1477
1478   if(!isDecompose)
1479     return;
1480
1481   slin.Clear();
1482   for(Standard_Integer i = 1; i <= dslin.Length(); i++ )
1483     slin.Append(dslin(i));
1484 }
1485
1486 // correct U parameter of the start point of line on Quadric
1487 // (change 0->2PI or vs, if necessary)
1488 static Standard_Real AdjustUFirst(Standard_Real U1,Standard_Real U2)
1489 {
1490   Standard_Real u = U1;
1491
1492   // case: no adjustment
1493   if( U1 > 0. && U1 < (2.*M_PI) )
1494     return u;
1495
1496   // case: near '0'
1497   if( U1 == 0. || fabs(U1) <= 1.e-9 ) {
1498     if( U2 > 0. && U2 < (2.*M_PI) )
1499       u = ( U2 < ((2.*M_PI)-U2) ) ? 0. : (2.*M_PI);
1500     else {
1501       Standard_Real uu = U2;
1502       if( U2 > (2.*M_PI) )
1503         while( uu > (2.*M_PI) )
1504           uu -= (2.*M_PI);
1505       else 
1506         while( uu < 0.)
1507           uu += (2.*M_PI);
1508
1509       u = ( uu < ((2.*M_PI)-uu) ) ? 0. : (2.*M_PI);
1510     }
1511   }
1512   // case: near '2PI'
1513   else if( U1 == (2.*M_PI) || fabs((2.*M_PI)-fabs(U1)) <= 1.e-9 ) {
1514     if( U2 > 0. && U2 < (2.*M_PI) )
1515       u = ( U2 < ((2.*M_PI)-U2) ) ? 0. : (2.*M_PI);
1516     else {
1517       Standard_Real uu = U2;
1518       if( U2 > (2.*M_PI) )
1519         while( uu > (2.*M_PI) )
1520           uu -= (2.*M_PI);
1521       else 
1522         while( uu < 0.)
1523           uu += (2.*M_PI);
1524
1525       u = ( uu < ((2.*M_PI)-uu) ) ? 0. : (2.*M_PI);
1526     }
1527   }
1528   // case: '<0. || >2PI'
1529   else {
1530     if(U1 < 0.)
1531       while(u < 0.)
1532         u += 2.*M_PI;
1533     if(U1 > (2.*M_PI))
1534       while(u > (2.*M_PI))
1535         u -= (2.*M_PI);
1536   }
1537
1538   return u;
1539 }
1540
1541 // collect vertices, reject equals
1542 static Handle(IntSurf_LineOn2S) GetVertices(const Handle(IntPatch_WLine)& WLine,
1543                                             const Standard_Real           TOL3D,
1544                                             const Standard_Real           TOL2D)
1545 {
1546   //  Standard_Real TOL3D = 1.e-12, TOL2D = 1.e-8;
1547
1548   Handle(IntSurf_LineOn2S) vertices = new IntSurf_LineOn2S();
1549
1550   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0.;
1551   Standard_Integer i = 0, k = 0;
1552   Standard_Integer NbVrt = WLine->NbVertex();
1553
1554   TColStd_Array1OfInteger anVrts(1,NbVrt);
1555   anVrts.Init(0);
1556
1557   // check equal vertices
1558   for(i = 1; i <= NbVrt; i++) {
1559
1560     if( anVrts(i) == -1 ) continue;
1561
1562     const IntPatch_Point& Pi = WLine->Vertex(i);
1563
1564     for(k = (i+1); k <= NbVrt; k++) {
1565
1566       if( anVrts(k) == -1 ) continue;
1567
1568       const IntPatch_Point& Pk = WLine->Vertex(k);
1569
1570       if(Pi.Value().Distance(Pk.Value()) <= TOL3D) {
1571         // suggest the points are equal;
1572         // test 2d parameters on surface
1573         Standard_Boolean sameU1 = Standard_False;
1574         Standard_Boolean sameV1 = Standard_False;
1575         Standard_Boolean sameU2 = Standard_False;
1576         Standard_Boolean sameV2 = Standard_False;
1577
1578         Pi.ParametersOnS1(U1,V1);
1579         Pk.ParametersOnS1(U2,V2);
1580         if(fabs(U1-U2) <= TOL2D) sameU1 = Standard_True;
1581         if(fabs(V1-V2) <= TOL2D) sameV1 = Standard_True;
1582
1583         Pi.ParametersOnS2(U1,V1);
1584         Pk.ParametersOnS2(U2,V2);
1585         if(fabs(U1-U2) <= TOL2D) sameU2 = Standard_True;
1586         if(fabs(V1-V2) <= TOL2D) sameV2 = Standard_True;
1587
1588         if((sameU1 && sameV1) && (sameU2 && sameV2))
1589           anVrts(k) = -1;
1590       }
1591     }
1592   }
1593
1594   // copy further processed vertices
1595   for(i = 1; i <= NbVrt; i++) {
1596     if( anVrts(i) == -1 ) continue;
1597     vertices->Add(WLine->Vertex(i).PntOn2S());
1598   }
1599   return vertices;
1600 }
1601
1602 static Standard_Boolean AreSamePoints(const IntSurf_PntOn2S& P1,
1603   const IntSurf_PntOn2S& P2)
1604 {
1605   Standard_Boolean result = Standard_False;
1606   Standard_Real T2D = 1.e-9, T3D = 1.e-8;
1607   const gp_Pnt& P3D1 = P1.Value();
1608   const gp_Pnt& P3D2 = P2.Value();
1609   if(P3D1.Distance(P3D2) <= T3D) {
1610     Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0., U3 = 0., V3 = 0., U4 = 0., V4 = 0.;
1611     P1.ParametersOnS1(U1,V1);
1612     P1.ParametersOnS2(U2,V2);
1613     P2.ParametersOnS1(U3,V3);
1614     P2.ParametersOnS2(U4,V4);
1615     gp_Pnt2d P2D11(U1,V1);
1616     gp_Pnt2d P2D12(U2,V2);
1617     gp_Pnt2d P2D21(U3,V3);
1618     gp_Pnt2d P2D22(U4,V4);
1619     Standard_Boolean sameS1 = (P2D11.Distance(P2D21) <= T2D) ? Standard_True : Standard_False;
1620     Standard_Boolean sameS2 = (P2D12.Distance(P2D22) <= T2D) ? Standard_True : Standard_False;
1621     if(sameS1 && sameS2)
1622       result = Standard_True;
1623   }
1624   return result;
1625 }
1626
1627 static void ForcedPurgePoints(const Handle(IntSurf_LineOn2S)& Result,
1628                               const Standard_Boolean          IsReversed,
1629                               const IntSurf_Quadric&          Quad)
1630 {
1631   if(Result->NbPoints() <= 30) return;
1632   Standard_Integer Index = 0, IndexLimF = 8, IndexLimL = 8;
1633
1634   Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
1635   if(IsReversed) {
1636     Result->Value(1).ParametersOnS2(U1,V1);
1637     Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
1638   }
1639   else {
1640     Result->Value(1).ParametersOnS1(U1,V1);   
1641     Result->Value(Result->NbPoints()).ParametersOnS1(U2,V2);
1642   }
1643
1644   if(Quad.TypeQuadric() == GeomAbs_Cone) {
1645     Standard_Real Uapx = 0., Vapx = 0.;
1646     Quad.Parameters(Quad.Cone().Apex(),Uapx,Vapx);
1647
1648     if(fabs(V1-Vapx) <= 1.e-3)
1649       IndexLimF = 12;
1650     if(fabs(V2-Vapx) <= 1.e-3)
1651       IndexLimL = 12;
1652   }
1653
1654   if(Quad.TypeQuadric() == GeomAbs_Sphere) {
1655     Standard_Real Vapx1 = M_PI/2., Vapx2 = -M_PI/2.;
1656
1657     if(fabs(V1-Vapx1) <= 1.e-3 || fabs(V1-Vapx2) <= 1.e-3)
1658       IndexLimF = 12;
1659     if(fabs(V2-Vapx1) <= 1.e-3 || fabs(V2-Vapx2) <= 1.e-3)
1660       IndexLimL = 12;
1661   }
1662
1663   while(Result->NbPoints() > 2 && Index < IndexLimF) {
1664     Result->RemovePoint(2);
1665     Index++;
1666   }
1667   Index = 0;
1668   while(Result->NbPoints() > 2 && Index < IndexLimL) {
1669     Result->RemovePoint(Result->NbPoints()-1);
1670     Index++;
1671   }
1672 }
1673
1674 // DEBUG FUNCTION !!!
1675 #if 0
1676 static void DumpLine(Handle(IntSurf_LineOn2S)& Line,
1677   Standard_Boolean          IsReversed,
1678   Standard_Integer          Number)
1679 {
1680   cout << "DUMP LINE" << endl;
1681   Standard_Integer i;
1682   Standard_Real U,V;
1683   for(i = 1; i <= Line->NbPoints(); i++) {
1684     if(i <= Number || i >= (Line->NbPoints()-Number)) {
1685       if(IsReversed)
1686         Line->Value(i).ParametersOnS2(U,V); // S2 - quadric
1687       else
1688         Line->Value(i).ParametersOnS1(U,V); // S1 - quadric
1689       cout << "point p" << i << " " << U << " " << V << endl;
1690     }
1691   }
1692   cout << endl;
1693 }
1694 #endif
1695 // DEBUG FUNCTION !!!
1696
1697 static void SearchVertices(const Handle(IntSurf_LineOn2S)& Line,
1698   const Handle(IntSurf_LineOn2S)& Vertices,
1699   TColStd_Array1OfInteger&        PTypes)
1700 {
1701   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
1702   Standard_Integer ip = 0, iv = 0;
1703   for(ip = 1; ip <= nbp; ip++) {
1704     const IntSurf_PntOn2S& aP = Line->Value(ip);
1705     Standard_Integer type = 0;
1706     for(iv = 1; iv <= nbv; iv++) {
1707       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
1708       if(AreSamePoints(aP,aV)) {
1709         type = iv; 
1710         break;
1711       }
1712     }
1713     PTypes(ip) = type;
1714   }
1715 }
1716
1717 static inline Standard_Boolean IsSeamParameter(const Standard_Real U,
1718   const Standard_Real TOL2D)
1719 {
1720   return (fabs(U) <= TOL2D || fabs(2.*M_PI - U) <= TOL2D);
1721 }
1722
1723 static inline Standard_Real AdjustU(const Standard_Real U)
1724 {
1725   Standard_Real u = U, DBLPI = 2.*M_PI;
1726   if(u < 0. || u > DBLPI) {
1727     if(u < 0.)
1728       while(u < 0.)
1729         u += DBLPI;
1730     else
1731       while(u > DBLPI)
1732         u -= DBLPI;
1733   }
1734   return u;
1735 }
1736
1737 static inline void Correct2DBounds(const Standard_Real UF,
1738   const Standard_Real UL,
1739   const Standard_Real VF,
1740   const Standard_Real VL,
1741   const Standard_Real TOL2D,
1742   Standard_Real&      U,
1743   Standard_Real&      V)
1744 {
1745   Standard_Real Eps = 1.e-16;
1746   Standard_Real dUF = fabs(U - UF);
1747   Standard_Real dUL = fabs(U - UL);
1748   Standard_Real dVF = fabs(V - VF);
1749   Standard_Real dVL = fabs(V - VL);
1750   if(dUF <= TOL2D && dUF > Eps) U = UF;
1751   if(dUL <= TOL2D && dUL > Eps) U = UL;
1752   if(dVF <= TOL2D && dVF > Eps) V = VF;
1753   if(dVL <= TOL2D && dVL > Eps) V = VL;
1754 }
1755
1756 static void AdjustLine(Handle(IntSurf_LineOn2S)& Line,
1757   const Standard_Boolean    IsReversed,
1758   const Handle(Adaptor3d_HSurface)&         QSurf,
1759   const Standard_Real       TOL2D)
1760 {
1761   Standard_Real VF = QSurf->FirstVParameter();
1762   Standard_Real VL = QSurf->LastVParameter();
1763   Standard_Real UF = QSurf->FirstUParameter();
1764   Standard_Real UL = QSurf->LastUParameter();
1765
1766   Standard_Integer nbp = Line->NbPoints(), ip = 0;
1767   Standard_Real U = 0., V = 0.;
1768   for(ip = 1; ip <= nbp; ip++) {
1769     if(IsReversed) {
1770       Line->Value(ip).ParametersOnS2(U,V);
1771       U = AdjustU(U);
1772       Correct2DBounds(UF,UL,VF,VL,TOL2D,U,V);
1773       Line->SetUV(ip,Standard_False,U,V);
1774     }
1775     else {
1776       Line->Value(ip).ParametersOnS1(U,V);
1777       U = AdjustU(U);
1778       Correct2DBounds(UF,UL,VF,VL,TOL2D,U,V);
1779       Line->SetUV(ip,Standard_True,U,V);
1780     }
1781   }
1782 }
1783
1784 static Standard_Boolean InsertSeamVertices(Handle(IntSurf_LineOn2S)&       Line,
1785   const Standard_Boolean          IsReversed,
1786   Handle(IntSurf_LineOn2S)&       Vertices,
1787   const TColStd_Array1OfInteger&  PTypes,
1788   const Standard_Real             TOL2D)
1789 {
1790   Standard_Boolean result = Standard_False;
1791   Standard_Integer ip = 0, nbp = Line->NbPoints();
1792   Standard_Real U = 0., V = 0.;
1793   for(ip = 1; ip <= nbp; ip++) {
1794     Standard_Integer ipt = PTypes(ip);
1795     if(ipt != 0) {
1796       const IntSurf_PntOn2S& aP = Line->Value(ip);
1797       if(IsReversed)
1798         aP.ParametersOnS2(U,V); // S2 - quadric
1799       else
1800         aP.ParametersOnS1(U,V); // S1 - quadric
1801       U = AdjustU(U);
1802       if(IsSeamParameter(U,TOL2D)) {
1803         if(ip == 1 || ip == nbp) {
1804           Standard_Real U1 = 0., V1 = 0.;
1805           Standard_Integer ipp = (ip == 1) ? (ip+1) : (ip-1);
1806           if(IsReversed)
1807             Line->Value(ipp).ParametersOnS2(U1,V1); // S2 - quadric
1808           else
1809             Line->Value(ipp).ParametersOnS1(U1,V1); // S1 - quadric
1810           Standard_Real u = AdjustUFirst(U,U1);
1811           if(fabs(u-U) >= 1.5*M_PI) {
1812             Standard_Real U2 = 0., V2 = 0.;
1813             if(IsReversed) {
1814               Line->Value(ip).ParametersOnS1(U2,V2); // prm
1815               Line->SetUV(ip,Standard_False,u,V);
1816               Line->SetUV(ip,Standard_True,U2,V2);
1817             }
1818             else {
1819               Line->Value(ip).ParametersOnS2(U2,V2); // prm
1820               Line->SetUV(ip,Standard_True,u,V);
1821               Line->SetUV(ip,Standard_False,U2,V2);
1822             }
1823           }
1824         }
1825         else {
1826           Standard_Integer ipp = ip - 1;
1827           Standard_Integer ipn = ip + 1;
1828           Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
1829           if(IsReversed) {
1830             Line->Value(ipp).ParametersOnS2(U1,V1); // quad
1831             Line->Value(ipn).ParametersOnS2(U2,V2); // quad
1832           }
1833           else {
1834             Line->Value(ipp).ParametersOnS1(U1,V1); // quad
1835             Line->Value(ipn).ParametersOnS1(U2,V2); // quad
1836           }
1837           U1 = AdjustU(U1);
1838           U2 = AdjustU(U2);
1839           Standard_Boolean pnearZero = (fabs(U1) < fabs(2.*M_PI-U1)) ? Standard_True : Standard_False;
1840           Standard_Boolean cnearZero = (fabs(U) < fabs(2.*M_PI-U)) ? Standard_True : Standard_False;
1841           if(pnearZero == cnearZero) {
1842             if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
1843               Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
1844               IntSurf_PntOn2S nP;
1845               nP.SetValue(aP.Value());
1846               Standard_Real U3 = 0., V3 = 0.;
1847               if(IsReversed) {
1848                 Line->Value(ip).ParametersOnS1(U3,V3); // prm
1849                 nP.SetValue(Standard_False,nU,V);
1850                 nP.SetValue(Standard_True,U3,V3);
1851               }
1852               else {
1853                 Line->Value(ip).ParametersOnS2(U3,V3); // prm
1854                 nP.SetValue(Standard_True,nU,V);
1855                 nP.SetValue(Standard_False,U3,V3);
1856               }
1857               Line->InsertBefore(ipn,nP);
1858               Vertices->Add(nP);
1859               result = Standard_True;
1860               break;
1861             }
1862           }
1863           else {
1864             if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
1865               Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
1866               IntSurf_PntOn2S nP;
1867               nP.SetValue(aP.Value());
1868               Standard_Real U3 = 0., V3 = 0.;
1869               if(IsReversed) {
1870                 Line->Value(ip).ParametersOnS1(U3,V3); // prm
1871                 nP.SetValue(Standard_False,nU,V);
1872                 nP.SetValue(Standard_True,U3,V3);
1873               }
1874               else {
1875                 Line->Value(ip).ParametersOnS2(U3,V3); // prm
1876                 nP.SetValue(Standard_True,nU,V);
1877                 nP.SetValue(Standard_False,U3,V3);
1878               }
1879               Line->InsertBefore(ip,nP);
1880               Vertices->Add(nP);
1881               result = Standard_True;
1882               break;
1883             }
1884             else {
1885               // Line->InsertBefore(ip,Line->Value(ipn));
1886               // Line->RemovePoint(ip+2);
1887               // result = Standard_True;
1888               // cout << "swap vertex " << endl;
1889               // break;
1890             }
1891           }
1892         }
1893       }
1894     }
1895   }
1896   return result;
1897 }
1898
1899 static void ToSmooth( const Handle(IntSurf_LineOn2S)& Line,
1900                       const Standard_Boolean          IsReversed,
1901                       const IntSurf_Quadric&          Quad,
1902                       const Standard_Boolean          IsFirst,
1903                       Standard_Real&                  D3D)
1904 {
1905   if(Line->NbPoints() <= 10)
1906     return;
1907
1908   D3D = 0.;
1909   Standard_Integer NbTestPnts = Line->NbPoints() / 5;
1910   if(NbTestPnts < 5) NbTestPnts = 5;
1911
1912   Standard_Integer startp = (IsFirst) ? 2 : (Line->NbPoints() - NbTestPnts - 2);
1913   Standard_Integer ip = 0;
1914   Standard_Real Uc = 0., Vc = 0., Un = 0., Vn = 0., DDU = 0., DDV = 0.;
1915
1916   for(ip = startp; ip <= NbTestPnts; ip++) {
1917     if(IsReversed) {
1918       Line->Value(ip).ParametersOnS2(Uc,Vc); // S2 - quadric
1919       Line->Value(ip+1).ParametersOnS2(Un,Vn);
1920     }
1921     else {
1922       Line->Value(ip).ParametersOnS1(Uc,Vc); // S1 - quadric
1923       Line->Value(ip+1).ParametersOnS1(Un,Vn);
1924     }
1925     DDU += fabs(fabs(Uc)-fabs(Un));
1926     DDV += fabs(fabs(Vc)-fabs(Vn));
1927
1928     if(ip > startp) {
1929       Standard_Real DP = Line->Value(ip).Value().Distance(Line->Value(ip-1).Value());
1930       D3D += DP;
1931     }
1932   }
1933
1934   DDU /= (Standard_Real) NbTestPnts + 1;
1935   DDV /= (Standard_Real) NbTestPnts + 1;
1936
1937   D3D /= (Standard_Real) NbTestPnts + 1;
1938
1939
1940   Standard_Integer Index1 = (IsFirst) ? 1 : (Line->NbPoints());
1941   Standard_Integer Index2 = (IsFirst) ? 2 : (Line->NbPoints()-1);
1942   Standard_Integer Index3 = (IsFirst) ? 3 : (Line->NbPoints()-2);
1943
1944   Standard_Boolean doU = Standard_False;
1945
1946   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0., U3 = 0., V3 = 0.;
1947
1948   if(IsReversed) {
1949     Line->Value(Index1).ParametersOnS2(U1,V1); // S2 - quadric
1950     Line->Value(Index2).ParametersOnS2(U2,V2);
1951     Line->Value(Index3).ParametersOnS2(U3,V3);
1952   }
1953   else {
1954     Line->Value(Index1).ParametersOnS1(U1,V1); // S1 - quadric
1955     Line->Value(Index2).ParametersOnS1(U2,V2);
1956     Line->Value(Index3).ParametersOnS1(U3,V3);
1957   }
1958
1959   if(!doU && Quad.TypeQuadric() == GeomAbs_Sphere) {
1960     if(fabs(fabs(U1)-fabs(U2)) > (M_PI/16.)) doU = Standard_True;
1961
1962     if(doU && (fabs(U1) <= 1.e-9 || fabs(U1-2.*M_PI) <= 1.e-9)) {
1963       if(fabs(V1-M_PI/2.) <= 1.e-9 || fabs(V1+M_PI/2.) <= 1.e-9) {}
1964       else {
1965         doU = Standard_False;
1966       }
1967     }
1968   }
1969
1970   if(Quad.TypeQuadric() == GeomAbs_Cone) {
1971     Standard_Real Uapx = 0., Vapx = 0.;
1972     Quad.Parameters(Quad.Cone().Apex(),Uapx,Vapx);
1973
1974     if(fabs(fabs(U1)-fabs(U2)) > M_PI/32.) doU = Standard_True;
1975
1976     if(doU && (fabs(U1) <= 1.e-9 || fabs(U1-2.*M_PI) <= 1.e-9)) {
1977       if(fabs(V1-Vapx) <= 1.e-9) {}
1978       else {
1979         doU = Standard_False;
1980       }
1981     }
1982   }
1983
1984   if(doU) {
1985     Standard_Real dU = Min((DDU/10.),5.e-8);
1986     Standard_Real U = (U2 > U3) ? (U2 + dU) : (U2 - dU);
1987     if(IsReversed)
1988       Line->SetUV(Index1,Standard_False,U,V1);
1989     else
1990       Line->SetUV(Index1,Standard_True,U,V1);
1991     U1 = U;
1992   }
1993
1994
1995 static Standard_Boolean TestMiddleOnPrm(const IntSurf_PntOn2S& aP,
1996                                         const IntSurf_PntOn2S& aV,
1997                                         const Standard_Boolean IsReversed,
1998                                         const Standard_Real    ArcTol,
1999                                         const Handle(Adaptor3d_TopolTool)&  PDomain)
2000
2001 {
2002   Standard_Boolean result = Standard_False;
2003   Standard_Real Up = 0., Vp = 0., Uv = 0., Vv = 0.;
2004   if(IsReversed) {
2005     aP.ParametersOnS1(Up,Vp); //S1 - parametric
2006     aV.ParametersOnS1(Uv,Vv);
2007   }
2008   else {
2009     aP.ParametersOnS2(Up,Vp); // S2 - parametric
2010     aV.ParametersOnS2(Uv,Vv);
2011   }
2012   Standard_Real Um = (Up + Uv)*0.5, Vm = (Vp + Vv)*0.5;
2013   gp_Pnt2d a2DPntM(Um,Vm);
2014   TopAbs_State PosM = PDomain->Classify(a2DPntM,ArcTol);
2015   if(PosM == TopAbs_ON || PosM == TopAbs_IN )
2016     result = Standard_True;
2017   return result;
2018 }
2019
2020 static void VerifyVertices( const Handle(IntSurf_LineOn2S)&    Line,
2021                             const Standard_Boolean             IsReversed,
2022                             const Handle(IntSurf_LineOn2S)&    Vertices,
2023                             const Standard_Real                TOL2D,
2024                             const Standard_Real       ArcTol,
2025                             const Handle(Adaptor3d_TopolTool)& PDomain,
2026                             IntSurf_PntOn2S&          VrtF,
2027                             Standard_Boolean&         AddFirst,
2028                             IntSurf_PntOn2S&          VrtL,
2029                             Standard_Boolean&         AddLast)
2030 {
2031   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
2032   Standard_Integer FIndexSame = 0, FIndexNear = 0, LIndexSame = 0, LIndexNear = 0;
2033   const IntSurf_PntOn2S& aPF = Line->Value(1);
2034   const IntSurf_PntOn2S& aPL = Line->Value(nbp);
2035   Standard_Real UF = 0., VF = 0., UL = 0., VL = 0.;
2036   if(IsReversed) {
2037     aPF.ParametersOnS2(UF,VF);
2038     aPL.ParametersOnS2(UL,VL);
2039   }
2040   else {
2041     aPF.ParametersOnS1(UF,VF);
2042     aPL.ParametersOnS1(UL,VL);
2043   }
2044   gp_Pnt2d a2DPF(UF,VF);
2045   gp_Pnt2d a2DPL(UL,VL);
2046   Standard_Real DistMinF = 1.e+100, DistMinL = 1.e+100;
2047   Standard_Integer FConjugated = 0, LConjugated = 0;
2048
2049   Standard_Integer iv = 0;
2050
2051   for(iv = 1; iv <= nbv; iv++) {
2052     Standard_Real Uv = 0., Vv = 0.;
2053     if(IsReversed) {
2054       Vertices->Value(iv).ParametersOnS2(Uv,Vv);
2055       Uv = AdjustU(Uv);
2056       Vertices->SetUV(iv,Standard_False,Uv,Vv);
2057     }
2058     else {
2059       Vertices->Value(iv).ParametersOnS1(Uv,Vv);
2060       Uv = AdjustU(Uv);
2061       Vertices->SetUV(iv,Standard_True,Uv,Vv);
2062     }
2063   }
2064
2065   for(iv = 1; iv <= nbv; iv++) {
2066     const IntSurf_PntOn2S& aV = Vertices->Value(iv);
2067     if(AreSamePoints(aPF,aV)) {
2068       FIndexSame = iv;
2069       break;
2070     }
2071     else {
2072       Standard_Real Uv = 0., Vv = 0.;
2073       if(IsReversed)
2074         aV.ParametersOnS2(Uv,Vv);
2075       else
2076         aV.ParametersOnS1(Uv,Vv);
2077       gp_Pnt2d a2DV(Uv,Vv);
2078       Standard_Real Dist = a2DV.Distance(a2DPF);
2079       if(Dist < DistMinF) {
2080         DistMinF = Dist;
2081         FIndexNear = iv;
2082         if(FConjugated != 0)
2083           FConjugated = 0;
2084       }
2085       if(IsSeamParameter(Uv,TOL2D)) {
2086         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2087         gp_Pnt2d a2DCV(Ucv,Vv);
2088         Standard_Real CDist = a2DCV.Distance(a2DPF);
2089         if(CDist < DistMinF) {
2090           DistMinF = CDist;
2091           FConjugated = iv;
2092           FIndexNear = iv;
2093         }
2094       }
2095     }
2096   }
2097
2098   for(iv = 1; iv <= nbv; iv++) {
2099     const IntSurf_PntOn2S& aV = Vertices->Value(iv);
2100     if(AreSamePoints(aPL,aV)) {
2101       LIndexSame = iv;
2102       break;
2103     }
2104     else {
2105       Standard_Real Uv = 0., Vv = 0.;
2106       if(IsReversed)
2107         aV.ParametersOnS2(Uv,Vv);
2108       else
2109         aV.ParametersOnS1(Uv,Vv);
2110       gp_Pnt2d a2DV(Uv,Vv);
2111       Standard_Real Dist = a2DV.Distance(a2DPL);
2112       if(Dist < DistMinL) {
2113         DistMinL = Dist;
2114         LIndexNear = iv;
2115         if(LConjugated != 0)
2116           LConjugated = 0;
2117       }
2118       if(IsSeamParameter(Uv,TOL2D)) {
2119         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2120         gp_Pnt2d a2DCV(Ucv,Vv);
2121         Standard_Real CDist = a2DCV.Distance(a2DPL);
2122         if(CDist < DistMinL) {
2123           DistMinL = CDist;
2124           LConjugated = iv;
2125           LIndexNear = iv;
2126         }
2127       }
2128     }
2129   }
2130
2131   AddFirst = Standard_False;
2132   AddLast  = Standard_False;
2133
2134   if(FIndexSame == 0) {
2135     if(FIndexNear != 0) {
2136       const IntSurf_PntOn2S& aV = Vertices->Value(FIndexNear);
2137       Standard_Real Uv = 0., Vv = 0.;
2138       if(IsReversed)
2139         aV.ParametersOnS2(Uv,Vv);
2140       else
2141         aV.ParametersOnS1(Uv,Vv);
2142       if(IsSeamParameter(Uv,TOL2D)) {
2143         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2144         Standard_Boolean test = TestMiddleOnPrm(aPF,aV,IsReversed,ArcTol,PDomain);
2145         if(test) {
2146           VrtF.SetValue(aV.Value());
2147           if(IsReversed) {
2148             Standard_Real U2 = 0., V2 = 0.;
2149             aV.ParametersOnS1(U2,V2); // S1 - prm
2150             VrtF.SetValue(Standard_True,U2,V2);
2151             if(FConjugated == 0)
2152               VrtF.SetValue(Standard_False,Uv,Vv);
2153             else
2154               VrtF.SetValue(Standard_False,Ucv,Vv);
2155           }
2156           else {
2157             Standard_Real U2 = 0., V2 = 0.;
2158             aV.ParametersOnS2(U2,V2); // S2 - prm
2159             VrtF.SetValue(Standard_False,U2,V2);
2160             if(FConjugated == 0)
2161               VrtF.SetValue(Standard_True,Uv,Vv);
2162             else
2163               VrtF.SetValue(Standard_True,Ucv,Vv);
2164           }
2165           Standard_Real Dist3D = VrtF.Value().Distance(aPF.Value());
2166           if(Dist3D > 1.5e-7 && DistMinF > TOL2D) {
2167             AddFirst = Standard_True;
2168           }
2169         }
2170       }
2171       else {
2172         // to do: analyze internal vertex
2173       }
2174     }
2175   }
2176
2177   if(LIndexSame == 0) {
2178     if(LIndexNear != 0) {
2179       const IntSurf_PntOn2S& aV = Vertices->Value(LIndexNear);
2180       Standard_Real Uv = 0., Vv = 0.;
2181       if(IsReversed)
2182         aV.ParametersOnS2(Uv,Vv);
2183       else
2184         aV.ParametersOnS1(Uv,Vv);
2185       if(IsSeamParameter(Uv,TOL2D)) {
2186         Standard_Real Ucv = (fabs(Uv) < fabs(2.*M_PI-Uv)) ? (2.*M_PI) : 0.;
2187         Standard_Boolean test = TestMiddleOnPrm(aPL,aV,IsReversed,ArcTol,PDomain);
2188         if(test) {
2189           VrtL.SetValue(aV.Value());
2190           if(IsReversed) {
2191             Standard_Real U2 = 0., V2 = 0.;
2192             aV.ParametersOnS1(U2,V2); // S1 - prm
2193             VrtL.SetValue(Standard_True,U2,V2);
2194             if(LConjugated == 0)
2195               VrtL.SetValue(Standard_False,Uv,Vv);
2196             else
2197               VrtL.SetValue(Standard_False,Ucv,Vv);
2198           }
2199           else {
2200             Standard_Real U2 = 0., V2 = 0.;
2201             aV.ParametersOnS2(U2,V2); // S2 - prm
2202             VrtL.SetValue(Standard_False,U2,V2);
2203             if(LConjugated == 0)
2204               VrtL.SetValue(Standard_True,Uv,Vv);
2205             else
2206               VrtL.SetValue(Standard_True,Ucv,Vv);
2207           }
2208           Standard_Real Dist3D = VrtL.Value().Distance(aPL.Value());
2209           if(Dist3D > 1.5e-7 && DistMinL > TOL2D) {
2210             AddLast = Standard_True;
2211           }
2212         }
2213       }
2214       else {
2215         // to do: analyze internal vertex
2216       }
2217     }
2218   }
2219 }
2220
2221 static Standard_Boolean AddVertices(Handle(IntSurf_LineOn2S)& Line,
2222   const IntSurf_PntOn2S&    VrtF,
2223   const Standard_Boolean    AddFirst,
2224   const IntSurf_PntOn2S&    VrtL,
2225   const Standard_Boolean    AddLast,
2226   const Standard_Real       D3DF,
2227   const Standard_Real       D3DL)
2228 {
2229   Standard_Boolean result = Standard_False;
2230   if(AddFirst) {
2231     Standard_Real DF = Line->Value(1).Value().Distance(VrtF.Value());
2232     if((D3DF*2.) > DF && DF > 1.5e-7) {
2233       Line->InsertBefore(1,VrtF);
2234       result = Standard_True;
2235     }
2236   }
2237   if(AddLast) {
2238     Standard_Real DL = Line->Value(Line->NbPoints()).Value().Distance(VrtL.Value());
2239     if((D3DL*2.) > DL && DL > 1.5e-7) {
2240       Line->Add(VrtL);
2241       result = Standard_True;
2242     }
2243   }
2244   return result;
2245 }
2246
2247
2248 static void PutIntVertices(const Handle(IntPatch_Line)&    Line,
2249   Handle(IntSurf_LineOn2S)& Result,
2250   Standard_Boolean          ,//IsReversed,
2251   Handle(IntSurf_LineOn2S)& Vertices,
2252   const Standard_Real       ArcTol)
2253 {
2254   Standard_Integer nbp = Result->NbPoints(), nbv = Vertices->NbPoints();
2255
2256   if(nbp < 3)
2257     return;
2258
2259   Handle(IntPatch_WLine) WLine (Handle(IntPatch_WLine)::DownCast (Line));
2260   Standard_Integer ip = 0, iv = 0;
2261   gp_Pnt aPnt;
2262   IntPatch_Point thePnt;
2263   Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
2264
2265   for(ip = 2; ip <= (nbp-1); ip++) {
2266     const IntSurf_PntOn2S& aP = Result->Value(ip);
2267     for(iv = 1; iv <= nbv; iv++) {
2268       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
2269       if(AreSamePoints(aP,aV)) {
2270         aPnt = Result->Value(ip).Value();
2271         Result->Value(ip).ParametersOnS1(U1,V1);
2272         Result->Value(ip).ParametersOnS2(U2,V2);
2273         thePnt.SetValue(aPnt,ArcTol,Standard_False);
2274         thePnt.SetParameters(U1,V1,U2,V2);
2275         thePnt.SetParameter((Standard_Real)ip);
2276         WLine->AddVertex(thePnt);
2277       }
2278     }
2279   }
2280 }
2281
2282 static Standard_Boolean HasInternals(Handle(IntSurf_LineOn2S)& Line,
2283   Handle(IntSurf_LineOn2S)& Vertices)
2284 {
2285   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
2286   Standard_Integer ip = 0, iv = 0;
2287   Standard_Boolean result = Standard_False;
2288
2289   if(nbp < 3)
2290     return result;
2291
2292   for(ip = 2; ip <= (nbp-1); ip++) {
2293     const IntSurf_PntOn2S& aP = Line->Value(ip);
2294     for(iv = 1; iv <= nbv; iv++) {
2295       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
2296       if(AreSamePoints(aP,aV)) {
2297         result = Standard_True;
2298         break;
2299       }
2300     }
2301     if(result)
2302       break;
2303   }
2304
2305   return result;
2306 }
2307 static Handle(IntPatch_WLine) MakeSplitWLine (Handle(IntPatch_WLine)&        WLine,
2308   Standard_Boolean         Tang,
2309   IntSurf_TypeTrans        Trans1,
2310   IntSurf_TypeTrans        Trans2,
2311   Standard_Real            ArcTol,
2312   Standard_Integer         ParFirst,
2313   Standard_Integer         ParLast)
2314 {
2315   Handle(IntSurf_LineOn2S) SLine = WLine->Curve();
2316   Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
2317
2318   Standard_Integer ip = 0;
2319   for(ip = ParFirst; ip <= ParLast; ip++)
2320     sline->Add(SLine->Value(ip));
2321
2322   Handle(IntPatch_WLine) wline = new IntPatch_WLine(sline,Tang,Trans1,Trans2);
2323
2324   gp_Pnt aSPnt;
2325   IntPatch_Point TPntF,TPntL;
2326   Standard_Real uu1 = 0., vv1 = 0., uu2 = 0., vv2 = 0.;
2327
2328   aSPnt = sline->Value(1).Value();
2329   sline->Value(1).ParametersOnS1(uu1,vv1);
2330   sline->Value(1).ParametersOnS2(uu2,vv2);
2331   TPntF.SetValue(aSPnt,ArcTol,Standard_False);
2332   TPntF.SetParameters(uu1,vv1,uu2,vv2);
2333   TPntF.SetParameter(1.);
2334   wline->AddVertex(TPntF);
2335   wline->SetFirstPoint(1);
2336
2337   aSPnt =  sline->Value(sline->NbPoints()).Value();
2338   sline->Value(sline->NbPoints()).ParametersOnS1(uu1,vv1);
2339   sline->Value(sline->NbPoints()).ParametersOnS2(uu2,vv2);
2340   TPntL.SetValue(aSPnt,ArcTol,Standard_False);
2341   TPntL.SetParameters(uu1,vv1,uu2,vv2);
2342   TPntL.SetParameter((Standard_Real)sline->NbPoints());
2343   wline->AddVertex(TPntL);
2344   wline->SetLastPoint(sline->NbPoints());
2345
2346   return wline;
2347 }
2348
2349 static Standard_Boolean SplitOnSegments(Handle(IntPatch_WLine)&        WLine,
2350   Standard_Boolean         Tang,
2351   IntSurf_TypeTrans        Trans1,
2352   IntSurf_TypeTrans        Trans2,
2353   Standard_Real            ArcTol,
2354   IntPatch_SequenceOfLine& Segments)
2355 {
2356   Standard_Boolean result = Standard_False;
2357   Segments.Clear();
2358
2359   Standard_Integer nbv = WLine->NbVertex();
2360   if(nbv > 3) {
2361     Standard_Integer iv = 0;
2362     for(iv = 1; iv < nbv; iv++) {
2363       Standard_Integer firstPar = 
2364                     (Standard_Integer) WLine->Vertex(iv).ParameterOnLine();
2365       Standard_Integer lastPar  = 
2366                     (Standard_Integer) WLine->Vertex(iv+1).ParameterOnLine();
2367       if((lastPar - firstPar) <= 1)
2368         continue;
2369       else {
2370         Handle(IntPatch_WLine) splitwline = MakeSplitWLine(WLine,Tang,Trans1,Trans2,
2371                                                                 ArcTol,firstPar,lastPar);
2372         Segments.Append(splitwline);
2373         if(!result)
2374           result = Standard_True;
2375       }
2376     }
2377   }
2378   return result;
2379 }
2380
2381 static Standard_Boolean DecomposeResult(const Handle(IntPatch_Line)& theLine,
2382                                         const Standard_Boolean       IsReversed,
2383                                         const IntSurf_Quadric&       theQuad,
2384                                         const Handle(Adaptor3d_TopolTool)& thePDomain,
2385                                         const Handle(Adaptor3d_HSurface)&  theQSurf,
2386                                         const Standard_Real                theArcTol,
2387                                         IntPatch_SequenceOfLine&           theLines)
2388 {
2389   // Split <theLine> in the places where it passes through seam edge or singularity
2390   // (apex of cone or pole of sphere). This passage is detected by jump of U-parameter
2391   // from point to point.
2392   
2393   const Standard_Real aDeltaUmax = 0.5*M_PI;
2394   const Standard_Real aTOL3D = 1.e-10, 
2395                       aTOL2D = Precision::PConfusion(),
2396                       aTOL2DS = Precision::PConfusion();
2397
2398   if( theLine->ArcType() != IntPatch_Walking )
2399   {
2400     return Standard_False;
2401   }
2402
2403   Handle(IntPatch_WLine) aWLine (Handle(IntPatch_WLine)::DownCast (theLine));
2404   const Handle(IntSurf_LineOn2S)& aSLine = aWLine->Curve();
2405
2406   if(aSLine->NbPoints() <= 2)
2407   {
2408     return Standard_False;
2409   }
2410   
2411   //Deletes repeated vertices
2412   Handle(IntSurf_LineOn2S) aVLine = GetVertices(aWLine,aTOL3D,aTOL2D);
2413   
2414   Handle(IntSurf_LineOn2S) aSSLine(aSLine);
2415
2416   if(aSSLine->NbPoints() <= 1)
2417     return Standard_False;
2418
2419   AdjustLine(aSSLine,IsReversed,theQSurf,aTOL2D);
2420
2421   {
2422     Standard_Boolean isInserted = Standard_True;
2423     while(isInserted)
2424     {
2425       const Standard_Integer aNbPnts = aSSLine->NbPoints();
2426       TColStd_Array1OfInteger aPTypes(1,aNbPnts);
2427       SearchVertices(aSSLine,aVLine,aPTypes);
2428       isInserted = InsertSeamVertices(aSSLine,IsReversed,aVLine,aPTypes,aTOL2D);
2429     }
2430   }
2431
2432   const Standard_Integer aLindex = aSSLine->NbPoints();
2433   Standard_Integer aFindex = 1, aBindex = 0;
2434
2435   IntPatch_Point aTPntF, aTPntL;
2436
2437   // build WLine parts (if any)
2438   Standard_Boolean flNextLine = Standard_True;
2439   Standard_Boolean hasBeenDecomposed = Standard_False;
2440   Standard_Boolean PrePointExist = Standard_False;
2441   IntSurf_PntOn2S PrePoint;
2442   while(flNextLine)
2443   {
2444     // reset variables
2445     flNextLine = Standard_False;
2446     Standard_Boolean isDecomposited = Standard_False;
2447     Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0., AnU1 = 0.;
2448
2449     Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
2450
2451     //if((Lindex-Findex+1) <= 2 )
2452     if(aLindex <= aFindex)
2453       return hasBeenDecomposed;
2454
2455     if (PrePointExist)
2456     {
2457       sline->Add(PrePoint);
2458       PrePointExist = Standard_False;
2459     }
2460     
2461     // analyze other points
2462     for(Standard_Integer k = aFindex; k <= aLindex; k++)
2463     {
2464       if( k == aFindex )
2465       {
2466         if(IsReversed)
2467         {
2468           aSSLine->Value(k).ParametersOnS2(AnU1,V1);   // S2 - quadric, set U,V by Pnt3D
2469         }
2470         else
2471         {
2472           aSSLine->Value(k).ParametersOnS1(AnU1,V1);    // S1 - quadric, set U,V by Pnt3D
2473         }
2474
2475         sline->Add(aSSLine->Value(k));
2476         PrePoint = aSSLine->Value(k);
2477         continue;
2478       }
2479
2480       if(IsReversed)
2481       {
2482         aSSLine->Value(k).ParametersOnS2(U1,V1);   // S2 - quadric, set U,V by Pnt3D
2483       }
2484       else
2485       {
2486         aSSLine->Value(k).ParametersOnS1(U1,V1);    // S1 - quadric, set U,V by Pnt3D
2487       }
2488
2489       if(Abs(U1-AnU1) > aDeltaUmax)
2490       {
2491         aBindex = k;
2492         isDecomposited = Standard_True;
2493         ////
2494         if (Abs(U1) <= Precision::PConfusion() ||
2495             Abs(U1 - 2*M_PI) <= Precision::PConfusion())
2496         {
2497           IntSurf_PntOn2S NewPoint;
2498           IntSurf_PntOn2S CurPoint = aSSLine->Value(k);
2499           gp_Pnt thePnt = CurPoint.Value();
2500           Standard_Real theU1, theV1, theU2, theV2;
2501           theU1 = (Abs(U1) <= Precision::PConfusion())? 2*M_PI : 0.;
2502           theV1 = V1;
2503           NewPoint.SetValue(thePnt);
2504           if (!IsReversed)
2505           {
2506             CurPoint.ParametersOnS2(theU2, theV2);
2507             NewPoint.SetValue(theU1, theV1, theU2, theV2);
2508           }
2509           else
2510           {
2511             CurPoint.ParametersOnS1(theU2, theV2);
2512             NewPoint.SetValue(theU2, theV2, theU1, theV1);
2513           }
2514           sline->Add(NewPoint);
2515         }
2516         else if (Abs(AnU1) <= Precision::PConfusion() ||
2517                  Abs(AnU1 - 2*M_PI) <= Precision::PConfusion())
2518         {
2519           //Modify <PrePoint>
2520           PrePointExist = Standard_True;
2521           Standard_Real theU1, theV1;
2522           if (!IsReversed)
2523           {
2524             PrePoint.ParametersOnS1(theU1, theV1);
2525             theU1 = (Abs(AnU1) <= Precision::PConfusion())? 2*M_PI : 0.;
2526             PrePoint.SetValue(Standard_True, //on first
2527                               theU1, theV1);
2528           }
2529           else
2530           {
2531             PrePoint.ParametersOnS2(theU1, theV1);
2532             theU1 = (Abs(AnU1) <= Precision::PConfusion())? 2*M_PI : 0.;
2533             PrePoint.SetValue(Standard_False, //on second
2534                               theU1, theV1);
2535           }
2536         }
2537         ////
2538         break;
2539       }
2540
2541       sline->Add(aSSLine->Value(k));
2542       PrePoint = aSSLine->Value(k);
2543       AnU1=U1;
2544     }
2545
2546     IntSurf_PntOn2S aVF, aVL;
2547     Standard_Boolean addVF = Standard_False, addVL = Standard_False;
2548     VerifyVertices(sline,IsReversed,aVLine,aTOL2DS,theArcTol,
2549                                     thePDomain,aVF,addVF,aVL,addVL);
2550
2551     Standard_Boolean hasInternals = HasInternals(sline,aVLine);
2552
2553     Standard_Real D3F = 0., D3L = 0.;
2554     ToSmooth(sline,IsReversed,theQuad,Standard_True,D3F);
2555     ToSmooth(sline,IsReversed,theQuad,Standard_False,D3L);
2556
2557     //if(D3F <= 1.5e-7 && sline->NbPoints() >=3) {
2558     //  D3F = sline->Value(2).Value().Distance(sline->Value(3).Value());
2559     //}
2560     //if(D3L <= 1.5e-7 && sline->NbPoints() >=3) {
2561     //  D3L = sline->Value(sline->NbPoints()-1).Value().Distance(sline->
2562     //                                  Value(sline->NbPoints()-2).Value());
2563     //}
2564
2565     if(addVF || addVL)
2566     {
2567       Standard_Boolean isAdded = AddVertices(sline,aVF,addVF,aVL,addVL,D3F,D3L);
2568       if(isAdded)
2569       {
2570         ToSmooth(sline,IsReversed,theQuad,Standard_True,D3F);
2571         ToSmooth(sline,IsReversed,theQuad,Standard_False,D3L);
2572       }
2573     }
2574
2575     if(!hasInternals)
2576     {
2577       ForcedPurgePoints(sline,IsReversed,theQuad);
2578     }
2579
2580     Handle(IntPatch_WLine) wline = 
2581                         new IntPatch_WLine(sline,Standard_False,
2582                         theLine->TransitionOnS1(),theLine->TransitionOnS2());
2583
2584     gp_Pnt aSPnt(sline->Value(1).Value());
2585     sline->Value(1).ParametersOnS1(U1,V1);
2586     sline->Value(1).ParametersOnS2(U2,V2);
2587     aTPntF.SetValue(aSPnt,theArcTol,Standard_False);
2588     aTPntF.SetParameters(U1,V1,U2,V2);
2589     aTPntF.SetParameter(1.);
2590     wline->AddVertex(aTPntF);
2591     wline->SetFirstPoint(1);
2592
2593     if(hasInternals)
2594     {
2595       PutIntVertices(wline,sline,IsReversed,aVLine,theArcTol);
2596     }
2597
2598     aSPnt =  sline->Value(sline->NbPoints()).Value();
2599     sline->Value(sline->NbPoints()).ParametersOnS1(U1,V1);
2600     sline->Value(sline->NbPoints()).ParametersOnS2(U2,V2);
2601     aTPntL.SetValue(aSPnt,theArcTol,Standard_False);
2602     aTPntL.SetParameters(U1,V1,U2,V2);
2603     aTPntL.SetParameter(sline->NbPoints());
2604     wline->AddVertex(aTPntL);
2605     wline->SetLastPoint(sline->NbPoints());
2606
2607     IntPatch_SequenceOfLine segm;
2608     Standard_Boolean isSplited = SplitOnSegments(wline,Standard_False,
2609                     theLine->TransitionOnS1(),theLine->TransitionOnS2(),theArcTol,segm);
2610
2611     if(!isSplited)
2612     {
2613       theLines.Append(wline);
2614     }
2615     else
2616     {
2617       Standard_Integer nbsegms = segm.Length();
2618       Standard_Integer iseg = 0;
2619       for(iseg = 1; iseg <= nbsegms; iseg++)
2620         theLines.Append(segm(iseg));
2621     }
2622
2623     if(isDecomposited)
2624     {
2625       aFindex = aBindex;
2626       flNextLine = hasBeenDecomposed = Standard_True;
2627     }
2628   }
2629
2630   return hasBeenDecomposed;
2631 }
2632
2633 static Standard_Boolean IsIn2DBox(const Bnd_Box2d& theBox,
2634                                   const Handle(IntPatch_PointLine)& theLine,
2635                                   const Standard_Real theUPeriod,
2636                                   const Standard_Real theVPeriod,
2637                                   const Standard_Boolean isTheSurface1Using)
2638 {
2639   const Standard_Integer aNbPnts = theLine->NbPnts();
2640
2641   const Standard_Real aDeltaUPeriod[] = {0.0, -theUPeriod, 2.0*theUPeriod};
2642   const Standard_Real aDeltaVPeriod[] = {0.0, -theVPeriod, 2.0*theVPeriod};
2643
2644   const Standard_Integer aSzOfUPArr = sizeof(aDeltaUPeriod)/sizeof(aDeltaUPeriod[0]);
2645   const Standard_Integer aSzOfVPArr = sizeof(aDeltaVPeriod)/sizeof(aDeltaVPeriod[0]);
2646
2647   for(Standard_Integer aPtID = 1; aPtID <= aNbPnts; aPtID++)
2648   {
2649     Standard_Real aU = 0.0, aV = 0.0;
2650     if(isTheSurface1Using)
2651       theLine->Point(aPtID).ParametersOnS1(aU, aV);
2652     else
2653       theLine->Point(aPtID).ParametersOnS2(aU, aV);
2654
2655     if(!theBox.IsOut(gp_Pnt2d(aU, aV)))
2656       continue;
2657
2658     Standard_Boolean isInscribe = Standard_False;
2659
2660     for(Standard_Integer aUind = 0; !isInscribe && (aUind < aSzOfUPArr); aUind++)
2661     {
2662       if((aUind > 0) && (aDeltaUPeriod[aUind] == 0.0))
2663       {
2664         break;
2665       }
2666
2667       aU += aDeltaUPeriod[aUind];
2668
2669       for(Standard_Integer aVind = 0; !isInscribe && (aVind < aSzOfVPArr); aVind++)
2670       {
2671         if((aVind > 0) && (aDeltaVPeriod[aVind] == 0.0))
2672         {
2673           break;
2674         }
2675
2676         aV += aDeltaVPeriod[aVind];
2677
2678         isInscribe = !theBox.IsOut(gp_Pnt2d(aU, aV));
2679       }
2680     }
2681
2682     if(!isInscribe)
2683       return Standard_False;
2684   }
2685   return Standard_True;
2686 }