count method (better than filter, then size).
parent
70f428fed1
commit
617040f503
|
|
@ -385,6 +385,17 @@ namespace gtsam {
|
||||||
ConstFiltered<ValueType>
|
ConstFiltered<ValueType>
|
||||||
filter(const boost::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
|
filter(const boost::function<bool(Key)>& filterFcn = &_truePredicate<Key>) const;
|
||||||
|
|
||||||
|
// Count values of given type \c ValueType
|
||||||
|
template<class ValueType>
|
||||||
|
bool count() const {
|
||||||
|
size_t i = 0;
|
||||||
|
for (const auto& key_value : *this) {
|
||||||
|
if (dynamic_cast<const GenericValue<ValueType>*>(&key_value.value))
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Filters based on ValueType (if not Value) and also based on the user-
|
// Filters based on ValueType (if not Value) and also based on the user-
|
||||||
// supplied \c filter function.
|
// supplied \c filter function.
|
||||||
|
|
|
||||||
|
|
@ -383,6 +383,7 @@ TEST(Values, filter) {
|
||||||
++ i;
|
++ i;
|
||||||
}
|
}
|
||||||
EXPECT_LONGS_EQUAL(2, (long)i);
|
EXPECT_LONGS_EQUAL(2, (long)i);
|
||||||
|
EXPECT_LONGS_EQUAL(2, (long)values.count<Pose3>());
|
||||||
|
|
||||||
// construct a values with the view
|
// construct a values with the view
|
||||||
Values actualSubValues2(pose_filtered);
|
Values actualSubValues2(pose_filtered);
|
||||||
|
|
|
||||||
|
|
@ -432,14 +432,14 @@ void writeG2o(const NonlinearFactorGraph& graph, const Values& estimate,
|
||||||
fstream stream(filename.c_str(), fstream::out);
|
fstream stream(filename.c_str(), fstream::out);
|
||||||
|
|
||||||
// save 2D & 3D poses
|
// save 2D & 3D poses
|
||||||
Values::ConstFiltered<Pose2> viewPose2 = estimate.filter<Pose2>();
|
const auto viewPose2 = estimate.filter<Pose2>();
|
||||||
for(const Values::ConstFiltered<Pose2>::KeyValuePair& key_value: viewPose2) {
|
for(const auto& key_value: viewPose2) {
|
||||||
stream << "VERTEX_SE2 " << key_value.key << " " << key_value.value.x() << " "
|
stream << "VERTEX_SE2 " << key_value.key << " " << key_value.value.x() << " "
|
||||||
<< key_value.value.y() << " " << key_value.value.theta() << endl;
|
<< key_value.value.y() << " " << key_value.value.theta() << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Values::ConstFiltered<Pose3> viewPose3 = estimate.filter<Pose3>();
|
const auto viewPose3 = estimate.filter<Pose3>();
|
||||||
for(const Values::ConstFiltered<Pose3>::KeyValuePair& key_value: viewPose3) {
|
for(const auto& key_value: viewPose3) {
|
||||||
Point3 p = key_value.value.translation();
|
Point3 p = key_value.value.translation();
|
||||||
Rot3 R = key_value.value.rotation();
|
Rot3 R = key_value.value.rotation();
|
||||||
stream << "VERTEX_SE3:QUAT " << key_value.key << " " << p.x() << " " << p.y() << " " << p.z()
|
stream << "VERTEX_SE3:QUAT " << key_value.key << " " << p.x() << " " << p.y() << " " << p.z()
|
||||||
|
|
@ -448,7 +448,7 @@ void writeG2o(const NonlinearFactorGraph& graph, const Values& estimate,
|
||||||
}
|
}
|
||||||
|
|
||||||
// save edges (2D or 3D)
|
// save edges (2D or 3D)
|
||||||
for(boost::shared_ptr<NonlinearFactor> factor_: graph) {
|
for(const auto& factor_: graph) {
|
||||||
boost::shared_ptr<BetweenFactor<Pose2> > factor =
|
boost::shared_ptr<BetweenFactor<Pose2> > factor =
|
||||||
boost::dynamic_pointer_cast<BetweenFactor<Pose2> >(factor_);
|
boost::dynamic_pointer_cast<BetweenFactor<Pose2> >(factor_);
|
||||||
if (factor){
|
if (factor){
|
||||||
|
|
@ -857,48 +857,47 @@ bool writeBAL(const string& filename, SfM_data &data) {
|
||||||
|
|
||||||
bool writeBALfromValues(const string& filename, const SfM_data &data,
|
bool writeBALfromValues(const string& filename, const SfM_data &data,
|
||||||
Values& values) {
|
Values& values) {
|
||||||
|
using Camera = PinholeCamera<Cal3Bundler>;
|
||||||
SfM_data dataValues = data;
|
SfM_data dataValues = data;
|
||||||
|
|
||||||
// Store poses or cameras in SfM_data
|
// Store poses or cameras in SfM_data
|
||||||
Values valuesPoses = values.filter<Pose3>();
|
size_t nrPoses = values.count<Pose3>();
|
||||||
if (valuesPoses.size() == dataValues.number_cameras()) { // we only estimated camera poses
|
if (nrPoses == dataValues.number_cameras()) { // we only estimated camera poses
|
||||||
for (size_t i = 0; i < dataValues.number_cameras(); i++) { // for each camera
|
for (size_t i = 0; i < dataValues.number_cameras(); i++) { // for each camera
|
||||||
Key poseKey = symbol('x', i);
|
Key poseKey = symbol('x', i);
|
||||||
Pose3 pose = values.at<Pose3>(poseKey);
|
Pose3 pose = values.at<Pose3>(poseKey);
|
||||||
Cal3Bundler K = dataValues.cameras[i].calibration();
|
Cal3Bundler K = dataValues.cameras[i].calibration();
|
||||||
PinholeCamera<Cal3Bundler> camera(pose, K);
|
Camera camera(pose, K);
|
||||||
dataValues.cameras[i] = camera;
|
dataValues.cameras[i] = camera;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Values valuesCameras = values.filter<PinholeCamera<Cal3Bundler> >();
|
size_t nrCameras = values.count<Camera>();
|
||||||
if (valuesCameras.size() == dataValues.number_cameras()) { // we only estimated camera poses and calibration
|
if (nrCameras == dataValues.number_cameras()) { // we only estimated camera poses and calibration
|
||||||
for (size_t i = 0; i < dataValues.number_cameras(); i++) { // for each camera
|
for (size_t i = 0; i < nrCameras; i++) { // for each camera
|
||||||
Key cameraKey = i; // symbol('c',i);
|
Key cameraKey = i; // symbol('c',i);
|
||||||
PinholeCamera<Cal3Bundler> camera =
|
Camera camera = values.at<Camera>(cameraKey);
|
||||||
values.at<PinholeCamera<Cal3Bundler> >(cameraKey);
|
|
||||||
dataValues.cameras[i] = camera;
|
dataValues.cameras[i] = camera;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cout
|
cout
|
||||||
<< "writeBALfromValues: different number of cameras in SfM_dataValues (#cameras= "
|
<< "writeBALfromValues: different number of cameras in SfM_dataValues (#cameras= "
|
||||||
<< dataValues.number_cameras() << ") and values (#cameras "
|
<< dataValues.number_cameras() << ") and values (#cameras "
|
||||||
<< valuesPoses.size() << ", #poses " << valuesCameras.size() << ")!!"
|
<< nrPoses << ", #poses " << nrCameras << ")!!"
|
||||||
<< endl;
|
<< endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store 3D points in SfM_data
|
// Store 3D points in SfM_data
|
||||||
Values valuesPoints = values.filter<Point3>();
|
size_t nrPoints = values.count<Point3>(), nrTracks = dataValues.number_tracks();
|
||||||
if (valuesPoints.size() != dataValues.number_tracks()) {
|
if (nrPoints != nrTracks) {
|
||||||
cout
|
cout
|
||||||
<< "writeBALfromValues: different number of points in SfM_dataValues (#points= "
|
<< "writeBALfromValues: different number of points in SfM_dataValues (#points= "
|
||||||
<< dataValues.number_tracks() << ") and values (#points "
|
<< nrTracks << ") and values (#points "
|
||||||
<< valuesPoints.size() << ")!!" << endl;
|
<< nrPoints << ")!!" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t j = 0; j < dataValues.number_tracks(); j++) { // for each point
|
for (size_t j = 0; j < nrTracks; j++) { // for each point
|
||||||
Key pointKey = P(j);
|
Key pointKey = P(j);
|
||||||
if (values.exists(pointKey)) {
|
if (values.exists(pointKey)) {
|
||||||
Point3 point = values.at<Point3>(pointKey);
|
Point3 point = values.at<Point3>(pointKey);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue