Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. require 'pp'
  2.  
  3. class Face < Array
  4. attr_accessor :width, :height
  5.  
  6. def initialize(file)
  7. header = file.read(8)
  8. self.width, self.height = header.unpack("LL")
  9. data = file.read(8 * length)
  10. data.unpack("D" * length).each do |pixel|
  11. self << pixel
  12. end
  13. end
  14.  
  15. def length
  16. width * height
  17. end
  18.  
  19. def to_h
  20. {
  21. #width: width,
  22. #height: height,
  23. data: self[0..1]
  24. }
  25. end
  26. end
  27.  
  28. class Faces < Array
  29. attr_accessor :count, :width, :height, :average
  30. def initialize(file)
  31. header = file.read(12)
  32. self.count, self.width, self.height = header.unpack('LLL')
  33. (1..count).each do |i|
  34. self << Face.new(file)
  35. end
  36. self.average = Face.new(file)
  37. end
  38.  
  39. def to_h
  40. {
  41. count: count,
  42. #width: width,
  43. #height: height,
  44. average: average.to_h,
  45. data: self.map(&:to_h)
  46. }
  47. end
  48. end
  49.  
  50. def error(faces1, faces2)
  51. faces1.each_with_index do |face, index|
  52. otherFace = faces2[index]
  53. scalar = otherFace[0] / face[0]
  54. error = 0.0
  55. face.each_with_index do |v, i|
  56. error += (v - otherFace[i] / scalar) ** 2
  57. end
  58. puts Math.sqrt(error)
  59. end
  60. end
  61.  
  62. face1 = nil
  63. face2 = nil
  64.  
  65. File.open(ARGV[0], 'rb') do |file|
  66. face1 = Faces.new(file)
  67. end
  68. File.open(ARGV[1], 'rb') do |file|
  69. face2 = Faces.new(file)
  70. end
  71.  
  72. puts ARGV[0]
  73. pp face1.to_h
  74. puts ARGV[1]
  75. pp face2.to_h
  76.  
  77. error(face1, face2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement