28#include <Eigen/Geometry>
44 const Eigen::Vector3d& circle) {
45 Eigen::Vector2d center = circle.head<2>();
46 double radius = circle(2);
48 for (
int i = 0; i < numPoints; ++i) {
49 double d = (points[i] - center).norm() - radius;
63 bool read(std::istream& )
override {
return false; }
65 bool write(std::ostream& )
const override {
return false; }
72 Eigen::Vector3d::ConstMapType v(update);
87 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
89 bool read(std::istream& )
override {
return false; }
90 bool write(std::ostream& )
const override {
return false; }
95 const T& radius = circle[2];
104int main(
int argc,
char** argv) {
109 arg.
param(
"numPoints", numPoints, 100,
110 "number of points sampled from the circle");
111 arg.
param(
"i", maxIterations, 10,
"perform n iterations");
112 arg.
param(
"v", verbose,
false,
"verbose output of the optimization process");
116 Eigen::Vector2d center(4.0, 2.0);
118 Eigen::Vector2d* points =
new Eigen::Vector2d[numPoints];
121 for (
int i = 0; i < numPoints; ++i) {
124 points[i].x() = center.x() + r * cos(angle);
125 points[i].y() = center.y() + r * sin(angle);
143 Eigen::Vector3d(3.0, 3.0, 3.0));
146 for (
int i = 0; i < numPoints; ++i) {
159 if (verbose) cout << endl;
162 cout <<
"Iterative least squares solution" << endl;
163 cout <<
"center of the circle " << circle->
estimate().head<2>().transpose()
165 cout <<
"radius of the cirlce " << circle->
estimate()(2) << endl;
180 Eigen::MatrixXd A(numPoints, 3);
181 Eigen::VectorXd b(numPoints);
182 for (
int i = 0; i < numPoints; ++i) {
183 A(i, 0) = -2 * points[i].x();
184 A(i, 1) = -2 * points[i].y();
186 b(i) = -pow(points[i].x(), 2) - pow(points[i].y(), 2);
188 Eigen::Vector3d solution =
189 (A.transpose() * A).ldlt().solve(A.transpose() * b);
191 solution(2) = sqrt(pow(solution(0), 2) + pow(solution(1), 2) - solution(2));
192 cout <<
"Linear least squares solution" << endl;
193 cout <<
"center of the circle " << solution.head<2>().transpose() << endl;
194 cout <<
"radius of the cirlce " << solution(2) << endl;
195 cout <<
"error " <<
errorOfSolution(numPoints, points, solution) << endl;
#define G2O_MAKE_AUTO_AD_FUNCTIONS
double errorOfSolution(int numPoints, Eigen::Vector2d *points, const Eigen::Vector3d &circle)
measurement for a point on the circle
bool operator()(const T *circle, T *error) const
bool write(std::ostream &) const override
write the vertex to a stream
EIGEN_MAKE_ALIGNED_OPERATOR_NEW EdgePointOnCircle()
bool read(std::istream &) override
read the vertex from a stream, i.e., the internal state of the vertex
a circle located at x,y with radius r
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
void oplusImpl(const double *update) override
void setToOriginImpl() override
sets the node to the origin (used in the multilevel stuff)
bool write(std::ostream &) const override
write the vertex to a stream
bool read(std::istream &) override
read the vertex from a stream, i.e., the internal state of the vertex
EIGEN_STRONG_INLINE const Measurement & measurement() const
accessor functions for the measurement represented by the edge
virtual void setMeasurement(const Measurement &m)
void setInformation(const InformationType &information)
const ErrorVector & error() const
const EstimateType & estimate() const
return the current estimate of the vertex
void setEstimate(const EstimateType &et)
set the estimate for the vertex also calls updateCache()
Command line parsing of argc and argv.
bool parseArgs(int argc, char **argv, bool exitOnError=true)
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
void setVertex(size_t i, Vertex *v)
virtual void setId(int id)
static OptimizationAlgorithmFactory * instance()
return the instance
static double gaussRand(double mean, double sigma)
static double uniformRand(double lowerBndr, double upperBndr)
int optimize(int iterations, bool online=false)
void setVerbose(bool verbose)
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
void setAlgorithm(OptimizationAlgorithm *algorithm)
#define __PRETTY_FUNCTION__
Eigen::Matrix< T, N, 1, Eigen::ColMajor > VectorN
#define G2O_USE_OPTIMIZATION_LIBRARY(libraryname)
virtual bool addEdge(HyperGraph::Edge *e)
virtual bool addVertex(HyperGraph::Vertex *v, Data *userData)
describe the properties of a solver