Optimize Curve:render;

No longer does dynamic memory allocation and sets the initial table
size properly to avoid reallocs.
This commit is contained in:
bjorn 2020-08-28 03:57:39 -06:00
parent 37522bd8a3
commit b8598dfe64
3 changed files with 11 additions and 23 deletions

View File

@ -33,20 +33,18 @@ static int l_lovrCurveRender(lua_State* L) {
if (lovrCurveGetPointCount(curve) == 2) {
n = 2;
}
float* points = malloc(4 * n * sizeof(float));
lovrAssert(points, "Out of memory");
lovrCurveRender(curve, t1, t2, points, n);
lua_createtable(L, n, 0);
int j = 1;
for (int i = 0; i < 4 * n; i += 4) {
lua_pushnumber(L, points[i + 0]);
lua_rawseti(L, -2, j++);
lua_pushnumber(L, points[i + 1]);
lua_rawseti(L, -2, j++);
lua_pushnumber(L, points[i + 2]);
lua_rawseti(L, -2, j++);
lua_createtable(L, n * 3, 0);
float step = 1.f / (n - 1);
for (int i = 0; i < n; i++) {
float point[4];
lovrCurveEvaluate(curve, t1 + (t2 - t1) * i * step, point);
lua_pushnumber(L, point[0]);
lua_rawseti(L, -2, 3 * i + 1);
lua_pushnumber(L, point[1]);
lua_rawseti(L, -2, 3 * i + 2);
lua_pushnumber(L, point[2]);
lua_rawseti(L, -2, 3 * i + 3);
}
free(points);
return 1;
}

View File

@ -77,15 +77,6 @@ void lovrCurveGetTangent(Curve* curve, float t, vec3 p) {
vec3_normalize(p);
}
void lovrCurveRender(Curve* curve, float t1, float t2, vec3 points, uint32_t n) {
lovrAssert(curve->points.length >= 8, "Need at least 2 points to render a Curve");
lovrAssert(t1 >= 0.f && t2 <= 1.f, "Curve render interval must be within [0, 1]");
float step = 1.f / (n - 1);
for (uint32_t i = 0; i < n; i++) {
evaluate(curve->points.data, curve->points.length / 4, t1 + (t2 - t1) * i * step, points + 4 * i);
}
}
Curve* lovrCurveSlice(Curve* curve, float t1, float t2) {
lovrAssert(curve->points.length >= 8, "Need at least 2 points to slice a Curve");
lovrAssert(t1 >= 0.f && t2 <= 1.f, "Curve slice interval must be within [0, 1]");

View File

@ -6,7 +6,6 @@ Curve* lovrCurveCreate(void);
void lovrCurveDestroy(void* ref);
void lovrCurveEvaluate(Curve* curve, float t, float point[4]);
void lovrCurveGetTangent(Curve* curve, float t, float point[4]);
void lovrCurveRender(Curve* curve, float t1, float t2, float* points, uint32_t n);
Curve* lovrCurveSlice(Curve* curve, float t1, float t2);
size_t lovrCurveGetPointCount(Curve* curve);
void lovrCurveGetPoint(Curve* curve, size_t index, float point[4]);