-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfileIO.h
226 lines (203 loc) · 6.21 KB
/
fileIO.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
//-------------------------------------------------------------------
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files ( the "Software" ), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall
// be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
//-------------------------------------------------------------------
//-------------------------------------------------------------------
#ifndef _FILEIO_H_
#define _FILEIO_H_
#include <cstdio>
#include <vector>
#include <string>
#include <fstream>
#include "vec3.h"
using std::vector;
using std::string;
void writeNrrdFile(string filename, vector<vector<vector<double> > > &myArray, vec3 myDim, vec3 mySpacing)
{
std::cout << "Writing file '" << filename << "'" << std::endl;
std::ofstream nrrd_file(filename.c_str(), std::ofstream::binary);
if(nrrd_file.is_open())
{
nrrd_file << "NRRD0001" << std::endl;
nrrd_file << "# Complete NRRD file format specification at:" << std::endl;
nrrd_file << "# http://teem.sourceforge.net/nrrd/format.html" << std::endl;
nrrd_file << "type: float" << std::endl;
nrrd_file << "dimension: 3" << std::endl;
nrrd_file << "sizes: " << myDim[0] << " " << myDim[1] << " " << myDim[2] << std::endl;
nrrd_file << "axis mins: " << 0 << ", " << 0 << ", " << 0 << std::endl;
nrrd_file << "spacings: " << mySpacing[0] << " " << mySpacing[1] << " " << mySpacing[2] << std::endl;
nrrd_file << "centerings: cell cell cell" << std::endl;
nrrd_file << "endian: little" << std::endl;
nrrd_file << "encoding: raw" << std::endl;
nrrd_file << std::endl;
// write data portion
for(int k=0; k < myDim[2]; k++)
{
for(int j=0; j < myDim[1]; j++)
{
for(int i=0; i < myDim[0]; i++)
{
float val = myArray[i][j][k];
nrrd_file.write((char*)&val, sizeof(float));
}
}
}
nrrd_file.close();
}
}
void augmentData(ScatteredData *data)
{
int n = data->x[0].size();
int increment[] = {0,0,100};
for(int i=0; i<n; i++)
{
for(int j=0; j<3; j++)
{
data->x[j].push_back(data->x[j][i] + increment[j]);
}
data->fnc.push_back(10);
}
for(int i=0; i<n; i++)
{
for(int j=0; j<3; j++)
{
data->x[j].push_back(data->x[j][i] - increment[j]);
}
data->fnc.push_back(-10);
}
}
vec3 findNormal(ScatteredData *data, int n)
{
int tot = data->x[0].size();
int prev = (n-1)>=0?n-1:tot-1;
int next = (n+1)<tot?n+1:0;
while(data->x[2][prev]!=data->x[2][n])
{
prev = (prev-1)>=0?prev-1:tot-1;
}
while(data->x[2][next]!=data->x[2][n])
{
next = (next+1)<tot?next+1:0;
}
//printf("%d %d %d %d\n", prev,n,next,tot); fflush(stdout);
vec3 a(data->x[0][n], data->x[1][n], data->x[2][n]);
vec3 b(data->x[0][prev], data->x[1][prev], data->x[2][prev]);
vec3 c(data->x[0][next], data->x[1][next], data->x[2][next]);
/*vec3 one = b-a;
vec3 two = c-a;
vec3 ret = one+two;*/
vec3 tangent = b-c;
//rotate by 90 degrees on the x-y plane
double ret_x = -tangent[1];
double ret_y = tangent[0];
vec3 ret(ret_x, ret_y, tangent[2]);
return ret;
}
void augmentNormalData(ScatteredData *data)
{
int n = data->x[0].size();
for(int i=0; i<n; i++)
{
vec3 myNormal = findNormal(data, i);
myNormal = normalize(myNormal);
for(int j=0; j<3; j++)
{
data->x[j].push_back(data->x[j][i] + myNormal[j]);
}
data->fnc.push_back(10);
for(int j=0; j<3; j++)
{
data->x[j].push_back(data->x[j][i] - myNormal[j]);
}
data->fnc.push_back(-10);
}
}
void readDataFile(string filename, ScatteredData *data)
{
std::cout <<"Reading file '" << filename <<"'"<<std::endl;
std::ifstream datafile(filename.c_str());
if(datafile.is_open())
{
std::cout<<"Opened"<<std::endl;
double myData[3];
while(!datafile.eof())
{
datafile>>myData[0]>>myData[1]>>myData[2];
for(int i=0; i<3; i++)
data->x[i].push_back(myData[i]);
data->fnc.push_back(0);
}
std::cout<<"Done"<<std::endl;
std::cout<<"Augmenting data here"<<std::endl;
augmentData(data);
std::cout<<"Done"<<std::endl;
}
else
{
std::cout <<"Could not open '" << filename <<"'"<<std::endl;
}
}
void readSurfaceDataFile(string filename, ScatteredData *data)
{
std::cout <<"Reading file '" << filename <<"'"<<std::endl;
std::ifstream datafile(filename.c_str());
if(datafile.is_open())
{
std::cout<<"Opened"<<std::endl;
double myData[3];
while(!datafile.eof())
{
datafile>>myData[0]>>myData[1]>>myData[2];
for(int i=0; i<3; i++)
data->x[i].push_back(myData[i]);
data->fnc.push_back(0);
data->axisInformation.push_back(Z);
}
for(int i=0; i<3; i++)
data->x[i].pop_back();
data->fnc.pop_back();
std::cout<<"Done"<<std::endl;
//data->computeOrdering();
int n = data->origSize = data->x[0].size();
vec3 sum(0,0,0);
for(int i=0; i<n; i++)
{
for(int j=0; j<3; j++)
{
sum[j] = sum[j]+data->x[j][i];
}
}
for(int j=0; j<3; j++)
{
sum[j]/=n;
}
//std::cout<<"Augmenting data here B"<<std::endl;
//augmentNormalData(data);
//std::cout<<"Done"<<std::endl;
}
else
{
std::cout <<"Could not open '" << filename <<"'"<<std::endl;
}
}
#endif //_FILEIO_H_