Difference between revisions of "MGED CMD simulate"

From BRL-CAD
m (Sean moved page Mged simulation to MGED CMD simulate)
 
(17 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
[[category:tutorials]]
 
[[category:tutorials]]
  
BRL-CAD recently integrated a new command in its mged tool, the simulate command. To simulate a cube falling to the ground plane one should follow those steps in order to run it in the mged tool.
+
BRL-CAD recently integrated a new command into '''mged''', the '''simulate''' command, which allows for rigid-body physics simulations. This tutorial demonstrates the usage of the '''simulate''' command with a simple scene.
  
 
{|align="right"
 
{|align="right"
Line 7: Line 7:
 
  |}
 
  |}
  
BRL-CAD is a CSG (Constructive Solid Geometry) program. The representation is done in a system of axis XYZ. The XY axis is the ground axis, so you should make sure later in the tutorial that your ground plane (referenced as "gp") is being parallel with this axis.
+
First, open the '''mged''' interface and create a new database. Next we want to add some geometry to the database. In the command window we will now type the following series of commands:
 
 
First open the mged tool, if you have BRL-CAD installed on your Linux system it should be easy, just type mged in a terminal and a mged terminal should open.
 
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
brlcad@brlcad: mged
+
mged> units m
 +
mged> in cube.s rpp -1 1 -1 1 -1 1
 +
mged> r cube.r u cube.s
 +
mged> attr set cube.r simulate::type region
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
In the mged terminal select the File menu, and then the New... (file dialog button). A window will appear and a name for your new database will be required. For the purpose of this tutorial we will name the new database, sim1. Introduce the chosen name (sim1) in the "File name:" field and hit save.
+
This will create a cube primitive and a containing region. We will use the region later to apply material appearances. We also set the '''simulate::type''' attribute so that the '''simulate''' command regards all primitives within this region as one object.  
Now you have opened a new database. Next we want to add some geometry to the database. Something that we will have to simulate on.  
+
 
In the mged command window we will now type the following series of commands :
 
<br>
 
{|align="left"
 
|<pre>
 
mged> in
 
Enter name of solid: cube
 
Enter solid type: rpp
 
Enter XMIN, XMAX, YMIN, YMAX, ZMIN, ZMAX: -1 1 -1 1 -1 1
 
</pre>
 
|}
 
 
<br style="clear: both" />
 
<br style="clear: both" />
The "in" command creates new geometry, it allows the user to type in the arguments to create a shape with the name provided by the second argument, in the example above is the name provided when "Enter name of solid:" appears on the screen. The arguments of the in command can be provided one by one, as in the example above or in one single line, like in the example below:
+
Now we want to add a fixed "ground" object to our simulation. BRL-CAD's simulated gravity acts in the ''x''-''y'' axis, so our ground plane should be parallel to this axis.
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
mged> in cube rpp -1 1 -1 1 -1 1
+
mged> in ground.s rpp -15 15 -15 15 -1 1
 +
mged> r ground.r u ground.s
 +
mged> attr set ground.r simulate::type region
 
</pre>
 
</pre>
 
|}
 
|}
 +
 
<br style="clear: both" />
 
<br style="clear: both" />
We have so far a box, now we want to add a ground plane to our simulation. Now is the moment to remember that the XY axis is the ground axis, so out plane should be parallel to it, that's why we first introduce the lengths of the the ground plane and then we introduce the "thickness", in our case 2mu. The Z axis is up, so the -1mu and +1mu on it gives the "thickness" of our ground plane. The ground plane must be name "gp", that's the way the program knows that you are defining a surface that will play the role of the ground. After creating regions, the ground region will be named "gp.r" so that the simulation will know that it is actually the plane on which you want your objects to fall onto.
+
 
(*mu = measuring units, by default it is mm)
+
We will also add an attribute specifying that this object's position is fixed:
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
mged> in gp rpp -15 15 -15 15 -1 1
+
mged> attr set ground.r simulate::mass 0.0
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
You should see now 2 boxes on your screen, one perpendicular on the other one, with a little overlap between them.
+
 
Using the sed command we will select the box and translate it 100 units on the Y axis.
+
We can also give the cube an initial angular velocity (linear velocity can be applied in a similar way):
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
mged> sed box
+
mged> attr set cube.r simulate::angular_velocity <2.0,-1.0,3.0>
mged> tra 0 0 100
 
mged> accept
 
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
  
To be able to run the simulate command our geometry shapes should be grouped in regions to do this we will use the "r" command and create 2 regions for the box and the ground shapes.
+
Now we will add some material appearances to the objects:
 
 
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
mged> r cube.r u cube
+
mged> mater cube.r "plastic {tr 0.2 re 0.2}" 255 0 0 y
mged> r gp.r u gp
+
mged> mater ground.r "plastic {tr 0.2 re 0.2}" 0 0 255 y
</pre>
 
|}
 
<br style="clear: both" />
 
We want our ground plane and cube to have a texture, for uniformity, we add textures to regions in BRL-CAD with the "matter" command, we will add a plastic texture with a transparency of 20% and a reflexivity of 20% also for both regions.
 
<br>
 
{|align="left"
 
|<pre>
 
mged> matter cube.r "plastic {tr 0.2 re 0.2}" 210 100 100 0
 
mged> matter gp.r "plastic {tr 0.2 re 0.2}" 210 100 100 0
 
</pre>
 
|}
 
<br style="clear: both" />
 
Now we are ready to run the simulate command:
 
<br>
 
{|align="left"
 
|<pre>
 
mged> simulate 100
 
</pre>
 
|}
 
<br style="clear: both" />
 
After the simulation runs its course, we will use the "who" command to find out what objects are being displayed.
 
<br>
 
{|align="left"
 
|<pre>
 
mged> who
 
</pre>
 
|}
 
<br style="clear: both" />
 
You should get the following answer:
 
<br>
 
{|align="left"
 
|<pre>
 
box ground sim.c
 
</pre>
 
|}
 
<br style="clear: both" />
 
Now, we don't really want to display the box and the ground shapes, we already have them in the sim.c group so we will unload all the geometry(using the "Z" command) and will load only the simulate geometry(using the "draw sim.c" command).
 
<br>
 
{|align="left"
 
|<pre>
 
mged> Z
 
mged> draw sim.c
 
</pre>
 
|}
 
<br style="clear: both" />
 
Now to actually see the simulation we will raytrace it, using the "rt" command.
 
<br>
 
{|align="left"
 
|<pre>
 
mged> rt
 
</pre>
 
|}
 
<br style="clear: both" />
 
To see the initial position of the cube just type :
 
<br>
 
{|align="left"
 
|<pre>
 
mged> draw cube.r
 
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
  
Now in order to create an animation from your simulation, you will have to run the script below. It simulates the cube falling on the ground every step for 300 steps and then it renders an image(for every step) with the specified elevation view (in degrees is being specified). The "rt" command executes the BRL-CAD rt program with the default options. The default dimensions for the image are 512x512pixels, that can be changed and it is changed for the purpose of this tutorial providing the -w(width) and -n(height) options. The -e option provides the elevation angle(The angle of elevation of an object as seen by an observer is the angle between the horizontal and the line from the object to the observer's eye (the line of sight) and there can also be an -a option for the azimuth angle(The azimuth of an object is the angular distance along the horizon to the location of the object.). The current database name is added to the end of the rt command line along with either the specified objects or, if none is specified, the list of currently displayed objects.
+
To specify our simulated system we will define a combination containing both objects. This combination will be modified in-place during the simulation.
 +
We will also change the units of gravity for the simulation to be
 +
acceleration in mm/s/s due to the small scale of our model.
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
#!/bin/sh
+
mged> comb system.c u cube.r u ground.r
# simulate.sh
+
mged> attr set system.c simulate::gravity <0,0,-9.80665>
########################################################################
 
 
 
for i in `seq 1 300` ; do
 
echo "Simulating..."
 
mged sim.g simulate $i
 
echo "Raytracing..."
 
                rt -e 35 -w 640 -n 480 -o image$i.png sim.g sim.c
 
done
 
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
  
 
+
We will now select the cube and translate it 50 units "up" along the ''y''-axis.
Now if you don't want just to specify the azimuth angle (or) and the elevation angle for your simulation, then you can create a whole scene setting for it. To understand this better, you can go back in the mged window terminal, and from the visual panel menu window select the "View" option, then the "az35,el25", this will set the view of your current database to azimuth 35 and elevation 25 degrees. To save this view for later use type, in the mged terminal:
 
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
mged> saveview az35
+
mged> B system.c
 +
mged> oed /system.c/cube.r cube.s
 +
mged> tra 0 0 50
 +
mged> accept
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
  
To add the saved view from the saved file to your own rt script, you have to open the az35 saved file with a text editor program, say gedit on Linux systems, and copy paste the info from starting with the "$*\" and ending with the second "EOF" line. What do those lines do? They specify the location of your database and the top regions from it that you want to take place in the simulation, much likely the "rt [- options] sim.g sim.c" command, only now you give the whole path to your sim.g file and in there you use the sim.c group. now lets explain those numbers that we just copy-pasted :
+
In order to see what we did so far we should set the view for the geometry we have so far. We will use an "ae 35 15", that means the azimuth angle is at 35 degrees and the elevation at 15 degrees. The angle of elevation of an object as seen by an observer is the angle between the horizontal and the line from the object to the observer's eye (the line of sight).The azimuth of an object is the angular distance along the horizon to the location of the object.We will use "autoview" command to reset the view size and the view center such that all displayed objects are within the view. And then we will do a translation on the Y axis.
* viewsize is a variable to resize the screen through software;
 
* orientation is placement of an object in a rotational coordinate system (XYZ in our case) with respect to a fixed point and a reference position;
 
* eye_pt is the point at which the eye is placed when looking at the scene.
 
The -M option for the rt command specifies that you are going to provide a view script for a file, that's what you are doing between the 2 EOF lines. The -s800 option makes it possible for you to get an 800x800 pixels image. You can try and modify it, to get different size images. The pix-png command outputs a png file from the pix file that was previously rendered.
 
 
 
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
#!/bin/sh
+
mged> ae 35 15
# simulate.sh
+
mged> autoview
########################################################################
+
mged> tra 0 10 0
 
 
for i in `seq 1 300` ; do
 
echo "Simulating..."
 
mged sim.g simulate $i
 
echo "Raytracing..."
 
rt -M -s800 -o image$i.pix \
 
$*\
 
'/home/brlcad/brlcad/sim.g'\
 
'sim.c' \
 
2>> sim.log\
 
<<EOF
 
viewsize 1.17972214000000e+02;
 
orientation 2.48097349045873e-01 4.76590573266048e-01 7.48097349045873e-01 3.89434830518390e-01;
 
eye_pt 1.21705426379985e+02 8.52190569922928e+01 9.82677168741064e+01;
 
start 0; clean;
 
end;
 
 
 
EOF
 
echo "Generating image..."
 
pix-png -s800 image$i.pix > image$i.png
 
rm -f image$i.pix
 
echo "image"$i".png done"
 
done
 
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
Now that we have a bunch of pictures we want to actually see the cube falling to the ground, we could watch the pictures on a slide-show, but we can do better than that. We will make an *mpg movie out of our pictures. For that you need to install the latest ImageMagick software on Linux , just type :
+
Now we can save this view for later:
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
brlcad@brlcad: sudo apt-get install imagemagick
+
mged> saveview ae3515.rt
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
You will get some dependencies that you must install before installing the ImageMagick package, install those. After everything has been installed, move to the folder where you have your images saved and run the following command:
+
 
 +
Now we are ready to run the simulate command. To preserve our configuration we will first create a copy of our combination.
 
<br>
 
<br>
 
{|align="left"
 
{|align="left"
 
  |<pre>
 
  |<pre>
brlcad@brlcad: convert *png simulation.mpg
+
mged> cp system.c output.c
 +
mged> B output.c
 +
mged> simulate output.c 10.0
 
</pre>
 
</pre>
 
|}
 
|}
 
<br style="clear: both" />
 
<br style="clear: both" />
That is it, now you can watch a movie of your simulation.
+
This will evaluate 10 seconds of simulated physics.
  
(* the install commands are for the Ubuntu - distributions, you must use your package manager for installing ImageMagick on your own Operating System)
+
In order to create an animation from your simulation, you will have to run the script below (note that it requires ImageMagick): (in progress)

Latest revision as of 17:58, 5 December 2017


BRL-CAD recently integrated a new command into mged, the simulate command, which allows for rigid-body physics simulations. This tutorial demonstrates the usage of the simulate command with a simple scene.

Cube falling to the ground plane modeled and rendered in BRL-CAD

First, open the mged interface and create a new database. Next we want to add some geometry to the database. In the command window we will now type the following series of commands:

mged> units m
mged> in cube.s rpp -1 1 -1 1 -1 1
mged> r cube.r u cube.s
mged> attr set cube.r simulate::type region


This will create a cube primitive and a containing region. We will use the region later to apply material appearances. We also set the simulate::type attribute so that the simulate command regards all primitives within this region as one object.


Now we want to add a fixed "ground" object to our simulation. BRL-CAD's simulated gravity acts in the x-y axis, so our ground plane should be parallel to this axis.

mged> in ground.s rpp -15 15 -15 15 -1 1
mged> r ground.r u ground.s
mged> attr set ground.r simulate::type region


We will also add an attribute specifying that this object's position is fixed:

mged> attr set ground.r simulate::mass 0.0


We can also give the cube an initial angular velocity (linear velocity can be applied in a similar way):

mged> attr set cube.r simulate::angular_velocity <2.0,-1.0,3.0>


Now we will add some material appearances to the objects:

mged> mater cube.r "plastic {tr 0.2 re 0.2}" 255 0 0 y
mged> mater ground.r "plastic {tr 0.2 re 0.2}" 0 0 255 y


To specify our simulated system we will define a combination containing both objects. This combination will be modified in-place during the simulation. We will also change the units of gravity for the simulation to be acceleration in mm/s/s due to the small scale of our model.

mged> comb system.c u cube.r u ground.r
mged> attr set system.c simulate::gravity <0,0,-9.80665>


We will now select the cube and translate it 50 units "up" along the y-axis.

mged> B system.c
mged> oed /system.c/cube.r cube.s
mged> tra 0 0 50
mged> accept


In order to see what we did so far we should set the view for the geometry we have so far. We will use an "ae 35 15", that means the azimuth angle is at 35 degrees and the elevation at 15 degrees. The angle of elevation of an object as seen by an observer is the angle between the horizontal and the line from the object to the observer's eye (the line of sight).The azimuth of an object is the angular distance along the horizon to the location of the object.We will use "autoview" command to reset the view size and the view center such that all displayed objects are within the view. And then we will do a translation on the Y axis.

mged> ae 35 15
mged> autoview
mged> tra 0 10 0


Now we can save this view for later:

mged> saveview ae3515.rt


Now we are ready to run the simulate command. To preserve our configuration we will first create a copy of our combination.

mged> cp system.c output.c
mged> B output.c
mged> simulate output.c 10.0


This will evaluate 10 seconds of simulated physics.

In order to create an animation from your simulation, you will have to run the script below (note that it requires ImageMagick): (in progress)