Point cloud limitations in Unreal Engine
| Required Info | |
|---|---|
| Camera Model | D455 |
| Firmware Version | 05.12.12.100 |
| Operating System & Version | Windows 10 x64 |
| Kernel Version (Linux Only) | |
| Platform | PC, Unreal Engine 4.26 |
| SDK Version | 2.43.0.3013 |
| Language | C++ |
| Segment | ? |
Issue Description
Hello, I am just getting started using a RealSense camera with the Unreal Engine wrapper. I understand the point cloud feature is experimental, and uses a third-party library. The problem I'm running into is that the world normals of the voxels making up the point cloud are all pointing in the same direction.
Is this expected behavior? Is it a limitation of the third party library or the way it was implemented in the UE wrapper?
I would like to turn the point cloud into a contiguous mesh with appropriate world normals, but I don't exactly know where to begin. Can anyone offer some insight on how to approach this problem?
Thanks!
Hi @jessetemp As there are no librealsense references available regarding this question, I researched about wrong-direction normals in PCL, as the UE wrapper instructions define the point cloud as being PCL-based.
The link below makes some suggestions about how the direction of normals might be corrected in a PCL point cloud, though I do not have information on how this knowledge might be adapted to the UE wrapper's point cloud implementation.
https://stackoverflow.com/questions/47428220/pcl-estimates-wrong-normal-direction-in-some-parts
There was also a non-RealSense PCL case where the normals of a point cloud were all pointing upwards in parts, and the user in that case thought that it might be related to the point cloud detail being sparse in those points (so PCL did not know how to process it correctly).
Thanks for the info @MartyG-RealSense. I'm a little embarrassed to admit I didn't realize PCL was its own library. Just thought it was an odd abbreviation for point cloud, and that it was all handled in the runtime mesh. This at least gives me another avenue to dig into.
Hi @jessetemp Do you require further assistance with this case, please? Thanks!
Hi @MartyG-RealSense, I don't require further assistance. The UE wrapper has actually been pretty easy to work with. I made some progress with the world normals, but still have some work to do to get the mesh aligned with the normals. I was going to post the changes here when I'm done in case anyone else finds them useful
Good to hear that your project has gone smoothly! Please do post the results here when you have them. I will close the case in the meantime as you do not currently need assistance. The case will remain visible in internet searches for others to read. Good luck!
Here are the changes I made to fix the world normals and make the point cloud a contiguous mesh. Instead of creating a square centered on each point in the cloud, I create a 4-sided polygon from four valid points in the cloud. There are some checks in place to make sure they are in frame and near each other. The normals are sort of hacked, I just take the cross product from the vectors along two edges of each polygon and use that for all four corners.
I also made the following two changes. The point cloud will now cull any points greater than DepthMax. And PclDensity will now reduce the number of points vertically as well as horizontally (previously it only worked horizontally).
There's still some cleanup to do around the edges and gaps to fill, but it's good enough for what I'm doing for now.
@MartyG-RealSense, I've never contributed to a public repo, but I wouldn't mind forking librealsense and submitting a pull request if this seems like a worthwhile contribution.
All of the changes take place in ARealSenseInspector::UpdatePointCloud(). For copy/paste simplicity, I've copied the entire function below, although much of it is unchanged.
void ARealSenseInspector::UpdatePointCloud()
{
SCOPED_PROFILER;
const size_t NumPoints = RsPoints->size();
const size_t DensityPoints = (size_t)FMath::RoundToInt(NumPoints * FMath::Clamp(PclDensity, 0.0f, 1.0f));
if (!DensityPoints) { PclMesh->SetVisibility(false); return; }
const size_t Step = (size_t)FMath::RoundToInt(NumPoints / (float)DensityPoints);
if (!Step) { PclMesh->SetVisibility(false); return; }
const size_t RenderPoints = (NumPoints / Step);
const size_t RenderVertices = RenderPoints * 4;
const size_t RenderIndices = RenderPoints * 6;
const size_t MaxBufferIndices = (MAX_BUFFER_U16 / 6) * 6;
const size_t NumSections = (RenderIndices / MaxBufferIndices) + (RenderIndices % MaxBufferIndices ? 1 : 0);
#if 1
REALSENSE_TRACE(TEXT(
"PCL Id=%zu NumPoints=%zu "
"RenderPoints=%zu RenderVertices=%zu RenderIndices=%zu NumSections=%zu "
"Density=%.3f Step=%zu"),
PclFramesetId, NumPoints,
RenderPoints, RenderVertices, RenderIndices, NumSections,
PclDensity, Step
);
#endif
const rs2::vertex* SrcVertices = RsPoints->get_vertices();
const rs2::texture_coordinate* SrcTexcoords = RsPoints->get_texture_coordinates();
FVector Position[4];
FPointCloudVertex PointCloudVertex[4];
const float Size = (PclVoxelSize * 0.5f);
size_t SectionId = 0;
size_t PointId = 0;
size_t VertexId = 0;
size_t IndexId = 0;
size_t NumInvalid = 0;
int VerticalCount = 0;
while (PointId < NumPoints && SectionId < NumSections)
{
// PointId runs horizontally then vertically along NumPoints
if (!PclMeshData.Contains(SectionId))
{
NAMED_PROFILER("AllocMeshSection");
PclMeshData.Add(SectionId, MakeUnique<FMeshSection>());
PclMeshData[SectionId]->PclVertices.SetNumUninitialized(MAX_BUFFER_U16, false);
PclMeshData[SectionId]->PclIndices.SetNumUninitialized(MAX_BUFFER_U16, false);
}
if (!PclMesh->DoesSectionExist(SectionId))
{
NAMED_PROFILER("CreateMeshSection");
PclMesh->CreateMeshSection(SectionId, PclMeshData[SectionId]->PclVertices, PclMeshData[SectionId]->PclIndices, false, EUpdateFrequency::Frequent);
PclMesh->SetMaterial(SectionId, PclMaterial);
}
FPointCloudVertex* DstVertices = &(PclMeshData[SectionId]->PclVertices[0]);
int32* DstIndices = &(PclMeshData[SectionId]->PclIndices[0]);
VertexId = 0;
IndexId = 0;
{NAMED_PROFILER("FillMeshBuffers");
while (PointId < NumPoints && IndexId + 6 <= MaxBufferIndices)
{
int ColumnIndex = PointId % DepthConfig.Width;
// skip rows depending on density
if (PointId > 0 && ColumnIndex == 0)
{
PointId += DepthConfig.Width * (Step - 1);
}
rs2::vertex Vertex0;
rs2::vertex Vertex1 = SrcVertices[PointId];
rs2::vertex Vertex2;
rs2::vertex Vertex3;
if (PointId >= DepthConfig.Width)
{
Vertex0 = SrcVertices[PointId - DepthConfig.Width * Step];
}
if (ColumnIndex >= Step)
{
Vertex2 = SrcVertices[PointId - Step];
}
if (PointId >= DepthConfig.Width && ColumnIndex >= Step)
{
Vertex3 = SrcVertices[PointId - DepthConfig.Width * Step - Step];
}
bool IsValid = (Vertex0.z && Vertex1.z && Vertex2.z && Vertex3.z);
bool IsBackground = (Vertex0.z > DepthMax || Vertex1.z > DepthMax || Vertex2.z > DepthMax || Vertex3.z > DepthMax);
float ContiguousLimit = 0.03 * PclScale;
bool IsContiguous = (
FMath::Abs(Vertex0.z - Vertex1.z) < ContiguousLimit &&
FMath::Abs(Vertex1.z - Vertex2.z) < ContiguousLimit &&
FMath::Abs(Vertex2.z - Vertex3.z) < ContiguousLimit &&
FMath::Abs(Vertex3.z - Vertex0.z) < ContiguousLimit
); // Prevents stretching
if (!IsValid || IsBackground || !IsContiguous)
{
PointId += Step;
NumInvalid++;
continue;
}
// the positive x-axis points to the right
// the positive y-axis points down
// the positive z-axis points forward
Position[0] = FVector(Vertex0.z, Vertex0.x, -Vertex0.y) * 100.0f * PclScale; // meters to mm
Position[1] = FVector(Vertex1.z, Vertex1.x, -Vertex1.y) * 100.0f * PclScale; // meters to mm
Position[2] = FVector(Vertex2.z, Vertex2.x, -Vertex2.y) * 100.0f * PclScale; // meters to mm
Position[3] = FVector(Vertex3.z, Vertex3.x, -Vertex3.y) * 100.0f * PclScale; // meters to mm
PointCloudVertex[0].Position = FVector(Position[0].X, Position[0].Y, Position[0].Z);
PointCloudVertex[1].Position = FVector(Position[1].X, Position[1].Y, Position[1].Z);
PointCloudVertex[2].Position = FVector(Position[2].X, Position[2].Y, Position[2].Z);
PointCloudVertex[3].Position = FVector(Position[3].X, Position[3].Y, Position[3].Z);
FVector Normal = FVector::CrossProduct(
PointCloudVertex[0].Position - PointCloudVertex[1].Position,
PointCloudVertex[0].Position - PointCloudVertex[3].Position
);
PointCloudVertex[0].Normal = Normal;
PointCloudVertex[1].Normal = Normal;
PointCloudVertex[2].Normal = Normal;
PointCloudVertex[3].Normal = Normal;
PointCloudVertex[0].Tangent = FVector(0, 0, 1);
PointCloudVertex[1].Tangent = FVector(0, 0, 1);
PointCloudVertex[2].Tangent = FVector(0, 0, 1);
PointCloudVertex[3].Tangent = FVector(0, 0, 1);
const auto T = SrcTexcoords[PointId];
PointCloudVertex[0].UV0 = FVector2D(T.u, T.v);
PointCloudVertex[1].UV0 = FVector2D(T.u, T.v);
PointCloudVertex[2].UV0 = FVector2D(T.u, T.v);
PointCloudVertex[3].UV0 = FVector2D(T.u, T.v);
DstVertices[0] = PointCloudVertex[0];
DstVertices[1] = PointCloudVertex[1];
DstVertices[2] = PointCloudVertex[2];
DstVertices[3] = PointCloudVertex[3];
DstIndices[0] = VertexId + 0;
DstIndices[1] = VertexId + 2;
DstIndices[2] = VertexId + 1;
DstIndices[3] = VertexId + 0;
DstIndices[4] = VertexId + 3;
DstIndices[5] = VertexId + 2;
DstVertices += 4;
DstIndices += 6;
VertexId += 4;
IndexId += 6;
PointId += Step;
}}
if (IndexId > 0)
{
#if 0
REALSENSE_TRACE(TEXT("section id=%zu points=%zu vert=%zu ind=%zu total=%zu invalid=%zu"),
SectionId, VertexId / 4, VertexId, IndexId, PointId, NumInvalid);
#endif
NAMED_PROFILER("UpdateMeshSection");
PclMeshData[SectionId]->PclVertices.SetNumUninitialized(VertexId, false);
PclMeshData[SectionId]->PclIndices.SetNumUninitialized(IndexId, false);
PclMesh->UpdateMeshSection(SectionId, PclMeshData[SectionId]->PclVertices, PclMeshData[SectionId]->PclIndices); // TODO: SLOW AS HELL
PclMesh->SetMeshSectionVisible(SectionId, true);
SectionId++;
}
}
const auto NumMeshSections = PclMesh->GetNumSections();
if (NumMeshSections > SectionId)
{
for (size_t i = SectionId; i < NumMeshSections; ++i)
{
//REALSENSE_TRACE(TEXT("hide id=%zu"), i);
PclMesh->SetMeshSectionVisible(i, false);
}
}
//REALSENSE_TRACE(TEXT("total=%zu invalid=%zu"), PointId, NumInvalid);
PclMesh->SetVisibility(SectionId != 0);
}
Thanks so much @jessetemp for sharing your solution with the RealSense community!
A pull-request contribution is certainly very welcome if you are able to make one.
@jessetemp What settings were you running your code under? I couldn't get it to work on my end with everything having a resolution of (Width=848,Height=480,Rate=30). I'm getting a nullptr exception/out of bounds exception when getting vertex0 and vertex3.
Hi @MatthewLKomar, I set the depth and color to 1280x720 @ 30 fps and disabled infrared. It may work with other resolutions but as you've seen, some will break. I ended up reworking UpdatePointCloud a few times since posting the code here. The main problem in it's current state (aside from various bugs) is that the voxels aren't sharing vertices. They're all discrete which prevents Unreal from smoothing the edges. I haven't looked at it in a few months, but it is on my list of projects to revisit.