21 #include "JNIHelper.h" 52 friend class Quaternion;
54 Vec2() { x_ = y_ = 0.f; }
56 Vec2(
const float fX,
const float fY) {
61 Vec2(
const Vec2& vec) {
66 Vec2(
const float* pVec) {
72 Vec2 operator*(
const Vec2& rhs)
const {
79 Vec2 operator/(
const Vec2& rhs)
const {
86 Vec2 operator+(
const Vec2& rhs)
const {
93 Vec2 operator-(
const Vec2& rhs)
const {
100 Vec2& operator+=(
const Vec2& rhs) {
106 Vec2& operator-=(
const Vec2& rhs) {
112 Vec2& operator*=(
const Vec2& rhs) {
118 Vec2& operator/=(
const Vec2& rhs) {
125 friend Vec2 operator-(
const Vec2& rhs) {
return Vec2(rhs) *= -1; }
127 friend Vec2 operator*(
const float lhs,
const Vec2& rhs) {
129 ret.x_ = lhs * rhs.x_;
130 ret.y_ = lhs * rhs.y_;
134 friend Vec2 operator/(
const float lhs,
const Vec2& rhs) {
136 ret.x_ = lhs / rhs.x_;
137 ret.y_ = lhs / rhs.y_;
142 Vec2 operator*(
const float& rhs)
const {
149 Vec2& operator*=(
const float& rhs) {
155 Vec2 operator/(
const float& rhs)
const {
162 Vec2& operator/=(
const float& rhs) {
169 bool operator==(
const Vec2& rhs)
const {
170 if (x_ != rhs.x_ || y_ != rhs.y_)
return false;
174 bool operator!=(
const Vec2& rhs)
const {
175 if (x_ == rhs.x_)
return false;
180 float Length()
const {
return sqrtf(x_ * x_ + y_ * y_); }
183 float len = Length();
189 float Dot(
const Vec2& rhs) {
return x_ * rhs.x_ + y_ * rhs.y_; }
192 if (std::isnan(x_) || std::isnan(y_))
return false;
196 void Value(
float& fX,
float& fY) {
201 void Dump() { LOGI(
"Vec2 %f %f", x_, y_); }
215 friend class Quaternion;
217 Vec3() { x_ = y_ = z_ = 0.f; }
219 Vec3(
const float fX,
const float fY,
const float fZ) {
225 Vec3(
const Vec3& vec) {
231 Vec3(
const float* pVec) {
237 Vec3(
const Vec2& vec,
float f) {
243 Vec3(
const Vec4& vec);
246 Vec3 operator*(
const Vec3& rhs)
const {
248 ret.x_ = x_ * rhs.x_;
249 ret.y_ = y_ * rhs.y_;
250 ret.z_ = z_ * rhs.z_;
254 Vec3 operator/(
const Vec3& rhs)
const {
256 ret.x_ = x_ / rhs.x_;
257 ret.y_ = y_ / rhs.y_;
258 ret.z_ = z_ / rhs.z_;
262 Vec3 operator+(
const Vec3& rhs)
const {
264 ret.x_ = x_ + rhs.x_;
265 ret.y_ = y_ + rhs.y_;
266 ret.z_ = z_ + rhs.z_;
270 Vec3 operator-(
const Vec3& rhs)
const {
272 ret.x_ = x_ - rhs.x_;
273 ret.y_ = y_ - rhs.y_;
274 ret.z_ = z_ - rhs.z_;
278 Vec3& operator+=(
const Vec3& rhs) {
285 Vec3& operator-=(
const Vec3& rhs) {
292 Vec3& operator*=(
const Vec3& rhs) {
299 Vec3& operator/=(
const Vec3& rhs) {
307 friend Vec3 operator-(
const Vec3& rhs) {
return Vec3(rhs) *= -1; }
309 friend Vec3 operator*(
const float lhs,
const Vec3& rhs) {
311 ret.x_ = lhs * rhs.x_;
312 ret.y_ = lhs * rhs.y_;
313 ret.z_ = lhs * rhs.z_;
317 friend Vec3 operator/(
const float lhs,
const Vec3& rhs) {
319 ret.x_ = lhs / rhs.x_;
320 ret.y_ = lhs / rhs.y_;
321 ret.z_ = lhs / rhs.z_;
326 Vec3 operator*(
const float& rhs)
const {
334 Vec3& operator*=(
const float& rhs) {
341 Vec3 operator/(
const float& rhs)
const {
349 Vec3& operator/=(
const float& rhs) {
357 bool operator==(
const Vec3& rhs)
const {
358 if (x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_)
return false;
362 bool operator!=(
const Vec3& rhs)
const {
363 if (x_ == rhs.x_)
return false;
368 float Length()
const {
return sqrtf(x_ * x_ + y_ * y_ + z_ * z_); }
371 float len = Length();
378 float Dot(
const Vec3& rhs) {
return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
380 Vec3 Cross(
const Vec3& rhs) {
382 ret.x_ = y_ * rhs.z_ - z_ * rhs.y_;
383 ret.y_ = z_ * rhs.x_ - x_ * rhs.z_;
384 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_;
389 if (std::isnan(x_) || std::isnan(y_) || std::isnan(z_))
return false;
393 void Value(
float& fX,
float& fY,
float& fZ) {
399 void Dump() { LOGI(
"Vec3 %f %f %f", x_, y_, z_); }
408 float x_, y_, z_, w_;
413 friend class Quaternion;
415 Vec4() { x_ = y_ = z_ = w_ = 0.f; }
417 Vec4(
const float fX,
const float fY,
const float fZ,
const float fW) {
424 Vec4(
const Vec4& vec) {
431 Vec4(
const Vec3& vec,
const float fW) {
438 Vec4(
const float* pVec) {
446 Vec4 operator*(
const Vec4& rhs)
const {
448 ret.x_ = x_ * rhs.x_;
449 ret.y_ = y_ * rhs.y_;
450 ret.z_ = z_ * rhs.z_;
451 ret.w_ = z_ * rhs.w_;
455 Vec4 operator/(
const Vec4& rhs)
const {
457 ret.x_ = x_ / rhs.x_;
458 ret.y_ = y_ / rhs.y_;
459 ret.z_ = z_ / rhs.z_;
460 ret.w_ = z_ / rhs.w_;
464 Vec4 operator+(
const Vec4& rhs)
const {
466 ret.x_ = x_ + rhs.x_;
467 ret.y_ = y_ + rhs.y_;
468 ret.z_ = z_ + rhs.z_;
469 ret.w_ = z_ + rhs.w_;
473 Vec4 operator-(
const Vec4& rhs)
const {
475 ret.x_ = x_ - rhs.x_;
476 ret.y_ = y_ - rhs.y_;
477 ret.z_ = z_ - rhs.z_;
478 ret.w_ = z_ - rhs.w_;
482 Vec4& operator+=(
const Vec4& rhs) {
490 Vec4& operator-=(
const Vec4& rhs) {
498 Vec4& operator*=(
const Vec4& rhs) {
506 Vec4& operator/=(
const Vec4& rhs) {
515 friend Vec4 operator-(
const Vec4& rhs) {
return Vec4(rhs) *= -1; }
517 friend Vec4 operator*(
const float lhs,
const Vec4& rhs) {
519 ret.x_ = lhs * rhs.x_;
520 ret.y_ = lhs * rhs.y_;
521 ret.z_ = lhs * rhs.z_;
522 ret.w_ = lhs * rhs.w_;
526 friend Vec4 operator/(
const float lhs,
const Vec4& rhs) {
528 ret.x_ = lhs / rhs.x_;
529 ret.y_ = lhs / rhs.y_;
530 ret.z_ = lhs / rhs.z_;
531 ret.w_ = lhs / rhs.w_;
536 Vec4 operator*(
const float& rhs)
const {
545 Vec4& operator*=(
const float& rhs) {
553 Vec4 operator/(
const float& rhs)
const {
562 Vec4& operator/=(
const float& rhs) {
571 bool operator==(
const Vec4& rhs)
const {
572 if (x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_)
577 bool operator!=(
const Vec4& rhs)
const {
578 if (x_ == rhs.x_)
return false;
583 Vec4 operator*(
const Mat4& rhs)
const;
585 float Length()
const {
return sqrtf(x_ * x_ + y_ * y_ + z_ * z_ + w_ * w_); }
588 float len = Length();
596 float Dot(
const Vec3& rhs) {
return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
598 Vec3 Cross(
const Vec3& rhs) {
600 ret.x_ = y_ * rhs.z_ - z_ * rhs.y_;
601 ret.y_ = z_ * rhs.x_ - x_ * rhs.z_;
602 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_;
607 if (std::isnan(x_) || std::isnan(y_) ||
608 std::isnan(z_) || std::isnan(w_))
614 void Value(
float& fX,
float& fY,
float& fZ,
float& fW) {
633 friend class Quaternion;
638 Mat4 operator*(
const Mat4& rhs)
const;
639 Vec4 operator*(
const Vec4& rhs)
const;
641 Mat4 operator+(
const Mat4& rhs)
const {
643 for (int32_t i = 0; i < 16; ++i) {
644 ret.f_[i] = f_[i] + rhs.f_[i];
649 Mat4 operator-(
const Mat4& rhs)
const {
651 for (int32_t i = 0; i < 16; ++i) {
652 ret.f_[i] = f_[i] - rhs.f_[i];
657 Mat4& operator+=(
const Mat4& rhs) {
658 for (int32_t i = 0; i < 16; ++i) {
664 Mat4& operator-=(
const Mat4& rhs) {
665 for (int32_t i = 0; i < 16; ++i) {
671 Mat4& operator*=(
const Mat4& rhs) {
673 ret.f_[0] = f_[0] * rhs.f_[0] + f_[4] * rhs.f_[1] + f_[8] * rhs.f_[2] +
675 ret.f_[1] = f_[1] * rhs.f_[0] + f_[5] * rhs.f_[1] + f_[9] * rhs.f_[2] +
677 ret.f_[2] = f_[2] * rhs.f_[0] + f_[6] * rhs.f_[1] + f_[10] * rhs.f_[2] +
679 ret.f_[3] = f_[3] * rhs.f_[0] + f_[7] * rhs.f_[1] + f_[11] * rhs.f_[2] +
682 ret.f_[4] = f_[0] * rhs.f_[4] + f_[4] * rhs.f_[5] + f_[8] * rhs.f_[6] +
684 ret.f_[5] = f_[1] * rhs.f_[4] + f_[5] * rhs.f_[5] + f_[9] * rhs.f_[6] +
686 ret.f_[6] = f_[2] * rhs.f_[4] + f_[6] * rhs.f_[5] + f_[10] * rhs.f_[6] +
688 ret.f_[7] = f_[3] * rhs.f_[4] + f_[7] * rhs.f_[5] + f_[11] * rhs.f_[6] +
691 ret.f_[8] = f_[0] * rhs.f_[8] + f_[4] * rhs.f_[9] + f_[8] * rhs.f_[10] +
693 ret.f_[9] = f_[1] * rhs.f_[8] + f_[5] * rhs.f_[9] + f_[9] * rhs.f_[10] +
695 ret.f_[10] = f_[2] * rhs.f_[8] + f_[6] * rhs.f_[9] + f_[10] * rhs.f_[10] +
697 ret.f_[11] = f_[3] * rhs.f_[8] + f_[7] * rhs.f_[9] + f_[11] * rhs.f_[10] +
700 ret.f_[12] = f_[0] * rhs.f_[12] + f_[4] * rhs.f_[13] + f_[8] * rhs.f_[14] +
702 ret.f_[13] = f_[1] * rhs.f_[12] + f_[5] * rhs.f_[13] + f_[9] * rhs.f_[14] +
704 ret.f_[14] = f_[2] * rhs.f_[12] + f_[6] * rhs.f_[13] + f_[10] * rhs.f_[14] +
706 ret.f_[15] = f_[3] * rhs.f_[12] + f_[7] * rhs.f_[13] + f_[11] * rhs.f_[14] +
713 Mat4 operator*(
const float rhs) {
715 for (int32_t i = 0; i < 16; ++i) {
716 ret.f_[i] = f_[i] * rhs;
721 Mat4& operator*=(
const float rhs) {
722 for (int32_t i = 0; i < 16; ++i) {
728 Mat4& operator=(
const Mat4& rhs) {
729 for (int32_t i = 0; i < 16; ++i) {
759 Mat4& PostTranslate(
float tx,
float ty,
float tz) {
760 f_[12] += (tx * f_[0]) + (ty * f_[4]) + (tz * f_[8]);
761 f_[13] += (tx * f_[1]) + (ty * f_[5]) + (tz * f_[9]);
762 f_[14] += (tx * f_[2]) + (ty * f_[6]) + (tz * f_[10]);
763 f_[15] += (tx * f_[3]) + (ty * f_[7]) + (tz * f_[11]);
767 float* Ptr() {
return f_; }
772 static Mat4 Perspective(
float width,
float height,
float nearPlane,
774 static Mat4 Ortho2D(
float left,
float top,
float right,
float bottom);
776 static Mat4 LookAt(
const Vec3& vEye,
const Vec3& vAt,
const Vec3& vUp);
778 static Mat4 Translation(
const float fX,
const float fY,
const float fZ);
779 static Mat4 Translation(
const Vec3 vec);
781 static Mat4 RotationX(
const float angle);
783 static Mat4 RotationY(
const float angle);
785 static Mat4 RotationZ(
const float angle);
787 static Mat4 Scale(
const float scaleX,
const float scaleY,
const float scaleZ);
789 static Mat4 Identity() {
811 LOGI(
"%f %f %f %f", f_[0], f_[1], f_[2], f_[3]);
812 LOGI(
"%f %f %f %f", f_[4], f_[5], f_[6], f_[7]);
813 LOGI(
"%f %f %f %f", f_[8], f_[9], f_[10], f_[11]);
814 LOGI(
"%f %f %f %f", f_[12], f_[13], f_[14], f_[15]);
824 float x_, y_, z_, w_;
838 Quaternion(
const float fX,
const float fY,
const float fZ,
const float fW) {
845 Quaternion(
const Vec3 vec,
const float fW) {
852 Quaternion(
const float* p) {
859 Quaternion operator*(
const Quaternion rhs) {
861 ret.x_ = x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_ + w_ * rhs.x_;
862 ret.y_ = -x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_ + w_ * rhs.y_;
863 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_ + w_ * rhs.z_;
864 ret.w_ = -x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_ + w_ * rhs.w_;
868 Quaternion& operator*=(
const Quaternion rhs) {
870 ret.x_ = x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_ + w_ * rhs.x_;
871 ret.y_ = -x_ * rhs.z_ + y_ * rhs.w_ + z_ * rhs.x_ + w_ * rhs.y_;
872 ret.z_ = x_ * rhs.y_ - y_ * rhs.x_ + z_ * rhs.w_ + w_ * rhs.z_;
873 ret.w_ = -x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_ + w_ * rhs.w_;
878 Quaternion Conjugate() {
886 Quaternion Conjugated() {
895 void ToMatrix(Mat4& mat) {
896 float x2 = x_ * x_ * 2.0f;
897 float y2 = y_ * y_ * 2.0f;
898 float z2 = z_ * z_ * 2.0f;
899 float xy = x_ * y_ * 2.0f;
900 float yz = y_ * z_ * 2.0f;
901 float zx = z_ * x_ * 2.0f;
902 float xw = x_ * w_ * 2.0f;
903 float yw = y_ * w_ * 2.0f;
904 float zw = z_ * w_ * 2.0f;
906 mat.f_[0] = 1.0f - y2 - z2;
910 mat.f_[5] = 1.0f - z2 - x2;
914 mat.f_[10] = 1.0f - x2 - y2;
916 mat.f_[3] = mat.f_[7] = mat.f_[11] = mat.f_[12] = mat.f_[13] = mat.f_[14] =
921 void ToMatrixPreserveTranslate(Mat4& mat) {
922 float x2 = x_ * x_ * 2.0f;
923 float y2 = y_ * y_ * 2.0f;
924 float z2 = z_ * z_ * 2.0f;
925 float xy = x_ * y_ * 2.0f;
926 float yz = y_ * z_ * 2.0f;
927 float zx = z_ * x_ * 2.0f;
928 float xw = x_ * w_ * 2.0f;
929 float yw = y_ * w_ * 2.0f;
930 float zw = z_ * w_ * 2.0f;
932 mat.f_[0] = 1.0f - y2 - z2;
936 mat.f_[5] = 1.0f - z2 - x2;
940 mat.f_[10] = 1.0f - x2 - y2;
942 mat.f_[3] = mat.f_[7] = mat.f_[11] = 0.0f;
946 static Quaternion RotationAxis(
const Vec3 axis,
const float angle) {
948 float s = sinf(angle / 2);
949 ret.x_ = s * axis.x_;
950 ret.y_ = s * axis.y_;
951 ret.z_ = s * axis.z_;
952 ret.w_ = cosf(angle / 2);
956 void Value(
float& fX,
float& fY,
float& fZ,
float& fW) {
Definition: gestureDetector.h:32