angles.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined(INCLUDED_ANGLES_H)
00023 #define INCLUDED_ANGLES_H
00024
00025 #include "ientity.h"
00026
00027 #include "math/quaternion.h"
00028 #include "generic/callback.h"
00029 #include "stringio.h"
00030
00031 #include "angle.h"
00032
00033 const Vector3 ANGLESKEY_IDENTITY = Vector3(0, 0, 0);
00034
00035 inline void default_angles (Vector3& angles)
00036 {
00037 angles = ANGLESKEY_IDENTITY;
00038 }
00039 inline void normalise_angles (Vector3& angles)
00040 {
00041 angles[0] = static_cast<float> (float_mod(angles[0], 360));
00042 angles[1] = static_cast<float> (float_mod(angles[1], 360));
00043 angles[2] = static_cast<float> (float_mod(angles[2], 360));
00044 }
00045 inline void read_angle (Vector3& angles, const char* value)
00046 {
00047 if (!string_parse_float(value, angles[2])) {
00048 default_angles(angles);
00049 } else {
00050 angles[0] = 0;
00051 angles[1] = 0;
00052 normalise_angles(angles);
00053 }
00054 }
00055 inline void read_angles (Vector3& angles, const char* value)
00056 {
00057 if (!string_parse_vector3(value, angles)) {
00058 default_angles(angles);
00059 } else {
00060 angles = Vector3(angles[2], angles[0], angles[1]);
00061 normalise_angles(angles);
00062 }
00063 }
00064 inline void write_angles (const Vector3& angles, Entity* entity)
00065 {
00066 if (angles[0] == 0 && angles[1] == 0 && angles[2] == 0) {
00067 entity->setKeyValue("angle", "");
00068 entity->setKeyValue("angles", "");
00069 } else {
00070 char value[64];
00071 bool invertangle1 = false;
00072 if (angles[1] == -0)
00073 invertangle1 = true;
00074 sprintf(value, "%g %g %g", invertangle1 ? -angles[1] : angles[1], angles[2], angles[0]);
00075 entity->setKeyValue("angle", "");
00076 entity->setKeyValue("angles", value);
00077 }
00078 }
00079
00080 inline Vector3 angles_rotated (const Vector3& angles, const Quaternion& rotation)
00081 {
00082 return matrix4_get_rotation_euler_xyz_degrees(matrix4_multiplied_by_matrix4(matrix4_rotation_for_euler_xyz_degrees(
00083 angles), matrix4_rotation_for_quaternion_quantised(rotation)));
00084 }
00085
00086 class AnglesKey
00087 {
00088 Callback m_anglesChanged;
00089 public:
00090 Vector3 m_angles;
00091
00092 AnglesKey (const Callback& anglesChanged) :
00093 m_anglesChanged(anglesChanged), m_angles(ANGLESKEY_IDENTITY)
00094 {
00095 }
00096
00097 void angleChanged (const std::string& value)
00098 {
00099 read_angle(m_angles, value.c_str());
00100 m_anglesChanged();
00101 }
00102 typedef MemberCaller1<AnglesKey, const std::string&, &AnglesKey::angleChanged> AngleChangedCaller;
00103
00104 void anglesChanged (const std::string& value)
00105 {
00106 read_angles(m_angles, value.c_str());
00107 m_anglesChanged();
00108 }
00109 typedef MemberCaller1<AnglesKey, const std::string&, &AnglesKey::anglesChanged> AnglesChangedCaller;
00110
00111 void write (Entity* entity) const
00112 {
00113 write_angles(m_angles, entity);
00114 }
00115 };
00116
00117 #endif