File size: 12,123 Bytes
fe2b563 |
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 |
#ifndef IO_H
#define IO_H
///--- hacked from OpenGP obj reader
#include <cstdio>
#include <Eigen/Dense>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
template <class MatrixType>
bool read_obj(MatrixType& vertices, MatrixType& normals, MatrixType& vert_colors, const std::string& filename, int D) {
char s[200];
float x, y, z, cx, cy, cz;
// open file (in ASCII mode)
FILE* in = fopen(filename.c_str(), "r");
if (!in) return false;
// clear line once
memset(&s, 0, 200);
//--- First pass, counts vertices
int n_vertices = 0;
int n_normals = 0;
while (in && !feof(in) && fgets(s, 200, in)) {
// comment
if (s[0] == '#' || isspace(s[0])) continue;
// vertex
else if (strncmp(s, "v ", 2) == 0)
n_vertices++;
else if (strncmp(s, "vn ", 2) == 0)
n_normals++;
}
fseek(in, 0, 0); ///< rewind
vertices.resize(D, n_vertices);
if(n_normals > 0)
normals.resize(D, n_vertices);
bool runonce = true;
//--- Second pass, fills in
int curr_vertex=0;
while (in && !feof(in) && fgets(s, 200, in)) {
// comment
if (s[0] == '#' || isspace(s[0])) continue;
// normal
else if (strncmp(s, "vn ", 3) == 0) {
if (sscanf(s, "vn %f %f %f", &x, &y, &z)) {
if (runonce)
{
normals.resize(D, n_vertices);
runonce = false;
}
normals(0, curr_vertex) = x;
normals(1, curr_vertex) = y;
normals(2, curr_vertex) = z;
}
}
// vertex
else if (strncmp(s, "v ", 2) == 0) {
if (sscanf(s, "v %f %f %f %f %f %f", &x, &y, &z, &cx, &cy, &cz)) {
vertices(0,curr_vertex) = x;
vertices(1,curr_vertex) = y;
vertices(2,curr_vertex) = z;
if(vert_colors.size()==0)
vert_colors.resize(D, n_vertices);
vert_colors(0, curr_vertex) = cx;
vert_colors(1, curr_vertex) = cy;
vert_colors(2, curr_vertex) = cz;
curr_vertex++;
}
else if (sscanf(s, "v %f %f %f", &x, &y, &z)) {
vertices(0,curr_vertex) = x;
vertices(1,curr_vertex) = y;
vertices(2,curr_vertex) = z;
curr_vertex++;
}
}
// face
else if (strncmp(s, "f ", 2) == 0) {
continue;
}
// clear line
memset(&s, 0, 200);
}
fclose(in);
return true;
}
//-----------------------------------------------------------------------------
///--- Replaces vertices in prev_filename with content of vertices, saves in filename
template <class MatrixType>
bool write_obj_replaceverts(const std::string& prev_filename, const MatrixType& vertices, const MatrixType& normals,
const MatrixType& vert_colors, const std::string& filename) {
typedef Eigen::Vector3d Texture_coordinate;
char s[200];
FILE* out = fopen(filename.c_str(), "w");
FILE* in = fopen(prev_filename.c_str(), "r");
if (!in || !out)
return false;
// clear line once
memset(&s, 0, 200);
//--- Second pass, fills in
int curr_vertex=0;
while (in && !feof(in) && fgets(s, 200, in)) {
// vertex
if (!isspace(s[0]) && strncmp(s, "v ", 2) == 0) {
fprintf(out, "vn %f %f %f\n", normals(0,curr_vertex), normals(1,curr_vertex), normals(2,curr_vertex));
fprintf(out, "v %f %f %f ", vertices(0,curr_vertex), vertices(1,curr_vertex), vertices(2,curr_vertex));
if(vert_colors.size())
fprintf(out, "%f %f %f\n", vert_colors(0,curr_vertex), vert_colors(1, curr_vertex), vert_colors(2, curr_vertex));
else
fprintf(out, "\n");
curr_vertex++;
} else {
fprintf(out, "%s", s);
}
// clear line
memset(&s, 0, 200);
}
fclose(in);
fclose(out);
return true;
}
template <class MatrixType>
bool read_transMat(MatrixType& trans, const std::string& filename)
{
std::ifstream input(filename);
std::string line;
int rows, cols;
std::vector<std::vector<double>> total_data;
while (getline(input, line)) {
if(line[0] == 'V' || line[0] == 'M')
continue;
std::istringstream iss(line);
std::vector<double> lineVec;
while (iss) {
double item;
if (iss >> item)
lineVec.push_back(item);
}
cols = lineVec.size();
total_data.push_back(lineVec);
}
if (total_data.size() == 0)
{
std::cout << filename << " is empty !! " << std::endl;
return false;
}
rows = total_data.size();
trans.resize(rows, cols);
std::cout << "rows = " << rows << " cols = " << cols << std::endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
trans(i, j) = total_data[i][j];
}
}
input.close();
std::cout << "read trans = \n" << trans << std::endl;
return true;
}
template <class MatrixType>
bool read_ply(MatrixType& vertices, MatrixType& normals, MatrixType& colors, const std::string& filename, int D) {
char s[200];
float x, y, z, nx, ny, nz;
int r, g, b;
int dim = 0;
int n_vertices, curr_vertex;
Eigen::Vector3i ID;
ID.setZero();
// open file (in ASCII mode)
FILE* in = fopen(filename.c_str(), "r");
if (!in) return false;
// clear line once
memset(&s, 0, 200);
//--- First pass, counts vertices
while (in && !feof(in) && fgets(s, 200, in)) {
// comment
if (strncmp(s, "element ", 8) == 0)
{
sscanf(s, "element vertex %d", &n_vertices);
}
if (strncmp(s, "property float x", 16) == 0)
{
if(strncmp(s, "property float x_", 17)==0)
{
continue;
}
vertices.resize(D,n_vertices);
ID[0]=1;
dim += 3;
}
if(strncmp(s, "property float nx", 17) == 0)
{
normals.resize(D,n_vertices);
ID[1]=1;
dim += 3;
}
if(strncmp(s, "property uchar red", 18) == 0)
{
colors.resize(3,n_vertices);
ID[2]=1;
dim += 3;
}
if(strncmp(s, "end_header", 10) == 0)
{
break;
}
memset(&s, 0, 200);
}
// clear line
memset(&s, 0, 200);
curr_vertex = 0;
while (in && !feof(in) && fgets(s, 200, in) && curr_vertex<n_vertices)
{
if(dim == 3)
{
if(sscanf(s, "%f %f %f",&x, &y, &z))
{
vertices(0,curr_vertex) = x;
vertices(1,curr_vertex) = y;
vertices(2,curr_vertex) = z;
curr_vertex++;
}
}
else if(dim==6)
{
if(ID[1])
{
if(sscanf(s, "%f %f %f %f %f %f",&x, &y, &z, &nx, &ny, &nz))
{
vertices(0,curr_vertex) = x;
vertices(1,curr_vertex) = y;
vertices(2,curr_vertex) = z;
normals(0, curr_vertex) = nx;
normals(1, curr_vertex) = ny;
normals(2, curr_vertex) = nz;
curr_vertex++;
}
}
else
{
if(sscanf(s, "%f %f %f %d %d %d",&x, &y, &z, &r, &g, &b))
{
vertices(0,curr_vertex) = x;
vertices(1,curr_vertex) = y;
vertices(2,curr_vertex) = z;
colors(0, curr_vertex) = r;
colors(1, curr_vertex) = g;
colors(2, curr_vertex) = b;
curr_vertex++;
}
}
}
else if(dim == 9)
{
if(sscanf(s, "%f %f %f %f %f %f %d %d %d", &x, &y, &z, &nx, &ny, &nz, &r, &g, &b))
{
vertices(0,curr_vertex) = x;
vertices(1,curr_vertex) = y;
vertices(2,curr_vertex) = z;
normals(0, curr_vertex) = nx;
normals(1, curr_vertex) = ny;
normals(2, curr_vertex) = nz;
colors(0, curr_vertex) = r;
colors(1, curr_vertex) = g;
colors(2, curr_vertex) = b;
curr_vertex ++;
}
}
if(curr_vertex > n_vertices)
{
n_vertices = curr_vertex;
vertices.resize(Eigen::NoChange, n_vertices);
if(normals.size())
{
normals.resize(Eigen::NoChange, n_vertices);
}
break;
}
// clear line
memset(&s, 0, 200);
}
fclose(in);
return true;
}
template <class MatrixType>
bool write_ply(MatrixType& vertices, MatrixType& normals, MatrixType& colors, const std::string& filename) {
char s[200];
int n_vertices, curr_vertex;
n_vertices = vertices.cols();
Eigen::Vector3d ID; // whether there are normal or color
ID.setZero();
if (vertices.cols())
{
ID[0] = 1;
}
else
{
std::cout << "Warning : No points!!!" << std::endl;
return false;
}
if (normals.cols())
{
ID[1] = 1;
// std::cout << "output file has normals !" << std::endl;
}
if (colors.cols())
{
ID[2] = 1;
// std::cout << "output file has colors !" << std::endl;
}
FILE* out = fopen(filename.c_str(), "w");
if (!out)
return false;
// clear line once
memset(&s, 0, 200);
fprintf(out, "ply\nformat ascii 1.0\nelement vertex %d\n", n_vertices);
fprintf(out, "property float x \nproperty float y\nproperty float z\n");
if(ID[1]) fprintf(out, "property float nx \nproperty float ny\nproperty float nz\n");
if(ID[2]) fprintf(out, "property uchar red\nproperty uchar green\nproperty uchar blue\n");
fprintf(out, "end_header\n");
// clear line
memset(&s, 0, 200);
curr_vertex = 0;
while (curr_vertex<n_vertices)
{
fprintf(out, "%f %f %f ", vertices(0, curr_vertex), vertices(1, curr_vertex), vertices(2, curr_vertex));
if (ID[1]) fprintf(out, "%f %f %f ", normals(0, curr_vertex), normals(1, curr_vertex), normals(2, curr_vertex));
if (ID[2]) fprintf(out, "%d %d %d ", (int)colors(0, curr_vertex), (int)colors(1, curr_vertex), (int)colors(2, curr_vertex));
fprintf(out, "\n");
// clear line
memset(&s, 0, 200);
curr_vertex++;
}
fclose(out);
return true;
}
template <class MatrixType>
bool read_file(MatrixType& vertices, MatrixType& normals, MatrixType& vert_colors,
const std::string& filename) {
if(strcmp(filename.substr(filename.size()-4,4).c_str(), ".obj") == 0)
{
return read_obj(vertices, normals, vert_colors, filename, 3);
}
else if(strcmp(filename.substr(filename.size()-4, 4).c_str(),".ply")==0)
{
return read_ply(vertices, normals, vert_colors, filename, 3);
}
else
{
std::cout << "Can't read file " << filename << std::endl;
}
}
template <class MatrixType>
bool write_file(const std::string& prev_filename, const MatrixType& vertices, const MatrixType& normals,
const MatrixType& vert_colors, const std::string& filename) {
if(strcmp(filename.substr(filename.size()-4,4).c_str(),".obj")==0)
{
return write_obj_replaceverts(prev_filename, vertices, normals, vert_colors, filename);
}
else if(strcmp(filename.substr(filename.size()-4,4).c_str(),".ply")==0)
{
return write_ply(vertices, normals, vert_colors, filename);
}
else
{
std::cout << "Can't write to file "<< filename << std::endl;
}
}
#endif // IO_H
|