| 196 | } |
| 197 | |
| 198 | bool ObjReader::readFace(FACE& face, PointViewPtr view) |
| 199 | { |
| 200 | long long lineOfFile = 0; |
| 201 | while(true) { |
| 202 | if(m_istream->peek() == EOF) |
| 203 | return false; |
| 204 | |
| 205 | std::string line; |
| 206 | std::getline(*m_istream, line); |
| 207 | lineOfFile++; |
| 208 | Utils::trim(line); |
| 209 | if (line.length() == 0) |
| 210 | continue; |
| 211 | |
| 212 | StringList fields = Utils::split2(line, ' '); |
| 213 | std::string key = fields[0]; |
| 214 | if (key == "#") |
| 215 | { |
| 216 | // Comment: Do nothing |
| 217 | } |
| 218 | else if (key == "v") |
| 219 | { |
| 220 | // Vertex |
| 221 | size_t numDims = fields.size() - 1; |
| 222 | |
| 223 | auto throwVertexError = [this, line, lineOfFile]() |
| 224 | { |
| 225 | std::stringstream errorMessage; |
| 226 | errorMessage << "Could not convert vertex specification to double on line #" |
| 227 | << lineOfFile << ": '" << line << "'" << std::endl; |
| 228 | throwError(errorMessage.str()); |
| 229 | }; |
| 230 | |
| 231 | if(numDims < 3) { |
| 232 | throwVertexError(); |
| 233 | } |
| 234 | else if(numDims == 3) { |
| 235 | double x, y, z; |
| 236 | if (Utils::fromString(fields[1], x) && Utils::fromString(fields[2], y) && |
| 237 | Utils::fromString(fields[3], z)) |
| 238 | newVertex(x, y, z); |
| 239 | else |
| 240 | throwVertexError(); |
| 241 | } |
| 242 | else if(numDims > 3) { |
| 243 | double x, y, z, w; |
| 244 | if (Utils::fromString(fields[1], x) && Utils::fromString(fields[2], y) && |
| 245 | Utils::fromString(fields[3], z) && Utils::fromString(fields[4], w)) |
| 246 | newVertex(x, y, z, w); |
| 247 | else |
| 248 | throwVertexError(); |
| 249 | } |
| 250 | } |
| 251 | else if (key == "vt") |
| 252 | { |
| 253 | auto throwTextureError = [this, line, lineOfFile]() |
| 254 | { |
| 255 | std::stringstream ss; |