Skip to Content.
Sympa Menu

cgal-discuss - [cgal-discuss] Simple triangle intersection

Subject: CGAL users discussion list

List archive

[cgal-discuss] Simple triangle intersection


Chronological Thread 
  • From: Blaz Bratanic <>
  • To:
  • Subject: [cgal-discuss] Simple triangle intersection
  • Date: Wed, 13 Aug 2014 17:17:50 +0200

I am trying to implement a simple triangle intersection test (below), but i cant get it to work. 

I get a bad_get error 

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_get> >'
  what():  boost::bad_get: failed value get using boost::get
Aborted

when trying to extract Point from Intersection_and_primitive_id (boost::get<Point>(i1.first)). I guess i must 
be missing something. 

Thanks in advance for any help.

// Code snippet 

#include <iostream>
#include <vector>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/AABB_triangle_primitive.h>

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;
typedef CGAL::Bbox_3 BoundingBox;
typedef std::vector<Triangle>::iterator Iterator;
typedef CGAL::AABB_triangle_primitive<K, Iterator> Primitive;
typedef CGAL::AABB_traits<K, Primitive> AABB_triangle_traits;
typedef CGAL::AABB_tree<AABB_triangle_traits> Tree;

typedef Tree::Intersection_and_primitive_id<Triangle>::Type
    Triangle_intersection;
typedef Tree::Primitive_id Primitive_id;

std::vector<bool> visible_triangles(std::vector<Triangle>& triangles,
                                    size_t nrays_per_axis) {
  // constructs AABB tree
  Tree tree(triangles.begin(), triangles.end());
  BoundingBox bbox = tree.bbox();

  std::vector<bool> visibility(triangles.size(), false);

  double x = bbox.xmin();
  double y = bbox.ymin();
  double dx = (bbox.xmax() - bbox.xmin()) / nrays_per_axis;
  double dy = (bbox.ymax() - bbox.ymin()) / nrays_per_axis;

  std::vector<Triangle_intersection> intersections;

  for (int iy = 0; iy < nrays_per_axis; iy++, y += dy) {
    for (int ix = 0; ix < nrays_per_axis; ix++, x += dx) {
      Point a = {x, y, 1};
      Point b = {x, y, 0};
      Ray ray_query(a, b);

      if (tree.do_intersect(ray_query)) {
        intersections.clear();
        tree.all_intersections(ray_query, std::back_inserter(intersections));

        auto min_el =
            std::min_element(intersections.begin(), intersections.end(),
                             [&a](Triangle_intersection const& i1,
                                  Triangle_intersection const& i2) {
              return squared_distance(a, boost::get<Point>(i1.first)) <
                     squared_distance(a, boost::get<Point>(i2.first));
            });
        visibility[std::distance(triangles.begin(), min_el->second)] = true;
      }
    }
  }
  return visibility;
}

int main() {
  Point a(0.5, 0.0, 0.0);
  Point b(0.0, 0.5, 0.0);
  Point c(0.0, 0.0, 0.5);
  Point d(0.0, 0.0, 0.0);
  std::vector<Triangle> triangles = {Triangle(a, b, c), Triangle(a, b, d),
                                     Triangle(a, d, c)};

  auto visibility = visible_triangles(triangles, 10);

  return 0;
}




Archive powered by MHonArc 2.6.18.

Top of Page