class CrystalCell::Povray

Constants

DEFAULT_ENVIRONMENTS

Attributes

axes[R]
camera[R]
cell[R]
environments[R]

Public Class Methods

new(cell: , camera_info: {}, environments: DEFAULT_ENVIRONMENTS) click to toggle source

camera_info indicates hash with the keys of camera info,

e.g., :camera_type, :location, :direction, :right, :sky, :up, :look_at
# File lib/crystalcell/povray.rb, line 25
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

Public Instance Methods

add(object) click to toggle source

object should have dump method

# File lib/crystalcell/povray.rb, line 136
def add(object)
  @objects << object
end
camera_location(vector) click to toggle source

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

# File lib/crystalcell/povray.rb, line 40
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) click to toggle source

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

# File lib/crystalcell/povray.rb, line 49
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) click to toggle source
# File lib/crystalcell/povray.rb, line 140
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) click to toggle source

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

# File lib/crystalcell/povray.rb, line 118
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) click to toggle source

shoot 4 angles and unite.

# File lib/crystalcell/povray.rb, line 68
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) click to toggle source
# File lib/crystalcell/povray.rb, line 59
def shoot_snap(basename)
  povfile = basename + '.pov'
  File.open(povfile, 'w') do |io|
    dump(io)
  end
  system "povray -D #{povfile} > /dev/null"
end