"Fossies" - the Fresh Open Source Software Archive

Member "CGAL-5.0/examples/AABB_tree/AABB_custom_example.cpp" (8 Nov 2019, 3455 Bytes) of package /linux/misc/CGAL-5.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Author(s): Camille Wormser, Pierre Alliez
    2 // An example of an AABB tree constructed with custom point and triangle types.
    3 
    4 #include <iostream>
    5 #include <list>
    6 #include <CGAL/Simple_cartesian.h>
    7 #include <CGAL/AABB_tree.h>
    8 #include <CGAL/AABB_traits.h>
    9 
   10 
   11 
   12 typedef CGAL::Simple_cartesian<double> K;
   13 
   14 
   15 // custom point type
   16 struct My_point {
   17     double m_x;
   18     double m_y;
   19     double m_z;
   20 
   21     My_point(const double x,
   22         const double y,
   23         const double z)
   24         : m_x(x), m_y(y), m_z(z) {}
   25 };
   26 
   27 // custom triangle type with
   28 // three pointers to points
   29 struct My_triangle {
   30     My_point *m_pa;
   31     My_point *m_pb;
   32     My_point *m_pc;
   33 
   34     My_triangle(My_point *pa,
   35         My_point *pb,
   36         My_point *pc)
   37         : m_pa(pa), m_pb(pb), m_pc(pc) {}
   38 };
   39 
   40 // the custom triangles are stored into a vector
   41 typedef std::vector<My_triangle>::const_iterator Iterator;
   42 
   43 // The following primitive provides the conversion facilities between
   44 // the custom triangle and point types and the CGAL ones
   45 struct My_triangle_primitive {
   46 public:
   47 
   48     // this is the type of data that the queries returns. For this example
   49     // we imagine that, for some reasons, we do not want to store the iterators
   50     // of the vector, but raw pointers. This is to show that the Id type
   51     // does not have to be the same as the one of the input parameter of the 
   52     // constructor.
   53     typedef const My_triangle* Id;
   54 
   55     // CGAL types returned
   56     typedef K::Point_3    Point; // CGAL 3D point type
   57     typedef K::Triangle_3 Datum; // CGAL 3D triangle type
   58 
   59 private:
   60     Id m_pt; // this is what the AABB tree stores internally
   61 
   62 public:
   63     My_triangle_primitive() {} // default constructor needed
   64 
   65     // the following constructor is the one that receives the iterators from the 
   66     // iterator range given as input to the AABB_tree
   67     My_triangle_primitive(Iterator it)
   68         : m_pt(&(*it)) {}
   69 
   70     const Id& id() const { return m_pt; }
   71 
   72     // utility function to convert a custom 
   73     // point type to CGAL point type.
   74     Point convert(const My_point *p) const
   75     {
   76         return Point(p->m_x,p->m_y,p->m_z);
   77     }
   78 
   79     // on the fly conversion from the internal data to the CGAL types
   80     Datum datum() const
   81     {
   82         return Datum(convert(m_pt->m_pa),
   83             convert(m_pt->m_pb),
   84             convert(m_pt->m_pc));
   85     }
   86 
   87     // returns a reference point which must be on the primitive
   88     Point reference_point() const
   89     { return convert(m_pt->m_pa); }
   90 };
   91 
   92 
   93 
   94 typedef CGAL::AABB_traits<K, My_triangle_primitive> My_AABB_traits;
   95 typedef CGAL::AABB_tree<My_AABB_traits> Tree;
   96 
   97 int main()
   98 {
   99     My_point a(1.0, 0.0, 0.0);
  100     My_point b(0.0, 1.0, 0.0);
  101     My_point c(0.0, 0.0, 1.0);
  102     My_point d(0.0, 0.0, 0.0);
  103 
  104     std::vector<My_triangle> triangles;
  105     triangles.push_back(My_triangle(&a,&b,&c));
  106     triangles.push_back(My_triangle(&a,&b,&d));
  107     triangles.push_back(My_triangle(&a,&d,&c));
  108 
  109     // constructs AABB tree
  110     Tree tree(triangles.begin(),triangles.end());
  111 
  112     // counts #intersections
  113     K::Ray_3 ray_query(K::Point_3(1.0, 0.0, 0.0), K::Point_3(0.0, 1.0, 0.0));
  114     std::cout << tree.number_of_intersected_primitives(ray_query)
  115         << " intersections(s) with ray query" << std::endl;
  116 
  117     // computes closest point
  118     K::Point_3 point_query(2.0, 2.0, 2.0);
  119     K::Point_3 closest_point = tree.closest_point(point_query);
  120     std::cerr << "closest point is: " << closest_point << std::endl;
  121 
  122     return EXIT_SUCCESS;
  123 }