<commit_before>//
//  tracer.swift
//  swiftracer
//
//  Created by <NAME> on 6/10/2015.
//  Copyright © 2015 khrob. All rights reserved.
//

import Foundation
import AppKit
import CoreGraphics

/**
    Pixel buffer to CGImage routines derived from Joseph Lord's work at
    http://blog.human-friendly.com/drawing-images-from-pixel-data-in-swift
**/

struct Sample
{
    let r:Double
    let g:Double
    let b:Double
    let a:Double
}

struct Pixel
{
    var a:UInt8 = 255
    var r:UInt8 = 0
    var g:UInt8 = 0
    var b:UInt8 = 0
    
    static func buffer (size:Int) -> [Pixel]
    {
        return [Pixel](count: size, repeatedValue: Pixel())
    }
}

let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo:CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)

func imageFromARGB32Bitmap(pixels:[Pixel], width:Int, height:Int) -> NSImage?
{
    assert(pixels.count == Int(width * height))
    
    var data = pixels
    let providerRef = CGDataProviderCreateWithCFData(
        NSData(bytes: &data, length: data.count * sizeof(Pixel))
    )
    
    if let cgim = CGImageCreate(width, height, 8, 32, width * sizeof(Pixel), rgbColorSpace, bitmapInfo, providerRef, nil, true, .RenderingIntentDefault)
    {
        return NSImage(CGImage: cgim, size: CGSizeMake(CGFloat(width),CGFloat(height)))
    }
    return nil
}

func simpleGradient (width:Int, height:Int) -> [Pixel]
{
    var buffer = [Pixel](count: width*height, repeatedValue: Pixel(a: 255, r: 0, g: 0, b: 0))
    var index = 0
    
    repeat
    {
        let dw = UInt8 (255 * index % width / width)
        let dh = UInt8 (255 * index / width / height)
        let dd = UInt8 (255 - dh)
        
        buffer[index].r = dw
        buffer[index].g = dh
        buffer[index].b = dd
        index++
    }
        while index < width * height
    
    return buffer
}
<commit_msg>simple checkerboard ray/plane intersection<commit_after>//
//  tracer.swift
//  swiftracer
//
//  Created by <NAME> on 6/10/2015.
//  Copyright © 2015 khrob. All rights reserved.
//

import Foundation
import AppKit
import CoreGraphics

/**
    Pixel buffer to CGImage routines derived from Joseph Lord's work at
    http://blog.human-friendly.com/drawing-images-from-pixel-data-in-swift
**/

struct Sample
{
    let r:Double
    let g:Double
    let b:Double
    let a:Double
}

struct Pixel
{
    var a:UInt8 = 255
    var r:UInt8 = 0
    var g:UInt8 = 0
    var b:UInt8 = 0
    
    static func buffer (size:Int) -> [Pixel]
    {
        return [Pixel](count: size, repeatedValue: Pixel())
    }
}

let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo:CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)

func imageFromARGB32Bitmap(pixels:[Pixel], width:Int, height:Int) -> NSImage?
{
    assert(pixels.count == Int(width * height))
    
    var data = pixels
    let providerRef = CGDataProviderCreateWithCFData(
        NSData(bytes: &data, length: data.count * sizeof(Pixel))
    )
    
    if let cgim = CGImageCreate(width, height, 8, 32, width * sizeof(Pixel), rgbColorSpace, bitmapInfo, providerRef, nil, true, .RenderingIntentDefault)
    {
        return NSImage(CGImage: cgim, size: CGSizeMake(CGFloat(width),CGFloat(height)))
    }
    return nil
}

func simpleGradient (width:Int, height:Int) -> [Pixel]
{
    var buffer = [Pixel](count: width*height, repeatedValue: Pixel(a: 255, r: 0, g: 0, b: 0))
    var index = 0
    
    repeat
    {
        let dw = UInt8 (255 * index % width / width)
        let dh = UInt8 (255 * index / width / height)
        let dd = UInt8 (255 - dh)
        
        buffer[index].r = dw
        buffer[index].g = dh
        buffer[index].b = dd
        index++
    }
        while index < width * height
    
    return buffer
}
func simpleRaycast (rays:[Ray], width:Int, height:Int) -> [Pixel]
{
    var pixels = Pixel.buffer(rays.count)
    let plane = Plane(normal:Vector(x:0, y:1, z:0), point:Vector(x:0, y:-4, z:0))
    
    var index = 0
    var max_t = 0.0
    
    repeat
    {
        if let t = plane.intersection(rays[index])
        {
            var inter = t * 255
            if inter > 255 { inter = 255 }
            let gray:UInt8 =  UInt8(255 - inter)
            
            pixels[index].r = gray / 2
            pixels[index].g = gray / 2
            pixels[index].b = gray / 2
            
            let p = rays[index].atT(t)
            let x = Int(abs(p.x))
            let y = Int(abs(p.z))
            
            if x % 2 == y % 2
            {
                pixels[index].r = gray / 4
                pixels[index].g = gray / 4
                pixels[index].b = gray / 4
            }

            if t > max_t { max_t = t }
        }
        else
        {
            let dw = UInt8 (255 * index % width / width)
            let dh = UInt8 (255 * index / width / height)
            let dd = UInt8 (255 - dh)
            
            pixels[index].r = dw
            pixels[index].g = dh
            pixels[index].b = dd
        }
        index++
        
    } while index < rays.count
    
    print ("max_t: \(max_t)")
    return pixels
}

{'fraction_non_alphanumeric': 0.11782945736434108, 'fraction_numerical': 0.025775193798449614, 'mean_word_length': 2.0629080118694363, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, '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': '3858797', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1688, 'n_words': 454}