/
Density.C
421 lines (345 loc) · 12.9 KB
/
Density.C
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// $Revision$ $Author$ $Date$
/// \brief Convert an xyz point set into a volume density
/// Uses a voxel representation
/***************************************************************************
* INCLUDES
***************************************************************************/
#include <cassert>
#include <cstdlib>
#include <cstdio>
// C++ includes
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string> // Good STL data types.
#include <vector>
// Local includes
#include "VolHeader.H"
#include "Density.H"
using namespace std;
/***************************************************************************
* LOCAL MACROS and DEFINES
***************************************************************************/
#ifdef __GNUC__
#define UNUSED __attribute((__unused__))
#else
/*! \def UNUSED
\brief GNU CC attribute to denote unused paramters in function calls.
The attribute remove compiler warning for unused arguments and variable. Only works
for GNU compilers such as gcc and g++.
*/
#define UNUSED
#endif
#ifdef REGRESSION_TEST
#define FAILED_HERE cout << __FILE__ << ":" << __LINE__ << " test failed" << endl
#else
/*! \def FAILED_HERE
\brief Used FAILED_HERE; to emit a string that looks like a compiler warning
Use this to allow emacs to jump to this source line with C-x `
*/
#define FAILED_HERE // Empty
#endif
/***************************************************************************
* GLOBALS
***************************************************************************/
/// Let the debugger find out which version is being used.
static const UNUSED char* RCSid ="@(#) $Id$";
//####################################################################
// DENSITY METHODS
//####################################################################
Density::Density() {
cout << "FIX: make sure that later, things get setup ok. Density()" << endl;
}
void
Density::resize(const size_t _width, const size_t _height, const size_t _depth,
const float minX, const float maxX,
const float minY, const float maxY,
const float minZ, const float maxZ)
{
assert (_width < 10000); // Would be bad to be much larger
assert (_height < 10000); // Would be bad to be much larger
assert (_depth < 10000); // Would be bad to be much larger
assert (minX < maxX);
assert (minY < maxY);
assert (minZ < maxZ);
width = _width;
height = _height;
depth = _depth;
dx = (maxX-minX)/width;
dy = (maxY-minY)/height;
dz = (maxZ-minZ)/depth;
xR[0]=minX;xR[1]=maxX;
yR[0]=minY;yR[1]=maxY;
zR[0]=minZ;zR[1]=maxZ;
counts.resize(width*height*depth,0); // set all to zero
//for (size_t i=0;i<counts.size();i++) counts[i]=0;
totalPointsInside=0;
return;
} // resize()
Density::Density(const size_t _width, const size_t _height, const size_t _depth,
const float minX, const float maxX,
const float minY, const float maxY,
const float minZ, const float maxZ)
{
resize(_width, _height, _depth, minX,maxX, minY,maxY, minZ,maxZ);
}
bool
Density::addPoint(const float x, const float y, const float z) {
const size_t cellNum=getCell(x,y,z);
if (cellNum==badValue()) {
#ifdef REGRESSION_TEST
cout << " Point outside volume: " << x << " " << y << " " << z << endl;
#endif
return (false); // Outside!!
}
assert (cellNum<counts.size());
counts[cellNum]++;
totalPointsInside++;
return(true);
}
void Density::printCellCounts()const {
cout << "# " << endl
<< "# i cx cy cz counts x y z " << endl
<< "# " << endl
<< "# " << endl
;
for (size_t i=0; i<width*height*depth;i++) {
float x,y,z; getCellCenter(i,x,y,z);
size_t cx,cy,cz;
getCellXYZ(i,cx,cy,cz);
cout << i << " " << counts[i]
<< " " << cx << " " << cy << " " << cz
<< " " << x << " " << y << " " << z
<< endl;
}
}
size_t Density::getCellFromWHD(const size_t xIndex, const size_t yIndex, const size_t zIndex) const {
const size_t zOff = zIndex * getWidth() * getHeight();
const size_t yOff = yIndex * getWidth();
const size_t off = xIndex + yOff + zOff;
if (!isValidCell(off)) {
cerr << "WARNING: getCellFromWHD out of bounds" << endl;
FAILED_HERE;
return (badValue());
}
return (off);
}
size_t
Density::getCell(const float x, const float y, const float z) const {
if (!(xR[0] <= x && x <= xR[1])) return (badValue()); // Outside
if (!(yR[0] <= y && y <= yR[1])) return (badValue()); // Outside
if (!(zR[0] <= z && z <= zR[1])) return (badValue()); // Outside
const size_t xIndex = getCellX(x); //size_t((x-xR[0])/dx);
const size_t yIndex = getCellY(y); //size_t((y-yR[0])/dy);
const size_t zIndex = getCellZ(z); //size_t((z-zR[0])/dz);
const size_t zOff = zIndex * getWidth() * getHeight();
const size_t yOff = yIndex * getWidth();
const size_t off = xIndex + yOff + zOff;
assert (off < 1e9); // FIX: get rid of this contrain as someday this may be ok (64bit machines baby!)
// wait, wasn't the dec alpha a 64bit machine in 1992? Yet we've regressed.
return (off);
}
void Density::getCellXYZ(const size_t index, size_t &cx, size_t &cy, size_t &cz) const {
assert (isValidCell(index));
assert (index<1000000);
cz = index/(getWidth() * getHeight());
const size_t i2=index-cz*(getWidth() * getHeight());
assert (i2<100000);
cy = i2/getWidth();
cx = i2 - cy*getWidth();
//cout << "getCellXYZ: " << index << " " << cx << " " << cy << " " << cz << endl;
assert (cx<100000);
assert (cy<100000);
assert (cz<100000);
}
size_t Density::getCellNeighbor(const size_t i, NeighborEnum which) const {
assert (isValidCell(i));
size_t cx,cy,cz;
getCellXYZ (i,cx,cy,cz);
size_t cxNew=cx,cyNew=cy,czNew=cz;
switch (which) {
case LEFT: cxNew = cx - 1; if (cxNew>cx) return (badValue()); break;
case RIGHT: cxNew = cx + 1; if (getWidth() <= cxNew) return (badValue()); break;
case FRONT: cyNew = cy - 1; if (cyNew>cy) return (badValue()); break;
case BACK: cyNew = cy + 1; if (getHeight() <= cyNew) return (badValue()); break;
case BELOW: czNew = cz - 1; if (czNew>cz) return (badValue()); break;
case ABOVE: czNew = cz + 1; if (getDepth() <= czNew) return (badValue()); break;
default:
assert(false);
}
return (getCellFromWHD(cxNew,cyNew,czNew));
} // getCellNeighbor
void Density::getCellCenter(const size_t cellNum, float &x, float &y, float &z) const {
assert(isValidCell(cellNum));
size_t cx, cy, cz; // number of cells from the origin
getCellXYZ(cellNum,cx,cy,cz);
//cout << "cIndex: " << cx << " " <<cy<<" "<<cz<<endl;
assert (cx<1000000);
assert (cy<1000000);
assert (cz<1000000);
x = xR[0] + (cx+0.5) * dx;
y = yR[0] + (cy+0.5) * dy;
z = zR[0] + (cz+0.5) * dz;
}
bool Density::writeVol(const string &filename) {
FILE *o=fopen(filename.c_str(),"wb");
if (!o) {perror("failed to open output file");cerr << " " << filename << endl;return(false);}
{
VolHeader hdr(getWidth(),getHeight(),getDepth());
hdr.write(o);
}
const size_t min=getMinCount();
const size_t max=getMaxCount();
// http://doc.coin3d.org/SIMVoleon/classSoVolumeData.html#a1
for (size_t i=0;i<counts.size();i++) {
// FIX: how do we fit the data?
const unsigned char data=scaleCount(i,min,max);
#ifdef REGRESSION_TEST
cout << " writing: " << i<<"("<<counts[i]<<") -> " << int(data)
<< " (" << min <<","<<max<<")"<<endl;
#endif
// No endian issue with 1 byte data
if (1!=fwrite(&data,sizeof(data),1,o)) {perror("write failed");fclose(o);return(false);}
}
if (0!=fclose(o)) {perror("close failed... bizarre");return(false);}
return (true);
}
unsigned char Density::scaleCount(const size_t i, const size_t min, const size_t max) const {
const float _0to1 = float(counts[i]-min)/(max-min);
const unsigned char r = (unsigned char)(_0to1 * std::numeric_limits<unsigned char>::max());
return (r);
}
size_t Density::getMaxCount() const {
size_t max = std::numeric_limits<size_t>::min();
for (size_t i=0;i<counts.size();i++) if (counts[i]>max) max=counts[i];
return (max);
}
size_t Density::getMinCount() const {
size_t min = std::numeric_limits<size_t>::max();
for (size_t i=0;i<counts.size();i++) if (counts[i]<min) min=counts[i];
return(min);
}
//####################################################################
// TEST CODE
//####################################################################
#ifdef REGRESSION_TEST
bool test1() {
cout << " test1" << endl;
Density d(1,1,1, 0.,1., 0.,1., 0.,1.);
if (0!=d.getCountInside()) {FAILED_HERE;return(false);}
if (0!=d.getCell(0.1,0.1,0.1)) {FAILED_HERE;return(false);}
if (0!=d.getCell(0.9,0.9,0.9)) {FAILED_HERE;return(false);}
if (!d.addPoint(0.5,0.5,0.5)) {FAILED_HERE;return(false);}
if (1!=d.getCountInside()) {FAILED_HERE;return(false);}
if (d.addPoint(5,0.5,0.5)) {FAILED_HERE;return(false);}
if (1!=d.getCountInside()) {FAILED_HERE;return(false);}
if (d.addPoint(.5,5,0.5)) {FAILED_HERE;return(false);}
if (1!=d.getCountInside()) {FAILED_HERE;return(false);}
if (d.addPoint(.5,0.5,5)) {FAILED_HERE;return(false);}
if (1!=d.getCountInside()) {FAILED_HERE;return(false);}
if (1!=d.getWidth()) {FAILED_HERE;return(false);}
if (1!=d.getHeight()) {FAILED_HERE;return(false);}
if (1!=d.getDepth()) {FAILED_HERE;return(false);}
cout << " cell counts: " << endl;
d.printCellCounts();
return (true);
} // test1
bool test2() {
cout << " test2" << endl;
Density d(2,1,1, 0.,2., 0.,1., 0.,1.);
if (0!=d.getCountInside()) {FAILED_HERE;return(false);}
d.addPoint(0.5,.1,.1);
if (1!=d.getCellCount(0)) {FAILED_HERE;return(false);}
if (0!=d.getCellCount(1)) {FAILED_HERE;return(false);}
d.addPoint(1.5,.1,.1);
if (1!=d.getCellCount(0)) {FAILED_HERE;return(false);}
if (1!=d.getCellCount(1)) {FAILED_HERE;return(false);}
#if 0
{
cout << "Danger: "<< endl;
Density dx(10,1,1, -5,5., 0.,1., 0.,1.);
for (size_t i=0;i<10;i++) {
float x,y,z;
dx.getCellCenter(i,x,y,z);
cout << " " << x << " " << y << " " << z << endl;
}
}
#endif
#if 0
{
cout << "Danger: Y"<< endl;
Density dy(1,10,1, 0,1., -5.,5., 0.,1.);
for (size_t i=0;i<10;i++) {
float x,y,z;
dy.getCellCenter(i,x,y,z);
cout << " Y: " << x << " " << y << " " << z << endl;
}
}
#endif
#if 0
{
cout << "Danger: Z"<< endl;
Density dy(1,1,10, 0,1., 0.,1., -5.,5.);
for (size_t i=0;i<10;i++) {
float x,y,z;
dy.getCellCenter(i,x,y,z);
cout << " Y: " << x << " " << y << " " << z << endl;
}
}
#endif
return(true);
} // test2
bool test3() {
cout << " test3" << endl;
Density d(2,2,2, 0.,2., 0.,2., 0.,2.);
d.addPoint(0.1,0.1,0.1); d.addPoint(0.1,0.1,0.1);
d.addPoint(1.5,0.1,0.1); d.addPoint(0.1,1.5,0.1);
for (size_t i=0;i<10;i++) d.addPoint(1.5,1.5,1.5);
if (!d.writeVol(string("test3.vol"))) {FAILED_HERE;return(false);}
return(true);
}
bool test4() {
bool ok=true;
{
// This is a pretty small/simple test case
Density d;
d.resize(1,2,3, 0.,2., 0.,2., 0.,2.);
if (1!=d.getWidth()) {ok=false;FAILED_HERE;}
if (2!=d.getHeight()) {ok=false;FAILED_HERE;}
if (3!=d.getDepth()) {ok=false;FAILED_HERE;}
if (6!=d.getSize()) {ok=false;FAILED_HERE;}
if (0!=d.getCellFromWHD(0,0,0)) {ok=false; FAILED_HERE;}
if (1!=d.getCellFromWHD(0,1,0)) {ok=false;FAILED_HERE;}
if (Density::badValue()!=d.getCellNeighbor(0,Density::LEFT)) {ok=false;FAILED_HERE;}
if (Density::badValue()!=d.getCellNeighbor(0,Density::RIGHT)) {ok=false;FAILED_HERE;}
if (Density::badValue()!=d.getCellNeighbor(0,Density::FRONT)) {ok=false;FAILED_HERE;}
if (1!=d.getCellNeighbor(0,Density::BACK)) {ok=false;FAILED_HERE;}
if (Density::badValue()!=d.getCellNeighbor(0,Density::BELOW)) {ok=false;FAILED_HERE;}
if (2!=d.getCellNeighbor(0,Density::ABOVE)) {ok=false;FAILED_HERE;}
}
//Density(string("filename")); //FIX: Implement and test
return(ok);
}
int main (UNUSED int argc, char *argv[]) {
// Put test code here
bool ok=true;
cout << " Size of Density (in bytes): " << sizeof(Density) << endl;
cout << " Size of VolHeader (in bytes): " << sizeof(VolHeader) << endl;
if (52 != sizeof(VolHeader)) {FAILED_HERE;ok=false;}
cout << " Size of float: " << sizeof(1.f) << endl;
cout << " Size of double: " << sizeof(1.) << endl;
if (4 != sizeof(1.f)) {FAILED_HERE;ok=false;}
if (8 != sizeof(1.)) {FAILED_HERE;ok=false;}
#ifdef BIGENDIAN
if (0x00010203!=hton_uint32(0x00010203)) {FAILED_HERE;ok=false;}
#endif
if (4!=sizeof(float)) {FAILED_HERE;ok=false;} // Must be 4 for vol_header
if (4!=sizeof(uint32_t)) {FAILED_HERE;ok=false;} // Must be 4 for vol_header
if (!test1()) {FAILED_HERE;ok=false;}
if (!test2()) {FAILED_HERE;ok=false;}
if (!test3()) {FAILED_HERE;ok=false;} // test writing
if (!test4()) {FAILED_HERE;ok=false;}
cout << " " << argv[0] << " test: " << (ok?"ok":"failed")<<endl;
return (ok?EXIT_SUCCESS:EXIT_FAILURE);
}
#endif // REGRESSION_TEST