sfml_impl 0.1.0
Vendor SFML 3.0.2 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SfmlPolygon.hpp
Go to the documentation of this file.
1
11#ifndef SFMLPOLYGON_HPP_
12#define SFMLPOLYGON_HPP_
13
14#include <vector>
15
16//Sfml
17#include <SFML/Graphics.hpp>
18
19// Interface
20#include "IPolygon.hpp"
21
22// system : Triangle, Vector2f, Epsilon
23#include "Shape.hpp"
24
25class SfmlPolygon : public graphic::IPolygon {
26
27public:
28 SfmlPolygon(std::vector<Vector2f> points) {
29 _color = sf::Color{255, 0, 0, 255};
30 _position = {0, 0};
31
32 for (auto point : points) {
33 _points.push_back(Vector2f{point.x, point.y});
34 }
35
36 triangulate();
37 buildVertices(); // une seule fois : le draw n'a plus qu'a envoyer
38 }
39
40 ~SfmlPolygon() = default;
41
42 bool isReady() const override {
43 return true;
44 }
45
46 Vector2f getPosition() const override {
47 return {_position.x, _position.y};
48 }
49
50 // position goes through a transform at draw time, so the vertices
51 // never have to be rebuilt
52 void setPosition(Vector2f position) override {
53 _position = sf::Vector2f{float(position.x), float(position.y)};
54 }
55
56 Color getColor() const override {
57 return Color{_color.r, _color.g, _color.b, _color.a};
58 }
59
60 void setColor(Color color) override {
61 _color = sf::Color{color.r, color.g, color.b, color.a};
62 for (size_t i = 0; i < _vertices.getVertexCount(); i++)
63 _vertices[i].color = _color;
64 }
65
66 std::vector<Vector2f> getPoints() const override {
67 return _points;
68 }
69
70 friend class SfmlWindow;
71
72private:
82 static bool is_convex(Vector2f A, Vector2f B, Vector2f C) {
83 const Vector2f BA = {A.x - B.x, A.y - B.y};
84 const Vector2f BC = {C.x - B.x, C.y - B.y};
85 const double cross = BA.cross(BC);
86
87 return (std::abs(cross) < epsilond) ? false : (cross < 0);
88 }
89
90 float polygon_area(const std::vector<Vector2f>& pts) {
91 float area = 0;
92 for (size_t i = 0; i < pts.size(); ++i) {
93 const Vector2f& a = pts[i];
94 const Vector2f& b = pts[(i + 1) % pts.size()];
95 area += (a.x * b.y - b.x * a.y);
96 }
97 return area * 0.5f;
98 }
99
100 void triangulate() {
101 std::vector<Vector2f> tmp = _points;
102
103 if (polygon_area(tmp) < 0)
104 std::reverse(tmp.begin(), tmp.end());
105
106 while (tmp.size() >= 3) {
107 bool earFound = false;
108
109 for (size_t i = 0; i < tmp.size(); i++) {
110 Vector2f A = tmp[i];
111 Vector2f B = tmp[(i + 1) % tmp.size()];
112 Vector2f C = tmp[(i + 2) % tmp.size()];
113 Triangle<double> t = Triangle<double>{A, B, C};
114
115 if (is_convex(A, B, C)) {
116 bool isEar = true;
117 for (size_t j = 0; j < tmp.size(); j++) {
118 if (j == i || j == (i + 1) % tmp.size() || j == (i + 2) % tmp.size()) continue;
119 if (t.isInside(tmp[j])) {
120 isEar = false;
121 break;
122 }
123 }
124
125 if (isEar) {
126 _triangles.push_back(Triangle<double>{A, B, C});
127 size_t earIndex = (i + 1) % tmp.size();
128 tmp.erase(tmp.begin() + earIndex);
129 earFound = true;
130 break;
131 }
132 }
133 }
134
135 if (!earFound)
136 break;
137 }
138 }
139
140 // sommets en coordonnees LOCALES, construits une fois apres la
141 // triangulation - aucune allocation par frame
142 void buildVertices() {
143 _vertices.setPrimitiveType(sf::PrimitiveType::Triangles);
144 _vertices.resize(_triangles.size() * 3);
145
146 size_t i = 0;
147 for (const Triangle<double> &t : _triangles) {
148 _vertices[i].position = {float(t.p1.x), float(t.p1.y)};
149 _vertices[i++].color = _color;
150 _vertices[i].position = {float(t.p2.x), float(t.p2.y)};
151 _vertices[i++].color = _color;
152 _vertices[i].position = {float(t.p3.x), float(t.p3.y)};
153 _vertices[i++].color = _color;
154 }
155 }
156
157 sf::Color _color;
158 sf::Vector2f _position;
159 sf::VertexArray _vertices;
160
161 std::vector<Vector2f> _points;
162 std::vector<Triangle<double>> _triangles;
163};
164
167#endif /* !SFMLPOLYGON_HPP_ */
Definition SfmlPolygon.hpp:25
std::vector< Vector2f > getPoints() const override
Definition SfmlPolygon.hpp:66
SfmlPolygon(std::vector< Vector2f > points)
Definition SfmlPolygon.hpp:28
Vector2f getPosition() const override
Definition SfmlPolygon.hpp:46
bool isReady() const override
Definition SfmlPolygon.hpp:42
~SfmlPolygon()=default
Color getColor() const override
Definition SfmlPolygon.hpp:56
void setColor(Color color) override
Definition SfmlPolygon.hpp:60
void setPosition(Vector2f position) override
Definition SfmlPolygon.hpp:52
Sfml Window class - implements IWindow2 only, sfml has no 3D.
Definition SfmlWindow.hpp:46