Integration of OCCT 6.5.0 from SVN
[occt.git] / src / IntPatch / IntPatch_WLine.cxx
1 // File:        IntPatch_WLine.cxx
2 // Created:     Mon May 27 13:18:45 1991
3 // Author:      Isabelle GRIGNON
4 // Copyright:   OPEN CASCADE 1991, 1992
5
6 #include <IntPatch_WLine.ixx>
7
8 #define DEBUG 0
9 #define DEBUGV 0
10
11 #include <IntSurf_PntOn2S.hxx>
12 #include <Precision.hxx>
13 #include <stdio.h>
14
15
16 IntPatch_WLine::IntPatch_WLine (const Handle(IntSurf_LineOn2S)& Line,
17                                 const Standard_Boolean Tang,
18                                 const IntSurf_TypeTrans Trans1,
19                                 const IntSurf_TypeTrans Trans2) :
20   IntPatch_Line(Tang,Trans1,Trans2),fipt(Standard_False),lapt(Standard_False),
21   hasArcOnS1(Standard_False),hasArcOnS2(Standard_False)
22 {
23   typ = IntPatch_Walking;
24   curv = Line;
25   Buv1.SetWhole();
26   Buv2.SetWhole();
27   Bxyz.SetWhole();
28   u1period=v1period=u2period=v2period=0.0;
29 }
30
31
32 IntPatch_WLine::IntPatch_WLine (const Handle(IntSurf_LineOn2S)& Line,
33                                 const Standard_Boolean Tang,
34                                 const IntSurf_Situation Situ1,
35                                 const IntSurf_Situation Situ2) :
36   IntPatch_Line(Tang,Situ1,Situ2),fipt(Standard_False),lapt(Standard_False),
37   hasArcOnS1(Standard_False),hasArcOnS2(Standard_False)
38 {
39   typ = IntPatch_Walking;
40   curv = Line;
41   Buv1.SetWhole();
42   Buv2.SetWhole();
43   Bxyz.SetWhole();
44   u1period=v1period=u2period=v2period=0.0;
45 }
46
47
48 IntPatch_WLine::IntPatch_WLine (const Handle(IntSurf_LineOn2S)& Line,
49                                 const Standard_Boolean Tang) :
50   IntPatch_Line(Tang),fipt(Standard_False),lapt(Standard_False),
51   hasArcOnS1(Standard_False),hasArcOnS2(Standard_False)
52 {
53   typ = IntPatch_Walking;
54   curv = Line;
55   Buv1.SetWhole();
56   Buv2.SetWhole();
57   Bxyz.SetWhole();
58   u1period=v1period=u2period=v2period=0.0;
59 }
60
61
62 void IntPatch_WLine::SetPoint(const Standard_Integer Index,
63                               const IntPatch_Point& thepoint)
64 {
65   curv->Value(Index,thepoint.PntOn2S());
66 }
67
68
69 Handle(IntSurf_LineOn2S) IntPatch_WLine::Curve() const
70
71   return(curv);
72 }
73
74 static void RecadreMemePeriode(Standard_Real& u1,Standard_Real& v1,
75                                Standard_Real& u2,Standard_Real& v2,
76                                const Standard_Real anu1,const Standard_Real anv1,
77                                const Standard_Real anu2,const Standard_Real anv2,
78                                const Standard_Real U1Period,const Standard_Real V1Period,
79                                const Standard_Real U2Period,const Standard_Real V2Period) { 
80   if(U1Period) { 
81     while(anu1-u1 > 0.8*U1Period) { u1+=U1Period; }
82     while(u1-anu1 > 0.8*U1Period) { u1-=U1Period; }
83   }
84   if(U2Period) { 
85     while(anu2-u2 > 0.8*U2Period) { u2+=U2Period; }
86     while(u2-anu2 > 0.8*U2Period) { u2-=U2Period; }
87   }
88   if(V1Period) { 
89     while(anv1-v1 > 0.8*V1Period) { v1+=V1Period; }
90     while(v1-anv1 > 0.8*V1Period) { v1-=V1Period; }
91   }
92   if(V2Period) { 
93     while(anv2-v2 > 0.8*V2Period) { v2+=V2Period; }
94     while(v2-anv2 > 0.8*V2Period) { v2-=V2Period; }
95   }
96
97 }
98
99 static void RecadreMemePeriode(IntSurf_PntOn2S& POn2S,const IntSurf_PntOn2S& RefPOn2S,
100                                const Standard_Real up1,
101                                const Standard_Real vp1,
102                                const Standard_Real up2,
103                                const Standard_Real vp2) { 
104   Standard_Real u1,v1,u2,v2,pu1,pv1,pu2,pv2;
105   POn2S.Parameters(u1,v1,u2,v2);
106   RefPOn2S.Parameters(pu1,pv1,pu2,pv2);
107   RecadreMemePeriode(u1,v1,u2,v2,pu1,pv1,pu2,pv2,up1,vp1,up2,vp2);
108   POn2S.SetValue(u1,v1,u2,v2);
109 }
110
111 static Standard_Boolean CompareVertexAndPoint(const gp_Pnt& V,const gp_Pnt& P,const Standard_Real& Tol) { 
112   return(V.Distance(P)<=Tol);
113 }
114
115 void IntPatch_WLine::SetPeriod(const Standard_Real pu1,
116                                const Standard_Real pv1,
117                                const Standard_Real pu2,
118                                const Standard_Real pv2) { 
119   u1period=pu1; v1period=pv1; u2period=pu2; v2period=pv2;
120 }
121 Standard_Real IntPatch_WLine::U1Period() const {    return(u1period); }
122 Standard_Real IntPatch_WLine::V1Period() const {    return(v1period); }
123 Standard_Real IntPatch_WLine::U2Period() const {    return(u2period); }
124 Standard_Real IntPatch_WLine::V2Period() const {    return(v2period); }
125
126
127 //------------------------------------------------------------------------
128 //--  En Entree : Une ligne de cheminement     +    Une Liste de Vetex
129 //--
130 //--   LineOn2S   :       1------2-------3-------4-----5---- ----nbp
131 //-- 
132 //--   Vertex     :   a     b c          d    e      f
133 //--
134 //--
135 //--  En Sortie 
136 //--
137 //--                  1--2-3-4--5--------6----7--8--9--10--------
138 //--
139 //--  avec   a de parametre 1 
140 //--         b              3
141 //--
142 //--   etc ...
143 //--
144 //--
145 //-- !!!!!!!!!!!!!!! On considere que deux vertex ne peuvent pas etre 
146 //-- !!!!!!!!!!!!!!!  a une distance inferieure a Tol
147 //------------------------------------------------------------------------
148 //--
149 //-- On Teste si la LineOn2S contient des points confondus. 
150 //-- Dans ce cas, on remove ces points.
151 //--
152 //------------------------------------------------------------------------
153
154 Standard_Boolean SameVtxRst(const IntPatch_Point& vtx1,const IntPatch_Point& vtx2) { 
155   if(vtx1.IsOnDomS1()) { 
156     if(vtx2.IsOnDomS1()) { 
157       if(vtx1.ArcOnS1() == vtx2.ArcOnS1()) { 
158         if(vtx1.ParameterOnArc1() == vtx2.ParameterOnArc1()) { 
159           
160         }
161         else { 
162           return(Standard_False);
163         }
164       }
165       else { 
166         return(Standard_False); 
167       }
168     }
169     else { 
170       return(Standard_False);
171     }
172   }
173   else { 
174     if(vtx2.IsOnDomS1()) { 
175       return(Standard_False);
176     }
177   }
178   if(vtx1.IsOnDomS2()) { 
179     if(vtx2.IsOnDomS2()) { 
180       if(vtx1.ArcOnS2() == vtx2.ArcOnS2()) { 
181         if(vtx1.ParameterOnArc2() == vtx2.ParameterOnArc2()) { 
182           
183         }
184         else { 
185           return(Standard_False);
186         }
187       }
188       else { 
189         return(Standard_False); 
190       }
191     }
192     else { 
193       return(Standard_False);
194     }
195   }
196   else {
197     if(vtx2.IsOnDomS2()) {
198       return(Standard_False);
199     }
200   }
201   return(Standard_True);
202 }
203
204
205 static Standard_Boolean CompareVerticesOnSurf(const IntPatch_Point& vtx1,
206                                               const IntPatch_Point& vtx2,
207                                               const Standard_Boolean onFirst)
208 {
209   Standard_Real u1,v1,u2,v2, tolU, tolV;
210   if (onFirst) {
211     vtx1.ParametersOnS1(u1,v1);
212     vtx2.ParametersOnS1(u2,v2);
213   }
214   else {
215     vtx1.ParametersOnS2(u1,v1);
216     vtx2.ParametersOnS2(u2,v2);
217   }
218   tolU = Precision::PConfusion();
219   tolV = Precision::PConfusion();
220   return (Abs(u1-u2) <= tolU && Abs(v1-v2) <= tolV);
221 }
222
223 inline Standard_Boolean CompareVerticesOnS1(const IntPatch_Point& vtx1, const IntPatch_Point& vtx2)
224 {return CompareVerticesOnSurf (vtx1, vtx2, Standard_True);}
225
226 inline Standard_Boolean CompareVerticesOnS2(const IntPatch_Point& vtx1, const IntPatch_Point& vtx2)
227 {return CompareVerticesOnSurf (vtx1, vtx2, Standard_False);}
228
229
230 void IntPatch_WLine::ComputeVertexParameters(const Standard_Real RTol) { 
231
232   // MSV Oct 15, 2001: use tolerance of vertex instead of RTol where 
233   //                   it is possible
234
235   Standard_Integer i,j,k,nbvtx,nbponline;
236   Standard_Integer indicevertexonline;
237   Standard_Real    indicevertex;
238
239   Standard_Real ParamMinOnLine,ParamMaxOnLine;
240   if(fipt) { ParamMinOnLine = Vertex(indf).ParameterOnLine(); } else { ParamMinOnLine = -100000.0; } 
241   if(lapt) { ParamMaxOnLine = Vertex(indl).ParameterOnLine(); } else { ParamMaxOnLine =  100000.0; } 
242   Standard_Boolean APointDeleted = Standard_False;
243   //----------------------------------------------------------
244   //--     F i l t r e   s u r   r e s t r i c t i o n s   --
245   //----------------------------------------------------------
246   //-- deux vertex sur la meme restriction et seulement 
247   //-- sur celle ci ne doivent pas avoir le meme parametre
248   //--
249   Standard_Real Tol=RTol;
250   nbvtx = NbVertex();
251
252 #if DEBUGV  
253   cout<<"\n----------- avant ComputeVertexParameters -------------"<<endl;
254   for(i=1;i<=nbvtx;i++) { 
255     Vertex(i).Dump();
256     Standard_Real  polr = Vertex(i).ParameterOnLine();
257     Standard_Real pol = (Standard_Integer)polr;
258     if(pol>=1 && pol<=nbvtx) { 
259       cout<<"----> IntSurf_PntOn2S : "<<polr<<"  Pnt ("<<Vertex(pol).Value().X()
260         <<","<<Vertex(pol).Value().Y()
261           <<","<<Vertex(pol).Value().Z()<<")"<<endl;
262     }
263   }
264   cout<<"\n----------------------------------------------------------"<<endl;
265 #endif  
266
267   
268   //-- ----------------------------------------------------------------------
269   //-- Traitement des aretes de couture : On duplique les points situes
270   //-- sur des restrictions differentes
271   //-- 
272   //-- Phase Creation de nouveaux points sur S1 
273   Standard_Boolean encoreunefois;
274   do { 
275     nbvtx=NbVertex();
276     encoreunefois=Standard_False;
277     for(i=1; i<=nbvtx && encoreunefois==Standard_False; i++) { 
278       IntPatch_Point& VTXi   = svtx.ChangeValue(i);
279       for(j=1; j<=nbvtx && encoreunefois==Standard_False; j++) { 
280         if(i!=j) { 
281           IntPatch_Point& VTXj   = svtx.ChangeValue(j);
282           if(VTXi.ParameterOnLine() != VTXj.ParameterOnLine()) { 
283             Standard_Real d = VTXi.Value().Distance(VTXj.Value());
284             Standard_Real toli = VTXi.Tolerance();
285             Standard_Real tolj = VTXj.Tolerance();
286             Standard_Real maxtol = Max(toli,tolj);
287             // MSV Oct 30, 2001: compare in 2D space also;
288             //                   increase tolerances
289             if (d < maxtol ||
290                CompareVerticesOnS1(VTXi,VTXj) || CompareVerticesOnS2(VTXi,VTXj)) { 
291               //-- Creation Vtx (REF:S1(i)  S2(j))    (On Garde S1(i))
292               Standard_Real newtoli = Max (toli, tolj+d*1.01);
293               Standard_Real newtolj = Max (tolj, toli+d*1.01);
294               Standard_Boolean acreer=Standard_False;
295               if(VTXi.IsOnDomS1()) { 
296                 if(VTXj.IsOnDomS1()) { 
297                   if(VTXj.ArcOnS1() != VTXi.ArcOnS1()) { 
298                     acreer=Standard_True;
299                   }
300                 }
301                 else { 
302                   acreer=Standard_True;
303                 }
304               }
305               if(acreer) { 
306                 IntPatch_Point vtx;
307                 vtx = VTXj;
308                 vtx.SetArc(Standard_True,
309                            VTXi.ArcOnS1(),
310                            VTXi.ParameterOnArc1(),
311                            VTXi.TransitionLineArc1(),
312                            VTXi.TransitionOnS1());
313                 for(k=1; encoreunefois==Standard_False && k<=nbvtx; k++) { 
314                   const IntPatch_Point& VTXk   = svtx.Value(k);
315                   if(SameVtxRst(VTXk,vtx)) { 
316                     encoreunefois=Standard_True;
317                   }
318                 }
319                 if(encoreunefois==Standard_False) {
320                   VTXi.SetTolerance(newtoli);
321                   VTXj.SetTolerance(newtolj);
322                   vtx.SetTolerance(newtolj);
323                   svtx.Append(vtx);
324                   encoreunefois=Standard_True;
325                 }
326                 else { 
327                   encoreunefois=Standard_False;
328                 }
329               }
330               //-- -----------------------------------------------------
331               //-- Creation Vtx (REF:S2(i)  S1(j))    (On Garde S2(i))
332               acreer=Standard_False;
333               if(VTXi.IsOnDomS2()) { 
334                 if(VTXj.IsOnDomS2()) { 
335                   if(VTXj.ArcOnS2() != VTXi.ArcOnS2()) { 
336                     acreer=Standard_True;
337                   }
338                 }
339                 else { 
340                   acreer=Standard_True;
341                 }
342               }
343               if(acreer) { 
344                 IntPatch_Point vtx;
345                 vtx = VTXj;
346                 vtx.SetArc(Standard_False,
347                            VTXi.ArcOnS2(),
348                            VTXi.ParameterOnArc2(),
349                            VTXi.TransitionLineArc2(),
350                            VTXi.TransitionOnS2());
351                 for(k=1; encoreunefois==Standard_False && k<=nbvtx; k++) { 
352                   const IntPatch_Point& VTXk   = svtx.Value(k);
353                   if(SameVtxRst(VTXk,vtx)) { 
354                     encoreunefois=Standard_True;
355                   }
356                 }
357                 if(encoreunefois==Standard_False) {
358                   VTXi.SetTolerance(newtoli);
359                   VTXj.SetTolerance(newtolj);
360                   vtx.SetTolerance(newtolj);
361                   svtx.Append(vtx);
362                   encoreunefois=Standard_True;
363                 }
364                 else { 
365                   encoreunefois=Standard_False;
366                 }
367               }     
368             }
369           }
370         }
371       }
372     }
373   }
374   while(encoreunefois);
375   
376   
377
378   //-- ----------------------------------------------------------------------
379
380
381
382   do { 
383     APointDeleted = Standard_False;
384     for(i=1; (i<=nbvtx) && (APointDeleted==Standard_False) ;i++) { 
385       const IntPatch_Point& VTXi   = svtx.Value(i);
386       if(VTXi.Tolerance() > Tol)        Tol = VTXi.Tolerance(); //-- 9 oct 97 
387       if((VTXi.IsOnDomS1()==Standard_True) && (VTXi.IsOnDomS2()==Standard_False)) { 
388         for(j=1; (j<=nbvtx) && (APointDeleted==Standard_False) ;j++) {
389           if(i!=j) { 
390             const IntPatch_Point& VTXj   = svtx.Value(j);
391             if((VTXj.IsOnDomS1()==Standard_True) && (VTXj.IsOnDomS2()==Standard_False)) {
392               if(VTXi.ParameterOnLine() == VTXj.ParameterOnLine()) { 
393                 if(VTXi.ArcOnS1() == VTXj.ArcOnS1()) { 
394                   svtx.Remove(j);
395                   nbvtx--;
396                   if(lapt) { if(indl>=j) indl--; } 
397                   if(fipt) { if(indf>=j) indf--; } 
398                   APointDeleted = Standard_True;
399                 }
400               }
401             }
402           }
403         }
404       }
405     }
406   }
407   while(APointDeleted == Standard_True);
408
409
410   do { 
411     APointDeleted = Standard_False;
412     for(i=1; (i<=nbvtx) && (APointDeleted==Standard_False) ;i++) { 
413       const IntPatch_Point& VTXi   = svtx.Value(i);
414       if((VTXi.IsOnDomS2()==Standard_True) && (VTXi.IsOnDomS1()==Standard_False)) { 
415         for(j=1; (j<=nbvtx) && (APointDeleted==Standard_False) ;j++) {
416           if(i!=j) { 
417             const IntPatch_Point& VTXj   = svtx.Value(j);
418             if((VTXj.IsOnDomS2()==Standard_True) && (VTXj.IsOnDomS1()==Standard_False)) {
419               if(VTXi.ParameterOnLine() == VTXj.ParameterOnLine()) { 
420                 if(VTXi.ArcOnS2() == VTXj.ArcOnS2()) { 
421                   svtx.Remove(j);
422                   nbvtx--;
423                   if(lapt) { if(indl>=j) indl--; } 
424                   if(fipt) { if(indf>=j) indf--; } 
425                   APointDeleted = Standard_True;
426                 }
427               }
428             }
429           }
430         }
431       }
432     }
433   }
434   while(APointDeleted == Standard_True);
435         
436   nbvtx     = NbVertex();
437   nbponline = NbPnts();
438
439   //----------------------------------------------------
440   //-- On trie les Vertex 
441   Standard_Boolean SortIsOK;
442   do { 
443     SortIsOK = Standard_True;
444     for(i=2; i<=nbvtx; i++) { 
445       if(svtx.Value(i-1).ParameterOnLine()  > svtx.Value(i).ParameterOnLine()) { 
446         SortIsOK = Standard_False;
447         svtx.Exchange(i-1,i);
448       }
449     }
450   }
451   while(!SortIsOK);
452   
453   //----------------------------------------------------
454   //-- On detecte les points confondus dans la LineOn2S
455   Standard_Real dmini = Precision::Confusion();
456   dmini*=dmini;
457   for(i=2; i<=nbponline; i++) { 
458     //const IntSurf_PntOn2S& aPntOn2S1=curv->Value(i-1);
459     //const IntSurf_PntOn2S& aPntOn2S2=curv->Value(i);
460     Standard_Real d = (curv->Value(i-1).Value()).SquareDistance((curv->Value(i).Value()));
461     if(d < dmini) { 
462       curv->RemovePoint(i);
463       nbponline--;
464       //----------------------------------------------
465       //-- On recadre les Vertex si besoin 
466       //-- 
467       for(j=1; j<=nbvtx; j++) { 
468         indicevertex = svtx.Value(j).ParameterOnLine();
469         if(indicevertex >= i) {
470           svtx.ChangeValue(j).SetParameter(indicevertex-1.0);
471         }
472       }
473       //modified by NIZNHY-PKV Mon Feb 11 09:28:02 2002 f
474       i--;
475       //modified by NIZNHY-PKV Mon Feb 11 09:28:04 2002 t
476     }
477   }
478   //----------------------------------------------------
479   for(i=1; i<=nbvtx; i++) {
480     const gp_Pnt& P    = svtx.Value(i).Value();
481     Standard_Real vTol = svtx.Value(i).Tolerance();
482     indicevertex = svtx.Value(i).ParameterOnLine();
483     indicevertexonline = (Standard_Integer)indicevertex;
484     //--------------------------------------------------
485     //-- On Compare le vertex avec les points de la ligne
486     //-- d indice   indicevertexOnLine-1
487     //--            indicevertexOnLine
488     //--            indicevertexOnLine+1
489     //--------------------------------------------------
490     if(indicevertexonline<1) { 
491       if(CompareVertexAndPoint(P,curv->Value(1).Value(),vTol)) {
492         //-------------------------------------------------------
493         //-- On remplace le point cheminement(1) par vertex(i)
494         //-- et   vertex(i) prend pour parametre 1
495         //-------------------------------------------------------
496         
497         IntSurf_PntOn2S POn2S = svtx.Value(i).PntOn2S();
498         RecadreMemePeriode(POn2S,curv->Value(1),U1Period(),V1Period(),U2Period(),V2Period());
499         curv->Value(1,POn2S);
500
501         //--curv->Value(1,svtx.Value(i).PntOn2S());
502         svtx.ChangeValue(i).SetParameter(1.0);
503       }
504       else { 
505         //-------------------------------------------------------
506         //-- On insere le point de cheminement Vertex(i)
507         //-- On recadre les parametres des autres vertex
508         //-------------------------------------------------------
509         IntSurf_PntOn2S POn2S = svtx.Value(i).PntOn2S();
510         RecadreMemePeriode(POn2S,curv->Value(1),U1Period(),V1Period(),U2Period(),V2Period());
511         curv->InsertBefore(1,POn2S);
512
513         //-- curv->InsertBefore(1,svtx.Value(i).PntOn2S());
514         svtx.ChangeValue(i).SetParameter(1.0);
515         nbponline++;
516         for(j=1;j<=nbvtx;j++) {
517           if(j!=1) { 
518             Standard_Real t = svtx.Value(j).ParameterOnLine();
519             if(t>1.0) { 
520               svtx.ChangeValue(j).SetParameter(t+1.0);
521             }
522           }
523         }
524       }
525     } //--- fin : if(indicevertexonline<1) 
526     else { 
527       //---------------------------------------------------------
528       //-- vertex(i)   ==   cheminement (indicevertexonline-1)
529       //-- vertex(i)   ==   cheminement (indicevertexonline)
530       //-- vertex(i)   ==   cheminement (indicevertexonline+1)
531       //---------------------------------------------------------
532       Standard_Boolean Substitution = Standard_False;
533       //-- for(k=indicevertexonline+1; !Substitution && k>=indicevertexonline-1;k--) {   avant le 9 oct 97 
534       for(k=indicevertexonline+1; k>=indicevertexonline-1;k--) { 
535         if(k>0 && k<=nbponline) { 
536           if(CompareVertexAndPoint(P,curv->Value(k).Value(),vTol)) {
537             //-------------------------------------------------------
538             //-- On remplace le point cheminement(k) 
539             //-- par vertex(i)  et vertex(i) prend pour parametre k
540             //-------------------------------------------------------
541             IntSurf_PntOn2S POn2S = svtx.Value(i).PntOn2S();
542             RecadreMemePeriode(POn2S,curv->Value(k),U1Period(),V1Period(),U2Period(),V2Period());
543             curv->Value(k,POn2S);
544             Standard_Real mu1,mv1,mu2,mv2;
545             POn2S.Parameters(mu1,mv1,mu2,mv2);
546             svtx.ChangeValue(i).SetParameter(k);
547             svtx.ChangeValue(i).SetParameters(mu1,mv1,mu2,mv2);
548             Substitution = Standard_True;
549           }
550         }
551       }
552       
553       //--static int deb6nov98=1;    Ne resout rien (a part partiellement BUC60409)
554       //--if(deb6nov98) { 
555       //--Substitution=Standard_True;
556       //-- }
557       
558       if(Substitution==Standard_False) { 
559         //-------------------------------------------------------
560         //-- On insere le point de cheminement Vertex(i)
561         //-- On recadre les parametres des autres vertex
562         //-------------------------------------------------------
563         IntSurf_PntOn2S POn2S = svtx.Value(i).PntOn2S();
564         if(indicevertexonline >= nbponline) { 
565           RecadreMemePeriode(POn2S,curv->Value(nbponline),U1Period(),V1Period(),U2Period(),V2Period());
566           curv->Add(POn2S);
567         }
568         else { 
569           RecadreMemePeriode(POn2S,curv->Value(indicevertexonline+1),U1Period(),V1Period(),U2Period(),V2Period());
570           curv->InsertBefore(indicevertexonline+1,POn2S);
571         }
572         //-- curv->InsertBefore(indicevertexonline+1,svtx.Value(i).PntOn2S());
573         svtx.ChangeValue(i).SetParameter(indicevertexonline+1);
574         nbponline++;
575         for(j=1;j<=nbvtx;j++) { 
576           if(j!=i) { 
577             Standard_Real t = svtx.Value(j).ParameterOnLine();
578             if(t>(Standard_Real)indicevertexonline) { 
579               svtx.ChangeValue(j).SetParameter(t+1.0);
580             }
581           }
582         }
583       } //-- Substitution
584     } //-- indicevertexonline>=1
585
586   } //-- boucle i sur vertex
587
588   
589   
590   
591   do {  
592     APointDeleted = Standard_False;
593     for(i=1; i<=nbvtx && (APointDeleted == Standard_False); i++) { 
594       const IntPatch_Point& VTX   = svtx.Value(i);      
595       for(j=1; j<=nbvtx && (APointDeleted == Standard_False) ; j++) { 
596         if(i!=j) { 
597           const IntPatch_Point& VTXM1 = svtx.Value(j);
598           
599           Standard_Boolean kill   = Standard_False;
600           Standard_Boolean killm1 = Standard_False;
601           if(VTXM1.ParameterOnLine() == VTX.ParameterOnLine()) { 
602             if(VTXM1.IsOnDomS1() && VTX.IsOnDomS1()) {  //-- OnS1    OnS1
603               if(VTXM1.ArcOnS1() == VTX.ArcOnS1()) {    //-- OnS1 == OnS1
604                 if(VTXM1.IsOnDomS2()) {                 //-- OnS1 == OnS1  OnS2  
605                   if(VTX.IsOnDomS2()==Standard_False) {   //-- OnS1 == OnS1  OnS2 PasOnS2
606                     kill=Standard_True;   
607                   }
608                   else {
609                     if(VTXM1.ArcOnS2() == VTX.ArcOnS2()) { //-- OnS1 == OnS1  OnS2 == OnS2
610                       kill=Standard_True;
611                     }
612                   }
613                 }
614                 else {                                  //-- OnS1 == OnS1  PasOnS2  
615                   if(VTX.IsOnDomS2()) {                 //-- OnS1 == OnS1  PasOnS2  OnS2
616                     killm1=Standard_True;
617                   }
618                 }
619               }
620             }
621             
622             if(!(kill || killm1)) {
623               if(VTXM1.IsOnDomS2() && VTX.IsOnDomS2()) {  //-- OnS2    OnS2
624                 if(VTXM1.ArcOnS2() == VTX.ArcOnS2()) {    //-- OnS2 == OnS2
625                   if(VTXM1.IsOnDomS1()) {                 //-- OnS2 == OnS2  OnS1  
626                     if(VTX.IsOnDomS1()==Standard_False) {   //-- OnS2 == OnS2  OnS1 PasOnS1
627                       kill=Standard_True;   
628                     }
629                     else {
630                       if(VTXM1.ArcOnS1() == VTX.ArcOnS1()) { //-- OnS2 == OnS2  OnS1 == OnS1
631                         kill=Standard_True;
632                       }
633                     }
634                   }
635                   else {                                  //-- OnS2 == OnS2  PasOnS1  
636                     if(VTX.IsOnDomS1()) {                 //-- OnS2 == OnS2  PasOnS1  OnS1
637                       killm1=Standard_True;
638                     }
639                   }
640                 }
641               }
642             }
643             if(kill) { 
644               APointDeleted = Standard_True;
645               svtx.Remove(i);
646               nbvtx--;
647             }
648             else if(killm1) { 
649               APointDeleted = Standard_True;
650               svtx.Remove(j);
651               nbvtx--; 
652             }
653           }
654         }
655       }
656     }
657   }
658   while(APointDeleted == Standard_True);
659   
660   do {  
661     SortIsOK = Standard_True;
662     for(i=2; i<=nbvtx && SortIsOK; i++) {
663       const IntPatch_Point& Pim1=svtx.Value(i-1);
664       const IntPatch_Point& Pii  =svtx.Value(i);
665       if(Pim1.ParameterOnLine()==Pii.ParameterOnLine()) { 
666         if(   (Pii.IsOnDomS1() == Standard_False)
667            && (Pii.IsOnDomS2() == Standard_False)) { 
668           SortIsOK = Standard_False;
669           svtx.Remove(i);
670           nbvtx--;
671         }
672         else {
673           if(   (Pim1.IsOnDomS1() == Standard_False)
674              && (Pim1.IsOnDomS2() == Standard_False)) { 
675             SortIsOK = Standard_False;
676             svtx.Remove(i-1);
677             nbvtx--;
678           }       
679         }
680       }
681     }
682   }
683   while(!SortIsOK);
684   //-- ----------------------------------------------------------------------------
685   //-- On ajoute les vertex de debut et de fin de ligne s il ne sont pas presents.
686   //-- 
687   //-- Existe t il un vertex de debut de ligne, de fin .
688   //--
689   //-- Si Besoin : il faudra dedoubler les points de debut et de fin sur les periodiques ??????
690
691
692
693   
694   Standard_Boolean bFirst = Standard_False;
695   Standard_Boolean bLast  = Standard_False;
696   nbponline = NbPnts();
697   for(i=1;i<=nbvtx;i++) { 
698     Standard_Real pol = svtx.Value(i).ParameterOnLine();
699     if(pol==1.0)       {
700       bFirst = fipt = Standard_True;
701       indf   = i;
702     }
703     if(pol==nbponline) {
704       bLast = lapt = Standard_True;
705       indl  = i;
706     }
707   }
708   if(bFirst == Standard_False) { 
709     Standard_Real pu1,pv1,pu2,pv2;
710     Standard_Boolean vtxfound = Standard_False;
711     IntPatch_Point vtx;
712     curv->Value(1).Parameters(pu1,pv1,pu2,pv2);
713     for(i=1;
714         (vtxfound==Standard_False) && (i<=nbvtx);i++) { 
715       const IntPatch_Point&  V = svtx.Value(i);
716       Standard_Real vTol = V.Tolerance();
717       if(CompareVertexAndPoint(V.Value(),curv->Value(1).Value(),vTol)) { 
718         vtx = V;
719         vtx.SetParameters(pu1,pv1,pu2,pv2);
720         vtxfound = Standard_True;
721       }
722     }
723     if(vtxfound == Standard_False)  { 
724       vtx.SetValue(curv->Value(1).Value(),Tol,Standard_False);
725       vtx.SetParameters(pu1,pv1,pu2,pv2);
726     }
727     vtx.SetParameter(1);
728     svtx.Prepend(vtx);    nbvtx++;
729     fipt = Standard_True;
730     indf = 1;
731   }
732   if(bLast == Standard_False) { 
733     Standard_Real pu1,pv1,pu2,pv2;
734     Standard_Boolean vtxfound = Standard_False;    
735     IntPatch_Point vtx;
736     curv->Value(nbponline).Parameters(pu1,pv1,pu2,pv2);   
737     for(i=1;
738         (vtxfound==Standard_False) && (i<=nbvtx);i++) { 
739       const IntPatch_Point&  V = svtx.Value(i);
740       Standard_Real vTol = V.Tolerance();
741       if(CompareVertexAndPoint(V.Value(),curv->Value(nbponline).Value(),vTol)) { 
742         vtx = V;
743         vtx.SetParameters(pu1,pv1,pu2,pv2);
744         vtxfound = Standard_True;
745       }
746     }
747     if(vtxfound == Standard_False)  {     
748       vtx.SetValue(curv->Value(nbponline).Value(),Tol,Standard_False);
749       vtx.SetParameters(pu1,pv1,pu2,pv2);
750     }
751     vtx.SetParameter(nbponline);
752     svtx.Append(vtx); nbvtx++;
753     lapt = Standard_True;
754     indl = nbvtx; 
755   }
756   
757
758
759
760   //--------------------------------------------------------------
761   //-- ** Detection de points trouves sur une meme restriction 
762   //--    avec la meme transition et avec des params on line 
763   //--    voisins.   
764   //-- ** Dans ce cas (-> donnerait un baillemenmt) on supprime 
765   //--    le point 'intermediaire'. 
766   //-- ** (exemple Vtx(1)  .....  Vtx(204) Vtx(205))
767   //--    on supprime le Vtx(204)
768   //-- ** (exemple Vtx(1) Vtx(2)  .....  Vtx(205))
769   //--    on supprime le Vtx(2)
770   //-- ** (exemple Vtx(1)  ...  Vtx(100) Vtx(101) ... Vtx(205))
771   //--    on supprime le Vtx(100)  (Vtx(100)et101 sur m restr)
772
773   //--------------------------------------------------------------
774   nbvtx = NbVertex();
775   do { 
776     APointDeleted = Standard_False;
777     for(i=1; (i<=nbvtx) && (APointDeleted==Standard_False) ;i++) { 
778       const IntPatch_Point& VTXi   = svtx.Value(i);
779       if((VTXi.IsOnDomS1()==Standard_True) && (VTXi.IsOnDomS2()==Standard_False)) { 
780         for(j=1; (j<=nbvtx) && (APointDeleted==Standard_False) ;j++) {
781           if(i!=j) { 
782             const IntPatch_Point& VTXj   = svtx.Value(j);
783             if((VTXj.IsOnDomS1()==Standard_True) && (VTXj.IsOnDomS2()==Standard_False)) {
784               if(   (VTXi.ParameterOnLine() == VTXj.ParameterOnLine()+1)
785                  || (VTXi.ParameterOnLine() == VTXj.ParameterOnLine()-1)) { 
786                 if(VTXi.ArcOnS1() == VTXj.ArcOnS1()) { 
787                   IntSurf_Transition t1 = VTXi.TransitionLineArc1();
788                   IntSurf_Transition t2 = VTXj.TransitionLineArc1();
789                   if(t1.TransitionType()==t2.TransitionType()) { 
790                     if((VTXj.ParameterOnLine()!=1) && (VTXj.ParameterOnLine()!=NbPnts())) {   
791                       svtx.Remove(j);
792                       nbvtx--;
793                       if(lapt) { if(indl>=j) indl--; } 
794                       if(fipt) { if(indf>=j) indf--; } 
795                       APointDeleted = Standard_True;
796                     }
797                   }
798                 }
799               }
800             }
801           }
802         }
803       }
804     }
805   }
806   
807   //-- meme traitement sur les restrictions du second shape
808
809   while(APointDeleted == Standard_True);
810   nbvtx = NbVertex();
811   do { 
812     APointDeleted = Standard_False;
813     for(i=1; (i<=nbvtx) && (APointDeleted==Standard_False) ;i++) { 
814       const IntPatch_Point& VTXi   = svtx.Value(i);
815       if((VTXi.IsOnDomS1()==Standard_False) && (VTXi.IsOnDomS2()==Standard_True)) { 
816         for(j=1; (j<=nbvtx) && (APointDeleted==Standard_False) ;j++) {
817           if(i!=j) { 
818             const IntPatch_Point& VTXj   = svtx.Value(j);
819             if((VTXj.IsOnDomS1()==Standard_False) && (VTXj.IsOnDomS2()==Standard_True)) {
820               if(   (VTXi.ParameterOnLine() == VTXj.ParameterOnLine()+1)
821                  || (VTXi.ParameterOnLine() == VTXj.ParameterOnLine()-1)) { 
822                 if(VTXi.ArcOnS2() == VTXj.ArcOnS2()) { 
823                   IntSurf_Transition t1 = VTXi.TransitionLineArc2();
824                   IntSurf_Transition t2 = VTXj.TransitionLineArc2();
825                   if(t1.TransitionType()==t2.TransitionType()) { 
826                     if((VTXj.ParameterOnLine()!=1) && (VTXj.ParameterOnLine()!=NbPnts())) {   
827                       svtx.Remove(j);
828                       nbvtx--;
829                       if(lapt) { if(indl>=j) indl--; } 
830                       if(fipt) { if(indf>=j) indf--; } 
831                       APointDeleted = Standard_True;
832                     }
833                   }
834                 }
835               }
836             }
837           }
838         }
839       }
840     }
841   }
842   while(APointDeleted == Standard_True);
843   //--------------------------------------------------------------
844
845   //--------------------------------------------------------------
846   //-- dans le cas de lignes periodiques du type : 
847   //--    Un point sur restriction R1 de param p1  -> P3d1   Vtx1
848   //--    Un point sur restriction R2 de param p2  -> P3d1   Vtx2
849   //--    
850   //--    Un point sur restriction R1 de param p1  -> P3d1   Vtx3
851   //--    pas de point sur R2 
852   //--
853   //-- On doit dans ce cas creer un nouveau Vtx4 = Vtx3 sur la 
854   //-- restriction R2
855   //--
856   //-- Ce cas se produit qd on a corrige un baillement avec le filtre
857   //-- precedent
858   //-- 
859   
860
861
862
863   
864
865   nbvtx = NbVertex();
866   do { 
867     SortIsOK = Standard_True;
868     for(i=2; i<=nbvtx; i++) { 
869       if(svtx.Value(i-1).ParameterOnLine()  > svtx.Value(i).ParameterOnLine()) { 
870         SortIsOK = Standard_False;
871         svtx.Exchange(i-1,i);
872       }
873     }
874   }
875   while(!SortIsOK);
876   
877   //-- Dump();
878
879 #if DEBUGV  
880   cout<<"\n----------- apres ComputeVertexParameters -------------"<<endl;
881   for(i=1;i<=nbvtx;i++) { 
882     Vertex(i).Dump();
883     Standard_Real  polr = Vertex(i).ParameterOnLine();
884     Standard_Real pol = (Standard_Integer)polr;
885     if(pol>=1 && pol<=nbvtx) { 
886       cout<<"----> IntSurf_PntOn2S : "<<polr<<"  Pnt ("<<Vertex(pol).Value().X()
887         <<","<<Vertex(pol).Value().Y()
888           <<","<<Vertex(pol).Value().Z()<<")"<<endl;
889     }
890   }
891   cout<<"\n----------------------------------------------------------"<<endl;
892 #endif  
893
894
895 }
896
897
898
899 Standard_Boolean IntPatch_WLine::IsOutSurf1Box(const gp_Pnt2d& P1uv)  { 
900   if(Buv1.IsWhole()) {
901     Standard_Integer n=NbPnts();
902     Standard_Real pu1,pu2,pv1,pv2;
903     Buv1.SetVoid();
904     for(Standard_Integer i=1;i<=n;i++) { 
905       curv->Value(i).Parameters(pu1,pv1,pu2,pv2);
906       Buv1.Add(gp_Pnt2d(pu1,pv1));
907     }
908     Buv1.Get(pu1,pv1,pu2,pv2);
909     pu2-=pu1;
910     pv2-=pv1;
911     if(pu2>pv2) { 
912       Buv1.Enlarge(pu2*0.01);
913     }
914     else { 
915       Buv1.Enlarge(pv2*0.01);
916     }
917   }
918   Standard_Boolean out=Buv1.IsOut(P1uv);
919   return(out);
920 }
921
922 Standard_Boolean IntPatch_WLine::IsOutSurf2Box(const gp_Pnt2d& P2uv)  { 
923   if(Buv2.IsWhole()) { 
924     Standard_Integer n=NbPnts();
925     Standard_Real pu1,pu2,pv1,pv2;
926     Buv2.SetVoid();
927     for(Standard_Integer i=1;i<=n;i++) { 
928       curv->Value(i).Parameters(pu1,pv1,pu2,pv2);
929       Buv2.Add(gp_Pnt2d(pu2,pv2));
930     }
931     Buv2.Get(pu1,pv1,pu2,pv2);
932     pu2-=pu1;
933     pv2-=pv1;
934     if(pu2>pv2) { 
935       Buv2.Enlarge(pu2*0.01);
936     }
937     else { 
938       Buv2.Enlarge(pv2*0.01);
939     }    
940   }
941   Standard_Boolean out=Buv2.IsOut(P2uv);
942   return(out);
943 }
944
945 Standard_Boolean IntPatch_WLine::IsOutBox(const gp_Pnt& Pxyz)  { 
946   if(Bxyz.IsWhole()) { 
947     Standard_Integer n=NbPnts();
948     Bxyz.SetVoid();
949     for(Standard_Integer i=1;i<=n;i++) { 
950       gp_Pnt P=curv->Value(i).Value();
951       Bxyz.Add(P);
952     }
953     Standard_Real x0,y0,z0,x1,y1,z1;
954     Bxyz.Get(x0,y0,z0,x1,y1,z1);
955     x1-=x0; y1-=y0; z1-=z0;
956     if(x1>y1) {
957       if(x1>z1) {
958         Bxyz.Enlarge(x1*0.01);
959       }
960       else { 
961         Bxyz.Enlarge(z1*0.01);
962       }
963     }
964     else { 
965       if(y1>z1) { 
966         Bxyz.Enlarge(y1*0.01);
967       }
968       else { 
969         Bxyz.Enlarge(z1*0.01);
970       }
971     }
972   }
973   Standard_Boolean out=Bxyz.IsOut(Pxyz);
974   return(out);
975 }
976
977
978 Standard_Boolean IntPatch_WLine::HasArcOnS1() const  {
979   return(hasArcOnS1);
980 }
981
982 void IntPatch_WLine::SetArcOnS1(const Handle(Adaptor2d_HCurve2d)& A) { 
983   hasArcOnS1=Standard_True;
984   theArcOnS1=A;
985 }
986
987 const Handle(Adaptor2d_HCurve2d)& IntPatch_WLine::GetArcOnS1() const  { 
988   return(theArcOnS1);
989 }
990
991 Standard_Boolean IntPatch_WLine::HasArcOnS2() const  {
992   return(hasArcOnS2);
993 }
994
995 void IntPatch_WLine::SetArcOnS2(const Handle(Adaptor2d_HCurve2d)& A) { 
996   hasArcOnS2=Standard_True;
997   theArcOnS2=A;
998 }
999
1000 const Handle(Adaptor2d_HCurve2d)& IntPatch_WLine::GetArcOnS2() const  { 
1001   return(theArcOnS2);
1002 }
1003
1004
1005 void IntPatch_WLine::Dump() const { 
1006   
1007   cout<<" ----------- D u m p    I n t P a t c h  _  W L i n e  --------------"<<endl;
1008   Standard_Integer i;
1009   Standard_Integer nbp = NbPnts();
1010   printf("Num    [X  Y  Z]     [U1  V1]   [U2  V2]\n");
1011 //  for(Standard_Integer i=1;i<=nbp;i++) { 
1012   for(i=1;i<=nbp;i++) {
1013     Standard_Real u1,v1,u2,v2;
1014     Point(i).Parameters(u1,v1,u2,v2);
1015     printf("%4d  [%+5.8e %+5.8e %+5.8e]  [%+5.8e %+5.8e]  [%+5.8e %+5.8e]\n",
1016            i,
1017            Point(i).Value().X(),
1018            Point(i).Value().Y(),
1019            Point(i).Value().Z(),
1020            u1,v1,u2,v2);
1021     
1022     
1023     //cout<<"IntSurf_PntOn2S : "<<i<<"  Pnt ("<<curv->Value(i).Value().X()
1024     //  <<","<<curv->Value(i).Value().Y()
1025     //  <<","<<curv->Value(i).Value().Z()<<")"<<endl;
1026     //cout<<"                :   u1("<<u1<<")   v1("<<v1<<")   u2("<<u2<<")   v2("<<v2<<")"<<endl; 
1027   }
1028   nbp = NbVertex();
1029   for(i=1;i<=nbp;i++) { 
1030     Vertex(i).Dump();
1031     Standard_Real  polr = Vertex(i).ParameterOnLine();
1032     Standard_Integer pol = (Standard_Integer)polr;
1033     if(pol>=1 && pol<=nbp) { 
1034       cout<<"----> IntSurf_PntOn2S : "<<polr<<"  Pnt ("<<Vertex(pol).Value().X()
1035         <<","<<Vertex(pol).Value().Y()
1036           <<","<<Vertex(pol).Value().Z()<<")"<<endl;
1037     }
1038   }
1039   cout<<"\n----------------------------------------------------------"<<endl;  
1040 }
1041