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