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

@ -41,6 +41,7 @@ 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);
@ -48,6 +49,7 @@ public:
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 {
@ -67,4 +67,11 @@ Vector3d Vector2d::toVector3d() const {
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 {
@ -56,4 +56,12 @@ 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;
@ -68,6 +72,11 @@ public:
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 {
@ -51,4 +51,13 @@ 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;
@ -60,6 +64,11 @@ public:
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