View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

vec_template.h
Go to the documentation of this file.
1#ifndef VEC_TEMPLATE_H
2#define VEC_TEMPLATE_H
3
4#include "scalars.h"
6
7#include <limits>
8#include <type_traits>
9
10#include <boost/mpl/size.hpp>
11
12namespace vistle {
13
14template<class T, int Dim>
16{
17 refreshImpl();
18}
19
20template<class T, int Dim>
21Vec<T, Dim>::Vec(Data *data): Vec::Base(data)
22{
23 refreshImpl();
24}
25
26template<class T, int Dim>
27Vec<T, Dim>::Vec(const Index size, const Meta &meta): Vec::Base(Data::create(size, meta))
28{
29 refreshImpl();
30}
31
32template<class T, int Dim>
35 for (int c = 0; c < Dim; ++c) {
36 d()->x[c] = ShmVector<T>();
37 d()->x[c].construct();
38 }
39 refresh();
41
42template<class T, int Dim>
44{
45 for (int c = 0; c < Dim; ++c) {
46 if (d()->x[c].valid()) {
47 d()->x[c]->resize(size);
48 } else {
49 d()->x[c].construct(size);
50 }
51 }
52 refresh();
53}
54
55template<class T, int Dim>
57{
58 auto s = Vec<T, Dim>::as(src);
59 if (!s)
60 return false;
61
62 for (int c = 0; c < Dim; ++c) {
63 x(c)[to] = s->x(c)[from];
64 }
65
66 return true;
67}
68
69template<class T, int Dim>
70void Vec<T, Dim>::setValue(Index idx, int component, const double &value)
71{
72 assert(component >= 0);
73 assert(component < Dim);
74 x(component)[idx] = value;
75}
77template<class T, int Dim>
78double Vec<T, Dim>::value(Index idx, int component) const
79{
80 assert(component >= 0);
81 assert(component < Dim);
82 return static_cast<double>(x(component)[idx]);
83}
84
85template<class T, int Dim>
87{
88 if (auto str = StructuredGridBase::as(grid)) {
89 auto m = guessMapping(shared_from_this());
90 for (int c = 0; c < Dim; ++c) {
91 if (m == DataBase::Vertex) {
92 d()->x[c]->setDimensionHint(str->getNumDivisions(0), str->getNumDivisions(1), str->getNumDivisions(2));
93 } else if (m == DataBase::Element) {
94 d()->x[c]->setDimensionHint(str->getNumDivisions(0) - 1,
95 std::max<Index>(1, str->getNumDivisions(1) - 1),
96 std::max<Index>(1, str->getNumDivisions(2) - 1));
97 }
98 }
99 } else {
100 for (int c = 0; c < Dim; ++c)
101 d()->x[c]->clearDimensionHint();
102 }
103}
104
105template<class T, int Dim>
106void Vec<T, Dim>::setExact(bool exact)
107{
108 d()->setExact(exact);
109}
110
111template<class T, int Dim>
112void Vec<T, Dim>::refreshImpl() const
113{
114 const Data *d = static_cast<Data *>(m_data);
115
116 for (int c = 0; c < Dim; ++c) {
117 m_x[c] = (d && d->x[c].valid()) ? d->x[c]->data() : nullptr;
118 }
119 for (int c = Dim; c < MaxDim; ++c) {
120 m_x[c] = nullptr;
121 }
122 m_size = (d && d->x[0]) ? d->x[0]->size() : 0;
123}
124
125template<class T, int Dim>
126bool Vec<T, Dim>::isEmpty() const
127{
128 return getSize() == 0;
129}
130
131template<class T, int Dim>
132bool Vec<T, Dim>::isEmpty()
133{
134 return getSize() == 0;
135}
136
137template<class T, int Dim>
138bool Vec<T, Dim>::checkImpl() const
139{
140 size_t size = d()->x[0]->size();
141 for (int c = 0; c < Dim; ++c) {
142 V_CHECK(d()->x[c]->check());
143 V_CHECK(d()->x[c]->size() == size);
144 if (size > 0) {
145 V_CHECK((d()->x[c])->at(0) * (d()->x[c])->at(0) >= 0)
146 V_CHECK((d()->x[c])->at(size - 1) * (d()->x[c])->at(size - 1) >= 0)
147 }
148 }
149
150 return true;
151}
152
153template<class T, int Dim>
154void Vec<T, Dim>::updateInternals()
155{
156 if (!d()->boundsValid())
157 d()->updateBounds();
158}
159
160template<class T, int Dim>
162{
163 if (d()->boundsValid()) {
164 return std::make_pair(d()->min, d()->max);
165 }
166
167 T smax = std::numeric_limits<T>::max();
168 T smin = std::numeric_limits<T>::lowest();
169 Vector min, max;
170 Index sz = getSize();
171 for (int c = 0; c < Dim; ++c) {
172 min[c] = smax;
173 max[c] = smin;
174 const T *d = &x(c)[0];
175 for (Index i = 0; i < sz; ++i) {
176 if (d[i] < min[c])
177 min[c] = d[i];
178 if (d[i] > max[c])
179 max[c] = d[i];
180 }
181 }
182 return std::make_pair(min, max);
183}
184
185template<class T, int Dim>
187{
188 invalidateBounds();
189}
190
191template<class T, int Dim>
192bool Vec<T, Dim>::Data::boundsValid() const
193{
194 for (int c = 0; c < Dim; ++c)
195 if (min[c] > max[c])
196 return false;
197 return true;
198}
199
200template<class T, int Dim>
201void Vec<T, Dim>::Data::invalidateBounds()
202{
203 for (int c = 0; c < Dim; ++c) {
204 min[c] = std::numeric_limits<T>::max();
205 max[c] = std::numeric_limits<T>::lowest();
206 }
207}
208
209template<class T, int Dim>
210void Vec<T, Dim>::Data::updateBounds()
211{
212 invalidateBounds();
213 for (int c = 0; c < Dim; ++c) {
214 Index sz = x[c]->size();
215 const T *d = x[c]->data();
216 for (Index i = 0; i < sz; ++i) {
217 if (d[i] < min[c])
218 min[c] = d[i];
219 if (d[i] > max[c])
220 max[c] = d[i];
221 }
222 }
223}
224
225template<class T, int Dim>
227{
228 for (int c = 0; c < Dim; ++c)
229 if (x[c])
230 x[c]->setExact(exact);
231}
232
233template<class T, int Dim>
234Vec<T, Dim>::Data::Data(const Index size, const std::string &name, const Meta &m)
235: Vec<T, Dim>::Base::Data(Vec<T, Dim>::type(), name, m)
236{
237 initData();
238
239 for (int c = 0; c < Dim; ++c)
240 x[c].construct(size);
241}
242
243template<class T, int Dim>
245{
246 static const size_t pos = boost::mpl::find<Scalars, T>::type::pos::value;
247 static_assert(pos < boost::mpl::size<Scalars>::value, "Scalar type not found");
248 return (Object::Type)(Object::VEC + (MaxDim + 1) * pos + Dim);
249}
250
251template<class T, int Dim>
252Vec<T, Dim>::Data::Data(const Index size, Type id, const std::string &name, const Meta &m)
253: Vec<T, Dim>::Base::Data(id, name, m)
254{
255 initData();
256
257 for (int c = 0; c < Dim; ++c)
258 x[c].construct(size);
259}
260
261template<class T, int Dim>
262Vec<T, Dim>::Data::Data(const Data &o, const std::string &n, Type id)
263: Vec<T, Dim>::Base::Data(o, n, id == Object::UNKNOWN ? o.type : id)
264{
265 initData();
266
267 for (int c = 0; c < Dim; ++c)
268 x[c] = o.x[c];
269}
270
271template<class T, int Dim>
273{
274 std::string name = Shm::the().createObjectId();
275 Data *t = shm<Data>::construct(name)(size, name, meta);
276 publish(t);
277
278 return t;
279}
280
281#if 0
283#else
284template<class T, int Dim>
285Vec<T, Dim>::Data::Data(Object::Type id, const std::string &name, const Meta &m): Vec<T, Dim>::Base::Data(id, name, m)
286{
287 initData();
288}
289
290template<class T, int Dim>
291typename Vec<T, Dim>::Data *Vec<T, Dim>::Data::createNamed(Object::Type id, const std::string &name, const Meta &meta)
292{
293 Data *t = shm<Data>::construct(name)(id, name, meta);
294 publish(t);
295 return t;
296}
297#endif
298
299} // namespace vistle
300
301#endif
Definition: objectmeta.h:16
Definition: object.h:58
std::shared_ptr< const Object > const_ptr
Definition: object.h:68
static void publish(const Data *d)
Definition: object.cpp:150
const Meta & meta() const
Definition: object.cpp:390
Type
Definition: object.h:84
@ UNKNOWN
Definition: object.h:89
@ VEC
Definition: object.h:114
std::string createObjectId(const std::string &name="")
Definition: shm.cpp:435
static Shm & the()
Definition: shm.cpp:315
Definition: vec.h:13
Vec(const Index size, const Meta &meta=Meta())
Definition: vec_template.h:15
VistleVector< Scalar, Dim > Vector
Definition: vec.h:24
array & x(int c=0)
Definition: vec.h:42
void setExact(bool exact) override
Definition: vec_template.h:106
Definition: shm_reference.h:15
void resize(const size_t size)
Definition: shm_array.h:134
T * data() const
Definition: shm_array.h:91
static T min(T a, T b)
Definition: messages.cpp:28
Definition: allobjects.cpp:30
Vector3 Vector
Definition: vector.h:36
uint32_t Index
Definition: index.h:13
#define V_OBJECT_CREATE_NAMED(ObjType)
Definition: object.h:542
#define V_CHECK(true_expr)
use in checkImpl
Definition: object.h:368
Definition: object.h:233
Definition: vec.h:84
static Data * createNamed(Object::Type id, const std::string &name, const Meta &meta=Meta())
Definition: vec_template.h:291
static Data * create(Index size=0, const Meta &meta=Meta())
Definition: vec_template.h:272
Data(const Index size=0, const std::string &name="", const Meta &meta=Meta())
Definition: vec_template.h:234
ShmVector< T > x[Dim]
Definition: vec.h:87
static managed_shm::segment_manager::template construct_proxy< T >::type construct(const std::string &name)
Definition: shm_impl.h:87