forked from pyvista/pyvista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometric_objects.py
454 lines (356 loc) · 12.1 KB
/
geometric_objects.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
"""Provides an easy way of generating several geometric objects
CONTAINS
--------
vtkArrowSource
vtkCylinderSource
vtkSphereSource
vtkPlaneSource
vtkLineSource
vtkCubeSource
vtkConeSource
vtkDiskSource
vtkRegularPolygonSource
"""
import numpy as np
import vtk
import pyvista
from pyvista import PolyData
def translate(surf, center=[0., 0., 0.], direction=[1., 0., 0.]):
"""
Translates and orientates a mesh centered at the origin and
facing in the x direction to a new center and direction
"""
normx = np.array(direction)/np.linalg.norm(direction)
normz = np.cross(normx, [0, 1.0, 0.0000001])
normz /= np.linalg.norm(normz)
normy = np.cross(normz, normx)
trans = np.zeros((4, 4))
trans[:3, 0] = normx
trans[:3, 1] = normy
trans[:3, 2] = normz
trans[3, 3] = 1
surf.transform(trans)
if not np.allclose(center, [0., 0., 0.]):
surf.points += np.array(center)
def Cylinder(center=(0.,0.,0.), direction=(1.,0.,0.), radius=0.5, height=1.0,
resolution=100, **kwargs):
"""
Create the surface of a cylinder.
Parameters
----------
center : list or np.ndarray
Location of the centroid in [x, y, z]
direction : list or np.ndarray
Direction cylinder points to in [x, y, z]
radius : float
Radius of the cylinder.
height : float
Height of the cylinder.
resolution : int
Number of points on the circular face of the cylinder.
capping : bool, optional
Cap cylinder ends with polygons. Default True
Returns
-------
cylinder : pyvista.PolyData
Cylinder surface.
Examples
--------
>>> import pyvista
>>> import numpy as np
>>> cylinder = pyvista.Cylinder(np.array([1, 2, 3]), np.array([1, 1, 1]), 1, 1)
>>> cylinder.plot() # doctest:+SKIP
"""
capping = kwargs.get('capping', kwargs.get('cap_ends', True))
cylinderSource = vtk.vtkCylinderSource()
cylinderSource.SetRadius(radius)
cylinderSource.SetHeight(height)
cylinderSource.SetCapping(capping)
cylinderSource.SetResolution(resolution)
cylinderSource.Update()
surf = PolyData(cylinderSource.GetOutput())
surf.rotate_z(-90)
translate(surf, center, direction)
return surf
def Arrow(start=(0.,0.,0.), direction=(1.,0.,0.), tip_length=0.25,
tip_radius=0.1, shaft_radius=0.05, shaft_resolution=20):
"""
Create a vtk Arrow
Parameters
----------
start : np.ndarray
Start location in [x, y, z]
direction : list or np.ndarray
Direction the arrow points to in [x, y, z]
tip_length : float, optional
Length of the tip.
tip_radius : float, optional
Radius of the tip.
shaft_radius : float, optional
Radius of the shaft.
shaft_resolution : int, optional
Number of faces around the shaft
Returns
-------
arrow : pyvista.PolyData
Arrow surface.
"""
# Create arrow object
arrow = vtk.vtkArrowSource()
arrow.SetTipLength(tip_length)
arrow.SetTipRadius(tip_radius)
arrow.SetShaftRadius(shaft_radius)
arrow.SetShaftResolution(shaft_resolution)
arrow.Update()
surf = PolyData(arrow.GetOutput())
translate(surf, start, direction)
return surf
def Sphere(radius=0.5, center=(0, 0, 0), direction=(0, 0, 1), theta_resolution=30,
phi_resolution=30, start_theta=0, end_theta=360, start_phi=0, end_phi=180):
"""
Create a vtk Sphere
Parameters
----------
radius : float, optional
Sphere radius
center : np.ndarray or list, optional
Center in [x, y, z]
direction : list or np.ndarray
Direction the top of the sphere points to in [x, y, z]
theta_resolution: int , optional
Set the number of points in the longitude direction (ranging from
start_theta to end theta).
phi_resolution : int, optional
Set the number of points in the latitude direction (ranging from
start_phi to end_phi).
start_theta : float, optional
Starting longitude angle.
end_theta : float, optional
Ending longitude angle.
start_phi : float, optional
Starting latitude angle.
end_phi : float, optional
Ending latitude angle.
Returns
-------
sphere : pyvista.PolyData
Sphere mesh.
"""
sphere = vtk.vtkSphereSource()
sphere.SetRadius(radius)
sphere.SetThetaResolution(theta_resolution)
sphere.SetPhiResolution(phi_resolution)
sphere.SetStartTheta(start_theta)
sphere.SetEndTheta(end_theta)
sphere.SetStartPhi(start_phi)
sphere.SetEndPhi(end_phi)
sphere.Update()
surf = PolyData(sphere.GetOutput())
surf.rotate_y(-90)
translate(surf, center, direction)
return surf
def Plane(center=(0, 0, 0), direction=(0, 0, 1), i_size=1, j_size=1,
i_resolution=10, j_resolution=10):
"""
Create a plane
Parameters
----------
center : list or np.ndarray
Location of the centroid in [x, y, z]
direction : list or np.ndarray
Direction cylinder points to in [x, y, z]
i_size : float
Size of the plane in the i direction.
j_size : float
Size of the plane in the i direction.
i_resolution : int
Number of points on the plane in the i direction.
j_resolution : int
Number of points on the plane in the j direction.
Returns
-------
plane : pyvista.PolyData
Plane mesh
"""
planeSource = vtk.vtkPlaneSource()
planeSource.SetXResolution(i_resolution)
planeSource.SetYResolution(j_resolution)
planeSource.Update()
surf = PolyData(planeSource.GetOutput())
surf.points[:, 0] *= i_size
surf.points[:, 1] *= j_size
surf.rotate_y(-90)
translate(surf, center, direction)
return surf
def Line(pointa=(-0.5, 0., 0.), pointb=(0.5, 0., 0.), resolution=1):
"""Create a line
Parameters
----------
pointa : np.ndarray or list
Location in [x, y, z].
pointb : np.ndarray or list
Location in [x, y, z].
resolution : int
number of pieces to divide line into
"""
if np.array(pointa).size != 3:
raise TypeError('Point A must be a length three tuple of floats.')
if np.array(pointb).size != 3:
raise TypeError('Point B must be a length three tuple of floats.')
src = vtk.vtkLineSource()
src.SetPoint1(*pointa)
src.SetPoint2(*pointb)
src.SetResolution(resolution)
src.Update()
line = pyvista.wrap(src.GetOutput())
# Compute distance of every point along line
compute = lambda p0, p1: np.sqrt(np.sum((p1 - p0)**2, axis=1))
distance = compute(np.array(pointa), line.points)
line['Distance'] = distance
return line
def Cube(center=(0., 0., 0.), x_length=1.0, y_length=1.0, z_length=1.0, bounds=None):
"""Create a cube by either specifying the center and side lengths or just
the bounds of the cube. If ``bounds`` are given, all other arguments are
ignored.
Parameters
----------
center : np.ndarray or list
Center in [x, y, z].
x_length : float
length of the cube in the x-direction.
y_length : float
length of the cube in the y-direction.
z_length : float
length of the cube in the z-direction.
bounds : np.ndarray or list
Specify the bounding box of the cube. If given, all other arguments are
ignored. ``(xMin,xMax, yMin,yMax, zMin,zMax)``
"""
src = vtk.vtkCubeSource()
if bounds is not None:
if np.array(bounds).size != 6:
raise TypeError('Bounds must be given as length 6 tuple: (xMin,xMax, yMin,yMax, zMin,zMax)')
src.SetBounds(bounds)
else:
src.SetCenter(center)
src.SetXLength(x_length)
src.SetYLength(y_length)
src.SetZLength(z_length)
src.Update()
return pyvista.wrap(src.GetOutput())
def Box(bounds=(-1.,1.,-1.,1.,-1.,1.)):
"""Creates a box with solid faces for the given bounds.
Parameters
----------
bounds : np.ndarray or list
Specify the bounding box of the cube. If given, all other arguments are
ignored. ``(xMin,xMax, yMin,yMax, zMin,zMax)``
"""
return Cube(bounds=bounds)
def Cone(center=(0.,0.,0.), direction=(1.,0.,0.), height=1.0, radius=None,
capping=True, angle=None, resolution=6):
"""Create a cone
Parameters
----------
center : np.ndarray or list
Center in [x, y, z]. middle of the axis of the cone.
direction : np.ndarray or list
direction vector in [x, y, z]. orientation vector of the cone.
height : float
height along the cone in its specified direction.
radius : float
base radius of the cone
capping : bool
Turn on/off whether to cap the base of the cone with a polygon.
angle : float
The angle degrees between the axis of the cone and a generatrix.
resolution : int
number of facets used to represent the cone
"""
src = vtk.vtkConeSource()
src.SetCapping(capping)
src.SetDirection(direction)
src.SetCenter(center)
src.SetHeight(height)
# Contributed by @kjelljorner in #249:
if angle and radius:
raise Exception ("Both radius and angle specified. They are mutually exclusive.")
elif angle and not radius:
src.SetAngle(angle)
elif not angle and radius:
src.SetRadius(radius)
elif not angle and not radius:
src.SetRadius(0.5)
src.SetResolution(resolution)
src.Update()
return pyvista.wrap(src.GetOutput())
def Polygon(center=(0.,0.,0.), radius=1, normal=(0,0,1), n_sides=6):
"""
Createa a polygonal disk with a hole in the center. The disk has zero
height. The user can specify the inner and outer radius of the disk, and
the radial and circumferential resolution of the polygonal representation.
Parameters
----------
center : np.ndarray or list
Center in [x, y, z]. middle of the axis of the polygon.
radius : float
The radius of the polygon
normal : np.ndarray or list
direction vector in [x, y, z]. orientation vector of the cone.
n_sides : int
Number of sides of the polygon
"""
src = vtk.vtkRegularPolygonSource()
src.SetCenter(center)
src.SetNumberOfSides(n_sides)
src.SetRadius(radius)
src.SetNormal(normal)
src.Update()
return pyvista.wrap(src.GetOutput())
def Disc(center=(0.,0.,0.), inner=0.25, outer=0.5, normal=(0,0,1), r_res=1,
c_res=6):
"""
Createa a polygonal disk with a hole in the center. The disk has zero
height. The user can specify the inner and outer radius of the disk, and
the radial and circumferential resolution of the polygonal representation.
Parameters
----------
center : np.ndarray or list
Center in [x, y, z]. middle of the axis of the disc.
inner : flaot
The inner radius
outer : float
The outer radius
normal : np.ndarray or list
direction vector in [x, y, z]. orientation vector of the cone.
r_res: int
number of points in radius direction.
r_res: int
number of points in circumferential direction.
"""
src = vtk.vtkDiskSource()
src.SetInnerRadius(inner)
src.SetOuterRadius(outer)
src.SetRadialResolution(r_res)
src.SetCircumferentialResolution(c_res)
src.Update()
return pyvista.wrap(src.GetOutput())
def Text3D(string, depth=0.5):
""" Create 3D text from a string"""
vec_text = vtk.vtkVectorText()
vec_text.SetText(string)
extrude = vtk.vtkLinearExtrusionFilter()
extrude.SetInputConnection(vec_text.GetOutputPort())
extrude.SetExtrusionTypeToNormalExtrusion();
extrude.SetVector(0, 0, 1 )
extrude.SetScaleFactor(depth)
tri_filter = vtk.vtkTriangleFilter()
tri_filter.SetInputConnection(extrude.GetOutputPort())
tri_filter.Update()
return pyvista.wrap(tri_filter.GetOutput())
def SuperToroid(**kwargs):
"""DEPRECATED: use :func:`pyvista.ParametricSuperToroid`"""
raise RuntimeError('use `pyvista.ParametricSuperToroid` instead')
def Ellipsoid(**kwargs):
"""DEPRECATED: use :func:`pyvista.ParametricEllipsoid`"""
raise RuntimeError('use `pyvista.ParametricEllipsoid` instead')