DeviationAnalysis: raycast selection.
This is the second post of of series about technical insights of the development of the new version of DeviationAnalysis, one of the main tools of cloud2model. You can read the first one here.
The new 1.13 release brings great improvements in the usability of DeviationAnalysis. We maintain the direct selection of individual faces, but we add two new selection modes that address two typical cases scenarios.
The first one, “SELECT OBJECTS”, is thought for elements with multiple faces that before must be selected one by one. Now we can just select the element and all the faces pointing to the camera will be automatically selected.
The second one, “SELECT BY CROSS WINDOW”, allows to make a rectangular window selection in the screen, with automatic detection of all the visible front elements that are partially or totally included in the rectangular area. For each element, all the faces looking to the camera will be automatically added.
This blog entry is about the development of the internal raycast engine that allows “SELECT BY CROSS WINDOW”.
The case scenario.
This kind of selection is thought for easy selection of many building envelope faces. In just one easy step we can add to the selection faces of many diferent walls, floor and roofs. The equivalent selection of picking individual faces will be heavy time consuming. Using the new “SELECT OBJECTS”, will be better that picking the faces one by one, but the facades and roofs can have many different objects that we have to select individually.
And why not using “SELECT OBJECTS” but with a normal window selection?. This is key of the problem. If we do so, we select not only the external facades, floors and roofs, but any interior object that included in the area of the pointed window. And we only want the external faces of the external objects. We want to automatically avoid any object beyond the first visible object looking to the camera.
Raycasting.
Raycast is a conventional procedure of any graphic engine. It is basically to found the existing elements first intersected from a point following a direction. A conventional use is to select objects with the pointer in the screen: from the pointer position we cast a ray in the camera direction and with the intersection result we can check if we are on top of any element.
We can reasonably assume that the the Revit internal graphic engine is using some sort of raycast for individual selection and highlighting when the pointer is over an element. Anyway, there is not any low level raycast exposed in the Revit API. And it is not really needed, because the normal objects selection is directly exposed.
But the Revit API has ReferenceIntersector, that provides some sort of raycast funcionality. It is not a low level high performance tool, as you will expect in a graphic engine. It is more a high level tool, thought for analytical ray trace studies. But it is fast enough. And this is important in this case, because we make hundred of rays when using “SELECT BY CROSS WINDOW”.
A network of rays.
A raycast is done typically in the context of single ray detection. But the use here is different. We want to find the first objects in the direction of the camera, not just in a point, but in the whole are of the window selection. The system is based in setting a grid of points by subdividing the rectangular area pointed in the screen. And we will make a raycast from each point.
The subdivision size is critical for finding the balance between performance and reliability. Too small will be good for catching small elements (very reliable) but bad for the performance (too many rays). Too big will imply to miss some small elements (not reliable) but good for the performance (not so many rays).
The smaller the better.
There are two main concepts to take into account for the efficient use of the tool:
If a object is “detected” for any ray of the grid, all its faces looking in the camera direction will be automatically added. There is no need that the window selection includes fully the object or the faces to add. This must be only partially overlapped by the selection, in a similar way of the window “cross” selection of Autocad.
The subdivision size of the grid division is set in absolute model dimensions. This is needed in order to maintain the reliability of the tool. The larger the window area pointed in the screen, the more rays to be processed.
We must avoid larger than needed window areas, if possible. In the example above, we would like to select all the external faces of walls and roofs looking to the camera. If the left side, we are using a much bigger than needed window area, with many more rays than needed. In the right side, we use a much better sized window area, that will give exactly the same result with far less rays.