Interfacing ArtiSynth to MATLAB

4 Querying ArtiSynth structures and models

Through the ArtiSynth handle, it is possible to access most of the Java objects associated with ArtiSynth and its loaded models. Public methods for these objects can be called directly from MATLAB. Java objects can be created by calling their constructors directly, without the need for the keyword new. For example,

   >> vec = maspack.matrix.Vector3d (1, 2, 3);

creates a new instance of maspack.matrix.Vector3d and initializes it to (1, 2, 3). As in Java, import statements can be used to allow classes to be specified without using their full package names:

   >> import maspack.matrix.*
   >>
   >> vec = Vector3d (1, 2, 3);

For more details on working with Java objects inside MATLAB, see Call Java Libraries in the MATLAB documentation.

To easily access particular components of a model, the handle method getsel() provides access to to the ArtiSynth selection list. That means you can select items in the ArtiSynth GUI (using either the viewer or navigation panel) and then retrieve these into MATLAB using getsel(). If called with no arguments, getsel() returns the entire selection list as a cell array. If called with an integer argument i, getsel(i) returns the i-th entry in the selection list (where the index i is 1-based).

For example, if two particles are currently selected in ArtiSynth, then getsel() can be used as follows:

   >> ah.getsel()       % get entire selection list
   ans =
       [1x1 artisynth.core.mechmodels.Particle]
       [1x1 artisynth.core.mechmodels.Particle]
   >> ah.getsel(1)      % get first item on the selection list
   artisynth.core.mechmodels.Particle@49752dd4

Once a component has been selected, then one has access to all its public methods. The functions mmat(), amat(), and avec() can be used to map between MATLAB arrays and ArtiSynth Matrix and Vector objects:

mmat(obj)

Creates a MATLAB array from an ArtiSynth Vector or Matrix object. If the Matrix is an instance of SparseMatrix, then mmat() returns a MATLAB sparse matrix. If obj is not a Vector or Matrix, then the method returns the empty matrix.

amat(M)

Creates an ArtiSynth Matrix from a MATLAB array: either a MatrixNd if M is a full matrix, or a SparseMatrixNd if M is sparse.

avec(M)

Creates an ArtiSynth VectorNd from a MATLAB array. At least one of the dimensions of M must be 1.

As a simple example, assume that part refers to an ArtiSynth Particle object. The following code fragment then obtains the particle’s position as a MATLAB array, scales it by 3, and then uses this to reset the position:

   >> import maspack.matrix.*
   >>
   >> pos = mmat(part.getPosition())
   pos =
         5
         0
        10
   >> pos = 3*pos;
   >> part.setPosition (Point3d (avec(pos)));

The particle’s position is returned by the method getPosition(), which returns a Point3d. Since this is an instance of Vector, we use mmat() to turn this into a MATLAB array named pos. After scaling, we turn this back into an ArtiSynth Vector using avec(pos) and reset the particle’s position using the setPosition() method. This method requires a Point3d argument, whereas avec(pos) returns a more general VectorNd object. However, we can create the required Point3d from the VectorNd using the Point3d(Vector) constructor.

As a more complex example, assume that fem refers to an ArtiSynth FemModel3d. The following code fragment then obtains the stiffness matrix for this model and uses MATLAB to find its largest Eigenvalues:

   >> K = mmat (fem.getActiveStiffness());
   >> eigs (K)
   ans =
      1.0e+04 *
      -8.5443
      -8.5443
      -6.6442
      -6.6442
      -5.0636
      -4.4762

The current stiffness matrix associated with the active nodes is returned by getActiveStiffness(). Since this is an instance of SparseBlockMatrix, mmat() converts it to a MATLAB sparse matrix, for which we can then find the largest Eigenvalues using eigs().

It is also possible to directly query and set the numeric data associated with ArtiSynth input and output probes. This makes it possible to use MATLAB to plot or process output probe data, or compute and prepare input probe data.

Methods to access probe data are provided by the ArtisynthHandle:

   getIprobeData (name);     // get data for the specified input probe
   setIprobeData (name, D)   // set data for the specified input probe
   getOprobeData (name)      // get data for the specified output probe
   setOprobeData (name, D)   // set data for the specified output probe

The probe in question must be a numeric probe, i.e., an instance of NumericProbeBase. name is a string giving either the name or number of the probe. The get() methods return the data as a MATLAB array, while the set() methods receive the data as the MATLAB array D. The data array is m\times n, where m is the number of knot points and n is the size of the probe’s data vector.

The following example shows the process of obtaining and then changing the numeric data for input probe 0 of the model SpringMeshDemo:

   >> D = ah.getIprobeData (’0’)
   D =
            0  -10.0000         0   20.0000
       1.0000         0         0   20.0000
       1.9400   10.0000         0    6.8000
       3.0000         0         0   10.0000
       4.0000  -10.0000         0   20.0000
       5.0000         0         0   20.0000
       6.0000   10.0000         0   20.0000
       7.0000         0         0   10.0000
       8.0000  -10.0000         0   20.0000
       9.0000         0         0   20.0000
      10.0000   10.0000         0   20.0000
      11.0000         0         0   10.0000
   >> % Now change D by removing all but the first 5 knot points:
   >> D = D(1:5,:);
   >> ah.setIprobeData (’0’, D);

When setting probe data from MATLAB, the number of columns in the supplied data array must equal the current size of the probe’s data vector.