0028990: Coding Rules - deprecate redundant class Prs3d_Root
[occt.git] / src / StdPrs / StdPrs_DeflectionCurve.cxx
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 // Great zoom leads to non-coincidence of
16 // a point and non-infinite lines passing throught this point:
17
18 #include <Adaptor3d_Curve.hxx>
19 #include <Bnd_Box.hxx>
20 #include <BndLib_Add3dCurve.hxx>
21 #include <GCPnts_QuasiUniformDeflection.hxx>
22 #include <GCPnts_TangentialDeflection.hxx>
23 #include <gp_Circ.hxx>
24 #include <gp_Dir.hxx>
25 #include <gp_Pnt.hxx>
26 #include <gp_Vec.hxx>
27 #include <Graphic3d_ArrayOfPolylines.hxx>
28 #include <Graphic3d_ArrayOfSegments.hxx>
29 #include <Graphic3d_Group.hxx>
30 #include <Precision.hxx>
31 #include <Prs3d.hxx>
32 #include <Prs3d_Arrow.hxx>
33 #include <Prs3d_ArrowAspect.hxx>
34 #include <Prs3d_LineAspect.hxx>
35 #include <Prs3d_Presentation.hxx>
36 #include <StdPrs_DeflectionCurve.hxx>
37 #include <TColgp_SequenceOfPnt.hxx>
38 #include <TColStd_Array1OfReal.hxx>
39
40 //==================================================================
41 // function: GetDeflection
42 // purpose:
43 //==================================================================
44 static Standard_Real GetDeflection(const Adaptor3d_Curve&        aCurve,
45                                    const Standard_Real         U1, 
46                                    const Standard_Real         U2, 
47                                    const Handle(Prs3d_Drawer)& aDrawer)
48 {
49   Standard_Real TheDeflection;
50
51   if (aDrawer->TypeOfDeflection() == Aspect_TOD_RELATIVE)
52   {
53     // On calcule la fleche en fonction des min max globaux de la piece:
54     Bnd_Box Total;
55     BndLib_Add3dCurve::Add(aCurve, U1, U2, 0.,Total);
56     Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
57     Total.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
58     Standard_Real m = RealLast();
59     if ( ! (Total.IsOpenXmin() || Total.IsOpenXmax() ))
60       m = Abs (aXmax-aXmin);
61     if ( ! (Total.IsOpenYmin() || Total.IsOpenYmax() ))
62       m = Max ( m , Abs (aYmax-aYmin));
63     if ( ! (Total.IsOpenZmin() || Total.IsOpenZmax() ))
64       m = Max ( m , Abs (aZmax-aZmin));
65       
66     m = Min ( m , aDrawer->MaximalParameterValue());
67     m = Max(m, Precision::Confusion());
68       
69     TheDeflection = m * aDrawer->DeviationCoefficient();
70   }
71   else
72     TheDeflection = aDrawer->MaximalChordialDeviation();
73
74   return TheDeflection;
75 }
76
77 //==================================================================
78 // function: FindLimits
79 // purpose:
80 //==================================================================
81 static Standard_Boolean FindLimits(const Adaptor3d_Curve& aCurve,
82                                    const Standard_Real  aLimit,
83                                    Standard_Real&       First,
84                                    Standard_Real&       Last)
85 {
86   First = aCurve.FirstParameter();
87   Last  = aCurve.LastParameter();
88   Standard_Boolean firstInf = Precision::IsNegativeInfinite(First);
89   Standard_Boolean lastInf  = Precision::IsPositiveInfinite(Last);
90
91   if (firstInf || lastInf) {
92     gp_Pnt P1,P2;
93     Standard_Real delta = 1;
94     Standard_Integer count = 0;
95     if (firstInf && lastInf) {
96       do {
97         if (count++ == 100000) return Standard_False;
98           delta *= 2;
99           First = - delta;
100           Last  =   delta;
101           aCurve.D0(First,P1);
102           aCurve.D0(Last,P2);
103       } while (P1.Distance(P2) < aLimit);
104     }
105     else if (firstInf) {
106       aCurve.D0(Last,P2);
107       do {
108         if (count++ == 100000) return Standard_False;
109           delta *= 2;
110           First = Last - delta;
111           aCurve.D0(First,P1);
112       } while (P1.Distance(P2) < aLimit);
113     }
114     else if (lastInf) {
115       aCurve.D0(First,P1);
116       do {
117         if (count++ == 100000) return Standard_False;
118           delta *= 2;
119           Last = First + delta;
120           aCurve.D0(Last,P2);
121       } while (P1.Distance(P2) < aLimit);
122     }
123   }    
124   return Standard_True;
125 }
126
127
128 //==================================================================
129 // function: drawCurve
130 // purpose:
131 //==================================================================
132 static void drawCurve (Adaptor3d_Curve&              aCurve,
133                        const Handle(Graphic3d_Group)& aGroup,
134                        const Standard_Real           TheDeflection,
135                        const Standard_Real           anAngle,
136                        const Standard_Real           U1,
137                        const Standard_Real           U2,
138                        TColgp_SequenceOfPnt&         Points)
139 {
140   switch (aCurve.GetType())
141   {
142     case GeomAbs_Line:
143     {
144       gp_Pnt p1 = aCurve.Value(U1);
145       gp_Pnt p2 = aCurve.Value(U2);
146       Points.Append(p1);
147       Points.Append(p2);
148       if (!aGroup.IsNull())
149       {
150         Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
151         aPrims->AddVertex(p1);
152         aPrims->AddVertex(p2);
153         aGroup->AddPrimitiveArray(aPrims);
154       }
155       break;
156     }
157     default:
158     {
159       const Standard_Integer nbinter = aCurve.NbIntervals(GeomAbs_C1);
160       TColStd_Array1OfReal T(1, nbinter+1);
161       aCurve.Intervals(T, GeomAbs_C1);
162
163       Standard_Real theU1, theU2;
164       Standard_Integer NumberOfPoints, i, j;
165       TColgp_SequenceOfPnt SeqP;
166
167       for (j = 1; j <= nbinter; j++) {
168         theU1 = T(j); theU2 = T(j+1);
169         if (theU2 > U1 && theU1 < U2) {
170           theU1 = Max(theU1, U1);
171           theU2 = Min(theU2, U2);
172           
173           GCPnts_TangentialDeflection Algo(aCurve, theU1, theU2, anAngle, TheDeflection);
174           NumberOfPoints = Algo.NbPoints();
175
176           if (NumberOfPoints > 0) {
177             for (i = 1; i <= NumberOfPoints; i++)
178               SeqP.Append(Algo.Value(i)); 
179           }
180         }
181       }
182
183       Handle(Graphic3d_ArrayOfPolylines) aPrims;
184       if (!aGroup.IsNull())
185         aPrims = new Graphic3d_ArrayOfPolylines(SeqP.Length());
186       
187       for (i = 1; i <= SeqP.Length(); i++) { 
188         const gp_Pnt& p = SeqP.Value(i);
189         Points.Append(p);
190         if (!aGroup.IsNull())
191         {
192           aPrims->AddVertex(p);
193         }
194       }
195       if (!aGroup.IsNull())
196       {
197         aGroup->AddPrimitiveArray (aPrims);
198       }
199     }
200   }
201 }
202
203
204 //==================================================================
205 // function: MatchCurve
206 // purpose:
207 //==================================================================
208 static Standard_Boolean MatchCurve (
209                        const Standard_Real X,
210                        const Standard_Real Y,
211                        const Standard_Real Z,
212                        const Standard_Real aDistance,
213                        const Adaptor3d_Curve&  aCurve,
214                        const Standard_Real TheDeflection,
215                        const Standard_Real   anAngle,
216                        const Standard_Real   U1,
217                        const Standard_Real   U2)
218 {
219   Standard_Real retdist;
220   switch (aCurve.GetType())
221   {
222     case GeomAbs_Line:
223     {
224       gp_Pnt p1 = aCurve.Value(U1);
225       if ( Abs(X-p1.X()) + Abs(Y-p1.Y()) + Abs(Z-p1.Z()) <= aDistance)
226         return Standard_True;
227       gp_Pnt p2 = aCurve.Value(U2);
228       if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
229         return Standard_True;
230       return Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist);
231     }
232     case GeomAbs_Circle:
233     {
234       const Standard_Real Radius = aCurve.Circle().Radius();
235       if (!Precision::IsInfinite(Radius)) {
236         const Standard_Real DU = Sqrt(8.0 * TheDeflection / Radius);
237         const Standard_Real Er = Abs( U2 - U1) / DU;
238         const Standard_Integer N = Max(2, (Standard_Integer)IntegerPart(Er));
239         if ( N > 0) {
240           gp_Pnt p1,p2;
241           for (Standard_Integer Index = 1; Index <= N+1; Index++) {
242             p2 = aCurve.Value(U1 + (Index - 1) * DU);
243             if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
244               return Standard_True;
245
246             if (Index>1) { 
247               if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist)) 
248                 return Standard_True;
249             }
250             p1=p2;
251           }
252         }
253       }
254       break;
255     }
256     default:
257     {
258       GCPnts_TangentialDeflection Algo(aCurve,U1, U2, anAngle, TheDeflection);
259       const Standard_Integer NumberOfPoints = Algo.NbPoints();
260       if (NumberOfPoints > 0) {
261         gp_Pnt p1,p2;
262         for (Standard_Integer i=1;i<=NumberOfPoints;i++) { 
263           p2 = Algo.Value(i);
264           if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
265             return Standard_True;
266           if (i>1) { 
267             if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist)) 
268               return Standard_True;
269           }
270           p1=p2;
271         }
272       }
273     }
274   }
275   return Standard_False;
276 }
277
278
279 //==================================================================
280 // function: Add
281 // purpose:
282 //==================================================================
283 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
284                                   Adaptor3d_Curve&                   aCurve,
285                                   const Handle (Prs3d_Drawer)&       aDrawer,
286                                   const Standard_Boolean theToDrawCurve)
287 {
288   Handle(Graphic3d_Group) aGroup;
289   if (theToDrawCurve)
290   {
291     aGroup = aPresentation->CurrentGroup();
292     aGroup->SetPrimitivesAspect (aDrawer->LineAspect()->Aspect());
293   }
294
295   Standard_Real V1, V2;
296   if (FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
297   {
298     TColgp_SequenceOfPnt Points;
299     drawCurve(aCurve,
300               aGroup,
301               GetDeflection(aCurve, V1, V2, aDrawer),
302               aDrawer->DeviationAngle(),
303               V1, V2, Points);
304
305     if (aDrawer->LineArrowDraw()
306     && !aGroup.IsNull())
307     {
308       gp_Pnt Location;
309       gp_Vec Direction;
310       aCurve.D1(V2, Location,Direction);
311       Prs3d_Arrow::Draw (aGroup,
312                          Location,
313                          gp_Dir(Direction),
314                          aDrawer->ArrowAspect()->Angle(),
315                          aDrawer->ArrowAspect()->Length());
316     }
317   }
318 }
319
320
321 //==================================================================
322 // function: Add
323 // purpose:
324 //==================================================================
325 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
326                                   Adaptor3d_Curve&                   aCurve,
327                                   const Standard_Real                U1,
328                                   const Standard_Real                U2,
329                                   const Handle (Prs3d_Drawer)&       aDrawer,
330                                   const Standard_Boolean theToDrawCurve)
331 {
332   Handle(Graphic3d_Group) aGroup;
333   if (theToDrawCurve)
334   {
335     aGroup = aPresentation->CurrentGroup();
336     aGroup->SetPrimitivesAspect(aDrawer->LineAspect()->Aspect());
337   }
338
339   Standard_Real V1 = U1;
340   Standard_Real V2 = U2;  
341
342   if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
343   if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
344
345   TColgp_SequenceOfPnt Points;
346   drawCurve(aCurve,
347             aGroup,
348             GetDeflection(aCurve, V1, V2, aDrawer),
349             aDrawer->DeviationAngle(),
350             V1 , V2, Points);
351
352   if (aDrawer->LineArrowDraw()
353   && !aGroup.IsNull())
354   {
355     gp_Pnt Location;
356     gp_Vec Direction;
357     aCurve.D1(V2, Location,Direction);
358     Prs3d_Arrow::Draw (aGroup,
359                        Location,
360                        gp_Dir(Direction),
361                        aDrawer->ArrowAspect()->Angle(),
362                        aDrawer->ArrowAspect()->Length());
363   }
364 }
365
366 //==================================================================
367 // function: Add
368 // purpose:
369 //==================================================================
370 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
371                                   Adaptor3d_Curve&                   aCurve,
372                                   const Standard_Real                U1,
373                                   const Standard_Real                U2,
374                                   const Standard_Real                aDeflection,
375                                   TColgp_SequenceOfPnt&              Points,
376                                   const Standard_Real                anAngle,
377                                   const Standard_Boolean theToDrawCurve)
378 {
379   Handle(Graphic3d_Group) aGroup;
380   if (theToDrawCurve)
381   {
382     aGroup = aPresentation->CurrentGroup();
383   }
384
385   drawCurve (aCurve, aGroup, aDeflection, anAngle, U1, U2, Points);
386 }
387
388 //==================================================================
389 // function: Add
390 // purpose:
391 //==================================================================
392 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
393                                   Adaptor3d_Curve&                   aCurve,
394                                   const Standard_Real                aDeflection,
395                                   const Standard_Real                aLimit,
396                                   const Standard_Real                anAngle,
397                                   const Standard_Boolean theToDrawCurve)
398 {
399   Standard_Real V1, V2;
400   if (!FindLimits(aCurve, aLimit, V1, V2))
401   {
402     return;
403   }
404
405   Handle(Graphic3d_Group) aGroup;
406   if (theToDrawCurve)
407   {
408     aGroup = aPresentation->CurrentGroup();
409   }
410
411   TColgp_SequenceOfPnt Points;
412   drawCurve (aCurve, aGroup, aDeflection, anAngle, V1, V2, Points);
413 }
414
415
416 //================================================================================
417 // function: Add
418 // purpose:
419 //================================================================================
420 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
421                                   Adaptor3d_Curve&                   aCurve,
422                                   const Standard_Real                aDeflection,
423                                   const Handle(Prs3d_Drawer)&        aDrawer,
424                                   TColgp_SequenceOfPnt&              Points,
425                                   const Standard_Boolean theToDrawCurve)
426 {
427   Standard_Real V1, V2;
428   if (!FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
429   {
430     return;
431   }
432
433   Handle(Graphic3d_Group) aGroup;
434   if (theToDrawCurve)
435   {
436     aGroup = aPresentation->CurrentGroup();
437   }
438   drawCurve (aCurve, aGroup, aDeflection, aDrawer->DeviationAngle(), V1, V2, Points);
439 }
440
441
442 //==================================================================
443 // function: Match
444 // purpose:
445 //==================================================================
446 Standard_Boolean StdPrs_DeflectionCurve::Match 
447                       (const Standard_Real        X,
448                        const Standard_Real        Y,
449                        const Standard_Real        Z,
450                        const Standard_Real        aDistance,
451                        const Adaptor3d_Curve&         aCurve,
452                        const Handle (Prs3d_Drawer)& aDrawer) 
453 {
454   Standard_Real V1, V2;
455   if (FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
456   {
457     return MatchCurve(X,Y,Z,aDistance,aCurve,
458                       GetDeflection(aCurve, V1, V2, aDrawer),
459                       aDrawer->DeviationAngle(),
460                       V1, V2);
461   }
462   return Standard_False;
463 }
464
465 //==================================================================
466 // function: Match
467 // purpose:
468 //==================================================================
469 Standard_Boolean StdPrs_DeflectionCurve::Match 
470                         (const Standard_Real        X,
471                          const Standard_Real        Y,
472                          const Standard_Real        Z,
473                          const Standard_Real        aDistance,
474                          const Adaptor3d_Curve&         aCurve,
475                          const Standard_Real          U1,
476                          const Standard_Real          U2,
477                          const Handle (Prs3d_Drawer)& aDrawer) 
478 {
479   Standard_Real V1 = U1;
480   Standard_Real V2 = U2;
481
482   if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
483   if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
484
485   return MatchCurve(X,Y,Z,aDistance,aCurve,
486                     GetDeflection(aCurve, V1, V2, aDrawer),
487                     aDrawer->DeviationAngle(), V1, V2);
488 }
489
490 //==================================================================
491 // function: Match
492 // purpose:
493 //==================================================================
494 Standard_Boolean StdPrs_DeflectionCurve::Match 
495                         (const Standard_Real X,
496                  const Standard_Real Y,
497                  const Standard_Real Z,
498                  const Standard_Real aDistance,
499                  const Adaptor3d_Curve&  aCurve,
500                  const Standard_Real   U1,
501                  const Standard_Real   U2,
502                  const Standard_Real   aDeflection,
503                  const Standard_Real   anAngle)
504 {
505   return MatchCurve(X,Y,Z,aDistance,aCurve,aDeflection,anAngle,U1,U2);
506 }
507
508 //==================================================================
509 // function: Match
510 // purpose:
511 //==================================================================
512 Standard_Boolean StdPrs_DeflectionCurve::Match 
513                           (const Standard_Real X,
514                            const Standard_Real Y,
515                            const Standard_Real Z,
516                            const Standard_Real aDistance,
517                            const Adaptor3d_Curve&  aCurve,
518                            const Standard_Real   aDeflection,
519                            const Standard_Real   aLimit,
520                            const Standard_Real   anAngle)
521 {
522   Standard_Real V1, V2;
523   if (FindLimits(aCurve, aLimit, V1, V2))
524   {
525     return MatchCurve(X,Y,Z,aDistance,aCurve,aDeflection,anAngle,V1,V2);
526   }
527   return Standard_False;
528 }