codac 2.0.0
Loading...
Searching...
No Matches
codac2_Slice.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_SliceBase.h"
14
15namespace codac2
16{
17 template<class T>
18 class SlicedTube;
19
20 template<class T>
21 class Slice : public SliceBase,
22 protected T
23 // T class inheritance is protected, because modifying a
24 // Slice's codomain can affect adjacent gates if they exist
25 {
26 public:
27
28 explicit Slice(const SlicedTubeBase& tube, const std::list<TSlice>::iterator& it_tslice, const T& codomain)
29 : SliceBase(tube, it_tslice), T(codomain)
30 { }
31
32 Slice(const Slice& s, const SlicedTubeBase& tube)
33 : SliceBase(tube, s._it_tslice), T(s.codomain())
34 { }
35
36 // Slice objects cannot be copyable or movable,
37 // as they are supposed to be connected to other Slice objects
38 Slice(const Slice& s) = delete;
39 Slice& operator=(const Slice&) = delete;
40 Slice(Slice&&) = delete;
41 Slice& operator=(Slice&&) = delete;
42
43 inline const SlicedTube<T>& tube() const
44 {
45 return static_cast<const SlicedTube<T>&>(_tube);
46 }
47
48 inline virtual std::shared_ptr<SliceBase> copy() const
49 {
50 return std::make_shared<Slice>(*this, this->_tube);
51 }
52
53 inline Index size() const
54 {
55 return this->T::size();
56 }
57
58 inline T& codomain()
59 {
60 return (T&)(*this);
61 }
62
63 inline const T& codomain() const
64 {
65 return (const T&)(*this);
66 }
67
68 inline bool is_gate() const
69 {
70 return t0_tf().is_degenerated();
71 }
72
73 inline std::shared_ptr<const Slice<T>> prev_slice() const
74 {
75 return std::static_pointer_cast<const Slice<T>>(
76 this->SliceBase::prev_slice());
77 }
78
79 inline std::shared_ptr<Slice<T>> prev_slice()
80 {
81 return std::const_pointer_cast<Slice<T>>(
82 static_cast<const Slice<T>&>(*this).prev_slice());
83 }
84
85 inline std::shared_ptr<const Slice<T>> next_slice() const
86 {
87 return std::static_pointer_cast<const Slice<T>>(
88 this->SliceBase::next_slice());
89 }
90
91 inline std::shared_ptr<Slice<T>> next_slice()
92 {
93 return std::const_pointer_cast<Slice<T>>(
94 static_cast<const Slice<T>&>(*this).next_slice());
95 }
96
97 inline T input_gate() const
98 {
99 if(!prev_slice())
100 return codomain();
101
102 else
103 {
104 if(prev_slice()->is_gate())
105 return prev_slice()->codomain();
106 else
107 return codomain() & prev_slice()->codomain();
108 }
109 }
110
111 inline T output_gate() const
112 {
113 if(!next_slice())
114 return codomain();
115
116 else
117 {
118 if(next_slice()->is_gate())
119 return next_slice()->codomain();
120 else
121 return codomain() & next_slice()->codomain();
122 }
123 }
124
125 inline std::pair<T,T> enclosed_bounds(const Interval& t) const
126 {
127 T x = *this; x.set_empty();
128 auto bounds = std::make_pair(x,x);
129
130 if(t.lb() < t0_tf().lb() || t.ub() > t0_tf().ub())
131 {
132 x.init(Interval(-oo,0));
133 bounds.first |= x;
134 x.init(Interval(0,oo));
135 bounds.second |= x;
136 }
137
138 if(t.contains(t0_tf().lb()))
139 {
140 bounds.first |= input_gate().lb();
141 bounds.second |= input_gate().ub();
142 }
143
144 if(t.contains(t0_tf().ub()))
145 {
146 bounds.first |= output_gate().lb();
147 bounds.second |= output_gate().ub();
148 }
149
150 if(t.is_subset(t0_tf()) && t != t0_tf().lb() && t != t0_tf().ub())
151 {
152 bounds.first |= this->lb();
153 bounds.second |= this->ub();
154 }
155
156 return bounds;
157 }
158
159 inline void set(const T& x, bool propagate = true)
160 {
161 assert_release(x.size() == this->size());
162 codomain() = x;
163 if(propagate)
164 update_adjacent_codomains();
165 }
166
167 inline void init()
168 {
169 this->T::init();
170 // Nothing to propagate to adjacent codomains
171 }
172
173 inline void set_empty()
174 {
175 set_empty(true);
176 }
177
178 inline bool operator==(const Slice& x) const
179 {
180 return codomain() == x.codomain();
181 }
182
183 inline ConvexPolygon polygon_slice(const Slice<T>& v) const
184 requires std::is_same_v<T,Interval>;
185 // -> is defined in codac2_Slice_polygon.h file
186
187 friend inline std::ostream& operator<<(std::ostream& os, const Slice& x)
188 {
189 os << x.t0_tf()
190 << "↦" << x.codomain()
191 << std::flush;
192 return os;
193 }
194
195 protected:
196
197 template<typename T_>
198 friend class SlicedTube;
199
200 inline void set_empty(bool propagate)
201 {
202 this->T::set_empty();
203 if(propagate)
204 update_adjacent_codomains();
205 }
206
207 inline void update_adjacent_codomains()
208 {
209 if(prev_slice())
210 {
211 assert(prev_slice()->size() == this->size());
212 if(is_gate())
213 codomain() &= prev_slice()->codomain();
214 else if(prev_slice()->is_gate())
215 prev_slice()->codomain() &= codomain();
216 }
217
218 if(next_slice())
219 {
220 assert(next_slice()->size() == this->size());
221 if(is_gate())
222 codomain() &= next_slice()->codomain();
223 else if(next_slice()->is_gate())
224 next_slice()->codomain() &= codomain();
225 }
226 }
227 };
228}
229
230#include "codac2_Slice_polygon.h"
void set_empty()
Marks the interval matrix as empty.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:299
auto lb() const
Returns a matrix containing the lower bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:91
auto ub() const
Returns a matrix containing the upper bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:103
Definition codac2_OctaSym.h:21