How to find wall thickness of 3d models (STL) files?

Now I’m lost. We were talking about the thickness of a model and now you’re saying that you want to find model’s volume.

Moreover, once again, could you provide a live code example with the model?

Use an appropriate type of geometry for the algorithm. The appropriate type is a non-indexed buffer geometry. And the loader already returns the geometry of this type.

Refer below code,

//stl file
var loader = new THREE.STLLoader();
loader.load(“file5.stl”, function (geometry1) {
material = new THREE.MeshPhongMaterial({ color: 0x5C98BD});
mesh = new THREE.Mesh(geometry1,material);
scene.add(mesh);
//your code
var minDist = 0.6;
var raycaster = new THREE.Raycaster();
var intersects = ;
var positions = geometry1.attributes.position;
//console.log(positions);

  var ori = new THREE.Vector3();
  var dir = new THREE.Vector3();
  var trc = new THREE.Vector3();
  var a = new THREE.Vector3(),
    b = new THREE.Vector3(),
    c = new THREE.Vector3(),
    midPoint = new THREE.Vector3(),
    tri = new THREE.Triangle();
  var closest = new THREE.Vector3();

  var faces = positions.count / 3;
  console.log(faces);
  for (let i = 0; i < faces; i++) {
    a.fromBufferAttribute(positions, i * 3 + 0);
    b.fromBufferAttribute(positions, i * 3 + 1);
    c.fromBufferAttribute(positions, i * 3 + 2);
    tri.set(a,b,c);
    tri.getMidpoint(ori);
    tri.getNormal(dir)
    raycaster.set(ori,dir.negate());
      alert(intersects.length);
   intersects = raycaster.intersectObject(mesh);
    minDist = Math.min(minDist, intersects[1].distance);
    console.log(minDist);

  }
  console.log("printable:", minDist >= 0.6);

});

As per previous discussion,
Now i have load STL file which is type of non-indexed buffer geometry.
But still i am unable to know whether stl file is printable or not?Here

console.log(“printable:”, minDist >= 0.6);

Could you please suggest where is changes required in my code.

Thanks

What about .side property of the material? Is it THREE.DoubleSide?

Yes ,
material = new THREE.MeshPhongMaterial({ color: 0x5C98BD, side: THREE.DoubleSide});

I was different in the code you’ve provided.

I forgot to change it.
I used that property of material side: THREE.DoubleSide.

Does .side property of the material effect wall thickness? If yes, then how it effects?

I’ve modified the algorithm:
https://jsfiddle.net/prisoner849/L0ecxuk4/
Used an exisiting stl model, as you couldn’t provide one.

Thanks,
Will check it and update you.

Thanks,
Your modified the algorithm work well
hats off to you :blush:

You’re welcome :slight_smile: :beers:

Hello, thank you for your solution…I have modified your JSFiddle to set color on each face based on the distance. The color function is just to show you the specific faces that the measured wall thickness for them is incorrect. The image shows the dimensions, and if you use the console, you can see that the measurement for 4 faces (two walls that are black-colored) are 110 instead if 10, and that is why they are black instead of red. I was wondering if you could give your opinion on why this happens. Many thanks!
https://jsfiddle.net/hesamoy/r7u1aqk8/40/
hexagon

Well, obviously, the algorithm is imperfect and works not correctly, thus it needs to be improved.
Looks like, it’s because of float precision, rays just go between two triangles.
Any thoughts on improvement?

Thank you, by ‘between two triangles’ you mean rays pass through the first triangle and just intersects with the next one? so I might need to look into the ray and how it works in order to improve it?

I mean this:
GoBetween

Ok, yeah, I got what you mean. What if we modify/enlarge the triangles when we define them?

Raycaster checks all the triangles in a mesh, I didn’t get what triangles do you want to modify/enlarge?

Oh I see, enlarging all triangles by scale may cause a problem with finding the correct intersection.

Here is a solution to lower the risk of ray passing between two triangles or similar situations:

1 Like

Hi,
i have read all discussion going on, and i am also facing such kind of problem.
below is my current code:
var minDist = 0.6;
var raycaster = new THREE.Raycaster();
var intersects = [];
var pos = threejsobject.position; //positions of geometry
var ori = new THREE.Vector3();
var dir = new THREE.Vector3();
var trc = new THREE.Vector3();
var a = new THREE.Vector3(),
b = new THREE.Vector3(),
c = new THREE.Vector3(),
midPoint = new THREE.Vector3(),
tri = new THREE.Triangle();
var closest = new THREE.Vector3();
var faces = threejsobject.positioncount; // faces of geometry
console.log(“Faces:”+faces);
for (let i = 0; i < faces; i++) {
a.fromBufferAttribute(pos, i * 3 + 0);
b.fromBufferAttribute(pos, i * 3 + 1);
c.fromBufferAttribute(pos, i * 3 + 2);
tri.set(a, b, c);
console.log(tri);
// tri.getMidpoint(ori);
// get some random point on face
let r0 = 0.33 * Math.random();
let r1 = 0.33 * Math.random();
let r2 = 1.0 - r0 - r1;

		    let ori = new THREE.Vector3(
		        r0*a.x + r1*b.x + r2*c.x,
		        r0*a.y + r1*b.y + r2*c.y,
		        r0*a.z + r1*b.z + r2*c.z
		    );
		    tri.getNormal(dir);
		    raycaster.set(ori, dir.negate());
		    intersects = raycaster.intersectObject(mesh);
		
		    //minDist = Math.min(minDist, intersects[intersects.length > 1 ? 1 : 0].distance);
		    minDist = intersects[intersects.length > 1 ? 1 : 0].distance;  
	  }
  	console.log("printable:", minDist >= 0.6);

i got correct number of faces of geometry, but sometimes in For Loop “tri.set(a, b, c);” function set/show only one triangle(and not give any result) how ever in some cases it set/show all triangle which finally gives expected result.
Also i gone through the link
https://stackoverflow.com/questions/55126766/improving-raycasting-precision-in-three-js/55159041#55159041

Can anyone suggest where i am going wrong?

Thanks

I used to work for a distributed manufacturing company (3d printing, CNC). We had a team of computational geometry engineers, with a few PHDs among them working on the problems of identifying thin walls on 3d models. They worked with c++, i was hired as a WebGL expert to visualize the data that they would end up computing. With our wages, benefits, office space in SF etc. this could have easily been $1.000.000 a year to solve. And we were solving it for a couple of years.

What i want to say is that i’m surprised that people are tackling this with three.js and would suggest other tools for this. Between javascript, the browser and three.js i wouldn’t be able to tell which tool is less suited for this kind of a task :slight_smile:

(threejs is great at visualizing thin walls on the web, not computing them)

3 Likes