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
12 ** $Id: graph3dview.cpp,v 1.10 2001/02/04 21:28:19 kevin Exp $
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 ******************************************************************************/
29 #pragma implementation
33 // For compilers that support precompilation, includes "wx.h".
34 #include "wx/wxprec.h"
47 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
51 #include "wx/glcanvas.h"
61 #include "dlgprojections.h"
62 #include "dlgreconstruct.h"
63 #include "backprojectors.h"
64 #include "reconstruct.h"
67 #if defined(MSVC) || HAVE_SSTREAM
70 #include <sstream_subst>
74 Graph3dFileView::intensityToColor (double dIntensity, GLfloat* vecColor)
76 if (dIntensity < 0 || dIntensity > 1) {
77 vecColor[0] = vecColor[1] = vecColor[2] = 1;
81 float fRange = dIntensity * 5;
82 int iRange = static_cast<int>(floor (fRange));
83 float fFrac = fRange - iRange;
85 // 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)
88 vecColor[0] = 1.0f - fFrac; vecColor[1] = 0.0f; vecColor[2] = 1.0f;
91 vecColor[0] = 0.0f; vecColor[1] = fFrac; vecColor[2] = 1.0f;
94 vecColor[0] = 0.0f; vecColor[1] = 1.0f; vecColor[2] = 1.0f - fFrac;
97 vecColor[0] = fFrac; vecColor[1] = 1.0f; vecColor[2] = 0.0f;
100 vecColor[0] = 1.0f; vecColor[1] = 1.0f - fFrac; vecColor[2] = 0.0f;
103 vecColor[0] = 1.0f; vecColor[1] = 0.0f; vecColor[2] = 0.0f;
108 //***********************************************************************
109 // Function: CalculateVectorNormal
111 // Purpose: Given three points of a 3D plane, this function calculates
112 // the normal vector of that plane.
115 // fVert1[] == array for 1st point (3 elements are x, y, and z).
116 // fVert2[] == array for 2nd point (3 elements are x, y, and z).
117 // fVert3[] == array for 3rd point (3 elements are x, y, and z).
120 // fNormalX == X vector for the normal vector
121 // fNormalY == Y vector for the normal vector
122 // fNormalZ == Z vector for the normal vector
123 //*************************************************************************
126 CalculateVectorNormal (GLfloat fVert1[], GLfloat fVert2[],
127 GLfloat fVert3[], GLfloat *fNormalX,
128 GLfloat *fNormalY, GLfloat *fNormalZ)
130 GLfloat Qx = fVert2[0] - fVert1[0];
131 GLfloat Qy = fVert2[1] - fVert1[1];
132 GLfloat Qz = fVert2[2] - fVert1[2];
133 GLfloat Px = fVert3[0] - fVert1[0];
134 GLfloat Py = fVert3[1] - fVert1[1];
135 GLfloat Pz = fVert3[2] - fVert1[2];
137 *fNormalX = Py*Qz - Pz*Qy;
138 *fNormalY = Pz*Qx - Px*Qz;
139 *fNormalZ = Px*Qy - Py*Qx;
144 CalculateVectorNormal (GLdouble fVert1[], GLdouble fVert2[],
145 GLdouble fVert3[], GLdouble *fNormalX,
146 GLdouble *fNormalY, GLdouble *fNormalZ)
148 GLdouble Qx = fVert2[0] - fVert1[0];
149 GLdouble Qy = fVert2[1] - fVert1[1];
150 GLdouble Qz = fVert2[2] - fVert1[2];
151 GLdouble Px = fVert3[0] - fVert1[0];
152 GLdouble Py = fVert3[1] - fVert1[1];
153 GLdouble Pz = fVert3[2] - fVert1[2];
155 *fNormalX = Py*Qz - Pz*Qy;
156 *fNormalY = Pz*Qx - Px*Qz;
157 *fNormalZ = Px*Qy - Py*Qx;
161 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileView, wxView)
163 BEGIN_EVENT_TABLE(Graph3dFileView, wxView)
164 EVT_MENU(IFMENU_FILE_PROPERTIES, Graph3dFileView::OnProperties)
165 EVT_MENU(GRAPH3D_VIEW_LIGHTING, Graph3dFileView::OnLighting)
166 EVT_MENU(GRAPH3D_VIEW_COLOR, Graph3dFileView::OnColor)
167 EVT_MENU(GRAPH3D_VIEW_SMOOTH, Graph3dFileView::OnSmooth)
168 EVT_MENU(GRAPH3D_VIEW_SURFACE, Graph3dFileView::OnSurface)
169 EVT_MENU(GRAPH3D_VIEW_SCALE_MINMAX, Graph3dFileView::OnScaleSet)
170 EVT_MENU(GRAPH3D_VIEW_SCALE_AUTO, Graph3dFileView::OnScaleAuto)
171 EVT_MENU(GRAPH3D_VIEW_SCALE_FULL, Graph3dFileView::OnScaleFull)
174 Graph3dFileView::Graph3dFileView ()
175 : m_pFileMenu(NULL), m_pViewMenu(NULL)
177 m_bDoubleBuffer = true;
186 m_bColorScaleMinSet = false;
187 m_bColorScaleMaxSet = false;
190 Graph3dFileView::~Graph3dFileView()
192 GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
193 GetDocumentManager()->ActivateView(this, false, true);
197 Graph3dFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
199 m_pFrame = CreateChildFrame(doc, this);
203 m_pFrame->GetClientSize (&width, &height);
204 m_pFrame->SetTitle("Graph3dFileView");
205 m_pCanvas = CreateCanvas (m_pFrame);
207 m_pFrame->Show (true);
208 m_pCanvas->SetCurrent();
212 int x, y; // X requires a forced resize
213 m_pFrame->GetSize(&x, &y);
214 m_pFrame->SetSize(-1, -1, x, y);
215 m_pFrame->SetFocus();
216 m_pFrame->Show(true);
219 m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
220 m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
221 m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
222 m_pViewMenu->Check (GRAPH3D_VIEW_SURFACE, m_bSurface);
227 Graph3dFileView::CreateCanvas (wxFrame* parent)
229 Graph3dFileCanvas* pCanvas;
231 parent->GetClientSize (&width, &height);
234 int *gl_attrib = NULL;
236 int gl_attrib[20] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
237 GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, None };
240 if(! m_bDoubleBuffer) {
246 pCanvas = new Graph3dFileCanvas (this, parent, wxPoint(0, 0), wxSize(200, 200), 0, gl_attrib);
248 pCanvas->SetBackgroundColour(*wxWHITE);
256 Graph3dFileView::DrawSurface()
259 glShadeModel (GL_SMOOTH);
261 glShadeModel (GL_FLAT);
265 glEnable (GL_LIGHTING);
267 glDisable (GL_LIGHTING);
271 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
273 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
280 unsigned int nx = GetDocument()->nx();
281 unsigned int ny = GetDocument()->ny();
282 glRotated (m_dZRotate, 0.0, 1.0, 0.0);
283 glTranslated (-static_cast<double>(nx) / 2, 0.0, -static_cast<double>(ny) / 2);
284 glRotated (m_dYRotate, 1.0, 0.0, 0.0);
285 glRotated (m_dXRotate, 0.0, 0.0, 1.0);
287 glCallList (DISPLAYLIST_SURFACE);
291 Graph3dFileView::CreateDisplayList()
296 unsigned int nx = GetDocument()->nx();
297 unsigned int ny = GetDocument()->ny();
298 const ImageFileArrayConst v = GetDocument()->getArray();
299 if (nx == 0 || ny == 0 || ! v)
302 glNewList (DISPLAYLIST_SURFACE, GL_COMPILE);
304 double dMin = m_dColorScaleMin;
305 double dIntensityScale = m_dColorScaleMax - m_dColorScaleMin;
306 double actOffset = m_dGraphMin;
307 double actScale = 0.4 * sqrt(nx*nx+ny*ny) / (m_dGraphMax - m_dGraphMin);
310 glColor3f (1.0f, 1.0f, 1.0f);
312 for (unsigned ix = 0; ix < nx - 1; ix++) {
313 glBegin(GL_QUAD_STRIP);
314 double p1[3], p2[3], p3[3];
316 p1[0] = ix; p1[1] = actScale * (v[ix][0] + actOffset); p1[2] = 0;
317 p2[0] = ix+1; p2[1] = actScale * (v[ix+1][0] + actOffset); p2[2] = 0;
318 p3[0] = ix; p3[1] = actScale * (v[ix][1] + actOffset); p3[2] = 1;
319 CalculateVectorNormal (p1, p2, p3, &n1[0], &n1[1], &n1[2]);
320 double dIntensity1, dIntensity2;
322 dIntensity1 = (v[ix][0] - dMin) / dIntensityScale;
323 dIntensity2 = (v[ix+1][0] - dMin) / dIntensityScale;
327 intensityToColor (dIntensity1, vecColor);
328 glColor3fv (vecColor);
333 intensityToColor (dIntensity2, vecColor);
334 glColor3fv (vecColor);
339 lastP[0] = ix; lastP[1] = actScale * (v[ix][0] + actOffset); lastP[2] = 0;
340 for (unsigned int iy = 1; iy < ny - 1; iy++) {
341 p1[0] = ix; p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = iy;
342 p2[0] = ix+1; p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = iy;
343 CalculateVectorNormal (p1, p2, lastP, &n1[0], &n1[1], &n1[2]);
344 lastP[0] = p1[0]; lastP[1] = p1[1]; lastP[2] = p1[2];
346 dIntensity1 = (v[ix][iy] - dMin) / dIntensityScale;
347 dIntensity2 = (v[ix+1][iy] - dMin) / dIntensityScale;
350 intensityToColor (dIntensity1, vecColor);
351 glColor3fv (vecColor);
353 glVertex3dv (p1); glNormal3dv (n1);
355 intensityToColor (dIntensity2, vecColor);
356 glColor3fv (vecColor);
358 glVertex3dv (p2); glNormal3dv (n1);
360 glEnd(); // QUAD_STRIP
367 Graph3dFileView::OnProperties (wxCommandEvent& event)
369 std::ostringstream os;
370 *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
371 wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
372 dialogMsg.ShowModal();
376 Graph3dFileView::OnLighting (wxCommandEvent& event)
378 m_bLighting = ! m_bLighting;
379 m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
381 m_pCanvas->Refresh();
385 Graph3dFileView::OnSurface (wxCommandEvent& event)
387 m_bSurface = ! m_bSurface;
388 m_pViewMenu->Check (GRAPH3D_VIEW_SURFACE, m_bSurface);
389 m_pCanvas->Refresh();
393 Graph3dFileView::OnColor (wxCommandEvent& event)
395 m_bColor = ! m_bColor;
396 m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
397 OnUpdate (this, NULL);
401 Graph3dFileView::OnSmooth (wxCommandEvent& event)
403 m_bSmooth = ! m_bSmooth;
404 m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
405 m_pCanvas->Refresh();
411 Graph3dFileView::OnDraw (wxDC* dc)
414 if (! m_pCanvas->GetContext()) return;
417 m_pCanvas->SwapBuffers();
419 std::ostringstream os;
420 os << "Xangle=" << m_dXRotate << ", Yangle=" << m_dYRotate << ", Zangle=" << m_dZRotate;
421 m_pStatusBar->SetStatusText (os.str().c_str());
422 m_pCanvas->SwapBuffers();
427 Graph3dFileView::Draw ()
429 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
438 Graph3dFileView::InitMaterials()
442 int nx = GetDocument()->nx();
443 int ny = GetDocument()->ny();
446 static float ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
447 static float diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
448 static float position0[] = {-nx/2, -ny/2, -ny/2, 0.0f, 0.0f};
449 static float position1[] = {-nx/2, -ny/2, ny/2, 0.0f};
450 static float ambient1[] = {0.5f, 0.5f, 0.5f, 1.0f};
451 static float diffuse1[] = {1.0f, 1.0f, 1.0f, 1.0f};
452 // static float position0[] = {0.0f, 0.0f, 20.0f, 0.0f};
453 // static float position1[] = {0.0f, 0.0f, -20.0f, 0.0f};
454 static float front_mat_shininess[] = {5.0f};
455 static float front_mat_specular[] = {0.1f, 0.1f, 0.1f, 1.0f};
456 static float front_mat_diffuse[] = {0.3f, 0.3f, 0.3f, 1.0f};
458 static float back_mat_shininess[] = {60.0f};
459 static float back_mat_specular[] = {0.2f, 0.2f, 0.2f, 1.0f};
460 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_FALSE};
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, ambient);
470 glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
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);
486 glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
487 // glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
488 glEnable(GL_COLOR_MATERIAL);
490 GLfloat impLPos[] = {1.0f, 1.0f, 1.0f, 0.0f};
492 GLfloat defaultLightAmb [] = {.2f, .2f, .2f, 1.0f};
493 GLfloat defaultLightDiff [] = {.2f, .2f, .2f, 1.0f};
494 GLfloat defaultLightSpec [] = { .3f, .3f, .3f, 1.0f};
496 GLfloat defaultGlobalAmb [] = {.3f, .3f, .3f, 1.0f};
497 GLfloat defaultGlobalDiff[] = {.3f, .3f, .3f, 1.0f};
499 GLfloat defaultMatShine[] = { 30.0f };
500 GLfloat defaultMatSpec[] = { .4f, .4f, .4f, 1.0f};
501 GLfloat defaultMatAmb[] = { .3f, .3f, .3f, 1.0f};
502 GLfloat defaultMatDiff[] = { .5f, .5f, .5f, 1.0f};
504 GLfloat brassMatAmb[] = { .33f, .22f, .03f, 1.0f};
505 GLfloat brassMatDiff[] = { .78f, .57f, .11f, 1.0f};
506 GLfloat brassMatSpec[] = { .99f, .91f, .81f, 1.0f};
507 GLfloat brassMatShine[] = { 27.8f };
509 GLfloat emeraldMatAmb[] = { .02f1, .1745f , .021f, 1.0f };
510 GLfloat emeraldMatDiff[] = { .075f, .6142f , .075f, 1.0f };
511 GLfloat emeraldMatSpec[] = { .633f, .7278f , .633f, 1.0f };
512 GLfloat emeraldMatShine[] = { 76.8f };
514 GLfloat slateMatAmb[] = { .02f, .02f , .02f, 1.0f };
515 GLfloat slateMatDiff[] = { .02f, .01f , .01f, 1.0f };
516 GLfloat slateMatSpec[] = { .4f, .4f , .4f , 1.0f };
517 GLfloat slateMatShine[] = { .768f };
519 // double opnX = nx, opnY = ny, opnZ = z;
520 // eyeX = 1; eyeY = 1, eyeZ = 1;
522 impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
524 //impGraphicsFlag = IMP__3D;
526 // glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM);
527 // glutInitWindowSize (IMP_WIN_X, IMP_WIN_Y);
528 // glutInitWindowPosition (100, 100);
529 // glutCreateWindow ("- imp3D graphics -" );
531 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
533 glShadeModel (GL_SMOOTH);
534 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
535 glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
536 glEnable(GL_NORMALIZE);
539 glEnable(GL_DEPTH_TEST);
541 glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
542 glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
543 glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
545 glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
546 glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
547 glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
549 glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
550 glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
551 glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
553 glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
554 glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
555 glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
557 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT , defaultMatAmb);
558 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE , defaultMatDiff);
559 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR , defaultMatSpec);
560 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, defaultMatShine);
562 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
564 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
566 glEnable(GL_COLOR_MATERIAL);
568 glEnable(GL_LIGHTING);
578 Graph3dFileView::InitGL ()
580 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
582 glDisable (GL_CULL_FACE);
583 glEnable (GL_DEPTH_TEST);
588 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
593 int nx = GetDocument()->nx();
594 int ny = GetDocument()->ny();
595 const ImageFileArrayConst v = GetDocument()->getArray();
596 if (v != NULL && nx != 0 && ny != 0) {
597 double min = v[0][0];
599 for (unsigned int ix = 0; ix < nx; ix++)
600 for (unsigned int iy = 0; iy < ny; iy++) {
601 double dVal = v[ix][iy];
609 if (! m_bColorScaleMinSet)
610 m_dColorScaleMin = min;
611 if (! m_bColorScaleMaxSet)
612 m_dColorScaleMax = max;
615 double dRadius = maxValue<int> (nx, ny) * SQRT2 / 2;
616 glMatrixMode(GL_PROJECTION);
618 glOrtho (-dRadius, dRadius, -dRadius, dRadius, dRadius*5, -dRadius*5);
620 glMatrixMode(GL_MODELVIEW);
623 GLfloat eyep[3], lookp[3], up[3];
624 eyep[0] = -nx/2; eyep[1] = 0; eyep[2] = -ny/2;
625 lookp[0] = 0; lookp[1] = 0, lookp[2] = 0;
626 up[0] = 0; up[1] = 1; up[2] = 0;
627 gluLookAt (eyep[0], eyep[1], eyep[2], lookp[0], lookp[1], lookp[2], up[0], up[1], up[2]);
633 m_pCanvas->Refresh();
637 Graph3dFileView::OnClose (bool deleteWindow)
639 if (! GetDocument() || ! GetDocument()->Close())
644 m_pCanvas->setView(NULL);
647 wxString s(theApp->GetAppName());
649 m_pFrame->SetTitle(s);
662 Graph3dFileView::OnScaleAuto (wxCommandEvent& event)
665 const ImageFile& rIF = GetDocument()->getImageFile();
666 double min, max, mean, mode, median, stddev;
667 rIF.statistics(min, max, mean, mode, median, stddev);
668 DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
669 int iRetVal = dialogAutoScale.ShowModal();
670 if (iRetVal == wxID_OK) {
671 m_bMinSpecified = true;
672 m_bMaxSpecified = true;
674 if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
677 m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
678 OnUpdate (this, NULL);
685 Graph3dFileView::OnScaleSet (wxCommandEvent& event)
690 unsigned int nx = GetDocument()->nx();
691 unsigned int ny = GetDocument()->ny();
692 const ImageFileArrayConst v = GetDocument()->getArray();
694 if (! m_bColorScaleMinSet && ! m_bColorScaleMaxSet) {
695 dMax = dMin = v[0][0];
696 for (unsigned ix = 0; ix < nx; ix++)
697 for (unsigned int iy = 0; iy < ny; iy++)
698 if (v[ix][iy] < dMin)
700 else if (v[ix][iy] > dMax)
703 if (m_bColorScaleMinSet)
704 dMin = m_dColorScaleMin;
705 if (m_bColorScaleMaxSet)
706 dMax = m_dColorScaleMax;
708 DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Color Scale Minimum & Maximum", dMin, dMax);
709 int retVal = dialogMinMax.ShowModal();
710 if (retVal == wxID_OK) {
711 m_bColorScaleMinSet = true;
712 m_bColorScaleMaxSet = true;
713 m_dColorScaleMin = dialogMinMax.getMinimum();
714 m_dColorScaleMax = dialogMinMax.getMaximum();
715 OnUpdate (this, NULL);
720 Graph3dFileView::OnScaleFull (wxCommandEvent& event)
722 if (m_bColorScaleMinSet || m_bColorScaleMaxSet) {
723 m_bColorScaleMinSet = false;
724 m_bColorScaleMaxSet = false;
725 OnUpdate (this, NULL);
734 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
737 wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
739 wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
741 theApp->setIconForFrame (subframe);
743 m_pStatusBar = new wxStatusBar (subframe, -1);
744 subframe->SetStatusBar (m_pStatusBar);
746 m_pFileMenu = new wxMenu;
748 m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
749 m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
750 m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
751 m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
753 m_pFileMenu->AppendSeparator();
754 m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
756 m_pFileMenu->AppendSeparator();
757 m_pFileMenu->Append(wxID_PRINT, "&Print...");
758 m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
759 m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
761 m_pFileMenu->AppendSeparator();
762 m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
764 GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
765 GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
767 m_pViewMenu = new wxMenu;
768 m_pViewMenu->Append(GRAPH3D_VIEW_SURFACE, "Su&rface\tCtrl-R", "", true);
769 m_pViewMenu->Append(GRAPH3D_VIEW_SMOOTH, "S&mooth\tCtrl-M", "", true);
770 m_pViewMenu->Append(GRAPH3D_VIEW_COLOR, "Co&lor\tCtrl-L", "", true);
771 m_pViewMenu->Append(GRAPH3D_VIEW_LIGHTING, "Li&ghting\tCtrl-G", "", true);
772 m_pViewMenu->AppendSeparator();
773 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_MINMAX, "Color Scale S&et Min/Max...\tCtrl-E");
774 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_AUTO, "Color Scale &Auto...\tCtrl-A");
775 m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_FULL, "Color F&ull Scale\tCtrl-U");
778 wxMenu *help_menu = new wxMenu;
779 help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
780 help_menu->Append(MAINMENU_HELP_TOPICS, "&Topics\tCtrl-H");
781 help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
783 wxMenuBar *menu_bar = new wxMenuBar;
785 menu_bar->Append(m_pFileMenu, "&File");
786 menu_bar->Append(m_pViewMenu, "&View");
787 menu_bar->Append(help_menu, "&Help");
789 subframe->SetMenuBar(menu_bar);
791 subframe->Centre(wxBOTH);
793 wxAcceleratorEntry accelEntries[12];
794 accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('O'), wxID_OPEN);
795 accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('H'), MAINMENU_HELP_TOPICS);
796 accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('P'), MAINMENU_FILE_CREATE_PHANTOM);
797 accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('F'), MAINMENU_FILE_CREATE_FILTER);
798 accelEntries[4].Set (wxACCEL_NORMAL, WXK_F1, MAINMENU_HELP_CONTENTS);
799 accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('R'), GRAPH3D_VIEW_SURFACE);
800 accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('L'), GRAPH3D_VIEW_COLOR);
801 accelEntries[7].Set (wxACCEL_CTRL, static_cast<int>('G'), GRAPH3D_VIEW_LIGHTING);
802 accelEntries[8].Set (wxACCEL_CTRL, static_cast<int>('M'), GRAPH3D_VIEW_SMOOTH);
803 accelEntries[9].Set (wxACCEL_CTRL, static_cast<int>('E'), GRAPH3D_VIEW_SCALE_MINMAX);
804 accelEntries[10].Set (wxACCEL_CTRL, static_cast<int>('A'), GRAPH3D_VIEW_SCALE_AUTO);
805 accelEntries[11].Set (wxACCEL_CTRL, static_cast<int>('U'), GRAPH3D_VIEW_SCALE_FULL);
806 wxAcceleratorTable accelTable (12, accelEntries);
807 subframe->SetAcceleratorTable (accelTable);
814 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
815 EVT_SIZE(Graph3dFileCanvas::OnSize)
816 EVT_PAINT(Graph3dFileCanvas::OnPaint)
817 EVT_CHAR(Graph3dFileCanvas::OnChar)
818 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
819 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
825 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent, const wxPoint& pos,
826 const wxSize& size, long style, int* gl_attrib):
827 wxGLCanvas (parent, -1, pos, size, style, _T("Graph3dCanvas"), gl_attrib), m_pView(view)
832 // Make sure server supports the vertex array extension
833 char* extensions = (char *) glGetString( GL_EXTENSIONS );
834 if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) {
835 m_pView->m_bUseVertexArrays = GL_FALSE;
841 Graph3dFileCanvas::~Graph3dFileCanvas()
846 Graph3dFileCanvas::OnDraw (wxDC& dc)
849 m_pView->OnDraw(& dc);
853 Graph3dFileCanvas::OnSize (wxSizeEvent& event)
856 if (!GetContext()) return;
861 GetClientSize (&width, &height);
862 Reshape (width, height);
866 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
871 wxCommandEvent dummyEvent;
872 switch (event.KeyCode()) {
874 m_pView->m_dZRotate += 15.0;
878 m_pView->m_dZRotate -= 15.0;
882 m_pView->m_dXRotate += 15.0;
886 m_pView->m_dXRotate -= 15.0;
890 m_pView->m_dYRotate += 15.0;
894 m_pView->m_dYRotate -= 15.0;
898 m_pView->OnSurface (dummyEvent);
901 m_pView->OnSmooth (dummyEvent);
904 m_pView->OnLighting (dummyEvent);
907 m_pView->OnColor (dummyEvent);
916 Graph3dFileCanvas::Reshape (int width, int height)
918 glViewport (0, 0, (GLint)width, (GLint)height);
923 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
925 static int dragging = 0;
926 static float last_x, last_y;
931 if(event.LeftIsDown()) {
935 m_pView->m_dXRotate -= (event.GetY() - last_y)*1.0;
936 m_pView->m_dZRotate += (event.GetX() - last_x)*1.0;
939 last_x = event.GetX();
940 last_y = event.GetY();
946 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
948 // Do nothing: avoid flashing.
952 #endif // wxUSE_GLCANVAS