Skip to content

Commit

Permalink
refine matrix code to core
Browse files Browse the repository at this point in the history
  • Loading branch information
onecoolx committed Sep 18, 2023
1 parent 3fa741e commit 4b39c18
Show file tree
Hide file tree
Showing 34 changed files with 481 additions and 751 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
tags
proj
vcproj
5 changes: 2 additions & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ libpicasso_la_SOURCES= \
include/platform.h \
include/vertex.h \
include/vertex_dist.h \
include/matrix.h \
\
core/device.cpp \
core/curve.cpp \
core/clipper.cpp \
core/fixedopt.cpp \
core/graphic_path.cpp \
core/matrix.cpp \
\
simd/fastcopy_sse.h \
\
Expand Down Expand Up @@ -70,7 +72,6 @@ libpicasso_la_SOURCES= \
gfx/gfx_scanline_storage.h \
gfx/gfx_span_generator.h \
gfx/gfx_span_image_filters.h \
gfx/gfx_trans_affine.h \
gfx/gfx_device.cpp \
gfx/gfx_raster_adapter.cpp \
gfx/gfx_gradient_adapter.cpp \
Expand All @@ -87,13 +88,11 @@ libpicasso_la_SOURCES= \
picasso_global.h \
picasso_gradient.h \
picasso_mask.h \
picasso_matrix.h \
picasso_objects.h \
picasso_painter.h \
picasso_private.h \
picasso_raster_adapter.h \
picasso_rendering_buffer.h \
picasso_matrix.cpp \
picasso_matrix_api.cpp \
picasso_painter.cpp \
picasso_rendering_buffer.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/core/graphic_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include "common.h"
#include "math_type.h"
#include "geometry.h"
#include "matrix.h"
#include "graphic_base.h"
#include "graphic_path.h"

#include "picasso_matrix.h"

namespace picasso {

Expand Down
213 changes: 213 additions & 0 deletions src/core/matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/* Picasso - a vector graphics library
*
* Copyright (C) 2008 Zhang Ji Peng
* Contact: [email protected]
*/

#include "common.h"
#include "matrix.h"

namespace picasso {

const scalar affine_epsilon = FLT_TO_SCALAR(1e-14f);

trans_affine::trans_affine()
: m_sx(FLT_TO_SCALAR(1.0f)), m_shy(FLT_TO_SCALAR(0.0f))
, m_shx(FLT_TO_SCALAR(0.0f)), m_sy(FLT_TO_SCALAR(1.0f))
, m_tx(FLT_TO_SCALAR(0.0f)), m_ty(FLT_TO_SCALAR(0.0f))
{
}

trans_affine::trans_affine(scalar sx, scalar shy, scalar shx, scalar sy, scalar tx, scalar ty)
: m_sx(sx), m_shy(shy), m_shx(shx), m_sy(sy), m_tx(tx), m_ty(ty)
{
}

trans_affine::trans_affine(const trans_affine& o)
{
m_sx = o.m_sx;
m_shy = o.m_shy;
m_shx = o.m_shx;
m_sy = o.m_sy;
m_tx = o.m_tx;
m_ty = o.m_ty;
}

trans_affine& trans_affine::operator=(const trans_affine& o)
{
if (this == &o)
return *this;

m_sx = o.m_sx;
m_shy = o.m_shy;
m_shx = o.m_shx;
m_sy = o.m_sy;
m_tx = o.m_tx;
m_ty = o.m_ty;

return *this;
}

void trans_affine::transform(scalar* x, scalar* y) const
{
_REGISTER_ scalar tmp = *x;
*x = tmp * m_sx + *y * m_shx + m_tx;
*y = tmp * m_shy + *y * m_sy + m_ty;
}

void trans_affine::transform_2x2(scalar* x, scalar* y) const
{
_REGISTER_ scalar tmp = *x;
*x = tmp * m_sx + *y * m_shx;
*y = tmp * m_shy + *y * m_sy;
}

void trans_affine::inverse_transform(scalar* x, scalar* y) const
{
_REGISTER_ scalar d = determinant_reciprocal();
_REGISTER_ scalar a = (*x - m_tx) * d;
_REGISTER_ scalar b = (*y - m_ty) * d;
*x = a * m_sy - b * m_shx;
*y = b * m_sx - a * m_shy;
}

const trans_affine& trans_affine::translate(scalar x, scalar y)
{
m_tx += x;
m_ty += y;
return *this;
}

const trans_affine& trans_affine::scale(scalar x, scalar y)
{
scalar m0 = x;
scalar m1 = y;
m_sx *= m0;
m_shx *= m0;
m_tx *= m0;
m_shy *= m1;
m_sy *= m1;
m_ty *= m1;
return *this;
}

const trans_affine& trans_affine::rotate(scalar a)
{
scalar ca = Cos(a);
scalar sa = Sin(a);
scalar t0 = m_sx * ca - m_shy * sa;
scalar t2 = m_shx * ca - m_sy * sa;
scalar t4 = m_tx * ca - m_ty * sa;
m_shy = m_sx * sa + m_shy * ca;
m_sy = m_shx * sa + m_sy * ca;
m_ty = m_tx * sa + m_ty * ca;
m_sx = t0;
m_shx = t2;
m_tx = t4;
return *this;
}

const trans_affine& trans_affine::shear(scalar x, scalar y)
{
scalar t0 = y * m_shx;
scalar t1 = y * m_sy;
scalar t2 = x * m_sx;
scalar t3 = x * m_shy;
m_sx += t0;
m_shy += t1;
m_shx += t2;
m_sy += t3;
return *this;
}

const trans_affine& trans_affine::invert(void)
{
scalar d = determinant_reciprocal();

scalar t0 = m_sy * d;
m_sy = m_sx * d;
m_shy = -m_shy * d;
m_shx = -m_shx * d;

scalar t4 = -m_tx * t0 - m_ty * m_shx;
m_ty = -m_tx * m_shy - m_ty * m_sy;

m_sx = t0;
m_tx = t4;
return *this;
}

const trans_affine& trans_affine::flip_x(void)
{
m_sx = -m_sx;
m_shy = -m_shy;
m_tx = -m_tx;
return *this;
}

const trans_affine& trans_affine::flip_y(void)
{
m_shx = -m_shx;
m_sy = -m_sy;
m_ty = -m_ty;
return *this;
}

const trans_affine& trans_affine::reset(void)
{
m_sx = m_sy = FLT_TO_SCALAR(1.0f);
m_shy = m_shx = m_tx = m_ty = FLT_TO_SCALAR(0.0f);
return *this;
}

const trans_affine& trans_affine::multiply(const trans_affine& o)
{
scalar t0 = m_sx * o.m_sx + m_shy * o.m_shx;
scalar t2 = m_shx * o.m_sx + m_sy * o.m_shx;
scalar t4 = m_tx * o.m_sx + m_ty * o.m_shx + o.m_tx;
m_shy = m_sx * o.m_shy + m_shy * o.m_sy;
m_sy = m_shx * o.m_shy + m_sy * o.m_sy;
m_ty = m_tx * o.m_shy + m_ty * o.m_sy + o.m_ty;
m_sx = t0;
m_shx = t2;
m_tx = t4;
return *this;
}

bool trans_affine::is_identity(void) const
{
return is_equal_eps(m_sx, FLT_TO_SCALAR(1.0f), affine_epsilon) &&
is_equal_eps(m_shy, FLT_TO_SCALAR(0.0f), affine_epsilon) &&
is_equal_eps(m_shx, FLT_TO_SCALAR(0.0f), affine_epsilon) &&
is_equal_eps(m_sy, FLT_TO_SCALAR(1.0f), affine_epsilon) &&
is_equal_eps(m_tx, FLT_TO_SCALAR(0.0f), affine_epsilon) &&
is_equal_eps(m_ty, FLT_TO_SCALAR(0.0f), affine_epsilon);
}

scalar trans_affine::determinant(void) const
{
return m_sx * m_sy - m_shy * m_shx;
}

scalar trans_affine::rotation(void) const
{
scalar x1 = FLT_TO_SCALAR(0.0f);
scalar y1 = FLT_TO_SCALAR(0.0f);
scalar x2 = FLT_TO_SCALAR(1.0f);
scalar y2 = FLT_TO_SCALAR(0.0f);
transform(&x1, &y1);
transform(&x2, &y2);
return Atan2(y2-y1, x2-x1);
}

bool trans_affine::is_equal(const trans_affine& o) const
{
return is_equal_eps(m_sx, o.sx(), affine_epsilon) &&
is_equal_eps(m_shy, o.shy(), affine_epsilon) &&
is_equal_eps(m_shx, o.shx(), affine_epsilon) &&
is_equal_eps(m_sy, o.sy(), affine_epsilon) &&
is_equal_eps(m_tx, o.tx(), affine_epsilon) &&
is_equal_eps(m_ty, o.ty(), affine_epsilon);
}

}
14 changes: 1 addition & 13 deletions src/gfx/gfx_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "gfx_gradient_adapter.h"
#include "gfx_font_adapter.h"
#include "gfx_mask_layer.h"
#include "gfx_trans_affine.h"
#include "gfx_pixfmt_rgba.h"
#include "gfx_pixfmt_rgb.h"
#include "gfx_pixfmt_rgb16.h"
Expand All @@ -34,17 +33,6 @@ gfx_device::~gfx_device()
{
}

abstract_trans_affine* gfx_device::create_trans_affine(scalar sx, scalar shy,
scalar shx, scalar sy, scalar tx, scalar ty)
{
return new gfx_trans_affine(sx, shy, shx, sy, tx, ty);
}

void gfx_device::destroy_trans_affine(abstract_trans_affine* m)
{
delete m;
}

abstract_painter* gfx_device::create_painter(pix_fmt fmt)
{
switch (fmt)
Expand Down Expand Up @@ -134,7 +122,7 @@ void gfx_device::destroy_gradient_adapter(abstract_gradient_adapter* g)
}

abstract_font_adapter* gfx_device::create_font_adapter(const char* name, int charset, scalar height, scalar weight,
bool italic, bool hint, bool flip, bool antialias, const abstract_trans_affine* mtx)
bool italic, bool hint, bool flip, bool antialias, const trans_affine* mtx)
{
return new gfx_font_adapter(name, charset, height, weight, italic, hint, flip, antialias, mtx);
}
Expand Down
6 changes: 1 addition & 5 deletions src/gfx/gfx_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ class gfx_device : public device
static gfx_device* create(void);
virtual ~gfx_device();

