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