<commit_before>require 'opengl'
require 'glu'
require 'glut'
include Gl,Glu,Glut

# extend Array class with new function
class Array
  def rotate!
    self << self.shift
  end
end

class App
  @@filters = [[GL_NEAREST,"None"],[GL_LINEAR_MIPMAP_NEAREST,"Bilinear"],[GL_LINEAR_MIPMAP_LINEAR,"Trilinear"]]
  @@anisotropy = [1,2,4,8,16,32]
  @@color_tint = ["Off","On"]

  def checker_texture(size,divide,color_a,color_b)
    strip_a = color_a * (size/divide)
    strip_b = color_b * (size/divide)
    line_strip_a = (strip_a + strip_b) * (size/2)
    line_strip_b = (strip_b + strip_a) * (size/2)
    (line_strip_a + line_strip_b) * (divide/2)
  end

  def printGlutBitmapFont(string, font, x,y, r,g,b)
    glDisable(GL_TEXTURE_2D)
    glColor3f(r, g, b)
    glRasterPos2i(x, y)
    string.each_byte do |x|
      glutBitmapCharacter(font, x)
    end
  end

  def ortho(w,h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0,w,0,h)
    glScalef(1,-1,1)
    glTranslatef(0,-h,0)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
  end

  def persp(w,h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity
    gluPerspective(90,w.to_f/h.to_f,1,100)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity
  end

  def reshape(w,h)
    @@w,@@h = w,h
    glViewport(0, 0, w, h)
    persp(w,h)
  end

  def initialize(size, threshold)
    if (not Gl.is_available?("GL_EXT_texture_filter_anisotropic"))
      puts "This program needs GL_EXT_texture_filter_anisotropic extension"
      exit
    end
    @@w,@@h = glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT)

    @t = glGenTextures(2)

    @@automata = Avalanche.new(size, threshold)

    # default checkerboard texture
    glBindTexture(GL_TEXTURE_2D,@t[0])
    data = checker_texture(64,4,[1,1,1],[0,0,0])
    gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,64,64,GL_RGB,GL_FLOAT,data.pack("f*"))

    # second texture with color tinted mipmaps
    glBindTexture(GL_TEXTURE_2D,@t[1])
    data = checker_texture(64,4,[1,1,1],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,64,64,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(32,4,[1,0,0],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,1,GL_RGBA,32,32,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(16,4,[0,1,0],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,2,GL_RGBA,16,16,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(8,4,[0,0,1],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,3,GL_RGBA,8,8,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(4,4,[1,1,0],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,4,GL_RGBA,4,4,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(2,2,[1,0,1],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,5,GL_RGBA,2,2,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = [0.5,0.5,0.5] # single pixel texture, just average it
    glTexImage2D(GL_TEXTURE_2D,6,GL_RGBA,1,1,0,GL_RGB,GL_FLOAT,data.pack("f*"))
  end

  def display_text
    ortho(@@w,@@h)
    printGlutBitmapFont("Texture Filtering ('f'): #{@@filters[0][1]}", GLUT_BITMAP_9_BY_15, 20, 20, 1.0, 1.0, 1.0)
    printGlutBitmapFont("Anisotropy factor ('a'): #{@@anisotropy[0]}x", GLUT_BITMAP_9_BY_15, 20, 40, 1.0, 1.0, 1.0)
    printGlutBitmapFont("Colored Mipmaps ('c'): #{@@color_tint[0]}", GLUT_BITMAP_9_BY_15, 20, 60, 1.0, 1.0, 1.0)
    printGlutBitmapFont("Snowing ('s')", GLUT_BITMAP_9_BY_15, 20, 80, 1.0, 1.0, 1.0)
    persp(@@w,@@h)
  end

  def display_plane()
    glEnable(GL_TEXTURE_2D)
    t_repeat = 16
    # x,y,z,u,v
    quad = [[-4,-1,1,  0,t_repeat],[4,-1,1,  t_repeat,t_repeat],[4,1,-8,  t_repeat,0],[-4,1,-8,  0,0]]
    glBegin(GL_QUADS)
    quad.each do |v|
      glTexCoord2f(v[3],v[4])
      glVertex3f(v[0],v[1],v[2])
    end
    glEnd()
    glDisable(GL_TEXTURE_2D)
  end

  def display
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    persp(@@w,@@h)

    # move back from scene
    glTranslatef(0,0,-2)

    # set anisotropy
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,@@anisotropy[0])

    # set color tint
    if (@@color_tint[0] == "On")
      glBindTexture(GL_TEXTURE_2D,@t[1])
    else
      glBindTexture(GL_TEXTURE_2D,@t[0])
    end

    # set filters
    f = @@filters[0][0]
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,f)

    # draw scene
    display_plane()
    display_text()

    sleep(0.001) # microsleep to avoid consuming all CPU time
    glutSwapBuffers()
  end

  def idle
    glutPostRedisplay()
  end

  def keyboard(key,x,y)
    case (key)
    when ?f
      @@filters.rotate!
    when ?a
      max_anisotropy = glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)
      begin  @@anisotropy.rotate! end until @@anisotropy[0]<=max_anisotropy
    when ?c
      @@color_tint.rotate!
    when ?\e # ESCAPE
      exit(0)
    when ?s # SNOWING
      @@automata.snowing!
    end
    glutPostRedisplay()
  end
end

class Avalanche
  
  def initialize (size, threshold, conservative = true)
    @@threshold = threshold
    @@size = size
    @@conservative = conservative
    @height = Array.new @@size
    @height.each do |ar| 
      ar = Array.new @@size
      ar.each {|cel| cel = 0}
    end

  end

  def snowing!
   # @point = 2.times.map{ rand(@@size) }
    @@point = [3,2]
    if @@conservative then
      @height[@@point[0]][@@point[1]] += 1
    else
      (0...@point[0]).each do |t|
        if rand(2) then
          @height[t][@point[1]] += 1
        else
          @height[@point[1]][t] += 1
        end
      end
    end
  end



end


# main<commit_msg>Update Automata.rb, compute_slope method added.<commit_after>require 'opengl'
require 'glu'
require 'glut'
include Gl,Glu,Glut

# extend Array class with new function
class Array
  def rotate!
    self << self.shift
  end
end

class App
  @@filters = [[GL_NEAREST,"None"],[GL_LINEAR_MIPMAP_NEAREST,"Bilinear"],[GL_LINEAR_MIPMAP_LINEAR,"Trilinear"]]
  @@anisotropy = [1,2,4,8,16,32]
  @@color_tint = ["Off","On"]

  def checker_texture(size,divide,color_a,color_b)
    strip_a = color_a * (size/divide)
    strip_b = color_b * (size/divide)
    line_strip_a = (strip_a + strip_b) * (size/2)
    line_strip_b = (strip_b + strip_a) * (size/2)
    (line_strip_a + line_strip_b) * (divide/2)
  end

  def printGlutBitmapFont(string, font, x,y, r,g,b)
    glDisable(GL_TEXTURE_2D)
    glColor3f(r, g, b)
    glRasterPos2i(x, y)
    string.each_byte do |x|
      glutBitmapCharacter(font, x)
    end
  end

  def ortho(w,h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0,w,0,h)
    glScalef(1,-1,1)
    glTranslatef(0,-h,0)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
  end

  def persp(w,h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity
    gluPerspective(90,w.to_f/h.to_f,1,100)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity
  end

  def reshape(w,h)
    @@w,@@h = w,h
    glViewport(0, 0, w, h)
    persp(w,h)
  end

  def initialize(size, threshold)
    if (not Gl.is_available?("GL_EXT_texture_filter_anisotropic"))
      puts "This program needs GL_EXT_texture_filter_anisotropic extension"
      exit
    end
    @@w,@@h = glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT)

    @t = glGenTextures(2)

    @automata = Avalanche.new(size, threshold)

    # default checkerboard texture
    glBindTexture(GL_TEXTURE_2D,@t[0])
    data = checker_texture(64,4,[1,1,1],[0,0,0])
    gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,64,64,GL_RGB,GL_FLOAT,data.pack("f*"))

    # second texture with color tinted mipmaps
    glBindTexture(GL_TEXTURE_2D,@t[1])
    data = checker_texture(64,4,[1,1,1],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,64,64,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(32,4,[1,0,0],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,1,GL_RGBA,32,32,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(16,4,[0,1,0],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,2,GL_RGBA,16,16,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(8,4,[0,0,1],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,3,GL_RGBA,8,8,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(4,4,[1,1,0],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,4,GL_RGBA,4,4,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = checker_texture(2,2,[1,0,1],[0,0,0])
    glTexImage2D(GL_TEXTURE_2D,5,GL_RGBA,2,2,0,GL_RGB,GL_FLOAT,data.pack("f*"))
    data = [0.5,0.5,0.5] # single pixel texture, just average it
    glTexImage2D(GL_TEXTURE_2D,6,GL_RGBA,1,1,0,GL_RGB,GL_FLOAT,data.pack("f*"))
  end

  def display_text
    ortho(@@w,@@h)
    printGlutBitmapFont("Texture Filtering ('f'): #{@@filters[0][1]}", GLUT_BITMAP_9_BY_15, 20, 20, 1.0, 1.0, 1.0)
    printGlutBitmapFont("Anisotropy factor ('a'): #{@@anisotropy[0]}x", GLUT_BITMAP_9_BY_15, 20, 40, 1.0, 1.0, 1.0)
    printGlutBitmapFont("Colored Mipmaps ('c'): #{@@color_tint[0]}", GLUT_BITMAP_9_BY_15, 20, 60, 1.0, 1.0, 1.0)
    printGlutBitmapFont("Snowing ('s')", GLUT_BITMAP_9_BY_15, 20, 80, 1.0, 1.0, 1.0)
    persp(@@w,@@h)
  end

  def display_plane()
    glEnable(GL_TEXTURE_2D)
    t_repeat = 16
    # x,y,z,u,v
    quad = [[-4,-1,1,  0,t_repeat],[4,-1,1,  t_repeat,t_repeat],[4,1,-8,  t_repeat,0],[-4,1,-8,  0,0]]
    glBegin(GL_QUADS)
    quad.each do |v|
      glTexCoord2f(v[3],v[4])
      glVertex3f(v[0],v[1],v[2])
    end
    glEnd()
    glDisable(GL_TEXTURE_2D)
  end

  def display
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    persp(@@w,@@h)

    # move back from scene
    glTranslatef(0,0,-2)

    # set anisotropy
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,@@anisotropy[0])

    # set color tint
    if (@@color_tint[0] == "On")
      glBindTexture(GL_TEXTURE_2D,@t[1])
    else
      glBindTexture(GL_TEXTURE_2D,@t[0])
    end

    # set filters
    f = @@filters[0][0]
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,f)

    # draw scene
    display_plane()
    display_text()

    sleep(0.001) # microsleep to avoid consuming all CPU time
    glutSwapBuffers()
  end

  def idle
    glutPostRedisplay()
  end

  def keyboard(key,x,y)
    case (key)
    when ?f
      @@filters.rotate!
    when ?a
      max_anisotropy = glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)
      begin  @@anisotropy.rotate! end until @@anisotropy[0]<=max_anisotropy
    when ?c
      @@color_tint.rotate!
    when ?\e # ESCAPE
      exit(0)
    when ?s # SNOWING
      @automata.snowing!
    end
    glutPostRedisplay()
  end
end

class Avalanche
  
  def initialize (size, threshold, conservative = true)
    @threshold = threshold
    @size = size
    @conservative = conservative
    @height = Array.new @size
    @height.map! do |ar| 
      ar = Array.new @size
      ar.map! {0}
    end

  end

  def snowing!
    @point = 2.times.map{ rand(@size) }
    if @conservative then
      @height[@point[0]][@point[1]] += 1
    else
      (0...@point[0]).each do |t|
        if rand(2) then
          @height[t][@point[1]] += 1
        else
          @height[@point[1]][t] += 1
        end
      end
    end
  end

  def compute_slope
    @slope = Array.new(@size-1)
    @slope.map! {Array.new(@size-1)}

    (@size-1).times do |i|
      (@size-1).times do |j|
        @slope[i][j] = @height[i][j] + @height[i-1][j+1] - @height[i][j+1] - @height[i][j+2] if i > 0 
        @slope[i][j] = 2*@height[i][j] - @height[i][j+1] - @height[i][j+2] if i == 0

      end
    end
  end


end


# main
{'fraction_non_alphanumeric': 0.15934309809143365, 'fraction_numerical': 0.04909010208610741, 'mean_word_length': 3.6746887966804977, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '443658', 'n_tokens_mistral': 5197, 'n_tokens_neox': 4835, 'n_words': 749}