forked from i-RIC/iriclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiriclib_polyline.cpp
115 lines (101 loc) · 1.92 KB
/
iriclib_polyline.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
#include <fstream>
#include "iriclib_polyline.h"
#include "iriclib_bstream.h"
using namespace std;
using namespace iRICLib;
InternalPolyline::InternalPolyline()
{
x = nullptr;
y = nullptr;
pointCount = 0;
}
InternalPolyline::~InternalPolyline()
{
delete x;
delete y;
}
InputBStream& operator >> (InputBStream& stream, InternalPolyline& 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 InternalPolyline& 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;
}
Polyline::Polyline()
{
polyline = nullptr;
}
Polyline::~Polyline()
{
clear();
}
int Polyline::load(const char* filename, bool noDimensions)
{
ifstream istream(filename, ios::in | ios::binary);
if (! istream){
// open error
return -1;
}
clear();
InputBStream str(istream);
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);
}
}
polyline = new iRICLib::InternalPolyline();
str >> *polyline;
istream.close();
return 0;
}
int Polyline::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 << *polyline;
ostream.close();
return 0;
}
void Polyline::clear()
{
delete polyline;
polyline = nullptr;
}