C++ / LibTorch implementation of NeRF. Given RGB images of a static scene with known camera intrinsics and per-image extrinsics, fits a SIREN-encoded radiance field and renders RGB and depth from arbitrary novel viewpoints.
![]() |
![]() |
|---|---|
| RGB | Depth |
- Ray generation. For each pixel the camera-space direction
((x − W/2)/f, −(y − H/2)/f, −1)is rotated into world space by the extrinsics; the camera origin is the translation column. - Sampling. Stratified jittered sampling between near and far bounds: the interval is split into bins and one sample is drawn uniformly inside each bin.
- SIREN encoding. Each input coordinate is passed through its
own
SirenLayer, replacing NeRF's Fourier positional encoding. Encoded features for the three axes are flattened per ray sample. - MLP. Sine-activated hidden layers feed a softplus σ head and a sigmoid RGB head; the RGB head additionally consumes the view-direction features.
- Volume render. Discretised quadrature of σ along the ray gives per-sample α and transmittance; the resulting weights composite RGB and depth, alpha-composited onto a configurable background.
- Loss. Photometric MSE or pseudo-Huber on the rendered image
(selectable via
LossTypeinsrc/main.cpp) plus a regulariser of the form∑ exp(−σᵢ²/c)on the std σᵢ of the first-layer SIREN encoder weights, pushing them away from zero to keep the encoded frequency set diverse.
Training uses Adam. Network width/depth, sample count, batch size,
learning rate, and iteration count live as constants in
src/main.cpp and the SirenNeRF constructor.
Trained and evaluated on the NeRF synthetic dataset (held-out test split).
| Scene | PSNR ↑ | RMSE ↓ | SSIM ↑ |
|---|---|---|---|
| lego | — | — | — |
| ship | — | — | — |
Requires a C++20 compiler, CMake ≥ 3.20, LibTorch (with CUDA if GPU training is wanted), and nlohmann_json.
mkdir build && cd build
cmake ..
make./NeRF.cpp <data_path> <output_path>load_dataset reads <data_path>/transforms.json — a shared
camera_angle_x (FoV) and a transform_matrix (4×4 camera-to-world)
per frame — plus the referenced image files. Scenes from the NeRF
synthetic dataset (e.g. lego, ship) work directly.
- NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis (Mildenhall et al., ECCV 2020)
- Implicit Neural Representations with Periodic Activation Functions (Sitzmann et al., NeurIPS 2020)
- cNeRF by rafaelanderka

