Skip to content

Commit

Permalink
add two more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
LiangliangNan committed Dec 26, 2024
1 parent b0ca682 commit 92ca840
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 128 deletions.
8 changes: 4 additions & 4 deletions python/examples/example_001-data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# "pip install YOUR_BUILD_DIRECTORY/lib/python/setup.py"
# -------------------------------------------------------------------------------
import sys
sys.path.append("../../cmake-build-debug/lib/python")
sys.path.append("../../cmake-build-release/lib/python")

# -------------------------------------------------------------------------------
# Importing Necessary Libraries
Expand All @@ -28,9 +28,9 @@
# -------------------------------------------------------------------------------
# Initializing Easy3D
# -------------------------------------------------------------------------------
# The `easy3d.initialize(True)` function initializes the Easy3D library.
# The `True` parameter enables detailed logging, which is useful for debugging.
easy3d.initialize(True)
# The `easy3d.initialize(False)` function initializes the Easy3D library.
# Use `True` to enable detailed logging, which is useful for debugging.
easy3d.initialize(False)

# -------------------------------------------------------------------------------
# Creating and Converting a vec2 Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# "pip install YOUR_BUILD_DIRECTORY/lib/python/setup.py"
# -------------------------------------------------------------------------------
import sys
sys.path.append("../../cmake-build-debug/lib/python")
sys.path.append("../../cmake-build-release/lib/python")

# -------------------------------------------------------------------------------
# Importing Necessary Libraries
Expand All @@ -27,9 +27,9 @@
# -------------------------------------------------------------------------------
# Initializing Easy3D
# -------------------------------------------------------------------------------
# The `easy3d.initialize(True)` function initializes the Easy3D library.
# The `True` parameter enables detailed logging, which is useful for debugging.
easy3d.initialize(True)
# The `easy3d.initialize(False)` function initializes the Easy3D library.
# Use `True` to enable detailed logging, which is useful for debugging.
easy3d.initialize(False)

# -------------------------------------------------------------------------------
# Creating a PointCloud Object and Initializing with Points
Expand Down
8 changes: 4 additions & 4 deletions python/examples/example_101-viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# "pip install YOUR_BUILD_DIRECTORY/lib/python/setup.py"
# -------------------------------------------------------------------------------
import sys
sys.path.append("../../cmake-build-debug/lib/python")
sys.path.append("../../cmake-build-release/lib/python")

# Importing necessary libraries
import easy3d # Easy3D library for 3D visualization and processing
Expand All @@ -25,9 +25,9 @@
# -------------------------------------------------------------------------------
# Initializing Easy3D
# -------------------------------------------------------------------------------
# The `easy3d.initialize(True)` function initializes the Easy3D library.
# The `True` parameter enables detailed logging, which is useful for debugging.
easy3d.initialize(True)
# The `easy3d.initialize(False)` function initializes the Easy3D library.
# Use `True` to enable detailed logging, which is useful for debugging.
easy3d.initialize(False)

# -------------------------------------------------------------------------------
# Creating a Viewer
Expand Down
61 changes: 61 additions & 0 deletions python/examples/example_102-offscreen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -------------------------------------------------------------------------------
# Example: Offscreen Rendering
# -------------------------------------------------------------------------------
# This script demonstrates how to render 3D models into images without using a viewer.
# The model is rendered offscreen and saved as PNG images.
# The script includes steps for changing the camera position to get different views.
# -------------------------------------------------------------------------------

# -------------------------------------------------------------------------------
# Adding Easy3D Python Bindings to the System Path
# -------------------------------------------------------------------------------
# This is required if the bindings are not installed via `pip` but are located in
# a local build directory. To install the bindings, use the following command:
# "pip install YOUR_BUILD_DIRECTORY/lib/python/setup.py"
# -------------------------------------------------------------------------------
import sys
sys.path.append("../../cmake-build-release/lib/python")

# -------------------------------------------------------------------------------
# Importing Necessary Libraries
# -------------------------------------------------------------------------------
import easy3d # Easy3D library for 3D visualization and processing

# -------------------------------------------------------------------------------
# Initializing Easy3D
# -------------------------------------------------------------------------------
# The `easy3d.initialize(False)` function initializes the Easy3D library.
# Use `True` to enable detailed logging, which is useful for debugging.
easy3d.initialize(False)

# -------------------------------------------------------------------------------
# Setting Up the Model and Renderer
# -------------------------------------------------------------------------------
# Define the input model file. In this case, a 3D model of a bunny in .ply format.
file_name = easy3d.directory() + "/data/bunny.ply"
print(f"Input file name: {file_name}")

