summaryrefslogtreecommitdiff
path: root/core/math/transform.h
diff options
context:
space:
mode:
authorNils ANDRÉ-CHANG <nils@nilsand.re>2019-09-12 21:28:49 +0100
committerNils ANDRÉ-CHANG <nils@nilsand.re>2019-09-26 20:36:12 +0100
commit0024dd7bb5a8a5194ed0283fc506edcd8b4a7737 (patch)
tree86316cccbf4fda58a275a7451e37fda83465bb20 /core/math/transform.h
parentcafb888361eba08297dd88b18dc71f4d418525c0 (diff)
parent24e1039eb6fe32115e8d1a62a84965e9be19a2ed (diff)
Merge branch 'master' into tab_key
Diffstat (limited to 'core/math/transform.h')
-rw-r--r--core/math/transform.h76
1 files changed, 56 insertions, 20 deletions
diff --git a/core/math/transform.h b/core/math/transform.h
index 2f43f6b035..90e2b07583 100644
--- a/core/math/transform.h
+++ b/core/math/transform.h
@@ -34,10 +34,7 @@
#include "core/math/aabb.h"
#include "core/math/basis.h"
#include "core/math/plane.h"
-
-/**
- @author Juan Linietsky <reduzio@gmail.com>
-*/
+#include "core/pool_vector.h"
class Transform {
public:
@@ -86,6 +83,9 @@ public:
_FORCE_INLINE_ AABB xform(const AABB &p_aabb) const;
_FORCE_INLINE_ AABB xform_inv(const AABB &p_aabb) const;
+ _FORCE_INLINE_ PoolVector<Vector3> xform(const PoolVector<Vector3> &p_array) const;
+ _FORCE_INLINE_ PoolVector<Vector3> xform_inv(const PoolVector<Vector3> &p_array) const;
+
void operator*=(const Transform &p_transform);
Transform operator*(const Transform &p_transform) const;
@@ -108,6 +108,7 @@ public:
operator String() const;
+ Transform(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t ox, real_t oy, real_t oz);
Transform(const Basis &p_basis, const Vector3 &p_origin = Vector3());
Transform() {}
};
@@ -157,22 +158,29 @@ _FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const {
}
_FORCE_INLINE_ AABB Transform::xform(const AABB &p_aabb) const {
- /* define vertices */
- Vector3 x = basis.get_axis(0) * p_aabb.size.x;
- Vector3 y = basis.get_axis(1) * p_aabb.size.y;
- Vector3 z = basis.get_axis(2) * p_aabb.size.z;
- Vector3 pos = xform(p_aabb.position);
- //could be even further optimized
- AABB new_aabb;
- new_aabb.position = pos;
- new_aabb.expand_to(pos + x);
- new_aabb.expand_to(pos + y);
- new_aabb.expand_to(pos + z);
- new_aabb.expand_to(pos + x + y);
- new_aabb.expand_to(pos + x + z);
- new_aabb.expand_to(pos + y + z);
- new_aabb.expand_to(pos + x + y + z);
- return new_aabb;
+
+ /* http://dev.theomader.com/transform-bounding-boxes/ */
+ Vector3 min = p_aabb.position;
+ Vector3 max = p_aabb.position + p_aabb.size;
+ Vector3 tmin, tmax;
+ for (int i = 0; i < 3; i++) {
+ tmin[i] = tmax[i] = origin[i];
+ for (int j = 0; j < 3; j++) {
+ real_t e = basis[i][j] * min[j];
+ real_t f = basis[i][j] * max[j];
+ if (e < f) {
+ tmin[i] += e;
+ tmax[i] += f;
+ } else {
+ tmin[i] += f;
+ tmax[i] += e;
+ }
+ }
+ }
+ AABB r_aabb;
+ r_aabb.position = tmin;
+ r_aabb.size = tmax - tmin;
+ return r_aabb;
}
_FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const {
@@ -201,4 +209,32 @@ _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const {
return ret;
}
+PoolVector<Vector3> Transform::xform(const PoolVector<Vector3> &p_array) const {
+
+ PoolVector<Vector3> array;
+ array.resize(p_array.size());
+
+ PoolVector<Vector3>::Read r = p_array.read();
+ PoolVector<Vector3>::Write w = array.write();
+
+ for (int i = 0; i < p_array.size(); ++i) {
+ w[i] = xform(r[i]);
+ }
+ return array;
+}
+
+PoolVector<Vector3> Transform::xform_inv(const PoolVector<Vector3> &p_array) const {
+
+ PoolVector<Vector3> array;
+ array.resize(p_array.size());
+
+ PoolVector<Vector3>::Read r = p_array.read();
+ PoolVector<Vector3>::Write w = array.write();
+
+ for (int i = 0; i < p_array.size(); ++i) {
+ w[i] = xform_inv(r[i]);
+ }
+ return array;
+}
+
#endif // TRANSFORM_H