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