123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #include <cmath>
- #include "client/math/Vector.h"
- Vector::Vector() : x(0), y(0), z(0) {
- }
- Vector::Vector(float ix, float iy, float iz) : x(ix), y(iy), z(iz) {
- }
- float Vector::getX() const {
- return x;
- }
- float Vector::getY() const {
- return y;
- }
- float Vector::getZ() const {
- return z;
- }
- Vector& Vector::setX(float ix) {
- x = ix;
- return *this;
- }
- Vector& Vector::setY(float iy) {
- y = iy;
- return *this;
- }
- Vector& Vector::setZ(float iz) {
- z = iz;
- return *this;
- }
- Vector& Vector::set(const Vector& v) {
- return set(v.x, v.y, v.z);
- }
- Vector& Vector::set(float ix, float iy, float iz) {
- x = ix;
- y = iy;
- z = iz;
- return *this;
- }
- Vector& Vector::setInverse(const Vector& v) {
- x = -v.x;
- y = -v.y;
- z = -v.z;
- return *this;
- }
- Vector& Vector::setMul(const Vector& v, float f) {
- x = v.x * f;
- y = v.y * f;
- z = v.z * f;
- return *this;
- }
- Vector& Vector::setAngles(float lengthAngle, float widthAngle) {
- lengthAngle = lengthAngle * M_PI / 180.0f;
- widthAngle = widthAngle * M_PI / 180.0f;
- x = cosf(widthAngle) * sinf(lengthAngle);
- y = sinf(widthAngle);
- z = cosf(widthAngle) * cosf(lengthAngle);
- return *this;
- }
- Vector& Vector::add(const Vector& v) {
- x += v.x;
- y += v.y;
- z += v.z;
- return *this;
- }
- Vector& Vector::sub(const Vector& v) {
- x -= v.x;
- y -= v.y;
- z -= v.z;
- return *this;
- }
- Vector& Vector::mul(float f) {
- x *= f;
- y *= f;
- z *= f;
- return *this;
- }
- Vector& Vector::mul(const Matrix& m) {
- const float* d = m.getValues();
- const float w = 1.0f;
- float nx = x * d[0] + y * d[4] + z * d[8] + w * d[12];
- float ny = x * d[1] + y * d[5] + z * d[9] + w * d[13];
- float nz = x * d[2] + y * d[6] + z * d[10] + w * d[14];
- float nw = x * d[3] + y * d[7] + z * d[11] + w * d[15];
- set(nx / nw, ny / nw, nz / nw);
- return *this;
- }
- Vector& Vector::addMul(const Vector& v, float f) {
- x += v.x * f;
- y += v.y * f;
- z += v.z * f;
- return *this;
- }
- Vector& Vector::cross(float ix, float iy, float iz) {
- return set(y * iz - z * iy, z * ix - x * iz, x * iy - y * ix);
- }
- Vector& Vector::cross(const Vector& v) {
- return set(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
- }
- Vector& Vector::normalize() {
- float f = 1.0f / sqrtf(squareLength());
- x *= f;
- y *= f;
- z *= f;
- return *this;
- }
- float Vector::squareLength() const {
- return x * x + y * y + z * z;
- }
- float Vector::dot(const Vector& v) const {
- return x * v.x + y * v.y + z * v.z;
- }
- float Vector::dotInverse(const Vector& v) const {
- return x * (-v.x) + y * (-v.y) + z * (-v.z);
- }
- std::ostream& operator<<(std::ostream& os, const Vector& v) {
- return os << "Vector(x = " << v.getX() << ", y = " << v.getY() << ", z = " << v.getZ() << ")";
- }
|