<commit_before>package main

// #cgo CFLAGS: -I../cpp
// #cgo LDFLAGS: -L../cpp -lraytracer -lstdc++ -lm
// #include "session_interface.h"
import "C"

import "unsafe"

import (
  "image"
  "image/color"
  "os"
  "image/png"
  "math"
)

type CComputationManager struct {
  cm unsafe.Pointer
}

func CCreateComputator(height, width int, objects []float32, objectsNumber int) CComputationManager {
  cObjects := (*C.float)(unsafe.Pointer(&objects[0]))
  cm := C.createComputator(C.int(height), C.int(width), C.float(30), C.int(objectsNumber), cObjects)
  return CComputationManager{cm}
}

func CTraceRays(computator CComputationManager, indicesSize int, raysIndices []int) [][]float32 {
  indices := (*C.int)(unsafe.Pointer(&raysIndices[0]))

  results := make([][]float32, indicesSize)
  refResults := make([]*float32, indicesSize)
  
  for i := range(results) {
    data := make([]float32, 3)
    results[i] = data
    refResults[i] = (*float32)(unsafe.Pointer(&data[0]))
  }
  
  resultsPtr := (**C.float)(unsafe.Pointer((&refResults[0])))
  C.traceRays(computator.cm, C.int(indicesSize), indices, resultsPtr)

  return results
}

func main() {
  width, height := 640, 480
  objects, objectsNumber := createScene()

  computator := CCreateComputator(height, width, objects, objectsNumber)

  vectorImg := render(computator, height, width, objects)

  println("Render finished. Saving... ")
  
    // save results
  img := image.NewRGBA(image.Rect(0, 0, width, height))
  for i, p := range vectorImg {
    r, g, b := p[0], p[1], p[2]
    x, y := i % width, i / width
    img.SetRGBA(x, y, color.RGBA{
      uint8(math.Min(1.0, float64(r))*255.0),
      uint8(math.Min(1.0, float64(g))*255.0),
      uint8(math.Min(1.0, float64(b))*255.0),
      255})
  }

  toimg, _ := os.Create("scene.png")
  defer toimg.Close()

  png.Encode(toimg, img)

  print("Done")
}

func createScene() ([]float32, int) {
  objectsNumber := 6
  objects := make([]float32, objectsNumber * 12)

  i := 0

  // 0
  i = packToVector(objects, i, 0, -10004, -20) // center
  i = packToVector(objects, i, 10000) // radius
  i = packToVector(objects, i, 0.2, 0.2, 0.2) // surface color
  i = packToVector(objects, i, 0) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // 1
  i = packToVector(objects, i, 0, 0, -20) // center
  i = packToVector(objects, i, 4) // radius
  i = packToVector(objects, i, 1.0, 0.32, 0.36) // surface color
  i = packToVector(objects, i, 1) // reflection
  i = packToVector(objects, i, 0.5) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // 2
  i = packToVector(objects, i, 5, -1, -15) // center
  i = packToVector(objects, i, 2) // radius
  i = packToVector(objects, i, 0.9, 0.76, 0.46) // surface color
  i = packToVector(objects, i, 1) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // 3
  i = packToVector(objects, i, 5, 0, -25) // center
  i = packToVector(objects, i, 3) // radius
  i = packToVector(objects, i, 0.65, 0.77, 0.97) // surface color
  i = packToVector(objects, i, 1) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // 4
  i = packToVector(objects, i, -5.5, 0, -15) // center
  i = packToVector(objects, i, 3) // radius
  i = packToVector(objects, i, 0.9, 0.9, 0.9) // surface color
  i = packToVector(objects, i, 1) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // light
  // 5
  i = packToVector(objects, i, 0, 20, -30) // center
  i = packToVector(objects, i, 3) // radius
  i = packToVector(objects, i, 0, 0, 0) // surface color
  i = packToVector(objects, i, 0) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 3, 3, 3) // emission color

  return objects, objectsNumber
}

func packToVector(objects []float32, index int, items ...float32) int {
  for i, v := range items {
    objects[index + i] = v
  }

  return index + len(items)
}

func render(computator CComputationManager, height, width int, objects []float32) [][]float32 {
  index := 0

  pairs := make([]int, width*height*2)
    
  for y := 0; y < height; y++ {
    for x := 0; x < width; x++ {
      pairs[index] = x; pairs[index + 1] = y
      index += 2
    }
  }

  results := CTraceRays(computator, width*height, pairs)
  return results
}
<commit_msg>Added computator memory release<commit_after>package main

// #cgo CFLAGS: -I../cpp
// #cgo LDFLAGS: -L../cpp -lraytracer -lstdc++ -lm
// #include "session_interface.h"
import "C"

import "unsafe"

import (
  "image"
  "image/color"
  "os"
  "image/png"
  "math"
)

type CComputationManager struct {
  cm unsafe.Pointer
}

func CCreateComputator(height, width int, objects []float32, objectsNumber int) CComputationManager {
  cObjects := (*C.float)(unsafe.Pointer(&objects[0]))
  cm := C.createComputator(C.int(height), C.int(width), C.float(30), C.int(objectsNumber), cObjects)
  return CComputationManager{cm}
}

func CTraceRays(computator CComputationManager, indicesSize int, raysIndices []int) [][]float32 {
  indices := (*C.int)(unsafe.Pointer(&raysIndices[0]))

  results := make([][]float32, indicesSize)
  refResults := make([]*float32, indicesSize)
  
  for i := range(results) {
    data := make([]float32, 3)
    results[i] = data
    refResults[i] = (*float32)(unsafe.Pointer(&data[0]))
  }
  
  resultsPtr := (**C.float)(unsafe.Pointer((&refResults[0])))
  C.traceRays(computator.cm, C.int(indicesSize), indices, resultsPtr)

  return results
}

func CDeleteComputator(computator CComputationManager) {
  C.destroyComputator(computator.cm)
}

func main() {
  width, height := 640, 480
  objects, objectsNumber := createScene()

  computator := CCreateComputator(height, width, objects, objectsNumber)
  defer CDeleteComputator(computator)

  vectorImg := render(computator, height, width, objects)

  println("Render finished. Saving... ")
  
    // save results
  img := image.NewRGBA(image.Rect(0, 0, width, height))
  for i, p := range vectorImg {
    r, g, b := p[0], p[1], p[2]
    x, y := i % width, i / width
    img.SetRGBA(x, y, color.RGBA{
      uint8(math.Min(1.0, float64(r))*255.0),
      uint8(math.Min(1.0, float64(g))*255.0),
      uint8(math.Min(1.0, float64(b))*255.0),
      255})
  }

  toimg, _ := os.Create("scene.png")
  defer toimg.Close()

  png.Encode(toimg, img)

  print("Done")
}

func createScene() ([]float32, int) {
  objectsNumber := 6
  objects := make([]float32, objectsNumber * 12)

  i := 0

  // 0
  i = packToVector(objects, i, 0, -10004, -20) // center
  i = packToVector(objects, i, 10000) // radius
  i = packToVector(objects, i, 0.2, 0.2, 0.2) // surface color
  i = packToVector(objects, i, 0) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // 1
  i = packToVector(objects, i, 0, 0, -20) // center
  i = packToVector(objects, i, 4) // radius
  i = packToVector(objects, i, 1.0, 0.32, 0.36) // surface color
  i = packToVector(objects, i, 1) // reflection
  i = packToVector(objects, i, 0.5) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // 2
  i = packToVector(objects, i, 5, -1, -15) // center
  i = packToVector(objects, i, 2) // radius
  i = packToVector(objects, i, 0.9, 0.76, 0.46) // surface color
  i = packToVector(objects, i, 1) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // 3
  i = packToVector(objects, i, 5, 0, -25) // center
  i = packToVector(objects, i, 3) // radius
  i = packToVector(objects, i, 0.65, 0.77, 0.97) // surface color
  i = packToVector(objects, i, 1) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // 4
  i = packToVector(objects, i, -5.5, 0, -15) // center
  i = packToVector(objects, i, 3) // radius
  i = packToVector(objects, i, 0.9, 0.9, 0.9) // surface color
  i = packToVector(objects, i, 1) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 0, 0, 0) // emission color

  // light
  // 5
  i = packToVector(objects, i, 0, 20, -30) // center
  i = packToVector(objects, i, 3) // radius
  i = packToVector(objects, i, 0, 0, 0) // surface color
  i = packToVector(objects, i, 0) // reflection
  i = packToVector(objects, i, 0) // transparency
  i = packToVector(objects, i, 3, 3, 3) // emission color

  return objects, objectsNumber
}

func packToVector(objects []float32, index int, items ...float32) int {
  for i, v := range items {
    objects[index + i] = v
  }

  return index + len(items)
}

func render(computator CComputationManager, height, width int, objects []float32) [][]float32 {
  index := 0

  pairs := make([]int, width*height*2)
    
  for y := 0; y < height; y++ {
    for x := 0; x < width; x++ {
      pairs[index] = x; pairs[index + 1] = y
      index += 2
    }
  }

  results := CTraceRays(computator, width*height, pairs)
  return results
}

{'fraction_non_alphanumeric': 0.1671920860963148, 'fraction_numerical': 0.04326557234482009, 'mean_word_length': 3.273107292150488, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '570409', 'n_tokens_mistral': 3666, 'n_tokens_neox': 3334, 'n_words': 913}