Slice mesh with trimesh - python

I work on a big .stl file which I want to cut into pieces using a bounding box.
For this purpose, I use trimesh python package to load the .stl.
Here is the piece of code used to generate the bounding box :
box = trimesh.creation.box(extents=[1.5, 1.5, 1.5])
print(box.facets_origin)
print(box.facets_normal)
So I get as a return :
print(box.facets_origin)
[[-0.75 -0.75 0.75]
[ 0.75 -0.75 -0.75]
[-0.75 0.75 -0.75]
[-0.75 -0.75 0.75]
[-0.75 0.75 0.75]
[ 0.75 0.75 -0.75]]
print(box.facets_normal)
[[-1. 0. 0.]
[ 0. -1. 0.]
[ 0. 0. -1.]
[ 0. 0. 1.]
[ 0. 1. 0.]
[ 1. 0. 0.]]
This means that the box's center of gravity is at (0, 0, 0)
And then I plan to cut the big stl using slice_plane function.
However, I would like to change the location of the bounding box's center of mass, or facets' location.
How this could be done using trimesh ? Or another Python package ?
Thanks in advance for your help !
Joachim

Can you not translate the box using
mesh.apply_transform(trimesh.transformations.scale_and_translate())
https://github.com/mikedh/trimesh/blob/master/trimesh/transformations.py

Related

From 3D world into a 2D screen by lookat matrix?

I would like to get 2D screen coordinates of a 3D coordinates point by LookAt matrix. Is there any simple function to do this?
For example:
I get one matrix by lookAt:
[[ 1. 0. 0. 0.]
[ 0. 1. 0. 0.]
[ 0. 0. 1. -1.]
[ 0. 0. 0. 1.]]
And I have one 3D vector [1,0,1]
What is its "2D screen coordinates"?
Thanks a lot.

Python kernel crash in Jupyter Notebook when calling TSNE.fit_transform()

I have an output of sklearn's tf-idf which I want to visualize with T-SNE. However, when calling fit_transform on sklearn's T-SNE object, I get the error message:
"Canceled future for execute_request message before replies were done
The Kernel crashed while executing code in the the current cell or a
previous cell. Please review the code in the cell(s) to identify a
possible cause of the failure. Click here for more info. View Jupyter
log for further details."
Why is this happening? Code below.
dense = np.array(
[[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 1. , 0. ],
[0. , 0. , 0. , 1. , 0. ],
[0.70710678, 0.70710678, 0. , 0. , 0. ],
[0. , 0. , 0.70710678, 0. , 0.70710678],
[0.70710678, 0.70710678, 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0.70710678, 0. , 0.70710678]])
from sklearn.manifold import TSNE
tsne = TSNE(n_components = 2, verbose = 1, perplexity = 50, n_iter = 1000)
results = tsne.fit_transform(dense)
I wasn't able to reproduce the error in a Google Colab. It works fine on my end, with the following output:
[t-SNE] Computing 10 nearest neighbors...
[t-SNE] Indexed 11 samples in 0.000s...
[t-SNE] Computed neighbors for 11 samples in 0.009s...
[t-SNE] Computed conditional probabilities for sample 11 / 11
[t-SNE] Mean sigma: 1125899906842624.000000
[t-SNE] KL divergence after 250 iterations with early exaggeration: 39.474655
[t-SNE] KL divergence after 1000 iterations: 0.268328
I've found an old thread on GitHub that may address the problem. It is a Mac related issue, but I don't know what OS does your machine has.
There is a chance that they fixed the error in newer versions of sklearn, so my first suggestion is to try upgrading, if you haven't already.
If the issue still persists, since the problem may be due to a dependency that sklearn uses (and even if you do not have a Mac, you still have a problem), I would recommend using a different library. I know about python-bhtsne that can be used in a similar way as sklearn's.

Why are 3D numpy arrays printed the way they are (How are they ordered)?

