-
Notifications
You must be signed in to change notification settings - Fork 0
/
Octree.cpp
470 lines (417 loc) · 10.2 KB
/
Octree.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Cloud of Voxels (CoV) project
* Author: mcidclan, m [.D'O'T.] cid [.D'O'T.] clan [.A'T.] gmail [.D'O'T.] com
* Creation Date: 2011
* Modification Date: 2020
*/
#include "./headers/Octree.h"
extern Vec3<SI> atom[18];
extern Vec3<SI> shell[54];
extern Vec3<SI> shellxl[150];
float Octree::size;
float Octree::half;
Octree::Octree()
{
this->accelerated = false;
}
Octree::~Octree()
{
delete [] this->root->facescenter;
delete this->root;
if(Options::ACCELERATED)
{
this->destroyAccelerator();
}
}
/*
*
*/
void Octree::destroyAccelerator()
{
UI i = 0, j =0;
while(i < Options::OCTREE_SIZE)
{
j = 0;
while(j < Options::OCTREE_SIZE)
{
delete [] this->accelerator[i][j];
j++;
}
delete [] this->accelerator[i];
i++;
}
delete [] this->accelerator;
}
/*
*
*/
void Octree::createAccelerator()
{
double msize = (pow(Options::OCTREE_SIZE, 3.0)*4.0) / pow(1000.0, 3);
printf("Trying to allocate %f GB for accelerator\n", msize);
this->accelerator = new Octant***[Options::OCTREE_SIZE];
UI i = 0, j = 0;
while(i < Options::OCTREE_SIZE)
{
this->accelerator[i] = new Octant**[Options::OCTREE_SIZE];
j = 0;
while(j < Options::OCTREE_SIZE)
{
this->accelerator[i][j] = new Octant*[Options::OCTREE_SIZE];
j++;
}
i++;
}
printf("Accelerator created\n");
}
/*
* fillAccelerator
*/
void Octree::fillAccelerator()
{
printf("Filling the accelerator, please wait...\n");
int i = 0, j = 0, k = 0;
const SI OCTREE_HALF_SIZE = Options::OCTREE_SIZE / 2;
while(i < Options::OCTREE_SIZE)
{
j = 0;
const SI x = i - OCTREE_HALF_SIZE;
while(j < Options::OCTREE_SIZE)
{
k = 0;
const SI y = j - OCTREE_HALF_SIZE;
while(k < Options::OCTREE_SIZE)
{
const SI z = k - OCTREE_HALF_SIZE;
this->accelerator[i][j][k] =
OctantManager::getBit(this->root, {x, y, z});
k++;
}
j++;
}
i++;
}
printf("Accelerator filled\n");
}
void Octree::initAccelerator()
{
this->createAccelerator();
this->fillAccelerator();
this->accelerated = true;
}
/*
* initRoot
*/
void Octree::initRoot(SUI size, SUI maxdepth, const SI raylength)
{
this->root = new Octant();
OctantManager::init(this->root);
this->root->depth = maxdepth;
this->root->half = size/2;
this->root->center.x =
this->root->center.y =
this->root->center.z = 0.0f;
OctantManager::setFacesCenter(this->root);
this->ray = NULL;
this->raylength = raylength;
this->colordepthstep = (1.0f/(float)raylength);
Octree::half = this->root->half;
Octree::size = Octree::half*2.0f;
}
/*
* initBasis
*/
void Octree::initBasis(Mat3f* const basis)
{
this->basis = basis;
this->kbase = &(basis->k);
}
/*
* setRay
*/
void Octree::setRay(Vec3<float>* const ray) {
this->ray = ray;
}
/*
* getBit
*/
Octant* Octree::getBit()
{
if(this->accelerated)
{
const SI x = (this->ray->x + Octree::half);
const SI y = (this->ray->y + Octree::half);
const SI z = (this->ray->z + Octree::half);
if(x >= 0 && x < Options::OCTREE_SIZE &&
y >= 0 && y < Options::OCTREE_SIZE &&
z >= 0 && z < Options::OCTREE_SIZE)
{
return this->accelerator[x][y][z];
}
}
return OctantManager::getBit(this->root, {
(SI)this->ray->x,
(SI)this->ray->y,
(SI)this->ray->z
});
}
/*
* getNextBit
*/
Octant* Octree::getNextBit(float* axis, const float side)
{
const float d = *axis;
*axis = ((SI)d) + side;
Octant* const curbit = this->getBit();
*axis = d;
return curbit;
}
/*
* avoidScanGlitches
*/
void Octree::avoidScanGlitches(Octant** const curbit)
{
if(Options::AVOID_SCAN_GLITCHES)
{
if((*curbit)->voxel == NULL) {
*curbit = getNextBit(&this->ray->x, 1.0f);
}
if((*curbit)->voxel == NULL) {
*curbit = getNextBit(&this->ray->y, 1.0f);
}
if((*curbit)->voxel == NULL) {
*curbit = getNextBit(&this->ray->z, 1.0f);
}
if((*curbit)->voxel == NULL) {
*curbit = getNextBit(&this->ray->x, -1.0f);
}
if((*curbit)->voxel == NULL) {
*curbit = getNextBit(&this->ray->y, -1.0f);
}
if((*curbit)->voxel == NULL) {
*curbit = getNextBit(&this->ray->z, -1.0f);
}
}
}
/*
* rayTrace
*/
bool Octree::rayTrace(vector<DynamicVoxel>* const voxels)
{
if(this->ray != NULL)
{
float depthray = 0.0f;
while(depthray < (float)this->raylength)
{
// Searches for the deepest available octant from the root,
// corresponding to the current ray position
Octant* curbit = this->getBit();
this->avoidScanGlitches(&curbit);
if(curbit->voxel != NULL)
{
if(!Options::TRANSPARENCY || (curbit->voxel->color & 0x000000FF) == 0xFF)
{
voxels->push_back({depthray, curbit->voxel});
return true;
}
if(Options::TRANSPARENCY)
{
if(voxels->size() == 0 || (voxels->size() > 0 && (depthray -
(*voxels)[voxels->size()-1].depth) >= Options::MIN_VOXELS_PROXIMITY))
{
voxels->push_back({depthray, curbit->voxel});
}
}
}
// Calculates the new ray position
this->getNextEntryDot(curbit);
depthray += this->raystep;
}
}
return voxels->size() > 0;
}
/*
* rayToBorder
*/
void Octree::rayToBorder(const float a, const float b, const float c)
{
if(c != 0.0f)
{
// Distance between the current ray position and
// the next potential octant limit position
const float d = math::absf(b - a) / math::absf(c);
if(d < this->raystep) {
this->raystep = d;
}
}
}
/*
* getNextEntryDot
*/
void Octree::getNextEntryDot(Octant* octant)
{
if(this->ray != NULL)
{
this->raystep = Octree::size;
rayToBorder(this->ray->x, octant->facescenter[
(this->kbase->x > 0.0f) ? 1 : 0], this->kbase->x);
rayToBorder(this->ray->y, octant->facescenter[
(this->kbase->y > 0.0f) ? 3 : 2], this->kbase->y);
rayToBorder(this->ray->z, octant->facescenter[
(this->kbase->z > 0.0f) ? 5 : 4], this->kbase->z);
if(this->raystep < RAYSTEP_MIN_UNIT)
{
this->raystep = RAYSTEP_MIN_UNIT;
}
this->ray->x += this->kbase->x * this->raystep;
this->ray->y += this->kbase->y * this->raystep;
this->ray->z += this->kbase->z * this->raystep;
}
}
/*
* addVoxels
*/
void Octree::addVoxels(Voxel* voxels, const UI nvoxel)
{
UI i = 0;
UC percent = 0;
const UI step = nvoxel / 100;
while(i < nvoxel)
{
if((step > 0) && (i % (step * 10) == 0))
{
printf(">%d ", percent);
percent+=10;
}
if(Options::SMOOTH_SIBLINGS)
{
this->addSmooths(&voxels[i]);
} else this->addSingleVoxel(&voxels[i]);
switch(Options::VOXEL_SHELL_TYPE)
{
case 1:
this->addShell(voxels[i]);
break;
case 2:
this->addShellXL(voxels[i]);
break;
case 3:
this->addShellLite(voxels[i]);
break;
}
i++;
}
printf("\n");
}
template<typename T>
void Octree::initBit(T voxel)
{
OctantManager::initBit(this->root, voxel);
}
/*
* add single voxel
*/
void Octree::addSingleVoxel(Voxel* const voxel)
{
this->initBit(voxel);
}
/*
* addSmooths
*/
void Octree::addSmooths(Voxel* const voxel)
{
UC i = 0;
while(i < 18)
{
Voxel v = *voxel;
v.coordinates.x += atom[i].x;
v.coordinates.y += atom[i].y;
v.coordinates.z += atom[i].z;
this->initBit(v);
i++;
}
this->initBit(voxel);
}
/*
* addSmooths
*/
void Octree::addShellLite(const Voxel voxel)
{
UC i = 0;
while(i < Options::SHELL_LITE_THICKNESS)
{
Voxel v = voxel;
math::vecxscl(&v.coordinates, 1.05f + i * 0.02f);
if(Options::SHELL_COLOR > 0)
{
v.color = Options::SHELL_COLOR;
}
else v.color = (v.color & 0xFFFFFF00) | 0x0F;
this->initBit(v);
i++;
}
}
/*
* addShellXL
*/
void Octree::addShellXL(const Voxel voxel)
{
UC i = 0;
while(i < 150)
{
Voxel v = voxel;
v.coordinates.x += shellxl[i].x;
v.coordinates.y += shellxl[i].y;
v.coordinates.z += shellxl[i].z;
if(Options::SHELL_COLOR > 0)
{
v.color = Options::SHELL_COLOR;
}
else v.color = (v.color & 0xFFFFFF00) | 0x0F;
this->initBit(v);
i++;
}
}
/*
* addShell
*/
void Octree::addShell(const Voxel voxel)
{
UC i = 0;
while(i < 54)
{
Voxel v = voxel;
v.coordinates.x += shell[i].x;
v.coordinates.y += shell[i].y;
v.coordinates.z += shell[i].z;
if(Options::SHELL_COLOR > 0)
{
v.color = Options::SHELL_COLOR;
}
else v.color = (v.color & 0xFFFFFF00) | 0x0F;
this->initBit(v);
i++;
}
}
/*
* setColorDepth
*/
Color Octree::getColorDepth(const DynamicVoxel* const dynamic)
{
const float darkness = getDarkness(dynamic->depth);
return {
(UC)(((dynamic->voxel->color & 0xFF000000) >> 24) * darkness),
(UC)(((dynamic->voxel->color & 0x00FF0000) >> 16) * darkness),
(UC)(((dynamic->voxel->color & 0x0000FF00) >> 8) * darkness),
(UC)(dynamic->voxel->color & 0x000000FF)
};
}
Color Octree::getColorDepth(const UC r, const UC g, const UC b, const float depth)
{
const float darkness = getDarkness(depth);
return {(UC)(r * darkness), (UC)(g * darkness), (UC)(b * darkness), 0};
}
float Octree::getDarkness(const float depth)
{
return 1.0f - depth * this->colordepthstep;
}