Class: AimsProject::CrystalViewer

Inherits:
Wx::Panel
  • Object
show all
Includes:
Aims, Gl, Glu, Math, Wx
Defined in:
lib/aims_project/crystal_viewer.rb

Constant Summary collapse

ID_ROTATE =
100
ID_PAN =
101
ID_ZOOM =
102
PICK_ID_ATOM =
0x10000000
PICK_ID_PLANE =
0x20000000
PICK_ID_BOND =
0x30000000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, parent, options = nil) ⇒ CrystalViewer

Returns a new instance of CrystalViewer.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/aims_project/crystal_viewer.rb', line 50

def initialize(controller, parent, options = nil)

  super(parent)
  @controller = controller
  
  # Register self as an observer of the options
  if options
    @options = options
  else
    @options = CrystalViewerOptions.new(parent)
  end 
  @options.add_observer(self)

  # Toolbar for the GL Canvas
  basedir = File.dirname(__FILE__)
  rotate_icon = Image.new(File.join(basedir,"rotate.gif"), BITMAP_TYPE_GIF)
  @rotate_tool = BitmapButton.new(self, :id => ID_ROTATE, :bitmap => rotate_icon.rescale(16,15).convert_to_bitmap,:name => "rotate")
  
  zoom_icon = Image.new(File.join(basedir,"zoom.gif"), BITMAP_TYPE_GIF)
  @zoom_tool = BitmapButton.new(self, :id => ID_ZOOM, :bitmap => zoom_icon.rescale(16,15).convert_to_bitmap, :name => "zoom")
  
  pan_icon = Image.new(File.join(basedir,"pan.gif"), BITMAP_TYPE_GIF)
  @pan_tool = BitmapButton.new(self, :id => ID_PAN, :bitmap => pan_icon.rescale(16,15).convert_to_bitmap, :name => "pan")

  evt_button(@rotate_tool) {|evt|
    set_mouse_motion_function(:rotate)
  }

  evt_button(@zoom_tool) {|evt|
    set_mouse_motion_function(:zoom)
  }

  evt_button(@pan_tool) {|evt|
    set_mouse_motion_function(:pan)
  }

  @buttonSizer = HBoxSizer.new
  @buttonSizer.add_item(@rotate_tool)
  @buttonSizer.add_item(@zoom_tool)
  @buttonSizer.add_item(@pan_tool)


  #@glPanel = CalendarCtrl.new(self)
  attrib = [Wx::GL_RGBA, Wx::GL_DOUBLEBUFFER, Wx::GL_DEPTH_SIZE, 24]
  @glPanel = GLCanvas.new(self, -1, [-1, -1], [-1, -1],
                             Wx::FULL_REPAINT_ON_RESIZE, "GLCanvas", attrib)
  
  vbox_sizer = VBoxSizer.new
  vbox_sizer.add_item(@buttonSizer, :flag => EXPAND)
  vbox_sizer.add_item(@glPanel, :proportion => 1, :flag => EXPAND)
  set_sizer(vbox_sizer)
  

  set_defaults
  
  # Define the method to call for paint requests
  evt_paint { @glPanel.paint { draw_scene }}
  # Create the graphics view and define event handlers

  # For some reason, not all left-clicks are captured, 
  # so we include this catchall as well, to prevent odd
  # behavior when rotating
  @glPanel.evt_mouse_events {|evt|
    if evt.button_down
      self.set_focus
      mouse_down(evt.get_x, evt.get_y)
    end
  }
  
  @glPanel.evt_left_up {|evt|
    mouse_up(evt.get_x, evt.get_y)
    draw_scene
  }
  
  @glPanel.evt_motion {|evt|
    if evt.dragging
      mouse_dragged(evt.get_x, evt.get_y)
      draw_scene
    end
  }
  
  @glPanel.evt_char {|evt| 
    nudge_dir = case evt.get_key_code
        when K_LEFT
          [-1,0,0]
        when K_RIGHT
          [1,0,0]
        when K_UP
          [0,1,0]
        when K_DOWN
          [0,-1,0]
        else
          [0,0,0]
        end
      nudge(nudge_dir)
  }
  
    # What to do when worker threads return
    evt_thread_callback {|evt|
      self.draw_scene
    }
end

Instance Attribute Details

#altObject

Returns the value of attribute alt.



36
37
38
# File 'lib/aims_project/crystal_viewer.rb', line 36

def alt
  @alt
end

#atomObject

Returns the value of attribute atom.



37
38
39
# File 'lib/aims_project/crystal_viewer.rb', line 37

def atom
  @atom
end

#atoms_changedObject

Returns the value of attribute atoms_changed.



46
47
48
# File 'lib/aims_project/crystal_viewer.rb', line 46

def atoms_changed
  @atoms_changed
end

#azObject

Returns the value of attribute az.



36
37
38
# File 'lib/aims_project/crystal_viewer.rb', line 36

def az
  @az
end

#backgroundObject

The background color (glClearColor)



