Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Nurbs2D.cpp
1 /*
2  * Nurbs2D.cpp
3  *
4  * Created on: Jul 13, 2010
5  * Author: jordt
6  */
7 
8 #include "Nurbs2D.hh"
9 #include <Base/Math/Vector3.hh>
10 #include <Base/Math/Vector4.hh>
11 #include <Base/Geometry/HomgPoint2D.hh>
12 #include <Base/Geometry/HomgPoint3D.hh>
13 #include <Base/Math/Operators.hh>
14 
15 #include "NurbsTemplateInit.hh"
16 
17 using namespace std;
18 using namespace BIAS;
19 
20 
21 
22 // This function allocates and initializes the control point- and weight-arrays
23 template <class ControlPointsT>
24 Nurbs2D<ControlPointsT>::Nurbs2D(int knotsPerDim, int degree ) : NurbsBase(knotsPerDim,degree,2){
25 
26  // Allocate and initialize
27  controlPoints_ = new ControlPointsT*[numCtrlPoints_];
28  weights_ = new double*[numCtrlPoints_];
29  for (int x = 0; x < numCtrlPoints_; x++){
30  controlPoints_[x] = new ControlPointsT[numCtrlPoints_];
31  weights_[x] = new double[numCtrlPoints_];
32  for (int y = 0; y < numCtrlPoints_; y++){
34  weights_[x][y] = 1.0;
35  }
36  }
37 
38 }
39 
40 // The serialized not-inline implementation of the nurbs evaluation function
41 template <class ControlPointsT>
42 void Nurbs2D<ControlPointsT>::Eval(const Vector2<double> &nurbsParam, ControlPointsT &result) const{
43 
44  BIASASSERT(nurbsParam[0] >= 0.0 && nurbsParam[0] <= 1.0 &&
45  nurbsParam[1] >= 0.0 && nurbsParam[1] <= 1.0)
46 
47  // Initialize the return value
48  TemplateInit::Init(result);
49 
50  // Get the relevant control points
51  double weightSum = 0.0;
52  Vector2<int> relevantBegin;
53  Vector2<int> relevantEnd;
54  GetRelevantBaseCtrlPoints(nurbsParam, relevantBegin, relevantEnd);
55  double* baseFuncCache = new double[degree_+1];
56 
57  // Evaluate the relevant base functions and use them to wheight the
58  // control points
59  for (int x = relevantBegin[0]; x <= relevantEnd[0]; x++)
60  for (int y = relevantBegin[1]; y <= relevantEnd[1]; y++){
61  double weight = BaseFunc(0,x,degree_,nurbsParam[0],baseFuncCache)*
62  BaseFunc(1,y,degree_,nurbsParam[1],baseFuncCache)*
63  weights_[x][y];
64  weightSum += weight;
65 
66  result += (weight * controlPoints_[x][y]);
67  }
68  if (weightSum != 0.0)
69  result = result * weightSum;
70 
71  delete[] baseFuncCache;
72 }
73 
74 
75 
76 // The serialized not-inline implementation of the nurbs evaluation function
77 template <class ControlPointsT>
78 void Nurbs2D<ControlPointsT>::EvalCustom(const Vector2<double> &nurbsParam, ControlPointsT &result,
79  const ControlPointsT** controlPoints) const{
80 
81  // Initialize the return value
82  TemplateInit::Init(result);
83 
84  // Get the relevant control points
85  double weightSum = 0.0;
86  Vector2<int> relevantBegin;
87  Vector2<int> relevantEnd;
88  GetRelevantBaseCtrlPoints(nurbsParam, relevantBegin, relevantEnd);
89  double* baseFuncCache = new double[degree_+1];
90 
91  // Evaluate the relevant base functions and use them to wheight the
92  // control points
93  for (int x = relevantBegin[0]; x <= relevantEnd[0]; x++)
94  for (int y = relevantBegin[1]; y <= relevantEnd[1]; y++){
95  double weight = BaseFunc(0,x,degree_,nurbsParam[0],baseFuncCache)*
96  BaseFunc(1,y,degree_,nurbsParam[1],baseFuncCache)*
97  weights_[x][y];
98  weightSum += weight;
99 
100  result += (weight * controlPoints[x][y]);
101  }
102 
103  // Warning: This has changed!
104  if (weightSum != 0.0)
105  result = result * (1.0 / weightSum);
106 
107  delete[] baseFuncCache;
108 }
109 
110 template <class ControlPointsT>
111 void Nurbs2D<ControlPointsT>::Init(const ControlPointsT minX,const ControlPointsT maxX,
112  const ControlPointsT minY,const ControlPointsT maxY){
113  for (int x = 0; x < numCtrlPoints_; x++)
114  for (int y = 0; y < numCtrlPoints_; y++){
115  double ratioX = (double)x/(double)(numCtrlPoints_-1);
116  double ratioY = (double)y/(double)(numCtrlPoints_-1);
117  controlPoints_[x][y] = ratioX * maxX + (1.0 - ratioX) * minX
118  + ratioY * maxY + (1.0 - ratioY) * minY;
119  }
120 
121 }
122 
123 
124 namespace BIAS {
125 
126  // Implement NURBS2D for all common types of vectors:
127  template class Nurbs2D<double >;
128  template class Nurbs2D<Vector<double> >;
129  template class Nurbs2D<Vector2<double> >;
130  template class Nurbs2D<Vector3<double> >;
131  template class Nurbs2D<Vector4<double> >;
132  template class Nurbs2D<HomgPoint2D >;
133  template class Nurbs2D<HomgPoint3D >;
134 
135 }
double ** weights_
The corresponding weights to the function points.
Definition: Nurbs2D.hh:54
void EvalCustom(const BIAS::Vector2< double > &nurbsParam, ControlPointsT &result, const ControlPointsT **controlPoints) const
Evaluate the NURBS function at a given point in the parameter space for a certain control point vecto...
Definition: Nurbs2D.cpp:78
void Eval(const BIAS::Vector2< double > &nurbsParam, ControlPointsT &result) const
Evaluate the NURBS function at a given point in the parameter space.
Definition: Nurbs2D.cpp:42
ControlPointsT ** controlPoints_
The array of control points.
Definition: Nurbs2D.hh:51
static void Init(double &value)
This class implements a NURBS-function with a two dimensional parameter space (A NURBS surface)...
Definition: Nurbs2D.hh:23
Base class for NURBS classes.
Definition: NurbsBase.hh:29
void Init(const ControlPointsT minX, const ControlPointsT maxX, const ControlPointsT minY, const ControlPointsT maxY)
Very generic initialization function for the control points.
Definition: Nurbs2D.cpp:111
int numCtrlPoints_
The number of control points per dimension.
Definition: NurbsBase.hh:71