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