Blame c++/perspective/src/perspective.cpp

4dc49c
4dc49c
#include "perspective.h"
4dc49c
4dc49c
4dc49c
namespace {
4dc49c
	const int border_width  = 2;
4dc49c
	const int max_width     = 16384 - 2*border_width;
4dc49c
	const int max_height    = 16384 - 2*border_width;
4dc49c
4dc49c
	const int max_area_width = 4096 - 2*border_width;
4dc49c
	const int max_area       = max_area_width * max_area_width;
4dc49c
4dc49c
	const Real max_scale_sqr = 4.0 * 4.0;
4dc49c
}
4dc49c
4dc49c
4dc49c
4dc49c
int
4dc49c
Perspective::truncate_line(
4dc49c
	Vector2 *out_points,
4dc49c
	const Pair2 &bounds,
4dc49c
	Real a,
4dc49c
	Real b,
4dc49c
	Real c )
4dc49c
{
4dc49c
	// equatation of line is a*x + b*y + c = 0
4dc49c
	
4dc49c
	int count = 0;
4dc49c
	
4dc49c
	if (fabs(a) > real_precision) {
4dc49c
		Real x0 = -(c + bounds.p0.y*b)/a;
4dc49c
		if ( x0 + real_precision >= bounds.p0.x
4dc49c
		  && x0 - real_precision <= bounds.p1.x )
4dc49c
		{
4dc49c
			if (out_points) out_points[count] = Vector2(x0, bounds.p0.y);
4dc49c
			if (++count >= 2) return count;
4dc49c
		}
4dc49c
		
4dc49c
		Real x1 = -(c + bounds.p1.y*b)/a;
4dc49c
		if ( x1 + real_precision >= bounds.p0.x
4dc49c
		  && x1 - real_precision <= bounds.p1.x )
4dc49c
		{
4dc49c
			if (out_points) out_points[count] = Vector2(x1, bounds.p1.y);
4dc49c
			if (++count >= 2) return count;
4dc49c
		}
4dc49c
	}
4dc49c
4dc49c
	if (fabs(b) > real_precision) {
4dc49c
		Real y0 = -(c + bounds.p0.x*a)/b;
4dc49c
		if ( y0 + real_precision >= bounds.p0.y
4dc49c
		  && y0 - real_precision <= bounds.p1.y )
4dc49c
		{
4dc49c
			if (out_points) out_points[count] = Vector2(bounds.p0.x, y0);
4dc49c
			if (++count >= 2) return count;
4dc49c
		}
4dc49c
		
4dc49c
		Real y1 = -(c + bounds.p1.x*a)/b;
4dc49c
		if ( y1 + real_precision >= bounds.p0.y
4dc49c
		  && y1 - real_precision <= bounds.p1.y )
4dc49c
		{
4dc49c
			if (out_points) out_points[count] = Vector2(bounds.p1.x, y1);
4dc49c
			if (++count >= 2) return count;
4dc49c
		}
4dc49c
	}
4dc49c
4dc49c
	return count;
4dc49c
}
4dc49c
4dc49c
4dc49c
Vector2
4dc49c
Perspective::calc_optimal_resolution(
4dc49c
	const Vector2 &ox,
4dc49c
	const Vector2 &oy )
4dc49c
{
4dc49c
	const Real a = ox.x * ox.x;
4dc49c
	const Real b = ox.y * ox.y;
4dc49c
	const Real c = oy.x * oy.x;
4dc49c
	const Real d = oy.y * oy.y;
4dc49c
	Real e = fabs(ox.x*oy.y - ox.y*oy.x);
4dc49c
	if (e < real_precision)
4dc49c
		return Vector2(); // paranoid check, e must be non-zero when matrix is invertible
4dc49c
	e = 1.0/e;
4dc49c
	
4dc49c
	const Real sum = a*d + b*c;
4dc49c
	Vector2 scale;
4dc49c
	if (2*a*b + real_precision >= sum) {
4dc49c
		scale.x = sqrt(2*b)*e;
4dc49c
		scale.y = sqrt(2*a)*e;
4dc49c
	} else
4dc49c
	if (2*c*d + real_precision >= sum) {
4dc49c
		scale.x = sqrt(2*d)*e;
4dc49c
		scale.y = sqrt(2*c)*e;
4dc49c
	} else {
4dc49c
		const Real dif = a*d - b*c;
4dc49c
		scale.x = sqrt(dif/(a - c))*e;
4dc49c
		scale.y = sqrt(dif/(d - b))*e;
4dc49c
	}
4dc49c
4dc49c
	return scale.x <= real_precision || scale.y <= real_precision
4dc49c
	     ? Vector2() : scale;
4dc49c
}
4dc49c
4dc49c
4dc49c
Vector3
4dc49c
Perspective::make_alpha_matrix_col(
4dc49c
	Real w0,
4dc49c
	Real w1,
4dc49c
	const Vector3 &w_col )
4dc49c
{
4dc49c
	Real k = w1 - w0;
4dc49c
	if (fabs(k) <= real_precision)
4dc49c
		return w_col;
4dc49c
4dc49c
	k = w1/k;
4dc49c
	return Vector3(
4dc49c
		k*w_col.x,
4dc49c
		k*w_col.y,
4dc49c
		k*(w_col.z - w0) );
4dc49c
}
4dc49c
4dc49c
4dc49c
Matrix3
4dc49c
Perspective::make_alpha_matrix(
4dc49c
	Real aw0, Real aw1,
4dc49c
	Real bw0, Real bw1,
4dc49c
	const Vector3 &w_col )
4dc49c
{
4dc49c
	const Vector3 a_col = make_alpha_matrix_col(aw0, aw1, w_col);
4dc49c
	const Vector3 b_col = make_alpha_matrix_col(bw0, bw1, w_col);
4dc49c
	return Matrix3(
4dc49c
		Vector3( a_col.x, b_col.x, w_col.x ),
4dc49c
		Vector3( a_col.y, b_col.y, w_col.y ),
4dc49c
		Vector3( a_col.z, b_col.z, w_col.z ) );
4dc49c
}
4dc49c
4dc49c
4dc49c
void
4dc49c
Perspective::calc_raster_size(
4dc49c
	Pair2 &out_bounds,
4dc49c
	IntVector2 &out_raster_size,
4dc49c
	Matrix3 &out_raster_matrix,
4dc49c
	const Vector2 &resolution,
4dc49c
	const Pair2 &bounds )
4dc49c
{
4dc49c
	const Vector2 offset = bounds.p0;
4dc49c
	const Vector2 raster_size_orig(
4dc49c
		(bounds.p1.x - bounds.p0.x)*resolution.x,
4dc49c
		(bounds.p1.y - bounds.p0.y)*resolution.y );
4dc49c
	
4dc49c
	Vector2 raster_size_float = raster_size_orig;
4dc49c
	if (raster_size_float.x > max_width)
4dc49c
		raster_size_float.x = max_width;
4dc49c
	if (raster_size_float.y > max_height)
4dc49c
		raster_size_float.y = max_height;
4dc49c
	IntVector2 raster_size(
4dc49c
		(int)ceil( raster_size_float.x - real_precision ),
4dc49c
		(int)ceil( raster_size_float.y - real_precision ) );
4dc49c
	if (raster_size.x * raster_size.y > max_area) {
4dc49c
		const Real k = sqrt(Real(max_area)/(raster_size.x * raster_size.y));
4dc49c
		raster_size.x = std::max(1, (int)floor(raster_size.x*k + real_precision));
4dc49c
		raster_size.y = std::max(1, (int)floor(raster_size.y*k + real_precision));
4dc49c
	}
4dc49c
	
4dc49c
	const Vector2 new_resolution(
4dc49c
		raster_size_orig.x > real_precision ? resolution.x * raster_size.x / raster_size_orig.x : resolution.x,
4dc49c
		raster_size_orig.y > real_precision ? resolution.y * raster_size.y / raster_size_orig.y : resolution.y );
4dc49c
	
4dc49c
	out_bounds = bounds.inflated( new_resolution * border_width );
4dc49c
	out_raster_size = raster_size + IntVector2(border_width, border_width)*2;
4dc49c
	out_raster_matrix = Matrix3::translation(Vector2(border_width, border_width))
4dc49c
					  * Matrix3::scaling(new_resolution)
4dc49c
					  * Matrix3::translation(-offset);
4dc49c
}
4dc49c
4dc49c
4dc49c
void
4dc49c
Perspective::create_layers(
4dc49c
	LayerList &out_layers,
4dc49c
	const Matrix3 &matrix,
4dc49c
	const IntPair2 &dst_bounds,
4dc49c
	const Real step )
4dc49c
{
4dc49c
	bool is_invertible = false;
4dc49c
	const Matrix3 back_matrix = matrix.inverted(&is_invertible);
4dc49c
	if (!is_invertible)
4dc49c
		return; // matrix is collapsed
4dc49c
4dc49c
	// calc src resolution
4dc49c
	const Vector2 resolution = calc_optimal_resolution(
4dc49c
		back_matrix.row_x().vec2(),
4dc49c
		back_matrix.row_y().vec2() );
4dc49c
	if (resolution.x <= real_precision || resolution.y <= real_precision)
4dc49c
		return; // 
4dc49c
4dc49c
	// corners
4dc49c
	Vector3 dst_corners[4] = {
4dc49c
		Vector3(dst_bounds.p0.x, dst_bounds.p0.y, 1),
4dc49c
		Vector3(dst_bounds.p1.x, dst_bounds.p0.y, 1),
4dc49c
		Vector3(dst_bounds.p1.x, dst_bounds.p1.y, 1),
4dc49c
		Vector3(dst_bounds.p0.x, dst_bounds.p1.y, 1) };
4dc49c
	
4dc49c
	// calc coefficient for equatation of "horizontal" line: A*x + B*y + C = 1/w (aka A*x + B*y = 1/w - C)
4dc49c
	//                     equatation of line of horizon is: A*x + B*y + C = 0   (aka A*x + B*y = -C)
4dc49c
	const Real A = back_matrix.m02;
4dc49c
	const Real B = back_matrix.m12;
4dc49c
	const Real C = back_matrix.m22;
4dc49c
	
4dc49c
	Real hd = sqrt(A*A + B*B);
4dc49c
	if (hd <= real_precision) {
4dc49c
		// orthogonal projection - no perspective - no subdiviosions
4dc49c
		
4dc49c
		if (fabs(C) < real_precision)
4dc49c
			return; // only when matrix was not invertible (additional check)
4dc49c
		
4dc49c
		// force w coord to one to avoid division to w
4dc49c
		Real k = 1/C;
4dc49c
		Matrix3 bm = back_matrix;
4dc49c
		for(int i = 0; i < 9; ++i) bm.a[i] *= k;
4dc49c
4dc49c
		// calc bounds
4dc49c
		Pair2 layer_src_bounds(Vector2(INFINITY, INFINITY), Vector2(-INFINITY, -INFINITY));
4dc49c
		for(int i = 0; i < 4; ++i)
4dc49c
			layer_src_bounds.expand( (bm*dst_corners[i]).vec2() );
4dc49c
		if (layer_src_bounds.empty())
4dc49c
			return;
4dc49c
		
4dc49c
		// make layer
4dc49c
		Layer layer;
4dc49c
		layer.dst_bounds = dst_bounds;
4dc49c
		calc_raster_size(
4dc49c
			layer.src_bounds,
4dc49c
			layer.src_size,
4dc49c
			layer.back_matrix,
4dc49c
			resolution * k,
4dc49c
			layer_src_bounds );
4dc49c
		layer.back_matrix *= bm;
4dc49c
		layer.back_alpha_matrix = Matrix3(
4dc49c
			Vector3(0, 0, 0),
4dc49c
			Vector3(0, 0, 0),
4dc49c
			Vector3(1, 1, 1) );
4dc49c
		out_layers.push_back(layer);
4dc49c
4dc49c
		return; 
4dc49c
	}
4dc49c
4dc49c
	// find visible w range
4dc49c
	hd = 1/hd;
4dc49c
	const Real horizonw1 = hd;
4dc49c
	const Real horizonw2 = hd/std::min(Real(2), step);
4dc49c
	const Real horizonw3 = hd/step;
4dc49c
	Real maxw = -INFINITY, minw = INFINITY;
4dc49c
	Vector3 src_corners[4];
4dc49c
	for(int i = 0; i < 4; ++i) {
4dc49c
		Vector3 src = back_matrix * dst_corners[i];
4dc49c
		if (fabs(src.z) > real_precision) {
4dc49c
			Real w = 1/src.z;
4dc49c
			if (w > 0 && w < horizonw1)
4dc49c
				src_corners[i] = Vector3(src.x*w, src.y*w, w);
4dc49c
			else
4dc49c
				w = horizonw1;
4dc49c
			if (minw > w) minw = w;
4dc49c
			if (maxw < w) maxw = w;
4dc49c
		}
4dc49c
	}
4dc49c
	if (minw >= maxw - real_precision)
4dc49c
		return; // all bounds too thin
4dc49c
	const Real maxw3 = std::min(maxw, horizonw3);
4dc49c
4dc49c
	// steps
4dc49c
	const Real stepLog = log(step);
4dc49c
	int minlog = (int)floor(log(minw)/stepLog + real_precision);
4dc49c
	int maxlog = (int)ceil(log(maxw3)/stepLog - real_precision);
4dc49c
	if (maxlog < minlog) maxlog = minlog;
4dc49c
	
4dc49c
	Real w = pow(step, Real(minlog));
4dc49c
	for(int i = minlog; i <= maxlog; ++i, w *= step) {
4dc49c
		// w range
4dc49c
		const Real w0  = w/step;
4dc49c
		const Real w1  = std::min(w*step, horizonw1);
4dc49c
		
4dc49c
		// alpha ranges
4dc49c
		const Real aw0 = w0;
4dc49c
		const Real aw1 = w;
4dc49c
		const Real bw0 = i == maxlog ? horizonw1 : w1;
4dc49c
		const Real bw1 = i == maxlog ? horizonw2 : w;
4dc49c
		
4dc49c
		// calc bounds
4dc49c
		Pair2 layer_src_bounds(Vector2(INFINITY, INFINITY), Vector2(-INFINITY, -INFINITY));
4dc49c
		Pair2 layer_dst_bounds(Vector2(INFINITY, INFINITY), Vector2(-INFINITY, -INFINITY));
4dc49c
		for(int j = 0; j < 4; ++j) {
4dc49c
			if (src_corners[j].z && src_corners[j].z > w0 && src_corners[j].z < w1) {
4dc49c
				layer_src_bounds.expand(src_corners[j].vec2());
4dc49c
				layer_dst_bounds.expand(dst_corners[j].vec2());
4dc49c
			}
4dc49c
		}
4dc49c
		Vector2 line[2];
4dc49c
		int line_count = truncate_line(line, Pair2(dst_bounds), A, B, 1/w0 - C);
4dc49c
		for(int j = 0; j < line_count; ++j) {
4dc49c
			layer_src_bounds.expand( (back_matrix * Vector3(line[j], 1)).vec2() * w0 );
4dc49c
			layer_dst_bounds.expand( line[j] );
4dc49c
		}
4dc49c
		
4dc49c
		line_count = truncate_line(line, Pair2(dst_bounds), A, B, 1/w1 - C);
4dc49c
		for(int j = 0; j < line_count; ++j) {
4dc49c
			layer_src_bounds.expand( (back_matrix * Vector3(line[j], 1)).vec2() * w1 );
4dc49c
			layer_dst_bounds.expand( line[j] );
4dc49c
		}
4dc49c
		
4dc49c
		if (layer_src_bounds.empty() || layer_dst_bounds.empty())
4dc49c
			continue;
4dc49c
		
4dc49c
		// make layer
4dc49c
		Layer layer;
4dc49c
		layer.dst_bounds = IntPair2(
4dc49c
			IntVector2( std::max(dst_bounds.p0.x, (int)floor(layer_dst_bounds.p0.x + real_precision)),
4dc49c
						std::max(dst_bounds.p0.y, (int)floor(layer_dst_bounds.p0.y + real_precision)) ),
4dc49c
			IntVector2( std::min(dst_bounds.p1.x, (int)ceil (layer_dst_bounds.p1.x - real_precision)),
4dc49c
						std::min(dst_bounds.p1.y, (int)ceil (layer_dst_bounds.p1.y - real_precision)) ));
4dc49c
		calc_raster_size(
4dc49c
			layer.src_bounds,
4dc49c
			layer.src_size,
4dc49c
			layer.back_matrix,
4dc49c
			resolution * w,
4dc49c
			layer_src_bounds );
4dc49c
		layer.back_matrix *= back_matrix;
4dc49c
		layer.back_alpha_matrix = make_alpha_matrix(
4dc49c
			aw0, aw1, bw0, bw1,
4dc49c
			layer.back_matrix.get_col(2) );
4dc49c
		out_layers.push_back(layer);
4dc49c
	}
4dc49c
}
4dc49c
4dc49c
4dc49c
void
4dc49c
Perspective::add_premulted(
4dc49c
	const Layer &layer,
4dc49c
	const RefPtr<surface> &src_surface,</surface>
4dc49c
	const RefPtr<surface> &dst_surface )</surface>
