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

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