| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- // This file is part of libigl, a simple c++ geometry processing library.
- //
- // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
- //
- // This Source Code Form is subject to the terms of the Mozilla Public License
- // v. 2.0. If a copy of the MPL was not distributed with this file, You can
- // obtain one at http://mozilla.org/MPL/2.0/.
- #include "readWRL.h"
- #include <iostream>
- template <typename Scalar, typename Index> IGL_INLINE bool igl::readWRL(const std::string wrl_file_name, std::vector<std::vector<Scalar>> &V, std::vector<std::vector<Index>> &F, std::vector<std::vector<Scalar>> &TexCoord, std::vector<std::vector<Index>> &TexCoordIndex) {
- using namespace std;
- FILE *wrl_file = fopen(wrl_file_name.c_str(), "r");
- if (NULL == wrl_file) {
- printf("IOError: %s could not be opened...", wrl_file_name.c_str());
- return false;
- }
- return readWRL(wrl_file, V, F, TexCoord, TexCoordIndex);
- }
- template <typename Scalar, typename Index> IGL_INLINE bool igl::readWRL(FILE *wrl_file, std::vector<std::vector<Scalar>> &V, std::vector<std::vector<Index>> &F, std::vector<std::vector<Scalar>> &TexCoord, std::vector<std::vector<Index>> &TexCoordIndex) {
- using namespace std;
- // Read lines until one of the needles occurs. Treat other lines as "comments"
- string needleVertices = string("point ["), needleFaces = string("coordIndex ["), needleTexCoords = string("point ["), needleTexCoordIndices = string("texCoordIndex [");
- bool foundVertices = false;
- while (true) {
- char line[1000];
- if (fgets(line, 1000, wrl_file) == NULL) {
- if (foundVertices) {
- break;
- } else {
- std::cerr << "readWRL, reached EOF without finding the needle" << std::endl;
- fclose(wrl_file);
- return false;
- }
- }
- string haystack(line);
- if (haystack.find(needleVertices) != string::npos && !foundVertices) { // read vertices in sets of 3
- foundVertices = true;
- int floats_read = 3;
- while (floats_read == 3) {
- double x, y, z;
- floats_read = fscanf(wrl_file, " %lf %lf %lf,", &x, &y, &z);
- if (floats_read == 3) {
- vector<Scalar> point;
- point.resize(3);
- point[0] = x;
- point[1] = y;
- point[2] = z;
- V.push_back(point);
- // printf("(%g, %g, %g)\n",x,y,z);
- } else if (floats_read != 0) {
- printf("ERROR: unrecognized format...\n");
- return false;
- }
- }
- } else if (haystack.find(needleFaces) != string::npos) { // read faces
- int ints_read = 1;
- while (ints_read > 0) {
- // read new face indices (until hit -1)
- vector<Index> face;
- while (true) {
- // indices are 0-indexed
- int i;
- ints_read = fscanf(wrl_file, " %d,", &i);
- if (ints_read > 0) {
- if (i >= 0) {
- face.push_back(i);
- } else {
- F.push_back(face);
- break;
- }
- } else {
- break;
- }
- }
- }
- } else if (haystack.find(needleTexCoords) != string::npos) { // read texture coordinates
- // read points in sets of 2
- int floats_read = 2;
- while (floats_read == 2) {
- double x, y;
- floats_read = fscanf(wrl_file, " %lf %lf,", &x, &y);
- if (floats_read == 2) {
- vector<Scalar> point;
- point.resize(2);
- point[0] = x;
- point[1] = y;
- TexCoord.push_back(point);
- // printf("(%g, %g)\n",x,y);
- } else if (floats_read != 0) {
- printf("ERROR: unrecognized format...\n");
- return false;
- }
- }
- } else if (haystack.find(needleTexCoordIndices) != string::npos) { // read texCoordIndex
- int ints_read = 1;
- while (ints_read > 0) {
- // read new texture coordinate indices (until hit -1)
- vector<Index> texCoordIndex;
- while (true) {
- // indices are 0-indexed
- int i;
- ints_read = fscanf(wrl_file, " %d,", &i);
- if (ints_read > 0) {
- if (i >= 0) {
- texCoordIndex.push_back(i);
- } else {
- TexCoordIndex.push_back(texCoordIndex);
- break;
- }
- } else {
- break;
- }
- }
- }
- }
- }
- fclose(wrl_file);
- return true;
- }
- #ifdef IGL_STATIC_LIBRARY
- // Explicit template instantiation
- template bool igl::readWRL<double, int>(std::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>> &, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>> &, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>> &, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>> &);
- #endif
|