New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mesh: Add volume method #27906
Mesh: Add volume method #27906
Conversation
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
Sorry for the blooper commit. Did not realize before clicking. |
Please don't include builds in PRs 🙏 |
Some simplifications... computeVolume( precision = 1 ) {
const geometry = this.geometry;
const vertices = geometry.attributes.position.array;
let volume = 0;
let indexes = geometry.index ? geometry.index.array : null;
function triangularVolume( p1, p2, p3 ) {
const v321 = p3.x * p2.y * p1.z;
const v231 = p2.x * p3.y * p1.z;
const v312 = p3.x * p1.y * p2.z;
const v132 = p2.x * p1.y * p3.z;
const v213 = p1.x * p3.y * p2.z;
const v123 = p1.x * p2.y * p3.z;
return ( 1.0 / 6.0 ) * ( - v321 + v231 + v312 - v132 - v213 + v123 );
}
if ( indexes !== null ) {
indexes = Array.from( { length: vertices.length / 3 }, ( _, i ) => i );
}
for ( let i = 0; i < indexes.length; i += 3 ) {
const a = new THREE.Vector3().fromArray( vertices, indexes[ i ] * 3 );
const b = new THREE.Vector3().fromArray( vertices, indexes[ i + 1 ] * 3 );
const c = new THREE.Vector3().fromArray( vertices, indexes[ i + 2 ] * 3 );
volume += triangularVolume( a, b, c );
}
return Math.abs( volume / precision );
} |
@@ -49,6 +49,59 @@ class Mesh extends Object3D { | |||
|
|||
} | |||
|
|||
volume( precision ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about moving this routine into SceneUtils
as SceneUtils.computeMeshVolume()
instead?
To me, this method is too specific to be for the core.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its not appropiate since that would be the scene's volume an not the mesh's volume; I would add a scene method to get all individual volumes added.
Not sure how often it could be used though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be a static method so you use it like so:
const volume = SceneUtils.computeMeshVolume( mesh );
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also do a MeshUtiks
...
src/objects/Mesh.js
Outdated
|
||
function triangularVolume( p1, p2, p3 ) { | ||
|
||
const v321 = p3.x * p2.y * p1.z; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this computation is based on some kind of resource/reference. Do you mind sharing it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@isalgadof I'm not sure this is correct.... Perhaps you're referring to equation (5), which computes the volume of a tetrahedron? But to do this you would need to first compute the tetrahedral triangulation of the mesh, which I don't see here. The delaunay-triangulate
npm package would do that part for you, or could be a good reference if you'd prefer to implement it manually.
In either case, it's likely this implementation will become more complex than can be added to the THREE.Mesh class... Either BufferGeometryUtils or SceneUtils would be good choices in my opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, never mind my comment above! I see the method. Tetrahedral triangulation can be skipped. I might suggest adding a comment on this method to explain that it is computing the signed volume of a tetrahedron, having the triangle as one face and the origin as the other.
Modifications requested added |
Regarding the build files: The idea is to remove just the diff but not the actual files. |
|
||
for ( let i = 0; i < indexes.length; i += 3 ) { | ||
|
||
vectorA.fromBufferAttribute( vertices, indexes[ i ] * 3 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not directly access the index array. I suggest you write a separate loop if no index is present:
if ( index ) {
for ( let i = 0; i < index.count; i += 3 ) {
vectorA.fromBufferAttribute( positionAttribute, index.getX( i ) );
vectorB.fromBufferAttribute( positionAttribute, index.getX( i + 1 ) );
vectorC.fromBufferAttribute( positionAttribute, index.getX( i + 2 ) );
volume += triangularVolume( vectorA, vectorB, vectorC );
}
} else {
for ( let i = 0; i < positionAttribute.count; i += 3 ) {
vectorA.fromBufferAttribute( positionAttribute, i );
vectorB.fromBufferAttribute( positionAttribute, i + 1 );
vectorC.fromBufferAttribute( positionAttribute, i + 2 );
volume += triangularVolume( vectorA, vectorB, vectorC );
}
}
volume( precision ) { | ||
|
||
let volume = 0; | ||
const vertices = this.geometry.attributes.position.array; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that your current code works because fromBufferAttribute()
expects a buffer attribute, not an array. Rewrite this line as:
const positionAttribute = this.geometry.attributes.position;
A quick way to do this would be...
... and then commit the resulting change. |
How is the |
Since the code came inherited from my 'Three-Volume' package, the params name came with it, it's rooted on a request-issue i received. It is used to provide a quick passthrough to change units (milliliter to liter), since originally it was always divided by default, and on micro-scale files the division (rather, the roundup) ends up losing precision that can be significant when you get 1000x closer. I believe there is definitively a need for a scale-prop, maybe the implementation could be more elegant (as per, receiving letters for the scale such as ml, dl, lt, etc). If agreed, i'll make the changes soon :) |
Maybe it would be helpful if you shared use cases or how people are using this function. |
Use case for which i designed it: needed to calculate the cost of manufacturing 3d files in meter-scale, i needed the material weight (thus, needing volume to multiply by density). The use case for which the precision param was added: similar situation but with micro scale with sub-millimeters files. The package itself has around a thousand monthly downloads-average, some dozen thousands total downloads, so i'd say volume calculation is without a doubt actively needed. The precision part could be solved with a default - Liter and optional unit to calculate in cl, ml, etc. |
I'll add one more use case. I'd love to include a new class, |
This method is application-specific, and only works on watertight, 3D printable meshes. In other cases, it will produce nonsense answers. This method does not belong in three.js. It belongs in a three.js application or external library. // Also, the I think it would make more sense if it operated on |
Well, of course. Only 3D parts ("watertight" meshes) can have volume, by literal definition. Also, the same package was used for CNC calculation, DLP, SLA and FDM calculations and even some wood routing quoters. The method absolutely belongs in the core Three.js since its output is nothing more than a core property of the rendered object. You can obtain its length, its width, its depth, color, etc. Why not the volume, if available? It would be more reasonable to simply exclude open meshes from accessing the method i think |
Suggested fixes, and unit tests: My preference would be that this method return a result in cubic units of the local coordinate space. The precision parameter can be removed, and any unit conversion left to the user, as is the case with other three.js methods. |
Detecting whether a mesh is non-manifold (and so not "watertight") is a complex problem in its own right. See libraries like https://github.com/elalish/manifold for that. We cannot practically test for that here, but it would be important to mention the requirement in the documentation. |
Agreed |
Can you elaborate on this? If this is going to be added to the project I think it's more practical to account for world transforms so that object scale can be accounted for. Otherwise how would you measure the volume of a non-uniformly scaled mesh? |
If .volume() remains as a method of THREE.Mesh, then yes I agree, returning a result of cubic units of the world space (i.e. cubic meters) would be better. If moved to BufferGeometryUtils, then perhaps...
... where the matrix argument is optional. |
Right now, the PR is in an invalid state since it deletes the build files and has a broken I suggest filing a fresh PR which honors this feedback. |
Closes #27905
Description
Three.js had no native method for Mesh objects to calculate 3D volume. I added the method and included a test exception.
This contribution is funded by Hacemos Software