transform.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*************************************************************************/
  2. /* transform.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #include "gdnative/transform.h"
  31. #include "core/math/transform.h"
  32. #include "core/variant.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. void _transform_api_anchor() {}
  37. void GDAPI godot_transform_new_with_axis_origin(godot_transform *r_dest, const godot_vector3 *p_x_axis, const godot_vector3 *p_y_axis, const godot_vector3 *p_z_axis, const godot_vector3 *p_origin) {
  38. const Vector3 *x_axis = (const Vector3 *)p_x_axis;
  39. const Vector3 *y_axis = (const Vector3 *)p_y_axis;
  40. const Vector3 *z_axis = (const Vector3 *)p_z_axis;
  41. const Vector3 *origin = (const Vector3 *)p_origin;
  42. Transform *dest = (Transform *)r_dest;
  43. dest->basis.set_axis(0, *x_axis);
  44. dest->basis.set_axis(1, *y_axis);
  45. dest->basis.set_axis(2, *z_axis);
  46. dest->origin = *origin;
  47. }
  48. void GDAPI godot_transform_new(godot_transform *r_dest, const godot_basis *p_basis, const godot_vector3 *p_origin) {
  49. const Basis *basis = (const Basis *)p_basis;
  50. const Vector3 *origin = (const Vector3 *)p_origin;
  51. Transform *dest = (Transform *)r_dest;
  52. *dest = Transform(*basis, *origin);
  53. }
  54. godot_basis GDAPI godot_transform_get_basis(const godot_transform *p_self) {
  55. godot_basis dest;
  56. const Transform *self = (const Transform *)p_self;
  57. *((Basis *)&dest) = self->basis;
  58. return dest;
  59. }
  60. void GDAPI godot_transform_set_basis(godot_transform *p_self, godot_basis *p_v) {
  61. Transform *self = (Transform *)p_self;
  62. const Basis *v = (const Basis *)p_v;
  63. self->basis = *v;
  64. }
  65. godot_vector3 GDAPI godot_transform_get_origin(const godot_transform *p_self) {
  66. godot_vector3 dest;
  67. const Transform *self = (const Transform *)p_self;
  68. *((Vector3 *)&dest) = self->origin;
  69. return dest;
  70. }
  71. void GDAPI godot_transform_set_origin(godot_transform *p_self, godot_vector3 *p_v) {
  72. Transform *self = (Transform *)p_self;
  73. const Vector3 *v = (const Vector3 *)p_v;
  74. self->origin = *v;
  75. }
  76. godot_string GDAPI godot_transform_as_string(const godot_transform *p_self) {
  77. godot_string ret;
  78. const Transform *self = (const Transform *)p_self;
  79. memnew_placement(&ret, String(*self));
  80. return ret;
  81. }
  82. godot_transform GDAPI godot_transform_inverse(const godot_transform *p_self) {
  83. godot_transform dest;
  84. const Transform *self = (const Transform *)p_self;
  85. *((Transform *)&dest) = self->inverse();
  86. return dest;
  87. }
  88. godot_transform GDAPI godot_transform_affine_inverse(const godot_transform *p_self) {
  89. godot_transform dest;
  90. const Transform *self = (const Transform *)p_self;
  91. *((Transform *)&dest) = self->affine_inverse();
  92. return dest;
  93. }
  94. godot_transform GDAPI godot_transform_orthonormalized(const godot_transform *p_self) {
  95. godot_transform dest;
  96. const Transform *self = (const Transform *)p_self;
  97. *((Transform *)&dest) = self->orthonormalized();
  98. return dest;
  99. }
  100. godot_transform GDAPI godot_transform_rotated(const godot_transform *p_self, const godot_vector3 *p_axis, const godot_real p_phi) {
  101. godot_transform dest;
  102. const Transform *self = (const Transform *)p_self;
  103. const Vector3 *axis = (const Vector3 *)p_axis;
  104. *((Transform *)&dest) = self->rotated(*axis, p_phi);
  105. return dest;
  106. }
  107. godot_transform GDAPI godot_transform_scaled(const godot_transform *p_self, const godot_vector3 *p_scale) {
  108. godot_transform dest;
  109. const Transform *self = (const Transform *)p_self;
  110. const Vector3 *scale = (const Vector3 *)p_scale;
  111. *((Transform *)&dest) = self->scaled(*scale);
  112. return dest;
  113. }
  114. godot_transform GDAPI godot_transform_translated(const godot_transform *p_self, const godot_vector3 *p_ofs) {
  115. godot_transform dest;
  116. const Transform *self = (const Transform *)p_self;
  117. const Vector3 *ofs = (const Vector3 *)p_ofs;
  118. *((Transform *)&dest) = self->translated(*ofs);
  119. return dest;
  120. }
  121. godot_transform GDAPI godot_transform_looking_at(const godot_transform *p_self, const godot_vector3 *p_target, const godot_vector3 *p_up) {
  122. godot_transform dest;
  123. const Transform *self = (const Transform *)p_self;
  124. const Vector3 *target = (const Vector3 *)p_target;
  125. const Vector3 *up = (const Vector3 *)p_up;
  126. *((Transform *)&dest) = self->looking_at(*target, *up);
  127. return dest;
  128. }
  129. godot_plane GDAPI godot_transform_xform_plane(const godot_transform *p_self, const godot_plane *p_v) {
  130. godot_plane raw_dest;
  131. Plane *dest = (Plane *)&raw_dest;
  132. const Transform *self = (const Transform *)p_self;
  133. const Plane *v = (const Plane *)p_v;
  134. *dest = self->xform(*v);
  135. return raw_dest;
  136. }
  137. godot_plane GDAPI godot_transform_xform_inv_plane(const godot_transform *p_self, const godot_plane *p_v) {
  138. godot_plane raw_dest;
  139. Plane *dest = (Plane *)&raw_dest;
  140. const Transform *self = (const Transform *)p_self;
  141. const Plane *v = (const Plane *)p_v;
  142. *dest = self->xform_inv(*v);
  143. return raw_dest;
  144. }
  145. void GDAPI godot_transform_new_identity(godot_transform *r_dest) {
  146. Transform *dest = (Transform *)r_dest;
  147. *dest = Transform();
  148. }
  149. godot_bool GDAPI godot_transform_operator_equal(const godot_transform *p_self, const godot_transform *p_b) {
  150. const Transform *self = (const Transform *)p_self;
  151. const Transform *b = (const Transform *)p_b;
  152. return *self == *b;
  153. }
  154. godot_transform GDAPI godot_transform_operator_multiply(const godot_transform *p_self, const godot_transform *p_b) {
  155. godot_transform raw_dest;
  156. Transform *dest = (Transform *)&raw_dest;
  157. const Transform *self = (const Transform *)p_self;
  158. const Transform *b = (const Transform *)p_b;
  159. *dest = *self * *b;
  160. return raw_dest;
  161. }
  162. godot_vector3 GDAPI godot_transform_xform_vector3(const godot_transform *p_self, const godot_vector3 *p_v) {
  163. godot_vector3 raw_dest;
  164. Vector3 *dest = (Vector3 *)&raw_dest;
  165. const Transform *self = (const Transform *)p_self;
  166. const Vector3 *v = (const Vector3 *)p_v;
  167. *dest = self->xform(*v);
  168. return raw_dest;
  169. }
  170. godot_vector3 GDAPI godot_transform_xform_inv_vector3(const godot_transform *p_self, const godot_vector3 *p_v) {
  171. godot_vector3 raw_dest;
  172. Vector3 *dest = (Vector3 *)&raw_dest;
  173. const Transform *self = (const Transform *)p_self;
  174. const Vector3 *v = (const Vector3 *)p_v;
  175. *dest = self->xform_inv(*v);
  176. return raw_dest;
  177. }
  178. godot_rect3 GDAPI godot_transform_xform_rect3(const godot_transform *p_self, const godot_rect3 *p_v) {
  179. godot_rect3 raw_dest;
  180. Rect3 *dest = (Rect3 *)&raw_dest;
  181. const Transform *self = (const Transform *)p_self;
  182. const Rect3 *v = (const Rect3 *)p_v;
  183. *dest = self->xform(*v);
  184. return raw_dest;
  185. }
  186. godot_rect3 GDAPI godot_transform_xform_inv_rect3(const godot_transform *p_self, const godot_rect3 *p_v) {
  187. godot_rect3 raw_dest;
  188. Rect3 *dest = (Rect3 *)&raw_dest;
  189. const Transform *self = (const Transform *)p_self;
  190. const Rect3 *v = (const Rect3 *)p_v;
  191. *dest = self->xform_inv(*v);
  192. return raw_dest;
  193. }
  194. #ifdef __cplusplus
  195. }
  196. #endif