Click or drag to resize

Picking

Picking allows users to select and interact with objects in the 3D scene. Picks are usually executed in response to a mouse click, mouse move, key press, or any combination of these. For picking on screen overlays, see the Screen Overlays topic.

Pick

The Pick method returns information about objects in the scene that are at a specified pixel. The inputs are X and Y coordinates. The origin is the top, left corner of the 3D control. Typically, the input values will be the location of the mouse cursor, in order to pick objects under the cursor. This section explains the collection returned by Pick, and the following sections describe how implement different types of picking.

Pick returns a collection of PickResults. Each PickResult represents one occurrence of a pick. This includes the object(s) involved in the pick and their position. This is shown in the image below:

Picking

If multiple objects are at a pixel, the collection will contain multiple pick results. The first item in the collection will be the top most object. Subsequent items will be "under" the previous item. For example, if the camera is looking straight down at a primitive on the ground and Pick is called with coordinates that match the primitive, both the primitive and central body will be returned, as shown below:

Picking a Primitive and Central Body
C#
Collection<PickResult> collection = Scene.Pick(x, y);

if (collection.Count == 2)
{
    Primitive primitive = collection[0].Objects[0] as Primitive;
    CentralBody centralBody = collection[1].Objects[0] as CentralBody;

    if (primitive != null && centralBody != null)
    {
        // primitive and central body underneath were picked
    }
}

In other cases, the collection will only contain a single pick result. For example, if a model primitive representing a satellite is picked, the central body may not be returned as shown below.

Picking a Primitive
C#
Collection<PickResult> collection = Scene.Pick(x, y);

if (collection.Count == 1)
{
    ModelPrimitive model = collection[0].Objects[0] as ModelPrimitive;

    if (model != null)
    {
        // Just a model primitive was picked
    }
}

As shown in the code examples, a pick result contains a collection of objects. In most cases, this collection will only contain one object. One exception is when a picked primitive is in a CompositePrimitive. In this case, both the primitive and its composite will be in the objects collection, as shown in the following image and code example:

Picking a Composite Primitive and Central Body
C#
Collection<PickResult> collection = Scene.Pick(x, y);

if (collection.Count == 2)
{
    Collection<object> objects = collection[0].Objects;

    if (objects.Count == 2)
    {
        CompositePrimitive composite = objects[0] as CompositePrimitive;
        Primitive primitive = objects[1] as Primitive;
        CentralBody centralBody = collection[1].Objects[0] as CentralBody;

        if (composite != null && primitive != null && centralBody != null)
        {
            // A primitive in a composite and the central body 
            // underneath were picked
        }
    }
}

For primitives that contain multiple items, like the MarkerBatchPrimitive, TextBatchPrimitive, PointBatchPrimitive, and PolylinePrimitive, it is often useful to know what particular item was picked. Per-item picking can be enabled for these primitives by setting the PerItemPickingEnabled property on the primitive to true. When enabled, the object collection will also contain the BatchPrimitiveIndex of the item in the primitive that was picked, as shown in the following image and code example:

Picking a Primitive Index
C#
Collection<PickResult> collection = Scene.Pick(x, y);

if (collection.Count == 2)
{
    Collection<object> objects = collection[0].Objects;

    if (objects.Count == 3)
    {
        CompositePrimitive composite = objects[0] as CompositePrimitive;
        Primitive primitive = objects[1] as Primitive;
        BatchPrimitiveIndex index = objects[2] as BatchPrimitiveIndex;
        CentralBody centralBody = collection[1].Objects[0] as CentralBody;

        if (composite != null && primitive != null && index != null && centralBody != null)
        {
            // An item in a primitive in a composite and the central body 
            // underneath were picked
        }
    }
}
Normal Picking

In normal picking, Pick is called as the result of an event, such as a mouse click in the 3D control, to perform an action, such as zooming to a primitive. This is demonstrated by the following example from the HowTo:

