Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated the viewer to plot elevation points as square pixels as it is characterized internally
  • Loading branch information
daminton committed Feb 19, 2022
1 parent 528a118 commit b273acb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
78 changes: 62 additions & 16 deletions python/ctem/ctem/ctem_viewer_3d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import os
import open3d
import ctem_io_readers
from ctem import ctem_io_readers
import os


Expand Down Expand Up @@ -57,31 +57,76 @@ def ctem2mesh(self, surface_file):
# Build mesh grid
s = parameters['gridsize']
pix = parameters['pix']
ygrid, xgrid = np.mgrid[-s / 2:s / 2, -s / 2:s / 2] * pix
ygrid, xgrid = np.mgrid[-s/2:s/2, -s/2:s/2] * pix
y0, x0 = ygrid - pix/2, xgrid - pix/2
y1, x1 = ygrid - pix/2, xgrid + pix/2
y2, x2 = ygrid + pix/2, xgrid + pix/2
y3, x3 = ygrid + pix/2, xgrid - pix/2


xvals = xgrid.flatten()
yvals = ygrid.flatten()
zvals = self.dem.flatten()
xvals = np.append(
np.append(
np.append(x0.flatten(),
x1.flatten()),
x2.flatten()),
x3.flatten())
yvals = np.append(
np.append(
np.append(y0.flatten(),
y1.flatten()),
y2.flatten()),
y3.flatten())
zvals = np.append(
np.append(
np.append(self.dem.flatten(),
self.dem.flatten()),
self.dem.flatten()),
self.dem.flatten())
verts = np.array((xvals, yvals, zvals)).T
faces = np.empty([2 * (s - 1) ** 2, 3], dtype=np.int64)
for j in range(s - 1):
for i in range(s - 1):
idx = (s - 1) * j + i
# faces[idx,:] = [ j*s+i, j*s+i+1, (j+1)*s+i+1, (j+1)*s+i ]
faces[idx, :] = [j * s + i, j * s + i + 1, (j + 1) * s + i + 1]
idx += (s - 1) ** 2
faces[idx, :] = [(j + 1) * s + i + 1, (j + 1) * s + i, j * s + i ]
nface_triangles = 10
faces = np.full([nface_triangles*s**2, 3], -1, dtype=np.int64)
for j in range(s):
for i in range(s):
i0 = s*j + i
i1 = i0 + s**2
i2 = i1 + s**2
i3 = i2 + s**2

fidx = np.arange(nface_triangles*i0,nface_triangles*(i0+1))
# Make the two top triangles
faces[fidx[0],:] = [i0, i1, i2]
faces[fidx[1],:] = [i0, i2, i3]
# Make the two west side triangles
if i > 0:
faces[fidx[0],:] = [i0, i3, i3-1]
faces[fidx[1],:] = [i0, i3-1, i0-1]
# Make the two south side triangles
if j > 0:
faces[fidx[4],:] = [i1, i0, i3-s ]
faces[fidx[5],:] = [i1, i3-s, i2-s]
# Make the two east side triangles
if i < (s - 1):
faces[fidx[6],:] = [i2, i1, i0+1]
faces[fidx[7],:] = [i2, i0+1, i3+1]
#Make the two north side triangles
if j < (s -1):
faces[fidx[8],:] = [i3, i2, i1+s]
faces[fidx[9],:] = [i3, i1+s, i0+s]
nz = faces[:,0] != -1
f2 = faces[nz,:]
self.mesh.vertices = open3d.utility.Vector3dVector(verts)
self.mesh.triangles = open3d.utility.Vector3iVector(faces)
self.mesh.triangles = open3d.utility.Vector3iVector(f2)
self.mesh.compute_vertex_normals()

self.mesh.compute_triangle_normals()

return

def visualize(self):
vis = open3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(self.mesh)

opt = vis.get_render_option()
opt.background_color = np.asarray([0, 0, 0])

# zmax = np.amax(surf.dem)
# zmin = np.amin(surf.dem)
Expand All @@ -99,6 +144,7 @@ def visualize(self):
surf = polysurface()
surf.ctem2mesh(surface_file="surface_dem.dat")
surf.visualize()
#open3d.visualization.draw_geometries_with_editing([surf.mesh])



Expand Down
13 changes: 7 additions & 6 deletions python/ctem/tests/viewer3d/polytest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy as np
from ctem import ctem_viewer_3d

surf = ctem_viewer_3d.surf()
surf.io_read_ply("216kleopatra-mesh.ply")
v = np.asarray(surf.mesh)
t = np.asarray(surf.mesh)
surf.render()
import open3d
surf = ctem_viewer_3d.polysurface()
#surf.io_read_ply("216kleopatra-mesh.ply")
surf.mesh = open3d.geometry.TriangleMesh.create_box()
v = np.asarray(surf.mesh.vertices)
t = np.asarray(surf.mesh.triangles)
surf.visualize()

0 comments on commit b273acb

Please sign in to comment.