A grid field specifies scalar or vector values at the vertices of a regular Cartesian grid and interpolates values between vertices. ArtiSynth currently provides two grid field components in artisynth.core.fields:
where T is any class implementing
maspack.matrix.VectorObject. For both grid types, the
value returned by getValue(p) is determined by finding the grid
cell containing and then using trilinear interpolation of the
surrounding nodal values. If
lies outside the grid volume,
then getValue(p) either returns the value at the nearest grid
point
(if the component property clipToGrid is set to true), or else returns a special value indicating that
is
outside the grid. This special value is ScalarGridField.OUTSIDE_GRID for scalar fields or null for
vector fields.
Grid field components are implemented as wrappers around the more basic objects ScalarGrid and VectorGrid<T> defined in maspack.geometry. Applications first create one of these primary grid objects and then use it to create the field component. Instances of ScalarGrid and VectorGrid<T> can be created with constructors such as
where widths gives the grid widths along each of the ,
and
axes, res gives the number of cells along each axis, and
for vector grids type is the class type of the
maspack.matrix.VectorObject object parameterized by T. TCL is an optional argument which, if not null,
describes the position and orientation of the grid center with respect
to local coordinates; otherwise, in local coordinates the grid is
centered at the origin and aligned with the
,
and
axes.
For the vector grid constructors above, T should be a fixed-size vector or matrix, such as Vector3d or Matrix3d. The variable sized objects VectorNd and MatrixNd can also be used with the aid of special wrapper classes, as described in Section 7.4.
By default, grids are axis-aligned and centered at the origin of the world coordinate system. A transform TLW can be specified to place the grid at a different position and/or orientation. TLW represents the transform from local to world coordinates and can be controlled with the methods
If not specified, TLW is the identity and local and world coordinates are the same.
Once a grid is created, it can be used to instantiate a grid field component using one of the constructors
where grid is the primary grid and name is an optional component name. Note that the primary grid is not copied, so any subsequent changes to it will be reflected in the enclosing field component.
Once the grid field component is created, its values can be set by specifying the values at its vertices. The methods to query and set vertex values for scalar and vector fields are
where xi, yj, and zk are the vertex’s indices along the x, y, and z axes, and vi is a general index that should be in the range 0 to field.numVertices()-1 and is related to xi, yj, and zk by
vi = xi + nx*yj + (nx*ny)*zk,
where nx and ny are the number of vertices along the
and
axes.
When computing a grid value using getValue(p), the point p is assumed to be in either grid local or world coordinates, depending on whether the field component’s property localValuesForField is true or false (local and world coordinates are the same unless the primary grid’s local-to-world transform TLW has been set as described above).
To find the spatial position of a vertex within a grid field component, one may use the methods
which return the vertex position in either local or world coordinates depending on the setting of localValuesForField.
The following code example shows the creation of a ScalarGridField, with widths and a cell
resolution of
, centered at the origin and
whose vertex values are set to their distance from the origin,
in order to create a simple distance field:
As shown in the example and as mentioned earlier, grid field are generally added to the fields list of a MechModel.