1 #ifndef vgl_sphere_3d_hxx_ 2 #define vgl_sphere_3d_hxx_ 12 # include <vcl_msvc_warnings.h> 34 if (
r_ < 0)
return false;
39 if (cp_sqr_len >
r_*
r_)
return false;
40 double arg = static_cast<double>(
r_*
r_ - cp_sqr_len);
41 T half_chord_len = static_cast<T>(std::sqrt(arg));
44 linevec *= half_chord_len / linevec.
length();
57 return os <<
"<vgl_sphere_3d centre=" <<
c_ 58 <<
"radius=" << r_ << '>
'; 62 //: Read from stream, possibly with formatting. 63 // Either just reads 4 blank-separated numbers, 64 // or reads 4 comma-separated numbers, 65 // or reads 4 numbers in parenthesized form "(123, 321, 567, 890)" 67 std::istream& vgl_sphere_3d<T>::read(std::istream& is) 69 if (! is.good()) return is; // (TODO: should throw an exception) 72 is >> std::ws; // jump over any leading whitespace 73 if (is.eof()) return is; // nothing to be set because of EOF (TODO: should throw an exception) 74 if (is.peek() == '(
') { is.ignore(); paren=true; } 75 is >> std::ws >> cx >> std::ws; 76 if (is.eof()) return is; 77 if (is.peek() == ',
') is.ignore(); 78 is >> std::ws >> cy >> std::ws; 79 if (is.eof()) return is; 80 if (is.peek() == ',
') is.ignore(); 81 is >> std::ws >> cz >> std::ws; 82 if (is.eof()) return is; 83 if (is.peek() == ',
') is.ignore(); 84 is >> std::ws >> r >> std::ws; 86 if (is.eof()) return is; 87 if (is.peek() == ')
') is.ignore(); 88 else return is; // closing parenthesis is missing (TODO: throw an exception) 90 set_centre(vgl_point_3d<T>(cx,cy,cz)); 96 void vgl_sphere_3d<Type>::spherical_to_cartesian(Type elevation_rad, Type azimuth_rad, 97 Type& x, Type& y, Type& z) const{ 99 double el = static_cast<double>(elevation_rad), az = static_cast<double>(azimuth_rad); 100 double cx = static_cast<double>(c_.x()),cy =static_cast<double>(c_.y()), cz = static_cast<double>(c_.z()); 101 double r = static_cast<double>(r_); 102 double se = std::sin(el), ce = std::cos(el); 103 double sa = std::sin(az), ca = std::cos(az); 105 x = static_cast<Type>((r*se*ca)+cx); 106 y = static_cast<Type>((r*se*sa)+cy); 107 z = static_cast<Type>((r*ce)+cz); 111 template <class Type> 112 void vgl_sphere_3d<Type>::spherical_to_cartesian(Type elevation_rad, Type azimuth_rad, vgl_point_3d<Type>& pt) const 116 spherical_to_cartesian(elevation_rad, azimuth_rad, x, y, z); 120 template <class Type> 121 void vgl_sphere_3d<Type>::cartesian_to_spherical(Type x, Type y, Type z, Type& elevation_rad, Type& azimuth_rad) const{ 122 double xd = static_cast<double>(x-c_.x()), yd = static_cast<double>(y-c_.y()), zd = static_cast<double>(z-c_.z()); 123 double r = std::sqrt(xd*xd + yd*yd +zd*zd); 124 elevation_rad = static_cast<Type>(std::acos(zd/r)); 125 azimuth_rad = static_cast<Type>(std::atan2(yd,xd)); 127 template <class Type> 128 void vgl_sphere_3d<Type>::cartesian_to_spherical(vgl_point_3d<Type> const& pt, Type& elevation_rad, Type& azimuth_rad) const{ 129 return cartesian_to_spherical(pt.x(), pt.y(), pt.z(),elevation_rad,azimuth_rad); 132 //: Writes "<vgl_sphere_3d centre=vgl_point_3d<x,y,z> radius=r)>" to stream 134 std::ostream& operator<<(std::ostream& os, const vgl_sphere_3d<T>& sph) 136 return sph.print(os); 140 //: Read from stream, possibly with formatting. 141 // Either just reads 4 blank-separated numbers, 142 // or reads 4 comma-separated numbers, 143 // or reads 4 numbers in parenthesized form "(123, 321, 567, 890)" 145 std::istream& operator>>(std::istream& is, vgl_sphere_3d<T>& sph) 151 #undef VGL_SPHERE_3D_INSTANTIATE 152 #define VGL_SPHERE_3D_INSTANTIATE(T) \ 153 template class vgl_sphere_3d<T >; \ 154 template std::ostream& operator<<(std::ostream&, vgl_sphere_3d<T >const&); \ 155 template std::istream& operator>>(std::istream&, vgl_sphere_3d<T >&) 158 #endif // vgl_sphere_3d_hxx_ vgl_point_2d< T > vgl_closest_point(vgl_line_2d< T > const &l, vgl_point_2d< T > const &p)
Return the point on the given line closest to the given point.
vgl_vector_3d< Type > direction() const
Return the direction vector of this line (not normalised - but perhaps it should be,...
A class to hold a non-homogeneous representation of a 3D line.
vgl_point_3d< Type > c_
centre
Set of closest-point functions.
std::ostream & print(std::ostream &os) const
Writes "<vgl_sphere_3d centre=vgl_point_3d<x,y,z> radius=r)>" to stream.
a point in 3D nonhomogeneous space
double length() const
Return the length of this vector.
a sphere in 3D nonhomogeneous space
T sqr_length(v const &a)
Return the squared length of a vector.
Direction vector in Euclidean 3D space, templated by type of element.
bool clip(const vgl_line_3d_2_points< Type > &line, vgl_point_3d< Type > &p1, vgl_point_3d< Type > &p2) const
Calculate the end points of a line clipped by this sphere.
non-homogeneous 3D line, represented by 2 points.
bool contains(vgl_point_3d< Type > const &p) const
Return true iff the point p is inside (or on) this sphere.