mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
LibGfx: Add a basic AffineTransform class
We can now perform some basic 2D transforms through an affine matrix. This patch adds translate() and scale() :^)
This commit is contained in:
parent
3bbc2c7300
commit
6f2c63000d
3 changed files with 223 additions and 0 deletions
148
Libraries/LibGfx/AffineTransform.cpp
Normal file
148
Libraries/LibGfx/AffineTransform.cpp
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <AK/LogStream.h>
|
||||||
|
#include <AK/Optional.h>
|
||||||
|
#include <LibGfx/AffineTransform.h>
|
||||||
|
#include <LibGfx/FloatRect.h>
|
||||||
|
#include <LibGfx/Rect.h>
|
||||||
|
|
||||||
|
namespace Gfx {
|
||||||
|
|
||||||
|
bool AffineTransform::is_identity() const
|
||||||
|
{
|
||||||
|
return m_values[0] == 1 && m_values[1] == 0 && m_values[2] == 0 && m_values[3] == 1 && m_values[4] == 0 && m_values[5] == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float hypotenuse(float x, float y)
|
||||||
|
{
|
||||||
|
// FIXME: This won't handle overflow :(
|
||||||
|
return sqrt(x * x + y * y);
|
||||||
|
}
|
||||||
|
|
||||||
|
float AffineTransform::x_scale() const
|
||||||
|
{
|
||||||
|
return hypotenuse(m_values[0], m_values[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
float AffineTransform::y_scale() const
|
||||||
|
{
|
||||||
|
return hypotenuse(m_values[2], m_values[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
AffineTransform& AffineTransform::scale(float sx, float sy)
|
||||||
|
{
|
||||||
|
m_values[0] *= sx;
|
||||||
|
m_values[1] *= sx;
|
||||||
|
m_values[2] *= sy;
|
||||||
|
m_values[3] *= sy;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
AffineTransform& AffineTransform::translate(float tx, float ty)
|
||||||
|
{
|
||||||
|
m_values[4] += tx * m_values[0] + ty * m_values[2];
|
||||||
|
m_values[5] += tx * m_values[1] + ty * m_values[3];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const
|
||||||
|
{
|
||||||
|
mapped_x = (m_values[0] * unmapped_x + m_values[2] * unmapped_y + m_values[4]);
|
||||||
|
mapped_y = (m_values[1] * unmapped_x + m_values[3] * unmapped_y + m_values[5]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point AffineTransform::map(const Point& point) const
|
||||||
|
{
|
||||||
|
float mapped_x;
|
||||||
|
float mapped_y;
|
||||||
|
map(point.x(), point.y(), mapped_x, mapped_y);
|
||||||
|
return Point(roundf(mapped_x), roundf(mapped_y));
|
||||||
|
}
|
||||||
|
|
||||||
|
FloatPoint AffineTransform::map(const FloatPoint& point) const
|
||||||
|
{
|
||||||
|
float mapped_x;
|
||||||
|
float mapped_y;
|
||||||
|
map(point.x(), point.y(), mapped_x, mapped_y);
|
||||||
|
return FloatPoint(mapped_x, mapped_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Size AffineTransform::map(const Size& size) const
|
||||||
|
{
|
||||||
|
return Size(roundf(size.width() * x_scale()), roundf(y_scale()));
|
||||||
|
}
|
||||||
|
|
||||||
|
FloatSize AffineTransform::map(const FloatSize& size) const
|
||||||
|
{
|
||||||
|
return { size.width() * x_scale(), size.height() * y_scale() };
|
||||||
|
}
|
||||||
|
|
||||||
|
Rect AffineTransform::map(const Rect& rect) const
|
||||||
|
{
|
||||||
|
return enclosing_int_rect(map(FloatRect(rect)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static T smallest_of(T p1, T p2, T p3, T p4)
|
||||||
|
{
|
||||||
|
return min(min(p1, p2), min(p3, p4));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static T largest_of(T p1, T p2, T p3, T p4)
|
||||||
|
{
|
||||||
|
return max(max(p1, p2), max(p3, p4));
|
||||||
|
}
|
||||||
|
|
||||||
|
FloatRect AffineTransform::map(const FloatRect& rect) const
|
||||||
|
{
|
||||||
|
FloatPoint p1 = map(rect.top_left());
|
||||||
|
FloatPoint p2 = map(rect.top_right().translated(1, 0));
|
||||||
|
FloatPoint p3 = map(rect.bottom_right().translated(1, 1));
|
||||||
|
FloatPoint p4 = map(rect.bottom_left().translated(0, 1));
|
||||||
|
float left = smallest_of(p1.x(), p2.x(), p3.x(), p4.x());
|
||||||
|
float top = smallest_of(p1.y(), p2.y(), p3.y(), p4.y());
|
||||||
|
float right = largest_of(p1.x(), p2.x(), p3.x(), p4.x());
|
||||||
|
float bottom = largest_of(p1.y(), p2.y(), p3.y(), p4.y());
|
||||||
|
return { left, top, right - left, bottom - top };
|
||||||
|
}
|
||||||
|
|
||||||
|
const LogStream& operator<<(const LogStream& stream, const AffineTransform& value)
|
||||||
|
{
|
||||||
|
if (value.is_identity())
|
||||||
|
return stream << "{ Identity }";
|
||||||
|
|
||||||
|
return stream << "{ "
|
||||||
|
<< value.a() << ", "
|
||||||
|
<< value.b() << ", "
|
||||||
|
<< value.c() << ", "
|
||||||
|
<< value.d() << ", "
|
||||||
|
<< value.e() << ", "
|
||||||
|
<< value.f() << " }";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
74
Libraries/LibGfx/AffineTransform.h
Normal file
74
Libraries/LibGfx/AffineTransform.h
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Forward.h>
|
||||||
|
#include <AK/LogStream.h>
|
||||||
|
#include <LibGfx/Forward.h>
|
||||||
|
|
||||||
|
namespace Gfx {
|
||||||
|
|
||||||
|
class AffineTransform {
|
||||||
|
public:
|
||||||
|
AffineTransform()
|
||||||
|
: m_values { 1, 0, 0, 1, 0, 0 }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_identity() const;
|
||||||
|
|
||||||
|
void map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const;
|
||||||
|
|
||||||
|
Point map(const Point&) const;
|
||||||
|
FloatPoint map(const FloatPoint&) const;
|
||||||
|
|
||||||
|
Size map(const Size&) const;
|
||||||
|
FloatSize map(const FloatSize&) const;
|
||||||
|
|
||||||
|
Rect map(const Rect&) const;
|
||||||
|
FloatRect map(const FloatRect&) const;
|
||||||
|
|
||||||
|
float a() const { return m_values[0]; }
|
||||||
|
float b() const { return m_values[1]; }
|
||||||
|
float c() const { return m_values[2]; }
|
||||||
|
float d() const { return m_values[3]; }
|
||||||
|
float e() const { return m_values[4]; }
|
||||||
|
float f() const { return m_values[5]; }
|
||||||
|
|
||||||
|
float x_scale() const;
|
||||||
|
float y_scale() const;
|
||||||
|
|
||||||
|
AffineTransform& scale(float sx, float sy);
|
||||||
|
AffineTransform& translate(float tx, float ty);
|
||||||
|
|
||||||
|
private:
|
||||||
|
float m_values[6] { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
const LogStream& operator<<(const LogStream&, const AffineTransform&);
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
OBJS = \
|
OBJS = \
|
||||||
|
AffineTransform.o \
|
||||||
Bitmap.o \
|
Bitmap.o \
|
||||||
CharacterBitmap.o \
|
CharacterBitmap.o \
|
||||||
Color.o \
|
Color.o \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue