#include <iostream>
#include <boost/iterator.hpp>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
typedef size_t* Point_index_iterator;
class Triangle_iterator
: public boost::iterator_adaptor<
Triangle_iterator
, Point_index_iterator
, boost::use_default
, boost::forward_traversal_tag
>
{
public:
Triangle_iterator()
: Triangle_iterator::iterator_adaptor_() {}
explicit Triangle_iterator(Point_index_iterator p)
: Triangle_iterator::iterator_adaptor_(p) {}
private:
friend class boost::iterator_core_access;
void increment() { this->base_reference() += 3; }
};
struct My_triangle_primitive {
public:
typedef Triangle_iterator Id;
typedef K::Point_3 Point;
typedef K::Triangle_3 Datum;
static const double* point_container;
private:
Id m_it;
public:
My_triangle_primitive() {}
My_triangle_primitive(Triangle_iterator a)
: m_it(a) {}
Id id() const { return m_it; }
Datum datum() const
{
Point_index_iterator p_it = m_it.base();
Point p(*(point_container + 3 * (*p_it)),
*(point_container + 3 * (*p_it) + 1),
*(point_container + 3 * (*p_it) + 2) );
++p_it;
Point q(*(point_container + 3 * (*p_it)),
*(point_container + 3 * (*p_it) + 1),
*(point_container + 3 * (*p_it) + 2));
++p_it;
Point r(*(point_container + 3 * (*p_it)),
*(point_container + 3 * (*p_it) + 1),
*(point_container + 3 * (*p_it) + 2));
return Datum(p, q, r);
}
Point reference_point() const
{
return Point(*(point_container + 3 * (*m_it)),
*(point_container + 3 * (*m_it) + 1),
*(point_container + 3 * (*m_it) + 2));
}
};
const double* My_triangle_primitive::point_container = 0;
int main()
{
double points[12];
My_triangle_primitive::point_container = points;
points[0] = 1.0; points[1] = 0.0; points[2] = 0.0;
points[3] = 0.0; points[4] = 1.0; points[5] = 0.0;
points[6] = 0.0; points[7] = 0.0; points[8] = 1.0;
points[9] = 0.0; points[10] = 0.0; points[11] = 0.0;
size_t triangles[9];
triangles[0] = 0; triangles[1] = 1; triangles[2] = 2;
triangles[3] = 0; triangles[4] = 1; triangles[5] = 3;
triangles[6] = 0; triangles[7] = 3; triangles[8] = 2;
Tree tree(Triangle_iterator(triangles),
Triangle_iterator(triangles+9));
K::Ray_3 ray_query(K::Point_3(0.2, 0.2, 0.2), K::Point_3(0.0, 1.0, 0.0));
std::cout << tree.number_of_intersected_primitives(ray_query)
<< " intersections(s) with ray query" << std::endl;
K::Point_3 point_query(2.0, 2.0, 2.0);
K::Point_3 closest_point = tree.closest_point(point_query);
std::cout << "closest point to " << point_query << " is: " << closest_point.x() << " " << closest_point.y() << " " << closest_point.z() << std::endl;
return EXIT_SUCCESS;
}