Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Nurbs3D.cpp
1 /*
2  * Nurbs3D.cpp
3  *
4  * Created on: Jul 1, 2010
5  * Author: jordt
6  */
7 
8 #include "Nurbs3D.hh"
9 #include <Base/Math/Vector2.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 // This function allocates and initializes the control point- and weight-arrays and
22 // calculates the sphere-points (control points in the vicinity of the unit sphere)
23 template <class ControlPointsT>
24 Nurbs3D<ControlPointsT>::Nurbs3D(int knotsPerDim, int degree ) : NurbsBase(knotsPerDim,degree,3){
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++){
33  controlPoints_[x][y] = new ControlPointsT[numCtrlPoints_];
34  weights_[x][y] = new double[numCtrlPoints_];
35  for (int z = 0; z < numCtrlPoints_; z++){
37  weights_[x][y][z] = 1.0;
38 
39  // Calculate the sphere points
40  double xSphere = ((double)x/((double)numCtrlPoints_-1.0) - 0.5) * 2.0;
41  double ySphere = ((double)y/((double)numCtrlPoints_-1.0) - 0.5) * 2.0;
42  double zSphere = ((double)z/((double)numCtrlPoints_-1.0) - 0.5) * 2.0;
43  Vector3<double> prm = Vector3<double>(xSphere,ySphere,zSphere);
44  if (prm.Length() > 0.0001) prm.Normalize();
45  prm[0] = (prm[0] + 1.0) / 2.0;
46  prm[1] = (prm[1] + 1.0) / 2.0;
47  prm[2] = (prm[2] + 1.0) / 2.0;
48  double testResult;
49  EvalBaseFunc(x,y,z,prm,testResult);
50  if (testResult > 0.0) sphereCtrlPoints_.push_back(Vector3<int>(x,y,z));
51 
52  //if (fabs(1.0 - sqrt(xSphere*xSphere+ySphere*ySphere+zSphere*zSphere)) <= (double)degree/(double)(numCtrlPoints_-1) )
53 
54  }
55  }
56  }
57 
58 }
59 
60 template <class ControlPointsT>
62  for (int x = 0; x < numCtrlPoints_; x++){
63  for (int y = 0; y < numCtrlPoints_; y++){
64  delete[] controlPoints_[x][y];
65  delete[] weights_[x][y];
66  }
67  delete[] controlPoints_[x];
68  delete[] weights_[x];
69  }
70  delete[] controlPoints_;
71  delete[] weights_;
72 
73 
74 }
75 
76 
77 template <class ControlPointsT>
78 void Nurbs3D<ControlPointsT>::Eval(const Vector3<double> &nurbsParam, ControlPointsT &result) const{
79 
80  BIASASSERT(nurbsParam[0] >= 0.0 && nurbsParam[0] <= 1.0 &&
81  nurbsParam[1] >= 0.0 && nurbsParam[1] <= 1.0 &&
82  nurbsParam[2] >= 0.0 && nurbsParam[2] <= 1.0)
83 
84  // Initialize the return value
85  TemplateInit::Init(result);
86 
87  // Get the relevant control points
88  double weightSum = 0.0;
89  Vector3<int> relevantBegin;
90  Vector3<int> relevantEnd;
91  GetRelevantBaseCtrlPoints(nurbsParam, relevantBegin, relevantEnd);
92  double* baseFuncCache = new double[degree_+1];
93 
94  // Evaluate the relevant base functions and use them to wheight the
95  // control points
96  for (int x = relevantBegin[0]; x <= relevantEnd[0]; x++)
97  for (int y = relevantBegin[1]; y <= relevantEnd[1]; y++)
98  for (int z = relevantBegin[2]; z <= relevantEnd[2]; z++){
99  double weight = (BaseFunc(0,x,degree_,nurbsParam[0],baseFuncCache)*
100  BaseFunc(1,y,degree_,nurbsParam[1],baseFuncCache)*
101  BaseFunc(2,z,degree_,nurbsParam[2],baseFuncCache))*
102  weights_[x][y][z];
103  weightSum += weight;
104 
105  result += (weight * controlPoints_[x][y][z]);
106  }
107 
108  if (weightSum != 0.0)
109  result = result * (1.0 / weightSum);
110 
111  delete[] baseFuncCache;
112 }
113 
114 template <class ControlPointsT>
115 void Nurbs3D<ControlPointsT>::Init(const ControlPointsT minX,const ControlPointsT maxX,
116  const ControlPointsT minY,const ControlPointsT maxY,
117  const ControlPointsT minZ,const ControlPointsT maxZ){
118  for (int x = 0; x < numCtrlPoints_; x++)
119  for (int y = 0; y < numCtrlPoints_; y++)
120  for (int z = 0; z < numCtrlPoints_; z++){
121  double ratioX = (double)x/(double)(numCtrlPoints_-1);
122  double ratioY = (double)y/(double)(numCtrlPoints_-1);
123  double ratioZ = (double)z/(double)(numCtrlPoints_-1);
124  controlPoints_[x][y][z] = ratioX * maxX + (1.0 - ratioX) * minX
125  + ratioY * maxY + (1.0 - ratioY) * minY
126  + ratioZ * maxZ + (1.0 - ratioZ) * minZ;
127  }
128 
129 }
130 
131 namespace BIAS {
132 
133  // Implement NURBS3D for all common types of vectors:
134  template class Nurbs3D<double >;
135  template class Nurbs3D<Vector<double> >;
136  template class Nurbs3D<Vector2<double> >;
137  template class Nurbs3D<Vector3<double> >;
138  template class Nurbs3D<Vector4<double> >;
139  template class Nurbs3D<HomgPoint2D >;
140  template class Nurbs3D<HomgPoint3D >;
141 
142 }
double Length() const
returns the Euclidean Length of the Vector
Definition: Vector3.hh:193
static void Init(double &value)
ControlPointsT *** controlPoints_
The array of control points.
Definition: Nurbs3D.hh:59
void Init(const ControlPointsT minX, const ControlPointsT maxX, const ControlPointsT minY, const ControlPointsT maxY, const ControlPointsT minZ, const ControlPointsT maxZ)
Very generic initialization function for the control points.
Definition: Nurbs3D.cpp:115
std::vector< BIAS::Vector3< int > > sphereCtrlPoints_
A vector containing the control points in the vicinity of the hyper sphere.
Definition: Nurbs3D.hh:65
Base class for NURBS classes.
Definition: NurbsBase.hh:29
This class implements a NURBS-function with a three dimensional parameter space (A NURBS volume)...
Definition: Nurbs3D.hh:31
void Eval(const BIAS::Vector3< double > &nurbsParam, ControlPointsT &result) const
Evaluate the NURBS function at a given point in the parameter space.
Definition: Nurbs3D.cpp:78
void EvalBaseFunc(const int x, const int y, const int z, const BIAS::Vector3< double > &nurbsParam, double &result) const
Evaluate the base function for the control point (x,y,z) for the point nurbsParam.
Definition: Nurbs3D.hh:103
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
int numCtrlPoints_
The number of control points per dimension.
Definition: NurbsBase.hh:71
double *** weights_
The corresponding weights to the function points.
Definition: Nurbs3D.hh:62