00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _BASE_ORIENT_HPP_
00026 #define _BASE_ORIENT_HPP_
00027
00028 #include <iostream>
00029 #include <math.h>
00030
00031 #include <base/base>
00032 #include <base/array>
00033 #include <base/consts>
00034 #include <base/Vector>
00035 #include <base/Matrix>
00036 #include <base/Quat4>
00037 #include <base/Matrix3>
00038
00039
00040 namespace base {
00041
00042 class Serializer;
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 class Orient
00094 {
00095
00096 public:
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 public:
00108 typedef Byte Representation;
00109
00110 public:
00111
00112 static const Representation EulerXYZs;
00113 static const Representation EulerXYXs;
00114 static const Representation EulerXZYs;
00115 static const Representation EulerXZXs;
00116 static const Representation EulerYZXs;
00117 static const Representation EulerYZYs;
00118 static const Representation EulerYXZs;
00119 static const Representation EulerYXYs;
00120 static const Representation EulerZXYs;
00121 static const Representation EulerZXZs;
00122 static const Representation EulerZYXs;
00123 static const Representation EulerZYZs;
00124
00125 static const Representation EulerZYXr;
00126 static const Representation EulerXYXr;
00127 static const Representation EulerYZXr;
00128 static const Representation EulerXZXr;
00129 static const Representation EulerXZYr;
00130 static const Representation EulerYZYr;
00131 static const Representation EulerZXYr;
00132 static const Representation EulerYXYr;
00133 static const Representation EulerYXZr;
00134 static const Representation EulerZXZr;
00135 static const Representation EulerXYZr;
00136 static const Representation EulerZYZr;
00137
00138 static const Representation LastEuler = 23;
00139
00140 static const Representation EulerRPY;
00141
00142 static const Representation Quat = 24;
00143 static const Representation Mat = 25;
00144 static const Representation Rodriguez = 26;
00145 static const Representation RepEnd = 27;
00146
00147
00148
00149 Orient();
00150
00151
00152 Orient(Representation representation);
00153
00154
00155 Orient(const Quat4& q);
00156
00157
00158 Orient(Real roll, Real pitch, Real yaw);
00159
00160
00161 explicit Orient(const Vector& v, Representation representation = EulerRPY);
00162
00163
00164 Orient(const Matrix3& m3);
00165
00166
00167 Orient(const Matrix& m);
00168
00169 Orient(const Orient& copy);
00170
00171 ~Orient();
00172
00173
00174 Representation representation() const { return rep; }
00175
00176 static String representationString(Representation rep);
00177
00178 static bool isEuler(Representation rep) { return rep<=LastEuler; }
00179 bool isEuler() const { return rep<=LastEuler; }
00180
00181 static Int size(Representation rep)
00182 {
00183 if (rep == Mat) return 9;
00184 if (rep == Quat) return 4;
00185 return 3;
00186 }
00187
00188 Int size() const
00189 {
00190 return size(rep);
00191 }
00192
00193
00194 Real& operator[](Int i)
00195 {
00196 if (rep!=Mat)
00197 return v[i];
00198 else {
00199 Int r=i/3;
00200 Int c=i-(r*3);
00201 return (*m)(r,c);
00202 }
00203 }
00204
00205 const Real& operator[](Int i) const
00206 {
00207 if (rep!=Mat)
00208 return v[i];
00209 else {
00210 Int r=i/3;
00211 Int c=i-(r*3);
00212 return (*m)(r,c);
00213 }
00214 }
00215
00216 Orient& operator=(const Quat4& q)
00217 {
00218 if (rep == Mat)
00219 delete &(*m);
00220 rep = Quat;
00221 v.resize(4);
00222 v[0]=q.v.x; v[1]=q.v.y; v[2]=q.v.z; v[3]=q.w;
00223 return *this;
00224 }
00225
00226 Orient& operator=(const Matrix3& mt);
00227
00228 Orient& operator=(const Orient& copy) throw();
00229
00230
00231 void setIdentity(Representation representation = Quat);
00232 void setFromRotationComponent(const Matrix4& mt);
00233
00234
00235
00236 bool operator==(const Orient& o) const throw();
00237
00238
00239 bool operator!=(const Orient& o) const throw()
00240 { return !operator==(o); }
00241
00242
00243 bool equals(const Orient& o, Real epsilon = consts::epsilon) const throw();
00244
00245
00246 void rotatePoint(Point3& p) const
00247 {
00248 Quat4 q(getQuat4());
00249 q.rotatePoint(p);
00250 }
00251
00252 Point3 rotate(const Point3& p) const
00253 {
00254 Quat4 q(getQuat4());
00255 return q.rotate(p);
00256 }
00257
00258 void rotatePoint(Point4& p) const
00259 {
00260 Quat4 q(getQuat4());
00261 q.rotatePoint(p);
00262 }
00263
00264 Point4 rotate(const Point4& p) const
00265 {
00266 Quat4 q(getQuat4());
00267 return q.rotate(p);
00268 }
00269
00270
00271
00272 Orient& invert();
00273
00274
00275
00276 static Orient interpolate(const Orient& lower, const Orient& upper, Real t);
00277
00278
00279 Matrix getRotationMatrix() const;
00280 Matrix3 getRotationMatrix3() const;
00281 Quat4 getQuat4() const;
00282 Vector getVector(Representation representation) const;
00283 Vector3 getVector3(Representation representation) const;
00284
00285
00286 operator Vector() const
00287 { if (rep!=Mat) return v; else return getVector(rep); }
00288
00289
00290 operator Matrix3() const
00291 { return getRotationMatrix3(); }
00292
00293
00294 operator Quat4() const
00295 { return getQuat4(); }
00296
00297
00298
00299 void changeRepresentation(Representation newRepresentation)
00300 { try { changeRep(newRepresentation); } catch (std::exception&e) {} }
00301
00302
00303
00304 Matrix getBinv() const;
00305
00306 void serialize(Serializer& s);
00307
00308 protected:
00309 mutable Representation rep;
00310 mutable Vector v;
00311 typedef Matrix* MatrixRef;
00312 mutable MatrixRef m;
00313
00314 enum Axis { X=0, Y=1, Z=2, W=3 };
00315 enum Frame { Static=0, Rotating=1 };
00316 enum Repetition { NonRepeating=0, Repeating=1 };
00317 enum Parity { Even=0, Odd=1 };
00318
00319 inline static Representation eulerRep(Axis axis, Parity parity, Repetition repetition, Frame frame)
00320 { return Representation(((((((Byte(axis))<<1)+(Byte(parity)))<<1)+(Byte(repetition)))<<1)+(Byte(frame))); }
00321
00322 static inline Frame frame(Representation rep) { return Frame(Byte(rep)&1); }
00323 static inline Repetition repetition(Representation rep) { return Repetition((Byte(rep)>>1)&1); }
00324 static inline Parity parity(Representation rep) { return Parity((Byte(rep)>>2)&1); }
00325
00326 static inline Axis axisI(Representation rep) { return Axis(EulSafe[((Byte(rep)>>3)&3)]); }
00327 static inline Axis axisJ(Representation rep) { return Axis(Byte(EulNext[axisI(rep)+(parity(rep)==Odd)])); }
00328 static inline Axis axisK(Representation rep) { return Axis(Byte(EulNext[axisI(rep)+(parity(rep)!=Odd)])); }
00329 static inline Axis axisH(Representation rep) { return ((repetition(rep)==NonRepeating)?axisK(rep):axisI(rep)); }
00330 static inline void getRep(Representation rep, Axis& i, Axis& j, Axis& k, Axis& h,
00331 Parity& n, Repetition& s, Frame& f)
00332 {
00333 Byte o=rep;
00334 f=Frame(o&1); o>>=1;
00335 s=Repetition(o&1); o>>=1;
00336 n=Parity(o&1); o>>=1;
00337 i=Axis(EulSafe[o&3]);
00338 j=Axis(EulNext[Byte(i)+Byte(n)]);
00339 k=Axis(EulNext[Byte(i)+1-Byte(n)]);
00340 h=s?k:i;
00341 }
00342
00343
00344 static Byte EulSafe[4];
00345 static Byte EulNext[4];
00346
00347
00348
00349 void changeRep(Representation newRep) const;
00350
00351 friend std::ostream& operator<<(std::ostream& out, const Orient& o);
00352 };
00353
00354
00355
00356
00357
00358 inline std::ostream& operator<<(std::ostream& out, const Orient& o)
00359 { return (o.rep!=Orient::Mat)?(out << o.v):(out << *o.m); }
00360
00361
00362
00363 }
00364
00365 #endif