Commit d1e87581 authored by sfritschi's avatar sfritschi
Browse files

Added open3d visualization for networks

parent 4c0a5a02
import sys
sys.path.append('../')
from netflow import *
import open3d as o3d
import numpy as np
from typing import List
def coord_transform(pos: List[float], lb: float, ub: float):
transform = lambda x: 10. / (ub - lb) * (2. * x - ub - lb)
return np.array(list(map(transform, pos)))
def main():
basenet = netflow.load_network_from("../netflow/network/network.h5")
# Setup GUI
vis = o3d.visualization.VisualizerWithKeyCallback()
vis.create_window()
# Add default primitives
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(
size=0.1, origin=[0, 0, 0]
)
vis.add_geometry(coordinate_frame) # coordinate frame
aabb = o3d.geometry.AxisAlignedBoundingBox(
min_bound=-10 * np.ones(3), max_bound=10 * np.ones(3)
)
aabb.color = [0.7, 0.7, 0.7]
vis.add_geometry(aabb) # bounding box
# Colors
black = np.array([0.0, 0.0, 0.0])
red = np.array([1.0, 0.0, 0.0])
blue = np.array([0.0, 0.0, 1.0])
magenta = np.array([1.0, 0.0, 1.0])
gold = np.array([186./255., 151./255., 47./255.])
n_zero_radius = 0
for i, pore in enumerate(basenet.pores):
radius = .6e4 * pore.r
if pore.r < 1e-6:
n_zero_radius += 1
continue
# Add sphere
sphere = o3d.geometry.TriangleMesh.create_sphere(radius=radius, resolution=10)
if pore.label == netflow.LABELS[0]:
sphere.paint_uniform_color(black)
elif pore.label[:2] == 'in':
sphere.paint_uniform_color(red)
elif pore.label[:3] == 'out':
sphere.paint_uniform_color(blue)
else:
sphere.paint_uniform_color(magenta)
pos = coord_transform(pore.pos, basenet.lb[0], basenet.ub[0])
sphere.translate(pos)
vis.add_geometry(sphere)
print("Did not render %d/%d pores because of too small radius" % (n_zero_radius, len(basenet.pores)))
n = 100
n_zero_radius = 0
for i, throat in enumerate(basenet.throats):
if i == n:
break
radius = .4e4 * throat.r
if throat.r < 1e-6:
n_zero_radius += 1
continue
x1, x2 = netflow.throat_ends(throat,
[ub-lb for lb,ub in zip(basenet.lb,basenet.ub)])
x1_t = coord_transform(x1, basenet.lb[0], basenet.ub[0])
x2_t = coord_transform(x2, basenet.lb[0], basenet.ub[0])
height = np.linalg.norm(x1_t - x2_t)
cylinder = o3d.geometry.TriangleMesh.create_cylinder(radius=radius, height=height, resolution=8, split=4)
cylinder.paint_uniform_color(gold)
# normalized vectors
cylinder_orient = np.array([0.0, 0.0, 1.0])
cylinder_direct = (x1_t - x2_t) / height
v = np.cross(cylinder_orient, cylinder_direct)
s = np.linalg.norm(v)
c = np.dot(cylinder_orient, cylinder_direct)
skew = np.array([[0., -v[2], v[1]], \
[v[2], 0., -v[0]], \
[-v[1], v[0], 0.]])
# Rotate cylinder to be aligned with throat direction
R = np.eye(3) + skew + skew.dot(skew) / (1. + c)
cylinder.rotate(R, np.array([0.0, 0.0, 0.0]))
cylinder.translate((x1_t + x2_t) / 2.)
vis.add_geometry(cylinder)
print("Did not render %d/%d throats because of too small radius" % (n_zero_radius, len(basenet.throats)))
while True:
if not vis.poll_events():
break
vis.update_renderer()
if __name__ == '__main__':
main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment