fermisurfer  2.0.0
fermisurfer
operation.cpp
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2014 Mitsuaki KAWAMURA
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24 /**@file
25 @brief Handle operations associated to mouse drag and window resize
26 */
27 #if defined(HAVE_CONFIG_H)
28 #include <config.h>
29 #endif
30 #include "wx/glcanvas.h"
31 #if defined(HAVE_GL_GLU_H)
32 #include <GL/glu.h>
33 #elif defined(HAVE_OPENGL_GLU_H)
34 #include <OpenGL/glu.h>
35 #endif
36 
37 #include <cmath>
38 #include "draw.hpp"
39 #include "variable.hpp"
40 #include "operation.hpp"
41 #include "menu.hpp"
42 
44 EVT_SIZE(TestGLCanvas::OnSize)
45 EVT_PAINT(TestGLCanvas::OnPaint)
46 EVT_CHAR(TestGLCanvas::OnChar)
47 EVT_MOUSE_EVENTS(TestGLCanvas::OnMouseEvent)
48 wxEND_EVENT_TABLE()
49 
50 /**
51  @brief Window resize
52 
53  Modify : ::sx, ::sy
54 */
55 void TestGLCanvas::OnSize(wxSizeEvent& event)
56 {
57  if (!IsShownOnScreen())
58  return;
59  // This is normally only necessary if there is more than one wxGLCanvas
60  // or more than one wxGLContext in the application.
61  SetCurrent(*m_glRC);
62 
63  /*
64  Scale of translation of mousepointer
65  */
66  sx = 1.0f / (GLfloat)event.GetSize().x;
67  sy = 1.0f / (GLfloat)event.GetSize().y;
68  // It's up to the application code to update the OpenGL viewport settings.
69  // This is OK here only because there is only one canvas that uses the
70  // context. See the cube sample for that case that multiple canvases are
71  // made current with one context.
72  glViewport(0, 0, event.GetSize().x, event.GetSize().y);
73  /**/
74  glMatrixMode(GL_PROJECTION);
75  /**/
76  glLoadIdentity();
77  gluPerspective(30.0, (GLfloat)event.GetSize().x / (GLfloat)event.GetSize().y, 1.0, 100.0);
78  //glOrtho(-1, 1, -(GLfloat)event.GetSize().y / (GLfloat)event.GetSize().x, (GLfloat)event.GetSize().y / (GLfloat)event.GetSize().x, 1.0, 100.0);
79  /**/
80  glMatrixMode(GL_MODELVIEW);
81  Refresh(false);
82  //myf->Show(true);
83 }
84 /**
85  @brief Glut mouse function
86 
87  Modify : ::cx, ::cy, ::scl
88 */
89 void TestGLCanvas::OnMouseEvent(wxMouseEvent& event)
90 {
91  static int dragging = 0;
92  static float last_x, last_y;
93  int i, j, wheel;
94  GLfloat dx, dy, a, rot0[3][3], rot1[3][3], ax, ay;
95 
96  // Allow default processing to happen, or else the canvas cannot gain focus
97  // (for key events).
98  event.Skip();
99 
100  if (event.LeftIsDown())
101  {
102  if (!dragging)
103  {
104  dragging = 1;
105  }
106  else
107  {
108  /*
109  Translation of mousepointer from starting point
110  */
111  dx = (event.GetX() - last_x) * sx;
112  dy = (event.GetY() - last_y) * sy;
113  /*
114  Distanse from starting point
115  */
116  a = sqrtf(dx * dx + dy * dy);
117  /**/
118  if (lmouse == 1) {
119  /**/
120  if (a != 0.0) {
121  /*
122  Compute rotational matrix from translation of mousepointer
123  */
124  ax = -dy;
125  ay = dx;
126  /**/
127  a = a * 10.0f;
128  /**/
129  rot0[0][0] = (ax * ax + ay * ay * cosf(a)) / (ax * ax + ay * ay);
130  rot0[0][1] = ax * ay * (cosf(a) - 1.0f) / (ax * ax + ay * ay);
131  rot0[0][2] = ay * sinf(a) / sqrtf(ax * ax + ay * ay);
132  rot0[1][0] = ax * ay * (cosf(a) - 1.0f) / (ax * ax + ay * ay);
133  rot0[1][1] = (ax * ax * cosf(a) + ay * ay) / (ax * ax + ay * ay);
134  rot0[1][2] = ax * sinf(a) / sqrtf(ax * ax + ay * ay);
135  rot0[2][0] = -ay * sinf(a) / sqrtf(ax * ax + ay * ay);
136  rot0[2][1] = -ax * sinf(a) / sqrtf(ax * ax + ay * ay);
137  rot0[2][2] = cosf(a);
138  /**/
139  for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) rot1[i][j] = rot[i][j];
140  /**/
141  for (i = 0; i < 3; i++) {
142  for (j = 0; j < 3; j++) {
143  rot[i][j] = rot0[i][0] * rot1[0][j]
144  + rot0[i][1] * rot1[1][j]
145  + rot0[i][2] * rot1[2][j];
146  }
147  }
148  /*
149  Print angle to text Box
150  */
151  thetay = asinf(rot[0][2]);
152  if (cosf(thetay) != 0.0) {
153  if (-rot[1][2] / cosf(thetay) >= 0.0) thetax = acosf(rot[2][2] / cosf(thetay));
154  else thetax = 6.283185307f - acosf(rot[2][2] / cosf(thetay));
155  if (-rot[0][1] / cosf(thetay) >= 0.0) thetaz = acosf(rot[0][0] / cosf(thetay));
156  else thetaz = 6.283185307f - acosf(rot[0][0] / cosf(thetay));
157  }
158  else {
159  thetax = 0.0;
160  if (rot[1][0] >= 0.0) thetaz = acosf(rot[1][1]);
161  else thetaz = 6.283185307f - acosf(rot[1][1]);
162  }
163  thetax *= 180.0f / 3.14159265f;
164  thetay *= 180.0f / 3.14159265f;
165  thetaz *= 180.0f / 3.14159265f;
166  myf->textbox_rotatex->ChangeValue(wxString::Format(wxT("%f"), thetax));
167  myf->textbox_rotatey->ChangeValue(wxString::Format(wxT("%f"), thetay));
168  myf->textbox_rotatez->ChangeValue(wxString::Format(wxT("%f"), thetaz));
169  myf->Show(true);
170  }
171  }
172  else if (lmouse == 2) {
173  scl = scl * expf(-dy);
174  myf->textbox_scale->ChangeValue(wxString::Format(wxT("%f"), scl));
175  linewidth *= expf(-dy);
176  myf->textbox_linewidth->ChangeValue(wxString::Format(wxT("%f"), linewidth));
177  myf->Show(true);
178  }
179  else {
180  trans[0] = trans[0] + dx;
181  trans[1] = trans[1] - dy;
182  myf->textbox_positionx->ChangeValue(wxString::Format(wxT("%f"), trans[0]));
183  myf->textbox_positiony->ChangeValue(wxString::Format(wxT("%f"), trans[1]));
184  myf->Show(true);
185  }
186  Refresh(false);
187  }
188  last_x = event.GetX();
189  last_y = event.GetY();
190  }
191  else
192  {
193  dragging = 0;
194  }
195 
196  wheel = event.GetWheelRotation();
197  if (wheel > 0) {
198  scl = scl * 1.1f;
199  myf->textbox_scale->ChangeValue(wxString::Format(wxT("%f"), scl));
200  linewidth *= 1.1f;
201  myf->textbox_linewidth->ChangeValue(wxString::Format(wxT("%f"), linewidth));
202  myf->Show(true);
203  Refresh(false);
204  }
205  else if (wheel < 0) {
206  scl = scl * 0.9f;
207  myf->textbox_scale->ChangeValue(wxString::Format(wxT("%f"), scl));
208  linewidth *= 0.9f;
209  myf->textbox_linewidth->ChangeValue(wxString::Format(wxT("%f"), linewidth));
210  myf->Show(true);
211  Refresh(false);
212  }
213  if (event.LeftDClick()) {
214  trans[0] = (event.GetX() * sx * 2.0 - 1.0) / scl;
215  trans[1] = -(event.GetY() * sy * 2.0 - 1.0) / scl;
216  myf->textbox_positionx->ChangeValue(wxString::Format(wxT("%f"), trans[0]));
217  myf->textbox_positiony->ChangeValue(wxString::Format(wxT("%f"), trans[1]));
218  Refresh(false);
219  }
220 }
221 /**
222  @brief Glut special key function
223 
224  Modify : ::trans
225 */
226 void TestGLCanvas::OnChar(wxKeyEvent& event)
227 {
228  switch (event.GetKeyCode())
229  {
230  case 'a':
231  case WXK_LEFT:
232  trans[0] += - 0.1f;
233  myf->textbox_positionx->ChangeValue(wxString::Format(wxT("%f"), trans[0]));
234  myf->Show(true);
235  Refresh(false);
236  break;
237 
238  case 'd':
239  case WXK_RIGHT:
240  trans[0] += 0.1f;
241  myf->textbox_positionx->ChangeValue(wxString::Format(wxT("%f"), trans[0]));
242  myf->Show(true);
243  Refresh(false);
244  break;
245 
246  case 'w':
247  case WXK_UP:
248  trans[1] += 0.1f;
249  myf->textbox_positiony->ChangeValue(wxString::Format(wxT("%f"), trans[1]));
250  myf->Show(true);
251  Refresh(false);
252  break;
253 
254  case 's':
255  case WXK_DOWN:
256  trans[1] += - 0.1f;
257  myf->textbox_positiony->ChangeValue(wxString::Format(wxT("%f"), trans[1]));
258  myf->Show(true);
259  Refresh(false);
260  break;
261 
262  default:
263  event.Skip();
264  return;
265  }
266 }
267 
269 {
270  // Make the new context current (activate it for use) with this canvas.
271  SetCurrent(*m_glRC);
272 
273  glClearColor(0.0, 0.0, 0.0, 0.0);
274  glEnable(GL_DEPTH_TEST);
275  glEnable(GL_LIGHTING);
276  glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
277  glEnable(GL_LIGHT0);
278  glEnable(GL_LIGHT1);
279  glEnable(GL_NORMALIZE);
280  glEnableClientState(GL_VERTEX_ARRAY);
281  glEnable(GL_COLOR_MATERIAL);
282  PostSizeEventToParent();
283 }
284 
285 TestGLCanvas::TestGLCanvas(wxWindow* parent,
286  wxWindowID id,
287  int* gl_attrib)
288  : wxGLCanvas(parent, id, gl_attrib)
289 {
290  // Explicitly create a new rendering context instance for this canvas.
291  m_glRC = new wxGLContext(this);
292 }
293 
295 {
296  delete m_glRC;
297 }
operation.hpp
MyFrame::textbox_rotatex
wxTextCtrl * textbox_rotatex
Definition: menu.hpp:60
MyFrame::textbox_positiony
wxTextCtrl * textbox_positiony
Definition: menu.hpp:59
MyFrame::textbox_scale
wxTextCtrl * textbox_scale
Definition: menu.hpp:57
TestGLCanvas::~TestGLCanvas
virtual ~TestGLCanvas()
Definition: operation.cpp:294
TestGLCanvas::OnSize
void OnSize(wxSizeEvent &event)
linewidth
GLfloat linewidth
BZ/nodal-line/Fermiline width.
Definition: fermisurfer.cpp:193
trans
GLfloat trans[3]
Translation.
Definition: fermisurfer.cpp:186
MyFrame::textbox_rotatez
wxTextCtrl * textbox_rotatez
Definition: menu.hpp:62
draw.hpp
thetax
GLfloat thetax
Rotation angle.
Definition: fermisurfer.cpp:190
TestGLCanvas::m_glRC
wxGLContext * m_glRC
Definition: operation.hpp:46
TestGLCanvas::OnPaint
void OnPaint(wxPaintEvent &event)
Glut Display function called by glutDisplayFunc.
Definition: draw.cpp:554
wxBEGIN_EVENT_TABLE
wxBEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) wxEND_EVENT_TABLE() void TestGLCanvas
Window resize.
Definition: operation.cpp:43
menu.hpp
variable.hpp
Global variables.
thetaz
GLfloat thetaz
Rotation angle.
Definition: fermisurfer.cpp:192
MyFrame::textbox_linewidth
wxTextCtrl * textbox_linewidth
Definition: menu.hpp:65
sx
GLfloat sx
Scale of mouse movement.
Definition: fermisurfer.cpp:181
TestGLCanvas::TestGLCanvas
TestGLCanvas(wxWindow *parent, wxWindowID id=wxID_ANY, int *gl_attrib=NULL)
Definition: operation.cpp:285
sy
GLfloat sy
Scale of mouse movement.
Definition: fermisurfer.cpp:182
TestGLCanvas::InitGL
void InitGL()
Definition: operation.cpp:268
myf
MyFrame * myf
Definition: fermisurfer.cpp:235
TestGLCanvas::OnChar
void OnChar(wxKeyEvent &event)
Glut special key function.
Definition: operation.cpp:226
lmouse
int lmouse
Switch for the mouse function.
Definition: fermisurfer.cpp:120
TestGLCanvas
Definition: operation.hpp:30
MyFrame::textbox_positionx
wxTextCtrl * textbox_positionx
Definition: menu.hpp:58
thetay
GLfloat thetay
Rotation angle.
Definition: fermisurfer.cpp:191
TestGLCanvas::OnMouseEvent
void OnMouseEvent(wxMouseEvent &event)
Glut mouse function.
Definition: operation.cpp:89
rot
GLfloat rot[3][3]
Rotation matrix.
Definition: fermisurfer.cpp:187
scl
GLfloat scl
Initial scale.
Definition: fermisurfer.cpp:185
MyFrame::textbox_rotatey
wxTextCtrl * textbox_rotatey
Definition: menu.hpp:61