FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_concavePolygon.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_CONCAVEPOLYGON_HPP_INCLUDED
18#define _FGE_C_CONCAVEPOLYGON_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_vector.hpp"
22#include "FastEngine/extra/extra_function.hpp"
23#include <map>
24#include <vector>
25
26/*
27 * Original from : https://github.com/mjjq/ConvexDecomposition
28 * Copyright (C) mjjq
29 * License MIT
30 * The algorithm for decomposing concave polygons to convex can be found here:
31 * https://mpen.ca/406/bayazit (Mark Bayazit).
32 *
33 * Altered/Modified by Guillaume Guillet
34 */
35
36namespace fge
37{
38
39class FGE_API ConcavePolygon
40{
41public:
42 using VertexArray = std::vector<fge::Vector2f>;
43
44 ConcavePolygon() = default;
45 explicit ConcavePolygon(VertexArray const& vertices);
46 explicit ConcavePolygon(VertexArray&& vertices);
47 ConcavePolygon(ConcavePolygon const& r) = default;
48 ConcavePolygon(ConcavePolygon&& r) noexcept = default;
49
50 ConcavePolygon& operator=(ConcavePolygon const& r) = default;
51 ConcavePolygon& operator=(ConcavePolygon&& r) noexcept = default;
52
53 [[nodiscard]] bool checkIfRightHanded();
54
55 void convexDecomposition();
56
57 void setVertices(VertexArray const& vertices);
58 void setVertices(VertexArray&& vertices);
59 [[nodiscard]] inline fge::Vector2f& vertex(std::size_t index) { return this->g_vertices[index]; }
60 [[nodiscard]] inline fge::Vector2f const& vertex(std::size_t index) const { return this->g_vertices[index]; }
61 [[nodiscard]] inline VertexArray const& vertices() const { return this->g_vertices; }
62
63 [[nodiscard]] inline VertexArray const& subPolygon(std::size_t subPolyIndex) const
64 {
65 return this->g_subPolygons[subPolyIndex];
66 }
67 [[nodiscard]] inline std::size_t subPolygonCount() const { return this->g_subPolygons.size(); }
68
69 [[nodiscard]] inline std::size_t totalVertexCount() const { return this->g_totalVertexCount; }
70
71 void clear();
72
73private:
74 VertexArray g_vertices;
75 std::vector<VertexArray> g_subPolygons;
76 std::size_t g_totalVertexCount = 0;
77
78 using VertexIndexMap = std::map<std::size_t, fge::Vector2f>;
79 using Indices = std::vector<std::size_t>;
80
81 [[nodiscard]] static bool checkIfRightHanded(VertexArray const& vertices);
82
83 [[nodiscard]] static std::pair<ConcavePolygon::VertexArray, ConcavePolygon::VertexArray>
84 slicePolygon(std::size_t const& startVertexIndex,
85 std::size_t const& stopVertexIndex,
86 VertexArray const& inputVertices);
87
88 [[nodiscard]] static Indices findVerticesInCone(fge::Line const& line1,
89 fge::Line const& line2,
90 fge::Vector2f const& origin,
91 VertexArray const& inputVertices);
92
93 [[nodiscard]] static bool checkVisibility(fge::Vector2f const& originalPosition,
94 fge::Vector2f const& vert,
95 VertexArray const& polygonVertices);
96
97 [[nodiscard]] static std::optional<std::size_t>
98 getBestVertexToConnect(Indices const& indices, VertexArray const& polygonVertices, fge::Vector2f const& origin);
99
100 [[nodiscard]] static std::optional<std::size_t> findFirstReflexVertex(VertexArray const& polygon);
101
102 static void flipPolygon(VertexArray& vertices);
103
104 [[nodiscard]] static VertexIndexMap verticesAlongLineSegment(fge::Line const& segment, VertexArray const& vertices);
105
106 [[nodiscard]] static std::optional<std::size_t>
107 addNewVertex(std::size_t& positionIndex, fge::Vector2f const& direction, VertexArray& vertices);
108};
109
110} // namespace fge
111
112#endif //_FGE_C_CONCAVEPOLYGON_HPP_INCLUDED
Definition C_concavePolygon.hpp:40
Definition extra_function.hpp:44