0022848: Optimize projection of points in ShapeAnalysis_Surface
[occt.git] / src / Extrema / Extrema_CurveCache.gxx
1 // File     : Extrema_CurveCache.gxx
2 // Created  : Sun Dec 28 2008
3 // Author   : Roman Lygin
4 //            roman.lygin@gmail.com
5 // Copyright: Roman Lygin, Open CASCADE 2008
6
7 #include <Precision.hxx>
8
9 //=======================================================================
10 //function : Extrema_CurveCache
11 //purpose  : 
12 //=======================================================================
13
14 Extrema_CurveCache::Extrema_CurveCache(const Curve& theC,
15                                    const Standard_Real theUFirst,
16                                    const Standard_Real theULast,
17                                    const Standard_Integer theNbSamples,
18                                    const Standard_Boolean theToCalculate) :
19     myC (0), myNbSamples (-1), myIsArrayValid (Standard_False)
20 {
21   SetCurve (theC, theUFirst, theULast, theNbSamples, theToCalculate);
22 }
23
24 //=======================================================================
25 //function : Extrema_CurveCache
26 //purpose  : 
27 //=======================================================================
28
29 Extrema_CurveCache::Extrema_CurveCache() : myC (0), myNbSamples (-1),
30     myIsArrayValid (Standard_False)
31 {
32 }
33
34 //=======================================================================
35 //function : SetCurve
36 //purpose  : 
37 //=======================================================================
38
39 void Extrema_CurveCache::SetCurve (const Curve& theC,
40                                    const Standard_Integer theNbSamples,
41                                    const Standard_Boolean theToCalculate)
42 {
43   myC = (Standard_Address)&theC;
44   myNbSamples = theNbSamples;
45   myIsArrayValid = Standard_False;
46   myPntArray.Nullify();
47   if (theToCalculate) {
48     CalculatePoints();
49   }
50 }
51
52 //=======================================================================
53 //function : SetCurve
54 //purpose  : 
55 //=======================================================================
56
57 void Extrema_CurveCache::SetCurve (const Curve& theC,
58                                    const Standard_Real theUFirst,
59                                    const Standard_Real theULast,
60                                    const Standard_Integer theNbSamples,
61                                    const Standard_Boolean theToCalculate)
62 {
63   SetCurve (theC, theNbSamples, Standard_False); //no calculation
64   SetRange (theUFirst, theULast, theToCalculate);
65 }
66
67 //=======================================================================
68 //function : SetRange
69 //purpose  : 
70 //=======================================================================
71
72 void Extrema_CurveCache::SetRange (const Standard_Real theUFirst,
73                                    const Standard_Real theULast,
74                                    const Standard_Boolean theToCalculate)
75 {
76   //myTrimFirst and myTrimLast are used to compute values on unlimited curves
77   myTrimFirst = myFirst = theUFirst;
78   if (Precision::IsInfinite(myTrimFirst)){
79     myTrimFirst = -1.0e+10;
80   }
81   myTrimLast = myLast = theULast;
82   if (Precision::IsInfinite(myTrimLast)){
83     myTrimLast = 1.0e+10;
84   }
85
86   myIsArrayValid = Standard_False;
87   myPntArray.Nullify();
88   if (theToCalculate) {
89     CalculatePoints();
90   }
91 }
92
93 //=======================================================================
94 //function : CalculatePoints
95 //purpose  : 
96 //=======================================================================
97
98 void Extrema_CurveCache::CalculatePoints()
99 {
100   if (myIsArrayValid) return; //no need to recalculate if nothing has changed
101   const Curve& aCurve = *((Curve*)myC);
102
103   // compute myNbSamples point along the [myTrimFirst, myTrimLast] range
104
105   Standard_Real aDelta = myTrimLast - myTrimFirst;
106   Standard_Real aPar0 = aDelta / myNbSamples / 100.;
107   aDelta = (aDelta - aPar0) / (myNbSamples - 1);
108   aPar0 = myTrimFirst + (aPar0/2.);
109
110   //Cache points
111
112   myPntArray = new ArrayOfPnt (1, myNbSamples);
113
114   Standard_Integer i;
115   Standard_Real aPar;
116   for (i = 1, aPar = aPar0; i <= myNbSamples; i++, aPar += aDelta) {
117     myPntArray->SetValue (i, aCurve.Value (aPar));
118   }
119
120   myIsArrayValid = Standard_True; //cache is available now
121 }