-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilter.cpp
152 lines (134 loc) · 4.01 KB
/
Filter.cpp
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
#include "Dataset.h"
#include "Filter.h"
///////////////////////////////////////////////////////////////////////////////
Filter::Filter() :
myRangeStamp(0),
myIndexStamp(0),
myNumFields(0),
myIndices(NULL),
myIndexLen(-1)
{
memset(myField, 0, sizeof(myField));
myUpdater.start(1);
}
///////////////////////////////////////////////////////////////////////////////
Filter::~Filter()
{
myUpdater.stop();
}
///////////////////////////////////////////////////////////////////////////////
void Filter::setField(uint index, Field* f)
{
if(f != NULL) {
myField[index] = f;
myMin[index] = f->getDimension()->floatRangeMin;
myMax[index] = f->getDimension()->floatRangeMax;
// Count the number of consecutive valid fields.
myNumFields = 0;
foreach(Field* f, myField) if(f != NULL) myNumFields++; else break;
}
}
///////////////////////////////////////////////////////////////////////////////
void Filter::setRange(uint index, float fmin, float fmax)
{
myMin[index] = fmin;
myMax[index] = fmax;
myRangeStamp = otimestamp();
myUpdater.clearQueue();
myUpdater.queue(this);
}
///////////////////////////////////////////////////////////////////////////////
void Filter::setNormalizedRange(uint index, float fmin, float fmax)
{
float fm = myField[index]->range()[0];
float fM = myField[index]->range()[1];
float delta = fM - fm;
myMin[index] = fm + delta * fmin;
myMax[index] = fm + delta * fmax;
myRangeStamp = otimestamp();
myUpdater.clearQueue();
myUpdater.queue(this);
}
///////////////////////////////////////////////////////////////////////////////
template<typename T>
void Filter::filterKernel(double timestamp)
{
uint sz = static_cast<uint>(myField[0]->domain.length);
uint* indices = (uint*)malloc(sz * sizeof(uint));
memset(indices, 0, sz * sizeof(uint));
uint len = 0;
for(uint i = 0; i < sz; i++)
{
// if the filter stamp was updated, we are processing stale data. exit now.
if(myRangeStamp > timestamp)
{
free(indices);
return;
}
bool pass = true;
for(uint j = 0; j < myNumFields; j++)
{
T* d = (T*)myField[j]->data;
if(d[i] < myMin[j] || d[i] > myMax[j])
{
pass = false;
break;
}
}
if(pass)
{
indices[len] = i; len++;
}
}
// Done filtering. copy the new indices over the old ones.
myLock.lock();
if(myIndices != NULL) free(myIndices);
myIndices = indices;
myIndexLen = len;
//ofmsg("index generated - length = %1%", %myIndexLen);
myIndexStamp = otimestamp();
myLock.unlock();
}
///////////////////////////////////////////////////////////////////////////////
void Filter::execute(WorkerTask::TaskInfo* ti)
{
if(myNumFields == 0)
{
myIndexLen = -1;
return;
}
//double curStamp = myRangeStamp;
// If any field is not loaded, queue for loading and exit.
foreach(Field* f, myField)
{
if(f != NULL && !f->loaded)
{
f->getDimension()->dataset->load(f);
return;
}
}
if(Dataset::useDoublePrecision()) filterKernel<double>(ti->getTimestamp());
else filterKernel<float>(ti->getTimestamp());
}
///////////////////////////////////////////////////////////////////////////////
void Filter::update()
{
}
///////////////////////////////////////////////////////////////////////////////
GpuBuffer* Filter::getIndexBuffer(const DrawContext& dc)
{
if(myGpuBuffer(dc) == NULL)
{
myGpuBuffer(dc) = dc.gpuContext->createVertexBuffer();
myGpuBuffer(dc)->setType(GpuBuffer::IndexData);
}
// Do we need to update the gpu buffer with new data?
if(myGpuBuffer.stamp(dc) < myIndexStamp)
{
myLock.lock();
myGpuBuffer.stamp(dc) = myIndexStamp;
myGpuBuffer(dc)->setData(myIndexLen * sizeof(uint), myIndices);
myLock.unlock();
}
return myGpuBuffer(dc);
}