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