33
34
35
# File 'lib/aims_project/crystal_viewer.rb', line 33

def background
  @background
end

#current_cellObject

If displaying multiple, then the current cell



27
28
29
# File 'lib/aims_project/crystal_viewer.rb', line 27

def current_cell
  @current_cell
end

#heightObject

Returns the value of attribute height.



37
38
39
# File 'lib/aims_project/crystal_viewer.rb', line 37

def height
  @height
end

#mouse_motion_funcObject

The keyboard actions



30
31
32
# File 'lib/aims_project/crystal_viewer.rb', line 30

def mouse_motion_func
  @mouse_motion_func
end

#offxObject

Returns the value of attribute offx.



36
37
38
# File 'lib/aims_project/crystal_viewer.rb', line 36

def offx
  @offx
end

#offyObject

Returns the value of attribute offy.



36
37
38
# File 'lib/aims_project/crystal_viewer.rb', line 36

def offy
  @offy
end

#offzObject

Returns the value of attribute offz.



36
37
38
# File 'lib/aims_project/crystal_viewer.rb', line 36

def offz
  @offz
end

#ortho_sideObject

Returns the value of attribute ortho_side.



35
36
37
# File 'lib/aims_project/crystal_viewer.rb', line 35

def ortho_side
  @ortho_side
end

#ortho_zmaxObject

Returns the value of attribute ortho_zmax.



35
36
37
# File 'lib/aims_project/crystal_viewer.rb', line 35

def ortho_zmax
  @ortho_zmax
end

#ortho_zminObject

Returns the value of attribute ortho_zmin.



35
36
37
# File 'lib/aims_project/crystal_viewer.rb', line 35

def ortho_zmin
  @ortho_zmin
end

#orthographicObject

Returns the value of attribute orthographic.



37
38
39
# File 'lib/aims_project/crystal_viewer.rb', line 37

def orthographic
  @orthographic
end

#pickingObject

Returns the value of attribute picking.



37
38
39
# File 'lib/aims_project/crystal_viewer.rb', line 37

def picking
  @picking
end

#render_modeObject

Returns the value of attribute render_mode.



38
39
40
# File 'lib/aims_project/crystal_viewer.rb', line 38

def render_mode
  @render_mode
end

#repeatObject

How many times to repeat the unit cell A three element vector



24
25
26
# File 'lib/aims_project/crystal_viewer.rb', line 24

def repeat
  @repeat
end

#selectionObject

Returns the value of attribute selection.



48
49
50
# File 'lib/aims_project/crystal_viewer.rb', line 48

def selection
  @selection
end

#slicesObject

Returns the value of attribute slices.



44
45
46
# File 'lib/aims_project/crystal_viewer.rb', line 44

def slices
  @slices
end

#stacksObject

Returns the value of attribute stacks.



44
45
46
# File 'lib/aims_project/crystal_viewer.rb', line 44

def stacks
  @stacks
end

#unit_cellObject

An array of Aims::UnitCell’s to display



19
20
21
# File 'lib/aims_project/crystal_viewer.rb', line 19

def unit_cell
  @unit_cell
end

#unit_cell_correctedObject (readonly)

Returns the value of attribute unit_cell_corrected.



20
21
22
# File 'lib/aims_project/crystal_viewer.rb', line 20

def unit_cell_corrected
  @unit_cell_corrected
end

#widthObject

Returns the value of attribute width.



37
38
39
# File 'lib/aims_project/crystal_viewer.rb', line 37

def width
  @width
end

#x_downObject

Returns the value of attribute x_down.



36
37
38
# File 'lib/aims_project/crystal_viewer.rb', line 36

def x_down
  @x_down
end

#x_lastObject

Returns the value of attribute x_last.



37
38
39
# File 'lib/aims_project/crystal_viewer.rb', line 37

def x_last
  @x_last
end

#xmax_planeObject

Returns the value of attribute xmax_plane.



40
41
42
# File 'lib/aims_project/crystal_viewer.rb', line 40

def xmax_plane
  @xmax_plane
end

#xmin_planeObject

Returns the value of attribute xmin_plane.



40
41
42
# File 'lib/aims_project/crystal_viewer.rb', line 40

def xmin_plane
  @xmin_plane
end

#y_downObject

Returns the value of attribute y_down.



36
37
38
# File 'lib/aims_project/crystal_viewer.rb', line 36

def y_down
  @y_down
end

#y_lastObject

Returns the value of attribute y_last.



37
38
39
# File 'lib/aims_project/crystal_viewer.rb', line 37

def y_last
  @y_last
end

#ymax_planeObject

Returns the value of attribute ymax_plane.



41
42
43
# File 'lib/aims_project/crystal_viewer.rb', line 41

def ymax_plane
  @ymax_plane
end

#ymin_planeObject

Returns the value of attribute ymin_plane.



41
42
43
# File 'lib/aims_project/crystal_viewer.rb', line 41

def ymin_plane
  @ymin_plane
end

#z_lastObject

Returns the value of attribute z_last.



