codac 2.0.0
Loading...
Searching...
No Matches
codac2_Paving.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <map>
13#include <memory>
14#include "codac2_PavingNode.h"
15
16namespace codac2
17{
18 template<typename P>
19 class PavingNode;
20
21 template<typename P>
22 class Subpaving;
23
24 template<typename P,typename... X>
25 requires (std::is_same_v<X,IntervalVector> && ...)
26 class Paving : public Domain
27 {
28 public:
29
30 using Node_ = std::shared_ptr<const PavingNode<P>>;
31 using NodeTuple_ = std::tuple<X...>;
32 using NodeValue_ = std::function<std::list<IntervalVector>(Node_)>;
33 using ConnectedSubset_ = Subpaving<P>;
34
35 protected:
36
37 Paving()
38 { }
39
40 public:
41
42 inline Index size() const
43 {
44 return std::get<0>(_tree->boxes()).size();
45 }
46
47 inline std::shared_ptr<const PavingNode<P>> tree() const
48 {
49 return _tree;
50 }
51
52 inline std::shared_ptr<PavingNode<P>> tree()
53 {
54 return std::const_pointer_cast<PavingNode<P>>(const_cast<const Paving<P,X...>*>(this)->tree());
55 }
56
57 inline std::list<IntervalVector> boxes(const NodeValue_& node_value) const
58 {
59 return boxes(node_value, IntervalVector(size()));
60 }
61
62 inline std::list<IntervalVector> boxes(const NodeValue_& node_value, const IntervalVector& intersecting_box) const
63 {
64 std::list<IntervalVector> l;
65
66 this->tree()->visit([&]
67 (Node_ n)
68 {
69 for(const auto& bi : node_value(n))
70 if(bi.intersects(intersecting_box))
71 l.push_back(bi);
72 return n->hull().intersects(intersecting_box);
73 });
74
75 if(l.empty())
76 l.push_back(IntervalVector::empty(this->size()));
77 return l;
78 }
79
80 inline double volume(const NodeValue_& node_value) const
81 {
82 double v = 0;
83 for(const auto& bi : this->boxes(node_value))
84 v += bi.volume();
85 return v;
86 }
87
88 inline std::list<ConnectedSubset_> connected_subsets(const NodeValue_& node_value) const
89 {
90 return connected_subsets(IntervalVector(size()), node_value);
91 }
92
93 inline std::list<ConnectedSubset_> connected_subsets(const IntervalVector& x0, const NodeValue_& node_value) const
94 {
95 std::list<IntervalVector> l_boxes = boxes(node_value, x0);
96 [[maybe_unused]] Index nb_boxes = l_boxes.size();
97 std::list<ConnectedSubset_> l_subsets;
98
99 while(!l_boxes.empty())
100 {
101 auto current_box = l_boxes.front();
102 l_subsets.push_back({ current_box });
103 l_boxes.pop_front();
104
105 std::list<IntervalVector> l_neighbouring_boxes_to_visit { current_box };
106
107 do
108 {
109 current_box = l_neighbouring_boxes_to_visit.front();
110 l_neighbouring_boxes_to_visit.pop_front();
111
112 for(const auto& ni : boxes(node_value, current_box))
113 {
114 if(std::find(l_boxes.begin(), l_boxes.end(), ni) != l_boxes.end())
115 {
116 l_boxes.remove(ni);
117 l_neighbouring_boxes_to_visit.push_back(ni);
118 l_subsets.back().push_back(ni);
119 }
120 }
121
122 } while(!l_neighbouring_boxes_to_visit.empty());
123 }
124
125 assert(l_boxes.empty() && "all the nodes should have been visited");
126 assert([&]() -> bool { Index s = 0; for(const auto& si : l_subsets) s += si.size(); return s == nb_boxes; } ()
127 && "the total number of boxes should match the sum of number of boxes of each subset");
128
129 return l_subsets;
130 }
131
132 protected:
133
134 friend class PavingNode<P>;
135
136 inline void init_tree(const IntervalVector& x)
137 {
138 _tree = std::make_shared<PavingNode<P>>(*static_cast<P*>(this), x);
139 }
140
141 inline static NodeTuple_ init_tuple(const IntervalVector& x)
142 {
143 return std::make_tuple(((X)x)...);
144 }
145
146 std::shared_ptr<PavingNode<P>> _tree; // must be a shared_ptr to allow enable_shared_from_this
147 };
148
149
150 class PavingOut;
151 using PavingOut_Node = PavingNode<PavingOut>;
152
153 class PavingOut : public Paving<PavingOut,IntervalVector>
154 {
155 public:
156
157 PavingOut(Index n);
158 PavingOut(const IntervalVector& x);
159
160 std::list<PavingOut::ConnectedSubset_> connected_subsets(const PavingOut::NodeValue_& node_value = PavingOut::outer) const;
161 std::list<PavingOut::ConnectedSubset_> connected_subsets(const IntervalVector& x0, const PavingOut::NodeValue_& node_value = PavingOut::outer) const;
162
163 inline IntervalVector operator&(const IntervalVector& x) const
164 {
165 assert_release(x.size() == this->size());
166
167 IntervalVector x_ = IntervalVector::empty(x.size());
168
169 if(x.is_empty())
170 return x_;
171
172 // The parts of the box that are outside of the paving can not be contracted
173 auto d = x.diff(this->tree()->hull());
174 for(const auto& di : d)
175 x_ |= di;
176
177 this->tree()->visit([&]
178 (Node_ n)
179 {
180 for(const auto& bi : PavingOut::outer(n))
181 x_ |= bi & x;
182 return n->hull().intersects(x);
183 });
184 assert(x_.is_subset(x));
185 return x_;
186 }
187
188 static const NodeValue_ outer, outer_complem;
189 };
190
191 IntervalVector operator&(const IntervalVector& x, const PavingOut& p);
192
193
194 class PavingInOut;
195 using PavingInOut_Node = PavingNode<PavingInOut>;
196
197 class PavingInOut : public Paving<PavingInOut,IntervalVector,IntervalVector>
198 {
199 public:
200
201 PavingInOut(Index n);
202 PavingInOut(const IntervalVector& x);
203
204 std::list<PavingInOut::ConnectedSubset_> connected_subsets(const PavingInOut::NodeValue_& node_value = PavingInOut::outer) const;
205 std::list<PavingInOut::ConnectedSubset_> connected_subsets(const IntervalVector& x0, const PavingInOut::NodeValue_& node_value = PavingInOut::outer) const;
206
207 static const NodeValue_ outer, outer_complem, inner, bound, all;
208 };
209}
double volume() const
Computes the volume of the interval matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:33
Definition codac2_OctaSym.h:21
Eigen::Matrix< Interval,-1, 1 > IntervalVector
Alias for a dynamic-size column vector of intervals.
Definition codac2_IntervalVector.h:25
CtcInterType< typenameC1::ContractedTypes >::Ctc operator&(const C1 &c1, const C2 &c2)
Builds an intersection contractor from two contractors.
Definition codac2_CtcInter.h:230