|
|
3c2463 |
/* === S Y N F I G ========================================================= */
|
|
|
248685 |
/*! \file synfig/rendering/primitive/transformation.cpp
|
|
|
3c2463 |
** \brief Transformation
|
|
|
3c2463 |
**
|
|
|
3c2463 |
** $Id$
|
|
|
3c2463 |
**
|
|
|
3c2463 |
** \legal
|
|
|
da4398 |
** ......... ... 2015-2018 Ivan Mahonin
|
|
|
3c2463 |
**
|
|
|
3c2463 |
** This package is free software; you can redistribute it and/or
|
|
|
3c2463 |
** modify it under the terms of the GNU General Public License as
|
|
|
3c2463 |
** published by the Free Software Foundation; either version 2 of
|
|
|
3c2463 |
** the License, or (at your option) any later version.
|
|
|
3c2463 |
**
|
|
|
3c2463 |
** This package is distributed in the hope that it will be useful,
|
|
|
3c2463 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
3c2463 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
3c2463 |
** General Public License for more details.
|
|
|
3c2463 |
** \endlegal
|
|
|
3c2463 |
*/
|
|
|
3c2463 |
/* ========================================================================= */
|
|
|
3c2463 |
|
|
|
3c2463 |
/* === H E A D E R S ======================================================= */
|
|
|
3c2463 |
|
|
|
3c2463 |
#ifdef USING_PCH
|
|
|
3c2463 |
# include "pch.h"
|
|
|
3c2463 |
#else
|
|
|
3c2463 |
#ifdef HAVE_CONFIG_H
|
|
|
3c2463 |
# include <config.h></config.h>
|
|
|
3c2463 |
#endif
|
|
|
3c2463 |
|
|
|
3c2463 |
#include "transformation.h"
|
|
|
3c2463 |
|
|
|
3c2463 |
#endif
|
|
|
3c2463 |
|
|
|
3c2463 |
using namespace synfig;
|
|
|
248685 |
using namespace rendering;
|
|
|
3c2463 |
|
|
|
3c2463 |
/* === M A C R O S ========================================================= */
|
|
|
3c2463 |
|
|
|
3c2463 |
/* === G L O B A L S ======================================================= */
|
|
|
3c2463 |
|
|
|
3c2463 |
/* === P R O C E D U R E S ================================================= */
|
|
|
3c2463 |
|
|
|
3c2463 |
/* === M E T H O D S ======================================================= */
|
|
|
3c2463 |
|
|
|
f44ff0 |
|
|
|
f44ff0 |
Transformation::DiscreteBounds
|
|
|
f44ff0 |
Transformation::make_discrete_bounds(const Bounds &bounds)
|
|
|
f44ff0 |
{
|
|
|
f44ff0 |
const int border_width = 4;
|
|
|
f44ff0 |
const int max_width = 16384 - 2*border_width;
|
|
|
f44ff0 |
const int max_height = 16384 - 2*border_width;
|
|
|
f44ff0 |
|
|
|
f44ff0 |
const int max_area_width = 4096 - 2*border_width;
|
|
|
f44ff0 |
const int max_area = max_area_width * max_area_width;
|
|
|
f44ff0 |
|
|
|
f44ff0 |
if (!bounds.is_valid())
|
|
|
f44ff0 |
return DiscreteBounds();
|
|
|
f44ff0 |
|
|
|
f44ff0 |
const Vector raster_size_orig = bounds.rect.get_size().multiply_coords( bounds.resolution );
|
|
|
f44ff0 |
|
|
|
f44ff0 |
Vector raster_size_float = raster_size_orig;
|
|
|
f44ff0 |
if (raster_size_float[0] > max_width)
|
|
|
f44ff0 |
raster_size_float[0] = max_width;
|
|
|
f44ff0 |
if (raster_size_float[1] > max_height)
|
|
|
f44ff0 |
raster_size_float[1] = max_height;
|
|
|
f44ff0 |
|
|
|
f44ff0 |
VectorInt raster_size(
|
|
|
f44ff0 |
std::max(1, (int)approximate_ceil(raster_size_float[0])),
|
|
|
f44ff0 |
std::max(1, (int)approximate_ceil(raster_size_float[1])) );
|
|
|
f44ff0 |
if (raster_size[0] * raster_size[1] > max_area) {
|
|
|
f44ff0 |
const Real k = sqrt(Real(max_area)/(raster_size[0] * raster_size[1]));
|
|
|
f44ff0 |
raster_size[0] = std::max(1, (int)approximate_floor(raster_size[0]*k));
|
|
|
f44ff0 |
raster_size[1] = std::max(1, (int)approximate_floor(raster_size[1]*k));
|
|
|
f44ff0 |
}
|
|
|
f44ff0 |
|
|
|
f44ff0 |
const Vector border(
|
|
|
f44ff0 |
border_width*raster_size_orig[0]/(bounds.resolution[0]*raster_size[0]),
|
|
|
f44ff0 |
border_width*raster_size_orig[1]/(bounds.resolution[1]*raster_size[1]) );
|
|
|
f44ff0 |
|
|
|
f44ff0 |
return DiscreteBounds(
|
|
|
f44ff0 |
Rect(
|
|
|
f44ff0 |
bounds.rect.get_min() - border,
|
|
|
f44ff0 |
bounds.rect.get_max() + border ),
|
|
|
f44ff0 |
raster_size + VectorInt(border_width, border_width) );
|
|
|
f44ff0 |
}
|
|
|
f44ff0 |
|
|
|
f44ff0 |
Matrix2
|
|
|
f44ff0 |
Transformation::derivative_vfunc(const Point &x) const
|
|
|
f44ff0 |
{
|
|
|
f44ff0 |
const Real step = real_low_precision<real>();</real>
|
|
|
f44ff0 |
const Real step_div = 1/step;
|
|
|
f44ff0 |
const Point p0 = transform(x);
|
|
|
f44ff0 |
const Point dx = transform(Point(x[0] + step, x[1])) - p0;
|
|
|
f44ff0 |
const Point dy = transform(Point(x[0], x[1] + step)) - p0;
|
|
|
f44ff0 |
return Matrix2(dx[0]*step_div, dx[1]*step_div, dy[0]*step_div, dy[1]*step_div);
|
|
|
f44ff0 |
}
|
|
|
f44ff0 |
|
|
|
81df8d |
bool
|
|
|
81df8d |
Transformation::can_merge_outer_vfunc(const Transformation&) const
|
|
|
81df8d |
{ return false; }
|
|
|
81df8d |
|
|
|
81df8d |
bool
|
|
|
81df8d |
Transformation::can_merge_inner_vfunc(const Transformation&) const
|
|
|
81df8d |
{ return false; }
|
|
|
81df8d |
|
|
|
81df8d |
void
|
|
|
81df8d |
Transformation::merge_outer_vfunc(const Transformation&)
|
|
|
81df8d |
{ }
|
|
|
81df8d |
|
|
|
81df8d |
void
|
|
|
81df8d |
Transformation::merge_inner_vfunc(const Transformation&)
|
|
|
81df8d |
{ }
|
|
|
81df8d |
|
|
|
6b62c7 |
Mesh::Handle
|
|
|
6b62c7 |
Transformation::build_mesh_vfunc(const Rect &target_rect, const Vector &precision) const
|
|
|
6b62c7 |
{
|
|
|
6b62c7 |
typedef std::pair<int, mesh::vertex=""> GridPoint;</int,>
|
|
|
5e2754 |
Transformation::Handle back_transform = create_inverted();
|
|
|
5e2754 |
if (!back_transform)
|
|
|
5e2754 |
return Mesh::Handle();
|
|
|
6b62c7 |
|
|
|
6b62c7 |
const Vector grid_p0 = target_rect.get_min();
|
|
|
6b62c7 |
const Vector grid_p1 = target_rect.get_max();
|
|
|
6b62c7 |
const Vector grid_size = grid_p1 - grid_p0;
|
|
|
6b62c7 |
const int grid_side_count_x = std::max(1, (int)round(grid_size[0]/precision[0])) + 1;
|
|
|
6b62c7 |
const int grid_side_count_y = std::max(1, (int)round(grid_size[1]/precision[1])) + 1;
|
|
|
6b62c7 |
|
|
|
6b62c7 |
const Vector grid_step(
|
|
|
6b62c7 |
grid_size[0]/(Real)(grid_side_count_x - 1),
|
|
|
6b62c7 |
grid_size[1]/(Real)(grid_side_count_y - 1) );
|
|
|
cda1e7 |
//const Real grid_step_diagonal = grid_step.mag();
|
|
|
6b62c7 |
|
|
|
6b62c7 |
// build grid
|
|
|
6b62c7 |
int visible_vertex_count = 0;
|
|
|
6b62c7 |
std::vector<gridpoint> grid;</gridpoint>
|
|
|
6b62c7 |
grid.reserve(grid_side_count_x * grid_side_count_y);
|
|
|
6b62c7 |
for(int j = 0; j < grid_side_count_y; ++j)
|
|
|
6b62c7 |
for(int i = 0; i < grid_side_count_x; ++i)
|
|
|
6b62c7 |
{
|
|
|
6b62c7 |
Vector p( grid_p0[0] + i*grid_step[0],
|
|
|
6b62c7 |
grid_p0[1] + j*grid_step[1] );
|
|
|
5e2754 |
Point tp = back_transform->transform(p);
|
|
|
da4398 |
if (tp.is_valid()) {
|
|
|
da4398 |
grid.push_back(GridPoint(visible_vertex_count, Mesh::Vertex(p, tp)));
|
|
|
6b62c7 |
++visible_vertex_count;
|
|
|
6b62c7 |
} else {
|
|
|
6b62c7 |
grid.push_back(GridPoint(-1, Mesh::Vertex()));
|
|
|
6b62c7 |
}
|
|
|
6b62c7 |
}
|
|
|
6b62c7 |
|
|
|
6b62c7 |
if (visible_vertex_count == 0)
|
|
|
6b62c7 |
return Mesh::Handle();
|
|
|
6b62c7 |
|
|
|
6b62c7 |
// copy vertices to mesh
|
|
|
6b62c7 |
Mesh::Handle mesh(new Mesh());
|
|
|
6b62c7 |
mesh->vertices.reserve(visible_vertex_count);
|
|
|
6b62c7 |
for(std::vector<gridpoint>::const_iterator i = grid.begin(); i != grid.end(); ++i)</gridpoint>
|
|
|
6b62c7 |
if (i->first >= 0) mesh->vertices.push_back(i->second);
|
|
|
6b62c7 |
|
|
|
6b62c7 |
// build triangles
|
|
|
6b62c7 |
mesh->triangles.reserve(visible_vertex_count * 2);
|
|
|
6b62c7 |
for(int j = 0; j < grid_side_count_y; ++j)
|
|
|
6b62c7 |
{
|
|
|
6b62c7 |
for(int i = 0; i < grid_side_count_x; ++i)
|
|
|
6b62c7 |
{
|
|
|
6b62c7 |
int tl = grid[(j-1)*grid_side_count_x + i-1].first;
|
|
|
6b62c7 |
int tr = grid[(j-1)*grid_side_count_x + i ].first;
|
|
|
6b62c7 |
int bl = grid[ j*grid_side_count_x + i-1].first;
|
|
|
6b62c7 |
int br = grid[ j*grid_side_count_x + i ].first;
|
|
|
6b62c7 |
int mode = (tl >= 0 ? 1 : 0)
|
|
|
6b62c7 |
| (tr >= 0 ? 2 : 0)
|
|
|
6b62c7 |
| (bl >= 0 ? 4 : 0)
|
|
|
6b62c7 |
| (br >= 0 ? 8 : 0);
|
|
|
6b62c7 |
switch(mode)
|
|
|
6b62c7 |
{
|
|
|
6b62c7 |
case 1|2|4|8:
|
|
|
6b62c7 |
mesh->triangles.push_back(Mesh::Triangle(tl, tr, bl));
|
|
|
6b62c7 |
mesh->triangles.push_back(Mesh::Triangle(bl, tr, br));
|
|
|
6b62c7 |
break;
|
|
|
6b62c7 |
case 2|4|8:
|
|
|
6b62c7 |
mesh->triangles.push_back(Mesh::Triangle(bl, tr, br));
|
|
|
6b62c7 |
break;
|
|
|
6b62c7 |
case 1|4|8:
|
|
|
6b62c7 |
mesh->triangles.push_back(Mesh::Triangle(tl, br, bl));
|
|
|
6b62c7 |
break;
|
|
|
6b62c7 |
case 1|2|8:
|
|
|
6b62c7 |
mesh->triangles.push_back(Mesh::Triangle(tl, tr, br));
|
|
|
6b62c7 |
break;
|
|
|
6b62c7 |
case 1|2|4:
|
|
|
6b62c7 |
mesh->triangles.push_back(Mesh::Triangle(tl, tr, bl));
|
|
|
6b62c7 |
break;
|
|
|
6b62c7 |
default:
|
|
|
6b62c7 |
break;
|
|
|
6b62c7 |
}
|
|
|
6b62c7 |
}
|
|
|
6b62c7 |
}
|
|
|
6b62c7 |
|
|
|
6b62c7 |
return mesh;
|
|
|
6b62c7 |
}
|
|
|
6b62c7 |
|
|
|
81df8d |
Transformation*
|
|
|
81df8d |
Transformation::create_merged(const Transformation& other) const
|
|
|
81df8d |
{
|
|
|
81df8d |
if (can_merge_inner(other)) {
|
|
|
81df8d |
Transformation *t = clone();
|
|
|
81df8d |
if (!t) return 0;
|
|
|
81df8d |
if (!t->can_merge_inner(other)) return 0;
|
|
|
81df8d |
t->merge_inner(other);
|
|
|
81df8d |
return t;
|
|
|
81df8d |
} else
|
|
|
81df8d |
if (other.can_merge_outer(*this)) {
|
|
|
81df8d |
Transformation *t = other.clone();
|
|
|
81df8d |
if (!t) return 0;
|
|
|
81df8d |
if (!t->can_merge_outer(*this)) return 0;
|
|
|
81df8d |
t->merge_outer(*this);
|
|
|
81df8d |
return t;
|
|
|
81df8d |
}
|
|
|
81df8d |
return 0;
|
|
|
81df8d |
}
|
|
|
81df8d |
|
|
|
81df8d |
bool
|
|
|
81df8d |
Transformation::can_merge_outer(const Transformation& other) const {
|
|
|
81df8d |
return (bool)dynamic_cast<const transformationnone*="">(&other)</const>
|
|
|
81df8d |
|| can_merge_outer_vfunc(other);
|
|
|
81df8d |
}
|
|
|
81df8d |
|
|
|
81df8d |
bool
|
|
|
81df8d |
Transformation::can_merge_inner(const Transformation& other) const {
|
|
|
81df8d |
return (bool)dynamic_cast<const transformationnone*="">(&other)</const>
|
|
|
81df8d |
|| can_merge_inner_vfunc(other);
|
|
|
81df8d |
}
|
|
|
81df8d |
|
|
|
81df8d |
bool
|
|
|
81df8d |
Transformation::merge_outer(const Transformation& other) {
|
|
|
81df8d |
if (!can_merge_outer(other)) return false;
|
|
|
81df8d |
merge_outer_vfunc(other);
|
|
|
81df8d |
return true;
|
|
|
81df8d |
}
|
|
|
81df8d |
|
|
|
81df8d |
bool
|
|
|
81df8d |
Transformation::merge_inner(const Transformation& other) {
|
|
|
81df8d |
if (!can_merge_inner(other)) return false;
|
|
|
81df8d |
merge_inner_vfunc(other);
|
|
|
81df8d |
return true;
|
|
|
81df8d |
}
|
|
|
81df8d |
|
|
|
6b62c7 |
Mesh::Handle
|
|
|
6b62c7 |
Transformation::build_mesh(const Rect &target_rect, const Vector &precision) const
|
|
|
6b62c7 |
{
|
|
|
da4398 |
const Real epsilon = real_low_precision<real>();</real>
|
|
|
6b62c7 |
|
|
|
da4398 |
if (!target_rect.is_valid())
|
|
|
6b62c7 |
return Mesh::Handle();
|
|
|
6b62c7 |
|
|
|
6b62c7 |
Vector valid_precision(fabs(precision[0]), fabs(precision[1]));
|
|
[d.j.a.y] Jerome Blanchi |
c14c8d |
if (std::isnan(valid_precision[0]) || std::isinf(valid_precision[0]))
|
|
|
da4398 |
valid_precision[0] = target_rect.get_width();
|
|
[d.j.a.y] Jerome Blanchi |
c14c8d |
if (std::isnan(valid_precision[1]) || std::isinf(valid_precision[1]))
|
|
|
da4398 |
valid_precision[1] = target_rect.get_height();
|
|
|
6b62c7 |
if (valid_precision[0] < epsilon)
|
|
|
6b62c7 |
valid_precision[0] = epsilon;
|
|
|
6b62c7 |
if (valid_precision[1] < epsilon)
|
|
|
6b62c7 |
valid_precision[1] = epsilon;
|
|
|
6b62c7 |
|
|
|
da4398 |
return build_mesh_vfunc(target_rect, valid_precision);
|
|
|
6b62c7 |
}
|
|
|
3c2463 |
|
|
|
3c2463 |
/* === E N T R Y P O I N T ================================================= */
|