37
38
39
# File 'lib/aims_project/crystal_viewer.rb', line 37

def z_last
  @z_last
end

#zmax_planeObject

Returns the value of attribute zmax_plane.



42
43
44
# File 'lib/aims_project/crystal_viewer.rb', line 42

def zmax_plane
  @zmax_plane
end

#zmin_planeObject

Returns the value of attribute zmin_plane.



42
43
44
# File 'lib/aims_project/crystal_viewer.rb', line 42

def zmin_plane
  @zmin_plane
end

Instance Method Details

#add_lightsObject



636
637
638
639
640
641
642
643
# File 'lib/aims_project/crystal_viewer.rb', line 636

def add_lights
  light0_position = [1,1,1,0]
  light1_position = [-1,-1,1,1]
  glLightModel(GL_LIGHT_MODEL_AMBIENT, [0.6, 0.6, 0.6 ,1.0])
  # glLightModel(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE)
  glLightfv(GL_LIGHT0, GL_POSITION, light0_position)              
  glLightfv(GL_LIGHT1, GL_POSITION, light1_position)
end

#alpha_image_dataObject



976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/aims_project/crystal_viewer.rb', line 976

def alpha_image_data
  # 1 byte/pixel
  bytewidth = self.width;
  
  bytes = bytewidth*height;
  @glPanel.set_current
  glFinish

  # Setup pixel store
  glPixelStorei(Gl::GL_PACK_ALIGNMENT, 1) # Force byte alignment
  glPixelStorei(Gl::GL_PACK_ROW_LENGTH, 0)
  glPixelStorei(Gl::GL_PACK_SKIP_ROWS, 0)
  glPixelStorei(Gl::GL_PACK_SKIP_PIXELS, 0)
  
  glReadPixels(0, 0, self.width, self.height, Gl::GL_ALPHA, Gl::GL_UNSIGNED_BYTE)
end

#apply_projectionObject



509
510
511
512
513
514
515
516
517
518
# File 'lib/aims_project/crystal_viewer.rb', line 509

def apply_projection
  glMatrixMode(GL_PROJECTION)
  glLoadIdentity()
  if self.orthographic
    glOrtho(-self.ortho_side, self.ortho_side, -self.ortho_side,self.ortho_side, self.ortho_zmin, self.ortho_zmax)
  else
    gluPerspective(60, self.width/self.height, 1, 120)
  end
  glMatrixMode(GL_MODELVIEW)
end

#bond_length=(l) ⇒ Object



283
284
285
286
287
# File 'lib/aims_project/crystal_viewer.rb', line 283

def bond_length=(l)
  @options.bond_length = l
  # FIXME The bonds should be made in the controller
  # self.unit_cell.each{|uc| uc.make_bonds(l)} if self.unit_cell
end

#cast_ray_to(x, y) ⇒ Object

Cast a ray from the near clip-plane to the far clip-plane through the point x,y return a two element array

element 0 is the model space coordinates of (x,y) on the near clip plane 
element 1 is the direction of the ray


380
381
382
383
384
# File 'lib/aims_project/crystal_viewer.rb', line 380

def cast_ray_to(x,y)
  x1,y1,z1 = unproject(x,y,0)
  x2,y2,z2 = unproject(x,y,1)
  [ Vector[x1, y1, z1], Vector[x2-x1, y2-y1, z2-z1] ]
end

#current_unit_cellObject

The currently displayed unit cell



272
273
274
# File 'lib/aims_project/crystal_viewer.rb', line 272

def current_unit_cell
  self.unit_cell
end

#delete_atomObject



359
360
361
362
363
364
# File 'lib/aims_project/crystal_viewer.rb', line 359

def delete_atom
  if self.atom
    # Remove 
    self.unit_cell.remove_atom(self.atom)
  end
end

#draw_bonds(origin = [0,0,0]) ⇒ Object



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/aims_project/crystal_viewer.rb', line 713

def draw_bonds(origin = [0,0,0])
  return unless self.unit_cell
  atoms = if @options.correct
    @unit_cell_corrected
  else
      @unit_cell
  end
  return unless atoms.bonds

  Material.black.apply
  glLineWidth(1.0)
  glBegin(GL_LINES)
  atoms.bonds.each{|b|
    glVertex3f(origin[0] + b[0].x, origin[1] + b[0].y, origin[2] + b[0].z)
    glVertex3f(origin[0] + b[1].x, origin[1] + b[1].y, origin[2] + b[1].z)				
  }
  glEnd()
end

#draw_clip_planesObject



869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# File 'lib/aims_project/crystal_viewer.rb', line 869

def draw_clip_planes
  return unless self.unit_cell
  atoms = self.unit_cell

  bb = atoms.bounding_box
  bbx1 = bb[0].x
  bbx2 = bb[1].x
  bby1 = bb[0].y
  bby2 = bb[1].y
  bbz1 = bb[0].z
  bbz2 = bb[1].z

  # draw z_planes
  # The are bounded by the min and max points in the x-y plane

  if @options.show_zclip and @zmax_plane and @zmin_plane
    z = -1*@zmax_plane.distance_to_point(0,0,0)
    draw_plane(@zmax_plane, [[bbx1, bby1, z],
                             [bbx2, bby1, z],
                             [bbx2, bby2, z], 
                             [bbx1, bby2, z]],1)

     z = @zmin_plane.distance_to_point(0,0,0)
     draw_plane(@zmin_plane, [[bbx1, bby1, z],
                              [bbx2, bby1, z],
                              [bbx2, bby2, z], 
                              [bbx1, bby2, z]],2)
  end

  if @options.show_xclip and @xmax_plane and @xmin_plane
    x = -1*@xmax_plane.distance_to_point(0,0,0)
    draw_plane(@xmax_plane, [[x, bby1, bbz1],
                             [x, bby1, bbz2],
                             [x, bby2, bbz2], 
                             [x, bby2, bbz1]],3)

     x = @xmin_plane.distance_to_point(0,0,0)
     draw_plane(@xmin_plane, [[x, bby1, bbz1],
                              [x, bby1, bbz2],
                              [x, bby2, bbz2], 
                              [x, bby2, bbz1]],4)
  end

  if @options.show_yclip and @ymax_plane and @ymin_plane
    y = -1*@ymax_plane.distance_to_point(0,0,0)
    draw_plane(@ymax_plane, [[bbx1, y, bbz1],
                             [bbx1, y, bbz2],
                             [bbx2, y, bbz2], 
                             [bbx2, y, bbz1]],5)

     y = @ymin_plane.distance_to_point(0,0,0)
     draw_plane(@ymin_plane, [[bbx1, y, bbz1],
                              [bbx1, y, bbz2],
                              [bbx2, y, bbz2], 
                              [bbx2, y, bbz1]],6)
  end

end

#draw_initObject



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/aims_project/crystal_viewer.rb', line 520

def draw_init
  glClearColor(background.r,background.g,background.b,(@options.solid_bg ? 1.0 : 0.0))
  glEnable(GL_DEPTH_TEST)
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)


  # Antialiasing
  glEnable(GL_LINE_SMOOTH)
  glEnable(GL_BLEND)
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
  glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST)

  if @options.show_lighting
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
  else
    glDisable(GL_LIGHTING)
  end

  # Point size
  glPointSize(5)
  glLineWidth(2)
end

#draw_lattice(origin = [0,0,0]) ⇒ Object



760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/aims_project/crystal_viewer.rb', line 760

def draw_lattice(origin = [0,0,0])

  return unless self.unit_cell
  
  atoms = if @options.correct
    @unit_cell.correct
  else
    @unit_cell
  end
  
  return unless atoms
  
  # Create sphere object
  rmin = 0.2
  rmax = 0.5

  if self.atoms_changed
    atoms.recache_visible_atoms 
    self.atoms_changed = false
  end
  
  # Calculate radius scaling factor vs. depth
  rrange = rmax - rmin
  # bb = atoms.bounding_box
  zmin = atoms.min{|a,b| a.z <=> b.z}.z
  zmax = atoms.max{|a,b| a.z <=> b.z}.z
  zrange = zmax - zmin
  if zrange == 0
    rscale = 0
  else
    rscale = rrange/zrange
  end



  sphere_quadric = nil
  for a in atoms
    a.material.apply(@options.show_lighting)
    case self.render_mode 
    when :ball_stick
      r = rmin+(a.z - zmin)*rscale
    else
      r = 2.0
    end
    
    sphere_quadric = draw_sphere(origin[0] + a.x, origin[1] + a.y, origin[2] + a.z, r, a.id, sphere_quadric)
    
  end
  gluDeleteQuadric(sphere_quadric) if sphere_quadric

end

#draw_plane(plane, lineLoop, pickid) ⇒ Object

Draw a plane use the vertices defined in lineLoop as the boundary of the plane



834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/aims_project/crystal_viewer.rb', line 834

def draw_plane(plane, lineLoop, pickid)

  glPushName(pickid | PICK_ID_PLANE) if self.picking
  
  # if is_picked?(:plane, pickid)
  #   Material.black.apply
  #   glLineWidth(3.0)
  #   glEdgeFlag(GL_TRUE)
  # end
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

  Material.new(0.9, 0.9, 0.0, 0.5).apply
  glBegin(GL_TRIANGLE_FAN)
  glNormal3f(plane.a, plane.b, plane.c)
  lineLoop.each{|p| 
      glVertex3f(p[0], p[1], p[2])
  }
  glEnd()
  
  glEdgeFlag(GL_FALSE)
  glPopName() if picking
  
  if (is_picked?(:plane, pickid))
    Material.black.apply
    glLineWidth(3.0)
    glBegin(GL_LINE_LOOP)
    lineLoop.each{|p|
      glVertex3f(p[0], p[1], p[2])
    }
    glEnd()
    glLineWidth(1.0)
  end
  
end

#draw_sceneObject



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/aims_project/crystal_viewer.rb', line 461

def draw_scene
  
  @glPanel.set_current
  sz = @glPanel.size
  viewport_setup(sz.width, sz.height)    
  
  
  begin
    draw_init
    apply_projection
    position_camera
    add_lights if @options.show_lighting
    outline_supercell if @options.show_supercell
    
    if self.unit_cell
      atoms = self.unit_cell
      @options.x_repeat.times do |i|
        @options.y_repeat.times do |j|
          @options.z_repeat.times do |k|
          
            origin = if self.unit_cell.lattice_vectors
              atoms.lattice_vectors[0]*i + atoms.lattice_vectors[1]*j + atoms.lattice_vectors[2]*k
            else
              Vector[0, 0, 0]
            end
            
            draw_bonds(origin) if @options.show_bonds
            draw_lattice(origin)
          end
        end
      end
    end
    draw_clip_planes
  rescue GeometryEvaluationException => e
  rescue AimsProjectException => e
    puts e.message
    puts e.backtrace.join("\n")
  end
  
  @glPanel.swap_buffers
end

#draw_sphere(x, y, z, r, name, sphere_quadric = nil) ⇒ Object

Draw an atom

Parameters:

  • x

    The x-coordinate

  • y

    The y-coordinate

  • z

    The z-coordinate

  • r

    The radius

  • name

    The name of the sphere (for picking)

Returns:

  • a sphere_quadric for reuse if desired. Make sure to delete it when done



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/aims_project/crystal_viewer.rb', line 740

def draw_sphere(x,y,z,r, name, sphere_quadric=nil)

  unless sphere_quadric 
    sphere_quadric = gluNewQuadric()
    gluQuadricDrawStyle(sphere_quadric, GLU_FILL)
    gluQuadricNormals(sphere_quadric, GLU_SMOOTH)
    gluQuadricOrientation(sphere_quadric, GLU_OUTSIDE)
  end

  # Load a new matrix onto the stack
  glPushMatrix()
  glPushName(name | PICK_ID_ATOM) if self.picking
  glTranslatef(x, y, z)
  gluSphere(sphere_quadric, r, slices, stacks)            
  glPopName() if picking
  glPopMatrix()
  
  sphere_quadric
end

#dump_geometryObject



276
277
278
279
280
281
# File 'lib/aims_project/crystal_viewer.rb', line 276

def dump_geometry
  atoms = self.unit_cell
  if atoms
    puts atoms.format_geometry_in
  end
end

#dump_propertiesObject



265
266
267
268
269
# File 'lib/aims_project/crystal_viewer.rb', line 265

def dump_properties
  self.instance_variables.each{|v|
    puts "#{v} = #{self.instance_variable_get(v)}"
  }
end

#hiResObject



443
444
445
446
# File 'lib/aims_project/crystal_viewer.rb', line 443

def hiRes
  self.slices = 20
  self.stacks = 20
end

#imageObject



949
950
951
952
953
954
# File 'lib/aims_project/crystal_viewer.rb', line 949

def image
  image = Image.new(self.width, self.height)
  image.set_rgb_data(self.rgb_image_data)
  image.set_alpha_data(self.alpha_image_data)
	image
end

#init_clip_planesObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/aims_project/crystal_viewer.rb', line 226

def init_clip_planes
    
    # Thread.new(self) { |evtHandler|
      # @unit_cell_corrected = @unit_cell.correct
    #   evt = ThreadCallbackEvent.new
    #   evtHandler.add_pending_event(evt)
    # }
  
    return unless (@unit_cell and @unit_cell.is_valid?) 
  
    # each bounding box is a 2 element array [max, min]
    bounding_box = @unit_cell.bounding_box(false)
    xmax = bounding_box[0].x
    xmin = bounding_box[1].x
    ymax = bounding_box[0].y
    ymin = bounding_box[1].y
    zmax = bounding_box[0].z
    zmin = bounding_box[1].z

    @xmax_plane = Plane.new( 1, 0, 0, xmax, 0, 0)
    @xmin_plane = Plane.new(-1, 0, 0, xmin, 0, 0)
    @ymax_plane = Plane.new( 0, 1, 0, 0, ymax, 0)
    @ymin_plane = Plane.new( 0,-1, 0, 0, ymin, 0)
    @zmax_plane = Plane.new( 0, 0, 1, 0, 0, zmax)
    @zmin_plane = Plane.new( 0, 0,-1, 0, 0, zmin)

    # Add clip-planes to each unit cell
    @unit_cell.clear_planes
    @unit_cell.add_plane(@xmax_plane, false)
    @unit_cell.add_plane(@xmin_plane, false)
    @unit_cell.add_plane(@ymax_plane, false)
    @unit_cell.add_plane(@ymin_plane, false)
    @unit_cell.add_plane(@zmax_plane, false)
    @unit_cell.add_plane(@zmin_plane, false)
    @unit_cell.recache_visible_atoms
    @unit_cell.make_bonds(@options.bond_length)
  
