利用CGAL库的Triangle_3_Segment_3_do_intersect.h中的函数
//计算segment(p0,p1)和polyhedron的交点.;
//注意顺序,p1是模型上的点,p0是segment另外一点;
//返回segment和polyhedron交点个数
int segment_intersection_polyhedron(Point_3 p0,Point_3 p1,GLMmodel* m){
Segment s(p0,p1);
Kernel k;
int crossing = 0;//交点个数,=0时可见;
GLfloat* vertices = m->vertices;//访问obj是用的glm库的方式
for(int i = 0;i<m->numtriangles;i++)//遍历每一个三角面片
{
GLMtriangle tri = m->triangles[i];
Point_3 a(vertices[3*tri.vindices[0]],vertices[3*tri.vindices[0]+1],vertices[3*tri.vindices[0]+2]);
Point_3 b(vertices[3*tri.vindices[1]],vertices[3*tri.vindices[1]+1],vertices[3*tri.vindices[1]+2]);
Point_3 c(vertices[3*tri.vindices[2]],vertices[3*tri.vindices[2]+1],vertices[3*tri.vindices[2]+2]);
//如果三角面片有任何一点是p1,就不算;
if(point_equal(a,p1) || point_equal(b,p1) || point_equal(c,p1)) continue;
Triangle t(a,b,c);
if(CGAL::internal::do_intersect(s,t,k)){//如果相交
crossing++;
}
}
return crossing;
}