ICF 3.1.1.10
Technical documentation of ICF Libraries
TSplineGridFunctionBase.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#ifndef imath_TSplineGridFunctionBase_included
7#define imath_TSplineGridFunctionBase_included
8
9
10// ICF includes
11#include <istd/TArray.h>
12#include <imath/TVector.h>
15
16
17namespace imath
18{
19
20
30template <class Argument, class Result, class Fulcrums, class Degree>
31class TSplineGridFunctionBase: public TFulcrumGridFunctionBase<Argument, Result, Fulcrums>
32{
33public:
35
36 typedef Degree DerivativeDegreeType;
37
38 // reimplemented (imath::TIMathFunction<Argument, Result>)
39 virtual bool GetValueAt(const Argument& argument, Result& result) const;
40 virtual Result GetValueAt(const Argument& argument) const;
41
42protected:
61 const Argument& argument,
62 int dimension,
63 const typename BaseClass::FulcrumSizes& sizes,
64 typename BaseClass::FulcrumIndex& index,
66 double cumulationFactor,
67 Result& result) const;
68
69 // abstract methods
74 const typename BaseClass::FulcrumIndex& index,
75 const DerivativeDegreeType& degree) const = 0;
76
80 virtual bool IsDerivativeDegreeSupported(const DerivativeDegreeType& degree) const = 0;
81};
82
83
84// public methods
85
86// reimplemented (imath::TIMathFunction<Argument, Result>)
87
88template <class Argument, class Result, class Fulcrums, class Degree>
89bool TSplineGridFunctionBase<Argument, Result, Fulcrums, Degree>::GetValueAt(const Argument& argument, Result& result) const
90{
91 result.Clear();
92
93 if (BaseClass::EnsureCacheValid()){
94 typename BaseClass::FulcrumIndex index = this->FindIndices(argument);
95
96 typename BaseClass::FulcrumSizes gridSize = BaseClass::GetGridSize();
97
98 if (index.IsInside(gridSize)){
99 int dimensionsCount = BaseClass::GetDimensionsCount();
100 Degree degree;
101 degree.SetDimensionsCount(dimensionsCount);
102
103 CumulateRecursiveValueAt(argument, dimensionsCount - 1, gridSize, index, degree, 1.0, result);
104
105 return true;
106 }
107 }
108
109 return false;
110}
111
112
113template <class Argument, class Result, class Fulcrums, class Degree>
115{
116 typename BaseClass::ResultType retVal;
117
118 GetValueAt(argument, retVal);
119
120 return retVal;
121}
122
123
124// protected methods
125
126template <class Argument, class Result, class Fulcrums, class Degree>
128 const Argument& argument,
129 int dimension,
130 const typename BaseClass::FulcrumSizes& sizes,
131 typename BaseClass::FulcrumIndex& index,
132 DerivativeDegreeType& derivativeDegree,
133 double cumulationFactor,
134 Result& result) const
135{
136 Q_ASSERT(dimension < BaseClass::GetDimensionsCount());
137 Q_ASSERT(sizes.GetDimensionsCount() == BaseClass::GetDimensionsCount());
138 Q_ASSERT(index.GetDimensionsCount() == BaseClass::GetDimensionsCount());
139
140 if (dimension < 0){
141 result.ScaledCumulate(GetFulcrumDerivativeAtIndex(index, derivativeDegree), cumulationFactor);
142
143 return;
144 }
145
146 int& indexElement = index[dimension];
147
148 if (indexElement >= 0){
149 if (indexElement < sizes[dimension] - 1){
150 double firstPosition = BaseClass::GetLayerPosition(dimension, indexElement);
151 double secondPosition = BaseClass::GetLayerPosition(dimension, indexElement + 1);
152 double layersDistance = secondPosition - firstPosition;
153 Q_ASSERT(layersDistance >= 0);
154 Q_ASSERT(argument[dimension] >= firstPosition);
155 Q_ASSERT(argument[dimension] <= secondPosition);
156 Q_ASSERT(derivativeDegree[dimension] == 0);
157
158 bool useDerivative = false;
159 if (derivativeDegree.IncreaseAt(dimension)){
160 useDerivative = IsDerivativeDegreeSupported(derivativeDegree);
161 derivativeDegree.DecreaseAt(dimension);
162 }
163
164 double alpha = (argument[dimension] - firstPosition) / layersDistance;
165
166 double firstValueFactor = useDerivative? CSplineSegmentFunction::GetValueKernelAt(alpha): (1 - alpha); // use linear interpolation if no derivative is available
167 if (firstValueFactor > I_BIG_EPSILON){
168 CumulateRecursiveValueAt(
169 argument,
170 dimension - 1,
171 sizes,
172 index,
173 derivativeDegree,
174 cumulationFactor * firstValueFactor,
175 result);
176 }
177
178 double secondValueFactor = useDerivative? CSplineSegmentFunction::GetValueKernelAt(1.0 - alpha): alpha; // use linear interpolation if no derivative is available
179
180 if (secondValueFactor > I_BIG_EPSILON){
181 ++indexElement;
182 CumulateRecursiveValueAt(
183 argument,
184 dimension - 1,
185 sizes,
186 index,
187 derivativeDegree,
188 cumulationFactor * secondValueFactor,
189 result);
190 --indexElement;
191 }
192
193 if (useDerivative){
194 derivativeDegree.IncreaseAt(dimension);
195
196 double firstDerivativeFactor = CSplineSegmentFunction::GetDerivativeKernelAt(alpha) * layersDistance;
197 if (firstDerivativeFactor > I_BIG_EPSILON){
198 CumulateRecursiveValueAt(
199 argument,
200 dimension - 1,
201 sizes,
202 index,
203 derivativeDegree,
204 cumulationFactor * firstDerivativeFactor,
205 result);
206 }
207
208 ++indexElement;
209
210 double secondDerivativeFactor = -CSplineSegmentFunction::GetDerivativeKernelAt(1.0 - alpha) * layersDistance;
211 if (secondDerivativeFactor < -I_BIG_EPSILON){
212 CumulateRecursiveValueAt(
213 argument,
214 dimension - 1,
215 sizes,
216 index,
217 derivativeDegree,
218 cumulationFactor * secondDerivativeFactor,
219 result);
220 }
221
222 --indexElement;
223
224 derivativeDegree.DecreaseAt(dimension);
225 }
226 }
227 else{
228 // element out of boundaries at this dimension
229 Q_ASSERT(indexElement == sizes[dimension] - 1);
230
231 CumulateRecursiveValueAt(
232 argument,
233 dimension - 1,
234 sizes,
235 index,
236 derivativeDegree,
237 cumulationFactor,
238 result);
239 }
240 }
241 else{
242 // element out of boundaries at this dimension
243 Q_ASSERT(indexElement == -1);
244
245 ++indexElement;
246 CumulateRecursiveValueAt(
247 argument,
248 dimension - 1,
249 sizes,
250 index,
251 derivativeDegree,
252 cumulationFactor,
253 result);
254 --indexElement;
255 }
256}
257
258
259} // namespace imath
260
261
262#endif // !imath_TSplineGridFunctionBase_included
263
264
static double GetValueKernelAt(double alpha)
Get kernel of value normalized to range [0, 1].
static double GetDerivativeKernelAt(double alpha)
Get kernel of derivative normalized to range [0, 1].
Base class for interpolated functions based on fulcrums in multi-dimesional grid.
Spline interpolation function using polynomial 3 degree segments.
virtual bool IsDerivativeDegreeSupported(const DerivativeDegreeType &degree) const =0
Check, if this or higher degree derrivatives are supported.
virtual bool GetValueAt(const Argument &argument, Result &result) const
Get function value for specified argument value.
void CumulateRecursiveValueAt(const Argument &argument, int dimension, const typename BaseClass::FulcrumSizes &sizes, typename BaseClass::FulcrumIndex &index, DerivativeDegreeType &degree, double cumulationFactor, Result &result) const
Cumulate interpolated value or derivative at specified recursion level.
virtual const BaseClass::ResultType & GetFulcrumDerivativeAtIndex(const typename BaseClass::FulcrumIndex &index, const DerivativeDegreeType &degree) const =0
Get derivative of specified degree at specified index position.
TFulcrumGridFunctionBase< Argument, Result, Fulcrums > BaseClass
Package with mathematical functions and algebraical primitives.

© Witold Gantzke and Kirill Lepskiy