This repository has been archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestream.cpp
159 lines (130 loc) · 3.4 KB
/
filestream.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "filestream.h"
#include <stdio.h>
FileStreamImp::FileStreamImp(){}
FileStreamImp::~FileStreamImp(){}
void FileStreamImp::openFile(const char* fileName)
{
if ((this->file = fopen(fileName, "r")) == NULL)
{
throw FileOpenException();
}
}
void FileStreamImp::closeFile()
{
fclose(this->file);
}
bool FileStreamImp::readCount(size_t& count)
{
return fscanf(this->file, "%lu", &count) == 1;
}
bool FileStreamImp::readPoint(double &x, double &y, double &z)
{
return fscanf(this->file, "%lf %lf %lf", &x, &y, &z) == 3;
}
bool FileStreamImp::readEdge(size_t& from , size_t& to)
{
return fscanf(this->file, "%lu %lu", &from, &to) == 2;
}
size_t FileStreamImp::readPoints(size_t count, BaseModel* model)
{
size_t i = 0;
double x,y,z;
for (i = 0; i < count && readPoint(x, y, z); i++)
model->addPoint(x, y, z);
return i;
}
size_t FileStreamImp::readEdges(size_t count, size_t pointsCount, BaseModel*model)
{
size_t from, to, i;
for (i = 0; i < count && readEdge(from, to) && from - 1 < pointsCount && to - 1 < pointsCount; i++)
{
model->addEdge( &( model->getPoints()[from-1] ), &( model->getPoints()[to-1] ) );
}
return i;
}
bool FileStreamImp::processPoints(BaseModel *model)
{
size_t count;
if (!this->readCount(count))
{
return false;
}
size_t readedCount = this->readPoints(count, model);
if (readedCount != count)
{
// return false;
}
return true;
}
bool FileStreamImp::processEdges(BaseModel *model)
{
size_t count;
if (!this->readCount(count))
{
return false;
}
size_t readedCount = this->readEdges(count, model->getPointsCount(), model);
if (readedCount < count)
{
return false;
}
return true;
}
bool FileStreamImp::readParams(double &alpha, double &beta, double &scale)
{
return fscanf(this->file, "%lf %lf %lf", &alpha, &beta, &scale) == 3;
}
bool FileStreamImp::processParams(double &x, double &y, double &z, double &alpha, double &beta, double &scale)
{
return readPoint(x,y,z) && readParams(alpha, beta, scale);
}
BaseObject* FileStreamImp::loadModel(const char *fileName, BaseModel *model)
{
openFile(fileName);
if (!this->processPoints(model) || !this->processEdges(model))
{
delete model;
closeFile();
throw InvalidModelFileException();
}
closeFile();
return model;
}
void FileStreamImp::loadCameraParams(const char*fileName, double &x, double &y, double &z, double &alpha, double &beta, double &scale)
{
openFile(fileName);
if ( !this->processParams(x, y, z, alpha, beta, scale) )
{
closeFile();
throw InvalidCameraFileException();
}
closeFile();
}
FileStream::FileStream(FileStreamImp *imp, AbstractFactory *factory)
{
this->imp = imp;
this->factory = factory;
}
FileStream::~FileStream()
{
}
BaseObject* FileStream::loadModel(const char *fileName)
{
BaseModel* model = this->factory->createModel();
if (!model)
{
throw MemoryException();
}
return this->imp->loadModel(fileName, model);
}
BaseCamera* FileStream::loadCamera(const char*fileName)
{
double x, y, z, alpha,beta,scale;
this->imp->loadCameraParams(fileName, x, y, z, alpha,beta,scale);
BaseCamera* camera = this->factory->createCamera(x, y, z, alpha,beta,scale);
if (!camera)
{
throw MemoryException();
}
return camera;
}