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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
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
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New

Other popular topics Top

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
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1122 25120 742
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
New

Latest in PragProg

View all threads ❯