1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2008 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
41 template<class KeyFieldT, class KeyValueFieldT, class ValueFieldT> inline
42 void InterpolationHelper<KeyFieldT,
44 ValueFieldT>::copyFirstValue(
46 const KeyValueFieldT &mfKeyValues,
49 fValue.setValue(mfKeyValues.front());
52 template<class KeyFieldT, class KeyValueFieldT, class ValueFieldT> inline
53 void InterpolationHelper<KeyFieldT,
55 ValueFieldT>::copyLastValue (
57 const KeyValueFieldT &mfKeyValues,
60 fValue.setValue(mfKeyValues.back());
66 void InterpolationHelper<MFReal32,
69 const UInt32 uiStopIndex,
70 const UInt32 uiStartIndex,
71 const Real32 rFraction,
72 const MFReal32 &mfKeys,
73 const MFQuaternion &mfKeyValues,
74 SFQuaternion &fValue )
76 if(osgAbs(mfKeys[uiStopIndex] - mfKeys[uiStartIndex]) <
77 TypeTraits<Real32>::getDefaultEps() )
86 (rFraction - mfKeys[uiStartIndex]) /
87 (mfKeys[uiStopIndex] - mfKeys[uiStartIndex]);
89 result.slerpThis(mfKeyValues[uiStartIndex],
90 mfKeyValues[uiStopIndex],
93 fValue.setValue(result);
100 void InterpolationHelper<MFReal32,
103 const UInt32 uiStopIndex,
104 const UInt32 uiStartIndex,
105 const Real32 rFraction,
106 const MFReal32 &mfKeys,
107 const MFVec3f &mfKeyValues,
110 if(osgAbs(mfKeys[uiStopIndex] - mfKeys[uiStartIndex]) <
111 TypeTraits<Real32>::getDefaultEps() )
120 (rFraction - mfKeys[uiStartIndex]) /
121 (mfKeys[uiStopIndex] - mfKeys[uiStartIndex]);
123 vResult = mfKeyValues[uiStopIndex ];
124 vResult -= mfKeyValues[uiStartIndex];
127 vResult += mfKeyValues[uiStartIndex];
129 fValue.setValue(vResult);
137 void InterpolationHelper<MFReal32,
139 MFPnt3f>::copyFirstValue(
140 const MFReal32 &mfKeys,
141 const MFPnt3f &mfKeyValues,
144 UInt32 uiNumPoints = UInt32(mfKeyValues.size() / mfKeys.size());
146 MFPnt3f::const_iterator startIt = mfKeyValues.begin();
147 MFPnt3f::const_iterator stopIt = startIt + uiNumPoints;
150 fValue.insert(fValue.begin(), startIt, stopIt);
154 void InterpolationHelper<MFReal32,
156 MFPnt3f>::copyLastValue (
157 const MFReal32 &mfKeys,
158 const MFPnt3f &mfKeyValues,
161 UInt32 uiNumPoints = UInt32(mfKeyValues.size() / mfKeys.size());
163 MFPnt3f::const_iterator stopIt = mfKeyValues.end();
164 MFPnt3f::const_iterator startIt = stopIt - uiNumPoints;
167 fValue.insert(fValue.begin(), startIt, stopIt);
171 void InterpolationHelper<MFReal32,
174 const UInt32 uiStopIndex,
175 const UInt32 uiStartIndex,
176 const Real32 rFraction,
177 const MFReal32 &mfKeys,
178 const MFPnt3f &mfKeyValues,
181 if(osgAbs(mfKeys[uiStopIndex] - mfKeys[uiStartIndex]) <
182 TypeTraits<Real32>::getDefaultEps() )
190 UInt32 uiNumPoints = UInt32(mfKeyValues.size() / mfKeys.size());
193 (rFraction - mfKeys[uiStartIndex]) /
194 (mfKeys[uiStopIndex] - mfKeys[uiStartIndex]);
196 UInt32 uiIndex1 = uiStartIndex * uiNumPoints;
197 UInt32 uiIndex2 = uiStopIndex * uiNumPoints;
201 for(UInt32 i = 0; i < uiNumPoints; i++)
203 vResult = mfKeyValues[uiIndex2];
204 vResult -= mfKeyValues[uiIndex1].subZero();
207 vResult += mfKeyValues[uiIndex1].subZero();
209 fValue.push_back(vResult);
221 void InterpolationHelper<MFReal32,
223 MFVec3f>::copyFirstValue(
224 const MFReal32 &mfKeys,
225 const MFVec3f &mfKeyValues,
228 UInt32 uiNumPoints = UInt32(mfKeyValues.size() / mfKeys.size());
230 MFVec3f::const_iterator startIt = mfKeyValues.begin();
231 MFVec3f::const_iterator stopIt = startIt + uiNumPoints;
234 fValue.insert(fValue.begin(), startIt, stopIt);
238 void InterpolationHelper<MFReal32,
240 MFVec3f>::copyLastValue (
241 const MFReal32 &mfKeys,
242 const MFVec3f &mfKeyValues,
245 UInt32 uiNumPoints = UInt32(mfKeyValues.size() / mfKeys.size());
247 MFVec3f::const_iterator stopIt = mfKeyValues.end();
248 MFVec3f::const_iterator startIt = stopIt - uiNumPoints;
251 fValue.insert(fValue.begin(), startIt, stopIt);
255 void InterpolationHelper<MFReal32,
258 const UInt32 uiStopIndex,
259 const UInt32 uiStartIndex,
260 const Real32 rFraction,
261 const MFReal32 &mfKeys,
262 const MFVec3f &mfKeyValues,
265 if(osgAbs(mfKeys[uiStopIndex] - mfKeys[uiStartIndex]) <
266 TypeTraits<Real32>::getDefaultEps() )
274 UInt32 uiNumPoints = UInt32(mfKeyValues.size() / mfKeys.size());
277 (rFraction - mfKeys[uiStartIndex]) /
278 (mfKeys[uiStopIndex] - mfKeys[uiStartIndex]);
280 UInt32 uiIndex1 = uiStartIndex * uiNumPoints;
281 UInt32 uiIndex2 = uiStopIndex * uiNumPoints;
285 for(UInt32 i = 0; i < uiNumPoints; i++)
287 vResult = mfKeyValues[uiIndex2];
288 vResult -= mfKeyValues[uiIndex1].subZero();
291 vResult += mfKeyValues[uiIndex1].subZero();
293 fValue.push_back(vResult);
305 void InterpolationHelper<MFReal32,
308 const UInt32 uiStopIndex,
309 const UInt32 uiStartIndex,
310 const Real32 rFraction,
311 const MFReal32 &mfKeys,
312 const MFReal32 &mfKeyValues,
315 if(osgAbs(mfKeys[uiStopIndex] - mfKeys[uiStartIndex]) <
316 TypeTraits<Real32>::getDefaultEps() )
325 (rFraction - mfKeys[uiStartIndex]) /
326 (mfKeys[uiStopIndex] - mfKeys[uiStartIndex]);
328 vResult = mfKeyValues[uiStopIndex ];
329 vResult -= mfKeyValues[uiStartIndex];
332 vResult += mfKeyValues[uiStartIndex];
334 fValue.setValue(vResult);
341 template<class KeyFieldT, class KeyValueFieldT, class ValueFieldT> inline
342 void InterpolationHelper<KeyFieldT,
344 ValueFieldT>::interpolate(
345 const Real32 rFraction,
346 const KeyFieldT &mfKeys,
347 const KeyValueFieldT &mfKeyValues,
348 ValueFieldT &fValue )
350 typename KeyFieldT::const_iterator keyIt;
352 if(mfKeys.size() == 0 || mfKeyValues.size() == 0)
357 keyIt = lower_bound(mfKeys.begin(),
361 if(keyIt != mfKeys.end())
363 if(keyIt == mfKeys.begin())
365 copyFirstValue(mfKeys, mfKeyValues, fValue);
369 UInt32 uiStopIndex = keyIt - mfKeys.begin();
370 UInt32 uiStartIndex = uiStopIndex - 1;
382 copyLastValue(mfKeys, mfKeyValues, fValue);
386 template<class KeyFieldT, class KeyValueFieldT, class ValueFieldT>
387 template<class ResortIndexTypeT> inline
388 void InterpolationHelper<KeyFieldT,
390 ValueFieldT>::resortKeyValues(
392 SizeT uiValuesPerKey,
393 KeyValueFieldT &mfKeyValues,
394 const ResortIndexTypeT &mfResortIndex)
396 KeyValueFieldT tmpKeyValues;
398 tmpKeyValues.resize(mfKeyValues.size());
400 SizeT uiGlobalIdx = 0;
402 for(SizeT i = 0; i < uiNumKeys; ++i)
404 SizeT uiGlobalBase = i * uiValuesPerKey;
406 for(SizeT j = 0; j < uiValuesPerKey; ++j, ++uiGlobalIdx)
408 tmpKeyValues[uiGlobalIdx] =
409 mfKeyValues[uiGlobalBase + mfResortIndex[j]];
413 mfKeyValues = tmpKeyValues;