microvg  2.0.0
microvg
LLVG_MATRIX_impl.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2021-2022 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
16 // -----------------------------------------------------------------------------
17 // Includes
18 // -----------------------------------------------------------------------------
19 
20 #include <math.h>
21 #include <string.h>
22 
23 #include <LLVG_MATRIX_impl.h>
24 
25 #include "microvg_helper.h"
26 
27 // -----------------------------------------------------------------------------
28 // LLVG_MATRIX_impl.h functions
29 // -----------------------------------------------------------------------------
30 
31 // See the header file for the function documentation
32 void LLVG_MATRIX_IMPL_identity(jfloat* matrix) {
33  LLVG_MATRIX_IMPL_setTranslate(matrix, 0, 0);
34 }
35 
36 // See the header file for the function documentation
37 void LLVG_MATRIX_IMPL_copy(jfloat* dest, jfloat* src) {
38  (void)memcpy((void*)dest, (void*)src, sizeof(float) * LLVG_MATRIX_SIZE);
39 }
40 
41 // See the header file for the function documentation
42 void LLVG_MATRIX_IMPL_setTranslate(jfloat* matrix, jfloat x, jfloat y) {
43  matrix[0] = 1.0f;
44  matrix[1] = 0.0f;
45  matrix[2] = x;
46  matrix[3] = 0.0f;
47  matrix[4] = 1.0f;
48  matrix[5] = y;
49  matrix[6] = 0.0f;
50  matrix[7] = 0.0f;
51  matrix[8] = 1.0f;
52 }
53 
54 // See the header file for the function documentation
55 void LLVG_MATRIX_IMPL_setScale(jfloat* matrix, jfloat sx, jfloat sy) {
56  LLVG_MATRIX_IMPL_identity(matrix);
57  LLVG_MATRIX_IMPL_scale(matrix, sx, sy);
58 }
59 
60 // See the header file for the function documentation
61 void LLVG_MATRIX_IMPL_setRotate(jfloat* matrix, jfloat degrees) {
62  LLVG_MATRIX_IMPL_identity(matrix);
63  LLVG_MATRIX_IMPL_rotate(matrix, degrees);
64 }
65 
66 // See the header file for the function documentation
67 void LLVG_MATRIX_IMPL_setConcat(jfloat* dest, jfloat* a, jfloat* b) {
68  LLVG_MATRIX_IMPL_copy(dest, a);
69  LLVG_MATRIX_IMPL_concatenate(dest, b);
70 }
71 
72 // See the header file for the function documentation
73 void LLVG_MATRIX_IMPL_translate(jfloat* matrix, jfloat x, jfloat y) {
74  matrix[2] = (matrix[0] * x) + (matrix[1] * y) + matrix[2];
75  matrix[5] = (matrix[3] * x) + (matrix[4] * y) + matrix[5];
76  matrix[8] = (matrix[6] * x) + (matrix[7] * y) + matrix[8];
77 }
78 
79 // See the header file for the function documentation
80 void LLVG_MATRIX_IMPL_scale(jfloat* matrix, jfloat scaleX, jfloat scaleY) {
81  matrix[0] *= scaleX;
82  matrix[1] *= scaleY;
83  matrix[3] *= scaleX;
84  matrix[4] *= scaleY;
85  matrix[6] *= scaleX;
86  matrix[7] *= scaleY;
87 }
88 
89 // See the header file for the function documentation
90 void LLVG_MATRIX_IMPL_rotate(jfloat* matrix, jfloat angleDegrees) {
91 
92  float angleRadians = DEG_TO_RAD(angleDegrees);
93 
94  // computes cosine and sine values.
95  float cosAngle = cosf(angleRadians);
96  float sinAngle = sinf(angleRadians);
97 
98  float tmp;
99 
100  tmp = (cosAngle * matrix[0]) + (sinAngle * matrix[1]);
101  matrix[1] = (cosAngle * matrix[1]) - (sinAngle * matrix[0]);
102  matrix[0] = tmp;
103 
104  tmp = (cosAngle * matrix[3]) + (sinAngle * matrix[4]);
105  matrix[4] = (cosAngle * matrix[4]) - (sinAngle * matrix[3]);
106  matrix[3] = tmp;
107 
108  tmp = (cosAngle * matrix[6]) + (sinAngle * matrix[7]);
109  matrix[7] = (cosAngle * matrix[7]) - (sinAngle * matrix[6]);
110  matrix[6] = tmp;
111 }
112 
113 // See the header file for the function documentation
114 void LLVG_MATRIX_IMPL_concatenate(jfloat* matrix, jfloat* other) {
115  // cppcheck-suppress [misra-c2012-18.8] the size is a define
116  float temp[LLVG_MATRIX_SIZE];
117 
118  temp[0] = (matrix[0] * other[0]) + (matrix[1] * other[3]) + (matrix[2] * other[6]);
119  temp[1] = (matrix[0] * other[1]) + (matrix[1] * other[4]) + (matrix[2] * other[7]);
120  temp[2] = (matrix[0] * other[2]) + (matrix[1] * other[5]) + (matrix[2] * other[8]);
121 
122  temp[3] = (matrix[3] * other[0]) + (matrix[4] * other[3]) + (matrix[5] * other[6]);
123  temp[4] = (matrix[3] * other[1]) + (matrix[4] * other[4]) + (matrix[5] * other[7]);
124  temp[5] = (matrix[3] * other[2]) + (matrix[4] * other[5]) + (matrix[5] * other[8]);
125 
126  temp[6] = (matrix[6] * other[0]) + (matrix[7] * other[3]) + (matrix[8] * other[6]);
127  temp[7] = (matrix[6] * other[1]) + (matrix[7] * other[4]) + (matrix[8] * other[7]);
128  temp[8] = (matrix[6] * other[2]) + (matrix[7] * other[5]) + (matrix[8] * other[8]);
129 
130  /* Copy temporary matrix into result. */
131  LLVG_MATRIX_IMPL_copy(matrix, temp);
132 }
133 
134 // See the header file for the function documentation
135 void LLVG_MATRIX_IMPL_postTranslate(jfloat* matrix, jfloat dx, jfloat dy) {
136  // cppcheck-suppress [misra-c2012-18.8] the size is a define
137  float temp[LLVG_MATRIX_SIZE];
138  LLVG_MATRIX_IMPL_setTranslate(temp, dx, dy);
139  LLVG_MATRIX_IMPL_concatenate(temp, matrix);
140  LLVG_MATRIX_IMPL_copy(matrix, temp);
141 }
142 
143 // See the header file for the function documentation
144 void LLVG_MATRIX_IMPL_postScale(jfloat* matrix, jfloat sx, jfloat sy) {
145  // cppcheck-suppress [misra-c2012-18.8] the size is a define
146  float temp[LLVG_MATRIX_SIZE];
147  LLVG_MATRIX_IMPL_identity(temp);
148  LLVG_MATRIX_IMPL_scale(temp, sx, sy);
149  LLVG_MATRIX_IMPL_concatenate(temp, matrix);
150  LLVG_MATRIX_IMPL_copy(matrix, temp);
151 }
152 
153 // See the header file for the function documentation
154 void LLVG_MATRIX_IMPL_postRotate(jfloat* matrix, jfloat degrees) {
155  // cppcheck-suppress [misra-c2012-18.8] the size is a define
156  float temp[LLVG_MATRIX_SIZE];
157  LLVG_MATRIX_IMPL_identity(temp);
158  LLVG_MATRIX_IMPL_rotate(temp, degrees);
159  LLVG_MATRIX_IMPL_concatenate(temp, matrix);
160  LLVG_MATRIX_IMPL_copy(matrix, temp);
161 }
162 
163 // See the header file for the function documentation
164 void LLVG_MATRIX_IMPL_postConcat(jfloat* matrix, jfloat* other) {
165  // cppcheck-suppress [misra-c2012-18.8] the size is a define
166  float temp[LLVG_MATRIX_SIZE];
167  LLVG_MATRIX_IMPL_copy(temp, other);
168  LLVG_MATRIX_IMPL_concatenate(temp, matrix);
169  LLVG_MATRIX_IMPL_copy(matrix, temp);
170 }
171 
172 // -----------------------------------------------------------------------------
173 // EOF
174 // -----------------------------------------------------------------------------
MicroEJ MicroVG library low level API: helper to implement library natives methods.