OpenWalnut  1.5.0dev
WBSpline.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <vector>
26 
27 #include "WBSpline.h"
28 
29 WBSpline::WBSpline( int order, std::vector< WVector3d > deBoorPoints )
30 {
31  m_deBoorPoints = deBoorPoints;
32  int n = m_deBoorPoints.size();
33  int k = m_order = order;
34 
35  //define a normalized knotVector
36  for( int i = 0; i < ( n + k ); i++ )
37  {
38  double tempKnot;
39  if( i < k )
40  {
41  tempKnot = k - 1;
42  }
43  else if( ( i >= k ) && ( i < n ) )
44  {
45  tempKnot = i;
46  }
47  else //if( i >= n)
48  {
49  tempKnot = n;
50  }
51 
52  m_knots.push_back( tempKnot );
53  }
54 }
55 
56 WBSpline::WBSpline( int order, std::vector< WVector3d > deBoorPoints, std::vector<double> knots )
57 {
58  m_order = order;
59  m_deBoorPoints = deBoorPoints;
60  m_knots = knots;
61 }
62 
64 {
65 }
66 
67 WVector3d WBSpline::f( double _t )
68 {
69  WVector3d result;
70  unsigned int r = 0, i;
71 
72  if( _t < m_knots[0] )
73  {
74  _t = m_knots[0];
75  }
76 
77  if( _t > m_knots[m_knots.size() - 1] )
78  {
79  _t = m_knots[m_knots.size() - 1];
80  }
81 
82  m_t = _t; // set current paramter _t as class variable
83 
84  for( i = 0; i < m_knots.size(); i++ ) // -1 ?
85  {
86  if( m_knots[i] > _t )
87  {
88  break;
89  }
90  }
91 
92  r = i - 1;
93 
94  if( _t == m_knots[m_knots.size() - 1] )
95  {
96  for( i = ( m_knots.size() - 1 ); i > 0; i-- )
97  {
98  if( m_knots[i] < _t )
99  {
100  break;
101  }
102  }
103  r = i;
104  }
105 
106  result = controlPoint_i_j( r, m_order - 1 );
107 
108  return result;
109 }
110 
111 std::vector< WVector3d > WBSpline::getDeBoorPoints()
112 {
113  return m_deBoorPoints;
114 }
115 
116 std::vector<double> WBSpline::getKnots()
117 {
118  return m_knots;
119 }
120 
122 {
123  return m_order;
124 }
125 
126 void WBSpline::setDeBoorPoints( std::vector< WVector3d > deBoorPoints )
127 {
128  m_deBoorPoints = deBoorPoints;
129 }
130 
131 void WBSpline::setKnots( std::vector<double> knots )
132 {
133  m_knots = knots;
134 }
135 
136 void WBSpline::setOrder( int order )
137 {
138  m_order = order;
139 }
140 
141 void WBSpline::samplePoints( std::vector< WVector3d > *points, double resolution )
142 {
143  double deltaT = resolution;
144  double currentT = m_knots[0];
145 
146  int steps = static_cast< int >( ( m_knots[m_knots.size() - 1] - m_knots[0] ) / deltaT + 1 );
147 
148  for( int step = 0; step < steps; step++ )
149  {
150  currentT = m_knots[0] + step * deltaT;
151  WVector3d samplePoint = f( currentT );
152  points->push_back( samplePoint );
153  }
154 }
155 
156 double WBSpline::getAlpha_i_j( int _i, int _j )
157 {
158  double result = ( m_t - m_knots[_i] ) / ( m_knots[_i + m_order - _j] - m_knots[_i] );
159  return result;
160 }
161 
163 {
164  WVector3d result;
165 
166  if( _j == 0 )
167  {
168  result[0] = m_deBoorPoints[_i][0];
169  result[1] = m_deBoorPoints[_i][1];
170  result[2] = m_deBoorPoints[_i][2];
171  return result;
172  }
173  double bufferedAlpha = getAlpha_i_j( _i, _j );
174 
175  result = ( 1 - bufferedAlpha ) * controlPoint_i_j( _i - 1, _j - 1 ) +
176  bufferedAlpha * controlPoint_i_j( _i, _j - 1 );
177 
178 
179  return result;
180 }
void setOrder(int order)
Sets a new order for the spline.
Definition: WBSpline.cpp:136
double m_t
The parameter value of the last spline evaluation i.e. the last call to f().
Definition: WBSpline.h:114
std::vector< WVector3d > getDeBoorPoints()
Returns the stored de Boor points.
Definition: WBSpline.cpp:111
void setKnots(std::vector< double > knots)
Sets new knots for the spline.
Definition: WBSpline.cpp:131
void samplePoints(std::vector< WVector3d > *p, double resolution)
Compute sample points on the spline for a given resolution.
Definition: WBSpline.cpp:141
WVector3d controlPoint_i_j(int _i, int _j)
Compute a control point of the de Boor algorithm for the given parameters.
Definition: WBSpline.cpp:162
int getOrder()
Returns the order of the spline.
Definition: WBSpline.cpp:121
WVector3d f(double t)
Compute a single sample point on the spline for the given parameter.
Definition: WBSpline.cpp:67
WBSpline(int order, std::vector< WVector3d > deBoorPoints)
Constructor for the spline that takes the order and de Boor points while constructing a normalized kn...
Definition: WBSpline.cpp:29
void setDeBoorPoints(std::vector< WVector3d > deBoorPoints)
Sets new de Boor points for the spline.
Definition: WBSpline.cpp:126
int m_order
The order of the spline.
Definition: WBSpline.h:111
std::vector< double > getKnots()
Returns the stored knots.
Definition: WBSpline.cpp:116
std::vector< double > m_knots
The knots of the spline.
Definition: WBSpline.h:113
~WBSpline()
Empty destructor.
Definition: WBSpline.cpp:63
double getAlpha_i_j(int _i, int _j)
Compute the alpha of the de Boor algorithm for the given parameters.
Definition: WBSpline.cpp:156
std::vector< WVector3d > m_deBoorPoints
The de Boor points of the spline.
Definition: WBSpline.h:112