Module: Chem::Writer

Included in:
PDFWriter, RMagickWriter
Defined in:
lib/chem/db/vector.rb

Instance Method Summary collapse

Instance Method Details

#draw_bodyObject

:nodoc:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/chem/db/vector.rb', line 34

def draw_body # :nodoc:
  self.fbox() if @params[:fbox]
  @mol.edges.each do |bond, atom1, atom2|
    bond.color = [0, 0, 0] unless bond.color
    a1 = atom1.v_pos.dup
    a2 = atom2.v_pos.dup
    diff = a1 - a2
    len = diff.r
    # 20 % shorter
    a1 = a1 - diff * (@default_pointsize / len / 2.5) if atom1.visible
    a2 = a2 + diff * (@default_pointsize / len / 2.5) if atom2.visible

    vert = Vector[- diff[1], diff[0], 0]

    case bond.stereo
    when :up
      fill([a1, a2 + vert * 0.1, a2 - vert * 0.1], bond.color)
    when :down
      7.times do |n|
        line(a1 - diff * (1.0/ 8) * n + vert * 0.015 * n,
             a1 - diff * (1.0/ 8) * n - vert * 0.015 * n,
             bond.color)
      end
    else
      line(a1, a2, bond.color)
      if bond.v == 2
        v = Vector[atom1.x - atom2.x, atom1.y - atom2.y]
        @mol.adjacent_to(atom1).each do |e, node|
          next if node == atom2
          vv = Vector[atom1.x - node.x, atom1.y - node.y]
          #p a = v.inner_product(vv) / v.r / vv.r
          #p Math.acos(a)
        end
        line(a1 - vert * 0.15 - diff * 0.1,
             a2 - vert * 0.15 + diff * 0.1,
             bond.color)
      end
    end
  end
  @mol.nodes.each do |atom|
    params = {}
    params[:color] = atom.color if atom.color
    if atom.visible
      text(atom.element.to_s, atom.v_pos[0], atom.v_pos[1], params)
    end
  end
end

#fboxObject

:nodoc:



25
26
27
28
29
30
31
32
# File 'lib/chem/db/vector.rb', line 25

def fbox # :nodoc:
  n = @params[:orig_point]
  m = [@params[:size][0] + n[0], @params[:size][1] + n[1]]
  line([m[0], m[1]], [m[0], n[1]], [0, 0, 0])
  line([m[0], m[1]], [n[0], m[1]], [0, 0, 0])
  line([m[0], n[1]], [n[0], n[1]], [0, 0, 0])
  line([n[0], m[1]], [n[0], n[1]], [0, 0, 0])
end

#initialize(mol, params) ⇒ Object

Constructors for Vector graphics Accepts several options

:fbox

true # black line frame

:upside_down

true # turns images upside down

:size
10, 20

# set box size

:pointsize

18 # set font size



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
# File 'lib/chem/db/vector.rb', line 88

def initialize mol, params
  @mol = mol
  @params = params

  unless params[:manual] == false
    mol.nodes.each do |node|
      node.visible = true unless node.element == :C
    end
  end

  @params[:fbox] = true
  @min = Vector[
    mol.nodes.min{|atom1, atom2| atom1.x <=> atom2.x}.x,
    mol.nodes.min{|atom1, atom2| atom1.y <=> atom2.y}.y,
    mol.nodes.min{|atom1, atom2| atom1.z <=> atom2.z}.z
  ]
  @max = Vector[
    mol.nodes.max{|atom1, atom2| atom1.x <=> atom2.x}.x,
    mol.nodes.max{|atom1, atom2| atom1.y <=> atom2.y}.y,
    mol.nodes.max{|atom1, atom2| atom1.z <=> atom2.z}.z
  ]
  @size = @max - @min
  @size = Vector[10.0, 10.0, 0.0] if @size[0] == 0.0 or @size[0] == 0.0
  x = (@params[:size][0] - @params[:margin][0] * 2) / @size[0]
  y = (@params[:size][1] - @params[:margin][1] * 2) / @size[1]
  scale = x < y ? x : y
  margin = Vector[@params[:margin][0], @params[:margin][1], 0]
  orig = Vector[* @params[:orig_point] << 0.0]
  mol.nodes.each do |atom|
    atom.v_pos = (atom.pos - @min ) * scale + orig + margin
    if @params[:upside_down]
      atom.v_pos = Vector[
        atom.v_pos[0],
        @params[:size][1] + @params[:margin][1] + @params[:orig_point][1] - atom.v_pos[1],
        atom.v_pos[2]]
    end
  end
end