1 /*************************************************************************/
2 /* quat.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "gdnative/quat.h"
32
33 #include "core/math/quat.h"
34 #include "core/variant.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 static_assert(sizeof(godot_quat) == sizeof(Quat), "Quat size mismatch");
41
godot_quat_new(godot_quat * r_dest,const godot_real p_x,const godot_real p_y,const godot_real p_z,const godot_real p_w)42 void GDAPI godot_quat_new(godot_quat *r_dest, const godot_real p_x, const godot_real p_y, const godot_real p_z, const godot_real p_w) {
43
44 Quat *dest = (Quat *)r_dest;
45 *dest = Quat(p_x, p_y, p_z, p_w);
46 }
47
godot_quat_new_with_axis_angle(godot_quat * r_dest,const godot_vector3 * p_axis,const godot_real p_angle)48 void GDAPI godot_quat_new_with_axis_angle(godot_quat *r_dest, const godot_vector3 *p_axis, const godot_real p_angle) {
49 const Vector3 *axis = (const Vector3 *)p_axis;
50 Quat *dest = (Quat *)r_dest;
51 *dest = Quat(*axis, p_angle);
52 }
53
godot_quat_new_with_basis(godot_quat * r_dest,const godot_basis * p_basis)54 void GDAPI godot_quat_new_with_basis(godot_quat *r_dest, const godot_basis *p_basis) {
55 const Basis *basis = (const Basis *)p_basis;
56 Quat *dest = (Quat *)r_dest;
57 *dest = Quat(*basis);
58 }
59
godot_quat_new_with_euler(godot_quat * r_dest,const godot_vector3 * p_euler)60 void GDAPI godot_quat_new_with_euler(godot_quat *r_dest, const godot_vector3 *p_euler) {
61 const Vector3 *euler = (const Vector3 *)p_euler;
62 Quat *dest = (Quat *)r_dest;
63 *dest = Quat(*euler);
64 }
65
godot_quat_get_x(const godot_quat * p_self)66 godot_real GDAPI godot_quat_get_x(const godot_quat *p_self) {
67 const Quat *self = (const Quat *)p_self;
68 return self->x;
69 }
70
godot_quat_set_x(godot_quat * p_self,const godot_real val)71 void GDAPI godot_quat_set_x(godot_quat *p_self, const godot_real val) {
72 Quat *self = (Quat *)p_self;
73 self->x = val;
74 }
75
godot_quat_get_y(const godot_quat * p_self)76 godot_real GDAPI godot_quat_get_y(const godot_quat *p_self) {
77 const Quat *self = (const Quat *)p_self;
78 return self->y;
79 }
80
godot_quat_set_y(godot_quat * p_self,const godot_real val)81 void GDAPI godot_quat_set_y(godot_quat *p_self, const godot_real val) {
82 Quat *self = (Quat *)p_self;
83 self->y = val;
84 }
85
godot_quat_get_z(const godot_quat * p_self)86 godot_real GDAPI godot_quat_get_z(const godot_quat *p_self) {
87 const Quat *self = (const Quat *)p_self;
88 return self->z;
89 }
90
godot_quat_set_z(godot_quat * p_self,const godot_real val)91 void GDAPI godot_quat_set_z(godot_quat *p_self, const godot_real val) {
92 Quat *self = (Quat *)p_self;
93 self->z = val;
94 }
95
godot_quat_get_w(const godot_quat * p_self)96 godot_real GDAPI godot_quat_get_w(const godot_quat *p_self) {
97 const Quat *self = (const Quat *)p_self;
98 return self->w;
99 }
100
godot_quat_set_w(godot_quat * p_self,const godot_real val)101 void GDAPI godot_quat_set_w(godot_quat *p_self, const godot_real val) {
102 Quat *self = (Quat *)p_self;
103 self->w = val;
104 }
105
godot_quat_as_string(const godot_quat * p_self)106 godot_string GDAPI godot_quat_as_string(const godot_quat *p_self) {
107 godot_string ret;
108 const Quat *self = (const Quat *)p_self;
109 memnew_placement(&ret, String(*self));
110 return ret;
111 }
112
godot_quat_length(const godot_quat * p_self)113 godot_real GDAPI godot_quat_length(const godot_quat *p_self) {
114 const Quat *self = (const Quat *)p_self;
115 return self->length();
116 }
117
godot_quat_length_squared(const godot_quat * p_self)118 godot_real GDAPI godot_quat_length_squared(const godot_quat *p_self) {
119 const Quat *self = (const Quat *)p_self;
120 return self->length_squared();
121 }
122
godot_quat_normalized(const godot_quat * p_self)123 godot_quat GDAPI godot_quat_normalized(const godot_quat *p_self) {
124 godot_quat dest;
125 const Quat *self = (const Quat *)p_self;
126 *((Quat *)&dest) = self->normalized();
127 return dest;
128 }
129
godot_quat_is_normalized(const godot_quat * p_self)130 godot_bool GDAPI godot_quat_is_normalized(const godot_quat *p_self) {
131 const Quat *self = (const Quat *)p_self;
132 return self->is_normalized();
133 }
134
godot_quat_inverse(const godot_quat * p_self)135 godot_quat GDAPI godot_quat_inverse(const godot_quat *p_self) {
136 godot_quat dest;
137 const Quat *self = (const Quat *)p_self;
138 *((Quat *)&dest) = self->inverse();
139 return dest;
140 }
141
godot_quat_dot(const godot_quat * p_self,const godot_quat * p_b)142 godot_real GDAPI godot_quat_dot(const godot_quat *p_self, const godot_quat *p_b) {
143 const Quat *self = (const Quat *)p_self;
144 const Quat *b = (const Quat *)p_b;
145 return self->dot(*b);
146 }
147
godot_quat_xform(const godot_quat * p_self,const godot_vector3 * p_v)148 godot_vector3 GDAPI godot_quat_xform(const godot_quat *p_self, const godot_vector3 *p_v) {
149 godot_vector3 dest;
150 const Quat *self = (const Quat *)p_self;
151 const Vector3 *v = (const Vector3 *)p_v;
152 *((Vector3 *)&dest) = self->xform(*v);
153 return dest;
154 }
155
godot_quat_slerp(const godot_quat * p_self,const godot_quat * p_b,const godot_real p_t)156 godot_quat GDAPI godot_quat_slerp(const godot_quat *p_self, const godot_quat *p_b, const godot_real p_t) {
157 godot_quat dest;
158 const Quat *self = (const Quat *)p_self;
159 const Quat *b = (const Quat *)p_b;
160 *((Quat *)&dest) = self->slerp(*b, p_t);
161 return dest;
162 }
163
godot_quat_slerpni(const godot_quat * p_self,const godot_quat * p_b,const godot_real p_t)164 godot_quat GDAPI godot_quat_slerpni(const godot_quat *p_self, const godot_quat *p_b, const godot_real p_t) {
165 godot_quat dest;
166 const Quat *self = (const Quat *)p_self;
167 const Quat *b = (const Quat *)p_b;
168 *((Quat *)&dest) = self->slerpni(*b, p_t);
169 return dest;
170 }
171
godot_quat_cubic_slerp(const godot_quat * p_self,const godot_quat * p_b,const godot_quat * p_pre_a,const godot_quat * p_post_b,const godot_real p_t)172 godot_quat GDAPI godot_quat_cubic_slerp(const godot_quat *p_self, const godot_quat *p_b, const godot_quat *p_pre_a, const godot_quat *p_post_b, const godot_real p_t) {
173 godot_quat dest;
174 const Quat *self = (const Quat *)p_self;
175 const Quat *b = (const Quat *)p_b;
176 const Quat *pre_a = (const Quat *)p_pre_a;
177 const Quat *post_b = (const Quat *)p_post_b;
178 *((Quat *)&dest) = self->cubic_slerp(*b, *pre_a, *post_b, p_t);
179 return dest;
180 }
181
godot_quat_operator_multiply(const godot_quat * p_self,const godot_real p_b)182 godot_quat GDAPI godot_quat_operator_multiply(const godot_quat *p_self, const godot_real p_b) {
183 godot_quat raw_dest;
184 Quat *dest = (Quat *)&raw_dest;
185 const Quat *self = (const Quat *)p_self;
186 *dest = *self * p_b;
187 return raw_dest;
188 }
189
godot_quat_operator_add(const godot_quat * p_self,const godot_quat * p_b)190 godot_quat GDAPI godot_quat_operator_add(const godot_quat *p_self, const godot_quat *p_b) {
191 godot_quat raw_dest;
192 Quat *dest = (Quat *)&raw_dest;
193 const Quat *self = (const Quat *)p_self;
194 const Quat *b = (const Quat *)p_b;
195 *dest = *self + *b;
196 return raw_dest;
197 }
198
godot_quat_operator_subtract(const godot_quat * p_self,const godot_quat * p_b)199 godot_quat GDAPI godot_quat_operator_subtract(const godot_quat *p_self, const godot_quat *p_b) {
200 godot_quat raw_dest;
201 Quat *dest = (Quat *)&raw_dest;
202 const Quat *self = (const Quat *)p_self;
203 const Quat *b = (const Quat *)p_b;
204 *dest = *self - *b;
205 return raw_dest;
206 }
207
godot_quat_operator_divide(const godot_quat * p_self,const godot_real p_b)208 godot_quat GDAPI godot_quat_operator_divide(const godot_quat *p_self, const godot_real p_b) {
209 godot_quat raw_dest;
210 Quat *dest = (Quat *)&raw_dest;
211 const Quat *self = (const Quat *)p_self;
212 *dest = *self / p_b;
213 return raw_dest;
214 }
215
godot_quat_operator_equal(const godot_quat * p_self,const godot_quat * p_b)216 godot_bool GDAPI godot_quat_operator_equal(const godot_quat *p_self, const godot_quat *p_b) {
217 const Quat *self = (const Quat *)p_self;
218 const Quat *b = (const Quat *)p_b;
219 return *self == *b;
220 }
221
godot_quat_operator_neg(const godot_quat * p_self)222 godot_quat GDAPI godot_quat_operator_neg(const godot_quat *p_self) {
223 godot_quat raw_dest;
224 Quat *dest = (Quat *)&raw_dest;
225 const Quat *self = (const Quat *)p_self;
226 *dest = -(*self);
227 return raw_dest;
228 }
229
godot_quat_set_axis_angle(godot_quat * p_self,const godot_vector3 * p_axis,const godot_real p_angle)230 void GDAPI godot_quat_set_axis_angle(godot_quat *p_self, const godot_vector3 *p_axis, const godot_real p_angle) {
231 Quat *self = (Quat *)p_self;
232 const Vector3 *axis = (const Vector3 *)p_axis;
233 self->set_axis_angle(*axis, p_angle);
234 }
235
236 #ifdef __cplusplus
237 }
238 #endif
239