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-2001 Kevin Rosenberg
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
28 #include "wx/wxprec.h"
37 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
41 #include "wx/glcanvas.h"
51 #include "dlgprojections.h"
52 #include "dlgreconstruct.h"
53 #include "backprojectors.h"
54 #include "reconstruct.h"
57 #if defined(MSVC) || HAVE_SSTREAM
60 #include <sstream_subst>
64 Graph3dFileView::intensityToColor (double dIntensity, GLfloat* vecColor)
66 if (dIntensity < 0 || dIntensity > 1) {
67 vecColor[0] = vecColor[1] = vecColor[2] = 1;
71 float fRange = dIntensity * 5;
72 int iRange = static_cast<int>(floor (fRange));
73 float fFrac = fRange - iRange;
75 // 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)
78 vecColor[0] = 1.0f - fFrac; vecColor[1] = 0.0f; vecColor[2] = 1.0f;
81 vecColor[0] = 0.0f; vecColor[1] = fFrac; vecColor[2] = 1.0f;
84 vecColor[0] = 0.0f; vecColor[1] = 1.0f; vecColor[2] = 1.0f - fFrac;
87 vecColor[0] = fFrac; vecColor[1] = 1.0f; vecColor[2] = 0.0f;
90 vecColor[0] = 1.0f; vecColor[1] = 1.0f - fFrac; vecColor[2] = 0.0f;
93 vecColor[0] = 1.0f; vecColor[1] = 0.0f; vecColor[2] = 0.0f;
98 //***********************************************************************
99 // Function: CalculateVectorNormal
101 // Purpose: Given three points of a 3D plane, this function calculates
102 // the normal vector of that plane.
105 // fVert1[] == array for 1st point (3 elements are x, y, and z).
106 // fVert2[] == array for 2nd point (3 elements are x, y, and z).
107 // fVert3[] == array for 3rd point (3 elements are x, y, and z).
110 // fNormalX == X vector for the normal vector
111 // fNormalY == Y vector for the normal vector
112 // fNormalZ == Z vector for the normal vector
113 //*************************************************************************
117 CalculateVectorNormal (T fVert1[], T fVert2[], T fVert3[], T *fNormalX, T *fNormalY, T *fNormalZ)
119 T Qx = fVert2[0] - fVert1[0];
120 T Qy = fVert2[1] - fVert1[1];
121 T Qz = fVert2[2] - fVert1[2];
122 T Px = fVert3[0] - fVert1[0];
123 T Py = fVert3[1] - fVert1[1];
124 T Pz = fVert3[2] - fVert1[2];
126 *fNormalX = Py*Qz - Pz*Qy;
127 *fNormalY = Pz*Qx - Px*Qz;
128 *fNormalZ = Px*Qy - Py*Qx;
131 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileView, wxView)
133 BEGIN_EVENT_TABLE(Graph3dFileView, wxView)
134 EVT_MENU(IFMENU_FILE_PROPERTIES, Graph3dFileView::OnProperties)
135 EVT_MENU(GRAPH3D_VIEW_LIGHTING, Graph3dFileView::OnLighting)
136 EVT_MENU(GRAPH3D_VIEW_COLOR, Graph3dFileView::OnColor)
137 EVT_MENU(GRAPH3D_VIEW_SMOOTH, Graph3dFileView::OnSmooth)
138 EVT_MENU(GRAPH3D_VIEW_WIREFRAME, Graph3dFileView::OnWireframe)
139 EVT_MENU(GRAPH3D_VIEW_SCALE_MINMAX, Graph3dFileView::OnScaleSet)
140 EVT_MENU(GRAPH3D_VIEW_SCALE_AUTO, Graph3dFileView::OnScaleAuto)
141 EVT_MENU(GRAPH3D_VIEW_SCALE_FULL, Graph3dFileView::OnScaleFull)
144 Graph3dFileView::Graph3dFileView ()
145 : m_pFileMenu(NULL), m_pViewMenu(NULL), m_pStatusBar(NULL), m_pCanvas(NULL),
146 m_dXRotate(-180), m_dYRotate(-210), m_dZRotate(195),
147 m_bDoubleBuffer(true), m_bSmooth(true), m_bWireframe(false),
148 m_bLighting(false), m_bColor(true), m_bUseVertexArrays(false),
149 m_bColorScaleMinSet(false), m_bColorScaleMaxSet(false),
154 Graph3dFileView::~Graph3dFileView()
156 GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
157 GetDocumentManager()->ActivateView(this, false);
161 Graph3dFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
163 m_pFrame = CreateChildFrame(doc, this);
165 m_pCanvas = CreateCanvas (m_pFrame);
167 m_pFrame->SetClientSize (m_pCanvas->GetBestSize());
168 m_pCanvas->SetClientSize (m_pCanvas->GetBestSize());
169 m_pFrame->SetTitle(_T("Graph3dFileView"));
171 m_pCanvas->SetCurrent();
173 m_pCanvas->SwapBuffers();
175 m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
176 m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
177 m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
178 m_pViewMenu->Check (GRAPH3D_VIEW_WIREFRAME, m_bWireframe);
180 m_pFrame->SetFocus();
181 m_pFrame->Show(true);
188 Graph3dFileView::CreateCanvas (wxFrame* parent)
190 Graph3dFileCanvas* pCanvas = new Graph3dFileCanvas (this, parent, wxPoint(-1,-1), wxSize(-1,-1), 0);
192 pCanvas->SetBackgroundColour(*wxWHITE);
193 pCanvas->ClearBackground();
200 Graph3dFileView::DrawSurface()
206 glShadeModel (GL_SMOOTH);
208 glShadeModel (GL_FLAT);
212 glEnable (GL_LIGHTING);
214 glDisable (GL_LIGHTING);
217 unsigned int nx = GetDocument()->nx();
218 unsigned int ny = GetDocument()->ny();
220 glRotated (m_dZRotate, 0.0, 1.0, 0.0);
221 glRotated (m_dXRotate, 0.0, 0.0, 1.0);
222 glRotated (m_dYRotate, 1.0, 0.0, 0.0);
223 glTranslated (-static_cast<double>(nx - 1) / 2, 0.0, -static_cast<double>(ny - 1) / 2);
228 glColor3f (1.0f, 1.0f, 1.0f);
229 glPolygonOffset (1.0f, 1.0f);
230 glEnable (GL_POLYGON_OFFSET_FILL);
231 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
232 glCallList (DISPLAYLIST_COLOR);
234 glEnable (GL_DEPTH_TEST);
235 glColor3f (1.0f, 1.0f, 1.0f);
236 glPolygonOffset (0.0f, 0.0f);
237 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
238 glCallList (DISPLAYLIST_NO_COLOR);
241 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
243 glColor3f (1.0f, 1.0f, 1.0f);
244 glCallList (DISPLAYLIST_NO_COLOR);
246 glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
247 glEnable (GL_COLOR_MATERIAL);
248 glShadeModel (GL_FLAT);
249 glCallList (DISPLAYLIST_COLOR);
256 Graph3dFileView::CreateDisplayList()
261 unsigned int nx = GetDocument()->nx();
262 unsigned int ny = GetDocument()->ny();
263 const ImageFileArrayConst v = GetDocument()->getArray();
264 if (nx == 0 || ny == 0 || ! v)
267 glNewList (DISPLAYLIST_COLOR, GL_COMPILE);
269 double dMin = m_dColorScaleMin;
270 double dIntensityScale = m_dColorScaleMax - m_dColorScaleMin;
271 double actOffset = m_dGraphMin;
272 double actScale = 0.4 * sqrt(nx*nx+ny*ny) / (m_dGraphMax - m_dGraphMin);
273 double dXOffset = -(static_cast<double>(nx) - 1) / 2.;
274 double dYOffset = -(static_cast<double>(ny) - 1) / 2.;
278 double dXPos = -dXOffset;
280 for (ix = 0; ix < nx - 1; ix++, dXPos++) {
281 double dYPos = -dYOffset;
282 glBegin(GL_QUAD_STRIP);
283 double p1[3], p2[3], p3[3], n1[3];
284 p1[0] = dXPos; p1[1] = actScale * (v[ix][0] + actOffset); p1[2] = dYPos;
285 p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][0] + actOffset); p2[2] = dYPos;
286 p3[0] = dXPos; p3[1] = actScale * (v[ix][1] + actOffset); p3[2] = dYPos + 1;
287 CalculateVectorNormal<double> (p1, p2, p3, &n1[0], &n1[1], &n1[2]);
289 double dIntensity1 = 0., dIntensity2 = 0.;
291 dIntensity1 = (v[ix][0] - dMin) / dIntensityScale;
292 dIntensity2 = (v[ix+1][0] - dMin) / dIntensityScale;
296 intensityToColor (dIntensity1, vecColor);
297 glColor3fv (vecColor);
299 glVertex3dv (p1); glNormal3dv (n1);
301 intensityToColor (dIntensity2, vecColor);
302 glColor3fv (vecColor);
304 glVertex3dv (p2); glNormal3dv (n1);
307 lastP[0] = ix; lastP[1] = actScale * (v[ix][0] + actOffset); lastP[2] = 0;
308 for (unsigned int iy = 1; iy < ny - 1; iy++, dYPos++) {
309 p1[0] = dXPos; p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = dYPos;
310 p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = dYPos;
311 CalculateVectorNormal (p1, p2, lastP, &n1[0], &n1[1], &n1[2]);
312 lastP[0] = p1[0]; lastP[1] = p1[1]; lastP[2] = p1[2];
314 dIntensity1 = (v[ix][iy] - dMin) / dIntensityScale;
315 dIntensity2 = (v[ix+1][iy] - dMin) / dIntensityScale;
316 intensityToColor (dIntensity1, vecColor);
317 glColor3fv (vecColor);
319 glVertex3dv (p1); glNormal3dv (n1);
321 intensityToColor (dIntensity2, vecColor);
322 glColor3fv (vecColor);
324 glVertex3dv (p2); glNormal3dv (n1);
326 glEnd(); // QUAD_STRIP
331 glNewList (DISPLAYLIST_NO_COLOR, GL_COMPILE);
333 for (ix = 0; ix < nx - 1; ix++, dXPos++) {
334 double dYPos = -dYOffset;
335 glBegin(GL_QUAD_STRIP);
336 double p1[3], p2[3], p3[3], n1[3];
337 p1[0] = dXPos; p1[1] = actScale * (v[ix][0] + actOffset); p1[2] = dYPos;
338 p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][0] + actOffset); p2[2] = dYPos;
339 p3[0] = dXPos; p3[1] = actScale * (v[ix][1] + actOffset); p3[2] = dYPos + 1;
340 CalculateVectorNormal<double> (p1, p2, p3, &n1[0], &n1[1], &n1[2]);
347 lastP[0] = ix; lastP[1] = actScale * (v[ix][0] + actOffset); lastP[2] = 0;
348 for (unsigned int iy = 1; iy < ny - 1; iy++, dYPos++) {
349 p1[0] = dXPos; p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = dYPos;
350 p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = dYPos;
351 CalculateVectorNormal (p1, p2, lastP, &n1[0], &n1[1], &n1[2]);
352 lastP[0] = p1[0]; lastP[1] = p1[1]; lastP[2] = p1[2];
353 glVertex3dv (p1); glNormal3dv (n1);
354 glVertex3dv (p2); glNormal3dv (n1);
356 glEnd(); // QUAD_STRIP
363 Graph3dFileView::OnProperties (wxCommandEvent& event)
366 *theApp->getLog() << _T(">>>>\n") << os << _T("<<<<\n");
367 wxMessageDialog dialogMsg (getFrameForChild(), os,
368 _T("Imagefile Properties"),
369 wxOK | wxICON_INFORMATION);
370 dialogMsg.ShowModal();
374 Graph3dFileView::OnLighting (wxCommandEvent& event)
376 m_bLighting = ! m_bLighting;
377 m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
378 m_pCanvas->Refresh();
382 Graph3dFileView::OnWireframe (wxCommandEvent& event)
384 m_bWireframe = ! m_bWireframe;
385 m_pViewMenu->Check (GRAPH3D_VIEW_WIREFRAME, m_bWireframe);
386 m_pCanvas->Refresh();
390 Graph3dFileView::OnColor (wxCommandEvent& event)
392 m_bColor = ! m_bColor;
393 m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
394 m_pCanvas->Refresh();
398 Graph3dFileView::OnSmooth (wxCommandEvent& event)
400 m_bSmooth = ! m_bSmooth;
401 m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
402 m_pCanvas->Refresh();
408 Graph3dFileView::OnDraw (wxDC* dc)
411 m_pCanvas->SetCurrent();
413 *theApp->getLog() << _T("Drawing 3d surface\n");
416 m_pCanvas->SwapBuffers();
421 os << _T("Xangle=") << m_dXRotate << _T(", Yangle=") << m_dYRotate
422 << _T(", Zangle=") << m_dZRotate;
423 m_pStatusBar->SetStatusText (os);
429 Graph3dFileView::Draw ()
431 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
440 Graph3dFileView::InitMaterials()
444 int nx = GetDocument()->nx();
445 int ny = GetDocument()->ny();
448 static float position0[] = {nx/2, ny*2, -ny*2, 0.0f,};
449 static float ambient0[] = {.1f, .1f, .1f, 1.0f};
450 static float diffuse0[] = {1.0f, 1.0f, 1.0f, 1.0f};
451 static float position1[] = {-nx/2, -ny*2, -ny*2, 0.0f,};
452 static float ambient1[] = {.1f, .1f, .1f, .1f};
453 static float diffuse1[] = {1.0f, 1.0f, 1.0f, 1.0f};
454 // static float position0[] = {0.0f, 0.0f, 20.0f, 0.0f};
455 // static float position1[] = {0.0f, 0.0f, -20.0f, 0.0f};
456 static float front_mat_shininess[] = {60.0f};
457 static float front_mat_specular[] = {0.2f, 0.2f, 0.2f, 1.0f};
458 static float front_mat_diffuse[] = {0.3f, 0.3f, 0.3f, 1.0f};
459 static float back_mat_shininess[] = {10.0f};
460 static float back_mat_specular[] = {0.1f, 0.1f, 0.1f, 1.0f};
461 static float back_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
462 static float lmodel_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
463 static float lmodel_twoside[] = {GL_TRUE};
465 //glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
466 glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
467 glEnable(GL_NORMALIZE);
469 glLightfv (GL_LIGHT0, GL_AMBIENT, ambient0);
470 glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse0);
471 glLightfv (GL_LIGHT0, GL_POSITION, position0);
472 glEnable (GL_LIGHT0);
474 glLightfv (GL_LIGHT1, GL_AMBIENT, ambient1);
475 glLightfv (GL_LIGHT1, GL_DIFFUSE, diffuse1);
476 glLightfv (GL_LIGHT1, GL_POSITION, position1);
477 glEnable (GL_LIGHT1);
479 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
480 glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
482 glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
483 glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
484 glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
485 glMaterialfv (GL_BACK, GL_SHININESS, back_mat_shininess);
486 glMaterialfv (GL_BACK, GL_SPECULAR, back_mat_specular);
487 glMaterialfv (GL_BACK, GL_DIFFUSE, back_mat_diffuse);
489 glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
490 glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
491 glEnable(GL_COLOR_MATERIAL);
493 GLfloat impLPos[] = {1.0f, 1.0f, 1.0f, 0.0f};
495 GLfloat defaultLightAmb [] = {.2f, .2f, .2f, 1.0f};
496 GLfloat defaultLightDiff [] = {.2f, .2f, .2f, 1.0f};
497 GLfloat defaultLightSpec [] = { .3f, .3f, .3f, 1.0f};
499 GLfloat defaultGlobalAmb [] = {.3f, .3f, .3f, 1.0f};
500 GLfloat defaultGlobalDiff[] = {.3f, .3f, .3f, 1.0f};
502 GLfloat defaultMatShine[] = { 30.0f };
503 GLfloat defaultMatSpec[] = { .4f, .4f, .4f, 1.0f};
504 GLfloat defaultMatAmb[] = { .3f, .3f, .3f, 1.0f};
505 GLfloat defaultMatDiff[] = { .5f, .5f, .5f, 1.0f};
507 GLfloat brassMatAmb[] = { .33f, .22f, .03f, 1.0f};
508 GLfloat brassMatDiff[] = { .78f, .57f, .11f, 1.0f};
509 GLfloat brassMatSpec[] = { .99f, .91f, .81f, 1.0f};
510 GLfloat brassMatShine[] = { 27.8f };
512 GLfloat emeraldMatAmb[] = { .02f1, .1745f , .021f, 1.0f };
513 GLfloat emeraldMatDiff[] = { .075f, .6142f , .075f, 1.0f };
514 GLfloat emeraldMatSpec[] = { .633f, .7278f , .633f, 1.0f };
515 GLfloat emeraldMatShine[] = { 76.8f };
517 GLfloat slateMatAmb[] = { .02f, .02f , .02f, 1.0f };
518 GLfloat slateMatDiff[] = { .02f, .01f , .01f, 1.0f };
519 GLfloat slateMatSpec[] = { .4f, .4f , .4f , 1.0f };
520 GLfloat slateMatShine[] = { .768f };
522 // double opnX = nx, opnY = ny, opnZ = z;
523 // eyeX = 1; eyeY = 1, eyeZ = 1;
525 impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
527 //impGraphicsFlag = IMP__3D;
529 // glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM);
530 // glutInitWindowSize (IMP_WIN_X, IMP_WIN_Y);
531 // glutInitWindowPosition (100, 100);
532 // glutCreateWindow ("- imp3D graphics -" );
534 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
536 glShadeModel (GL_SMOOTH);
537 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
538 glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
539 glEnable(GL_NORMALIZE);
542 glEnable(GL_DEPTH_TEST);
544 glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
545 glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
546 glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
548 glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
549 glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
550 glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
552 glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
553 glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
554 glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
556 glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
557 glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
558 glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
560 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT , defaultMatAmb);
561 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE , defaultMatDiff);
562 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR , defaultMatSpec);
563 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, defaultMatShine);
565 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
567 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
569 glEnable(GL_COLOR_MATERIAL);
571 glEnable(GL_LIGHTING);
581 Graph3dFileView::InitGL ()
583 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
585 glDisable (GL_CULL_FACE);
586 glEnable (GL_DEPTH_TEST);
591 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
596 unsigned int nx = GetDocument()->nx();
597 unsigned int ny = GetDocument()->ny();
598 const ImageFileArrayConst v = GetDocument()->getArray();
599 if (v != NULL && nx != 0 && ny != 0) {
600 double min = v[0][0];
602 for (unsigned int ix = 0; ix < nx; ix++)
603 for (unsigned int iy = 0; iy < ny; iy++) {
604 double dVal = v[ix][iy];
612 if (! m_bColorScaleMinSet)
613 m_dColorScaleMin = min;
614 if (! m_bColorScaleMaxSet)
615 m_dColorScaleMax = max;
617 double dRadius = maxValue<int> (nx, ny) * SQRT2 / 2;
620 m_pCanvas->SetCurrent();
622 glMatrixMode(GL_PROJECTION);
624 glOrtho (-dRadius, dRadius, -dRadius, dRadius, dRadius*5, -dRadius*5);
626 glMatrixMode(GL_MODELVIEW);
629 GLfloat eyep[3], lookp[3], up[3];
630 eyep[0] = -nx/2; eyep[1] = 0; eyep[2] = -ny/2;
631 lookp[0] = 0; lookp[1] = 0, lookp[2] = 0;
632 up[0] = 0; up[1] = 1; up[2] = 0;
633 gluLookAt (eyep[0], eyep[1], eyep[2], lookp[0], lookp[1], lookp[2], up[0], up[1], up[2]);
639 m_pCanvas->SwapBuffers();
640 m_pCanvas->Refresh();
645 Graph3dFileView::OnClose (bool deleteWindow)
647 if (! GetDocument() || ! GetDocument()->Close())
652 m_pCanvas->setView(NULL);
655 wxString s(theApp->GetAppName());
657 m_pFrame->SetTitle(s);
664 if (GetDocument() && GetDocument()->getBadFileOpen())
665 ::wxYield(); // wxWindows bug workaround
672 Graph3dFileView::setInitialClientSize ()
674 if (m_pFrame && m_pCanvas) {
675 wxSize bestSize = m_pCanvas->GetBestSize();
677 m_pFrame->SetClientSize (bestSize);
678 m_pFrame->Show (true);
679 m_pFrame->SetFocus();
684 Graph3dFileView::OnScaleAuto (wxCommandEvent& event)
687 const ImageFile& rIF = GetDocument()->getImageFile();
688 double min, max, mean, mode, median, stddev;
689 rIF.statistics(min, max, mean, mode, median, stddev);
690 DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
691 int iRetVal = dialogAutoScale.ShowModal();
692 if (iRetVal == wxID_OK) {
693 m_bMinSpecified = true;
694 m_bMaxSpecified = true;
696 if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
699 m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
700 OnUpdate (this, NULL);
707 Graph3dFileView::OnScaleSet (wxCommandEvent& event)
712 unsigned int nx = GetDocument()->nx();
713 unsigned int ny = GetDocument()->ny();
714 const ImageFileArrayConst v = GetDocument()->getArray();
715 double dMin = 0., dMax = 0.;
716 if (! m_bColorScaleMinSet && ! m_bColorScaleMaxSet) {
717 dMax = dMin = v[0][0];
718 for (unsigned ix = 0; ix < nx; ix++)
719 for (unsigned int iy = 0; iy < ny; iy++)
720 if (v[ix][iy] < dMin)
722 else if (v[ix][iy] > dMax)
725 if (m_bColorScaleMinSet)
726 dMin = m_dColorScaleMin;
727 if (m_bColorScaleMaxSet)
728 dMax = m_dColorScaleMax;
730 DialogGetMinMax dialogMinMax (getFrameForChild(), _T("Set Color Scale Minimum & Maximum"), dMin, dMax);
731 int retVal = dialogMinMax.ShowModal();
732 if (retVal == wxID_OK) {
733 m_bColorScaleMinSet = true;
734 m_bColorScaleMaxSet = true;
735 m_dColorScaleMin = dialogMinMax.getMinimum();
736 m_dColorScaleMax = dialogMinMax.getMaximum();
737 OnUpdate (this, NULL);
742 Graph3dFileView::OnScaleFull (wxCommandEvent& event)
744 if (m_bColorScaleMinSet || m_bColorScaleMaxSet) {
745 m_bColorScaleMinSet = false;
746 m_bColorScaleMaxSet = false;
747 OnUpdate (this, NULL);
756 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
759 wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, _T("Graph3dFile Frame"), wxPoint(-1,-1), wxSize(-1,-1), wxDEFAULT_FRAME_STYLE);
761 wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, _T("Graph3dFile Frame"), wxPoint(-1,-1), wxSize(-1,-1), wxDEFAULT_FRAME_STYLE);
763 theApp->setIconForFrame (subframe);
765 // status bar text not showing tested with enlightenment. disabling for now...
767 m_pStatusBar = new wxStatusBar (subframe, -1);
768 subframe->SetStatusBar (m_pStatusBar);
769 m_pStatusBar->Show(true);
772 m_pFileMenu = new wxMenu;
774 m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, _T("Cr&eate Phantom...\tCtrl-P"));
775 m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, _T("Create &Filter...\tCtrl-F"));
776 m_pFileMenu->Append(wxID_OPEN, _T("&Open...\tCtrl-O"));
777 m_pFileMenu->Append(wxID_CLOSE, _T("&Close\tCtrl-W"));
779 m_pFileMenu->AppendSeparator();
780 m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, _T("P&roperties"));
782 m_pFileMenu->AppendSeparator();
783 m_pFileMenu->Append(wxID_PRINT, _T("&Print..."));
784 m_pFileMenu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
785 m_pFileMenu->Append(wxID_PREVIEW, _T("Print Preview"));
786 m_pFileMenu->AppendSeparator();
787 m_pFileMenu->Append (MAINMENU_FILE_PREFERENCES, _T("Prefere&nces..."));
788 m_pFileMenu->Append(MAINMENU_FILE_EXIT, _T("E&xit"));
790 GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
791 GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
793 m_pViewMenu = new wxMenu;
794 m_pViewMenu->Append(GRAPH3D_VIEW_WIREFRAME, _T("Wi&reframe\tCtrl-R"), _T(""), true);
795 m_pViewMenu->Append(GRAPH3D_VIEW_SMOOTH, _T("S&mooth\tCtrl-M"), _T(""), true);
796 m_pViewMenu->Append(GRAPH3D_VIEW_COLOR, _T("Co&lor\tCtrl-L"), _T(""), true);
797 m_pViewMenu->Append(GRAPH3D_VIEW_LIGHTING, _T("Li&ghting\tCtrl-G"), _T(""), true);
798 m_pViewMenu->AppendSeparator();
799 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_MINMAX, _T("Color Scale S&et Min/Max...\tCtrl-E"));
800 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_AUTO, _T("Color Scale &Auto...\tCtrl-A"));
801 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_FULL, _T("Color F&ull Scale\tCtrl-U"));
804 wxMenu *help_menu = new wxMenu;
805 help_menu->Append(MAINMENU_HELP_CONTENTS, _T("&Contents\tF1"));
806 help_menu->Append(MAINMENU_HELP_ABOUT, _T("&About"));
808 wxMenuBar *menu_bar = new wxMenuBar;
810 menu_bar->Append(m_pFileMenu, _T("&File"));
811 menu_bar->Append(m_pViewMenu, _T("&View"));
812 menu_bar->Append(help_menu, _T("&Help"));
814 subframe->SetMenuBar(menu_bar);
816 subframe->Centre(wxBOTH);
818 wxAcceleratorEntry accelEntries[7];
819 accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('R'), GRAPH3D_VIEW_WIREFRAME);
820 accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('L'), GRAPH3D_VIEW_COLOR);
821 accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('G'), GRAPH3D_VIEW_LIGHTING);
822 accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), GRAPH3D_VIEW_SMOOTH);
823 accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('E'), GRAPH3D_VIEW_SCALE_MINMAX);
824 accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('A'), GRAPH3D_VIEW_SCALE_AUTO);
825 accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('U'), GRAPH3D_VIEW_SCALE_FULL);
826 wxAcceleratorTable accelTable (7, accelEntries);
827 subframe->SetAcceleratorTable (accelTable);
835 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
836 EVT_PAINT(Graph3dFileCanvas::OnPaint)
837 EVT_SIZE(Graph3dFileCanvas::OnSize)
838 EVT_CHAR(Graph3dFileCanvas::OnChar)
839 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
840 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
845 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent, const wxPoint& pos,
846 const wxSize& size, long style)
847 : wxGLCanvas (parent, -1, pos, size, style), m_pView(view)
849 // parent->Show (true);
851 // Make sure server supports the vertex array extension
852 char* extensions = (char *) glGetString( GL_EXTENSIONS );
853 if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) {
854 m_pView->m_bUseVertexArrays = GL_FALSE;
860 Graph3dFileCanvas::~Graph3dFileCanvas()
865 Graph3dFileCanvas::OnPaint (wxPaintEvent& event)
869 m_pView->OnDraw(& dc);
874 Graph3dFileCanvas::GetBestSize() const
876 return wxSize (400,400);
880 Graph3dFileCanvas::OnSize (wxSizeEvent& event)
883 // if (!GetContext()) return;
887 GetClientSize (&width, &height);
888 Reshape (width, height);
892 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
897 wxCommandEvent dummyEvent;
898 switch (event.GetKeyCode()) {
900 m_pView->m_dZRotate += 15.0;
904 m_pView->m_dZRotate -= 15.0;
908 m_pView->m_dXRotate += 15.0;
912 m_pView->m_dXRotate -= 15.0;
916 m_pView->m_dYRotate += 15.0;
920 m_pView->m_dYRotate -= 15.0;
924 m_pView->OnWireframe (dummyEvent);
927 m_pView->OnSmooth (dummyEvent);
930 m_pView->OnLighting (dummyEvent);
933 m_pView->OnColor (dummyEvent);
942 Graph3dFileCanvas::Reshape (int width, int height)
945 glViewport (0, 0, (GLint)width, (GLint)height);
951 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
953 static int dragging = 0;
954 static float last_x, last_y;
959 if(event.LeftIsDown()) {
963 m_pView->m_dXRotate -= (event.GetY() - last_y)*1.0;
964 m_pView->m_dZRotate += (event.GetX() - last_x)*1.0;
967 last_x = event.GetX();
968 last_y = event.GetY();
974 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
976 // Do nothing: avoid flashing.
980 #endif // wxUSE_GLCANVAS