[Talk] Qpik::SimpleRays Overhaul

[Talk] Qpik::SimpleRays Overhaul

Postby Dan Rathbun » Fri Jan 04, 2013 5:15 am

    Qpik::SimpleRays

    :arrow: Go to download page in Plugins forum

    Jakub Kupikowski (qpik) is not a programmer, so I overhauled
    his plugin so it can be removed from the Quarantine List.
    (See original posting in Plugins forum for info.)

    v 1.0.1 : 2013-01-05
      Corrected @@lang assignment in language file rescue block.
        simple_rays.rb: lines 109 and 111
        @@lang == 'en' # just use English
        should be making an assignment:
        @@lang = 'en' # just use English

    v 1.0.0 : 2013-01-04
      First release.



    Notes:
    • All code within the Qpik::SimpleRays namespace
    • No longer modifies Ruby nor API classes.
    • Now cleanups the temporary image files and dirs after itself.
    • Remembers inputbox entries during the session
    • commented out the purge all materials
    • multi-language

    What I did not do:
    • Re-write methods for optimization
        It is likely the code could benefit from some re-write, for instance I think the whole Bitmap#lbo_dword() method could be replaced with inline use of [n].pack("V") ?

    :idea:
    0
    Last edited by Dan Rathbun on Sun Apr 21, 2013 12:10 am, edited 12 times in total.
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Changes: Qpik::SimpleRays OVERHAUL v1.0.0

      Postby Dan Rathbun » Sat Jan 05, 2013 7:24 am

        Overhaul of qpik's SimpleRays plugin by Dan Rathbun, 2013-01-03

        MISC:

        a) Formalized versioning and added version entries to doc header.
          (The previous 7 versions are numnered 0.1.0 thru 0.7.0)

        b) This overhaul will be version 1.0.0



        GENERAL FIXES & CHANGES TO AVOID BAD CODING STYLES

        1) Use of Ruby keywords or common method identifiers:
          ~ Use of "method" as local var replaced with "meth"

        2) Use of local varname same as the method name it is within.
          (We try not to make the Ruby interpreter do more work by having to determine whether we are making a recursive call, or making an assignment to a local variable. Also bad style.)
          ~ local var names replaced with shorter nicknames

        3) Use of do keyword with for .. in statements.
          (Although most Ruby books say this is allowed, it is uneeded because the "for" keyword is the block opener. The Ruby interpreter often in older versions, raises SyntaxError when "do" is used this way. The interpreter expects an "end" to match the "for" and another "end" to match the "do".)
          ~ removed all unneeded "do" keywords from for..in statements.

        4) ALL code wrapped within Qpik::SimpleRays namespace !!!
          ~ defining global methods in the ObjectSpace is a NO-NO !
          (Everyone else's classes and modules inherit them!)

        5) Eliminated all use of $ global variables !
          ~ There is NEVER a good reason to clutter the global ObjectSpace with variables that a only specific plugin uses!
          (Instead, wrap code in a module namespace hierarchy and use class / module @@ vars, or constants within your namespaces.)

        6) Where possible, each() iterators replaced with faster for .. in.


        7) Indentation:
          ~ Weird odd indents (ie, 1 space, 3 space, etc.,) replaced with conventional Ruby 2 space indents.
          ~ Removed ALL outdenting. (Bad Style, makes blocks hard to read.)

        8) Plugin is within it's own sub-directory of "Qpik" directory.



        FIXED RUBY VERSION COMPATIBILITY ISSUES

        a) Parenthesis ()
          ~ Added parentheses around argument lists where appropriate, and
          definately when argument list contains more than one parameter.
          (Readability and future Ruby version Compatibility.)

        b) id() vs object_id()
          ~ id() is deprecated and outputs a warning to $stdout on each call. Replaced all uses with object_id()



        FIXED ILLEGAL ADDITION OF CUSTOM METHODS TO API & BASE CLASSES

        class Array methods:
          exposure()
          • moved into plugin module
          • xyz_array is now passed in as 1st argument
          frontside()
          • renamed to frontside_vector_array()
          • moved into plugin module
          • vector_array is passed in as 2nd argument
          get_bounds_2d()
          • moved into plugin module
          • array_of_xy_arrays is now passed in
          • internal min & max calculations have been replaced with min() & max() from Enumerable
          get_point2d()
          • moved into plugin module
          • point is passed a 1st argument
          get_point3d()
          • moved into plugin module
          • point is passed a 1st argument
          move()
          • removed (was not used, but if needed later can be replaced with API bulit-in Array.offset method)
          move!()
          • replaced with API bulit-in Array.offset! method
          round_to()
          • replaced with: Array.map {|n| round_f(n,x) }
          • it was only called twice from lightmap()
          • see Float#round_to() which was renamed plugin method round_f()
          scale()
          • replaced with an Array scaling transformation
          • used in only in Bitmap#blur()
          to_i()
          • renamed intized_array()
          • moved into Qpik::SimpleRays::Bitmap class
          • xyz_array is passed in as argument
          • called only by Bitmap#blur()

        class Bitmap
        • moved within the Qpik::SimpleRays namespace
        • (The global ObjectSpace is where Ruby defines it's classes, that EVERYONE will use. Custom classes should ALWAYS be defined within your namespace.)

          blur()
          • !! Bad style: making assignments within boolean expressions !!
              (These assignments have NOT been changed.)
          write()
          • wrapped file handling in begin .. rescue .. ensure block.
          to_byte()
          • renamed --> lbo_dword()
              (It does not output a byte.
              Outputs a 4-byte (32bit) DWORD String, in little-endian byte order.)
          • !!!! this method might be replaced with: [n].pack("V")
          from_byte()
          • NOT USED (commented out)

        class Float methods:
          round_to()
          • moved into plugin module
          • renamed round_f()
          • number n is passed in as 1st argument

        class Numeric methods:
          rgb()
          • moved into plugin module
          • number is passed in as 1st argument

        class Sketchup::Face methods:
          lightmap()
          • moved into plugin module
          • face is passed in as 1st argument

        class Sketchup::Model methods:
          raytest2() : removed (was not used)
          to_a() : removed (was empty, and not used.)


        FIXED PLUGIN METHODS:

        + Menu command is now a UI::Command object with a validation proc that grays the menu item if the selection is empty.

        ~ Creation of command and context menu handler wrapped within a unless file_loaded?() block.

        ~ These methods were global, now defined within plugin module:

          raytest()
            + short-circuit return nil if params from inputbox is false (ie, the user cancelled the inputbox.)
            ~ The user's previous working Dir is remembered in @@prevDir, and restored within an ensure clause.
            + Temporary image files and directories are cleaned up (This cleanup is disabled when debug mode is on.)
            ~ Untitled (unsaved) models now use "~/_temp_simple_rays" as temp image directory. (Previously they used a temporary dir beneath the application "plugins" directory.)
          raytest_dialog()
            + remembers the last used options in @@def_opts



        ADDITIONS:

        + MULTI-LANGUAGE SUPPORT

        + SketchupExtension loader file "load_qpik_simple_rays.rb"



        :ugeek: (The above list is supplied with the code as "v1.0.0_overhaul.txt")
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: [Plugin] Qpik::SimpleRays OVERHAUL v1.0.1

          Postby Anton_S » Sat Jan 05, 2013 9:29 am

          Nice Work Dan!!! :thumb:


          A suggestion:

          Qpic's round_f function isn't proper. He could avoid math operations just by using an existing function.
          An existing function Kernel.sprintf can round a number to any positive precision and amazing it's availiable in ruby 1.8.x.

          After a bit of experience, I written two round_to methods wrapping the Kernel.sprintf.

          Code: Select all
          module AMS; end # Top namespace

          module AMS::Numeric

            
          # Rounds num to a given precision in decimal digits (default 0 digits).
            # Retrieves value in numeric (int or float)
            def self.round_to(num, ndigits = 0)
              n = ndigits.to_i.abs
              x 
          = Kernel.format("%.#{n}f", num) # Kernel sprintf - format string
              x = ((ndigits == 0) ? x.to_i : x.to_f)
              x = x.abs if x.zero? # remove (-) sign if x is (-0.0 or -0)
              return x
            end 
          # def round_to

            # Retrieves value in string form
            def self.round_to2(num, ndigits = 0)
              n = ndigits.to_i.abs
              x 
          = Kernel.format("%.#{n}f", num) # Kernel sprintf - format string
              x = x.delete("-") if x.to_f.zero? # remove (-) sign if x is (-0.0 or -0)
              return x
            end 
          # def round_to2

          end # AMS::Numeric  
          0
          Last edited by Anton_S on Sun Jan 06, 2013 7:10 am, edited 1 time in total.

          Anton_S 
          PluginStore Author
          PluginStore Author
           

          Re: [Plugin] Qpik::SimpleRays OVERHAUL v1.0.1

          Postby Jim » Sat Jan 05, 2013 11:33 am

          Anton_S wrote:n existing function Kernel.sprintf can round a number


          Off-Topic:
          Formatting (sprintf) is not the same as rounding. sprintf is for formatting strings and is not a substitute for rounding.

          Also, you know by now not to add methods to the base classes!
          0
          Hi

          Jim 
          Global Moderator
           

          Re: [Plugin] Qpik::SimpleRays OVERHAUL v1.0.1

          Postby Aerilius » Sat Jan 05, 2013 11:50 am

          I think string manipulation would also probably be a lot slower. (otherwise very creative thought outside the box)
          0

          Aerilius 
          PluginStore Author
          PluginStore Author
           

          Re: [Plugin] Qpik::SimpleRays OVERHAUL v1.0.1

          Postby Dan Rathbun » Sat Jan 05, 2013 9:28 pm

          Anton_S wrote:qpik's round_f function isn't proper. He could avoid math operations just by ...

          (1) The basic mathematical operations are built-in to every CPU and programming language, and are FAST

          (2) String operations will always be MUCH slower than math operations (Agreeing with Aerilius.)

          (3) qpik got the statement (n * 10**x).round.to_f / 10**x from one of our (Jim, TIG or myself's) postings here on the subject of rounding Float objects.
          (4) Agreeing with Jim, why did you post code that modifys a Ruby base class (Numeric,)
            ... when the whole point of overhauling the SimpleRays code, was NOT to modify Ruby base classes and SketchUp API classes ??


          Incidentally, Ruby 1.9.x has the decplaces argument added to the Numeric#round() method. Hopefully the next SketchUp version will use one of the Ruby releases in the 1.9 trunk.

          In the meantime, I think it would be better to use the Backport gem, if you must have 1.9.x features NOW.

          :ugeek:
          0
          Last edited by Dan Rathbun on Sat Jan 05, 2013 9:52 pm, edited 1 time in total.
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: [Plugin] Qpik::SimpleRays OVERHAUL v1.0.1

            Postby Dan Rathbun » Sat Jan 05, 2013 9:36 pm

            Aerilius wrote:.... (otherwise very creative thought outside the box)

            Yes .. I do not wish to discourage you (Anton, or anyone,) from making suggestions!

            :P All optimization suggestions are welcome. :thumb:

            The code could REALLY benefit from more overhaul for speed.

            Maybe I should put this up on GitHub ... with Jakub's permission, of course. (He lst logged into SCF a week ago, and has not yet gotten my PM about this overhaul.)
            I am wanting him to weight in on this ...
            0
              I'm not here much anymore. But a PM will fire email notifications.
              User avatar
              Dan Rathbun 
              PluginStore Author
              PluginStore Author
               

              Re: [Plugin] Qpik::SimpleRays OVERHAUL v1.0.1

              Postby Dan Rathbun » Sat Jan 05, 2013 10:54 pm

              Jim wrote:
              Anton_S wrote:... function Kernel.sprintf can round a number

              Off-Topic:
              Formatting (sprintf) is not the same as rounding. sprintf is for formatting strings and is not a substitute for rounding.

              To be fair ... Anton is correct.

              Kernel.sprintf(), as well as format_string.%() both WILL INDEED round when a precision "dot" is used in the format string.
              0
                I'm not here much anymore. But a PM will fire email notifications.
                User avatar
                Dan Rathbun 
                PluginStore Author
                PluginStore Author
                 

                Re: [Plugin] Qpik::SimpleRays OVERHAUL v1.0.1

                Postby Anton_S » Sun Jan 06, 2013 7:43 am

                Jim wrote:Also, you know by now not to add methods to the base classes!

                :oops: Changed it!

                Off-Topic:
                Aerilius wrote:I think string manipulation would also probably be a lot slower

                I compared the timing using ruby console+, but got random and similiar results.

                In any way, how do you know that Kernel.format string is slower than few statements of math operations?
                Perhaps, format string is the ruby native function that was written in c++ and compiled?



                Dan, I'm sorry for flooding the topic with questions :oops:
                You can move the posts to new developers topic or remove them :roll:
                0

                Anton_S 
                PluginStore Author
                PluginStore Author
                 

                Re: [Plugin] Qpik::SimpleRays OVERHAUL v1.0.1

                Postby Dan Rathbun » Sun Jan 06, 2013 8:28 am

                Off-Topic:
                Anton_S wrote:In any way, how do you know that string functions is slower than few statements of math operations?

                Experience. 36 years engineering, 30 years using, programming, repairing and building computers.
                And every time we test Ruby string methods against other means (logical or math, etc.,) the string methods are slower. Maybe things will be faster in Ruby 1.9.x, as I believe the core team has done alot of optimization.

                Anton_S wrote:Perhaps, format string is the ruby native function that was written in c++ and compiled?

                Anton, ALL MRI Ruby functions are written in C (not C++,) and they are ALL compiled into the interpreter DLL.
                0
                  I'm not here much anymore. But a PM will fire email notifications.
                  User avatar
                  Dan Rathbun 
                  PluginStore Author
                  PluginStore Author
                   

                  Re: [Talk] Qpik::SimpleRays Overhaul

                  Postby Dan Rathbun » Sun Apr 21, 2013 12:13 am

                    v1.0.1 download Moved to Plugins forum.

                    Old v1.0.0 deleted.

                    THIS thread retitled as "[Talk] Qpik::SimpleRays Overhaul"

                    :idea:
                    0
                      I'm not here much anymore. But a PM will fire email notifications.
                      User avatar
                      Dan Rathbun 
                      PluginStore Author
                      PluginStore Author
                       

                      SketchUcation One-Liner Adverts

                      by Ad Machine » 5 minutes ago



                      Ad Machine 
                      Robot
                       



                       

                      Return to Developers' Forum

                      Who is online

                      Users browsing this forum: alx and 8 guests

                      Visit our sponsors: