void Kernel::visualize(PointBufferPtr buffer) const { BufferReader bufferReader; bufferReader.addBuffer(buffer); StageFactory f; WriterPtr writer(f.createWriter("writers.pclvisualizer")); writer->setInput(&bufferReader); PointContext ctx; writer->prepare(ctx); writer->execute(ctx); }
int SmoothKernel::execute() { PointContext ctx; Options readerOptions; readerOptions.add("filename", m_inputFile); readerOptions.add("debug", isDebug()); readerOptions.add("verbose", getVerboseLevel()); std::unique_ptr<Stage> readerStage = makeReader(readerOptions); // go ahead and prepare/execute on reader stage only to grab input // PointBufferSet, this makes the input PointBuffer available to both the // processing pipeline and the visualizer readerStage->prepare(ctx); PointBufferSet pbSetIn = readerStage->execute(ctx); // the input PointBufferSet will be used to populate a BufferReader that is // consumed by the processing pipeline PointBufferPtr input_buffer = *pbSetIn.begin(); BufferReader bufferReader; bufferReader.setOptions(readerOptions); bufferReader.addBuffer(input_buffer); Options smoothOptions; std::ostringstream ss; ss << "{"; ss << " \"pipeline\": {"; ss << " \"filters\": [{"; ss << " \"name\": \"MovingLeastSquares\""; ss << " }]"; ss << " }"; ss << "}"; std::string json = ss.str(); smoothOptions.add("json", json); smoothOptions.add("debug", isDebug()); smoothOptions.add("verbose", getVerboseLevel()); std::unique_ptr<Stage> smoothStage(new filters::PCLBlock()); smoothStage->setOptions(smoothOptions); smoothStage->setInput(&bufferReader); Options writerOptions; writerOptions.add("filename", m_outputFile); setCommonOptions(writerOptions); WriterPtr writer(KernelSupport::makeWriter(m_outputFile, smoothStage.get())); writer->setOptions(writerOptions); std::vector<std::string> cmd = getProgressShellCommand(); UserCallback *callback = cmd.size() ? (UserCallback *)new ShellScriptCallback(cmd) : (UserCallback *)new HeartbeatCallback(); writer->setUserCallback(callback); std::map<std::string, Options> extra_opts = getExtraStageOptions(); std::map<std::string, Options>::iterator pi; for (pi = extra_opts.begin(); pi != extra_opts.end(); ++pi) { std::string name = pi->first; Options options = pi->second; std::vector<Stage*> stages = writer->findStage(name); std::vector<Stage*>::iterator s; for (s = stages.begin(); s != stages.end(); ++s) { Options opts = (*s)->getOptions(); std::vector<Option>::iterator o; for (o = options.getOptions().begin(); o != options.getOptions().end(); ++o) opts.add(*o); (*s)->setOptions(opts); } } writer->prepare(ctx); // process the data, grabbing the PointBufferSet for visualization of the // resulting PointBuffer PointBufferSet pbSetOut = writer->execute(ctx); if (isVisualize()) visualize(*pbSetOut.begin()); //visualize(*pbSetIn.begin(), *pbSetOut.begin()); return 0; }
int HeightAboveGroundKernel::execute() { // we require separate contexts for the input and ground files PointContextRef input_ctx; PointContextRef ground_ctx; // because we are appending HeightAboveGround to the input buffer, we must // register it's Dimension input_ctx.registerDim(Dimension::Id::HeightAboveGround); // StageFactory will be used to create required stages StageFactory f; // setup the reader, inferring driver type from the filename std::string reader_driver = f.inferReaderDriver(m_input_file); std::unique_ptr<Reader> input(f.createReader(reader_driver)); Options readerOptions; readerOptions.add("filename", m_input_file); input->setOptions(readerOptions); // go ahead and execute to get the PointBuffer input->prepare(input_ctx); PointBufferSet pbSetInput = input->execute(input_ctx); PointBufferPtr input_buf = *pbSetInput.begin(); PointBufferSet pbSetGround; PointBufferPtr ground_buf; if (m_use_classification) { // the user has indicated that the classification dimension exists, so // we will find all ground returns Option source("source", "import numpy as np\n" "def yow1(ins,outs):\n" " cls = ins['Classification']\n" " keep_classes = [2]\n" " keep = np.equal(cls, keep_classes[0])\n" " outs['Mask'] = keep\n" " return True\n" ); Option module("module", "MyModule"); Option function("function", "yow1"); Options opts; opts.add(source); opts.add(module); opts.add(function); // and create a PointBuffer of only ground returns std::unique_ptr<Filter> pred(f.createFilter("filters.predicate")); pred->setOptions(opts); pred->setInput(input.get()); pred->prepare(ground_ctx); pbSetGround = pred->execute(ground_ctx); ground_buf = *pbSetGround.begin(); } else { // the user has provided a file containing only ground returns, setup // the reader, inferring driver type from the filename std::string ground_driver = f.inferReaderDriver(m_ground_file); std::unique_ptr<Reader> ground(f.createReader(ground_driver)); Options ro; ro.add("filename", m_ground_file); ground->setOptions(ro); // go ahead and execute to get the PointBuffer ground->prepare(ground_ctx); pbSetGround = ground->execute(ground_ctx); ground_buf = *pbSetGround.begin(); } typedef pcl::PointXYZ PointT; typedef pcl::PointCloud<PointT> Cloud; typedef Cloud::Ptr CloudPtr; // convert the input PointBuffer to a PointCloud CloudPtr cloud(new Cloud); BOX3D const& bounds = input_buf->calculateBounds(); pclsupport::PDALtoPCD(*input_buf, *cloud, bounds); // convert the ground PointBuffer to a PointCloud CloudPtr cloud_g(new Cloud); // here, we offset the ground cloud by the input bounds so that the two are aligned pclsupport::PDALtoPCD(*ground_buf, *cloud_g, bounds); // create a set of planar coefficients with X=Y=0,Z=1 pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients()); coefficients->values.resize(4); coefficients->values[0] = coefficients->values[1] = 0; coefficients->values[2] = 1.0; coefficients->values[3] = 0; // create the filtering object and project ground returns into xy plane pcl::ProjectInliers<PointT> proj; proj.setModelType(pcl::SACMODEL_PLANE); proj.setInputCloud(cloud_g); proj.setModelCoefficients(coefficients); CloudPtr cloud_projected(new Cloud); proj.filter(*cloud_projected); // setup the KdTree pcl::KdTreeFLANN<PointT> tree; tree.setInputCloud(cloud_projected); // loop over all points in the input cloud, finding the nearest neighbor in // the ground returns (XY plane only), and calculating the difference in z int32_t k = 1; for (size_t idx = 0; idx < cloud->points.size(); ++idx) { // Search for nearesrt neighbor of the query point std::vector<int32_t> neighbors(k); std::vector<float> distances(k); PointT temp_pt = cloud->points[idx]; temp_pt.z = 0.0f; int num_neighbors = tree.nearestKSearch(temp_pt, k, neighbors, distances); double hag = cloud->points[idx].z - cloud_g->points[neighbors[0]].z; input_buf->setField(Dimension::Id::HeightAboveGround, idx, hag); } // populate BufferReader with the input PointBuffer, which now has the // HeightAboveGround dimension BufferReader bufferReader; bufferReader.addBuffer(input_buf); // we require that the output be BPF for now, to house our non-standard // dimension Options wo; wo.add("filename", m_output_file); std::unique_ptr<Writer> writer(f.createWriter("writers.bpf")); writer->setOptions(wo); writer->setInput(&bufferReader); writer->prepare(input_ctx); writer->execute(input_ctx); return 0; }
int Ground::execute() { PointContext ctx; Options readerOptions; readerOptions.add<std::string>("filename", m_inputFile); readerOptions.add<bool>("debug", isDebug()); readerOptions.add<boost::uint32_t>("verbose", getVerboseLevel()); std::unique_ptr<Stage> readerStage = makeReader(readerOptions); // go ahead and prepare/execute on reader stage only to grab input // PointBufferSet, this makes the input PointBuffer available to both the // processing pipeline and the visualizer readerStage->prepare(ctx); PointBufferSet pbSetIn = readerStage->execute(ctx); // the input PointBufferSet will be used to populate a BufferReader that is // consumed by the processing pipeline PointBufferPtr input_buffer = *pbSetIn.begin(); BufferReader bufferReader; bufferReader.setOptions(readerOptions); bufferReader.addBuffer(input_buffer); Options groundOptions; std::ostringstream ss; ss << "{"; ss << " \"pipeline\": {"; ss << " \"filters\": [{"; ss << " \"name\": \"ProgressiveMorphologicalFilter\","; ss << " \"setMaxWindowSize\": " << m_maxWindowSize << ","; ss << " \"setSlope\": " << m_slope << ","; ss << " \"setMaxDistance\": " << m_maxDistance << ","; ss << " \"setInitialDistance\": " << m_initialDistance << ","; ss << " \"setCellSize\": " << m_cellSize << ","; ss << " \"setBase\": " << m_base << ","; ss << " \"setExponential\": " << m_exponential; ss << " }]"; ss << " }"; ss << "}"; std::string json = ss.str(); groundOptions.add<std::string>("json", json); groundOptions.add<bool>("debug", isDebug()); groundOptions.add<boost::uint32_t>("verbose", getVerboseLevel()); std::unique_ptr<Stage> groundStage(new filters::PCLBlock()); groundStage->setInput(&bufferReader); groundStage->setOptions(groundOptions); // the PCLBlock groundStage consumes the BufferReader rather than the // readerStage groundStage->setInput(&bufferReader); Options writerOptions; writerOptions.add<std::string>("filename", m_outputFile); setCommonOptions(writerOptions); std::unique_ptr<Writer> writer(AppSupport::makeWriter(m_outputFile, groundStage.get())); writer->setOptions(writerOptions); std::vector<std::string> cmd = getProgressShellCommand(); UserCallback *callback = cmd.size() ? (UserCallback *)new ShellScriptCallback(cmd) : (UserCallback *)new HeartbeatCallback(); writer->setUserCallback(callback); for (auto pi: getExtraStageOptions()) { std::string name = pi.first; Options options = pi.second; std::vector<Stage*> stages = writer->findStage(name); for (auto s: stages) { Options opts = s->getOptions(); for (auto o: options.getOptions()) opts.add(o); s->setOptions(opts); } } writer->prepare(ctx); // process the data, grabbing the PointBufferSet for visualization of the // resulting PointBuffer PointBufferSet pbSetOut = writer->execute(ctx); if (isVisualize()) visualize(*pbSetOut.begin()); //visualize(*pbSetIn.begin(), *pbSetOut.begin()); return 0; }
int PCLKernel::execute() { PointContext ctx; Options readerOptions; readerOptions.add<std::string>("filename", m_inputFile); readerOptions.add<bool>("debug", isDebug()); readerOptions.add<uint32_t>("verbose", getVerboseLevel()); std::unique_ptr<Stage> readerStage = makeReader(readerOptions); // go ahead and prepare/execute on reader stage only to grab input // PointBufferSet, this makes the input PointBuffer available to both the // processing pipeline and the visualizer readerStage->prepare(ctx); PointBufferSet pbSetIn = readerStage->execute(ctx); // the input PointBufferSet will be used to populate a BufferReader that is // consumed by the processing pipeline PointBufferPtr input_buffer = *pbSetIn.begin(); BufferReader bufferReader; bufferReader.addBuffer(input_buffer); Options pclOptions; pclOptions.add<std::string>("filename", m_pclFile); pclOptions.add<bool>("debug", isDebug()); pclOptions.add<uint32_t>("verbose", getVerboseLevel()); std::unique_ptr<Stage> pclStage(new filters::PCLBlock()); pclStage->setInput(&bufferReader); pclStage->setOptions(pclOptions); // the PCLBlock stage consumes the BufferReader rather than the // readerStage Options writerOptions; writerOptions.add<std::string>("filename", m_outputFile); setCommonOptions(writerOptions); if (m_bCompress) writerOptions.add<bool>("compression", true); if (m_bForwardMetadata) writerOptions.add("forward_metadata", true); std::vector<std::string> cmd = getProgressShellCommand(); UserCallback *callback = cmd.size() ? (UserCallback *)new ShellScriptCallback(cmd) : (UserCallback *)new HeartbeatCallback(); WriterPtr writer(KernelSupport::makeWriter(m_outputFile, pclStage.get())); // Some options are inferred by makeWriter based on filename // (compression, driver type, etc). writer->setOptions(writerOptions+writer->getOptions()); writer->setUserCallback(callback); for (const auto& pi : getExtraStageOptions()) { std::string name = pi.first; Options options = pi.second; std::vector<Stage*> stages = writer->findStage(name); for (const auto& s : stages) { Options opts = s->getOptions(); for (const auto& o : options.getOptions()) opts.add(o); s->setOptions(opts); } } writer->prepare(ctx); // process the data, grabbing the PointBufferSet for visualization of the // resulting PointBuffer PointBufferSet pbSetOut = writer->execute(ctx); if (isVisualize()) visualize(*pbSetOut.begin()); //visualize(*pbSetIn.begin(), *pbSetOut.begin()); return 0; }