42 #include <pcl/common/bivariate_polynomial.h>
54 template<
typename real>
57 setDegree(new_degree);
61 template<
typename real>
68 template<
typename real>
75 template<
typename real>
void
84 int oldDegree = degree;
86 if (oldDegree != degree)
89 parameters =
new real[getNoOfParameters ()];
91 delete gradient_x; gradient_x =
nullptr;
92 delete gradient_y; gradient_y =
nullptr;
96 template<
typename real>
void
99 delete[] parameters; parameters =
nullptr;
100 delete gradient_x; gradient_x =
nullptr;
101 delete gradient_y; gradient_y =
nullptr;
105 template<
typename real>
void
108 if (
this == &other)
return;
109 if (degree != other.
degree)
113 parameters =
new real[getNoOfParameters ()];
119 gradient_x =
nullptr;
120 gradient_y =
nullptr;
122 else if (!gradient_x)
128 std::copy_n(other.
parameters, getNoOfParameters (), parameters);
138 template<
typename real>
void
141 if (gradient_x!=
nullptr && !forceRecalc)
return;
143 if (gradient_x ==
nullptr)
145 if (gradient_y ==
nullptr)
148 unsigned int parameterPosDx=0, parameterPosDy=0;
149 for (
int xDegree=degree; xDegree>=0; xDegree--)
151 for (
int yDegree=degree-xDegree; yDegree>=0; yDegree--)
155 gradient_x->parameters[parameterPosDx] = xDegree * parameters[parameterPosDx];
160 gradient_y->parameters[parameterPosDy] = yDegree * parameters[ ( (degree+2-xDegree)* (degree+1-xDegree))/2 -
169 template<
typename real> real
172 unsigned int parametersSize = getNoOfParameters ();
173 real* tmpParameter = ¶meters[parametersSize-1];
174 real tmpX=1.0, tmpY, ret=0;
175 for (
int xDegree=0; xDegree<=degree; xDegree++)
178 for (
int yDegree=0; yDegree<=degree-xDegree; yDegree++)
180 ret += (*tmpParameter)*tmpX*tmpY;
190 template<
typename real>
void
193 calculateGradient ();
194 gradX = gradient_x->getValue (x, y);
195 gradY = gradient_y->getValue (x, y);
199 template<
typename real>
void
201 std::vector<int>& types)
const
209 real x = (
static_cast<real
>(2)*parameters[2]*parameters[3] - parameters[1]*parameters[4]) /
210 (parameters[1]*parameters[1] -
static_cast<real
>(4)*parameters[0]*parameters[3]),
211 y = (
static_cast<real
>(-2)*parameters[0]*x - parameters[2]) / parameters[1];
213 if (!std::isfinite(x) || !std::isfinite(y))
217 real det_H =
static_cast<real
>(4)*parameters[0]*parameters[3] - parameters[1]*parameters[1];
219 if (det_H >
static_cast<real
>(0))
221 if (parameters[0]+parameters[3] <
static_cast<real
>(0))
226 x_values.push_back(x);
227 y_values.push_back(y);
228 types.push_back(type);
232 std::cerr << __PRETTY_FUNCTION__ <<
" is not implemented for polynomials of degree "<<degree<<
". Sorry.\n";
237 template<
typename real> std::ostream&
242 real currentParameter;
243 for (
int xDegree=p.
degree; xDegree>=0; xDegree--)
245 for (
int yDegree=p.
degree-xDegree; yDegree>=0; yDegree--)
247 currentParameter = *tmpParameter;
250 os << (currentParameter<0.0?
" - ":
" + ");
251 currentParameter = std::abs (currentParameter);
253 os << currentParameter;
275 template<
typename real>
void
278 os.write (
reinterpret_cast<const char*
> (°ree),
sizeof (
int));
279 unsigned int paramCnt = getNoOfParametersFromDegree (this->degree);
280 os.write (
reinterpret_cast<const char*
> (this->parameters), paramCnt *
sizeof (real));
284 template<
typename real>
void
287 std::ofstream fout (filename);
292 template<
typename real>
void
296 os.read (
reinterpret_cast<char*
> (&this->degree),
sizeof (
int));
297 unsigned int paramCnt = getNoOfParametersFromDegree (this->degree);
298 parameters =
new real[paramCnt];
299 os.read (
reinterpret_cast<char*
> (&(*this->parameters)), paramCnt *
sizeof (real));
303 template<
typename real>
void
306 std::ifstream fin (filename);
This represents a bivariate polynomial and provides some functionality for it.
~BivariatePolynomialT()
Destructor.
void deepCopy(const BivariatePolynomialT< real > &other)
Create a deep copy of the given polynomial.
void findCriticalPoints(std::vector< real > &x_values, std::vector< real > &y_values, std::vector< int > &types) const
Returns critical points of the polynomial.
BivariatePolynomialT(int new_degree=0)
Constructor.
void memoryCleanUp()
Delete all members.
BivariatePolynomialT< real > * gradient_y
void readBinary(std::istream &os)
read binary from a stream
void writeBinary(std::ostream &os) const
write as binary to a stream
real getValue(real x, real y) const
Calculate the value of the polynomial at the given point.
void calculateGradient(bool forceRecalc=false)
Calculate the gradient of this polynomial If forceRecalc is false, it will do nothing when the gradie...
void setDegree(int new_degree)
Initialize members to default values.
BivariatePolynomialT< real > * gradient_x
void getValueOfGradient(real x, real y, real &gradX, real &gradY)
Calculate the value of the gradient at the given point.
std::ostream & operator<<(std::ostream &os, const BivariatePolynomialT< real > &p)