1 /*****************************************************************************
4 ** Name: graph3dview.cpp
5 ** Purpose: 3d graph view classes
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: Jan 2001
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
26 #include "wx/wxprec.h"
35 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
39 #include "wx/glcanvas.h"
49 #include "dlgprojections.h"
50 #include "dlgreconstruct.h"
51 #include "backprojectors.h"
52 #include "reconstruct.h"
55 #if defined(MSVC) || HAVE_SSTREAM
58 #include <sstream_subst>
62 Graph3dFileView::intensityToColor (double dIntensity, GLfloat* vecColor)
64 if (dIntensity < 0 || dIntensity > 1) {
65 vecColor[0] = vecColor[1] = vecColor[2] = 1;
69 float fRange = dIntensity * 5;
70 int iRange = static_cast<int>(floor (fRange));
71 float fFrac = fRange - iRange;
73 // Rainbow: Purple->Blue->Cyan->Green->Yellow->Red = (1,0,1)-(0,0,1)-(0,1,1)-(0,1,0)-(1,1,0)-(1,0,0)
76 vecColor[0] = 1.0f - fFrac; vecColor[1] = 0.0f; vecColor[2] = 1.0f;
79 vecColor[0] = 0.0f; vecColor[1] = fFrac; vecColor[2] = 1.0f;
82 vecColor[0] = 0.0f; vecColor[1] = 1.0f; vecColor[2] = 1.0f - fFrac;
85 vecColor[0] = fFrac; vecColor[1] = 1.0f; vecColor[2] = 0.0f;
88 vecColor[0] = 1.0f; vecColor[1] = 1.0f - fFrac; vecColor[2] = 0.0f;
91 vecColor[0] = 1.0f; vecColor[1] = 0.0f; vecColor[2] = 0.0f;
96 //***********************************************************************
97 // Function: CalculateVectorNormal
99 // Purpose: Given three points of a 3D plane, this function calculates
100 // the normal vector of that plane.
103 // fVert1[] == array for 1st point (3 elements are x, y, and z).
104 // fVert2[] == array for 2nd point (3 elements are x, y, and z).
105 // fVert3[] == array for 3rd point (3 elements are x, y, and z).
108 // fNormalX == X vector for the normal vector
109 // fNormalY == Y vector for the normal vector
110 // fNormalZ == Z vector for the normal vector
111 //*************************************************************************
115 CalculateVectorNormal (T fVert1[], T fVert2[], T fVert3[], T *fNormalX, T *fNormalY, T *fNormalZ)
117 T Qx = fVert2[0] - fVert1[0];
118 T Qy = fVert2[1] - fVert1[1];
119 T Qz = fVert2[2] - fVert1[2];
120 T Px = fVert3[0] - fVert1[0];
121 T Py = fVert3[1] - fVert1[1];
122 T Pz = fVert3[2] - fVert1[2];
124 *fNormalX = Py*Qz - Pz*Qy;
125 *fNormalY = Pz*Qx - Px*Qz;
126 *fNormalZ = Px*Qy - Py*Qx;
129 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileView, wxView)
131 BEGIN_EVENT_TABLE(Graph3dFileView, wxView)
132 EVT_MENU(IFMENU_FILE_PROPERTIES, Graph3dFileView::OnProperties)
133 EVT_MENU(GRAPH3D_VIEW_LIGHTING, Graph3dFileView::OnLighting)
134 EVT_MENU(GRAPH3D_VIEW_COLOR, Graph3dFileView::OnColor)
135 EVT_MENU(GRAPH3D_VIEW_SMOOTH, Graph3dFileView::OnSmooth)
136 EVT_MENU(GRAPH3D_VIEW_WIREFRAME, Graph3dFileView::OnWireframe)
137 EVT_MENU(GRAPH3D_VIEW_SCALE_MINMAX, Graph3dFileView::OnScaleSet)
138 EVT_MENU(GRAPH3D_VIEW_SCALE_AUTO, Graph3dFileView::OnScaleAuto)
139 EVT_MENU(GRAPH3D_VIEW_SCALE_FULL, Graph3dFileView::OnScaleFull)
142 Graph3dFileView::Graph3dFileView ()
143 : m_pFileMenu(NULL), m_pViewMenu(NULL), m_pStatusBar(NULL), m_pCanvas(NULL),
144 m_dXRotate(-180), m_dYRotate(-210), m_dZRotate(195),
145 m_bDoubleBuffer(true), m_bSmooth(true), m_bWireframe(false),
146 m_bLighting(false), m_bColor(true),
147 m_bColorScaleMinSet(false), m_bColorScaleMaxSet(false),
148 m_pGLContext(NULL), m_pFrame(NULL)
152 Graph3dFileView::~Graph3dFileView()
154 GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
155 GetDocumentManager()->ActivateView(this, false);
161 Graph3dFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
163 m_pFrame = CreateChildFrame(doc, this);
166 m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
167 m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
168 m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
169 m_pViewMenu->Check (GRAPH3D_VIEW_WIREFRAME, m_bWireframe);
171 m_pFrame->SetFocus();
172 m_pFrame->Show(true);
176 m_pCanvas = CreateCanvas (m_pFrame);
177 m_pGLContext = new wxGLContext (m_pCanvas);
178 m_pFrame->SetClientSize (m_pCanvas->GetBestSize());
179 m_pCanvas->SetClientSize (m_pCanvas->GetBestSize());
180 m_pFrame->SetTitle(doc->GetFilename());
182 m_pCanvas->SetCurrent(*m_pGLContext);
184 m_pCanvas->SwapBuffers();
191 Graph3dFileView::CreateCanvas (wxFrame* parent)
193 Graph3dFileCanvas* pCanvas =
194 new Graph3dFileCanvas (this, parent, NULL,
195 wxDefaultPosition, wxDefaultSize,
196 wxFULL_REPAINT_ON_RESIZE);
198 pCanvas->SetBackgroundColour(*wxWHITE);
199 pCanvas->ClearBackground();
206 Graph3dFileView::DrawSurface()
212 glShadeModel (GL_SMOOTH);
214 glShadeModel (GL_FLAT);
218 glEnable (GL_LIGHTING);
220 glDisable (GL_LIGHTING);
223 unsigned int nx = GetDocument()->nx();
224 unsigned int ny = GetDocument()->ny();
226 glRotated (m_dZRotate, 0.0, 1.0, 0.0);
227 glRotated (m_dXRotate, 0.0, 0.0, 1.0);
228 glRotated (m_dYRotate, 1.0, 0.0, 0.0);
229 glTranslated (-static_cast<double>(nx - 1) / 2, 0.0, -static_cast<double>(ny - 1) / 2);
234 glColor3f (1.0f, 1.0f, 1.0f);
235 glPolygonOffset (1.0f, 1.0f);
236 glEnable (GL_POLYGON_OFFSET_FILL);
237 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
238 glCallList (DISPLAYLIST_COLOR);
240 glEnable (GL_DEPTH_TEST);
241 glColor3f (1.0f, 1.0f, 1.0f);
242 glPolygonOffset (0.0f, 0.0f);
243 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
244 glCallList (DISPLAYLIST_NO_COLOR);
247 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
249 glColor3f (1.0f, 1.0f, 1.0f);
250 glCallList (DISPLAYLIST_NO_COLOR);
252 glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
253 glEnable (GL_COLOR_MATERIAL);
254 glShadeModel (GL_FLAT);
255 glCallList (DISPLAYLIST_COLOR);
262 Graph3dFileView::CreateDisplayList()
267 unsigned int nx = GetDocument()->nx();
268 unsigned int ny = GetDocument()->ny();
269 const ImageFileArrayConst v = GetDocument()->getArray();
270 if (nx == 0 || ny == 0 || ! v)
273 glNewList (DISPLAYLIST_COLOR, GL_COMPILE);
275 double dMin = m_dColorScaleMin;
276 double dIntensityScale = m_dColorScaleMax - m_dColorScaleMin;
277 double actOffset = m_dGraphMin;
278 double actScale = 0.4 * sqrt(nx*nx+ny*ny) / (m_dGraphMax - m_dGraphMin);
279 double dXOffset = -(static_cast<double>(nx) - 1) / 2.;
280 double dYOffset = -(static_cast<double>(ny) - 1) / 2.;
284 double dXPos = -dXOffset;
286 for (ix = 0; ix < nx - 1; ix++, dXPos++) {
287 double dYPos = -dYOffset;
288 glBegin(GL_QUAD_STRIP);
289 double p1[3], p2[3], p3[3], n1[3];
290 p1[0] = dXPos; p1[1] = actScale * (v[ix][0] + actOffset); p1[2] = dYPos;
291 p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][0] + actOffset); p2[2] = dYPos;
292 p3[0] = dXPos; p3[1] = actScale * (v[ix][1] + actOffset); p3[2] = dYPos + 1;
293 CalculateVectorNormal<double> (p1, p2, p3, &n1[0], &n1[1], &n1[2]);
295 double dIntensity1 = 0., dIntensity2 = 0.;
297 dIntensity1 = (v[ix][0] - dMin) / dIntensityScale;
298 dIntensity2 = (v[ix+1][0] - dMin) / dIntensityScale;
302 intensityToColor (dIntensity1, vecColor);
303 glColor3fv (vecColor);
305 glVertex3dv (p1); glNormal3dv (n1);
307 intensityToColor (dIntensity2, vecColor);
308 glColor3fv (vecColor);
310 glVertex3dv (p2); glNormal3dv (n1);
313 lastP[0] = ix; lastP[1] = actScale * (v[ix][0] + actOffset); lastP[2] = 0;
314 for (unsigned int iy = 1; iy < ny - 1; iy++, dYPos++) {
315 p1[0] = dXPos; p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = dYPos;
316 p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = dYPos;
317 CalculateVectorNormal (p1, p2, lastP, &n1[0], &n1[1], &n1[2]);
318 lastP[0] = p1[0]; lastP[1] = p1[1]; lastP[2] = p1[2];
320 dIntensity1 = (v[ix][iy] - dMin) / dIntensityScale;
321 dIntensity2 = (v[ix+1][iy] - dMin) / dIntensityScale;
322 intensityToColor (dIntensity1, vecColor);
323 glColor3fv (vecColor);
325 glVertex3dv (p1); glNormal3dv (n1);
327 intensityToColor (dIntensity2, vecColor);
328 glColor3fv (vecColor);
330 glVertex3dv (p2); glNormal3dv (n1);
332 glEnd(); // QUAD_STRIP
337 glNewList (DISPLAYLIST_NO_COLOR, GL_COMPILE);
339 for (ix = 0; ix < nx - 1; ix++, dXPos++) {
340 double dYPos = -dYOffset;
341 glBegin(GL_QUAD_STRIP);
342 double p1[3], p2[3], p3[3], n1[3];
343 p1[0] = dXPos; p1[1] = actScale * (v[ix][0] + actOffset); p1[2] = dYPos;
344 p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][0] + actOffset); p2[2] = dYPos;
345 p3[0] = dXPos; p3[1] = actScale * (v[ix][1] + actOffset); p3[2] = dYPos + 1;
346 CalculateVectorNormal<double> (p1, p2, p3, &n1[0], &n1[1], &n1[2]);
353 lastP[0] = ix; lastP[1] = actScale * (v[ix][0] + actOffset); lastP[2] = 0;
354 for (unsigned int iy = 1; iy < ny - 1; iy++, dYPos++) {
355 p1[0] = dXPos; p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = dYPos;
356 p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = dYPos;
357 CalculateVectorNormal (p1, p2, lastP, &n1[0], &n1[1], &n1[2]);
358 lastP[0] = p1[0]; lastP[1] = p1[1]; lastP[2] = p1[2];
359 glVertex3dv (p1); glNormal3dv (n1);
360 glVertex3dv (p2); glNormal3dv (n1);
362 glEnd(); // QUAD_STRIP
369 Graph3dFileView::OnProperties (wxCommandEvent& event)
372 *theApp->getLog() << _T(">>>>\n") << os << _T("<<<<\n");
373 wxMessageDialog dialogMsg (getFrameForChild(), os,
374 _T("Imagefile Properties"),
375 wxOK | wxICON_INFORMATION);
376 dialogMsg.ShowModal();
380 Graph3dFileView::OnLighting (wxCommandEvent& event)
382 m_bLighting = ! m_bLighting;
383 m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
384 m_pCanvas->Refresh();
388 Graph3dFileView::OnWireframe (wxCommandEvent& event)
390 m_bWireframe = ! m_bWireframe;
391 m_pViewMenu->Check (GRAPH3D_VIEW_WIREFRAME, m_bWireframe);
392 m_pCanvas->Refresh();
396 Graph3dFileView::OnColor (wxCommandEvent& event)
398 m_bColor = ! m_bColor;
399 m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
400 m_pCanvas->Refresh();
404 Graph3dFileView::OnSmooth (wxCommandEvent& event)
406 m_bSmooth = ! m_bSmooth;
407 m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
408 m_pCanvas->Refresh();
414 Graph3dFileView::OnDraw (wxDC* dc)
417 m_pCanvas->SetCurrent(*m_pGLContext);
419 *theApp->getLog() << _T("Drawing 3d surface\n");
422 m_pCanvas->SwapBuffers();
427 os << _T("Xangle=") << m_dXRotate << _T(", Yangle=") << m_dYRotate
428 << _T(", Zangle=") << m_dZRotate;
429 m_pStatusBar->SetStatusText (os);
435 Graph3dFileView::Draw ()
437 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
446 Graph3dFileView::InitMaterials()
450 int nx = GetDocument()->nx();
451 int ny = GetDocument()->ny();
454 static float position0[] = {nx/2, ny*2, -ny*2, 0.0f,};
455 static float ambient0[] = {.1f, .1f, .1f, 1.0f};
456 static float diffuse0[] = {1.0f, 1.0f, 1.0f, 1.0f};
457 static float position1[] = {-nx/2, -ny*2, -ny*2, 0.0f,};
458 static float ambient1[] = {.1f, .1f, .1f, .1f};
459 static float diffuse1[] = {1.0f, 1.0f, 1.0f, 1.0f};
460 // static float position0[] = {0.0f, 0.0f, 20.0f, 0.0f};
461 // static float position1[] = {0.0f, 0.0f, -20.0f, 0.0f};
462 static float front_mat_shininess[] = {60.0f};
463 static float front_mat_specular[] = {0.2f, 0.2f, 0.2f, 1.0f};
464 static float front_mat_diffuse[] = {0.3f, 0.3f, 0.3f, 1.0f};
465 static float back_mat_shininess[] = {10.0f};
466 static float back_mat_specular[] = {0.1f, 0.1f, 0.1f, 1.0f};
467 static float back_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
468 static float lmodel_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
469 static float lmodel_twoside[] = {GL_TRUE};
471 //glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
472 glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
473 glEnable(GL_NORMALIZE);
475 glLightfv (GL_LIGHT0, GL_AMBIENT, ambient0);
476 glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse0);
477 glLightfv (GL_LIGHT0, GL_POSITION, position0);
478 glEnable (GL_LIGHT0);
480 glLightfv (GL_LIGHT1, GL_AMBIENT, ambient1);
481 glLightfv (GL_LIGHT1, GL_DIFFUSE, diffuse1);
482 glLightfv (GL_LIGHT1, GL_POSITION, position1);
483 glEnable (GL_LIGHT1);
485 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
486 glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
488 glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
489 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
490 glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
491 glMaterialfv (GL_BACK, GL_SHININESS, back_mat_shininess);
492 glMaterialfv (GL_BACK, GL_SPECULAR, back_mat_specular);
493 glMaterialfv (GL_BACK, GL_DIFFUSE, back_mat_diffuse);
495 glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
496 glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
497 glEnable(GL_COLOR_MATERIAL);
499 GLfloat impLPos[] = {1.0f, 1.0f, 1.0f, 0.0f};
501 GLfloat defaultLightAmb [] = {.2f, .2f, .2f, 1.0f};
502 GLfloat defaultLightDiff [] = {.2f, .2f, .2f, 1.0f};
503 GLfloat defaultLightSpec [] = { .3f, .3f, .3f, 1.0f};
505 GLfloat defaultGlobalAmb [] = {.3f, .3f, .3f, 1.0f};
506 GLfloat defaultGlobalDiff[] = {.3f, .3f, .3f, 1.0f};
508 GLfloat defaultMatShine[] = { 30.0f };
509 GLfloat defaultMatSpec[] = { .4f, .4f, .4f, 1.0f};
510 GLfloat defaultMatAmb[] = { .3f, .3f, .3f, 1.0f};
511 GLfloat defaultMatDiff[] = { .5f, .5f, .5f, 1.0f};
513 GLfloat brassMatAmb[] = { .33f, .22f, .03f, 1.0f};
514 GLfloat brassMatDiff[] = { .78f, .57f, .11f, 1.0f};
515 GLfloat brassMatSpec[] = { .99f, .91f, .81f, 1.0f};
516 GLfloat brassMatShine[] = { 27.8f };
518 GLfloat emeraldMatAmb[] = { .02f1, .1745f , .021f, 1.0f };
519 GLfloat emeraldMatDiff[] = { .075f, .6142f , .075f, 1.0f };
520 GLfloat emeraldMatSpec[] = { .633f, .7278f , .633f, 1.0f };
521 GLfloat emeraldMatShine[] = { 76.8f };
523 GLfloat slateMatAmb[] = { .02f, .02f , .02f, 1.0f };
524 GLfloat slateMatDiff[] = { .02f, .01f , .01f, 1.0f };
525 GLfloat slateMatSpec[] = { .4f, .4f , .4f , 1.0f };
526 GLfloat slateMatShine[] = { .768f };
528 // double opnX = nx, opnY = ny, opnZ = z;
529 // eyeX = 1; eyeY = 1, eyeZ = 1;
531 impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
533 //impGraphicsFlag = IMP__3D;
535 // glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM);
536 // glutInitWindowSize (IMP_WIN_X, IMP_WIN_Y);
537 // glutInitWindowPosition (100, 100);
538 // glutCreateWindow ("- imp3D graphics -" );
540 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
542 glShadeModel (GL_SMOOTH);
543 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
544 glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
545 glEnable(GL_NORMALIZE);
548 glEnable(GL_DEPTH_TEST);
550 glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
551 glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
552 glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
554 glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
555 glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
556 glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
558 glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
559 glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
560 glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
562 glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
563 glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
564 glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
566 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT , defaultMatAmb);
567 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE , defaultMatDiff);
568 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR , defaultMatSpec);
569 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, defaultMatShine);
571 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
573 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
575 glEnable(GL_COLOR_MATERIAL);
577 glEnable(GL_LIGHTING);
587 Graph3dFileView::InitGL ()
589 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
591 glDisable (GL_CULL_FACE);
592 glEnable (GL_DEPTH_TEST);
597 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
602 unsigned int nx = GetDocument()->nx();
603 unsigned int ny = GetDocument()->ny();
604 const ImageFileArrayConst v = GetDocument()->getArray();
605 if (v != NULL && nx != 0 && ny != 0) {
606 double min = v[0][0];
608 for (unsigned int ix = 0; ix < nx; ix++)
609 for (unsigned int iy = 0; iy < ny; iy++) {
610 double dVal = v[ix][iy];
618 if (! m_bColorScaleMinSet)
619 m_dColorScaleMin = min;
620 if (! m_bColorScaleMaxSet)
621 m_dColorScaleMax = max;
623 double dRadius = maxValue<int> (nx, ny) * SQRT2 / 2;
626 m_pCanvas->SetCurrent(*m_pGLContext);
628 glMatrixMode(GL_PROJECTION);
630 glOrtho (-dRadius, dRadius, -dRadius, dRadius, dRadius*5, -dRadius*5);
632 glMatrixMode(GL_MODELVIEW);
635 GLfloat eyep[3], lookp[3], up[3];
636 eyep[0] = -nx/2; eyep[1] = 0; eyep[2] = -ny/2;
637 lookp[0] = 0; lookp[1] = 0, lookp[2] = 0;
638 up[0] = 0; up[1] = 1; up[2] = 0;
639 gluLookAt (eyep[0], eyep[1], eyep[2], lookp[0], lookp[1], lookp[2], up[0], up[1], up[2]);
645 m_pCanvas->SwapBuffers();
646 m_pCanvas->Refresh();
651 Graph3dFileView::OnClose (bool deleteWindow)
653 if (! GetDocument() || ! GetDocument()->Close())
658 m_pCanvas->setView(NULL);
661 wxString s(theApp->GetAppName());
663 m_pFrame->SetTitle(s);
670 if (GetDocument() && GetDocument()->getBadFileOpen())
671 ::wxYield(); // wxWindows bug workaround
678 Graph3dFileView::setInitialClientSize ()
680 if (m_pFrame && m_pCanvas) {
681 wxSize bestSize = m_pCanvas->GetBestSize();
683 m_pFrame->SetClientSize (bestSize);
684 m_pFrame->Show (true);
685 m_pFrame->SetFocus();
690 Graph3dFileView::OnScaleAuto (wxCommandEvent& event)
693 const ImageFile& rIF = GetDocument()->getImageFile();
694 double min, max, mean, mode, median, stddev;
695 rIF.statistics(min, max, mean, mode, median, stddev);
696 DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
697 int iRetVal = dialogAutoScale.ShowModal();
698 if (iRetVal == wxID_OK) {
699 m_bMinSpecified = true;
700 m_bMaxSpecified = true;
702 if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
705 m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
706 OnUpdate (this, NULL);
713 Graph3dFileView::OnScaleSet (wxCommandEvent& event)
718 unsigned int nx = GetDocument()->nx();
719 unsigned int ny = GetDocument()->ny();
720 const ImageFileArrayConst v = GetDocument()->getArray();
721 double dMin = 0., dMax = 0.;
722 if (! m_bColorScaleMinSet && ! m_bColorScaleMaxSet) {
723 dMax = dMin = v[0][0];
724 for (unsigned ix = 0; ix < nx; ix++)
725 for (unsigned int iy = 0; iy < ny; iy++)
726 if (v[ix][iy] < dMin)
728 else if (v[ix][iy] > dMax)
731 if (m_bColorScaleMinSet)
732 dMin = m_dColorScaleMin;
733 if (m_bColorScaleMaxSet)
734 dMax = m_dColorScaleMax;
736 DialogGetMinMax dialogMinMax (getFrameForChild(), _T("Set Color Scale Minimum & Maximum"), dMin, dMax);
737 int retVal = dialogMinMax.ShowModal();
738 if (retVal == wxID_OK) {
739 m_bColorScaleMinSet = true;
740 m_bColorScaleMaxSet = true;
741 m_dColorScaleMin = dialogMinMax.getMinimum();
742 m_dColorScaleMax = dialogMinMax.getMaximum();
743 OnUpdate (this, NULL);
748 Graph3dFileView::OnScaleFull (wxCommandEvent& event)
750 if (m_bColorScaleMinSet || m_bColorScaleMaxSet) {
751 m_bColorScaleMinSet = false;
752 m_bColorScaleMaxSet = false;
753 OnUpdate (this, NULL);
762 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
765 wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, _T("Graph3dFile Frame"), wxPoint(-1,-1), wxSize(-1,-1), wxDEFAULT_FRAME_STYLE);
767 wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, _T("Graph3dFile Frame"), wxPoint(-1,-1), wxSize(-1,-1), wxDEFAULT_FRAME_STYLE);
769 theApp->setIconForFrame (subframe);
771 // status bar text not showing tested with enlightenment. disabling for now...
773 m_pStatusBar = new wxStatusBar (subframe, -1);
774 subframe->SetStatusBar (m_pStatusBar);
775 m_pStatusBar->Show(true);
778 m_pFileMenu = new wxMenu;
780 m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, _T("Cr&eate Phantom...\tCtrl-P"));
781 m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, _T("Create &Filter...\tCtrl-F"));
782 m_pFileMenu->Append(wxID_OPEN, _T("&Open...\tCtrl-O"));
783 m_pFileMenu->Append(wxID_CLOSE, _T("&Close\tCtrl-W"));
785 m_pFileMenu->AppendSeparator();
786 m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, _T("P&roperties"));
788 m_pFileMenu->AppendSeparator();
789 m_pFileMenu->Append(wxID_PRINT, _T("&Print..."));
790 m_pFileMenu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
791 m_pFileMenu->Append(wxID_PREVIEW, _T("Print Preview"));
792 m_pFileMenu->AppendSeparator();
793 m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, _T("Prefere&nces..."));
794 m_pFileMenu->Append(MAINMENU_FILE_EXIT, _T("E&xit"));
796 GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
797 GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
799 m_pViewMenu = new wxMenu;
800 m_pViewMenu->Append(GRAPH3D_VIEW_WIREFRAME, _T("Wi&reframe\tCtrl-R"), _T(""), true);
801 m_pViewMenu->Append(GRAPH3D_VIEW_SMOOTH, _T("S&mooth\tCtrl-M"), _T(""), true);
802 m_pViewMenu->Append(GRAPH3D_VIEW_COLOR, _T("Co&lor\tCtrl-L"), _T(""), true);
803 m_pViewMenu->Append(GRAPH3D_VIEW_LIGHTING, _T("Li&ghting\tCtrl-G"), _T(""), true);
804 m_pViewMenu->AppendSeparator();
805 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_MINMAX, _T("Color Scale S&et Min/Max...\tCtrl-E"));
806 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_AUTO, _T("Color Scale &Auto...\tCtrl-A"));
807 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_FULL, _T("Color F&ull Scale\tCtrl-U"));
810 wxMenu *help_menu = new wxMenu;
811 help_menu->Append(MAINMENU_HELP_CONTENTS, _T("&Contents\tF1"));
812 help_menu->Append(MAINMENU_HELP_ABOUT, _T("&About"));
814 wxMenuBar *menu_bar = new wxMenuBar;
816 menu_bar->Append(m_pFileMenu, _T("&File"));
817 menu_bar->Append(m_pViewMenu, _T("&View"));
818 menu_bar->Append(help_menu, _T("&Help"));
820 subframe->SetMenuBar(menu_bar);
822 subframe->Centre(wxBOTH);
824 wxAcceleratorEntry accelEntries[7];
825 accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('R'), GRAPH3D_VIEW_WIREFRAME);
826 accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('L'), GRAPH3D_VIEW_COLOR);
827 accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('G'), GRAPH3D_VIEW_LIGHTING);
828 accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), GRAPH3D_VIEW_SMOOTH);
829 accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('E'), GRAPH3D_VIEW_SCALE_MINMAX);
830 accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('A'), GRAPH3D_VIEW_SCALE_AUTO);
831 accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('U'), GRAPH3D_VIEW_SCALE_FULL);
832 wxAcceleratorTable accelTable (7, accelEntries);
833 subframe->SetAcceleratorTable (accelTable);
841 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
842 EVT_PAINT(Graph3dFileCanvas::OnPaint)
843 EVT_SIZE(Graph3dFileCanvas::OnSize)
844 EVT_CHAR(Graph3dFileCanvas::OnChar)
845 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
846 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
850 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent,
853 const wxSize& size, long style)
854 : wxGLCanvas (parent, wxID_ANY, attribList, pos, size, style), m_pView(view)
859 Graph3dFileCanvas::~Graph3dFileCanvas()
864 Graph3dFileCanvas::OnPaint (wxPaintEvent& event)
868 m_pView->OnDraw(& dc);
873 Graph3dFileCanvas::GetBestSize() const
875 return wxSize (400,400);
879 Graph3dFileCanvas::OnSize (wxSizeEvent& event)
882 // if (!GetContext()) return;
886 GetClientSize (&width, &height);
887 Reshape (width, height); // Crash
891 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
896 wxCommandEvent dummyEvent;
897 switch (event.GetKeyCode()) {
899 m_pView->m_dZRotate += 15.0;
903 m_pView->m_dZRotate -= 15.0;
907 m_pView->m_dXRotate += 15.0;
911 m_pView->m_dXRotate -= 15.0;
915 m_pView->m_dYRotate += 15.0;
919 m_pView->m_dYRotate -= 15.0;
923 m_pView->OnWireframe (dummyEvent);
926 m_pView->OnSmooth (dummyEvent);
929 m_pView->OnLighting (dummyEvent);
932 m_pView->OnColor (dummyEvent);
941 Graph3dFileCanvas::Reshape (int width, int height)
943 SetCurrent(*m_pView->m_pGLContext);
944 glViewport (0, 0, (GLint)width, (GLint)height);
950 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
952 static int dragging = 0;
953 static float last_x, last_y;
958 if(event.LeftIsDown()) {
962 m_pView->m_dXRotate -= (event.GetY() - last_y)*1.0;
963 m_pView->m_dZRotate += (event.GetX() - last_x)*1.0;
966 last_x = event.GetX();
967 last_y = event.GetY();
973 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
975 // Do nothing: avoid flashing.
979 #endif // wxUSE_GLCANVAS