end

#is_picked?(type, id) ⇒ Boolean

a test method to see of an object of a particular type was picked. valid types are :atom, :plane

Returns:

  • (Boolean)


814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/aims_project/crystal_viewer.rb', line 814

def is_picked?(type, id)
  if @picked
  case type
    when :atom
      @picked[:atoms].member? id
    when :plane
      @picked[:planes].member? id
    when :bond
      @picked[:bonds].member? id
    else
      false
    end
  else
    false
  end
end

#loResObject



438
439
440
441
# File 'lib/aims_project/crystal_viewer.rb', line 438

def loRes
  self.slices = 5
  self.stacks = 5
end

#mouse_down(x, y) ⇒ Object



289
290
291
292
293
294
295
# File 'lib/aims_project/crystal_viewer.rb', line 289

def mouse_down(x,y)
  self.x_down = x
  self.y_down = y
  self.x_last = x
  self.y_last = y	  
  self.loRes
end

#mouse_dragged(x, y) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/aims_project/crystal_viewer.rb', line 337

def mouse_dragged(x, y)

  # Harmless correction for bug in WxRuby that doesn't register all mouse down events
  self.x_last = x if self.x_last.nil?
  self.y_last = y if self.y_last.nil? 
  self.z_last = z if self.z_last.nil?
  
  case self.mouse_motion_func
  when :rotate 
    rotate(x,y)
  when :zoom
    zoom(x,y)
  when :pan
    pan(x,y)
  when :move_clip_plane
    move_clip_plane(x,y)
  else
    rotate(x,y)
  end
  glutPostRedisplay if @using_glut
end

#mouse_up(x, y) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/aims_project/crystal_viewer.rb', line 297

def mouse_up(x,y)
  return unless self.unit_cell
  @picked = pick_object(x,y)
  self.atom = self.unit_cell.atoms.find{|a| a.id == @picked[:atoms].last}
  if self.atom
    @controller.select_atom(self.atom)
    # puts self.atom.format_geometry_in
  end
  
  unless @picked[:planes].empty?
    clip_plane_id = @picked[:planes].first
    @active_clip_plane = case clip_plane_id
    when 1 
      @zmax_plane
    when 2
      @zmin_plane
    when 3
      @xmax_plane
    when 4
      @xmin_plane
    when 5
      @ymax_plane
    when 6
      @ymin_plane
    else
      nil
    end
    if @active_clip_plane
      set_mouse_motion_function(:move_clip_plane)
    end
  end
  self.hiRes

  # Harmless correction for bug in WxRuby that doesn't register all mouse down events
  self.x_last = nil
  self.y_last = nil

  glutPostRedisplay if @using_glut
end

#move_clip_plane(x, y) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/aims_project/crystal_viewer.rb', line 386

def move_clip_plane(x,y)
  
  x_obj_last, y_obj_last, z_obj_last = unproject(x_last, y_last, 0)
  x_obj, y_obj, z_obj = unproject(x,y,0)

  w = [x_obj - x_obj_last, y_obj - y_obj_last, z_obj - z_obj_last]
  n = @active_clip_plane.unit_normal
  d = w[0]*n[0] + w[1]*n[1] + w[2]*n[2]
  
  # scale = 0.1
  # dx = (x - self.x_last)
  # dy = (y - self.y_last)
  # dr = sqrt(dx*dx + dy*dy)*(0 > dy ? -1 : 1)*scale
  self.x_last = x
  self.y_last = y

  @active_clip_plane.displace_along_normal(d)
  self.atoms_changed = true
end

#nudge(dir) ⇒ Object



197
198
199
# File 'lib/aims_project/crystal_viewer.rb', line 197

def nudge(dir)
  @controller.nudge_selected_atoms(dir[0]*0.5, dir[1]*0.5, 0)
end

#outline_supercellObject



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/aims_project/crystal_viewer.rb', line 645

def outline_supercell
  return unless self.unit_cell
  uc = self.unit_cell

  vecs = uc.lattice_vectors
  return unless vecs

  origin = [0, 0, 0]
  v1 = vecs[0]
  v2 = vecs[1]
  v3 = vecs[2]

  # Corner #1
  c1 = v1 + v3

  # Corner #2
  c2 = v2 + v3

  # Corner #3
  c3 = v1 + v2

  # Corner #4
  c4 = v1 + v2 + v3

  Material.black.apply
  glLineWidth(1.0)

  glBegin(GL_LINES)

  glVertex3f(origin[0], origin[1], origin[2])
  glVertex3f(v1[0], v1[1], v1[2])				

  glVertex3f(origin[0], origin[1], origin[2])
  glVertex3f(v2[0], v2[1], v2[2])       

  glVertex3f(origin[0], origin[1], origin[2])
  glVertex3f(v3[0], v3[1], v3[2])       

  glVertex3f(v1[0], v1[1], v1[2])
  glVertex3f(c3[0], c3[1], c3[2])       

  glVertex3f(v2[0], v2[1], v2[2])
  glVertex3f(c3[0], c3[1], c3[2])       

  glVertex3f(c3[0], c3[1], c3[2])       
  glVertex3f(c4[0], c4[1], c4[2])       

  glVertex3f(v1[0], v1[1], v1[2])
  glVertex3f(c1[0], c1[1], c1[2])       

  glVertex3f(v2[0], v2[1], v2[2])
  glVertex3f(c2[0], c2[1], c2[2])       

  glVertex3f(c1[0], c1[1], c1[2])       
  glVertex3f(c4[0], c4[1], c4[2])       

  glVertex3f(c2[0], c2[1], c2[2])       
  glVertex3f(c4[0], c4[1], c4[2])       

  glVertex3f(v3[0], v3[1], v3[2])
  glVertex3f(c1[0], c1[1], c1[2])       

  glVertex3f(v3[0], v3[1], v3[2])
  glVertex3f(c2[0], c2[1], c2[2])       

  glEnd()        
