Skip to content
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

Closed
wants to merge 3 commits into from
Closed

Mesh: Add volume method #27906

wants to merge 3 commits into from

Conversation

isalgadof
Copy link

@isalgadof isalgadof commented Mar 12, 2024

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

Copy link

github-actions bot commented Mar 12, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Filesize dev Filesize PR Diff
669.7 kB (166.2 kB) 670.2 kB (166.4 kB) +536 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Filesize dev Filesize PR Diff
449.9 kB (108.8 kB) 450.4 kB (109 kB) +536 B

@isalgadof
Copy link
Author

Sorry for the blooper commit. Did not realize before clicking.

@mrdoob
Copy link
Owner

mrdoob commented Mar 12, 2024

Please don't include builds in PRs 🙏

@mrdoob
Copy link
Owner

mrdoob commented Mar 12, 2024

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 );

	}

@mrdoob mrdoob changed the title #27905 Add volume calculator for 3d mesh Mesh: Add volume method Mar 12, 2024
@@ -49,6 +49,59 @@ class Mesh extends Object3D {

}

volume( precision ) {
Copy link
Collaborator

@Mugen87 Mugen87 Mar 12, 2024

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.

Copy link
Author

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

Copy link
Collaborator

@Mugen87 Mugen87 Mar 12, 2024

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 );

Copy link
Owner

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 Show resolved Hide resolved
src/objects/Mesh.js Outdated Show resolved Hide resolved

function triangularVolume( p1, p2, p3 ) {

const v321 = p3.x * p2.y * p1.z;
Copy link
Collaborator

@Mugen87 Mugen87 Mar 12, 2024

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

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.

Copy link
Collaborator

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.

@isalgadof
Copy link
Author

Modifications requested added

@isalgadof isalgadof requested a review from Mugen87 March 12, 2024 13:11
@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 12, 2024

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 );
Copy link
Collaborator

@Mugen87 Mugen87 Mar 12, 2024

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;
Copy link
Collaborator

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;

@donmccurdy
Copy link
Collaborator

donmccurdy commented Mar 12, 2024

Regarding the build files: The idea is to remove just the diff but not the actual files.

A quick way to do this would be...

git checkout dev -- build

... and then commit the resulting change.

@LeviPesin
Copy link
Contributor

How is the precision parameter used? You seem only to divide the volume by it in the end which doesn't make much sense.

@isalgadof
Copy link
Author

How is the precision parameter used? You seem only to divide the volume by it in the end which doesn't make much sense.

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 :)

@mrdoob
Copy link
Owner

mrdoob commented Mar 14, 2024

Maybe it would be helpful if you shared use cases or how people are using this function.

@isalgadof
Copy link
Author

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.

@donmccurdy
Copy link
Collaborator

I'll add one more use case. I'd love to include a new class, MeshVolumeSampler – analogous to MeshSurfaceSampler, but sampling randomly from the 3D volume of a mesh rather than its surface. To do that, I think we'd need a tetrahedral triangulation of the mesh, which (as mentioned in #27906 (comment)) may also be necessary here.

@WestLangley
Copy link
Collaborator

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 Mesh class has a world transform, which should be honored in any implementation of this method that accepts a three.js Mesh as input.

I think it would make more sense if it operated on BufferGeometry.

@isalgadof
Copy link
Author

This method is application-specific, and only works on watertight, 3D printable meshes. In other cases, it will produce nonsense answers.

Well, of course. Only 3D parts ("watertight" meshes) can have volume, by literal definition.
Open meshes are just vectors in space, nothing to obtain volume from without inferring some stitching.
You wouldn't want -nor should you ever need- to get the volume of open meshes.

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

@donmccurdy
Copy link
Collaborator

donmccurdy commented Mar 15, 2024

Suggested fixes, and unit tests:

0f9550b

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.

@donmccurdy
Copy link
Collaborator

donmccurdy commented Mar 15, 2024

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.

@isalgadof
Copy link
Author

We cannot practically test for that here, but it would be important to mention the requirement in the documentation.

Agreed

@gkjohnson
Copy link
Collaborator

My preference would be that this method return a result in cubic units of the local coordinate space.

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?

@donmccurdy
Copy link
Collaborator

donmccurdy commented Mar 15, 2024

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...

const volume = BufferGeometryUtils.computeVolume( geometry, matrixWorld );

... where the matrix argument is optional.

@Mugen87
Copy link
Collaborator

Mugen87 commented Apr 17, 2024

Right now, the PR is in an invalid state since it deletes the build files and has a broken volume() implementation. It should also be moved to addons as a utils function (MeshUtils.volume()).

I suggest filing a fresh PR which honors this feedback.

@Mugen87 Mugen87 closed this Apr 17, 2024
@Mugen87 Mugen87 added this to the r164 milestone Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add volume calculator for 3d mesh
7 participants