scummvm/graphics/vector3d.h

189 lines
4.6 KiB
C
Raw Normal View History

2009-05-26 14:22:57 +00:00
/* Residual - A 3D game interpreter
*
* Residual is the legal property of its developers, whose names
2011-04-16 12:12:44 +00:00
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
2006-04-02 14:20:45 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*/
2003-08-15 18:00:22 +00:00
2009-05-25 12:28:16 +00:00
#ifndef GRAPHICS_VECTOR3D_H
#define GRAPHICS_VECTOR3D_H
2003-08-15 18:00:22 +00:00
#include "common/scummsys.h"
#include "common/endian.h"
namespace Graphics {
2005-08-08 07:05:58 +00:00
2003-08-15 18:00:22 +00:00
class Vector3d {
public:
2004-12-09 23:55:43 +00:00
float _coords[3]; // Make sure this stays as an array so
2004-12-09 23:55:43 +00:00
float& x() { return _coords[0]; }
float x() const { return _coords[0]; }
float& y() { return _coords[1]; }
float y() const { return _coords[1]; }
float& z() { return _coords[2]; }
float z() const { return _coords[2]; }
Vector3d() { x() = 0; y() = 0; z() = 0; }
Vector3d(float lx, float ly, float lz) {
x() = lx; y() = ly; z() = lz;
}
2004-12-09 23:55:43 +00:00
Vector3d(const Vector3d &v) {
x() = v.x(); y() = v.y(); z() = v.z();
}
void set(float lx, float ly, float lz) {
x() = lx; y() = ly; z() = lz;
}
Vector3d& operator =(const Vector3d &v) {
x() = v.x(); y() = v.y(); z() = v.z();
return *this;
}
bool operator ==(const Vector3d &v) {
return ( (x() == v.x()) && (y() == v.y()) && (z() == v.z()) );
}
bool operator !=(const Vector3d &v) {
return ( (x() != v.x()) || (y() != v.y()) || (z() != v.z()) );
}
Vector3d& operator +=(const Vector3d &v) {
x() += v.x(); y() += v.y(); z() += v.z();
return *this;
}
Vector3d& operator -=(const Vector3d &v) {
x() -= v.x(); y() -= v.y(); z() -= v.z();
return *this;
}
Vector3d& operator *=(float s) {
x() *= s; y() *= s; z() *= s;
return *this;
}
Vector3d& operator /=(float s) {
x() /= s; y() /= s; z() /= s;
return *this;
}
float magnitude() const {
2009-05-10 14:15:07 +00:00
return sqrt(x() * x() + y() * y() + z() * z());
}
// Get the angle a vector is around the unit circle
// (ignores z-component)
float unitCircleAngle() const {
const float mag = sqrt(x() * x() + y() * y());
float a = x() / mag;
float b = y() / mag;
float yaw;
2009-05-09 17:47:28 +00:00
// find the angle on the upper half of the unit circle
2009-05-10 14:15:07 +00:00
yaw = acos(a) * (180.0f / LOCAL_PI);
if (b < 0.0f)
// adjust for the lower half of the unit circle
return 360.0f - yaw;
else
// no adjustment, angle is on the upper half
return yaw;
}
2009-07-01 19:23:58 +00:00
void normalize() {
float len = sqrt(dotProduct(x(), y(), z()));
if (len != 0.0f) {
float t = 1.0f / len;
2011-04-21 16:08:34 +00:00
set(x() * t, y() * t, z() * t);
2009-07-01 19:23:58 +00:00
}
}
2009-05-17 08:57:38 +00:00
float dotProduct(float sx, float sy, float sz) {
return x() * sx + y() * sy + z() * sz;
}
bool isZero() {
2009-05-17 08:57:38 +00:00
if (x() == 0.f && y() == 0.f && z() == 0.f)
return true;
return false;
}
2003-08-15 18:00:22 +00:00
};
inline float dot(const Vector3d& v1, const Vector3d& v2) {
return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
2003-08-15 18:00:22 +00:00
}
inline Vector3d cross(const Vector3d& v1, const Vector3d& v2) {
return Vector3d(v1.y() * v2.z() - v1.z() * v2.y(),
v1.z() * v2.x() - v1.x() * v2.z(),
v1.x() * v2.y() - v1.y() * v2.x());
2003-08-15 18:00:22 +00:00
}
inline float angle(const Vector3d& v1, const Vector3d& v2) {
2009-05-10 14:15:07 +00:00
return acos(dot(v1, v2) / (v1.magnitude() * v2.magnitude()));
2003-08-15 18:00:22 +00:00
}
inline Vector3d operator +(const Vector3d& v1, const Vector3d& v2) {
Vector3d result = v1;
result += v2;
return result;
2003-08-15 18:00:22 +00:00
}
inline Vector3d operator -(const Vector3d& v1, const Vector3d& v2) {
Vector3d result = v1;
result -= v2;
return result;
2003-08-15 18:00:22 +00:00
}
inline Vector3d operator *(float s, const Vector3d& v) {
Vector3d result = v;
result *= s;
return result;
2003-08-15 18:00:22 +00:00
}
inline Vector3d operator -(const Vector3d& v) {
return (-1.0f) * v;
}
2003-08-15 18:00:22 +00:00
inline Vector3d operator *(const Vector3d& v, float s) {
return s * v;
2003-08-15 18:00:22 +00:00
}
inline Vector3d operator /(const Vector3d& v, float s) {
Vector3d result = v;
result /= s;
return result;
2003-08-15 18:00:22 +00:00
}
inline bool operator ==(const Vector3d& v1, const Vector3d& v2) {
return v1.x() == v2.x() && v1.y() == v2.y() && v1.z() == v2.z();
2003-08-15 18:00:22 +00:00
}
inline Vector3d get_vector3d(const char *data) {
return Vector3d(get_float(data), get_float(data + 4), get_float(data + 8));
}
} // end of namespace Graphics
2003-08-15 18:00:22 +00:00
#endif