Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitsuba rendering option #35

Merged
merged 16 commits into from
May 14, 2024
2 changes: 1 addition & 1 deletion .github/workflows/publish-to-pypi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Python 3.9
uses: actions/setup-python@v1
with:
python-version: 3.9
python-version: 3.11
- name: Install pypa/build
run: >-
python -m
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ jobs:
fail-fast: false
matrix:
python-version:
- 3.8
- 3.9
- "3.10"
- "3.11"
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fail_fast: true

repos:
- repo: https://github.com/psf/black
rev: 24.2.0
rev: 24.4.2
hooks:
- id: black

Expand Down
69 changes: 69 additions & 0 deletions examples/material_spheres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
ZnVis: A Zincwarecode package.
License
-------
This program and the accompanying materials are made available under the terms
of the Eclipse Public License v2.0 which accompanies this distribution, and is
available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zincwarecode Project.
Contact Information
-------------------
email: zincwarecode@gmail.com
github: https://github.com/zincware
web: https://zincwarecode.com/
Citation
--------
If you use this module please cite us with:

Summary
-------
Tutorial script to visualize simple spheres over a random trajectory.
"""

import mitsuba as mi
import numpy as np

import znvis as vis

if __name__ == "__main__":
"""
Run the simple spheres example.
"""
# Particle 1 definition
hairy = mi.load_dict({"type": "conductor", "material": "Au"})
material_1 = vis.Material(
colour=np.array([30, 144, 255]) / 255, alpha=0.6, mitsuba_bsdf=hairy
)

# Define the first particle.
trajectory = np.random.uniform(-100, 100, (100, 1000, 3))
mesh = vis.Sphere(radius=2.0, resolution=10, material=material_1)
particle = vis.Particle(
name="Blue", mesh=mesh, position=trajectory, smoothing=False
)

# Define the second particle.
bsdf_smooth_plastic = mi.load_dict(
{
"type": "plastic",
"diffuse_reflectance": {"type": "rgb", "value": [0.1, 0.27, 0.36]},
"int_ior": 1.9,
}
)
material_2 = vis.Material(
colour=np.array([255, 140, 0]) / 255,
alpha=1.0,
mitsuba_bsdf=bsdf_smooth_plastic,
)

# Define the second particle.
trajectory_2 = np.random.uniform(-10, 10, (100, 1000, 3))
mesh_2 = vis.Sphere(radius=1.0, resolution=10, material=material_2)
particle_2 = vis.Particle(
name="Orange", mesh=mesh_2, position=trajectory_2, smoothing=False
)

# Construct the visualizer and run
visualizer = vis.Visualizer(particles=[particle, particle_2], frame_rate=20)
visualizer.run_visualization()
14 changes: 8 additions & 6 deletions examples/simple_spheres.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@
"""
material_1 = vis.Material(colour=np.array([30, 144, 255]) / 255, alpha=0.6)
# Define the first particle.
trajectory = np.random.uniform(-10, 10, (10, 10, 3))
mesh = vis.Sphere(radius=2.0, resolution=50, material=material_1)
particle = vis.Particle(name="Blue", mesh=mesh, position=trajectory, smoothing=True)
trajectory = np.random.uniform(-100, 100, (100, 1000, 3))
mesh = vis.Sphere(radius=2.0, resolution=10, material=material_1)
particle = vis.Particle(
name="Blue", mesh=mesh, position=trajectory, smoothing=False
)

material_2 = vis.Material(colour=np.array([255, 140, 0]) / 255, alpha=1.0)

# Define the second particle.
trajectory_2 = np.random.uniform(-10, 10, (10, 10, 3))
mesh_2 = vis.Sphere(radius=1.0, resolution=50, material=material_2)
trajectory_2 = np.random.uniform(-10, 10, (100, 1000, 3))
mesh_2 = vis.Sphere(radius=1.0, resolution=10, material=material_2)
particle_2 = vis.Particle(
name="Orange", mesh=mesh_2, position=trajectory_2, smoothing=True
name="Orange", mesh=mesh_2, position=trajectory_2, smoothing=False
)

# Construct the visualizer and run
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ numpy
pytest
rich
opencv-python
mitsuba
2 changes: 2 additions & 0 deletions znvis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
init file for the main ZnVis package.
"""

from znvis import rendering
from znvis.bounding_objects.bounding_box import BoundingBox
from znvis.material.material import Material
from znvis.mesh.custom import CustomMesh
Expand All @@ -38,4 +39,5 @@
CustomMesh.__name__,
BoundingBox.__name__,
Material.__name__,
rendering.__name__,
]
12 changes: 7 additions & 5 deletions znvis/bounding_objects/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Create bounding box.
"""

from dataclasses import dataclass
from dataclasses import dataclass, field

import numpy as np
import open3d as o3d
Expand All @@ -43,10 +43,12 @@ class BoundingBox:
colour : np.ndarray shape=(3,)
"""

center: np.ndarray = np.array([0, 0, 0])
box_size: np.ndarray = np.array([1.0, 1.0, 1.0])
rotation_matrix: np.ndarray = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
colour: np.ndarray = np.array([0.0, 0.0, 0.0])
center: np.ndarray = field(default_factory=lambda: np.array([0, 0, 0]))
box_size: np.ndarray = field(default_factory=lambda: np.array([1.0, 1.0, 1.0]))
rotation_matrix: np.ndarray = field(
default_factory=lambda: np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
)
colour: np.ndarray = field(default_factory=lambda: np.array([0.0, 0.0, 0.0]))

def __call__(self) -> o3d.geometry.TriangleMesh:
"""
Expand Down
9 changes: 7 additions & 2 deletions znvis/material/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Material parent class.
"""

from dataclasses import dataclass
from dataclasses import dataclass, field

import numpy as np

Expand All @@ -46,11 +46,16 @@ class Material:
How reflective the material is.
anisotropy: float
How anisotopic the material is.
mitsuba_bsdf: mitsuba.bsdf (default: None)
Mitsuba bsdf object.
"""

colour: np.ndarray = np.array([59.0, 53.0, 97.0]) / 255
colour: np.ndarray = field(
default_factory=lambda: np.array([59.0, 53.0, 97.0]) / 255
)
alpha: float = 1.0
roughness: float = 0.5
metallic: float = 0.0
reflectance: float = 0.4
anisotropy: float = 0.4
mitsuba_bsdf: object = None
4 changes: 3 additions & 1 deletion znvis/mesh/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CustomMesh(Mesh):
"""

file: str = None
scale: float = 1.0

def create_mesh(
self, starting_position: np.ndarray, starting_orientation: np.ndarray = None
Expand All @@ -66,9 +67,10 @@ def create_mesh(
"""
mesh = o3d.io.read_triangle_mesh(self.file)
mesh.compute_vertex_normals()
mesh.scale(self.scale, center=mesh.get_center())
mesh.translate(starting_position.astype(float))
if starting_orientation is not None:
matrix = rotation_matrix(np.array([0, 0, 1]), starting_orientation)
matrix = rotation_matrix(self.base_direction, starting_orientation)
mesh.rotate(matrix)

return mesh
2 changes: 1 addition & 1 deletion znvis/mesh/cylinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def create_mesh(
cylinder.compute_vertex_normals()
cylinder.translate(starting_position.astype(float))
if starting_orientation is not None:
matrix = rotation_matrix(np.array([0, 0, 1]), starting_orientation)
matrix = rotation_matrix(self.base_direction, starting_orientation)
cylinder.rotate(matrix)

return cylinder
5 changes: 3 additions & 2 deletions znvis/mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Module for the mesh parent class.
"""

from dataclasses import dataclass
from dataclasses import dataclass, field

import numpy as np
import open3d as o3d
Expand All @@ -40,7 +40,8 @@ class Mesh:
A ZnVis material class.
"""

material: Material = Material()
material: Material = field(default_factory=lambda: Material())
base_direction: np.ndarray = field(default_factory=lambda: np.array([1, 0, 0]))

def __post_init__(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion znvis/mesh/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def create_mesh(
sphere.compute_vertex_normals()
sphere.translate(starting_position.astype(float))
if starting_orientation is not None:
matrix = rotation_matrix(np.array([0, 0, 1]), starting_orientation)
matrix = rotation_matrix(self.base_direction, starting_orientation)
sphere.rotate(matrix)

return sphere
26 changes: 26 additions & 0 deletions znvis/rendering/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
ZnVis: A Zincwarecode package.
License
-------
This program and the accompanying materials are made available under the terms
of the Eclipse Public License v2.0 which accompanies this distribution, and is
available at https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zincwarecode Project.
Contact Information
-------------------
email: zincwarecode@gmail.com
github: https://github.com/zincware
web: https://zincwarecode.com/
Citation
--------
If you use this module please cite us with:

Summary
-------
Init file for the rendering module.
"""

from znvis.rendering.mitsuba import Mitsuba

__all__ = [Mitsuba.__name__]
Loading
Loading