Refactored Vector3 to class
This commit is contained in:
parent
ea8f7f6fb1
commit
5054d3583d
11 changed files with 251 additions and 171 deletions
|
@ -1,15 +1,33 @@
|
|||
#include "Vector3.h"
|
||||
#include "Vector3.inline.cpp"
|
||||
|
||||
#include <math.h>
|
||||
#include <cmath>
|
||||
#include "PackStream.h"
|
||||
|
||||
Vector3 VECTOR_ZERO = {0.0, 0.0, 0.0};
|
||||
Vector3 VECTOR_DOWN = {0.0, -1.0, 0.0};
|
||||
Vector3 VECTOR_UP = {0.0, 1.0, 0.0};
|
||||
Vector3 VECTOR_NORTH = {0.0, 0.0, -1.0};
|
||||
Vector3 VECTOR_SOUTH = {0.0, 0.0, 1.0};
|
||||
Vector3 VECTOR_WEST = {-1.0, 0.0, 0.0};
|
||||
Vector3 VECTOR_EAST = {1.0, 0.0, 0.0};
|
||||
const Vector3 paysages::basics::VECTOR_ZERO(0.0, 0.0, 0.0);
|
||||
const Vector3 paysages::basics::VECTOR_DOWN(0.0, -1.0, 0.0);
|
||||
const Vector3 paysages::basics::VECTOR_UP(0.0, 1.0, 0.0);
|
||||
const Vector3 paysages::basics::VECTOR_NORTH(0.0, 0.0, -1.0);
|
||||
const Vector3 paysages::basics::VECTOR_SOUTH(0.0, 0.0, 1.0);
|
||||
const Vector3 paysages::basics::VECTOR_WEST(-1.0, 0.0, 0.0);
|
||||
const Vector3 paysages::basics::VECTOR_EAST(1.0, 0.0, 0.0);
|
||||
|
||||
Vector3::Vector3(const VectorSpherical &v):
|
||||
x(v.r * cos(v.phi) * cos(v.theta)), y(v.r * sin(v.theta)), z(-v.r * sin(v.phi) * cos(v.theta))
|
||||
{
|
||||
}
|
||||
|
||||
void Vector3::save(PackStream* stream) const
|
||||
{
|
||||
stream->write(&x);
|
||||
stream->write(&y);
|
||||
stream->write(&z);
|
||||
}
|
||||
void Vector3::load(PackStream* stream)
|
||||
{
|
||||
stream->read(&x);
|
||||
stream->read(&y);
|
||||
stream->read(&z);
|
||||
}
|
||||
|
||||
static inline double _euclidGet2DAngle(double x, double y)
|
||||
{
|
||||
|
@ -43,114 +61,18 @@ static inline double _euclidGet2DAngle(double x, double y)
|
|||
}
|
||||
return ret < 0.0 ? ret + 2.0 * M_PI : ret;
|
||||
}
|
||||
void v3Save(PackStream* stream, Vector3* v)
|
||||
{
|
||||
stream->write(&v->x);
|
||||
stream->write(&v->y);
|
||||
stream->write(&v->z);
|
||||
}
|
||||
|
||||
void v3Load(PackStream* stream, Vector3* v)
|
||||
{
|
||||
stream->read(&v->x);
|
||||
stream->read(&v->y);
|
||||
stream->read(&v->z);
|
||||
}
|
||||
|
||||
Vector3 v3Translate(Vector3 v1, double x, double y, double z)
|
||||
{
|
||||
Vector3 result;
|
||||
result.x = v1.x + x;
|
||||
result.y = v1.y + y;
|
||||
result.z = v1.z + z;
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3 v3Add(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
Vector3 result;
|
||||
result.x = v1.x + v2.x;
|
||||
result.y = v1.y + v2.y;
|
||||
result.z = v1.z + v2.z;
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3 v3Sub(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
Vector3 result;
|
||||
result.x = v1.x - v2.x;
|
||||
result.y = v1.y - v2.y;
|
||||
result.z = v1.z - v2.z;
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3 v3Neg(Vector3 v)
|
||||
{
|
||||
Vector3 result;
|
||||
result.x = -v.x;
|
||||
result.y = -v.y;
|
||||
result.z = -v.z;
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3 v3Scale(Vector3 v, double scale)
|
||||
{
|
||||
Vector3 result;
|
||||
result.x = v.x * scale;
|
||||
result.y = v.y * scale;
|
||||
result.z = v.z * scale;
|
||||
return result;
|
||||
}
|
||||
|
||||
double v3Norm(Vector3 v)
|
||||
{
|
||||
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
|
||||
}
|
||||
|
||||
Vector3 v3Normalize(Vector3 v)
|
||||
{
|
||||
double norm = v3Norm(v);
|
||||
if (norm == 0.0)
|
||||
{
|
||||
return VECTOR_ZERO;
|
||||
}
|
||||
else
|
||||
{
|
||||
return v3Scale(v, 1.0 / norm);
|
||||
}
|
||||
}
|
||||
|
||||
double v3Dot(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||
}
|
||||
|
||||
Vector3 v3Cross(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
Vector3 result;
|
||||
result.x = v1.y * v2.z - v1.z * v2.y;
|
||||
result.y = v1.z * v2.x - v1.x * v2.z;
|
||||
result.z = v1.x * v2.y - v1.y * v2.x;
|
||||
return result;
|
||||
}
|
||||
|
||||
VectorSpherical v3ToSpherical(Vector3 v)
|
||||
VectorSpherical Vector3::toSpherical() const
|
||||
{
|
||||
VectorSpherical result;
|
||||
|
||||
result.phi = _euclidGet2DAngle(v.x, -v.z);
|
||||
result.theta = _euclidGet2DAngle(sqrt(v.x * v.x + v.z * v.z), v.y);
|
||||
if (v.y < 0.0)
|
||||
result.phi = _euclidGet2DAngle(x, -z);
|
||||
result.theta = _euclidGet2DAngle(sqrt(x * x + z * z), y);
|
||||
if (y < 0.0)
|
||||
{
|
||||
result.theta -= 2.0 * M_PI;
|
||||
}
|
||||
result.r = v3Norm(v);
|
||||
result.r = getNorm();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3 v3FromSpherical(VectorSpherical v)
|
||||
{
|
||||
Vector3 result = {v.r * cos(v.phi) * cos(v.theta), v.r * sin(v.theta), -v.r * sin(v.phi) * cos(v.theta)};
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
|
||||
#include "basics_global.h"
|
||||
|
||||
namespace paysages {
|
||||
namespace basics {
|
||||
|
||||
/*
|
||||
* Cartesian coordinates (X, Y, Z) - right handed :
|
||||
*
|
||||
|
@ -30,17 +33,6 @@
|
|||
* X=0 Y=-1 Z=0 => THETA=-PI/2
|
||||
*/
|
||||
|
||||
namespace paysages {
|
||||
namespace system {class PackStream;}
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
} Vector3;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
double r;
|
||||
|
@ -48,31 +40,111 @@ typedef struct
|
|||
double phi;
|
||||
} VectorSpherical;
|
||||
|
||||
BASICSSHARED_EXPORT extern Vector3 VECTOR_ZERO;
|
||||
BASICSSHARED_EXPORT extern Vector3 VECTOR_DOWN;
|
||||
BASICSSHARED_EXPORT extern Vector3 VECTOR_UP;
|
||||
BASICSSHARED_EXPORT extern Vector3 VECTOR_NORTH;
|
||||
BASICSSHARED_EXPORT extern Vector3 VECTOR_SOUTH;
|
||||
BASICSSHARED_EXPORT extern Vector3 VECTOR_EAST;
|
||||
BASICSSHARED_EXPORT extern Vector3 VECTOR_WEST;
|
||||
class BASICSSHARED_EXPORT Vector3
|
||||
{
|
||||
public:
|
||||
Vector3();
|
||||
Vector3(double x, double y, double z);
|
||||
Vector3(const VectorSpherical &v);
|
||||
|
||||
void save(PackStream *stream) const;
|
||||
void load(PackStream *stream);
|
||||
|
||||
Vector3 inverse() const;
|
||||
Vector3 scale(double scaling) const;
|
||||
double getNorm() const;
|
||||
Vector3 normalize() const;
|
||||
|
||||
Vector3 add(double x, double y, double z) const;
|
||||
Vector3 add(const Vector3 &other) const;
|
||||
Vector3 sub(const Vector3 &other) const;
|
||||
double dotProduct(const Vector3 &other) const;
|
||||
Vector3 crossProduct(const Vector3 &other) const;
|
||||
|
||||
VectorSpherical toSpherical() const;
|
||||
|
||||
public:
|
||||
// TODO Make private
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
};
|
||||
|
||||
BASICSSHARED_EXPORT extern const Vector3 VECTOR_ZERO;
|
||||
BASICSSHARED_EXPORT extern const Vector3 VECTOR_DOWN;
|
||||
BASICSSHARED_EXPORT extern const Vector3 VECTOR_UP;
|
||||
BASICSSHARED_EXPORT extern const Vector3 VECTOR_NORTH;
|
||||
BASICSSHARED_EXPORT extern const Vector3 VECTOR_SOUTH;
|
||||
BASICSSHARED_EXPORT extern const Vector3 VECTOR_EAST;
|
||||
BASICSSHARED_EXPORT extern const Vector3 VECTOR_WEST;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Inlining
|
||||
#if PAYSAGES_USE_INLINING
|
||||
#ifndef VECTOR3_INLINE_CPP
|
||||
#include "Vector3.inline.cpp"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Compat API
|
||||
|
||||
static inline Vector3 v3(double x, double y, double z)
|
||||
{
|
||||
Vector3 result = {x, y, z};
|
||||
return result;
|
||||
return Vector3(x, y, z);
|
||||
}
|
||||
static inline void v3Save(PackStream* stream, Vector3* v)
|
||||
{
|
||||
v->save(stream);
|
||||
}
|
||||
static inline void v3Load(PackStream* stream, Vector3* v)
|
||||
{
|
||||
v->load(stream);
|
||||
}
|
||||
static inline Vector3 v3Translate(const Vector3 &v1, double x, double y, double z)
|
||||
{
|
||||
return v1.add(x, y, z);
|
||||
}
|
||||
static inline Vector3 v3Add(const Vector3 &v1, const Vector3 &v2)
|
||||
{
|
||||
return v1.add(v2);
|
||||
}
|
||||
static inline Vector3 v3Sub(const Vector3 &v1, const Vector3 &v2)
|
||||
{
|
||||
return v1.sub(v2);
|
||||
}
|
||||
static inline Vector3 v3Neg(const Vector3 &v)
|
||||
{
|
||||
return v.inverse();
|
||||
}
|
||||
static inline Vector3 v3Scale(const Vector3 &v, double scale)
|
||||
{
|
||||
return v.scale(scale);
|
||||
}
|
||||
static inline double v3Norm(const Vector3 &v)
|
||||
{
|
||||
return v.getNorm();
|
||||
}
|
||||
static inline Vector3 v3Normalize(const Vector3 &v)
|
||||
{
|
||||
return v.normalize();
|
||||
}
|
||||
static inline double v3Dot(const Vector3 &v1, const Vector3 &v2)
|
||||
{
|
||||
return v1.dotProduct(v2);
|
||||
}
|
||||
static inline Vector3 v3Cross(const Vector3 &v1, const Vector3 &v2)
|
||||
{
|
||||
return v1.crossProduct(v2);
|
||||
}
|
||||
static inline VectorSpherical v3ToSpherical(const Vector3 &v)
|
||||
{
|
||||
return v.toSpherical();
|
||||
}
|
||||
static inline Vector3 v3FromSpherical(const VectorSpherical &v)
|
||||
{
|
||||
return Vector3(v);
|
||||
}
|
||||
BASICSSHARED_EXPORT void v3Save(PackStream* stream, Vector3* v);
|
||||
BASICSSHARED_EXPORT void v3Load(PackStream* stream, Vector3* v);
|
||||
BASICSSHARED_EXPORT Vector3 v3Translate(Vector3 v1, double x, double y, double z);
|
||||
BASICSSHARED_EXPORT Vector3 v3Add(Vector3 v1, Vector3 v2);
|
||||
BASICSSHARED_EXPORT Vector3 v3Sub(Vector3 v1, Vector3 v2);
|
||||
BASICSSHARED_EXPORT Vector3 v3Neg(Vector3 v);
|
||||
BASICSSHARED_EXPORT Vector3 v3Scale(Vector3 v, double scale);
|
||||
BASICSSHARED_EXPORT double v3Norm(Vector3 v);
|
||||
BASICSSHARED_EXPORT Vector3 v3Normalize(Vector3 v);
|
||||
BASICSSHARED_EXPORT double v3Dot(Vector3 v1, Vector3 v2);
|
||||
BASICSSHARED_EXPORT Vector3 v3Cross(Vector3 v1, Vector3 v2);
|
||||
BASICSSHARED_EXPORT VectorSpherical v3ToSpherical(Vector3 v);
|
||||
BASICSSHARED_EXPORT Vector3 v3FromSpherical(VectorSpherical v);
|
||||
|
||||
#endif // VECTOR3_H
|
||||
|
|
78
src/basics/Vector3.inline.cpp
Normal file
78
src/basics/Vector3.inline.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
#define VECTOR3_INLINE_CPP
|
||||
|
||||
#ifdef VECTOR3_H
|
||||
# define METHSPEC inline
|
||||
#else
|
||||
# include "Vector3.h"
|
||||
# define METHSPEC
|
||||
#endif
|
||||
|
||||
#include <cmath>
|
||||
|
||||
METHSPEC Vector3::Vector3(double x, double y, double z):
|
||||
x(x), y(y), z(z)
|
||||
{
|
||||
}
|
||||
|
||||
METHSPEC Vector3::Vector3():
|
||||
x(0.0), y(0.0), z(0.0)
|
||||
{
|
||||
}
|
||||
|
||||
METHSPEC Vector3 Vector3::add(double x, double y, double z) const
|
||||
{
|
||||
return Vector3(this->x + x, this->y + y, this->z + z);
|
||||
}
|
||||
|
||||
METHSPEC Vector3 Vector3::add(const Vector3 &other) const
|
||||
{
|
||||
return Vector3(x + other.x, y + other.y, z + other.z);
|
||||
}
|
||||
|
||||
METHSPEC Vector3 Vector3::sub(const Vector3 &other) const
|
||||
{
|
||||
return Vector3(x - other.x, y - other.y, z - other.z);
|
||||
}
|
||||
|
||||
METHSPEC Vector3 Vector3::inverse() const
|
||||
{
|
||||
return Vector3(-x, -y, -z);
|
||||
}
|
||||
|
||||
METHSPEC Vector3 Vector3::scale(double scaling) const
|
||||
{
|
||||
return Vector3(x * scaling, y * scaling, z * scaling);
|
||||
}
|
||||
|
||||
METHSPEC double Vector3::getNorm() const
|
||||
{
|
||||
return sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
METHSPEC Vector3 Vector3::normalize() const
|
||||
{
|
||||
double norm = sqrt(x * x + y * y + z * z);
|
||||
if (norm == 0.0)
|
||||
{
|
||||
return VECTOR_ZERO;
|
||||
}
|
||||
else
|
||||
{
|
||||
norm = 1.0 / norm;
|
||||
return Vector3(x * norm, y * norm, z * norm);
|
||||
}
|
||||
}
|
||||
|
||||
METHSPEC double Vector3:: dotProduct(const Vector3 &other) const
|
||||
{
|
||||
return x * other.x + y * other.y + z * other.z;
|
||||
}
|
||||
|
||||
METHSPEC Vector3 Vector3::crossProduct(const Vector3 &other) const
|
||||
{
|
||||
return Vector3(
|
||||
y * other.z - z * other.y,
|
||||
z * other.x - x * other.z,
|
||||
x * other.y - y * other.x
|
||||
);
|
||||
}
|
|
@ -20,7 +20,8 @@ SOURCES += \
|
|||
NoiseFunctionSimplex.cpp \
|
||||
Interpolation.cpp \
|
||||
SpaceCoordinates.cpp \
|
||||
Vector3.cpp
|
||||
Vector3.cpp \
|
||||
Vector3.inline.cpp
|
||||
|
||||
HEADERS +=\
|
||||
basics_global.h \
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#ifndef BASICS_GLOBAL_H
|
||||
#define BASICS_GLOBAL_H
|
||||
|
||||
/* Shared object helpers */
|
||||
#include <QtCore/qglobal.h>
|
||||
#if defined(BASICS_LIBRARY)
|
||||
# define BASICSSHARED_EXPORT Q_DECL_EXPORT
|
||||
|
@ -9,13 +8,13 @@
|
|||
# define BASICSSHARED_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
/* Namespace using */
|
||||
namespace paysages
|
||||
{
|
||||
namespace system {}
|
||||
namespace basics {}
|
||||
#include "system_global.h"
|
||||
|
||||
namespace paysages {
|
||||
namespace basics {
|
||||
class Vector3;
|
||||
}
|
||||
}
|
||||
using namespace paysages::system;
|
||||
using namespace paysages::basics;
|
||||
|
||||
#endif // BASICS_GLOBAL_H
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
#QMAKE_CXXFLAGS = -std=c++11 -stdlib=libc++
|
||||
#QMAKE_LFLAGS = -std=c++11 -stdlib=libc++
|
||||
CONFIG(release, debug|release): DEFINES += NDEBUG
|
||||
|
||||
QMAKE_CXXFLAGS = -std=c++11
|
||||
|
|
|
@ -207,7 +207,7 @@ void cameraSetLocation(CameraDefinition* camera, Vector3 location)
|
|||
|
||||
void cameraSetLocationCoords(CameraDefinition* camera, double x, double y, double z)
|
||||
{
|
||||
Vector3 v = {x, y, z};
|
||||
Vector3 v(x, y, z);
|
||||
cameraSetLocation(camera, v);
|
||||
}
|
||||
|
||||
|
@ -228,7 +228,7 @@ void cameraSetTarget(CameraDefinition* camera, Vector3 target)
|
|||
|
||||
void cameraSetTargetCoords(CameraDefinition* camera, double x, double y, double z)
|
||||
{
|
||||
Vector3 v = {x, y, z};
|
||||
Vector3 v(x, y, z);
|
||||
cameraSetTarget(camera, v);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "Thread.h"
|
||||
#include "Mutex.h"
|
||||
#include "System.h"
|
||||
#include "Vector3.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
@ -21,7 +22,11 @@ typedef struct
|
|||
} flags;
|
||||
union
|
||||
{
|
||||
Vector3 location;
|
||||
struct {
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
} location;
|
||||
struct {
|
||||
double r;
|
||||
double g;
|
||||
|
@ -343,7 +348,9 @@ static void _pushFragment(RenderArea* area, int x, int y, double z, int edge, Ve
|
|||
pixel_data->flags.dirty = (unsigned char)1;
|
||||
pixel_data->flags.edge = (unsigned char)edge;
|
||||
pixel_data->flags.callback = (unsigned char)callback;
|
||||
pixel_data->data.location = location;
|
||||
pixel_data->data.location.x = location.x;
|
||||
pixel_data->data.location.y = location.y;
|
||||
pixel_data->data.location.z = location.z;
|
||||
pixel_data->z = z;
|
||||
_setDirtyPixel(area, x, y);
|
||||
}
|
||||
|
@ -617,7 +624,8 @@ void* _renderPostProcessChunk(void* data)
|
|||
callback = chunk->area->fragment_callbacks[fragment->flags.callback];
|
||||
if (callback.function)
|
||||
{
|
||||
col = callback.function(chunk->area->renderer, fragment->data.location, callback.data);
|
||||
Vector3 location(fragment->data.location.x, fragment->data.location.y, fragment->data.location.z);
|
||||
col = callback.function(chunk->area->renderer, location, callback.data);
|
||||
/*colorNormalize(&col);*/
|
||||
}
|
||||
else
|
||||
|
|
|
@ -37,7 +37,7 @@ bool PackStream::bindToFile(const char* filepath, bool write)
|
|||
return stream != NULL;
|
||||
}
|
||||
|
||||
void PackStream::write(int* value)
|
||||
void PackStream::write(const int*value)
|
||||
{
|
||||
if (stream and value)
|
||||
{
|
||||
|
@ -45,7 +45,7 @@ void PackStream::write(int* value)
|
|||
}
|
||||
}
|
||||
|
||||
void PackStream::write(double* value)
|
||||
void PackStream::write(const double *value)
|
||||
{
|
||||
if (stream and value)
|
||||
{
|
||||
|
@ -53,7 +53,7 @@ void PackStream::write(double* value)
|
|||
}
|
||||
}
|
||||
|
||||
void PackStream::write(char* value, int max_length)
|
||||
void PackStream::write(const char *value, int max_length)
|
||||
{
|
||||
if (stream and value)
|
||||
{
|
||||
|
@ -62,7 +62,7 @@ void PackStream::write(char* value, int max_length)
|
|||
}
|
||||
}
|
||||
|
||||
void PackStream::write(QString value)
|
||||
void PackStream::write(const QString &value)
|
||||
{
|
||||
if (stream)
|
||||
{
|
||||
|
|
|
@ -22,10 +22,10 @@ public:
|
|||
|
||||
bool bindToFile(const char* filepath, bool write=false);
|
||||
|
||||
void write(int* value);
|
||||
void write(double* value);
|
||||
void write(char* value, int max_length);
|
||||
void write(QString value);
|
||||
void write(const int *value);
|
||||
void write(const double *value);
|
||||
void write(const char *value, const int max_length);
|
||||
void write(const QString &value);
|
||||
|
||||
void read(int* value);
|
||||
void read(double* value);
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#ifndef SYSTEM_GLOBAL_H
|
||||
#define SYSTEM_GLOBAL_H
|
||||
|
||||
/* Shared object helpers */
|
||||
#define PAYSAGES_USE_INLINING 1
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
#if defined(SYSTEM_LIBRARY)
|
||||
# define SYSTEMSHARED_EXPORT Q_DECL_EXPORT
|
||||
|
@ -9,10 +10,10 @@
|
|||
# define SYSTEMSHARED_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
/* Namespace using */
|
||||
namespace paysages
|
||||
{
|
||||
namespace system {}
|
||||
namespace paysages {
|
||||
namespace system {
|
||||
class PackStream;
|
||||
}
|
||||
}
|
||||
using namespace paysages::system;
|
||||
|
||||
|
|
Loading…
Reference in a new issue