MATH: Add stream-reading to the vector-classes

This commit is contained in:
Einar Johan T. Sømåen 2012-01-30 21:04:08 +01:00
parent 6668354920
commit 74047546a0
8 changed files with 63 additions and 9 deletions

View File

@ -87,5 +87,5 @@ Matrix4 Quaternion::toMatrix() {
dst.setData(newMat);
return dst;
}
}

View File

@ -41,13 +41,15 @@ public:
Quaternion() : Vector4d(0, 0, 0, 0) {}
Quaternion(float lx, float ly, float lz, float lw) : Vector4d(lx, ly, lz, lw) {}
Quaternion(const Quaternion &q) : Vector4d(q.x(), q.y(), q.z(), q.w()) {}
Quaternion(const Vector4d &vec) : Vector4d(vec.x(), vec.y(), vec.z(), vec.w()) {}
Matrix4 toMatrix();
void slerpQuat(Quaternion dst, const Quaternion from, const Quaternion to, const float t);
inline static Quaternion get_quaternion(const char *data) {
return Quaternion(get_float(data), get_float(data + 4), get_float(data + 8), get_float(data + 12));
}
Quaternion& operator=(Vector4d &vec);
};
} // end of namespace Math

View File

@ -21,7 +21,7 @@
*/
#include "math/vector2d.h"
#include "common/stream.h"
#include "common/streamdebug.h"
namespace Math {
@ -66,5 +66,12 @@ Vector3d Vector2d::toVector3d() const {
Vector3d v(value(0), value(1), 0);
return v;
}
void Vector2d::readFromStream(Common::ReadStream *stream) {
char buf[8];
stream->read(buf, 8);
setX(get_float(buf));
setY(get_float(buf + 4));
}
}

View File

@ -26,6 +26,10 @@
#include "math/vector.h"
#include "math/vector3d.h"
namespace Common {
class ReadStream;
}
namespace Math {
template<>
@ -45,6 +49,12 @@ public:
Angle getAngle() const;
Vector3d toVector3d() const;
/**
* Reads 2 floats from the passed stream, and uses them
* as x and y in chronological order.
*/
void readFromStream(Common::ReadStream *stream);
};
typedef Matrix<2, 1> Vector2d;

View File

@ -21,7 +21,7 @@
*/
#include "common/streamdebug.h"
#include "common/stream.h"
#include "math/vector3d.h"
namespace Math {
@ -55,5 +55,13 @@ void Vector3d::set(float lx, float ly, float lz) {
Angle Vector3d::unitCircleAngle() const {
return Angle::arcTangent2(y(), x());
}
void Vector3d::readFromStream(Common::ReadStream *stream) {
char buf[12];
stream->read(buf, 12);
x() = get_float(buf);
y() = get_float(buf + 4);
z() = get_float(buf + 8);
}
} // end of namespace Math

View File

@ -29,6 +29,10 @@
#include "math/vector.h"
#include "math/angle.h"
namespace Common {
class ReadStream;
}
namespace Math {
typedef Matrix<3, 1> Vector3d;
@ -67,7 +71,12 @@ public:
inline static Vector3d get_vector3d(const char *data) {
return Vector3d(get_float(data), get_float(data + 4), get_float(data + 8));
}
/**
* Reads 3 floats from the passed stream, and uses them
* as x,y,z in chronological order.
*/
void readFromStream(Common::ReadStream *stream);
};
} // end of namespace Math

View File

@ -21,7 +21,7 @@
*/
#include "common/streamdebug.h"
#include "common/stream.h"
#include "math/vector4d.h"
namespace Math {
@ -50,5 +50,14 @@ Vector4d::Matrix(const float *data) :
void Vector4d::set(float lx, float ly, float lz, float lw) {
x() = lx; y() = ly; z() = lz; w() = lw;
}
void Vector4d::readFromStream(Common::ReadStream *stream) {
char buf[16];
stream->read(buf, 16);
x() = get_float(buf);
y() = get_float(buf + 4);
z() = get_float(buf + 8);
w() = get_float(buf + 12);
}
} // end of namespace Math

View File

@ -29,6 +29,10 @@
#include "math/vector.h"
#include "math/angle.h"
namespace Common {
class ReadStream;
}
namespace Math {
typedef Matrix<4, 1> Vector4d;
@ -59,7 +63,12 @@ public:
inline static Vector4d get_vector4d(const char *data) {
return Vector4d(get_float(data), get_float(data + 4), get_float(data + 8), get_float(data + 12));
}
/**
* Reads 4 floats from the passed stream, and uses them
* as x,y,z,w in chronological order.
*/
void readFromStream(Common::ReadStream *stream);
};
} // end of namespace Math