|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 - 2025 Geode-solutions |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +#include <geode/geometry/frame_transform.hpp> |
| 25 | + |
| 26 | +#include <geode/basic/logger.hpp> |
| 27 | +#include <geode/basic/pimpl_impl.hpp> |
| 28 | + |
| 29 | +#include <geode/geometry/frame.hpp> |
| 30 | +#include <geode/geometry/vector.hpp> |
| 31 | + |
| 32 | +namespace geode |
| 33 | +{ |
| 34 | + template < index_t dimension > |
| 35 | + class FrameTransform< dimension >::Impl |
| 36 | + { |
| 37 | + public: |
| 38 | + Impl( const Frame< dimension >& from, const Frame< dimension >& to ) |
| 39 | + { |
| 40 | + for( const auto d_from : LRange{ dimension } ) |
| 41 | + { |
| 42 | + const auto from_vector = from.direction( d_from ).normalize(); |
| 43 | + double max_dot{ 0 }; |
| 44 | + for( const auto d_to : LRange{ dimension } ) |
| 45 | + { |
| 46 | + const auto to_vector = to.direction( d_to ).normalize(); |
| 47 | + const auto dot = from_vector.dot( to_vector ); |
| 48 | + const auto abs_dot = std::fabs( dot ); |
| 49 | + if( abs_dot > max_dot ) |
| 50 | + { |
| 51 | + directions_[d_from] = d_to; |
| 52 | + max_dot = abs_dot; |
| 53 | + orientations_[d_from] = dot > 0 ? 1 : -1; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + local_index_t direction( local_index_t index ) const |
| 60 | + { |
| 61 | + return directions_[index]; |
| 62 | + } |
| 63 | + |
| 64 | + signed_index_t orientation( local_index_t index ) const |
| 65 | + { |
| 66 | + return orientations_[index]; |
| 67 | + } |
| 68 | + |
| 69 | + Frame< dimension > apply( const Frame< dimension >& frame ) const |
| 70 | + { |
| 71 | + Frame< dimension > result; |
| 72 | + for( const auto d : LRange{ dimension } ) |
| 73 | + { |
| 74 | + result.set_direction( |
| 75 | + direction( d ), frame.direction( d ) * orientation( d ) ); |
| 76 | + } |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + Point< dimension > apply( const Point< dimension >& point ) const |
| 81 | + { |
| 82 | + Point< dimension > result; |
| 83 | + for( const auto d : LRange{ dimension } ) |
| 84 | + { |
| 85 | + result.set_value( |
| 86 | + d, orientations_[d] * point.value( directions_[d] ) ); |
| 87 | + } |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + Vector< dimension > apply( const Vector< dimension >& vector ) const |
| 92 | + { |
| 93 | + Vector< dimension > result; |
| 94 | + for( const auto d : LRange{ dimension } ) |
| 95 | + { |
| 96 | + result.set_value( |
| 97 | + d, orientations_[d] * vector.value( directions_[d] ) ); |
| 98 | + } |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + private: |
| 103 | + std::array< local_index_t, dimension > directions_; |
| 104 | + std::array< signed_index_t, dimension > orientations_; |
| 105 | + }; |
| 106 | + |
| 107 | + template < index_t dimension > |
| 108 | + FrameTransform< dimension >::FrameTransform( |
| 109 | + const Frame< dimension >& from, const Frame< dimension >& to ) |
| 110 | + : impl_{ from, to } |
| 111 | + { |
| 112 | + } |
| 113 | + |
| 114 | + template < index_t dimension > |
| 115 | + FrameTransform< dimension >::~FrameTransform() = default; |
| 116 | + |
| 117 | + template < index_t dimension > |
| 118 | + local_index_t FrameTransform< dimension >::direction( |
| 119 | + local_index_t index ) const |
| 120 | + { |
| 121 | + return impl_->direction( index ); |
| 122 | + } |
| 123 | + |
| 124 | + template < index_t dimension > |
| 125 | + signed_index_t FrameTransform< dimension >::orientation( |
| 126 | + local_index_t index ) const |
| 127 | + { |
| 128 | + return impl_->orientation( index ); |
| 129 | + } |
| 130 | + |
| 131 | + template < index_t dimension > |
| 132 | + Frame< dimension > FrameTransform< dimension >::apply( |
| 133 | + const Frame< dimension >& frame ) const |
| 134 | + { |
| 135 | + return impl_->apply( frame ); |
| 136 | + } |
| 137 | + |
| 138 | + template < index_t dimension > |
| 139 | + Vector< dimension > FrameTransform< dimension >::apply( |
| 140 | + const Vector< dimension >& vector ) const |
| 141 | + { |
| 142 | + return impl_->apply( vector ); |
| 143 | + } |
| 144 | + |
| 145 | + template < index_t dimension > |
| 146 | + Point< dimension > FrameTransform< dimension >::apply( |
| 147 | + const Point< dimension >& point ) const |
| 148 | + { |
| 149 | + return impl_->apply( point ); |
| 150 | + } |
| 151 | + template class opengeode_geometry_api FrameTransform< 2 >; |
| 152 | + template class opengeode_geometry_api FrameTransform< 3 >; |
| 153 | +} // namespace geode |
0 commit comments