raisin::SensorBatchRenderer
class · #include <rayrai/SensorBatchRenderer.hpp>
Renders the cameras and spinning LiDARs of many raisim Worlds together, for vectorized environments.
Rendering every environment with a RayraiWindow of its own repeats the per-render work (scene update, render-target switch, readback) once per environment. A SensorBatchRenderer keeps a lean copy of the render state of every World, built from physics snapshots (PhysicsSnapshotCapture), and loads each mesh file once for all of them. Cameras of one kind, resolution and lens type (pinhole or fisheye) form a group: each camera draws into its own tile of one render target (or of a few, for many large cameras), and the images of a group are read into one array with one read per column of tiles. Spinning LiDARs are cast on the GPU into tiles of one target as well.
A camera sees only the objects of its own World, drawn from their visual bodies (no visualization objects):
- depth cameras as RayraiWindow::renderDepthPlaneDistance() draws them: depth along the optical axis in metres, 0 where nothing was hit;
- segmentation cameras as RayraiWindow::renderSegmentation() draws them: object, body and label ids, -1 (SegmentationCamera::kBackground) where nothing was hit;
- RGB cameras with simplified shading that is not RayraiWindow's: the albedo of each surface (object or mesh color, vertex colors, albedo texture), lit by an ambient term and one directional light without shadows (RgbLighting), gamma encoded, on a uniform background. There are no reflections, fog, sky or post-processing; transparent surfaces are drawn opaque. Color, depth and segmentation cameras of one pose cover the same pixels, up to the rare edge pixels of tiles at different target positions (see below).
Fisheye cameras render a pinhole image of their field of view into a tile of a second target and resample it through the lens with RayraiWindow's lens model: depth and ids at the nearest source pixel, colors bilinearly (from the 8-bit gamma-encoded image, where RayraiWindow filters linear color).
The GPU interpolates depth from window coordinates, and a tile has other window coordinates than a render target of its own, so depths can differ from Camera::readLinearDepth() in their last bits (about 1e-6 relative), where two surfaces nearly coincide the nearer one can differ, and an edge can fall on the other side of a pixel centre (well under 0.01% of pixels in rayrai_sensor_batch_benchmark). A tile at the origin of its target matches RayraiWindow exactly.
Spinning LiDARs reproduce RayraiWindow::measureSpinningLidarSingleDrawGPU(): capture() advances the head (SpinningLidar::advanceSweep()) and render() casts the swept yaw columns against the World's collision bodies (World::getRayCastBodies()), ignoring the lidar's own body.
raisin::SensorBatchRenderer batch; // an OpenGL context must be current
for (size_t i = 0; i < worlds.size(); ++i) {
const size_t world = batch.addWorld(*worlds[i]);
batch.addDepthCamera(world, *depthCameras[i]);
batch.addRgbCamera(world, *rgbCameras[i]);
batch.addSpinningLidar(world, *lidars[i]); // measurement source MANUAL
}
// Every step, after integrate():
for (size_t i = 0; i < worlds.size(); ++i) batch.capture(i); // may run in parallel
batch.render();
const auto& depth = batch.depthGroups().front(); // depth.depth holds [camera][row][column]
const auto& rgb = batch.rgbGroups().front(); // rgb.bgra holds [camera][row][column][4]
const float* ranges = batch.lidarRangeImage(0); // [pitch][yaw sample]It needs desktop OpenGL 3.3 or later (or OpenGL 4.1 on macOS) and is not part of the browser build.
Threading
Construction, render() and destruction issue OpenGL calls, so they run on the thread whose context was current at construction. render() restores the framebuffer bindings, viewport and the capabilities it changes, but leaves other bindings (program, vertex array, array buffer, active texture unit and textures) changed. It expects the default front face (counterclockwise), polygon mode (fill), depth range and logic operation, with polygon offset, rasterizer discard and depth clamping off, as a context that only rayrai uses keeps them. capture() of different Worlds may run concurrently on any threads (for instance each environment's worker, right after it stepped its World), but not concurrently with render() or the setup calls. The apply*Noise() and write*ToSensor() calls of different sensors may run concurrently with each other after render() returned, but not with capture() of the sensor's World.
A World with spinning LiDARs is read by render() as well: capture() copies the poses of its collision bodies, and render() reads their shapes, so the World must not remove objects or change collision geometry between its capture() and render(). If render() throws, lidars whose scans it did not read back keep their swept columns for the next render().
Nested classes
structDepthGroup — Depth cameras of one resolution and lens type, whose images are stored together.structLidarSweep — Yaw columns a spinning LiDAR swept, as raisim::SpinningLidar::Sweep.structRgbGroup — RGB cameras of one resolution and lens type, whose images are stored together.structRgbLighting — Lighting of the RGB cameras.structSegmentationGroup — Segmentation cameras of one resolution and lens type, whose images are stored together.
Constructors & destructor
SensorBatchRenderer
SensorBatchRenderer()Requires a current OpenGL context (see RayraiWindow::createOffscreenGlContext()).
SensorBatchRenderer(const SensorBatchRenderer &) = delete~SensorBatchRenderer
~SensorBatchRenderer()Destroy it with the construction context current. Otherwise it leaks everything it holds (OpenGL objects, meshes and snapshots) with a warning, since deleting OpenGL names in another context would delete that context's objects.
Public member functions
operator=
SensorBatchRenderer &operator=(const SensorBatchRenderer &) = deleteaddWorld
size_t addWorld(raisim::World &world)Add a World whose sensors this renderer renders.
Parameters
| Parameter | Description |
|---|---|
world | World to mirror; capture() reads it, and render() too when it has lidars. |
Returns Index of the World, for the add*() calls and capture().
Exceptions
| Exception | Description |
|---|---|
std::invalid_argument | if the World was added already. |
addDepthCamera
size_t addDepthCamera(size_t world, raisim::DepthCamera &camera)Add a depth camera of a World.
The camera's properties (resolution, field of view, clip planes, lens, image window offset) are copied now; later changes to them are not seen. The camera joins the DepthGroup of its resolution and lens type, created when it is the first of them. Adding a camera resizes its group's DepthGroup::depth, so pointers into it (depthImage(), array views) must be taken again, and a camera of a new group moves every DepthGroup (references to them dangle). The same holds for the other camera kinds and their groups.
Parameters
| Parameter | Description |
|---|---|
world | Index from addWorld() of the World the camera belongs to. |
camera | Depth camera of the World; capture() reads its pose, writeDepthToSensor() and applyDepthNoise() its noise model, so it must exist while they are called. |
Returns Index of the camera among the depth cameras.
Exceptions
| Exception | Description |
|---|---|
std::invalid_argument | for an unknown World, a camera added already or one on a robot of another World, a resolution of no pixels or larger than one render target can hold (for a segmentation camera, three times its height), or clip planes no projection can render (a near plane of 0 with a far plane of 0.01 m or less). |
addRgbCamera
size_t addRgbCamera(size_t world, raisim::RGBCamera &camera)Add an RGB camera of a World (see addDepthCamera() and RgbLighting).
Returns Index of the camera among the RGB cameras.
addSegmentationCamera
size_t addSegmentationCamera(
size_t world,
raisim::SegmentationCamera &camera)Add a segmentation camera of a World (see addDepthCamera()).
Returns Index of the camera among the segmentation cameras.
addSpinningLidar
size_t addSpinningLidar(
size_t world,
raisim::SpinningLidar &lidar,
const raisim::Object *objectToExclude=nullptr)Add a spinning LiDAR of a World.
Its properties are read by capture() (to advance the head) and render() (to cast the swept columns), so they may change between steps.
Parameters
| Parameter | Description |
|---|---|
world | Index from addWorld() of the World the lidar belongs to. |
lidar | Lidar of the World. Its measurement source must be MANUAL, so that World::integrate() does not advance its head and cast it on the CPU as well. |
objectToExclude | Optional object whose bodies the scans ignore (e.g. the carrier); the lidar's own body is always ignored. It must exist while the renderer renders. |
Returns Index of the lidar.
Exceptions
| Exception | Description |
|---|---|
std::invalid_argument | for an unknown World, a lidar added already or one on a robot of another World, a RAISIM measurement source, or more yaw or pitch samples than one render target holds; render() throws std::runtime_error if later property changes exceed it. A change of the sample counts drops the columns swept since the previous render(). |
capture
void capture(size_t world)Capture the render state of a World and the poses of its sensors (no OpenGL).
Call after the World stepped and before render(). Calls updatePose() of the World's sensors and advances the heads of its lidars to the World time. A World that was not captured since the previous render() is rendered as captured then, and its lidars cast nothing; columns a lidar swept in a capture() that no render() followed are not cast.
Parameters
| Parameter | Description |
|---|---|
world | Index from addWorld(). |
render
void render()Render every camera and lidar and read the results into the groups and range images.
Uploads what changed in the captured Worlds, then draws and reads back each group and the lidar tiles. Results are noise-free; see the apply*Noise() and write*ToSensor() calls.
setRgbLighting
void setRgbLighting(const RgbLighting &lighting)Set the lighting of the RGB cameras, used from the next render().
Exceptions
| Exception | Description |
|---|---|
std::invalid_argument | for a zero or non-finite light direction or a gamma <= 0. |
rgbLighting
const RgbLighting &rgbLighting() constworldCount
size_t worldCount() constdepthCameraCount
size_t depthCameraCount() constrgbCameraCount
size_t rgbCameraCount() constsegmentationCameraCount
size_t segmentationCameraCount() constspinningLidarCount
size_t spinningLidarCount() constdepthGroups
const std::vector<DepthGroup> &depthGroups() constReturns The groups, in the order of the first camera of each resolution and lens type.
rgbGroups
const std::vector<RgbGroup> &rgbGroups() constsegmentationGroups
const std::vector<SegmentationGroup> &segmentationGroups() constdepthImage
const float *depthImage(size_t camera) constParameters
| Parameter | Description |
|---|---|
camera | Index from addDepthCamera(). |
Returns The camera's image in its group's DepthGroup::depth, of width * height depths.
rgbImage
const unsigned char *rgbImage(size_t camera) constParameters
| Parameter | Description |
|---|---|
camera | Index from addRgbCamera(). |
Returns The camera's image in its group's RgbGroup::bgra, of width * height * 4 bytes.
segmentationImages
const int32_t *segmentationImages(size_t camera) constParameters
| Parameter | Description |
|---|---|
camera | Index from addSegmentationCamera(). |
Returns The camera's object, body and label images in its group's SegmentationGroup::ids, of 3 * width * height ids.
lidarRangeImage
const float *lidarRangeImage(size_t lidar) constParameters
| Parameter | Description |
|---|---|
lidar | Index from addSpinningLidar(). |
Returns The lidar's latest turn: pitchSamples rows (pitchMinAngle first) of yawSamples ranges (yaw sample i at yaw i * 2 pi / yawSamples) in metres, +0 where nothing was hit and -0 for a hit at the sensor. render() overwrites the columns that the latest capture() swept; the others keep their ranges (initially +0). render() reallocates and clears the image when the lidar's sample counts changed.
lidarSweep
LidarSweep lidarSweep(size_t lidar) constReturns The yaw columns the latest render() cast for the lidar (count 0 for none).
applyDepthNoise
void applyDepthNoise(size_t camera)Apply the camera's noise model (DepthCamera::applyNoise()) to its image in place.
writeDepthToSensor() applies the noise model too, so call one of them per render().
Parameters
| Parameter | Description |
|---|---|
camera | Index from addDepthCamera(). |
Exceptions
| Exception | Description |
|---|---|
std::logic_error | if the camera's resolution changed after it was added (as do the other apply*Noise() and write*ToSensor() calls of cameras). |
writeDepthToSensor
void writeDepthToSensor(size_t camera)Copy the camera's image into DepthCamera::getDepthArray() and apply its noise model, as Camera::getRawImage() with SensorStorageMode::SENSOR_SETTER does.
Parameters
| Parameter | Description |
|---|---|
camera | Index from addDepthCamera(). |
applyRgbNoise
void applyRgbNoise(size_t camera)Apply the camera's noise model (RGBCamera::applyNoise()) to its image in place; writeRgbToSensor() applies it too, so call one of them per render().
Parameters
| Parameter | Description |
|---|---|
camera | Index from addRgbCamera(). |
writeRgbToSensor
void writeRgbToSensor(size_t camera)Copy the camera's image into RGBCamera::getImageBuffer() (BGRA) and apply its noise model, as Camera::getRawImage() with SensorStorageMode::SENSOR_SETTER does.
Parameters
| Parameter | Description |
|---|---|
camera | Index from addRgbCamera(). |
writeSegmentationToSensor
void writeSegmentationToSensor(size_t camera)Copy the images the camera's SegmentationType selects into the SegmentationCamera.
Parameters
| Parameter | Description |
|---|---|
camera | Index from addSegmentationCamera(). |
writeLidarToSensor
void writeLidarToSensor(size_t lidar)Store the hits of the yaw columns the latest render() cast as the lidar's scan (SpinningLidar::setScan(), in SpinningLidar::update() order) and apply its noise model, as RayraiWindow::measureSpinningLidarSingleDrawGPU() does.
Parameters
| Parameter | Description |
|---|---|
lidar | Index from addSpinningLidar(). |
debugLimitTargetExtent
void debugLimitTargetExtent(int extent)Internal (tests): render into targets of at most extent pixels a side, so groups need several tile columns and pages. Throws std::logic_error after a camera or lidar was added.