1 // Created on: 2008-12-28
2 // Created by: Roman Lygin
3 // Copyright (c) 2008-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 // roman.lygin@gmail.com
22 #include <Precision.hxx>
24 //=======================================================================
25 //function : Extrema_CurveCache
27 //=======================================================================
29 Extrema_CurveCache::Extrema_CurveCache(const Curve& theC,
30 const Standard_Real theUFirst,
31 const Standard_Real theULast,
32 const Standard_Integer theNbSamples,
33 const Standard_Boolean theToCalculate) :
34 myC (0), myNbSamples (-1), myIsArrayValid (Standard_False)
36 SetCurve (theC, theUFirst, theULast, theNbSamples, theToCalculate);
39 //=======================================================================
40 //function : Extrema_CurveCache
41 //purpose : with sampling by knots and between them
42 //=======================================================================
44 Extrema_CurveCache::Extrema_CurveCache(const Curve& theC,
45 const Standard_Real theUFirst,
46 const Standard_Real theULast,
47 const TColStd_Array1OfReal& IntervalsCN,
48 const Standard_Integer StartIndex,
49 const Standard_Integer EndIndex,
50 const Standard_Real Coeff)
52 myC = (Standard_Address)&theC;
53 myIsArrayValid = Standard_False;
54 myParamArray.Nullify();
57 myTrimFirst = myFirst = theUFirst;
58 myTrimLast = myLast = theULast;
60 Standard_Integer Nbp = 2;
61 if (2 * Coeff < 10000.0)
62 Nbp = (Standard_Integer) (2 * Coeff);
63 myNbSamples = (EndIndex - StartIndex)*Nbp + 1;
65 const Standard_Integer aNbSTresh = 10000;
66 if (myNbSamples > aNbSTresh)
68 Nbp = aNbSTresh / (EndIndex - StartIndex);
69 myNbSamples = (EndIndex - StartIndex)*Nbp + 1;
73 myParamArray = new TColStd_HArray1OfReal(1, myNbSamples);
74 myPntArray = new ArrayOfPnt (1, myNbSamples);
76 const Curve& aCurve = *((Curve*)myC);
78 Standard_Integer i, j, k = 1;
80 for (i = StartIndex; i < EndIndex; i++)
82 Standard_Real delta = (IntervalsCN(i+1) - IntervalsCN(i)) / Nbp;
83 for (j = 0; j < Nbp; j++)
85 aPar = IntervalsCN(i) + j*delta;
86 myParamArray->SetValue(k, aPar);
87 myPntArray->SetValue(k++, aCurve.Value(aPar));
90 Standard_Real aDelta = (myTrimLast - myTrimFirst) / myNbSamples / 200.;
91 myParamArray->SetValue(1, myTrimFirst + aDelta);
92 myPntArray->SetValue(1, aCurve.Value(myTrimFirst + aDelta));
93 myParamArray->SetValue(myNbSamples, myTrimLast - aDelta);
94 myPntArray->SetValue(myNbSamples, aCurve.Value(myTrimLast - aDelta));
96 myIsArrayValid = Standard_True; //cache is available now
99 //=======================================================================
100 //function : Extrema_CurveCache
102 //=======================================================================
104 Extrema_CurveCache::Extrema_CurveCache() : myC (0), myNbSamples (-1),
105 myIsArrayValid (Standard_False)
109 //=======================================================================
110 //function : SetCurve
112 //=======================================================================
114 void Extrema_CurveCache::SetCurve (const Curve& theC,
115 const Standard_Integer theNbSamples,
116 const Standard_Boolean theToCalculate)
118 myC = (Standard_Address)&theC;
119 myNbSamples = theNbSamples;
120 myIsArrayValid = Standard_False;
121 myParamArray.Nullify();
122 myPntArray.Nullify();
123 if (theToCalculate) {
128 //=======================================================================
129 //function : SetCurve
131 //=======================================================================
133 void Extrema_CurveCache::SetCurve (const Curve& theC,
134 const Standard_Real theUFirst,
135 const Standard_Real theULast,
136 const Standard_Integer theNbSamples,
137 const Standard_Boolean theToCalculate)
139 SetCurve (theC, theNbSamples, Standard_False); //no calculation
140 SetRange (theUFirst, theULast, theToCalculate);
143 //=======================================================================
144 //function : SetRange
146 //=======================================================================
148 void Extrema_CurveCache::SetRange (const Standard_Real theUFirst,
149 const Standard_Real theULast,
150 const Standard_Boolean theToCalculate)
152 //myTrimFirst and myTrimLast are used to compute values on unlimited curves
153 myTrimFirst = myFirst = theUFirst;
154 if (Precision::IsInfinite(myTrimFirst)){
155 myTrimFirst = -1.0e+10;
157 myTrimLast = myLast = theULast;
158 if (Precision::IsInfinite(myTrimLast)){
159 myTrimLast = 1.0e+10;
162 myIsArrayValid = Standard_False;
163 myParamArray.Nullify();
164 myPntArray.Nullify();
165 if (theToCalculate) {
170 //=======================================================================
171 //function : CalculatePoints
173 //=======================================================================
175 void Extrema_CurveCache::CalculatePoints()
177 if (myIsArrayValid) return; //no need to recalculate if nothing has changed
178 const Curve& aCurve = *((Curve*)myC);
180 // compute myNbSamples point along the [myTrimFirst, myTrimLast] range
182 Standard_Real aDelta = myTrimLast - myTrimFirst;
183 Standard_Real aPar0 = aDelta / myNbSamples / 100.;
184 aDelta = (aDelta - aPar0) / (myNbSamples - 1);
185 aPar0 = myTrimFirst + (aPar0/2.);
189 myParamArray = new TColStd_HArray1OfReal(1, myNbSamples);
190 myPntArray = new ArrayOfPnt (1, myNbSamples);
194 for (i = 1, aPar = aPar0; i <= myNbSamples; i++, aPar += aDelta) {
195 myParamArray->SetValue(i, aPar);
196 myPntArray->SetValue (i, aCurve.Value (aPar));
199 myIsArrayValid = Standard_True; //cache is available now