4dc49c
{
4dc49c
	if (!src_surface || src_surface->empty()) return;
4dc49c
	if (!dst_surface || dst_surface->empty()) return;
4dc49c
	
4dc49c
	const int x0 = std::max(layer.dst_bounds.p0.x, 0);
4dc49c
	const int y0 = std::max(layer.dst_bounds.p0.y, 0);
4dc49c
	const int x1 = std::min(layer.dst_bounds.p1.x, dst_surface->width());
4dc49c
	const int y1 = std::min(layer.dst_bounds.p1.y, dst_surface->width());
4dc49c
	if (x0 >= x1 || y0 >= y1) return;
4dc49c
	
4dc49c
	const int w = x1 - x0;
4dc49c
	const int h = y1 - y0;
4dc49c
	if (w <= 0 || h <= 0) return;
4dc49c
	
4dc49c
	const Vector3 coord_dx = layer.back_matrix.row_x();
4dc49c
	const Vector3 coord_dy = layer.back_matrix.row_y() + coord_dx*(x0 - x1);
4dc49c
	Vector3 coord = layer.back_matrix * Vector3(x0, y0, 1);
4dc49c
4dc49c
	const Vector2 alpha_dx = layer.back_alpha_matrix.row_x().vec2();
4dc49c
	const Vector2 alpha_dy = layer.back_alpha_matrix.row_y().vec2() + alpha_dx*(x0 - x1);
4dc49c
	Vector2 alpha = (layer.back_alpha_matrix * Vector3(x0, y0, 1)).vec2();
4dc49c
	
4dc49c
	const int dr = dst_surface->pitch() - x1 + x0;
4dc49c
	Color *c = &dst_surface->row(y0)[x0];
4dc49c
	for(int r = y0; r < y1; ++r, c += dr, coord += coord_dy, alpha += alpha_dy) {
4dc49c
		for(Color *end = c + w; c != end; ++c, coord += coord_dx, alpha += alpha_dx) {
4dc49c
			if (coord.z > real_precision) {
4dc49c
				const Real w = 1/coord.z;
4dc49c
				const Real a = clamp(alpha.x*w, 0, 1) * clamp(alpha.y*w, 0, 1);
4dc49c
				if (a > real_precision)
4dc49c
					*c += src_surface->get_pixel_premulted(coord.x*w, coord.y*w) * a;
4dc49c
			}
4dc49c
		}
4dc49c
	}
4dc49c
}
4dc49c