-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometryContainer.h
89 lines (74 loc) · 1.76 KB
/
GeometryContainer.h
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
#pragma once
#include <string>
#include <fstream>
#include <vector>
#include <memory>
struct IShape
{
virtual bool read(std::ifstream& stream) = 0;
};
struct Circle: public IShape
{
bool read(std::ifstream& stream)
{
return static_cast<bool>(stream >> center.first >> center.second >> radius);
}
std::pair<double, double> center;
double radius;
};
struct Ellipse: public IShape
{
bool read(std::ifstream& stream)
{
return static_cast<bool>(stream >> focus1.first >> focus1.second
>> focus2.first >> focus2.second
>> eccentricity);
}
std::pair<double, double> focus1;
std::pair<double, double> focus2;
double eccentricity;
};
class GeometryContainer
{
public:
bool readCirclesData(const std::string& fileName)
{
return read<Circle>(fileName);
}
bool readEllipsesData(const std::string& fileName)
{
return read<Ellipse>(fileName);
}
size_t numberOfGeometries() const
{
return geometries.size();
}
const IShape* getShape(size_t index) const
{
return index < geometries.size() ? geometries[index].get() : nullptr;
}
private:
template <class Type>
bool read(const std::string& fileName)
{
std::ifstream infile(fileName);
if (!infile.is_open()) {
return false;
}
// TODO: because the first row contains columm headers we skip it for now
// we can parse it and adjust columns order
char tempData[1024];
infile.getline(tempData, 1024);
do{
auto shape = std::make_unique<Type>();
if (!shape->read(infile)){
break;
}
geometries.push_back(std::move(shape));
}while(true);
infile.close();
return true;
}
private:
std::vector<std::unique_ptr<IShape>> geometries;
};