Skip to main content

Ellipse.ixx File

Basic ellipse shape for 2D rendering. More...

Included Headers

#include <vector> #include <cmath> #include <numbers> #include <helios.rendering.mesh.PrimitiveType> #include <helios.math.types> #include <helios.rendering.Vertex> #include <helios.rendering.asset.shape.Shape>

Namespaces Index

namespacehelios
namespacerendering

Graphics rendering infrastructure. More...

namespaceasset

Rendering asset management. More...

namespaceshape

Geometric shape definitions. More...

namespacebasic

Basic primitive shapes. More...

Classes Index

structEllipse

Basic Ellipse Shape (2D, in XY plane). More...

Description

Basic ellipse shape for 2D rendering.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file Ellipse.ixx
3 * @brief Basic ellipse shape for 2D rendering.
4 */
5module;
6
7#include <vector>
8#include <cmath>
9#include <numbers>
10
11export module helios.rendering.asset.shape.basic.Ellipse;
12
13import helios.rendering.asset.shape.Shape;
14import helios.rendering.Vertex;
15import helios.math.types;
16import helios.rendering.mesh.PrimitiveType;
17
18using namespace helios::rendering;
19
21
22 /**
23 * @brief Basic Ellipse Shape (2D, in XY plane).
24 *
25 * Generates a 2D ellipse mesh, centered at the origin on the XY plane (Z=0).
26 * An ellipse is defined by two radii: one along the X-axis (semi-major or semi-minor)
27 * and one along the Y-axis. When both radii are equal, the ellipse degenerates to a circle.
28 *
29 * The normals point in the positive Z direction.
30 * The indices are arranged as a Triangle Fan (for GL_TRIANGLES).
31 */
32 struct Ellipse : public Shape {
33
34 private:
35 /**
36 * @brief Dynamically creates the vertex list for an ellipse.
37 *
38 * Generates one center vertex (Index 0) and 'segments' vertices on the circumference.
39 *
40 * @param radiusX The semi-axis length along the X-axis.
41 * @param radiusY The semi-axis length along the Y-axis.
42 * @param segments The number of segments used to approximate the ellipse curve.
43 * @return A vector of vertices defining the ellipse mesh.
44 */
45 static std::vector<Vertex> generateVertices(float radiusX, float radiusY, unsigned int segments) {
46 std::vector<Vertex> vertices;
47 // 1 for the center vertex
48 vertices.reserve(segments + 1);
49
50 // center vertex (index 0)
51 vertices.push_back({
52 {0.0f, 0.0f, 0.0f}, // Position
53 {0.0f, 0.0f, 1.0f}, // Normal
54 {0.5f, 0.5f} // TexCoord (center of texture)
55 });
56
57 // circumference vertices (Indices 1 to segments)
58 for (unsigned int i = 0; i < segments; ++i) {
59 // calculate the angle for this segment
60 const float angle = (static_cast<float>(i) / static_cast<float>(segments)) * 2.0f * std::numbers::pi_v<float>;
61
62 float x = radiusX * std::cos(angle);
63 float y = radiusY * std::sin(angle);
64
65 // texture coordinates: map to [0, 1] range
66 // use the larger radius to normalize for consistent UV mapping
67 float maxRadius = std::max(radiusX, radiusY);
68 float u = (x / maxRadius) * 0.5f + 0.5f;
69 float v = (y / maxRadius) * 0.5f + 0.5f;
70
71 vertices.push_back({
72 {x, y, 0.0f},
73 {0.0f, 0.0f, 1.0f},
74 {u, v}
75 });
76 }
77 return vertices;
78 }
79
80 /**
81 * @brief Creates the index list for a Triangle Fan (for GL_TRIANGLES).
82 *
83 * Each triangle consists of (center, point_i, point_i+1).
84 *
85 * @param segments The number of segments used to build the ellipse.
86 * @return A vector of indices defining the triangle fan.
87 */
88 static std::vector<unsigned int> generateIndices(unsigned int segments) {
89 std::vector<unsigned int> indices;
90 indices.reserve(segments * 3); // 'segments' triangles, 3 indices per triangle
91
92 for (unsigned int i = 1; i <= segments; ++i) {
93 indices.push_back(0); // center vertex
94 indices.push_back(i); // current circumference vertex
95
96 // next circumference vertex
97 // for the last triangle (i == segments), connect back to Vertex 1
98 unsigned int next_i = (i == segments) ? 1 : (i + 1);
99 indices.push_back(next_i);
100 }
101 return indices;
102 }
103
104 public:
105 /**
106 * @brief Creates a default ellipse (radiusX = 1.0, radiusY = 0.5, 64 segments).
107 */
108 Ellipse() : Ellipse(1.0f, 0.5f, 64) {}
109
110 /**
111 * @brief Creates an ellipse with specific radii and segment count.
112 *
113 * @param radiusX The semi-axis length along the X-axis.
114 * @param radiusY The semi-axis length along the Y-axis.
115 * @param segments The number of triangle segments that make up the ellipse.
116 * More segments result in a smoother curve.
117 */
118 explicit Ellipse(float radiusX, float radiusY, unsigned int segments) :
119 Shape(
120 generateVertices(radiusX, radiusY, segments),
121 generateIndices(segments)
122 ) {}
123
124 /**
125 * @brief Returns the primitive type used for rendering.
126 * @return PrimitiveType::Triangles for triangle-based rendering.
127 */
128 [[nodiscard]] helios::rendering::mesh::PrimitiveType primitiveType() const noexcept override {
130 }
131 };
132
133}
134

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.