2 #ifndef vgl_rotation_3d_h_ 3 #define vgl_rotation_3d_h_ 25 #include <vnl/vnl_vector_fixed.h> 26 #include <vnl/vnl_matrix_fixed.h> 27 #include <vnl/vnl_quaternion.h> 28 #include <vnl/vnl_cross.h> 29 #include <vnl/vnl_math.h> 36 # include <vcl_msvc_warnings.h> 57 T mag = rvector.magnitude();
59 q_ = vnl_quaternion<T>(rvector/mag,mag);
61 q_ = vnl_quaternion<T>(0,0,0,1);
67 T mag = rvector.magnitude();
69 q_ = vnl_quaternion<T>(rvector/mag,mag);
71 q_ = vnl_quaternion<T>(0,0,0,1);
81 {
q_ = matrix.transpose();
return *
this; }
94 vnl_vector_fixed<T,3>
const& b)
96 vnl_vector_fixed<T,3> c = vnl_cross_3d(a, b);
97 double aa = 0.0;
if (
dot_product(a, b) < 0) { aa = vnl_math::pi; c=-c; }
98 double cmag = static_cast<double>(c.magnitude())
99 / static_cast<double>(a.magnitude())
100 / static_cast<double>(b.magnitude());
102 if (cmag>1.0) cmag=1.0;
106 if (aa!=vnl_math::pi) {
107 q_ = vnl_quaternion<T>(0, 0, 0, 1);
112 double ax = std::fabs(static_cast<double>(a[0]));
113 double ay = std::fabs(static_cast<double>(a[1]));
114 double az = std::fabs(static_cast<double>(a[2]));
115 vnl_vector_fixed<T,3>
v(T(0));
116 double amin = ax;
v[0]=T(1);
117 if (ay<amin) { amin = ay;
v[0]=T(0);
v[1]=T(1); }
118 if (az<amin) {
v[0]=T(0);
v[1]=T(0);
v[2]=T(1); }
120 vnl_vector_fixed<T,3> pi_axis = vnl_cross_3d(
v, a);
121 q_ = vnl_quaternion<T>(pi_axis/pi_axis.magnitude(), aa);
125 double angl = std::asin(cmag)+aa;
126 q_ = vnl_quaternion<T>(c/c.magnitude(), angl);
134 vnl_vector_fixed<T,3> na(a.
x(), a.
y(), a.
z());
135 vnl_vector_fixed<T,3> nb(b.
x(), b.
y(), b.
z());
154 return q_.rotation_euler_angles();
162 double ang =
q_.angle();
164 return vnl_vector_fixed<T,3>(T(0));
165 return q_.axis()*T(ang);
171 return q_.rotation_matrix_transpose().transpose();
181 vnl_vector_fixed<T,3>
axis()
const {
return q_.axis(); }
206 vnl_vector_fixed<T,3> rp =
q_.rotate(vnl_vector_fixed<T,3>(p.
x(),p.
y(),p.
z()));
213 vnl_vector_fixed<T,3> rp =
q_.rotate(vnl_vector_fixed<T,3>(p.
a(),p.
b(),p.
c()));
227 vnl_vector_fixed<T,3> rp =
q_.rotate(vnl_vector_fixed<T,3>(p.
x(),p.
y(),p.
z()));
234 vnl_vector_fixed<T,3> rp =
q_.rotate(vnl_vector_fixed<T,3>(p.
a(),p.
b(),p.
c()));
242 this->
operator*(l.
point2()));
249 this->
operator*(l.
point2()));
255 vnl_vector_fixed<T,3> rv =
q_.rotate(vnl_vector_fixed<T,3>(
v.x(),
v.y(),
v.z()));
260 vnl_vector_fixed<T, 3>
operator*( vnl_vector_fixed<T,3>
const&
v )
const 270 vnl_quaternion<T>
q_;
297 template <
class T>
inline 300 vnl_matrix_fixed<T,3,3> R = rot.as_3matrix();
302 itr != pts.end(); ++itr)
305 p.
set(R[0][0]*p.
x()+R[0][1]*p.
y()+R[0][2]*p.
z(),
306 R[1][0]*p.
x()+R[1][1]*p.
y()+R[1][2]*p.
z(),
307 R[2][0]*p.
x()+R[2][1]*p.
y()+R[2][2]*p.
z(), p.
w());
311 #endif // vgl_rotation_3d_h_ vgl_rotation_3d(vnl_vector_fixed< T, 3 > const &a, vnl_vector_fixed< T, 3 > const &b)
Construct to rotate (direction of) vector a to vector b.
vgl_rotation_3d(vnl_vector_fixed< T, 3 > const &rvector)
Construct from a Rodrigues vector.
point in projective 3D space
vnl_matrix_fixed< T, 3, 3 > as_matrix() const
Output the matrix representation of this rotation in 3x3 form.
vgl_point_3d< Type > point1() const
vgl_rotation_3d & operator=(vgl_h_matrix_3d< T > const &h)
Construct from a vgl_h_matrix_3d.
vgl_rotation_3d(vnl_quaternion< T > const &q)
Construct from a quaternion.
bool operator==(vgl_rotation_3d< T > const &r) const
comparison operator.
T a() const
Return x coefficient.
vgl_point_3d< Type > point2() const
vnl_quaternion< T > q_
The internal representation of the rotation is a quaternion.
Type b() const
Return y coefficient.
Type c() const
Return z coefficient.
double angle() const
Returns the angle of rotation on the range [ 0 360 ] in radians.
vgl_rotation_3d< T > inverse() const
The inverse rotation.
vnl_vector_fixed< T, 3 > as_rodrigues() const
Output Rodrigues vector.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
Type d() const
Return homogeneous scaling coefficient.
Represents a homogeneous 3D point.
vnl_quaternion< T > as_quaternion() const
Output unit quaternion.
vgl_rotation_3d & operator=(vnl_vector_fixed< T, 3 > const &rvector)
Construct from a Rodrigues vector.
vgl_rotation_3d(vgl_vector_3d< T > const &a, vgl_vector_3d< T > const &b)
Construct to rotate (direction of) vector a to vector b.
A class to hold a non-homogeneous representation of a 3D line.
vnl_matrix_fixed< T, 3, 3 > get_upper_3x3_matrix() const
T b() const
Return y coefficient.
a point in 3D nonhomogeneous space
A class to hold a 3-d projective transformation matrix and to perform common operations using it e....
vgl_point_3d< Type > point1() const
Return the first point representing this line.
Represents a Euclidean 3D plane.
vgl_rotation_3d(vnl_matrix_fixed< T, 3, 3 > const &matrix)
Construct from a 3x3 rotation matrix.
Represents a 3D line segment using two points.
vgl_rotation_3d()
Construct the identity rotation.
T d() const
Return constant coefficient.
Type a() const
Return x coefficient.
vgl_h_matrix_3d< T > as_h_matrix_3d() const
Output the matrix representation of this rotation in 4x4 form.
vgl_rotation_3d< T > operator *(vgl_rotation_3d< T > const &first_rotation) const
Composition of two rotations.
Direction vector in Euclidean 3D space, templated by type of element.
void vgl_rotate_3d(vgl_rotation_3d< T > const &rot, std::vector< vgl_homg_point_3d< T > > &pts)
In-place rotation of a vector of homogeneous points.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
void set(Type px, Type py, Type pz, Type pw=(Type) 1)
Set x,y,z,w.
vgl_rotation_3d & operator=(vnl_matrix_fixed< T, 3, 3 > const &matrix)
Construct from a 3x3 rotation matrix.
vgl_rotation_3d(T const &rx, T const &ry, T const &rz)
Construct from Euler angles.
T dot_product(v const &a, v const &b)
dot product or inner product of two vectors.
vgl_point_3d< Type > point2() const
Return the second point representing this line.
vgl_rotation_3d(vgl_h_matrix_3d< T > const &h)
Construct from a vgl_h_matrix_3d.
vgl_rotation_3d & set_identity()
Make the rotation the identity (i.e. no rotation).
vgl_rotation_3d< T > transpose() const
The transpose or conjugate of the rotation.
4x4 3D-to-3D projectivity
vnl_vector_fixed< T, 3 > as_euler_angles() const
Output Euler angles.
vgl_homg_point_3d< Type > point_finite() const
Finite point (Could be an ideal point, if the whole line is at infinity.).
vnl_vector_fixed< T, 3 > axis() const
Returns the axis of rotation (unit vector).
T c() const
Return z coefficient.
vgl_homg_point_3d< Type > point_infinite() const
Infinite point: the intersection of the line with the plane at infinity.
Represents a homogeneous 3D line using two points.