Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to use with pcl::PointXYZI #33

Closed
DingFeng9313 opened this issue Sep 7, 2023 · 3 comments
Closed

trying to use with pcl::PointXYZI #33

DingFeng9313 opened this issue Sep 7, 2023 · 3 comments
Assignees

Comments

@DingFeng9313
Copy link

Hi, I am trying to use pico_tree with pcl::pointXYZI

looks like I should use both custom spacetraits and custom point. My custom SpaceTraits are as follows:

template <>
struct PointTraits<pcl::PointXYZI> {
  using PointType = pcl::PointXYZI;
  using ScalarType = float;
  // Spatial dimension. Set to pico_tree::kDynamicSize when the dimension is
  // only known at run-time.
  static std::size_t constexpr Dim = 3;

  // Returns a pointer to the coordinates of the input point.
  inline static float const* data(pcl::PointXYZI const& point) { return point.data; }

  // Returns the number of coordinates or spatial dimension of each point.
  inline static constexpr std::size_t size(pcl::PointXYZI const&) { return Dim; }
};

// Provides an interface for an std::deque<std::array>.
template <typename Scalar_, std::size_t Dim_>
struct SpaceTraits<std::deque<pcl::PointXYZI>> {
  using SpaceType = std::deque<pcl::PointXYZI>;
  using PointType = pcl::PointXYZI;
  using ScalarType = Scalar_;
  // Spatial dimension. Set to pico_tree::kDynamicSize when the dimension is
  // only known at run-time.
  static std::size_t constexpr Dim = Dim_;

  // Returns a point from the input space at the specified index.
  template <typename Index_>
  inline static PointType const& PointAt(
      SpaceType const& space, Index_ const index) {
    return space[static_cast<std::size_t>(index)];
  }

  // Returns number of points contained by the space.
  inline static std::size_t size(SpaceType const& space) {
    return space.size();
  }

  // Returns the number of coordinates or spatial dimension of each point.
  inline static constexpr std::size_t sdim(SpaceType const&) { return Dim; }
};

But it reports

 error: template parameters not deducible in partial specialization:
 struct SpaceTraits<std::deque<pcl::PointXYZI>>

Is it possible to use pico_tree this way? How can I use it with pcl library?

@Jaybro
Copy link
Owner

Jaybro commented Sep 7, 2023

You can definitely use pico_tree with PCL. At first glance your custom PointTraits are fine, except that I don't see a namespace:

namespace pico_tree {

template <>
struct PointTraits<pcl::PointXYZI> {
  ...
};

}  // namespace pico_tree

However, your custom SpaceTraits needs a few changes:

namespace pico_tree {

// pcl::PointXYZI doesn't require template arguments, so we leave the template
// parameters for SpaceTraits empty. This results in a full template
// specialization for std::deque<pcl::PointXYZI>.
template <>
struct SpaceTraits<std::deque<pcl::PointXYZI>> {
  ...
  using ScalarType = float;
  static std::size_t constexpr Dim = 3;
  ...
};

}  // namespace pico_tree

@Jaybro
Copy link
Owner

Jaybro commented Sep 7, 2023

Note that I would recommend using an std::vector over an std::deque because it would be faster. All you have to do is #include <pico_tree/vector_traits.hpp> and add your custom PointTraits<pcl::PointXYZI>.

If you really want to work with std::deque then here is a generic SpaceTraits<std::deque<Point_>> that will work for any point type Point_ supported by a PointTraits<Point_>:

namespace pico_tree {

// Provides an interface for an std::deque<Point_>.
template <typename Point_>
struct SpaceTraits<std::deque<Point_>> {
  using SpaceType = std::deque<Point_>;
  using PointType = Point_;
  using ScalarType = typename PointTraits<Point_>::ScalarType;
  static std::size_t constexpr Dim = PointTraits<Point_>::Dim;

  // It is not reliably possible to get the spatial dimension of an std::deque.
  // I.e., when the point set is empty.
  static_assert(
      Dim != kDynamicSize, "DEQUE_OF_POINT_DOES_NOT_SUPPORT_DYNAMIC_DIM");

  // Returns a point from the input space at the specified index.
  template <typename Index_>
  inline static PointType const& PointAt(
      SpaceType const& space, Index_ const index) {
    return space[static_cast<std::size_t>(index)];
  }

  // Returns number of points contained by the space.
  inline static std::size_t size(SpaceType const& space) {
    return space.size();
  }

  // Returns the number of coordinates or spatial dimension of each point.
  inline static constexpr std::size_t sdim(SpaceType const&) { return Dim; }
};

}  // namespace pico_tree

@DingFeng9313
Copy link
Author

Thanks a lot. That solved my problem. I copied code from example files "kd_tree_custom_space_type.cpp", that is why it is std::deque. Initially I want to use std::vector.

Thanks again.

@Jaybro Jaybro self-assigned this Sep 7, 2023
@Jaybro Jaybro closed this as completed Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants