https://brlcad.org/w/api.php?action=feedcontributions&user=50.35.159.228&feedformat=atomBRL-CAD - User contributions [en]2019-10-13T23:08:16ZUser contributionsMediaWiki 1.32.2https://brlcad.org/w/index.php?title=Spiral&diff=10038Spiral2017-06-15T04:19:15Z<p>50.35.159.228: /* Scripting MGED with Perl */</p>
<hr />
<div>[[category:tutorials]]<br />
<center><br />
{|align="right"<br />
|[[Image:Spiral.png|thumb|left|256px|Spiraling boxes, modeled with a Perl script and rendered in BRL-CAD]]<br />
|}<br />
</center><br />
<br />
Included below is an example Perl script that uses BRL-CAD tools to procedurally create and render a spiral of boxes. The script creates boxes (RPPs) with holes cut out of them as an underlying shape pattern, then replicates that shape around in an outward spiraling pattern. The shapes are then combined together into a proper region, and a resulting render is shown.<br />
<br />
=Scripting MGED with Perl=<br />
BRL-CAD's solid modeling editor, MGED, will run most commands provided after the name of the geometry database on the command line. The "-c" option tells MGED to run in classic console mode without a GUI, which is perfect for scripting from most any language including Perl. First in Ruby written by, Dwayne Hans, 2016.<br />
<br />
This script takes advantage of the fact that it is considerably more efficient to batch multiple MGED commands together instead of re-invoking MGED multiple times. It builds up a list of commands for one segment, then groups them together in batches of 100 before running MGED. This avoids considerable operating system and process initialization overhead, allowing the script to run orders of magnitude faster than if the segments were created one at a time.<br />
<br />
=The Script=<br />
{|<br />
|<pre><br />
#!/usr/bin/env perl<br />
#<br />
# Generates a 3D model of a spiral via BRL-CAD.<br />
# This script is in the public domain.<br />
#<br />
######################################################################<br />
<br />
use warnings;<br />
use constant PI => 4.0 * atan2(1, 1);<br />
<br />
$t_min = 0;<br />
$t_max = 10 * PI; # 2*PI per revolution so 5 revolutions<br />
$granularity = 1500; # this is the total number of segments<br />
<br />
# This is the basic hollowed-out box shape:<br />
# in 1.s rpp 0 .5 0 1 0 1<br />
# in 2.s rpp -2 2 0.1 0.9 0.1 0.9<br />
# 1.s - 2.s<br />
<br />
# dimensions of the box<br />
$xmin = 0;<br />
$xmax = .5;<br />
$ymin = 0;<br />
$ymax = 1;<br />
$zmin = 0;<br />
$zmax = 1;<br />
<br />
# Thickness of the walls.<br />
$thickness = .1;<br />
<br />
# You want this going through so that you can cut a hole through the object.<br />
$xmin2 = -2; # $xmin+$thickness;<br />
$xmax2 = 2; #$xmax-$thickness;<br />
$ymin2 = $ymin + $thickness;<br />
$ymax2 = $ymax - $thickness;<br />
$zmin2 = $zmin + $thickness;<br />
$zmax2 = $zmax - $thickness;<br />
<br />
# default keypoint is bottom corner, so calculate the box base center<br />
$xkey = ($xmax - $xmin) / 2.0;<br />
$ykey = ($ymax - $ymin) / 2.0;<br />
<br />
# Future improvement possibility: make it spiral downward.<br />
<br />
# generate the spiral<br />
$i = 0; # $i is the counter.<br />
$command = "";<br />
for ($j = $t_min; $j <= $t_max; $j += $t_max / $granularity, $i++) {<br />
# x/y position of the box (not exact)<br />
$x = $j * cos($j); $y = $j * sin($j);<br />
<br />
# here we calculate the angle to rotate the box.<br />
# we add 6 degrees "fudge factor" since we're not calculating an exact x/y<br />
$ang = 96.0 - atan2($y, $x) * 180.0 / PI;<br />
<br />
# command to create a hollowed box segment, rotate, and move it into place<br />
$command .= "in box_$i.s rpp $xmin $xmax $ymin $ymax $zmin $zmax ;<br />
in hole_$i.s rpp $xmin2 $xmax2 $ymin2 $ymax2 $zmin2 $zmax2 ;<br />
comb seg_$i.c u box_$i.s - hole_$i.s ;<br />
draw seg_$i.c ;<br />
oed / /seg_$i.c/box_$i.s ;<br />
keypoint $xkey $ykey 0 ;<br />
rot 0 0 $ang ;<br />
translate $x $y 0 ;<br />
accept ;";<br />
<br />
# create 100 segments at a time to reduce process initialization overhead<br />
if ($i % 100 == 0) {<br />
print "#$i at\tx = $x\ty=$y\n\t$command\n\n";<br />
`mged -c spiral.g \'$command\'`;<br />
$command = "";<br />
}<br />
}<br />
# pick up any remaining segments if $granularity isn't a multiple of 100<br />
if ($command ne "") {<br />
print "#$i at\tx = $x\ty=$y\n\t$command\n\n";<br />
`mged -c spiral.g \'$command\'`;<br />
$command = "";<br />
}<br />
<br />
print "Grouping the spiral segments together into one shape...(please wait)...";<br />
`mged -c spiral.g 'g spiral.c *.c'`;<br />
print "done.\n";<br />
<br />
print "Making the shape into a solid region (so it has mass and occupies space)...\n";<br />
`mged -c spiral.g 'r spiral.r u spiral.c'`;<br />
<br />
print "All done.\n=n";<br />
</pre><br />
|}<br />
<br />
There are, of course, many ways that this script could be improved upon such as spacing the segments out evenly. For example, there are minor issues with the way the script calculates the position and rotation of individual box segments (hence the fudge factor), but it does the job well enough for this demonstration. <br />
<br />
This script was originally written by [http://heybryan.org Bryan Bishop] and is available for download from [http://brlcad.svn.sourceforge.net/viewvc/brlcad/brlcad/trunk/src/proc-db/spiral.pl '''here'''].</div>50.35.159.228