I am trying to wrap my head around 3D arrays (or multi-dimensional arrays in general), but it's blowing my brains a bit. Especially the way in which 3D numpy arrays are printed is counter-intuitive to me. This question is similar but it is more about the differences between programming languages, and I still do not fully get it. Let me try to explain.
Say I want to create a 3D array with 3 rows (length), 5 columns(width) and 2 depth. So a 3x5x2 matrix.
I do the following:
import numpy as np
a = np.zeros(30).reshape(3, 5, 2)
To me, a logical way to print this would be like this:
[[[0. 0. 0. 0. 0.] #We can still see three rows from top to bottom
[0. 0. 0. 0. 0.]] #We can still see five columns from left to right
[[0. 0. 0. 0. 0.] #Depth values are shown underneath each other
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]
However, when I print this array it prints like this:
[[[0. 0.] #We can still see three rows from top to bottom,
[0. 0.] #However columns now also appear from top to bottom instead of from left to right
[0. 0.] #Depth values are now shown from left to right
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]]
It is unobvious to me why the array would be printed in this way. Maybe it is just me (Maybe my spatial reasoning is lacking here), or is there a specific reason why NumPy arrays are printed like this?
Synthesizing the comments into a proper answer:
First, take a look at np.zeros(10).reshape(5, 2). That's 5 rows of 2 columns, not 2 rows of 5 columns. Adding 3 at the front means 3 planes of 5 rows and 2 columns. What you're missing is that you new dimension is at the front, not the end. In mathematics, usually the extra dimensions are added at the end (Like extending an (x,y) with a z becomes (x,y,z). However, in computer science array dimensions are typically done this way. It reflects the way arrays are typically stored in row-major order in memory.

3D model filled with cubes of fixed size

I have geometry definition in .obj file /WaveFront format/. I can load meshes of this 3D model and get vertices :
[[ 0. 0. 0.] ,[ 0. 0. 3.25] ,[-2.48 14. 0.] ,[ 0. 0. 0.] ,[0. 3.25
-2.48] ,[9.01 0. 0.] ,...]
QUESTIONS -- what i have accomplish :
How fill the model with the same cubes of the given size /in mathematical sense/ using Python 2.7 ?
In advanced mode I have each squares have random|different color and draw by using pyOpenGL ?

Don't show zero values on 2D heat map

I want to plot a 2D map of a sillicon wafer dies. Hence only the center portion have values and corners have the value 0. I'm using matplotlib's plt.imshow to obtain a simple map as follows:
data = np.array([[ 0. , 0. , 1. , 1. , 0. , 0. ],
[ 0. , 1. , 1. , 1. , 1. , 0. ],
[ 1. , 2. , 0.1, 2. , 2. , 1. ],
[ 1. , 2. , 2. , 0.1, 2. , 1. ],
[ 0. , 1. , 1. , 1. , 1. , 0. ],
[ 0. , 0. , 1. , 1. , 0. , 0. ]])
plt.figure(1)
plt.imshow(data ,interpolation='none')
plt.colorbar()
And I obtain the following map:
Is there any way to remove the dark blue areas where the values are zeros while retaining the shape of the 'wafer' (the green, red and lighter blue areas)? Meaning the corners would be whitespaces while the remainder retains the color configuration.
Or is there a better function I could use to obtain this?
There are two ways to get rid of the dark blue corners:
You can flag the data with zero values:
data[data == 0] = np.nan
plt.imshow(data, interpolation = 'none', vmin = 0)
Or you can create a masked array for imshow:
data_masked = np.ma.masked_where(data == 0, data)
plt.imshow(data_masked, interpolation = 'none', vmin = 0)
The two solutions above both solve your problem, although the use of masks is a bit more general.
If you want to retain the exact color configuration you need to manually set the vmin/vmax arguments for plotting the image. Passing vmin = 0 to plt.imshow above makes sure that the discarded zeros still show up on the color bar.

Categories