Skip to content

Commit 14ee0ee

Browse files
Merge pull request #3 from lorycontixd/geometry
2 parents bb91314 + 2d563af commit 14ee0ee

6 files changed

Lines changed: 125 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ Please make sure to update tests as appropriate.
5151

5252
## License
5353
![GitHub License](https://img.shields.io/github/license/lorycontixd/RaytracingAlgorithm)
54+
5455
[GPL-3.0](https://choosealicense.com/licenses/gpl-3.0/)

RaytracingAlgorithm.nimble

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ srcDir = "src"
1111
requires "nim >= 1.6.4"
1212
requires "therapist"
1313
requires "SimplePNG"
14+
requires "neo"
1415

1516
task mytest, "Run the packages tests!":
1617
exec "nim cpp -r tests/test_color.nim"

examples/example_create_image.nim

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var k: int = 0
1010
for i in 0..hdrImageWrite.width-1:
1111
for j in 0..hdrImageWrite.height-1:
1212
hdrImageWrite.set_pixel(i,j, newColor(
13-
float( (i) / (hdrImageWrite.width + hdrImageWrite.height) ),
13+
float( (j) / (hdrImageWrite.width + hdrImageWrite.height) ),
1414
float( (i+j) / (hdrImageWrite.width + hdrImageWrite.height) ),
1515
float( (i) / (hdrImageWrite.width + hdrImageWrite.height) ),
1616
))
@@ -20,7 +20,4 @@ for i in 0..hdrImageWrite.width-1:
2020
hdrImageWrite.write_pfm(strmWrite)
2121
var strm = newFileStream("colored_image1.pfm", fmRead)
2222
var hdrRead = newHdrImage()
23-
hdrRead.read_pfm(strm)
24-
25-
26-
23+
hdrRead.read_pfm(strm)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import neo
2+
import std/[math, macros, typetraits, strformat]
3+
4+
type
5+
Vector* = object
6+
x*, y*, z*: float32
7+
8+
Point* = object
9+
x*, y*, z*: float32
10+
11+
Normal* = object
12+
x*, y*, z*: float32
13+
14+
Transformation* = object
15+
m*, inverse*: Matrix[float32]
16+
17+
## -------------------------------- CONSTRUCTORS ------------------------------------------
18+
19+
macro define_empty_constructors(type1: typedesc): typed =
20+
let source = fmt"""
21+
proc new{$type1}*(): {$type1} =
22+
result = {$type1}(x: 0.0, y: 0.0, z: 0.0)
23+
"""
24+
result = parseStmt(source)
25+
26+
macro define_constructors(type1: typedesc): typed =
27+
let source = fmt"""
28+
proc new{$type1}*(x,y,z: float32): {$type1} =
29+
result = {$type1}(x: x, y: y, z: z)
30+
"""
31+
result = parseStmt(source)
32+
33+
macro define_copy_constructors(type1: typedesc): typed =
34+
let source = fmt"""
35+
proc new{$type1}*(other: {$type1}): {$type1} =
36+
result = {$type1}(x: other.x, y: other.y, z: other.z)
37+
"""
38+
result = parseStmt(source)
39+
40+
define_empty_constructors(Point)
41+
define_empty_constructors(Vector)
42+
define_empty_constructors(Normal)
43+
define_constructors(Point)
44+
define_constructors(Vector)
45+
define_constructors(Normal)
46+
define_copy_constructors(Point)
47+
define_copy_constructors(Vector)
48+
define_copy_constructors(Normal)
49+
50+
51+
## -------------------------------- Sum + Subtraction ------------------------------------------
52+
53+
template define_operations(fname: untyped, type1: typedesc, type2: typedesc, rettype: typedesc) =
54+
proc fname*(a: type1, b: type2): rettype =
55+
result.x = fname(a.x, b.x)
56+
result.y = fname(a.y, b.y)
57+
result.z = fname(a.z, b.z)
58+
59+
define_operations(`+`, Vector, Vector, Vector)
60+
define_operations(`-`, Vector, Vector, Vector)
61+
define_operations(`+`, Vector, Point, Point)
62+
define_operations(`-`, Vector, Point, Point)
63+
define_operations(`+`, Point, Vector, Point)
64+
define_operations(`-`, Point, Vector, Point)
65+
define_operations(`+`, Normal, Normal, Normal)
66+
define_operations(`-`, Normal, Normal, Normal)
67+
68+
## --------------------------------------- Products ------------------------------------------
69+
70+
template define_product(type1: typedesc) =
71+
# Cross
72+
proc `*`*(a: type1, b: float32): type1 =
73+
result.x = a.x * b
74+
result.y = a.y * b
75+
result.z = a.z * b
76+
77+
define_product(Vector)
78+
define_product(Point)
79+
define_product(Normal)
80+
81+
proc dot*(this, other: Vector): float32 {.inline.} =
82+
result = this.x * other.x + this.y * other.y + this.z * other.z
83+
84+
proc `*`*(this, other: Vector): float32 {.inline.} =
85+
result = this.dot(other)
86+
87+
proc cross*(this, other: Vector): Vector {.inline.}=
88+
result.x = this.y * other.z - this.z * other.y
89+
result.y = this.z * other.x - this.x * other.z
90+
result.z = this.x * other.y - this.y * other.x
91+
92+
93+
## ---------------------------------------- Norm ----------------------------------------------
94+
95+
template define_norm(type1: typedesc)=
96+
proc norm*(a: type1): float32=
97+
result = sqrt(pow(a.x,2) + pow(a.y,2) + pow(a.z,2))
98+
99+
define_norm(Vector)
100+
define_norm(Point)
101+

src/RaytracingAlgorithm/hdrimage.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ proc parse_img_size*(self: HdrImage, line:string): (int,int) = # Remove public o
8484
except ValueError:
8585
raise newException(ValueError, "Invalid width/height") # Convert to custom exception
8686
return (width, height)
87-
87+
##new branch
8888

8989
proc valid_coordinates*(self: HdrImage, x,y:int): bool=
9090
##

tests/test_geometry.nim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import "../src/RaytracingAlgorithm/geometry.nim"
2+
3+
var
4+
a = newVector(3.0, 6.0, 2.0)
5+
b = newVector(-1.0, -10.0, 2.0)
6+
7+
assert a+b == newVector(2.0, -4.0, 4.0)
8+
assert a-b == newVector(4.0, 16.0, 0.0)
9+
assert b-a == newVector(-4.0, -16.0, 0.0)
10+
echo a*b
11+
assert a*b == -59.0
12+
assert b*a == a*b
13+
assert a.norm() == 7.0
14+
15+
var
16+
c = newPoint(1.0, 1.0, 2.0)
17+
18+
assert a+c == newPoint(4.0, 7.0, 4.0)
19+
assert c-a == newPoint(-2.0, -5.0, 0.0)

0 commit comments

Comments
 (0)