|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 - 2024 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/model/helpers/detail/mappings_merger.h> |
| 25 | + |
| 26 | +#include <geode/model/mixin/core/block.h> |
| 27 | +#include <geode/model/mixin/core/block_collection.h> |
| 28 | +#include <geode/model/mixin/core/corner.h> |
| 29 | +#include <geode/model/mixin/core/corner_collection.h> |
| 30 | +#include <geode/model/mixin/core/line.h> |
| 31 | +#include <geode/model/mixin/core/line_collection.h> |
| 32 | +#include <geode/model/mixin/core/model_boundary.h> |
| 33 | +#include <geode/model/mixin/core/surface.h> |
| 34 | +#include <geode/model/mixin/core/surface_collection.h> |
| 35 | + |
| 36 | +namespace |
| 37 | +{ |
| 38 | + class GenericandCopyMappingsMerger |
| 39 | + { |
| 40 | + public: |
| 41 | + GenericandCopyMappingsMerger( |
| 42 | + const geode::ModelGenericMapping& mappings1, |
| 43 | + const geode::ModelCopyMapping& mappings2 ) |
| 44 | + : mappings1_( mappings1 ), mappings2_( mappings2 ) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + geode::ModelGenericMapping merge() |
| 49 | + { |
| 50 | + geode::ModelGenericMapping result; |
| 51 | + for( const auto& component_mapping : |
| 52 | + mappings1_.components_mappings() ) |
| 53 | + { |
| 54 | + if( mappings2_.has_mapping_type( component_mapping.first ) ) |
| 55 | + { |
| 56 | + result.emplace( component_mapping.first, |
| 57 | + merge_mappings( component_mapping.second, |
| 58 | + mappings2_.at( component_mapping.first ) ) ); |
| 59 | + } |
| 60 | + } |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + private: |
| 65 | + geode::ModelGenericMapping::Mapping merge_mappings( |
| 66 | + const geode::ModelGenericMapping::Mapping& mappings1, |
| 67 | + const geode::ModelCopyMapping::Mapping& mappings2 ) |
| 68 | + { |
| 69 | + geode::ModelGenericMapping::Mapping result; |
| 70 | + for( const auto& in2between : mappings1.in2out_map() ) |
| 71 | + { |
| 72 | + for( const auto& between_id : in2between.second ) |
| 73 | + { |
| 74 | + if( !mappings2.has_mapping_input( between_id ) ) |
| 75 | + { |
| 76 | + continue; |
| 77 | + } |
| 78 | + result.map( |
| 79 | + in2between.first, mappings2.in2out( between_id ) ); |
| 80 | + } |
| 81 | + } |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + private: |
| 86 | + const geode::ModelGenericMapping& mappings1_; |
| 87 | + const geode::ModelCopyMapping& mappings2_; |
| 88 | + }; |
| 89 | + |
| 90 | + class GenericMappingsMerger |
| 91 | + { |
| 92 | + public: |
| 93 | + GenericMappingsMerger( const geode::ModelGenericMapping& mappings1, |
| 94 | + const geode::ModelGenericMapping& mappings2 ) |
| 95 | + : mappings1_( mappings1 ), mappings2_( mappings2 ) |
| 96 | + { |
| 97 | + } |
| 98 | + |
| 99 | + geode::ModelGenericMapping merge() |
| 100 | + { |
| 101 | + geode::ModelGenericMapping result; |
| 102 | + for( const auto& component_mapping : |
| 103 | + mappings1_.components_mappings() ) |
| 104 | + { |
| 105 | + if( mappings2_.has_mapping_type( component_mapping.first ) ) |
| 106 | + { |
| 107 | + result.emplace( component_mapping.first, |
| 108 | + merge_mappings( component_mapping.second, |
| 109 | + mappings2_.at( component_mapping.first ) ) ); |
| 110 | + } |
| 111 | + } |
| 112 | + return result; |
| 113 | + } |
| 114 | + |
| 115 | + private: |
| 116 | + geode::ModelGenericMapping::Mapping merge_mappings( |
| 117 | + const geode::ModelGenericMapping::Mapping& mappings1, |
| 118 | + const geode::ModelGenericMapping::Mapping& mappings2 ) |
| 119 | + { |
| 120 | + geode::ModelGenericMapping::Mapping result; |
| 121 | + for( const auto& in2between : mappings1.in2out_map() ) |
| 122 | + { |
| 123 | + for( const auto& between_id : in2between.second ) |
| 124 | + { |
| 125 | + if( !mappings2.has_mapping_input( between_id ) ) |
| 126 | + { |
| 127 | + continue; |
| 128 | + } |
| 129 | + for( const auto& out_id : mappings2.in2out( between_id ) ) |
| 130 | + { |
| 131 | + result.map( in2between.first, out_id ); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return result; |
| 136 | + } |
| 137 | + |
| 138 | + private: |
| 139 | + const geode::ModelGenericMapping& mappings1_; |
| 140 | + const geode::ModelGenericMapping& mappings2_; |
| 141 | + }; |
| 142 | + |
| 143 | + class CopyToGenericMappings |
| 144 | + { |
| 145 | + public: |
| 146 | + CopyToGenericMappings( const geode::ModelCopyMapping& mappings ) |
| 147 | + : mappings_( mappings ) |
| 148 | + { |
| 149 | + } |
| 150 | + |
| 151 | + geode::ModelGenericMapping transfer() |
| 152 | + { |
| 153 | + for( const auto& component_mapping : |
| 154 | + mappings_.components_mappings() ) |
| 155 | + { |
| 156 | + result_.emplace( component_mapping.first, |
| 157 | + transfer_mappings( component_mapping.second ) ); |
| 158 | + } |
| 159 | + return std::move( result_ ); |
| 160 | + } |
| 161 | + |
| 162 | + private: |
| 163 | + geode::ModelGenericMapping::Mapping transfer_mappings( |
| 164 | + const geode::ModelCopyMapping::Mapping& copy_mappings ) |
| 165 | + { |
| 166 | + geode::ModelGenericMapping::Mapping result; |
| 167 | + for( const auto& in2out : copy_mappings.in2out_map() ) |
| 168 | + { |
| 169 | + result.map( in2out.first, in2out.second ); |
| 170 | + } |
| 171 | + return result; |
| 172 | + } |
| 173 | + |
| 174 | + private: |
| 175 | + const geode::ModelCopyMapping& mappings_; |
| 176 | + geode::ModelGenericMapping result_; |
| 177 | + }; |
| 178 | + |
| 179 | + geode::MeshElementMapping merge_element_mappings( |
| 180 | + const geode::MeshElementMapping& mappings1, |
| 181 | + const geode::MeshElementMapping& mappings2 ) |
| 182 | + { |
| 183 | + geode::MeshElementMapping result; |
| 184 | + for( const auto& first_mapping : mappings1.in2out_map() ) |
| 185 | + { |
| 186 | + for( const auto& intermediate_element : first_mapping.second ) |
| 187 | + { |
| 188 | + if( !mappings2.has_mapping_input( intermediate_element ) ) |
| 189 | + { |
| 190 | + continue; |
| 191 | + } |
| 192 | + for( const auto& out_element : |
| 193 | + mappings2.in2out( intermediate_element ) ) |
| 194 | + { |
| 195 | + result.map( first_mapping.first, out_element ); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + return result; |
| 200 | + } |
| 201 | + |
| 202 | + geode::MeshVertexMapping merge_vertex_mappings( |
| 203 | + const geode::MeshVertexMapping& mappings1, |
| 204 | + const geode::MeshVertexMapping& mappings2 ) |
| 205 | + { |
| 206 | + geode::MeshVertexMapping result; |
| 207 | + for( const auto& first_mapping : mappings1.in2out_map() ) |
| 208 | + { |
| 209 | + for( const auto& intermediate_vertex : first_mapping.second ) |
| 210 | + { |
| 211 | + if( !mappings2.has_mapping_input( intermediate_vertex ) ) |
| 212 | + { |
| 213 | + continue; |
| 214 | + } |
| 215 | + for( const auto& out_vertex : |
| 216 | + mappings2.in2out( intermediate_vertex ) ) |
| 217 | + { |
| 218 | + result.map( first_mapping.first, out_vertex ); |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + return result; |
| 223 | + } |
| 224 | +} // namespace |
| 225 | + |
| 226 | +namespace geode |
| 227 | +{ |
| 228 | + namespace detail |
| 229 | + { |
| 230 | + ModelGenericMapping merge_mappings( |
| 231 | + const ModelGenericMapping& mappings1, |
| 232 | + const ModelCopyMapping& mappings2 ) |
| 233 | + { |
| 234 | + GenericandCopyMappingsMerger merger{ mappings1, mappings2 }; |
| 235 | + return merger.merge(); |
| 236 | + } |
| 237 | + |
| 238 | + ModelGenericMapping merge_mappings( |
| 239 | + const ModelGenericMapping& mappings1, |
| 240 | + const ModelGenericMapping& mappings2 ) |
| 241 | + { |
| 242 | + GenericMappingsMerger merger{ mappings1, mappings2 }; |
| 243 | + return merger.merge(); |
| 244 | + } |
| 245 | + |
| 246 | + ModelGenericMapping copy_to_generic_mappings( |
| 247 | + const ModelCopyMapping& mappings ) |
| 248 | + { |
| 249 | + CopyToGenericMappings transferer{ mappings }; |
| 250 | + return transferer.transfer(); |
| 251 | + } |
| 252 | + |
| 253 | + ModelMappings merge_mappings( |
| 254 | + const ModelMappings& mappings1, const ModelMappings& mappings2 ) |
| 255 | + { |
| 256 | + ModelMappings result; |
| 257 | + result.component_mapping = merge_mappings( |
| 258 | + mappings1.component_mapping, mappings2.component_mapping ); |
| 259 | + result.mesh_element_mapping.corners = |
| 260 | + merge_element_mappings( mappings1.mesh_element_mapping.corners, |
| 261 | + mappings2.mesh_element_mapping.corners ); |
| 262 | + result.mesh_element_mapping.lines = |
| 263 | + merge_element_mappings( mappings1.mesh_element_mapping.lines, |
| 264 | + mappings2.mesh_element_mapping.lines ); |
| 265 | + result.mesh_element_mapping.surfaces = |
| 266 | + merge_element_mappings( mappings1.mesh_element_mapping.surfaces, |
| 267 | + mappings2.mesh_element_mapping.surfaces ); |
| 268 | + result.mesh_vertices_mapping.corners = |
| 269 | + merge_vertex_mappings( mappings1.mesh_vertices_mapping.corners, |
| 270 | + mappings2.mesh_vertices_mapping.corners ); |
| 271 | + result.mesh_vertices_mapping.lines = |
| 272 | + merge_vertex_mappings( mappings1.mesh_vertices_mapping.lines, |
| 273 | + mappings2.mesh_vertices_mapping.lines ); |
| 274 | + result.mesh_vertices_mapping.surfaces = |
| 275 | + merge_vertex_mappings( mappings1.mesh_vertices_mapping.surfaces, |
| 276 | + mappings2.mesh_vertices_mapping.surfaces ); |
| 277 | + } |
| 278 | + } // namespace detail |
| 279 | +} // namespace geode |
0 commit comments