common math: code refactoring

introduce common math functions to improve the reusability.
This commit is contained in:
Hermet Park 2021-11-03 17:07:38 +09:00 committed by Hermet Park
parent 7c1b16ba11
commit c3a29bd648
10 changed files with 163 additions and 164 deletions

View file

@ -17,6 +17,7 @@ source_file = [
'tvgLoader.h',
'tvgLoadModule.h',
'tvgLzw.h',
'tvgMath.h',
'tvgPictureImpl.h',
'tvgRender.h',
'tvgIteratorAccessor.h',

View file

@ -299,9 +299,6 @@ bool mathSmallCubic(const SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, Sw
SwFixed mathMean(SwFixed angle1, SwFixed angle2);
SwPoint mathTransform(const Point* to, const Matrix* transform);
bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, SwBBox& renderRegion, bool fastTrack);
bool mathInverse(const Matrix* m, Matrix* invM);
bool mathMultiply(const Matrix* lhs, Matrix* rhs);
bool mathIdentity(const Matrix* m);
void shapeReset(SwShape* shape);
bool shapePrepare(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid, bool hasComposite);

View file

@ -19,8 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <float.h>
#include <math.h>
#include "tvgMath.h"
#include "tvgSwCommon.h"
@ -117,10 +116,10 @@ bool _prepareLinear(SwFill* fill, const LinearGradient* linear, const Matrix* tr
fill->linear.offset = -fill->linear.dx * x1 - fill->linear.dy * y1;
auto gradTransform = linear->transform();
bool isTransformation = !mathIdentity(&gradTransform);
bool isTransformation = !mathIdentity((const Matrix*)(&gradTransform));
if (isTransformation) {
if (transform) mathMultiply(transform, &gradTransform);
if (transform) gradTransform = mathMultiply(transform, &gradTransform);
} else if (transform) {
gradTransform = *transform;
isTransformation = true;
@ -156,10 +155,10 @@ bool _prepareRadial(SwFill* fill, const RadialGradient* radial, const Matrix* tr
fill->radial.a = radius;
auto gradTransform = radial->transform();
bool isTransformation = !mathIdentity(&gradTransform);
bool isTransformation = !mathIdentity((const Matrix*)(&gradTransform));
if (isTransformation) {
if (transform) mathMultiply(transform, &gradTransform);
if (transform) gradTransform = mathMultiply(transform, &gradTransform);
} else if (transform) {
gradTransform = *transform;
isTransformation = true;

View file

@ -20,7 +20,6 @@
* SOFTWARE.
*/
#include <math.h>
#include <float.h>
#include "tvgSwCommon.h"
@ -496,64 +495,4 @@ bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, S
renderRegion.max.x <= clipRegion.min.x || renderRegion.max.y <= clipRegion.min.y) return false;
return true;
}
bool mathInverse(const Matrix* m, Matrix* invM)
{
auto det = m->e11 * (m->e22 * m->e33 - m->e32 * m->e23) -
m->e12 * (m->e21 * m->e33 - m->e23 * m->e31) +
m->e13 * (m->e21 * m->e32 - m->e22 * m->e31);
if (fabsf(det) < FLT_EPSILON) return false;
auto invDet = 1 / det;
invM->e11 = (m->e22 * m->e33 - m->e32 * m->e23) * invDet;
invM->e12 = (m->e13 * m->e32 - m->e12 * m->e33) * invDet;
invM->e13 = (m->e12 * m->e23 - m->e13 * m->e22) * invDet;
invM->e21 = (m->e23 * m->e31 - m->e21 * m->e33) * invDet;
invM->e22 = (m->e11 * m->e33 - m->e13 * m->e31) * invDet;
invM->e23 = (m->e21 * m->e13 - m->e11 * m->e23) * invDet;
invM->e31 = (m->e21 * m->e32 - m->e31 * m->e22) * invDet;
invM->e32 = (m->e31 * m->e12 - m->e11 * m->e32) * invDet;
invM->e33 = (m->e11 * m->e22 - m->e21 * m->e12) * invDet;
return true;
}
bool mathMultiply(const Matrix* lhs, Matrix* rhs)
{
Matrix m;
m.e11 = lhs->e11 * rhs->e11 + lhs->e12 * rhs->e21 + lhs->e13 * rhs->e31;
m.e12 = lhs->e11 * rhs->e12 + lhs->e12 * rhs->e22 + lhs->e13 * rhs->e32;
m.e13 = lhs->e11 * rhs->e13 + lhs->e12 * rhs->e23 + lhs->e13 * rhs->e33;
m.e21 = lhs->e21 * rhs->e11 + lhs->e22 * rhs->e21 + lhs->e23 * rhs->e31;
m.e22 = lhs->e21 * rhs->e12 + lhs->e22 * rhs->e22 + lhs->e23 * rhs->e32;
m.e23 = lhs->e21 * rhs->e13 + lhs->e22 * rhs->e23 + lhs->e23 * rhs->e33;
m.e31 = lhs->e31 * rhs->e11 + lhs->e32 * rhs->e21 + lhs->e33 * rhs->e31;
m.e32 = lhs->e31 * rhs->e12 + lhs->e32 * rhs->e22 + lhs->e33 * rhs->e32;
m.e33 = lhs->e31 * rhs->e13 + lhs->e32 * rhs->e23 + lhs->e33 * rhs->e33;
*rhs = m;
return true;
}
bool mathIdentity(const Matrix* m)
{
if (m) {
if (m->e11 != 1.0f || m->e12 != 0.0f || m->e13 != 0.0f ||
m->e21 != 0.0f || m->e22 != 1.0f || m->e23 != 0.0f ||
m->e31 != 0.0f || m->e32 != 0.0f || m->e33 != 1.0f) {
return false;
}
}
return true;
}
}

View file

@ -19,10 +19,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <float.h>
#include <math.h>
#include "tvgSwCommon.h"
#include "tvgMath.h"
#include "tvgRender.h"
#include "tvgSwCommon.h"
#include "tvgSwRasterC.h"
#include "tvgSwRasterAvx.h"
#include "tvgSwRasterNeon.h"

139
src/lib/tvgMath.h Normal file
View file

@ -0,0 +1,139 @@
/*
* Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _TVG_MATH_H_
#define _TVG_MATH_H_
#define _USE_MATH_DEFINES
#include <float.h>
#include <math.h>
#include "tvgCommon.h"
static inline bool mathIdentity(const Matrix* m)
{
if (m) {
if (fabs(m->e11 - 1) > FLT_EPSILON || fabs(m->e12) > FLT_EPSILON || fabs(m->e13) > FLT_EPSILON ||
fabs(m->e21) > FLT_EPSILON || fabs(m->e22 - 1) > FLT_EPSILON || fabs(m->e23) > FLT_EPSILON ||
fabs(m->e31) > FLT_EPSILON || fabs(m->e32) > FLT_EPSILON || fabs(m->e33 - 1) > FLT_EPSILON) {
return false;
}
}
return true;
}
static inline bool mathInverse(const Matrix* m, Matrix* out)
{
auto det = m->e11 * (m->e22 * m->e33 - m->e32 * m->e23) -
m->e12 * (m->e21 * m->e33 - m->e23 * m->e31) +
m->e13 * (m->e21 * m->e32 - m->e22 * m->e31);
if (fabsf(det) < FLT_EPSILON) return false;
auto invDet = 1 / det;
out->e11 = (m->e22 * m->e33 - m->e32 * m->e23) * invDet;
out->e12 = (m->e13 * m->e32 - m->e12 * m->e33) * invDet;
out->e13 = (m->e12 * m->e23 - m->e13 * m->e22) * invDet;
out->e21 = (m->e23 * m->e31 - m->e21 * m->e33) * invDet;
out->e22 = (m->e11 * m->e33 - m->e13 * m->e31) * invDet;
out->e23 = (m->e21 * m->e13 - m->e11 * m->e23) * invDet;
out->e31 = (m->e21 * m->e32 - m->e31 * m->e22) * invDet;
out->e32 = (m->e31 * m->e12 - m->e11 * m->e32) * invDet;
out->e33 = (m->e11 * m->e22 - m->e21 * m->e12) * invDet;
return true;
}
static inline void mathIdentity(Matrix* m)
{
m->e11 = 1.0f;
m->e12 = 0.0f;
m->e13 = 0.0f;
m->e21 = 0.0f;
m->e22 = 1.0f;
m->e23 = 0.0f;
m->e31 = 0.0f;
m->e32 = 0.0f;
m->e33 = 1.0f;
}
static inline void mathScale(Matrix* m, float scale)
{
m->e11 = scale;
m->e22 = scale;
}
static inline void mathTranslate(Matrix* m, float x, float y)
{
m->e13 = x;
m->e23 = y;
}
static inline void mathRotate(Matrix* m, float degree)
{
auto radian = degree / 180.0f * M_PI;
auto cosVal = cosf(radian);
auto sinVal = sinf(radian);
m->e12 = m->e11 * -sinVal;
m->e11 *= cosVal;
m->e21 = m->e22 * sinVal;
m->e22 *= cosVal;
}
static inline void mathMultiply(Point* pt, const Matrix* transform)
{
auto tx = pt->x * transform->e11 + pt->y * transform->e12 + transform->e13;
auto ty = pt->x * transform->e21 + pt->y * transform->e22 + transform->e23;
pt->x = tx;
pt->y = ty;
}
static inline Matrix mathMultiply(const Matrix* lhs, const Matrix* rhs)
{
Matrix m;
m.e11 = lhs->e11 * rhs->e11 + lhs->e12 * rhs->e21 + lhs->e13 * rhs->e31;
m.e12 = lhs->e11 * rhs->e12 + lhs->e12 * rhs->e22 + lhs->e13 * rhs->e32;
m.e13 = lhs->e11 * rhs->e13 + lhs->e12 * rhs->e23 + lhs->e13 * rhs->e33;
m.e21 = lhs->e21 * rhs->e11 + lhs->e22 * rhs->e21 + lhs->e23 * rhs->e31;
m.e22 = lhs->e21 * rhs->e12 + lhs->e22 * rhs->e22 + lhs->e23 * rhs->e32;
m.e23 = lhs->e21 * rhs->e13 + lhs->e22 * rhs->e23 + lhs->e23 * rhs->e33;
m.e31 = lhs->e31 * rhs->e11 + lhs->e32 * rhs->e21 + lhs->e33 * rhs->e31;
m.e32 = lhs->e31 * rhs->e12 + lhs->e32 * rhs->e22 + lhs->e33 * rhs->e32;
m.e33 = lhs->e31 * rhs->e13 + lhs->e32 * rhs->e23 + lhs->e33 * rhs->e33;
return m;
}
#endif //_TVG_MATH_H_

View file

@ -21,6 +21,7 @@
*/
#include <float.h>
#include <math.h>
#include "tvgMath.h"
#include "tvgPaint.h"
/************************************************************************/
@ -261,17 +262,9 @@ bool Paint::Impl::bounds(float* x, float* y, float* w, float* h, bool transforme
auto x2 = -FLT_MAX;
auto y2 = -FLT_MAX;
//function = Point * Matrix
auto multiply = [&](Point* pt, const Matrix* transform) {
auto tx = pt->x * transform->e11 + pt->y * transform->e12 + transform->e13;
auto ty = pt->x * transform->e21 + pt->y * transform->e22 + transform->e23;
pt->x = tx;
pt->y = ty;
};
//Compute the AABB after transformation
for (int i = 0; i < 4; i++) {
multiply(&pt[i], m);
mathMultiply(&pt[i], m);
if (pt[i].x < x1) x1 = pt[i].x;
if (pt[i].x > x2) x2 = pt[i].x;

View file

@ -19,8 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <float.h>
#include <math.h>
#include "tvgMath.h"
#include "tvgRender.h"
/************************************************************************/
@ -46,8 +45,6 @@ void RenderTransform::override(const Matrix& m)
bool RenderTransform::update()
{
constexpr auto PI = 3.141592f;
if (overriding) return true;
//Init Status
@ -56,35 +53,13 @@ bool RenderTransform::update()
return false;
}
//identity
m.e11 = 1.0f;
m.e12 = 0.0f;
m.e13 = 0.0f;
m.e21 = 0.0f;
m.e22 = 1.0f;
m.e23 = 0.0f;
m.e31 = 0.0f;
m.e32 = 0.0f;
m.e33 = 1.0f;
mathIdentity(&m);
//scale
m.e11 = scale;
m.e22 = scale;
mathScale(&m, scale);
//rotation
if (fabsf(degree) > FLT_EPSILON) {
auto radian = degree / 180.0f * PI;
auto cosVal = cosf(radian);
auto sinVal = sinf(radian);
if (fabsf(degree) > FLT_EPSILON) mathRotate(&m, degree);
m.e12 = m.e11 * -sinVal;
m.e11 *= cosVal;
m.e21 = m.e22 * sinVal;
m.e22 *= cosVal;
}
m.e13 = x;
m.e23 = y;
mathTranslate(&m, x, y);
return true;
}
@ -97,15 +72,5 @@ RenderTransform::RenderTransform()
RenderTransform::RenderTransform(const RenderTransform* lhs, const RenderTransform* rhs)
{
m.e11 = lhs->m.e11 * rhs->m.e11 + lhs->m.e12 * rhs->m.e21 + lhs->m.e13 * rhs->m.e31;
m.e12 = lhs->m.e11 * rhs->m.e12 + lhs->m.e12 * rhs->m.e22 + lhs->m.e13 * rhs->m.e32;
m.e13 = lhs->m.e11 * rhs->m.e13 + lhs->m.e12 * rhs->m.e23 + lhs->m.e13 * rhs->m.e33;
m.e21 = lhs->m.e21 * rhs->m.e11 + lhs->m.e22 * rhs->m.e21 + lhs->m.e23 * rhs->m.e31;
m.e22 = lhs->m.e21 * rhs->m.e12 + lhs->m.e22 * rhs->m.e22 + lhs->m.e23 * rhs->m.e32;
m.e23 = lhs->m.e21 * rhs->m.e13 + lhs->m.e22 * rhs->m.e23 + lhs->m.e23 * rhs->m.e33;
m.e31 = lhs->m.e31 * rhs->m.e11 + lhs->m.e32 * rhs->m.e21 + lhs->m.e33 * rhs->m.e31;
m.e32 = lhs->m.e31 * rhs->m.e12 + lhs->m.e32 * rhs->m.e22 + lhs->m.e33 * rhs->m.e32;
m.e33 = lhs->m.e31 * rhs->m.e13 + lhs->m.e32 * rhs->m.e23 + lhs->m.e33 * rhs->m.e33;
}
m = mathMultiply(&lhs->m, &rhs->m);
}

View file

@ -19,8 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <float.h>
#include <math.h>
#include "tvgMath.h"
#include "tvgSaveModule.h"
#include "tvgTvgSaver.h"
#include "tvgLzw.h"
@ -57,35 +56,6 @@ static inline TvgBinCounter SERIAL_DONE(TvgBinCounter cnt)
}
static Matrix _multiply(const Matrix* lhs, const Matrix* rhs)
{
Matrix m;
m.e11 = lhs->e11 * rhs->e11 + lhs->e12 * rhs->e21 + lhs->e13 * rhs->e31;
m.e12 = lhs->e11 * rhs->e12 + lhs->e12 * rhs->e22 + lhs->e13 * rhs->e32;
m.e13 = lhs->e11 * rhs->e13 + lhs->e12 * rhs->e23 + lhs->e13 * rhs->e33;
m.e21 = lhs->e21 * rhs->e11 + lhs->e22 * rhs->e21 + lhs->e23 * rhs->e31;
m.e22 = lhs->e21 * rhs->e12 + lhs->e22 * rhs->e22 + lhs->e23 * rhs->e32;
m.e23 = lhs->e21 * rhs->e13 + lhs->e22 * rhs->e23 + lhs->e23 * rhs->e33;
m.e31 = lhs->e31 * rhs->e11 + lhs->e32 * rhs->e21 + lhs->e33 * rhs->e31;
m.e32 = lhs->e31 * rhs->e12 + lhs->e32 * rhs->e22 + lhs->e33 * rhs->e32;
m.e33 = lhs->e31 * rhs->e13 + lhs->e32 * rhs->e23 + lhs->e33 * rhs->e33;
return m;
}
static void _multiply(Point* pt, const Matrix* transform)
{
auto tx = pt->x * transform->e11 + pt->y * transform->e12 + transform->e13;
auto ty = pt->x * transform->e21 + pt->y * transform->e22 + transform->e23;
pt->x = tx;
pt->y = ty;
}
/* if the properties are identical, we can merge the shapes. */
static bool _merge(Shape* from, Shape* to)
{
@ -325,11 +295,7 @@ TvgBinCounter TvgSaver::writeTagProperty(TvgBinTag tag, TvgBinCounter cnt, const
TvgBinCounter TvgSaver::writeTransform(const Matrix* transform, TvgBinTag tag)
{
if (fabs(transform->e11 - 1) > FLT_EPSILON || fabs(transform->e12) > FLT_EPSILON || fabs(transform->e13) > FLT_EPSILON ||
fabs(transform->e21) > FLT_EPSILON || fabs(transform->e22 - 1) > FLT_EPSILON || fabs(transform->e23) > FLT_EPSILON ||
fabs(transform->e31) > FLT_EPSILON || fabs(transform->e32) > FLT_EPSILON || fabs(transform->e33 - 1) > FLT_EPSILON) {
return writeTagProperty(tag, SIZE(Matrix), transform);
}
if (!mathIdentity(transform)) return writeTagProperty(tag, SIZE(Matrix), transform);
return 0;
}
@ -451,7 +417,8 @@ TvgBinCounter TvgSaver::serializeFill(const Fill* fill, TvgBinTag tag, const Mat
cnt += writeTagProperty(TVG_TAG_FILL_COLORSTOPS, stopsCnt * SIZE(Fill::ColorStop), stops);
auto gTransform = fill->transform();
if (pTransform) gTransform = _multiply(pTransform, &gTransform);
if (pTransform) gTransform = mathMultiply(pTransform, &gTransform);
cnt += writeTransform(&gTransform, TVG_TAG_FILL_TRANSFORM);
writeReservedCount(cnt);
@ -536,7 +503,7 @@ TvgBinCounter TvgSaver::serializePath(const Shape* shape, const Matrix* transfor
fabs(transform->e21) > FLT_EPSILON || fabs(transform->e22 - 1) > FLT_EPSILON || fabs(transform->e23) > FLT_EPSILON ||
fabs(transform->e31) > FLT_EPSILON || fabs(transform->e32) > FLT_EPSILON || fabs(transform->e33 - 1) > FLT_EPSILON) {
auto p = const_cast<Point*>(pts);
for (uint32_t i = 0; i < ptsCnt; ++i) _multiply(p++, transform);
for (uint32_t i = 0; i < ptsCnt; ++i) mathMultiply(p++, transform);
}
}
@ -710,7 +677,7 @@ TvgBinCounter TvgSaver::serialize(const Paint* paint, const Matrix* pTransform,
if (!compTarget && paint->opacity() == 0) return 0;
auto transform = const_cast<Paint*>(paint)->transform();
if (pTransform) transform = _multiply(pTransform, &transform);
if (pTransform) transform = mathMultiply(pTransform, &transform);
switch (paint->identifier()) {
case TVG_CLASS_ID_SHAPE: return serializeShape(static_cast<const Shape*>(paint), pTransform, &transform);

Binary file not shown.