summaryrefslogtreecommitdiff
path: root/src/SSystem/SComponent/c_xyz.cpp
blob: 9f05b9e61cf68c462e96e9b72d3f383513eefd7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * c_xyz.cpp
 *
 */

#include "SSystem/SComponent/c_xyz.h"
#include "SSystem/SComponent/c_math.h"
#include "JSystem/JUtility/JUTAssert.h"

#ifndef __MWERKS__
#include <limits>
#define FLT_EPSILON std::numeric_limits<float>::epsilon()
#endif

cXyz cXyz::operator+(const Vec& vec) const {
    Vec ret;
    PSVECAdd(this, &vec, &ret);
    return ret;
}

cXyz cXyz::operator-(const Vec& vec) const {
    Vec ret;
    PSVECSubtract(this, &vec, &ret);
    return ret;
}

cXyz cXyz::operator*(f32 scale) const {
    Vec ret;
    PSVECScale(this, &ret, scale);
    return ret;
}

cXyz cXyz::operator*(const Vec& vec) const {
    cXyz ret;
    ret.x = this->x * vec.x;
    ret.y = this->y * vec.y;
    ret.z = this->z * vec.z;
    return ret;
}

cXyz cXyz::operator/(f32 scale) const {
    Vec ret;
    PSVECScale(this, &ret, 1.0f / scale);
    return ret;
}

cXyz cXyz::getCrossProduct(const Vec& vec) const {
    Vec ret;
    PSVECCrossProduct(this, &vec, &ret);
    return ret;
}

cXyz cXyz::outprod(const Vec& vec) const {
    return this->getCrossProduct(vec);
}

cXyz cXyz::norm() const {
    Vec ret;
    JUT_CONFIRM(251, isNearZeroSquare() == FALSE);
    PSVECNormalize(this, &ret);
    return ret;
}

cXyz cXyz::normZP() const {
    Vec vec;
    if (this->isNearZeroSquare() == false) {
        PSVECNormalize(this, &vec);
    } else {
        vec = cXyz::Zero;
    }
    return vec;
}

cXyz cXyz::normZC() const {
    Vec outVec;
    if (isNearZeroSquare() == 0) {
        PSVECNormalize(this, &outVec);
    } else {
        outVec = (*this * 1.25f * 1000000.0f).normZP();

        if (isNearZeroSquare(outVec)) {
            outVec.x = 0.0f;
            outVec.y = 0.0f;
            outVec.z = 1.0f;
            outVec = (Vec){0,0,1};
        }
    }

    return outVec;
}

cXyz cXyz::normalize() {
    JUT_ASSERT(285, isNearZeroSquare() == FALSE);
    PSVECNormalize(this, this);
    return *this;
}

cXyz cXyz::normalizeZP() {
    if (this->isNearZeroSquare() == false) {
        PSVECNormalize(this, this);
    } else {
        *this = cXyz::Zero;
    }
    return *this;
}

bool cXyz::normalizeRS() {
    if (this->isNearZeroSquare()) {
        return false;
    } else {
        PSVECNormalize(this, this);
        return true;
    }
}

bool cXyz::operator==(const Vec& vec) const {
    return this->x == vec.x && this->y == vec.y && this->z == vec.z;
}

bool cXyz::operator!=(const Vec& vec) const {
    return this->x != vec.x || this->y != vec.y || this->z != vec.z;
}

bool cXyz::isZero() const {
    return fabsf(this->x) < 32.0f * FLT_EPSILON && fabsf(this->y) < 32.0f * FLT_EPSILON &&
           fabsf(this->z) < 32.0f * FLT_EPSILON;
}

s16 cXyz::atan2sX_Z() const {
    return cM_atan2s(this->x, this->z);
}

s16 cXyz::atan2sY_XZ() const {
    return cM_atan2s(-this->y, absXZ());
}

const cXyz cXyz::Zero(0, 0, 0);
const cXyz cXyz::BaseX(1, 0, 0);
const cXyz cXyz::BaseY(0, 1, 0);
const cXyz cXyz::BaseZ(0, 0, 1);
const cXyz cXyz::BaseXY(1, 1, 0);
const cXyz cXyz::BaseXZ(1, 0, 1);
const cXyz cXyz::BaseYZ(0, 1, 1);
const cXyz cXyz::BaseXYZ(1, 1, 1);