end

#pan(x, y) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/aims_project/crystal_viewer.rb', line 423

def pan(x,y)
  
  z = 0.0 # This value is normalized with respect to the near and far clip planes

  obj_x, obj_y, obj_z = unproject(x, y, z)
  obj_x_last, obj_y_last, obj_z_last = unproject(x_last, y_last, z_last)
  @center.x += obj_x_last - obj_x
  @center.y += obj_y_last - obj_y
  @center.z += obj_z_last - obj_z
  
  self.x_last = x
  self.y_last = y
  self.z_last = z
end

#pick_object(x, y) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/aims_project/crystal_viewer.rb', line 545

def pick_object(x,y)

  buf = glSelectBuffer(512)
  glRenderMode(GL_SELECT)
  model = glGetDoublev(GL_MODELVIEW_MATRIX)
  proj = glGetDoublev(GL_PROJECTION_MATRIX)
  viewport = glGetIntegerv(GL_VIEWPORT);
  
  self.picking = true
  glMatrixMode(GL_PROJECTION)
  glLoadIdentity()
  gluPickMatrix(x,viewport[3]-y,5,5,viewport)
  if self.orthographic
    glOrtho(-self.ortho_side, self.ortho_side, -self.ortho_side,self.ortho_side, self.ortho_zmin, self.ortho_zmax)
  else
    gluPerspective(60, self.width/self.height, 1, 60)
  end
  
  z = 0.0 # This value is normalized with respect to the near and far clip planes
  obj_x, obj_y, obj_z = gluUnProject(x, viewport[3]-y, z, model, proj, viewport)
  
  glMatrixMode(GL_MODELVIEW)

  glInitNames
  if self.unit_cell 
    atoms = self.unit_cell
    @options.x_repeat.times do |i|
      @options.y_repeat.times do |j|
        @options.z_repeat.times do |k|
          
          # Hack to display non-periodic systems
          origin = if self.unit_cell.lattice_vectors
            atoms.lattice_vectors[0]*i + atoms.lattice_vectors[1]*j + atoms.lattice_vectors[2]*k
          else
            Vector[0,0,0]
          end
          
          self.draw_lattice(origin)
        end
      end
    end
  end
  self.draw_clip_planes

  self.picking = false

  glMatrixMode(GL_MODELVIEW);
  glFlush();

  count = glRenderMode(GL_RENDER)
  data = buf.unpack("L!*")
  names = []
  count.times do 
    num_names = data.shift
    min_depth = data.shift
    max_depth = data.shift
    num_names.times do 
      names << data.shift
    end
  end

  picked_objects = {:atoms => [], :planes => [], :bonds => []}
  names.each{|n|
    if (n & PICK_ID_ATOM) == PICK_ID_ATOM
      picked_objects[:atoms] << (n ^ PICK_ID_ATOM)
    end
    if (n & PICK_ID_PLANE) == PICK_ID_PLANE
      picked_objects[:planes] << (n ^ PICK_ID_PLANE)
    end
  }

  picked_objects
end

#position_cameraObject



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/aims_project/crystal_viewer.rb', line 619

def position_camera
  return unless self.unit_cell
  atoms = self.unit_cell
  
  # Find the center of all atoms, not just visible ones.
  @center = atoms.center unless @center
  
  # Move camera out along z-axis
  glMatrixMode(GL_MODELVIEW)
  glLoadIdentity()

  glTranslatef(self.offx,self.offy,self.offz)
  glRotatef(self.alt, 1, 0, 0)
  glRotatef(self.az, 0, 0, 1)
  glTranslatef(-@center.x, -@center.y, -@center.z)
end

#rgb_image_dataObject



956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/aims_project/crystal_viewer.rb', line 956

