-
Notifications
You must be signed in to change notification settings - Fork 6
/
Billboards.py
167 lines (131 loc) · 5.21 KB
/
Billboards.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# Author: Nick R. Rypkema ([email protected])
# License: MIT
'''
The LightField Visualizer - a VTK/PyQt4-based lightweight field robotics viewer
-------------------------------------------------------------------------------
billboard classes
'''
''' standard libs '''
import math
import warnings
import numpy as np
''' VTK '''
import vtk
class TextBillboard(vtk.vtkTextActor):
''' a fixed-size text label (i.e. a 2D label in the 3D world) '''
def __init__(self, text, text_size=None):
self.vtkTransform = None
self.pos = vtk.vtkCoordinate()
self.SetInput(text)
self.GetTextProperty().BoldOn()
self.GetTextProperty().SetColor(0.0,0.0,0.0)
self.GetTextProperty().SetFontFamilyToArial()
if text_size is not None:
self.GetTextProperty().SetFontSize(text_size)
self.pos.SetCoordinateSystemToWorld()
coordView = vtk.vtkCoordinate()
coordView.SetReferenceCoordinate(self.pos)
coordView.SetCoordinateSystemToViewport()
mapper = vtk.vtkPolyDataMapper2D()
mapper.SetTransformCoordinate(coordView)
self.SetMapper(mapper)
def SetPosition(self, x, y, z):
self.pos.SetValue(x, y, z)
def SetOrientation(self, roll, pitch, yaw):
prop = self.GetTextProperty()
prop.SetOrientation(roll)
def ApplyTransformation(self):
mat = self.vtkTransform.GetMatrix()
a = np.zeros((4,4))
for r in xrange(4):
for c in xrange(4):
a[r][c] = mat.GetElement(r, c)
self.SetPosition(a[0,3],a[1,3],a[2,3])
def SetUserTransform(self, transform):
self.vtkTransform = transform
self.ApplyTransformation()
def GetUserTransform(self):
return self.vtkTransform
class ImageBillboard(vtk.vtkTexturedActor2D):
''' a fixed-size billboard for images (i.e. a 2D sprite in the 3D world) '''
def __init__(self, image_path, width=None, height=None):
self.vtkTransform = None
self.pos = vtk.vtkCoordinate()
if image_path[-4:].lower() == '.jpg' or image_path[-4:].lower() == '.jpeg':
reader = vtk.vtkJPEGReader()
reader.SetFileName(image_path)
reader.Update()
elif image_path[-4:].lower() == '.png':
reader = vtk.vtkPNGReader()
reader.SetFileName(image_path)
reader.Update()
texture = vtk.vtkTexture()
texture.SetInputConnection(reader.GetOutputPort())
image_dims = reader.GetOutput().GetDimensions()
self.SetTexture(texture)
if width is not None and height is not None:
self.construct(width, height)
else:
self.construct(image_dims[0], image_dims[1])
def construct(self, width, height):
points = vtk.vtkPoints()
points.InsertNextPoint([-float(width)/2, -float(height)/2, 0.0])
points.InsertNextPoint([float(width)/2, -float(height)/2, 0.0])
points.InsertNextPoint([float(width)/2, float(height)/2, 0.0])
points.InsertNextPoint([-float(width)/2, float(height)/2, 0.0])
polys = vtk.vtkCellArray()
poly = vtk.vtkPolygon()
poly.GetPointIds().SetNumberOfIds(4)
poly.GetPointIds().SetId(0,0)
poly.GetPointIds().SetId(1,1)
poly.GetPointIds().SetId(2,2)
poly.GetPointIds().SetId(3,3)
polys.InsertNextCell(poly)
self.data = vtk.vtkPolyData()
self.data.SetPoints(points)
self.data.SetPolys(polys)
texCoords = vtk.vtkFloatArray()
texCoords.SetNumberOfComponents(2)
texCoords.SetName("TextureCoordinates")
texCoords.InsertNextTuple((0.0, 0.0))
texCoords.InsertNextTuple((1.0, 0.0))
texCoords.InsertNextTuple((1.0, 1.0))
texCoords.InsertNextTuple((0.0, 1.0))
self.data.GetPointData().SetTCoords(texCoords)
self.pos.SetCoordinateSystemToWorld()
coordView = vtk.vtkCoordinate()
coordView.SetReferenceCoordinate(self.pos)
coordView.SetCoordinateSystemToViewport()
mapper = vtk.vtkPolyDataMapper2D()
mapper.SetInput(self.data)
mapper.SetTransformCoordinate(coordView)
self.SetMapper(mapper)
def SetPosition(self, x, y, z):
self.pos.SetValue(x, y, z)
def SetOrientation(self, roll, pitch, yaw):
t = vtk.vtkTransform()
t.RotateZ(roll)
tf = vtk.vtkTransformPolyDataFilter()
tf.SetInput(self.data)
tf.SetTransform(t)
tf.Update()
coordView = vtk.vtkCoordinate()
coordView.SetReferenceCoordinate(self.pos)
coordView.SetCoordinateSystemToViewport()
mapper = vtk.vtkPolyDataMapper2D()
mapper.SetInputConnection(tf.GetOutputPort())
mapper.SetTransformCoordinate(coordView)
self.SetMapper(mapper)
def ApplyTransformation(self):
mat = self.vtkTransform.GetMatrix()
a = np.zeros((4,4))
for r in xrange(4):
for c in xrange(4):
a[r][c] = mat.GetElement(r, c)
self.SetPosition(a[0,3],a[1,3],a[2,3])
def SetUserTransform(self, transform):
self.vtkTransform = transform
self.ApplyTransformation()
def GetUserTransform(self):
return self.vtkTransform