-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathAnnotation.cpp
More file actions
212 lines (188 loc) · 5.54 KB
/
Annotation.cpp
File metadata and controls
212 lines (188 loc) · 5.54 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "Annotation.h"
#include "AnnotationGroup.h"
#include "psimpl.h"
#include <limits>
#include <cmath>
#include <iterator>
const char* Annotation::_typeStrings[7] = { "None", "Dot", "Polygon", "Spline", "PointSet", "Measurement", "Rectangle"};
Annotation::Annotation() :
_type(Annotation::Type::NONE),
_coordinates()
{
}
std::string Annotation::getTypeAsString() const {
return _typeStrings[static_cast<int>(_type)];
}
bool Annotation::isClockwise() const {
if (!_coordinates.empty()) {
std::vector<Point> coords = _coordinates;
coords.push_back(coords[0]);
double cwise = 0;
for (unsigned int pt = 0; pt < coords.size() - 1; ++pt) {
double toAdd = (coords[pt + 1].getX() - coords[pt].getX()) * ((coords[pt + 1].getY() + coords[pt].getY()));
cwise += toAdd;
}
return cwise < 0;
}
else {
return false;
}
}
void Annotation::setTypeFromString(const std::string& type) {
for (unsigned int i = 0; i < 7; ++i) {
if (type == std::string(_typeStrings[i])) {
_type = (Annotation::Type)i;
}
}
_modified = true;
}
float Annotation::getArea() const {
if (!_coordinates.empty()) {
double area = 0.;
int j = _coordinates.size() - 1;
for (int i = 0; i < _coordinates.size(); i++) {
area += (_coordinates[j].getX() + _coordinates[i].getX())*(_coordinates[j].getY() - _coordinates[i].getY());
j = i;
}
return std::abs(area*.5);
}
else{
return 0.0;
}
}
unsigned int Annotation::getNumberOfPoints() const {
return _coordinates.size();
}
void Annotation::addCoordinate(const float& x, const float& y)
{
_coordinates.push_back(Point(x, y));
_modified = true;
}
void Annotation::addCoordinate(const Point& xy)
{
_coordinates.push_back(xy);
_modified = true;
}
void Annotation::insertCoordinate(const int& index, const Point& xy) {
if (index < 0) {
_coordinates.insert(_coordinates.end() - abs(index), xy);
}
else {
_coordinates.insert(_coordinates.begin() + index, xy);
}
_modified = true;
}
void Annotation::insertCoordinate(const int& index, const float& x, const float& y) {
this->insertCoordinate(index, Point(x, y));
_modified = true;
}
void Annotation::removeCoordinate(const int& index) {
if (index < 0) {
_coordinates.erase(_coordinates.end() - abs(index));
}
else {
_coordinates.erase(_coordinates.begin() + index);
}
_modified = true;
}
Point Annotation::getCoordinate(const int& index) const
{
if (index < 0) {
return *(_coordinates.end() - abs(index));
}
else {
return *(_coordinates.begin() + index);
}
}
std::vector<Point> Annotation::getCoordinates() const
{
return _coordinates;
}
void Annotation::setCoordinates(const std::vector<Point>& coordinates)
{
_coordinates = coordinates;
_modified = true;
}
void Annotation::clearCoordinates() {
_coordinates.clear();
_modified = true;
}
void Annotation::setType(const Annotation::Type& type)
{
_type = type;
_modified = true;
}
Annotation::Type Annotation::getType() const
{
return _type;
}
std::vector<Point> Annotation::getImageBoundingBox() const {
std::vector<Point> bbox;
Point topLeft(std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
Point bottomRight(std::numeric_limits<float>::min(), std::numeric_limits<float>::min());
if (_coordinates.empty()) {
topLeft = Point(0, 0);
bottomRight = Point(0, 0);
}
else {
for (std::vector<Point>::const_iterator it = _coordinates.begin(); it != _coordinates.end(); ++it) {
if (it->getX() > bottomRight.getX()) {
bottomRight.setX(it->getX());
}
if (it->getY() > bottomRight.getY()) {
bottomRight.setY(it->getY());
}
if (it->getX() < topLeft.getX()) {
topLeft.setX(it->getX());
}
if (it->getY() < topLeft.getY()) {
topLeft.setY(it->getY());
}
}
}
bbox.push_back(topLeft);
bbox.push_back(bottomRight);
return bbox;
}
void Annotation::simplify(unsigned int nrPoints, float epsilon) {
std::vector<float> inPoints;
std::vector<float> outPoints;
for (std::vector<Point>::iterator it = _coordinates.begin(); it != _coordinates.end(); ++it) {
inPoints.push_back(it->getX());
inPoints.push_back(it->getY());
}
std::vector<float>::const_iterator begin = inPoints.begin();
std::vector<float>::const_iterator end = inPoints.end();
if (nrPoints > 0) {
psimpl::simplify_douglas_peucker_n<2>(begin, end, nrPoints, std::back_inserter(outPoints));
}
else {
psimpl::simplify_douglas_peucker<2>(begin, end, epsilon, std::back_inserter(outPoints));
}
_coordinates.clear();
for (std::vector<float>::iterator it = outPoints.begin(); it != outPoints.end(); ++it) {
float x = *it;
++it;
float y = *it;
_coordinates.push_back(Point(x,y));
}
_modified = true;
}
Point Annotation::getCenter() {
Point center(0, 0);
if (!_coordinates.empty()) {
std::vector<Point> bbox = getImageBoundingBox();
center.setX((bbox[0].getX() + bbox[1].getX())/2.);
center.setY((bbox[0].getY() + bbox[1].getY()) / 2.);
}
return center;
}
std::vector<Point> Annotation::getLocalBoundingBox() {
Point center = getCenter();
std::vector<Point> bbox = getImageBoundingBox();
bbox[0].setX(bbox[0].getX() - center.getX());
bbox[1].setX(bbox[1].getX() - center.getX());
bbox[0].setY(bbox[0].getY() - center.getY());
bbox[1].setY(bbox[1].getY() - center.getY());
return bbox;
}