jiricodes

jiricodes

The Ray Tracer Challenge: Capped cone normal vector (page 190)

Hi @jamis,
I’m not sure if this has been discussed elsewhere, so apologies for a potential duplicate.

I refer to cone normal calculation at page 190, where author states "

Lastly, for the normal vector, compute the end cap normals just as you did for the cylinder, but …"

This unfortunately doesn’t work since the code for cylinder cap normal assumes constant diameter of 1.0. Instead one should calculate an actual distance from y axis (including sqrt) and check it against the currently considered y limit’s absolute value.

Pseudo codes and tests

Assumed original pseudo-code (combining cylinder and cone):

function local_normal_at(cone, point)
    #part from the cylinder caps check
    dist = point.x ^ 2 + point.z ^ 2
    # if squared distance is < 1.0 then distance is also < 1.0 therefore we don't need sqrt here
    if dist < 1 and point.y >= cone.maximum - EPSILON
         return vector(0, 1, 0)
    else if dist < 1 and point.y <= cone.minimum + EPSILON
       return vector(0, -1, 0)
    else
        y = sqrt(point.x ^ 2 + point.z ^ 2)
       if point.y > 0.0
           y = -y
       return (point.x, y, point.z)

Test that fails with above example:

Scenario Outline: Computing the normal vector on a capped cone
    Given shape = cone()
    And shape.minimum = -1
    And shape.maximum = 2
    And p  = point(1.5, 2, 0.0)
    When n = local_normal_at(shape, p)
    Then n = point(0, 1, 0)

This test assumes a point that should be located at the upper cone cap with distance from y axis larger than 1.0. In this case we expect the cap’s (plane) normal to be returned.\

With the above pseudocode, we get instead vector(1.5, -1.5, 0)

Pseudocode of a proposed fix:

function local_normal_at(cone, point)
    #calculate distance from y axis,
    dist = sqrt(point.x ^ 2 + point.z ^ 2)
    if dist < cone.maximum and point.y >= cone.maximum - EPSILON
         return vector(0, 1, 0)
    else if dist < cone.minimum and point.y <= cone.minimum + EPSILON
       return vector(0, -1, 0)
    else
        # note that y calculation is same as dist, we can potentially reuse the value here
        y = sqrt(point.x ^ 2 + point.z ^ 2)
       if point.y > 0.0
           y = -y
       return (point.x, y, point.z)

Popular Prag Prog topics Top

jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1017 16959 374
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New

Latest in PragProg

View all threads ❯