Skip to content

Add TetraMeshData class to handle tetrahedral mesh data #407

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions demos/materials/tetrahedra_mesh_emission.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@

import os
import time
from pathlib import Path

import numpy as np
from matplotlib import pyplot as plt
from Py3DViewer import Tetmesh

from raysect.optical import World, translate, Point3D, Vector3D, rotate_basis
from raysect.core.math.function.float.function3d.interpolate import Discrete3DMesh
from raysect.optical import Point3D, Vector3D, World, rotate_basis, translate
from raysect.optical.library import RoughTitanium
from raysect.optical.material import InhomogeneousVolumeEmitter
from raysect.optical.observer import PinholeCamera, RGBPipeline2D, RGBAdaptiveSampler2D
from raysect.primitive import Box
from raysect.optical.observer import PinholeCamera, RGBAdaptiveSampler2D, RGBPipeline2D
from raysect.primitive import Box, TetraMeshData

BASE_PATH = os.path.split(os.path.realpath(__file__))[0]
RESOURCE_PATH = Path(__file__).parent.parent / "resources"


class UnityEmitter(InhomogeneousVolumeEmitter):
Expand All @@ -28,26 +28,37 @@ def emission_function(self, point, direction, spectrum, world, ray, primitive, t


# Rabbit tetrahedra mesh emitter
mesh = Tetmesh(os.path.join(BASE_PATH, "../resources/stanford_bunny.mesh"))

try:
tetra = Discrete3DMesh(mesh.vertices, mesh.polys, np.ones((mesh.num_polys)), False, 0)
except AttributeError: # for backward compatibility with older versions of Py3DViewer
tetra = Discrete3DMesh(mesh.vertices, mesh.tets, np.ones((mesh.num_tets)), False, 0)
data = np.load(RESOURCE_PATH / "stanford_bunny_tetra_mesh.npz")
vertices = data["vertices"]
tetrahedra = data["tetrahedra"]
print("Creating tetra mesh...")
mesh = TetraMeshData(vertices, tetrahedra)
tetra = Discrete3DMesh.from_mesh(mesh, np.ones(mesh.tetrahedra.shape[0]), limit=False, default_value=0.0)
print("Done.")

# scene
world = World()
lower = Point3D(mesh.bbox[0, 0], mesh.bbox[0, 1], mesh.bbox[0, 2])
upper = Point3D(mesh.bbox[1, 0], mesh.bbox[1, 1], mesh.bbox[1, 2])
emitter = Box(lower, upper, material=UnityEmitter(tetra), parent=world)
bbox = mesh.bounding_box(world.to(world))
emitter = Box(bbox.lower, bbox.upper, material=UnityEmitter(tetra), parent=world)
floor = Box(Point3D(-100, -0.1, -100), Point3D(100, -0.01, 100), world, material=RoughTitanium(0.1))

# camera
camera_pos = Point3D(-0.015, 0.204, 0.25)
rgb_pipeline = RGBPipeline2D(display_update_time=5)
sampler = RGBAdaptiveSampler2D(rgb_pipeline, min_samples=100, fraction=0.2)
camera = PinholeCamera((512, 512), parent=world, transform=translate(camera_pos.x, camera_pos.y, camera_pos.z) * rotate_basis(camera_pos.vector_to(Point3D(0, 0.06, 0)), Vector3D(0, 1, 0)),
pipelines=[rgb_pipeline], frame_sampler=sampler)
camera = PinholeCamera(
(512, 512),
parent=world,
pipelines=[rgb_pipeline],
frame_sampler=sampler,
transform=(
translate(camera_pos.x, camera_pos.y, camera_pos.z)
* rotate_basis(
camera_pos.vector_to(Point3D(0, 0.06, 0)),
Vector3D(0, 1, 0)
)
)
)
camera.fov = 50
camera.spectral_bins = 1
camera.spectral_rays = 1
Expand All @@ -60,7 +71,7 @@ def emission_function(self, point, direction, spectrum, world, ray, primitive, t
os.nice(15)
plt.ion()
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S")
for p in range(1, 1000):
for p in range(1, 100):
print("Rendering pass {}...".format(p))
camera.observe()
rgb_pipeline.save("demo_volume_{}_pass_{}.png".format(timestamp, p))
Expand Down
Loading
Loading