wolf merge requestshttps://gitlab.iri.upc.edu/mobile_robotics/wolf_projects/wolf_lib/wolf/-/merge_requests2016-11-27T00:42:17+01:00https://gitlab.iri.upc.edu/mobile_robotics/wolf_projects/wolf_lib/wolf/-/merge_requests/105[WIP] Emplace(): create objects and link them to the wolf tree in one line2016-11-27T00:42:17+01:00Joan Solà Ortega[WIP] Emplace(): create objects and link them to the wolf tree in one lineHello @joanvallve @artivis @acoromin
In line with some old discussions about a proper and useful API, I have a suggestion for improving the creation of nodes (of the correct type) and linking them to the wolf tree in a simple way.
...Hello @joanvallve @artivis @acoromin
In line with some old discussions about a proper and useful API, I have a suggestion for improving the creation of nodes (of the correct type) and linking them to the wolf tree in a simple way.
The idea is similar to the Factory. It might use a Factory, or not. But it uses what it is known as 'factory method'.
My suggestion is to have a method in each constraint class called `emplace()`, which does the following:
- Create an object of the correct type
- add it to its parent in the tree
- establish all the necessary links within the tree
The most intricate case is the `ConstraintDerived` classes, but the same applies for __frames__ and __landmarks__, which are dynamically created in the processors.
Let me first expose the problem with a particularly difficult case: the class `ConstraintAHP` or 'constraint for the anchored homogeneous point landmark':
- `ConstraintAHP` is child of a feature (as all constraints are)
- it points to this feature
- this feature has a pointer to the constraint in its list of constraints
- It constrains what is called its 'anchor frame'
- it points to this frame
- this frame has a pointer to the constraint in its list of constrained_by
- It constrains a landmark, which knows who is the 'anchor frame'
- it points to this landmark
- this landmark has a pointer to the constraint in its list of constrained_by
The idea is to have this `emplace()` method as a __static method of the derived constraint class__, which does all the job:
```c++
static ConstraintAHP::Ptr ConstraintAHP::emplace(FeatureBasePtr ftr,
LandmarkAHP::Ptr lmk,
bool apply_loss_fcn = false,
ConstraintStatus st = CTR_ACTIVE)
{
ConstraintAHP::Ptr ctr = make_shared<ConstraintAHP>(ftr, lmk, apply_loss_fcn, st); // create constraint
ftr->addConstraint(ctr); // link it to parent feature
lmk->getAnchorFrame()->addConstrainedBy(ctr); // link it to constrained frame
lmk->addConstrainedBy(ctr); // link it to constrained landmark
}
```
At user level, at the time we need to create such constraint, we just need to know the two affected objects: feature, and landmark. then:
```c++
// these two guys must exist already:
FeaturePointImage::Ptr _feature_ptr = [...]; // a feature
LandmarkAHP::Ptr _landmark_other_ptr = [...]; // a landmark
// then the constraint from the feature to the landmark is:
ConstraintAHP::emplace(_feature_ptr, _landmark_other_ptr); // the constraint is created and fully linked in one line!
```
In the branch of this MR, `emplace`, I have been playing a bit with `ConstraintFactory` (a new class in this branch). I suggest that you read the documentation of the `ConstraintFactory`, which is a `typedef` at the end of the file `Factory.h`. The design of `emplace()` there goes through a previous `create()`, which is needed should we also want a factory. --- for the best reading experience, use Doxygen to compile!emplace() APIJoan Solà OrtegaJoan Solà Ortega