Selecting faces of a mesh based on vertices coordinates in numpy












2















I have two numpy arrays, one is for 3D vertices of a mesh, call it vert and one is for the triangular faces, call it faces:



The vert array is a N x 3 shape array of float, hence N three dimensional points. The x coordinate of each point can have both positive and negative values.
As a pure example this can be the vert array:



[[  2.886495  24.886948  15.909558]
[ -13.916695 -58.985245 19.655312]
[ 40.415527 8.968353 8.515955]
...
[ 13.392465 -58.20602 18.752457]
[ -12.504704 -58.307934 18.912386]
[ 13.322185 -58.52817 19.165733]]


Since the mesh is centered, the left part of the mesh is the one with positive x component and the corresponding vertex indices are found by a np.where



i_vert_left = np.where(vert[:,0]>0)[0]


I now would like to filter out those faces made of triangles with coordinates entirely in the positive x axis.



However I have a problem in doing this indexing operation correctly.
My first attempt was to subset the faces such that their corresponding vertices have x>0



faces_left = np.asarray([f for f in faces if np.all(np.isin(i_vert_left,f)) ])


but the operation is incredibly slow on large meshes.
How can I exploit a smart indexing of the faces?










share|improve this question























  • What's faces like? Shape?

    – Divakar
    Nov 19 '18 at 14:37
















2















I have two numpy arrays, one is for 3D vertices of a mesh, call it vert and one is for the triangular faces, call it faces:



The vert array is a N x 3 shape array of float, hence N three dimensional points. The x coordinate of each point can have both positive and negative values.
As a pure example this can be the vert array:



[[  2.886495  24.886948  15.909558]
[ -13.916695 -58.985245 19.655312]
[ 40.415527 8.968353 8.515955]
...
[ 13.392465 -58.20602 18.752457]
[ -12.504704 -58.307934 18.912386]
[ 13.322185 -58.52817 19.165733]]


Since the mesh is centered, the left part of the mesh is the one with positive x component and the corresponding vertex indices are found by a np.where



i_vert_left = np.where(vert[:,0]>0)[0]


I now would like to filter out those faces made of triangles with coordinates entirely in the positive x axis.



However I have a problem in doing this indexing operation correctly.
My first attempt was to subset the faces such that their corresponding vertices have x>0



faces_left = np.asarray([f for f in faces if np.all(np.isin(i_vert_left,f)) ])


but the operation is incredibly slow on large meshes.
How can I exploit a smart indexing of the faces?










share|improve this question























  • What's faces like? Shape?

    – Divakar
    Nov 19 '18 at 14:37














2












2








2








I have two numpy arrays, one is for 3D vertices of a mesh, call it vert and one is for the triangular faces, call it faces:



The vert array is a N x 3 shape array of float, hence N three dimensional points. The x coordinate of each point can have both positive and negative values.
As a pure example this can be the vert array:



[[  2.886495  24.886948  15.909558]
[ -13.916695 -58.985245 19.655312]
[ 40.415527 8.968353 8.515955]
...
[ 13.392465 -58.20602 18.752457]
[ -12.504704 -58.307934 18.912386]
[ 13.322185 -58.52817 19.165733]]


Since the mesh is centered, the left part of the mesh is the one with positive x component and the corresponding vertex indices are found by a np.where



i_vert_left = np.where(vert[:,0]>0)[0]


I now would like to filter out those faces made of triangles with coordinates entirely in the positive x axis.



However I have a problem in doing this indexing operation correctly.
My first attempt was to subset the faces such that their corresponding vertices have x>0



faces_left = np.asarray([f for f in faces if np.all(np.isin(i_vert_left,f)) ])


but the operation is incredibly slow on large meshes.
How can I exploit a smart indexing of the faces?










share|improve this question














I have two numpy arrays, one is for 3D vertices of a mesh, call it vert and one is for the triangular faces, call it faces:



The vert array is a N x 3 shape array of float, hence N three dimensional points. The x coordinate of each point can have both positive and negative values.
As a pure example this can be the vert array:



[[  2.886495  24.886948  15.909558]
[ -13.916695 -58.985245 19.655312]
[ 40.415527 8.968353 8.515955]
...
[ 13.392465 -58.20602 18.752457]
[ -12.504704 -58.307934 18.912386]
[ 13.322185 -58.52817 19.165733]]


Since the mesh is centered, the left part of the mesh is the one with positive x component and the corresponding vertex indices are found by a np.where



i_vert_left = np.where(vert[:,0]>0)[0]


I now would like to filter out those faces made of triangles with coordinates entirely in the positive x axis.



However I have a problem in doing this indexing operation correctly.
My first attempt was to subset the faces such that their corresponding vertices have x>0



faces_left = np.asarray([f for f in faces if np.all(np.isin(i_vert_left,f)) ])


but the operation is incredibly slow on large meshes.
How can I exploit a smart indexing of the faces?







python numpy indexing mesh matrix-indexing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 14:33









linellolinello

3,51774982




3,51774982













  • What's faces like? Shape?

    – Divakar
    Nov 19 '18 at 14:37



















  • What's faces like? Shape?

    – Divakar
    Nov 19 '18 at 14:37

















What's faces like? Shape?

– Divakar
Nov 19 '18 at 14:37





What's faces like? Shape?

– Divakar
Nov 19 '18 at 14:37












1 Answer
1






active

oldest

votes


















1














Assuming faces is a Nx3 array of integers indexing the three vertices of each triangle, I think you should just need:



# Check whether each vertex is left or not
vert_left_mask = vert[:, 0] > 0
# Check whether each face has all vertices on left or not
faces_left_mask = np.all(vert_left_mask[faces], axis=1)
# Select resulting left faces
faces_left = faces[faces_left_mask]


The main "trick" here is in vert_left_mask[faces], which replaces each integer vertex number with a boolean indicating whether the vertex is left or not, so it's easy to tell which face is fully left with np.all.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376827%2fselecting-faces-of-a-mesh-based-on-vertices-coordinates-in-numpy%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Assuming faces is a Nx3 array of integers indexing the three vertices of each triangle, I think you should just need:



    # Check whether each vertex is left or not
    vert_left_mask = vert[:, 0] > 0
    # Check whether each face has all vertices on left or not
    faces_left_mask = np.all(vert_left_mask[faces], axis=1)
    # Select resulting left faces
    faces_left = faces[faces_left_mask]


    The main "trick" here is in vert_left_mask[faces], which replaces each integer vertex number with a boolean indicating whether the vertex is left or not, so it's easy to tell which face is fully left with np.all.






    share|improve this answer




























      1














      Assuming faces is a Nx3 array of integers indexing the three vertices of each triangle, I think you should just need:



      # Check whether each vertex is left or not
      vert_left_mask = vert[:, 0] > 0
      # Check whether each face has all vertices on left or not
      faces_left_mask = np.all(vert_left_mask[faces], axis=1)
      # Select resulting left faces
      faces_left = faces[faces_left_mask]


      The main "trick" here is in vert_left_mask[faces], which replaces each integer vertex number with a boolean indicating whether the vertex is left or not, so it's easy to tell which face is fully left with np.all.






      share|improve this answer


























        1












        1








        1







        Assuming faces is a Nx3 array of integers indexing the three vertices of each triangle, I think you should just need:



        # Check whether each vertex is left or not
        vert_left_mask = vert[:, 0] > 0
        # Check whether each face has all vertices on left or not
        faces_left_mask = np.all(vert_left_mask[faces], axis=1)
        # Select resulting left faces
        faces_left = faces[faces_left_mask]


        The main "trick" here is in vert_left_mask[faces], which replaces each integer vertex number with a boolean indicating whether the vertex is left or not, so it's easy to tell which face is fully left with np.all.






        share|improve this answer













        Assuming faces is a Nx3 array of integers indexing the three vertices of each triangle, I think you should just need:



        # Check whether each vertex is left or not
        vert_left_mask = vert[:, 0] > 0
        # Check whether each face has all vertices on left or not
        faces_left_mask = np.all(vert_left_mask[faces], axis=1)
        # Select resulting left faces
        faces_left = faces[faces_left_mask]


        The main "trick" here is in vert_left_mask[faces], which replaces each integer vertex number with a boolean indicating whether the vertex is left or not, so it's easy to tell which face is fully left with np.all.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 14:41









        jdehesajdehesa

        23.1k43252




        23.1k43252






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376827%2fselecting-faces-of-a-mesh-based-on-vertices-coordinates-in-numpy%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?