Sharing an object between residual blocks.

53 views
Skip to first unread message

Gohteu Nuetah

unread,
Apr 25, 2017, 12:05:25 PM4/25/17
to Ceres Solver
Hey everyone

I have a two all my cost functions have in common one rather performance expensive "buildup" of an object based on parameters they share. I wanted to know wether it is possible to have this object have built up once and share it between the different cost functions.

My code looks something like this:

struct C1{
//...

bool operator()(T const* const* parameters, T* residuals) const{

auto instance = reference->WithParameters(parameters[0]);
 
//...
}
}; 

struct C1{
//...

bool operator()(T const* const* parameters, T* residuals) const{

auto instance = reference->WithParameters(parameters[0]);
 
//...
}
}; 

Is there a way to have the instance computed just once every cycle?

Adel Ahmadyan

unread,
May 8, 2017, 8:17:03 PM5/8/17
to Ceres Solver
One solution would be to use the factory to create the cost function classes C1 and C2 and then define a shared pointer as a member in these classes.

class C1{
shared_ptr<data> sharedData;
public:
    bool operator() ...
    static ceres::CostFunction* Create(shared_tr<data>){
         return (new ceres::AutoDiffCostFunction<C1, ...>(new C1(data)));
    }
}

Reply all
Reply to author
Forward
0 new messages