math: introduced a custom floating epsilon

Rather than aiming for extremely high accuracy,
a compromise can achieve with better performance.

This modification helps prevent unnecessary image rotation.

issue: https://github.com/thorvg/thorvg/issues/2265
This commit is contained in:
Hermet Park 2024-05-23 17:33:03 +09:00 committed by Hermet Park
parent 2e50ee3c3d
commit 975907731d
9 changed files with 22 additions and 21 deletions

View file

@ -31,6 +31,7 @@
#define MATH_PI 3.14159265358979323846f
#define MATH_PI2 1.57079632679489661923f
#define FLOAT_EPSILON 1.0e-06f //1.192092896e-07f
#define PATH_KAPPA 0.552284f
#define mathMin(x, y) (((x) < (y)) ? (x) : (y))
@ -58,7 +59,7 @@ static inline float mathRad2Deg(float radian)
static inline bool mathZero(float a)
{
return (fabsf(a) < FLT_EPSILON) ? true : false;
return (fabsf(a) <= FLOAT_EPSILON) ? true : false;
}
@ -70,7 +71,7 @@ static inline bool mathZero(const Point& p)
static inline bool mathEqual(float a, float b)
{
return (fabsf(a - b) < FLT_EPSILON);
return mathZero(a - b);
}
@ -88,14 +89,14 @@ static inline bool mathEqual(const Matrix& a, const Matrix& b)
static inline bool mathRightAngle(const Matrix* m)
{
auto radian = fabsf(atan2f(m->e21, m->e11));
if (radian < FLT_EPSILON || mathEqual(radian, MATH_PI2) || mathEqual(radian, MATH_PI)) return true;
if (radian < FLOAT_EPSILON || mathEqual(radian, MATH_PI2) || mathEqual(radian, MATH_PI)) return true;
return false;
}
static inline bool mathSkewed(const Matrix* m)
{
return (fabsf(m->e21 + m->e12) > FLT_EPSILON);
return !mathZero(m->e21 + m->e12);
}

View file

@ -165,7 +165,7 @@ bool LottieLoader::header()
++p;
}
if (frameRate < FLT_EPSILON) {
if (frameRate < FLOAT_EPSILON) {
TVGLOG("LOTTIE", "Not a Lottie file? Frame rate is 0!");
return false;
}

View file

@ -988,7 +988,7 @@ static bool _attrParseSvgNode(void* data, const char* key, const char* value)
} else if (!strcmp(key, "style")) {
return simpleXmlParseW3CAttribute(value, strlen(value), _parseStyleAttr, loader);
#ifdef THORVG_LOG_ENABLED
} else if ((!strcmp(key, "x") || !strcmp(key, "y")) && fabsf(strToFloat(value, nullptr)) > FLT_EPSILON) {
} else if ((!strcmp(key, "x") || !strcmp(key, "y")) && fabsf(strToFloat(value, nullptr)) > FLOAT_EPSILON) {
TVGLOG("SVG", "Unsupported attributes used [Elements type: Svg][Attribute: %s][Value: %s]", key, value);
#endif
} else {
@ -1824,8 +1824,8 @@ static bool _attrParseRectNode(void* data, const char* key, const char* value)
if (!strncmp(rectTags[i].tag, "rx", sz)) rect->hasRx = true;
if (!strncmp(rectTags[i].tag, "ry", sz)) rect->hasRy = true;
if ((rect->rx >= FLT_EPSILON) && (rect->ry < FLT_EPSILON) && rect->hasRx && !rect->hasRy) rect->ry = rect->rx;
if ((rect->ry >= FLT_EPSILON) && (rect->rx < FLT_EPSILON) && !rect->hasRx && rect->hasRy) rect->rx = rect->ry;
if ((rect->rx >= FLOAT_EPSILON) && (rect->ry < FLOAT_EPSILON) && rect->hasRx && !rect->hasRy) rect->ry = rect->rx;
if ((rect->ry >= FLOAT_EPSILON) && (rect->rx < FLOAT_EPSILON) && !rect->hasRx && rect->hasRy) rect->rx = rect->ry;
return ret;
}
}
@ -3693,7 +3693,7 @@ SvgLoader::~SvgLoader()
void SvgLoader::run(unsigned tid)
{
//According to the SVG standard the value of the width/height of the viewbox set to 0 disables rendering
if ((viewFlag & SvgViewFlag::Viewbox) && (fabsf(vw) <= FLT_EPSILON || fabsf(vh) <= FLT_EPSILON)) {
if ((viewFlag & SvgViewFlag::Viewbox) && (fabsf(vw) <= FLOAT_EPSILON || fabsf(vh) <= FLOAT_EPSILON)) {
TVGLOG("SVG", "The <viewBox> width and/or height set to 0 - rendering disabled.");
root = Scene::gen().release();
return;

View file

@ -140,7 +140,7 @@ bool _prepareLinear(SwFill* fill, const LinearGradient* linear, const Matrix* tr
fill->linear.dy = y2 - y1;
fill->linear.len = fill->linear.dx * fill->linear.dx + fill->linear.dy * fill->linear.dy;
if (fill->linear.len < FLT_EPSILON) return true;
if (fill->linear.len < FLOAT_EPSILON) return true;
fill->linear.dx /= fill->linear.len;
fill->linear.dy /= fill->linear.len;
@ -182,7 +182,7 @@ bool _prepareRadial(SwFill* fill, const RadialGradient* radial, const Matrix* tr
auto fy = P(radial)->fy;
auto fr = P(radial)->fr;
if (r < FLT_EPSILON) return true;
if (r < FLOAT_EPSILON) return true;
fill->radial.dr = r - fr;
fill->radial.dx = cx - fx;

View file

@ -1553,7 +1553,7 @@ static bool _rasterSolidGradientRect(SwSurface* surface, const SwBBox& region, c
static bool _rasterLinearGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill)
{
if (fill->linear.len < FLT_EPSILON) return false;
if (fill->linear.len < FLOAT_EPSILON) return false;
if (_compositing(surface)) {
if (_matting(surface)) return _rasterGradientMattedRect<FillLinear>(surface, region, fill);

View file

@ -124,7 +124,7 @@ static void _dashLineTo(SwDashStroke& dash, const Point* to, const Matrix* trans
len -= dash.curLen;
lineSplitAt(cur, dash.curLen, left, right);
if (!dash.curOpGap) {
if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLOAT_EPSILON) {
_outlineMoveTo(*dash.outline, &left.pt1, transform);
dash.move = false;
}
@ -185,7 +185,7 @@ static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ct
len -= dash.curLen;
bezSplitAt(cur, dash.curLen, left, right);
if (!dash.curOpGap) {
if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLT_EPSILON) {
if (dash.move || dash.pattern[dash.curIdx] - dash.curLen < FLOAT_EPSILON) {
_outlineMoveTo(*dash.outline, &left.start, transform);
dash.move = false;
}

View file

@ -67,7 +67,7 @@ struct Shape::Impl
if (opacity == 0) return false;
//Shape composition is only necessary when stroking & fill are valid.
if (!rs.stroke || rs.stroke->width < FLT_EPSILON || (!rs.stroke->fill && rs.stroke->color[3] == 0)) return false;
if (!rs.stroke || rs.stroke->width < FLOAT_EPSILON || (!rs.stroke->fill && rs.stroke->color[3] == 0)) return false;
if (!rs.fill && rs.color[3] == 0) return false;
//translucent fill & stroke
@ -301,7 +301,7 @@ struct Shape::Impl
}
for (uint32_t i = 0; i < cnt; i++) {
if (pattern[i] < FLT_EPSILON) return Result::InvalidArguments;
if (pattern[i] < FLOAT_EPSILON) return Result::InvalidArguments;
}
//Reset dash

View file

@ -133,7 +133,7 @@ bool GifSaver::save(Animation* animation, Paint* bg, const string& path, TVG_UNU
if (x < 0) vsize[0] += x;
if (y < 0) vsize[1] += y;
if (vsize[0] < FLT_EPSILON || vsize[1] < FLT_EPSILON) {
if (vsize[0] < FLOAT_EPSILON || vsize[1] < FLOAT_EPSILON) {
TVGLOG("GIF_SAVER", "Saving animation(%p) has zero view size.", animation);
return false;
}

View file

@ -105,7 +105,7 @@ static bool _merge(Shape* from, Shape* to)
if (r != r2 || g != g2 || b != b2 || a != a2) return false;
if (fabs(from->strokeWidth() - to->strokeWidth()) > FLT_EPSILON) return false;
if (fabs(from->strokeWidth() - to->strokeWidth()) > FLOAT_EPSILON) return false;
//OPTIMIZE: Yet we can't merge outlining shapes unless we can support merging shapes feature.
if (from->strokeWidth() > 0 || to->strokeWidth() > 0) return false;
@ -115,7 +115,7 @@ static bool _merge(Shape* from, Shape* to)
if (from->strokeDash(nullptr) > 0 || to->strokeDash(nullptr) > 0) return false;
if (from->strokeFill() || to->strokeFill()) return false;
if (fabsf(from->strokeMiterlimit() - to->strokeMiterlimit()) > FLT_EPSILON) return false;
if (fabsf(from->strokeMiterlimit() - to->strokeMiterlimit()) > FLOAT_EPSILON) return false;
//fill rule
if (from->fillRule() != to->fillRule()) return false;
@ -484,7 +484,7 @@ TvgBinCounter TvgSaver::serializeStroke(const Shape* shape, const Matrix* pTrans
//miterlimit (the default value is 4)
auto miterlimit = shape->strokeMiterlimit();
if (fabsf(miterlimit - 4.0f) > FLT_EPSILON) {
if (fabsf(miterlimit - 4.0f) > FLOAT_EPSILON) {
cnt += writeTagProperty(TVG_TAG_SHAPE_STROKE_MITERLIMIT, SIZE(miterlimit), &miterlimit);
}
@ -788,7 +788,7 @@ bool TvgSaver::save(Paint* paint, Paint* bg, const string& path, TVG_UNUSED uint
if (x < 0) vsize[0] += x;
if (y < 0) vsize[1] += y;
if (vsize[0] < FLT_EPSILON || vsize[1] < FLT_EPSILON) {
if (vsize[0] < FLOAT_EPSILON || vsize[1] < FLOAT_EPSILON) {
TVGLOG("TVG_SAVER", "Saving paint(%p) has zero view size.", paint);
return false;
}