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() 函數重繪顯示內容
使用此方法,不但可以繪制動態的三維曲線,還可以繪制靜態的典線;同時可以繪制一條或者多條三維曲線,下面是我的實驗結果: