microvg  3.0.1
microvg
LLVG_MATRIX_impl.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2021-2023 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_multiply(jfloat* dest, jfloat* a, jfloat* b) {
43 
44  dest[0] = (a[0] * b[0]) + (a[1] * b[3]) + (a[2] * b[6]);
45  dest[1] = (a[0] * b[1]) + (a[1] * b[4]) + (a[2] * b[7]);
46  dest[2] = (a[0] * b[2]) + (a[1] * b[5]) + (a[2] * b[8]);
47 
48  dest[3] = (a[3] * b[0]) + (a[4] * b[3]) + (a[5] * b[6]);
49  dest[4] = (a[3] * b[1]) + (a[4] * b[4]) + (a[5] * b[7]);
50  dest[5] = (a[3] * b[2]) + (a[4] * b[5]) + (a[5] * b[8]);
51 
52  dest[6] = (a[6] * b[0]) + (a[7] * b[3]) + (a[8] * b[6]);
53  dest[7] = (a[6] * b[1]) + (a[7] * b[4]) + (a[8] * b[7]);
54  dest[8] = (a[6] * b[2]) + (a[7] * b[5]) + (a[8] * b[8]);
55 }
56 
57 // See the header file for the function documentation
58 void LLVG_MATRIX_IMPL_setTranslate(jfloat* matrix, jfloat x, jfloat y) {
59  matrix[0] = 1.0f;
60  matrix[1] = 0.0f;
61  matrix[2] = x;
62  matrix[3] = 0.0f;
63  matrix[4] = 1.0f;
64  matrix[5] = y;
65  matrix[6] = 0.0f;
66  matrix[7] = 0.0f;
67  matrix[8] = 1.0f;
68 }
69 
70 // See the header file for the function documentation
71 void LLVG_MATRIX_IMPL_setScale(jfloat* matrix, jfloat sx, jfloat sy) {
72  LLVG_MATRIX_IMPL_identity(matrix);
73  LLVG_MATRIX_IMPL_scale(matrix, sx, sy);
74 }
75 
76 // See the header file for the function documentation
77 void LLVG_MATRIX_IMPL_setRotate(jfloat* matrix, jfloat degrees) {
78  LLVG_MATRIX_IMPL_identity(matrix);
79  LLVG_MATRIX_IMPL_rotate(matrix, degrees);
80 }
81 
82 // See the header file for the function documentation
83 void LLVG_MATRIX_IMPL_setConcat(jfloat* dest, jfloat* a, jfloat* b) {
84  if(dest == a) {
85  // cppcheck-suppress [misra-c2012-18.8] the size is a define
86  float temp[LLVG_MATRIX_SIZE];
87  LLVG_MATRIX_IMPL_copy(temp, a);
88  LLVG_MATRIX_IMPL_multiply(dest, temp, b);
89  }
90  else if (dest == b) {
91  // cppcheck-suppress [misra-c2012-18.8] the size is a define
92  float temp[LLVG_MATRIX_SIZE];
93  LLVG_MATRIX_IMPL_copy(temp, b);
94  LLVG_MATRIX_IMPL_multiply(dest, a, temp);
95  }
96  else {
97  LLVG_MATRIX_IMPL_multiply(dest, a, b);
98  }
99 }
100 
101 // See the header file for the function documentation
102 void LLVG_MATRIX_IMPL_translate(jfloat* matrix, jfloat x, jfloat y) {
103  matrix[2] = (matrix[0] * x) + (matrix[1] * y) + matrix[2];
104  matrix[5] = (matrix[3] * x) + (matrix[4] * y) + matrix[5];
105  matrix[8] = (matrix[6] * x) + (matrix[7] * y) + matrix[8];
106 }
107 
108 // See the header file for the function documentation
109 void LLVG_MATRIX_IMPL_scale(jfloat* matrix, jfloat scaleX, jfloat scaleY) {
110  matrix[0] *= scaleX;
111  matrix[1] *= scaleY;
112  matrix[3] *= scaleX;
113  matrix[4] *= scaleY;
114  matrix[6] *= scaleX;
115  matrix[7] *= scaleY;
116 }
117 
118 // See the header file for the function documentation
119 void LLVG_MATRIX_IMPL_rotate(jfloat* matrix, jfloat angleDegrees) {
120 
121  float angleRadians = DEG_TO_RAD(angleDegrees);
122 
123  // computes cosine and sine values.
124  float cosAngle = cosf(angleRadians);
125  float sinAngle = sinf(angleRadians);
126 
127  float tmp;
128 
129  tmp = (cosAngle * matrix[0]) + (sinAngle * matrix[1]);
130  matrix[1] = (cosAngle * matrix[1]) - (sinAngle * matrix[0]);
131  matrix[0] = tmp;
132 
133  tmp = (cosAngle * matrix[3]) + (sinAngle * matrix[4]);
134  matrix[4] = (cosAngle * matrix[4]) - (sinAngle * matrix[3]);
135  matrix[3] = tmp;
136 
137  tmp = (cosAngle * matrix[6]) + (sinAngle * matrix[7]);
138  matrix[7] = (cosAngle * matrix[7]) - (sinAngle * matrix[6]);
139  matrix[6] = tmp;
140 }
141 
142 // See the header file for the function documentation
143 void LLVG_MATRIX_IMPL_concatenate(jfloat* matrix, jfloat* other) {
144  // cppcheck-suppress [misra-c2012-18.8] the size is a define
145  float temp[LLVG_MATRIX_SIZE];
146  LLVG_MATRIX_IMPL_copy(temp, matrix);
147  LLVG_MATRIX_IMPL_multiply(matrix, temp, other);
148 }
149 
150 // See the header file for the function documentation
151 void LLVG_MATRIX_IMPL_postTranslate(jfloat* matrix, jfloat dx, jfloat dy) {
152  // cppcheck-suppress [misra-c2012-18.8] the size is a define
153  float a[LLVG_MATRIX_SIZE];
154  // cppcheck-suppress [misra-c2012-18.8] the size is a define
155  float b[LLVG_MATRIX_SIZE];
156  LLVG_MATRIX_IMPL_setTranslate(a, dx, dy);
157  LLVG_MATRIX_IMPL_copy(b, matrix);
158  LLVG_MATRIX_IMPL_multiply(matrix, a, b);
159 }
160 
161 // See the header file for the function documentation
162 void LLVG_MATRIX_IMPL_postScale(jfloat* matrix, jfloat sx, jfloat sy) {
163  // cppcheck-suppress [misra-c2012-18.8] the size is a define
164  float a[LLVG_MATRIX_SIZE];
165  // cppcheck-suppress [misra-c2012-18.8] the size is a define
166  float b[LLVG_MATRIX_SIZE];
167  LLVG_MATRIX_IMPL_identity(a);
168  LLVG_MATRIX_IMPL_scale(a, sx, sy);
169  LLVG_MATRIX_IMPL_copy(b, matrix);
170  LLVG_MATRIX_IMPL_multiply(matrix, a, b);
171 }
172 
173 // See the header file for the function documentation
174 void LLVG_MATRIX_IMPL_postRotate(jfloat* matrix, jfloat degrees) {
175  // cppcheck-suppress [misra-c2012-18.8] the size is a define
176  float a[LLVG_MATRIX_SIZE];
177  // cppcheck-suppress [misra-c2012-18.8] the size is a define
178  float b[LLVG_MATRIX_SIZE];
179  LLVG_MATRIX_IMPL_identity(a);
180  LLVG_MATRIX_IMPL_rotate(a, degrees);
181  LLVG_MATRIX_IMPL_copy(b, matrix);
182  LLVG_MATRIX_IMPL_multiply(matrix, a, b);
183 }
184 
185 // See the header file for the function documentation
186 void LLVG_MATRIX_IMPL_postConcat(jfloat* matrix, jfloat* other) {
187  // cppcheck-suppress [misra-c2012-18.8] the size is a define
188  float a[LLVG_MATRIX_SIZE];
189  LLVG_MATRIX_IMPL_copy(a, matrix);
190  LLVG_MATRIX_IMPL_multiply(matrix, other, a);
191 }
192 
193 // -----------------------------------------------------------------------------
194 // EOF
195 // -----------------------------------------------------------------------------
MicroEJ MicroVG library low level API: helper to implement library natives methods.