Library of Assembled Shared Sources
spline.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::num::Spline
46 * @brief abstract base class of splines.
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_SPLINE_H
51#define LASS_GUARDIAN_OF_INCLUSION_NUM_SPLINE_H
52
53#include "num_common.h"
55
56namespace lass
57{
58namespace num
59{
60
61template
62<
63 typename ScalarType,
64 typename DataType
65>
66class Spline
67{
68public:
69
70 typedef ScalarType TScalar;
71 typedef DataType TData;
72
73 class ControlRange
74 {
75 public:
76 ControlRange(TScalar begin, TScalar end): begin_(begin), end_(end) {}
77 const TScalar begin() const { return begin_; }
78 const TScalar end() const { return end_; }
79 private:
80 TScalar begin_;
81 TScalar end_;
82 };
83
84 virtual ~Spline() {}
85
86 virtual const TData operator()(TScalar iX) const = 0;
87 virtual const TData derivative(TScalar iX) const = 0;
88 virtual const TData derivative2(TScalar iX) const = 0;
89 virtual const TData integral(TScalar iA, TScalar iB) const = 0;
90
91 virtual bool isEmpty() const = 0;
92 virtual const ControlRange controlRange() const = 0;
93};
94
95
96
97
98
99//#pragma LASS_TODO("below are data traits that should move to a seperate header when appropriate, [Bramz]")
100
101/** @defgroup DataTraits
102 * A set of trait classes on how to manipulate data
103 *
104 * This is the concept each data traits class has to model:
105 *
106 * @code
107 * struct
108 * {
109 * typedef ... TData; // type of data
110 * typedef ... TScalar; // type of a single scalar value in data
111 * static size_t dimension(const TData& iY); // returns number of scalar values in iY
112 * static void zero(TData& iY, size_t iDim); // resets iY to a data object with iDim zeros
113 * static TScalar get(const TData& iY, size_t iIndex); // return iIndex'th scalar in iY
114 * static void set(TData& ioY, size_t iIndex, TScalar iV); // set iIndex'th scalar of iY to iV
115 * static void scale(TDAta& ioAcc, TScalar iS); // scale each scalar of ioAcc by iS
116 * static void multiplyAccumulate(TData& ioAcc, const TData& iY, TScalar iS); // add each
117 * // element of iY multiplied by iS to the element in ioAcc with same index.
118 * };
119 * @endcode
120 *
121 * - @ref DataTraitsSequence: concerning data acting like an STL sequence
122 * - @ref DataTraitsScalar: concerning scalar data
123 * - @ref DataTraitsDynamicVector: concerning data like num::Vector
124 * - @ref DataTraitsStaticVector: concerning data like prim::Vector2D
125 */
126
127/** @ingroup DataTraits
128 */
129template <typename SequenceType>
130struct DataTraitsSequence
131{
132 typedef SequenceType TData;
133 typedef typename SequenceType::value_type TScalar;
134 static size_t dimension(const TData& iY) { return std::distance(iY.begin(), iY.end()); }
135 static void zero(TData& ioY, size_t iDim) { ioY = TData(iDim); }
136 static TScalar get(const TData& iY, size_t iIndex) { return *stde::next(iY.begin(), iIndex); }
137 static void set(TData& ioY, size_t iIndex, TScalar iV) { *stde::next(ioY.begin(), iIndex) = iV; }
138 static void scale(TData& ioAcc, TScalar iS)
139 {
140 std::transform(ioAcc.begin(), ioAcc.end(), ioAcc.begin(), [iS](TScalar a) { return a * iS; });
141 }
142 static void multiplyAccumulate(TData& ioAcc, const TData& iY, TScalar iS)
143 {
144 std::transform(ioAcc.begin(), ioAcc.end(), iY.begin(), ioAcc.begin(), [iS](TScalar a, TScalar b) { return a + iS * b; });
145 }
146};
147
148/** @ingroup DataTraits
149 */
150template <typename ScalarType>
151struct DataTraitsScalar
152{
153 typedef ScalarType TData;
154 typedef ScalarType TScalar;
155 static size_t dimension(TScalar /*iY*/) { return 1; }
156 static void zero(TScalar& ioY, size_t /*iDim*/) { ioY = TScalar(); }
157 static TScalar get(TScalar iY, size_t /*iIndex*/) { return iY; }
158 static void set(TScalar& ioY, size_t /*iIndex*/, TScalar iV) { ioY = iV; }
159 static void scale(TScalar& ioAcc, TScalar iS) { ioAcc *= iS; }
160 static void multiplyAccumulate(TScalar& ioAcc, TScalar iY, TScalar iS) { ioAcc += iY * iS; }
161};
162
163
164/** @ingroup DataTraits
165 */
166template <typename DataType>
167struct DataTraitsDynamicVector
168{
169 typedef DataType TData;
170 typedef typename DataType::TValue TScalar;
171 static size_t dimension(const TData& iY) { return iY.size(); }
172 static void zero(TData& ioY, size_t iDim) { ioY = TData(iDim); }
173 static TScalar get(const TData& iY, size_t iIndex) { return iY[iIndex]; }
174 static void set(TData& ioY, size_t iIndex, TScalar iV) { ioY[iIndex] = iV; }
175 static void scale(TData& ioAcc, TScalar iS) { ioAcc *= iS; }
176 static void multiplyAccumulate(TData& ioAcc, const TData& iY, TScalar iS) { ioAcc += iY * iS; }
177};
178
179/** @ingroup DataTraits
180 */
181template <typename DataType>
182struct DataTraitsStaticVector
183{
184 typedef DataType TData;
185 typedef typename DataType::TValue TScalar;
186 static size_t dimension(const TData& /*iY*/) { return TData::dimension; }
187 static void zero(TData& ioY, size_t /*iDim*/) { ioY = TData(); }
188 static TScalar get(const TData& iY, size_t iIndex) { return iY[iIndex]; }
189 static void set(TData& ioY, size_t iIndex, TScalar iV) { ioY[iIndex] = iV; }
190 static void scale(TData& ioAcc, TScalar iS) { ioAcc *= iS; }
191 static void multiplyAccumulate(TData& ioAcc, const TData& iY, TScalar iS) { ioAcc += iY * iS; }
192};
193
194
195}
196
197}
198
199#endif
200
201// EOF
abstract base class of splines.
Definition spline.h:67
numeric types and traits.
Definition basic_ops.h:70
const SplineLinear< S, D, T >::TData SplineLinear< S, D, T >::derivative2(TScalar) const
Get the second derivative of data value that corresponds with constrol value iX.
Library for Assembled Shared Sources.
Definition config.h:53