Beginner question with CostFunctiontoFunctor

227 views
Skip to first unread message

Minh Vo

unread,
Jul 12, 2016, 6:23:24 PM7/12/16
to Ceres Solver
Hi,

I am trying out the CostFunctionToFunctor api code on ceres-solver.org. Essentially, it is just this:

template<typename T>void RotateAndTranslatePoint(const T* rotation, const T* translation, const T* point, T* result)
{
     return;
}
class IntrinsicProjection : public SizedCostFunction<2, 5, 3>
{
     public:
        IntrinsicProjection(const double* observations);
        virtual bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const
        {
             residuals[0] = 0.0;
             jacobians[0][0] = 0.0;
             return true;
        }      
};

struct CameraProjection
{
         CameraProjection(double* observation)
         {
             intrinsic_projection_.reset(new CostFunctionToFunctor<2, 5, 3>(new IntrinsicProjection(observation)));
         }
        
         template <typename T> bool operator()(const T* rotation, const T* translation, const T* intrinsics, const T* point, T* residual) const
         {
              T transformed_point[3];
              RotateAndTranslatePoint(rotation, translation, point, transformed_point);
              return (*intrinsic_projection_)(intrinsics, transformed_point, residual);
         }

    private:
        ceres::internal::scoped_ptr<CostFunctionToFunctor<2, 5, 3> > intrinsic_projection_;
};


and it is being called by:

double dummy[2];
CostFunction* cost_function = new AutoDiffCostFunction<CameraProjection, 2, 3, 3, 3, 5>(new CameraProjection(dummy));
problem.AddResidualBlock(cost_function, NULL, dummy, dummy, dummy, dummy);


However, I keep getting the error when compliing:unresolved external symbol "public: __cdecl IntrinsicProjection::IntrinsicProjection(double const *).....
I must have missed something really basic. Can someone help?

Thanks,
Minh.

Minh Vo

unread,
Jul 12, 2016, 6:32:12 PM7/12/16
to Ceres Solver
I realized that if I don't call:

double dummy[2];
CostFunction* cost_function = new AutoDiffCostFunction<CameraProjection, 2, 3, 3, 3, 5>(new CameraProjection(dummy));
problem.AddResidualBlock(cost_function, NULL, dummy, dummy, dummy, dummy);

It can compile without error.

Chris Sweeney

unread,
Jul 12, 2016, 6:39:00 PM7/12/16
to Ceres Solver

Maybe I'm wrong here (c++ gurus feel free to chime in)... but you are trying to call a virtual function (run time decision) in a templated method which requires the call to be known at compile time. You can try to use something like a curiously recurring template pattern to get around this.


--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/5ae1237e-58e8-4478-9f84-e20c8592b075%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sameer Agarwal

unread,
Jul 12, 2016, 6:39:29 PM7/12/16
to Ceres Solver
also you cannot pass the same ptr to AddResidualBlock.
Sameer

William Rucklidge

unread,
Jul 12, 2016, 6:42:48 PM7/12/16
to ceres-...@googlegroups.com
It's something very simple; you'll hit yourself. You declared, but never defined, IntrinsicProjection's ctor.
        IntrinsicProjection(const double* observations);
should be
        IntrinsicProjection(const double* observations) {}


Minh Vo

unread,
Jul 12, 2016, 6:52:28 PM7/12/16
to ceres-...@googlegroups.com
William: Ahh, it is really a silly error. That's for pointing that out!



--
You received this message because you are subscribed to a topic in the Google Groups "Ceres Solver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ceres-solver/9pOqee7AIAQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/CABsh3u4_Nq5-pRA8tD1pTfrDT9GCjnXB0%2BexrHk-gUAn0g5Btg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages