[Plugin] Voxelize - Updated 2011-04-04
-
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 . 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.
-
-
Cool, fun plugin.
One problem it reverses faces on blue axis.
-
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.
-
No, see image, up faces are revered after operation.
-
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?
-
Real fun to play with.
Voxel turtle.
-
@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!
-
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.
-
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.
-
This screams out for Sketchy Physics to drop all those cubes.
-
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?
-
guys...
Minecraft!?!?!
if you can get this to save out to the Minecraft format...
-
@thedro said:
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 -
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:
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.)
-
@thedro said:
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.
-
@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 . 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.
-
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?
-
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.
-
@thedro said:
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.
-
@unknownuser said:
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...@unknownuser said:
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 http://forums.sketchucation.com/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.
Advertisement