|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/* <editor-fold desc="MIT License"> |
| 4 | +
|
| 5 | +Copyright(c) 2018 Robert Osfield |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +
|
| 9 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 10 | +
|
| 11 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 12 | +
|
| 13 | +</editor-fold> */ |
| 14 | + |
| 15 | +// we can't implement the anonymous union/structs combination without causing warnings, so disable them for just this header |
| 16 | +#if defined(__GNUC__) |
| 17 | +# pragma GCC diagnostic push |
| 18 | +# pragma GCC diagnostic ignored "-Wpedantic" |
| 19 | +#endif |
| 20 | +#if defined(__clang__) |
| 21 | +# pragma clang diagnostic push |
| 22 | +# pragma clang diagnostic ignored "-Wgnu-anonymous-struct" |
| 23 | +# pragma clang diagnostic ignored "-Wnested-anon-types" |
| 24 | +#endif |
| 25 | + |
| 26 | +#include <vsg/maths/vec3.h> |
| 27 | + |
| 28 | +namespace vsg |
| 29 | +{ |
| 30 | + |
| 31 | + /// ucoord class for managing astronomical scale coordinates |
| 32 | + struct ucoord |
| 33 | + { |
| 34 | + using value_type = double; |
| 35 | + using vec_type = t_vec3<value_type>; |
| 36 | + |
| 37 | + vec_type origin; |
| 38 | + vec_type offset; |
| 39 | + |
| 40 | + constexpr ucoord() {} |
| 41 | + |
| 42 | + constexpr ucoord(const ucoord& uc) = default; |
| 43 | + constexpr ucoord(value_type in_x, value_type in_y, value_type in_z) : |
| 44 | + origin(0.0, 0.0, 0.0), |
| 45 | + offset(in_x, in_y, in_z) {} |
| 46 | + template<typename R> |
| 47 | + constexpr explicit ucoord(const t_vec3<R>& v) : |
| 48 | + origin(0.0, 0.0, 0.0), |
| 49 | + offset(static_cast<value_type>(v.x), static_cast<value_type>(v.y), static_cast<value_type>(v.z)) {} |
| 50 | + template<typename R> |
| 51 | + constexpr ucoord(const t_vec3<R>& in_origin, const t_vec3<R>& in_offset) : |
| 52 | + origin(in_origin), |
| 53 | + offset(in_offset){} |
| 54 | + |
| 55 | + ucoord& operator=(const ucoord& rhs) |
| 56 | + { |
| 57 | + origin = rhs.origin; |
| 58 | + offset = rhs.offset; |
| 59 | + return *this; |
| 60 | + } |
| 61 | + |
| 62 | + void set(value_type in_x, value_type in_y, value_type in_z) |
| 63 | + { |
| 64 | + origin.set(in_x, in_y, in_z); |
| 65 | + offset.set(in_x, in_y, in_z); |
| 66 | + } |
| 67 | + |
| 68 | + inline ucoord& operator+=(const ucoord& rhs) |
| 69 | + { |
| 70 | + origin += rhs.origin; |
| 71 | + offset += rhs.offset; |
| 72 | + return *this; |
| 73 | + } |
| 74 | + |
| 75 | + inline ucoord& operator-=(const ucoord& rhs) |
| 76 | + { |
| 77 | + origin -= rhs.origin; |
| 78 | + offset -= rhs.offset; |
| 79 | + return *this; |
| 80 | + } |
| 81 | + |
| 82 | + explicit operator bool() const noexcept { return origin || offset; } |
| 83 | + }; |
| 84 | + |
| 85 | + VSG_type_name(vsg::ucoord); |
| 86 | + |
| 87 | + inline constexpr bool operator==(const ucoord& lhs, const ucoord& rhs) |
| 88 | + { |
| 89 | + return lhs.origin == rhs.origin && lhs.offset == rhs.offset; |
| 90 | + } |
| 91 | + |
| 92 | + inline constexpr bool operator!=(const ucoord& lhs, const ucoord& rhs) |
| 93 | + { |
| 94 | + return lhs.origin != rhs.origin || lhs.offset != rhs.offset; |
| 95 | + } |
| 96 | + |
| 97 | + inline bool operator<(const ucoord& lhs, const ucoord& rhs) |
| 98 | + { |
| 99 | + ucoord::vec_type delta = (rhs.origin-lhs.origin) + (rhs.offset - lhs.offset); |
| 100 | + if (delta[0] < 0.0) return true; |
| 101 | + if (delta[0] > 0.0) return false; |
| 102 | + if (delta[1] < 0.0) return true; |
| 103 | + if (delta[1] > 0.0) return false; |
| 104 | + return (delta[2] < 0.0); |
| 105 | + } |
| 106 | + |
| 107 | + inline constexpr ucoord operator-(const ucoord& lhs, const ucoord& rhs) |
| 108 | + { |
| 109 | + return ucoord(lhs.origin - rhs.origin, lhs.offset - rhs.offset); |
| 110 | + } |
| 111 | + |
| 112 | + inline constexpr ucoord operator+(const ucoord& lhs, const ucoord& rhs) |
| 113 | + { |
| 114 | + return ucoord(lhs.origin + rhs.origin, lhs.offset + rhs.offset); |
| 115 | + } |
| 116 | + |
| 117 | + inline constexpr ucoord::value_type length(const ucoord& uc) |
| 118 | + { |
| 119 | + return length(uc.origin + uc.offset); |
| 120 | + } |
| 121 | + |
| 122 | + inline constexpr ucoord::value_type length2(const ucoord& uc) |
| 123 | + { |
| 124 | + return length2(uc.origin + uc.offset); |
| 125 | + } |
| 126 | + |
| 127 | + constexpr ucoord mix(const ucoord& start, const ucoord& end, ucoord::value_type r) |
| 128 | + { |
| 129 | + ucoord::value_type one_minus_r = 1.0 - r; |
| 130 | + return ucoord(start.origin * one_minus_r + start.origin * r, |
| 131 | + start.offset * one_minus_r + end.offset * r); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | +} // namespace vsg |
| 136 | + |
| 137 | +#if defined(__clang__) |
| 138 | +# pragma clang diagnostic pop |
| 139 | +#endif |
| 140 | +#if defined(__GNUC__) |
| 141 | +# pragma GCC diagnostic pop |
| 142 | +#endif |
0 commit comments