2d: add translate2d and its friends

This commit is contained in:
Recep Aslantas
2020-07-31 11:40:52 +03:00
parent 6dc37f6cc9
commit bb751d32ca
6 changed files with 186 additions and 3 deletions

100
include/cglm/affine2d.h Normal file
View File

@@ -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 */

View File

@@ -32,6 +32,7 @@ extern "C" {
#include "call/curve.h"
#include "call/bezier.h"
#include "call/ray.h"
#include "call/affine2d.h"
#ifdef __cplusplus
}

View File

@@ -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 */

View File

@@ -31,5 +31,6 @@
#include "curve.h"
#include "bezier.h"
#include "ray.h"
#include "affine2d.h"
#endif /* cglm_h */