OSG_BASE_DLLMAPPING bool intersect(const CylinderVolume &cyl1, const CylinderVolume &cyl2) { Pnt3f p1, p2; Vec3f v1, v2; cyl1.getAxis(p1, v1); cyl2.getAxis(p2, v2); return dist3D_Segment_to_Segment(p1, v1, p2, v2) <= (cyl1.getRadius() + cyl2.getRadius()); }
OSG_BASE_DLLMAPPING bool intersect(const BoxVolume &box, const CylinderVolume &cylinder) { bool retCode; Pnt3f apos; Vec3f adir; cylinder.getAxis(apos, adir); if(box.isEmpty() == true || cylinder.isEmpty() == true) { retCode = false; } else if(box.isInfinite() == true || cylinder.isInfinite() == true) { retCode = true; } else { Real32 s1 = 0, s2 = 0, s3 = 0, s4 = 0, d = 0, d1 = 0, d2 = 0; Pnt3f c, p, p1, p2; Vec3f u, u1, u2; // find the distance between the min and the max of the box //with the lower point and the upper point of the cylinder respectively s1 = (apos - box.getMin()).length(); s2 = (apos - box.getMax()).length(); s3 = (apos + adir - box.getMin()).length(); s4 = (apos + adir - box.getMax()).length(); //Check the minimum of the above distances if(s1 <= s2) { d1 = s1; p1 = box.getMin(); } else { d1 = s2; p1 = box.getMax(); } if(s3 <= s4) { d2 = s3; p2 = box.getMin(); } else { d2 = s4; p2 = box.getMax(); } //set the value of the vector corresponding to the shortest distance if(d1 <= d2) { d = d1; c = apos; p = p1; } else { d = d2; c = apos + adir; p = p2; } // decompose the vector in u1 and u2 which are parallel and // perpendicular to the cylinder axis respectively u = p - c; u1 = (u[0] * adir[0] + u[1] * adir[1] + u[2] * adir[2]) / (adir.length() * adir.length()) * adir; u2 = u - u1; if(u1.length() <= 10e-6) { retCode = true; } else if(u2.length() <= 10e-6) { retCode = (d <= 10e-6); } else { retCode = (u2.length() <= cylinder.getRadius()); } } return retCode; }
OSG_BASE_DLLMAPPING bool intersect(const SphereVolume &sphere, const CylinderVolume &cylinder) { bool retCode; Pnt3f apos; Vec3f adir; cylinder.getAxis(apos, adir); if(sphere.isEmpty() || cylinder.isEmpty()) { retCode = false; } else if(sphere.isInfinite() || cylinder.isInfinite()) { retCode = true; } else { Real32 d = 0.f, s1 = 0.f, s2 = 0.f; Pnt3f c; Vec3f u, u1, u2; //get the distance between the upper and lower point of the cylinder // and the sphere center s1 = (apos - sphere.getCenter()).length(); s2 = (apos + adir - sphere.getCenter()).length(); if ((s1<=DBL_EPSILON) || (s2<=DBL_EPSILON)) return true; //check the smallest distance and set the vector coordinate if(s1 <= s2) { d = s1; c = apos; } else { d = s2; c = apos + adir; } // decompose the vector in u1 and u2 which are parallel and // perpendicular to the cylinder axis respectively u = ((d - sphere.getRadius()) / d) * (c - sphere.getCenter()); u1 = (u[0] * adir[0] + u[1] * adir[1] + u[2] * adir[2]) / (adir.length() * adir.length()) * adir; u2 = u - u1; if(u2.length() <= 10e-6) { retCode = (d <= sphere.getRadius()); } else { retCode = (u2.length() <= cylinder.getRadius()); } } return retCode; }
bool Line::intersect(const CylinderVolume &cyl, Real &enter, Real &exit ) const { Real radius = cyl.getRadius(); Vec3r adir; Vec3r o_adir; Pnt3r apos; cyl.getAxis(apos, adir); o_adir = adir; adir.normalize(); bool isect; Real ln; Real dl; Vec3r RC; Vec3r n; Vec3r D; RC = _pos - apos; n = _dir.cross (adir); ln = n .length( ); if(ln == 0.f) // IntersectionLine is parallel to CylinderAxis { D = RC - (RC.dot(adir)) * adir; dl = D.length(); if(dl <= radius) // line lies in cylinder { enter = 0.f; exit = Inf; } else { return false; } } else { n.normalize(); dl = osgAbs(RC.dot(n)); //shortest distance isect = (dl <= radius); if(isect) { // if ray hits cylinder Real t; Real s; Vec3r O; O = RC.cross(adir); t = - (O.dot(n)) / ln; O = n.cross(adir); O.normalize(); s = osgAbs ( (osgSqrt ((radius * radius) - (dl * dl))) / (_dir.dot(O))); exit = t + s; if(exit < 0.f) return false; enter = t - s; if(enter < 0.f) enter = 0.f; } else { return false; } } Real t; Plane bottom(-adir, apos); if(bottom.intersect(*this, t)) { if(bottom.isInHalfSpace(_pos)) { if(t > enter) enter = t; } else { if(t < exit) exit = t; } } else { if(bottom.isInHalfSpace(_pos)) return false; } Plane top(adir, apos + o_adir); if(top.intersect(*this, t)) { if(top.isInHalfSpace(_pos)) { if(t > enter) enter = t; } else { if(t < exit) exit = t; } } else { if(top.isInHalfSpace(_pos)) return false; } return (enter < exit); }