Skip to content

Commit

Permalink
Make blender, povray, and zdog backends use mesh.unfoldPrimitives()
Browse files Browse the repository at this point in the history
This should make the broadcasting behavior of the various backends
more consistent.
  • Loading branch information
klarh committed Feb 22, 2020
1 parent 467c6e5 commit db9c4df
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 45 deletions.
8 changes: 4 additions & 4 deletions plato/draw/blender/ConvexPolyhedra.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import bpy
import itertools
from ... import geometry, math
from ... import draw
from ... import draw, geometry, math, mesh
import numpy as np

class ConvexPolyhedra(draw.ConvexPolyhedra):
Expand All @@ -22,8 +21,9 @@ def render(self, scene, suffix='', translation=(0, 0, 0),
group = bpy.data.groups.new(prim_name)
positions = math.quatrot(rotation[np.newaxis, :], self.positions)

for (i, position, orientation, color) in zip(
itertools.count(), positions, self.orientations, self.colors):
particles = zip(*mesh.unfoldProperties([
positions, self.orientations, self.colors]))
for (i, (position, orientation, color)) in enumerate(particles):
shape_name = prim_name + '_{}'.format(i)
shape = bpy.data.objects.new(shape_name, object_data=mesh)
shape.location = position
Expand Down
8 changes: 4 additions & 4 deletions plato/draw/blender/Spheres.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import bpy
import itertools
from ... import math
from ... import draw
from ... import draw, math, mesh
import numpy as np

class Spheres(draw.Spheres):
Expand All @@ -21,8 +20,9 @@ def render(self, scene, suffix='', translation=(0, 0, 0),
group = bpy.data.groups.new(prim_name)
positions = math.quatrot(rotation[np.newaxis, :], self.positions)

for (i, position, radius, color) in zip(
itertools.count(), positions, self.radii, self.colors):
particles = zip(*mesh.unfoldProperties([
positions, self.radii, self.colors]))
for (i, (position, (radius,), color)) in enumerate(particles):
shape_name = prim_name + '_{}'.format(i)
shape = bpy.data.objects.new(shape_name, object_data=shape_params)
shape.location = position
Expand Down
18 changes: 10 additions & 8 deletions plato/draw/povray/ConvexPolyhedra.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class ConvexPolyhedra(draw.ConvexPolyhedra):

def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
rotation = np.asarray(rotation)
quat_magnitude = np.linalg.norm(self.orientations, axis=-1, keepdims=True)
(positions, orientations, colors) = pmesh.unfoldProperties([
self.positions, self.orientations, self.colors])
quat_magnitude = np.linalg.norm(orientations, axis=-1, keepdims=True)

lines = []

Expand All @@ -39,7 +41,7 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
lines.append(shapedef)

qs = pmath.quatquat(rotation[np.newaxis, :],
self.orientations/quat_magnitude)
orientations/quat_magnitude)
rotmat = np.array([[1 - 2*qs[:, 2]**2 - 2*qs[:, 3]**2,
2*(qs[:, 1]*qs[:, 2] - qs[:, 3]*qs[:, 0]),
2*(qs[:, 1]*qs[:, 3] + qs[:, 2]*qs[:, 0])],
Expand All @@ -52,9 +54,9 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
rotmat = rotmat.transpose([2, 1, 0]).reshape((-1, 9))
rotmat[:] *= quat_magnitude[:, 0, np.newaxis]**2

positions = pmath.quatrot(rotation[np.newaxis, :], self.positions)
positions = pmath.quatrot(rotation[np.newaxis, :], positions)

for (p, m, a) in zip(positions, rotmat, 1 - self.colors[:, 3]):
for (p, m, a) in zip(positions, rotmat, 1 - colors[:, 3]):
args = [shapeName] + m.tolist() + p.tolist() + [0, 0, 0, a]
lines.append('object {{{} matrix <{},{},{},{},{},{},{},{},{},'
'{},{},{}> pigment {{color <{},{},{}> transmit {} }}}}'.format(
Expand All @@ -71,7 +73,7 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
shapedef = '#declare {} = {}'.format(shapeName, meshStr)
lines.append(shapedef)

qs = pmath.quatquat(rotation[np.newaxis, :], self.orientations/quat_magnitude)
qs = pmath.quatquat(rotation[np.newaxis, :], orientations/quat_magnitude)
rotmat = np.array([[1 - 2*qs[:, 2]**2 - 2*qs[:, 3]**2,
2*(qs[:, 1]*qs[:, 2] - qs[:, 3]*qs[:, 0]),
2*(qs[:, 1]*qs[:, 3] + qs[:, 2]*qs[:, 0])],
Expand All @@ -84,10 +86,10 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
rotmat = rotmat.transpose([2, 1, 0]).reshape((-1, 9))
rotmat *= quat_magnitude[:, 0, np.newaxis]**2

positions = pmath.quatrot(rotation[np.newaxis, :], self.positions)
positions = pmath.quatrot(rotation[np.newaxis, :], positions)

for (p, m, (r, g, b), a) in zip(positions, rotmat, self.colors[:, :3],
1 - self.colors[:, 3]):
for (p, m, (r, g, b), a) in zip(positions, rotmat, colors[:, :3],
1 - colors[:, 3]):
args = [shapeName] + m.tolist() + p.tolist() + [r, g, b, a]
lines.append('object {{{} matrix <{},{},{},{},{},{},{},{},{},'
'{},{},{}> pigment {{color <{},{},{}> transmit '
Expand Down
10 changes: 6 additions & 4 deletions plato/draw/povray/ConvexSpheropolyhedra.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class ConvexSpheropolyhedra(draw.ConvexSpheropolyhedra):

def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
rotation = np.asarray(rotation)
quat_magnitude = np.linalg.norm(self.orientations, axis=-1, keepdims=True)
(positions, orientations, colors) = pmesh.unfoldProperties([
self.positions, self.orientations, self.colors])
quat_magnitude = np.linalg.norm(orientations, axis=-1, keepdims=True)

lines = []
decomp = geometry.convexDecomposition(self.vertices)
Expand All @@ -32,7 +34,7 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
shapeName, '\n'.join(elt for elt in [meshStr] + edges + spheres))
lines.append(shapedef)

qs = pmath.quatquat(rotation[np.newaxis, :], self.orientations/quat_magnitude)
qs = pmath.quatquat(rotation[np.newaxis, :], orientations/quat_magnitude)
rotmat = np.array([[1 - 2*qs[:, 2]**2 - 2*qs[:, 3]**2,
2*(qs[:, 1]*qs[:, 2] - qs[:, 3]*qs[:, 0]),
2*(qs[:, 1]*qs[:, 3] + qs[:, 2]*qs[:, 0])],
Expand All @@ -45,9 +47,9 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
rotmat = rotmat.transpose([2, 1, 0]).reshape((-1, 9))
rotmat[:] *= quat_magnitude[:, 0, np.newaxis]**2

positions = pmath.quatrot(rotation[np.newaxis, :], self.positions)
positions = pmath.quatrot(rotation[np.newaxis, :], positions)

for (p, m, c) in zip(positions, rotmat, self.colors[:, :3]):
for (p, m, c) in zip(positions, rotmat, colors[:, :3]):
args = [shapeName] + m.tolist() + p.tolist() + c.tolist()
lines.append('object {{{} matrix <{},{},{},{},{},{},{},{},{},'
'{},{},{}> pigment {{color <{},{},{}>}}}}'.format(
Expand Down
10 changes: 6 additions & 4 deletions plato/draw/povray/Ellipsoids.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import numpy as np
import rowan
from ... import draw
from ... import draw, mesh

class Ellipsoids(draw.Ellipsoids):
__doc__ = draw.Ellipsoids.__doc__

def render(self, rotation=(1, 0, 0, 0), **kwargs):
positions = rowan.rotate(rotation, self.positions)
(positions, orientations, colors) = mesh.unfoldProperties([
self.positions, self.orientations, self.colors])
positions = rowan.rotate(rotation, positions)
orientations = rowan.multiply(
rotation, rowan.normalize(self.orientations))
rotation, rowan.normalize(orientations))
rotations = np.degrees(rowan.to_euler(orientations))

lines = []
for (pos, rot, col, alpha) in zip(
positions, rotations, self.colors[:, :3], 1 - self.colors[:, 3]):
positions, rotations, colors[:, :3], 1 - colors[:, 3]):
lines.append('sphere {{ '
'0, 1 scale<{a}, {b}, {c}> '
'rotate <{rot[2]}, {rot[1]}, {rot[0]}> '
Expand Down
8 changes: 4 additions & 4 deletions plato/draw/povray/Mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
vertex_colors = np.tile(
vertex_colors, (int(np.ceil(len(verts)/len(vertex_colors))), 1))

positions = self.positions
shape_colors = self.shape_colors
(positions, orientations, shape_colors) = mesh.unfoldProperties([
self.positions, self.orientations, self.shape_colors])
if len(shape_colors) < len(positions):
shape_colors = np.tile(
shape_colors, (int(np.ceil(len(positions)/len(shape_colors))), 1))
Expand All @@ -31,8 +31,8 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', **kwargs):
vertex_normals = mesh.computeNormals_(verts, self.indices)
vertex_normal_text = ' '.join('<{},{},{}>'.format(*v) for v in vertex_normals)

quat_magnitude = np.linalg.norm(self.orientations, axis=-1, keepdims=True)
qs = pmath.quatquat(rotation[np.newaxis, :], self.orientations/quat_magnitude)
quat_magnitude = np.linalg.norm(orientations, axis=-1, keepdims=True)
qs = pmath.quatquat(rotation[np.newaxis, :], orientations/quat_magnitude)
rotmats = np.array([[1 - 2*qs[:, 2]**2 - 2*qs[:, 3]**2,
2*(qs[:, 1]*qs[:, 2] - qs[:, 3]*qs[:, 0]),
2*(qs[:, 1]*qs[:, 3] + qs[:, 2]*qs[:, 0])],
Expand Down
12 changes: 8 additions & 4 deletions plato/draw/povray/SphereUnions.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import numpy as np
from ... import draw
from ... import math
from ... import mesh

class SphereUnions(draw.SphereUnions):
__doc__ = draw.SphereUnions.__doc__

def render(self, rotation=(1, 0, 0, 0), **kwargs):
rotation = np.asarray(rotation)

positions = np.tile(self.positions[:, np.newaxis, :], (1, len(self.points), 1))
positions += math.quatrot(self.orientations[:, np.newaxis], self.points[np.newaxis])
(positions, orientations) = mesh.unfoldProperties([
self.positions, self.orientations])

radii = np.repeat(self.radii[np.newaxis, :], len(self.positions),axis=0)
colors = np.repeat(self.colors[np.newaxis, :], len(self.positions), axis=0)
positions = np.tile(positions[:, np.newaxis, :], (1, len(self.points), 1))
positions += math.quatrot(orientations[:, np.newaxis], self.points[np.newaxis])

radii = np.repeat(self.radii[np.newaxis, :], len(positions),axis=0)
colors = np.repeat(self.colors[np.newaxis, :], len(positions), axis=0)

colors_reshaped = colors.reshape(-1,4)

Expand Down
9 changes: 6 additions & 3 deletions plato/draw/povray/Spheres.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import numpy as np
from ... import draw
from ... import math
from ... import mesh

class Spheres(draw.Spheres):
__doc__ = draw.Spheres.__doc__

def render(self, rotation=(1, 0, 0, 0), **kwargs):
rotation = np.asarray(rotation)

positions = math.quatrot(rotation[np.newaxis, :], self.positions)
(positions, radii, colors) = mesh.unfoldProperties([
self.positions, self.radii, self.colors])
positions = math.quatrot(rotation[np.newaxis, :], positions)

lines = []
for (p, r, c, a) in zip(positions, self.radii, self.colors[:, :3],
1 - self.colors[:, 3]):
for (p, r, c, a) in zip(positions, radii[:, 0], colors[:, :3],
1 - colors[:, 3]):
args = p.tolist() + [r] + c.tolist() + [a]
lines.append('sphere {{<{},{},{}> {} pigment {{color '
'<{},{},{}> transmit {}}}}}'.format(*args))
Expand Down
7 changes: 4 additions & 3 deletions plato/draw/zdog/Lines.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ... import draw
from ... import mesh

class Lines(draw.Lines):
__doc__ = draw.Lines.__doc__
Expand All @@ -9,10 +10,10 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', illo_id='illo',
# and z is toward you
lines = []

particles = zip(
particles = zip(*mesh.unfoldProperties([
self.start_points*(1, -1, 1), self.end_points*(1, -1, 1),
self.widths, self.colors*255)
for i, (start, end, width, color) in enumerate(particles):
self.widths, self.colors*255]))
for i, (start, end, (width,), color) in enumerate(particles):
path = ', '.join('{{x: {}, y: {}, z: {}}}'.format(*v) for v in [start, end])

(r, g, b) = map(int, color[:3])
Expand Down
8 changes: 4 additions & 4 deletions plato/draw/zdog/Spheres.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import collections
import itertools
import numpy as np
from ... import draw
from ... import draw, mesh
from ...draw import internal

LightInfo = collections.namedtuple(
Expand Down Expand Up @@ -34,9 +34,9 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', illo_id='illo',

light_info.append(LightInfo(normal, mag))

particles = zip(
self.positions*(1, -1, 1), self.diameters, self.colors*255)
for i, (position, diameter, color) in enumerate(particles):
particles = zip(*mesh.unfoldProperties([
self.positions*(1, -1, 1), self.diameters, self.colors*255]))
for i, (position, (diameter,), color) in enumerate(particles):
group_index = 'sphere_{}_{}'.format(name_suffix, i)

lines.append("""
Expand Down
6 changes: 3 additions & 3 deletions plato/draw/zdog/internal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import rowan
from ... import geometry
from ... import geometry, mesh

class PolyhedronRenderer:
def render(self, rotation=(1, 0, 0, 0), name_suffix='', illo_id='illo',
Expand Down Expand Up @@ -31,9 +31,9 @@ def render(self, rotation=(1, 0, 0, 0), name_suffix='', illo_id='illo',
orientations_euler = rowan.to_euler(
self.orientations, convention='xyz', axis_type='intrinsic')

particles = zip(
particles = zip(*mesh.unfoldProperties([
self.positions*(1, -1, 1), self.orientations,
-orientations_euler, self.colors*255)
-orientations_euler, self.colors*255]))
for i, (position, orientation, eulers, color) in enumerate(particles):
group_index = 'convexPoly_{}_{}'.format(name_suffix, i)

Expand Down

0 comments on commit db9c4df

Please sign in to comment.