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 Pragmatic Bookshelf topics Top

Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
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
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
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
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New