Skip to content

Commit

Permalink
Optimize PointCloudVisualization by reducing usage of GetComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
msz-rai committed Sep 11, 2023
1 parent 41872f9 commit 63bd494
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
14 changes: 11 additions & 3 deletions Assets/RGLUnityPlugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ The native RGL library needs a once-per-scene preparation to access models on th

1. Create an empty object
2. Attach script `LidarSensor.cs`.
3. `PointCloudVisualization.cs` will be added automatically, however, you can disable it.
4. Now you can add a callback from another script to receive a notification when data is ready:
3. (Optional) Attach script `PointCloudVisualization.cs` for visualization purposes.
4. To obtain point cloud data from another script you have to create a new `RGLNodeSequence` and connect it to `LidarSensor`:
```cs
rglOutSubgraph = new RGLNodeSequence().AddNodePointsYield("OUT_XYZ", RGLField.XYZ_F32);
lidarSensor = GetComponent<LidarSensor>();
lidarSensor.OnOutputData += HandleLidarDataMethod;
lidarSensor.ConnectToWorldFrame(rglOutSubgraph); // you can also connect to Lidar frame using ConnectToLidarFrame
// You can add a callback to receive a notification when new data is ready
lidarSensor.onNewData += HandleLidarDataMethod;
```
5. To get data from `RGLNodeSequence` call `GetResultData`:
```cs
Vector3[] xyz = new Vector3[0];
rglOutSubgraph.GetResultData<Vector3>(ref xyz);
```

## Adding new lidar models
Expand Down
16 changes: 0 additions & 16 deletions Assets/RGLUnityPlugin/Scripts/LidarSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace RGLUnityPlugin
/// <summary>
/// Encapsulates all non-ROS components of a RGL-based Lidar.
/// </summary>
[RequireComponent(typeof(PointCloudVisualization))]
public class LidarSensor : MonoBehaviour
{
/// <summary>
Expand All @@ -37,7 +36,6 @@ public class LidarSensor : MonoBehaviour
/// <summary>
/// Delegate used in callbacks.
/// </summary>
/// <param name="outputData">Data output for each hz</param>
public delegate void OnNewDataDelegate();

/// <summary>
Expand Down Expand Up @@ -74,7 +72,6 @@ public class LidarSensor : MonoBehaviour
private RGLNodeSequence rglGraphLidar;
private RGLNodeSequence rglSubgraphCompact;
private RGLNodeSequence rglSubgraphToLidarFrame;
private RGLNodeSequence rglSubgraphVisualizationOutput;
private SceneManager sceneManager;

private readonly string lidarRaysNodeId = "LIDAR_RAYS";
Expand All @@ -86,7 +83,6 @@ public class LidarSensor : MonoBehaviour
private readonly string noiseDistanceNodeId = "NOISE_DISTANCE";
private readonly string pointsCompactNodeId = "POINTS_COMPACT";
private readonly string toLidarFrameNodeId = "TO_LIDAR_FRAME";
private readonly string visualizationOutputNodeId = "OUT_VISUALIZATION";

private LidarModel? validatedPreset;
private float timer;
Expand All @@ -108,12 +104,8 @@ public void Awake()
rglSubgraphToLidarFrame = new RGLNodeSequence()
.AddNodePointsTransform(toLidarFrameNodeId, Matrix4x4.identity);

rglSubgraphVisualizationOutput = new RGLNodeSequence()
.AddNodePointsYield(visualizationOutputNodeId, RGLField.XYZ_F32);

RGLNodeSequence.Connect(rglGraphLidar, rglSubgraphCompact);
RGLNodeSequence.Connect(rglSubgraphCompact, rglSubgraphToLidarFrame);
RGLNodeSequence.Connect(rglSubgraphCompact, rglSubgraphVisualizationOutput);
}

public void Start()
Expand Down Expand Up @@ -226,14 +218,6 @@ public void Capture()
rglSubgraphToLidarFrame.UpdateNodePointsTransform(toLidarFrameNodeId, lidarPose.inverse);

rglGraphLidar.Run();

// Could be moved to PointCloudVisualization
if (GetComponent<PointCloudVisualization>().isActiveAndEnabled)
{
Vector3[] onlyHits = new Vector3[0];
rglSubgraphVisualizationOutput.GetResultData<Vector3>(ref onlyHits);
GetComponent<PointCloudVisualization>().SetPoints(onlyHits);
}
}
}
}
34 changes: 34 additions & 0 deletions Assets/RGLUnityPlugin/Scripts/PointCloudVisualization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace RGLUnityPlugin
{
[System.Serializable]
[RequireComponent(typeof(LidarSensor))]
public class PointCloudVisualization : MonoBehaviour
{
public enum PointShape
Expand Down Expand Up @@ -63,8 +64,22 @@ public enum PointShape

private Mesh mesh;

private LidarSensor lidarSensor;
private RGLNodeSequence rglSubgraphVisualizationOutput;
private readonly string visualizationOutputNodeId = "OUT_VISUALIZATION";

public void Awake()
{
rglSubgraphVisualizationOutput = new RGLNodeSequence()
.AddNodePointsYield(visualizationOutputNodeId, RGLField.XYZ_F32);

lidarSensor = GetComponent<LidarSensor>();
}

public void Start()
{
lidarSensor.ConnectToWorldFrame(rglSubgraphVisualizationOutput);

mesh = new Mesh();
if (!material)
{
Expand All @@ -79,6 +94,18 @@ public void Start()
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
}

public void OnEnable()
{
rglSubgraphVisualizationOutput.SetActive(visualizationOutputNodeId, true);
lidarSensor.onNewData += OnNewLidarData;
}

public void OnDisable()
{
rglSubgraphVisualizationOutput.SetActive(visualizationOutputNodeId, false);
lidarSensor.onNewData -= OnNewLidarData;
}

public void OnValidate()
{
if (!material)
Expand Down Expand Up @@ -126,5 +153,12 @@ public void Update()
{
Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, visualizationLayerID);
}

private void OnNewLidarData()
{
Vector3[] onlyHits = new Vector3[0];
rglSubgraphVisualizationOutput.GetResultData<Vector3>(ref onlyHits);
SetPoints(onlyHits);
}
}
}
2 changes: 1 addition & 1 deletion docs/Components/Sensors/LiDARSensor/AddNewLiDAR/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To add a new *LiDAR* model, perform the following steps:
1. Create an empty object and name it appropriately according to the *LiDAR* model.
1. Attach script `LidarSensor.cs` to created object.
1. Set the new added *LiDAR* model in `Model Preset` field, check if the configuration loads correctly. You can now customize it however you like.
1. `PointCloudVisualization.cs` will be added automatically, however, you can disable it.
1. (Optional) Attach script `PointCloudVisualization.cs` for visualization purposes.
1. For publishing point cloud via *ROS2* attach script `RglLidarPublisher.cs` script to created object.
1. Set the topics on which you want the data to be published and their frame.
2. Save the prefab in the project.
Expand Down

0 comments on commit 63bd494

Please sign in to comment.