BaseVTKReader::BaseVTKDataIO* BaseVTKReader::newVTKDataIO(const string& typestr, int num) { BaseVTKDataIO* result = NULL; if (num == 1) result = newVTKDataIO(typestr); else { if (!strcasecmp(typestr.c_str(), "char") || !strcasecmp(typestr.c_str(), "Int8")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, char> >; break; case 3: result = new VTKDataIO<Vec<3, char> >; break; case 4: result = new VTKDataIO<Vec<4, char> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "unsigned_char") || !strcasecmp(typestr.c_str(), "UInt8")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, std::uint8_t> >; break; case 3: result = new VTKDataIO<Vec<3, std::uint8_t> >; break; case 4: result = new VTKDataIO<Vec<4, std::uint8_t> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "short") || !strcasecmp(typestr.c_str(), "Int16")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, std::int16_t> >; break; case 3: result = new VTKDataIO<Vec<3, std::int16_t> >; break; case 4: result = new VTKDataIO<Vec<4, std::int16_t> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "unsigned_short") || !strcasecmp(typestr.c_str(), "UInt16")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, std::uint16_t> >; break; case 3: result = new VTKDataIO<Vec<3, std::uint16_t> >; break; case 4: result = new VTKDataIO<Vec<4, std::uint16_t> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "int") || !strcasecmp(typestr.c_str(), "Int32")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, std::int32_t> >; break; case 3: result = new VTKDataIO<Vec<3, std::int32_t> >; break; case 4: result = new VTKDataIO<Vec<4, std::int32_t> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "unsigned_int") || !strcasecmp(typestr.c_str(), "UInt32")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, std::uint32_t> >; break; case 3: result = new VTKDataIO<Vec<3, std::uint32_t> >; break; case 4: result = new VTKDataIO<Vec<4, std::uint32_t> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "long") || !strcasecmp(typestr.c_str(), "Int64")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, std::int64_t> >; break; case 3: result = new VTKDataIO<Vec<3, std::int64_t> >; break; case 4: result = new VTKDataIO<Vec<4, std::int64_t> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "unsigned_long") || !strcasecmp(typestr.c_str(), "UInt64")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, std::uint64_t> >; break; case 3: result = new VTKDataIO<Vec<3, std::uint64_t> >; break; case 4: result = new VTKDataIO<Vec<4, std::uint64_t> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "float") || !strcasecmp(typestr.c_str(), "Float32")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, float> >; break; case 3: result = new VTKDataIO<Vec<3, float> >; break; case 4: result = new VTKDataIO<Vec<4, float> >; break; default: return NULL; } } if (!strcasecmp(typestr.c_str(), "double") || !strcasecmp(typestr.c_str(), "Float64")) { switch (num) { case 2: result = new VTKDataIO<Vec<2, double> >; break; case 3: result = new VTKDataIO<Vec<3, double> >; break; case 4: result = new VTKDataIO<Vec<4, double> >; break; default: return NULL; } } } result->nestedDataSize = num; return result; }
bool Mesh::loadVtk(const char* filename) { std::ifstream inVTKFile(filename, std::ifstream::in & std::ifstream::binary); if( !inVTKFile.is_open() ) { return false; } std::string line; // Part 1 std::getline(inVTKFile, line); if (std::string(line,0,23) != "# vtk DataFile Version ") return false; std::string version(line,23); // Part 2 std::string header; std::getline(inVTKFile, header); // Part 3 std::getline(inVTKFile, line); bool binary; if (line == "BINARY") binary = true; else if (line == "ASCII") binary = false; else return false; // Part 4 do std::getline(inVTKFile, line); while (line == ""); if (line != "DATASET POLYDATA") { return false; } std::cout << (binary ? "Binary" : "Text") << " VTK File (version " << version << "): " << header << std::endl; BaseVTKDataIO* inputPoints = NULL; VTKDataIO<int>* inputPolygons = NULL; int flags = MESH_POINTS_POSITION|MESH_FACES; int nbp = 0, nbf = 0; while(!inVTKFile.eof()) { std::getline(inVTKFile, line); std::istringstream ln(line); std::string kw; ln >> kw; if (kw == "POINTS") { int n; std::string typestr; ln >> n >> typestr; std::cout << "Found " << n << " " << typestr << " points" << std::endl; inputPoints = newVTKDataIO(typestr); if (inputPoints == NULL) return false; if (!inputPoints->read(inVTKFile, 3*n, binary)) { if (inputPoints) delete inputPoints; if (inputPolygons) delete inputPolygons; return false; } nbp = n; } else if (kw == "POLYGONS")