Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BSplineCurve.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009, 2005 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS 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 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS 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 BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #include "BSplineCurve.hh"
27 #include <Base/Common/BIASpragma.hh>
28 
29 using namespace BIAS;
30 using namespace std;
31 
33 #ifdef BIAS_DEBUG
34  initializedQuadratic_=false;
35  initializedCubic_=false;
36 #endif
37 }
38 
40 #ifdef BIAS_DEBUG
41  if(cPnts_.size()<3){
42  if(cPnts_.size()==0){
43  BIASERR("BSpline: no control points given - call ");
44  BIASERR("SetControlPoints(cPnts)!\r\n");
45  exit(-1);
46  }
47  else{
48  BIASERR("BSpline: less then 3 control points given - call ");
49  BIASERR("SetControlPoints(cPnts) with 3 or more control points!\r\n");
50  exit(-1);
51  }
52  }
53 #endif
54  Matrix<double> coeffM(3,3);
55  Vector<double> xItem(3);
56  Vector<double> yItem(3);
57  Vector<double> xTmp(3);
58  Vector<double> yTmp(3);
59 
60 
61  coeffM[0][0]=1;
62  coeffM[0][1]=-2;
63  coeffM[0][2]=1;
64  coeffM[1][0]=-2;
65  coeffM[1][1]=2;
66  coeffM[1][2]=0;
67  coeffM[2][0]=1;
68  coeffM[2][1]=1;
69  coeffM[2][2]=0;
70 
71  xCoeff_.clear();
72  yCoeff_.clear();
73 
74  //compute coeff matrix for all segments
75  for(unsigned int i=0; i<cPnts_.size()-2; i++){
76  for(unsigned int j=0; j<3;j++){
77  xTmp[j]=cPnts_[i+j][0];
78  yTmp[j]=cPnts_[i+j][1];
79  }
80 
81  coeffM.Mult(xTmp, xItem);
82  coeffM.Mult(yTmp, yItem);
83  xCoeff_.push_back(xItem);
84  yCoeff_.push_back(yItem);
85  }
86 #ifdef BIAS_DEBUG
87  initializedQuadratic_=true;
88  initializedCubic_=false;
89 #endif
90 
91 }
92 
94 #ifdef BIAS_DEBUG
95  if(cPnts_.size()<4){
96  if(cPnts_.size()==0){
97  BIASERR("BSpline: no control points given - call ");
98  BIASERR("SetControlPoints(cPnts)!\r\n");
99  exit(-1);
100  }
101  else{
102  BIASERR("BSpline: less then 4 control points given - call ")
103  BIASERR("SetControlPoints(cPnts) with 4 or more control points!\r\n");
104  exit(-1);
105  }
106  }
107 #endif
108  Matrix<double> coeffM(4,4);
109  Vector<double> xItem(4);
110  Vector<double> yItem(4);
111  Vector<double> xTmp(4);
112  Vector<double> yTmp(4);
113 
114  coeffM[0][0]=-1;
115  coeffM[0][1]=3;
116  coeffM[0][2]=-3;
117  coeffM[0][3]=1;
118  coeffM[1][0]=3;
119  coeffM[1][1]=-6;
120  coeffM[1][2]=3;
121  coeffM[1][3]=0;
122  coeffM[2][0]=-3;
123  coeffM[2][1]=0;
124  coeffM[2][2]=3;
125  coeffM[2][3]=0;
126  coeffM[3][0]=1;
127  coeffM[3][1]=4;
128  coeffM[3][2]=1;
129  coeffM[3][3]=0;
130 
131  xCoeff_.clear();
132  yCoeff_.clear();
133 
134  //compute coeffmatrix for all segments
135  for(unsigned int i=0; i<cPnts_.size()-3;i++){
136  for(unsigned int j=0; j<4; j++){
137  xTmp[j]=cPnts_[i+j][0];
138  yTmp[j]=cPnts_[i+j][1];
139  }
140  coeffM.Mult(xTmp,xItem);
141  coeffM.Mult(yTmp,yItem);
142  xCoeff_.push_back(xItem);
143  yCoeff_.push_back(yItem);
144  }
145 #ifdef BIAS_DEBUG
146  initializedCubic_=true;
147  initializedQuadratic_=false;
148 #endif
149 }
150 
151 
153 #ifdef BIAS_DEBUG
154  if(!initializedQuadratic_){
155  BIASERR("BSpline: call InitQuadraticUniformBSpline() at first!\r\n");
156  exit(-1);
157  }
158 #endif
159  unsigned int actSegment;
160 
161  //compute spline segment and new t for the result segment
162  if(t<=0.){
163  actSegment=0;
164  t=0.;
165  } else if(t>=1.){
166  actSegment=cPnts_.size()-3;
167  t=1.;
168  } else {
169  actSegment= (unsigned int)(t * (double)(cPnts_.size()-2));
170  t= t* (double)(cPnts_.size()-2) - (double) actSegment;
171  }
172 
173  Vector<double> tVec(3);
174  tVec[0]=t*t;
175  tVec[1]=t;
176  tVec[2]=1;
177 
178  res[0]=0.5 * tVec.ScalarProduct(xCoeff_[actSegment]);
179  res[1]=0.5 * tVec.ScalarProduct(yCoeff_[actSegment]);
180 }
181 
183 #ifdef BIAS_DEBUG
184  if(!initializedCubic_){
185  BIASERR("BSpline: call InitCubicUniformBSpline() at first!\r\n");
186  exit(-1);
187  }
188 #endif
189  unsigned int actSegment;
190 
191  //compute spline segment and new t for the result segment
192  if(t<=0.){
193  actSegment=0;
194  t=0.;
195  } else if(t>=1.){
196  actSegment=cPnts_.size()-4;
197  t=1.;
198  } else {
199  actSegment= (unsigned int) (t * (double)(cPnts_.size()-3));
200  t= t * (double)(cPnts_.size()-3) - (double) actSegment;
201  }
202 
203  Vector<double> tVec(4);
204  tVec[0]=t*t*t;
205  tVec[1]=t*t;
206  tVec[2]=t;
207  tVec[3]=1;
208 
209  res[0]=(1./6.) * tVec.ScalarProduct(xCoeff_[actSegment]);
210  res[1]=(1./6.) * tVec.ScalarProduct(yCoeff_[actSegment]);
211 }
T ScalarProduct(const Vector< T > &argvec) const
scalar product (inner product) of two vectors returning a scalr
Definition: Vector.hh:355
void InitQuadraticUniformBSpline()
computes coefficients for all segments of a quadratic uniform B-spline
void CubicUniformBSpline(Vector2< double > &res, double t)
computes the point on a cubic uniform B-spline curve at time t
virtual void clear()
stl conform interface destroys Matrix JW
Definition: Matrix.hh:534
void InitCubicUniformBSpline()
computes coefficients for all segments of a cubic uniform B-spline
void Mult(const Matrix< T > &arg, Matrix< T > &result) const
matrix multiplication, result is not allocated
Definition: Matrix.hh:913
void QuadraticUniformBSpline(Vector2< double > &res, double t)
computes the point on a quadratic uniform B-spline curve at time t