Description

Renderer of granular bodies as GPU instances of shape templates.

Each selected body is decomposed into its collision shapes (or, optionally, its visual shapes) and each shape is drawn as an instance of a template. Primitives that scale (sphere, ellipsoid, box, cylinder, cone) share one unit template per type, with a per-instance scale. Shapes that do not scale (capsules, rounded boxes and cylinders, triangle meshes, convex hulls) get one template per distinct geometry. The world transform of an instance is the body reference frame times the shape frame, so bodies with several shapes (e.g., clumps of spheres) are drawn as one instance per shape. All shapes of a body have the color of the body.

By default, the bodies drawn as instances are selected automatically by size: among the bodies that are active at initialization and whose shapes are all supported, those whose size (largest dimension of the bounding box of their shapes) is at most a factor (default: 10) times the median body size. Larger bodies (walls, containers, platens) keep the default binding of the visual system. An explicit selection (a list of bodies or a predicate) replaces the automatic one. The selected bodies are excluded from the default binding of the visual system.

Inactive bodies (fixed or sleeping) are hidden by default. Per-body color and visibility callbacks are supported.

This renderer is a component for VSG plugins. A plugin creates it, calls Initialize() from its OnBindAssets() function, and calls Update() at every frame, typically from its OnRender() function.

#include <ChInstancedShapesVSG.h>

Classes

class  ColorCallback
 Callback interface for per-body colors. More...
 
class  VisibilityCallback
 Callback interface for per-body visibility. More...
 

Public Types

enum  ShapeSource { ShapeSource::COLLISION, ShapeSource::VISUAL }
 Source of the shapes drawn for each body. More...
 

Public Member Functions

void SetShapeSource (ShapeSource source)
 Set the source of the shapes drawn for each body (default: COLLISION). More...
 
void SetSizeFactor (double factor)
 Set the size factor for the automatic selection of bodies (default: 10). More...
 
void SetBodies (const std::vector< std::shared_ptr< ChBody >> &bodies)
 Set an explicit list of bodies drawn as instances, replacing the automatic selection. More...
 
void SetBodySelector (std::function< bool(const ChBody &body)> selector)
 Set a predicate selecting the bodies drawn as instances, replacing the automatic selection. More...
 
void SetResolution (int num_divs)
 Set the number of divisions of a full circle used to tessellate round templates (default: 24). More...
 
void SetMaterial (std::shared_ptr< ChVisualMaterial > material)
 Set the material of the instanced shapes (default: ChVisualMaterial::Default()). More...
 
void SetDefaultColor (const ChColor &color)
 Set the color of bodies whose visual model defines no color (default: [0.75, 0.75, 0.75]). More...
 
void SetColorCallback (std::shared_ptr< ColorCallback > callback)
 Set a callback for the body colors, replacing the colors from the visual models.
 
void SetVisibilityCallback (std::shared_ptr< VisibilityCallback > callback)
 Set a callback for the body visibility. More...
 
void SetInactiveBodyVisibility (bool visible)
 Enable or disable drawing of inactive (fixed or sleeping) bodies (default: false).
 
void SetVisibility (bool visible)
 Show or hide all instanced bodies (default: visible).
 
bool IsVisible () const
 Return true if the instanced bodies are visible.
 
void Initialize (ChVisualSystemVSG &vsys, const std::vector< std::shared_ptr< ChBody >> &candidates)
 Select the bodies drawn as instances, create the templates, and add the instance nodes to the visual system. More...
 
void Update ()
 Update the instances from the current body states, colors, and visibility.
 
const std::vector< std::shared_ptr< ChBody > > & GetBodies () const
 Get the bodies drawn as instances.
 
bool IsInstanced (const ChBody &body) const
 Return true if the specified body is drawn as instances.
 
double GetMedianSize () const
 Get the median size of the candidate bodies, as computed by Initialize().
 
double GetSizeLimit () const
 Get the size limit for the automatic selection (the size factor times the median size).
 
unsigned int GetNumTemplates () const
 Get the number of templates.
 
unsigned int GetNumInstances () const
 Get the total number of shape instances (over all bodies drawn as instances).
 
unsigned int GetNumVisibleInstances () const
 Get the number of shape instances drawn at the last update.
 

Static Public Member Functions

static double GetBodySize (const ChBody &body, ShapeSource source)
 Get the size of a body: the largest dimension of the bounding box of its shapes, in the body reference frame. More...
 

Member Enumeration Documentation

◆ ShapeSource

Source of the shapes drawn for each body.

Enumerator
COLLISION 

shapes in the body collision model

VISUAL 

shapes in the body visual model

Member Function Documentation

◆ GetBodySize()

double chrono::vsg3d::ChInstancedShapesVSG::GetBodySize ( const ChBody &  body,
ShapeSource  source 
)
static

Get the size of a body: the largest dimension of the bounding box of its shapes, in the body reference frame.

Returns 0 if the body has no shapes from the specified source.

◆ Initialize()

void chrono::vsg3d::ChInstancedShapesVSG::Initialize ( ChVisualSystemVSG &  vsys,
const std::vector< std::shared_ptr< ChBody >> &  candidates 
)

Select the bodies drawn as instances, create the templates, and add the instance nodes to the visual system.

The candidates are typically all bodies in the system. The selected bodies are excluded from the default binding of the visual system, so this function must be called before the visual system binds its assets; a plugin calls it from its OnBindAssets() function, before the visual system is initialized.

◆ SetBodies()

void chrono::vsg3d::ChInstancedShapesVSG::SetBodies ( const std::vector< std::shared_ptr< ChBody >> &  bodies)

Set an explicit list of bodies drawn as instances, replacing the automatic selection.

This function must be called before Initialize().

◆ SetBodySelector()

void chrono::vsg3d::ChInstancedShapesVSG::SetBodySelector ( std::function< bool(const ChBody &body)>  selector)
inline

Set a predicate selecting the bodies drawn as instances, replacing the automatic selection.

This function must be called before Initialize().

◆ SetDefaultColor()

void chrono::vsg3d::ChInstancedShapesVSG::SetDefaultColor ( const ChColor &  color)
inline

Set the color of bodies whose visual model defines no color (default: [0.75, 0.75, 0.75]).

Without a color callback, a body has the diffuse color of the first material in its visual model, if any, and this color otherwise. This function must be called before Initialize().

◆ SetMaterial()

void chrono::vsg3d::ChInstancedShapesVSG::SetMaterial ( std::shared_ptr< ChVisualMaterial >  material)
inline

Set the material of the instanced shapes (default: ChVisualMaterial::Default()).

The body colors replace the diffuse color of this material. This function must be called before Initialize().

◆ SetResolution()

void chrono::vsg3d::ChInstancedShapesVSG::SetResolution ( int  num_divs)

Set the number of divisions of a full circle used to tessellate round templates (default: 24).

This function must be called before Initialize().

◆ SetShapeSource()

void chrono::vsg3d::ChInstancedShapesVSG::SetShapeSource ( ShapeSource  source)
inline

Set the source of the shapes drawn for each body (default: COLLISION).

This function must be called before Initialize().

◆ SetSizeFactor()

void chrono::vsg3d::ChInstancedShapesVSG::SetSizeFactor ( double  factor)
inline

Set the size factor for the automatic selection of bodies (default: 10).

Bodies whose size is at most this factor times the median body size are drawn as instances. This function must be called before Initialize().

◆ SetVisibilityCallback()

void chrono::vsg3d::ChInstancedShapesVSG::SetVisibilityCallback ( std::shared_ptr< VisibilityCallback >  callback)
inline

Set a callback for the body visibility.

A body is drawn if the callback returns true and, unless SetInactiveBodyVisibility(true) was called, the body is active.


The documentation for this class was generated from the following files:
  • /builds/uwsbel/chrono/src/chrono_vsg/utils/ChInstancedShapesVSG.h
  • /builds/uwsbel/chrono/src/chrono_vsg/utils/ChInstancedShapesVSG.cpp