Blob Blame Raw
#include <iostream>
#include <utility>

#include <gdkmm/pixbuf.h>
#include <gtkmm/hvbox.h>
#include <gtkmm/image.h>

#include "mainwindow.h"


// it's not a math cross product
// just speculative name for function: dot(a, Vector2(-y, x))
static Real
cross_product2(const Vector2 &a, const Vector2 &b)
	{ return a*b.perp(); }

static bool
line_cross(const Pair2 &line_a, const Pair2 &line_b, Vector2 &out) {
	Vector2 da = line_a.distance();
	Vector2 db = line_b.distance();
	Real denominator = cross_product2(da, db);
	if (real_equal(denominator, 0.0))
		return false;
	Real numerator = cross_product2(da, line_a.p0 - line_b.p0);
	out = line_a.p0 + db*(numerator/denominator);
	return true;
}

static Vector4
make_matrix_row(const Vector2 &p0, const Vector2 &p1)
	{ return Vector4(p1.x - p0.x, p1.y - p0.y, 0.0, 0.0); }

static Vector4
make_matrix_row(const Vector2 &p0, const Vector2 &p1, const Vector2 &p2) {
	Real l2 = (p2 - p1).length();
	if (real_less_or_equal(l2, 0.0))
		return Vector4();
	Real l1 = (p1 - p0).length();
	Real w = l1/l2;
	return Vector4(p2.x*w, p2.y*w, 0.0, w);
}

static Matrix4
make_perspective_matrix(const Vector2 &p0, const Vector2 &px, const Vector2 &py, const Vector2 &p1) {
	Vector2 focus;
	Matrix4 m;
	m[0] = line_cross(Pair2(p0, px), Pair2(py, p1), focus)
		 ? make_matrix_row(p0, px, focus)
		 : make_matrix_row(p0, px);
	m[1] = line_cross(Pair2(p0, py), Pair2(px, p1), focus)
		 ? make_matrix_row(p0, py, focus)
		 : make_matrix_row(p0, py);
	m[2] = Vector4(0.0, 0.0, 1.0, 0.0);
	m[3] = Vector4(p0.x, p0.y, 0.0, 1.0);
	return m;
}

static Matrix4
make_plain_matrix(const Pair2 &bounds) {
	const Vector2 &p = bounds.p0;
	const Vector2 d = bounds.distance();
	return Matrix4(
		Vector4(d.x, 0.0, 0.0, 0.0),
		Vector4(0.0, d.y, 0.0, 0.0),
		Vector4(0.0, 0.0, 1.0, 0.0),
		Vector4(p.x, p.y, 0.0, 1.0) );
}

static void
make_layers(const Pair2 &bounds, const Matrix4 &matrix, MainWindow::LayerList &out_layers) {
	const Real min_distance_to_horizon = 1.0; // one pixel
	
	if (bounds.empty() || real_equal(matrix.row_w().w, 0.0))
		return;
	
	// get focuses
	Vector2 focus_x, focus_y;
	bool focus_x_exists = real_not_equal(matrix.row_x().w, 0.0);
	bool focus_y_exists = real_not_equal(matrix.row_y().w, 0.0);
	if (focus_x_exists)
		focus_x = matrix.row_x().vec2() / matrix.row_x().w;
	if (focus_y_exists)
		focus_y = matrix.row_y().vec2() / matrix.row_y().w;

	// get horizon
	Vector2 h, dh;
	if (focus_x_exists && focus_x_exists) {
		h = focus_x;
		dh = (focus_y - focus_x).normalized();
	} else
	if (focus_x_exists) {
		h = focus_x;
		dh = matrix.row_y().vec2().normalized();
	} else
	if (focus_y_exists) {
		h = focus_y;
		dh = matrix.row_x().vec2().normalized();
	} else {
		// TODO
		return;
	}
	
	// get horison perpendicular
	Vector2 hp = dh.perp().normalized();
	
	// get perspective bounds
	Vector2 corners[] = {
		Vector2(bounds.p0.x, bounds.p0.y),
		Vector2(bounds.p0.x, bounds.p1.y),
		Vector2(bounds.p1.x, bounds.p1.y),
		Vector2(bounds.p1.x, bounds.p0.y),
		Vector2(bounds.p0.x, bounds.p0.y) }; // close the ring
	Real lmin = 1.0/real_precision;
	Real lmax = 0.0;
	for(int i = 0; i < 4; ++i) {
		Real p = hp*(corners[i] - h);
		if (p < lmin) lmin = p;
		if (p > lmax) lmax = p;
	}
	lmin = std::max(min_distance_to_horizon, lmin);
	lmax = std::max(min_distance_to_horizon, lmax);
	if (real_less_or_equal(lmax, min_distance_to_horizon))
		return;
	
	// layers
	Real scale_step = 4.0;
	Real l = lmin;
	Matrix4 back_matrix = matrix.inverted();
	while(real_less(l, lmax)) {
		Vector2 p0 = h + hp*l;
		l *= scale_step;
		Vector2 p1 = h + hp*l;
		
		Vector4 points[8];
		int points_count = 0;
		for(int i = 0; i < 4; ++i) {
			const Vector2 &cp0 = corners[i];
			const Vector2 &cp1 = corners[i+1];
			Vector2 dc = cp1 - cp0;
			Real kk = hp*dc;
			if (real_not_equal(kk, 0.0)) {
				kk = 1.0/kk;
				Real cl0 = clamp( hp*(p0 - cp0)*kk, 0.0, 1.0 );
				Real cl1 = clamp( hp*(p1 - cp0)*kk, 0.0, 1.0 );
				if (real_not_equal(cl1 - cl0, 0.0)) {
					points[points_count++] = Vector4(cp0 + dc*cl0, 0.0, 1.0);
					points[points_count++] = Vector4(cp0 + dc*cl1, 0.0, 1.0);
				}
			}
		}

		Vector2 src_res;
		{
			Vector2 c = (p0 + p1)*0.5;
			Vector2 px = c*0.75 + p1*0.25;
			Vector2 dx = px - c;
			Vector2 dy = dx.perp();
			Vector2 py = c + dy;
			Real len = dx.length();
			
			Vector2 cc  = (back_matrix*Vector4(c , 0, 1)).persp_divide().vec2();
			Vector2 cpx = (back_matrix*Vector4(px, 0, 1)).persp_divide().vec2();
			Vector2 cpy = (back_matrix*Vector4(py, 0, 1)).persp_divide().vec2();
			Vector2 ddx = cpx - cc;
			Vector2 ddy = cpy - cc;
			src_res = Vector2(
				len/std::max(fabs(ddx.x), fabs(ddy.x)),
				len/std::max(fabs(ddx.y), fabs(ddy.y)) );
		}
		

		Pair2 src_rect;
		Pair2 dst_rect;
		for(int i = 0; i < points_count; ++i) {
			const Vector4 &p = points[i];
			Vector4 pp = back_matrix*p;
			assert(real_not_equal(pp.w, 0.0));
			pp /= pp.w;
			if (i) {
				if (src_rect.p0.x > pp.x) src_rect.p0.x = pp.x;
				if (src_rect.p0.y > pp.y) src_rect.p0.y = pp.y;
				if (src_rect.p1.x < pp.x) src_rect.p1.x = pp.x;
				if (src_rect.p1.y < pp.y) src_rect.p1.y = pp.y;

				if (dst_rect.p0.x > p.x) dst_rect.p0.x = p.x;
				if (dst_rect.p0.y > p.y) dst_rect.p0.y = p.y;
				if (dst_rect.p1.x < p.x) dst_rect.p1.x = p.x;
				if (dst_rect.p1.y < p.y) dst_rect.p1.y = p.y;
			} else {
				src_rect.p0 = src_rect.p1 = pp.vec2();
				dst_rect.p0 = dst_rect.p1 = points[i].vec2();
			}
		}
		
		if (!src_rect.empty() && !dst_rect.empty())
		{
			Vector2 src_size_r = src_rect.size();
			IntVector2 src_size(
				(int)ceil(src_size_r.x * src_res.x),
				(int)ceil(src_size_r.y * src_res.y) );
			if (src_size.x > 0 && src_size.y > 0 && src_size.x*src_size.x < 100000000) {
				out_layers.push_back(MainWindow::Layer());
				MainWindow::Layer &layer = out_layers.back();
				layer.src_bounds = src_rect;
				layer.src_size = src_size;
				layer.dst_bounds.p0.x = (int)floor(dst_rect.p0.x) - 2;
				layer.dst_bounds.p0.y = (int)floor(dst_rect.p0.y) - 2;
				layer.dst_bounds.p1.x = (int)ceil(dst_rect.p1.x) + 2;
				layer.dst_bounds.p1.y = (int)ceil(dst_rect.p1.y) + 2;
				layer.back_transform = back_matrix;
			}
		}
	}
}

static void
make_surface(const Generator &generator, MainWindow::Layer &layer) {
	if (!layer.src_surface) {
		layer.dst_surface.reset();
		layer.cairo_surface.clear();
		
		if (layer.src_size.x > 0 && layer.src_size.y > 0) {
			Surface *surface = new DataSurface(layer.src_size.x, layer.src_size.y);
			generator.generate(*surface, layer.src_bounds);
			layer.src_surface = RefPtr<Surface>(surface);
		}
	}
	
	if (layer.src_surface && !layer.dst_surface) {
		layer.cairo_surface.clear();
		
		if ( layer.dst_bounds.p0.x < layer.dst_bounds.p1.x
		  && layer.dst_bounds.p0.y < layer.dst_bounds.p1.y )
		{
			int w = layer.dst_bounds.p1.x - layer.dst_bounds.p0.x;
			int h = layer.dst_bounds.p1.y - layer.dst_bounds.p0.y;
			Surface *surface = new DataSurface(w, h);
			for(int y = 0; y < h; ++y) {
				for(int x = 0; x < w; ++x) {
					Vector4 pos = layer.back_transform * Vector4(x + layer.dst_bounds.p0.x, y + layer.dst_bounds.p0.y, 0, 1);
					if (fabs(pos.w) > real_precision)
						pos /= pos.w;
					surface->row(y)[x] = layer.src_surface->get_pixel(pos.x, pos.y);
				}
			}
			layer.dst_surface = RefPtr<Surface>(surface);
		}
	}
	
	if (layer.dst_surface && !layer.cairo_surface) {
		layer.cairo_surface = layer.dst_surface->to_cairo_surface();
	}
}




