You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've used the opportunity to use one of Nim features that allows representing physics units in the type system and prevent compilation if there is an inconsistency.
For example I've represented Vec3 as a distinct type from Color and even UnitVector as a distinct type from a vector to ensure I don't pass a non-unit vector to the wrong functions.
Similarly I've created a distinct "Attenuation" type to ensure I don't multiply colors together.
In particular substracting 2 point3 give a vec3, adding a vec3and apoint3gives avec3but adding 2point3is a compile-time error as-is callinglengthon apoint3`.
func`-`*(a, b: Point3): Vec3 {.inline.}=## Substracting points from one point to the other## gives a vectorresult.x = a.x - b.x
result.y = a.y - b.y
result.z = a.z - b.z
template`+`*(p: Point3, v: Vec3): Point3=## Adding a vector to a point results in a pointPoint3(Vec3(p) + v)
template`-`*(p: Point3, v: Vec3): Point3=## Substracting a vector to a point results in a pointPoint3(Vec3(p) - v)
func`+`*(a, b: Point3): Point3 {.error: "Adding 2 Point3 doesn't make physical sense".}
And it seems like this caught an error in the book at chapter 13
In this expression (center - vec3(4, 0.2, 0)).length(), center is a point, substracting a vector gives a new point but points have no length (unles that was meant as distance from the origin).
So should vec3(4, 0.2, 0) be point3(4, 0.2, 0) instead?
Thank you very much for this excellent introduction to raytracing.
The text was updated successfully, but these errors were encountered:
Book 1, we were computing the length of a point - vector, but that
doesn't make sense; it should have been computing the length of the
point - point.
Resolves#609
Uh oh!
There was an error while loading. Please reload this page.
Hello team,
I just finished the first book and implemented it in the Nim programming language.
https://github.com/mratsim/trace-of-radiance
I've used the opportunity to use one of Nim features that allows representing physics units in the type system and prevent compilation if there is an inconsistency.
For example I've represented
Vec3
as a distinct type fromColor
and evenUnitVector
as a distinct type from a vector to ensure I don't pass a non-unit vector to the wrong functions.Similarly I've created a distinct "Attenuation" type to ensure I don't multiply colors together.
In particular substracting 2
point3
give a vec3, adding a
vec3and a
point3gives a
vec3but adding 2
point3is a compile-time error as-is calling
lengthon a
point3`.And it seems like this caught an error in the book at chapter 13
In this expression
(center - vec3(4, 0.2, 0)).length()
, center is a point, substracting a vector gives a new point but points have no length (unles that was meant as distance from the origin).So should
vec3(4, 0.2, 0)
bepoint3(4, 0.2, 0)
instead?Thank you very much for this excellent introduction to raytracing.
The text was updated successfully, but these errors were encountered: