// Dolaczamy plik naglowkowy naszej klasy MyWindow #include "mywindow.h" // Dolaczamy plik naglowkowy zawierajacy definicje GUI // Plik ten jest generowany automatycznie // z pliku XML "mywindow.ui" #include "ui_mywindow.h" #include #include #include #include #include #include // for float,double macros #include #include #include #define PI 3.1415 const int VIEW_SIZE = 4.0f; std::vector matrixMul(std::vector p, std::vector m) { std::vector res(3); for(int i = 0; i < 3; i++) { res.push_back(0); for(int j = 0; j < 3; j++) { res[i] += m[i * 3 + j] * p[j]; } } return res; } std::vector MyWindow::GetXY(Point p, std::vector m) { std::vector res(3); res[0] = ((p.x + VIEW_SIZE / 2.0f) / VIEW_SIZE * szer ); res[1] = ((p.y + VIEW_SIZE / 2.0f) / VIEW_SIZE * wys); return res; } std::vector matrixMul3x3(std::vector m1, std::vector m2) { std::vector res(9, 0); for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { res[i * 3 + j] += m1[i * 3 + k] * m2[k * 3 + j]; } } } return res; } std::vector matrixMul4x4(std::vector m1, std::vector m2) { std::vector res(16, 0); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { for(int k = 0; k < 4; k++) { res[i * 4 + j] += m1[i * 4 + k] * m2[k * 4 + j]; } } } return res; } // Definicja konstruktora, wywolujemy najpierw // konstruktor klasy nadrzednej, nastepnie tworzymy // obiekt klasy Ui_MyWindow reprezentujacy GUI MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MyWindow) { // Wywolujemy funkcje tworzaca elementy GUI // Jej definicja znajduje sie w pliku "ui_mywindow.h" ui->setupUi(this); // Pobieramy wymiary i wspolrzedne lewego gornego naroznika ramki // i ustawiamy wartosci odpowiednich pol // Uwaga: ramke "rysujFrame" wykorzystujemy tylko do // wygodnego ustaiwenia tych wymiarow. Rysunek bedziemy wyswietlac // bezposrednio w glownym oknie aplikacji. szer = ui->rysujFrame->width(); wys = ui->rysujFrame->height(); poczX = ui->rysujFrame->x(); poczY = ui->rysujFrame->y(); // Tworzymy obiekt klasy QImage, o odpowiedniej szerokosci // i wysokosci. Ustawiamy format bitmapy na 32 bitowe RGB // (0xffRRGGBB). img = new QImage(szer,wys,QImage::Format_RGB32); loaded_img = new QImage("/Users/dawidpietrykowski/Desktop/projects/umk/GK/QT/2d-transformations/p.png"); rotation_angle = 0; translation_vec.push_back(0); translation_vec.push_back(0); // points[0] = Point(-1.0f, -1.0f, 0.0f); // points[1] = Point(1.0f, -1.0f, 0.0f); // points[2] = Point(1.0f, 1.0f, 0.0f); // points[3] = Point(-1.0f, 1.0f, 0.0f); // points[4] = Point(-1.0f, -1.0f, 0.0f); // points[5] = Point(1.0f, -1.0f, 0.0f); // points[6] = Point(1.0f, 1.0f, 0.0f); // points[7] = Point(-1.0f, 1.0f, 0.0f); scale_vec.push_back(1.0f); scale_vec.push_back(1.0f); sh_vec.push_back(0.0f); sh_vec.push_back(0.0f); UpdateImage(); } // Definicja destruktora MyWindow::~MyWindow() { delete ui; } // Funkcja (slot) wywolywana po nacisnieciu przycisku "Wyjscie" (exitButton) // Uwaga: polaczenie tej funkcji z sygnalem "clicked" // emitowanym przez przycisk jest realizowane // za pomoca funkcji QMetaObject::connectSlotsByName(MyWindow) // znajdujacej sie w automatycznie generowanym pliku "ui_mywindow.h" // Nie musimy wiec sami wywolywac funkcji "connect" void MyWindow::on_exitButton_clicked() { // qApp to globalny wskaznik do obiektu reprezentujacego aplikacje // quit() to funkcja (slot) powodujaca zakonczenie aplikacji z kodem 0 (brak bledu) qApp->quit(); } void MyWindow::on_slider_tx_valueChanged(int val){ translation_vec[0] = val / 100.0f - 1.0f; } void MyWindow::on_slider_ty_valueChanged(int val){ translation_vec[1] = val / 100.0f - 1.0f; } void MyWindow::on_slider_a_valueChanged(int val){ rotation_angle = val; } void MyWindow::on_slider_sx_valueChanged(int val){ scale_vec[0] = 1.0f / ( val / 100.0f ); } void MyWindow::on_slider_sy_valueChanged(int val){ scale_vec[1] = 1.0f / ( val / 100.0f ); } void MyWindow::on_slider_shx_valueChanged(int val){ sh_vec[0] = val / 100.0f - 1.0f; } void MyWindow::on_slider_shy_valueChanged(int val){ sh_vec[1] = val / 100.0f - 1.0f; } void MyWindow::UpdateImage(){ float sina = sin(rotation_angle * 0.01745329252f); float cosa = cos(rotation_angle * 0.01745329252f); std::vector translate_mat = { 1, 0, 0, translation_vec[0], 0, 1, 0, translation_vec[1], 0, 0, 1, 0, 0, 0, 0, 1, }; std::vector scale_mat = { scale_vec[0], 0, 0, 0, 0, scale_vec[1], 0, 0, 0, 0, scale_vec[1], 0, 0, 0, 0, 1, }; std::vector rotation_mat_x = { 1, 0, 0, 0, 0, cosa, -sina, 0, 0, sina, cosa, 0, 0, 0, 0, 1 }; std::vector rotation_mat_y = { cosa, 0, sina, 0, 0, 1, 0, 0, -sina, 0, cosa, 0, 0, 0, 0, 1 }; std::vector rotation_mat_z = { cosa, -sina, 0, 0, sina, cosa, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; std::vector sh_mat = { 1, 0, sh_vec[0], 0, 0, 1, sh_vec[1], 0, 0, 0, 1, 0, 0, 0, 0, 1, }; std::vector tansform_mat = matrixMul4x4( matrixMul4x4( matrixMul4x4( matrixMul4x4( matrixMul4x4( scale_mat, rotation_mat_x), rotation_mat_y), rotation_mat_z), sh_mat), translate_mat); for(int i = 1; i < 8; i++){ Point P1 = points[i-1]; Point P2 = points[i]; std::vector p1_vec = GetXY(P1, std::vector{}); std::vector p2_vec = GetXY(P2, std::vector{}); DrawLine(p1_vec[0], p1_vec[1], p2_vec[0], p2_vec[1], img); } update(); } QColor MyWindow::GetPixel(QImage* img, int x, int y){ QColor res; int width = img->width(); int height = img->height(); if(x >= width || y >= height || x < 0 || y < 0) return res; unsigned char* ptr = img->bits(); res.setBlue(ptr[width*4*y + 4*x]); res.setGreen(ptr[width*4*y + 4*x + 1]); res.setRed(ptr[width*4*y + 4*x + 2]); res.setAlpha(ptr[width*4*y + 4*x + 3]); return res; } QColor mul(float v, QColor c){ int red = (int)(c.red() * v); if(red > 255) red = 255; int green = (int)(c.green() * v); if(green > 255) green = 255; int blue = (int)(c.blue() * v); if(blue > 255) blue = 255; return QColor(red, green, blue); } QColor add(QColor c1, QColor c2){ int red = (int)(c1.red() + c2.red()); if(red > 255) red = 255; int green = (int)(c1.green() + c2.green()); if(green > 255) green = 255; int blue = (int)(c1.blue() + c2.blue()); if(blue > 255) blue = 255; return QColor(red, green, blue); } QColor MyWindow::GetInterpolatedColor(float x, float y){ float width = loaded_img->width(); float height = loaded_img->height(); float xw = (x * width); float yw = (y * height); int x_f = (int) (x * width); int y_f = (int) (y * height); int x_c = ceil(x * width); int y_c = ceil(y * height); float a = (xw - (float) x_f); float b = ((yw) - (float) y_f); QColor P1 = GetPixel(loaded_img, x_f, y_c); QColor P2 = GetPixel(loaded_img, x_c, y_c); QColor P3 = GetPixel(loaded_img, x_c, y_f); QColor P4 = GetPixel(loaded_img, x_f, y_f); QColor v1 = mul(1.0f - a, P1); QColor v2 = mul(a, P2); QColor v3 = mul(a, P3); QColor v4 = mul(1.0f - a, P4); QColor top = add(v1, v2); QColor bottom = add(v3, v4); QColor btop = mul(b, top); QColor bbottom = mul(1.0f - b, bottom); QColor res = add(btop, bbottom); return res; } // Funkcja "odmalowujaca" komponent void MyWindow::paintEvent(QPaintEvent*) { // Obiekt klasy QPainter pozwala nam rysowac na komponentach QPainter p(this); UpdateImage(); // Rysuje obrazek "img" w punkcie (poczX,poczY) // (tu bedzie lewy gorny naroznik) p.drawImage(poczX,poczY,*img); } void MyWindow::DrawLine(int x1, int y1, int x2, int y2, QImage *img){ if(x1 > x2){ std::swap(x1, x2); std::swap(y1, y2); } float diff = x2 - x1; float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX; QColor color(255, 255, 255, 255); if(abs(a) < 1.0f){ for(int x = x1; x <= x2; x++){ int x_form = x - x1; int y = a * x_form + y1; DrawPixel(img, x, y, color); } } else{ if(y1 > y2){ std::swap(x1, x2); std::swap(y1, y2); } float diff = x2 - x1; float a = diff != 0 ? (y2 - y1) / diff : FLT_MAX; for(int y = y1; y <= y2; y++){ int y_form = y - y1; int x = ((float)(y_form) / a) + x1; DrawPixel(img, x, y, color); } } } void MyWindow::DrawPixel(QImage* img, int x, int y, QColor color){ if(x >= szer || y >= wys || x < 0 || y < 0) return; unsigned char* ptr = img->bits(); ptr[szer*4*y + 4*x] = color.blue(); ptr[szer*4*y + 4*x + 1] = color.green(); ptr[szer*4*y + 4*x + 2] = color.red(); ptr[szer*4*y + 4*x + 3] = color.alpha(); } void MyWindow::ClearImage(QImage *img){ unsigned char* empty_val = (unsigned char*)malloc(4); empty_val[0] = 0; empty_val[1] = 0; empty_val[2] = 0; empty_val[3] = 255; unsigned char* ptr = img->bits(); for(int i = 0; i < img->width(); i++){ for(int j = 0; j < img->height(); j++){ memcpy(ptr + 4 * (i + j * img->width()), empty_val, 4); } } }