AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
LidarOutputRenderer.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
20
21#if AGX_USE_AGXSENSOR()
22 #include <agxOSG/export.h>
23
24 #include <agx/Quat.h>
25 #include <agx/Vec4.h>
26
27 #include <osg/Geode>
28 #include <osg/Geometry>
29 #include <osg/MatrixTransform>
30 #include <osg/PrimitiveSet>
31
32 #include <functional>
33
34#endif // AGX_USE_AGXSENSOR()
35
36#if AGX_USE_AGXSENSOR()
37
38namespace agxSensor
39{
40 class Lidar;
41}
42
43namespace agxOSG
44{
49 {
50 float intensity;
51 osg::Vec4 color;
52
54 LidarOutputRendererLegendPoint(float intensity, osg::Vec4 color);
55 };
56
61 class AGXOSG_EXPORT LidarOutputRenderer : public osg::MatrixTransform
62 {
63 public:
64 using GeodeRef = osg::ref_ptr<osg::Geode>;
65 using GeometryRef = osg::ref_ptr<osg::Geometry>;
66 using FloatArrayRef = osg::ref_ptr<osg::FloatArray>;
67 using Vec3ArrayRef = osg::ref_ptr<osg::Vec3Array>;
68 using Vec4ArrayRef = osg::ref_ptr<osg::Vec4Array>;
69 using ColorArrayRef = osg::ref_ptr<osg::Vec4Array>;
70 using ProgramRef = osg::ref_ptr<osg::Program>;
71 using UniformRef = osg::ref_ptr<osg::Uniform>;
72
74 {
75 using ColorFunc = std::function<osg::Vec4(float intensity)>;
76
77 enum Mode
78 {
81 };
82
83 float scale = 120.0f;
84 ColorFunc colorFunc = {};
85 struct
86 {
87 std::vector<LidarOutputRendererLegendPoint> colorLegend;
88 float exponent;
89 } interpolationFunctionValues;
90 };
91
92 class LidarObserverHolder;
93
94 public:
100 LidarOutputRenderer(const agxSensor::Lidar* lidar, bool renderRays = false);
101
105 void setHitPointScale(float scale);
106
110 float getHitPointScale() const;
111
117 void setIntensityScale(float intensityScale);
118
122 float getIntensityScale() const;
123
130
139 void setIntensityColorFunctionFromInterpolation(osg::Vec4 low, osg::Vec4 high, float exponent);
140
148 const std::vector<LidarOutputRendererLegendPoint>& legend, float exponent);
149
156
157 #ifndef SWIG
162
163 #endif
164
169 void setHitPointLifetime(float hitPointLifetime);
170
174 float getHitPointLifetime() const;
175
180 void setHitPointsAlwaysOnTop(bool alwaysOnTop);
181
186
188
189 public:
191 virtual ~LidarOutputRenderer() noexcept;
192
193 LidarOutputRenderer& operator=(const LidarOutputRenderer& object);
194
196
197 private:
198 struct HitOutput : agx::Vec4f
199 {
200 using agx::Vec4f::Vec4f;
201
202 agx::Vec3f position() const { return {x(), y(), z()}; }
203
204 osg::Vec3 osgPosition() const { return {x(), y(), z()}; }
205
206 osg::Vec4 osgZToNormal(agx::Quat32 toWorld) const
207 {
208 agx::Quat32 q = agx::Quat32 {agx::Vec3f::Z_AXIS(), normal} * toWorld.conj();
209 return {q.x(), q.y(), q.z(), q.w()};
210 }
211
212 float intensity() const { return w(); }
213
214 agx::Vec3f normal;
215 };
216
217 struct FrameCallback : osg::NodeCallback
218 {
219 virtual void operator()(osg::Node* node, osg::NodeVisitor* /*visitor*/) override
220 {
221 dynamic_cast<LidarOutputRenderer*>(node)->onPreRender();
222 }
223 };
224
225 struct CloudRenderData
226 {
227 CloudRenderData(size_t maxNumPoints);
228 GeometryRef createGeometry(osg::PrimitiveSet::Mode renderMode);
229 osg::StateSet* getCommonStateSet();
230
231 void age(float dt);
232 void push(
233 size_t newCount, const HitOutput* newPoints, float lifetime, const agx::Quat32& rotationToWorldF,
234 const IntensityState& intensityState);
235 void updatePrimitiveCount(size_t primitiveCount);
236 void invalidate();
237
238 GeodeRef geode;
239 size_t count;
240 FloatArrayRef lifetimes;
241 Vec3ArrayRef vertices;
242 ColorArrayRef colors;
243 Vec4ArrayRef zToNormals;
244 };
245
246 struct GeometryShaderData
247 {
248 GeometryRef rayGeometry;
249 ProgramRef rayProgram;
250 UniformRef rayColor;
251
252 GeometryRef pointsGeometry;
253 ProgramRef pointsProgram;
254 UniformRef hitPointScale;
255 };
256
257 private:
258 static size_t determinePointBufferCount(const agxSensor::Lidar* lidar, float hitPointLifetime);
259
260 private:
261 friend FrameCallback;
262
263 void synchronizeTransform();
264 ProgramRef createShader(const agx::String& filename);
265
266 void initializeOutputCloudGeometryShader(bool renderRays);
267 void onPreRender();
268
269 private:
270 LidarObserverHolder* m_lidarObserver; // This is fully managed by the LidarOutputRenderer
271 CloudRenderData m_cloudRenderData;
272 GeometryShaderData m_geometryShaderData;
273 IntensityState m_intensityState;
274 agx::Real m_lastRenderTime;
275 float m_hitPointLifetime;
276 };
277
278 using LidarOutputRendererRef = osg::ref_ptr<LidarOutputRenderer>;
279}
280
281#endif // AGX_USE_AGXSENSOR()
#define AGXOSG_EXPORT
Lidar point cloud renderer.
osg::ref_ptr< osg::Vec4Array > Vec4ArrayRef
void setIntensityColorFunction(IntensityState::ColorFunc colorFunc)
Set custom color function of the hits.
void setIntensityScale(float intensityScale)
Set scale of the output intensity of the lidar.
void setHitPointLifetime(float hitPointLifetime)
Set how long the lidar points should remain rendered for.
osg::ref_ptr< osg::Uniform > UniformRef
void setIntensityColorFunctionFromLegend(const std::vector< LidarOutputRendererLegendPoint > &legend, float exponent)
Set lidar point color intensity function based on the given color interpolation legend.
float getIntensityScale() const
void setIntensityColorFunctionFromInterpolation(osg::Vec4 low, osg::Vec4 high, float exponent)
Set lidar point color intensity function based on an interpolation of the intensity,...
float getHitPointScale() const
osg::ref_ptr< osg::Vec4Array > ColorArrayRef
LidarOutputRenderer(const agxSensor::Lidar *lidar, bool renderRays=false)
Construct given lidar and option to render the lidar rays (default: false).
osg::ref_ptr< osg::FloatArray > FloatArrayRef
void setHitPointScale(float scale)
Set scale of the circle representing a hit.
osg::ref_ptr< osg::Vec3Array > Vec3ArrayRef
void setIntensityColorFunctionFromMode(IntensityState::Mode mode)
Set lidar point color intensity function from the given mode of how the intensity of a hit is visuali...
osg::ref_ptr< osg::Program > ProgramRef
float getHitPointLifetime() const
osg::ref_ptr< osg::Geode > GeodeRef
void setHitPointsAlwaysOnTop(bool alwaysOnTop)
Set if the lidar hit points should always be drawn over all other geometry (disabling depth testing).
bool isAlwaysRenderedOnTop() const
IntensityState::ColorFunc getIntensityColorFunction() const
osg::ref_ptr< osg::Geometry > GeometryRef
Lidar instance class defined by a frame/transform and a model.
Definition: Lidar.h:40
The object holding quaternions and providing operations on these.
Definition: QuatTemplate.h:57
QuatT< T > conj() const
Definition: QuatTemplate.h:730
static Vec3T Z_AXIS()
Definition: Vec3Template.h:783
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:89
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
agx::ref_ptr< Geometry > GeometryRef
Definition: Geometry.h:77
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
osg::ref_ptr< LidarOutputRenderer > LidarOutputRendererRef
The agxSensor namespace contains components to model real-time sensors connected to the physics of AG...
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
double Real
Definition: Real.h:41
Reference color point for lidar hit points when using a color legend.
LidarOutputRendererLegendPoint(float intensity, osg::Vec4 color)
@ COLOR_A
Color alpha - hit points with intensity as transparency.
@ COLOR_RG
Color red (low intensity) -> green (high intensity).
std::vector< LidarOutputRendererLegendPoint > colorLegend
std::function< osg::Vec4(float intensity)> ColorFunc