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