virtual abstract_trans_affine* create_trans_affine(scalar sx, scalar shy,
scalar shx, scalar sy, scalar tx, scalar ty);
virtual void destroy_trans_affine(abstract_trans_affine* m);

virtual abstract_painter* create_painter(pix_fmt fmt);
virtual void destroy_painter(abstract_painter* p);

Expand All @@ -43,7 +39,7 @@ class gfx_device : public device

virtual abstract_font_adapter* create_font_adapter(const char* name, int charset,
scalar height, scalar weight, bool italic, bool hint, bool flip,
bool antialias, const abstract_trans_affine* mtx);
bool antialias, const trans_affine* mtx);
virtual void destroy_font_adapter(abstract_font_adapter* f);
protected:
gfx_device();
Expand Down
2 changes: 1 addition & 1 deletion src/gfx/gfx_font_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class gfx_font_adapter : public abstract_font_adapter
{
public:
gfx_font_adapter(const char* name, int charset, scalar height, scalar weight,
bool italic, bool hint, bool flip, bool antialias, const abstract_trans_affine* mtx);
bool italic, bool hint, bool flip, bool antialias, const trans_affine* mtx);
virtual ~gfx_font_adapter();

virtual scalar height(void) const;
Expand Down
4 changes: 2 additions & 2 deletions src/gfx/gfx_font_adapter_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#include <stdio.h>
#include "common.h"
#include "convert.h"
#include "matrix.h"
#include "gfx_font_adapter.h"
#include "gfx_rasterizer_scanline.h"
#include "gfx_scanline.h"
#include "gfx_scanline_renderer.h"
#include "gfx_scanline_storage.h"
#include "gfx_trans_affine.h"

#import <CoreText/CTFont.h>
#import <CoreGraphics/CGPath.h>
Expand Down Expand Up @@ -69,7 +69,7 @@
};

gfx_font_adapter::gfx_font_adapter(const char* name, int charset, scalar size, scalar weight,
bool italic, bool hint, bool flip, bool a, const abstract_trans_affine* mtx)
bool italic, bool hint, bool flip, bool a, const trans_affine* mtx)
:m_impl(new font_adapter_impl)
{
CFStringRef fName = CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8);
Expand Down
3 changes: 1 addition & 2 deletions src/gfx/gfx_font_adapter_dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
#include "common.h"
#include "convert.h"
#include "gfx_font_adapter.h"
#include "gfx_trans_affine.h"

#if !defined(WIN32) && !ENABLE(FREE_TYPE2) && !defined(__APPLE__)

// dummy font interface
namespace gfx {

gfx_font_adapter::gfx_font_adapter(const char* name, int charset, scalar size, scalar weight,
bool italic, bool hint, bool flip, bool a, const abstract_trans_affine* mtx) { }
bool italic, bool hint, bool flip, bool a, const trans_affine* mtx) { }
gfx_font_adapter::~gfx_font_adapter() { }
void gfx_font_adapter::active(void) { }
void gfx_font_adapter::deactive(void) { }
Expand Down
Loading

0 comments on commit 4b39c18

Please sign in to comment.