Skip to content

Commit

Permalink
Make curve functions universal.
Browse files Browse the repository at this point in the history
Add operators for Vector.
  • Loading branch information
gregersn committed Nov 14, 2024
1 parent 14f867b commit 69ecaf4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
8 changes: 6 additions & 2 deletions include/pyro/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ namespace Pyro
const int QUADS{6};
const int QUAD_STRIP{7};

float bezierpoint(float a, float b, float c, float d, float t);
float curvepoint(float p0, float p1, float p2, float p3, float t);
template <typename T>
T bezierpoint(T a, T b, T c, T d, float t);

template <typename T>
T curvepoint(T p0, T p1, T p2, T p3, float t);

enum class PointType
{
VERTEX,
Expand Down
8 changes: 8 additions & 0 deletions include/pyro/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ namespace Pyro
return Vector(x - other.x, y - other.y, z - other.z);
}

Vector operator-() const
{
return Vector(-x, -y, -z);
}

void operator-=(Vector const &other)
{
x -= other.x;
Expand Down Expand Up @@ -139,6 +144,9 @@ namespace Pyro
return oss.str();
}
};

Vector operator*(float const f, Vector const v);

}

#endif
27 changes: 12 additions & 15 deletions src/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ namespace Pyro
{
unsigned int curve_resolution = 32;
void curveresolution(unsigned int res) { curve_resolution = res; }
float bezierpoint(float a, float b, float c, float d, float t)

template <typename T>
T bezierpoint(T a, T b, T c, T d, float t)
{
float ab = lerp(a, b, t);
float bc = lerp(b, c, t);
float cd = lerp(c, d, t);
T ab = lerp(a, b, t);
T bc = lerp(b, c, t);
T cd = lerp(c, d, t);

float ac = lerp(ab, bc, t);
float bd = lerp(bc, cd, t);
T ac = lerp(ab, bc, t);
T bd = lerp(bc, cd, t);

return lerp(ac, bd, t);
}

float curvepoint(float p0, float p1, float p2, float p3, float t)
template <typename T>
T curvepoint(T p0, T p1, T p2, T p3, float t)
{
return 0.5 * ((2 * p1) +
(-p0 + p2) * t +
Expand Down Expand Up @@ -103,10 +106,7 @@ namespace Pyro

for (unsigned int i = 0; i < curve_resolution + 1; i++)
{
contour.push_back(
Pyro::Vector(
curvepoint(p0.v.x, point.v.x, p2.v.x, p3.v.x, i * delta),
curvepoint(p0.v.y, point.v.y, p2.v.y, p3.v.y, i * delta)));
contour.push_back(curvepoint(p0.v, point.v, p2.v, p3.v, i * delta));
}
}
else if (point.type == PointType::BEZIERVERTEX)
Expand All @@ -129,10 +129,7 @@ namespace Pyro

for (unsigned int i = 1; i < curve_resolution + 1; i++)
{
contour.push_back(
Pyro::Vector(
bezierpoint(p0.v.x, point.v.x, p2.v.x, p3.v.x, i * delta),
bezierpoint(p0.v.y, point.v.y, p2.v.y, p3.v.y, i * delta)));
contour.push_back(bezierpoint(p0.v, point.v, p2.v, p3.v, i * delta));
}
curveiterator += 2;
}
Expand Down
4 changes: 4 additions & 0 deletions src/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,9 @@ namespace Pyro
{
return Vector(x / v, y / v, z / v);
}
Vector operator*(float const f, Vector const v)
{
return Vector(v.x * f, v.y * f, v.z * f);
}

}

0 comments on commit 69ecaf4

Please sign in to comment.