-
-
Notifications
You must be signed in to change notification settings - Fork 35.1k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,59 @@ class Mesh extends Object3D { | |
|
||
} | ||
|
||
volume( precision ) { | ||
|
||
function calculateVolume( { geometry, precision } ) { | ||
|
||
let volume = 0; | ||
const vertices = geometry.attributes.position.array; | ||
let indexes = geometry.index ? geometry.index.array : null; | ||
|
||
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 commentThe 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 commentThe 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 commentThe 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 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 commentThe 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. |
||
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 ) { | ||
|
||
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 ); | ||
isalgadof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 ); | ||
|
||
} | ||
|
||
if ( precision ) { | ||
|
||
return Math.abs( volume ); | ||
|
||
} else { | ||
|
||
return Math.abs( volume / 1000 ); | ||
|
||
} | ||
|
||
} | ||
|
||
const vol = calculateVolume( { geometry: this.geometry, precision: precision } ); | ||
isalgadof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return vol; | ||
|
||
} | ||
|
||
copy( source, recursive ) { | ||
|
||
super.copy( source, recursive ); | ||
|
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
asSceneUtils.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:
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
...