sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [Code] Raytest Util

    Scheduled Pinned Locked Moved Developers' Forum
    23 Posts 6 Posters 1.3k Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • jiminy-billy-bobJ Offline
      jiminy-billy-bob
      last edited by

      Coming back to the main topic...

      I've got a 20% gain in speed by using hashes lookups instead of arrays.

      <span class="syntaxdefault">ents </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> unless ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(Array)<br /></span><span class="syntaxdefault">entIDs </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Hash</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">map </span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entityID</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 1</span><span class="syntaxkeyword">]}]<br /><br /></span><span class="syntaxcomment">#...<br /><br /></span><span class="syntaxdefault">entID </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hit</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">][</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">entityID if hit</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">][</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">return result if entIDs</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">entID</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"></span>
      

      25% off Skatter for SketchUcation Premium Members

      1 Reply Last reply Reply Quote 0
      • A Offline
        Anton_S
        last edited by

        @jiminy-billy-bob said:

        Coming back to the main topic...
        I've got a 20% gain?

        👍 I never thought Hashes were faster than Arrays.
        I will edit the code above.

        1 Reply Last reply Reply Quote 0
        • AdamBA Offline
          AdamB
          last edited by

          @oricatmos said:

          @aerilius said:

          I really see no advantage of having an imperfect built-in (or Ruby plugin) raytracer, while there are already so many dedicated renderers with better illumination models, more speed and more realistic output.

          Yeah, I guess the raytest function would need to be able to take an array of rays and do multithreaded casting to be comparable to an external raytracer. By the way, we're doing room acoustics simulation, not visual rendering, with our plugin.

          The wavelength of light is small so mostly you can ignore diffraction effects and treat light as moving in straight lines. Audio wavelengths are much longer and have significant diffraction around corners (hence you can hear round corners!), so how does using (straight line) raytracing help here?

          Just interested..

          Adam

          Developer of LightUp Click for website

          1 Reply Last reply Reply Quote 0
          • jiminy-billy-bobJ Offline
            jiminy-billy-bob
            last edited by

            @anton_s said:

            👍 I never thought Hashes were faster than Arrays.
            I will edit the code above.

            This is because in this exemple, we look for keys in the hash, compared to looking for values in the array.
            I guess they're as fast as each other in both cases, but in hashes you can have custom keys.

            25% off Skatter for SketchUcation Premium Members

            1 Reply Last reply Reply Quote 0
            • A Offline
              Aerilius
              last edited by

              @anton_s said:

              👍 I never thought Hashes were faster than Arrays.
              Ruby saves developers from using primitive data types how data is stored in RAM, but it has more abstract, optimized data types.
              Arrays are likely implemented as a double linked list (correct my if I'm wrong) and hashes as some sort of trees. You can see in Wikipedia what each of them are good at (see indexing).

              1 Reply Last reply Reply Quote 0
              • jolranJ Offline
                jolran
                last edited by

                Howbout Array.uniq! ?

                1 Reply Last reply Reply Quote 0
                • AdamBA Offline
                  AdamB
                  last edited by

                  @aerilius said:

                  @anton_s said:

                  👍 I never thought Hashes were faster than Arrays.
                  Ruby saves developers from using primitive data types how data is stored in RAM, but it has more abstract, optimized data types.
                  Arrays are likely implemented as a double linked list (correct my if I'm wrong) and hashes as some sort of trees. You can see in Wikipedia what each of them are good at (see indexing).

                  Ruby Arrays are simply contiguous sequences of VALUES - no linked lists here.

                  Hashes are just Arrays - just not using an integer index to access but the digest of an arbitrary "Key".
                  So a bit like:

                  array["my key string".hash] = a_value array[123.hash] = a_value

                  Hash tables take care of the generated key being larger than the array (aka Table) and also collisions (2 keys generating the same hash) - but essentially its just an array.

                  Hash tables have no "ordering" - its a Set or more properly a Collection since Values are not unique.

                  Aerilius, you may be thinking of Ordered-collections which can be implemented using a tree (red-black trees being a common example).

                  Developer of LightUp Click for website

                  1 Reply Last reply Reply Quote 0
                  • AdamBA Offline
                    AdamB
                    last edited by

                    Another way of thinking about it is that Arrays are actually Hash tables using a really simple hashing algorithm that is:

                    hash(N) => N

                    Have a good weekend!

                    Developer of LightUp Click for website

                    1 Reply Last reply Reply Quote 0
                    • OricAtmosO Offline
                      OricAtmos
                      last edited by

                      @adamb said:

                      The wavelength of light is small so mostly you can ignore diffraction effects and treat light as moving in straight lines. Audio wavelengths are much longer and have significant diffraction around corners (hence you can hear round corners!), so how does using (straight line) raytracing help here?

                      Good observation! Raytracing is essentially just an approximation of how sound travels. Diffraction can't be modeled that way. As far as I know, simulating diffraction is still a hot topic in the field of acoustics and there's no catch-all automatic solution yet. (I'm not an expert in acoustics, I'm just a Computer Science student doing some coding at an acoustics chair/institute)

                      1 Reply Last reply Reply Quote 0
                      • jiminy-billy-bobJ Offline
                        jiminy-billy-bob
                        last edited by

                        I've been playing with raytest a bit. From my understanding, it works like this:

                        • It checks ALL entities in the model root context
                        • If something is hit and it's a face/edge/etc, the raytest returns the entity
                        • If the hit entity is a group or component, it again checks ALL entities in that group/comp
                        • And so forth until it hits a face/edge/etc

                        So if you have a lot of loose geometry in one of the hit contexts, it will be damn slow. But you can have a shitload of geometry in a group/comp not on the path of the ray, and you'll have decent performances.

                        To check if a group/comp is hit, the raytest doesn't look at the geometry inside it, but rather at its bounding box.
                        This is really important because you could have a lot of geometry away from the ray, but if the bounding box of the containing group is on the ray path, all of that geometry will be checked. Resulting in a slow raytest.

                        I hope it's clear?

                        25% off Skatter for SketchUcation Premium Members

                        1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 2 / 2
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement