![csf1](https://github.com/jianboqi/CSF/blob/master/CSFDemo/CSF1.png) ![csf2](https://github.com/jianboqi/CSF/blob/master/CSFDemo/CSF2.png)
# CSF
Airborne LiDAR filtering method based on Cloth Simulation.
This is the code for the article:

W. Zhang, J. Qi*, P. Wan, H. Wang, D. Xie, X. Wang, and G. Yan, “An Easy-to-Use Airborne LiDAR Data Filtering Method Based on Cloth Simulation,” Remote Sens., vol. 8, no. 6, p. 501, 2016.
(http://www.mdpi.com/2072-4292/8/6/501/htm)


**This is a modified version of the original Code for 3DFin purposes**

For now this is only intended to be used inside [3DFin](https://github.com/3DFin/3Dfin)/[dendromatics](https://github.com/3DFin/dendromatics). 
But this repository also serve as a playground to bootstrap a planned full rewrite of CSF.

List of changes:

- Better handling of numpy arrays in the python bindings (avoid expensive copies)
- Port to nanobind (instead of SWIG)
- Bug fixes and improvement (mostly backport from CloudCompare version of CSF and original ones)
- Better (but yet to be improved) documentation and code structure.
- Removed ascii handling in favor of pure numpy interface

### How to use CSF in Python
```python
pip install csf-3dfin
```

#### Python examples

```python
import laspy
from CSF_3DFin import CSF
import numpy as np

las_file = laspy.read(r"sample.las") # read a las file
csf = CSF()

# prameter settings
csf.params.smooth_slope = False
csf.params.cloth_resolution = 0.5
# more details about parameter: http://ramm.bnu.edu.cn/projects/CSF/download/

csf.set_point_cloud(las_file.xyz)
ground, _ = csf.do_filtering() # do actual filtering.

out_file = laspy.LasData(las_file.header)
out_file.points = las_file.points[np.array(ground)] # extract ground points, and save it to a las file.
out_file.write(r"out.las")
```

### How to use CSF in Matlab
see more details from file `demo_mex.m` under matlab folder.

### How to use CSF in R

Thanks to the nice work of @Jean-Romain, through the collaboration, the CSF has been made as a R package, the details can be found in the [RCSF repository](https://github.com/Jean-Romain/RCSF). This package can be used easily with the [lidR package](https://github.com/Jean-Romain/lidR):

```r
library(lidR)
las  <- readLAS("file.las")
las  <- lasground(las, csf())
```

### How to use CSF in C++
Now, CSF is built by CMake, it produces a static library, which can be used by other c++ programs.
#### linux
To build the library, run:
```bash
mkdir build #or other name
cd build
cmake ..
make
sudo make install
```
or if you want to build the library and the demo executable `csfdemo`

```bash
mkdir build #or other name
cd build
cmake -DBUILD_DEMO=ON ..
make
sudo make install
```

#### Windows
You can use CMake GUI to generate visual studio solution file.

### Binary Version
For binary release version, it can be downloaded at: http://ramm.bnu.edu.cn/projects/CSF/download/

Note: This code has been changed a lot since the publication of the corresponding paper. A lot of optimizations have been made. We are still working on it, and wish it could be better.

### Cloudcompare Pulgin
At last, if you are interested in Cloudcompare, there is a good news. our method has been implemented as a Cloudcompare plugin, you can refer to : https://github.com/cloudcompare/trunk

### Related project
A tool named `CSFTools` has been recently released, it is based on CSF, and provides dem/chm generation, normalization. Please refer to: https://github.com/jianboqi/CSFTools

### License
CSF is maintained and developed by Jianbo QI. It is now released under Apache 2.0.

