-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiriclib_polygon.cpp
More file actions
131 lines (117 loc) · 2.43 KB
/
iriclib_polygon.cpp
File metadata and controls
131 lines (117 loc) · 2.43 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
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
#include <fstream>
#include "iriclib_polygon.h"
#include "iriclib_bstream.h"
using namespace std;
using namespace iRICLib;
InternalPolygon::InternalPolygon()
{
x = 0;
y = 0;
pointCount = 0;
}
InternalPolygon::~InternalPolygon()
{
if (x != 0){ delete x; }
if (y != 0){ delete y; }
}
InputBStream& operator >> (InputBStream& stream, InternalPolygon& pol)
{
stream >> pol.pointCount;
pol.x = new double[pol.pointCount];
pol.y = new double[pol.pointCount];
for (int i = 0; i < pol.pointCount; ++i){
stream >> *(pol.x + i);
}
for (int i = 0; i < pol.pointCount; ++i){
stream >> *(pol.y + i);
}
return stream;
}
OutputBStream& operator << (OutputBStream& stream, const InternalPolygon& pol)
{
stream << pol.pointCount;
for (int i = 0; i < pol.pointCount; ++i){
stream << *(pol.x + i);
}
for (int i = 0; i < pol.pointCount; ++i){
stream << *(pol.y + i);
}
return stream;
}
Polygon::Polygon()
{
polygon = 0;
}
Polygon::~Polygon()
{
clear();
}
int Polygon::load(const char* filename, bool noDimensions)
{
ifstream istream(filename, ios::in | ios::binary);
if (! istream){
// open error
return -1;
}
clear();
InputBStream str(istream);
int size;
values.clear();
double val;
if (noDimensions){
str >> val;
values.push_back(val);
} else {
int count;
str >> count;
for (int i = 0; i < count; ++i){
str >> val;
values.push_back(val);
}
}
polygon = new iRICLib::InternalPolygon();
str >> *polygon;
str >> size;
holes.clear();
for (int i = 0; i < size; ++i){
InternalPolygon* hole = new InternalPolygon();
str >> *hole;
holes.push_back(hole);
}
istream.close();
return 0;
}
int Polygon::save(const char *filename, bool noDimensions)
{
ofstream ostream(filename, ios::out | ios::binary);
if (! ostream){
// open error
return -1;
}
OutputBStream str(ostream);
if (noDimensions){
str << values[0];
} else {
int count = static_cast<int>(values.size());
str << count;
for (int i = 0; i < count; ++i){
str << values[i];
}
}
str << *polygon;
str << static_cast<int>(holes.size());
for (unsigned int i = 0; i < holes.size(); ++i){
InternalPolygon* hole = holes[i];
str << *hole;
}
ostream.close();
return 0;
}
void Polygon::clear()
{
if (polygon != 0){delete polygon;}
for (unsigned int i = 0; i < holes.size(); ++i){
delete holes[i];
}
holes.clear();
}