inline typename Cylinder<T>::position_type random_position(Cylinder<T> const& shape, Trng& rng) { // -1 < rng() < 1. See for example CylindricalSurface.hpp. return add(shape.position(), multiply(shape.unit_z(), rng() * shape.half_length())); }
inline typename Cylinder<T_>::length_type distance(Cylinder<T_> const& obj, typename Cylinder<T_>::position_type const& pos) { typedef typename Cylinder<T_>::position_type position_type; typedef typename Cylinder<T_>::length_type length_type; /* First compute the (z,r) components of pos in a coordinate system * defined by the vectors unitR and unit_z, where unitR is * choosen such that unitR and unit_z define a plane in which * pos lies. */ const std::pair<length_type, length_type> r_z(to_internal(obj, pos)); /* Then compute distance to cylinder. */ const length_type dz(std::fabs(r_z.second) - obj.half_length()); const length_type dr(r_z.first - obj.radius()); length_type distance; if (dz > 0) { // pos is (either) to the right or to the left of the cylinder. if (r_z.first > obj.radius()) { // Compute distance to edge. distance = std::sqrt( dz * dz + dr * dr ); } else { distance = dz; } } else { if (dr > obj.radius()) { // pos is somewhere 'parallel' to the cylinder. distance = dr; } else { // Inside cylinder. distance = std::max(dr, dz); } } return distance; }
bool operator==(const Cylinder& rhs) const { return position_ == rhs.position() && radius_ == rhs.radius() && unit_z_ == rhs.unit_z() && half_length_ == rhs.half_length(); }