Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
[submodule "extlibs/liteviz-gs"]
path = extlibs/liteviz-gs
url = https://github.com/panxkun/liteviz-gs
[submodule "extlibs/tinyply"]
path = extlibs/tinyply
url = https://github.com/ddiakopoulos/tinyply
[submodule "extlibs/tbb"]
path = extlibs/tbb
url = https://github.com/uxlfoundation/oneTBB
34 changes: 27 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ set(HEADER_FILES
${SOFA_POINTCLOUD_SRC}/components/PointCloudVisualModel.h
${SOFA_POINTCLOUD_SRC}/components/PointCloudInspector.h
${SOFA_POINTCLOUD_SRC}/components/liteviz-dataloader.h
extlibs/liteviz-gs/liteviz/dataloader.h
extlibs/liteviz-gs/liteviz/renderer.h
extlibs/liteviz-gs/liteviz/shader.h
extlibs/liteviz-gs/liteviz/utils.h
# extlibs/liteviz-gs/liteviz/dataloader.h
# extlibs/liteviz-gs/liteviz/renderer.h
# extlibs/liteviz-gs/liteviz/shader.h
# extlibs/liteviz-gs/liteviz/utils.h
extlibs/tinyply/source/tinyply.h
extlibs/diff-gaussian-rasterization/cuda_rasterizer/rasterizer.h
extlibs/diff-gaussian-rasterization/cuda_rasterizer/rasterizer_impl.h
extlibs/diff-gaussian-rasterization/cuda_rasterizer/forward.h
Expand All @@ -44,15 +45,34 @@ set(SOURCE_FILES
${SOFA_POINTCLOUD_SRC}/components/PointCloudRendererCUDA.cpp
)

# Tinyply and liteviz
include_directories("extlibs/tinyply/source")

# TBB
set(TBB_TEST OFF CACHE BOOL "" FORCE)
set(TBB_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE)
set(TBB_ENABLE_IPO OFF CACHE BOOL "" FORCE)
add_subdirectory("extlibs/tbb")
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Core Sofa.Component.Visual Sofa.GL SofaPython3::Plugin tbb)
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_ARCHITECTURES "native")
if(TARGET TBB::tbb)
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:TBB::tbb>
$<TARGET_FILE_DIR:${PROJECT_NAME}>
)
endif()

target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Core Sofa.Component.Visual Sofa.GL SofaPython3::Plugin TBB::tbb)
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_ARCHITECTURES 89)

sofa_create_package_with_targets(
PACKAGE_NAME ${PROJECT_NAME}
PACKAGE_VERSION ${Sofa_VERSION}
TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES
INCLUDE_SOURCE_DIR "src" "extlibs"
INCLUDE_SOURCE_DIR "src"
INCLUDE_INSTALL_DIR "${PROJECT_NAME}"
RELOCATABLE "plugins"
)
2 changes: 1 addition & 1 deletion src/sofa/pointcloud/components/PointCloudContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace sofa::core::objectmodel

/// Specialization for reading strings
template<>
bool Data<Eigen::MatrixXf>::read( const std::string& str ){}
bool Data<Eigen::MatrixXf>::read(const std::string& str) { return false; }

