From fc0a9758b25af1e2a7c123d7994f2390c6461eee Mon Sep 17 00:00:00 2001 From: Hugo Ledoux Date: Mon, 11 Mar 2024 12:34:47 +0100 Subject: [PATCH] Update docs /examples page and add those to the demo folder --- demo/example_exporting_geojson.py | 9 +++++++++ demo/example_exporting_meshio.py | 13 +++++++++++++ demo/example_matplotlib.py | 18 ++++++++++++++++++ demo/example_polyscope.py | 27 +++++++++++++++++++++++++++ demo/example_reading_geotiff.py | 20 ++++++++++++++++++++ demo/example_reading_laz.py | 11 +++++++++++ docs/examples.md | 13 ++++++++----- 7 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 demo/example_exporting_geojson.py create mode 100644 demo/example_exporting_meshio.py create mode 100644 demo/example_matplotlib.py create mode 100644 demo/example_polyscope.py create mode 100644 demo/example_reading_geotiff.py create mode 100644 demo/example_reading_laz.py diff --git a/demo/example_exporting_geojson.py b/demo/example_exporting_geojson.py new file mode 100644 index 0000000..6e6e3dd --- /dev/null +++ b/demo/example_exporting_geojson.py @@ -0,0 +1,9 @@ +import startinpy +import numpy as np + +#-- generate 100 points randomly in the plane +rng = np.random.default_rng(seed=42) +pts = rng.random((100, 3)) +dt = startinpy.DT() +dt.insert(pts, insertionstrategy="AsIs") +dt.write_geojson("myfile.geojson") \ No newline at end of file diff --git a/demo/example_exporting_meshio.py b/demo/example_exporting_meshio.py new file mode 100644 index 0000000..d30bdbb --- /dev/null +++ b/demo/example_exporting_meshio.py @@ -0,0 +1,13 @@ +import startinpy +import meshio +import laspy +import numpy as np + +las = laspy.read("../data/small.laz") +pts = np.vstack((las.x, las.y, las.z)).transpose() +dt = startinpy.DT() +dt.insert(pts) +vs = dt.points +vs[0] = vs[1] #-- to ensure that infinite vertex is not blocking the viz +cells = [("triangle", dt.triangles)] +meshio.write_points_cells("mydt.vtu", vs, cells) \ No newline at end of file diff --git a/demo/example_matplotlib.py b/demo/example_matplotlib.py new file mode 100644 index 0000000..8ed67ad --- /dev/null +++ b/demo/example_matplotlib.py @@ -0,0 +1,18 @@ +import startinpy +import numpy as np + +#-- generate 100 points randomly in the plane +rng = np.random.default_rng(seed=42) +pts = rng.random((100, 3)) +#-- scale to [0, 100] +pts = pts * 100 +t = startinpy.DT() +t.insert(pts) +pts = t.points +trs = t.triangles +#-- plot +import matplotlib.pyplot as plt +plt.triplot(pts[:,0], pts[:,1], trs) +#-- the vertex "0" shouldn't be plotted, so start at 1 +plt.plot(pts[1:,0], pts[1:,1], 'o') +plt.show() \ No newline at end of file diff --git a/demo/example_polyscope.py b/demo/example_polyscope.py new file mode 100644 index 0000000..d0d33ac --- /dev/null +++ b/demo/example_polyscope.py @@ -0,0 +1,27 @@ +import startinpy +import numpy as np +import polyscope as ps +import laspy + +las = laspy.read("../data/small.laz") +pts = np.vstack((las.x, las.y, las.z)).transpose() +pts = pts[::10] #-- thinning to speed up, put ::10 to keep 1/10 of the points +dt = startinpy.DT() +dt.insert(pts) + +pts = dt.points +pts[0] = pts[1] #-- first vertex has inf and could mess things +trs = dt.triangles + +ps.init() +ps.set_program_name("mydt") +ps.set_up_dir("z_up") +ps.set_ground_plane_mode("shadow_only") +ps.set_ground_plane_height_factor(0.01, is_relative=True) +ps.set_autocenter_structures(True) +ps.set_autoscale_structures(True) +pc = ps.register_point_cloud("mypoints", pts[1:], radius=0.0015, point_render_mode='sphere') +ps_mesh = ps.register_surface_mesh("mysurface", pts, trs) +ps_mesh.reset_transform() +pc.reset_transform() +ps.show() \ No newline at end of file diff --git a/demo/example_reading_geotiff.py b/demo/example_reading_geotiff.py new file mode 100644 index 0000000..78465fd --- /dev/null +++ b/demo/example_reading_geotiff.py @@ -0,0 +1,20 @@ +import startinpy +import rasterio +import random + +d = rasterio.open('../data/dem_01.tif') +band1 = d.read(1) +t = d.transform +pts = [] +for i in range(band1.shape[0]): + for j in range(band1.shape[1]): + x = t[2] + (j * t[0]) + (t[0] / 2) + y = t[5] + (i * t[4]) + (t[4] / 2) + z = band1[i][j] + if (z != d.nodatavals) and (random.randint(0, 100) == 5): + pts.append([x, y, z]) +dt = startinpy.DT() +dt.insert(pts, insertionstrategy="BBox") +#-- exaggerate the elevation by a factor 2.0 +dt.vertical_exaggeration(2.0) +dt.write_ply("mydt.ply") \ No newline at end of file diff --git a/demo/example_reading_laz.py b/demo/example_reading_laz.py new file mode 100644 index 0000000..7d06827 --- /dev/null +++ b/demo/example_reading_laz.py @@ -0,0 +1,11 @@ +import startinpy +import numpy as np +import laspy + +las = laspy.read("../data/small.laz") +pts = np.vstack((las.x, las.y, las.z)).transpose() +pts = pts[::1] #-- thinning to speed up, put ::10 to keep 1/10 of the points +dt = startinpy.DT() +dt.insert(pts) +print("number vertices:", dt.number_of_vertices()) +# -- number vertices: 39231 \ No newline at end of file diff --git a/docs/examples.md b/docs/examples.md index 9ca76fc..51c7385 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -14,7 +14,7 @@ pts = np.vstack((las.x, las.y, las.z)).transpose() pts = pts[::1] #-- thinning to speed up, put ::10 to keep 1/10 of the points dt = startinpy.DT() dt.insert(pts) -print("# vertices:", dt.number_of_vertices()) +print("number vertices:", dt.number_of_vertices()) ``` ## Exporting the DT to GeoJSON @@ -28,7 +28,7 @@ rng = np.random.default_rng(seed=42) pts = rng.random((100, 3)) dt = startinpy.DT() dt.insert(pts, insertionstrategy="AsIs") -dt.write_geojson("/home/elvis/myfile.geojson") +dt.write_geojson("myfile.geojson") ``` ## Exporting the DT to several mesh formats with [meshio](https://github.com/nschloe/meshio) @@ -36,10 +36,13 @@ dt.write_geojson("/home/elvis/myfile.geojson") ```python import startinpy import meshio +import laspy +import numpy as np -las = laspy.read("myfile.laz") +las = laspy.read("../data/small.laz") pts = np.vstack((las.x, las.y, las.z)).transpose() -dt = startinpy.DT(pts) +dt = startinpy.DT() +dt.insert(pts) vs = dt.points vs[0] = vs[1] #-- to ensure that infinite vertex is not blocking the viz cells = [("triangle", dt.triangles)] @@ -63,7 +66,7 @@ import startinpy import rasterio import random -d = rasterio.open('data/dem_01.tif') +d = rasterio.open('../data/dem_01.tif') band1 = d.read(1) t = d.transform pts = []