2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-05-19 03:48:29 +00:00
|
|
|
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.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file OFFLoader.cpp
|
2015-05-19 03:52:10 +00:00
|
|
|
* @brief Implementation of the OFF importer class
|
2015-05-19 03:48:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_OFF_IMPORTER
|
|
|
|
|
|
|
|
// internal headers
|
|
|
|
#include "OFFLoader.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/ParsingUtils.h>
|
|
|
|
#include <assimp/fast_atof.h>
|
2016-04-05 21:23:53 +00:00
|
|
|
#include <memory>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/IOSystem.hpp>
|
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
namespace Assimp {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
static constexpr aiImporterDesc desc = {
|
2015-05-19 03:57:13 +00:00
|
|
|
"OFF Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportBinaryFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"off"
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Returns whether the class can handle the format of the given file.
|
2023-11-11 08:59:42 +00:00
|
|
|
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);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2023-11-11 08:59:42 +00:00
|
|
|
const aiImporterDesc *OFFImporter::GetInfo() const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return &desc;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 12:08:47 +00:00
|
|
|
// skip blank space, lines and comments
|
2023-11-11 08:59:42 +00:00
|
|
|
static void NextToken(const char **car, const char *end) {
|
2018-12-02 12:08:47 +00:00
|
|
|
SkipSpacesAndLineEnd(car);
|
2023-11-11 08:59:42 +00:00
|
|
|
while (*car < end && (**car == '#' || **car == '\n' || **car == '\r')) {
|
|
|
|
SkipLine(car);
|
|
|
|
SkipSpacesAndLineEnd(car);
|
|
|
|
}
|
2018-12-02 12:08:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Imports the given file into the given scene structure.
|
2023-11-11 08:59:42 +00:00
|
|
|
void OFFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
|
|
|
|
std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Check whether we can read from the file
|
2022-11-08 16:03:55 +00:00
|
|
|
if (file == nullptr) {
|
2023-11-11 08:59:42 +00:00
|
|
|
throw DeadlyImportError("Failed to open OFF file ", pFile, ".");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// allocate storage and copy the contents of the file to a memory buffer
|
|
|
|
std::vector<char> mBuffer2;
|
2023-11-11 08:59:42 +00:00
|
|
|
TextFileToBuffer(file.get(), mBuffer2);
|
|
|
|
const char *buffer = &mBuffer2[0];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-12-02 12:08:47 +00:00
|
|
|
// 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;
|
2023-11-11 08:59:42 +00:00
|
|
|
const char *car = buffer;
|
|
|
|
const char *end = buffer + mBuffer2.size();
|
2018-12-02 12:08:47 +00:00
|
|
|
NextToken(&car, end);
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2018-12-02 12:08:47 +00:00
|
|
|
if (car < end - 2 && car[0] == 'S' && car[1] == 'T') {
|
2023-11-11 08:59:42 +00:00
|
|
|
hasTexCoord = true;
|
|
|
|
car += 2;
|
2018-12-02 12:08:47 +00:00
|
|
|
}
|
|
|
|
if (car < end - 1 && car[0] == 'C') {
|
2023-11-11 08:59:42 +00:00
|
|
|
hasColors = true;
|
|
|
|
car++;
|
2018-12-02 12:08:47 +00:00
|
|
|
}
|
2023-11-11 08:59:42 +00:00
|
|
|
if (car < end - 1 && car[0] == 'N') {
|
|
|
|
hasNormals = true;
|
|
|
|
car++;
|
2018-12-02 12:08:47 +00:00
|
|
|
}
|
|
|
|
if (car < end - 1 && car[0] == '4') {
|
2023-11-11 08:59:42 +00:00
|
|
|
hasHomogenous = true;
|
|
|
|
car++;
|
2018-12-02 12:08:47 +00:00
|
|
|
}
|
|
|
|
if (car < end - 1 && car[0] == 'n') {
|
2023-11-11 08:59:42 +00:00
|
|
|
hasDimension = true;
|
|
|
|
car++;
|
2018-12-02 12:08:47 +00:00
|
|
|
}
|
|
|
|
if (car < end - 3 && car[0] == 'O' && car[1] == 'F' && car[2] == 'F') {
|
|
|
|
car += 3;
|
2023-11-11 08:59:42 +00:00
|
|
|
NextToken(&car, end);
|
2018-12-02 12:08:47 +00:00
|
|
|
} else {
|
2023-11-11 08:59:42 +00:00
|
|
|
// 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");
|
|
|
|
}
|
2018-12-02 12:08:47 +00:00
|
|
|
}
|
|
|
|
if (hasDimension) {
|
|
|
|
dimensions = strtoul10(car, &car);
|
2023-11-11 08:59:42 +00:00
|
|
|
NextToken(&car, end);
|
2018-12-02 12:08:47 +00:00
|
|
|
}
|
|
|
|
if (dimensions > 3) {
|
2023-11-11 08:59:42 +00:00
|
|
|
throw DeadlyImportError("OFF: Number of vertex coordinates higher than 3 unsupported");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 12:08:47 +00:00
|
|
|
NextToken(&car, end);
|
|
|
|
const unsigned int numVertices = strtoul10(car, &car);
|
|
|
|
NextToken(&car, end);
|
|
|
|
const unsigned int numFaces = strtoul10(car, &car);
|
|
|
|
NextToken(&car, end);
|
2023-11-11 08:59:42 +00:00
|
|
|
strtoul10(car, &car); // skip edge count
|
2018-12-02 12:08:47 +00:00
|
|
|
NextToken(&car, end);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (!numVertices) {
|
|
|
|
throw DeadlyImportError("OFF: There are no valid vertices");
|
|
|
|
}
|
|
|
|
if (!numFaces) {
|
|
|
|
throw DeadlyImportError("OFF: There are no valid faces");
|
|
|
|
}
|
|
|
|
|
2015-10-28 11:30:56 +00:00
|
|
|
pScene->mNumMeshes = 1;
|
2023-11-11 08:59:42 +00:00
|
|
|
pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
|
2015-10-28 11:30:56 +00:00
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
aiMesh *mesh = new aiMesh();
|
2015-10-28 11:30:56 +00:00
|
|
|
pScene->mMeshes[0] = mesh;
|
|
|
|
|
|
|
|
mesh->mNumFaces = numFaces;
|
2023-11-11 08:59:42 +00:00
|
|
|
aiFace *faces = new aiFace[mesh->mNumFaces];
|
2015-10-28 11:30:56 +00:00
|
|
|
mesh->mFaces = faces;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-12-02 12:08:47 +00:00
|
|
|
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;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now read all vertex lines
|
2018-12-02 12:08:47 +00:00
|
|
|
for (unsigned int i = 0; i < numVertices; ++i) {
|
2023-11-11 08:59:42 +00:00
|
|
|
if (!GetNextLine(buffer, line)) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("OFF: The number of verts in the header is incorrect");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-11-11 08:59:42 +00:00
|
|
|
aiVector3D &v = mesh->mVertices[i];
|
2018-12-02 12:08:47 +00:00
|
|
|
sz = line;
|
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
// 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);
|
|
|
|
sz = fast_atoreal_move<ai_real>(sz, *vec[dim]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if has homogeneous coordinate, divide others by this one
|
|
|
|
if (hasHomogenous) {
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
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);
|
|
|
|
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)n.x);
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)n.y);
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
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);
|
|
|
|
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)c.r);
|
2018-12-02 12:08:47 +00:00
|
|
|
if (*sz != '#' && *sz != '\n' && *sz != '\r') {
|
2023-11-11 08:59:42 +00:00
|
|
|
SkipSpaces(&sz);
|
|
|
|
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)c.g);
|
2018-12-02 12:08:47 +00:00
|
|
|
} else {
|
2023-11-11 08:59:42 +00:00
|
|
|
c.g = 0.;
|
|
|
|
}
|
2018-12-02 12:08:47 +00:00
|
|
|
if (*sz != '#' && *sz != '\n' && *sz != '\r') {
|
2023-11-11 08:59:42 +00:00
|
|
|
SkipSpaces(&sz);
|
|
|
|
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)c.b);
|
2018-12-02 12:08:47 +00:00
|
|
|
} else {
|
2023-11-11 08:59:42 +00:00
|
|
|
c.b = 0.;
|
|
|
|
}
|
2018-12-02 12:08:47 +00:00
|
|
|
if (*sz != '#' && *sz != '\n' && *sz != '\r') {
|
2023-11-11 08:59:42 +00:00
|
|
|
SkipSpaces(&sz);
|
|
|
|
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)c.a);
|
2018-12-02 12:08:47 +00:00
|
|
|
} else {
|
2023-11-11 08:59:42 +00:00
|
|
|
c.a = 1.;
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 12:08:47 +00:00
|
|
|
if (hasTexCoord) {
|
2023-11-11 08:59:42 +00:00
|
|
|
aiVector3D &t = mesh->mTextureCoords[0][i];
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)t.x);
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
fast_atoreal_move<ai_real>(sz, (ai_real &)t.y);
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-02 12:08:47 +00:00
|
|
|
// load faces with their indices
|
|
|
|
faces = mesh->mFaces;
|
2023-11-11 08:59:42 +00:00
|
|
|
for (unsigned int i = 0; i < numFaces;) {
|
|
|
|
if (!GetNextLine(buffer, line)) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("OFF: The number of faces in the header is incorrect");
|
2023-06-14 15:50:50 +00:00
|
|
|
throw DeadlyImportError("OFF: The number of faces in the header is incorrect");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
unsigned int idx;
|
2023-11-11 08:59:42 +00:00
|
|
|
sz = line;
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
idx = strtoul10(sz, &sz);
|
|
|
|
if (!idx || idx > 9) {
|
|
|
|
ASSIMP_LOG_ERROR("OFF: Faces with zero indices aren't allowed");
|
2018-12-02 12:08:47 +00:00
|
|
|
--mesh->mNumFaces;
|
2022-12-10 01:22:00 +00:00
|
|
|
++i;
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
2023-11-11 08:59:42 +00:00
|
|
|
}
|
|
|
|
faces->mNumIndices = idx;
|
2018-12-02 12:08:47 +00:00
|
|
|
faces->mIndices = new unsigned int[faces->mNumIndices];
|
2023-11-11 08:59:42 +00:00
|
|
|
for (unsigned int m = 0; m < faces->mNumIndices; ++m) {
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipSpaces(&sz);
|
2023-11-11 08:59:42 +00:00
|
|
|
idx = strtoul10(sz, &sz);
|
2018-12-02 12:08:47 +00:00
|
|
|
if (idx >= numVertices) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("OFF: Vertex index is out of range");
|
2018-12-02 12:08:47 +00:00
|
|
|
idx = numVertices - 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2018-12-02 12:08:47 +00:00
|
|
|
faces->mIndices[m] = idx;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
++i;
|
|
|
|
++faces;
|
|
|
|
}
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// generate the output node graph
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mName.Set("<OFFRoot>");
|
2015-10-28 11:30:56 +00:00
|
|
|
pScene->mRootNode->mNumMeshes = 1;
|
2023-11-11 08:59:42 +00:00
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[pScene->mRootNode->mNumMeshes];
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mRootNode->mMeshes[0] = 0;
|
|
|
|
|
|
|
|
// generate a default material
|
2015-10-28 11:30:56 +00:00
|
|
|
pScene->mNumMaterials = 1;
|
2023-11-11 08:59:42 +00:00
|
|
|
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
|
|
|
|
aiMaterial *pcMat = new aiMaterial();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
aiColor4D clr(ai_real(0.6), ai_real(0.6), ai_real(0.6), ai_real(1.0));
|
|
|
|
pcMat->AddProperty(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mMaterials[0] = pcMat;
|
|
|
|
|
2018-12-02 12:08:47 +00:00
|
|
|
const int twosided = 1;
|
|
|
|
pcMat->AddProperty(&twosided, 1, AI_MATKEY_TWOSIDED);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-11 08:59:42 +00:00
|
|
|
} // namespace Assimp
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif // !! ASSIMP_BUILD_NO_OFF_IMPORTER
|