/// Specialization for reading strings
template<>
Expand Down
62 changes: 30 additions & 32 deletions src/sofa/pointcloud/components/PointCloudRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void PointCloudRenderer::doInitVisual(const sofa::core::visual::VisualParams* vp
{
if (!sofa::gl::GLSLShader::InitGLSL())
{
msg_info() << "InitGLSL failed" ;
msg_warning() << "InitGLSL failed" ;
return;
}

Expand Down Expand Up @@ -197,7 +197,8 @@ void append(GaussianData& dest, const GaussianData& src)

std::vector<int> range(int maxSize)
{
std::vector<int> tmp {maxSize};
std::vector<int> tmp;
tmp.reserve(maxSize);
for(auto i=0;i<maxSize;++i) tmp.emplace_back(i);
return tmp;
}
Expand Down Expand Up @@ -350,6 +351,7 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
{
isFirstRun = false;
clear(renderingData);
clear(referenceData);
for(auto visual : visualModels)
{
visual->updateVisual(vparams);
Expand All @@ -361,26 +363,18 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
if(!visual->l_geometry->data){
continue;
}
int offset = renderingData.xyz.rows();

//if(isFirstRun)
//{
// visual->initTransform();
//}

//if(visual->d_isStaticModel.getValue())
// hasStaticData=true;
int modelBeginIndex = renderingData.xyz.rows();

// First build the reference geometry
int beginIndex = renderingData.size();
int size = visual->l_geometry->data->xyz.rows()*(3+4+3+1+visual->l_geometry->data->sh_dim());
dataCache[visual] = std::make_tuple(beginIndex, size);
dataCache[visual] = std::make_tuple(modelBeginIndex, size);

// only copy position, rotation and scale values
append(referenceData.xyz, visual->l_geometry->data->xyz);
append(referenceData.rot, visual->l_geometry->data->rot);
append(referenceData.scale, visual->l_geometry->data->scale);

append(renderingData.xyz, visual->l_geometry->data->xyz);
append(renderingData.sh, visual->l_geometry->data->sh);
append(renderingData.opacity, visual->l_geometry->data->opacity);
append(renderingData.scale, visual->l_geometry->data->scale);
append(renderingData.rot, visual->l_geometry->data->rot);
append(renderingData, *visual->l_geometry->data);

auto scale = visual->d_uniformScale.getValue();
auto initFrames = visual->initFrames;
Expand All @@ -390,17 +384,15 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
std::vector<std::vector<int>> frameMap{frames.size()};
for(size_t i=0;i<frameIndices.size();++i)
{
frameMap[frameIndices[i]].push_back(offset+i);
frameMap[frameIndices[i]].push_back(modelBeginIndex+i);
}

//visual->doUpdateVisual(sofa::core::visual::VisualParams::defaultInstance());

// Now we need to apply the transformation
this->transform(scale,
initFrames,
frames, frameMap, renderingData.xyz, renderingData.rot, renderingData.scale);
msg_warning() << "Batching a new data set " << visual->getPathName() << " with frames " << frames.size();
msg_warning() << " data set offset & size " << beginIndex << ", " << size;
msg_info() << "Batching a new data set " << visual->getPathName() << " with frames " << frames.size();
msg_info() << " data set modelBeginIndex & modelSize " << modelBeginIndex << ", " << size;
}
}else{
SCOPED_TIMER("PointCloud::doDrawVisual::sceneParsing");
Expand All @@ -417,7 +409,8 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
if(visual->d_isStaticModel.getValue())
continue;

auto [offset, size] = dataCache[visual];
auto [modelBeginIndex, modelSize] = dataCache[visual];
int modelSplatsCount = visual->l_geometry->data->xyz.rows();

auto scale = visual->d_uniformScale.getValue();
auto initFrames = visual->initFrames;
Expand All @@ -427,9 +420,14 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
std::vector<std::vector<int>> frameMap{frames.size()};
for(size_t i=0;i<frameIndices.size();++i)
{
frameMap[frameIndices[i]].push_back(offset+i);
frameMap[frameIndices[i]].push_back(modelBeginIndex+i);
}

// Reset to untransformed reference before applying new pose
renderingData.xyz.block(modelBeginIndex, 0, modelSplatsCount, 3) = referenceData.xyz.block(modelBeginIndex, 0, modelSplatsCount, 3);
renderingData.rot.block(modelBeginIndex, 0, modelSplatsCount, 4) = referenceData.rot.block(modelBeginIndex, 0, modelSplatsCount, 4);
renderingData.scale.block(modelBeginIndex, 0, modelSplatsCount, 3) = referenceData.scale.block(modelBeginIndex, 0, modelSplatsCount, 3);

{
SCOPED_TIMER("PointCloud::doDrawVisual::sceneTransform");
// Now we need to apply the transformation
Expand All @@ -438,11 +436,12 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
frames, frameMap, renderingData.xyz, renderingData.rot, renderingData.scale);

}
msg_warning() << "Updating a new data set " << visual->getPathName() << " with frames " << frames.size();
msg_warning() << " data set offset & size " << offset << ", " << size;
updatesBufferParts.push_back({offset,size});
msg_info() << "Updating a new data set " << visual->getPathName() << " with frames " << frames.size();
msg_info() << " data set modelBeginIndex & modelSize " << modelBeginIndex << ", " << modelSize;
updatesBufferParts.push_back({modelBeginIndex,modelSize});
}
}

