vec3.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef UI_VEC3_H
  17. #define UI_VEC3_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <ui/vec2.h>
  21. namespace android {
  22. // -------------------------------------------------------------------------------------
  23. template <typename T>
  24. class tvec3 : public TVecProductOperators<tvec3, T>,
  25. public TVecAddOperators<tvec3, T>,
  26. public TVecUnaryOperators<tvec3, T>,
  27. public TVecComparisonOperators<tvec3, T>,
  28. public TVecFunctions<tvec3, T>
  29. {
  30. public:
  31. enum no_init { NO_INIT };
  32. typedef T value_type;
  33. typedef T& reference;
  34. typedef T const& const_reference;
  35. typedef size_t size_type;
  36. union {
  37. struct { T x, y, z; };
  38. struct { T s, t, p; };
  39. struct { T r, g, b; };
  40. Impersonator< tvec2<T> > xy;
  41. Impersonator< tvec2<T> > st;
  42. Impersonator< tvec2<T> > rg;
  43. };
  44. enum { SIZE = 3 };
  45. inline static size_type size() { return SIZE; }
  46. // array access
  47. inline T const& operator [] (size_t i) const { return (&x)[i]; }
  48. inline T& operator [] (size_t i) { return (&x)[i]; }
  49. // -----------------------------------------------------------------------
  50. // we don't provide copy-ctor and operator= on purpose
  51. // because we want the compiler generated versions
  52. // constructors
  53. // leaves object uninitialized. use with caution.
  54. explicit tvec3(no_init) { }
  55. // default constructor
  56. tvec3() : x(0), y(0), z(0) { }
  57. // handles implicit conversion to a tvec4. must not be explicit.
  58. template<typename A>
  59. tvec3(A v) : x(v), y(v), z(v) { }
  60. template<typename A, typename B, typename C>
  61. tvec3(A x, B y, C z) : x(x), y(y), z(z) { }
  62. template<typename A, typename B>
  63. tvec3(const tvec2<A>& v, B z) : x(v.x), y(v.y), z(z) { }
  64. template<typename A>
  65. explicit tvec3(const tvec3<A>& v) : x(v.x), y(v.y), z(v.z) { }
  66. template<typename A>
  67. tvec3(const Impersonator< tvec3<A> >& v)
  68. : x(((const tvec3<A>&)v).x),
  69. y(((const tvec3<A>&)v).y),
  70. z(((const tvec3<A>&)v).z) { }
  71. template<typename A, typename B>
  72. tvec3(const Impersonator< tvec2<A> >& v, B z)
  73. : x(((const tvec2<A>&)v).x),
  74. y(((const tvec2<A>&)v).y),
  75. z(z) { }
  76. // cross product works only on vectors of size 3
  77. template <typename RT>
  78. friend inline
  79. tvec3 __attribute__((pure)) cross(const tvec3& u, const tvec3<RT>& v) {
  80. return tvec3(
  81. u.y*v.z - u.z*v.y,
  82. u.z*v.x - u.x*v.z,
  83. u.x*v.y - u.y*v.x);
  84. }
  85. };
  86. // ----------------------------------------------------------------------------------------
  87. typedef tvec3<float> vec3;
  88. // ----------------------------------------------------------------------------------------
  89. }; // namespace android
  90. #endif /* UI_VEC4_H */