commit 1486954791e2587ae59cc9eec8d67f1014276a8c Author: abc Date: Tue Sep 24 15:20:33 2024 +0800 完成断层和轮廓的初版 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86a9657 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pro.user \ No newline at end of file diff --git a/aoi.h b/aoi.h new file mode 100644 index 0000000..f3d6aa9 --- /dev/null +++ b/aoi.h @@ -0,0 +1,27 @@ +#ifndef AOI_H +#define AOI_H + +// +#include "basicgeometryoperator.h" +#include "contour.h" + + +//std +#include +#include + + +//Qt +#include +#include + +class aoi : public BasicGeometryOperator +{ +public: + aoi(); + +private: +// Outline outline; +}; + +#endif // AOI_H diff --git a/basicgeometry.h b/basicgeometry.h new file mode 100644 index 0000000..ac95ef9 --- /dev/null +++ b/basicgeometry.h @@ -0,0 +1,36 @@ +#ifndef BASICGEOMETRY_H +#define BASICGEOMETRY_H + + +class BasicGeometry +{ +public: + BasicGeometry(); + virtual void set_color(int red,int green, int blue)=0; + + virtual void set_unit(QString unit)=0; + + virtual void set_move(bool isMove)=0; + + + // points + virtual void add_points(int x,int y)=0; + virtual int get_point_id()=0; + virtual void remove_by_id()=0;// 删除一个点之后,是否遍历拓扑表删除和这个点相关的所有数据 + + // topology + virtual void add_topology()=0; + virtual int get_topology_id()=0; + virtual void remove_by_id()=0; + + // rgb + virtual int red()=0; + virtual int green()=0; + virtual int blue()=0; + + virtual QString unit()=0; + virtual bool isMove()=0; + +}; + +#endif // BASICGEOMETRY_H diff --git a/basicgeometryoperator.h b/basicgeometryoperator.h new file mode 100644 index 0000000..df21807 --- /dev/null +++ b/basicgeometryoperator.h @@ -0,0 +1,14 @@ +#ifndef BASICGEOMETRYOPERATOR_H +#define BASICGEOMETRYOPERATOR_H + + +class BasicGeometryOperator +{ +public: + virtual void paint()=0; + virtual void saveData()=0; + virtual void pick()=0;// 点、线、单元,区域 四种拾取 + virtual void move()=0; +}; + +#endif // BASICGEOMETRYOPERATOR_H diff --git a/contour.h b/contour.h new file mode 100644 index 0000000..6ce13da --- /dev/null +++ b/contour.h @@ -0,0 +1,356 @@ +#ifndef OUTLINE_H +#define OUTLINE_H + +// +#include "basicgeometryoperator.h" + + +//Qt +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define d(x) qDebug()<<__FUNCTION__<<(x) + +class Contour +{ +public: + Contour(double scalex,double scaley,bool bmove,double tolerance, QString title = "Fault_0"):_scalex(scalex),_scaley(scaley), + _move(bmove),_tolerance(tolerance){ + _title = title; + _painter = new QPainter(); + } + + Contour(QString title = "contour_0"):_scalex(1),_scaley(1), + _move(false),_tolerance(15){ + _title = title; + _painter = new QPainter(); + } + ~Contour(){} + + void addPoint(qreal x,qreal y){ + QPointF f(x,y); + _points.push_back(f); + if(polygonSelfIntersects()){ + setColor(255,0,0,50); + }else{ + setColor(0,0,0,150); + } + _rectPath = QPainterPath(); + _rectPath.addPolygon(_points); + _rectPath.setFillRule(Qt::OddEvenFill); + } + + bool polygonSelfIntersects() { + int n = _points.size(); + for(int i=0;i=0 || cdca*cdcb >=0); + } + + void setPoint(int i,qreal x,qreal y){ + QPointF f(x,y); + _rectPath.setElementPositionAt(i,x,y); + _points[i] = f; + if(_move){ + if(i==0 || i==_rectPath.elementCount()-1){ + _rectPath.setElementPositionAt(0,x,y); + _rectPath.setElementPositionAt(_rectPath.elementCount()-1,x,y); + _points[i] = f; + _points[_rectPath.elementCount()-1] = f; + } + setDotline(); + } + } + + bool isInside(const QPointF& a){ + int n = _points.count(); + for(int i=1;i=qMin(_points[i].x(),_points[i+1].x())){ + return true; + } + } + } + return false; + } + + int pointCount(){ + return _points.count(); + } + + int elementCount(){ + return _rectPath.elementCount(); + } + + void setColor(int red,int green,int blue,int alpha=50){ + _contourColor.setRgb(red,green,blue,alpha); + } + + QColor getColor() const{ + return _contourColor; + } + + QString getTitle() const{ + return _title; + } + + void setTitle(QString title){ + _title = title; + + } + + void saveData(){ + // TODO + } + + void setScalex(double scalex){ + _scalex = scalex; + } + double getScalex() const{ + return _scalex; + } + double getScaley() const{ + return _scaley; + } + void setScaley(double scaley){ + _scaley = scaley; + } + + bool isMove() const + { + return _move; + } + + void setMove(bool move) + { + _move = move; + } + + QPainterPath getRectPath() const + { + return _rectPath; + } + + void addRectPath(const QPainterPath &rectPath) + { + _rectPath.addPath(rectPath); + } + + QVector getPoints() const + { + return _points; + } + + double getTolerance() const + { + return _tolerance; + } + + void setTolerance(double value) + { + _tolerance = value; + } + + void clear(){ + _rectPath = QPainterPath(); + _dotPath = QPainterPath(); + _points.clear(); + } + + void generate(){ + _points.pop_back(); + int n = _points.count(); + if(_contourColor==QColor(255,0,0,50) || polygonSelfIntersects() || n < 3){ + clear(); + return; + } + _points.push_back(_points[0]); + _rectPath = QPainterPath(); + _rectPath.addPolygon(_points); + _rectPath.closeSubpath(); + _rectPath.setFillRule(Qt::OddEvenFill); + setDotline(); + + } + + void move(QMouseEvent *event){ + qreal x = event->posF().x(); + qreal y = event->posF().y(); + qreal tx = x - last.x(); + qreal ty = y - last.y(); + _rectPath.translate(tx,ty); + _dotPath.translate(tx,ty); + int n = _points.count(); + for(int i=0;iposF(); + } + + void pick(QMouseEvent* event){ + QPointF f = event->posF(); + if(!contains(f)){ + return; + } + setMove(true); + last = f; + } + + void setDotline(){ + QPainterPathStroker stroker; + stroker.setCapStyle(Qt::RoundCap); + stroker.setDashPattern(Qt::DashLine); + stroker.setJoinStyle(Qt::RoundJoin); + _dotPath = stroker.createStroke(_rectPath); + } + + void clearDotPath(){ + _dotPath = QPainterPath(); + } + + bool contains(const QPointF& f){ + for(int i=0;i<=_tolerance;++i){ + if(_dotPath.contains(f+QPointF(i,0))|| + _dotPath.contains(f+QPointF(-i,0))|| + _dotPath.contains(f+QPointF(0,-i))|| + _dotPath.contains(f+QPointF(0,i))){ + return true; + } + } + return false; + } + + int dotContains(const QPointF& f){ + qreal x = f.x(); + qreal y = f.y(); + for(int j=0;j<_points.size();++j) { + qreal x1 = _points[j].x(); + qreal y1 = _points[j].y(); + for(int i=0;i<=_tolerance;++i){ + if(x1 <= (x+i)&& + x1 >= (x-i)&& + y1 >= (y-i)&& + y1 <= (y+i)){ + return j; + } + } + } + return -1; + } + + + void paint(QWidget *w, QPaintEvent *event){ + _painter->begin(w); + _painter->scale(_scalex, _scaley); + _painter->setRenderHint(QPainter::Antialiasing); + pen.setColor(_contourColor); + pen.setWidth(2); + _painter->setPen(pen); + _painter->setBrush(_contourColor); + int n = _points.count(); + if(_move){ + _painter->save(); + for(int i=0;isetPen(Qt::NoPen); + _painter->setBrush(_contourColor); + _painter->drawEllipse(QPointF(_rectPath.elementAt(i).x,_rectPath.elementAt(i).y),5,5); + } + _painter->restore(); + _painter->drawPath(_dotPath); + }else{ + _painter->drawPath(_rectPath); + } + _painter->end(); + } + +private: + QColor _contourColor; + QPen pen; + // 记录标题,比如contour_1 + QString _title; + QVector _points; + QPainter *_painter; + + QPointF last; + + + // 缩放 + double _scalex; + double _scaley; + + // 移动 + bool _move; + // 容差 + double _tolerance; + + // fault的路径 + QPainterPath _rectPath; + + // fault的虚线框 + QPainterPath _dotPath; + +}; + +#endif // OUTLINE_H diff --git a/fault.h b/fault.h new file mode 100644 index 0000000..e64f60e --- /dev/null +++ b/fault.h @@ -0,0 +1,301 @@ +#ifndef FAULT_H +#define FAULT_H + +// +#include "basicgeometryoperator.h" + + +//Qt +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class Fault +{ +public: + Fault(double scalex,double scaley,bool bmove,double tolerance, QString title = "Fault_0"):_scalex(scalex),_scaley(scaley), + _move(bmove),_tolerance(tolerance){ + _title = title; + _isGenerated = false; + _painter = new QPainter(); + } + + Fault(QString title = "Fault_0"):_scalex(1),_scaley(1), + _move(false),_tolerance(30){ + _title = title; + _isGenerated = false; + _painter = new QPainter(); + } + + ~Fault(){ + clear(); + } + + void addPoint(qreal x,qreal y){ + if(_rectPath.elementCount()==0){ + _rectPath.moveTo(x,y); + }else{ + _rectPath.lineTo(x,y); + } + _points.push_back(QPointF(x,y)); + } + + void setPoint(int i,qreal x,qreal y){ + _rectPath.setElementPositionAt(i,x,y); + _points[i] = QPointF(x,y); + + if(_move){ + setDotline(); + setTitle(_title); + } + } + + int pointCount(){ + return _points.count(); + } + + int elementCount(){ + return _rectPath.elementCount(); + } + + void setColor(int red,int green,int blue,int alpha = 50){ + _faultColor.setRgb(red,green,blue,alpha); + } + + QColor getColor() const{ + return _faultColor; + } + + QString getTitle() const{ + return _title; + } + + void setTitle(const QString& title){ + _title = title; + _titlePath = QPainterPath(); + _titlePath.addText(_rectPath.elementAt(_rectPath.elementCount()-1).x+1, + _rectPath.elementAt(_rectPath.elementCount()-1).y+1, + QFont(),_title); + } + + void saveData(){ + // TODO + } + + void setScalex(double scalex){ + _scalex = scalex; + } + double getScalex() const{ + return _scalex; + } + double getScaley() const{ + return _scaley; + } + void setScaley(double scaley){ + _scaley = scaley; + } + + bool isMove() const + { + return _move; + } + + void setMove(bool move) + { + _move = move; + } + + QPainterPath getRectPath() const + { + return _rectPath; + } + + void addRectPath(const QPainterPath &rectPath) + { + _rectPath.addPath(rectPath); + } + + QVector getPoints() const + { + return _points; + } + + double getTolerance() const + { + return _tolerance; + } + + void setTolerance(double value) + { + _tolerance = value; + } + void clear(){ + _titlePath = QPainterPath(); + _rectPath = QPainterPath(); + _dotPath = QPainterPath(); + _points.clear(); + _isGenerated = false; + _move = false; + _title = "Fault_0"; + last = QPointF(); + _tolerance = 30; + } + + void generate(){ + _isGenerated = true; + _points.pop_back(); + int n = _points.count(); + if(n < 2){ + clear(); + return; + } + _rectPath = QPainterPath(); + _rectPath.addPolygon(_points); + _titlePath = QPainterPath(); + _titlePath.addText(_rectPath.elementAt(_rectPath.elementCount()-1).x+1, + _rectPath.elementAt(_rectPath.elementCount()-1).y+1, + QFont(),_title); + } + + void move(QMouseEvent *event){ + qreal x = event->posF().x(); + qreal y = event->posF().y(); + qreal tx = x - last.x(); + qreal ty = y - last.y(); + _rectPath.translate(tx,ty); + _dotPath.translate(tx,ty); + _titlePath.translate(tx,ty); + int n = _points.count(); + for(int i=0;iposF(); + } + bool isGenerated(){ + return _isGenerated; + } + + void pick(QMouseEvent* event){ + QPointF f = event->posF(); + setDotline(); + if(!contains(f)){ + return; + } + setMove(true); + last = f; + } + + void setDotline(){ + QPainterPathStroker stroker; + stroker.setCapStyle(Qt::RoundCap); + stroker.setDashPattern(Qt::DashLine); + stroker.setJoinStyle(Qt::RoundJoin); + stroker.setWidth(2); + _dotPath = stroker.createStroke(_rectPath); + } + + void clearDotPath(){ + _dotPath = QPainterPath(); + } + + bool contains(const QPointF& f){ + qreal x = f.x(); + qreal y = f.y(); + for(int i=0;i<=_tolerance;++i){ + if(_dotPath.contains(f+QPointF(i,0))|| + _dotPath.contains(f+QPointF(-i,0))|| + _dotPath.contains(f+QPointF(0,-i))|| + _dotPath.contains(f+QPointF(0,i))){ + return true; + } + } + return false; + } + + int dotContains(const QPointF& f){ + qreal x = f.x(); + qreal y = f.y(); + for(int j=0;j<_points.size();++j) { + qreal x1 = _points[j].x(); + qreal y1 = _points[j].y(); + for(int i=0;i<=_tolerance;++i){ + if(x1 <= (x+i)&& + x1 >= (x-i)&& + y1 >= (y-i)&& + y1 <= (y+i)){ + return j; + } + } + } + return -1; + } + + void paint(QWidget *w, QPaintEvent *event){ + _painter->begin(w); + _painter->scale(_scalex, _scaley); + _painter->setRenderHint(QPainter::Antialiasing); + QPen pen; + pen.setWidth(2); + pen.setColor(_faultColor); + _painter->setPen(pen); + int n = _points.count(); + if(_move){ + for(int i=0;idrawEllipse(QPointF(_rectPath.elementAt(i).x,_rectPath.elementAt(i).y),3,3); + } + _painter->drawPath(_dotPath); + } + else{ + _painter->drawPath(_rectPath); + } + if(_titlePath.elementCount()>0){ + pen.setWidth(1); + _painter->setPen(pen); + _painter->drawPath(_titlePath); + } + _painter->end(); + } + +private: + QColor _faultColor; + + // 记录标题,比如fault_1 + QString _title; + QVector _points; + QPainter *_painter; + + QPointF last; + QWidget *w; + + // 缩放 + double _scalex; + double _scaley; + + // 移动 + bool _move; + + bool _isGenerated; + + // 容差 + double _tolerance; + + // fault的路径 + QPainterPath _rectPath; + // title + QPainterPath _titlePath; + + // fault的虚线框 + QPainterPath _dotPath; + +}; + +#endif // FAULT_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b48f94e --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..9ed7ad4 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,170 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + this->setMouseTracking(true); + ui->centralWidget->setMouseTracking(true); + rectPath.moveTo(20.0, 30.0); + rectPath.lineTo(80.0, 30.0); + rectPath.lineTo(80.0, 70.0); + rectPath.lineTo(20.0, 70.0); + scalex = 1; + scaley = 1; +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::mousePressEvent(QMouseEvent *event){ + for(int i=0;iposF())){ + isPressed = true; + iPoint = f.dotContains(event->posF()); + }else if(c.contains(event->posF())){ + isPressed = true; + iPoint = c.dotContains(event->posF()); + } + if(event->button()==Qt::RightButton){ + f.setMove(false); + c.setMove(false); + } + if(ui->checkBox->checkState()==Qt::Unchecked && event->button()==Qt::LeftButton){ + f.pick(event); + } + } + if(ui->checkBox->checkState()==Qt::Checked){ + // Fault 测试集 + int n = f.elementCount(); + if(event->button()==Qt::LeftButton){ + qreal x = event->posF().x(); + qreal y = event->posF().y(); + if(n>1){ + f.setPoint(n-1,x+0.01,y+0.01); + } + f.addPoint(x,y); + if(n==0){ + f.addPoint(x+3,y+3); + } + + } + else if(event->button()==Qt::RightButton){ + f.generate(); + vf.push_back(f); + f.clear(); + ui->checkBox->setCheckState(Qt::Unchecked); + } + } + else if(ui->checkBox2->checkState()==Qt::Checked){ + // contour测试集 + int n = c.elementCount(); + if(event->button()==Qt::LeftButton){ + qreal x = event->posF().x(); + qreal y = event->posF().y(); + c.addPoint(x,y); + if(n==0){ + c.addPoint(x+3,y+3); + } + }else if(event->button()==Qt::RightButton){ + c.generate(); + ui->checkBox2->setCheckState(Qt::Unchecked); + } + } + if(ui->checkBox2->checkState()==Qt::Unchecked && event->button()==Qt::LeftButton){ + c.pick(event); + } + this->update(); +} + +void MainWindow::mouseReleaseEvent(QMouseEvent *event){ + isPressed = false; + this->update(); +} +void MainWindow::mouseMoveEvent(QMouseEvent *event){ + if(ui->checkBox->checkState()==Qt::Checked){ + int n = f.elementCount(); + qreal x1 = event->posF().x(); + qreal y1 = event->posF().y(); + if(n > 1){ + f.setPoint(n-1,x1,y1); + } + } + else if(ui->checkBox2->checkState()==Qt::Checked){ + int n = c.elementCount(); + qreal x1 = event->posF().x(); + qreal y1 = event->posF().y(); + if(n > 1){ + c.setPoint(n-1,x1,y1); + } + } + if(ui->checkBox2->checkState()==Qt::Unchecked&&ui->checkBox->checkState()==Qt::Unchecked&&isPressed){ + if(c.isMove()){ + if(iPoint!=-1){ + c.setPoint(iPoint,event->posF().x(),event->posF().y()); + }else{ + c.move(event); + } + } + } + if(ui->checkBox->checkState()==Qt::Unchecked&&ui->checkBox2->checkState()==Qt::Unchecked&&isPressed){ + for(int i=0;iposF().x(),event->posF().y()); + }else{ + f.move(event); + } + } + } + } + + this->update(); +} + +void MainWindow::paintEvent(QPaintEvent *event){ + c.paint(this,event); + f.paint(this,event); + for(int i=0;iwidth() / 100.0; +// scaley = this->height() / 100.0; +// painter.begin(this); +// painter.setRenderHint(QPainter::Antialiasing); +// painter.scale(scalex, scaley); +// QColor color(255,30,1); +// QPen pen; +// pen.setColor(color); +// painter.setPen(pen); +// int n = rectPath.elementCount(); +// if(ismove){ +// painter.save(); +// for(int i=0;i +#include "QtGui" + +#include + +#include +#include +#include +#include + +#include "fault.h" +#include "contour.h" + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; + QPainterPath rectPath; + QPainterPath outline; + QPainter painter; + int x; + int y; + double scalex; + double scaley; + Contour c; + bool isPressed; + int iPoint; + QVectorvf; + Fault f; + + + // QWidget interface +protected: + void mouseReleaseEvent(QMouseEvent *); + void mouseMoveEvent(QMouseEvent *); + void paintEvent(QPaintEvent *event); + void mousePressEvent(QMouseEvent *event); +}; + +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..cc4f0bd --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,73 @@ + + + MainWindow + + + + 0 + 0 + 560 + 482 + + + + MainWindow + + + + + + 220 + 10 + 79 + 18 + + + + 断层 + + + true + + + false + + + + + + 370 + 10 + 79 + 18 + + + + 轮廓 + + + + + + + TopToolBarArea + + + false + + + + + + 0 + 0 + 560 + 26 + + + + + + + + diff --git a/oilpool.pro b/oilpool.pro new file mode 100644 index 0000000..37cfce2 --- /dev/null +++ b/oilpool.pro @@ -0,0 +1,41 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2024-09-15T14:22:53 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = oilpool +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which as been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + scalegraphicsview.cpp + +HEADERS += \ + mainwindow.h \ + basicgeometry.h \ + basicgeometryoperator.h \ + fault.h \ + aoi.h \ + contour.h \ + scalegraphicsview.h + +FORMS += \ + mainwindow.ui