Class: CrystalCell::Povray

Inherits:
Object
  • Object
show all
Defined in:
lib/crystalcell/povray.rb,
lib/crystalcell/povray.rb

Defined Under Namespace

Classes: Camera, Cell, Color, Cylinder, Element, Tetrahedron, Triangle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cell:, camera_info: {}, environments: DEFAULT_ENVIRONMENTS) ⇒ Povray

camera_info indicates hash with the keys of camera info,

e.g., :camera_type, :location, :direction, :right, :sky, :up, :look_at


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/crystalcell/povray.rb', line 24

def initialize(cell: , camera_info: {}, environments: DEFAULT_ENVIRONMENTS)
  @camera      = CrystalCell::Povray::Camera.new(camera_info)
  @environments = environments
  @cell = cell.to_povcell
  @axes = nil
  @objects     = []

  ## @camera.look_at
  center = Mageo::Vector3D[0.0, 0.0, 0.0]
  @cell.axes.each { |axis| center += axis.to_v3d * 0.5 }
  @camera.look_at = center
end

Instance Attribute Details

#axesObject (readonly)

Returns the value of attribute axes.



20
21
22
# File 'lib/crystalcell/povray.rb', line 20

def axes
  @axes
end

#cameraObject (readonly)

Returns the value of attribute camera.



20
21
22
# File 'lib/crystalcell/povray.rb', line 20

def camera
  @camera
end

#cellObject (readonly)

Returns the value of attribute cell.



20
21
22
# File 'lib/crystalcell/povray.rb', line 20

def cell
  @cell
end

#environmentsObject (readonly)

Returns the value of attribute environments.



20
21
22
# File 'lib/crystalcell/povray.rb', line 20

def environments
  @environments
end

Instance Method Details

#add(object) ⇒ Object

object should have dump method



135
136
137
# File 'lib/crystalcell/povray.rb', line 135

def add(object)
  @objects << object
end

#camera_location(vector) ⇒ Object

Reset camera location, using relative vector from center of cell with cartesian coordinate.



39
40
41
42
43
# File 'lib/crystalcell/povray.rb', line 39

def camera_location(vector)
  look_at = @camera.look_at || [0.0, 0.0, 0.0]
  look_at = Mageo::Vector3D[*look_at]
  @camera.location = look_at + Mageo::Vector3D[*vector]
end

#camera_location_polar(r, theta, phi) ⇒ Object

Reset camera location, using relative vector from center of cell with polar coordinates. theta and phi is set as degree. Not radian.



48
49
50
51
52
53
54
55
# File 'lib/crystalcell/povray.rb', line 48

def camera_location_polar(r, theta, phi)
  theta =  (2.0 * Math::PI) * theta / 360.0
  phi   =  (2.0 * Math::PI) * phi   / 360.0

  look_at = Mageo::Vector3D[*(@camera.look_at || [0.0, 0.0, 0.0])]
  polar = Mageo::Polar3D.new(r, theta, phi)
  @camera.location = look_at + polar.to_v3d
end

#dump(io) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/crystalcell/povray.rb', line 139

def dump(io)
  @camera.dump(io)
  @environments.each { |item| io.puts item }
  @cell.dump(io)

  if @axes
    @axes.each {|axis| axis.dump(io)}
  end
  
  @objects.each { |obj| obj.dump(io) }
end

#set_axes(position) ⇒ Object

3つの棒で座標軸を配置x, y, z 軸をそれぞれ red, green, blue で表示。



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/crystalcell/povray.rb', line 117

def set_axes(position)
  #o = Vector3D[-1.0, -1.0, 0.0]
  o = Mageo::Vector3D[*position]
  x = Mageo::Vector3D[1.0, 0.0, 0.0]
  y = Mageo::Vector3D[0.0, 1.0, 0.0]
  z = Mageo::Vector3D[0.0, 0.0, 1.0]
  ox = o + x
  oy = o + y
  oz = o + z

  @axes = [
    CrystalCell::Povray::Cylinder.new(o, ox, 0.04, x),
    CrystalCell::Povray::Cylinder.new(o, oy, 0.04, y),
    CrystalCell::Povray::Cylinder.new(o, oz, 0.04, z)
  ]
end

#shoot_4in1(basename, remain_intermediate = false) ⇒ Object

shoot 4 angles and unite.



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
# File 'lib/crystalcell/povray.rb', line 67

def shoot_4in1(basename, remain_intermediate = false)
  name_x = basename + '-x'
  name_y = basename + '-y'
  name_z = basename + '-z'
  name_w = basename + '-w'
  name_zw = basename + '-zw'
  name_xy = basename + '-xy'
  #name_zwxy = basename + '-zwxy'
  name_zwxy = basename

  r = 10.0
  povray = Marshal.load(Marshal.dump(self))
  povray.camera_location_polar(r, 0, 0)   ; povray.shoot_snap( name_z )
  povray.camera_location_polar(r, 90, 0)  ; povray.shoot_snap( name_x )
  povray.camera_location_polar(r, 90, 90) ; povray.shoot_snap( name_y )
  #povray.camera_location_polar(r, 80, 70) ; povray.shoot_snap( name_w )
  povray.camera_location_polar(r, 120, 230) ; povray.shoot_snap( name_w )

  system "convert +append #{name_z }.png #{name_w }.png #{name_zw  }.png"
  system "convert +append #{name_x }.png #{name_y }.png #{name_xy  }.png"
  system "convert -append #{name_zw}.png #{name_xy}.png #{name_zwxy}.png"

  #中間ファイルを消す。
  unless remain_intermediate
    [
      name_w + '.png',
      name_w + '.pov',
      name_x + '.png',
      name_x + '.pov',
      name_y + '.png',
      name_y + '.pov',
      name_z + '.png',
      name_z + '.pov',
      name_zw + '.png',
      name_xy + '.png',
    ].each do |file|
      FileUtils.rm file if FileTest.exist? file
    end
  end
end

#shoot_snap(basename) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/crystalcell/povray.rb', line 58

def shoot_snap(basename)
  povfile = basename + '.pov'
  File.open(povfile, 'w') do |io|
    dump(io)
  end
  system "povray -D #{povfile}"
end