Python mayavi : How to draw spheres at random positions in 3D space - python

This is a code for generating random sized spheres with mayavi,
I want to make the spheres to be connected with each other by the surface or with a bond line:
Spheres must be at random positions in 3D space
Spheres must be with the same radius
from mayavi import mlab
import numpy as np
[phi,theta] = np.mgrid[0:2*np.pi:12j,0:np.pi:12j]
x = np.cos(phi)*np.sin(theta)
y = np.sin(phi)*np.sin(theta)
z = np.cos(theta)
def plot_sphere(p):
r,a,b,c = p
r=1
return mlab.mesh(r*x+a, r*y+b, r*z )
for k in range(8):
c = np.random.rand(4)
c[0] /= 10.
plot_sphere(c)
mlab.show()

From sphere equation:
So when passing arguments to mlab.mesh we would like to set [x_0, y_0, z_0] for each sphere such as they are at different positions from the axis.
The problem was that the numbers generated by np.random.rand(4) are random, but not distinct.
Let's modify so that the arguments [x_0, y_0, z_0] are random and distinct:
We use sample to get distinct index numbers in a cube
We convert using index_to_3d the index to an (x, y, z) coordinates
The radius, r, can be adjusted to have more or less spacing between the spheres.
Spheres at 3D space
Code:
import random
from itertools import product
from mayavi import mlab
import numpy as np
[phi, theta] = np.mgrid[0:2 * np.pi:12j, 0:np.pi:12j]
x = np.cos(phi) * np.sin(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(theta)
def plot_sphere(x_0, y_0, z_0):
r = 0.5
return mlab.mesh(r * x + x_0, r * y + y_0, r * z + z_0)
SPHERES_NUMBER = 200
CUBE_SIZE = 10
def index_to_3d(i, SIZE):
z = i // (SIZE * SIZE)
i -= (z * SIZE * SIZE)
y = i // SIZE
x = i % SIZE
return x, y, z
random_tuples = [index_to_3d(i, CUBE_SIZE) for i in random.sample(range(CUBE_SIZE ** 3), SPHERES_NUMBER)]
for k in range(SPHERES_NUMBER):
x_0, y_0, z_0 = random_tuples[k]
plot_sphere(x_0, y_0, z_0)
mlab.show()
Output:
Spheres cluster
Let's utilize gauss to create coordinates for the cluster points.
Code:
import random
from itertools import product
from mayavi import mlab
import numpy as np
[phi, theta] = np.mgrid[0:2 * np.pi:12j, 0:np.pi:12j]
x = np.cos(phi) * np.sin(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(theta)
def plot_sphere(x_0, y_0, z_0):
r = 0.5
return mlab.mesh(r * x + x_0, r * y + y_0, r * z + z_0)
SPHERES_NUMBER = 200
def create_cluster(CLUSTER_SIZE):
means_and_deviations = [(1, 1.5), (1, 1.5), (1, 1.5)]
def generate_point(means_and_deviations):
return tuple(random.gauss(mean, deviation) for mean, deviation in means_and_deviations)
cluster_points = set()
while len(cluster_points) < CLUSTER_SIZE:
cluster_points.add(generate_point(means_and_deviations))
return list(cluster_points)
cluster_points = create_cluster(SPHERES_NUMBER)
for k in range(SPHERES_NUMBER):
x_0, y_0, z_0 = cluster_points[k]
plot_sphere(x_0, y_0, z_0)
mlab.show()
Output:

What about just using the mayavi points3d function? By default the mode parameter is set to sphere and you can set the diameter by using the scale_factor parameter. You can also increase the resolution of the sphere by varying the resolution parameter.
Here is the code:
def draw_sphere(
center_coordinates,
radius,
figure_title,
color,
background,
foreground
):
sphere = mlab.figure(figure_title)
sphere.scene.background = background
sphere.scene.foreground = foreground
mlab.points3d(
center_coordinates[0],
center_coordinates[1],
center_coordinates[2],
color=color,
resolution=256,
scale_factor=2*radius,
figure=sphere
)
Regarding the connected with each other by the surface issue, your explanation is poor. Maybe you mean just tangent spheres, but I would need more details.

Related

how to create a square on top of a given Vector. This square should be orthogonal to the given vector

Programming in Python (Blender):
I want to create a square and print all vertices (A;B;C;D) into my console on top of a given Vector. The square should be orthogonal to this vector, like this:
def create_verts_around_point(radius, vert):
# given Vector
vec = np.array([vert[0], vert[1], vert[2]])
# side_length of square
side_length = radius
# Vctor x-direction (1,0,0)
x_vec = np.array([1,0,0])
# Vekctor y-direction (0,1,0)
y_vec = np.array([0,1,0])
# Vector z-direction (0,0,1)
z_vec = np.array([0,0,1])
p1 = vec + (side_length/2) * x_vec + (side_length/2) * y_vec + (side_length/2) * z_vec
p2 = vec - (side_length/2) * x_vec + (side_length/2) * y_vec + (side_length/2) * z_vec
p3 = vec - (side_length/2) * x_vec - (side_length/2) * y_vec + (side_length/2) * z_vec
p4 = vec + (side_length/2) * x_vec - (side_length/2) * y_vec + (side_length/2) * z_vec
But my output looks like this in the end (Square is always parallel to my x-axis and y-axis):
I don't think you're really thinking about this problem in 3D, but see if this is close.
I create a square, perpendicular to the X axis. I then rotate that square based on the angles in x, y, and z. I then position the square at the end of the vector and plot it. I add plot points for the origin and the end of the vector, and I duplicate the last point in the square do it draws all the lines.
import math
import numpy as np
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
def create_verts_around_point(sides, vert):
x0, y0, z0 = vert
# Here is the unrotated square.
half = sides/2
square = [
[0, -half,-half],
[0, -half, half],
[0, half, half],
[0, half,-half],
]
# Now find the rotation in each direction.
thetax = math.atan2( z0, y0 )
thetay = math.atan2( z0, x0 )
thetaz = math.atan2( y0, x0 )
# Now rotate the cube, first in x.
cubes = []
txcos = math.cos(thetax)
txsin = math.sin(thetax)
tycos = math.cos(thetay)
tysin = math.sin(thetay)
tzcos = math.cos(thetaz)
tzsin = math.sin(thetaz)
for x,y,z in square:
x,y,z = (x, y * txcos - z * txsin, y * txsin + z * txcos)
x,y,z = (x * txcos - z * txsin, y, x * txsin + z * txcos)
x,y,z = (x * txcos - y * txsin, x * txsin + y * txcos, z)
cubes.append( (x0+x, y0+y, z0+z) )
return cubes
point = (10,10,10)
square = create_verts_around_point(5, point)
points = [(0,0,0),point] + square + [square[0]]
x = [p[0] for p in points]
y = [p[1] for p in points]
z = [p[2] for p in points]
ax = plt.figure().add_subplot(111, projection='3d')
ax.plot( x, y, z )
plt.show()
Output:

Holoviz Polygon orientation settings

I am using holoviews+bokeh, and I would like to encircle my scatter plot data with a measure of standard deviation. Unfortunately I can't seem to get the orientation setting right. I am confused by the available descriptions:
Orientation in the Cartesian coordinate system, the
counterclockwise angle in radians between the first axis and the
horizontal
and
you can set the orientation (in radians, rotating anticlockwise)
My script and data example:
def create_plot(x, y, nstd=5):
x, y = np.asarray(x), np.asarray(y)
cov_matrix = np.cov([x, y])
eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
order = eigenvalues.argsort()[0]
angle = np.arctan2(eigenvectors[1, order], eigenvectors[1, order])
x0 = np.mean(x)
y0 = np.mean(y)
x_dir = np.cos(angle) * x - np.sin(angle) * y
y_dir = np.sin(angle) * x + np.cos(angle) * y
w = nstd * np.std(x_dir)
h = nstd * np.std(y_dir)
return hv.Ellipse(x0, y0, (w, h), orientation=-angle) * hv.Scatter((x, y))
c2x = np.random.normal(loc=-2, scale=0.6, size=200)
c2y = np.random.normal(loc=-2, scale=0.1, size=200)
combined = create_plot(c2x, c2y)
combined.opts(shared_axes=False)
Here is a solution, which draws Ellipse around the data. You math is just simplified.
import numpy as np
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
x = np.random.normal(loc=-2, scale=0.6, size=200)
y = np.random.normal(loc=-2, scale=0.1, size=200)
def create_plot(x, y, nstd=5):
x, y = np.asarray(x), np.asarray(y)
x0 = np.mean(x)
y0 = np.mean(y)
w = np.std(x)*nstd
h = np.std(y)*nstd
return hv.Ellipse(x0, y0, (w, h)) * hv.Scatter((x, y))
combined = create_plot(c2x, c2y)
combined.opts()
This gives you a plot which looks like a circle. To make it more visiable that it is a Ellipse your could genereate the plot calling
def hook(plot, element):
plot.handles['x_range'].start = -4
plot.handles['x_range'].end = 0
plot.handles['y_range'].start = -2.5
plot.handles['y_range'].end = -1
combined.opts(hooks=[hook])
which set fixed ranges and deactivates the auto focus.
In your example w and h were nearly the same, that means, you drawed a cercle. The orientation didn't have any effect. With the code above you can turn the Ellipse like
hv.Ellipse(x0, y0, (w, h), orientation=np.pi/2)
to see that it is working, but there is no need to do it anymore.

How to smooth pyvista.StructuredGrid?

Here is a Hopf torus made in Python with PyVista:
import numpy as np
import pyvista as pv
A = 0.44
n = 3
def Gamma(t):
alpha = np.pi/2 - (np.pi/2-A)*np.cos(n*t)
beta = t + A*np.sin(2*n*t)
return np.array([
np.sin(alpha) * np.cos(beta),
np.sin(alpha) * np.sin(beta),
np.cos(alpha)
])
def HopfInverse(p, phi):
return np.array([
(1+p[2])*np.cos(phi),
p[0]*np.sin(phi) - p[1]*np.cos(phi),
p[0]*np.cos(phi) + p[1]*np.sin(phi),
(1+p[2])*np.sin(phi)
]) / np.sqrt(2*(1+p[2]))
def Stereo(q):
return 2*q[0:3] / (1-q[3])
def F(t, phi):
return Stereo(HopfInverse(Gamma(t), phi))
angle = np.linspace(0, 2 * np.pi, 300)
theta, phi = np.meshgrid(angle, angle)
x, y, z = F(theta, phi)
# Display the mesh
grid = pv.StructuredGrid(x, y, z)
grid.plot(smooth_shading=True)
The color is not entirely smooth: on the lobe at the bottom right, you can see a line which separates pale gray and dark gray. How to get rid of this line?
I think what's going on here is that there's no connectivity information where the two ends of your structured grid meet. One way to fix this is to turn your grid into a PolyData using the extract_geometry() method, and then using clean with a larger tolerance. This will force pyvista to realise that there's a seam in the mesh where points are doubled, causing the points to be merged and the seam closed:
import numpy as np
import pyvista as pv
A = 0.44
n = 3
def Gamma(t):
alpha = np.pi/2 - (np.pi/2-A)*np.cos(n*t)
beta = t + A*np.sin(2*n*t)
return np.array([
np.sin(alpha) * np.cos(beta),
np.sin(alpha) * np.sin(beta),
np.cos(alpha)
])
def HopfInverse(p, phi):
return np.array([
(1+p[2])*np.cos(phi),
p[0]*np.sin(phi) - p[1]*np.cos(phi),
p[0]*np.cos(phi) + p[1]*np.sin(phi),
(1+p[2])*np.sin(phi)
]) / np.sqrt(2*(1+p[2]))
def Stereo(q):
return 2*q[0:3] / (1-q[3])
def F(t, phi):
return Stereo(HopfInverse(Gamma(t), phi))
angle = np.linspace(0, 2 * np.pi, 300)
theta, phi = np.meshgrid(angle, angle)
x, y, z = F(theta, phi)
# Display the mesh, show seam
grid = pv.StructuredGrid(x, y, z)
grid.plot(smooth_shading=True)
# convert to PolyData and clean to remove the seam
cleaned_poly = grid.extract_geometry().clean(tolerance=1e-6)
cleaned_poly.plot(smooth_shading=True)
Your mileage for the tolerance parameter may vary.
Just as a piece of trivia, we can visualize the original seam by extracting the feature edges of your original grid:
grid.extract_feature_edges().plot()
These curves correspond to the open edges in your original grid:
>>> grid.extract_surface().n_open_edges
1196
Since your surface is closed and watertight, it should have 0 open edges:
>>> cleaned_poly.n_open_edges
0

Python program to create sphere coordinates not working

I am trying to generate points that lies on the surface of a sphere centered on (0,0) in python.
# r - the radius of the sphere
def createSphere(r):
lst = []
for z in range(-r, r+1):
r_ = r - abs(z)
if r_ == 0:
lst.append((0,0,r*np.sign(z)))
else:
for d in range(r_):
lst.append((r_ * cos(d * (360/float(r_))), r_ * sin(d * 360/float(r_))), z) )
return lst
It will return a list [(x1,y1,z1),...].
This is how the result looks like:
The surface isn't smooth, and it looks kinda like a cube with extra sharp corners.
Does anyone know whats wrong?
Thanks
Use the standard spherical to cartesian coordinate transformation:
import math
pi = math.pi
sin = math.sin
cos = math.cos
def createSphere(r, N=10):
lst = []
thetas = [(2*pi*i)/N for i in range(N)]
phis = [(pi*i)/N for i in range(N)]
for theta in thetas:
for phi in phis:
x = r * sin(phi) * cos(theta)
y = r * sin(phi) * sin(theta)
z = r * cos(phi)
lst.append((x, y, z))
return lst
Per the comments below: If you wish to vary the number of points depending on the height (or phi), you could let thetas depend on phi:
def createSphere(r, N=10):
lst = []
for phi in [(pi*i)/(N-1) for i in range(N)]:
M = int(sin(phi)*(N-1))+1
for theta in [(2*pi*i)/M for i in range(M)]:
x = r * sin(phi) * cos(theta)
y = r * sin(phi) * sin(theta)
z = r * cos(phi)
lst.append((x, y, z))
return lst
Above, the key line is
M = int(sin(phi)*(N-1))+1
M will equal 1 when phi is 0 or pi, and it will equal N when phi equals pi/2 (at the "equator"). Note that this is just one possible way to define M. Instead of using sin, you could instead define a piecewise linear function with the same boundary values, for example...

Python: heat density plot in a disk

My goal is to make a density heat map plot of sphere in 2D. The plotting code below the line works when I use rectangular domains. However, I am trying to use the code for a circular domain. The radius of sphere is 1. The code I have so far is:
from pylab import *
import numpy as np
from matplotlib.colors import LightSource
from numpy.polynomial.legendre import leggauss, legval
xi = 0.0
xf = 1.0
numx = 500
yi = 0.0
yf = 1.0
numy = 500
def f(x):
if 0 <= x <= 1:
return 100
if -1 <= x <= 0:
return 0
deg = 1000
xx, w = leggauss(deg)
L = np.polynomial.legendre.legval(xx, np.identity(deg))
integral = (L * (f(x) * w)[None,:]).sum(axis = 1)
c = (np.arange(1, 500) + 0.5) * integral[1:500]
def r(x, y):
return np.sqrt(x ** 2 + y ** 2)
theta = np.arctan2(y, x)
x, y = np.linspace(0, 1, 500000)
def T(x, y):
return (sum(r(x, y) ** l * c[:,None] *
np.polynomial.legendre.legval(xx, identity(deg)) for l in range(1, 500)))
T(x, y) should equal the sum of c the coefficients times the radius as a function of x and y to the l power times the legendre polynomial where the argument is of the legendre polynomial is cos(theta).
In python: integrating a piecewise function, I learned how to use the Legendre polynomials in a summation but that method is slightly different, and for the plotting, I need a function T(x, y).
This is the plotting code.
densityinterpolation = 'bilinear'
densitycolormap = cm.jet
densityshadedflag = False
densitybarflag = True
gridflag = True
plotfilename = 'laplacesphere.eps'
x = arange(xi, xf, (xf - xi) / (numx - 1))
y = arange(yi, yf, (yf - yi) / (numy - 1))
X, Y = meshgrid(x, y)
z = T(X, Y)
if densityshadedflag:
ls = LightSource(azdeg = 120, altdeg = 65)
rgb = ls.shade(z, densitycolormap)
im = imshow(rgb, extent = [xi, xf, yi, yf], cmap = densitycolormap)
else:
im = imshow(z, extent = [xi, xf, yi, yf], cmap = densitycolormap)
im.set_interpolation(densityinterpolation)
if densitybarflag:
colorbar(im)
grid(gridflag)
show()
I made the plot in Mathematica for reference of what my end goal is
If you set the values outside of the disk domain (or whichever domain you want) to float('nan'), those points will be ignored when plotting (leaving them in white color).

Categories

Resources