11 #include <Base/Common/XMLIO.hh>
13 #include <Base/Math/Random.hh>
16 #include <Base/Common/XMLIO.hh>
18 #include <MathAlgo/NurbsSurface.hh>
23 NurbsSurface::NurbsSurface() {
27 NurbsSurface::~NurbsSurface() {
31 void NurbsSurface::Init(
int dimu,
int dimv,
int degree)
39 if(dimu_ < degree_ || dimv_ < degree_){
40 cout <<
"NurbsSurface::Init - The Dimensions have to be greater then the degree" << endl;
44 knotsu_.resize(dimu_ + degree_ + 1);
45 knotsv_.resize(dimv_ + degree_ + 1);
49 ctrlpoints_.resize(dimu_);
50 for (i = 0; i < dimu_; i++){
51 ctrlpoints_[i].resize(dimv_);
58 void NurbsSurface::SetKnotsToDefault()
61 cout <<
"NurbsSurface::SetKnotsToDefault - Please initialize the Surface first" << endl;
65 for(
int i = 0; i < (dimu_ + degree_ + 1); i++){
68 }
else if(i > (dimu_ - 1)){
71 tmp = (double)(1.0/(dimu_ - degree_))*(
double)(i-degree_);
76 for(
int i = 0; i < (dimv_ + degree_ + 1); i++){
79 }
else if(i > (dimv_ - 1)){
82 tmp = (double)(1.0/(dimv_ - degree_))*(
double)(i-degree_);
92 cout <<
"NurbsSurface::SetCTRLPointsToRandom - Please initialize the Surface first" << endl;
100 double xdiff, ydiff, xstart, ystart;
103 xdiff = (ende.
GetX() - start.
GetX())/(dimu_ - 1);
104 xstart = start.
GetX();
106 xdiff = (start.
GetX() - ende.
GetX())/(dimu_ - 1);
107 xstart = ende.
GetX();
111 ydiff = (ende.
GetY() - start.
GetY())/(dimv_ - 1);
112 ystart = start.
GetY();
114 ydiff = (start.
GetY() - ende.
GetY())/(dimv_ - 1);
115 ystart = ende.
GetY();
119 for(
int x = 0; x < dimu_; x++){
120 for(
int y = 0; y < dimv_; y++){
122 SetCtrlPoint(x,y,tmp);
128 #ifdef BIAS_HAVE_XML2
129 int NurbsSurface::LoadFromXML(std::string xmlfile)
132 cout <<
"NurbsSurface::LoadFromXML - Your initialization will be overwritten by the XML" << endl;
136 cout <<
"NurbsSurface::LoadFromXML - No XML-File given" << endl;
143 xmlNodePtr currentNode;
144 xmlNodePtr ctrlpointsNode;
145 xmlNodePtr currentCtrlPoint;
147 rootNode = myXML.
read(xmlfile);
149 if (rootNode == NULL) {
150 cout <<
"NurbsSurface::LoadFromXML - Error while parsing XML-File" << endl;
154 currentNode = myXML.
getChild(rootNode,
"dimu");
156 currentNode = myXML.
getChild(rootNode,
"dimv");
158 currentNode = myXML.
getChild(rootNode,
"degree");
162 Init(dimu,dimv,degree);
165 ctrlpointsNode = myXML.
getChild(rootNode,
"ctrlpoints");
170 for (
int i = 0; i < dimu_; i++){
171 for (
int j = 0; j < dimv_; j++){
173 SetCtrlPoint(i,j,vec);
181 void NurbsSurface::SaveToXML(std::string xmlfile)
185 cout <<
"NurbsSurface::SaveToXML - Cannot save uninitialized NURBS surface" << endl;
189 cout <<
"NurbsSurface::SaveToXML - No XML-File given" << endl;
196 xmlNodePtr ctrlpointsNode;
197 xmlNodePtr currentCtrlPoint;
210 ctrlpointsNode = myXML.
getChild(rootNode,
"ctrlpoints");
214 for (
int i = 0; i < dimu_; i++){
215 for (
int j = 0; j < dimv_; j++){
216 currentCtrlPoint = myXML.
addChildNode(ctrlpointsNode,
"ctrlpoint");
227 myXML.
write(xmlfile);
231 void NurbsSurface::LoadFromXMLWeighted(std::string xmlfile,
float weight){
233 for (
int u = 0; u < dimu_; u++){
234 for (
int v = 0; v < dimv_; v++){
235 oldCtrlPoints[u+v*dimu_] = GetCtrlPoint(u,v);
238 LoadFromXML(xmlfile);
240 for (
int u = 0; u < dimu_; u++){
241 for (
int v = 0; v < dimv_; v++){
243 pNew[0] = pNew[0] * (weight) + oldCtrlPoints[u+v*dimu_][0] * (1.0 - weight);
244 pNew[1] = pNew[1] * (weight) + oldCtrlPoints[u+v*dimu_][1] * (1.0 - weight);
245 pNew[2] = pNew[2] * (weight) + oldCtrlPoints[u+v*dimu_][2] * (1.0 - weight);
246 SetCtrlPoint(u,v,pNew);
255 int NurbsSurface::FindSpan(
int n,
int p,
double u, std::vector<double> U){
256 if(u == U[n+1])
return n;
259 int mid = (low+high)/2;
260 while(u < U[mid] || u >= U[mid+1]){
270 std::vector<double> NurbsSurface::BasisFuns(
int i,
double u,
int p, std::vector<double> U){
271 std::vector<double> N, left, right;
277 for(
int j = 1; j <= p; j++){
278 left[j] = u-U[i+1-j];
281 for(
int r = 0; r < j; r++){
282 temp = N[r]/(right[r+1]+left[j-r]);
283 N[r] = saved+right[r+1]*temp;
284 saved = left[j-r]*temp;
300 int uspan = FindSpan(dimu_,degree_,u,knotsu_);
301 std::vector<double> Nu = BasisFuns(uspan,u,degree_,knotsu_);
302 int vspan = FindSpan(dimv_,degree_,v,knotsv_);
303 std::vector<double> Nv = BasisFuns(vspan,v,degree_,knotsv_);
304 int uind = uspan - degree_;
308 for(
int l = 0; l <= degree_; l++){
310 int vind = vspan-degree_+l;
311 for(
int k = 0; k <= degree_; k++){
312 double* ptr = GetCtrlPoint(uind+k,vind).GetData();
313 temp[0] += Nu[k] * ptr[0];
314 temp[1] += Nu[k] * ptr[1];
315 temp[2] += Nu[k] * ptr[2];
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this->data_
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn't change the ...
xmlNodePtr getNextChild()
Get the next child of the parent specified in the last getFirstChild() call, the class remembers the ...
int write(const std::string &Filename, bool AutoAddCompressionSuffix=true) const
Write the whole tree that was constructed in memory to disk.
xmlNodePtr read(const std::string &Filename)
Read and parse an XML file from disk, DtD validation is not yet implemented.
xmlNodePtr getChild(const xmlNodePtr ParentNode, const std::string &ChildName)
Get a child of a Parent node by specifying the childs name, NULL is returned if the ParentNode has no...
void Set(const HOMGPOINT3D_TYPE &x, const HOMGPOINT3D_TYPE &y, const HOMGPOINT3D_TYPE &z)
set elementwise with given 3 euclidean scalar values.
xmlNodePtr create(const std::string &RootNodeName)
Create the base of a new XML-Tree in memory, already with a one and only root node.
void SetZero()
set all values to 0
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
void addContent(const xmlNodePtr Node, const std::string &Content)
Add content to a node.
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
xmlNodePtr getFirstChild(const xmlNodePtr ParentNode)
Get the first child of a given parent, or NULL for no childs.
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
T & GetX()
read only access functions to use names instead of indizes
double getAttributeValueDouble(const xmlAttrPtr Attribute) const
void Reset()
calls srand() with a seed generated from system call time, also initializes some other variables ...
int getNodeContentInt(const xmlNodePtr Node) const
class for producing random numbers from different distributions