added comments, increased performance

This commit is contained in:
h4570
2020-12-24 10:33:30 +01:00
parent 5a932b35ca
commit 8589a298da
2 changed files with 180 additions and 200 deletions
+80 -19
View File
@@ -12,6 +12,7 @@
#define _TYRA_VECTOR3_
#include <tamtypes.h>
#include <stdio.h>
class Math; // Forward definition
@@ -20,6 +21,7 @@ class Vector3
{
public:
/** This trick will allow use to use vec.x and vex[0] */
union
{
struct
@@ -31,32 +33,91 @@ public:
float xyz[3] __attribute__((__aligned__(16)));
};
Vector3(float t_x, float t_y, float t_z);
Vector3(const Vector3 &v);
// ----
// Constructors/Destructors
// ----
/** Create empty vector. */
Vector3();
~Vector3();
Vector3 operator+(Vector3 v);
Vector3 operator-(const Vector3 &v);
Vector3 operator*(Vector3 &v);
Vector3 operator*(const float &t);
/** Create vector with given vector values. */
Vector3(const Vector3 &t_v) { set(t_v); }
/** Create vector with given values. */
Vector3(const float &t_x, const float &t_y, const float &t_z) { set(t_x, t_y, t_z); }
// ----
// Operators
// ----
Vector3 operator+(const Vector3 &v) const;
Vector3 operator-(const Vector3 &v) const;
/** Also called "cross product" */
Vector3 operator*(const Vector3 &v) const;
Vector3 operator*(const float &t) const;
Vector3 operator/(const float &t) const;
Vector3 operator-(void) const { return Vector3(-x, -y, -z); }
void operator+=(const Vector3 &t);
void operator*=(const float &t);
Vector3 operator/(float t);
Vector3 operator-(void);
static u8 shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *v0, const Vector3 *v1, const Vector3 *v2);
u8 collidesSquare(const Vector3 &t_min, const Vector3 &t_max) const;
u8 isOnSquare(const Vector3 &t_min, const Vector3 &t_max) const;
float length();
void normalize();
void setByLerp(const Vector3 &v1, const Vector3 &v2, const float &t_interp, const float &t_scale);
float innerProduct(Vector3 &v);
// ----
// Functions
// ----
/** Set vector values via VU0. */
void set(const Vector3 &v);
void rotate(const Vector3 &v, u8 inversed = false);
/** Set vector values. */
void set(const float &t_x, const float &t_y, const float &t_z);
void copy(Vector3 &v);
/** Also called dot3. */
float innerProduct(const Vector3 &v) const;
/** Get vector length */
float length() const;
/** Normalize vector. */
void normalize();
/** Returns distance between two vectors */
float distanceTo(const Vector3 &v) const;
const void print() const;
/**
* Checks intersection with given box
* @param t_min Opposite min vertex (ex. near, left, down vertex of bounding box)
* @param t_max Opposite max vertex (ex. far, right, up vertex of bounding box)
*/
u8 collidesBox(const Vector3 &t_min, const Vector3 &t_max) const
{
return ((this->x <= t_max.x && this->x >= t_min.x) && (this->y < t_max.y && this->y >= t_min.y) && (this->z <= t_max.z && this->z >= t_min.z)) ? 1 : 0;
}
/**
* Checks if this vector is on given box (this->y >= box.y)
* @param t_min Opposite min vertex (ex. near, left, down vertex of bounding box)
* @param t_max Opposite max vertex (ex. far, right, up vertex of bounding box)
*/
u8 isOnBox(const Vector3 &t_min, const Vector3 &t_max) const
{
return ((this->x <= t_max.x && this->x >= t_min.x) && (this->y >= t_max.y) && (this->z <= t_max.z && this->z >= t_min.z)) ? 1 : 0;
}
/** Check if given triangle should be backface culled. */
static u8 shouldBeBackfaceCulled(const Vector3 *t_cameraPos, const Vector3 *t_v0, const Vector3 *t_v1, const Vector3 *t_v2);
/**
* Set by linear interpolation between two vertices (animation).
* @param t_v1 Before vertex
* @param t_v2 After vertex
* @param t_interp State of interpolation
* @param t_scale Scale
*/
void setByLerp(const Vector3 &t_v1, const Vector3 &t_v2, const float &t_interp, const float &t_scale);
const void print() const
{
printf("Vector3(%f, %f, %f)\n", x, y, z);
}
};
#endif