-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
217 lines (173 loc) · 4.39 KB
/
Image.cpp
File metadata and controls
217 lines (173 loc) · 4.39 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
213
214
215
216
217
#include "stdafx.h"
#include "Globals.h"
#include "Image.h"
Image::Image(void)
{
}
Image::~Image(void)
{
}
Box Image::minimalBox() const
{
Box result;
for(std::vector<Point>::const_iterator it = m_points.begin(); it != m_points.end(); ++it)
result.add(*it);
return result;
}
Vector Image::vector() const
{
if (m_points.size() < 2)
return Vector(0,0);
else
return Vector(m_points.front(), m_points.back());
}
std::vector<double> Image::getAngleDeltas(int minDist) const
{
std::vector<double> result;
Vector previousVec(0,0);
std::vector<Point>::const_iterator it_start = m_points.begin();
while(it_start != m_points.end())
{
Point start = *it_start;
std::vector<Point>::const_iterator it_end = it_start+1;
while(it_end != m_points.end() && start.dist(*it_end) <= minDist)
{
++it_end;
}
Point end = *(it_end-1);
Vector currentVec(start,end);
currentVec.normalize();
if (previousVec.length() != 0)
{
double cosA = max(min(currentVec * previousVec,1),-1);
double da = acos(cosA);
result.push_back(da);
}
it_start = it_end;
previousVec = currentVec;
}
return result;
}
Point Image::project(const Point & pt, const RECT & rect) const
{
return Point(
(Coord)Globals::interpolate(pt.x, m_box.topLeft.x, m_box.bottomRight.x, rect.left, rect.right),
(Coord)Globals::interpolate(pt.y, m_box.topLeft.y, m_box.bottomRight.y, rect.top, rect.bottom));
}
void Image::draw(HDC dc, RECT drawRect) const
{
if (! m_points.empty() )
{
POINT oldPoint;
Point start = project(* m_points.begin(), drawRect);
MoveToEx(dc, start.x, start.y, &oldPoint);
for(unsigned int i = 1; i < m_points.size(); ++i)
{
const Point & unprojectedPoint = m_points[i];
Point nextPoint = project(unprojectedPoint, drawRect);
if ( m_breaks.find(i) == m_breaks.end())
{
LineTo(dc, nextPoint.x, nextPoint.y);
}
else
{
MoveToEx(dc, nextPoint.x, nextPoint.y, NULL);
}
}
MoveToEx(dc, oldPoint.x, oldPoint.y, NULL);
}
}
void Image::scale(double dx, double dy)
{
for(std::vector<Point>::iterator it = m_points.begin(); it != m_points.end(); ++it)
{
Point & point = *it;
point.scale(dx,dy);
}
}
void Image::transfer(int dx, int dy)
{
for(std::vector<Point>::iterator it = m_points.begin(); it != m_points.end(); ++it)
{
Point & point = *it;
point.transfer(dx,dy);
}
}
void CompositeImage::draw(HDC dc, RECT drawRect) const
{
for(const_iterator it = begin(); it != end(); ++it)
it->draw(dc, drawRect);
}
void CompositeImage::fit(const Box & box)
{
Box realBox = minimalBox();
double dx = (double)box.width() / realBox.width();
double dy = (double)box.height() / realBox.height();
double dmin = min(dx,dy);
if (dmin < 1.0)
{
scale(dmin, dmin);
realBox = minimalBox();
}
if (realBox.height() > 0.3 * box.height()) //the only shape that needn't be centered is
//decimal point, and here we check for it
{
Point currentCenter = realBox.center();
Point requiredCenter = box.center();
transfer(requiredCenter.x - currentCenter.x, requiredCenter.y - currentCenter.y);
}
}
void CompositeImage::scale(double dx, double dy)
{
for(iterator it = begin(); it != end(); ++it)
it->scale(dx,dy);
}
void CompositeImage::transfer(int dx, int dy)
{
for(iterator it = begin(); it != end(); ++it)
it->transfer(dx,dy);
}
Box CompositeImage::minimalBox() const
{
Box result;
for(const_iterator it = begin(); it != end(); ++it)
result.add(it->minimalBox());
return result;
}
struct angle_collector {
angle_collector(Point _center, std::vector<double> & _result) : center(_center), result(_result) {}
void operator () (const Point & point) {
result.push_back( Vector(center, point).clock_angle() );
}
Point center;
std::vector<double> & result;
};
std::vector<double> Image::collectAngles (Point center) const
{
std::vector<double> result;
angle_collector collect_angles(center, result);
std::for_each(m_points.begin(), m_points.end(), collect_angles);
std::sort(result.begin(), result.end());
return result;
}
Point Image::closestPoint(Point rayStart, Vector rayDir) const
{
Point result;
double dist;
bool found = false;
for (std::vector<Point>::const_iterator it = m_points.begin(); it != m_points.end(); ++it)
{
Point point = *it;
double curr_dist = point.dist(rayStart, rayDir);
if (curr_dist >= 0)
{
if ( !found || curr_dist < dist )
{
found = true;
dist = curr_dist;
result = point;
}
}
}
return result;
}