vrshoot

view libs/assimp/OFFLoader.cpp @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
42 /** @file OFFLoader.cpp
43 * @brief Implementation of the OFF importer class
44 */
46 #include "AssimpPCH.h"
47 #ifndef ASSIMP_BUILD_NO_OFF_IMPORTER
49 // internal headers
50 #include "OFFLoader.h"
51 #include "ParsingUtils.h"
52 #include "fast_atof.h"
55 using namespace Assimp;
57 static const aiImporterDesc desc = {
58 "OFF Importer",
59 "",
60 "",
61 "",
62 aiImporterFlags_SupportBinaryFlavour,
63 0,
64 0,
65 0,
66 0,
67 "off"
68 };
70 // ------------------------------------------------------------------------------------------------
71 // Constructor to be privately used by Importer
72 OFFImporter::OFFImporter()
73 {}
75 // ------------------------------------------------------------------------------------------------
76 // Destructor, private as well
77 OFFImporter::~OFFImporter()
78 {}
80 // ------------------------------------------------------------------------------------------------
81 // Returns whether the class can handle the format of the given file.
82 bool OFFImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
83 {
84 const std::string extension = GetExtension(pFile);
86 if (extension == "off")
87 return true;
88 else if (!extension.length() || checkSig)
89 {
90 if (!pIOHandler)return true;
91 const char* tokens[] = {"off"};
92 return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
93 }
94 return false;
95 }
97 // ------------------------------------------------------------------------------------------------
98 const aiImporterDesc* OFFImporter::GetInfo () const
99 {
100 return &desc;
101 }
103 // ------------------------------------------------------------------------------------------------
104 // Imports the given file into the given scene structure.
105 void OFFImporter::InternReadFile( const std::string& pFile,
106 aiScene* pScene, IOSystem* pIOHandler)
107 {
108 boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
110 // Check whether we can read from the file
111 if( file.get() == NULL) {
112 throw DeadlyImportError( "Failed to open OFF file " + pFile + ".");
113 }
115 // allocate storage and copy the contents of the file to a memory buffer
116 std::vector<char> mBuffer2;
117 TextFileToBuffer(file.get(),mBuffer2);
118 const char* buffer = &mBuffer2[0];
120 char line[4096];
121 GetNextLine(buffer,line);
122 if ('O' == line[0]) {
123 GetNextLine(buffer,line); // skip the 'OFF' line
124 }
126 const char* sz = line; SkipSpaces(&sz);
127 const unsigned int numVertices = strtoul10(sz,&sz);SkipSpaces(&sz);
128 const unsigned int numFaces = strtoul10(sz,&sz);
130 pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes = 1 ];
131 aiMesh* mesh = pScene->mMeshes[0] = new aiMesh();
132 aiFace* faces = mesh->mFaces = new aiFace [mesh->mNumFaces = numFaces];
134 std::vector<aiVector3D> tempPositions(numVertices);
136 // now read all vertex lines
137 for (unsigned int i = 0; i< numVertices;++i)
138 {
139 if(!GetNextLine(buffer,line))
140 {
141 DefaultLogger::get()->error("OFF: The number of verts in the header is incorrect");
142 break;
143 }
144 aiVector3D& v = tempPositions[i];
146 sz = line; SkipSpaces(&sz);
147 sz = fast_atoreal_move<float>(sz,(float&)v.x); SkipSpaces(&sz);
148 sz = fast_atoreal_move<float>(sz,(float&)v.y); SkipSpaces(&sz);
149 fast_atoreal_move<float>(sz,(float&)v.z);
150 }
153 // First find out how many vertices we'll need
154 const char* old = buffer;
155 for (unsigned int i = 0; i< mesh->mNumFaces;++i)
156 {
157 if(!GetNextLine(buffer,line))
158 {
159 DefaultLogger::get()->error("OFF: The number of faces in the header is incorrect");
160 break;
161 }
162 sz = line;SkipSpaces(&sz);
163 if(!(faces->mNumIndices = strtoul10(sz,&sz)) || faces->mNumIndices > 9)
164 {
165 DefaultLogger::get()->error("OFF: Faces with zero indices aren't allowed");
166 --mesh->mNumFaces;
167 continue;
168 }
169 mesh->mNumVertices += faces->mNumIndices;
170 ++faces;
171 }
173 if (!mesh->mNumVertices)
174 throw DeadlyImportError("OFF: There are no valid faces");
176 // allocate storage for the output vertices
177 aiVector3D* verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
179 // second: now parse all face indices
180 buffer = old;faces = mesh->mFaces;
181 for (unsigned int i = 0, p = 0; i< mesh->mNumFaces;)
182 {
183 if(!GetNextLine(buffer,line))break;
185 unsigned int idx;
186 sz = line;SkipSpaces(&sz);
187 if(!(idx = strtoul10(sz,&sz)) || idx > 9)
188 continue;
190 faces->mIndices = new unsigned int [faces->mNumIndices];
191 for (unsigned int m = 0; m < faces->mNumIndices;++m)
192 {
193 SkipSpaces(&sz);
194 if ((idx = strtoul10(sz,&sz)) >= numVertices)
195 {
196 DefaultLogger::get()->error("OFF: Vertex index is out of range");
197 idx = numVertices-1;
198 }
199 faces->mIndices[m] = p++;
200 *verts++ = tempPositions[idx];
201 }
202 ++i;
203 ++faces;
204 }
206 // generate the output node graph
207 pScene->mRootNode = new aiNode();
208 pScene->mRootNode->mName.Set("<OFFRoot>");
209 pScene->mRootNode->mMeshes = new unsigned int [pScene->mRootNode->mNumMeshes = 1];
210 pScene->mRootNode->mMeshes[0] = 0;
212 // generate a default material
213 pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = 1];
214 aiMaterial* pcMat = new aiMaterial();
216 aiColor4D clr(0.6f,0.6f,0.6f,1.0f);
217 pcMat->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
218 pScene->mMaterials[0] = pcMat;
220 const int twosided =1;
221 pcMat->AddProperty(&twosided,1,AI_MATKEY_TWOSIDED);
222 }
224 #endif // !! ASSIMP_BUILD_NO_OFF_IMPORTER