|
| 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 | + |
| 19 | +#include "itkPipeline.h" |
| 20 | +#include "itkInputMesh.h" |
| 21 | +#include "itkOutputMesh.h" |
| 22 | +#include "itkSupportInputMeshTypes.h" |
| 23 | + |
| 24 | +#include "itkmeshToGeogramMesh.h" |
| 25 | +#include "itkgeogramMeshToMesh.h" |
| 26 | + |
| 27 | +#include <geogram/mesh/mesh_remesh.h> |
| 28 | +#include <geogram/mesh/mesh_geometry.h> |
| 29 | +#include <geogram/basic/command_line.h> |
| 30 | +#include <geogram/basic/command_line_args.h> |
| 31 | + |
| 32 | +#include <geogram/basic/common.h> |
| 33 | +#include <geogram/basic/process.h> |
| 34 | +#include <geogram/basic/logger.h> |
| 35 | +#include <geogram/basic/command_line.h> |
| 36 | + |
| 37 | +template <typename TMesh> |
| 38 | +int repairMesh(itk::wasm::Pipeline &pipeline, const TMesh *inputMesh) |
| 39 | +{ |
| 40 | + using MeshType = TMesh; |
| 41 | + constexpr unsigned int Dimension = MeshType::PointDimension; |
| 42 | + using PixelType = typename MeshType::PixelType; |
| 43 | + |
| 44 | + pipeline.get_option("input-mesh")->required()->type_name("INPUT_MESH"); |
| 45 | + |
| 46 | + double numberPointsPercent = 75.0; |
| 47 | + pipeline.add_option("--number-points", numberPointsPercent, "Number of points as a percent of the bounding box diagonal. Output may have slightly more points."); |
| 48 | + |
| 49 | + double triangleShapeAdaptation = 1.0; |
| 50 | + pipeline.add_option("--triangle-shape-adaptation", triangleShapeAdaptation, "Triangle shape adaptation factor. Use 0.0 to disable."); |
| 51 | + |
| 52 | + double triangleSizeAdaptation = 0.0; |
| 53 | + pipeline.add_option("--triangle-size-adaptation", triangleSizeAdaptation, "Triangle size adaptation factor. Use 0.0 to disable."); |
| 54 | + |
| 55 | + uint32_t normalIterations = 3; |
| 56 | + pipeline.add_option("--normal-iterations", normalIterations, "Number of normal smoothing iterations."); |
| 57 | + |
| 58 | + uint32_t lloydIterations = 5; |
| 59 | + pipeline.add_option("--lloyd-iterations", lloydIterations, "Number of Lloyd relaxation iterations."); |
| 60 | + |
| 61 | + uint32_t newtonIterations = 30; |
| 62 | + pipeline.add_option("--newton-iterations", newtonIterations, "Number of Newton iterations."); |
| 63 | + |
| 64 | + uint32_t newtonM = 7; |
| 65 | + pipeline.add_option("--newton-m", newtonM, "Number of Newton evaluations per step for Hessian approximation."); |
| 66 | + |
| 67 | + uint64_t lfsSamples = 10000; |
| 68 | + pipeline.add_option("--lfs-samples", lfsSamples, "Number of samples for size adaptation if triangle size adaptation is not 0.0."); |
| 69 | + |
| 70 | + itk::wasm::OutputMesh<MeshType> outputMesh; |
| 71 | + pipeline.add_option("output-mesh", outputMesh, "The output repaired mesh.")->type_name("OUTPUT_MESH"); |
| 72 | + |
| 73 | + ITK_WASM_PARSE(pipeline); |
| 74 | + |
| 75 | + // GEO::initialize(GEO::GEOGRAM_INSTALL_ALL); |
| 76 | + |
| 77 | + GEO::Logger::initialize(); GEO::Logger::instance()->set_quiet(true); |
| 78 | + GEO::CmdLine::initialize(); |
| 79 | + const auto flags = GEO::GEOGRAM_INSTALL_ALL; |
| 80 | + GEO::Process::initialize(flags); |
| 81 | + GEO::CmdLine::import_arg_group("algo"); |
| 82 | + |
| 83 | + GEO::Mesh geoMesh(Dimension, false); |
| 84 | + itk::meshToGeogramMesh<MeshType>(inputMesh, geoMesh); |
| 85 | + |
| 86 | + if (geoMesh.facets.nb() == 0) |
| 87 | + { |
| 88 | + std::cerr << "The input mesh has no facets." << std::endl; |
| 89 | + return EXIT_FAILURE; |
| 90 | + } |
| 91 | + |
| 92 | + if (!geoMesh.facets.are_simplices()) |
| 93 | + { |
| 94 | + std::cerr << "The mesh needs to be simplicial. Use repair." << std::endl; |
| 95 | + return EXIT_FAILURE; |
| 96 | + } |
| 97 | + |
| 98 | + uint64_t numberPoints = static_cast<uint64_t>(std::ceil(numberPointsPercent * 0.01 * geoMesh.vertices.nb())); |
| 99 | + |
| 100 | + std::cout << "Remeshing with " << numberPoints << " points" << std::endl; |
| 101 | + |
| 102 | + if(triangleShapeAdaptation != 0.0) |
| 103 | + { |
| 104 | + triangleShapeAdaptation *= 0.02; |
| 105 | + GEO::compute_normals(geoMesh); |
| 106 | + if(normalIterations != 0) |
| 107 | + { |
| 108 | + GEO::simple_Laplacian_smooth(geoMesh, normalIterations, true); |
| 109 | + } |
| 110 | + GEO::set_anisotropy(geoMesh, triangleShapeAdaptation); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + geoMesh.vertices.set_dimension(3); |
| 115 | + } |
| 116 | + |
| 117 | + if(triangleSizeAdaptation != 0.0) |
| 118 | + { |
| 119 | + GEO::compute_sizing_field(geoMesh, triangleSizeAdaptation, lfsSamples); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + GEO::AttributesManager& attributes = geoMesh.vertices.attributes(); |
| 124 | + if(attributes.is_defined("weight")) |
| 125 | + { |
| 126 | + attributes.delete_attribute_store("weight"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + GEO::Mesh remesh; |
| 131 | + |
| 132 | + GEO::remesh_smooth(geoMesh, remesh, numberPoints, 0, lloydIterations, newtonIterations, newtonM); |
| 133 | + |
| 134 | + GEO::MeshElementsFlags what = GEO::MeshElementsFlags( |
| 135 | + GEO::MESH_VERTICES | GEO::MESH_EDGES | GEO::MESH_FACETS | GEO::MESH_CELLS |
| 136 | + ); |
| 137 | + geoMesh.clear(); |
| 138 | + geoMesh.copy(remesh, true, what); |
| 139 | + |
| 140 | + typename MeshType::Pointer itkMesh = MeshType::New(); |
| 141 | + itk::geogramMeshToMesh<MeshType>(geoMesh, itkMesh); |
| 142 | + |
| 143 | + outputMesh.Set(itkMesh); |
| 144 | + |
| 145 | + return EXIT_SUCCESS; |
| 146 | +} |
| 147 | + |
| 148 | +template <typename TMesh> |
| 149 | +class PipelineFunctor |
| 150 | +{ |
| 151 | +public: |
| 152 | + int operator()(itk::wasm::Pipeline &pipeline) |
| 153 | + { |
| 154 | + using MeshType = TMesh; |
| 155 | + |
| 156 | + itk::wasm::InputMesh<MeshType> testMesh; |
| 157 | + pipeline.add_option("input-mesh", testMesh, "The input mesh")->type_name("INPUT_MESH"); |
| 158 | + |
| 159 | + ITK_WASM_PRE_PARSE(pipeline); |
| 160 | + |
| 161 | + typename MeshType::ConstPointer inputMeshRef = testMesh.Get(); |
| 162 | + return repairMesh<MeshType>(pipeline, inputMeshRef); |
| 163 | + } |
| 164 | +}; |
| 165 | + |
| 166 | +int main(int argc, char *argv[]) |
| 167 | +{ |
| 168 | + itk::wasm::Pipeline pipeline("remesh", "Smooth and remesh a mesh to improve quality.", argc, argv); |
| 169 | + |
| 170 | + return itk::wasm::SupportInputMeshTypes<PipelineFunctor, |
| 171 | + // uint8_t, |
| 172 | + // int8_t, |
| 173 | + // uint16_t, |
| 174 | + // int16_t, |
| 175 | + // uint32_t, |
| 176 | + // int32_t, |
| 177 | + // float, |
| 178 | + // double>::Dimensions< |
| 179 | + float>::Dimensions< |
| 180 | + 3U>("input-mesh", pipeline); |
| 181 | +} |
0 commit comments