OGS
FindCellsForPoints.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <vtkCellData.h>
7#include <vtkCellTreeLocator.h>
8#include <vtkCharArray.h>
9#include <vtkExtractCells.h>
10#if VTK_VERSION_STRIPPED < 940
11#include <vtkIdFilter.h>
12#else
13#include <vtkGenerateIds.h>
14#endif
15#include <vtkIdList.h>
16#include <vtkNew.h>
17#include <vtkUnstructuredGrid.h>
18
19#include <Eigen/Core>
20#include <array>
21#include <range/v3/view/iota.hpp>
22#include <range/v3/view/transform.hpp>
23#include <vector>
24
25#include "BaseLib/Logging.h"
26
27namespace ApplicationUtils
28{
38{
39public:
40 void initialize(vtkUnstructuredGrid* const bulk_mesh)
41 {
42#if VTK_VERSION_STRIPPED < 940
43 vtkNew<vtkIdFilter> id_filter;
44 id_filter->SetInputData(bulk_mesh);
45 id_filter->SetCellIdsArrayName("bulk_element_ids");
46 id_filter->SetPointIds(false);
47 id_filter->SetCellIds(true);
48 id_filter->Update();
49 bulk_mesh_ = id_filter->GetOutput();
50#else
51 vtkNew<vtkGenerateIds> generate_ids;
52 generate_ids->SetInputData(bulk_mesh);
53 generate_ids->SetCellIdsArrayName("bulk_element_ids");
54 generate_ids->PointIdsOff();
55 generate_ids->CellIdsOn();
56 generate_ids->Update();
58 static_cast<vtkUnstructuredGrid*>(generate_ids->GetOutput());
59#endif
60 locator_ = vtkSmartPointer<vtkCellTreeLocator>::New();
61 locator_->SetDataSet(bulk_mesh_);
62 locator_->BuildLocator();
63 }
64
65 std::vector<vtkIdType> findCellIds(Eigen::Vector3d const& coords,
66 double const tolerance)
67 {
68 using namespace ranges;
69 auto const cell_ids = findCellIdsImpl(coords, tolerance);
70 return views::iota(0, cell_ids->GetNumberOfIds()) |
71 views::transform([&](vtkIdType i)
72 { return cell_ids->GetId(i); }) |
73 to_vector;
74 }
75
76 vtkSmartPointer<vtkUnstructuredGrid> find(Eigen::Vector3d const& coords,
77 double const tolerance)
78 {
79 auto const cell_ids = findCellIdsImpl(coords, tolerance);
80
81 vtkNew<vtkExtractCells> extract_cells;
82 extract_cells->SetInputData(bulk_mesh_);
83 extract_cells->SetCellList(cell_ids);
84 extract_cells->Update();
85 auto const filtered_bulk_mesh = extract_cells->GetOutput();
86
87 return filtered_bulk_mesh;
88 }
89
90private:
91 vtkSmartPointer<vtkIdList> findCellIdsImpl(Eigen::Vector3d const& coords,
92 double const tolerance)
93 {
94 std::array<double, 6> bbox = {
95 coords[0] - tolerance, coords[0] + tolerance,
96 coords[1] - tolerance, coords[1] + tolerance,
97 coords[2] - tolerance, coords[2] + tolerance};
98
99 vtkNew<vtkIdList> cell_ids;
100 locator_->FindCellsWithinBounds(bbox.data(), cell_ids);
101
102 return cell_ids;
103 }
104
105 vtkSmartPointer<vtkDataSet> bulk_mesh_;
106 vtkSmartPointer<vtkCellTreeLocator> locator_;
107};
108
109} // namespace ApplicationUtils
std::vector< vtkIdType > findCellIds(Eigen::Vector3d const &coords, double const tolerance)
vtkSmartPointer< vtkDataSet > bulk_mesh_
vtkSmartPointer< vtkIdList > findCellIdsImpl(Eigen::Vector3d const &coords, double const tolerance)
vtkSmartPointer< vtkCellTreeLocator > locator_
vtkSmartPointer< vtkUnstructuredGrid > find(Eigen::Vector3d const &coords, double const tolerance)
void initialize(vtkUnstructuredGrid *const bulk_mesh)