vec2.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_VEC2_H
  17. #define UI_VEC2_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #define TVEC_IMPLEMENTATION
  21. #include <ui/TVecHelpers.h>
  22. namespace android {
  23. // -------------------------------------------------------------------------------------
  24. template <typename T>
  25. class tvec2 : public TVecProductOperators<tvec2, T>,
  26. public TVecAddOperators<tvec2, T>,
  27. public TVecUnaryOperators<tvec2, T>,
  28. public TVecComparisonOperators<tvec2, T>,
  29. public TVecFunctions<tvec2, T>
  30. {
  31. public:
  32. enum no_init { NO_INIT };
  33. typedef T value_type;
  34. typedef T& reference;
  35. typedef T const& const_reference;
  36. typedef size_t size_type;
  37. union {
  38. struct { T x, y; };
  39. struct { T s, t; };
  40. struct { T r, g; };
  41. };
  42. enum { SIZE = 2 };
  43. inline static size_type size() { return SIZE; }
  44. // array access
  45. inline T const& operator [] (size_t i) const { return (&x)[i]; }
  46. inline T& operator [] (size_t i) { return (&x)[i]; }
  47. // -----------------------------------------------------------------------
  48. // we don't provide copy-ctor and operator= on purpose
  49. // because we want the compiler generated versions
  50. // constructors
  51. // leaves object uninitialized. use with caution.
  52. explicit tvec2(no_init) { }
  53. // default constructor
  54. tvec2() : x(0), y(0) { }
  55. // handles implicit conversion to a tvec4. must not be explicit.
  56. template<typename A>
  57. tvec2(A v) : x(v), y(v) { }
  58. template<typename A, typename B>
  59. tvec2(A x, B y) : x(x), y(y) { }
  60. template<typename A>
  61. explicit tvec2(const tvec2<A>& v) : x(v.x), y(v.y) { }
  62. template<typename A>
  63. tvec2(const Impersonator< tvec2<A> >& v)
  64. : x(((const tvec2<A>&)v).x),
  65. y(((const tvec2<A>&)v).y) { }
  66. };
  67. // ----------------------------------------------------------------------------------------
  68. typedef tvec2<float> vec2;
  69. // ----------------------------------------------------------------------------------------
  70. }; // namespace android
  71. #endif /* UI_VEC4_H */