-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
170 lines (143 loc) · 5.37 KB
/
main.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
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
#include <iostream>
#include <SFML/Graphics.hpp>
#include "./include/VerticesFromBitmap.hpp"
using namespace vfb;
void drawIslands(sf::RenderWindow & window,
std::vector<Island > const & islands,
float circleRadius = 1.0f)
{
sf::CircleShape circ(circleRadius);
circ.setOrigin({circleRadius, circleRadius});
std::vector<sf::Vertex > verts;
for(auto & island : islands)
{
verts.clear();
for(unsigned int i=0; i<island.vertexPositions.size(); ++i)
{
Vec2 position = island.vertexPositions[i];
verts.push_back({{(float)(position.x+0.5), (float)(position.y+0.5)}, sf::Color::Green});
circ.setPosition({(float)(position.x+0.5), (float)(position.y+0.5)});
window.draw(circ);
}
window.draw(verts.data(), verts.size(), sf::LineStrip);
}
}
void resetView(sf::RenderWindow & window,
sf::Image & image)
{
sf::View view;
view.setCenter({image.getSize().x/2.0f, image.getSize().y/2.0f});
float minSize = image.getSize().x < image.getSize().y ? image.getSize().x : image.getSize().y;
view.zoom(0.8f*minSize/window.getSize().x);
window.setView(view);
}
void reloadImage(sf::Image & image,
sf::Sprite & background,
VerticesFromBitmap & bmp,
sf::RenderWindow & window,
sf::Texture & texture,
bool applyContrast = false)
{
image.loadFromFile("./test.png");
if(applyContrast)
bmp.applyContrast(image);
texture.loadFromImage(image);
background.setTexture(texture);
resetView(window, image);
}
int main()
{
sf::RenderWindow window;
sf::VideoMode mode(800, 800);
window.create(mode, "test");
VerticesFromBitmap bmp;
sf::Sprite sprite;
sf::Image image;
image.loadFromFile("./test.png");
sf::Texture texture;
bmp.applyContrast(image);
texture.loadFromImage(image);
sprite.setTexture(texture);
std::vector<Island > islands = bmp.generateIslands(image);
std::cout << islands.size() << "numIslands\n";
for(unsigned int i=0; i<islands.size(); ++i)
{
std::cout << "island " << i << " has " << islands[i].vertexPositions.size() << " vertices\n";
}
//std::cout << islands[0].vertexPositions.size() << "numVerts\n";
bool drawImage = false;
float averageSpacing = 30.0f;
float circleRadius = 2.0f;
bool applyContrast = false;
resetView(window, image);
while(window.isOpen())
{
sf::Event currEvent;
while(window.pollEvent(currEvent))
{
switch(currEvent.type)
{
case(sf::Event::Closed):
window.close();
break;
case(sf::Event::KeyPressed):
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num1))
{
averageSpacing *= 2.0f;
bmp.setAverageSpacing(averageSpacing);
reloadImage(image, sprite, bmp, window, texture, applyContrast);
std::cout << averageSpacing << " avgSpacing\n";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num2))
{
averageSpacing /= 2.0f;
bmp.setAverageSpacing(averageSpacing);
reloadImage(image, sprite, bmp, window, texture, applyContrast);
std::cout << averageSpacing << " avgSpacing\n";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::R))
{
reloadImage(image, sprite, bmp, window, texture, applyContrast);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
drawImage = !drawImage;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::PageUp))
{
circleRadius *= 2.0f;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::PageDown))
{
circleRadius /= 2.0f;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::K))
{
bmp.setColinearThreshold(bmp.getColinearThreshold() - 0.05f);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::L))
{
bmp.setColinearThreshold(bmp.getColinearThreshold() + 0.05f);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
applyContrast = !applyContrast;
reloadImage(image, sprite, bmp, window, texture, applyContrast);
}
islands.clear();
islands = bmp.generateIslands(image);
std::cout << islands.size() << " numOfIslands\n";
}
default:
break;
}
}
window.clear(sf::Color::Black);
if(drawImage)
window.draw(sprite);
drawIslands(window, islands, circleRadius);
sf::sleep(sf::milliseconds(16));
window.display();
}
}