impl_attributes.hh 8.9 KB
Newer Older
Philip Trettner's avatar
Philip Trettner committed
1
2
3
4
5
6
#pragma once

#include "../Mesh.hh"

namespace polymesh
{
Philip Trettner's avatar
Philip Trettner committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
template <class tag, class AttrT>
AttrT &primitive_attribute<tag, AttrT>::operator[](handle_t h)
{
    assert(this->mMesh == h.mesh && "Handle belongs to a different mesh");
    return mData[h.idx.value];
}
template <class tag, class AttrT>
AttrT const &primitive_attribute<tag, AttrT>::operator[](handle_t h) const
{
    assert(this->mMesh == h.mesh && "Handle belongs to a different mesh");
    return mData[h.idx.value];
}
template <class tag, class AttrT>
AttrT &primitive_attribute<tag, AttrT>::operator()(handle_t h)
{
    assert(this->mMesh == h.mesh && "Handle belongs to a different mesh");
    return mData[h.idx.value];
}
template <class tag, class AttrT>
AttrT const &primitive_attribute<tag, AttrT>::operator()(handle_t h) const
{
    assert(this->mMesh == h.mesh && "Handle belongs to a different mesh");
    return mData[h.idx.value];
}

template <class AttrT>
AttrT &edge_attribute<AttrT>::operator[](halfedge_handle h)
{
    assert(this->mMesh == h.mesh && "Handle belongs to a different mesh");
    return this->mData[h.idx.value >> 1];
}
template <class AttrT>
AttrT const &edge_attribute<AttrT>::operator[](halfedge_handle h) const
{
    assert(this->mMesh == h.mesh && "Handle belongs to a different mesh");
    return this->mData[h.idx.value >> 1];
}
template <class AttrT>
AttrT &edge_attribute<AttrT>::operator()(halfedge_handle h)
{
    assert(this->mMesh == h.mesh && "Handle belongs to a different mesh");
    return this->mData[h.idx.value >> 1];
}
template <class AttrT>
AttrT const &edge_attribute<AttrT>::operator()(halfedge_handle h) const
{
    assert(this->mMesh == h.mesh && "Handle belongs to a different mesh");
    return this->mData[h.idx.value >> 1];
}

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
template <class tag, class AttrT>
void primitive_attribute<tag, AttrT>::copy_from(const std::vector<AttrT> &data)
{
    auto s = std::min((int)data.size(), this->mDataSize);
    for (auto i = 0; i < s; ++i)
        this->mData[i] = data[i];
}

template <class tag, class AttrT>
void primitive_attribute<tag, AttrT>::copy_from(const AttrT *data, int cnt)
{
    auto s = std::min(cnt, this->mDataSize);
    for (auto i = 0; i < s; ++i)
        this->mData[i] = data[i];
}

Philip Trettner's avatar
Philip Trettner committed
73
74
75
76
77
78
79
80
template <class tag, class AttrT>
void primitive_attribute<tag, AttrT>::copy_from(attribute<AttrT> const &data)
{
    auto s = std::min(data.mDataSize, this->mDataSize);
    for (auto i = 0; i < s; ++i)
        this->mData[i] = data.mData[i];
}

81
82
83
84
85
86
87
template <class tag, class AttrT>
void primitive_attribute<tag, AttrT>::apply_remapping(const std::vector<int> &map)
{
    for (auto i = 0u; i < map.size(); ++i)
        this->mData[i] = this->mData[map[i]];
}

88
89
90
91
92
93
94
95
template <class tag, class AttrT>
void primitive_attribute<tag, AttrT>::apply_transpositions(std::vector<std::pair<int, int>> const &ts)
{
    using std::swap;
    for (auto t : ts)
        swap(this->mData[t.first], this->mData[t.second]);
}

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
template <class tag, class AttrT>
primitive_attribute<tag, AttrT>::primitive_attribute(primitive_attribute const &rhs) noexcept : primitive_attribute_base<tag>(rhs.mMesh) // copy
{
    this->mDefaultValue = rhs.mDefaultValue;
    this->mData = rhs.mData;
    this->mDataSize = rhs.mDataSize;

    this->register_attr();
}

template <class tag, class AttrT>
primitive_attribute<tag, AttrT>::primitive_attribute(primitive_attribute &&rhs) noexcept : primitive_attribute_base<tag>(rhs.mMesh) // move
{
    this->mDefaultValue = std::move(rhs.mDefaultValue);
    this->mData = std::move(rhs.mData);
    this->mDataSize = rhs.mDataSize;

    rhs.deregister_attr();
    this->register_attr();
}

template <class tag, class AttrT>
primitive_attribute<tag, AttrT> &primitive_attribute<tag, AttrT>::operator=(primitive_attribute const &rhs) noexcept // copy
{
    this->deregister_attr();

    this->mMesh = rhs.mMesh;
    this->mDefaultValue = rhs.mDefaultValue;
    this->mData = rhs.mData;
    this->mDataSize = rhs.mDataSize;

    this->register_attr();

    return *this;
}

template <class tag, class AttrT>
primitive_attribute<tag, AttrT> &primitive_attribute<tag, AttrT>::operator=(primitive_attribute &&rhs) noexcept // move
{
    this->deregister_attr();

    this->mMesh = rhs.mMesh;
    this->mDefaultValue = std::move(rhs.mDefaultValue);
    this->mData = std::move(rhs.mData);
    this->mDataSize = rhs.mDataSize;

    rhs.deregister_attr();
    this->register_attr();

    return *this;
}

Philip Trettner's avatar
Philip Trettner committed
148
149
150
151
152
153
154
155
156
157
inline void Mesh::register_attr(primitive_attribute_base<vertex_tag> *attr) const
{
    // insert in front
    auto nextAttrs = mVertexAttrs;
    mVertexAttrs = attr;
    attr->mNextAttribute = nextAttrs;
    if (nextAttrs)
        nextAttrs->mPrevAttribute = attr;

    // resize attr
158
    attr->resize(all_vertices().size(), false);
Philip Trettner's avatar
Philip Trettner committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
}

inline void Mesh::deregister_attr(primitive_attribute_base<vertex_tag> *attr) const
{
    if (attr->mPrevAttribute)
        attr->mPrevAttribute->mNextAttribute = attr->mNextAttribute;

    if (attr->mNextAttribute)
        attr->mNextAttribute->mPrevAttribute = attr->mPrevAttribute;

    if (mVertexAttrs == attr)
        mVertexAttrs = attr->mNextAttribute;

    attr->mNextAttribute = nullptr;
    attr->mPrevAttribute = nullptr;
}

inline void Mesh::register_attr(primitive_attribute_base<face_tag> *attr) const
{
    // insert in front
    auto nextAttrs = mFaceAttrs;
    mFaceAttrs = attr;
    attr->mNextAttribute = nextAttrs;
    if (nextAttrs)
        nextAttrs->mPrevAttribute = attr;

    // resize attr
186
    attr->resize(all_faces().size(), false);
Philip Trettner's avatar
Philip Trettner committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
}

inline void Mesh::deregister_attr(primitive_attribute_base<face_tag> *attr) const
{
    if (attr->mPrevAttribute)
        attr->mPrevAttribute->mNextAttribute = attr->mNextAttribute;

    if (attr->mNextAttribute)
        attr->mNextAttribute->mPrevAttribute = attr->mPrevAttribute;

    if (mFaceAttrs == attr)
        mFaceAttrs = attr->mNextAttribute;

    attr->mNextAttribute = nullptr;
    attr->mPrevAttribute = nullptr;
}

inline void Mesh::register_attr(primitive_attribute_base<edge_tag> *attr) const
{
    // insert in front
    auto nextAttrs = mEdgeAttrs;
    mEdgeAttrs = attr;
    attr->mNextAttribute = nextAttrs;
    if (nextAttrs)
        nextAttrs->mPrevAttribute = attr;

    // resize attr
214
    attr->resize(all_edges().size(), false);
Philip Trettner's avatar
Philip Trettner committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
}

inline void Mesh::deregister_attr(primitive_attribute_base<edge_tag> *attr) const
{
    if (attr->mPrevAttribute)
        attr->mPrevAttribute->mNextAttribute = attr->mNextAttribute;

    if (attr->mNextAttribute)
        attr->mNextAttribute->mPrevAttribute = attr->mPrevAttribute;

    if (mEdgeAttrs == attr)
        mEdgeAttrs = attr->mNextAttribute;

    attr->mNextAttribute = nullptr;
    attr->mPrevAttribute = nullptr;
}

inline void Mesh::register_attr(primitive_attribute_base<halfedge_tag> *attr) const
{
    // insert in front
    auto nextAttrs = mHalfedgeAttrs;
    mHalfedgeAttrs = attr;
    attr->mNextAttribute = nextAttrs;
    if (nextAttrs)
        nextAttrs->mPrevAttribute = attr;

    // resize attr
242
    attr->resize(all_halfedges().size(), false);
Philip Trettner's avatar
Philip Trettner committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
}

inline void Mesh::deregister_attr(primitive_attribute_base<halfedge_tag> *attr) const
{
    if (attr->mPrevAttribute)
        attr->mPrevAttribute->mNextAttribute = attr->mNextAttribute;

    if (attr->mNextAttribute)
        attr->mNextAttribute->mPrevAttribute = attr->mPrevAttribute;

    if (mHalfedgeAttrs == attr)
        mHalfedgeAttrs = attr->mNextAttribute;

    attr->mNextAttribute = nullptr;
    attr->mPrevAttribute = nullptr;
}

template <class tag>
void primitive_attribute_base<tag>::register_attr()
{
    mMesh->register_attr(this);
}

template <class tag>
void primitive_attribute_base<tag>::deregister_attr()
{
    if (mMesh)
    {
        mMesh->deregister_attr(this);
        mMesh = nullptr;
    }
}

template <class tag, class AttrT>
primitive_attribute<tag, AttrT>::primitive_attribute(const Mesh *mesh, const AttrT &def_value)
  : primitive_attribute_base<tag>(mesh), mDefaultValue(def_value)
{
    this->register_attr();
}

template <class tag, class AttrT>
int primitive_attribute<tag, AttrT>::size() const
{
Philip Trettner's avatar
Philip Trettner committed
286
    return primitive<tag>::all_collection_of(*this->mMesh).size();
Philip Trettner's avatar
Philip Trettner committed
287
288
289
290
291
}

template <class tag, class AttrT>
void primitive_attribute<tag, AttrT>::clear(AttrT const &value)
{
Philip Trettner's avatar
Philip Trettner committed
292
    this->mData.clear(value);
Philip Trettner's avatar
Philip Trettner committed
293
294
295
296
297
298
299
300
301
302
}

template <class tag, class AttrT>
void primitive_attribute<tag, AttrT>::clear()
{
    this->clear(this->mDefaultValue);
}

template <class tag, class AttrT>
template <class FuncT>
Philip Trettner's avatar
Philip Trettner committed
303
auto primitive_attribute<tag, AttrT>::map(FuncT &&f) const -> attribute<tmp::decayed_result_type_of<FuncT, AttrT>>
Philip Trettner's avatar
Philip Trettner committed
304
{
Philip Trettner's avatar
Philip Trettner committed
305
    auto attr = primitive<tag>::all_collection_of(*this->mMesh).template make_attribute<tmp::decayed_result_type_of<FuncT, AttrT>>();
Philip Trettner's avatar
Philip Trettner committed
306
307
308
309
310
311
312
    auto s = size();
    auto d_in = data();
    auto d_out = attr.data();
    for (auto i = 0; i < s; ++i)
        d_out[i] = f(d_in[i]);
    return attr; // copy elison
}
Philip Trettner's avatar
Philip Trettner committed
313
314
315
316
317
318
319
320

template <class tag, class AttrT>
template <class FuncT>
void primitive_attribute<tag, AttrT>::apply(FuncT &&f)
{
    auto s = size();
    auto d = data();
    for (auto i = 0; i < s; ++i)
321
        f(d[i]);
Philip Trettner's avatar
Philip Trettner committed
322
}
Philip Trettner's avatar
Philip Trettner committed
323
}