C#
// Get a collection of picked objects under the mouse location.
// The collection is sorted with the closest object at index zero.
Collection<PickResult> collection = Scene.Pick(mouseX, mouseY);
if (collection.Count != 0)
{
    Collection<object> objects = collection[0].Objects;
    CompositePrimitive composite = objects[0] as CompositePrimitive;

    // Was a model in our composite picked?
    if (composite == m_models)
    {
        ModelPrimitive model = objects[1] as ModelPrimitive;
        CentralBody earth = CentralBodiesFacet.GetFromContext().Earth;
        BoundingSphere sphere = model.BoundingSphere;
        double azimuthAngle = Trig.DegreesToRadians(-90.0);
        double elevationAngle = Trig.DegreesToRadians(-30.0);

        PointFixedOffset boundingSphereCenter = new PointFixedOffset(earth.FixedFrame, sphere.Center);
        Axes boundingSphereAxes = new AxesEastNorthUp(earth, boundingSphereCenter);

        Cartesian offset = new Cartesian(new AzimuthElevationRange(azimuthAngle, elevationAngle, Scene.Camera.DistancePerRadius * sphere.Radius));
        Scene.Camera.ViewOffset(boundingSphereAxes, boundingSphereCenter, offset);
        Scene.Render();
    }
}

This code snippet is called in response to a double click event in the 3D control. A pick is executed using the current position of the mouse cursor. If the collection returned is empty, nothing was picked. Otherwise, since we only want to zoom to a model if it is in our composite, the topmost picked object is checked to see if it is the instance we expect. If the topmost picked object was something else, perhaps because only the central body was picked, the zoom will not occur.

Roll-over Picking

In roll-over picking, Pick is called in response to the mouse moving across the 3D control. The results of the pick may be used to display the mouse cursor's cartographic position over a globe or to highlight the primitive under the cursor, as shown in the following example from the HowTo:

C#
// Get a collection of picked objects under the mouse location.
// The collection is sorted with the closest object at index zero.
Collection<PickResult> collection = Scene.Pick(mouseX, mouseY);
if (collection.Count != 0)
{
    Collection<object> objects = collection[0].Objects;
    CompositePrimitive composite = objects[0] as CompositePrimitive;

    // Was a model in our composite picked?
    if (composite == models)
    {
        ModelPrimitive model = objects[1] as ModelPrimitive;

        // Selected Model
        model.Color = Color.Cyan;

        if (model != m_selectedModel)
        {
            // Unselect previous model
            if (m_selectedModel != null)
            {
                m_selectedModel.Color = Color.Red;
            }

            m_selectedModel = model;
            Scene.Render();
        }

        return;
    }
}

// Unselect previous model
if (m_selectedModel != null)
{
    m_selectedModel.Color = Color.Red;
    m_selectedModel = null;
    Scene.Render();
}
Roll Over Picking

m_selectedModel is a reference to the currently highlighted model, that is, the model currently under the cursor. Initially, it is null. Pick is called with the mouse cursor's position in response to a mouse move event.

The model under the cursor is highlighted by setting its color to cyan. The previously selected model is set to red. Note that a model may be unhighlighted because either another model is now under the cursor or no model in the composite in under the mouse cursor. Since redrawing the scene every time the mouse moves may affect performance, the scene is only redrawn (by calling Render) if a model is highlighted or unhighlighted.

Drill Picking

Drill picking is used to "drill down" to objects underneath the top object. For example, the first time a user double clicks, the top object is picked. If the mouse doesn't move and the scene doesn't change (e.g. primitives aren't added or removed), the second time the user double clicks, the object underneath the top object is picked, and so on.

This is implemented using the Pick method. The collection from the first call is saved and the closest object (the object at index 0) is the picked object. The next time a pick occurs (e.g. next double click), if the mouse position and scene did not change, the picked object is now at index 1 in the collection. Typically, the index will roll back to 0 once it goes all the way through the collection.

Rectangular Picking

Rectangular, or rubber band picking, is used to pick objects within a rectangular region. For example, when a 100 by 100 rectangle is picked, all the objects within that rectangle will be returned.

Rectangular picking is provided by the PickRectangular method. The following code sample executes a rectangular pick, and then iterates through all of the objects that were contained within that rectangle:

Picking Rectangular
C#
Collection<PickResult> collection = Scene.PickRectangular(mouseX - 50, mouseY + 50, mouseX + 50, mouseY - 50);
foreach (PickResult pickResult in collection)
{
    Collection<object> objects = pickResult.Objects;
    CompositePrimitive composite = objects[0] as CompositePrimitive;

    if (composite == modelsComposite)
    {
        // A model in our composite was among the items that were picked
        ModelPrimitive model = objects[1] as ModelPrimitive;
    }
}