|
| 1 | +/***************************************************************************** |
| 2 | + * Copyright (c) 2026, Lutra Consulting Ltd. and Hobu, Inc. * |
| 3 | + * * |
| 4 | + * All rights reserved. * |
| 5 | + * * |
| 6 | + * This program is free software; you can redistribute it and/or modify * |
| 7 | + * it under the terms of the GNU General Public License as published by * |
| 8 | + * the Free Software Foundation; either version 3 of the License, or * |
| 9 | + * (at your option) any later version. * |
| 10 | + * * |
| 11 | + ****************************************************************************/ |
| 12 | + |
| 13 | +#include <filesystem> |
| 14 | +#include <iostream> |
| 15 | +#include <thread> |
| 16 | + |
| 17 | +#include <pdal/PipelineManager.hpp> |
| 18 | +#include <pdal/Polygon.hpp> |
| 19 | +#include <pdal/Stage.hpp> |
| 20 | +#include <pdal/pdal_types.hpp> |
| 21 | +#include <pdal/util/ProgramArgs.hpp> |
| 22 | + |
| 23 | +#include <gdal_utils.h> |
| 24 | + |
| 25 | +#include "alg.hpp" |
| 26 | +#include "utils.hpp" |
| 27 | +#include "vpc.hpp" |
| 28 | + |
| 29 | +using namespace pdal; |
| 30 | + |
| 31 | +void ComparePointClouds::addArgs() |
| 32 | +{ |
| 33 | + argOutput = &programArgs.add("output,o", "Output point cloud file", outputFile); |
| 34 | + argComparedInputFile = &programArgs.add("input-compare", "Point cloud file to compare against input", comparedInputFile); |
| 35 | + |
| 36 | + programArgs.add("subsampling-cell-size", "Minimum spacing between points", subsamplingCellSize, 0.0); |
| 37 | + |
| 38 | + programArgs.add( "normal-radius", "Radius of the sphere around each core point that defines the neighbors from which normals are calculated.", normalRadius, 2.0); |
| 39 | + programArgs.add("cyl-radius", "Radius of the cylinder inside of which points are searched for when calculating change", cylRadius, 2.0); |
| 40 | + programArgs.add("cyl-halflen", "The half-length of the cylinder of neighbors used for calculating change", cylHalflen, 5.0); |
| 41 | + programArgs.add("reg-error", "Registration error", regError, 0.0); |
| 42 | + argOrientation = &programArgs.add( "cyl-orientation", "Which direction to orient the cylinder/normal vector used for comparison between the two point clouds. (up, origin, none)", cylOrientation, "up"); |
| 43 | +} |
| 44 | + |
| 45 | +bool ComparePointClouds::checkArgs() |
| 46 | +{ |
| 47 | + |
| 48 | + if (ends_with(inputFile, ".vpc")) { |
| 49 | + std::cerr << "input cannot be a VPC file" << std::endl; |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + if (ends_with(comparedInputFile, ".vpc")) { |
| 54 | + std::cerr << "compared input cannot be a VPC file" << std::endl; |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + if (!argOutput->set()) { |
| 59 | + std::cerr << "missing output" << std::endl; |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + if (!argComparedInputFile->set()) { |
| 64 | + std::cerr << "missing compared input file" << std::endl; |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if (argOrientation->set()) { |
| 69 | + if (cylOrientation != "up" && cylOrientation != "origin" && |
| 70 | + cylOrientation != "none") { |
| 71 | + std::cerr << "unknown orientation: " << cylOrientation << std::endl; |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +static std::unique_ptr<PipelineManager> pipeline(ParallelJobInfo *tile, std::string compareFile, double subsamplingCellSize, double normalRadius, double cylRadius, double cylHalflen, double regError, std::string cylOrientation) |
| 80 | +{ |
| 81 | + std::unique_ptr<PipelineManager> manager(new PipelineManager); |
| 82 | + |
| 83 | + Stage &reader1 = makeReader(manager.get(), tile->inputFilenames[0]); |
| 84 | + Stage *last = &reader1; |
| 85 | + |
| 86 | + // filtering |
| 87 | + if (!tile->filterBounds.empty()) |
| 88 | + { |
| 89 | + Options filter_opts; |
| 90 | + filter_opts.add(pdal::Option("bounds", tile->filterBounds)); |
| 91 | + |
| 92 | + if (readerSupportsBounds(reader1)) |
| 93 | + { |
| 94 | + // Reader of the format can do the filtering - use that whenever possible! |
| 95 | + reader1.addOptions(filter_opts); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + // Reader can't do the filtering - do it with a filter |
| 100 | + last = &manager->makeFilter("filters.crop", *last, filter_opts); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (!tile->filterExpression.empty()) |
| 105 | + { |
| 106 | + Options filter_opts; |
| 107 | + filter_opts.add(pdal::Option("expression", tile->filterExpression)); |
| 108 | + last = &manager->makeFilter("filters.expression", *last, filter_opts); |
| 109 | + } |
| 110 | + |
| 111 | + Stage *reader1FilteredCropped = last; |
| 112 | + |
| 113 | + Stage &reader2 = makeReader(manager.get(), compareFile); |
| 114 | + last = &reader2; |
| 115 | + |
| 116 | + // filtering |
| 117 | + if (!tile->filterBounds.empty()) |
| 118 | + { |
| 119 | + Options filter_opts; |
| 120 | + filter_opts.add(pdal::Option("bounds", tile->filterBounds)); |
| 121 | + |
| 122 | + if (readerSupportsBounds(reader2)) |
| 123 | + { |
| 124 | + // Reader of the format can do the filtering - use that whenever possible! |
| 125 | + reader2.addOptions(filter_opts); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + // Reader can't do the filtering - do it with a filter |
| 130 | + last = &manager->makeFilter("filters.crop", *last, filter_opts); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (!tile->filterExpression.empty()) |
| 135 | + { |
| 136 | + Options filter_opts; |
| 137 | + filter_opts.add(pdal::Option("expression", tile->filterExpression)); |
| 138 | + last = &manager->makeFilter("filters.expression", *last, filter_opts); |
| 139 | + } |
| 140 | + Stage *reader2FilteredCropped = last; |
| 141 | + |
| 142 | + Stage *corePoints = nullptr; |
| 143 | + if (subsamplingCellSize != 0.0) |
| 144 | + { |
| 145 | + pdal::Options sample_opts; |
| 146 | + sample_opts.add(pdal::Option("cell", subsamplingCellSize)); |
| 147 | + corePoints = &manager->makeFilter("filters.sample", *reader1FilteredCropped, sample_opts); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + corePoints = reader1FilteredCropped; |
| 152 | + } |
| 153 | + |
| 154 | + Options compare_opts; |
| 155 | + compare_opts.add(pdal::Option("normal_radius", normalRadius)); |
| 156 | + compare_opts.add(pdal::Option("cyl_radius", cylRadius)); |
| 157 | + compare_opts.add(pdal::Option("cyl_halflen", cylHalflen)); |
| 158 | + compare_opts.add(pdal::Option("reg_error", regError)); |
| 159 | + compare_opts.add(pdal::Option("orientation", cylOrientation)); |
| 160 | + |
| 161 | + Stage *filterM3c2 = &manager->makeFilter("filters.m3c2", compare_opts); |
| 162 | + |
| 163 | + // inputs to M3C2 filter are origin file, compared file and core points (in order) |
| 164 | + filterM3c2->setInput(*reader1FilteredCropped); |
| 165 | + filterM3c2->setInput(*reader2FilteredCropped); |
| 166 | + filterM3c2->setInput(*corePoints); |
| 167 | + |
| 168 | + makeWriter(manager.get(), tile->outputFilename, filterM3c2); |
| 169 | + |
| 170 | + return manager; |
| 171 | +} |
| 172 | + |
| 173 | +void ComparePointClouds::preparePipelines(std::vector<std::unique_ptr<PipelineManager>> &pipelines) |
| 174 | +{ |
| 175 | + ParallelJobInfo tile(ParallelJobInfo::Single, BOX2D(), filterExpression, |
| 176 | + filterBounds); |
| 177 | + tile.inputFilenames.push_back(inputFile); |
| 178 | + tile.outputFilename = outputFile; |
| 179 | + pipelines.push_back(pipeline(&tile, comparedInputFile, subsamplingCellSize, normalRadius, cylRadius, cylHalflen, regError, cylOrientation)); |
| 180 | +} |
| 181 | + |
| 182 | +void ComparePointClouds::finalize( std::vector<std::unique_ptr<PipelineManager>> &) |
| 183 | +{ |
| 184 | +} |
0 commit comments