INSTALLATION

This library is known to work on Ubuntu 24.04 using g++ 13, but it will
likely work with other configurations too.  Lines starting with "$" are bash
commands.  For the purposes of these instructions, we will assume that we are
installing into /home/user/PhysBAM.  You will need to adjust the paths
based on the actual installation location.

--------------------------------------------------------------------------------
PhysBAM setup
--------------------------------------------------------------------------------
1. Download the library.  This code requires version PhysBAM-0 or PhysBAM-1.

$ cd /home/user
$ wget https://www.cs.ucr.edu/~craigs/papers/physbam/PhysBAM-1.zip

2. Unpack the library:

$ unzip PhysBAM-1.zip

3. This extracts the library into a directory called PhysBAM, which in this case
is at /home/user/PhysBAM.  Next, we need to move the surface_reconstruction_fast
directory into Projects. 

$ mv /path/to/surface_reconstruction_fast /home/user/PhysBAM/Projects/

4. If using PhysBAM-0, use the Arrays folder in surface_reconstruction_fast
directory to replace the Arrays folder in /home/user/PhysBAM/Public_Library/Core/Arrays.
If using PhysBAM-1, no changes are required.

$ rm -rf /home/user/PhysBAM/Public_Library/Core/Arrays
$ cp -r /home/user/PhysBAM/Projects/surface_reconstruction_fast/Arrays /home/user/PhysBAM/Public_Library/Core/

5. Follow the library build instructions in /home/user/PhysBAM/README.txt.
Building the library automatically builds all projects located in the Projects
directory, so this project will be built automatically.

--------------------------------------------------------------------------------
Install libtorch C++
--------------------------------------------------------------------------------
$ cd /home/user
$ wget https://download.pytorch.org/libtorch/cu128/libtorch-shared-with-deps-2.8.0%2Bcu128.zip
$ unzip libtorch-shared-with-deps-2.8.0%2Bcu128.zip

Add to /home/user/PhysBAM/SConstruct.options:

LTDIR='/home/user/libtorch'
torch_enable=1
torch_includes=[LTDIR+'/include',LTDIR+'/include/torch/csrc/api/include']
torch_libpath=[LTDIR+'/lib']
torch_rpath=[LTDIR+'/lib']

--------------------------------------------------------------------------------
Install partio 
--------------------------------------------------------------------------------
$ cd /home/user
$ git clone https://github.com/wdas/partio.git
$ cd partio
$ make -j prefix=/home/user/pt install

Add to /home/user/PhysBAM/SConstruct.options:

PTDIR='/home/user/pt'
partio_enable=1
partio_includes=[PTDIR+'/include']
partio_libpath=[PTDIR+'/lib']
partio_rpath=[PTDIR+'/lib']

--------------------------------------------------------------------------------
Install openvdb c++ (need CMake 3.18 or higher)
--------------------------------------------------------------------------------
1. Download prerequisite packages
$ apt-get install -y libboost-iostreams-dev
$ apt-get install -y libtbb-dev
$ apt-get install -y libblosc-dev

2. Download openvdb
$ cd /home/user
$ wget https://github.com/AcademySoftwareFoundation/openvdb/archive/refs/tags/v10.1.0.zip
$ unzip v10.1.0.zip
$ cd openvdb-10.1.0
$ mkdir build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=/home/user/openvdb ..
$ make -j4 && make install

Add to /home/user/PhysBAM/SConstruct.options:

OPDIR='/home/user/openvdb'
openvdb_enable=1
openvdb_includes=[OPDIR+'/include']
openvdb_libpath=[OPDIR+'/lib']
openvdb_rpath=[OPDIR+'/lib']
openvdb_libs=['openvdb','tbb']

--------------------------------------------------------------------------------
Install pytorch and tensorboard
--------------------------------------------------------------------------------
$ pip3 install torch
$ pip3 install onnx
$ pip3 install tensorboard
$ pip3 install tensorboardX

# incompatibility jason parameter for some later versions >5
$ pip3 install protobuf==4.25.5

