bool Polyhedron::Intersects(const LineSegment &lineSegment) const { if (Contains(lineSegment)) return true; for(int i = 0; i < NumFaces(); ++i) { float t; Plane plane = FacePlane(i); bool intersects = Plane::IntersectLinePlane(plane.normal, plane.d, lineSegment.a, lineSegment.b - lineSegment.a, t); if (intersects && t >= 0.f && t <= 1.f) if (FaceContains(i, lineSegment.GetPoint(t))) return true; } return false; }
void CalculateBoundaryVertexNormal3D(vector3& nOut, Grid& grid, Vertex* vrt, TAAPosVRT& aaPos) { // The algorithm is a little cumbersome. However, through this setup, we // make sure that the orientation of the normal indeed points outwards, // based only on the topology. // set nOut to 0 VecSet(nOut, 0); // iterate over associated volumes std::vector<Volume*> vols; CollectAssociated(vols, grid, vrt); FaceDescriptor fd; for(size_t i_vol = 0; i_vol < vols.size(); ++i_vol){ Volume* v = vols[i_vol]; // check for each side of f whether it is a boundary edge for(size_t i_side = 0; i_side < v->num_sides(); ++i_side){ if(IsBoundaryFace3D(grid, grid.get_face(v, i_side))){ v->face_desc(i_side, fd); // make sure that fd contains the given vertex if(!FaceContains(&fd, vrt)) continue; // the normal pointing outwards is clearly defined from the // orientation of the face descriptor vector3 n; CalculateNormal(n, &fd, aaPos); VecAdd(nOut, nOut, n); } } } VecNormalize(nOut, nOut); }
bool Polyhedron::Contains(const float3 &point) const { int numIntersections = 0; for(int i = 0; i < (int)f.size(); ++i) { Plane p(v[f[i].v[0]] - point, v[f[i].v[1]] - point, v[f[i].v[2]] - point); // Find the intersection of the plane and the ray (0,0,0) -> (t,0,0), t >= 0. // <normal, point_on_ray> == d // n.x * t == d // t == d / n.x if (Abs(p.normal.x) > 1e-5f) { float t = p.d / p.normal.x; // If t >= 0, the plane and the ray intersect, and the ray potentially also intersects the polygon. // Finish the test by checking whether the point of intersection is contained in the polygon, in // which case the ray-polygon intersection occurs. if (t >= 0.f && FaceContains(i, point + float3(t,0,0))) ++numIntersections; } } return numIntersections % 2 == 1; }