From bb751d32ca0fbcd9adda9daed4ba9bd120018904 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Fri, 31 Jul 2020 11:40:52 +0300 Subject: [PATCH] 2d: add translate2d and its friends --- Makefile.am | 9 ++-- include/cglm/affine2d.h | 100 +++++++++++++++++++++++++++++++++++ include/cglm/call.h | 1 + include/cglm/call/affine2d.h | 39 ++++++++++++++ include/cglm/cglm.h | 1 + src/affine2d.c | 39 ++++++++++++++ 6 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 include/cglm/affine2d.h create mode 100644 include/cglm/call/affine2d.h create mode 100644 src/affine2d.c diff --git a/Makefile.am b/Makefile.am index cc9c692..a4ec31d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,7 +65,8 @@ cglm_HEADERS = include/cglm/version.h \ include/cglm/curve.h \ include/cglm/bezier.h \ include/cglm/applesimd.h \ - include/cglm/ray.h + include/cglm/ray.h \ + include/cglm/affine2d.h cglm_calldir=$(includedir)/cglm/call cglm_call_HEADERS = include/cglm/call/mat4.h \ @@ -87,7 +88,8 @@ cglm_call_HEADERS = include/cglm/call/mat4.h \ include/cglm/call/ease.h \ include/cglm/call/curve.h \ include/cglm/call/bezier.h \ - include/cglm/call/ray.h + include/cglm/call/ray.h \ + include/cglm/call/affine2d.h cglm_simddir=$(includedir)/cglm/simd cglm_simd_HEADERS = include/cglm/simd/intrin.h \ @@ -151,7 +153,8 @@ libcglm_la_SOURCES=\ src/ease.c \ src/curve.c \ src/bezier.c \ - src/ray.c + src/ray.c \ + src/affine2d.c test_tests_SOURCES=\ test/runner.c \ diff --git a/include/cglm/affine2d.h b/include/cglm/affine2d.h new file mode 100644 index 0000000..19e76b3 --- /dev/null +++ b/include/cglm/affine2d.h @@ -0,0 +1,100 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Functions: + + */ + +#ifndef cglm_affine2d_h +#define cglm_affine2d_h + +#include "common.h" +#include "util.h" +#include "vec2.h" +#include "vec3.h" +#include "vec4.h" +#include "mat4.h" +#include "mat3.h" +#include "affine-mat.h" + +/*! + * @brief translate existing 2d transform matrix by v vector + * and stores result in same matrix + * + * @param[in, out] m affine transfrom + * @param[in] v translate vector [x, y, z] + */ +CGLM_INLINE +void +glm_translate2d(mat3 m, vec2 v) { + m[2][0] = m[0][0] * v[0] + m[1][0] * v[1] + m[2][0]; + m[2][1] = m[0][1] * v[0] + m[1][1] * v[1] + m[2][1]; + m[2][2] = m[0][2] * v[0] + m[1][2] * v[1] + m[2][2]; +} + +/*! + * @brief translate existing 2d transform matrix by v vector + * and store result in dest + * + * source matrix will remain same + * + * @param[in] m affine transfrom + * @param[in] v translate vector [x, y, z] + * @param[out] dest translated matrix + */ +CGLM_INLINE +void +glm_translate2d_to(mat3 m, vec2 v, mat3 dest) { + glm_mat3_copy(m, dest); + glm_translate2d(dest, v); +} + +/*! + * @brief translate existing 2d transform matrix by x factor + * + * @param[in, out] m affine transfrom + * @param[in] x x factor + */ +CGLM_INLINE +void +glm_translate2d_x(mat3 m, float x) { + m[2][0] = m[0][0] * x + m[2][0]; + m[2][1] = m[0][1] * x + m[2][1]; + m[2][2] = m[0][2] * x + m[2][2]; +} + +/*! + * @brief translate existing 2d transform matrix by y factor + * + * @param[in, out] m affine transfrom + * @param[in] y y factor + */ +CGLM_INLINE +void +glm_translate2d_y(mat3 m, float y) { + m[2][0] = m[1][0] * y + m[2][0]; + m[2][1] = m[1][1] * y + m[2][1]; + m[2][2] = m[1][2] * y + m[2][2]; +} + +/*! + * @brief creates NEW translate 2d transform matrix by v vector + * + * @param[out] m affine transfrom + * @param[in] v translate vector [x, y, z] + */ +CGLM_INLINE +void +glm_translate2d_make(mat3 m, vec2 v) { + glm_mat3_identity(m); + m[2][0] = v[0]; + m[2][1] = v[1]; +} + + +#endif /* cglm_affine2d_h */ diff --git a/include/cglm/call.h b/include/cglm/call.h index b51b731..ffb3532 100644 --- a/include/cglm/call.h +++ b/include/cglm/call.h @@ -32,6 +32,7 @@ extern "C" { #include "call/curve.h" #include "call/bezier.h" #include "call/ray.h" +#include "call/affine2d.h" #ifdef __cplusplus } diff --git a/include/cglm/call/affine2d.h b/include/cglm/call/affine2d.h new file mode 100644 index 0000000..cbc644e --- /dev/null +++ b/include/cglm/call/affine2d.h @@ -0,0 +1,39 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglmc_affine2d_h +#define cglmc_affine2d_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" + +CGLM_EXPORT +void +glmc_translate2d_make(mat3 m, vec2 v); + +CGLM_EXPORT +void +glmc_translate2d_to(mat3 m, vec2 v, mat3 dest); + +CGLM_EXPORT +void +glmc_translate2d(mat3 m, vec2 v); + +CGLM_EXPORT +void +glmc_translate2d_x(mat3 m, float to); + +CGLM_EXPORT +void +glmc_translate2d_y(mat3 m, float to); + +#ifdef __cplusplus +} +#endif +#endif /* cglmc_affine2d_h */ diff --git a/include/cglm/cglm.h b/include/cglm/cglm.h index afc7f99..5ff3421 100644 --- a/include/cglm/cglm.h +++ b/include/cglm/cglm.h @@ -31,5 +31,6 @@ #include "curve.h" #include "bezier.h" #include "ray.h" +#include "affine2d.h" #endif /* cglm_h */ diff --git a/src/affine2d.c b/src/affine2d.c new file mode 100644 index 0000000..bb24ea3 --- /dev/null +++ b/src/affine2d.c @@ -0,0 +1,39 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "../include/cglm/cglm.h" +#include "../include/cglm/call.h" + +CGLM_EXPORT +void +glmc_translate2d_make(mat3 m, vec2 v) { + glm_translate2d_make(m, v); +} + +CGLM_EXPORT +void +glmc_translate2d_to(mat3 m, vec2 v, mat3 dest) { + glm_translate2d_to(m, v, dest); +} + +CGLM_EXPORT +void +glmc_translate2d(mat3 m, vec2 v) { + glm_translate2d(m, v); +} + +CGLM_EXPORT +void +glmc_translate2d_x(mat3 m, float to) { + glm_translate2d_x(m, to); +} + +CGLM_EXPORT +void +glmc_translate2d_y(mat3 m, float to) { + glm_translate2d_y(m, to); +}