Debian Codegraph @

include/deal.II/base/bounding_box_data_out.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 2023 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_bounding_box_data_out_h
16 #define dealii_bounding_box_data_out_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/data_out_base.h>
21 #include <deal.II/base/geometry_info.h>
22 #include <deal.II/base/iterator_range.h>
23 #include <deal.II/base/point.h>
24
25 #include <deal.II/boost_adaptors/bounding_box.h>
26 #include <deal.II/boost_adaptors/point.h>
27 #include <deal.II/boost_adaptors/segment.h>
28
29 #include <boost/geometry/index/rtree.hpp>
30 #include <boost/geometry/strategies/strategies.hpp>
31
32
33 DEAL_II_NAMESPACE_OPEN
34
35 /**
36 * This class generates graphical output for BoundingBox objects, starting from
37 * any object that can be converted by boost to a BoundingBox.
38 */
39 template <int dim>
40 class BoundingBoxDataOut : public DataOutInterface<dim, dim>
41 {
42 public:
43 /**
44 * Constructor.
45 */
46 BoundingBoxDataOut() = default;
47
48 /**
49 * Destructor.
50 */
51 ~BoundingBoxDataOut() = default;
52
53 /**
54 * Generate patches from a range of objects that can be converted by boost to
55 * a collection of BoundingBox objects.
56 *
57 * You could pass to this function iterators to BoundingBox objects, or to
58 * pairs or tuples in which the first element is a BoundingBox object.
59 */
60 template <class ConvertibleToBoundingBoxIterator>
61 void
62 build_patches(const ConvertibleToBoundingBoxIterator &begin,
63 const ConvertibleToBoundingBoxIterator &end);
64
65 /**
66 * Generate patches from a container of objects that can be converted by boost
67 * to a collection of BoundingBox objects.
68 *
69 * You could pass to this function a container of BoundingBox objects, or a
70 * container of pairs or tuples in which the first element is a BoundingBox
71 * object.
72 */
73 template <class Container>
74 void
75 build_patches(const Container &boxes);
76
77 /**
78 * Attach data to each output patch that was generated by build_patches().
79 *
80 * The @p datasets parameter is expected to have the same size of the
81 * container you used in the call to build_patches(), and each entry should
82 * have the same size of the @p dataset_names argument.
83 *
84 * @param[in] datasets The actual data to attach to each patch
85 * @param[in] dataset_names The name of each component of the dataset
86 */
87 void
88 add_datasets(const std::vector<std::vector<double>> &datasets,
89 const std::vector<std::string> &dataset_names);
90
91 protected:
92 // Copy doc
93 virtual const std::vector<dealii::DataOutBase::Patch<dim, dim>> &
94 get_patches() const override;
95
96 // Copy doc
97 virtual std::vector<std::string>
98 get_dataset_names() const override;
99
100 private:
101 /**
102 * The actual boxes.
103 */
104 std::vector<DataOutBase::Patch<dim, dim>> patches;
105
106 /**
107 * Names of datasets.
108 */
109 std::vector<std::string> dataset_names;
110 };
111
112
113 // Template and inline functions
114 #ifndef DOXYGEN
115 template <int dim>
116 template <class ConvertibleToBoundingBoxIterator>
117 void
118 BoundingBoxDataOut<dim>::build_patches(
119 const ConvertibleToBoundingBoxIterator &begin,
120 const ConvertibleToBoundingBoxIterator &end)
121 {
122 using Getter = boost::geometry::index::indexable<
123 typename ConvertibleToBoundingBoxIterator::value_type>;
124 Getter getter;
125 constexpr unsigned int boxdim =
126 boost::geometry::dimension<typename Getter::result_type>::value;
127 const unsigned int N = std::distance(begin, end);
128 static_assert(boxdim == dim, "Bounding boxes are of the wrong dimension!");
129
130 dataset_names.clear();
131 patches.resize(N);
132
133 unsigned int i = 0;
134 for (const auto &value :
135 IteratorRange<ConvertibleToBoundingBoxIterator>(begin, end))
136 {
137 BoundingBox<dim> box;
138 boost::geometry::convert(getter(*value), box);
139 for (unsigned int v = 0; v < GeometryInfo<dim>::vertices_per_cell; ++v)
140 {
141 patches[i].vertices[v] = box.vertex(v);
142 patches[i].patch_index = i;
143 patches[i].n_subdivisions = 1;
144 patches[i].reference_cell = ReferenceCells::get_hypercube<dim>();
145 patches[i].points_are_available = false;
146 }
147 ++i;
148 }
149 }
150
151
152
153 template <int dim>
154 template <class Container>
155 void
156 BoundingBoxDataOut<dim>::build_patches(const Container &boxes)
157 {
158 build_patches(boxes.begin(), boxes.end());
159 }
160
161
162
163 template <int dim>
164 void
165 BoundingBoxDataOut<dim>::add_datasets(
166 const std::vector<std::vector<double>> &datasets,
167 const std::vector<std::string> &names)
168 {
169 AssertDimension(datasets.size(), patches.size());
170 dataset_names = names;
171 for (unsigned int i = 0; i < datasets.size(); ++i)
172 {
173 AssertDimension(datasets[i].size(), names.size());
174 patches[i].data.reinit(names.size(),
175 GeometryInfo<dim>::vertices_per_cell);
176 for (unsigned int j = 0; j < names.size(); ++j)
177 for (unsigned int k = 0; k < GeometryInfo<dim>::vertices_per_cell; ++k)
178 patches[i].data(j, k) = datasets[i][j];
179 }
180 }
181
182
183
184 template <int dim>
185 const std::vector<DataOutBase::Patch<dim, dim>> &
186 BoundingBoxDataOut<dim>::get_patches() const
187 {
188 return patches;
189 }
190
191
192
193 template <int dim>
194 std::vector<std::string>
195 BoundingBoxDataOut<dim>::get_dataset_names() const
196 {
197 return dataset_names;
198 }
199
200 #endif
201
202 DEAL_II_NAMESPACE_CLOSE
203
204 #endif