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