feat(graphics_utils): Optimize 3D geometric utilities with GPU support and type safety #1300
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🚀 Summary
This PR significantly refactors
utils/graphics_utils.pyto modernize the core geometry utilities. The changes introduce GPU-accelerated operations, an object-oriented API for better data encapsulation, and full type hinting, while maintaining full backward compatibility. This refactoring aims to improve performance, code clarity, and developer experience for 3D Gaussian Splatting and related tasks.✨ Motivation
The original utility functions, while functional, were procedural and not optimized for batch operations or modern PyTorch workflows. This refactor addresses these limitations by:
📋 Key Changes
1. New Data Structures (Core Enhancement)
PointCloudclass: Adataclasswrapper for(points, colors, normals)that handles device management, data validation, and transformations. It supports easy.to(device)movement and.numpy()conversion.CameraParametersclass: Encapsulates intrinsic/extrinsic camera parameters. It automatically precomputes derived matrices (projection, view) and provides properties likecamera_center.2. Optimized & New Core Functions
transform_points: Enhanced to support batched transformation matrices and improved numerical stability.get_world_to_view_matrix/get_view_to_world_matrix: More logically named, torch-native versions of the original functions.get_projection_matrix: Unchanged in logic, but now can target a specificdevice.normalize_points,compute_bounding_box,project_points_to_image, andcompute_point_densityfor common 3D tasks.3. Code Quality & Developer Experience
snake_casefor consistency with PyTorch conventions.4. Backward Compatibility
geom_transform_points,getWorld2View,getWorld2View2,getProjectionMatrix,fov2focal,focal2fov, and theBasicPointCloudNamedTupleare preserved with identical signatures. They act as wrappers to the new implementations where applicable.🧪 Testing
To ensure robustness, the following tests were performed:
transform_points) match the originalnumpy-based functions within a tolerance of1e-6.cpuandcuda(if available) devices.if __name__ == "__main__":block in the file provides a basic execution test.📝 Checklist
graphics_utils.py) and its logical improvements.🔮 Future Work (Optional Notes)
This refactor lays the groundwork for several potential future improvements:
compute_point_densityfor very large point clouds.CameraParametersframework.❓ Additional Notes
PointCloudclass usestorch.Tensorinternally. Users withnumpyarrays can easily use thePointCloud.from_numpy()constructor.