sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Fast Vector scale

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 2 Posters 982 Views 2 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.
    • AdamBA Offline
      AdamB
      last edited by

      Anyone got any good tips for a fast Geom::Vector3d scale? Along with a few others, its a function thats sorely missing in the API. Initially, I trivially did:

      def scale(s)
      	Geom::Vector3d.new(self.x * s,self.y * s, self.z * s)
      end
      

      However, this is waaay slower than the Vector3d#length function which (must) do slightly more work. I'm currently using (brace yourself for the horror):

      vec = (vtmp = aVector; vtmp.length = vtmp.length * aNewLength; vtmp)

      where aVector and aNewLength are effective parameters.

      Its actually faster..but just so ugly.

      Developer of LightUp Click for website

      1 Reply Last reply Reply Quote 0
      • tbdT Offline
        tbd
        last edited by

        why this testcase doesnt work ? I am missing something ?

        later edit: duh, vtmp = v.clone otherwise it will change the original vector
        from my tests scale2 is like 26-30% speed increase

        
        def scale(v,s)
          Geom;;Vector3d.new(v.x * s,v.y * s, v.z * s)
        end
        
        def scale2(v,s)  
          vtmp = v; 
          vtmp.length = vtmp.length * s
          vtmp
        end
        
        v1 = Geom;;Vector3d.new(5,1,5)
        n1 = 10
        
        t1 = Time.now
        begin
          50000.times do |x|
            scale2(v1,n1)   
          end
        rescue => err
         p err
        end
        p Time.now - t1
        
        

        SketchUp Ruby Consultant | Podium 1.x developer
        http://plugins.ro

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

          yes, that my point - that the internal function (length) that does something like *Math.sqrt(v.dot(v))*is faster than 3 scalar multiplies.. A bit sad.

          Adam

          Developer of LightUp Click for website

          1 Reply Last reply Reply Quote 0
          • tbdT Offline
            tbd
            last edited by

            because the internal function length is done in C world and those 3 scalar multiplies in Ruby world (which of course is slower)

            SketchUp Ruby Consultant | Podium 1.x developer
            http://plugins.ro

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

            Advertisement