MainWindow::MainWindow():
	src_rect_p0   (new View::Point( Vector2(-1, -1) )),
	src_rect_p1   (new View::Point( Vector2( 1,  1) )),
	dst_rect_p0   (new View::Point( Vector2(-1, -1) )),
	dst_rect_px   (new View::Point( Vector2( 1, -1) )),
	dst_rect_py   (new View::Point( Vector2(-1,  1) )),
	dst_rect_p1   (new View::Point( Vector2( 1,  1) )),
	dst_bounds_p0 (new View::Point( Vector2(-2, -2) )),
	dst_bounds_p1 (new View::Point( Vector2( 2,  2) ))
{
	std::cout << "generator seed: " << generator.seed() << std::endl;
	set_default_size(750, 500);
	
	Gtk::HBox *hbox = manage(new Gtk::HBox());
	hbox->set_homogeneous(true);
	
	src_view.transform.scale(50, 50);
	src_view.points.push_back(src_rect_p0);
	src_view.points.push_back(src_rect_p1);
	src_view.signal_point_motion.connect(
		sigc::mem_fun(*this, &MainWindow::on_point_motion) );
	src_view.signal_point_changed.connect(
		sigc::mem_fun(*this, &MainWindow::on_point_changed) );
	src_view.signal_transform_changed.connect(
		sigc::bind(
			sigc::mem_fun(*this, &MainWindow::on_view_transform_changed),
			&src_view ));
	src_view.signal_draw_view.connect(
		sigc::mem_fun(*this, &MainWindow::on_draw_src_view) );
	hbox->add(src_view);
	
	dst_view.transform.scale(50, 50);
	dst_view.points.push_back(dst_rect_p0);
	dst_view.points.push_back(dst_rect_px);
	dst_view.points.push_back(dst_rect_py);
	dst_view.points.push_back(dst_rect_p1);
	dst_view.points.push_back(dst_bounds_p0);
	dst_view.points.push_back(dst_bounds_p1);
	dst_view.signal_point_motion.connect(
		sigc::mem_fun(*this, &MainWindow::on_point_motion) );
	dst_view.signal_point_changed.connect(
		sigc::mem_fun(*this, &MainWindow::on_point_changed) );
	src_view.signal_transform_changed.connect(
		sigc::bind(
			sigc::mem_fun(*this, &MainWindow::on_view_transform_changed),
			&dst_view ));
	dst_view.signal_draw_view.connect(
		sigc::mem_fun(*this, &MainWindow::on_draw_dst_view) );
	hbox->add(dst_view);
	
	//hbox->add(*manage(generator_demo()));
	
	add(*hbox);
	show_all();
	
	update_view_surface();
	update_layers();
}

Gtk::Widget*
MainWindow::generator_demo() {
	std::cout << "generator demo: generate" << std::endl;
	DataSurface surface(256, 256);
	generator.generate(surface, Pair2(Vector2(0, 0), Vector2(16, 16)));
	
	std::cout << "generator demo: convert" << std::endl;
	Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create(
		Gdk::COLORSPACE_RGB, true, 8, surface.width(), surface.height() );
	guint8 *pixel = pixbuf->get_pixels();
	for(int r = 0; r < surface.height(); ++r) {
		Color *row = surface[r];
		for(int c = 0; c < surface.width(); ++c) {
			const Color &color = row[c];
			*(pixel++) = (guint8)(color.r*255.9);
			*(pixel++) = (guint8)(color.g*255.9);
			*(pixel++) = (guint8)(color.b*255.9);
			*(pixel++) = (guint8)(color.a*255.9);
		}
	}

	return new Gtk::Image(pixbuf);
}

void
MainWindow::update_view_surface() {
	Matrix4 transform = src_view.transform.inverted();
	int w = src_view.get_width();
	int h = src_view.get_height();
	
	Pair2 rect(
		(transform * Vector4(0, 0, 0, 1)).vec2(),
		(transform * Vector4(w, h, 0, 1)).vec2() );
	
	if (w <= 0 || h <= 0) {
		view_surface.clear();
	} else
	if ( !view_surface
	  || view_surface->get_width() != w
	  || view_surface->get_height() != h
	  || view_surface_rect != rect )
	{
		DataSurface surface(w, h);
		generator.generate(surface, rect);
		view_surface = surface.to_cairo_surface();
		view_surface_rect = rect;
	}
}

