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
|
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2024, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file OFFLoader.cpp
* @brief Implementation of the OFF importer class
*/
#ifndef ASSIMP_BUILD_NO_OFF_IMPORTER
// internal headers
#include "OFFLoader.h"
#include <assimp/ParsingUtils.h>
#include <assimp/fast_atof.h>
#include <memory>
#include <assimp/IOSystem.hpp>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/importerdesc.h>
namespace Assimp {
static constexpr aiImporterDesc desc = {
"OFF Importer",
"",
"",
"",
aiImporterFlags_SupportBinaryFlavour,
0,
0,
0,
0,
"off"
};
// ------------------------------------------------------------------------------------------------
// Returns whether the class can handle the format of the given file.
bool OFFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
static const char *tokens[] = { "off" };
return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens), 3);
}
// ------------------------------------------------------------------------------------------------
const aiImporterDesc *OFFImporter::GetInfo() const {
return &desc;
}
// skip blank space, lines and comments
static void NextToken(const char **car, const char *end) {
SkipSpacesAndLineEnd(car, end);
while (*car < end && (**car == '#' || **car == '\n' || **car == '\r')) {
SkipLine(car, end);
SkipSpacesAndLineEnd(car, end);
}
}
// ------------------------------------------------------------------------------------------------
// Imports the given file into the given scene structure.
void OFFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
// Check whether we can read from the file
if (file == nullptr) {
throw DeadlyImportError("Failed to open OFF file ", pFile, ".");
}
// allocate storage and copy the contents of the file to a memory buffer
std::vector<char> mBuffer2;
TextFileToBuffer(file.get(), mBuffer2);
const char *buffer = &mBuffer2[0];
// Proper OFF header parser. We only implement normal loading for now.
bool hasTexCoord = false, hasNormals = false, hasColors = false;
bool hasHomogenous = false, hasDimension = false;
unsigned int dimensions = 3;
const char *car = buffer;
const char *end = buffer + mBuffer2.size();
NextToken(&car, end);
if (car < end - 2 && car[0] == 'S' && car[1] == 'T') {
hasTexCoord = true;
car += 2;
}
if (car < end - 1 && car[0] == 'C') {
hasColors = true;
car++;
}
if (car < end - 1 && car[0] == 'N') {
hasNormals = true;
car++;
}
if (car < end - 1 && car[0] == '4') {
hasHomogenous = true;
car++;
}
if (car < end - 1 && car[0] == 'n') {
hasDimension = true;
car++;
}
if (car < end - 3 && car[0] == 'O' && car[1] == 'F' && car[2] == 'F') {
car += 3;
NextToken(&car, end);
} else {
// in case there is no OFF header (which is allowed by the
// specification...), then we might have unintentionally read an
// additional dimension from the primitive count fields
dimensions = 3;
hasHomogenous = false;
NextToken(&car, end);
// at this point the next token should be an integer number
if (car >= end - 1 || *car < '0' || *car > '9') {
throw DeadlyImportError("OFF: Header is invalid");
}
}
if (hasDimension) {
dimensions = strtoul10(car, &car);
NextToken(&car, end);
}
if (dimensions > 3) {
throw DeadlyImportError("OFF: Number of vertex coordinates higher than 3 unsupported");
}
NextToken(&car, end);
const unsigned int numVertices = strtoul10(car, &car);
NextToken(&car, end);
const unsigned int numFaces = strtoul10(car, &car);
NextToken(&car, end);
strtoul10(car, &car); // skip edge count
NextToken(&car, end);
if (!numVertices) {
throw DeadlyImportError("OFF: There are no valid vertices");
}
if (!numFaces) {
throw DeadlyImportError("OFF: There are no valid faces");
}
pScene->mNumMeshes = 1;
pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
aiMesh *mesh = new aiMesh();
pScene->mMeshes[0] = mesh;
mesh->mNumFaces = numFaces;
aiFace *faces = new aiFace[mesh->mNumFaces];
mesh->mFaces = faces;
mesh->mNumVertices = numVertices;
mesh->mVertices = new aiVector3D[numVertices];
mesh->mNormals = hasNormals ? new aiVector3D[numVertices] : nullptr;
mesh->mColors[0] = hasColors ? new aiColor4D[numVertices] : nullptr;
if (hasTexCoord) {
mesh->mNumUVComponents[0] = 2;
mesh->mTextureCoords[0] = new aiVector3D[numVertices];
}
char line[4096];
buffer = car;
const char *sz = car;
const char *lineEnd = &line[4096];
// now read all vertex lines
for (unsigned int i = 0; i < numVertices; ++i) {
if (!GetNextLine(buffer, line)) {
ASSIMP_LOG_ERROR("OFF: The number of verts in the header is incorrect");
break;
}
aiVector3D &v = mesh->mVertices[i];
sz = line;
// helper array to write a for loop over possible dimension values
ai_real *vec[3] = { &v.x, &v.y, &v.z };
// stop at dimensions: this allows loading 1D or 2D coordinate vertices
for (unsigned int dim = 0; dim < dimensions; ++dim) {
SkipSpaces(&sz, lineEnd);
sz = fast_atoreal_move<ai_real>(sz, *vec[dim]);
}
// if has homogeneous coordinate, divide others by this one
if (hasHomogenous) {
SkipSpaces(&sz, lineEnd);
ai_real w = 1.;
sz = fast_atoreal_move<ai_real>(sz, w);
for (unsigned int dim = 0; dim < dimensions; ++dim) {
*(vec[dim]) /= w;
}
}
// read optional normals
if (hasNormals) {
aiVector3D &n = mesh->mNormals[i];
SkipSpaces(&sz, lineEnd);
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)n.x);
SkipSpaces(&sz, lineEnd);
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)n.y);
SkipSpaces(&sz, lineEnd);
fast_atoreal_move<ai_real>(sz, (ai_real &)n.z);
}
// reading colors is a pain because the specification says it can be
// integers or floats, and any number of them between 1 and 4 included,
// until the next comment or end of line
// in theory should be testing type !
if (hasColors) {
aiColor4D &c = mesh->mColors[0][i];
SkipSpaces(&sz, lineEnd);
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)c.r);
if (*sz != '#' && *sz != '\n' && *sz != '\r') {
SkipSpaces(&sz, lineEnd);
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)c.g);
} else {
c.g = 0.;
}
if (*sz != '#' && *sz != '\n' && *sz != '\r') {
SkipSpaces(&sz, lineEnd);
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)c.b);
} else {
c.b = 0.;
}
if (*sz != '#' && *sz != '\n' && *sz != '\r') {
SkipSpaces(&sz, lineEnd);
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)c.a);
} else {
c.a = 1.;
}
}
if (hasTexCoord) {
aiVector3D &t = mesh->mTextureCoords[0][i];
SkipSpaces(&sz, lineEnd);
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)t.x);
SkipSpaces(&sz, lineEnd);
fast_atoreal_move<ai_real>(sz, (ai_real &)t.y);
}
}
// load faces with their indices
faces = mesh->mFaces;
for (unsigned int i = 0; i < numFaces;) {
if (!GetNextLine(buffer, line)) {
ASSIMP_LOG_ERROR("OFF: The number of faces in the header is incorrect");
throw DeadlyImportError("OFF: The number of faces in the header is incorrect");
}
unsigned int idx;
sz = line;
SkipSpaces(&sz, lineEnd);
idx = strtoul10(sz, &sz);
if (!idx || idx > 9) {
ASSIMP_LOG_ERROR("OFF: Faces with zero indices aren't allowed");
--mesh->mNumFaces;
++i;
continue;
}
faces->mNumIndices = idx;
faces->mIndices = new unsigned int[faces->mNumIndices];
for (unsigned int m = 0; m < faces->mNumIndices; ++m) {
SkipSpaces(&sz, lineEnd);
idx = strtoul10(sz, &sz);
if (idx >= numVertices) {
ASSIMP_LOG_ERROR("OFF: Vertex index is out of range");
idx = numVertices - 1;
}
faces->mIndices[m] = idx;
}
++i;
++faces;
}
// generate the output node graph
pScene->mRootNode = new aiNode();
pScene->mRootNode->mName.Set("<OFFRoot>");
pScene->mRootNode->mNumMeshes = 1;
pScene->mRootNode->mMeshes = new unsigned int[pScene->mRootNode->mNumMeshes];
pScene->mRootNode->mMeshes[0] = 0;
// generate a default material
pScene->mNumMaterials = 1;
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
aiMaterial *pcMat = new aiMaterial();
aiColor4D clr(0.6f, 0.6f, 0.6f, 1.0f);
pcMat->AddProperty(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
pScene->mMaterials[0] = pcMat;
const int twosided = 1;
pcMat->AddProperty(&twosided, 1, AI_MATKEY_TWOSIDED);
}
} // namespace Assimp
#endif // !! ASSIMP_BUILD_NO_OFF_IMPORTER
|