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