Qt QWT3D 之 三维动态曲线的实现


  QWT3D 和QWT 都是QT的同门师弟,QWT3D主要信赖于QT中的QGLWidget类,可以直接使用opengl 命令,QWT3D的编译比较简单,在此不再赘述,下面展示一下QWT3D自带的例子运行效果:

   

      下面是经典的,牛逼的MESH例子,生成经典的帽子曲面等 

QWT3D例子展示就到此为至吧,不然童鞋又说我喧宾夺主了。下面进入正题。

   2、  上述几个类中都是绘制曲面的例子,都使用到SurfacePlot类,SurfacePlot类应该可以定位于一个曲面绘制功能类,使用该类可以方便绘制曲面。

           为了实现曲线的绘制,

       1)实现派生类Line3D

 1 //////////////////////////////////////////////////////////////
 2 
 3 // Line3D.h
 4 
 5 class QWT3D_EXPORT Line3D: public VertexEnrichment  6 {  7 public:  8 Line3D();  9 Line3D(double thick,bool smooth); 10 Qwt3D::Enrichment * clone() const{ return new Line3D(*this);} 11 
12 void configure(double thick, bool smooth); 13 void drawBegin(); 14 void drawEnd(); 15 virtual void draw(Qwt3D::Triple const&); 16 
17 virtual void draw(); 18 
19 virtual void add(Qwt3D::Triple const & t); 20 virtual void setLineColor(RGBA color); 21 
22 private: 23 bool smooth_; 24 double lineThick; 25 GLboolean oldstate_; 26 
27 std::vector<Qwt3D::Triple> lineData; 28 
29 RGBA rgba; 30 };
 1 //////////////////////////////////////////////////////  2 
 3 //Line3D.cpp
 4 
 5 Qwt3D::Line3D::Line3D()  6 {  7 
 8 
 9 rgba.a = 1; 10 rgba.b = 0.3; 11 rgba.r = 0.6; 12 rgba.g = 1; 13 } 14 Qwt3D::Line3D::Line3D(double thick,bool smooth) 15 { 16 lineThick = thick; 17 smooth_  = smooth; 18 rgba.a = 1; 19 rgba.b = 0.3; 20 rgba.r = 0.6; 21 rgba.g = 1; 22 } 23 
24 void Qwt3D::Line3D::configure(double thick, bool smooth) 25 { 26 lineThick = thick; 27 smooth_  = smooth; 28 
29 } 30 
31 void Qwt3D::Line3D::drawBegin() 32 { 33 setDeviceLineWidth(lineThick); 34 
35 oldstate_ = glIsEnabled(GL_LINE_SMOOTH); 36 if (smooth_) 37 glEnable(GL_LINE_SMOOTH); 38 else
39 glDisable(GL_LINE_SMOOTH); 40 
41 //glPointSize(10);
42 glBegin( GL_LINE_STRIP); 43 
44 } 45 
46 void Qwt3D::Line3D::drawEnd() 47 { 48 glEnd(); 49 
50 
51 if (oldstate_) 52 glEnable(GL_LINE_SMOOTH); 53 else
54 glDisable(GL_LINE_SMOOTH); 55 } 56 
57 
58 void Qwt3D::Line3D::draw(Qwt3D::Triple const& pos) 59 { 60 
61 glColor4d(rgba.r,rgba.g,rgba.b,rgba.a); 62 
63 glVertex3d(pos.x,pos.y,pos.z); 64 
65 } 66 
67 void Qwt3D::Line3D::draw() 68 { 69 for (int i = 0; i < lineData.size(); i ++) 70 { 71 draw(lineData[i]); 72 } 73 } 74 
75 void Qwt3D::Line3D::add(Qwt3D::Triple const & t) 76 { 77 lineData.push_back(t); 78 } 79 
80 void Qwt3D::Line3D::setLineColor(RGBA color) 81 { 82 this->rgba = color; 83 }

类成员变量  lineData 保存三维线的 点集

2)为了让Line3D 被 SurfacePlot 调用,需对SurfacePlot::createEnrichment(Enrichment& p) 扩展,为了兼容QWT3D原来的功能,将

 1 SurfacePlot::createEnrichment(Enrichment& p) 函数实现片断  2 
 3  VertexEnrichment* ve = (VertexEnrichment*)&p;  4   if (actualData_p->datatype == Qwt3D::POLYGON)  5  {  6     for (unsigned i = 0; i != actualDataC_->normals.size(); ++i)  7    ve->draw(actualDataC_->nodes[i]);  8  }  9   else if (actualData_p->datatype == Qwt3D::GRID) 10  { 11     int step = resolution(); 12     for (int i = 0; i <= actualDataG_->columns() - step; i += step) 13       for (int j = 0; j <= actualDataG_->rows() - step; j += step) 14   ve->draw(Triple(actualDataG_->vertices[i][j][0], 15              actualDataG_->vertices[i][j][1], 16                                   actualDataG_->vertices[i][j][2])); 17  } 18 
19 修改为: 20 
21  VertexEnrichment* ve = (VertexEnrichment*)&p; 22   if (actualData_p->datatype == Qwt3D::POLYGON) 23  { 24     for (unsigned i = 0; i != actualDataC_->normals.size(); ++i) 25    ve->draw(actualDataC_->nodes[i]); 26  } 27   else if (actualData_p->datatype == Qwt3D::GRID) 28  { 29     int step = resolution(); 30     for (int i = 0; i <= actualDataG_->columns() - step; i += step) 31       for (int j = 0; j <= actualDataG_->rows() - step; j += step) 32   ve->draw(Triple(actualDataG_->vertices[i][j][0], 33              actualDataG_->vertices[i][j][1], 34                                   actualDataG_->vertices[i][j][2])); 35  } 36   else if (actualData_p->datatype == Qwt3D::LINE3D_STYLE)  /// 新增片断,这样在不暴露Line3D 三维曲线类的点集的情况下,绘制三维曲线
37  { 38  p.draw(); 39   }

3、使用方法  动态绘制三维曲线

       1)定义  

1 Qwt3D::Line3D _l3d; 2 
3  SurfacePlot plot; 4 
5       myLine1 = dynamic_cast<Qwt3D::Line3D *>(plot.addEnrichment(_l3d)); 6 
7         myLine1->configure(3,true); 8 myLine1->setLineColor(Qwt3D::RGBA(1,0,0,1));

 这里对SurfacePlot ::addEnrichment返回的指针说一下,该指针的内容来源于对 _l3d的拷贝,内存由SurfacePlo对象管理,所以千万不要使用

      delete myLine1 ;类似这样的语句对返回的指针释放内存,这样操作除了程序崩溃还是崩溃。大笑 大笑 大笑 

   2) 定时调用 myLine1->add  接口添加了 点数据,然后调用 

      updateData();
     updateGL();  触发   QT  QGLWidget的 pl()  函数重绘显示内容

    使用此方法,不但可以绘制动态的三维曲线,还可以绘制静态的典线;同时可以绘制一条或者多条三维曲线,下面是我的实验结果:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM