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