[Plugin] Voxelize - Updated 2011-04-04

[Plugin] Voxelize - Updated 2011-04-04

Postby TheDro » Tue Mar 22, 2011 4:49 pm

Hey everyone! I've been working on a plugin for the past couple weeks and I think it's about time I share it with the community. It's still very much in its infancy (beta version) and still runs very slowly for moderately complicated solids but you guys can still try it out and let me know what you think. The script is called voxelize. Basically what it does is transforms a solid (within a group) into a bunch of little cubes. So in the same way as you can for example "pixelize" a circle, that is, transform a smooth drawing into a bunch of pixels, my plugin allows you to voxelize a solid, a voxel being a 3-dimensional pixel (cube).

When the plugin is called on a group, a pop-up window will ask the user the size of each cube and whether a full solid or a shell should be made.(The three other options aren't quite implemented yet so don't worry about them.) The shell option usually runs a lot faster since it will produce a lot less cubes. As I mentioned, the plugin can take quite a while to compute the position of the cubes and so on. This still causes Sketchup to whiteout and "not respond" for a while, even though it's running fine in the background. There is a way to fix this problem as mentioned in this thread:http://forums.sketchucation.com/viewtopic.php?f=180&t=34205&hilit=not+responding, but I haven't gotten to that yet. One thing to keep in mind, is that the amount of time it takes to run the script is "proportional" to the number of faces in the solid. As a reference though, it takes about 30 seconds to render a full sphere with a radius of 20 cubes and about 4-5 second to render a shell of the same sphere with my 2.4 GHz processor. This rendering time will shorten as I improve the algorithm and just this morning I've thought of two new algorithms which may or may not speed up the process.

Anyways, enjoy!

P.S. Almost forgot to mention, the cubes that are produces are all instances of a component which means that by modifying one cube, you can change all of them.


EDIT: Beta 02

Alright! The second version is out and it's WAY faster! Here's a list of the major changes:

- The new algorithm for finding where cubes should be places is much faster now that it doesn't check the whole zy grid for intersections with EVERY face. Each face is probed by a sub grid. If you don't understand what this means, it doesn't even matter :P. For a sphere with a radius of 20 cubes, it used to take about 4 seconds to compute the voxelized grid and now it takes about 0.3 seconds.

- The x-y-z offset options have been removed since they weren't all that useful in the first place.

- Shell mode has now been made the default since it is the faster mode.

- Fixed a small bug pointed out by Solo where one face of the cubes was upside down.

EDIT: Beta 03

Dan Rathbun helped me out with proper module wrapping, method naming and error checking/catching. This version wasn't released but definitely brought many changes which makes my code more stable. Thanks again, Dan.

Beta 04

Another release! Most of the changes won't be noticeable to the common user but there is still much improvement that was made.

- The new "spiral" algorithm is used to properly address hitting edges and vertices when checking where to place the cubes.

- It is possible to call voxelize(false) in order to suppress the method from drawing cubes in case the method is just used by another program/plugin.

- The method now returns the binCubes and cubes arrays. The former can be used to export the binary 3D array. See my new upcoming plugin, MCExport, which exports the array to a .schematic file which can be imported into MineCraft. (There you go, Khai ;) )


note: No improvements have been made to the algorithm that places the cubes which means that this process is still slow for large models.
1
Last edited by TheDro on Tue Apr 05, 2011 2:15 am, edited 6 times in total.

TheDro 
 

Re: Voxelize

Postby solo » Tue Mar 22, 2011 5:06 pm

Cool, fun plugin.

One problem it reverses faces on blue axis.
0
www.solos-art.com

I'm a social vegan, I avoid meet.
User avatar
solo 
Global Moderator
 

Re: Voxelize

Postby TheDro » Tue Mar 22, 2011 5:22 pm

What do you mean by reverse faces? The cube faces? If you're talking about the image, it's not just a sphere, it's a sphere with a cylinder pushed into the top of it.
0

TheDro 
 

Re: Voxelize

Postby solo » Tue Mar 22, 2011 5:33 pm

No, see image, up faces are revered after operation.
0
www.solos-art.com

I'm a social vegan, I avoid meet.
User avatar
solo 
Global Moderator
 

Re: Voxelize

Postby kdasilva » Tue Mar 22, 2011 5:37 pm

very cool!, you said each cube is an instance, could you swap out the definition to make an object composed to a bunch of tiny anythings? could auto scale the component to fit in your cube, then make all sorts of crazy things.

also, not that it matters, but it seems like the red face matches the red axis, but the blue face follows the green axis and the green face follows the blue axis. Intentional?
0

kdasilva 
 

Re: Voxelize

Postby TheDro » Tue Mar 22, 2011 6:03 pm

@kdasilva: I added the colors manually and arbitrarily just to make it easier to see which faces were in which direction. I thought of an autoscale feature but then thought it was better the way it is now although I could add that relatively easily, I think. It also wouldn't be difficult for the user to do the calculation on his own by setting the cube length to the length of the group divided by the number of cubes he wants. Could you suggest what options I should make available on the message box for such a features?

@solo: I was actually unaware that faces had a direction but I'll look into flipping them in the definition of the cubes.
Edit: This has been fixed and will be in the next release.

Thanks for the input, guys!
0
Last edited by TheDro on Tue Mar 22, 2011 6:27 pm, edited 1 time in total.

TheDro 
 

Re: Voxelize

Postby solo » Tue Mar 22, 2011 6:24 pm

Real fun to play with.

Voxel turtle.
1
www.solos-art.com

I'm a social vegan, I avoid meet.
User avatar
solo 
Global Moderator
 

Re: Voxelize

Postby Dan Rathbun » Tue Mar 22, 2011 7:03 pm

I can think of a possible use for this.

If the components were lego blocks... a user could draw a 3D design of what they wanted to build, and then voxelize it in lego blocks.

If it were possible to "layerize" the voxelization, it may be easier for the user to actaully build the real deal using real lego blocks.
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Voxelize

    Postby TheDro » Tue Mar 22, 2011 7:17 pm

    Well to "layerize", if by that you mean look at the model layer by layer, you can use the Section Plane tool. The plugin produces a 3D matrix of 1s and nils which could easily be manipulated, too. As for the lego blocks, that could be implemented by using this matrix in a clever way.
    0

    TheDro 
     

    Re: Voxelize

    Postby Rich O Brien » Tue Mar 22, 2011 7:38 pm

    This screams out for Sketchy Physics to drop all those cubes.
    1
    There's a frontroom and a backroom....reverse faces
    User avatar
    Rich O Brien 
    Administrator
     

    Re: Voxelize

    Postby Dan Rathbun » Tue Mar 22, 2011 8:21 pm

    Made a few changes for you Andrew:

    • properly module wrapped the plugin.
    • cube size is remembered during session in var @@cube_size
    • made it easier to control menu with constants defined at top of file
    • added validation for the menu item

    I think the "group test" should fire before the inputbox is displayed.
    But otherwise it looks good.

    Latest version now in the first post
    0
    Last edited by Dan Rathbun on Fri Mar 25, 2011 10:16 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: Voxelize

      Postby TheDro » Tue Mar 22, 2011 9:11 pm

      Thanks for the changes, Dan. It's my first time programming in ruby and I'm still a bit iffy about modules and classes and when to use which. I've looked at some documentation about them but haven't had time to read it thoroughly. I've preceded my method names by td_ based on something I read in a thread around here having to do with name conflicts between plugins. Do I need to do that or can I skip it since my plugin is now in its own module?
      0

      TheDro 
       

      Re: Voxelize

      Postby Khai » Tue Mar 22, 2011 9:18 pm

      guys...

      Minecraft!?!?!

      if you can get this to save out to the Minecraft format...
      0

      Khai 
       

      Re: Voxelize

      Postby Dan Rathbun » Tue Mar 22, 2011 9:24 pm

      Another suggestion for the Undo operation:

      (1) Define a module var @@op=0 (or whatever) at the top of the code.

      (2) Then in your td_voxelize() method, wrap the operation in a begin .. rescue .. end block:
      Code: Select all
      begin
        model.start_operation( "Voxelize(#{@@op+=1})" )
        # .. voxelize code ...
        model.commit_operation
      rescue Exception => e
        model.abort_operation
        puts("Voxelize Operation Error: #{e.message}")
        return nil
      else
        # only executes if no errors occur
        return true # or whatever for success
      ensure
        # ALWAYS executes no matter what happens
      end # operation

      (a) If any untrapped errors occur, the rescue clause will abort the operation (hopefully preventing model corruption.) Also if you do trap any errors that you feel should cause an abort, you can raise an exception (of some kind,) within the voxelize code, to cause execution of the rescue clause.
      If you don't know which exception to raise, using RuntimeError is fine, and you can set the message to whatever suits your fancy:
      raise(RuntimeError,"Some custom Voxelize Error Message here.")

      (b) The @@op counter gives each operation a unique name on the Undo stack. (Similar to when you open multiple new documents in an application. The first gets a name "untitled.doc", the second "untitled2.doc", etc.)

      EDIT: Added the else and ensure clauses to example, so you can see where they'd go (if you use them.)
      0
      Last edited by Dan Rathbun on Tue Mar 22, 2011 9:39 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: Voxelize

        Postby Dan Rathbun » Tue Mar 22, 2011 9:33 pm

        TheDro wrote:I've preceded my method names by td_ based on something I read in a thread around here having to do with name conflicts between plugins. Do I need to do that or can I skip it since my plugin is now in its own module?

        You can skip it now... since it's within it's own namespace, it cannot clash with any other method outside the namespace (including your other plugin submodules.) So td_voxelize() can become just plain ol' voxelize()

        I wrote a couple of topics on using modules:
        [info] Using Ruby Modules
        [talk] Using Ruby Modules
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: Voxelize

          Postby Dan Rathbun » Tue Mar 22, 2011 9:51 pm

          TheDro wrote:It's my first time programming in ruby and I'm still a bit iffy about modules and classes and when to use which.

          Generally...

          If only 1 copy of the code is needed, it should be a module.

          If more than one copy (instance,) is needed, it should be a class. (There are exceptions, the so-called "Singleton Classes" like TrueClass, FalseClass, NilClass and in practice, most Sketchup Tools should be written as a Singleton Class. For a Singleton, only 1 instance should be in existance any any given time.

          The link in my signature line will lead you to a bunch of Resources. Tutorials and alot of books you can downlaod for free.
          0
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: Voxelize

            Postby TheDro » Wed Mar 23, 2011 5:10 am

            @Khai Hah! Minecraft is actually what first inspired me to write this plugin since it would be a relatively simple way of creating complicated geometries (building plans). It may have other applications though such as Monte Carlo simulation using Geant4. Geant4 is basically a particle physics toolkit that's used to simulate particles interacting with matter. Making geometry with the toolkit can be quite tedious to do code-wise and is far more difficult than using Sketchup. Making cubes is simple though and the application of my plugin is obvious in this case. Anyways, back to Minecraft :P. As for exporting to a compatible format, I would first have to figure out what that format is but I'm sure it wouldn't be too difficult since the plugin already creates a 3D "binary" matrix. I'll get to that ... eventually.

            @Dan Thanks again. I'll get to reading your links concerning modules either tomorrow or the day after.
            0

            TheDro 
             

            Re: [Plugin] Voxelize

            Postby 22curious » Thu Mar 24, 2011 2:46 pm

            Thanks TheDro & Dan!

            How about adding options for:
            * spreading the cubes out, with inputs for x, y, and z separation spaces
            * choosing which planes to slice, one, some, or all... x-z, y-z, x-y
            ?

            Are there any existing plugins do these slicing, dicing, and spread-em-out actions?
            0

            22curious 
             

            Re: [Plugin] Voxelize

            Postby TheDro » Thu Mar 24, 2011 3:59 pm

            Well it's possible to spread out the cubes just by modifying any one of the cubes since they are all instances of the same component. So you can double click on one of the cubes, then use the scaling tool to shrink it, and they will all shrink while remaining in position, and this will produce spacing between them all. As for the plane slicing, I'm not sure I follow. Could explain in more detail, 22curious?

            I'll probably release a new version tonight that calculates the position of the cubes MUCH faster.
            0

            TheDro 
             

            Re: [Plugin] Voxelize

            Postby Dan Rathbun » Thu Mar 24, 2011 4:08 pm

            TheDro wrote:I'll probably release a new version tonight that calculates the position of the cubes MUCH faster.

            It is customary (here at SCF,) to replace the file in the first post of the topic, with the new version, and then edit the topic title (which only you can do because you "own" the topic,) to reflect the revision date.
            Example revised topic title:
            "[Plugin] Voxelize - Updated 2011-03-24"

            * standard date format for Ruby is YYYY-MM-DD

            Then you can slide all the way down to the bottom of the webpage, and click the "Bump" link to bring the topic to the top of the forum list (without needing to post a new message.)

            After you do that, I'll remove my version 1a and put a link back to the first post.
            0
              I'm not here much anymore. But a PM will fire email notifications.
              User avatar
              Dan Rathbun 
              PluginStore Author
              PluginStore Author
               

              Re: [Plugin] Voxelize

              Postby 22curious » Thu Mar 24, 2011 7:31 pm

              spread out the cubes...double click on one...use scaling tool to shrink it.


              This is where that Homer [doh] audio plugin button comes in handy. Of course!
              'Wearing too many hats, my SKU skills are SKrUsty...

              As for the plane slicing
              My need may be met using asteronimo's SliceModeler.rb "plugin that slices a solid 3d model along 2 axes, f.i. an X- and a Z-axis." SliceModeler.rb is based on Tig's Slicer.rb (that isn't on the plugins list? but found at viewtopic.php?f=180&t=10413&p=68825&hilit=slicer.rb#p688). I'm checking SliceModeler and Voxelize out now!

              ...another path may be combining a lattice>>cutting component>>Tig's Hole punching Tool.
              0

              22curious 
               

              Re: [Plugin] Voxelize - Updated 2011-03-24

              Postby Gillman » Tue Mar 29, 2011 2:19 pm

              Very intesting plug-in, and practical too. I was looking for a quick way to place a repeating element on a sphere (in this case Clint Howard's ship from Star Trek TOS "The Corbomite Manuver"). I edited the cube and replaced it with a pyramid with geodesic spheres at each angle.
              Fesperus.jpg


              It turned what would have been a mind-numbing task into something trivial. Now all I have to do is upload it to the warehouse.
              0

              Gillman 
               

              Re: [Plugin] Voxelize - Updated 2011-03-24

              Postby TheDro » Wed Mar 30, 2011 1:52 pm

              @Gillman: I'm glad to hear that it was of use to you.

              @Khai: I've been looking at the structure of the .schematic file which is used to import structures into Minecraft using MCEdit. I think I've got it figured out but I still need to write the code and I'm reading the book of ruby to try and figure out the best way to do it.

              @Everyone: I've added a new "spiral" algorithm to prevent interpreting sharp edges of solids as "entrances" to the solids. Basically, I improved the algorithm to work with more solids which is good. I'll upload the new version once I'm not too busy with work/school.
              0

              TheDro 
               

              Re: [Plugin] Voxelize - Updated 2011-03-24

              Postby 22curious » Wed Mar 30, 2011 2:07 pm

              @TheDro, the spiral refinement will be useful - looking forward to it - thanks!
              0

              22curious 
               

              Re: [Plugin] Voxelize - Updated 2011-03-24

              Postby Khai » Tue Apr 05, 2011 1:37 am

              thanks Dro :D
              0

              Khai 
               

              Re: [Plugin] Voxelize - Updated 2011-04-04

              Postby mitcorb » Tue Apr 05, 2011 11:55 am

              You will discover, while scaling an instance of a component, that the others do not scale accordingly, at least while using the native scale tool.
              0
              I take the slow, deliberate approach in my aimless wandering.
              User avatar
              mitcorb 
              Top SketchUcator
               

              Re: [Plugin] Voxelize - Updated 2011-04-04

              Postby TIG » Tue Apr 05, 2011 12:18 pm

              Scaling one Component Instance from the 'outside' won't affect other Component Instances of the same Component BUT scaling a Component Instance's 'contents' will change all Component Instances of that Component - that's because when you edit the contents of a Component Instance you change the Component Definition itself and this is repeated across all of its Instances]...
              0
              TIG
              User avatar
              TIG 
              Global Moderator
               

              Re: [Plugin] Voxelize - Updated 2011-04-04

              Postby mitcorb » Tue Apr 05, 2011 12:39 pm

              Aaah yes. Thank you. :)
              0
              I take the slow, deliberate approach in my aimless wandering.
              User avatar
              mitcorb 
              Top SketchUcator
               

              Re: [Plugin] Voxelize - Updated 2011-04-04

              Postby Lobster » Sat Jun 11, 2011 12:07 pm

              Thanks for this,

              Interesting plug in.

              voxelise.png
              0

              Lobster 
               

              Re: [Plugin] Voxelize - Updated 2011-04-04

              Postby Dave R » Sat Jun 11, 2011 1:09 pm

              Is there any chance of making this work to allow selection of a component as well as a group?
              0
              %

              (THERE'S NO PLACE LIKE)

              G28 X0.0 Y0.0 Z0.0

              M30

              %
              User avatar
              Dave R 
              Global Moderator
               

              SketchUcation One-Liner Adverts

              by Ad Machine » 5 minutes ago



              Ad Machine 
              Robot
               

              Next


               

              Return to Plugins

              Who is online

              Users browsing this forum: AntBM, Debora Macedo, Google Bot, ljus, ngoctung0211 and 13 guests

              Visit our sponsors: