Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ Please make sure to update tests as appropriate.

## License
![GitHub License](https://img.shields.io/github/license/lorycontixd/RaytracingAlgorithm)

[GPL-3.0](https://choosealicense.com/licenses/gpl-3.0/)
1 change: 1 addition & 0 deletions RaytracingAlgorithm.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ srcDir = "src"
requires "nim >= 1.6.4"
requires "therapist"
requires "SimplePNG"
requires "neo"

task mytest, "Run the packages tests!":
exec "nim cpp -r tests/test_color.nim"
Expand Down
7 changes: 2 additions & 5 deletions examples/example_create_image.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var k: int = 0
for i in 0..hdrImageWrite.width-1:
for j in 0..hdrImageWrite.height-1:
hdrImageWrite.set_pixel(i,j, newColor(
float( (i) / (hdrImageWrite.width + hdrImageWrite.height) ),
float( (j) / (hdrImageWrite.width + hdrImageWrite.height) ),
float( (i+j) / (hdrImageWrite.width + hdrImageWrite.height) ),
float( (i) / (hdrImageWrite.width + hdrImageWrite.height) ),
))
Expand All @@ -20,7 +20,4 @@ for i in 0..hdrImageWrite.width-1:
hdrImageWrite.write_pfm(strmWrite)
var strm = newFileStream("colored_image1.pfm", fmRead)
var hdrRead = newHdrImage()
hdrRead.read_pfm(strm)



hdrRead.read_pfm(strm)
101 changes: 101 additions & 0 deletions src/RaytracingAlgorithm/geometry.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import neo
import std/[math, macros, typetraits, strformat]

type
Vector* = object
x*, y*, z*: float32

Point* = object
x*, y*, z*: float32

Normal* = object
x*, y*, z*: float32

Transformation* = object
m*, inverse*: Matrix[float32]

## -------------------------------- CONSTRUCTORS ------------------------------------------

macro define_empty_constructors(type1: typedesc): typed =
let source = fmt"""
proc new{$type1}*(): {$type1} =
result = {$type1}(x: 0.0, y: 0.0, z: 0.0)
"""
result = parseStmt(source)

macro define_constructors(type1: typedesc): typed =
let source = fmt"""
proc new{$type1}*(x,y,z: float32): {$type1} =
result = {$type1}(x: x, y: y, z: z)
"""
result = parseStmt(source)

macro define_copy_constructors(type1: typedesc): typed =
let source = fmt"""
proc new{$type1}*(other: {$type1}): {$type1} =
result = {$type1}(x: other.x, y: other.y, z: other.z)
"""
result = parseStmt(source)

define_empty_constructors(Point)
define_empty_constructors(Vector)
define_empty_constructors(Normal)
define_constructors(Point)
define_constructors(Vector)
define_constructors(Normal)
define_copy_constructors(Point)
define_copy_constructors(Vector)
define_copy_constructors(Normal)


## -------------------------------- Sum + Subtraction ------------------------------------------

template define_operations(fname: untyped, type1: typedesc, type2: typedesc, rettype: typedesc) =
proc fname*(a: type1, b: type2): rettype =
result.x = fname(a.x, b.x)
result.y = fname(a.y, b.y)
result.z = fname(a.z, b.z)

define_operations(`+`, Vector, Vector, Vector)
define_operations(`-`, Vector, Vector, Vector)
define_operations(`+`, Vector, Point, Point)
define_operations(`-`, Vector, Point, Point)
define_operations(`+`, Point, Vector, Point)
define_operations(`-`, Point, Vector, Point)
define_operations(`+`, Normal, Normal, Normal)
define_operations(`-`, Normal, Normal, Normal)

## --------------------------------------- Products ------------------------------------------

template define_product(type1: typedesc) =
# Cross
proc `*`*(a: type1, b: float32): type1 =
result.x = a.x * b
result.y = a.y * b
result.z = a.z * b

define_product(Vector)
define_product(Point)
define_product(Normal)

proc dot*(this, other: Vector): float32 {.inline.} =
result = this.x * other.x + this.y * other.y + this.z * other.z

proc `*`*(this, other: Vector): float32 {.inline.} =
result = this.dot(other)

proc cross*(this, other: Vector): Vector {.inline.}=
result.x = this.y * other.z - this.z * other.y
result.y = this.z * other.x - this.x * other.z
result.z = this.x * other.y - this.y * other.x


## ---------------------------------------- Norm ----------------------------------------------

template define_norm(type1: typedesc)=
proc norm*(a: type1): float32=
result = sqrt(pow(a.x,2) + pow(a.y,2) + pow(a.z,2))

define_norm(Vector)
define_norm(Point)

2 changes: 1 addition & 1 deletion src/RaytracingAlgorithm/hdrimage.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ proc parse_img_size*(self: HdrImage, line:string): (int,int) = # Remove public o
except ValueError:
raise newException(ValueError, "Invalid width/height") # Convert to custom exception
return (width, height)

##new branch

proc valid_coordinates*(self: HdrImage, x,y:int): bool=
##
Expand Down
19 changes: 19 additions & 0 deletions tests/test_geometry.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "../src/RaytracingAlgorithm/geometry.nim"

var
a = newVector(3.0, 6.0, 2.0)
b = newVector(-1.0, -10.0, 2.0)

assert a+b == newVector(2.0, -4.0, 4.0)
assert a-b == newVector(4.0, 16.0, 0.0)
assert b-a == newVector(-4.0, -16.0, 0.0)
echo a*b
assert a*b == -59.0
assert b*a == a*b
assert a.norm() == 7.0

var
c = newPoint(1.0, 1.0, 2.0)

assert a+c == newPoint(4.0, 7.0, 4.0)
assert c-a == newPoint(-2.0, -5.0, 0.0)