# Create an offscreen renderer instance. This allows rendering without a viewer.
renderer = easy3d.OffScreen()

# Add the model to the renderer.
model = renderer.add_model(file_name)

# Render the model to a PNG file.
renderer.render(file_name + ".png")

# -------------------------------------------------------------------------------
# Modifying Camera Position
# -------------------------------------------------------------------------------
# Get the current camera of the renderer.
camera = renderer.camera()

# Move the camera position slightly by adding a small offset.
# This gives a different view of the model.
pos = camera.position() + easy3d.vec3(0.2, 0.2, 0.2)

# Set the new camera position.
camera.setPosition(pos)

# Render the model again with the new camera position.
renderer.render(file_name + "2.png")
71 changes: 71 additions & 0 deletions python/examples/example_201_normal_estimtion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -------------------------------------------------------------------------------
# Example: Estimate Point Cloud Normals
# -------------------------------------------------------------------------------
# This script demonstrates how to estimate normals for a point cloud using Easy3D.
# The script loads a point cloud from a file, estimates its normals, reorients
# them, saves the resulting point cloud, and visualizes it in a viewer.
# -------------------------------------------------------------------------------

# -------------------------------------------------------------------------------
# Adding Easy3D Python Bindings to the System Path
# -------------------------------------------------------------------------------
# This step is necessary if the Easy3D bindings are not installed via `pip`
# but are located in a local build directory. To install the bindings,
# use the following command:
# "pip install YOUR_BUILD_DIRECTORY/lib/python/setup.py"
# -------------------------------------------------------------------------------
import sys
sys.path.append("../../cmake-build-release/lib/python")

# -------------------------------------------------------------------------------
# Importing Necessary Libraries
# -------------------------------------------------------------------------------
import easy3d # Easy3D library for 3D visualization and processing

# -------------------------------------------------------------------------------
# Initializing Easy3D
# -------------------------------------------------------------------------------
# Initialize the Easy3D library. The `True` parameter enables detailed logging,
# which is helpful for debugging.
easy3d.initialize(True)

# -------------------------------------------------------------------------------
# Loading the Point Cloud
# -------------------------------------------------------------------------------
# Define the input point cloud file. In this case, a point cloud in .bin format.
file_name = easy3d.directory() + "/data/bunny.bin"
print(f"Input file name: {file_name}")

# Load the point cloud from the specified file.
pc = easy3d.PointCloudIO.load(file_name)

# -------------------------------------------------------------------------------
# Estimating Point Cloud Normals
# -------------------------------------------------------------------------------
# Estimate normals for the point cloud. The `16` parameter specifies the
# number of neighbors to use for normal estimation.
easy3d.PointCloudNormals.estimate(pc, 16)

# Reorient the normals to ensure consistency.
easy3d.PointCloudNormals.reorient(pc, 16)

# -------------------------------------------------------------------------------
# Saving the Processed Point Cloud
# -------------------------------------------------------------------------------
# Save the point cloud with estimated normals to a new file in .ply format.
output_file = file_name + "-result.ply"
easy3d.PointCloudIO.save(output_file, pc)
print(f"Processed point cloud saved to: {output_file}")

# -------------------------------------------------------------------------------
# Visualizing the Point Cloud
# -------------------------------------------------------------------------------
# Create a viewer instance to visualize the point cloud.
viewer = easy3d.Viewer("Easy3D Viewer")

# Set to an empty to get rid of the lengthy usage instructions
viewer.set_usage("")

# Add the point cloud model to the viewer and run the viewer to interact with the model.
viewer.add_model(pc)
viewer.run()
24 changes: 0 additions & 24 deletions python/examples/example_Offscreen.py

This file was deleted.

44 changes: 0 additions & 44 deletions python/examples/example_PointCloudNormalEstimtion.py

This file was deleted.

90 changes: 42 additions & 48 deletions python/examples/tmp.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,71 @@
# -------------------------------------------------------------------------------
# Example: The basic Easy3D viewer
# Example: Estimate Point Cloud Normals
# -------------------------------------------------------------------------------
# This script demonstrates how to estimate normals for a point cloud using Easy3D.
# The script loads a point cloud from a file, estimates its normals, reorients
# them, saves the resulting point cloud, and visualizes it in a viewer.
# -------------------------------------------------------------------------------
# This script demonstrates how to use the Easy3D `Viewer` class to:
# 1. Create a 3D viewer with a custom title.
# 2. Load and visualize a 3D mesh model (`SurfaceMesh`) from a file.
# 3. Load and visualize a 3D point cloud model (`PointCloud`) from a file.
# 4. Run the viewer to interact with the models (e.g., rotate, zoom, pan).


# -------------------------------------------------------------------------------
# Adding Easy3D Python Bindings to the System Path
# -------------------------------------------------------------------------------
# This is required if the bindings are not installed via `pip` but are located in
# a local build directory. To install the bindings, use the following command:
# This step is necessary if the Easy3D bindings are not installed via `pip`
# but are located in a local build directory. To install the bindings,
# use the following command:
# "pip install YOUR_BUILD_DIRECTORY/lib/python/setup.py"
# -------------------------------------------------------------------------------
import sys
sys.path.append("../../cmake-build-debug/lib/python")
sys.path.append("../../cmake-build-release/lib/python")

# Importing necessary libraries
import easy3d # Easy3D library for 3D visualization and processing
import numpy as np # NumPy library (used for handling data if needed)
# -------------------------------------------------------------------------------
# Importing Necessary Libraries
# -------------------------------------------------------------------------------
import easy3d # Easy3D library for 3D visualization and processing

# -------------------------------------------------------------------------------
# Initializing Easy3D
# -------------------------------------------------------------------------------
# The `easy3d.initialize(True)` function initializes the Easy3D library.
# The `True` parameter enables detailed logging, which is useful for debugging.
# Initialize the Easy3D library. The `True` parameter enables detailed logging,
# which is helpful for debugging.
easy3d.initialize(True)

# -------------------------------------------------------------------------------
# Creating a Viewer
# Loading the Point Cloud
# -------------------------------------------------------------------------------
# The `Viewer` class provides a GUI window for visualizing 3D models.
# Here, we create a viewer instance with a custom title, "Easy3D Viewer".
viewer = easy3d.Viewer("Easy3D Viewer")
# Define the input point cloud file. In this case, a point cloud in .bin format.
file_name = easy3d.directory() + "/data/bunny.bin"
print(f"Input file name: {file_name}")

# Load the point cloud from the specified file.
pc = easy3d.PointCloudIO.load(file_name)

# -------------------------------------------------------------------------------
# Loading and Adding a 3D Mesh Model
# Estimating Point Cloud Normals
# -------------------------------------------------------------------------------
# Load a 3D mesh file (e.g., Stanford Bunny in .ply format).
# The `easy3d.directory()` function retrieves the root directory of Easy3D's
# resources. The model is located in the `data` subdirectory.
mesh_file = easy3d.directory() + "/data/bunny.ply"

# Add the mesh model to the viewer.
# This can be done directly using the file path, as shown below:
viewer.add_model(mesh_file)
# Estimate normals for the point cloud. The `16` parameter specifies the
# number of neighbors to use for normal estimation.
easy3d.PointCloudNormals.estimate(pc, 16)

# Alternatively, you can load the model as a `SurfaceMesh` object and add it:
# model = easy3d.SurfaceMeshIO.load(mesh_file)
# viewer.add_model(model)
# Reorient the normals to ensure consistency.
easy3d.PointCloudNormals.reorient(pc, 16)

# -------------------------------------------------------------------------------
# Loading and Adding a 3D Point Cloud Model
# Saving the Processed Point Cloud
# -------------------------------------------------------------------------------
# Load a 3D point cloud file (e.g., a polyhedron in .bin format).
pointcloud_file = easy3d.directory() + "/data/polyhedron.bin"

# The `PointCloudIO.load()` function reads the point cloud data from the file.
pointcloud = easy3d.PointCloudIO.load(pointcloud_file)

# Add the point cloud model to the viewer.
# You can add the point cloud object directly as shown below:
viewer.add_model(pointcloud)

# Alternatively, the point cloud file path can also be added directly:
# viewer.add_model(pointcloud_file)
# Save the point cloud with estimated normals to a new file in .ply format.
output_file = file_name + "-result.ply"
easy3d.PointCloudIO.save(output_file, pc)
print(f"Processed point cloud saved to: {output_file}")

# -------------------------------------------------------------------------------
# Running the Viewer
# Visualizing the Point Cloud
# -------------------------------------------------------------------------------
# The `viewer.run()` function launches the viewer window, where users can:
# - Interact with the 3D models (e.g., rotate, zoom, pan).
# - Explore the structure and details of the loaded models.
# Create a viewer instance to visualize the point cloud.
viewer = easy3d.Viewer("Easy3D Viewer")

# Set to an empty to get rid of the lengthy usage instructions
viewer.set_usage("")

# Add the point cloud model to the viewer and run the viewer to interact with the model.
viewer.add_model(pc)
viewer.run()

0 comments on commit 92ca840

Please sign in to comment.