def rgb_image_data

  # 3 bytes/pixel
  bytewidth = self.width*3
  bytes = bytewidth * self.height

  # Finish any pending Commands
  @glPanel.set_current
  glFinish

  # Setup pixel store
  glPixelStorei(Gl::GL_PACK_ALIGNMENT, 1) # Force byte alignment
  glPixelStorei(Gl::GL_PACK_ROW_LENGTH, 0)
  glPixelStorei(Gl::GL_PACK_SKIP_ROWS, 0)
  glPixelStorei(Gl::GL_PACK_SKIP_PIXELS, 0)

  glReadPixels(0, 0, self.width, self.height, Gl::GL_RGB, Gl::GL_UNSIGNED_BYTE)

end

#rgba_image_dataObject



928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
# File 'lib/aims_project/crystal_viewer.rb', line 928

def rgba_image_data

  # 4 bytes/pixel
  bytewidth = self.width*4
  bytewidth = (bytewidth.to_i + 3) & ~3 # Align to 4 bytes
  bytes = bytewidth * self.height

  # Finish any pending Commands
  @glPanel.set_current
  glFinish

  # Setup pixel store
  glPixelStorei(Gl::GL_PACK_ALIGNMENT, 4) # Force 4-byte alignment
  glPixelStorei(Gl::GL_PACK_ROW_LENGTH, 0)
  glPixelStorei(Gl::GL_PACK_SKIP_ROWS, 0)
  glPixelStorei(Gl::GL_PACK_SKIP_PIXELS, 0)

  glReadPixels(0, 0, self.width, self.height, Gl::GL_RGBA, Gl::GL_UNSIGNED_BYTE)

end

#rotate(x, y) ⇒ Object



406
407
408
409
410
411
# File 'lib/aims_project/crystal_viewer.rb', line 406

def rotate(x,y)
  self.az += 5*(x - self.x_last)
  self.alt += 5*(y - self.y_last)
  self.x_last = x
  self.y_last = y
end

#set_defaultsObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/aims_project/crystal_viewer.rb', line 158

def set_defaults
  self.current_cell = 0
  self.background = Material.new(0.7, 0.7, 1.0, 1)
  self.x_down = 0
  self.y_down = 0
  self.x_last = 0
  self.y_last = 0
  self.z_last = 0
  self.alt = 0
  self.az = 0
  self.offx = 0
  self.offy = 0
  self.offz = -20
  self.orthographic = true
  self.ortho_side = 15
  self.ortho_zmin = -1
  self.ortho_zmax = 50
  self.width = 500
  self.height = 500
  self.picking =false      
  # The last clicked atom
  self.atom = nil
  self.atoms_changed = true
  self.hiRes
  self.mouse_motion_func = :rotate
  self.repeat = Vector[1,1,1]
  self.render_mode = :ball_stick
  @options.set_properties(
    :bond_length => 3,
    :show_bonds => true,
    :show_lighting => true,
    :show_xclip => false,
    :show_yclip => false,
    :show_zclip => true,
    :show_supercell => true
  )
end

#set_mouse_motion_function(method_name) ⇒ Object

Executes the named method when the mouse is dragged



457
458
459
# File 'lib/aims_project/crystal_viewer.rb', line 457

def set_mouse_motion_function(method_name)
  self.mouse_motion_func = method_name.to_sym
end

#set_view(offset_x, offset_y, offset_z, alt, az) ⇒ Object



448
449
450
451
452
453
454
# File 'lib/aims_project/crystal_viewer.rb', line 448

def set_view(offset_x, offset_y, offset_z, alt, az)
  self.offx = offset_x
  self.offy = offset_y
  self.offz = offset_z
  self.alt = alt
  self.az = az
end

#unit_cell_changedObject



221
222
223
224
# File 'lib/aims_project/crystal_viewer.rb', line 221

def unit_cell_changed
  init_clip_planes
  draw_scene
end

#unproject(x, y, z) ⇒ Object

Unproject the mouse coordinates to model space Handles the flipping of the y-coordinate



368
369
370
371
372
373
374
# File 'lib/aims_project/crystal_viewer.rb', line 368

def unproject(x,y,z)
  model = glGetDoublev(GL_MODELVIEW_MATRIX)
  proj = glGetDoublev(GL_PROJECTION_MATRIX)
  viewport = glGetIntegerv(GL_VIEWPORT);

  gluUnProject(x, viewport[3]-y, z, model, proj, viewport)
end

#updateObject

Called when the options changes



154
155
156
# File 'lib/aims_project/crystal_viewer.rb', line 154

def update
  self.draw_scene
end

#viewport_setup(width, height) ⇒ Object



503
504
505
506
507
# File 'lib/aims_project/crystal_viewer.rb', line 503

def viewport_setup(width, height)
  self.width = width
  self.height = height
  glViewport(0, 0, self.width, self.height)
end

#zoom(x, y) ⇒ Object



413
414
415
416
417
418
419
420
421
# File 'lib/aims_project/crystal_viewer.rb', line 413

def zoom(x,y)
  if self.orthographic
    self.ortho_side -= (y - self.y_last)*0.1
  else
    self.offz -= (y - self.y_last)*0.1
  end
  self.x_last = x
  self.y_last = y
end