Sign in to follow this  
jerome

Area of any mesh

Recommended Posts

Hi people,

As this has been requested from time to time on this forum, here is a simple and fast method for computing any mesh area :

    var area = function(mesh) {
        if (!mesh) {
            return 0.0;
        }
        var indices = mesh.getIndices();
        var positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
        var v1x = 0.0;
        var v1y = 0.0;
        var v1z = 0.0;
        var v2x = 0.0;
        var v2y = 0.0;
        var v2z = 0.0;
        var crossx = 0.0;
        var crossy = 0.0;
        var crossz = 0.0;
        var ar = 0.0;
        var i1 = 0;
        var i2 = 0;
        var i3 = 0;
        var nbFaces = indices.length / 3;

        for (var i = 0; i < nbFaces; i++) {
            i1 = indices[i * 3];
            i2 = indices[i * 3 + 1];
            i3 = indices[i * 3 + 2];
            v1x = positions[i1 * 3] - positions[i2 * 3];
            v1y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
            v1z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
            v2x = positions[i3 * 3] - positions[i2 * 3];
            v2y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
            v2z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
            crossx = v1y * v2z - v1z * v2y;
            crossy = v1z * v2x - v1x * v2z;
            crossz = v1x * v2y - v1y * v2x; 
            ar = ar + Math.sqrt(crossx * crossx + crossy * crossy + crossz * crossz);
        }
        return ar * 0.5;
    };

As it doesn't allocate any object, nor calls external functions, it should be fast, GC friendly and it might be called within the render loop.

Just let me know if you want it to be integrated in BJS as a method of the class Mesh or AbstractMesh.

 

[EDIT] For experts, note that this computation could be shared and done in one line only within the method ComputeNormals() of the class VertexData just after this line : https://github.com/BabylonJS/Babylon.js/blob/master/src/Mesh/babylon.mesh.vertexData.ts#L2007  with something like :

area += length * 0.5;

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.