Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bezier curve functions #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ config.set('TAISEI_BUILDCONF_HAVE_INT128', cc.sizeof('__int128') == 16)
config.set('TAISEI_BUILDCONF_HAVE_LONG_DOUBLE', cc.sizeof('long double') > 8)
config.set('TAISEI_BUILDCONF_HAVE_POSIX', have_posix)
config.set('TAISEI_BUILDCONF_HAVE_SINCOS', cc.has_function('sincos', dependencies : dep_m))
config.set('TAISEI_BUILDCONF_HAVE_SINCOSF', cc.has_function('sincosf', dependencies : dep_m))

if config.get('TAISEI_BUILDCONF_HAVE_SINCOS')
config.set('_GNU_SOURCE', true)
Expand Down
66 changes: 66 additions & 0 deletions src/util/bezier.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <[email protected]>.
* Copyright (c) 2012-2019, Andrei Alexeyev <[email protected]>.
*/

#include "taisei.h"

#include "bezier.h"
#include "miscmath.h"

cmplxf cbezier_point_at(CubicBezier curve, float t) {
float u = 1.0f - t;
float uu = u * u;
float tt = t * t;
float uuu = uu * u;
float uut3 = uu * t * 3.0f;
float utt3 = u * tt * 3.0f;
float ttt = tt * t;

return
curve.points[0] * uuu +
curve.points[1] * uut3 +
curve.points[2] * utt3 +
curve.points[3] * ttt;
}

cmplxf cbezier_derivative_at(CubicBezier curve, float t) {
float u = 1.0f - t;

return
(curve.points[1] - curve.points[0]) * (3.0f * u * u) +
(curve.points[2] - curve.points[1]) * (6.0f * u * t) +
(curve.points[3] - curve.points[2]) * (3.0f * t * t);
}

cmplxf cbezier_derivative2_at(CubicBezier curve, float t) {
float t6 = 6.0f * t;
float u6 = 6.0f - t6;

return
(curve.points[2] - 2.0f * curve.points[1] + curve.points[0]) * u6 +
(curve.points[3] - 2.0f * curve.points[2] + curve.points[1]) * t6;
}

void cbezier_scale(CubicBezier *curve, cmplxf x) {
curve->points[0] *= x;
curve->points[1] *= x;
curve->points[2] *= x;
curve->points[3] *= x;
}

void cbezier_translate(CubicBezier *curve, cmplxf x) {
curve->points[0] += x;
curve->points[1] += x;
curve->points[2] += x;
curve->points[3] += x;
}

void cbezier_rotate(CubicBezier *curve, cmplxf pivot, float angle) {
cbezier_translate(curve, -pivot);
cbezier_scale(curve, cdirf(angle));
cbezier_translate(curve, pivot);
}
31 changes: 31 additions & 0 deletions src/util/bezier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <[email protected]>.
* Copyright (c) 2012-2019, Andrei Alexeyev <[email protected]>.
*/

#ifndef IGUARD_util_bezier_h
#define IGUARD_util_bezier_h

#include "taisei.h"

typedef union CubicBezier {
struct {
cmplxf start, control[2], end;
};
struct {
cmplxf points[4];
};
} CubicBezier;

cmplxf cbezier_point_at(CubicBezier curve, float t) attr_const;
cmplxf cbezier_derivative_at(CubicBezier curve, float t) attr_const;
cmplxf cbezier_derivative2_at(CubicBezier curve, float t) attr_const;

void cbezier_scale(CubicBezier *curve, cmplxf x);
void cbezier_translate(CubicBezier *curve, cmplxf x);
void cbezier_rotate(CubicBezier *curve, cmplxf pivot, float angle);

#endif // IGUARD_util_bezier_h
1 change: 1 addition & 0 deletions src/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ util_deps = []

util_src = files(
'assert.c',
'bezier.c',
'crap.c',
'env.c',
'fbmgr.c',
Expand Down
12 changes: 12 additions & 0 deletions src/util/miscmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ cmplx cdir(double angle) {
#endif
}

cmplxf cdirf(float angle) {
// this is faster than cexp(I*angle)

#ifdef TAISEI_BUILDCONF_HAVE_SINCOSF
float s, c;
sincosf(angle, &s, &c);
return CMPLXF(c, s);
#else
return CMPLXF(cosf(angle), sinf(angle));
#endif
}

cmplx cwmul(cmplx c0, cmplx c1) {
return CMPLX(creal(c0)*creal(c1), cimag(c0)*cimag(c1));
}
Expand Down
1 change: 1 addition & 0 deletions src/util/miscmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cmplx cnormalize(cmplx c) attr_const;
cmplx cclampabs(cmplx c, double maxabs) attr_const;
cmplx cwclamp(cmplx c, cmplx cmin, cmplx cmax) attr_const;
cmplx cdir(double angle) attr_const;
cmplxf cdirf(float angle) attr_const;
cmplx cwmul(cmplx c0, cmplx c1) attr_const;
cmplxf cwmulf(cmplxf c0, cmplxf c1) attr_const;
cmplx cswap(cmplx c) attr_const;
Expand Down