Class: ImageTooth

Inherits:
Object
  • Object
show all
Defined in:
lib/image_tooth.rb

Constant Summary collapse

@@colors =
{ :n => 'black', :w => 'white', :g => 'green',
:r => 'red', :b => 'blue' }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImageTooth

Returns a new instance of ImageTooth.



28
29
30
31
32
# File 'lib/image_tooth.rb', line 28

def initialize
  @root_path = './'
  @file_name_base = 'base_tooth.png'
  @extension = ".png"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/image_tooth.rb', line 169

def method_missing(method_name, *args)

  case method_name.to_s
  when /paint_img/
    case args[0]
    when /^ttttt/
      self.paint2extract
    when /^xxxxx/
      self.paint_extracted
    else
      self.paint(file_name2hash_faces(args[0]), args[0])
    end
  when /paint2extract/
    self.paint_extract 'blue', 'ttttt'
  when /paint_extracted/
    self.paint_extract 'red', 'xxxxx'
  else
    super.method_missing
  end

end

Instance Attribute Details

#root_pathObject

Returns the value of attribute root_path.



23
24
25
# File 'lib/image_tooth.rb', line 23

def root_path
  @root_path
end

Class Method Details

.chars_colorsObject



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

def ImageTooth.chars_colors
  @@colors.keys
end

.colors=(hash_colors) ⇒ Object



34
35
36
# File 'lib/image_tooth.rb', line 34

def ImageTooth.colors=(hash_colors)
  @@colors = hash_colors
end

Instance Method Details

#create_foldersObject

Create all folders ones for each color and 2 more for extractions (x/ and t/)



201
202
203
204
205
206
207
208
209
# File 'lib/image_tooth.rb', line 201

def create_folders
  #FIXME
  Dir.mkdir(@root_path) if not File.exist?(@root_path)
  ImageTooth.chars_colors.each { |f|
    Dir.mkdir("#{@root_path}/#{f}") if not File.exist?("#{@root_path}/#{f}")
  }
  Dir.mkdir("#{@root_path}/t")
  Dir.mkdir("#{@root_path}/x")
end

#file_name2hash_faces(file_name) ⇒ Object

Generates a hash based on tooth’s file name, where keys are the tooth’s faces and value is the colour to painted.



132
133
134
135
136
137
138
139
140
141
# File 'lib/image_tooth.rb', line 132

def file_name2hash_faces file_name
  desc = file_name.split('.').first
  hash = Hash.new
  hash[:top]      = @@colors[desc[0].chr.to_sym]
  hash[:right]    = @@colors[desc[1].chr.to_sym]
  hash[:bottom]   = @@colors[desc[2].chr.to_sym]
  hash[:left]     = @@colors[desc[3].chr.to_sym]
  hash[:center]   = @@colors[desc[4].chr.to_sym]
  hash
end

#gen_allObject

Generates all tooth’s images in a folder structure like this: b/ w/ n/ r/ g/

  • b/ - Blue (those wich it’s top face is blue)

  • w/ - White (those wich it’s top face is white)

  • n/ - Black (those wich it’s top face is Black)

  • g/ - Green (those wich it’s top face is Green)

  • t/ - To extrate (this will be extracted)

  • x/ - Extracted (this was extracted)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/image_tooth.rb', line 152

def gen_all
  root_path_aux = self.root_path
  self.permut.each{|i|
    self.root_path = "#{root_path_aux}/#{i[0].chr}"
    self.paint_img i
  }

  #Generate the 2 extractions marcs
  self.root_path = "#{root_path_aux}/t"
  self.paint2extract
  self.root_path = "#{root_path_aux}/x"

  self.paint_extracted

  self.root_path = root_path_aux
end

#graph_base(file = @file_name_base) ⇒ Object

Generate the base tooth image, with all faces in white color. This are store in root_path/base_path.png



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/image_tooth.rb', line 46

def graph_base file = @file_name_base
  background = Image.new(25, 35)
  gc = Draw.new
  gc.stroke('black')
  gc.fill('white')
  gc.rectangle(6, 6, 18, 28)
  gc.line(0, 0, 6, 6)
  gc.line(24, 0, 18, 6)
  gc.line(0, 35, 6, 28)
  gc.line(25, 35, 18, 28)
  gc.draw(background)
  background.border!(1, 1, 'black')
  background.write( "#{root_path}/#{file}" )
end

#paint(hash_faces, name_file) ⇒ Object

Paints the faces of the tooth image on hash colors based. The tooth’s filename represents the faces painted, The filename’s characters are:

  • w - White

  • g - Green

  • b - Blue

  • n - Black

  • r - Red

  • t - To extract

  • x - Extracted

and are ordered, begining on the upper face of the the tooth and moving clockwise ending on the center face.

Example: wrrgw.png


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
# File 'lib/image_tooth.rb', line 74

def paint hash_faces, name_file
  # FIXME
  self.graph_base

  tooth = Magick::Image.read("#{@root_path}/#{@file_name_base}").first
  gc = Draw.new

  gc.fill(hash_faces[:top])
  x = 1
  y = 1
  gc.polygon(x, y, x+7, y+5, x+18, y+5, x+23, y)

  gc.fill(hash_faces[:bottom])
  x = 3
  y = 35
  gc.polygon(x, y, x+5, y-5, x+16, y-5, x+21, y)

  gc.fill(hash_faces[:center])
  x = 8
  y = 8
  gc.rectangle(x, y, x+10, y+20)

  gc.fill(hash_faces[:right])
  x = 20
  y = 8
  gc.polygon(x, y, x+5, y-6, x+5, y+25, x, y+20)

  gc.fill(hash_faces[:left])
  x = 1
  y = 35
  gc.polygon(x, y, x+5, y-6, x+5, 7, 1, 2)

  gc.draw(tooth)
  tooth.write("#{self.root_path}/#{name_file}")
  File.delete("#{self.root_path}/#{@file_name_base}")
end

#paint_extract(color, filename) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/image_tooth.rb', line 111

def paint_extract color, filename
  self.graph_base

  tooth = Magick::Image.read("#{@root_path}/#{@file_name_base}").first
  gc = Draw.new

  x = 4
  y = 10
  gc.stroke(color)
  gc.stroke_width(3)
  gc.line(x, y, x+19, y+15)
  gc.line(x + 18, y, x, y+16)

  gc.draw(tooth)
  tooth.write("#{self.root_path}/#{filename}#{@extension}")
  File.delete("#{self.root_path}/#{@file_name_base}")
end

#permutObject

Generates all permutations tooth’s filenames.



192
193
194
# File 'lib/image_tooth.rb', line 192

def permut
  @@colors.keys.permut(5).collect{ |v| v.join('') + @extension}
end

#wrap_color(char_color) ⇒ Object



196
197
198
# File 'lib/image_tooth.rb', line 196

def wrap_color char_color
  @@colors[char_color]
end