ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
VectorField2D.h
Go to the documentation of this file.
1/**
2 * @file VectorField2D.h
3 * @brief Represents a 2D vector field with various dynamic field effects and rendering capabilities.
4 *
5 * The `VectorField2D` class provides methods to manipulate and visualize a 2D vector field,
6 * including operations for boundary conditions, diffusion, advection, and specialized effects.
7 *
8 * @date 22/12/2024
9 * @author Coela Can't
10 */
11
12#pragma once
13
14#include "../Material.h"
15#include "../../../Utils/Math/Mathematics.h"
16#include "../../../Utils/Math/Vector2D.h"
17#include "../../../Renderer/Utils/Triangle2D.h"
18#include "../../Objects/Object3D.h"
19
20/**
21 * @class VectorField2D
22 * @brief A class for managing and rendering 2D vector fields.
23 */
24class VectorField2D : public Material {
25private:
26 int8_t* vecXP; ///< X-component of previous vectors.
27 int8_t* vecYP; ///< Y-component of previous vectors.
28 int8_t* vecDP; ///< Density of previous vectors.
29 int8_t* vecX; ///< X-component of current vectors.
30 int8_t* vecY; ///< Y-component of current vectors.
31 int8_t* vecD; ///< Density of current vectors.
32
33 Vector2D size; ///< Size of the vector field.
34 Vector2D position; ///< Position of the vector field.
35 const uint16_t countX; ///< Number of vectors along the X-axis.
36 const uint16_t countY; ///< Number of vectors along the Y-axis.
37 float rotation = 0.0f; ///< Rotation of the field in degrees.
38 bool density = false; ///< Indicates if the field should render density values.
39
40public:
41 /**
42 * @brief Constructs a 2D vector field with specified dimensions.
43 *
44 * @param x Number of vectors along the X-axis.
45 * @param y Number of vectors along the Y-axis.
46 */
47 VectorField2D(uint16_t x, uint16_t y);
48
49 /**
50 * @brief Destructor for the `VectorField2D` class.
51 */
53
54 /**
55 * @brief Applies boundary conditions to the vector field.
56 */
57 void Boundary();
58
59 /**
60 * @brief Performs diffusion on the vector field.
61 *
62 * @param viscosity The viscosity of the field.
63 * @param dt The time step for diffusion.
64 */
65 void Diffuse(float viscosity, float dt);
66
67 /**
68 * @brief Advances the vector field using advection.
69 *
70 * @param dt The time step for advection.
71 */
72 void Advect(float dt);
73
74 /**
75 * @brief Creates a sine wave effect in the vector field.
76 *
77 * @param ratio The frequency multiplier.
78 * @param period The period of the sine wave.
79 * @param amplitude The amplitude of the sine wave.
80 */
81 void SineField(float ratio, float period, float amplitude);
82
83 /**
84 * @brief Creates a stepped pattern effect in the vector field.
85 *
86 * @param ratio The frequency multiplier.
87 * @param period The period of the steps.
88 * @param intensity The intensity of the steps.
89 */
90 void StepField(float ratio, float period, float intensity);
91
92 /**
93 * @brief Creates a moving square pattern in the vector field.
94 *
95 * @param ratio The frequency multiplier.
96 * @param period The period of the movement.
97 * @param intensity The intensity of the movement.
98 */
99 void MovingSquareField(float ratio, float period, float intensity);
100
101 /**
102 * @brief Creates a spiral pattern in the vector field.
103 *
104 * @param ratio The frequency multiplier.
105 * @param period The period of the spiral.
106 * @param amplitude The amplitude of the spiral.
107 */
108 void SpiralField(float ratio, float period, float amplitude);
109
110 /**
111 * @brief Generates a field effect based on a 3D object's position and shape.
112 *
113 * @param object Pointer to the 3D object.
114 * @param intensity The intensity of the effect.
115 */
116 void ObjectField(Object3D* object, float intensity);
117
118 /**
119 * @brief Retrieves the number of vectors along the X-axis.
120 *
121 * @return The count of vectors along the X-axis.
122 */
123 uint16_t GetCountX();
124
125 /**
126 * @brief Retrieves the number of vectors along the Y-axis.
127 *
128 * @return The count of vectors along the Y-axis.
129 */
130 uint16_t GetCountY();
131
132 /**
133 * @brief Renders the density values of the vector field.
134 */
135 void RenderDensity();
136
137 /**
138 * @brief Renders the vector field as arrows.
139 */
140 void RenderVector();
141
142 /**
143 * @brief Sets the size of the vector field.
144 *
145 * @param sizeX The width of the field.
146 * @param sizeY The height of the field.
147 */
148 void SetSize(float sizeX, float sizeY);
149
150 /**
151 * @brief Sets the position of the vector field.
152 *
153 * @param posX The X-coordinate of the position.
154 * @param posY The Y-coordinate of the position.
155 */
156 void SetPosition(float posX, float posY);
157
158 /**
159 * @brief Sets the rotation of the vector field.
160 *
161 * @param rotation The rotation angle in degrees.
162 */
163 void SetRotation(float rotation);
164
165 /**
166 * @brief Retrieves the vector at a specific position in the field.
167 *
168 * @param x The X-coordinate of the position.
169 * @param y The Y-coordinate of the position.
170 * @param inBounds Output parameter indicating if the position is within bounds.
171 * @return A packed integer representing the vector components and density.
172 */
173 uint32_t GetVectorAtPosition(float x, float y, bool& inBounds);
174
175 /**
176 * @brief Retrieves the RGB color corresponding to a position in the field.
177 *
178 * @param position The 3D position in the field.
179 * @param normal The normal vector at the position.
180 * @param uvw UVW coordinates at the position.
181 * @return The RGB color at the specified position.
182 */
183 RGBColor GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) override;
184};
Abstract base class for rendering materials.
Definition Material.h:27
Represents a 3D object with geometry, material, and transformation data.
Definition Object3D.h:28
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
A class for managing and rendering 2D vector fields.
uint16_t GetCountX()
Retrieves the number of vectors along the X-axis.
int8_t * vecD
Density of current vectors.
int8_t * vecY
Y-component of current vectors.
float rotation
Rotation of the field in degrees.
void RenderVector()
Renders the vector field as arrows.
~VectorField2D()
Destructor for the VectorField2D class.
Vector2D position
Position of the vector field.
void SetRotation(float rotation)
Sets the rotation of the vector field.
void RenderDensity()
Renders the density values of the vector field.
int8_t * vecX
X-component of current vectors.
bool density
Indicates if the field should render density values.
uint16_t GetCountY()
Retrieves the number of vectors along the Y-axis.
int8_t * vecDP
Density of previous vectors.
void SineField(float ratio, float period, float amplitude)
Creates a sine wave effect in the vector field.
void StepField(float ratio, float period, float intensity)
Creates a stepped pattern effect in the vector field.
void Advect(float dt)
Advances the vector field using advection.
uint32_t GetVectorAtPosition(float x, float y, bool &inBounds)
Retrieves the vector at a specific position in the field.
void SetPosition(float posX, float posY)
Sets the position of the vector field.
RGBColor GetRGB(const Vector3D &position, const Vector3D &normal, const Vector3D &uvw) override
Retrieves the RGB color corresponding to a position in the field.
const uint16_t countY
Number of vectors along the Y-axis.
const uint16_t countX
Number of vectors along the X-axis.
int8_t * vecXP
X-component of previous vectors.
void ObjectField(Object3D *object, float intensity)
Generates a field effect based on a 3D object's position and shape.
void MovingSquareField(float ratio, float period, float intensity)
Creates a moving square pattern in the vector field.
void Boundary()
Applies boundary conditions to the vector field.
void SpiralField(float ratio, float period, float amplitude)
Creates a spiral pattern in the vector field.
void Diffuse(float viscosity, float dt)
Performs diffusion on the vector field.
void SetSize(float sizeX, float sizeY)
Sets the size of the vector field.
Vector2D size
Size of the vector field.
int8_t * vecYP
Y-component of previous vectors.