This section reviews the interface for selecting objects in the Visualize database. The APIs covered here are fairly low-level, so if you need a ready-made solution that takes care of selection details, you are encouraged to use a selection operator.
The Visualize selection process involves the following steps:
- your application receives selection-location information from the GUI, based on user input
- your application calls Visualize selection routines whose arguments specify a selection point, ray, rectangle, polyline, polygon, volume or shell. The HPS::SelectionControl class is available from HPS::WindowKey, and provides the various SelectBy[type] methods.
- Visualize returns the selection results to you via HPS::SelectionResults
- your application code operates on the selection results. It may highlight, edit, delete and/or query the selected item(s). Note that in addition to selection operators, highlight operators are also available.
Visualize supports various selection options that allow you to control what should be selected, and to make selection faster and thus more interactive for the user. These are important, as selection can be computationally intensive over large datasets. Visualize can also return results much faster if you set up filters to exclude geometry that you aren't interested in.
The HPS::SelectionOptionsControl class is available from a HPS::WindowKey. Alternatively, you can specify the options in a HPS::SelectionOptionsKit and pass it into the variants of the HPS::SelectionControl::SelectBy[type] methods that accept a HPS::SelectionOptionsKit.
The SelectBy[type] methods all perform a selection based on a start segment. The start segment and the contents of all descendant segments will be tested against the selection criteria. This is an optimization you can use if you know your selection should be restricted to a certain subtree. The start segment is set using the HPS::SelectionOptionsKit::SetScope method. The method also accepts a HPS::KeyPath object which can be used to initialize a start segment that's not under a window key. It is expected to be a path, from leaf to root, through any number of include segments. The leaf is the start segment and the root is the window key.
HPS::SelectionOptionsControl::SetAlgorithm allows you to choose between two types of selection algorithms: analytic and visual.
This is the default selection algorithm, and is based on a projection from the camera position (or other arbitrary position) through a point or points in world coordinates. It does not ignore objects which are obscured by other objects, but will return the list of objects that are intersected by the provided selection information. It also sorts the objects from back to front (relative to viewpoint space) so that the object foremost in the view will be first in the list of selection results.
In some situations, this can give unwanted results. For example, if both faces and edges are visible and selectable, and you select slightly to the side of the front-most visible edge of a cube, it's possible that the 'best' edge returned by Visualize will be the back side edge, even though it is obscured. This is because Visualize fires a ray through the scene, and the ray may end up directly hitting the backside edge or coming closest to it. To avoid this scenario, you can instruct the system to use the visual selection algorithm described below.
If you only want visible objects to be selectable, then the visual selection algorithm can be specified. Visual selection performs a frame-buffer based postprocess of the selected items to remove obscured items from the selection list. It is important to note that visual selection is more algorithmically intensive than analytic selection.
Before doing any sort of selection, it is important to configure the selection level because this directly affects the selection results. Selection level is controlled by calling HPS::SelectionOptionsControl::SetLevel, which in turns allows you to chose from three different levels. An example of setting the selection level is shown here.
NOTE: Cylinders and spheres are a special type of primitive geometry. They are dynamically tessellated, and therefore their subentities are not selectable. If you try to do subentity selection on a sphere or cylinder, the entity key will be returned but there will be no subentity information.
In addition to the selection level, the proximity also has a large impact on returned results. The selection proximity is a floating point value that augments a point-based selection by also including the area immediately surrounding the selection point (point-based selection is sometimes referred to as 'aperture' selection). The proximity is specified in real-world centimeters. To disable, set the proximity to zero.
NOTE: Proximity has no effect in area and volume selections.
The granularity of a selection determines whether the selection test is performed on the exact shape of certain entity types. One exception is a line segment or polyline. For lines, a Detailed selection granularity has an effect when the line has a special pattern or heavy weight. For example, in a dashed line, the dashes would be selectable but the space between them would not. For a heavy weight line, the increased line thickness actually becomes part of the selectable region of the line.
Using General granularity will typically provide the best performance.
A point selection is one of the simplest types of selection and is performed by calling HPS::SelectionControl::SelectByPoint. It is based on a window-space point, and any objects underneath the projected point are returned as potential selection objects, even if they are obscured by geometry in the foreground.
For this example, we are using the pixel coordinates of an imaginary mouse click, then converting those pixel coordinates to window space. Note that this example sets up the selection options using the HPS::SelectionOptionsControl.
In the code snippet above, the value returned by HPS::SelectionControl::SelectByPoint is the number of objects found in the selection. An entity-level selection was performed, so a collection of geometric entities would be expected. Likewise, if a segment-level selection was performed, a HPS::SegmentKey array would be returned. For an expanded example, click here.
The objects themselves can be accessed through the HPS::SelectionResults object as shown below:
IMPORTANT: Using the Type() function has performance implications. Please see our section on using keys for details.
Selection by ray
A similar type of selection is also available - HPS::SelectionControl::SelectByRay. Selection by ray is similar to selection by point, but can perform a selection based on any arbitrary ray.
Two other ways to perform a selection are by area and by volume. An area selection is achieved by specifying a rectangle in window space. The snippet below shows the basic procedure, and in this example, note that the selection options are set up in a HPS::SelectionOptionsKit, which is then passed into a different variant of HPS::SelectionControl::SelectByArea method:
We should note that selecting by area will project the selection area through 3D space, and thus forms a selection frustum with infinite near and far planes. Similarly, a volume selection will select entities within a cuboid volume. Specify the two opposite corners of the cuboid to select against:
Subentities are the faces, edges, and vertices that make up geometry. HOOPS Visualize supports subentity selection for shells and meshes. Some functionality is also available for lines, text, polygons, and NURBS curves.
The framework for selecting subentities is similar to entity-level selection. The only difference during the selection stage is setting the selection level as HPS::Selection::Level::Subentity.
However, during the retrieval stage, you also need to explicitly get the subentities. Faces, vertices, and edges are returned in separate arrays. Note that edges are returned in two parallel arrays where the edge is defined by its corresponding endpoints in each array. The point in the first array is considered the closer match to the selection criteria.
Please note that as with all Show() functions, if the information is undefined or non-existent, the function will return false. Accordingly, for entities other than shells or meshes, the following conditions apply:
- Polygon: ShowEdges() will return only the nearest single edge, and ShowVertices() will return only the nearest single vertex. ShowFaces() is not supported.
- NURBS Curve: Only ShowVertices() is supported, and it will return the nearest single control point.
- Text: Only ShowCharacters() is supported, and it will return the nearest character in the text string.
- Lines: Only ShowEdges() is supported, and it will return all the line segments (defined by pairs of points) that were selected.
It may be necessary to determine if a piece of geometry intersects a particular shell. This is commonly referred to as collision or clash detection. Detecting intersections between shells is implemented as another form of selection - selection by shell. Selection by shell is always performed as an analytic selection.
The user provides a shell kit to HPS::Database::SelectByShell, and Visualize uses it to calculate a selection on any intersecting faceted items within or below the start segment (faceted items include shells, meshes, NURBS surfaces, circles, ellipses, polygons, and polycylinders). The start segment can be set using HPS::SelectionOptionsKit::SetScope. The selection process is subject, as usual, to selectability attributes. The return value indicates the number of items selected. The selection is always performed in world space, so the method does not need to be called from HPS::WindowKey. If any transform needs to be applied to the points, it must be applied before calling SelectByShell.
Also related to selection by shell is the shell relation interface, used to determine spatial relationships between shells and point sets.
Proximity value for collision detection
When performing a selection by shell, the selection proximity has a slightly different meaning from the proximity value of other selection methods. In this context, a positive proximity value of x will cause the selection algorithm to detect a collision when the distance between the two bodies is x or less. This means the bodies do not have to be touching in order for Visualize to detect a collision.
If the proximity value is equal to 0, the bodies must be coincident or penetrating for a collision to be detected. When the proximity value has a negative value, for example, -10, the bodies must penetrate each other at least 10 units before a collision is detected.
In the following diagram, the objects are separated by 0.08 units. Normally, this is not a collision. However, it would be detected as such if the selection proximity value was 0.08 or greater.
Below, the objects penetrate each other to a depth of 0.16. This is a collision in all cases except for selection proximity values less than -0.16.
Using a TreeContext to improve performance
Use of a HPS::TreeContext may be beneficial when selecting by shell. If many shell selections are going to be performed without modifying the segment tree, using an HPS::TreeContext can significantly boost performance if you have many selections to perform between modifying the segment hierarchy. If a tree is not specified, a new one is computed each time a selection is performed. To use the tree context with your selection, simply instantiate a HPS::TreeContext object and pass it to HPS::SelectionOptionsKit::SetTreeContext.
So far, we've discussed different selection algorithms, as well as different ways to select objects. There are also several ways to limit selection results to a subset of scene geometry.
The HPS::SelectabilityControl allows you to specify which types of geometry (markers, vertices, edges, lines, faces) and what aspects of the scene (windows, lights) are actually selectable by the selection routines. If the selectability of a type of scene-graph item is disabled, then the selection routines will not select on them, thereby filtering results and speeding up the overall selection process.
Let's look at a sample case. Imagine we have a large amount of faceted geometry represented by shells, and the faces, edges, and vertices are all visible - but we only want to select the faces. We would use the HPS::SelectabilityControl to indicate that all items except for faces are selectable. The easiest way is to first call HPS::SelectabilityControl::SetEverything(false) to turn everything off, and then turn on selectability just for faces.
If a geometry type is not currently visible (due to the HPS::VisibilityControl having been used to indicate that its visibility is off), then the selection routines will not select that type. If you want the selection routines to still select items even if their visibility is set to off, you must specify the HPS::Selectability::Value::ForcedOn value for those types, when calling one of the HPS::SelectabilityControl 'Set' methods.
When doing entity-level or segment-level selection, another way to limit results is to use HPS::SelectionOptionsKit::SetRelatedLimit. This directive instructs the selection mechanism to limit the number of objects that are selected and returned. For example, setting the related limit to 2 means that only 2 items will be returned, with preference based on proximity to the selection point (if proximity applies). This can be used in conjunction with the HPS::SelectabilityControl.
This limits the number of subentities for shells and meshes that will be returned if performing subentity selection, and is set via SelectionOptionsKit::SetInternalLimit.
The following code sample demonstrates these three selection options:
HOOPS Visualize offers various types of culling to improve rendering performance. You have the ability to control whether culled objects are selectable using the HPS::SelectionOptionsKit or HPS::SelectionOptionsControl. If selection culling is disabled, HOOPS Visualize will consider culled objects when processing a selection event. As a result, the user could end up highlighting culled geometry, suddenly making it appear visually in the scene or as an entry in selection results.
Selection culling can be used with deferral extent, extent, frustum, and vector culling.
Boolean operations on selection results
Selection results may be further refined or augmented by applying Boolean operations to two HPS::SelectionResults objects. Operations include union, intersection, difference, and symmetric difference. These operations are especially useful during user interaction.
For example, let's say the user has performed an area selection and was returned a set of results. Before applying his operation, he notices there is a single object that does not belong in the selection set. It is common to expect the ability to hold CTRL (or another modifier key) and click a selected object to deselect it.
Programmatically speaking, both the first area selection and the second CTRL-click selection would each be represented by a single HPS::SelectionResults object. As the developer, you can call HPS::SelectionResults::SymmetricDifference to remove the second selection set from the first.
You can also expect this behavior to happen automatically when using a selection operator. When control is held, the HPS::SelectAreaOperator will perform a union with the another results object, and the HPS::SelectOperator will perform a symmetric difference (exclusive OR).
The Boolean operations only work for selections that were performed at the same selection level. For example, trying to apply a Boolean operation to an entity-level selection and a segment-level selection will not work.
The purpose of the selection operator is to perform a selection at a particular pixel in the window. This usually means at the mouse cursor or at the location of a touch-screen event. If you have some special selection mechanism that isn't covered by these input events, you are free to perform a manual selection or create your own custom operator.
When using the HPS::SelectOperator or HPS::SelectAreaOperator, you push them onto the operator stack just like any other operator. When selection is performed, results are returned to you in a HPS::SelectionResults object. Once you have the results, you can parse them according to your needs. An example of how to do that is demonstrated here.
You can control how the selection is performed by configuring a HPS::SelectionOptionsKit and setting it on the operator. The kit controls things such as internal limit, level, and proximity. The selection process is detailed in following snippet: