0023388: Boolean operations hang up trying to build section of two customer's shapes
[occt.git] / src / IntWalk / IntWalk_IWalking_3.gxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19
20
21 #ifndef DEB
22 #define No_Standard_RangeError
23 #define No_Standard_OutOfRange
24 #endif
25
26 // modified by NIZHNY-MKK  Thu Nov  2 15:07:26 2000.BEGIN
27 static Standard_Boolean TestPassedSolutionWithNegativeState(const TColStd_SequenceOfInteger& etat,
28                                                             const TColStd_SequenceOfReal& Umult,
29                                                             const TColStd_SequenceOfReal& Vmult,
30                                                             const TColStd_SequenceOfReal& ustart,
31                                                             const TColStd_SequenceOfReal& vstart,
32                                                             const Standard_Real& prevUp, 
33                                                             const Standard_Real& prevVp,
34                                                             const TColStd_SequenceOfInteger& nbMultiplicities,
35                                                             const math_Vector& tolerance,
36                                                             TheIWFunction& sp,
37                                                             math_Vector& UV,
38                                                             Standard_Integer& Irang);
39 // modified by NIZHNY-MKK  Thu Nov  2 15:07:39 2000.END
40
41
42 void IntWalk_IWalking::ComputeOpenLine(const TColStd_SequenceOfReal& Umult,
43                                        const TColStd_SequenceOfReal& Vmult,
44                                        const ThePOPIterator& Pnts1,
45                                        TheIWFunction& Func,
46                                        Standard_Boolean& Rajout) 
47
48 // Processing of open line.
49 //
50 // 1) for any starting point, which is not passing and not tangent and not yet processed,
51 //    calculation of the step of advancement = step depending on the arrow and the maximum step.
52 //
53 // 2) calculate a point of approach (this point is on the tangent to the section
54 // of distance = no point in the interior)
55 //  
56 // 3) conditions  {
57 //            (all calculated points do not exceed a point in the
58 //             list of starting points)
59 //                              or                    
60 //            (all points do not form an open line going 
61 //            from one border of the domain to the other or from a point tangent 
62 //            to border or from 2 tangent points : single cases)
63 //  
64 //     1) framing of approached point on borders if necessary (there is
65 //        calculation of step)
66 //     2) calculation of the point
67 //     3) if the point is not found the step is divided
68 //     4) stpo tests    
69 //     5) calculation of the step depending on the arrow and the max step,
70 //        (TestDeflection)
71 //        stop  possible.
72 //    end of conditions.
73
74 {
75   Standard_Integer I, N;
76   static math_Vector BornInf(1,2), BornSup(1,2), UVap(1,2);
77   Standard_Real PasC, PasCu, PasCv;
78   Standard_Boolean Arrive; // shows if the line ends
79   Standard_Boolean Cadre;  // shows if one is on border of the domain
80   Standard_Boolean ArretAjout;  //shows if one is on added point
81   IntSurf_PntOn2S Psol;
82   Handle(IntWalk_TheIWLine)  CurrentLine;    // line under construction
83   Standard_Boolean Tgtend;
84
85   IntWalk_StatusDeflection Status, StatusPrecedent;
86   
87   Standard_Integer NbDivision; 
88   // number of divisions of step for each section
89
90   Standard_Integer StepSign;
91   
92   ThePointOfPath PathPnt;
93
94   BornInf(1) = Um;
95   BornSup(1) = UM;
96   BornInf(2) = Vm;
97   BornSup(2) = VM;
98
99   math_FunctionSetRoot Rsnld(Func, tolerance);
100   Standard_Integer nbPath = Pnts1.Length();
101
102   // modified by NIZHNY-MKK  Fri Oct 27 12:32:34 2000.BEGIN
103   TColStd_SequenceOfInteger movementdirectioninfo;
104   for (I = 1; I <= nbPath; I++) {
105     movementdirectioninfo.Append(0);
106   }
107   // modified by NIZHNY-MKK  Fri Oct 27 12:32:38 2000.END
108
109   for (I = 1; I <= nbPath; I++) {
110     //start point of the progression
111     //     if (etat1(I) > 11) {                
112     // modified by NIZHNY-MKK  Fri Oct 27 12:33:37 2000.BEGIN
113     if ((etat1(I) > 11) || ((etat1(I) < -11) && (movementdirectioninfo(I)!=0))) {
114     // modified by NIZHNY-MKK  Fri Oct 27 12:33:43 2000.END
115       PathPnt = Pnts1.Value(I);     
116       CurrentLine = new IntWalk_TheIWLine ();
117       CurrentLine->SetTangencyAtBegining(Standard_False);
118       Tgtend = Standard_False;
119       CurrentLine->AddStatusFirst(Standard_False, Standard_True, I, PathPnt);
120       UVap(1) = ustart1(I);
121       UVap(2) = vstart1(I);
122       MakeWalkingPoint(11, UVap(1), UVap(2), Func, previousPoint);
123       previousd3d = Func.Direction3d();
124       previousd2d = Func.Direction2d();
125       CurrentLine->AddPoint(previousPoint);
126       // modified by NIZHNY-MKK  Fri Oct 27 12:34:32 2000.BEGIN
127       if(movementdirectioninfo(I) !=0) {
128         if(movementdirectioninfo(I) < 0) {
129           StepSign = -1;
130           CurrentLine->SetTangentVector(previousd3d.Reversed(),1);
131         } else {
132           StepSign = 1; 
133           CurrentLine->SetTangentVector(previousd3d,1);
134         }
135       } else {
136         Standard_Real tyutuyt=ThePointOfPathTool::Direction3d(PathPnt) * previousd3d;
137         if( tyutuyt < 0) {
138           StepSign = -1;
139           CurrentLine->SetTangentVector(previousd3d.Reversed(),1);
140         }
141         else {
142           StepSign = 1; 
143           CurrentLine->SetTangentVector(previousd3d,1);
144         }
145       }
146       // modified by NIZHNY-MKK  Fri Oct 27 12:34:37 2000.END
147
148 //  Modified by Sergey KHROMOV - Tue Nov 20 10:41:45 2001 Begin
149       etat1(I) = - abs(etat1(I));
150       movementdirectioninfo(I) = (movementdirectioninfo(I)==0) ? StepSign : 0;
151 //  Modified by Sergey KHROMOV - Tue Nov 20 10:41:56 2001 End
152       // first step of advancement
153       Standard_Real d2dx = Abs(previousd2d.X()); 
154       Standard_Real d2dy = Abs(previousd2d.Y()); 
155       if (d2dx < tolerance(1)) {
156         PasC = pas * (VM-Vm)/d2dy;
157       }
158       else if (d2dy < tolerance(2)) {
159         PasC = pas * (UM-Um)/d2dx;
160       }
161       else {
162         PasC = pas * Min((UM-Um)/d2dx,(VM-Vm)/d2dy);
163       }
164
165       Arrive = Standard_False;
166       ArretAjout = Standard_False;
167       NbDivision = 0;
168       StatusPrecedent = IntWalk_OK;
169       // modified by NIZHNY-MKK  Fri Oct 27 12:39:37 2000
170       Standard_Integer IndexOfPathPointDoNotCheck=0;
171
172       while (!Arrive) { //    as one of stop tests is not checked
173
174         Cadre = Cadrage(BornInf,BornSup,UVap,PasC,StepSign);
175         //  Border?
176
177 #ifdef CHRONO
178         Chronrsnld.Start();
179 #endif
180
181         Rsnld.Perform(Func,UVap,BornInf,BornSup);
182
183 #ifdef CHRONO
184         Chronrsnld.Stop();
185 #endif
186
187         if (Cadre) {
188           BornInf(1) = Um; BornSup(1) = UM; BornInf(2) = Vm; BornSup(2) = VM;
189         }
190         if (Rsnld.IsDone()) {
191           if (Abs(Func.Root()) > Func.Tolerance()) {
192             PasC = PasC / 2.0;
193             PasCu = Abs(PasC*previousd2d.X());
194             PasCv = Abs(PasC*previousd2d.Y());
195             if (PasCu <= tolerance(1) && PasCv <= tolerance(2)) {
196               if (CurrentLine->NbPoints() == 1) break;
197               Arrive = Standard_True;
198               CurrentLine->AddStatusLast(Standard_False);
199               Tgtend = Standard_True; // check
200               Rajout = Standard_True;
201               seqAjout.Append(lines.Length() + 1);
202             }  
203           }
204           else { // test stop
205             Rsnld.Root(UVap);
206             Arrive = TestArretPassage(Umult, Vmult, Func, UVap, N);
207             if (Arrive) {
208               Cadre = Standard_False;
209               //in case if there is a frame and arrive at the same time
210             }
211             else {
212               if (Rajout) {
213                 ArretAjout =TestArretAjout(Func, UVap, N, Psol);
214                 if (ArretAjout) {
215                   // jag 940615
216                   Tgtend = lines.Value(N)->IsTangentAtEnd();
217                   N = -N;
218                 }
219               }
220               // modified by NIZHNY-MKK  Thu Nov  2 15:09:08 2000.BEGIN
221               if(!(Rajout && ArretAjout)) {
222                 Standard_Real prevUp, prevVp;
223                 if (!reversed) {
224                   previousPoint.ParametersOnS2(prevUp, prevVp);
225                 }
226                 else {
227                   previousPoint.ParametersOnS1(prevUp, prevVp);
228                 }
229                 Arrive = TestPassedSolutionWithNegativeState(etat1, Umult, Vmult, ustart1, vstart1, prevUp, prevVp,
230                                                              nbMultiplicities, tolerance, Func, UVap, N);               
231                 if(Arrive) {
232                   Cadre = Standard_False;
233                 }
234               }
235               // modified by NIZHNY-MKK  Thu Nov  2 15:09:13 2000.END
236               if (!ArretAjout && Cadre) {
237                 if (CurrentLine->NbPoints() == 1) break; // cancel the line
238                 TestArretCadre(Umult, Vmult, CurrentLine, Func, UVap, N);
239 //              if (N == 0) {
240                 if (N <= 0) { // jag 941017
241                   MakeWalkingPoint(2, UVap(1), UVap(2), Func, Psol);
242                   Tgtend = Func.IsTangent();
243                   N = -N;
244                 }
245               }
246             }
247             Status = TestDeflection(Func, Arrive, UVap, StatusPrecedent,
248                                     NbDivision,PasC,StepSign);
249             StatusPrecedent = Status;
250             if (Status == IntWalk_PasTropGrand) {
251               Arrive = Standard_False;
252               ArretAjout = Standard_False;
253               Tgtend = Standard_False; // jag 940615
254               if (!reversed) {
255                 previousPoint.ParametersOnS2(UVap(1), UVap(2));
256               }
257               else {
258                 previousPoint.ParametersOnS1(UVap(1), UVap(2));
259               }
260             }
261             else if (ArretAjout || Cadre) {
262               Arrive = Standard_True;
263               CurrentLine->AddStatusLast(Standard_False);
264               if (Status != IntWalk_ArretSurPointPrecedent) {
265                 CurrentLine->AddPoint(Psol);                      
266               }
267               if (Cadre && N==0) {
268                 Rajout = Standard_True;
269                 seqAjout.Append(lines.Length()+1);
270               }
271             }
272             else if (Status == IntWalk_ArretSurPointPrecedent) {
273               if (CurrentLine->NbPoints() == 1) { //cancel the line
274                 Arrive = Standard_False;
275                 break;
276               }
277               Arrive = Standard_True;
278               Rajout = Standard_True;
279               seqAjout.Append(lines.Length() + 1);
280               CurrentLine->AddStatusLast(Standard_False);
281               Tgtend = Standard_True; // check
282             }
283             else if (Arrive)  {
284               if (CurrentLine->NbPoints() == 1 &&    // cancel the line
285                   (N == I || Status == IntWalk_PointConfondu) ) {
286                 // if N == I the main uv is probably lost
287                 // or the point is a point of accumulation
288                 // if point is confused the start data is bad
289                 Arrive =  Standard_False;
290                 break;
291               }
292               // necessairily N > 0 jag 940617
293               // point of stop given at input 
294               PathPnt = Pnts1.Value(N);
295               
296               Standard_Integer etat1N=etat1(N);
297               // modified by NIZHNY-MKK  Thu Nov  2 15:09:51 2000.BEGIN
298               //              if (etat1N < 11) { // passing point that is a stop  
299               if (Abs(etat1N) < 11) { // passing point that is a stop    
300                 // modified by NIZHNY-MKK  Thu Nov  2 15:12:11 2000.END
301                 if (Status == IntWalk_ArretSurPoint) { 
302                   CurrentLine->AddStatusLast(Standard_False);
303                   Tgtend = Standard_True; // need check
304                 }
305                 else { 
306                   Arrive = Standard_False;
307                 }
308                 CurrentLine->AddIndexPassing(N);
309               }
310               else { // point of stop given at input
311                 if (etat1N == 11) {
312                   Tgtend = Standard_True;
313                 }
314                 CurrentLine->AddStatusLast(Standard_True, N, PathPnt);
315               }
316               AddPointInCurrentLine(N,PathPnt,CurrentLine);
317               if ((etat1N != 1 && etat1N != 11)) {
318                 // modified by NIZHNY-MKK  Fri Oct 27 12:43:05 2000.BEGIN
319                 //              etat1(N)= - etat1N;
320                 etat1(N)= - Abs(etat1N);                
321                 movementdirectioninfo(N) = (movementdirectioninfo(N)==0) ? StepSign : 0;
322                 if(Arrive && movementdirectioninfo(N)!=0) {
323                   IndexOfPathPointDoNotCheck = N;
324                 }
325
326                 if(Arrive) {
327                   Rajout = Standard_True;
328                   seqAjout.Append(lines.Length() + 1);
329                 }
330                 // modified by NIZHNY-MKK  Fri Oct 27 12:45:33 2000.END
331               }
332             }
333             else if (Status == IntWalk_ArretSurPoint) {
334               Arrive = Standard_True;                   
335               CurrentLine->AddStatusLast(Standard_False);
336               Tgtend = Standard_True;
337               MakeWalkingPoint(1, UVap(1), UVap(2), Func, Psol);
338               CurrentLine->AddPoint(Psol);
339               Rajout = Standard_True;
340               seqAjout.Append(lines.Length() + 1);
341             }
342             else if (Status == IntWalk_OK) { 
343               MakeWalkingPoint(2, UVap(1), UVap(2), Func, previousPoint);
344               previousd3d = Func.Direction3d();
345               previousd2d = Func.Direction2d();
346               CurrentLine->AddPoint(previousPoint);
347             }                           
348           }
349         }
350         else { // no numerical solution
351           PasC = PasC / 2.;
352           PasCu = Abs(PasC*previousd2d.X());
353           PasCv = Abs(PasC*previousd2d.Y());
354           if (PasCu <= tolerance(1) && PasCv <= tolerance(2)) {
355             if (CurrentLine->NbPoints()==1) break;
356             Arrive = Standard_True;
357             CurrentLine->AddStatusLast(Standard_False);
358             Tgtend = Standard_True; // need check
359             Rajout = Standard_True;
360             seqAjout.Append(lines.Length() + 1);
361           }  
362         }
363       } // end of started line
364       
365       if (Arrive) {
366         CurrentLine->SetTangencyAtEnd(Tgtend);
367         lines.Append(CurrentLine);
368         // modified by NIZHNY-MKK  Fri Oct 27 12:59:29 2000.BEGIN
369         movementdirectioninfo(I)=0;
370         if(etat1(I) > 0)
371         // modified by NIZHNY-MKK  Fri Oct 27 12:59:42 2000.END
372           etat1(I)=-etat1(I);
373
374         //-- lbr le 5 juin 97 (Pb ds Contap)
375         for(Standard_Integer av=1; av<=nbPath; av++) { 
376           // modified by NIZHNY-MKK  Fri Oct 27 13:00:22 2000.BEGIN
377           //      if (etat1(av) > 11) {
378           if ((etat1(av) > 11) || 
379               ((av!=I) && 
380                (av!=IndexOfPathPointDoNotCheck) && 
381                (etat1(av) < -11)  && 
382                (movementdirectioninfo(av)!=0))) {
383           // modified by NIZHNY-MKK  Fri Oct 27 13:00:26 2000.END
384             Standard_Real Uav=ustart1(av);
385             Standard_Real Vav=vstart1(av);
386             Standard_Real Uavp,Vavp;
387             const IntSurf_PntOn2S &avP=CurrentLine->Value(CurrentLine->NbPoints());
388             if (!reversed) {
389               avP.ParametersOnS2(Uavp,Vavp);
390             }
391             else {
392               avP.ParametersOnS1(Uavp,Vavp);
393             }
394             Uav-=Uavp;
395             Vav-=Vavp;
396             Uav*=0.001; Vav*=0.001;
397             if(Abs(Uav)<tolerance(1) && Abs(Vav)<tolerance(2)) { 
398               // modified by NIZHNY-MKK  Fri Oct 27 13:01:38 2000.BEGIN
399               //              etat1(av)=-etat1(av);
400               if(etat1(av) < 0) {
401                 movementdirectioninfo(av) = 0;
402               } else {
403                 etat1(av)=-etat1(av);
404                 movementdirectioninfo(av) = StepSign;
405               }
406               // modified by NIZHNY-MKK  Fri Oct 27 13:01:42 2000.END
407               CurrentLine->AddStatusLast(Standard_True, av, Pnts1.Value(av));
408               //-- cout<<"\n Debug ? lbr ds IntWalk_IWalking_3.gxx"<<endl;
409             }
410             
411             const IntSurf_PntOn2S &avPP=CurrentLine->Value(1);
412             if (!reversed) {
413               avPP.ParametersOnS2(Uavp,Vavp);
414             }
415             else {
416               avPP.ParametersOnS1(Uavp,Vavp);
417             }
418             Uav=ustart1(av);
419             Vav=vstart1(av);
420             Uav-=Uavp;
421             Vav-=Vavp;
422             Uav*=0.001; Vav*=0.001;
423             if(Abs(Uav)<tolerance(1) && Abs(Vav)<tolerance(2)) { 
424               // modified by NIZHNY-MKK  Fri Oct 27 13:02:49 2000.BEGIN
425               //              etat1(av)=-etat1(av);
426               if(etat1(av) < 0) {
427                 movementdirectioninfo(av) = 0;
428               } else {
429                 etat1(av)=-etat1(av);
430                 movementdirectioninfo(av) = -StepSign;
431               }
432               // modified by NIZHNY-MKK  Fri Oct 27 13:02:52 2000.END
433               //-- cout<<"\n Debug ? lbr ds IntWalk_IWalking_3.gxx"<<endl;
434               CurrentLine->AddStatusFirst(Standard_False, Standard_True, av, Pnts1.Value(av));
435             }
436           }
437         }
438           
439       }
440     } //end of point processing
441   } //end of all points
442 }
443
444 // modified by NIZHNY-MKK  Thu Nov  2 15:07:53 2000.BEGIN
445 static Standard_Boolean TestPassedSolutionWithNegativeState(const TColStd_SequenceOfInteger& etat,
446                                                             const TColStd_SequenceOfReal& Umult,
447                                                             const TColStd_SequenceOfReal& Vmult,
448                                                             const TColStd_SequenceOfReal& ustart,
449                                                             const TColStd_SequenceOfReal& vstart,
450                                                             const Standard_Real& prevUp,
451                                                             const Standard_Real& prevVp,
452                                                             const TColStd_SequenceOfInteger& nbMultiplicities,
453                                                             const math_Vector& tolerance,
454                                                             TheIWFunction& sp,
455                                                             math_Vector& UV,
456                                                             Standard_Integer& Irang) {
457   Standard_Boolean Arrive = Standard_False;
458   Standard_Real Dup, Dvp, Utest,Vtest;
459   Standard_Real tolu = tolerance(1);
460   Standard_Real tolv = tolerance(2);
461   Standard_Integer i, j, k, N;
462   for (i = 1; i <= etat.Length(); i++) {
463     if (etat(i) < -11) {
464
465  // debug jag see with isg
466
467       Utest = ustart(i);
468       Vtest = vstart(i);
469       Dup = prevUp - Utest;
470       Dvp = prevVp - Vtest;
471       if (Abs(Dup) >= tolu || Abs(Dvp) >= tolv) {
472         Standard_Real UV1mUtest = UV(1)-Utest;
473         Standard_Real UV2mVtest = UV(2)-Vtest;
474         if(( (Dup*UV1mUtest + Dvp*UV2mVtest) < 0) ||
475            (   Abs(UV1mUtest) < tolu 
476             && Abs(UV2mVtest) < tolv)) {
477           Irang=i;
478           Arrive = Standard_True;
479           UV(1) = Utest;
480           UV(2) = Vtest;
481         }
482         else if (nbMultiplicities(i) > 0) {
483           N=0;
484           for (k = 1; k < i; k++) { 
485             N+=nbMultiplicities(k);
486           }
487           for (j = N + 1; j <= N + nbMultiplicities(i); j++) {
488             if (((prevUp-Umult(j))*(UV(1)-Umult(j)) +
489                  (prevVp-Vmult(j))*(UV(2)-Vmult(j)) < 0) ||
490                 (Abs(UV(1)-Umult(j)) < tolu &&
491                  Abs(UV(2)-Vmult(j)) < tolv)) {
492               Irang=i;
493               Arrive = Standard_True;
494               UV(1) = Utest;
495               UV(2) = Vtest;
496               break;
497             }
498           }
499         }
500         if (Arrive) {
501           static math_Vector bidF(1,1);
502           static math_Matrix bidD(1,1,1,2);
503       sp.Values(UV,bidF,bidD);
504           break;
505         }
506       }
507     }    
508   }
509   return Arrive;
510 }
511 // modified by NIZHNY-MKK  Thu Nov  2 15:07:58 2000.END