Mach1 Spatial SDK
Loading...
Searching...
No Matches
Mach1Point3D.h
1#ifndef MACH1POINT3D_H
2#define MACH1POINT3D_H
3
4// Mach1 Spatial SDK
5// Copyright © 2017 Mach1. All rights reserved.
6
7#include <cmath>
8
9#ifndef DEG_TO_RAD
10# define DEG_TO_RAD (PI / 180.0f)
11#endif
12
13#ifndef PI
14# define PI 3.14159265358979323846f
15#endif
16
17typedef struct Mach1Point3D {
18 float x, y, z;
19
20 bool operator==(const Mach1Point3D &p2) {
21 return (this->x == p2.x && this->y == p2.y && this->z == p2.z);
22 }
23
24 bool operator!=(const Mach1Point3D &p2) {
25 return !(*this == p2);
26 }
27
28 float length() const {
29 return (float)sqrtf(x * x + y * y + z * z);
30 }
31
32 float operator[](int index) {
33 float arr[3] = {x, y, z};
34 return arr[index];
35 }
36
37 Mach1Point3D operator+(const Mach1Point3D &pnt) {
38 Mach1Point3D p = {0, 0, 0}; // initialize for stricter compilers
39 p.x = this->x + pnt.x;
40 p.y = this->y + pnt.y;
41 p.z = this->z + pnt.z;
42 return p;
43 }
44
45 Mach1Point3D operator*(const float f) {
46 Mach1Point3D p = {0, 0, 0}; // initialize for stricter compilers
47 p.x = this->x * f;
48 p.y = this->y * f;
49 p.z = this->z * f;
50 return p;
51 }
52
53 Mach1Point3D operator*(const Mach1Point3D &pnt) {
54 Mach1Point3D p = {0, 0, 0}; // initialize for stricter compilers
55 p.x = this->x * pnt.x;
56 p.y = this->y * pnt.y;
57 p.z = this->z * pnt.z;
58 return p;
59 }
60
61 Mach1Point3D operator-(const Mach1Point3D &pnt) {
62 Mach1Point3D p = {0, 0, 0}; // initialize for stricter compilers
63 p.x = this->x - pnt.x;
64 p.y = this->y - pnt.y;
65 p.z = this->z - pnt.z;
66 return p;
67 }
68
69 Mach1Point3D operator/(float f) {
70 return {this->x / f, this->y / f, this->z / f};
71 }
72
73 Mach1Point3D operator-() {
74 return {-this->x, -this->y, -this->z};
75 }
76
77 Mach1Point3D rotate(float angle, const Mach1Point3D &axis) {
78 Mach1Point3D ax = axis.getNormalized();
79 float a = (float)(angle * DEG_TO_RAD);
80 float sina = sinf(a);
81 float cosa = cosf(a);
82 float cosb = 1.0f - cosa;
83
84 Mach1Point3D p = {0, 0, 0}; // initialize for stricter compilers
85 float nx = this->x * (ax.x * ax.x * cosb + cosa) + this->y * (ax.x * ax.y * cosb - ax.z * sina) + this->z * (ax.x * ax.z * cosb + ax.y * sina);
86 float ny = this->x * (ax.y * ax.x * cosb + ax.z * sina) + this->y * (ax.y * ax.y * cosb + cosa) + this->z * (ax.y * ax.z * cosb - ax.x * sina);
87 float nz = this->x * (ax.z * ax.x * cosb - ax.y * sina) + this->y * (ax.z * ax.y * cosb + ax.x * sina) + this->z * (ax.z * ax.z * cosb + cosa);
88 p.x = nx;
89 p.y = ny;
90 p.z = nz;
91 return p;
92 }
93
94 Mach1Point3D normalize() {
95 Mach1Point3D p = {0, 0, 0}; // initialize for stricter compilers
96 float length = (float)sqrtf(this->x * this->x + this->y * this->y + this->z * this->z);
97 if (length > 0) {
98 p.x /= length;
99 p.y /= length;
100 p.z /= length;
101 } else {
102 // error
103 }
104 return p;
105 }
106
107 Mach1Point3D getNormalized() const {
108 float length = (float)sqrtf(x * x + y * y + z * z);
109 if (length > 0) {
110 Mach1Point3D p;
111 p.x = this->x / length;
112 p.y = this->y / length;
113 p.z = this->z / length;
114 return p;
115 } else {
116 Mach1Point3D p;
117 p.x = 0;
118 p.y = 0;
119 p.z = 0;
120 return p;
121 }
122 }
123
124 Mach1Point3D getRotated(float angle, const Mach1Point3D &axis) const {
125 Mach1Point3D ax = axis.getNormalized();
126 float a = (float)(angle * DEG_TO_RAD);
127 float sina = sinf(a);
128 float cosa = cosf(a);
129 float cosb = 1.0f - cosa;
130
131 Mach1Point3D p;
132 p.x = this->x * (ax.x * ax.x * cosb + cosa) + this->y * (ax.x * ax.y * cosb - ax.z * sina) + this->z * (ax.x * ax.z * cosb + ax.y * sina);
133 p.y = this->x * (ax.y * ax.x * cosb + ax.z * sina) + this->y * (ax.y * ax.y * cosb + cosa) + this->z * (ax.y * ax.z * cosb - ax.x * sina);
134 p.z = this->x * (ax.z * ax.x * cosb - ax.y * sina) + this->y * (ax.z * ax.y * cosb + ax.x * sina) + this->z * (ax.z * ax.z * cosb + cosa);
135 return p;
136 }
137
138 Mach1Point3D getCrossed(const Mach1Point3D &b) const {
139 Mach1Point3D p;
140 p.x = this->y * b.z - this->z * b.y;
141 p.y = this->z * b.x - this->x * b.z;
142 p.z = this->x * b.y - this->y * b.x;
143 return p;
144 }
145
146 float dot(const Mach1Point3D &vec1, const Mach1Point3D &vec2) {
147 return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
148 }
149
150 float distance(const Mach1Point3D &vec1, const Mach1Point3D &vec2) {
151 return sqrt(powf(vec1.x - vec2.x, 2) + powf(vec1.y - vec2.y, 2) + powf(vec1.z - vec2.z, 2));
152 }
153
154 Mach1Point3D Mach1Point3D_create() {
155 Mach1Point3D p;
156 p.x = 0;
157 p.y = 0;
158 p.z = 0;
159 return p;
160 }
161 Mach1Point3D Mach1Point3D_create(float X, float Y) {
162 Mach1Point3D p;
163 p.x = X;
164 p.y = Y;
165 p.z = 0;
166 return p;
167 }
168 Mach1Point3D Mach1Point3D_create(float X, float Y, float Z) {
169 Mach1Point3D p;
170 p.x = X;
171 p.y = Y;
172 p.z = Z;
173 return p;
174 }
176
177#endif // MACH1POINT3D_H
Definition Mach1Point3D.h:17