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