How to map a 3d model's vertices to a spherical surface? - graphics

Assume I have a spherical surface with n vertices, and I have a 3d model with m vertices(m<=n), how can I map this m vertices to this spherical surface(one vertex on the 3d model map to one vertex on the spherical surface)?

Related

Finding a third vertex of N-dimension equilateral triangle

Given two vectors X and Y, where the number of elements in each is 5. Find a V vector that satisfies :
||X-V||=||Y-V||=||X-Y||
(X,Y,V) are the vertices of an equilateral triangle.
I have tried the following:
To get a vector V that is perpendicular to A and B :
import NumPy as np
# Example vectors
x = [ 0.93937874, 0.05568767, -2.05847484, -1.15965884, -0.34035054]
y = [-0.45921145, -0.55653187, 0.6027685, 0.13113272, -1.2176953 ]
# convert those vectors to a matrix to apply SVD (sure there is a shorter code to do so)
A_list=[]
A_list.append(x)
A_list.append(y)
A=np.array(A_list) # A is a Numpy matrix
u,s,vh=np.linalg.svd(A)
v=vh[-1:1]
From here, what should I do? assuming that what I have done so far is correct

create a 3d cylinder inside 3d volume

I have 3d volume. Which has shape of (399 x 512 x 512). And It has voxel spacing of 0.484704 x 0.484704 x 0.4847
Now, I want to define a cylinder inside this volume with length 5mm, diameter 1mm, intensity 1 inside, intensity 0 outside.
I saw an example to define a cylinder in internet like this code:
from mpl_toolkits.mplot3d import Axes3D
def data_for_cylinder_along_z(center_x,center_y,radius,height_z):
z = np.linspace(0, height_z, 50)
theta = np.linspace(0, 2*np.pi, 50)
theta_grid, z_grid=np.meshgrid(theta, z)
x_grid = radius*np.cos(theta_grid) + center_x
y_grid = radius*np.sin(theta_grid) + center_y
return x_grid,y_grid,z_grid
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Xc,Yc,Zc = data_for_cylinder_along_z(0.2,0.2,0.05,0.1)
ax.plot_surface(Xc, Yc, Zc, alpha=0.5)
plt.show()
However, I don't know how to define the cylinder inside the 3d volume keeping all the conditions (length 5mm, diameter 1mm, intensity 1 inside, intensity 0 outside) true. I also want to define the center of cylinder automatically. So that I can define the cylinder at any place of inside the 3d volume keeping the other condition true. Can anyone show or provide any example?
Thanks a lot in advance.
One simple way of solving this would be to perform each of the checks individually and then just keep the voxels that satisfy all of your constraints.
If you build a grid with all of the centers of the voxels: P (399 x 512 x 512 x 3), each voxel at (i,j,k) will be associated with its real-world position (x,y,z).
That's a little tricky, but it should look something like this:
np.stack(np.meshgrid(np.arange(0, shape[0]),
np.arange(0, shape[1]),
np.arange(0, shape[2]), indexing='ij'), axis=3)
If you subtract the cylinder's center (center_x,center_y, center_z), you're left with the relative positions of each (i,j,k) voxel P_rel (399 x 512 x 512 x 3)
When you have that, you can apply each of your tests one after the other. For a Z-oriented cylinder with a radius and height_z it would look something like:
# constrain the Z-axis
not_too_high = P_rel[:,:,:,2]<= (0.5*height_z)
not_too_low = P_rel[:,:,:,2]>= (-0.5*height_z)
# constrain the radial direction
not_too_far = np.linalg.norm(P_rel[:,:,:,:2],axis=3)<=radius
voxels_in_cyl = not_too_high & not_too_low & not_too_far
I haven't tested the code, but you get the idea.
If you wanted to have an cylinder with an arbitrary orientation you would have to project P_rel into axial and radial components and then do an analogous check without "hard-coding" the indices as I did in this example

How to generate "triangles" parameter of Mayavi triangular_mesh using triangles which are known vertices coordinates

I have triangles which construct surface of sphere. I want to draw the surface using mayavi triangular_mesh function. The function has "triangles" parameter which is list of triplets (or an array) list the vertices in each triangle. I know vertices coordinate all of triangles. But I don't know how to generate this parameter.
I also tried to generate "triangles" parameter using matplotlib Triangulation function (triangles = matplotlib.tri.Triangulation(longitudes, latitudes).triangles). However, in some places of surface, the triangles are connected incorrectly (can be seen Figure 1).
Figure 1. Incorrect connection of triangles
How to generate "triangles" parameter of Mayavi triangular_mesh using triangles which are known vertices coordinates
Data Format
x, y and z are Cartesian coordinate of triangles. For example; (x[0], y[0], z[0]), (x[1], y[1], z[1]) and (x[2], y[2], z[2]) are vertices of a triangle. Next triangle has (x[3], y[3], z[3]), (x[4], y[4], z[4]) and (x[5], y[5], z[5]) vertices coordinate.
You just need to put it together using the indices.
Take a look at the shape of triangles.
It has three indices per triangle that is used
on x, y and z.
import numpy as np
import mayavi.mlab as mlab
x = np.random.rand(30)
y = np.random.rand(30)
z = np.random.rand(30)
s = np.random.rand(30)
triangles = np.random.randint(0, 30, size=(10, 3))
print(triangles)
mlab.triangular_mesh(x, y, z, triangles, scalars=s)
mlab.orientation_axes()
mlab.show()

error when using networkx to drow a precision matrix of financial network

I want to draw the precision matrix (inverse covariance) of a financial networks including 200 stocks. It popped up an error
'AttributeError: module 'matplotlib.pyplot' has no attribute 'ishold''.
plus:how can I draw pic like that the size of vertex is proportionate to its variance, the width of the edges is proportionate to its correlation.
G=nx.from_pandas_dataframe(prec_filtered, 'var1', 'var2')
nx.draw(G, with_labels=True, node_color='orange', node_size=400, edge_color='black', linewidths=1, font_size=1)

Polar contour plot in Maxima

How can I make a polar contour plot in Maxima? Given an expression such as
exp(-r) * cos(phi)
I'd like to plot contours in the x-y plane which have the same value of the expression at all points along the contour.
I've tried
draw3d(cylindrical(exp(-r) * cos(phi), r, 0, 5, phi, 0, 2*%pi), contour=map))
but cylindrical plots r as a function of z and phi, not z as a function of r and phi. It would be nice to not have to convert manually to Cartesian coordinates.
contour_plot(exp(-r)*cos(phi), [r,0,2], [phi, 0, 2*%pi], [transform_xy, polar_to_xy],
[gnuplot_preamble, "set cntrparam levels 10;"]);
The polar_to_xy option interprets the first two variables as distance from the z axis and azimuthal angle.
What is the problem using something like
draw3d(explicit(20*exp(-x^2-y^2)-10,x,0,2,y,-3,3),
contour_levels = 15,
contour = map,
surface_hide = true) ;
I think that in that case is straigthforward to do it.

Resources