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