#pragma once #include "colmap/util/hash_containers.h" #include "colmap/util/types.h" #include "pycolmap/feature/opaque_types.h" #include #include #include #include #include #include #include #include #include #include #include namespace pycolmap { // Wrapper for Rotation3d that can hold either owned data or a view into // existing memory (e.g., Rigid3d.params). This enables zero-copy access // to rotation data while providing the full quaternion interface. struct Rotation3dWrapper { pybind11::array_t data; // Default constructor - creates owned identity quaternion Rotation3dWrapper() : data(4) { map() = Eigen::Quaterniond::Identity(); } // Copy from Eigen::Quaterniond - creates owned data explicit Rotation3dWrapper(const Eigen::Quaterniond& q) : data(4) { map() = q; } // From xyzw coefficients explicit Rotation3dWrapper(const Eigen::Vector4d& xyzw) : data(4) { map().coeffs() = xyzw; } // From rotation matrix explicit Rotation3dWrapper(const Eigen::Matrix3d& matrix) : data(4) { map() = Eigen::Quaterniond(matrix); } // From axis-angle vector explicit Rotation3dWrapper(const Eigen::Vector3d& axis_angle) : data(4) { map() = Eigen::Quaterniond( Eigen::AngleAxisd(axis_angle.norm(), axis_angle.normalized())); } // View constructor - borrows existing array (zero-copy) explicit Rotation3dWrapper(pybind11::array_t arr) : data(std::move(arr)) {} Eigen::Map map() { return Eigen::Map(data.mutable_data()); } Eigen::Map map() const { return Eigen::Map(data.data()); } }; } // namespace pycolmap namespace PYBIND11_NAMESPACE { namespace detail { // Type caster to allow functions taking Eigen::Quaterniond to accept // Rotation3dWrapper, and functions returning Eigen::Quaterniond to return // Rotation3dWrapper. template <> struct type_caster { PYBIND11_TYPE_CASTER(Eigen::Quaterniond, const_name("Rotation3d")); // Python -> C++ (when calling functions that take Eigen::Quaterniond) bool load(handle src, bool) { // Try to load from Rotation3dWrapper if (isinstance(src)) { value = src.cast().map(); return true; } // Also accept raw numpy arrays try { auto arr = src.cast>(); if (arr.size() == 4) { value = Eigen::Map(arr.data()); return true; } } catch (...) { } return false; } // C++ -> Python (when returning Eigen::Quaterniond) static handle cast(const Eigen::Quaterniond& src, return_value_policy, handle) { pycolmap::Rotation3dWrapper wrapper(src); return pybind11::cast(std::move(wrapper)).release(); } }; // Bind COLMAP's backport implementation of std::span. This copies the content // into a list. We could instead create a view with an Eigen::Map but the cast // should be explicit and cannot be automatic - likely not worth the added // logic. template struct type_caster> : list_caster, Type> { }; // Autocast from numpy.ndarray to std::vector template struct type_caster>> { public: using MatrixType = typename Eigen::Matrix; using VectorType = typename Eigen::Matrix; using props = EigenProps; PYBIND11_TYPE_CASTER(std::vector, props::descriptor); bool load(handle src, bool) { const auto buf = array::ensure(src); if (!buf) { return false; } const buffer_info info = buf.request(); if (info.ndim != 2 || info.shape[1] != Size) { return false; } const size_t num_elements = info.shape[0]; value.resize(num_elements); const auto& mat = src.cast>(); Eigen::Map( reinterpret_cast(value.data()), num_elements, Size) = mat; return true; } static handle cast(const std::vector& vec, return_value_policy /* policy */, handle h) { Eigen::Map mat( reinterpret_cast(vec.data()), vec.size(), Size); return type_caster>::cast( mat, return_value_policy::copy, h); } }; // pybind11's built-in STL casters only recognize std:: containers, so provide // casters for the colmap flat hash aliases used across the bindings (e.g. // ObservationManager::ImagePairs, IncrementalMapper::FilteredFrames, // BundleAdjustmentConfig::VariablePoints, the FilterPoints3D* parameters). Only // needed for the BOOST backend; for STD the aliases are std:: types pybind11 // already handles. Flat containers are never PYBIND11_MAKE_OPAQUE, so these // generic casters cannot collide with opaque bound types. (The NodeHashMap // caster lives in pycolmap/scene/types.h next to the opaque element-store // maps.) #if defined(COLMAP_HASH_BOOST) template struct type_caster> : map_caster, Key, Value> {}; template struct type_caster> : set_caster, Key> {}; #endif } // namespace detail template class classh_ext : public classh { public: using Parent = classh; using Parent::Parent; // inherit constructors using type = type_; template classh_ext& def_readwrite(const char* name, D C::* pm, const Extra&... extra) { static_assert( std::is_same::value || std::is_base_of::value, "def_readwrite() requires a class member (or base class member)"); cpp_function fget([pm](type& c) -> D& { return c.*pm; }, is_method(*this)), fset([pm](type& c, const D& value) { c.*pm = value; }, is_method(*this)); this->def_property( name, fget, fset, return_value_policy::reference_internal, extra...); return *this; } template classh_ext& def(Args&&... args) { Parent::def(std::forward(args)...); return *this; } template classh_ext& def_property(Args&&... args) { Parent::def_property(std::forward(args)...); return *this; } }; } // namespace PYBIND11_NAMESPACE