-
Notifications
You must be signed in to change notification settings - Fork 4
/
DataViewers.h
323 lines (273 loc) · 7.16 KB
/
DataViewers.h
1
2
3
4
5
6
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
148
149
150
151
152
153
154
155
156
157
158
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
186
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
214
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
242
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Pharmit
Copyright (c) David Ryan Koes, University of Pittsburgh and contributors.
All rights reserved.
Pharmit is licensed under both the BSD 3-clause license and the GNU
Public License version 2. Any use of the code that retains its reliance
on the GPL-licensed OpenBabel library is subject to the terms of the GPL2.
Use of the Pharmit code independently of OpenBabel (or any other
GPL2 licensed software) may choose between the BSD or GPL licenses.
See the LICENSE file provided with the distribution for more information.
*/
/*
* DataViewers.h
*
* Created on: Oct 18, 2011
* Author: dkoes
*
* Some interfaces for looking at tree data the generalize leaf (single tree)
* and node (double tree) data.
*/
#ifndef DATAVIEWERS_H_
#define DATAVIEWERS_H_
#include <vector>
#include "MappableOctTree.h"
#include "GSSTypes.h"
#include "GSSTreeStructures.h"
using namespace std;
//a slice of a dataviewer, the trees are always allocated (not mapped to data)
struct Cluster
{
vector<unsigned> indices; //indexing into a dataview
const MappableOctTree *MIV;
const MappableOctTree *MSV;
Cluster() :
MIV(NULL), MSV(NULL)
{
}
Cluster(const Cluster& rhs) :
MIV(NULL), MSV(NULL)
{
indices = rhs.indices;
if (rhs.MIV)
MIV = rhs.MIV->clone();
if (rhs.MSV)
MSV = rhs.MSV->clone();
}
Cluster(const vector<unsigned>& inds, const MappableOctTree **mivs,
const MappableOctTree **msvs) :
indices(inds)
{
MIV = MappableOctTree::createFromIntersection(inds.size(), mivs);
MSV = MappableOctTree::createFromUnion(inds.size(), msvs);
}
friend void swap(Cluster& first, Cluster& second)
{
// enable ADL (not necessary in our case, but good practice)
using std::swap;
swap(first.indices, second.indices);
swap(first.MIV, second.MIV);
swap(first.MSV, second.MSV);
}
void setToSingleton(unsigned i, const MappableOctTree* miv,
const MappableOctTree* msv)
{
clear();
indices.push_back(i);
MIV = miv->clone();
MSV = msv->clone();
}
bool isValid() const
{
return MIV != NULL && MSV != NULL;
}
unsigned size() const
{
return indices.size();
}
unsigned operator[](unsigned i) const
{
return indices[i];
}
//invalidates a and b
void mergeInto(Cluster& a, Cluster& b)
{
indices.reserve(a.indices.size() + b.indices.size());
copy(a.indices.begin(), a.indices.end(),
inserter(indices, indices.end()));
copy(b.indices.begin(), b.indices.end(),
inserter(indices, indices.end()));
const MappableOctTree *itrees[2] =
{ a.MIV, b.MIV };
MIV = MappableOctTree::createFromIntersection(2, itrees);
const MappableOctTree *utrees[2] =
{ a.MSV, b.MSV };
MSV = MappableOctTree::createFromUnion(2, utrees);
a.clear();
b.clear();
}
//invalidates a,b,c,d
void mergeInto(Cluster& a, Cluster& b, Cluster& c, Cluster& d)
{
indices.reserve(a.indices.size() + b.indices.size() + c.indices.size() + d.indices.size());
copy(a.indices.begin(), a.indices.end(),
inserter(indices, indices.end()));
copy(b.indices.begin(), b.indices.end(),
inserter(indices, indices.end()));
copy(c.indices.begin(), c.indices.end(),
inserter(indices, indices.end()));
copy(d.indices.begin(), d.indices.end(),
inserter(indices, indices.end()));
const MappableOctTree *itrees[4] =
{ a.MIV, b.MIV, c.MIV, d.MIV };
MIV = MappableOctTree::createFromIntersection(4, itrees);
const MappableOctTree *utrees[4] =
{ a.MSV, b.MSV, c.MSV, d.MSV };
MSV = MappableOctTree::createFromUnion(4, utrees);
a.clear();
b.clear();
c.clear();
d.clear();
}
void addInto(Cluster& a)
{
copy(a.indices.begin(), a.indices.end(),
inserter(indices, indices.end()));
const MappableOctTree *itrees[2] =
{ MIV, a.MIV };
MIV = MappableOctTree::createFromIntersection(2, itrees);
free((void*) itrees[0]);
const MappableOctTree *utrees[2] =
{ MSV, a.MSV };
MSV = MappableOctTree::createFromUnion(2, utrees);
free((void*) utrees[0]);
a.clear();
}
//invalidates a
void moveInto(Cluster& a)
{
swap(indices, a.indices);
MIV = a.MIV;
MSV = a.MSV;
a.MIV = NULL;
a.MSV = NULL;
a.clear();
}
~Cluster()
{
if (MIV)
free((void*) MIV);
if (MSV)
free((void*) MSV);
}
void clear()
{
if (MIV)
free((void*) MIV);
if (MSV)
free((void*) MSV);
MIV = NULL;
MSV = NULL;
indices.clear();
}
};
//a wrapper that can view single tree leaves the same as internal nodes
class DataViewer
{
protected:
const char *treeptr;
vector<file_index> pointtos; //what these trees point to (either objects or nodes)
vector<file_index> treeindices;
protected:
//create a reindices subview
DataViewer(const DataViewer& par, const vector<unsigned>& indices) :
treeptr(par.treeptr), pointtos(indices.size()), treeindices(
indices.size())
{
unsigned N = indices.size();
pointtos.resize(N);
treeindices.resize(N);
for (unsigned i = 0; i < N; i++)
{
pointtos[i] = par.pointtos[indices[i]];
treeindices[i] = par.treeindices[indices[i]];
}
}
public:
DataViewer(void *data, vector<file_index>& treei, vector<file_index>& pt) :
treeptr((const char*) data)
{
swap(pt, pointtos);
swap(treei, treeindices);
assert(pointtos.size() == treeindices.size());
pt.reserve(pointtos.size() / 2);
treei.reserve(treeindices.size() / 2);
}
virtual ~DataViewer()
{
}
//these are file ind
virtual const MappableOctTree* getMSV(unsigned i) const = 0;
virtual const MappableOctTree* getMIV(unsigned i) const = 0;
file_index getIndex(unsigned i) const
{
return pointtos[i];
}
unsigned size() const
{
return treeindices.size();
}
virtual bool isLeaf() const = 0;
virtual DataViewer* createSlice(const vector<unsigned>& indices) const = 0;
};
//this class has pointers to single trees and the actual object data
class LeafViewer: public DataViewer
{
LeafViewer(const LeafViewer& par, const vector<unsigned>& indices): DataViewer(par, indices)
{
}
public:
LeafViewer(void *data, vector<file_index>& treei, vector<file_index>& pt) :
DataViewer(data, treei, pt)
{
}
virtual const MappableOctTree* getMSV(unsigned i) const
{
return (const MappableOctTree*) &treeptr[treeindices[i]];
}
virtual const MappableOctTree* getMIV(unsigned i) const
{
return (const MappableOctTree*) &treeptr[treeindices[i]];
}
virtual bool isLeaf() const
{
return true;
}
virtual DataViewer* createSlice(const vector<unsigned>& indices) const
{
return new LeafViewer(*this, indices);
}
};
//this class has pointers to double trees and nodes
class NodeViewer: public DataViewer
{
NodeViewer(const NodeViewer& par, const vector<unsigned>& indices): DataViewer(par, indices)
{
}
public:
NodeViewer(void *data, vector<file_index>& treei, vector<file_index>& pt) :
DataViewer(data, treei, pt)
{
}
virtual const MappableOctTree* getMSV(unsigned i) const
{
const GSSDoubleTree *dbl =
(const GSSDoubleTree*) &treeptr[treeindices[i]];
return dbl->getMSV();
}
virtual const MappableOctTree* getMIV(unsigned i) const
{
const GSSDoubleTree *dbl =
(const GSSDoubleTree*) &treeptr[treeindices[i]];
return dbl->getMIV();
}
virtual bool isLeaf() const
{
return false;
}
virtual DataViewer* createSlice(const vector<unsigned>& indices) const
{
return new NodeViewer(*this, indices);
}
};
#endif /* DATAVIEWERS_H_ */