guillaume 1.0.0
Guillaume is a component-based UI framework library built around a primitive rendering system. It's designed for building user interfaces across XR platforms and traditional platforms with custom rendering backends and input handling. The framework is lightweight, flexible, and easy to integrate into existing projects.
Loading...
Searching...
No Matches
polygon.hpp
1/*
2 Copyright (c) 2025 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <cmath>
26#include <vector>
27
28#include "primitive.hpp"
29#include "vertex.hpp"
30#include "point.hpp"
31
40class Polygon : public Primitive {
41private:
42 std::vector<Vertex> _vertices;
43 Point _rotation;
44 Point _translation;
45
46public:
50 Polygon(void) : Primitive(), _vertices(), _rotation(), _translation() {}
51
60 Polygon(const std::vector<Vertex> &vertices,
61 const Point &rotation = Point())
62 : Primitive(), _vertices(vertices), _rotation(rotation), _translation() {}
63
67 Polygon(const std::vector<Point> &points, const Point &rotation = Point())
68 : Primitive(), _rotation(rotation), _translation() {
69 _vertices.clear();
70 for (const auto &p : points) {
71 _vertices.emplace_back(p, Color(1.0f, 1.0f, 1.0f));
72 }
73 }
74
78 ~Polygon(void) override = default;
79
87 Polygon &addVertex(const Vertex &vertex) {
88 _vertices.push_back(vertex);
89 return *this;
90 }
91
97 const std::vector<Vertex> &getVertices(void) const { return _vertices; }
98
104 const Point &getRotation(void) const { return _rotation; }
105
111 const Point &getTranslation(void) const { return _translation; }
112
120 Polygon &setRotation(const Point &rotation) {
121 _rotation = rotation;
122 return *this;
123 }
124
132 Polygon &setTranslation(const Point &translation) {
133 _translation = translation;
134 return *this;
135 }
136
140 void setVertices(const std::vector<Vertex> &vertices) { _vertices = vertices; }
141
145 std::vector<Point> getPoints(void) const {
146 std::vector<Point> pts;
147 pts.reserve(_vertices.size());
148 for (const auto &v : _vertices) {
149 pts.push_back(v.position);
150 }
151 return pts;
152 }
153
154 // /**
155 // * @brief Calculates the surface normal for this polygon (assuming planar
156 // * polygon)
157 // *
158 // * Uses the first three points to calculate the normal vector using cross
159 // * product. This is useful for 3D rendering, lighting calculations, and
160 // * back-face culling.
161 // *
162 // * @return Point representing the normalized surface normal (0,0,0) if less
163 // * than 3 points
164 // */
165 // Point calculateNormal(void) const {
166 // if (_points.size() < 3) {
167 // return Point(0, 0, 0);
168 // }
169
170 // // Get vectors from first point to second and third points
171 // Point v1(_points[1].x() - _points[0].x(), _points[1].y() -
172 // _points[0].y(),
173 // _points[1].z() - _points[0].z());
174
175 // Point v2(_points[2].x() - _points[0].x(), _points[2].y() -
176 // _points[0].y(),
177 // _points[2].z() - _points[0].z());
178
179 // // Calculate cross product to get normal
180 // float nx = v1.y() * v2.z() - v1.z() * v2.y();
181 // float ny = v1.z() * v2.x() - v1.x() * v2.z();
182 // float nz = v1.x() * v2.y() - v1.y() * v2.x();
183
184 // // Normalize the vector
185 // float length = std::sqrt(nx * nx + ny * ny + nz * nz);
186 // if (length > 0.0001f) { // Avoid division by zero
187 // return Point(nx / length, ny / length, nz / length);
188 // }
189
190 // return Point(0, 0, 1); // Default to Z-up if calculation fails
191 // }
192
193 // /**
194 // * @brief Calculates the centroid (geometric center) of the polygon
195 // *
196 // * @return Point representing the centroid of all vertices
197 // */
198 // Point calculateCentroid(void) const {
199 // if (_points.empty()) {
200 // return Point(0, 0, 0);
201 // }
202
203 // float x = 0, y = 0, z = 0;
204 // for (const auto &point : _points) {
205 // x += point.x();
206 // y += point.y();
207 // z += point.z();
208 // }
209
210 // float count = static_cast<float>(_points.size());
211 // return Point(x / count, y / count, z / count);
212 // }
213};
Represents an RGBA color with floating-point channels in [0, 1].
Definition color.hpp:42
Represents a point in 3D space.
Definition point.hpp:35
Represents a polygon primitive for 3D rendering.
Definition polygon.hpp:40
Polygon(const std::vector< Point > &points, const Point &rotation=Point())
Construct polygon from a list of Points (converts to vertices with default color)
Definition polygon.hpp:67
Polygon & addVertex(const Vertex &vertex)
Adds a vertex to the polygon.
Definition polygon.hpp:87
Polygon(void)
Default constructor - initializes an empty polygon.
Definition polygon.hpp:50
Polygon(const std::vector< Vertex > &vertices, const Point &rotation=Point())
Constructor from a list of vertices.
Definition polygon.hpp:60
Polygon & setRotation(const Point &rotation)
Sets the rotation of the polygon in Euler angles (radians)
Definition polygon.hpp:120
const std::vector< Vertex > & getVertices(void) const
Gets the vertices defining the polygon.
Definition polygon.hpp:97
const Point & getTranslation(void) const
Gets the translation of the polygon in 3D space.
Definition polygon.hpp:111
~Polygon(void) override=default
Destroy the Polygon object.
void setVertices(const std::vector< Vertex > &vertices)
Replace the internal vertex list.
Definition polygon.hpp:140
const Point & getRotation(void) const
Gets the rotation of the polygon in Euler angles (radians)
Definition polygon.hpp:104
Polygon & setTranslation(const Point &translation)
Sets the translation of the polygon in 3D space.
Definition polygon.hpp:132
std::vector< Point > getPoints(void) const
Return the polygon corner positions as Points.
Definition polygon.hpp:145
Base class for all drawing primitives.
Definition primitive.hpp:34