КомпГр_ЛР3_Заболотников_Петрова_Романова_9373
.pdfQLabel *ql_ValueOfRow;
QLabel *ql_ValueOfCol;
QPushButton *qpb_ValueOfMatrice;
QHBoxLayout *qhbl_ValueOfMatrice;
};
#endif // SETSIZEWINDOW_H
Файл «surface_bezier.h»
#ifndef SURFACE_BEZIER_H #define SURFACE_BEZIER_H
#include "curve_bezier.h"
class Surface_Bezier : public Curve_Bezier
{
public:
Surface_Bezier();
Surface_Bezier(vector<vector<double> > x, vector<vector<double> > y, vector<vector<double> > z, int depth);
void set_surf (vector<vector<double> > x, vector<vector<double> > y, vector<vector<double> > z, int depth);
vector <Curve_Bezier> get_Row(); vector <Curve_Bezier> get_Col();
private:
vector <Curve_Bezier> row; vector <Curve_Bezier> col; void qs();
};
#endif // SURFACE_BEZIER_H
Файл «curve_bezier.h»
#ifndef CURVE_BEZIER_H
#define CURVE_BEZIER_H
#include <vector>
#include <qdebug.h>
using namespace std;
class Curve_Bezier
{
public:
Curve_Bezier();
11
Curve_Bezier(vector <double> root_x, vector <double> root_y, vector <double> root_z, int depth);
void show();
vector <double> get_X(); vector <double> get_Y(); vector <double> get_Z();
private:
double coord(double first, double end, double turn); void calculate(vector <double> root_x,
vector <double> root_y, vector <double> root_z,
int depth); // Вычисление точек кривой vector <double> x; // Сами значения
vector <double> y; // Сами значение vector <double> z; //
};
#endif // CURVE_BEZIER_H
Файл «main.cpp»
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv); MainWindow w;
w.show();
return a.exec();
}
Файл «mainwindow.cpp»
#include "mainwindow.h" #include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setSizeWindow = new SetSizeWindow();
12
setSizeWindow->show(); connect(setSizeWindow,SIGNAL(ValueOfMatrice(int,int)),
SLOT(build_WindowOfMatrice(int,int)));
qpb_ValueOfMatrice = new QPushButton("OK"); flagFirst = 1;
connect(qpb_ValueOfMatrice,SIGNAL(clicked()),SLOT(build_WindowOfTools()));
//connect(qpb_ValueOfMatrice,SIGNAL(clicked()),qw_WindowEntireData,SLOT(delet eLater()));
}
MainWindow::~MainWindow()
{
delete ui; delete scene; delete qw_Main;
delete qw_buttons; delete qvbl_MainWindow; delete qvbl_Main; delete qhbl_buttons; delete surface;
delete qpb_Scale_Plus; delete qpb_Scale_Minus; delete qsb_Rotation_X; delete qsb_Rotation_Y; delete qsb_Rotation_Z; delete ql_Rotation_X; delete ql_Rotation_Y; delete ql_Rotation_Z; delete ql_AddLine; delete qcb_AddLine; delete setSizeWindow;
delete qpb_ValueOfMatrice; delete qsb_Depth;
delete qw_WindowEntireData; delete qgl_Main;
delete qpb_Inv_X; delete qpb_Inv_Y; delete ql_Scale;
}
void MainWindow::scale_Plus(){ scene->scale_Plus();
}
void MainWindow::scale_Minus(){ scene->scale_Minus();
}
void MainWindow::rotation_X(int x){ scene->rotation_X(x);
}
void MainWindow::rotation_Y(int y){ scene->rotation_Y(y);
}
void MainWindow::rotation_Z(int z){ scene->rotation_Z(z);
}
void MainWindow::show_AddLine(bool show){ scene->show_AddLine(show);
}
void MainWindow::invert_X(){
13
scene->invert_X();
}
void MainWindow::invert_Y(){ scene->invert_Y();
}
void MainWindow::build_WindowOfTools(){
x.resize(qsb_X.size()); y.resize(qsb_Y.size()); z.resize(qsb_Z.size());
for(int i=0;i<qsb_X.size();i++){ x[i].resize(qsb_X[i].size()); y[i].resize(qsb_X[i].size()); z[i].resize(qsb_X[i].size()); for(int j=0;j<qsb_X[0].size();j++){
x[i][j] = qsb_X[i][j]->value(); y[i][j] = qsb_Y[i][j]->value(); z[i][j] = qsb_Z[i][j]->value();
}
}
if (flagFirst) {
qw_buttons = new QWidget();
qhbl_buttons = new QHBoxLayout();
qpb_Scale_Plus = new QPushButton ("+"); qpb_Scale_Minus = new QPushButton ("-");
qpb_Inv_X = new QPushButton("Отразить по X"); qpb_Inv_Y = new QPushButton("Отразить по Y");
qsb_Rotation_X = new QSpinBox(); qsb_Rotation_Y = new QSpinBox(); qsb_Rotation_Z = new QSpinBox();
qsb_Rotation_X->setRange(-180,180); qsb_Rotation_Y->setRange(-180,180); qsb_Rotation_Z->setRange(-180,180);
ql_Rotation_X = new QLabel("X (Красная ось)"); ql_Rotation_Y = new QLabel("Y (Зелёная ось)"); ql_Rotation_Z = new QLabel("Z (Синяя ось)"); ql_Scale = new QLabel("Масштаб:");
ql_AddLine = new QLabel("Добавить Плоскости");
qcb_AddLine = new QCheckBox();
qsb_Rotation_X->setValue(25); qsb_Rotation_Y->setValue(-45);
qhbl_buttons->addWidget(qcb_AddLine); qhbl_buttons->addWidget(ql_AddLine);
qhbl_buttons->addWidget(qpb_Inv_X); qhbl_buttons->addWidget(qpb_Inv_Y);
qhbl_buttons->addWidget(qsb_Rotation_X); qhbl_buttons->addWidget(ql_Rotation_X);
14
qhbl_buttons->addWidget(qsb_Rotation_Y); qhbl_buttons->addWidget(ql_Rotation_Y);
qhbl_buttons->addWidget(qsb_Rotation_Z); qhbl_buttons->addWidget(ql_Rotation_Z);
qhbl_buttons->addWidget(ql_Scale); qhbl_buttons->addWidget(qpb_Scale_Plus); qhbl_buttons->addWidget(qpb_Scale_Minus);
connect (qpb_Scale_Plus,SIGNAL(clicked()),SLOT(scale_Plus())); connect (qpb_Scale_Minus,SIGNAL(clicked()),SLOT(scale_Minus()));
connect(qsb_Rotation_X,SIGNAL(valueChanged(int)),SLOT(rotation_X(int))); connect(qsb_Rotation_Y,SIGNAL(valueChanged(int)),SLOT(rotation_Y(int))); connect(qsb_Rotation_Z,SIGNAL(valueChanged(int)),SLOT(rotation_Z(int))); connect(qcb_AddLine,SIGNAL(clicked(bool)),SLOT(show_AddLine(bool))); connect(qpb_Inv_X,SIGNAL(clicked()),SLOT(invert_X())); connect(qpb_Inv_Y,SIGNAL(clicked()),SLOT(invert_Y()));
qvbl_MainWindow -> addLayout(qhbl_buttons);
surface = new Surface_Bezier(x,y,z,qsb_Depth->value()); scene = new Scene(x,y,z,*surface);
qvbl_MainWindow -> addWidget(scene);
}
surface->set_surf(x,y,z,qsb_Depth->value()); scene->set_scene(x,y,z,*surface);
//qw_buttons->setLayout(qhbl_buttons);
//qw_buttons->show();
//scene->show();
qw_WindowEntireData->show(); flagFirst = 0;
}
void MainWindow::build_WindowOfMatrice(int value_row, int value_col){ if(flagFirst)
{
QTime midnight(0,0,0);
srand(QTime::currentTime().second());//qsrand(midnight.secsTo(QTime::currentT ime()));
qgl_Main = new QGridLayout();
qw_WindowEntireData = new QWidget();
qsb_Depth = new QSpinBox(); qsb_Depth->setValue(1); qsb_Depth->setRange(1,4);
for (int i=0;i<value_row;i++){
vector <QSpinBox*> temp_qsb_X; vector <QSpinBox*> temp_qsb_Y; vector <QSpinBox*> temp_qsb_Z;
15
vector <QLabel*> temp_ql_XYZ;
for(int j=0;j<value_col;j++){
QSpinBox |
*test_x; |
test_x = |
new QSpinBox(); |
QSpinBox |
*test_y; |
test_y = |
new QSpinBox(); |
QSpinBox |
*test_z; |
test_z = |
new QSpinBox(); |
temp_qsb_X.push_back(test_x); temp_qsb_Y.push_back(test_y); temp_qsb_Z.push_back(test_z); QLabel *labelTmp;
labelTmp = new QLabel("("+QString::number(i)+","+QString::number(j)+")");
temp_ql_XYZ.push_back(labelTmp);
temp_qsb_X[j]->setRange(-50,50); temp_qsb_Y[j]->setRange(-50,50); temp_qsb_Z[j]->setRange(-50,50);
temp_qsb_X[j]->setValue(rand()%100 - 50); temp_qsb_Y[j]->setValue(rand()%100 - 50); temp_qsb_Z[j]->setValue(rand()%100 - 50);
}
qsb_X.push_back(temp_qsb_X); qsb_Y.push_back(temp_qsb_Y); qsb_Z.push_back(temp_qsb_Z);
ql_XYZ.push_back(temp_ql_XYZ);
}
for(int i=0;i<value_row;i++) for(int j=0;j<value_col;j++){
qgl_Main->addWidget(ql_XYZ[i][j],i,j*8); qgl_Main->addWidget(qsb_X[i][j],i,j*8+1); qgl_Main->addWidget(qsb_Y[i][j],i,j*8+2); qgl_Main->addWidget(qsb_Z[i][j],i,j*8+3);
}
qgl_Main->addWidget(qpb_ValueOfMatrice,value_row,2.5*value_col);
QLabel *labelTmp2;
labelTmp2 = new QLabel("D");
qgl_Main->addWidget(labelTmp2,value_row,3*value_col+1); qgl_Main->addWidget(qsb_Depth,value_row,3*value_col+2);
//qw_WindowEntireData->setLayout(qgl_Main);
qvbl_MainWindow = new QVBoxLayout(qw_WindowEntireData); qvbl_MainWindow -> addLayout(qgl_Main);
}
qw_WindowEntireData->show();
}
Файл «scene.cpp»
16
#include "scene.h"
Scene::Scene()
{
}
Scene::Scene(vector <vector<double> > new_x, vector <vector<double> > new_y,
vector <vector<double> > new_z, Surface_Bezier surface){
x= new_x;
y= new_y;
z= new_z;
this->surface = surface;
for(int i=0;i<surface.get_Col().size();i++){
for(int k=0;k<surface.get_Col()[i].get_X().size();k++){ x_test.push_back(surface.get_Col()[i].get_X()[k]); y_test.push_back(surface.get_Col()[i].get_Y()[k]); z_test.push_back(surface.get_Col()[i].get_Z()[k]);
}
}
for(int i=0;i<surface.get_Row().size();i++){
for(int k=0;k<surface.get_Row()[i].get_X().size();k++){ x_test.push_back(surface.get_Row()[i].get_X()[k]); y_test.push_back(surface.get_Row()[i].get_Y()[k]); z_test.push_back(surface.get_Row()[i].get_Z()[k]);
}
}
}
void Scene::set_scene(vector <vector<double> > new_x, vector <vector<double> > new_y,
vector <vector<double> > new_z, Surface_Bezier surface){
x= new_x;
y= new_y;
z= new_z;
x_test.clear(); y_test.clear(); z_test.clear();
this->surface = surface;
for(int i=0;i<surface.get_Col().size();i++){
for(int k=0;k<surface.get_Col()[i].get_X().size();k++){ x_test.push_back(surface.get_Col()[i].get_X()[k]); y_test.push_back(surface.get_Col()[i].get_Y()[k]); z_test.push_back(surface.get_Col()[i].get_Z()[k]);
}
}
for(int i=0;i<surface.get_Row().size();i++){
for(int k=0;k<surface.get_Row()[i].get_X().size();k++){ x_test.push_back(surface.get_Row()[i].get_X()[k]); y_test.push_back(surface.get_Row()[i].get_Y()[k]); z_test.push_back(surface.get_Row()[i].get_Z()[k]);
}
17
}
}
void Scene::initializeGL(){
qDebug() << "Initialization GL start";
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); //Заменили цвет было: qglClearColor(Qt::blue);
glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE);
qDebug() << "Initialization GL Finish";
}
void Scene::resizeGL(int nWidth, int nHeight){
//устанавливаем текущей проекционную матрицу glMatrixMode(GL_PROJECTION);
//присваиваем проекционной матрице единичную матрицу glLoadIdentity();
//мировое окно
//параметры видимости ортогональной проекции glOrtho(-1.0, 1.0, -1.0, 1.0, -10.0, 1.0);
//плоскости отсечения (левая, правая, верхняя, нижняя, передняя,
задняя)
//параметры видимости перспективной проекции
//glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
//плоскости отсечения: (левая, правая, верхняя, нижняя, ближняя,
дальняя)
//поле просмотра
//устанавливаем видовое окно с размерами равными окну виджета
glViewport(0, 0, (GLint)nWidth, (GLint)nHeight);
}
void Scene::paintGL(){
//glClear(GL_COLOR_BUFFER_BIT); // окно виджета очищается текущим цветом очистки
//очистка буфера изображения и глубины
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//устанавливаем положение и ориентацию матрице моделирования glMatrixMode(GL_MODELVIEW);
//загружаем единичную матрицу моделирования glLoadIdentity(); glScalef(var_Scale,var_Scale,var_Scale);
glRotatef(var_Rot_X, 1.0f, 0.0f, 0.0f); glRotatef(var_Rot_Y, 0.0f, 1.0f, 0.0f); glRotatef(var_Rot_Z, 0.0f, 0.0f, 1.0f);
draw_XYZ(); draw_Surface_Bezier(); draw_Polyhedron();
}
void Scene::draw_XYZ(){ glLineWidth(3.0f);
glBegin(GL_LINES);
glColor4f(1.00f, 0.00f, 0.00f, 0.0f); // красная ось X
18
glVertex3f( 50.0f, 0.0f, 0.0f); // первая точка glVertex3f(-50.0f, 0.0f, 0.0f); // вторая точка
glEnd();
glBegin(GL_LINES);
glColor4f(0.00f, 0.50f, 0.00f, 0.0f); // зеленная ось Y glVertex3f( 0.0f, 50.0f, 0.0f); // первая точка glVertex3f( 0.0f, -50.0f, 0.0f); // вторая точка
glEnd();
glBegin(GL_LINES);
glColor4f(0.00f, 0.00f, 1.00f, 0.0f); // синия ось X glVertex3f( 0.0f, 0.0f, 50.0f); // первая точка glVertex3f( 0.0f, 0.0f, -50.0f); // вторая точка
glEnd();
if(var_Alpha){
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLineWidth(0.5f);
glColor4f(1.00f, 0.00f, 0.00f, 0.0f); for (int i=-50;i<50;i+=5){
glColor4f(1.00f, 0.00f, 0.00f, 0.8f); glBegin(GL_LINES);
glVertex3f(-50,i,0); glVertex3f(50,i,0);
glEnd(); glBegin(GL_LINES);
glVertex3f(i,-50,0); glVertex3f(i,50,0);
glEnd();
glColor4f(0.00f, 0.00f, 1.00f, 0.8f); glBegin(GL_LINES);
glVertex3f(-50,0,i); glVertex3f(50,0,i);
glEnd(); glBegin(GL_LINES);
glVertex3f(i,0,-50); glVertex3f(i,0,50);
glEnd();
glColor4f(0.00f, 0.50f, 0.00f, 0.8f); glBegin(GL_LINES);
glVertex3f(0,-50,i); glVertex3f(0,50,i);
glEnd(); glBegin(GL_LINES);
glVertex3f(0,i,-50); glVertex3f(0,i,50);
glEnd();
}
glDisable(GL_BLEND);
}
}
void Scene::draw_Surface_Bezier(){
int pointOfLine = surface.get_Col().size(); double size = x_test.size();
for(int i=0;i<x_test.size();i++){ glLineWidth(1.0f);
19
if(i%pointOfLine == 0) glBegin(GL_LINE_STRIP);
double color = i/size; //double color = i/size; glColor4f(color,1-color,0.0f,1.0f);
glColor4f(1.0f,1-color,color,1.0f);
glVertex3f(var_Inv_X*x_test[i],var_Inv_Y*y_test[i],z_test[i]);
if(i%pointOfLine == pointOfLine - 1 or i == x_test.size()) glEnd();
}
}
void Scene::draw_Polyhedron(){
for(int i=0;i<x.size();i++){ glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLineWidth(1.0f);
glBegin(GL_LINE_STRIP); double size = x.size(),
color = i/size; glColor4f(1-color, color, 1.00f, 0.2f);
for(int j=0;j<x[0].size();j++){ glVertex3f(var_Inv_X*x[i][j],y[i][j],z[i][j]);
}
glEnd(); glDisable(GL_BLEND);
}
glPointSize(7.0f); glBegin(GL_POINTS);
for(int i=0;i<x.size();i++){ for(int j=0;j<x[0].size();j++){
glColor4f(1.00f, 1.00f, 1.00f, 1.0f); glVertex3f(var_Inv_X*x[i][j],var_Inv_Y*y[i][j],z[i][j]);
}
}
glEnd();
}
void Scene::scale_Plus(){ var_Scale *= 1.1;
this->update(); //this->updateGL();
}
void Scene::scale_Minus(){ var_Scale /= 1.1; this->update();
}
void Scene::rotation_X(int x){ var_Rot_X = x;
this->update();
}
void Scene::rotation_Y(int y){ var_Rot_Y = y; this->update();
}
void Scene::rotation_Z(int z){ var_Rot_Z = z; this->update();
}
20