b311480e |
1 | // Created on: 2008-12-28 |
2 | // Created by: Roman Lygin |
973c2be1 |
3 | // Copyright (c) 2008-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
973c2be1 |
7 | // This library is free software; you can redistribute it and / or modify it |
8 | // under the terms of the GNU Lesser General Public version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
b311480e |
15 | |
7fd59977 |
16 | // roman.lygin@gmail.com |
7fd59977 |
17 | |
18 | #include <Precision.hxx> |
19 | |
20 | //======================================================================= |
21 | //function : Extrema_CurveCache |
22 | //purpose : |
23 | //======================================================================= |
24 | |
25 | Extrema_CurveCache::Extrema_CurveCache(const Curve& theC, |
26 | const Standard_Real theUFirst, |
27 | const Standard_Real theULast, |
28 | const Standard_Integer theNbSamples, |
29 | const Standard_Boolean theToCalculate) : |
30 | myC (0), myNbSamples (-1), myIsArrayValid (Standard_False) |
31 | { |
32 | SetCurve (theC, theUFirst, theULast, theNbSamples, theToCalculate); |
33 | } |
34 | |
afed7fd7 |
35 | //======================================================================= |
36 | //function : Extrema_CurveCache |
37 | //purpose : with sampling by knots and between them |
38 | //======================================================================= |
39 | |
40 | Extrema_CurveCache::Extrema_CurveCache(const Curve& theC, |
41 | const Standard_Real theUFirst, |
42 | const Standard_Real theULast, |
43 | const TColStd_Array1OfReal& IntervalsCN, |
44 | const Standard_Integer StartIndex, |
45 | const Standard_Integer EndIndex, |
46 | const Standard_Real Coeff) |
47 | { |
48 | myC = (Standard_Address)&theC; |
49 | myIsArrayValid = Standard_False; |
50 | myParamArray.Nullify(); |
51 | myPntArray.Nullify(); |
52 | |
53 | myTrimFirst = myFirst = theUFirst; |
54 | myTrimLast = myLast = theULast; |
55 | |
aa094b3e |
56 | Standard_Integer Nbp = 3; |
84f48301 |
57 | if (2 * Coeff < 10000.0) |
aa094b3e |
58 | Nbp = Max((Standard_Integer) (2 * Coeff), Nbp); |
afed7fd7 |
59 | myNbSamples = (EndIndex - StartIndex)*Nbp + 1; |
60 | |
61 | const Standard_Integer aNbSTresh = 10000; |
62 | if (myNbSamples > aNbSTresh) |
63 | { |
64 | Nbp = aNbSTresh / (EndIndex - StartIndex); |
65 | myNbSamples = (EndIndex - StartIndex)*Nbp + 1; |
66 | } |
67 | |
68 | //Cache points |
69 | myParamArray = new TColStd_HArray1OfReal(1, myNbSamples); |
70 | myPntArray = new ArrayOfPnt (1, myNbSamples); |
71 | |
72 | const Curve& aCurve = *((Curve*)myC); |
73 | |
74 | Standard_Integer i, j, k = 1; |
75 | Standard_Real aPar; |
76 | for (i = StartIndex; i < EndIndex; i++) |
77 | { |
78 | Standard_Real delta = (IntervalsCN(i+1) - IntervalsCN(i)) / Nbp; |
79 | for (j = 0; j < Nbp; j++) |
80 | { |
81 | aPar = IntervalsCN(i) + j*delta; |
82 | myParamArray->SetValue(k, aPar); |
83 | myPntArray->SetValue(k++, aCurve.Value(aPar)); |
84 | } |
85 | } |
86 | Standard_Real aDelta = (myTrimLast - myTrimFirst) / myNbSamples / 200.; |
87 | myParamArray->SetValue(1, myTrimFirst + aDelta); |
88 | myPntArray->SetValue(1, aCurve.Value(myTrimFirst + aDelta)); |
89 | myParamArray->SetValue(myNbSamples, myTrimLast - aDelta); |
90 | myPntArray->SetValue(myNbSamples, aCurve.Value(myTrimLast - aDelta)); |
91 | |
92 | myIsArrayValid = Standard_True; //cache is available now |
93 | } |
94 | |
7fd59977 |
95 | //======================================================================= |
96 | //function : Extrema_CurveCache |
97 | //purpose : |
98 | //======================================================================= |
99 | |
100 | Extrema_CurveCache::Extrema_CurveCache() : myC (0), myNbSamples (-1), |
101 | myIsArrayValid (Standard_False) |
102 | { |
103 | } |
104 | |
105 | //======================================================================= |
106 | //function : SetCurve |
107 | //purpose : |
108 | //======================================================================= |
109 | |
110 | void Extrema_CurveCache::SetCurve (const Curve& theC, |
111 | const Standard_Integer theNbSamples, |
112 | const Standard_Boolean theToCalculate) |
113 | { |
114 | myC = (Standard_Address)&theC; |
115 | myNbSamples = theNbSamples; |
116 | myIsArrayValid = Standard_False; |
afed7fd7 |
117 | myParamArray.Nullify(); |
7fd59977 |
118 | myPntArray.Nullify(); |
119 | if (theToCalculate) { |
120 | CalculatePoints(); |
121 | } |
122 | } |
123 | |
124 | //======================================================================= |
125 | //function : SetCurve |
126 | //purpose : |
127 | //======================================================================= |
128 | |
129 | void Extrema_CurveCache::SetCurve (const Curve& theC, |
130 | const Standard_Real theUFirst, |
131 | const Standard_Real theULast, |
132 | const Standard_Integer theNbSamples, |
133 | const Standard_Boolean theToCalculate) |
134 | { |
135 | SetCurve (theC, theNbSamples, Standard_False); //no calculation |
136 | SetRange (theUFirst, theULast, theToCalculate); |
137 | } |
138 | |
139 | //======================================================================= |
140 | //function : SetRange |
141 | //purpose : |
142 | //======================================================================= |
143 | |
144 | void Extrema_CurveCache::SetRange (const Standard_Real theUFirst, |
145 | const Standard_Real theULast, |
146 | const Standard_Boolean theToCalculate) |
147 | { |
148 | //myTrimFirst and myTrimLast are used to compute values on unlimited curves |
149 | myTrimFirst = myFirst = theUFirst; |
150 | if (Precision::IsInfinite(myTrimFirst)){ |
151 | myTrimFirst = -1.0e+10; |
152 | } |
153 | myTrimLast = myLast = theULast; |
154 | if (Precision::IsInfinite(myTrimLast)){ |
155 | myTrimLast = 1.0e+10; |
156 | } |
157 | |
158 | myIsArrayValid = Standard_False; |
afed7fd7 |
159 | myParamArray.Nullify(); |
7fd59977 |
160 | myPntArray.Nullify(); |
161 | if (theToCalculate) { |
162 | CalculatePoints(); |
163 | } |
164 | } |
165 | |
166 | //======================================================================= |
167 | //function : CalculatePoints |
168 | //purpose : |
169 | //======================================================================= |
170 | |
171 | void Extrema_CurveCache::CalculatePoints() |
172 | { |
173 | if (myIsArrayValid) return; //no need to recalculate if nothing has changed |
174 | const Curve& aCurve = *((Curve*)myC); |
175 | |
176 | // compute myNbSamples point along the [myTrimFirst, myTrimLast] range |
177 | |
178 | Standard_Real aDelta = myTrimLast - myTrimFirst; |
179 | Standard_Real aPar0 = aDelta / myNbSamples / 100.; |
180 | aDelta = (aDelta - aPar0) / (myNbSamples - 1); |
181 | aPar0 = myTrimFirst + (aPar0/2.); |
182 | |
183 | //Cache points |
184 | |
afed7fd7 |
185 | myParamArray = new TColStd_HArray1OfReal(1, myNbSamples); |
7fd59977 |
186 | myPntArray = new ArrayOfPnt (1, myNbSamples); |
187 | |
188 | Standard_Integer i; |
189 | Standard_Real aPar; |
190 | for (i = 1, aPar = aPar0; i <= myNbSamples; i++, aPar += aDelta) { |
afed7fd7 |
191 | myParamArray->SetValue(i, aPar); |
7fd59977 |
192 | myPntArray->SetValue (i, aCurve.Value (aPar)); |
193 | } |
194 | |
195 | myIsArrayValid = Standard_True; //cache is available now |
196 | } |