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