| 1 |
#include <iostream> |
| 2 |
#include <vector> |
| 3 |
#include <opengv/absolute_pose/AbsoluteAdapterBase.hpp> |
| 4 |
#include <opengv/absolute_pose/methods.hpp> |
| 5 |
#include <opengv/relative_pose/RelativeAdapterBase.hpp> |
| 6 |
#include <opengv/relative_pose/methods.hpp> |
| 7 |
#include <opengv/sac/Ransac.hpp> |
| 8 |
#include <opengv/sac/Lmeds.hpp> |
| 9 |
#include <opengv/sac_problems/absolute_pose/AbsolutePoseSacProblem.hpp> |
| 10 |
#include <opengv/sac_problems/relative_pose/CentralRelativePoseSacProblem.hpp> |
| 11 |
#include <opengv/sac_problems/relative_pose/RotationOnlySacProblem.hpp> |
| 12 |
#include <opengv/triangulation/methods.hpp> |
| 13 |
|
| 14 |
#include "types.hpp" |
| 15 |
|
| 16 |
|
| 17 |
namespace pyopengv { |
| 18 |
|
| 19 |
|
| 20 |
opengv::bearingVector_t bearingVectorFromArray( |
| 21 |
const pyarray_d &array, |
| 22 |
size_t index ) |
| 23 |
{ |
| 24 |
opengv::bearingVector_t v; |
| 25 |
v[0] = *array.data(index, 0); |
| 26 |
v[1] = *array.data(index, 1); |
| 27 |
v[2] = *array.data(index, 2); |
| 28 |
return v; |
| 29 |
} |
| 30 |
|
| 31 |
opengv::point_t pointFromArray( |
| 32 |
const pyarray_d &array, |
| 33 |
size_t index ) |
| 34 |
{ |
| 35 |
opengv::point_t p; |
| 36 |
p[0] = *array.data(index, 0); |
| 37 |
p[1] = *array.data(index, 1); |
| 38 |
p[2] = *array.data(index, 2); |
| 39 |
return p; |
| 40 |
} |
| 41 |
|
| 42 |
py::object arrayFromPoints( const opengv::points_t &points ) |
| 43 |
{ |
| 44 |
std::vector<double> data(points.size() * 3); |
| 45 |
for (size_t i = 0; i < points.size(); ++i) { |
| 46 |
data[3 * i + 0] = points[i][0]; |
| 47 |
data[3 * i + 1] = points[i][1]; |
| 48 |
data[3 * i + 2] = points[i][2]; |
| 49 |
} |
| 50 |
return py_array_from_data(&data[0], points.size(), 3); |
| 51 |
} |
| 52 |
|
| 53 |
py::object arrayFromTranslation( const opengv::translation_t &t ) |
| 54 |
{ |
| 55 |
return py_array_from_data(t.data(), 3); |
| 56 |
} |
| 57 |
|
| 58 |
py::object arrayFromRotation( const opengv::rotation_t &R ) |
| 59 |
{ |
| 60 |
Eigen::Matrix<double, 3, 3, Eigen::RowMajor> R_row_major = R; |
| 61 |
return py_array_from_data(R_row_major.data(), 3, 3); |
| 62 |
} |
| 63 |
|
| 64 |
py::list listFromRotations( const opengv::rotations_t &Rs ) |
| 65 |
{ |
| 66 |
py::list retn; |
| 67 |
for (size_t i = 0; i < Rs.size(); ++i) { |
| 68 |
retn.append(arrayFromRotation(Rs[i])); |
| 69 |
} |
| 70 |
return retn; |
| 71 |
} |
| 72 |
|
| 73 |
py::object arrayFromEssential( const opengv::essential_t &E ) |
| 74 |
{ |
| 75 |
Eigen::Matrix<double, 3, 3, Eigen::RowMajor> E_row_major = E; |
| 76 |
return py_array_from_data(E_row_major.data(), 3, 3); |
| 77 |
} |
| 78 |
|
| 79 |
py::list listFromEssentials( const opengv::essentials_t &Es ) |
| 80 |
{ |
| 81 |
py::list retn; |
| 82 |
for (size_t i = 0; i < Es.size(); ++i) { |
| 83 |
retn.append(arrayFromEssential(Es[i])); |
| 84 |
} |
| 85 |
return retn; |
| 86 |
} |
| 87 |
|
| 88 |
py::object arrayFromTransformation( const opengv::transformation_t &t ) |
| 89 |
{ |
| 90 |
Eigen::Matrix<double, 3, 4, Eigen::RowMajor> t_row_major = t; |
| 91 |
return py_array_from_data(t_row_major.data(), 3, 4); |
| 92 |
} |
| 93 |
|
| 94 |
py::list listFromTransformations( const opengv::transformations_t &t ) |
| 95 |
{ |
| 96 |
py::list retn; |
| 97 |
for (size_t i = 0; i < t.size(); ++i) { |
| 98 |
retn.append(arrayFromTransformation(t[i])); |
| 99 |
} |
| 100 |
return retn; |
| 101 |
} |
| 102 |
|
| 103 |
std::vector<int> getNindices( int n ) |
| 104 |
{ |
| 105 |
std::vector<int> indices; |
| 106 |
for(int i = 0; i < n; i++) |
| 107 |
indices.push_back(i); |
| 108 |
return indices; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
namespace absolute_pose { |
| 113 |
|
| 114 |
class CentralAbsoluteAdapter : public opengv::absolute_pose::AbsoluteAdapterBase |
| 115 |
{ |
| 116 |
protected: |
| 117 |
using AbsoluteAdapterBase::_t; |
| 118 |
using AbsoluteAdapterBase::_R; |
| 119 |
|
| 120 |
public: |
| 121 |
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 122 |
|
| 123 |
CentralAbsoluteAdapter( |
| 124 |
pyarray_d &bearingVectors, |
| 125 |
pyarray_d &points ) |
| 126 |
: _bearingVectors(bearingVectors) |
| 127 |
, _points(points) |
| 128 |
{} |
| 129 |
|
| 130 |
CentralAbsoluteAdapter( |
| 131 |
pyarray_d &bearingVectors, |
| 132 |
pyarray_d &points, |
| 133 |
pyarray_d &R ) |
| 134 |
: _bearingVectors(bearingVectors) |
| 135 |
, _points(points) |
| 136 |
{ |
| 137 |
for (int i = 0; i < 3; ++i) { |
| 138 |
for (int j = 0; j < 3; ++j) { |
| 139 |
_R(i, j) = *R.data(i, j); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
CentralAbsoluteAdapter( |
| 145 |
pyarray_d &bearingVectors, |
| 146 |
pyarray_d &points, |
| 147 |
pyarray_d &t, |
| 148 |
pyarray_d &R ) |
| 149 |
: _bearingVectors(bearingVectors) |
| 150 |
, _points(points) |
| 151 |
{ |
| 152 |
for (int i = 0; i < 3; ++i) { |
| 153 |
_t(i) = *t.data(i); |
| 154 |
} |
| 155 |
for (int i = 0; i < 3; ++i) { |
| 156 |
for (int j = 0; j < 3; ++j) { |
| 157 |
_R(i, j) = *R.data(i, j); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
virtual ~CentralAbsoluteAdapter() {} |
| 163 |
|
| 164 |
//Access of correspondences |
| 165 |
|
| 166 |
virtual opengv::bearingVector_t getBearingVector( size_t index ) const { |
| 167 |
return bearingVectorFromArray(_bearingVectors, index); |
| 168 |
} |
| 169 |
|
| 170 |
virtual double getWeight( size_t index ) const { |
| 171 |
return 1.0; |
| 172 |
} |
| 173 |
|
| 174 |
virtual opengv::translation_t getCamOffset( size_t index ) const { |
| 175 |
return Eigen::Vector3d::Zero(); |
| 176 |
} |
| 177 |
|
| 178 |
virtual opengv::rotation_t getCamRotation( size_t index ) const { |
| 179 |
return opengv::rotation_t::Identity(); |
| 180 |
} |
| 181 |
|
| 182 |
virtual opengv::point_t getPoint( size_t index ) const { |
| 183 |
return pointFromArray(_points, index); |
| 184 |
} |
| 185 |
|
| 186 |
virtual size_t getNumberCorrespondences() const { |
| 187 |
return _bearingVectors.shape(0); |
| 188 |
} |
| 189 |
|
| 190 |
protected: |
| 191 |
pyarray_d _bearingVectors; |
| 192 |
pyarray_d _points; |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
py::object p2p( pyarray_d &v, pyarray_d &p, pyarray_d &R ) |
| 198 |
{ |
| 199 |
CentralAbsoluteAdapter adapter(v, p, R); |
| 200 |
return arrayFromTranslation( |
| 201 |
opengv::absolute_pose::p2p(adapter, 0, 1)); |
| 202 |
} |
| 203 |
|
| 204 |
py::object p3p_kneip( pyarray_d &v, pyarray_d &p ) |
| 205 |
{ |
| 206 |
CentralAbsoluteAdapter adapter(v, p); |
| 207 |
return listFromTransformations( |
| 208 |
opengv::absolute_pose::p3p_kneip(adapter, 0, 1, 2)); |
| 209 |
} |
| 210 |
|
| 211 |
py::object p3p_gao( pyarray_d &v, pyarray_d &p ) |
| 212 |
{ |
| 213 |
CentralAbsoluteAdapter adapter(v, p); |
| 214 |
return listFromTransformations( |
| 215 |
opengv::absolute_pose::p3p_gao(adapter, 0, 1, 2)); |
| 216 |
} |
| 217 |
|
| 218 |
py::object gp3p( pyarray_d &v, pyarray_d &p ) |
| 219 |
{ |
| 220 |
CentralAbsoluteAdapter adapter(v, p); |
| 221 |
return listFromTransformations( |
| 222 |
opengv::absolute_pose::gp3p(adapter, 0, 1, 2)); |
| 223 |
} |
| 224 |
|
| 225 |
py::object epnp( pyarray_d &v, pyarray_d &p ) |
| 226 |
{ |
| 227 |
CentralAbsoluteAdapter adapter(v, p); |
| 228 |
return arrayFromTransformation( |
| 229 |
opengv::absolute_pose::epnp(adapter)); |
| 230 |
} |
| 231 |
|
| 232 |
py::object gpnp( pyarray_d &v, pyarray_d &p ) |
| 233 |
{ |
| 234 |
CentralAbsoluteAdapter adapter(v, p); |
| 235 |
return arrayFromTransformation( |
| 236 |
opengv::absolute_pose::gpnp(adapter)); |
| 237 |
} |
| 238 |
|
| 239 |
py::object upnp( pyarray_d &v, pyarray_d &p ) |
| 240 |
{ |
| 241 |
CentralAbsoluteAdapter adapter(v, p); |
| 242 |
return listFromTransformations( |
| 243 |
opengv::absolute_pose::upnp(adapter)); |
| 244 |
} |
| 245 |
|
| 246 |
py::object optimize_nonlinear( pyarray_d &v, |
| 247 |
pyarray_d &p, |
| 248 |
pyarray_d &t, |
| 249 |
pyarray_d &R ) |
| 250 |
{ |
| 251 |
CentralAbsoluteAdapter adapter(v, p, t, R); |
| 252 |
return arrayFromTransformation( |
| 253 |
opengv::absolute_pose::optimize_nonlinear(adapter)); |
| 254 |
} |
| 255 |
|
| 256 |
py::object ransac( |
| 257 |
pyarray_d &v, |
| 258 |
pyarray_d &p, |
| 259 |
std::string algo_name, |
| 260 |
double threshold, |
| 261 |
int max_iterations, |
| 262 |
double probability ) |
| 263 |
{ |
| 264 |
using namespace opengv::sac_problems::absolute_pose; |
| 265 |
|
| 266 |
CentralAbsoluteAdapter adapter(v, p); |
| 267 |
|
| 268 |
// Create a ransac problem |
| 269 |
AbsolutePoseSacProblem::algorithm_t algorithm = AbsolutePoseSacProblem::KNEIP; |
| 270 |
if (algo_name == "TWOPT") algorithm = AbsolutePoseSacProblem::TWOPT; |
| 271 |
else if (algo_name == "KNEIP") algorithm = AbsolutePoseSacProblem::KNEIP; |
| 272 |
else if (algo_name == "GAO") algorithm = AbsolutePoseSacProblem::GAO; |
| 273 |
else if (algo_name == "EPNP") algorithm = AbsolutePoseSacProblem::EPNP; |
| 274 |
else if (algo_name == "GP3P") algorithm = AbsolutePoseSacProblem::GP3P; |
| 275 |
|
| 276 |
std::shared_ptr<AbsolutePoseSacProblem> |
| 277 |
absposeproblem_ptr( |
| 278 |
new AbsolutePoseSacProblem(adapter, algorithm)); |
| 279 |
|
| 280 |
// Create a ransac solver for the problem |
| 281 |
opengv::sac::Ransac<AbsolutePoseSacProblem> ransac; |
| 282 |
|
| 283 |
ransac.sac_model_ = absposeproblem_ptr; |
| 284 |
ransac.threshold_ = threshold; |
| 285 |
ransac.max_iterations_ = max_iterations; |
| 286 |
ransac.probability_ = probability; |
| 287 |
|
| 288 |
// Solve |
| 289 |
ransac.computeModel(); |
| 290 |
return arrayFromTransformation(ransac.model_coefficients_); |
| 291 |
} |
| 292 |
|
| 293 |
py::object lmeds( |
| 294 |
pyarray_d &v, |
| 295 |
pyarray_d &p, |
| 296 |
std::string algo_name, |
| 297 |
double threshold, |
| 298 |
int max_iterations, |
| 299 |
double probability ) |
| 300 |
{ |
| 301 |
using namespace opengv::sac_problems::absolute_pose; |
| 302 |
|
| 303 |
CentralAbsoluteAdapter adapter(v, p); |
| 304 |
|
| 305 |
// Create a lmeds problem |
| 306 |
AbsolutePoseSacProblem::algorithm_t algorithm = AbsolutePoseSacProblem::KNEIP; |
| 307 |
if (algo_name == "TWOPT") algorithm = AbsolutePoseSacProblem::TWOPT; |
| 308 |
else if (algo_name == "KNEIP") algorithm = AbsolutePoseSacProblem::KNEIP; |
| 309 |
else if (algo_name == "GAO") algorithm = AbsolutePoseSacProblem::GAO; |
| 310 |
else if (algo_name == "EPNP") algorithm = AbsolutePoseSacProblem::EPNP; |
| 311 |
else if (algo_name == "GP3P") algorithm = AbsolutePoseSacProblem::GP3P; |
| 312 |
|
| 313 |
std::shared_ptr<AbsolutePoseSacProblem> |
| 314 |
absposeproblem_ptr( |
| 315 |
new AbsolutePoseSacProblem(adapter, algorithm)); |
| 316 |
|
| 317 |
// Create a ransac solver for the problem |
| 318 |
opengv::sac::Lmeds<AbsolutePoseSacProblem> lmeds; |
| 319 |
|
| 320 |
lmeds.sac_model_ = absposeproblem_ptr; |
| 321 |
lmeds.threshold_ = threshold; |
| 322 |
lmeds.max_iterations_ = max_iterations; |
| 323 |
lmeds.probability_ = probability; |
| 324 |
|
| 325 |
// Solve |
| 326 |
lmeds.computeModel(); |
| 327 |
return arrayFromTransformation(lmeds.model_coefficients_); |
| 328 |
} |
| 329 |
|
| 330 |
} // namespace absolute_pose |
| 331 |
|
| 332 |
|
| 333 |
namespace relative_pose |
| 334 |
{ |
| 335 |
|
| 336 |
class CentralRelativeAdapter : public opengv::relative_pose::RelativeAdapterBase |
| 337 |
{ |
| 338 |
protected: |
| 339 |
using RelativeAdapterBase::_t12; |
| 340 |
using RelativeAdapterBase::_R12; |
| 341 |
|
| 342 |
public: |
| 343 |
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 344 |
|
| 345 |
CentralRelativeAdapter( |
| 346 |
pyarray_d &bearingVectors1, |
| 347 |
pyarray_d &bearingVectors2 ) |
| 348 |
: _bearingVectors1(bearingVectors1) |
| 349 |
, _bearingVectors2(bearingVectors2) |
| 350 |
{} |
| 351 |
|
| 352 |
CentralRelativeAdapter( |
| 353 |
pyarray_d &bearingVectors1, |
| 354 |
pyarray_d &bearingVectors2, |
| 355 |
pyarray_d &R12 ) |
| 356 |
: _bearingVectors1(bearingVectors1) |
| 357 |
, _bearingVectors2(bearingVectors2) |
| 358 |
{ |
| 359 |
for (int i = 0; i < 3; ++i) { |
| 360 |
for (int j = 0; j < 3; ++j) { |
| 361 |
_R12(i, j) = *R12.data(i, j); |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
CentralRelativeAdapter( |
| 367 |
pyarray_d &bearingVectors1, |
| 368 |
pyarray_d &bearingVectors2, |
| 369 |
pyarray_d &t12, |
| 370 |
pyarray_d &R12 ) |
| 371 |
: _bearingVectors1(bearingVectors1) |
| 372 |
, _bearingVectors2(bearingVectors2) |
| 373 |
{ |
| 374 |
for (int i = 0; i < 3; ++i) { |
| 375 |
_t12(i) = *t12.data(i); |
| 376 |
} |
| 377 |
for (int i = 0; i < 3; ++i) { |
| 378 |
for (int j = 0; j < 3; ++j) { |
| 379 |
_R12(i, j) = *R12.data(i, j); |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
virtual ~CentralRelativeAdapter() {} |
| 385 |
|
| 386 |
virtual opengv::bearingVector_t getBearingVector1( size_t index ) const { |
| 387 |
return bearingVectorFromArray(_bearingVectors1, index); |
| 388 |
} |
| 389 |
|
| 390 |
virtual opengv::bearingVector_t getBearingVector2( size_t index ) const { |
| 391 |
return bearingVectorFromArray(_bearingVectors2, index); |
| 392 |
} |
| 393 |
|
| 394 |
virtual double getWeight( size_t index ) const { |
| 395 |
return 1.0; |
| 396 |
} |
| 397 |
|
| 398 |
virtual opengv::translation_t getCamOffset1( size_t index ) const { |
| 399 |
return Eigen::Vector3d::Zero(); |
| 400 |
} |
| 401 |
|
| 402 |
virtual opengv::rotation_t getCamRotation1( size_t index ) const { |
| 403 |
return opengv::rotation_t::Identity(); |
| 404 |
} |
| 405 |
|
| 406 |
virtual opengv::translation_t getCamOffset2( size_t index ) const { |
| 407 |
return Eigen::Vector3d::Zero(); |
| 408 |
} |
| 409 |
|
| 410 |
virtual opengv::rotation_t getCamRotation2( size_t index ) const { |
| 411 |
return opengv::rotation_t::Identity(); |
| 412 |
} |
| 413 |
|
| 414 |
virtual size_t getNumberCorrespondences() const { |
| 415 |
return _bearingVectors1.shape(0); |
| 416 |
} |
| 417 |
|
| 418 |
protected: |
| 419 |
pyarray_d _bearingVectors1; |
| 420 |
pyarray_d _bearingVectors2; |
| 421 |
}; |
| 422 |
|
| 423 |
|
| 424 |
py::object twopt( pyarray_d &b1, pyarray_d &b2, pyarray_d &R ) |
| 425 |
{ |
| 426 |
CentralRelativeAdapter adapter(b1, b2, R); |
| 427 |
return arrayFromTranslation( |
| 428 |
opengv::relative_pose::twopt(adapter, true, 0, 1)); |
| 429 |
} |
| 430 |
|
| 431 |
py::object twopt_rotationOnly( pyarray_d &b1, pyarray_d &b2 ) |
| 432 |
{ |
| 433 |
CentralRelativeAdapter adapter(b1, b2); |
| 434 |
return arrayFromRotation( |
| 435 |
opengv::relative_pose::twopt_rotationOnly(adapter, 0, 1)); |
| 436 |
} |
| 437 |
|
| 438 |
py::object rotationOnly( pyarray_d &b1, pyarray_d &b2 ) |
| 439 |
{ |
| 440 |
CentralRelativeAdapter adapter(b1, b2); |
| 441 |
return arrayFromRotation( |
| 442 |
opengv::relative_pose::rotationOnly(adapter)); |
| 443 |
} |
| 444 |
|
| 445 |
py::object fivept_nister( pyarray_d &b1, pyarray_d &b2 ) |
| 446 |
{ |
| 447 |
CentralRelativeAdapter adapter(b1, b2); |
| 448 |
return listFromEssentials( |
| 449 |
opengv::relative_pose::fivept_nister(adapter)); |
| 450 |
} |
| 451 |
|
| 452 |
py::object fivept_kneip( pyarray_d &b1, pyarray_d &b2 ) |
| 453 |
{ |
| 454 |
CentralRelativeAdapter adapter(b1, b2); |
| 455 |
return listFromRotations( |
| 456 |
opengv::relative_pose::fivept_kneip(adapter, getNindices(5))); |
| 457 |
} |
| 458 |
|
| 459 |
py::object sevenpt( pyarray_d &b1, pyarray_d &b2 ) |
| 460 |
{ |
| 461 |
CentralRelativeAdapter adapter(b1, b2); |
| 462 |
return listFromEssentials( |
| 463 |
opengv::relative_pose::sevenpt(adapter)); |
| 464 |
} |
| 465 |
|
| 466 |
py::object eightpt( pyarray_d &b1, pyarray_d &b2 ) |
| 467 |
{ |
| 468 |
CentralRelativeAdapter adapter(b1, b2); |
| 469 |
return arrayFromEssential( |
| 470 |
opengv::relative_pose::eightpt(adapter)); |
| 471 |
} |
| 472 |
|
| 473 |
py::object eigensolver( pyarray_d &b1, pyarray_d &b2, pyarray_d &R ) |
| 474 |
{ |
| 475 |
CentralRelativeAdapter adapter(b1, b2, R); |
| 476 |
return arrayFromRotation( |
| 477 |
opengv::relative_pose::eigensolver(adapter)); |
| 478 |
} |
| 479 |
|
| 480 |
py::object sixpt( pyarray_d &b1, pyarray_d &b2 ) |
| 481 |
{ |
| 482 |
CentralRelativeAdapter adapter(b1, b2); |
| 483 |
return listFromRotations( |
| 484 |
opengv::relative_pose::sixpt(adapter)); |
| 485 |
} |
| 486 |
|
| 487 |
py::object optimize_nonlinear( pyarray_d &b1, |
| 488 |
pyarray_d &b2, |
| 489 |
pyarray_d &t12, |
| 490 |
pyarray_d &R12 ) |
| 491 |
{ |
| 492 |
CentralRelativeAdapter adapter(b1, b2, t12, R12); |
| 493 |
return arrayFromTransformation( |
| 494 |
opengv::relative_pose::optimize_nonlinear(adapter)); |
| 495 |
} |
| 496 |
|
| 497 |
py::object ransac( |
| 498 |
pyarray_d &b1, |
| 499 |
pyarray_d &b2, |
| 500 |
std::string algo_name, |
| 501 |
double threshold, |
| 502 |
int max_iterations, |
| 503 |
double probability ) |
| 504 |
{ |
| 505 |
using namespace opengv::sac_problems::relative_pose; |
| 506 |
|
| 507 |
CentralRelativeAdapter adapter(b1, b2); |
| 508 |
|
| 509 |
// Create a ransac problem |
| 510 |
CentralRelativePoseSacProblem::algorithm_t algorithm = CentralRelativePoseSacProblem::NISTER; |
| 511 |
if (algo_name == "STEWENIUS") algorithm = CentralRelativePoseSacProblem::STEWENIUS; |
| 512 |
else if (algo_name == "NISTER") algorithm = CentralRelativePoseSacProblem::NISTER; |
| 513 |
else if (algo_name == "SEVENPT") algorithm = CentralRelativePoseSacProblem::SEVENPT; |
| 514 |
else if (algo_name == "EIGHTPT") algorithm = CentralRelativePoseSacProblem::EIGHTPT; |
| 515 |
|
| 516 |
std::shared_ptr<CentralRelativePoseSacProblem> |
| 517 |
relposeproblem_ptr( |
| 518 |
new CentralRelativePoseSacProblem(adapter, algorithm)); |
| 519 |
|
| 520 |
// Create a ransac solver for the problem |
| 521 |
opengv::sac::Ransac<CentralRelativePoseSacProblem> ransac; |
| 522 |
|
| 523 |
ransac.sac_model_ = relposeproblem_ptr; |
| 524 |
ransac.threshold_ = threshold; |
| 525 |
ransac.max_iterations_ = max_iterations; |
| 526 |
ransac.probability_ = probability; |
| 527 |
|
| 528 |
// Solve |
| 529 |
ransac.computeModel(); |
| 530 |
return arrayFromTransformation(ransac.model_coefficients_); |
| 531 |
} |
| 532 |
|
| 533 |
py::object lmeds( |
| 534 |
pyarray_d &b1, |
| 535 |
pyarray_d &b2, |
| 536 |
std::string algo_name, |
| 537 |
double threshold, |
| 538 |
int max_iterations, |
| 539 |
double probability ) |
| 540 |
{ |
| 541 |
using namespace opengv::sac_problems::relative_pose; |
| 542 |
|
| 543 |
CentralRelativeAdapter adapter(b1, b2); |
| 544 |
|
| 545 |
// Create a lmeds problem |
| 546 |
CentralRelativePoseSacProblem::algorithm_t algorithm = CentralRelativePoseSacProblem::NISTER; |
| 547 |
if (algo_name == "STEWENIUS") algorithm = CentralRelativePoseSacProblem::STEWENIUS; |
| 548 |
else if (algo_name == "NISTER") algorithm = CentralRelativePoseSacProblem::NISTER; |
| 549 |
else if (algo_name == "SEVENPT") algorithm = CentralRelativePoseSacProblem::SEVENPT; |
| 550 |
else if (algo_name == "EIGHTPT") algorithm = CentralRelativePoseSacProblem::EIGHTPT; |
| 551 |
|
| 552 |
std::shared_ptr<CentralRelativePoseSacProblem> |
| 553 |
relposeproblem_ptr( |
| 554 |
new CentralRelativePoseSacProblem(adapter, algorithm)); |
| 555 |
|
| 556 |
// Create a lmeds solver for the problem |
| 557 |
opengv::sac::Lmeds<CentralRelativePoseSacProblem> lmeds; |
| 558 |
|
| 559 |
lmeds.sac_model_ = relposeproblem_ptr; |
| 560 |
lmeds.threshold_ = threshold; |
| 561 |
lmeds.max_iterations_ = max_iterations; |
| 562 |
lmeds.probability_ = probability; |
| 563 |
|
| 564 |
// Solve |
| 565 |
lmeds.computeModel(); |
| 566 |
return arrayFromTransformation(lmeds.model_coefficients_); |
| 567 |
} |
| 568 |
|
| 569 |
py::object ransac_rotationOnly( |
| 570 |
pyarray_d &b1, |
| 571 |
pyarray_d &b2, |
| 572 |
double threshold, |
| 573 |
int max_iterations, |
| 574 |
double probability ) |
| 575 |
{ |
| 576 |
using namespace opengv::sac_problems::relative_pose; |
| 577 |
|
| 578 |
CentralRelativeAdapter adapter(b1, b2); |
| 579 |
|
| 580 |
std::shared_ptr<RotationOnlySacProblem> |
| 581 |
relposeproblem_ptr( |
| 582 |
new RotationOnlySacProblem(adapter)); |
| 583 |
|
| 584 |
// Create a ransac solver for the problem |
| 585 |
opengv::sac::Ransac<RotationOnlySacProblem> ransac; |
| 586 |
|
| 587 |
ransac.sac_model_ = relposeproblem_ptr; |
| 588 |
ransac.threshold_ = threshold; |
| 589 |
ransac.max_iterations_ = max_iterations; |
| 590 |
ransac.probability_ = probability; |
| 591 |
|
| 592 |
// Solve |
| 593 |
ransac.computeModel(); |
| 594 |
return arrayFromRotation(ransac.model_coefficients_); |
| 595 |
} |
| 596 |
|
| 597 |
py::object lmeds_rotationOnly( |
| 598 |
pyarray_d &b1, |
| 599 |
pyarray_d &b2, |
| 600 |
double threshold, |
| 601 |
int max_iterations, |
| 602 |
double probability ) |
| 603 |
{ |
| 604 |
using namespace opengv::sac_problems::relative_pose; |
| 605 |
|
| 606 |
CentralRelativeAdapter adapter(b1, b2); |
| 607 |
|
| 608 |
std::shared_ptr<RotationOnlySacProblem> |
| 609 |
relposeproblem_ptr( |
| 610 |
new RotationOnlySacProblem(adapter)); |
| 611 |
|
| 612 |
// Create a lmeds solver for the problem |
| 613 |
opengv::sac::Lmeds<RotationOnlySacProblem> lmeds; |
| 614 |
|
| 615 |
lmeds.sac_model_ = relposeproblem_ptr; |
| 616 |
lmeds.threshold_ = threshold; |
| 617 |
lmeds.max_iterations_ = max_iterations; |
| 618 |
lmeds.probability_ = probability; |
| 619 |
|
| 620 |
// Solve |
| 621 |
lmeds.computeModel(); |
| 622 |
return arrayFromRotation(lmeds.model_coefficients_); |
| 623 |
} |
| 624 |
|
| 625 |
} // namespace relative_pose |
| 626 |
|
| 627 |
namespace triangulation |
| 628 |
{ |
| 629 |
|
| 630 |
py::object triangulate( pyarray_d &b1, |
| 631 |
pyarray_d &b2, |
| 632 |
pyarray_d &t12, |
| 633 |
pyarray_d &R12 ) |
| 634 |
{ |
| 635 |
pyopengv::relative_pose::CentralRelativeAdapter adapter(b1, b2, t12, R12); |
| 636 |
|
| 637 |
opengv::points_t points; |
| 638 |
for (size_t i = 0; i < adapter.getNumberCorrespondences(); ++i) |
| 639 |
{ |
| 640 |
opengv::point_t p = opengv::triangulation::triangulate(adapter, i); |
| 641 |
points.push_back(p); |
| 642 |
} |
| 643 |
return arrayFromPoints(points); |
| 644 |
} |
| 645 |
|
| 646 |
py::object triangulate2( pyarray_d &b1, |
| 647 |
pyarray_d &b2, |
| 648 |
pyarray_d &t12, |
| 649 |
pyarray_d &R12 ) |
| 650 |
{ |
| 651 |
pyopengv::relative_pose::CentralRelativeAdapter adapter(b1, b2, t12, R12); |
| 652 |
|
| 653 |
opengv::points_t points; |
| 654 |
for (size_t i = 0; i < adapter.getNumberCorrespondences(); ++i) |
| 655 |
{ |
| 656 |
opengv::point_t p = opengv::triangulation::triangulate2(adapter, i); |
| 657 |
points.push_back(p); |
| 658 |
} |
| 659 |
return arrayFromPoints(points); |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
} // namespace triangulation |
| 664 |
|
| 665 |
} // namespace pyopengv |
| 666 |
|
| 667 |
|
| 668 |
PYBIND11_MODULE(pyopengv, m) { |
| 669 |
namespace py = pybind11; |
| 670 |
|
| 671 |
m.def("absolute_pose_p2p", pyopengv::absolute_pose::p2p); |
| 672 |
m.def("absolute_pose_p3p_kneip", pyopengv::absolute_pose::p3p_kneip); |
| 673 |
m.def("absolute_pose_p3p_gao", pyopengv::absolute_pose::p3p_gao); |
| 674 |
m.def("absolute_pose_gp3p", pyopengv::absolute_pose::gp3p); |
| 675 |
m.def("absolute_pose_epnp", pyopengv::absolute_pose::epnp); |
| 676 |
m.def("absolute_pose_gpnp", pyopengv::absolute_pose::gpnp); |
| 677 |
m.def("absolute_pose_upnp", pyopengv::absolute_pose::upnp); |
| 678 |
m.def("absolute_pose_optimize_nonlinear", pyopengv::absolute_pose::optimize_nonlinear); |
| 679 |
m.def("absolute_pose_ransac", pyopengv::absolute_pose::ransac, |
| 680 |
py::arg("v"), |
| 681 |
py::arg("p"), |
| 682 |
py::arg("algo_name"), |
| 683 |
py::arg("threshold"), |
| 684 |
py::arg("iterations") = 1000, |
| 685 |
py::arg("probability") = 0.99 |
| 686 |
); |
| 687 |
m.def("absolute_pose_lmeds", pyopengv::absolute_pose::lmeds, |
| 688 |
py::arg("v"), |
| 689 |
py::arg("p"), |
| 690 |
py::arg("algo_name"), |
| 691 |
py::arg("threshold"), |
| 692 |
py::arg("iterations") = 1000, |
| 693 |
py::arg("probability") = 0.99 |
| 694 |
); |
| 695 |
|
| 696 |
m.def("relative_pose_twopt", pyopengv::relative_pose::twopt); |
| 697 |
m.def("relative_pose_twopt_rotation_only", pyopengv::relative_pose::twopt_rotationOnly); |
| 698 |
m.def("relative_pose_rotation_only", pyopengv::relative_pose::rotationOnly); |
| 699 |
m.def("relative_pose_fivept_nister", pyopengv::relative_pose::fivept_nister); |
| 700 |
m.def("relative_pose_fivept_kneip", pyopengv::relative_pose::fivept_kneip); |
| 701 |
m.def("relative_pose_sevenpt", pyopengv::relative_pose::sevenpt); |
| 702 |
m.def("relative_pose_eightpt", pyopengv::relative_pose::eightpt); |
| 703 |
m.def("relative_pose_eigensolver", pyopengv::relative_pose::eigensolver); |
| 704 |
m.def("relative_pose_sixpt", pyopengv::relative_pose::sixpt); |
| 705 |
m.def("relative_pose_optimize_nonlinear", pyopengv::relative_pose::optimize_nonlinear); |
| 706 |
m.def("relative_pose_ransac", pyopengv::relative_pose::ransac, |
| 707 |
py::arg("b1"), |
| 708 |
py::arg("b2"), |
| 709 |
py::arg("algo_name"), |
| 710 |
py::arg("threshold"), |
| 711 |
py::arg("iterations") = 1000, |
| 712 |
py::arg("probability") = 0.99 |
| 713 |
); |
| 714 |
m.def("relative_pose_lmeds", pyopengv::relative_pose::lmeds, |
| 715 |
py::arg("b1"), |
| 716 |
py::arg("b2"), |
| 717 |
py::arg("algo_name"), |
| 718 |
py::arg("threshold"), |
| 719 |
py::arg("iterations") = 1000, |
| 720 |
py::arg("probability") = 0.99 |
| 721 |
); |
| 722 |
m.def("relative_pose_ransac_rotation_only", pyopengv::relative_pose::ransac_rotationOnly, |
| 723 |
py::arg("b1"), |
| 724 |
py::arg("b2"), |
| 725 |
py::arg("threshold"), |
| 726 |
py::arg("iterations") = 1000, |
| 727 |
py::arg("probability") = 0.99 |
| 728 |
); |
| 729 |
m.def("relative_pose_lmeds_rotation_only", pyopengv::relative_pose::lmeds_rotationOnly, |
| 730 |
py::arg("b1"), |
| 731 |
py::arg("b2"), |
| 732 |
py::arg("threshold"), |
| 733 |
py::arg("iterations") = 1000, |
| 734 |
py::arg("probability") = 0.99 |
| 735 |
); |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
m.def("triangulation_triangulate", pyopengv::triangulation::triangulate); |
| 740 |
m.def("triangulation_triangulate2", pyopengv::triangulation::triangulate2); |
| 741 |
} |