Blob Blame Raw
#include <iostream>

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

#include "log.h"
#include "perspective.h"

#include "mainwindow.h"


const bool checks = false;
const bool checks_on_generator = true;
const bool paint_outside_bounds = true;


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_src_surface();
	update_dst_surface();
}

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_src_surface() {
	std::cout << "update_src_surface() - begin" << std::endl;

	Matrix4 transform = src_view.transform_from_pixels();
	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) {
		src_surface.clear();
	} else {
		DataSurface surface(w, h);
		if (checks) generator.generate_checks(surface, rect);
			   else generator.generate(surface, rect, checks_on_generator);
		src_surface = surface.to_cairo_surface();
	}

	std::cout << "update_src_surface() - end" << std::endl;
}

void
MainWindow::update_dst_surface() {
	std::cout << "update_dst_surface() - begin" << std::endl;

	dst_surface.clear();
	
	// full dst_bounds
	const IntPair2 full_dst_bounds_pixels(
		IntVector2(0, 0),
		IntVector2(src_view.get_width(), src_view.get_height()) );
	
	// src matrix
	const Pair2 src_bounds(
		src_rect_p0->position,
		src_rect_p1->position );
	const Vector2 src_dist = src_bounds.distance();
	if (fabs(src_dist.x) < real_precision || fabs(src_dist.y) < real_precision)
		return;
	
	Real kx = 1/src_dist.x;
	Real ky = 1/src_dist.y;
	Real x0 = -src_bounds.p0.x*kx;
	Real y0 = -src_bounds.p0.y*ky;
	Matrix3 src_matrix(
		Vector3(kx,  0, 0),
		Vector3( 0, ky, 0),
		Vector3(x0, y0, 1) );
	
	// dst matrix
	const Matrix dst_transform = dst_view.transform_to_pixels();
	const Vector2 dst_rect_p0_pixels(dst_transform * Vector4(dst_rect_p0->position, 0.0, 1.0));
	const Vector2 dst_rect_px_pixels(dst_transform * Vector4(dst_rect_px->position, 0.0, 1.0));
	const Vector2 dst_rect_py_pixels(dst_transform * Vector4(dst_rect_py->position, 0.0, 1.0));
	const Vector2 dst_rect_p1_pixels(dst_transform * Vector4(dst_rect_p1->position, 0.0, 1.0));
	Matrix3 dst_matrix = Perspective::make_matrix(
		dst_rect_p0_pixels,
		dst_rect_px_pixels,
		dst_rect_py_pixels,
		dst_rect_p1_pixels );

	// final perspective matrix
	Matrix3 matrix = dst_matrix * src_matrix;

	// dst bounds
	const Pair2 dst_bounds_pixels_float(
		(dst_transform * Vector4(dst_bounds_p0->position, 0.0, 1.0)).vec2(),
		(dst_transform * Vector4(dst_bounds_p1->position, 0.0, 1.0)).vec2() );
	const IntPair2 dst_bounds_pixels(
		IntVector2( (int)floor(dst_bounds_pixels_float.p0.x + real_precision),
					(int)floor(dst_bounds_pixels_float.p0.y + real_precision) ),
		IntVector2( (int)ceil (dst_bounds_pixels_float.p1.x - real_precision),
					(int)ceil (dst_bounds_pixels_float.p1.y - real_precision) ));
	if (dst_bounds_pixels.empty())
		return;

	std::cout << Log::tab() << "dst_bounds_pixels: " << Log::to_string(dst_bounds_pixels) << std::endl;
	std::cout << Log::tab() << "matrix:" << std::endl
			  << Log::to_string(matrix, 10, Log::tab(2));

	// make layers
	Perspective::LayerList layers;
	Perspective::create_layers(
		layers,
		matrix,
		dst_bounds_pixels,
		2.0 );
	
	std::cout << Log::tab() << "created layers:" << std::endl;
	Perspective::print_layers(layers, Log::tab(2));
	std::cout << Log::tab() << "---------------" << std::endl;

	//std::cout << Log::tab() << "optimal step: "
	//		  << Perspective::find_optimal_step(matrix, dst_bounds_pixels) << std::endl;
	
	// override dst_bounds
	if (paint_outside_bounds)
		for(Perspective::LayerList::iterator i = layers.begin(); i != layers.end(); ++i)
			i->dst_bounds = full_dst_bounds_pixels;
	
	// make surface
	DataSurface surface(full_dst_bounds_pixels.p1.x, full_dst_bounds_pixels.p1.y);
	for(Perspective::LayerList::const_iterator i = layers.begin(); i != layers.end(); ++i) {
		DataSurface layer_surface(i->src_size.x, i->src_size.y);
		if (checks) generator.generate_checks(layer_surface, i->src_bounds);
			   else generator.generate(layer_surface, i->src_bounds, checks_on_generator);
		layer_surface.mult_alpha();
		Perspective::add_premulted(*i, layer_surface, surface);
	}
	for(Perspective::LayerList::const_iterator i = layers.begin(); i != layers.end(); ++i)
		Perspective::paint_cross(surface, i->center);
	dst_surface = surface.to_cairo_surface(true);

	std::cout << "update_dst_surface() - end" << std::endl;
}

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

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

void
MainWindow::on_view_transform_changed(View *view) {
	if (view == &src_view) {
		update_src_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 ( !src_surface
	  || src_view.get_width() != src_surface->get_width()
	  || src_view.get_height() != src_surface->get_height() )
		update_src_surface();
	if (src_surface) {
		context->save();
		context->transform( src_view.transform_from_pixels().to_cairo() );
		context->set_source(src_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 = dst_view.get_pixel_size();

	context->save();
	if (dst_surface) {
		context->save();
		context->transform( dst_view.transform_from_pixels().to_cairo() );
		context->set_source(dst_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();
}