-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvoxel_grid.cpp
159 lines (137 loc) · 4.34 KB
/
voxel_grid.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
#include "voxel_grid.h"
#include <GL/glew.h>
#include <GL/glut.h>
#include <string>
#include <stdlib.h>
#include "voxel_file_format.h"
#include <iostream>
#include <stdio.h>
#include <vector>
#include "my_defs.h"
VoxelGrid::~VoxelGrid() {
for(int i=0;i<theDim[0];i++){
for(int j=0;j<theDim[2];j++){
delete[] data[i][j];
}
delete[] data[i];
}
delete[] data;
}
void VoxelGrid::loadGrid(const char* filename) {
FILE* voxel_file = fopen(filename, "rb");
if (voxel_file != NULL) {
float scaleFactor[3];
float zeroCoord[3];
//Read in Header File
voxelfile_file_header file_hdr;
fread(&file_hdr,sizeof(file_hdr),1,voxel_file);
//Read in Object Header
voxelfile_object_header object_hdr;
fread(&object_hdr,sizeof(object_hdr),1,voxel_file);
//Set Resolution
theDim[0] = object_hdr.voxel_resolution[0];
theDim[1] = object_hdr.voxel_resolution[1];
theDim[2] = object_hdr.voxel_resolution[2];
//Set Scale Factor
scaleFactor[0] = object_hdr.model_scale_factor*ADJUST_SCALE;
scaleFactor[1] = object_hdr.model_scale_factor*ADJUST_SCALE;
scaleFactor[2] = object_hdr.model_scale_factor*ADJUST_SCALE;
//Set Voxel Grid Size
voxelSize[0] = object_hdr.voxel_size[0]*scaleFactor[0];
voxelSize[1] = object_hdr.voxel_size[1]*scaleFactor[1];
voxelSize[2] = object_hdr.voxel_size[2]*scaleFactor[2];
//Set Voxel Grid Size
offset[0] = object_hdr.model_offset[0]+ADJUST_OFFSET_X;
offset[1] = object_hdr.model_offset[1]+ADJUST_OFFSET_Y;
offset[2] = object_hdr.model_offset[2]+ADJUST_OFFSET_Z;
printf("Loading Voxel Grid...");
printf("Resolution %d x %d x %d \n",theDim[0],theDim[1],theDim[2]);
printf("Voxel Size %f x %f x %f \n",voxelSize[0],voxelSize[1],voxelSize[2]);
printf("Scale Factor %f x %f x %f \n",scaleFactor[0],scaleFactor[1],scaleFactor[2]);
printf("Origin Offset %f x %f x %f \n",offset[0],offset[1],offset[2]);
float size_x = theDim[0];
float size_y = theDim[2];
float size_z = theDim[1];
data = new bool**[size_x];
for (int i = 0; i < size_x; ++i) {
data[i] = new bool*[size_y];
for (int j = 0; j < size_y; ++j) {
data[i][j] = new bool[size_z];
for (int k = 0; k < size_z; ++k) {
data[i][j][k] = false;
}
}
}
adj = new short**[theDim[0]];
for (int i = 0; i < size_x; ++i) {
adj[i] = new short*[size_y];
for (int j = 0; j < size_y; ++j) {
adj[i][j] = new short[size_z];
for (int k = 0; k < size_z; ++k) {
adj[i][j][k] = 0;
}
}
}
/* Original version
data = new bool**[theDim[0]];
for (int i = 0; i < theDim[0]; i++) {
data[i] = new bool*[theDim[2]];
for (int j = 0; j < theDim[2]; j++) {
data[i][j] = new bool[theDim[1]];
for (int k = 0; k < theDim[1]; k++) {
data[i][j][k] = false;
}
}
}
adj = new short**[theDim[0]];
for (int i = 0; i < theDim[0]; i++) {
adj[i] = new short*[theDim[2]];
for (int j = 0; j < theDim[2]; j++) {
adj[i][j] = new short[theDim[1]];
for (int k = 0; k < theDim[1]; k++) {
adj[i][j][k] = 0; // all sides have ice
}
}
}*/
char v;
voxelfile_voxel c_voxel;
//Alternative Method
int count=0;
while(count<object_hdr.num_voxels)
{
fread(&v,sizeof(char),1,voxel_file);
if(v != ASCIIVOXEL_NOVOXEL){
fread(&c_voxel,sizeof(c_voxel),1,voxel_file);
data[c_voxel.i][c_voxel.k][c_voxel.j] = true;
count++;
}
}
std::cout << " count in voxel grid " << count << std::endl;
fclose(voxel_file);
} else {
std::cout << "File Does not Exist" << std::endl;
}
}
Vector3DF VoxelGrid::getCellCenter(int i, int j, int k)
{
double x = (i + 0.5f) * voxelSize[0] + offset[0];
double y = (j + 0.5f) * voxelSize[1] + offset[1];
double z = (k + 0.5f) * voxelSize[2] + offset[2];
return Vector3DF(x,y,z);
}
Vector3DF VoxelGrid::inVoxelGrid(double x, double y, double z) {
float size_x = theDim[0];
float size_y = theDim[2];
float size_z = theDim[1];
int i = (x-offset[0])/voxelSize[0];
int j = (y-offset[1])/voxelSize[2]; // intentional switch of axis
int k = (z-offset[2])/voxelSize[1];
//if (i < 0 || j < 0 || k < 0 || i >= theDim[0]-1 || j >= theDim[2]-1 || k >= theDim[1]-1)
if (i < 0 || j < 0 || k < 0 || i >= size_x|| j >= size_y|| k >= size_z)
return Vector3DF(-1.0, -1.0, -1.0);
//std::cout << "(" << i << "," << j << "," << k << ")" << std::endl;
if(data[i][j][k])
return Vector3DF(i,j,k);
else
return Vector3DF(-1.0, -1.0, -1.0);
}