|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | +#ifndef itkmeshToGeogramMesh_h |
| 19 | +#define itkmeshToGeogramMesh_h |
| 20 | + |
| 21 | +#include <memory> |
| 22 | + |
| 23 | +#include "geogram/mesh/mesh.h" |
| 24 | + |
| 25 | +namespace itk |
| 26 | +{ |
| 27 | + |
| 28 | +template <typename TMesh> |
| 29 | +auto |
| 30 | +meshToGeogramMesh(const TMesh * itkMesh) -> std::unique_ptr<GEO::Mesh> |
| 31 | +{ |
| 32 | + using MeshType = TMesh; |
| 33 | + static constexpr unsigned int Dimension = MeshType::PointDimension; |
| 34 | + static constexpr bool SinglePrecision = std::is_same<typename MeshType::CoordRepType, float>::value; |
| 35 | + std::unique_ptr<GEO::Mesh> geoMesh = std::make_unique<GEO::Mesh>(Dimension, SinglePrecision); |
| 36 | + |
| 37 | + // Copy vertices |
| 38 | + auto points = itkMesh->GetPoints(); |
| 39 | + geoMesh->vertices.create_vertices(points->Size()); |
| 40 | + |
| 41 | + for(auto it = points->Begin(); it != points->End(); ++it) |
| 42 | + { |
| 43 | + const auto& point = it.Value(); |
| 44 | + GEO::index_t v = it.Index(); |
| 45 | + for (unsigned int d = 0; d < Dimension; ++d) |
| 46 | + { |
| 47 | + geoMesh->vertices.point(v)[d] = point[d]; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Copy faces/cells |
| 52 | + auto cells = itkMesh->GetCells(); |
| 53 | + for(auto it = cells->Begin(); it != cells->End(); ++it) |
| 54 | + { |
| 55 | + const auto& cell = it.Value(); |
| 56 | + GEO::vector<GEO::index_t> vertices; |
| 57 | + for(auto* pit = cell->PointIdsBegin(); pit != cell->PointIdsEnd(); ++pit) { |
| 58 | + vertices.push_back(*pit); |
| 59 | + } |
| 60 | + geoMesh->facets.create_polygon(vertices); |
| 61 | + } |
| 62 | + |
| 63 | + return geoMesh; |
| 64 | +} |
| 65 | + |
| 66 | +} // namespace itk |
| 67 | + |
| 68 | +#endif // itkmeshToGeogramMesh_h |
0 commit comments