Diligent Engine API Reference: D:/GitHub/DiligentEngine/DiligentCore/External/Android/ndk_helper/include/vecmath.h Source File

Diligent Engine API Reference

vecmath.h
1 /*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef VECMATH_H_
18 #define VECMATH_H_
19
20 #include <cmath>
21 #include "JNIHelper.h"
22
23 namespace ndk_helper {
24
25 /******************************************************************
26 * Helper class for vector math operations
27 * Currently all implementations are in pure C++.
28 * Each class is an opaque class so caller does not have a direct access
29 * to each element. This is for an ease of future optimization to use vector
30 *operations.
31 *
32 */
33
34 class Vec2;
35 class Vec3;
36 class Vec4;
37 class Mat4;
38
39 /******************************************************************
40 * 2 elements vector class
41 *
42 */
43 class Vec2 {
44 private:
45 float x_;
46 float y_;
47
48 public:
49 friend class Vec3;
50 friend class Vec4;
51 friend class Mat4;
52 friend class Quaternion;
53
54 Vec2() { x_ = y_ = 0.f; }
55
56 Vec2(const float fX, const float fY) {
57 x_ = fX;
58 y_ = fY;
59 }
60
61 Vec2(const Vec2& vec) {
62 x_ = vec.x_;
63 y_ = vec.y_;
64 }
65
66 Vec2(const float* pVec) {
67 x_ = (*pVec++);
68 y_ = (*pVec++);
69 }
70
71 // Operators
72 Vec2 operator*(const Vec2& rhs) const {
73 Vec2 ret;
74 ret.x_ = x_ * rhs.x_;
75 ret.y_ = y_ * rhs.y_;
76 return ret;
77 }
78
79 Vec2 operator/(const Vec2& rhs) const {
80 Vec2 ret;
81 ret.x_ = x_ / rhs.x_;
82 ret.y_ = y_ / rhs.y_;
83 return ret;
84 }
85
86 Vec2 operator+(const Vec2& rhs) const {
87 Vec2 ret;
88 ret.x_ = x_ + rhs.x_;
89 ret.y_ = y_ + rhs.y_;
90 return ret;
91 }
92
93 Vec2 operator-(const Vec2& rhs) const {
94 Vec2 ret;
95 ret.x_ = x_ - rhs.x_;
96 ret.y_ = y_ - rhs.y_;
97 return ret;
98 }
99
100 Vec2& operator+=(const Vec2& rhs) {
101 x_ += rhs.x_;
102 y_ += rhs.y_;
103 return *this;
104 }
105
106 Vec2& operator-=(const Vec2& rhs) {
107 x_ -= rhs.x_;
108 y_ -= rhs.y_;
109 return *this;
110 }
111
112 Vec2& operator*=(const Vec2& rhs) {
113 x_ *= rhs.x_;
114 y_ *= rhs.y_;
115 return *this;
116 }
117
118 Vec2& operator/=(const Vec2& rhs) {
119 x_ /= rhs.x_;
120 y_ /= rhs.y_;
121 return *this;
122 }
123
124 // External operators
125 friend Vec2 operator-(const Vec2& rhs) { return Vec2(rhs) *= -1; }
126
127 friend Vec2 operator*(const float lhs, const Vec2& rhs) {
128 Vec2 ret;
129 ret.x_ = lhs * rhs.x_;
130 ret.y_ = lhs * rhs.y_;
131 return ret;
132 }
133
134 friend Vec2 operator/(const float lhs, const Vec2& rhs) {
135 Vec2 ret;
136 ret.x_ = lhs / rhs.x_;
137 ret.y_ = lhs / rhs.y_;
138 return ret;
139 }
140
141 // Operators with float
142 Vec2 operator*(const float& rhs) const {
143 Vec2 ret;
144 ret.x_ = x_ * rhs;
145 ret.y_ = y_ * rhs;
146 return ret;
147 }
148
149 Vec2& operator*=(const float& rhs) {
150 x_ = x_ * rhs;
151 y_ = y_ * rhs;
152 return *this;
153 }
154
155 Vec2 operator/(const float& rhs) const {
156 Vec2 ret;
157 ret.x_ = x_ / rhs;
158 ret.y_ = y_ / rhs;
159 return ret;
160 }
161
162 Vec2& operator/=(const float& rhs) {
163 x_ = x_ / rhs;
164 y_ = y_ / rhs;
165 return *this;
166 }
167
168 // Compare
169 bool operator==(const Vec2& rhs) const {
170 if (x_ != rhs.x_ || y_ != rhs.y_) return false;
171 return true;
172 }
173
174 bool operator!=(const Vec2& rhs) const {
175 if (x_ == rhs.x_) return false;
176
177 return true;
178 }
179
180 float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
181
182 Vec2 Normalize() {
183 float len = Length();
184 x_ = x_ / len;
185 y_ = y_ / len;
186 return *this;
187 }
188
189 float Dot(const Vec2& rhs) { return x_ * rhs.x_ + y_ * rhs.y_; }
190
191 bool Validate() {
192 if (std::isnan(x_) || std::isnan(y_)) return false;
193 return true;
194 }
195
196 void Value(float& fX, float& fY) {
197 fX = x_;
198 fY = y_;
199 }
200
201 void Dump() { LOGI("Vec2 %f %f", x_, y_); }
202 };
203
204 /******************************************************************
205 * 3 elements vector class
206 *
207 */
208 class Vec3 {
209 private:
210 float x_, y_, z_;
211
212 public:
213 friend class Vec4;
214 friend class Mat4;
215 friend class Quaternion;
216
217 Vec3() { x_ = y_ = z_ = 0.f; }
218
219 Vec3(const float fX, const float fY, const float fZ) {
220 x_ = fX;
221 y_ = fY;
222 z_ = fZ;
223 }
224
225 Vec3(const Vec3& vec) {
226 x_ = vec.x_;
227 y_ = vec.y_;
228 z_ = vec.z_;
229 }
230
231 Vec3(const float* pVec) {
232 x_ = (*pVec++);
233 y_ = (*pVec++);
234 z_ = *pVec;
235 }
236
237 Vec3(const Vec2& vec, float f) {
238 x_ = vec.x_;
239 y_ = vec.y_;
240 z_ = f;
241 }
242
243 Vec3(const Vec4& vec);
244
245 // Operators
246 Vec3 operator*(const Vec3& rhs) const {
247 Vec3 ret;
248 ret.x_ = x_ * rhs.x_;
249 ret.y_ = y_ * rhs.y_;
250 ret.z_ = z_ * rhs.z_;
251 return ret;
252 }
253
254 Vec3 operator/(const Vec3& rhs) const {
255 Vec3 ret;
256 ret.x_ = x_ / rhs.x_;
257 ret.y_ = y_ / rhs.y_;
258 ret.z_ = z_ / rhs.z_;
259 return ret;
260 }
261
262 Vec3 operator+(const Vec3& rhs) const {
263 Vec3 ret;
264 ret.x_ = x_ + rhs.x_;
265 ret.y_ = y_ + rhs.y_;
266 ret.z_ = z_ + rhs.z_;
267 return ret;
268 }
269
270 Vec3 operator-(const Vec3& rhs) const {
271 Vec3 ret;
272 ret.x_ = x_ - rhs.x_;
273 ret.y_ = y_ - rhs.y_;
274 ret.z_ = z_ - rhs.z_;
275 return ret;
276 }
277
278 Vec3& operator+=(const Vec3& rhs) {
279 x_ += rhs.x_;
280 y_ += rhs.y_;
281 z_ += rhs.z_;
282 return *this;
283 }
284
285 Vec3& operator-=(const Vec3& rhs) {
286 x_ -= rhs.x_;
287 y_ -= rhs.y_;
288 z_ -= rhs.z_;
289 return *this;
290 }
291
292 Vec3& operator*=(const Vec3& rhs) {
293 x_ *= rhs.x_;
294 y_ *= rhs.y_;
295 z_ *= rhs.z_;
296 return *this;
297 }
298
299 Vec3& operator/=(const Vec3& rhs) {
300 x_ /= rhs.x_;
301 y_ /= rhs.y_;
302 z_ /= rhs.z_;
303 return *this;
304 }
305
306 // External operators
307 friend Vec3 operator-(const Vec3& rhs) { return Vec3(rhs) *= -1; }
308
309 friend Vec3 operator*(const float lhs, const Vec3& rhs) {
310 Vec3 ret;
311 ret.x_ = lhs * rhs.x_;
312 ret.y_ = lhs * rhs.y_;
313 ret.z_ = lhs * rhs.z_;
314 return ret;
315 }
316
317 friend Vec3 operator/(const float lhs, const Vec3& rhs) {
318 Vec3 ret;
319 ret.x_ = lhs / rhs.x_;
320 ret.y_ = lhs / rhs.y_;
321 ret.z_ = lhs / rhs.z_;
322 return ret;
323 }
324
325 // Operators with float
326 Vec3 operator*(const float& rhs) const {
327 Vec3 ret;
328 ret.x_ = x_ * rhs;
329 ret.y_ = y_ * rhs;
330 ret.z_ = z_ * rhs;
331 return ret;
332 }
333
334 Vec3& operator*=(const float& rhs) {
335 x_ = x_ * rhs;
336 y_ = y_ * rhs;
337 z_ = z_ * rhs;
338 return *this;
339 }
340
341 Vec3 operator/(const float& rhs) const {
342 Vec3 ret;
343 ret.x_ = x_ / rhs;
344 ret.y_ = y_ / rhs;
345 ret.z_ = z_ / rhs;
346 return ret;
347 }
348
349 Vec3& operator/=(const float& rhs) {
350 x_ = x_ / rhs;
351 y_ = y_ / rhs;
352 z_ = z_ / rhs;
353 return *this;
354 }
355
356 // Compare
357 bool operator==(const Vec3& rhs) const {
358 if (x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_) return false;
359 return true;
360 }
361
362 bool operator!=(const Vec3& rhs) const {
363 if (x_ == rhs.x_) return false;
364
365 return true;
366 }
367
368 float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_); }
369
370 Vec3 Normalize() {
371 float len = Length();
372 x_ = x_ / len;
373 y_ = y_ / len;
374 z_ = z_ / len;
375 return *this;
376 }
377
378 float Dot(const Vec3& rhs) { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
379
380 Vec3 Cross(const Vec3& rhs) {
381 Vec3 ret;
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_;
385 return ret;
386 }
387
388 bool Validate() {
389 if (std::isnan(x_) || std::isnan(y_) || std::isnan(z_)) return false;
390 return true;
391 }
392
393 void Value(float& fX, float& fY, float& fZ) {
394 fX = x_;
395 fY = y_;
396 fZ = z_;
397 }
398
399 void Dump() { LOGI("Vec3 %f %f %f", x_, y_, z_); }
400 };
401
402 /******************************************************************
403 * 4 elements vector class
404 *
405 */
406 class Vec4 {
407 private:
408 float x_, y_, z_, w_;
409
410 public:
411 friend class Vec3;
412 friend class Mat4;
413 friend class Quaternion;
414
415 Vec4() { x_ = y_ = z_ = w_ = 0.f; }
416
417 Vec4(const float fX, const float fY, const float fZ, const float fW) {
418 x_ = fX;
419 y_ = fY;
420 z_ = fZ;
421 w_ = fW;
422 }
423
424 Vec4(const Vec4& vec) {
425 x_ = vec.x_;
426 y_ = vec.y_;
427 z_ = vec.z_;
428 w_ = vec.w_;
429 }
430
431 Vec4(const Vec3& vec, const float fW) {
432 x_ = vec.x_;
433 y_ = vec.y_;
434 z_ = vec.z_;
435 w_ = fW;
436 }
437
438 Vec4(const float* pVec) {
439 x_ = (*pVec++);
440 y_ = (*pVec++);
441 z_ = *pVec;
442 w_ = *pVec;
443 }
444
445 // Operators
446 Vec4 operator*(const Vec4& rhs) const {
447 Vec4 ret;
448 ret.x_ = x_ * rhs.x_;
449 ret.y_ = y_ * rhs.y_;
450 ret.z_ = z_ * rhs.z_;
451 ret.w_ = z_ * rhs.w_;
452 return ret;
453 }
454
455 Vec4 operator/(const Vec4& rhs) const {
456 Vec4 ret;
457 ret.x_ = x_ / rhs.x_;
458 ret.y_ = y_ / rhs.y_;
459 ret.z_ = z_ / rhs.z_;
460 ret.w_ = z_ / rhs.w_;
461 return ret;
462 }
463
464 Vec4 operator+(const Vec4& rhs) const {
465 Vec4 ret;
466 ret.x_ = x_ + rhs.x_;
467 ret.y_ = y_ + rhs.y_;
468 ret.z_ = z_ + rhs.z_;
469 ret.w_ = z_ + rhs.w_;
470 return ret;
471 }
472
473 Vec4 operator-(const Vec4& rhs) const {
474 Vec4 ret;
475 ret.x_ = x_ - rhs.x_;
476 ret.y_ = y_ - rhs.y_;
477 ret.z_ = z_ - rhs.z_;
478 ret.w_ = z_ - rhs.w_;
479 return ret;
480 }
481
482 Vec4& operator+=(const Vec4& rhs) {
483 x_ += rhs.x_;
484 y_ += rhs.y_;
485 z_ += rhs.z_;
486 w_ += rhs.w_;
487 return *this;
488 }
489
490 Vec4& operator-=(const Vec4& rhs) {
491 x_ -= rhs.x_;
492 y_ -= rhs.y_;
493 z_ -= rhs.z_;
494 w_ -= rhs.w_;
495 return *this;
496 }
497
498 Vec4& operator*=(const Vec4& rhs) {
499 x_ *= rhs.x_;
500 y_ *= rhs.y_;
501 z_ *= rhs.z_;
502 w_ *= rhs.w_;
503 return *this;
504 }
505
506 Vec4& operator/=(const Vec4& rhs) {
507 x_ /= rhs.x_;
508 y_ /= rhs.y_;
509 z_ /= rhs.z_;
510 w_ /= rhs.w_;
511 return *this;
512 }
513
514 // External operators
515 friend Vec4 operator-(const Vec4& rhs) { return Vec4(rhs) *= -1; }
516
517 friend Vec4 operator*(const float lhs, const Vec4& rhs) {
518 Vec4 ret;
519 ret.x_ = lhs * rhs.x_;
520 ret.y_ = lhs * rhs.y_;
521 ret.z_ = lhs * rhs.z_;
522 ret.w_ = lhs * rhs.w_;
523 return ret;
524 }
525
526 friend Vec4 operator/(const float lhs, const Vec4& rhs) {
527 Vec4 ret;
528 ret.x_ = lhs / rhs.x_;
529 ret.y_ = lhs / rhs.y_;
530 ret.z_ = lhs / rhs.z_;
531 ret.w_ = lhs / rhs.w_;
532 return ret;
533 }
534
535 // Operators with float
536 Vec4 operator*(const float& rhs) const {
537 Vec4 ret;
538 ret.x_ = x_ * rhs;
539 ret.y_ = y_ * rhs;
540 ret.z_ = z_ * rhs;
541 ret.w_ = w_ * rhs;
542 return ret;
543 }
544
545 Vec4& operator*=(const float& rhs) {
546 x_ = x_ * rhs;
547 y_ = y_ * rhs;
548 z_ = z_ * rhs;
549 w_ = w_ * rhs;
550 return *this;
551 }
552
553 Vec4 operator/(const float& rhs) const {
554 Vec4 ret;
555 ret.x_ = x_ / rhs;
556 ret.y_ = y_ / rhs;
557 ret.z_ = z_ / rhs;
558 ret.w_ = w_ / rhs;
559 return ret;
560 }
561
562 Vec4& operator/=(const float& rhs) {
563 x_ = x_ / rhs;
564 y_ = y_ / rhs;
565 z_ = z_ / rhs;
566 w_ = w_ / rhs;
567 return *this;
568 }
569
570 // Compare
571 bool operator==(const Vec4& rhs) const {
572 if (x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_)
573 return false;
574 return true;
575 }
576
577 bool operator!=(const Vec4& rhs) const {
578 if (x_ == rhs.x_) return false;
579
580 return true;
581 }
582
583 Vec4 operator*(const Mat4& rhs) const;
584
585 float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_ + w_ * w_); }
586
587 Vec4 Normalize() {
588 float len = Length();
589 x_ = x_ / len;
590 y_ = y_ / len;
591 z_ = z_ / len;
592 w_ = w_ / len;
593 return *this;
594 }
595
596 float Dot(const Vec3& rhs) { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
597
598 Vec3 Cross(const Vec3& rhs) {
599 Vec3 ret;
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_;
603 return ret;
604 }
605
606 bool Validate() {
607 if (std::isnan(x_) || std::isnan(y_) ||
608 std::isnan(z_) || std::isnan(w_))
609 return false;
610
611 return true;
612 }
613
614 void Value(float& fX, float& fY, float& fZ, float& fW) {
615 fX = x_;
616 fY = y_;
617 fZ = z_;
618 fW = w_;
619 }
620 };
621
622 /******************************************************************
623 * 4x4 matrix
624 *
625 */
626 class Mat4 {
627 private:
628 float f_[16];
629
630 public:
631 friend class Vec3;
632 friend class Vec4;
633 friend class Quaternion;
634
635 Mat4();
636 Mat4(const float*);
637
638 Mat4 operator*(const Mat4& rhs) const;
639 Vec4 operator*(const Vec4& rhs) const;
640
641 Mat4 operator+(const Mat4& rhs) const {
642 Mat4 ret;
643 for (int32_t i = 0; i < 16; ++i) {
644 ret.f_[i] = f_[i] + rhs.f_[i];
645 }
646 return ret;
647 }
648
649 Mat4 operator-(const Mat4& rhs) const {
650 Mat4 ret;
651 for (int32_t i = 0; i < 16; ++i) {
652 ret.f_[i] = f_[i] - rhs.f_[i];
653 }
654 return ret;
655 }
656
657 Mat4& operator+=(const Mat4& rhs) {
658 for (int32_t i = 0; i < 16; ++i) {
659 f_[i] += rhs.f_[i];
660 }
661 return *this;
662 }
663
664 Mat4& operator-=(const Mat4& rhs) {
665 for (int32_t i = 0; i < 16; ++i) {
666 f_[i] -= rhs.f_[i];
667 }
668 return *this;
669 }
670
671 Mat4& operator*=(const Mat4& rhs) {
672 Mat4 ret;
673 ret.f_[0] = f_[0] * rhs.f_[0] + f_[4] * rhs.f_[1] + f_[8] * rhs.f_[2] +
674 f_[12] * rhs.f_[3];
675 ret.f_[1] = f_[1] * rhs.f_[0] + f_[5] * rhs.f_[1] + f_[9] * rhs.f_[2] +
676 f_[13] * rhs.f_[3];
677 ret.f_[2] = f_[2] * rhs.f_[0] + f_[6] * rhs.f_[1] + f_[10] * rhs.f_[2] +
678 f_[14] * rhs.f_[3];
679 ret.f_[3] = f_[3] * rhs.f_[0] + f_[7] * rhs.f_[1] + f_[11] * rhs.f_[2] +
680 f_[15] * rhs.f_[3];
681
682 ret.f_[4] = f_[0] * rhs.f_[4] + f_[4] * rhs.f_[5] + f_[8] * rhs.f_[6] +
683 f_[12] * rhs.f_[7];
684 ret.f_[5] = f_[1] * rhs.f_[4] + f_[5] * rhs.f_[5] + f_[9] * rhs.f_[6] +
685 f_[13] * rhs.f_[7];
686 ret.f_[6] = f_[2] * rhs.f_[4] + f_[6] * rhs.f_[5] + f_[10] * rhs.f_[6] +
687 f_[14] * rhs.f_[7];
688 ret.f_[7] = f_[3] * rhs.f_[4] + f_[7] * rhs.f_[5] + f_[11] * rhs.f_[6] +
689 f_[15] * rhs.f_[7];
690
691 ret.f_[8] = f_[0] * rhs.f_[8] + f_[4] * rhs.f_[9] + f_[8] * rhs.f_[10] +
692 f_[12] * rhs.f_[11];
693 ret.f_[9] = f_[1] * rhs.f_[8] + f_[5] * rhs.f_[9] + f_[9] * rhs.f_[10] +
694 f_[13] * rhs.f_[11];
695 ret.f_[10] = f_[2] * rhs.f_[8] + f_[6] * rhs.f_[9] + f_[10] * rhs.f_[10] +
696 f_[14] * rhs.f_[11];
697 ret.f_[11] = f_[3] * rhs.f_[8] + f_[7] * rhs.f_[9] + f_[11] * rhs.f_[10] +
698 f_[15] * rhs.f_[11];
699
700 ret.f_[12] = f_[0] * rhs.f_[12] + f_[4] * rhs.f_[13] + f_[8] * rhs.f_[14] +
701 f_[12] * rhs.f_[15];
702 ret.f_[13] = f_[1] * rhs.f_[12] + f_[5] * rhs.f_[13] + f_[9] * rhs.f_[14] +
703 f_[13] * rhs.f_[15];
704 ret.f_[14] = f_[2] * rhs.f_[12] + f_[6] * rhs.f_[13] + f_[10] * rhs.f_[14] +
705 f_[14] * rhs.f_[15];
706 ret.f_[15] = f_[3] * rhs.f_[12] + f_[7] * rhs.f_[13] + f_[11] * rhs.f_[14] +
707 f_[15] * rhs.f_[15];
708
709 *this = ret;
710 return *this;
711 }
712
713 Mat4 operator*(const float rhs) {
714 Mat4 ret;
715 for (int32_t i = 0; i < 16; ++i) {
716 ret.f_[i] = f_[i] * rhs;
717 }
718 return ret;
719 }
720
721 Mat4& operator*=(const float rhs) {
722 for (int32_t i = 0; i < 16; ++i) {
723 f_[i] *= rhs;
724 }
725 return *this;
726 }
727
728 Mat4& operator=(const Mat4& rhs) {
729 for (int32_t i = 0; i < 16; ++i) {
730 f_[i] = rhs.f_[i];
731 }
732 return *this;
733 }
734
735 Mat4 Inverse();
736
737 Mat4 Transpose() {
738 Mat4 ret;
739 ret.f_[0] = f_[0];
740 ret.f_[1] = f_[4];
741 ret.f_[2] = f_[8];
742 ret.f_[3] = f_[12];
743 ret.f_[4] = f_[1];
744 ret.f_[5] = f_[5];
745 ret.f_[6] = f_[9];
746 ret.f_[7] = f_[13];
747 ret.f_[8] = f_[2];
748 ret.f_[9] = f_[6];
749 ret.f_[10] = f_[10];
750 ret.f_[11] = f_[14];
751 ret.f_[12] = f_[3];
752 ret.f_[13] = f_[7];
753 ret.f_[14] = f_[11];
754 ret.f_[15] = f_[15];
755 *this = ret;
756 return *this;
757 }
758
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]);
764 return *this;
765 }
766
767 float* Ptr() { return f_; }
768
769 //--------------------------------------------------------------------------------
770 // Misc
771 //--------------------------------------------------------------------------------
772 static Mat4 Perspective(float width, float height, float nearPlane,
773 float farPlane);
774 static Mat4 Ortho2D(float left, float top, float right, float bottom);
775
776 static Mat4 LookAt(const Vec3& vEye, const Vec3& vAt, const Vec3& vUp);
777
778 static Mat4 Translation(const float fX, const float fY, const float fZ);
779 static Mat4 Translation(const Vec3 vec);
780
781 static Mat4 RotationX(const float angle);
782
783 static Mat4 RotationY(const float angle);
784
785 static Mat4 RotationZ(const float angle);
786
787 static Mat4 Scale(const float scaleX, const float scaleY, const float scaleZ);
788
789 static Mat4 Identity() {
790 Mat4 ret;
791 ret.f_[0] = 1.f;
792 ret.f_[1] = 0;
793 ret.f_[2] = 0;
794 ret.f_[3] = 0;
795 ret.f_[4] = 0;
796 ret.f_[5] = 1.f;
797 ret.f_[6] = 0;
798 ret.f_[7] = 0;
799 ret.f_[8] = 0;
800 ret.f_[9] = 0;
801 ret.f_[10] = 1.f;
802 ret.f_[11] = 0;
803 ret.f_[12] = 0;
804 ret.f_[13] = 0;
805 ret.f_[14] = 0;
806 ret.f_[15] = 1.f;
807 return ret;
808 }
809
810 void Dump() {
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]);
815 }
816 };
817
818 /******************************************************************
819 * Quaternion class
820 *
821 */
822 class Quaternion {
823 private:
824 float x_, y_, z_, w_;
825
826 public:
827 friend class Vec3;
828 friend class Vec4;
829 friend class Mat4;
830
831 Quaternion() {
832 x_ = 0.f;
833 y_ = 0.f;
834 z_ = 0.f;
835 w_ = 1.f;
836 }
837
838 Quaternion(const float fX, const float fY, const float fZ, const float fW) {
839 x_ = fX;
840 y_ = fY;
841 z_ = fZ;
842 w_ = fW;
843 }
844
845 Quaternion(const Vec3 vec, const float fW) {
846 x_ = vec.x_;
847 y_ = vec.y_;
848 z_ = vec.z_;
849 w_ = fW;
850 }
851
852 Quaternion(const float* p) {
853 x_ = *p++;
854 y_ = *p++;
855 z_ = *p++;
856 w_ = *p++;
857 }
858
859 Quaternion operator*(const Quaternion rhs) {
860 Quaternion ret;
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_;
865 return ret;
866 }
867
868 Quaternion& operator*=(const Quaternion rhs) {
869 Quaternion ret;
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_;
874 *this = ret;
875 return *this;
876 }
877
878 Quaternion Conjugate() {
879 x_ = -x_;
880 y_ = -y_;
881 z_ = -z_;
882 return *this;
883 }
884
885 // Non destuctive version
886 Quaternion Conjugated() {
887 Quaternion ret;
888 ret.x_ = -x_;
889 ret.y_ = -y_;
890 ret.z_ = -z_;
891 ret.w_ = w_;
892 return ret;
893 }
894
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;
905
906 mat.f_[0] = 1.0f - y2 - z2;
907 mat.f_[1] = xy + zw;
908 mat.f_[2] = zx - yw;
909 mat.f_[4] = xy - zw;
910 mat.f_[5] = 1.0f - z2 - x2;
911 mat.f_[6] = yz + xw;
912 mat.f_[8] = zx + yw;
913 mat.f_[9] = yz - xw;
914 mat.f_[10] = 1.0f - x2 - y2;
915
916 mat.f_[3] = mat.f_[7] = mat.f_[11] = mat.f_[12] = mat.f_[13] = mat.f_[14] =
917 0.0f;
918 mat.f_[15] = 1.0f;
919 }
920
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;
931
932 mat.f_[0] = 1.0f - y2 - z2;
933 mat.f_[1] = xy + zw;
934 mat.f_[2] = zx - yw;
935 mat.f_[4] = xy - zw;
936 mat.f_[5] = 1.0f - z2 - x2;
937 mat.f_[6] = yz + xw;
938 mat.f_[8] = zx + yw;
939 mat.f_[9] = yz - xw;
940 mat.f_[10] = 1.0f - x2 - y2;
941
942 mat.f_[3] = mat.f_[7] = mat.f_[11] = 0.0f;
943 mat.f_[15] = 1.0f;
944 }
945
946 static Quaternion RotationAxis(const Vec3 axis, const float angle) {
947 Quaternion ret;
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);
953 return ret;
954 }
955
956 void Value(float& fX, float& fY, float& fZ, float& fW) {
957 fX = x_;
958 fY = y_;
959 fZ = z_;
960 fW = w_;
961 }
962 };
963
964 } // namespace ndk_helper
965 #endif /* VECMATH_H_ */
ndk_helper
Definition: gestureDetector.h:32