-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_utils.cpp
More file actions
98 lines (84 loc) · 3.68 KB
/
file_utils.cpp
File metadata and controls
98 lines (84 loc) · 3.68 KB
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
#include "file_utils.h"
float File::scaleFactor = 1; // by default coordinates are given in meters
vector<PlaneType>* File::getCoordinatesFromPlainFile(char *filename) {
filebuf fb;
if (!fb.open(filename,ios::in)) {
cerr << "An error occurred while opening file" << endl;
return NULL;
}
istream fileInputStream(&fb);
vector<PlaneType>* coordinatesList = new vector<PlaneType>();
ThreePoints *tempThreePoints;
int i;
while(!fileInputStream.eof()) {
tempThreePoints = new ThreePoints();
// read 3 points
for (i = 0;i < 3;i++) {
fileInputStream >> tempThreePoints->set[i].x
>> tempThreePoints->set[i].y
>> tempThreePoints->set[i].z;
if (fileInputStream.fail()) {
if (!fileInputStream.eof())
fileInputStream.clear();
break;
}
}
// if all values have been read successfuly then push array to result list
if (i == 3) {
try {
PlaneType *pt = new PlaneType(*tempThreePoints);
coordinatesList->push_back(*pt);
for (i = 0;i < 3;i++) {
coordinatesList->back().set[i].x *= scaleFactor;
coordinatesList->back().set[i].y *= scaleFactor;
coordinatesList->back().set[i].z *= scaleFactor;
}
} catch (ZeroNormal zn) {}
}
// then delete it
delete tempThreePoints;
// scipping remaining characters in current string
while (!fileInputStream.eof() && fileInputStream.get() != '\n');
}
fb.close();
return coordinatesList;
}
vector<PlaneType>* File::getCoordinatesFromSpecialFile(char *filename) {
Assimp::Importer importer;
aiScene *aiscene = (aiScene*)importer
.ReadFile(filename,aiProcess_Triangulate|aiProcess_FixInfacingNormals|aiProcess_FindDegenerates
|aiProcess_PreTransformVertices|aiProcess_OptimizeMeshes|aiProcess_FindInvalidData|aiProcess_RemoveRedundantMaterials);
if (aiscene == NULL) {
cerr << "An error occurred while opening file" << endl;
return NULL;
}
vector<PlaneType>* coordinatesList = new vector<PlaneType>();
ThreePoints *tempThreePoints;
int failedNumber = 0;
for(unsigned int i = 0;i < aiscene->mNumMeshes;++i) {
if (aiscene->mMeshes[i]->HasFaces()) {
for(unsigned int j = 0;j < aiscene->mMeshes[i]->mNumFaces;++j) {
tempThreePoints = new ThreePoints();
for(int k = 0;k < 3;k++) {
tempThreePoints->set[k].x = aiscene->mMeshes[i]->mVertices[aiscene->mMeshes[i]->mFaces[j].mIndices[k]].x;
tempThreePoints->set[k].y = aiscene->mMeshes[i]->mVertices[aiscene->mMeshes[i]->mFaces[j].mIndices[k]].y;
tempThreePoints->set[k].z = aiscene->mMeshes[i]->mVertices[aiscene->mMeshes[i]->mFaces[j].mIndices[k]].z;
}
try {
PlaneType *pt = new PlaneType(*tempThreePoints);
coordinatesList->push_back(*pt);
for (int g = 0;g < 3;g++) {
coordinatesList->back().set[g].x *= scaleFactor;
coordinatesList->back().set[g].y *= scaleFactor;
coordinatesList->back().set[g].z *= scaleFactor;
}
} catch (ZeroNormal zn) {
++failedNumber;
}
delete tempThreePoints;
}
}
}
failedNumber && cerr << "failed: " << failedNumber << " polygons" << endl;
return coordinatesList;
}