Real Cylinder3D::closestPoint(const Point3D& pt,Point3D& closest) const { Real h = dot(pt-center,axis); if(h < 0) { Circle3D c; getBase(c); return c.closestPoint(pt,closest); } else if (h > height) { Circle3D c; getCap(c); return c.closestPoint(pt,closest); } else { Vector3 vperp = (pt-center) - h*axis; Real rv = vperp.norm(); if(rv <= radius) { //inside cylinder closest = pt; return 0; } else { closest = center + axis*h + vperp*(radius/rv); return rv - radius; } } }