<commit_before>"""
An example showing how to select points out of a cloud.

This example shows two tricks:

 1) How to select from a list of visualization objects

 2) How to find the point number, when selecting in a cloud of points
    represented by glyphs.

To handle mouse events, we register a set of callbacks that will call
the picker when the mouse button is pressed, but there is no movement.
The picker is called, and triggers the picker callback.

The trick to act only on specific visualization objects is to test that
the actors of the objects we are interested in are in the list of actors
selected by the picker.

Finally, we need to find the specific data point corresponding to the
glyph selected. For this we find out how many points each individual
glyph contains, and we look at the point id of vertex selected. Dividing
the later by the former gives us the corresponding data point
id.
"""

import numpy as np
from enthought.mayavi import mlab 

################################################################################
# Disable the rendering, to get bring up the figure quicker:
figure = mlab.gcf()
mlab.clf()
figure.scene.disable_render = True

# Creates two set of points using mlab.points3d: red point and
# white points
x1, y1, z1 = np.random.random((3, 10))
red_glyphs = mlab.points3d(x1, y1, z1, color=(1, 0, 0),
                resolution=20)

x2, y2, z2 = np.random.random((3, 10))
white_glyphs = mlab.points3d(x2, y2, z2, color=(0.9, 0.9, 0.9), 
                resolution=20)

# Add an outline to show the selected point and center it on the first
# data point.
outline = mlab.outline(line_width=3)
outline.outline_mode = 'cornered'
outline.bounds = (x1[0]-0.1, x1[0]+0.1, 
                  y1[0]-0.1, y1[0]+0.1, 
                  z1[0]-0.1, z1[0]+0.1)

# Every object has been created, we can reenable the rendering.
figure.scene.disable_render = False
################################################################################


# Here, we grab the points describing the individual glyph, to figure
# out how many points are in an individual glyph.
n_glyph_src = \
    red_glyphs.glyph.glyph_source.glyph_source.output.points.to_array().shape[0]

picker = figure.scene.picker

def picker_callback(vtk_picker, event):
    """ Picker callback: this get called when on pick events. 
    """
    # Here we are using a the module-level variable for the picker. We
    # could also retrieve it doing:
    # picker = tvtk.to_vtk(tvtk_picker)

    if picker.pointpicker.actor in red_glyphs.actor.actors:
        # Find which data point corresponds to the point picked:
        # we have to account for the fact that each data point is
        # represented by a glyph with several points 
        point_id = picker.pointpicker.point_id/n_glyph_src
        # If the no points have been selected, we have '-1'
        if point_id != -1:
            # Retrieve the coordinnates coorresponding to that data
            # point<commit_msg>DOC: Make the point selection example more robust.<commit_after>"""
An example showing how to select points out of a cloud.

This example shows two tricks:

 1) How to select from a list of visualization objects

 2) How to find the point number, when selecting in a cloud of points
    represented by glyphs.

To handle mouse events, we register a set of callbacks that will call
the picker when the mouse button is pressed, but there is no movement.
The picker is called, and triggers the picker callback.

The trick to act only on specific visualization objects is to test that
the actor of the objects we are interested in is the actor selected by
the picker.

Finally, we need to find the specific data point corresponding to the
glyph selected. For this we find out how many points each individual
glyph contains, and we look at the point id of vertex selected. Dividing
the later by the former gives us the corresponding data point
id.
"""

import numpy as np
from enthought.mayavi import mlab 

################################################################################
# Disable the rendering, to get bring up the figure quicker:
figure = mlab.gcf()
mlab.clf()
figure.scene.disable_render = True

# Creates two set of points using mlab.points3d: red point and
# white points
x1, y1, z1 = np.random.random((3, 10))
red_glyphs = mlab.points3d(x1, y1, z1, color=(1, 0, 0),
                resolution=20)

x2, y2, z2 = np.random.random((3, 10))
white_glyphs = mlab.points3d(x2, y2, z2, color=(0.9, 0.9, 0.9), 
                resolution=20)

# Add an outline to show the selected point and center it on the first
# data point.
outline = mlab.outline(line_width=3)
outline.outline_mode = 'cornered'
outline.bounds = (x1[0]-0.1, x1[0]+0.1, 
                  y1[0]-0.1, y1[0]+0.1, 
                  z1[0]-0.1, z1[0]+0.1)

# Every object has been created, we can reenable the rendering.
figure.scene.disable_render = False
################################################################################


# Here, we grab the points describing the individual glyph, to figure
# out how many points are in an individual glyph.
n_glyph_src = \
    red_glyphs.glyph.glyph_source.glyph_source.output.points.to_array().shape[0]

picker = figure.scene.picker

def picker_callback(vtk_picker, event):
    """ Picker callback: this get called when on pick events. 
    """
    # Here we are using a the module-level variable for the picker. We
    # could also retrieve it doing:
    # picker = tvtk.to_vtk(tvtk_picker)

    if picker.pointpicker.actor == red_glyphs.actor.actor:
        # Find which data point corresponds to the point picked:
        # we have to account for the fact that each data point is
        # represented by a glyph with several points 
        point_id = picker.pointpicker.point_id/n_glyph_src
        # If the no points have been selected, we have '-1'
        if point_id != -1:
            # Retrieve the coordinnates coorresponding to that data
            # point
{'fraction_non_alphanumeric': 0.12550200803212852, 'fraction_numerical': 0.0214190093708166, 'mean_word_length': 3.46044776119403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1685039', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1695, 'n_words': 742}