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