[Code] Raytest Util
-
<span class="syntaxdefault"><br />module Author</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end </span><span class="syntaxcomment"># <--<< Author's proprietary top-level namespace.<br /><br /># The ray utility adds more options to the +Sketchup.active_model.raytest+<br /># method.<br /></span><span class="syntaxdefault">module Author</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">RayUtil<br /><br /> module_function<br /><br /> </span><span class="syntaxcomment"># Retrieves an array of points intersecting the ray.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># _T1_ ; type 1 checks all model entities.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Point3d, Array] point Ray position.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Vector3d, Array] vector Ray direction.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Boolean] chg Whether to consider hidden geometry.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @return [Array<Geom;;Point3d>] An array of points intersecting the ray.<br /></span><span class="syntaxdefault"> def deepray_t1</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br /> pts </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[]<br /></span><span class="syntaxdefault"> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> while true<br /> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">raytest</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!</span><span class="syntaxdefault">chg</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> break unless hit<br /> x </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hit</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> pts</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push x<br /> point </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> x<br /> end<br /> pts<br /> end<br /><br /><br /> </span><span class="syntaxcomment"># Retrieves an array of points intersecting the ray.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># _T2_ ; type 2 checks all the given entities.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Array<entity>] ents An array of entities to include.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Point3d, Array] point Ray position.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Vector3d, Array] vector Ray direction.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Boolean] chg Whether to consider hidden geometry.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @return [Array<Geom;;Point3d>] An array of points intersecting the ray.<br /></span><span class="syntaxdefault"> def deepray_t2</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br /> unless ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(Array)<br /></span><span class="syntaxdefault"> ents </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">respond_to</span><span class="syntaxkeyword">?(;</span><span class="syntaxdefault">to_a</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">to_a </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> end<br /> 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 /></span><span class="syntaxdefault"> pts </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[]<br /></span><span class="syntaxdefault"> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> while true<br /> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">raytest</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!</span><span class="syntaxdefault">chg</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> break unless hit<br /> x </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hit</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> pts</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push x if entIDs</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</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> point </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> x<br /> end<br /> pts<br /> end<br /><br /><br /> </span><span class="syntaxcomment"># Retrieves an array of points intersecting the ray.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># _T3_ ; type 3 checks all, but the given entities.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Array<entity>] ents An array of entities to ignore.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Point3d, Array] point Ray position.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Vector3d, Array] vector Ray direction.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Boolean] chg Whether to consider hidden geometry.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @return [Array<Geom;;Point3d>] An array of points intersecting the ray.<br /></span><span class="syntaxdefault"> def deepray_t3</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br /> unless ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(Array)<br /></span><span class="syntaxdefault"> ents </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">respond_to</span><span class="syntaxkeyword">?(;</span><span class="syntaxdefault">to_a</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">to_a </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> end<br /> 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 /></span><span class="syntaxdefault"> pts </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[]<br /></span><span class="syntaxdefault"> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> while true<br /> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">raytest</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!</span><span class="syntaxdefault">chg</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> break unless hit<br /> x </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hit</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> pts</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">push x unless entIDs</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</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> point </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> x<br /> end<br /> pts<br /> end<br /><br /><br /> </span><span class="syntaxcomment"># Casts a ray through the model and return the first thing that the ray hits.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># _T1_ ; type 1 checks all given entities.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Array<entity>] ents An array of entities to include.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Point3d, Array] point Ray position.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Vector3d, Array] vector Ray direction.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Boolean] chg Whether to consider hidden geometry.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @return [Array, nil] A ray result;<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># http://www.sketchup.com/intl/en/developer/docs/ourdoc/model.php#raytest<br /></span><span class="syntaxdefault"> def raytest_t1</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br /> unless ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(Array)<br /></span><span class="syntaxdefault"> ents </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">respond_to</span><span class="syntaxkeyword">?(;</span><span class="syntaxdefault">to_a</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">to_a </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> end<br /> 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 /></span><span class="syntaxdefault"> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> while true<br /> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">raytest</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!</span><span class="syntaxdefault">chg</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> break unless hit<br /> return hit if entIDs</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</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> point </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hit</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> end<br /> nil<br /> end<br /><br /><br /> </span><span class="syntaxcomment"># Casts a ray through the model and return the first thing that the ray hits.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># _T2_ ; type 2 checks all, but the given entities.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Array<entity>] ents An array of entities to ignore.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Point3d, Array] point Ray position.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Vector3d, Array] vector Ray direction.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Boolean] chg Whether to consider hidden geometry.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @return [Array, nil] A ray result;<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># http://www.sketchup.com/intl/en/developer/docs/ourdoc/model.php#raytest<br /></span><span class="syntaxdefault"> def raytest_t2</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br /> unless ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(Array)<br /></span><span class="syntaxdefault"> ents </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">respond_to</span><span class="syntaxkeyword">?(;</span><span class="syntaxdefault">to_a</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">to_a </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> end<br /> 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 /></span><span class="syntaxdefault"> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> while true<br /> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">raytest</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!</span><span class="syntaxdefault">chg</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> break unless hit<br /> return hit unless entIDs</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</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> point </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hit</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> end<br /> nil<br /> end<br /><br /><br /> </span><span class="syntaxcomment"># Casts a ray through the model and return the first thing that the ray hits.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># _T3_ ; type 3 passes through transparent faces and stops until it hits a<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># solid face.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Point3d, Array] point Ray position.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Geom;;Vector3d, Array] vector Ray direction.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @param [Boolean] chg Whether to consider hidden geometry.<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># @return [Array, nil] A ray result;<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># http://www.sketchup.com/intl/en/developer/docs/ourdoc/model.php#raytest<br /></span><span class="syntaxdefault"> def raytest_t3</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> chg </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br /> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> cam </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">camera<br /> while true<br /> hit </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">raytest</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">point</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> vector</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!</span><span class="syntaxdefault">chg</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> break unless hit<br /> ent </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">last<br /> return hit unless ent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> angle </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normal</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">angle_between</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">cam</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">direction</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> mat </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> normal </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normal<br /> for i in 0</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">size</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> e </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">i</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> normal</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform</span><span class="syntaxkeyword">!(</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> mat </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material if e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> nil<br /> end<br /> fmat </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> angle </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> 90</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">degrees </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> ent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">back_material </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> ent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material<br /> mat </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> fmat if fmat </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> nil<br /> return hit if mat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> or mat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">alpha </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> 1.0<br /> point </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> hit</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> end<br /> nil<br /> end<br /><br />end </span><span class="syntaxcomment"># module Author;;RayUtil </span><span class="syntaxdefault"></span>
ray_util.rb
Edit: Changed arrays to hashes. According to Jimmy-Billy-Bob, hashes give you 20% gain over arrays. -
Thanks for posting. Might be handy someday.
/Joel -
Thanks, very useful !
-
Thanks for these well-designed and documented code examples!
The raytest is so much more useful when used in a loop (ie. for finding an entity in for example vertical direction). -
Thanks guys
I improved Ruby in some, changed documentation to Yard, and added raytest_t3 which passes through transparent faces and stops until it hits a solid face. See first post...
-
Does anyone know how raytest is implemented? Does SketchUp use a BSP or some other data structure to speed up geometry searches?
In other words, is raytest reasonably fast or is it done in a brute-force manner? -
I have no idea, but it doesn't seem fast to me...
-
It's fast enough for what it was designed for - which is casting some rays into your model to orient/locate GUI elements.
No, it is not suitable for raytracing etc where you'll be casting millions of rays.
Yes, it uses a spatial structure to accelerate queries.
-
Thanks!
@adamb said:
No, it is not suitable for raytracing etc where you'll be casting millions of rays.
That's too bad, since this could be useful.
-
Just did a very quick'n'dirty test between Ruby raytest and LightUp raytest and its not terrible under SU2014.
Raycasting a simple scene:
Ruby: ~250,000 rays/sec
LightUp: ~2,000,000 rays/sec (but in real usage its multi-threaded so more like 10,000,000 rays/sec) -
@oricatmos said:
That's too bad, since this could be useful.
If you are curious about raytracing, take a look at this script based on Adam's raytracer. The preview took about 3/4 hour.
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. -
@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.
-
@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.
Well, raytracing can be used to so many different things than rendering.
-
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>
-
@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. -
@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
-
@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. -
@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). -
Howbout Array.uniq! ?
-
@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).
Advertisement