4 : countX(x), countY(y) {
5 this->
vecXP =
new int8_t[x * y];
6 this->
vecYP =
new int8_t[x * y];
7 this->
vecDP =
new int8_t[x * y];
8 this->
vecX =
new int8_t[x * y];
9 this->
vecY =
new int8_t[x * y];
10 this->
vecD =
new int8_t[x * y];
27 float a = dt * viscosity * (float)
countX * (
float)
countY;
28 float aS = 1.0f + (4.0f * a);
34 for (
int x = 1; x <
countX - 1; x++) {
35 for (
int y = 1; y <
countY - 1; y++) {
36 uint32_t index = x +
countX * y;
37 uint32_t top = x +
countX * (y - 1);
38 uint32_t bot = x +
countX * (y + 1);
39 uint32_t rgt = index + 1;
40 uint32_t lft = index - 1;
42 float value = ((float)
vecDP[index] + a * ((
float)
vecD[top] + (float)
vecD[bot] + (
float)
vecD[rgt] + (float)
vecD[lft])) / aS;
55 float dtX = dt * (float)
countX;
56 float dtY = dt * (float)
countY;
58 for (
int x = 1; x <
countX - 1; x++) {
59 for (
int y = 1; y <
countY - 1; y++) {
60 uint32_t index = x +
countX * y;
61 float xA = (float)x - dtX * (
float)
vecX[index];
62 float yA = (float)y - dtY * (
float)
vecY[index];
64 if (xA < 0.5f) xA = 0.5f;
69 if (yA < 0.5f) yA = 0.5f;
84 float value = s0 * (td00 + td01) + s1 * (td10 + td11);
94 for (
int x = 0; x <
countX; x++) {
95 for (
int y = 0; y <
countY; y++) {
96 float posX = (((float)x) / ((float)
countX) - 0.5f) * 2.0f *
size.
X;
97 float posY = (((float)y) / ((float)
countY) - 0.5f) * 2.0f *
size.
Y;
99 uint32_t index = x +
countX * y;
101 vecX[index] =
Mathematics::Constrain(sinf((posX + posY) / (period * 6.28f * 1000.0f) + ratio * 6.28f) * amplitude, -1.0f, 1.0f) * 127.0f;
102 vecY[index] =
Mathematics::Constrain(cosf((posX - posY) / (period * 6.28f * 1000.0f) + ratio * 6.28f) * amplitude, -1.0f, 1.0f) * 127.0f;
103 vecD[index] =
Mathematics::Constrain((sinf((posX + posY) / (period * 6.28f * 50.0f)) + cosf((posX - posY) / (period * 6.28f * 50.0f))) * amplitude, -1.0f, 1.0f) * 127.0f;
112 for (
int x = 0; x <
countX; x++) {
113 for (
int y = 0; y <
countY; y++) {
114 float posX = (((float)x) / ((float)
countX) - 0.5f) * 2.0f *
size.
X + offsetX;
115 float posY = (((float)y) / ((float)
countY) - 0.5f) * 2.0f *
size.
Y + offsetY;
117 uint32_t index = x +
countX * y;
119 bool xOdd = (int)(posX * 0.3f / (10.0f / period)) % 2;
120 bool yOdd = (int)(posY * 0.3f / (10.0f / period)) % 2;
122 vecX[index] = xOdd ? 127 : -128;
123 vecY[index] = yOdd ? 127 : -128;
133 for (
int x = 0; x <
countX; x++) {
134 for (
int y = 0; y <
countY; y++) {
135 float posX = (((float)x) / ((float)
countX) - 0.5f) * 2.0f *
size.
X + offsetX;
136 float posY = (((float)y) / ((float)
countY) - 0.5f) * 2.0f *
size.
Y + offsetY;
138 uint32_t index = x +
countX * y;
140 if (posX < period / 2.0f && posX > -period / 2.0f && posY < period / 2.0f && posY > -period / 2.0f) {
152 for(
int x = 0; x <
countX; x++){
153 for(
int y = 0; y <
countY; y++){
154 float posX = (((float)x) / ((float)
countX) - 0.5f) * 2.0f *
size.
X;
155 float posY = (((float)y) / ((float)
countY) - 0.5f) * 2.0f *
size.
Y;
157 float magn = powf(posX * posX + posY * posY, 0.5f);
159 uint32_t index = x +
countX * y;
168 int numTriangles =
object->GetTriangleGroup()->GetTriangleCount();
172 for (
int i = 0; i < numTriangles; i++) {
179 for(
int x = 0; x <
countX; x++){
180 for(
int y = 0; y <
countY; y++){
181 float posX = (((float)x) / ((float)
countX) - 0.5f) * 2.0f *
size.
X;
182 float posY = (((float)y) / ((float)
countY) - 0.5f) * 2.0f *
size.
Y;
184 uint32_t index = x +
countX * y;
186 float u = 0.0f, v = 0.0f, w = 0.0f;
187 bool didIntersect =
false;
189 for (
int t = 0; t < numTriangles; t++) {
190 if (triangles[t]->DidIntersect(posX, posY, u, v, w)){
210 for (
int i = 0; i < numTriangles; i++){
234 this->
size.
X = sizeX;
235 this->
size.
Y = sizeY;
257 inBounds = input.
X > 0 && input.
X <
size.
X &&
258 input.
Y > 0 && input.
Y <
size.
Y;
263 uint16_t colX = (uint16_t)floorf(scaleX);
264 uint16_t rowY = (uint16_t)floorf(scaleY);
267 uint32_t q11 = rowY *
countX + colX;
268 uint32_t q12 = rowY *
countX + (colX + 1);
269 uint32_t q21 = (rowY + 1) *
countX + colX;
270 uint32_t q22 = (rowY + 1) *
countX + (colX + 1);
274 float x2 = (colX + 1) * (
size.
X / (
float)(
countX - 1));
276 float y2 = (rowY + 1) * (
size.
Y / (
float)(
countY - 1));
278 float v11 =
vecD[q11];
279 float v12 =
vecD[q12];
280 float v21 =
vecD[q21];
281 float v22 =
vecD[q22];
285 return (uint32_t)value;
298 float red = (float)densityValue / 127.0f;
300 float blue = 1.0f - (float)densityValue / 127.0f;
Represents a 2D vector field with various dynamic field effects and rendering capabilities.
virtual Triangle3D * GetTriangles()=0
Retrieves the array of Triangle3D objects representing the triangles.
static float BilinearInterpolation(float scaleX, float scaleY, float x1, float y1, float x2, float y2, float v11, float v12, float v21, float v22)
Performs a bilinear interpolation on a 2D grid.
static const float MPI
Mathematical constant (3.14159265358979323846...).
static bool IsClose(float v1, float v2, float epsilon)
Checks if two values are close within a specified epsilon.
static T Map(T value, T inLow, T inMax, T outMin, T outMax)
Maps a value from one range to another.
static T Constrain(T value, T minimum, T maximum)
Constrains a value between minimum and maximum.
Represents a 3D object with geometry, material, and transformation data.
ITriangleGroup * GetTriangleGroup()
Retrieves the modifiable geometry of the object.
Represents an RGB color and provides methods for manipulation.
Represents a 2D triangle with support for UV mapping, depth, and intersection testing.
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Vector2D Rotate(const float &angle, const Vector2D &offset) const
Rotates this vector by a specified angle (in degrees or radians) around a given offset.
float X
The X-component of the 2D vector.
float Y
The Y-component of the 2D vector.
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
float X
The X-component of the 3D vector.
float Y
The Y-component of the 3D vector.
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.
VectorField2D(uint16_t x, uint16_t y)
Constructs a 2D vector field with specified dimensions.
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.