|
| 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 | + |
0 commit comments