msg_info() << " total number of splats to render: " << renderingData.size() << " splats ";

isFirstRun=false;
Expand All @@ -455,7 +454,7 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
// Here we have geometries to draw and a camera that look at it.
// We first send the camera parameters to the gl rendering backend, then the geometries.
auto c = l_camera->getPosition();
Eigen::Vector3f cam_pos {c[0],c[1],c[2]};
Eigen::Vector3f cam_pos {float(c[0]),float(c[1]), float(c[2])};

vparams->getModelViewMatrix(dviewmat.data());
vparams->getProjectionMatrix(dprojmat.data());
Expand Down Expand Up @@ -503,7 +502,6 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
static bool firstTime = true;
if(firstTime)
{
std::cout << "INTIIALIZE BUFFER " << std::endl;
firstTime = false;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, _ssbo_splat[SplatProperty::SCALE]);
glBufferData(GL_SHADER_STORAGE_BUFFER, renderingData.scale.rows() * sizeof(float) * 3, renderingData.scale.data(), GL_DYNAMIC_DRAW);
Expand Down Expand Up @@ -531,7 +529,6 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, _ssbo_splat[SplatProperty::ROTATION]);
}

std::cout << "UPDATE BUFFER PARTS " << updatesBufferParts.size() << std::endl;
if(updatesBufferParts.size())
{
SCOPED_TIMER("PointCloud::doDrawVisual::bufferUpdate");
Expand All @@ -546,7 +543,6 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp
}

if(depths.size()!=indices.size()){
std::cout << "RESIZE DEPTH BUFFER" << std::endl;
depths.resize(indices.size());
glBindBuffer(GL_SHADER_STORAGE_BUFFER, _ssbo_splat[SplatProperty::DEPTHS]);
glBufferData(GL_SHADER_STORAGE_BUFFER, depths.size() * sizeof(float), depths.data(), GL_DYNAMIC_DRAW);
Expand All @@ -564,6 +560,8 @@ void PointCloudRenderer::doDrawVisual(const sofa::core::visual::VisualParams* vp

if(d_withCuda.getValue())
{
selectedIndices.resize(renderingData.size());
std::iota(selectedIndices.begin(), selectedIndices.end(), 0);
PointCloudRendererBackend::transform_and_sort_cuda(viewmat, interop_positions, interop_depths, interop_indices, renderingData.size());
}else
{
Expand Down
1 change: 1 addition & 0 deletions src/sofa/pointcloud/components/PointCloudRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class PointCloudRenderer : public sofa::core::visual::VisualModel {
std::vector<float> depths;
gl::GLSLShader shader;
GaussianData renderingData;
GaussianData referenceData; // untransformed source data

BaseGLBuffer *interop_positions;
BaseGLBuffer *interop_depths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public:

void reset(int N_)
{
std::cout << "RESET CUDA BUFFER " << std::endl;
//std::cout << "RESET CUDA BUFFER " << std::endl;
N = N_;

short* d_depths_out;
Expand Down
4 changes: 4 additions & 0 deletions src/sofa/pointcloud/components/PointCloudRendererBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once
#ifdef _WIN32
#include <windows.h>
#endif

#include <GL/gl.h>
#include <Eigen/Dense>

Expand Down
2 changes: 1 addition & 1 deletion src/sofa/pointcloud/components/PointCloudRendererCUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void PointCloudRendererCUDA::doDrawVisual(const sofa::core::visual::VisualParams
// Here we have geometries to draw and a camera that look at it.
// We first send the camera parameters to the gl rendering backend, then the geometries.
auto c = l_camera->getPosition();
Eigen::Vector3f cam_pos {c[0],c[1],c[2]};
Eigen::Vector3f cam_pos { float(c[0]),float(c[1]), float(c[2]) };

float fov = l_camera->getFieldOfView();
float tanHalfFov = tan((fov / 180.0 * M_PI) / 2.0f);
Expand Down