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