--------------------------------------------------------------------------------
Install cuda toolkit 12.8 
--------------------------------------------------------------------------------
1. Install cuda toolkit from Nvidia website (tested on version 12.8 Ubuntu 24.04):
https://developer.nvidia.com/cuda-downloads

$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-ubuntu2404.pin
$ sudo mv cuda-ubuntu2404.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ wget https://developer.download.nvidia.com/compute/cuda/12.8.0/local_installers/cuda-repo-ubuntu2404-12-8-local_12.8.0-570.86.10-1_amd64.deb
$ sudo dpkg -i cuda-repo-ubuntu2404-12-8-local_12.8.0-570.86.10-1_amd64.deb
$ sudo cp /var/cuda-repo-ubuntu2404-12-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
$ sudo apt-get update
$ sudo apt-get -y install cuda-toolkit-12-8

Add to /home/user/PhysBAM/SConstruct.options:
torch_includes.append('/usr/local/cuda-12.8/targets/x86_64-linux/include')
torch_libpath.append('/usr/local/cuda-12.8/targets/x86_64-linux/lib')

--------------------------------------------------------------------------------
Install tensorrt 
--------------------------------------------------------------------------------
1. Install tensorrt from Nvidia website (tested on version 10.12):
https://developer.nvidia.com/tensorrt

$ os="ubuntu2404"
$ tag="10.12.0-cuda-12.9"

$ wget https://developer.download.nvidia.com/compute/tensorrt/10.12.0/local_installers/nv-tensorrt-local-repo-${os}-${tag}_1.0-1_amd64.deb
$ sudo dpkg -i nv-tensorrt-local-repo-${os}-${tag}_1.0-1_amd64.deb
$ sudo cp /var/nv-tensorrt-local-repo-${os}-${tag}/*-keyring.gpg /usr/share/keyrings/
 
$ sudo apt-get update
$ sudo apt-get install tensorrt-dev

Add to /home/user/PhysBAM/SConstruct.options:
torch_libs=['c10','torch_cpu','torch_cuda','cudart','nvinfer','nvonnxparser']
torch_linkflags=['-Wl,--no-as-needed']

--------------------------------------------------------------------------------
Train and test model
--------------------------------------------------------------------------------
1. This code has been tested on NVIDIA Driver Version of 570 and CUDA Version
12.8, but might work with other configuration as ell, Adjust the batch_size or
the -P argument of xargs (maximum processes) if you encounter a CUDA out-of-memory
error. Rebuild the project after you modify the SConstruct.options file, then:

$ cd /home/user/PhysBAM/Projects/surface_reconstruction_fast
$ ./run_all.sh

2. By default, the run_all.sh script runs the entire pipeline, which includes
generating training data, training the neural network model, and performing the
evaluation. We also provide a pre-trained model named test_nnf in the models
directory. If you want to skip the training step and perform only the evaluation,
set the GENERATE_DATA, DO_SELECT, and TRAIN_MODEL variables in train_model.sh
to 0 before running the script.

3. By default the trained model (located in the models directory) is tested on
10 frames from a dambreak simulation. You can find the particle data and surface 
reconstruction results under the dambreak directory:
Input:    dambreak/xyz/  (compressed particle data)
          dambreak/obj/  (particle data in obj format)
Output:   dambreak/vdb/  (surface vdb files)
          dambreak/bgeo/ (particle data in bgeo format)

Unless specified otherwise, the evaluation program expects compressed particle data as
input. To use OBJ files instead, add the -obj flag when running ./evaluate in the
eval_model.sh folder, and set -i to your OBJ path (e.g., -i $out_dir/obj/X-%03d.obj).

--------------------------------------------------------------------------------
View training results
--------------------------------------------------------------------------------
$ tensorboard --logdir logs 

--------------------------------------------------------------------------------
View test results on Houdini
--------------------------------------------------------------------------------
File->Import->Geometry->path to bgeo/vdb files

vier surface using vdb:
1. double click imported vdb->tab->ConvertVdb, and connect it to vdb node.
2. Set ConvertTo to Polygons
2. set Adaptivity to 0.005 for better visual 

--------------------------------------------------------------------------------
