SO3Engine
ALSceneLoader.cpp
Go to the documentation of this file.
1#include <boost/algorithm/string.hpp>
3
4// boost
5#include <boost/filesystem.hpp>
6
7// Default load flags
8// aiProcess_OptimizeGraph|aiProcess_JoinIdenticalVertices|aiProcess_FixInfacingNormals|aiProcess_MakeLeftHanded
9#define AL_ASSIMP_DEFAULT_FLAG aiProcess_ImproveCacheLocality|aiProcess_RemoveRedundantMaterials|aiProcess_Triangulate|aiProcess_SortByPType|aiProcess_FindInvalidData|aiProcess_LimitBoneWeights|aiProcess_FindDegenerates|aiProcess_FindInstances|aiProcess_CalcTangentSpace //|aiProcess_GlobalScale
10#define AL_ASSIMP_ADDITIONNAL_FLAG aiProcess_TransformUVCoords|aiProcess_GenUVCoords
11#define AL_ASSIMP_NULL_FLAGS aiProcess_SplitLargeMeshes|aiProcess_OptimizeMeshes
12//aiProcess_ValidateDataStructure make fail point clouds
13namespace SO3
14{
15 ALSceneLoader::ALSceneLoader(SScene* scene, std::string filename, std::string group, unsigned int flags, float scale)
16 {
17 mScene = scene;
18 mFilename = filename;
19 mGroup = group;
20 mSo3Flags = flags;
21 mFlags = 0;
22 mScale = scale;
23
24 if (mSo3Flags & so3Converter_SplitLargeMeshes)
25 mFlags |= aiProcess_SplitLargeMeshes;
26
27 if (mSo3Flags & so3Converter_GenSmoothNormals)
28 mFlags |= aiProcess_GenSmoothNormals;
29
30 if (mSo3Flags & so3Converter_OptimizeMeshes)
31 mFlags |= aiProcess_OptimizeMeshes;
32
33 if (mSo3Flags & so3Converter_OptimizeGraph)
34 mFlags |= aiProcess_OptimizeGraph;
35
36 if ((mFlags == 0) || (mFlags != 0 && !(mFlags & aiProcess_GenSmoothNormals)))
37 mFlags |= aiProcess_GenNormals;
38 else if (mFlags != 0)
39 mFlags |= aiProcess_RemoveComponent;
40 }
41
42 ALSceneLoader::ALSceneLoader(std::string filename, std::string group, unsigned int flags, float scale)
43 {
44 mScene = 0;
45 mFilename = filename;
46 mGroup = group;
47 mSo3Flags = flags;
48 mFlags = 0;
49 mScale = scale;
50
51 if (mSo3Flags & so3Converter_SplitLargeMeshes)
52 mFlags |= aiProcess_SplitLargeMeshes;
53
54 if (mSo3Flags & so3Converter_GenSmoothNormals)
55 mFlags |= aiProcess_GenSmoothNormals;
56
57 if (mSo3Flags & so3Converter_OptimizeMeshes)
58 mFlags |= aiProcess_OptimizeMeshes;
59
60 if (mSo3Flags & so3Converter_OptimizeGraph)
61 mFlags |= aiProcess_OptimizeGraph;
62
63 if ((mFlags == 0) || (mFlags != 0 && !(mFlags & aiProcess_GenSmoothNormals)))
64 mFlags |= aiProcess_GenNormals;
65 else if (mFlags != 0)
66 mFlags |= aiProcess_RemoveComponent;
67 }
68
73
74 bool ALSceneLoader::loadFile(const std::string& sceneName, SNode* parent, std::vector<SNode*> &objects, std::vector<SMaterial*> &materials)
75 {
76 const aiScene* loadedScene = NULL;
77
78 mImporter.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_LINE /*| aiPrimitiveType_POINT*/);
79 //mImporter.SetPropertyInteger(AI_CONFIG_FAVOUR_SPEED, 1);
80 mImporter.SetPropertyBool(AI_CONFIG_IMPORT_IFC_SKIP_SPACE_REPRESENTATIONS, true);
81 //AI_CONFIG_IMPORT_IFC_CUSTOM_TRIANGULATION
82 //AI_CONFIG_IMPORT_IFC_SMOOTHING_ANGLE
83 //AI_CONFIG_IMPORT_IFC_CYLINDRICAL_TESSELLATION
84
85 unsigned int defFlags = AL_ASSIMP_NULL_FLAGS;
86 if (mFlags != 0)
87 defFlags = mFlags;
88
89 if (defFlags & aiProcess_SplitLargeMeshes)
90 {
91 mImporter.SetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT, 65535 / 3);
92 mImporter.SetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT, 65535);
93 }
94
95 if (defFlags & aiProcess_OptimizeMeshes)
96 defFlags = defFlags | aiProcess_JoinIdenticalVertices;
97
98 if ((defFlags & aiProcess_GenSmoothNormals) && (defFlags & aiProcess_RemoveComponent))
99 {
100 mImporter.SetPropertyFloat(AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE, 45.0f);
101 mImporter.SetPropertyFloat(AI_CONFIG_PP_CT_MAX_SMOOTHING_ANGLE, 30.0f);
102 mImporter.SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, aiComponent_NORMALS | aiComponent_TANGENTS_AND_BITANGENTS);
103 }
104
105 //always scale even if 1.0
106 defFlags |= aiProcess_GlobalScale;
107 mImporter.SetPropertyFloat(AI_CONFIG_GLOBAL_SCALE_FACTOR_KEY, mScale);
108
109 std::string ext = boost::filesystem::extension(mFilename);
110 boost::algorithm::to_lower(ext);
111
112 if (ext == ".ply")
113 loadedScene = mImporter.ReadFile(to_utf8(mFilename).c_str(), AL_ASSIMP_DEFAULT_FLAG | defFlags);
114 else
115 loadedScene = mImporter.ReadFile(to_utf8(mFilename).c_str(), AL_ASSIMP_DEFAULT_FLAG | AL_ASSIMP_ADDITIONNAL_FLAG | defFlags);
116
117 //mImporter.SetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, false);
118
119 if (loadedScene != 0)
120 {
121 mALScene = ALScene(this, mScene, mGroup, loadedScene, parent, sceneName, true);
122 mALScene.load(objects, materials);
124 return true;
125 }
126 return false;
127 }
128
129 bool ALSceneLoader::convertFile(boost::filesystem::path expFolder, const std::string& sceneName)
130 {
131 const aiScene* loadedScene = NULL;
132
133 mImporter.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE, aiPrimitiveType_LINE /*| aiPrimitiveType_POINT*/);
134 //mImporter.SetPropertyInteger(AI_CONFIG_FAVOUR_SPEED, 1);
135 mImporter.SetPropertyBool(AI_CONFIG_IMPORT_IFC_SKIP_SPACE_REPRESENTATIONS, true);
136 //AI_CONFIG_IMPORT_IFC_CUSTOM_TRIANGULATION
137 //AI_CONFIG_IMPORT_IFC_SMOOTHING_ANGLE
138 //AI_CONFIG_IMPORT_IFC_CYLINDRICAL_TESSELLATION
139
140 unsigned int defFlags = AL_ASSIMP_NULL_FLAGS;
141 if (mFlags != 0)
142 defFlags = mFlags;
143
144 if (defFlags & aiProcess_SplitLargeMeshes)
145 {
146 mImporter.SetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT, 65535 / 3);
147 mImporter.SetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT, 65535);
148 }
149
150 if (defFlags & aiProcess_OptimizeMeshes)
151 defFlags = defFlags | aiProcess_JoinIdenticalVertices;
152
153 if ((defFlags & aiProcess_GenSmoothNormals) && (defFlags & aiProcess_RemoveComponent))
154 {
155 mImporter.SetPropertyFloat(AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE, 45.0f);
156 mImporter.SetPropertyFloat(AI_CONFIG_PP_CT_MAX_SMOOTHING_ANGLE, 30.0f);
157 mImporter.SetPropertyInteger(AI_CONFIG_PP_RVC_FLAGS, aiComponent_NORMALS | aiComponent_TANGENTS_AND_BITANGENTS);
158 }
159
160 //always scale even if 1.0
161 defFlags |= aiProcess_GlobalScale;
162 mImporter.SetPropertyFloat(AI_CONFIG_GLOBAL_SCALE_FACTOR_KEY, mScale);
163
164 std::string ext = boost::filesystem::extension(mFilename);
165 boost::algorithm::to_lower(ext);
166
167 if (ext == ".ply")
168 loadedScene = mImporter.ReadFile(to_utf8(mFilename).c_str(), AL_ASSIMP_DEFAULT_FLAG | defFlags);
169 else
170 loadedScene = mImporter.ReadFile(to_utf8(mFilename).c_str(), AL_ASSIMP_DEFAULT_FLAG | AL_ASSIMP_ADDITIONNAL_FLAG | defFlags);
171
172 if (loadedScene != 0)
173 {
174 createDirectory(expFolder);
175 mALScene = ALScene(this, mScene, mGroup, loadedScene, 0, sceneName);
176 mALScene.convert(expFolder);
178
179 //MMechostr (MSKDEBUG, "ASSIMP LOG : %s\n", mImporter.GetErrorString());
180 return true;
181 }
182 else
183 {
184 //MMechostr (MSKDEBUG, "ASSIMP LOG : %s\n", mImporter.GetErrorString());
185 return false;
186 }
187 }
188
189 void ALSceneLoader::createDirectory(boost::filesystem::path path)
190 {
191 if (!boost::filesystem::exists(path))
192 boost::filesystem::create_directories(path);
193
194 boost::filesystem::path texPath = path;
195 texPath /= "textures";
196 boost::filesystem::create_directory(texPath);
197
198 boost::filesystem::path meshPath = path;
199 meshPath /= "meshes";
200 boost::filesystem::create_directory(meshPath);
201
202 boost::filesystem::path matPath = path;
203 matPath /= "materials";
204 boost::filesystem::create_directory(matPath);
205 }
206
207 boost::filesystem::path ALSceneLoader::getFilePath()
208 {
209 return boost::filesystem::path(mFilename);
210 }
211
212}
static void resetUID()
reset the uid to 0.
bool load(std::vector< SNode * > &scolNodes, std::vector< SMaterial * > &scolMats)
Definition ALScene.cpp:73
void convert(boost::filesystem::path exportFolder)
Definition ALScene.cpp:195
bool convertFile(boost::filesystem::path expFolder, const std::string &sceneName)
bool loadFile(const std::string &sceneName, SNode *parent, std::vector< SNode * > &objects, std::vector< SMaterial * > &materials)
void createDirectory(boost::filesystem::path path)
ALSceneLoader(SScene *scene, std::string filename, std::string group, unsigned int flags, float scale=1.0f)
boost::filesystem::path getFilePath()
STBI_EXTERN unsigned long flags
Definition stb_image.h:1182