void
MainWindow::update_layers() {
	Matrix dst_transform = dst_view.transform_to_pixels();
	Vector2 dst_rect_p0_pixels(dst_transform * Vector4(dst_rect_p0->position, 0.0, 1.0));
	Vector2 dst_rect_px_pixels(dst_transform * Vector4(dst_rect_px->position, 0.0, 1.0));
	Vector2 dst_rect_py_pixels(dst_transform * Vector4(dst_rect_py->position, 0.0, 1.0));
	Vector2 dst_rect_p1_pixels(dst_transform * Vector4(dst_rect_p1->position, 0.0, 1.0));
	Vector2 dst_bounds_p0_pixels(dst_transform * Vector4(dst_bounds_p0->position, 0.0, 1.0));
	Vector2 dst_bounds_p1_pixels(dst_transform * Vector4(dst_bounds_p1->position, 0.0, 1.0));

	Matrix in_matrix = make_plain_matrix( Pair2(
		src_rect_p0->position,
		src_rect_p1->position ));
	Matrix out_matrix = make_perspective_matrix(
		dst_rect_p0_pixels,
		dst_rect_px_pixels,
		dst_rect_py_pixels,
		dst_rect_p1_pixels );
	Matrix matrix = out_matrix * in_matrix.inverted();

	Pair2 bounds(
		dst_bounds_p0_pixels,
		dst_bounds_p1_pixels );
	bounds.sort();

	LayerList layers;
	make_layers(bounds, matrix, layers);
	
	for(LayerList::iterator i = layers.begin(); i != layers.end(); ++i)
		make_surface(generator, *i);
	
	target_surface.clear();
	int w = dst_view.get_width();
	int h = dst_view.get_height();
	if (w > 0 && h > 0) {
		target_surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, w, h);
		Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create(target_surface);
		context->set_operator(Cairo::OPERATOR_ADD);
		for(LayerList::const_iterator i = layers.begin(); i != layers.end(); ++i) {
			if (i->cairo_surface) {
				context->set_source(i->cairo_surface, i->dst_bounds.p0.x, i->dst_bounds.p0.y);
				context->paint();
			}
		}
	}
}

void
MainWindow::on_point_motion(const View::PointPtr &/*point*/) {
	queue_draw();
}

void
MainWindow::on_point_changed(const View::PointPtr &/*point*/) {
	update_layers();
}

void
MainWindow::on_view_transform_changed(View *view) {
	if (view == &src_view) {
		update_view_surface();
		view->queue_draw();
	}
}

void
MainWindow::on_draw_src_view(const Cairo::RefPtr<Cairo::Context> &context) {
	const Real ps = src_view.get_pixel_size();

	context->save();

	if ( !view_surface
	  || src_view.get_width() != view_surface->get_width()
	  || src_view.get_height() != view_surface->get_height() )
		update_view_surface();
	if (view_surface) {
		context->save();
		context->transform( src_view.transform_from_pixels().to_cairo() );
		context->set_source(view_surface, 0, 0);
		context->paint();
		context->restore();
	}
	
	context->move_to(src_rect_p0->position.x, src_rect_p0->position.y);
	context->line_to(src_rect_p0->position.x, src_rect_p1->position.y);
	context->line_to(src_rect_p1->position.x, src_rect_p1->position.y);
	context->line_to(src_rect_p1->position.x, src_rect_p0->position.y);
	context->close_path();
	context->set_line_width(ps);
	context->set_source_rgba(1, 1, 0, 0.5);
	context->stroke();
	
	context->restore();
}

void
MainWindow::on_draw_dst_view(const Cairo::RefPtr<Cairo::Context> &context) {
	const Real ps = src_view.get_pixel_size();

	context->save();
	if (target_surface) {
		context->save();
		context->transform( dst_view.transform.inverted().to_cairo() );
		context->set_source(target_surface, 0, 0);
		context->paint();
		context->restore();
	}
	
	context->move_to(dst_rect_p0->position.x, dst_rect_p0->position.y);
	context->line_to(dst_rect_px->position.x, dst_rect_px->position.y);
	context->line_to(dst_rect_p1->position.x, dst_rect_p1->position.y);
	context->line_to(dst_rect_py->position.x, dst_rect_py->position.y);
	context->close_path();
	context->set_line_width(ps);
	context->set_source_rgba(1, 1, 0, 0.5);
	context->stroke();

	context->move_to(dst_bounds_p0->position.x, dst_bounds_p0->position.y);
	context->line_to(dst_bounds_p0->position.x, dst_bounds_p1->position.y);
	context->line_to(dst_bounds_p1->position.x, dst_bounds_p1->position.y);
	context->line_to(dst_bounds_p1->position.x, dst_bounds_p0->position.y);
	context->close_path();
	context->set_line_width(ps);
	context->set_source_rgba(1, 0, 0, 0.5);
	context->stroke();

	context->restore();
}