source: trunk/FACT++/gui/Q3DCameraWidget.cc@ 18486

Last change on this file since 18486 was 15015, checked in by tbretz, 12 years ago
Fixed complete messed includes; moved them where they belong, to the source files where possible.
File size: 8.1 KB
Line 
1/*
2 * Q3DCameraWidget.cc
3 *
4 * Created on: Aug 26, 2011
5 * Author: lyard
6 */
7#include "Q3DCameraWidget.h"
8
9#include <math.h>
10#include <sstream>
11
12#include <GL/glu.h>
13
14#include <QMouseEvent>
15
16 Q3DCameraWidget::Q3DCameraWidget(QWidget* pparent) : BasicGlCamera(pparent),
17 currentLoc()
18 {
19 _data.resize(432000);
20 _colorR.resize(432000);
21 _colorG.resize(432000);
22 _colorB.resize(432000);
23 _x.resize(432000);
24 _y.resize(432000);
25 _z.resize(432000);
26 for (int i=0;i<432000;i++)
27 {
28 _data[i] = 0;
29 _colorR[i] = 0;
30 _colorG[i] = 0;
31 _colorB[i] = 0;
32 _x[i] = 0;
33 _y[i] = 0;
34 _z[i] = 0;
35 }
36 _warningWritten = false;
37 }
38 Q3DCameraWidget::~Q3DCameraWidget()
39 {
40
41 }
42 void Q3DCameraWidget::timedUpdate()
43 {
44 updateGL();
45 }
46
47 int rotation =130;
48 int rotationy = 30;
49 float transZ = 0;
50 void Q3DCameraWidget::mousePressEvent(QMouseEvent* cEvent)
51 {
52
53 if (cEvent->buttons() & Qt::LeftButton)
54 {
55 rotationy = -60 + (cEvent->pos().y()/(float)height())*120.f;
56 rotation = 130 + (cEvent->pos().x()/(float)width())*180.f;
57 }
58 else if (cEvent->buttons() & Qt::RightButton)
59 {
60 if (cEvent->pos().y() > height()/2)
61 transZ -= 0.5;
62 else
63 transZ += 0.5;
64 }
65 updateGL();
66 }
67 void Q3DCameraWidget::mouseDoubleClickEvent(QMouseEvent *cEvent)
68 {
69
70 }
71 void Q3DCameraWidget::mouseMoveEvent(QMouseEvent *cEvent)
72 {
73 if (cEvent->buttons() & Qt::LeftButton) {
74 mousePressEvent(cEvent);
75 }
76
77 }
78 void Q3DCameraWidget::paintGL()
79 {
80 makeCurrent();
81 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
82 glLoadIdentity();
83
84 glTranslatef(-0.0,-0.0, -5);
85 glTranslatef(0,0,(float)(transZ));
86 glRotatef((float)rotationy,1.0,0.0,0.0);
87 glRotatef((float)rotation, 0.0, 1.0, 0.0);
88
89 glColor3f(1.0,0.0,0.0);
90
91 glBegin(GL_TRIANGLES);
92 for (int i=0;i<1439;i++)
93 {
94 for (int j=6;j<250;j++)
95 {
96 //get the 4 vertices that we need for drawing this patch
97 glColor3f(_colorR[i*300+j],_colorG[i*300+j],_colorB[i*300+j]);
98 glVertex3f(_x[i*300+j], _y[i*300+j], _z[i*300+j]);
99 glColor3f(_colorR[i*300+j+1],_colorG[i*300+j+1],_colorB[i*300+j+1]);
100 glVertex3f(_x[i*300+j+1], _y[i*300+j+1], _z[i*300+j+1]);
101 glColor3f(_colorR[(i+1)*300+j],_colorG[(i+1)*300+j],_colorB[(i+1)*300+j]);
102 glVertex3f(_x[(i+1)*300+j], _y[(i+1)*300+j], _z[(i+1)*300+j]);
103
104 glColor3f(_colorR[i*300+j+1],_colorG[i*300+j+1],_colorB[i*300+j+1]);
105 glVertex3f(_x[i*300+j+1], _y[i*300+j+1], _z[i*300+j+1]);
106 glColor3f(_colorR[(i+1)*300+j+1],_colorG[(i+1)*300+j+1],_colorB[(i+1)*300+j+1]);
107 glVertex3f(_x[(i+1)*300+j+1], _y[(i+1)*300+j+1], _z[(i+1)*300+j+1]);
108 glColor3f(_colorR[(i+1)*300+j],_colorG[(i+1)*300+j],_colorB[(i+1)*300+j]);
109 glVertex3f(_x[(i+1)*300+j], _y[(i+1)*300+j], _z[(i+1)*300+j]);
110
111 }
112 }
113 glEnd();
114
115 }
116 void Q3DCameraWidget::calculateColorsAndPositions()
117 {
118 short min = 10000;
119 short max = -10000;
120 for (int k=0;k<1440;k++)
121 for (int j=6;j<251;j++)
122 {
123 int i = k*300+j;
124 if (_data[i] < min)
125 min = _data[i];
126 if (_data[i] > max)
127 max = _data[i];
128 }
129 float span = max - min;
130
131
132 //max should be at one, min at -1
133
134 for (int i=0;i<1440;i++)
135 {
136 for (int j=6;j<251;j++)
137 {
138 _x[i*300+j] = -1 + (2.f*i)/1440.f;
139 _y[i*300+j] = -0.5 + 1.0f*(_data[i*300+j] - min)/span;
140 _z[i*300+j] = -1+(2.f*j)/300.f;
141 float value = (_data[i*300 + j] - min)/span;
142 if (value < 0.33)
143 {
144 _colorR[i*300+j] = 0;
145 _colorG[i*300+j] = 0;
146 _colorB[i*300+j] = value/0.33;
147 }
148 if (value >= 0.33 && value <= 0.66)
149 {
150 _colorR[i*300+j] = 0;
151 _colorG[i*300+j] = (value-0.33)/0.33;
152 _colorB[i*300+j] = 1 - ((value-0.33)/0.33);
153 }
154 if (value > 0.66)
155 {
156 _colorR[i*300+j] = (value-0.66)/0.33;
157 _colorG[i*300+j] = 1 - ((value-0.66)/0.33);
158 _colorB[i*300+j] = 0;
159
160 }
161 }
162 }
163
164
165
166 }
167 void Q3DCameraWidget::setData(float* ddata)
168 {
169 if (!_warningWritten)
170 {
171 _warningWritten = true;
172 cout << "Info : 3D plotter disabled. requires more work so that less than 300 slices per pixel can be loaded" << endl;
173 cout << "Contact Etienne (etienne.lyard@unige.ch) for more information." << endl;
174 }
175 //disabled for now as I am working with 150 slices only
176/* for (int i=0;i<1440;i++)
177 for (int j=0;j<300;j++)
178 _data[i*300+j] = (short)(ddata[i*300 + j]);
179 calculateColorsAndPositions();
180 if (isVisible())
181 updateGL();
182*/
183 }
184 void Q3DCameraWidget::setData(short* ddata)
185 {
186 if (!_warningWritten)
187 {
188 _warningWritten = true;
189 cout << "Info : 3D plotter disabled. requires more work so that less than 300 slices per pixel can be loaded" << endl;
190 cout << "Contact Etienne (etienne.lyard@unige.ch) for more information." << endl;
191 }
192 /* for (int i=0;i<1440;i++)
193 for (int j=0;j<300;j++)
194 _data[i*300+j] = ddata[i* 300 + j];
195 calculateColorsAndPositions();
196 if (isVisible())
197 updateGL();
198*/
199 }
200 void Q3DCameraWidget::drawCameraBody()
201 {
202 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
203
204 GLfloat color[4] = {0.8f, 1.f, 1.f, 1.f};
205 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
206 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 1.0f);
207 gluCylinder( gluNewQuadric(),
208 0.62,
209 0.62,
210 1.83,
211 30,
212 2 );
213 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
214
215
216
217 }
218
219 void Q3DCameraWidget::initializeGL()
220 {
221 qglClearColor(QColor(25,25,38));
222
223 glShadeModel(GL_SMOOTH);
224 glEnable(GL_DEPTH_TEST);
225 glDepthFunc(GL_LESS);
226 glDisable(GL_LIGHTING);
227// glEnable(GL_LIGHTING);
228// glEnable(GL_LIGHT0);
229// glEnable(GL_AUTO_NORMAL);
230 glDisable(GL_CULL_FACE);
231// glCullFace(GL_FRONT);
232
233 glEnable(GL_POLYGON_SMOOTH);
234 glEnable(GL_BLEND);
235 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
236 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
237 }
238 void Q3DCameraWidget::resizeGL(int cWidth, int cHeight)
239 {
240 glViewport(0,0,cWidth, cHeight);
241 glMatrixMode(GL_PROJECTION);
242 glLoadIdentity();
243 GLfloat windowRatio = (float)cWidth/(float)cHeight;
244 if (windowRatio < 1)
245 {
246// windowRatio = 1.0f/windowRatio;
247 gluPerspective(40.f, windowRatio, 1, 100);
248// gluOrtho2D(-viewSize, viewSize, -viewSize*windowRatio, viewSize*windowRatio);
249 pixelSize = 2*viewSize/(float)cWidth;
250 shownSizex = 2*viewSize;
251 shownSizey = 2*viewSize*windowRatio;
252 }
253 else
254 {
255 gluPerspective(40.f, windowRatio,1, 8);
256// gluOrtho2D(-viewSize*windowRatio, viewSize*windowRatio, -viewSize, viewSize);
257 pixelSize = 2*viewSize/(float)cHeight;
258 shownSizex = 2*viewSize*windowRatio;
259 shownSizey = 2*viewSize;
260 }
261 glMatrixMode(GL_MODELVIEW);
262 }
Note: See TracBrowser for help on using the repository browser.