/* * Copyright 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef MATH_VEC3_H_ #define MATH_VEC3_H_ #include #include #include #include namespace filament { namespace math { // ------------------------------------------------------------------------------------- namespace details { template class MATH_EMPTY_BASES TVec3 : public TVecProductOperators, public TVecAddOperators, public TVecUnaryOperators, public TVecComparisonOperators, public TVecFunctions { public: typedef T value_type; typedef T& reference; typedef T const& const_reference; typedef size_t size_type; static constexpr size_t SIZE = 3; union { T v[SIZE] MATH_CONSTEXPR_INIT; TVec2 xy; TVec2 st; TVec2 rg; struct { union { T x; T s; T r; }; union { struct { T y, z; }; struct { T t, p; }; struct { T g, b; }; TVec2 yz; TVec2 tp; TVec2 gb; }; }; }; inline constexpr size_type size() const { return SIZE; } // array access inline constexpr T const& operator[](size_t i) const noexcept { assert(i < SIZE); return v[i]; } inline constexpr T& operator[](size_t i) noexcept { assert(i < SIZE); return v[i]; } // constructors // default constructor MATH_DEFAULT_CTOR_CONSTEXPR TVec3() noexcept MATH_DEFAULT_CTOR // handles implicit conversion to a tvec3. must not be explicit. template> constexpr TVec3(A v) noexcept : v{ T(v), T(v), T(v) } {} template> constexpr TVec3(A x, B y, C z) noexcept : v{ T(x), T(y), T(z) } {} template> constexpr TVec3(const TVec2& v, B z) noexcept : v{ T(v[0]), T(v[1]), T(z) } {} template> constexpr TVec3(const TVec3& v) noexcept : v{ T(v[0]), T(v[1]), T(v[2]) } {} // cross product works only on vectors of size 3 template friend inline constexpr TVec3> cross(const TVec3& u, const TVec3& v) noexcept { return { u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0] }; } }; } // namespace details // ---------------------------------------------------------------------------------------- template> using vec3 = details::TVec3; using double3 = vec3; using float3 = vec3; using half3 = vec3; using int3 = vec3; using uint3 = vec3; using short3 = vec3; using ushort3 = vec3; using byte3 = vec3; using ubyte3 = vec3; using bool3 = vec3; // ---------------------------------------------------------------------------------------- } // namespace math } // namespace filament #endif // MATH_VEC3_H_