| 1 | /************************************************************************* |
| 2 | * * |
| 3 | * Vega FEM Simulation Library Version 3.1 * |
| 4 | * * |
| 5 | * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Code authors: Jernej Barbic, Christopher Twigg, Daniel Schroeder * |
| 9 | * http://www.jernejbarbic.com/code * |
| 10 | * * |
| 11 | * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, * |
| 12 | * Doug L. James, Jovan Popovic * |
| 13 | * * |
| 14 | * Funding: National Science Foundation, Link Foundation, * |
| 15 | * Singapore-MIT GAMBIT Game Lab, * |
| 16 | * Zumberge Research and Innovation Fund at USC * |
| 17 | * * |
| 18 | * This library is free software; you can redistribute it and/or * |
| 19 | * modify it under the terms of the BSD-style license that is * |
| 20 | * included with this library in the file LICENSE.txt * |
| 21 | * * |
| 22 | * This library is distributed in the hope that it will be useful, * |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 25 | * LICENSE.TXT for more details. * |
| 26 | * * |
| 27 | *************************************************************************/ |
| 28 | |
| 29 | #if defined(_WIN32) || defined(WIN32) |
| 30 | #pragma warning(disable : 4996) |
| 31 | #pragma warning(disable : 4267) |
| 32 | #pragma warning(disable : 4244) |
| 33 | #endif |
| 34 | |
| 35 | #include <vector> |
| 36 | #include <queue> |
| 37 | #include <string> |
| 38 | #include <string.h> |
| 39 | #include <map> |
| 40 | #include <fstream> |
| 41 | #include <sstream> |
| 42 | #include <algorithm> |
| 43 | using namespace std; |
| 44 | #include "objMeshBinaryLoader.h" |
| 45 | |
| 46 | //#define VERBOSE |
| 47 | |
| 48 | int ObjMeshBinaryLoader::readStringFromBinary(std::string & name, std::ifstream * fin) |
| 49 | { |
| 50 | #ifdef VERBOSE |
| 51 | printf("readString\n"); |
| 52 | #endif |
| 53 | |
| 54 | int strl; |
| 55 | fin->read((char*)&strl, sizeof(int)); |
| 56 | char * nameBuffer = (char*) malloc (strl); |
| 57 | fin->read(nameBuffer,strl); |
| 58 | name = string(nameBuffer); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int ObjMeshBinaryLoader::readVec3dFromBinary(Vec3d & vec, std::ifstream * fin) |
| 64 | { |
| 65 | #ifdef VERBOSE |
| 66 | printf("readVec3d\n"); |
| 67 | #endif |
| 68 | |
| 69 | double x,y,z; |
| 70 | fin->read((char*)&x, sizeof(double)); |
| 71 | fin->read((char*)&y, sizeof(double)); |
| 72 | fin->read((char*)&z, sizeof(double)); |
| 73 | vec = Vec3d(x,y,z); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int ObjMeshBinaryLoader::writeStringToBinary(const std::string name, std::ofstream * fout) |
| 79 | { |
| 80 | char * nameBuffer = (char*)name.c_str(); |
| 81 | int strl = strlen(nameBuffer) + 1; |
| 82 | fout->write((char*)&strl, sizeof(int)); |
| 83 | fout->write(nameBuffer,strl); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int ObjMeshBinaryLoader::writeVec3dToBinary(Vec3d & vec, std::ofstream * fout) |
| 89 | { |
| 90 | double x = vec[0]; |
| 91 | double y = vec[1]; |
| 92 | double z = vec[2]; |
| 93 | |
| 94 | fout->write((char*)&x, sizeof(double)); |
| 95 | fout->write((char*)&y, sizeof(double)); |
| 96 | fout->write((char*)&z, sizeof(double)); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int ObjMeshBinaryLoader::readFromBinaryFile(ObjMesh ** objMesh, const std::string & filename) |
| 102 | { |
| 103 | *objMesh = new ObjMesh(); |
| 104 | |
| 105 | ifstream fin(filename.c_str(),ios::binary); |
| 106 | |
| 107 | unsigned int numMaterials; |
| 108 | fin.read((char*)&numMaterials, sizeof(unsigned int)); |
| 109 | for(unsigned int i=0; i<numMaterials; i++) |
| 110 | { |
| 111 | ObjMesh::Material * material; |
| 112 | readFromBinary(&material, &fin); |
| 113 | (*objMesh)->addMaterial(*material); |
| 114 | } |
| 115 | |
| 116 | unsigned int numGroups; |
| 117 | fin.read((char*)&numGroups, sizeof(unsigned int)); |
| 118 | for(unsigned int i=0; i<numGroups; i++) |
| 119 | { |
| 120 | ObjMesh::Group * group; |
| 121 | readFromBinary(&group, &fin); |
| 122 | (*objMesh)->addGroup(*group); |
| 123 | } |
| 124 | |
| 125 | unsigned int numVertexPositions; |
| 126 | fin.read((char*)&numVertexPositions, sizeof(unsigned int)); |
| 127 | for(unsigned int i=0; i<numVertexPositions; i++) |
| 128 | { |
| 129 | Vec3d pos; |
| 130 | readVec3dFromBinary(pos, &fin); |
| 131 | (*objMesh)->addVertexPosition(pos); |
| 132 | } |
| 133 | |
| 134 | unsigned int numTextureCoords; |
| 135 | fin.read((char*)&numTextureCoords, sizeof(unsigned int)); |
| 136 | for(unsigned int i=0; i<numTextureCoords; i++) |
| 137 | { |
| 138 | Vec3d uv; |
| 139 | readVec3dFromBinary(uv, &fin); |
| 140 | (*objMesh)->addTextureCoordinate(uv); |
| 141 | } |
| 142 | |
| 143 | unsigned int numNormals; |
| 144 | fin.read((char*)&numNormals, sizeof(unsigned int)); |
| 145 | for(unsigned int i=0; i<numNormals; i++) |
| 146 | { |
| 147 | Vec3d normal; |
| 148 | readVec3dFromBinary(normal, &fin); |
| 149 | (*objMesh)->addVertexNormal(normal); |
| 150 | } |
| 151 | |
| 152 | fin.close(); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int ObjMeshBinaryLoader::writeToBinaryFile(ObjMesh * objMesh, const std::string & filename) |
| 158 | { |
| 159 | ofstream fout(filename.c_str(),ios::binary); |
| 160 | |
| 161 | unsigned int numMaterials = objMesh->getNumMaterials(); |
| 162 | fout.write((char*)&numMaterials, sizeof(unsigned int)); |
| 163 | for(unsigned int i=0; i<numMaterials; i++) |
| 164 | writeToBinary((ObjMesh::Material*)objMesh->getMaterialHandle(i), &fout); |
| 165 | |
| 166 | unsigned int numGroups = objMesh->getNumGroups(); |
| 167 | fout.write((char*)&numGroups, sizeof(unsigned int)); |
| 168 | for(unsigned int i=0; i<numGroups; i++) |
| 169 | writeToBinary((ObjMesh::Group*)objMesh->getGroupHandle(i), &fout); |
| 170 | |
| 171 | unsigned int numVertexPositions = objMesh->getNumVertices(); |
| 172 | fout.write((char*)&numVertexPositions, sizeof(unsigned int)); |
| 173 | for(unsigned int i=0; i<numVertexPositions; i++) |
| 174 | { |
| 175 | Vec3d pos = objMesh->getPosition(i); |
| 176 | writeVec3dToBinary(pos, &fout); |
| 177 | } |
| 178 | |
| 179 | unsigned int numTextureCoords = objMesh->getNumTextureCoordinates(); |
| 180 | fout.write((char*)&numTextureCoords, sizeof(unsigned int)); |
| 181 | for(unsigned int i=0; i<numTextureCoords; i++) |
| 182 | { |
| 183 | Vec3d texCoord = objMesh->getTextureCoordinate(i); |
| 184 | writeVec3dToBinary(texCoord, &fout); |
| 185 | } |
| 186 | |
| 187 | unsigned int numNormals = objMesh->getNumNormals(); |
| 188 | fout.write((char*)&numNormals, sizeof(unsigned int)); |
| 189 | for(unsigned int i=0; i<numNormals; i++) |
| 190 | { |
| 191 | Vec3d normal = objMesh->getNormal(i); |
| 192 | writeVec3dToBinary(normal, &fout); |
| 193 | } |
| 194 | |
| 195 | fout.close(); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int ObjMeshBinaryLoader::readFromBinary(ObjMesh::Vertex ** vertex, std::ifstream * fin) |
| 201 | { |
| 202 | #ifdef VERBOSE |
| 203 | printf("readVertex\n"); |
| 204 | #endif |
| 205 | |
| 206 | unsigned int position; |
| 207 | fin->read((char*)&position, sizeof(unsigned int)); |
| 208 | |
| 209 | bool textureBool; |
| 210 | int textureInt; |
| 211 | fin->read((char*)&textureBool, sizeof(bool)); |
| 212 | fin->read((char*)&textureInt, sizeof(int)); |
| 213 | std::pair<bool, unsigned int> texture(textureBool,textureInt); |
| 214 | |
| 215 | bool normalBool; |
| 216 | int normalInt; |
| 217 | fin->read((char*)&normalBool, sizeof(bool)); |
| 218 | fin->read((char*)&normalInt, sizeof(int)); |
| 219 | std::pair<bool, unsigned int> normal(normalBool, normalInt); |
| 220 | |
| 221 | *vertex = new ObjMesh::Vertex(position, texture, normal); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | int ObjMeshBinaryLoader::writeToBinary(ObjMesh::Vertex * vertex, std::ofstream * fout) |
| 227 | { |
| 228 | unsigned int position = vertex->getPositionIndex(); |
| 229 | fout->write((char*)&position, sizeof(unsigned int)); |
| 230 | |
| 231 | bool textureBool = (vertex->getTextureIndexPair()).first; |
| 232 | int textureInt = (vertex->getTextureIndexPair()).second; |
| 233 | fout->write((char*)&textureBool, sizeof(bool)); |
| 234 | fout->write((char*)&textureInt, sizeof(int)); |
| 235 | |
| 236 | bool normalBool = (vertex->getNormalIndexPair()).first; |
| 237 | int normalInt = (vertex->getNormalIndexPair()).second; |
| 238 | fout->write((char*)&normalBool, sizeof(bool)); |
| 239 | fout->write((char*)&normalInt, sizeof(int)); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | int ObjMeshBinaryLoader::readFromBinary(ObjMesh::Face ** face, std::ifstream * fin) |
| 245 | { |
| 246 | #ifdef VERBOSE |
| 247 | printf("readFace\n"); |
| 248 | #endif |
| 249 | |
| 250 | unsigned int numVertices; |
| 251 | fin->read((char*)&numVertices, sizeof(unsigned int)); |
| 252 | |
| 253 | *face = new ObjMesh::Face(); |
| 254 | |
| 255 | for(unsigned int i=0; i<numVertices; i++) |
| 256 | { |
| 257 | ObjMesh::Vertex * vertex; |
| 258 | readFromBinary(&vertex, fin); |
| 259 | (*face)->addVertex(*vertex); |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | int ObjMeshBinaryLoader::writeToBinary(ObjMesh::Face * face, std::ofstream * fout) |
| 266 | { |
| 267 | unsigned int numVertices = face->getNumVertices(); |
| 268 | fout->write((char*)&numVertices, sizeof(unsigned int)); |
| 269 | |
| 270 | for(unsigned int i=0; i<numVertices; i++) |
| 271 | writeToBinary((ObjMesh::Face*)face->getVertexHandle(i), fout); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | int ObjMeshBinaryLoader::readFromBinary(ObjMesh::Group ** group, std::ifstream * fin) |
| 277 | { |
| 278 | #ifdef VERBOSE |
| 279 | printf("readGroup\n"); |
| 280 | #endif |
| 281 | |
| 282 | string name; |
| 283 | readStringFromBinary(name, fin); |
| 284 | |
| 285 | unsigned int materialIndex; |
| 286 | fin->read((char*)&materialIndex, sizeof(unsigned int)); |
| 287 | |
| 288 | *group = new ObjMesh::Group(name, materialIndex); |
| 289 | |
| 290 | unsigned int numFaces; |
| 291 | fin->read((char*)&numFaces, sizeof(unsigned int)); |
| 292 | |
| 293 | for(unsigned int i=0; i<numFaces; i++) |
| 294 | { |
| 295 | ObjMesh::Face * face; |
| 296 | readFromBinary(&face, fin); |
| 297 | (*group)->addFace(*face); |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | int ObjMeshBinaryLoader::writeToBinary(ObjMesh::Group * group, std::ofstream * fout) |
| 305 | { |
| 306 | writeStringToBinary(group->getName(), fout); |
| 307 | |
| 308 | unsigned int materialIndex = group->getMaterialIndex(); |
| 309 | fout->write((char*)&materialIndex, sizeof(unsigned int)); |
| 310 | |
| 311 | unsigned int numFaces = group->getNumFaces(); |
| 312 | fout->write((char*)&numFaces, sizeof(unsigned int)); |
| 313 | |
| 314 | for(unsigned int i=0; i<numFaces; i++) |
| 315 | writeToBinary((ObjMesh::Face*)group->getFaceHandle(i), fout); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | int ObjMeshBinaryLoader::readFromBinary(ObjMesh::Material ** material, std::ifstream * fin) |
| 321 | { |
| 322 | #ifdef VERBOSE |
| 323 | printf("readMaterial\n"); |
| 324 | #endif |
| 325 | |
| 326 | string name; |
| 327 | readStringFromBinary(name, fin); |
| 328 | |
| 329 | #ifdef VERBOSE |
| 330 | cout << name << endl; |
| 331 | #endif |
| 332 | |
| 333 | double shininess; |
| 334 | fin->read((char*)&shininess, sizeof(double)); |
| 335 | |
| 336 | Vec3d Ka, Kd, Ks; |
| 337 | readVec3dFromBinary(Ka, fin); |
| 338 | readVec3dFromBinary(Kd, fin); |
| 339 | readVec3dFromBinary(Ks, fin); |
| 340 | |
| 341 | *material = new ObjMesh::Material(name, Ka, Kd, Ks, shininess); |
| 342 | |
| 343 | #ifdef VERBOSE |
| 344 | printf("done readMaterial\n"); |
| 345 | #endif |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | int ObjMeshBinaryLoader::writeToBinary(ObjMesh::Material * material, std::ofstream * fout) |
| 351 | { |
| 352 | writeStringToBinary(material->getName(), fout); |
| 353 | |
| 354 | double shininess = material->getShininess(); |
| 355 | fout->write((char*)&shininess, sizeof(double)); |
| 356 | |
| 357 | Vec3d Ka = material->getKa(); |
| 358 | writeVec3dToBinary(Ka, fout); |
| 359 | Vec3d Kd = material->getKd(); |
| 360 | writeVec3dToBinary(Kd, fout); |
| 361 | Vec3d Ks = material->getKs(); |
| 362 | writeVec3dToBinary(Ks, fout); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 |