0

Let us consider the following example. This is a standart Matlab function, so everyone can access "BracketWithHole.stl".

figure
gm = importGeometry("BracketWithHole.stl");
pdegplot(gm,"FaceLabels","on")

Example or a STL

My goal is this: The STL geometry is to be "inserted" into a 3D matrix. The areas of the matrix that lie outside the geometry should have a value of 1; those that lie inside the geometry should have a value of 0.

Here is a 2D example: All places in the matrix that are "empty" receive a 0; all others receive a 1. 2D-Example

Edit: length, height and width of the 3D-matrix need not be the same

273K
  • 29,503
  • 10
  • 41
  • 64
S B
  • 67
  • 5
  • Is this what you want? : https://stackoverflow.com/questions/51158078/load-stl-file-in-matlab-and-convert-to-a-3d-array/51158671#51158671 – Ander Biguri Jan 11 '23 at 08:59
  • Yes, that should be, what I was looking for :) I have a question regarding the code. What is meant by "get the range of your data from the STL file". Is this e.g. 50x30x20? – S B Jan 11 '23 at 09:10
  • Maximum and minimum values of the coordinates, the range. e.g. ~[-0.1, 0.3] for your x in the first figure. The blue box in your second figure, essentially. You gotta define that, such that it contains the entire mesh. – Ander Biguri Jan 11 '23 at 09:11
  • And where do I get "fv" from? Unfortunately I didn't understand this, – S B Jan 11 '23 at 09:16
  • its a structure with faces and vertices, one of the most common ways of storing STL type data. `fv.faces` contains the indexes of each point that makes triangles `(nTriangles,3)`, `fv.vertices` contains the points themselves `(npoints,3)`. 3 because its a 3D mesh. – Ander Biguri Jan 11 '23 at 09:18
  • So It should be "fv = importGeometry("BracketWithHole.stl")" in my code, right? But then I get the error: "Unrecognized method, property, or field 'vertices' for class 'pde.DiscreteGeometry'." – S B Jan 11 '23 at 09:26
  • No, it should not, it is your responsibility to make this matrix the right format to input to the code, or change the code to fit your data type. – Ander Biguri Jan 11 '23 at 09:27
  • Or use this: https://github.com/AnderBiguri/TriangleCT/blob/56a5248e769155a971d9d12429e770af549253ac/Utilities/third_party/STLRead/stlread.m – Ander Biguri Jan 11 '23 at 09:29
  • That seems to work very well so far. Hopefully last question: What exactly is "yourboundariesSize" in the line "img=reshape(in,yourboundariesSize);" I tried "yourboundariesSize = size(yourboundaries)", but it is not. – S B Jan 11 '23 at 09:46
  • Try to understand the code. Test it, read the variable names. You will relize that the result `in` is a `1D` array, but you wanted a 3D array, so you need to reshape it. What shape do you want to make it? – Ander Biguri Jan 11 '23 at 09:48
  • You simply render your STL into your 3D matrix using [triangle voxel grid rasterization](https://stackoverflow.com/a/74251621/2521214) and then simply flood fill the interrior ... – Spektre Jan 11 '23 at 10:35
  • @AnderBiguri Now I understand, it is the dimension in x/y/z. The problem I am fighting with is that `in` only contains zeros. The boundary limits are not wrong, are they? `boundaries_x= [-0.1,0.3]; boundaries_y= [0,0.2]; boundaries_z= [0,0.2]; [x,y,z]=meshgrid(boundaries_x,boundaries_y,boundaries_z);` – S B Jan 11 '23 at 10:56
  • @AnderBiguri I also tried it with e.g. `boundaries_x= linspace(-0.1,0.3,10)`, but it didn't help. – S B Jan 11 '23 at 10:58
  • No, but I suggest you read `meshgrid` documentation, as you are calling it wrong. With the linespace (for all 3 of them) should be right, but you may need more than 10 points. – Ander Biguri Jan 11 '23 at 11:09
  • I have used a simple cuboid as the STL. Nevertheless, the variable `in` contains only zeros as entries, no matter how many intermediate values i use with linspace. So this code does not seem to work (for me). – S B Jan 11 '23 at 23:25

0 Answers0