Home | History | Annotate | Download | only in math
      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 #pragma once
     18 
     19 #include <math/TVecHelpers.h>
     20 #include <math/half.h>
     21 #include <assert.h>
     22 #include <stdint.h>
     23 #include <sys/types.h>
     24 #include <type_traits>
     25 
     26 #pragma clang diagnostic push
     27 #pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
     28 #pragma clang diagnostic ignored "-Wnested-anon-types"
     29 
     30 namespace android {
     31 // -------------------------------------------------------------------------------------
     32 
     33 namespace details {
     34 
     35 template <typename T>
     36 class TVec2 :   public TVecProductOperators<TVec2, T>,
     37                 public TVecAddOperators<TVec2, T>,
     38                 public TVecUnaryOperators<TVec2, T>,
     39                 public TVecComparisonOperators<TVec2, T>,
     40                 public TVecFunctions<TVec2, T>,
     41                 public TVecDebug<TVec2, T> {
     42 public:
     43     enum no_init { NO_INIT };
     44     typedef T value_type;
     45     typedef T& reference;
     46     typedef T const& const_reference;
     47     typedef size_t size_type;
     48 
     49     union {
     50         struct { T x, y; };
     51         struct { T s, t; };
     52         struct { T r, g; };
     53     };
     54 
     55     static constexpr size_t SIZE = 2;
     56     inline constexpr size_type size() const { return SIZE; }
     57 
     58     // array access
     59     inline constexpr T const& operator[](size_t i) const {
     60 #if __cplusplus >= 201402L
     61         // only possible in C++0x14 with constexpr
     62         assert(i < SIZE);
     63 #endif
     64         return (&x)[i];
     65     }
     66 
     67     inline T& operator[](size_t i) {
     68         assert(i < SIZE);
     69         return (&x)[i];
     70     }
     71 
     72     // -----------------------------------------------------------------------
     73     // we want the compiler generated versions for these...
     74     TVec2(const TVec2&) = default;
     75     ~TVec2() = default;
     76     TVec2& operator = (const TVec2&) = default;
     77 
     78     // constructors
     79 
     80     // leaves object uninitialized. use with caution.
     81     explicit
     82     constexpr TVec2(no_init) { }
     83 
     84     // default constructor
     85     constexpr TVec2() : x(0), y(0) { }
     86 
     87     // handles implicit conversion to a tvec4. must not be explicit.
     88     template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type>
     89     constexpr TVec2(A v) : x(v), y(v) { }
     90 
     91     template<typename A, typename B>
     92     constexpr TVec2(A x, B y) : x(x), y(y) { }
     93 
     94     template<typename A>
     95     explicit
     96     constexpr TVec2(const TVec2<A>& v) : x(v.x), y(v.y) { }
     97 
     98     // cross product works only on vectors of size 2 or 3
     99     template<typename RT>
    100     friend inline
    101     constexpr value_type cross(const TVec2& u, const TVec2<RT>& v) {
    102         return value_type(u.x*v.y - u.y*v.x);
    103     }
    104 };
    105 
    106 }  // namespace details
    107 
    108 // ----------------------------------------------------------------------------------------
    109 
    110 typedef details::TVec2<double> double2;
    111 typedef details::TVec2<float> float2;
    112 typedef details::TVec2<float> vec2;
    113 typedef details::TVec2<half> half2;
    114 typedef details::TVec2<int32_t> int2;
    115 typedef details::TVec2<uint32_t> uint2;
    116 typedef details::TVec2<int16_t> short2;
    117 typedef details::TVec2<uint16_t> ushort2;
    118 typedef details::TVec2<int8_t> byte2;
    119 typedef details::TVec2<uint8_t> ubyte2;
    120 typedef details::TVec2<bool> bool2;
    121 
    122 // ----------------------------------------------------------------------------------------
    123 }  // namespace android
    124 
    125 #pragma clang diagnostic pop
    126