Skip to content

Instantly share code, notes, and snippets.

@afabri
Created April 25, 2016 13:09
Show Gist options
  • Save afabri/494d730472d6f108483ee99b5ecb0a0b to your computer and use it in GitHub Desktop.
Save afabri/494d730472d6f108483ee99b5ecb0a0b to your computer and use it in GitHub Desktop.
ray shooting
#include <iostream>
#include <fstream>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/AABB_face_graph_triangle_primitive.h>
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
typedef CGAL::Simple_cartesian<double> K;
typedef K::FT FT;
typedef K::Point_3 Point;
typedef K::Vector_3 Vector;
typedef K::Ray_3 Ray;
typedef CGAL::Surface_mesh<Point> Polyhedron;
typedef boost::graph_traits<Polyhedron>::face_descriptor face_descriptor;
typedef boost::graph_traits<Polyhedron>::halfedge_descriptor halfedge_descriptor;
typedef CGAL::AABB_face_graph_triangle_primitive<Polyhedron> Primitive;
typedef CGAL::AABB_traits<K, Primitive> Traits;
typedef CGAL::AABB_tree<Traits> Tree;
typedef boost::optional< Tree::Intersection_and_primitive_id<Ray>::Type > Ray_intersection;
struct Skip {
face_descriptor fd;
Skip(const face_descriptor fd)
: fd(fd)
{}
bool operator()(const face_descriptor& t) const
{ if(t == fd){
std::cerr << "ignore" << t <<std::endl;
};
return(t == fd);
}
};
int main(int argc, char* argv[])
{
const char* filename = (argc > 1) ? argv[1] : "data/bunny.off";
std::ifstream input(filename);
Polyhedron polyhedron;
input >> polyhedron;
Tree tree( faces(polyhedron).first, faces(polyhedron).second, polyhedron);
BOOST_FOREACH(face_descriptor fd, faces(polyhedron)){
halfedge_descriptor hd = halfedge(fd,polyhedron);
Point p = CGAL::centroid(polyhedron.point(source(hd,polyhedron)),
polyhedron.point(target(hd,polyhedron)),
polyhedron.point(target(next(hd,polyhedron),polyhedron)));
Vector v = CGAL::Polygon_mesh_processing::compute_face_normal(fd,polyhedron);
Ray ray(p,v);
Ray_intersection intersection = tree.first_intersection(ray, Skip(fd));
if(intersection){
if(boost::get<Point>(&(intersection->first))){
Point* p = boost::get<Point>(&(intersection->first) );
std::cout << p << std::endl;
}
}
}
std::cerr << "done" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment