r635: no message
[ctsim.git] / src / graph3dview.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          graph3dview.cpp
5 **   Purpose:       3d graph view classes
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Jan 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: graph3dview.cpp,v 1.19 2001/03/13 04:44:25 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #ifdef __GNUG__
29 #pragma implementation
30 #pragma interface
31 #endif
32
33 #include "wx/wxprec.h"
34
35 #ifndef WX_PRECOMP
36 #include "wx/wx.h"
37 #endif
38
39 #if wxUSE_GLCANVAS
40
41 #if !wxUSE_GLCANVAS
42 #error Please set wxUSE_GLCANVAS to 1 in setup.h.
43 #endif
44
45 #include "wx/timer.h"
46 #include "wx/glcanvas.h"
47
48 #include <GL/gl.h>
49 #include <GL/glu.h>
50
51 #include "ct.h"
52 #include "ctsim.h"
53 #include "docs.h"
54 #include "views.h"
55 #include "dialogs.h"
56 #include "dlgprojections.h"
57 #include "dlgreconstruct.h"
58 #include "backprojectors.h"
59 #include "reconstruct.h"
60 #include "timer.h"
61
62 #if defined(MSVC) || HAVE_SSTREAM
63 #include <sstream>
64 #else
65 #include <sstream_subst>
66 #endif
67
68 void 
69 Graph3dFileView::intensityToColor (double dIntensity, GLfloat* vecColor)
70 {
71   if (dIntensity < 0 || dIntensity > 1) {
72     vecColor[0] = vecColor[1] = vecColor[2] = 1;
73     return;
74   }
75   
76   float fRange = dIntensity * 5;
77   int iRange = static_cast<int>(floor (fRange));
78   float fFrac = fRange - iRange;
79   
80   // 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)
81   switch (iRange) {
82   case 0:
83     vecColor[0] = 1.0f - fFrac; vecColor[1] = 0.0f; vecColor[2] = 1.0f;
84     break;
85   case 1:
86     vecColor[0] = 0.0f; vecColor[1] = fFrac; vecColor[2] = 1.0f;
87     break;
88   case 2:
89     vecColor[0] = 0.0f; vecColor[1] = 1.0f; vecColor[2] = 1.0f - fFrac;
90     break;
91   case 3:
92     vecColor[0] = fFrac; vecColor[1] = 1.0f; vecColor[2] = 0.0f;
93     break;
94   case 4:
95     vecColor[0] = 1.0f; vecColor[1] = 1.0f - fFrac; vecColor[2] = 0.0f;
96     break;
97   case 5:
98     vecColor[0] = 1.0f; vecColor[1] = 0.0f; vecColor[2] = 0.0f;
99     break;
100   }
101 }
102
103 //***********************************************************************
104 // Function: CalculateVectorNormal
105 //
106 // Purpose: Given three points of a 3D plane, this function calculates
107 //          the normal vector of that plane.
108 //
109 // Parameters:
110 //     fVert1[]   == array for 1st point (3 elements are x, y, and z).
111 //     fVert2[]   == array for 2nd point (3 elements are x, y, and z).
112 //     fVert3[]   == array for 3rd point (3 elements are x, y, and z).
113 //
114 // Returns:
115 //     fNormalX   == X vector for the normal vector
116 //     fNormalY   == Y vector for the normal vector
117 //     fNormalZ   == Z vector for the normal vector
118 //*************************************************************************
119
120 void 
121 CalculateVectorNormal (GLfloat fVert1[], GLfloat fVert2[], 
122                        GLfloat fVert3[], GLfloat *fNormalX,
123                        GLfloat *fNormalY, GLfloat *fNormalZ)
124 {
125   GLfloat Qx = fVert2[0] - fVert1[0];
126   GLfloat Qy = fVert2[1] - fVert1[1];
127   GLfloat Qz = fVert2[2] - fVert1[2];
128   GLfloat Px = fVert3[0] - fVert1[0];
129   GLfloat Py = fVert3[1] - fVert1[1];
130   GLfloat Pz = fVert3[2] - fVert1[2];
131   
132   *fNormalX = Py*Qz - Pz*Qy;
133   *fNormalY = Pz*Qx - Px*Qz;
134   *fNormalZ = Px*Qy - Py*Qx;
135   
136
137
138 void 
139 CalculateVectorNormal (GLdouble fVert1[], GLdouble fVert2[], 
140                        GLdouble fVert3[], GLdouble *fNormalX,
141                        GLdouble *fNormalY, GLdouble *fNormalZ)
142 {
143   GLdouble Qx = fVert2[0] - fVert1[0];
144   GLdouble Qy = fVert2[1] - fVert1[1];
145   GLdouble Qz = fVert2[2] - fVert1[2];
146   GLdouble Px = fVert3[0] - fVert1[0];
147   GLdouble Py = fVert3[1] - fVert1[1];
148   GLdouble Pz = fVert3[2] - fVert1[2];
149   
150   *fNormalX = Py*Qz - Pz*Qy;
151   *fNormalY = Pz*Qx - Px*Qz;
152   *fNormalZ = Px*Qy - Py*Qx;
153   
154
155
156 IMPLEMENT_DYNAMIC_CLASS(Graph3dFileView, wxView)
157
158 BEGIN_EVENT_TABLE(Graph3dFileView, wxView)
159 EVT_MENU(IFMENU_FILE_PROPERTIES, Graph3dFileView::OnProperties)
160 EVT_MENU(GRAPH3D_VIEW_LIGHTING, Graph3dFileView::OnLighting)
161 EVT_MENU(GRAPH3D_VIEW_COLOR, Graph3dFileView::OnColor)
162 EVT_MENU(GRAPH3D_VIEW_SMOOTH, Graph3dFileView::OnSmooth)
163 EVT_MENU(GRAPH3D_VIEW_SURFACE, Graph3dFileView::OnSurface)
164 EVT_MENU(GRAPH3D_VIEW_SCALE_MINMAX, Graph3dFileView::OnScaleSet)
165 EVT_MENU(GRAPH3D_VIEW_SCALE_AUTO, Graph3dFileView::OnScaleAuto)
166 EVT_MENU(GRAPH3D_VIEW_SCALE_FULL, Graph3dFileView::OnScaleFull)
167 END_EVENT_TABLE()
168
169 Graph3dFileView::Graph3dFileView ()
170   : m_pFileMenu(NULL), m_pViewMenu(NULL), m_pCanvas(NULL), m_pFrame(NULL)
171 {
172   m_bDoubleBuffer = true;
173   m_bSmooth = true;
174   m_bLighting = true;
175   m_bSurface = true;
176   m_bLighting = true;
177   m_bColor = false;
178   m_dXRotate = -45;
179   m_dYRotate = 0;
180   m_dZRotate = -45;
181   m_bColorScaleMinSet = false;
182   m_bColorScaleMaxSet = false;
183 }
184
185 Graph3dFileView::~Graph3dFileView()
186 {
187   GetDocumentManager()->FileHistoryRemoveMenu (m_pFileMenu);
188   GetDocumentManager()->ActivateView(this, false, true);
189 }
190
191 bool 
192 Graph3dFileView::OnCreate (wxDocument *doc, long WXUNUSED(flags) )
193 {
194   m_pFrame = CreateChildFrame(doc, this);
195   
196   int width, height;
197   m_pFrame->GetClientSize (&width, &height);
198   m_pFrame->SetTitle("Graph3dFileView");
199   m_pCanvas = CreateCanvas (m_pFrame);
200   
201   m_pFrame->Show (true);
202   m_pCanvas->SetCurrent();
203   
204   InitGL();
205   
206   int x, y;  // X requires a forced resize
207   m_pFrame->GetSize(&x, &y);
208   m_pFrame->SetSize(-1, -1, x, y);
209   m_pFrame->SetFocus();
210   m_pFrame->Show(true);
211   Activate(true);
212   
213   m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
214   m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
215   m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
216   m_pViewMenu->Check (GRAPH3D_VIEW_SURFACE, m_bSurface);
217   return true;
218
219
220 Graph3dFileCanvas* 
221 Graph3dFileView::CreateCanvas (wxFrame* parent)
222 {
223   Graph3dFileCanvas* pCanvas;
224   int width, height;
225   parent->GetClientSize (&width, &height);
226   
227   pCanvas = new Graph3dFileCanvas (this, parent, wxPoint(0, 0), wxSize(200, 200), 0);
228   
229   pCanvas->SetBackgroundColour(*wxWHITE);
230   pCanvas->Clear();
231   
232   return pCanvas;
233 }
234
235
236 void
237 Graph3dFileView::DrawSurface()
238 {
239   if (m_bSmooth) {
240     glShadeModel (GL_SMOOTH);
241   } else {
242     glShadeModel (GL_FLAT);
243   }
244   
245   if (m_bLighting) {
246     glEnable (GL_LIGHTING);
247   } else {
248     glDisable (GL_LIGHTING);
249   }
250   
251   if (m_bSurface)
252     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
253   else
254     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
255   
256   InitMaterials(); 
257   
258   if (! GetDocument())
259     return;
260   
261   unsigned int nx = GetDocument()->nx();
262   unsigned int ny = GetDocument()->ny();
263   glRotated (m_dZRotate, 0.0, 1.0, 0.0);
264   glRotated (m_dXRotate, 0.0, 0.0, 1.0);
265   glRotated (m_dYRotate, 1.0, 0.0, 0.0);
266   glTranslated (-static_cast<double>(nx - 1) / 2, 0.0, -static_cast<double>(ny - 1) / 2);
267   glCallList (DISPLAYLIST_SURFACE);
268 }
269
270 void
271 Graph3dFileView::CreateDisplayList()
272 {
273   if (! GetDocument())
274     return;
275   
276   unsigned int nx = GetDocument()->nx();
277   unsigned int ny = GetDocument()->ny();
278   const ImageFileArrayConst v = GetDocument()->getArray();
279   if (nx == 0 || ny == 0 || ! v)
280     return;
281   
282   glNewList (DISPLAYLIST_SURFACE, GL_COMPILE);
283   
284   double dMin = m_dColorScaleMin;
285   double dIntensityScale = m_dColorScaleMax - m_dColorScaleMin;
286   double actOffset = m_dGraphMin;
287   double actScale = 0.4 * sqrt(nx*nx+ny*ny) / (m_dGraphMax - m_dGraphMin);
288   double dXOffset = -(static_cast<double>(nx) - 1) / 2.;
289   double dYOffset = -(static_cast<double>(ny) - 1) / 2.;
290   dXOffset = 0;
291   dYOffset = 0;
292
293   if (! m_bColor)
294     glColor3f (1.0f, 1.0f, 1.0f);
295   
296   double dXPos = -dXOffset;
297   for (unsigned ix = 0; ix < nx - 1; ix++, dXPos++) {
298     double dYPos = -dYOffset;
299     glBegin(GL_QUAD_STRIP);
300     double p1[3], p2[3], p3[3], n1[3]; 
301     p1[0] = dXPos;  p1[1] = actScale * (v[ix][0] + actOffset); p1[2] = dYPos;
302     p2[0] = dXPos+1; p2[1] = actScale * (v[ix+1][0] + actOffset); p2[2] = dYPos; 
303     p3[0] = dXPos; p3[1] = actScale * (v[ix][1] + actOffset); p3[2] = dYPos + 1;
304     CalculateVectorNormal (p1, p2, p3, &n1[0], &n1[1], &n1[2]);
305
306     double dIntensity1, dIntensity2;
307     if (m_bColor) {
308       dIntensity1 = (v[ix][0] - dMin) / dIntensityScale;
309       dIntensity2 = (v[ix+1][0] - dMin) / dIntensityScale;
310     }
311     float vecColor[3];
312     if (m_bColor) {
313       intensityToColor (dIntensity1, vecColor);
314       glColor3fv (vecColor);
315     }
316     glVertex3dv (p1); 
317     glNormal3dv (n1);                                   
318     if (m_bColor) {
319       intensityToColor (dIntensity2, vecColor);
320       glColor3fv (vecColor);
321     }
322     glVertex3dv (p2); 
323     glNormal3dv (n1);                                   
324     double lastP[3];
325     lastP[0] = ix; lastP[1] = actScale * (v[ix][0] + actOffset); lastP[2] = 0; 
326     for (unsigned int iy = 1; iy < ny - 1; iy++, dYPos++) {       
327       p1[0] = dXPos; p1[1] = actScale * (v[ix][iy] + actOffset); p1[2] = dYPos;
328       p2[0] = dXPos+1;  p2[1] = actScale * (v[ix+1][iy] + actOffset); p2[2] = dYPos;
329       CalculateVectorNormal (p1, p2, lastP, &n1[0], &n1[1], &n1[2]);
330       lastP[0] = p1[0]; lastP[1] = p1[1]; lastP[2] = p1[2];
331       if (m_bColor) {
332         dIntensity1 = (v[ix][iy] - dMin) / dIntensityScale;
333         dIntensity2 = (v[ix+1][iy] - dMin) / dIntensityScale;
334       }
335       if (m_bColor) {
336         intensityToColor (dIntensity1, vecColor);
337         glColor3fv (vecColor);
338       }
339       glVertex3dv (p1); glNormal3dv (n1);                                       
340       if (m_bColor) {
341         intensityToColor (dIntensity2, vecColor);
342         glColor3fv (vecColor);
343       }
344       glVertex3dv (p2); glNormal3dv (n1);                                       
345     }                   
346     glEnd(); // QUAD_STRIP
347   }
348   glEndList();
349 }
350
351
352 void
353 Graph3dFileView::OnProperties (wxCommandEvent& event)
354 {
355   std::ostringstream os;
356   *theApp->getLog() << ">>>>\n" << os.str().c_str() << "<<<<\n";
357   wxMessageDialog dialogMsg (getFrameForChild(), os.str().c_str(), "Imagefile Properties", wxOK | wxICON_INFORMATION);
358   dialogMsg.ShowModal();
359 }
360
361 void
362 Graph3dFileView::OnLighting (wxCommandEvent& event)
363 {
364   m_bLighting = ! m_bLighting;
365   m_pViewMenu->Check (GRAPH3D_VIEW_LIGHTING, m_bLighting);
366   
367   m_pCanvas->Refresh();
368 }
369
370 void
371 Graph3dFileView::OnSurface (wxCommandEvent& event)
372 {
373   m_bSurface = ! m_bSurface;
374   m_pViewMenu->Check (GRAPH3D_VIEW_SURFACE, m_bSurface);
375   m_pCanvas->Refresh();
376 }
377
378 void
379 Graph3dFileView::OnColor (wxCommandEvent& event)
380 {
381   m_bColor = ! m_bColor;
382   m_pViewMenu->Check (GRAPH3D_VIEW_COLOR, m_bColor);
383   OnUpdate (this, NULL);
384 }
385
386 void
387 Graph3dFileView::OnSmooth (wxCommandEvent& event)
388 {
389   m_bSmooth = ! m_bSmooth;
390   m_pViewMenu->Check (GRAPH3D_VIEW_SMOOTH, m_bSmooth);
391   m_pCanvas->Refresh();
392 }
393
394
395
396 void 
397 Graph3dFileView::OnDraw (wxDC* dc)
398 {
399   if (! m_pCanvas)
400     return;
401   m_pCanvas->SetCurrent();
402
403 #ifndef __WXMOTIF__
404   //  if (! m_pCanvas->GetContext()) return;
405 #endif
406   
407   Draw();
408   std::ostringstream os;
409   os << "Xangle=" << m_dXRotate << ", Yangle=" << m_dYRotate << ", Zangle=" << m_dZRotate;
410   m_pStatusBar->SetStatusText (os.str().c_str());
411   m_pCanvas->SwapBuffers();
412 }
413
414
415 void 
416 Graph3dFileView::Draw ()
417 {
418   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
419   glPushMatrix();
420   DrawSurface();  
421   glPopMatrix();
422   glFlush();
423 }
424
425
426 void
427 Graph3dFileView::InitMaterials()
428 {
429   if (! GetDocument())
430     return;
431   int nx = GetDocument()->nx();
432   int ny = GetDocument()->ny();
433   
434 #if 1
435   static float ambient[] = {0.1f, 0.1f, 0.1f, 1.0f};
436   static float diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
437   static float position0[] = {-nx/2, -ny/2, -ny/2, 0.0f, 0.0f};
438   static float position1[] = {-nx/2, -ny/2, ny/2, 0.0f};
439   static float ambient1[] = {0.5f, 0.5f, 0.5f, 1.0f};
440   static float diffuse1[] = {1.0f, 1.0f, 1.0f, 1.0f};
441   //  static float position0[] = {0.0f, 0.0f, 20.0f, 0.0f};
442   //  static float position1[] = {0.0f, 0.0f, -20.0f, 0.0f};
443   static float front_mat_shininess[] = {5.0f};
444   static float front_mat_specular[] = {0.1f, 0.1f, 0.1f, 1.0f};
445   static float front_mat_diffuse[] = {0.3f, 0.3f, 0.3f, 1.0f};
446   /*
447   static float back_mat_shininess[] = {60.0f};
448   static float back_mat_specular[] = {0.2f, 0.2f, 0.2f, 1.0f};
449   static float back_mat_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
450   */
451   static float lmodel_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
452   static float lmodel_twoside[] = {GL_FALSE};
453   
454   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
455   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
456   glEnable(GL_NORMALIZE);
457   
458   glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
459   glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
460   glLightfv (GL_LIGHT0, GL_POSITION, position0);
461   glEnable (GL_LIGHT0);
462   
463   glLightfv (GL_LIGHT1, GL_AMBIENT, ambient1);
464   glLightfv (GL_LIGHT1, GL_DIFFUSE, diffuse1);
465   glLightfv (GL_LIGHT1, GL_POSITION, position1);
466   glEnable (GL_LIGHT1);
467   
468   glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
469   glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
470   
471   glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
472   glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, front_mat_specular);
473   glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, front_mat_diffuse);
474   
475   glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
476   //  glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
477   glEnable(GL_COLOR_MATERIAL);
478 #else
479   GLfloat impLPos[]  = {1.0f, 1.0f, 1.0f, 0.0f};
480   
481   GLfloat defaultLightAmb   [] = {.2f, .2f, .2f, 1.0f};
482   GLfloat defaultLightDiff  [] = {.2f, .2f, .2f, 1.0f};
483   GLfloat defaultLightSpec  [] = { .3f, .3f, .3f, 1.0f};
484   
485   GLfloat defaultGlobalAmb [] = {.3f, .3f, .3f, 1.0f};
486   GLfloat defaultGlobalDiff[] = {.3f, .3f, .3f, 1.0f};
487   
488   GLfloat defaultMatShine[] = {  30.0f };
489   GLfloat defaultMatSpec[]  = { .4f, .4f, .4f, 1.0f};
490   GLfloat defaultMatAmb[]   = { .3f, .3f, .3f, 1.0f};
491   GLfloat defaultMatDiff[]  = { .5f, .5f, .5f, 1.0f};
492   
493   GLfloat brassMatAmb[]   = { .33f, .22f, .03f, 1.0f};
494   GLfloat brassMatDiff[]  = { .78f, .57f, .11f, 1.0f};
495   GLfloat brassMatSpec[]  = { .99f, .91f, .81f, 1.0f};
496   GLfloat brassMatShine[] = {  27.8f };
497   
498   GLfloat emeraldMatAmb[]   = { .02f1, .1745f , .021f, 1.0f };
499   GLfloat emeraldMatDiff[]  = { .075f, .6142f , .075f, 1.0f };
500   GLfloat emeraldMatSpec[]  = { .633f, .7278f , .633f, 1.0f };
501   GLfloat emeraldMatShine[] = {  76.8f };
502   
503   GLfloat slateMatAmb[]   = { .02f, .02f , .02f, 1.0f };
504   GLfloat slateMatDiff[]  = { .02f, .01f , .01f, 1.0f };
505   GLfloat slateMatSpec[]  = { .4f,  .4f ,  .4f , 1.0f };
506   GLfloat slateMatShine[] = { .768f };
507   
508   //       double opnX = nx, opnY = ny, opnZ = z;
509   //       eyeX = 1; eyeY = 1, eyeZ = 1;
510   
511   impLPos[0] = nx/2.; impLPos[1]= ny/2.; impLPos[2] = 0.;
512   //opnListNum = 1;
513   //impGraphicsFlag = IMP__3D;
514   
515   //       glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH | GLUT_ACCUM);
516   //       glutInitWindowSize (IMP_WIN_X, IMP_WIN_Y);
517   //  glutInitWindowPosition (100, 100);
518   //       glutCreateWindow ("- imp3D graphics -" );
519   
520   glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
521   
522   glShadeModel (GL_SMOOTH);
523   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
524   glHint(GL_LINE_SMOOTH, GL_DONT_CARE);
525   glEnable(GL_NORMALIZE);
526   
527   
528   glEnable(GL_DEPTH_TEST);
529   
530   glLightfv(GL_LIGHT0, GL_AMBIENT, defaultLightAmb);
531   glLightfv(GL_LIGHT0, GL_DIFFUSE, defaultLightDiff);
532   glLightfv(GL_LIGHT0, GL_SPECULAR,defaultLightSpec);
533   
534   glLightfv(GL_LIGHT1, GL_AMBIENT, defaultLightAmb);
535   glLightfv(GL_LIGHT1, GL_DIFFUSE, defaultLightDiff);
536   glLightfv(GL_LIGHT1, GL_SPECULAR,defaultLightSpec);
537   
538   glLightfv(GL_LIGHT2, GL_AMBIENT , defaultLightAmb);
539   glLightfv(GL_LIGHT2, GL_DIFFUSE , defaultLightDiff);
540   glLightfv(GL_LIGHT2, GL_SPECULAR, defaultLightSpec);
541   
542   glLightfv(GL_LIGHT0, GL_POSITION,impLPos);
543   glLightfv(GL_LIGHT1, GL_POSITION,impLPos);
544   glLightfv(GL_LIGHT2, GL_POSITION,impLPos);
545   
546   glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT  , defaultMatAmb);
547   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE  , defaultMatDiff);
548   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR , defaultMatSpec);
549   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, defaultMatShine);
550   
551   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, defaultGlobalAmb);
552   
553   glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
554   
555   glEnable(GL_COLOR_MATERIAL);
556   
557   glEnable(GL_LIGHTING);
558   glEnable(GL_LIGHT1);
559   glEnable(GL_LIGHT2);
560   glEnable(GL_LIGHT0);
561 #endif
562   
563 }
564
565
566 void 
567 Graph3dFileView::InitGL ()
568 {
569   glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
570   
571   glDisable (GL_CULL_FACE);
572   glEnable (GL_DEPTH_TEST);
573   
574 }
575
576 void 
577 Graph3dFileView::OnUpdate (wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
578 {
579   if (! GetDocument())
580     return;
581   
582   unsigned int nx = GetDocument()->nx();
583   unsigned int ny = GetDocument()->ny();
584   const ImageFileArrayConst v = GetDocument()->getArray();
585   if (v != NULL && nx != 0 && ny != 0) {
586     double min = v[0][0];
587     double max = min;
588     for (unsigned int ix = 0; ix < nx; ix++)
589       for (unsigned int iy = 0; iy < ny; iy++) {
590         double dVal = v[ix][iy];
591         if (min > dVal)
592           min = dVal;
593         else if (max < dVal)
594           max = dVal;
595       }
596       m_dGraphMin = min;
597       m_dGraphMax = max;
598       if (! m_bColorScaleMinSet)
599         m_dColorScaleMin = min;
600       if (! m_bColorScaleMaxSet)
601         m_dColorScaleMax = max;  
602   }
603   
604   double dRadius = maxValue<int> (nx, ny) * SQRT2 / 2;
605   glMatrixMode(GL_PROJECTION);
606   glLoadIdentity();
607   glOrtho (-dRadius, dRadius, -dRadius, dRadius, dRadius*5, -dRadius*5);
608   
609   glMatrixMode(GL_MODELVIEW);
610   glLoadIdentity();
611 #if 0
612   GLfloat eyep[3], lookp[3], up[3];
613   eyep[0] = -nx/2; eyep[1] = 0; eyep[2] = -ny/2;
614   lookp[0] = 0; lookp[1] = 0, lookp[2] = 0;
615   up[0] = 0; up[1] = 1; up[2] = 0;
616   gluLookAt (eyep[0], eyep[1], eyep[2], lookp[0], lookp[1], lookp[2], up[0], up[1], up[2]);
617 #endif
618   
619   CreateDisplayList();
620   
621   if (m_pCanvas)
622     m_pCanvas->Refresh();
623 }
624
625 bool 
626 Graph3dFileView::OnClose (bool deleteWindow)
627 {
628   if (! GetDocument() || ! GetDocument()->Close())
629     return false;
630   
631   Activate (false);
632   if (m_pCanvas) {
633     m_pCanvas->setView(NULL);
634     m_pCanvas = NULL;
635   }
636   wxString s(theApp->GetAppName());
637   if (m_pFrame)
638     m_pFrame->SetTitle(s);
639   
640   SetFrame(NULL);
641   
642   if (deleteWindow) {
643     delete m_pFrame;
644     m_pFrame = NULL;
645   }
646   
647   return true;
648 }
649
650 void 
651 Graph3dFileView::OnScaleAuto (wxCommandEvent& event)
652 {
653 #if 0
654   const ImageFile& rIF = GetDocument()->getImageFile();
655   double min, max, mean, mode, median, stddev;
656   rIF.statistics(min, max, mean, mode, median, stddev);
657   DialogAutoScaleParameters dialogAutoScale (getFrameForChild(), mean, mode, median, stddev, m_dAutoScaleFactor);
658   int iRetVal = dialogAutoScale.ShowModal();
659   if (iRetVal == wxID_OK) {
660     m_bMinSpecified = true;
661     m_bMaxSpecified = true;
662     double dMin, dMax;
663     if (dialogAutoScale.getMinMax (&dMin, &dMax)) {
664       m_dMinPixel = dMin;
665       m_dMaxPixel = dMax;
666       m_dAutoScaleFactor = dialogAutoScale.getAutoScaleFactor();
667       OnUpdate (this, NULL);
668     }
669   }
670 #endif
671 }
672
673 void 
674 Graph3dFileView::OnScaleSet (wxCommandEvent& event)
675 {
676   if (! GetDocument())
677     return;
678   
679   unsigned int nx = GetDocument()->nx();
680   unsigned int ny = GetDocument()->ny();
681   const ImageFileArrayConst v = GetDocument()->getArray();
682   double dMin, dMax;
683   if (! m_bColorScaleMinSet && ! m_bColorScaleMaxSet) {
684     dMax = dMin = v[0][0];
685     for (unsigned ix = 0; ix < nx; ix++)
686       for (unsigned int iy = 0; iy < ny; iy++)
687         if (v[ix][iy] < dMin)
688           dMin = v[ix][iy];
689         else if (v[ix][iy] > dMax)
690           dMax = v[ix][iy];
691   }
692   if (m_bColorScaleMinSet)
693     dMin = m_dColorScaleMin;
694   if (m_bColorScaleMaxSet)
695     dMax = m_dColorScaleMax;
696   
697   DialogGetMinMax dialogMinMax (getFrameForChild(), "Set Color Scale Minimum & Maximum", dMin, dMax);
698   int retVal = dialogMinMax.ShowModal();
699   if (retVal == wxID_OK) {
700     m_bColorScaleMinSet = true;
701     m_bColorScaleMaxSet = true;
702     m_dColorScaleMin = dialogMinMax.getMinimum();
703     m_dColorScaleMax = dialogMinMax.getMaximum();
704     OnUpdate (this, NULL);
705   }
706 }
707
708 void 
709 Graph3dFileView::OnScaleFull (wxCommandEvent& event)
710 {
711   if (m_bColorScaleMinSet || m_bColorScaleMaxSet) {
712     m_bColorScaleMinSet = false;
713     m_bColorScaleMaxSet = false;
714     OnUpdate (this, NULL);
715   }
716 }
717
718 #if CTSIM_MDI
719 wxDocMDIChildFrame*
720 #else
721 wxDocChildFrame*
722 #endif
723 Graph3dFileView::CreateChildFrame (wxDocument *doc, wxView *view)
724 {
725 #if CTSIM_MDI
726   wxDocMDIChildFrame* subframe = new wxDocMDIChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
727 #else
728   wxDocChildFrame* subframe = new wxDocChildFrame (doc, view, theApp->getMainFrame(), -1, "Graph3dFile Frame", wxPoint(-1, -1), wxSize(0, 0), wxDEFAULT_FRAME_STYLE);
729 #endif
730   theApp->setIconForFrame (subframe);
731   
732   m_pStatusBar = new wxStatusBar (subframe, -1);
733   subframe->SetStatusBar (m_pStatusBar);
734   
735   m_pFileMenu = new wxMenu;
736   
737   m_pFileMenu->Append(MAINMENU_FILE_CREATE_PHANTOM, "Cr&eate Phantom...\tCtrl-P");
738   m_pFileMenu->Append(MAINMENU_FILE_CREATE_FILTER, "Create &Filter...\tCtrl-F");
739   m_pFileMenu->Append(wxID_OPEN, "&Open...\tCtrl-O");
740   m_pFileMenu->Append(wxID_CLOSE, "&Close\tCtrl-W");
741   
742   m_pFileMenu->AppendSeparator();
743   m_pFileMenu->Append(IFMENU_FILE_PROPERTIES, "P&roperties");
744   
745   m_pFileMenu->AppendSeparator();
746   m_pFileMenu->Append(wxID_PRINT, "&Print...");
747   m_pFileMenu->Append(wxID_PRINT_SETUP, "Print &Setup...");
748   m_pFileMenu->Append(wxID_PREVIEW, "Print Preview");
749 #ifdef CTSIM_MDI
750   m_pFileMenu->AppendSeparator();
751   m_pFileMenu->Append(MAINMENU_FILE_EXIT, "E&xit");
752 #endif
753   GetDocumentManager()->FileHistoryAddFilesToMenu(m_pFileMenu);
754   GetDocumentManager()->FileHistoryUseMenu(m_pFileMenu);
755   
756   m_pViewMenu = new wxMenu;
757   m_pViewMenu->Append(GRAPH3D_VIEW_SURFACE, "Su&rface\tCtrl-R", "", true);
758   m_pViewMenu->Append(GRAPH3D_VIEW_SMOOTH, "S&mooth\tCtrl-M", "", true);
759   m_pViewMenu->Append(GRAPH3D_VIEW_COLOR, "Co&lor\tCtrl-L", "", true);
760   m_pViewMenu->Append(GRAPH3D_VIEW_LIGHTING, "Li&ghting\tCtrl-G", "", true);
761   m_pViewMenu->AppendSeparator();
762   m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_MINMAX, "Color Scale S&et Min/Max...\tCtrl-E");
763   m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_AUTO, "Color Scale &Auto...\tCtrl-A");
764   m_pViewMenu->Append(GRAPH3D_VIEW_SCALE_FULL, "Color F&ull Scale\tCtrl-U");
765   
766   
767   wxMenu *help_menu = new wxMenu;
768   help_menu->Append(MAINMENU_HELP_CONTENTS, "&Contents\tF1");
769   help_menu->Append(MAINMENU_HELP_ABOUT, "&About");
770   
771   wxMenuBar *menu_bar = new wxMenuBar;
772   
773   menu_bar->Append(m_pFileMenu, "&File");
774   menu_bar->Append(m_pViewMenu, "&View");
775   menu_bar->Append(help_menu, "&Help");
776   
777   subframe->SetMenuBar(menu_bar);
778   
779   subframe->Centre(wxBOTH);
780
781   wxAcceleratorEntry accelEntries[7];
782   accelEntries[0].Set (wxACCEL_CTRL, static_cast<int>('R'), GRAPH3D_VIEW_SURFACE);
783   accelEntries[1].Set (wxACCEL_CTRL, static_cast<int>('L'), GRAPH3D_VIEW_COLOR);
784   accelEntries[2].Set (wxACCEL_CTRL, static_cast<int>('G'), GRAPH3D_VIEW_LIGHTING);
785   accelEntries[3].Set (wxACCEL_CTRL, static_cast<int>('M'), GRAPH3D_VIEW_SMOOTH);
786   accelEntries[4].Set (wxACCEL_CTRL, static_cast<int>('E'), GRAPH3D_VIEW_SCALE_MINMAX);
787   accelEntries[5].Set (wxACCEL_CTRL, static_cast<int>('A'), GRAPH3D_VIEW_SCALE_AUTO);
788   accelEntries[6].Set (wxACCEL_CTRL, static_cast<int>('U'), GRAPH3D_VIEW_SCALE_FULL);
789   wxAcceleratorTable accelTable (7, accelEntries);
790   subframe->SetAcceleratorTable (accelTable);
791
792   return subframe;
793 }
794
795
796
797 BEGIN_EVENT_TABLE(Graph3dFileCanvas, wxGLCanvas)
798 EVT_SIZE(Graph3dFileCanvas::OnSize)
799 EVT_PAINT(Graph3dFileCanvas::OnPaint)
800 EVT_CHAR(Graph3dFileCanvas::OnChar)
801 EVT_MOUSE_EVENTS(Graph3dFileCanvas::OnMouseEvent)
802 EVT_ERASE_BACKGROUND(Graph3dFileCanvas::OnEraseBackground)
803 END_EVENT_TABLE()
804
805
806
807
808 Graph3dFileCanvas::Graph3dFileCanvas (Graph3dFileView* view, wxWindow *parent, const wxPoint& pos, 
809                                       const wxSize& size, long style):
810 wxGLCanvas (parent, -1, pos, size, style, _T("Graph3dCanvas")
811                         ), m_pView(view)
812 {
813   parent->Show (true);
814   SetCurrent();
815 #if 0
816   // Make sure server supports the vertex array extension 
817   char* extensions = (char *) glGetString( GL_EXTENSIONS );
818   if (!extensions || !strstr( extensions, "GL_EXT_vertex_array" )) {
819     m_pView->m_bUseVertexArrays = GL_FALSE;
820   }
821 #endif
822 }
823
824
825 Graph3dFileCanvas::~Graph3dFileCanvas()
826 {
827 }
828
829 void 
830 Graph3dFileCanvas::OnDraw (wxDC& dc)
831 {
832   if (m_pView)
833     m_pView->OnDraw(& dc);
834 }
835
836 void 
837 Graph3dFileCanvas::OnSize (wxSizeEvent& event)
838 {
839 #ifndef __WXMOTIF__
840   if (!GetContext()) return;
841 #endif
842   
843   SetCurrent();
844   int width, height;
845   GetClientSize (&width, &height);
846   Reshape (width, height);
847 }
848
849 void 
850 Graph3dFileCanvas::OnChar(wxKeyEvent& event)
851 {
852   if (! m_pView)
853     return;
854   
855   wxCommandEvent dummyEvent;
856   switch (event.KeyCode()) {
857   case WXK_LEFT:
858         m_pView->m_dZRotate += 15.0;
859     Refresh (false);
860     break;
861   case WXK_RIGHT:
862     m_pView->m_dZRotate -= 15.0;
863     Refresh (false);
864     break;
865   case WXK_UP:
866     m_pView->m_dXRotate += 15.0;
867     Refresh (false);
868     break;
869   case WXK_DOWN:
870     m_pView->m_dXRotate -= 15.0;
871     Refresh (false);
872     break;
873   case 'y': case 'Y':
874     m_pView->m_dYRotate += 15.0;
875     Refresh (false);
876     break;
877   case 't': case 'T':
878     m_pView->m_dYRotate -= 15.0;
879     Refresh (false);
880     break;
881   case 'r': case 'R':
882     m_pView->OnSurface (dummyEvent);
883     break;
884   case 's': case 'S':
885     m_pView->OnSmooth (dummyEvent);
886     break;
887   case 'l': case 'L':
888     m_pView->OnLighting (dummyEvent);
889     break;
890   case 'c': case 'C':
891     m_pView->OnColor (dummyEvent);
892     break;
893   default:
894     event.Skip();
895     return;
896   }
897 }
898
899 void
900 Graph3dFileCanvas::Reshape (int width, int height)
901 {
902   glViewport (0, 0, (GLint)width, (GLint)height);
903 }
904
905
906 void 
907 Graph3dFileCanvas::OnMouseEvent(wxMouseEvent& event)
908 {
909   static int dragging = 0;
910   static float last_x, last_y;
911   
912   if (! m_pView)
913     return;
914   
915   if(event.LeftIsDown()) {
916     if(! dragging) {
917       dragging = 1;
918     } else {
919       m_pView->m_dXRotate -= (event.GetY() - last_y)*1.0;
920       m_pView->m_dZRotate += (event.GetX() - last_x)*1.0;
921       Refresh (false);
922     }
923     last_x = event.GetX();
924     last_y = event.GetY();
925   } else
926     dragging = 0;
927 }
928
929 void 
930 Graph3dFileCanvas::OnEraseBackground(wxEraseEvent& event)
931 {
932   // Do nothing: avoid flashing.
933 }
934
935
936 #endif // wxUSE_GLCANVAS