Skip to content

Instantly share code, notes, and snippets.

@sloriot
Last active November 29, 2019 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloriot/066c335168b4120484e7c000b4aae555 to your computer and use it in GitHub Desktop.
Save sloriot/066c335168b4120484e7c000b4aae555 to your computer and use it in GitHub Desktop.
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/AABB_primitive.h>
#include <boost/iterator/counting_iterator.hpp>
typedef CGAL::Simple_cartesian<double> K;
typedef K::FT FT;
typedef K::Ray_3 Ray;
typedef K::Line_3 Line;
typedef K::Point_3 Point;
typedef K::Triangle_3 Triangle;
template <class Kernel>
struct MyBox
{
std::array<K::Point_3,2> b;
typename Kernel::Iso_cuboid_3
to_cub() const
{
return typename Kernel::Iso_cuboid_3(b[0],b[1]);
}
};
// property maps for the primitive
template <class Kernel>
struct Datum_map
{
typedef boost::readable_property_map_tag category;
typedef std::size_t key_type;
typedef typename Kernel::Iso_cuboid_3 value_type;
typedef value_type reference;
const std::vector<MyBox<Kernel> >* boxes_ptr;
Datum_map() : boxes_ptr(nullptr) {}
Datum_map(const std::vector<MyBox<Kernel> >& boxes) : boxes_ptr(&boxes) {}
friend value_type get(const Datum_map& m, key_type k)
{
return (*m.boxes_ptr)[k].to_cub();
}
};
template <class Kernel>
struct Point_map
{
typedef boost::readable_property_map_tag category;
typedef std::size_t key_type;
typedef typename Kernel::Point_3 value_type;
typedef value_type reference;
const std::vector<MyBox<Kernel> >* boxes_ptr;
Point_map() : boxes_ptr(nullptr) {}
Point_map(const std::vector<MyBox<Kernel> >& boxes) : boxes_ptr(&boxes) {}
friend value_type get(const Point_map& m, key_type k)
{
return (*m.boxes_ptr)[k].b[0];
}
};
typedef CGAL::AABB_primitive<std::size_t, Datum_map<K>, Point_map<K>, CGAL::Tag_true /*UseSharedData*/, CGAL::Tag_false /*CacheDatum*/> Primitive;
typedef CGAL::AABB_traits<K, Primitive> AABB_traits;
typedef CGAL::AABB_tree<AABB_traits> Tree;
int main()
{
std::vector< MyBox<K> > boxes;
Datum_map<K> datum_map(boxes);
Point_map<K> point_map(boxes);
// constructs AABB tree
Tree tree(boost::counting_iterator<std::size_t>(0),
boost::counting_iterator<std::size_t>(boxes.size()),
datum_map,
point_map);
// counts #intersections
K::Ray_3 ray_query;
tree.do_intersect(ray_query);
std::vector<Primitive> intersected_primitives;
tree.all_intersected_primitives(ray_query, std::back_inserter(intersected_primitives));
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment