My Project
loader.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "../stdafx.h"
3 #include "loader.h"
4 
6 {
7 }
8 
9 Loader::Loader(char* filename)
10 {
11  this->filename = filename;
12 
13  this->maxX = 0;
14  this->maxY = 0;
15  this->maxZ = 0;
16  this->minX = 0;
17  this->minY = 0;
18  this->minZ = 0;
19 }
20 
22 {
23 }
24 
25 void Loader::openFile()
26 {
27  if ((this->file = fopen(this->filename, "r")) == NULL)
28  {
29  throw LoaderOpenFileError();
30  }
31 }
32 
33 void Loader::closeFile()
34 {
35  fclose(this->file);
36 }
37 
38 Vertex Loader::readVertex()
39 {
40  double tmpX, tmpY, tmpZ;
41 
42  if (fscanf_s(this->file, " %lf %lf %lf", &tmpX, &tmpY, &tmpZ) != 3)
43  {
44  throw LoaderBadFile();
45  }
46 
47  tmpX *= 0.5;
48  tmpY *= 0.5;
49  tmpZ *= 0.5;
50 
51  Vertex v(tmpX, tmpY, tmpZ);
52 
53  if (tmpX < this->minX) this->minX = tmpX;
54  if (tmpY < this->minY) this->minY = tmpY;
55  if (tmpZ < this->minZ) this->minZ = tmpZ;
56  if (tmpX > this->maxX) this->maxX = tmpX;
57  if (tmpY > this->maxY) this->maxY = tmpY;
58  if (tmpZ > this->maxZ) this->maxZ = tmpZ;
59 
60  return v;
61 }
62 
63 Face Loader::readFace()
64 {
65  int tmpA, tmpB, tmpC;
66  int tmpnA, tmpnB, tmpnC;
67 
68  if (fscanf_s(this->file, " %d//%d %d//%d %d//%d", &tmpA, &tmpnA, &tmpB, &tmpnB, &tmpC, &tmpnC) != 6)
69  {
70  throw LoaderBadFile();
71  }
72 
73  Face f(tmpA, tmpnA, tmpB, tmpnB, tmpC, tmpnC);
74 
75  return f;
76 }
77 
78 GVector Loader::readNormal()
79 {
80  double tmpX, tmpY, tmpZ;
81 
82  if (fscanf_s(this->file, " %lf %lf %lf", &tmpX, &tmpY, &tmpZ) != 3)
83  {
84  throw LoaderBadFile();
85  }
86 
87  GVector N(tmpX, tmpY, tmpZ, 0);
88 
89  return N;
90 }
91 
93 {
94  Brick* brick = new Brick;
95 
96  this->openFile();
97  FILE* f = this->file;
98 
99  char linetype;
100  try
101  {
102  while (!feof(f))
103  {
104  fscanf_s(f, "%c", &linetype);
105  switch (linetype)
106  {
107  case 'v':
108  brick->addVertex(this->readVertex());
109  break;
110 
111  case 'f':
112  brick->addFace(this->readFace());
113  break;
114 
115  case 'n':
116  brick->addNormal(this->readNormal());
117  break;
118 
119  case '#':
120  while (linetype != '\0' && linetype != '\n')
121  {
122  fscanf_s(f, "%c", &linetype);
123  }
124  break;
125  }
126  }
127 
128 
129  //for (int i = 0; i < brick->facesCount(); i++)
130  //{
131  // Face c1Face = brick->faces[i];
132  // int c1Vertex = c1Face.getVertex();
133  //
134  // for (int k = 0; k < 3; k++)
135  // {
136  // vector<int> facesGroup;
137  // facesGroup.push_back(i);
138 
139  // for (int j = 0; j < brick->facesCount(); j++)
140  // {
141  // if (i != j)
142  // {
143  // Face c2Face = brick->faces[j];
144  // int c2Vertex = c2Face.getVertex();
145  // for (int l = 0; l < 3; l++)
146  // {
147  // if (c1Vertex == c2Vertex)
148  // {
149  // double angle = GVector::angle(brick->FNormal[c1Face.getNormal() - 1], brick->FNormal[c2Face.getNormal() - 1]);
150  // if (angle <= 40 && angle > 1)
151  // {
152  // facesGroup.push_back(j);
153  // }
154  // }
155  // c2Vertex = c2Face.getNextVertex();
156  // }
157  // }
158  // }
159 
160  // if (facesGroup.size() > 1)
161  // {
162  // vector<GVector> usedNormals;
163  // int used = 1;
164  // GVector RNormal = brick->FNormal[c1Face.getNormal() - 1];
165  // usedNormals.push_back(RNormal);
166  // for (int fgIndex = 0; fgIndex < facesGroup.size(); fgIndex++)
167  // {
168  // Face f = brick->faces[facesGroup[fgIndex]];
169  // GVector N = brick->FNormal[f.getNormal() - 1];
170  // bool fnd = false;
171  // for (int i1 = 0; i1 < usedNormals.size(); i1++)
172  // {
173  // if (N == usedNormals[i1])
174  // {
175  // fnd = true;
176  // }
177  // }
178  // if (!fnd)
179  // {
180  // RNormal = RNormal + N;
181  // usedNormals.push_back(N);
182  // used++;
183  // }
184  // }
185 
186  // RNormal = RNormal / used;
187  // brick->VNormal[i][k] = RNormal;
188 
189  // }
190 
191  // c1Vertex = c1Face.getNextVertex();
192  // }
193 
194  //}
195 
196  brick->ID = obj->ID;
197 
198  double cX = this->maxX - (this->maxX - this->minX) / 2;
199  double cY = this->maxY - (this->maxY - this->minY) / 2;
200  double cZ = this->maxZ - (this->maxZ - this->minZ) / 2;
201 
202  Vertex center(cX, cY, cZ);
203  brick->center = center;
204 
205  obj->add(brick);
206  }
207  catch (BaseException& err)
208  {
209  delete brick;
210  brick = nullptr;
211  throw;
212  }
213 
214  this->closeFile();
215 
216  return brick;
217 }
Contains loaded bricks.
Definition: composite.h:23
Define vertex consisting 3 double coordinates.
Definition: vertex.h:24
Define face consisting 3 links to vetices.
Definition: face.h:25
void addNormal(Normal normal)
Definition: brick.cpp:77
Define geometric vector.
Definition: gvector.h:26
Brick archetecture.
Definition: brick.h:25
Brick * load(Composite *obj)
Definition: loader.cpp:92
void addFace(Face face)
Definition: brick.cpp:64
void addVertex(Vertex v)
Definition: brick.cpp:58
Loader()
Definition: loader.cpp:5
void add(Brick *obj)
Definition: composite.cpp:15
~Loader()
Definition: loader.cpp:21
Vertex center
Definition: brick.h:93