1 Visualization {#occt_user_guides__visualization}
2 ========================
5 @section occt_visu_1 Introduction
7 Visualization in Open CASCADE Technology is based on the separation of:
8 * on the one hand -- the data which stores the geometry and topology of the entities you want to display and select, and
9 * on the other hand -- its **presentation** (what you see when an object is displayed in a scene) and **selection** (possibility to choose the whole object or its sub-parts interactively to apply application-defined operations to the selected entities).
11 Presentations are managed through the **Presentation** component, and selection through the **Selection** component.
13 **Application Interactive Services** (AIS) provides the means to create links between an application GUI viewer and the packages, which are used to manage selection and presentation, which makes management of these functionalities in 3D more intuitive and consequently, more transparent.
15 *AIS* uses the notion of the *Interactive Object*, a displayable and selectable entity, which represents an element from the application data. As a result, in 3D, you, the user, have no need to be familiar with any functions underlying AIS unless you want to create your own interactive objects or selection filters.
17 If, however, you require types of interactive objects and filters other than those provided, you will need to know the mechanics of presentable and selectable objects, specifically how to implement their virtual functions. To do this requires familiarity with such fundamental concepts as the Sensitive Primitive and the Presentable Object.
19 The the following packages are used to display 3D objects:
27 The packages used to display 3D objects are also applicable for visualization of 2D objects.
29 The figure below presents a schematic overview of the relations between the key concepts and packages in visualization. Naturally, "Geometry & Topology" is just an example of application data that can be handled by *AIS*, and application-specific interactive objects can deal with any kind of data.
31 @figure{visualization_image003.png,"Key concepts and packages in visualization",400}
33 To answer different needs of CASCADE users, this User's Guide offers the following three paths in reading it.
35 * If the 3D services proposed in AIS meet your requirements, you need only read chapter 3 @ref occt_visu_3 "AIS: Application Interactive Services".
36 * If you need more detail, for example, a selection filter on another type of entity -- you should read chapter 2 @ref occt_visu_2 "Fundamental Concepts", chapter 3 @ref occt_visu_3 "AIS: Application Interactive Services", and 4 @ref occt_visu_4 "3D Presentations". You may want to begin with the chapter presenting AIS.
38 For advanced information on visualization algorithms, see our <a href="https://www.opencascade.com/content/tutorial-learning">E-learning & Training</a> offerings.
40 @section occt_visu_2 Fundamental Concepts
42 @subsection occt_visu_2_1 Presentation
44 In Open CASCADE Technology, presentation services are separated from the data, which they represent, which is generated by applicative algorithms. This division allows you to modify a geometric or topological algorithm and its resulting objects without modifying the visualization services.
46 @subsubsection occt_visu_2_1_1 Structure of the Presentation
48 Displaying an object on the screen involves three kinds of entities:
49 * a presentable object, the *AIS_InteractiveObject*
51 * an interactive context, the *AIS_InteractiveContext*.
53 #### The presentable object
55 The purpose of a presentable object is to provide the graphical representation of an object in the form of *Graphic3d* structure. On the first display request, it creates this structure by calling the appropriate algorithm and retaining this framework for further display.
57 Standard presentation algorithms are provided in the *StdPrs* and *Prs3d* packages. You can, however, write specific presentation algorithms of your own, provided that they create presentations made of structures from the *Graphic3d* packages. You can also create several presentations of a single presentable object: one for each visualization mode supported by your application.
59 Each object to be presented individually must be presentable or associated with a presentable object.
63 The viewer allows interactively manipulating views of the object. When you zoom, translate or rotate a view, the viewer operates on the graphic structure created by the presentable object and not on the data model of the application. Creating Graphic3d structures in your presentation algorithms allows you to use the 3D viewers provided in Open CASCADE Technology for 3D visualisation.
65 #### The Interactive Context
67 The interactive context controls the entire presentation process from a common high-level API. When the application requests the display of an object, the interactive context requests the graphic structure from the presentable object and sends it to the viewer for displaying.
69 @subsubsection occt_visu_2_1_2 Presentation packages
71 Presentation involves at least the *AIS, PrsMgr, StdPrs* and *V3d* packages. Additional packages, such as *Prs3d* and *Graphic3d* may be used if you need to implement your own presentation algorithms.
73 * *AIS* package provides all classes to implement interactive objects (presentable and selectable entities).
74 * *PrsMgr* package provides low level services and is only to be used when you do not want to use the services provided by AIS. It contains all classes needed to implement the presentation process: abstract classes *Presentation* and *PresentableObject* and concrete class *PresentationManager3d*.
75 * *StdPrs* package provides ready-to-use standard presentation algorithms for specific geometries: points, curves and shapes of the geometry and topology toolkits.
76 * *Prs3d* package provides generic presentation algorithms such as wireframe, shading and hidden line removal associated with a *Drawer* class, which controls the attributes of the presentation to be created in terms of color, line type, thickness, etc.
77 * *V3d* package provides the services supported by the 3D viewer.
78 * *Graphic3d* package provides resources to create 3D graphic structures.
79 * *Visual3d* package contains classes implementing commands for 3D viewer.
80 * *DsgPrs* package provides tools for display of dimensions, relations and XYZ trihedrons.
82 @subsubsection occt_visu_2_1_3 A Basic Example: How to display a 3D object
85 Handle(V3d_Viewer) theViewer;
86 Handle(AIS_InteractiveContext) aContext = new AIS_InteractiveContext (theViewer);
88 BRepPrimAPI_MakeWedge aWedgeMaker (theWedgeDX, theWedgeDY, theWedgeDZ, theWedgeLtx);
89 TopoDS_Solid aShape = aWedgeMaker.Solid();
90 Handle(AIS_Shape) aShapePrs = new AIS_Shape (aShape); // creation of the presentable object
91 aContext->Display (aShapePrs, AIS_Shaded, 0, true); // display the presentable object and redraw 3d viewer
94 The shape is created using the *BRepPrimAPI_MakeWedge* command. An *AIS_Shape* is then created from the shape. When calling the *Display* command, the interactive context calls the Compute method of the presentable object to calculate the presentation data and transfer it to the viewer. See figure below.
96 @figure{visualization_image004.svg,"Processes involved in displaying a presentable shape",400}
98 @subsection occt_visu_2_2 Selection
100 Standard OCCT selection algorithm is represented by 2 parts: dynamic and static. Dynamic selection causes objects to be automatically highlighted as the mouse cursor moves over them. Static selection allows to pick particular object (or objects) for further processing.
102 There are 3 different selection types:
103 - **Point selection** -- allows picking and highlighting a single object (or its part) located under the mouse cursor;
104 - **Rectangle selection** -- allows picking objects or parts located under the rectangle defined by the start and end mouse cursor positions;
105 - **Polyline selection** -- allows picking objects or parts located under a user-defined non-self-intersecting polyline.
107 For OCCT selection algorithm, all selectable objects are represented as a set of sensitive zones, called **sensitive entities**. When the mouse cursor moves in the view, the sensitive entities of each object are analyzed for collision.
109 @subsubsection occt_visu_2_2_1 Terms and notions
111 This section introduces basic terms and notions used throughout the algorithm description.
113 #### Sensitive entity
115 Sensitive entities in the same way as entity owners are links between objects and the selection mechanism.
117 The purpose of entities is to define what parts of the object will be selectable in particular. Thus, any object that is meant to be selectable must be split into sensitive entities (one or several). For instance, to apply face selection to an object it is necessary to explode it into faces and use them for creation of a sensitive entity set.
119 @figure{visualization_image005.png,"Example of a shape divided into sensitive entities",400}
121 Depending on the user's needs, sensitive entities may be atomic (point or edge) or complex. Complex entities contain many sub-elements that can be handled by detection mechanism in a similar way (for example, a polyline stored as a set of line segments or a triangulation).
123 Entities are used as internal units of the selection algorithm and do not contain any topological data, hence they have a link to an upper-level interface that maintains topology-specific methods.
127 Each sensitive entity stores a reference to its owner, which is a class connecting the entity and the corresponding selectable object. Besides, owners can store any additional information, for example, the topological shape of the sensitive entity, highlight colors and methods, or if the entity is selected or not.
131 To simplify the handling of different selection modes of an object, sensitive entities linked to their owners are organized into sets, called **selections**.
132 Each selection contains entities created for a certain mode along with the sensitivity and update states.
134 #### Selectable object
136 Selectable object stores information about all created selection modes and sensitive entities.
138 All successors of a selectable object must implement the method that splits its presentation into sensitive entities according to the given mode. The computed entities are arranged in one selection and added to the list of all selections of this object. No selection will be removed from the list until the object is deleted permanently.
140 For all standard OCCT shapes, zero mode is supposed to select the whole object (but it may be redefined easily in the custom object). For example, the standard OCCT selection mechanism and *AIS_Shape* determine the following modes (see AIS_Shape::SelectionMode()):
141 - 0 -- selection of the entire object *(AIS_Shape)*;
142 - 1 -- selection of the vertices (TopAbs_VERTEX);
143 - 2 -- selection of the edges (TopAbs_EDGE);
144 - 3 -- selection of the wires (TopAbs_WIRE);
145 - 4 -- selection of the faces (TopAbs_FACE);
146 - 5 -- selection of the shells (TopAbs_SHELL);
147 - 6 -- selection of the constituent solids (TopAbs_SOLID).
149 @figure{visualization_image006.png,"Hierarchy of references from sensitive entity to selectable object",400}
151 @figure{visualization_image007.png,"The principle of entities organization within the selectable object",400}
155 For each OCCT viewer there is a **Viewer selector** class instance. It provides a high-level API for the whole selection algorithm and encapsulates the processing of objects and sensitive entities for each mouse pick.
156 The viewer selector maintains activation and deactivation of selection modes, launches the algorithm, which detects candidate entities to be picked, and stores its results, as well as implements an interface for keeping selection structures up-to-date.
158 #### Selection manager
160 Selection manager is a high-level API to manipulate selection of all displayed objects. It handles all viewer selectors, activates and deactivates selection modes for the objects in all or particular selectors, manages computation and update of selections for each object. Moreover, it keeps selection structures updated taking into account applied changes.
162 @figure{visualization_image008.png,"The relations chain between viewer selector and selection manager",400}
164 @subsubsection occt_visu_2_2_2 Algorithm
166 All three types of OCCT selection are implemented as a single concept, based on the search for overlap between frustum and sensitive entity through 3-level BVH tree traversal.
168 #### Selection Frustum
170 The first step of each run of selection algorithm is to build the selection frustum according to the currently activated selection type.
172 For the point or the rectangular selection the base of the frustum is a rectangle built in conformity with the pixel tolerance or the dimensions of a user-defined area, respectively. For the polyline selection, the polygon defined by the constructed line is triangulated and each triangle is used as the base for its own frustum. Thus, this type of selection uses a set of triangular frustums for overlap detection.
174 The frustum length is limited by near and far view volume planes and each plane is built parallel to the corresponding view volume plane.
176 @figure{visualization_image009.png,"",400}
178 The image above shows the rectangular frustum: a) after mouse move or click, b) after applying the rectangular selection.
180 @figure{visualization_image010.png,"",400}
182 In the image above triangular frustum is set: a) by a user-defined polyline, b) by triangulation of the polygon based on the given polyline, c) by a triangular frustum based on one of the triangles.
186 To maintain selection mechanism at the viewer level, a speedup structure composed of 3 BVH trees is used.
188 The first level tree is constructed of axis-aligned bounding boxes of each selectable object. Hence, the root of this tree contains the combination of all selectable boundaries even if they have no currently activated selections. Objects are added during the display of *AIS_InteractiveObject* and will be removed from this tree only when the object is destroyed. The 1st level BVH tree is build on demand simultaneously with the first run of the selection algorithm.
190 The second level BVH tree consists of all sensitive entities of one selectable object. The 2nd level trees are built automatically when the default mode is activated and rebuilt whenever a new selection mode is calculated for the first time.
192 The third level BVH tree is used for complex sensitive entities that contain many elements: for example, triangulations, wires with many segments, point sets, etc. It is built on demand for sensitive entities with under 800K sub-elements.
194 @figure{visualization_image022.png,"Selection BVH tree hierarchy: from the biggest object-level (first) to the smallest complex entity level (third)",400}
196 #### Stages of the algorithm
198 The algorithm includes pre-processing and three main stages.
202 Implies calculation of the selection frustum and its main characteristics.
204 ##### First stage -- traverse of the first level BVH tree
206 After successful building of the selection frustum, the algorithm starts traversal of the object-level BVH tree. The nodes containing axis-aligned bounding boxes are tested for overlap with the selection frustum following the terms of *separating axis theorem (SAT)*. When the traversal goes down to the leaf node, it means that a candidate object with possibly overlapping sensitive entities has been found. If no such objects have been detected, the algorithm stops and it is assumed that no object needs to be selected. Otherwise it passes to the next stage to process the entities of the found selectable object.
208 ##### Second stage -- traversal of the second level BVH tree
210 At this stage it is necessary to determine if there are candidates among all sensitive entities of one object.
212 First of all, at this stage the algorithm checks if there is any transformation applied for the current object. If it has its own location, then the correspondingly transformed frustum will be used for further calculations. At the next step the nodes of the second level BVH tree of the given object are visited to search for overlapping leaves. If no such leafs have been found, the algorithm returns to the second stage. Otherwise it starts processing the found entities by performing the following checks:
213 - activation check - the entity may be inactive at the moment as it belongs to deactivated selection;
214 - tolerance check - current selection frustum may be too large for further checks as it is always built with the maximum tolerance among all activated entities. Thus, at this step the frustum may be scaled.
216 After these checks the algorithm passes to the last stage.
218 ##### Third stage -- overlap or inclusion test of a particular sensitive entity
220 If the entity is atomic, a simple SAT test is performed. In case of a complex entity, the third level BVH tree is traversed. The quantitative characteristics (like depth, distance to the center of geometry) of matched sensitive entities is analyzed and clipping planes are applied (if they have been set). The result of detection is stored and the algorithm returns to the second stage.
222 @subsubsection occt_visu_2_2_3 Packages and classes
224 Selection is implemented as a combination of various algorithms divided among several packages -- *SelectBasics*, *Select3D*, *SelectMgr* and *StdSelect*.
228 *SelectBasics* package contains basic classes and interfaces for selection. The most notable are:
229 - *SelectBasics_PickResult* -- the structure for storing quantitative results of detection procedure, for example, depth and distance to the center of geometry;
230 - *SelectBasics_SelectingVolumeManager* -- the interface for interaction with the current selection frustum.
232 Each custom sensitive entity must inherit at least *SelectBasics_SensitiveEntity*.
236 *Select3D* package provides a definition of standard sensitive entities, such as:
248 Each basic sensitive entity inherits *Select3D_SensitiveEntity*.
249 The package also contains two auxiliary classes, *Select3D_SensitivePoly* and *Select3D_SensitiveSet*.
251 *Select3D_SensitiveEntity* -- the base definition of a sensitive entity.
253 *Select3D_SensitiveSet* -- a base class for all complex sensitive entities that require the third level BVH usage. It implements traverse of the tree and defines an interface for the methods that check sub-entities.
255 *Select3D_SensitivePoly* -- describes an arbitrary point set and implements basic functions for selection. It is important to know that this class does not perform any internal data checks. Hence, custom implementations of sensitive entity inherited from *Select3D_SensitivePoly* must satisfy the terms of Separating Axis Theorem to use standard OCCT overlap detection methods.
259 *SelectMgr* package is used to maintain the whole selection process. For this purpose, the package provides the following services:
260 - activation and deactivation of selection modes for all selectable objects;
261 - interfaces to compute selection mode of the object;
262 - definition of selection filter classes;
263 - keeping selection BVH data up-to-date.
265 A brief description of the main classes:
266 - *SelectMgr_BaseFrustum*, *SelectMgr_Frustum*, *SelectMgr_RectangularFrustum*, *SelectMgr_TriangularFrustum* and *SelectMgr_TriangularFrustumSet* -- interfaces and implementations of selecting frustums, these classes implement different SAT tests for overlap and inclusion detection. They also contain methods to measure characteristics of detected entities (depth, distance to center of geometry);
267 - *SelectMgr_SensitiveEntity*, *SelectMgr_Selection* and *SelectMgr_SensitiveEntitySet* -- store and handle sensitive entities; *SelectMgr_SensitiveEntitySet* implements a primitive set for the second level BVH tree;
268 - *SelectMgr_SelectableObject* and *SelectMgr_SelectableObjectSet* -- describe selectable objects. They also manage storage, calculation and removal of selections. *SelectMgr_SelectableObjectSet* implements a primitive set for the first level BVH tree;
269 - *SelectMgr_ViewerSelector* -- encapsulates all logics of the selection algorithm and implements the third level BVH tree traverse;
270 - *SelectMgr_SelectionManager* -- manages activation/deactivation, calculation and update of selections of every selectable object, and keeps BVH data up-to-date.
274 *StdSelect* package contains the implementation of some *SelectMgr* classes and tools for creation of selection structures. For example,
275 - *StdSelect_BRepOwner* -- defines an entity owner with a link to its topological shape and methods for highlighting;
276 - *StdSelect_BRepSelectionTool* -- contains algorithms for splitting standard AIS shapes into sensitive primitives;
277 - *StdSelect_ViewerSelector3d* -- an example of *SelectMgr_ViewerSelector* implementation, which is used in a default OCCT selection mechanism;
278 - *StdSelect_FaceFilter*, *StdSelect_EdgeFilter* -- implementation of selection filters.
280 @subsubsection occt_visu_2_2_4 Examples of usage
282 The first code snippet illustrates the implementation of *SelectMgr_SelectableObject::ComputeSelection()* method in a custom interactive object. The method is used for computation of user-defined selection modes.
283 Let us assume it is required to make a box selectable in two modes -- the whole shape (mode 0) and each of its edges (mode 1).
284 To select the whole box, the application can create a sensitive primitive for each face of the interactive object. In this case, all primitives share the same owner -- the box itself.
285 To select box's edge, the application must create one sensitive primitive per edge. Here all sensitive entities cannot share the owner since different geometric primitives must be highlighted as the result of selection procedure.
288 void InteractiveBox::ComputeSelection (const Handle(SelectMgr_Selection)& theSel,
289 const Standard_Integer theMode)
293 case 0: // creation of face sensitives for selection of the whole box
295 Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this, 5);
296 for (Standard_Integer aFaceIter = 1; aFaceIter <= myNbFaces; ++aFaceIter)
298 Select3D_TypeOfSensitivity aSensType = myIsInterior;
299 theSel->Add (new Select3D_SensitiveFace (anOwner, myFaces[aFaceIter]->PointArray(), aSensType));
303 case 1: // creation of edge sensitives for selection of box edges only
305 for (Standard_Integer anEdgeIter = 1; anEdgeIter <= 12; ++anEdgeIter)
307 // 1 owner per edge, where 6 is a priority of the sensitive
308 Handle(MySelection_EdgeOwner) anOwner = new MySelection_EdgeOwner (this, anEdgeIter, 6);
309 theSel->Add (new Select3D_SensitiveSegment (anOwner, myFirstPnt[anEdgeIter]), myLastPnt[anEdgeIter]));
317 The algorithms for creating selection structures store sensitive primitives in *SelectMgr_Selection* instance. Each *SelectMgr_Selection* sequence in the list of selections of the object must correspond to a particular selection mode.
318 To describe the decomposition of the object into selectable primitives, a set of ready-made sensitive entities is supplied in *Select3D* package. Custom sensitive primitives can be defined through inheritance from *Select3D_SensitiveEntity*.
319 To make custom interactive objects selectable or customize selection modes of existing objects, the entity owners must be defined. They must inherit *SelectMgr_EntityOwner* interface.
321 Selection structures for any interactive object are created in *SelectMgr_SelectableObject::ComputeSelection()* method.
322 The example below shows how computation of different selection modes of the topological shape can be done using standard OCCT mechanisms, implemented in *StdSelect_BRepSelectionTool*.
325 void MyInteractiveObject::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
326 const Standard_Integer theMode)
330 case 0: StdSelect_BRepSelectionTool::Load (theSelection, this, myShape, TopAbs_SHAPE); break;
331 case 1: StdSelect_BRepSelectionTool::Load (theSelection, this, myShape, TopAbs_VERTEX); break;
332 case 2: StdSelect_BRepSelectionTool::Load (theSelection, this, myShape, TopAbs_EDGE); break;
333 case 3: StdSelect_BRepSelectionTool::Load (theSelection, this, myShape, TopAbs_WIRE); break;
334 case 4: StdSelect_BRepSelectionTool::Load (theSelection, this, myShape, TopAbs_FACE); break;
339 The *StdSelect_BRepSelectionTool* class provides a high level API for computing sensitive entities of the given type (for example, face, vertex, edge, wire and others) using topological data from the given *TopoDS_Shape*.
341 The traditional way of highlighting selected entity owners adopted by Open CASCADE Technology assumes that each entity owner highlights itself on its own. This approach has two drawbacks:
343 - each entity owner has to maintain its own *Graphic3d_Structure* object, that results in a considerable memory overhead;
344 - drawing selected owners one by one is not efficient from the visualization point of view.
346 Therefore, to overcome these limitations, OCCT has an alternative way to implement the highlighting of a selected presentation. Using this approach, the interactive object itself will be responsible for the highlighting, not the entity owner.
348 On the basis of *SelectMgr_EntityOwner::IsAutoHilight()* return value, *AIS_InteractiveContext* object either uses the traditional way of highlighting (in case if *IsAutoHilight()* returns TRUE) or groups such owners according to their selectable objects and finally calls *SelectMgr_SelectableObject::HilightSelected()* or *SelectMgr_SelectableObject::ClearSelected()*, passing a group of owners as an argument.
350 Hence, an application can derive its own interactive object and redefine virtual methods *HilightSelected()*, *ClearSelected()* and *HilightOwnerWithColor()* from *SelectMgr_SelectableObject*. *SelectMgr_SelectableObject::GetHilightPresentation* and *SelectMgr_SelectableObject::GetSelectPresentation* methods can be used to optimize filling of selection and highlight presentations according to the user's needs.
352 After all the necessary sensitive entities are computed and packed in *SelectMgr_Selection* instance with the corresponding owners in a redefinition of *SelectMgr_SelectableObject::ComputeSelection()* method, it is necessary to register the prepared selection in *SelectMgr_SelectionManager* through the following steps:
353 - if there was no *AIS_InteractiveContext* opened, create an interactive context and display the selectable object in it;
354 - load the selectable object to the selection manager of the interactive context using *AIS_InteractiveContext::Load()* method. If the selection mode passed as a parameter to this method is not equal to -1, *ComputeSelection()* for this selection mode will be called;
355 - activate or deactivate the defined selection mode using *AIS_InteractiveContext::Activate()* or *AIS_InteractiveContext::Deactivate()* methods.
357 After these steps, the selection manager of the created interactive context will contain the given object and its selection entities, and they will be involved in the detection procedure.
359 The code snippet below illustrates the above steps. It also contains the code to start the detection procedure and parse the results of selection.
362 // Suppose there is an instance of class InteractiveBox from the previous sample.
363 // It contains an implementation of method InteractiveBox::ComputeSelection() for selection
364 // modes 0 (whole box must be selected) and 1 (edge of the box must be selectable)
365 Handle(InteractiveBox) theBox;
366 Handle(AIS_InteractiveContext) theContext;
367 // To prevent automatic activation of the default selection mode
368 theContext->SetAutoActivateSelection (false);
369 theContext->Display (theBox, false);
371 // Load a box to the selection manager without computation of any selection mode
372 theContext->Load (theBox, -1, true);
373 // Activate edge selection
374 theContext->Activate (theBox, 1);
376 // Run the detection mechanism for activated entities in the current mouse coordinates and in the current view.
377 // Detected owners will be highlighted with context highlight color
378 theContext->MoveTo (aXMousePos, aYMousePos, myView, false);
379 // Select the detected owners
380 theContext->Select();
381 // Iterate through the selected owners
382 for (theContext->InitSelected(); theContext->MoreSelected() && !aHasSelected; theContext->NextSelected())
384 Handle(AIS_InteractiveObject) anIO = theContext->SelectedInteractive();
387 // deactivate all selection modes for aBox1
388 theContext->Deactivate (aBox1);
391 It is also important to know, that there are 2 types of detection implemented for rectangular selection in OCCT:
392 - <b>inclusive</b> detection. In this case the sensitive primitive is considered detected only when all its points are included in the area defined by the selection rectangle;
393 - <b>overlap</b> detection. In this case the sensitive primitive is considered detected when it is partially overlapped by the selection rectangle.
395 The standard OCCT selection mechanism uses inclusion detection by default. To change this, use the following code:
398 // Assume there is a created interactive context
399 const Handle(AIS_InteractiveContext) theContext;
400 // Retrieve the current viewer selector
401 const Handle(StdSelect_ViewerSelector3d)& aMainSelector = theContext->MainSelector();
402 // Set the flag to allow overlap detection
403 aMainSelector->AllowOverlapDetection (true);
406 @section occt_visu_3 Application Interactive Services
407 @subsection occt_visu_3_1 Introduction
409 Application Interactive Services allow managing presentations and dynamic selection in a viewer in a simple and transparent manner.
410 The central entity for management of visualization and selections is the **Interactive Context**. It is connected to the main viewer.
412 Interactive context by default starts at **Neutral Point** with each selectable object picked as a whole, but the user might activate **Local Selection** for specific objects to make selectable parts of the objects.
413 Local/global selection is managed by a list of selection modes activated for each displayed object with 0 (default selection mode) usually meaning Global (entire object) selection.
415 **Interactive Objects** are the entities, which are visualized and selected. You can use classes of standard interactive objects for which all necessary functions have already been programmed, or you can implement your own classes of interactive objects, by respecting a certain number of rules and conventions described below.
417 An Interactive Object is a "virtual" entity, which can be presented and selected. An Interactive Object can have a certain number of specific graphic attributes, such as visualization mode, color and material.
418 When an Interactive Object is visualized, the required graphic attributes are taken from its own **Drawer** (*Prs3d_Drawer*) if it has the required custom attributes or otherwise from the context drawer.
420 @figure{visualization_image017.png,"",360}
422 It can be necessary to filter the entities to be selected. Consequently there are **Filter** entities, which allow refining the dynamic detection context. Some of these filters can be used only within at the Neutral Point, others only within Local Selection. It is possible to program custom filters and load them into the interactive context.
424 @subsection occt_visu_3_2 Interactive objects
426 Entities which are visualized and selected in the AIS viewer are objects. They connect the underlying reference geometry of a model to its graphic representation in *AIS*. You can use the predefined OCCT classes of standard interactive objects, for which all necessary functions have already been programmed, or, if you are an advanced user, you can implement your own classes of interactive objects.
428 @subsubsection occt_visu_3_2_1 Presentations
430 An interactive object can have as many presentations as its creator wants to give it.
431 3D presentations are managed by **Presentation Manager** (*PrsMgr_PresentationManager*). As this is transparent in AIS, the user does not have to worry about it.
433 A presentation is identified by an index (*Display Mode*) and by the reference to the Presentation Manager, which it depends on.
434 By convention, the default mode of representation for the Interactive Object has index 0.
436 @figure{visualization_image018.png,"",360}
438 Calculation of different presentations of an interactive object is done by the *Compute* functions inheriting from *PrsMgr_PresentableObject::Compute* functions. They are automatically called by *PresentationManager* at a visualization or an update request.
440 If you are creating your own type of interactive object, you must implement the Compute function in one of the following ways:
445 void PackageName_ClassName::Compute (const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
446 const Handle(Prs3d_Presentation)& thePresentation,
447 const Standard_Integer theMode);
450 #### For hidden line removal (HLR) mode in 3D:
453 void PackageName_ClassName::Compute (const Handle(Prs3d_Projector)& theProjector,
454 const Handle(Prs3d_Presentation)& thePresentation);
457 @subsubsection occt_visu_3_2_2 Hidden Line Removal
459 The view can have two states: the normal mode or the computed mode (Hidden Line Removal mode). When the latter is active, the view looks for all presentations displayed in the normal mode, which have been signalled as accepting HLR mode. An internal mechanism allows calling the interactive object's own *Compute*, that is projector function.
461 By convention, the Interactive Object accepts or rejects the representation of HLR mode. It is possible to make this declaration in one of two ways:
463 * Initially by using one of the values of the enumeration *PrsMgr_TypeOfPresentation3d*:
464 * *PrsMgr_TOP_AllView*,
465 * *PrsMgr_TOP_ProjectorDependant*
467 * Later by using the function *PrsMgr_PresentableObject::SetTypeOfPresentation*
469 *AIS_Shape* class is an example of an interactive object that supports HLR representation.
470 The type of the HLR algorithm is stored in *Prs3d_Drawer* of the shape. It is a value of the *Prs3d_TypeOfHLR* enumeration and can be set to:
471 * *Prs3d_TOH_PolyAlgo* for a polygonal algorithm based on the shape's triangulation;
472 * *Prs3d_TOH_Algo* for an exact algorithm that works with the shape's real geometry;
473 * *Prs3d_TOH_NotSet* if the type of algorithm is not set for the given interactive object instance.
475 The type of the HLR algorithm used for *AIS_Shape* can be changed by calling the *AIS_Shape::SetTypeOfHLR()* method.
476 The current HLR algorithm type can be obtained using *AIS_Shape::TypeOfHLR()* method is to be used.
478 These methods get the value from the drawer of *AIS_Shape*. If the HLR algorithm type in the *Prs3d_Drawer* is set to *Prs3d_TOH_NotSet*, the *Prs3d_Drawer* gets the value from the default drawer of *AIS_InteractiveContext*.
479 So it is possible to change the default HLR algorithm used by all newly displayed interactive objects. The value of the HLR algorithm type stored in the context drawer can be *Prs3d_TOH_Algo* or *Prs3d_TOH_PolyAlgo*. The polygonal algorithm is the default one.
481 @subsubsection occt_visu_3_2_3 Presentation modes
483 There are four types of interactive objects in AIS:
484 * the "construction element" or Datum,
485 * the Relation (dimensions and constraints)
487 * the None type (when the object is of an unknown type).
489 Inside these categories, additional characterization is available by means of a signature (an index.) By default, the interactive object has a NONE type and a signature of 0 (equivalent to NONE). If you want to give a particular type and signature to your interactive object, you must redefine two virtual functions:
490 * *AIS_InteractiveObject::Type*
491 * *AIS_InteractiveObject::Signature*.
493 **Note** that some signatures are already used by "standard" objects provided in AIS (see the @ref occt_visu_3_5 "List of Standard Interactive Object Classes").
495 The interactive context can have a default mode of representation for the set of interactive objects. This mode may not be accepted by a given class of objects.
496 Consequently, to get information about this class it is necessary to use virtual function *AIS_InteractiveObject::AcceptDisplayMode*.
500 The functions *AIS_InteractiveContext::SetDisplayMode* and *AIS_InteractiveContext::UnsetDisplayMode* allow setting a custom display mode for an objects, which can be different from that proposed by the interactive context.
504 At dynamic detection, the presentation echoed by the Interactive Context, is by default the presentation already on the screen.
506 The functions *AIS_InteractiveObject::SetHilightMode* and *AIS_InteractiveObject::UnsetHilightMode* allow specifying the display mode used for highlighting (so called highlight mode), which is valid independently from the active representation of the object. It makes no difference whether this choice is temporary or definitive.
508 Note that the same presentation (and consequently the same highlight mode) is used for highlighting *detected* objects and for highlighting *selected* objects, the latter being drawn with a special *selection color* (refer to the section related to *Interactive Context* services).
510 For example, you want to systematically highlight the wireframe presentation of a shape - non regarding if it is visualized in wireframe presentation or with shading. Thus, you set the highlight mode to *0* in the constructor of the interactive object. Do not forget to implement this representation mode in the *Compute* functions.
514 If you do not want an object to be affected by a *FitAll* view, you must declare it infinite; you can cancel its "infinite" status using *AIS_InteractiveObject::SetInfiniteState* and *AIS_InteractiveObject::IsInfinite* functions.
516 Let us take for example the class called *IShape* representing an interactive object:
519 myPk_IShape::myPk_IShape (const TopoDS_Shape& theShape, PrsMgr_TypeOfPresentation theType)
520 : AIS_InteractiveObject (theType), myShape (theShape) { SetHilightMode (0); }
522 Standard_Boolean myPk_IShape::AcceptDisplayMode (const Standard_Integer theMode) const
524 return theMode == 0 || theMode == 1;
527 void myPk_IShape::Compute (const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
528 const Handle(Prs3d_Presentation)& thePrs,
529 const Standard_Integer theMode)
533 // algo for calculation of wireframe presentation
534 case 0: StdPrs_WFDeflectionShape::Add (thePrs, myShape, myDrawer); return;
535 // algo for calculation of shading presentation
536 case 1: StdPrs_ShadedShape::Add (thePrs, myShape, myDrawer); return;
540 void myPk_IShape::Compute (const Handle(Prs3d_Projector)& theProjector,
541 const Handle(Prs3d_Presentation)& thePrs)
543 // Hidden line mode calculation algorithm
544 StdPrs_HLRPolyShape::Add (thePrs, myShape, myDrawer, theProjector);
548 @subsubsection occt_visu_3_2_4 Selection
550 An interactive object can have an indefinite number of selection modes, each representing a "decomposition" into sensitive primitives. Each primitive has an **Owner** (*SelectMgr_EntityOwner*) which allows identifying the exact interactive object or shape which has been detected (see @ref occt_visu_2_2 "Selection" chapter).
552 The set of sensitive primitives, which correspond to a given mode, is stocked in a **Selection** (*SelectMgr_Selection*).
554 Each selection mode is identified by an index. By convention, the default selection mode that allows us to grasp the interactive object in its entirety is mode *0*. However, it can be modified in the custom interactive objects using method *SelectMgr_SelectableObject::setGlobalSelMode()*.
556 The calculation of selection primitives (or sensitive entities) is done in a virtual function *ComputeSelection*. It should be implemented for each type of interactive object that is assumed to have different selection modes using the function *AIS_InteractiveObject::ComputeSelection*.
557 A detailed explanation of the mechanism and the manner of implementing this function has been given in @ref occt_visu_2_2 "Selection" chapter.
559 There are some examples of selection mode calculation for the most widely used interactive object in OCCT -- *AIS_Shape* (selection by vertex, by edges, etc). To create new classes of interactive objects with the same selection behavior as *AIS_Shape* -- such as vertices and edges -- you must redefine the virtual function *AIS_InteractiveObject::AcceptShapeDecomposition*.
561 @subsubsection occt_visu_3_2_5 Graphic attributes
563 Graphic attributes manager, or *Prs3d_Drawer*, stores graphic attributes for specific interactive objects and for interactive objects controlled by interactive context.
565 Initially, all drawer attributes are filled out with the predefined values which will define the default 3D object appearance.
566 When an interactive object is visualized, the required graphic attributes are first taken from its own drawer if one exists, or from the context drawer if no specific drawer for that type of object exists.
568 Keep in mind the following points concerning graphic attributes:
569 * Each interactive object can have its own visualization attributes.
570 * By default, the interactive object takes the graphic attributes of the context in which it is visualized (visualization mode, deflection values for the calculation of presentations, number of isoparameters, color, type of line, material, etc.)
571 * In the *AIS_InteractiveObject* abstract class, standard attributes including color, line thickness, material, and transparency have been privileged. Consequently, there is a certain number of virtual functions, which allow acting on these attributes. Each new class of interactive object can redefine these functions and change the behavior of the class.
573 @figure{visualization_image020.svg,"Redefinition of virtual functions for changes in AIS_Shape and AIS_TextLabel.",360}
575 The following virtual functions provide settings for color, width, material and transparency:
576 * *AIS_InteractiveObject::UnsetColor*
577 * *AIS_InteractiveObject::SetWidth*
578 * *AIS_InteractiveObject::UnsetWidth*
579 * *AIS_InteractiveObject::SetMaterial*
580 * *AIS_InteractiveObject::UnsetMaterial*
581 * *AIS_InteractiveObject::SetTransparency*
582 * *AIS_InteractiveObject::UnsetTransparency*
584 These methods can be used as a shortcut assigning properties in common way, but result might be not available.
585 Some interactive objects might not implement these methods at all or implement only a sub-set of them.
586 Direct modification of *Prs3d_Drawer* properties returned by *AIS_InteractiveObject::Attributes* can be used for more precise and predictable configuration.
588 It is important to know which functions may imply the recalculation of presentations of the object.
589 If the presentation mode of an interactive object is to be updated, a flag from *PrsMgr_PresentableObject* indicates this.
590 The mode can be updated using the functions *Display* and *Redisplay* in *AIS_InteractiveContext*.
592 @subsubsection occt_visu_3_2_6 Complementary Services
594 When you use complementary services for interactive objects, pay special attention to the cases mentioned below.
596 #### Change the location of an interactive object
598 The following functions allow "moving" the representation and selection of Interactive Objects in a view without recalculation (and modification of the original shape).
599 * *AIS_InteractiveContext::SetLocation*
600 * *AIS_InteractiveContext::ResetLocation*
601 * *AIS_InteractiveContext::HasLocation*
602 * *AIS_InteractiveContext::Location*
604 #### Connect an interactive object to an applicative entity
606 Each Interactive Object has functions that allow attributing it an *GetOwner* in form of a *Transient*.
607 * *AIS_InteractiveObject::SetOwner*
608 * *AIS_InteractiveObject::HasOwner*
609 * *AIS_InteractiveObject::GetOwner*
611 An interactive object can therefore be associated or not with an applicative entity, without affecting its behavior.
613 **NOTE:** Don't be confused by owners of another kind - *SelectMgr_EntityOwner* used for identifying selectable parts of the object or object itself.
615 #### Resolving coincident topology
617 Due to the fact that the accuracy of three-dimensional graphics coordinates has a finite resolution the elements of topological objects can coincide producing the effect of "popping" some elements one over another.
619 To the problem when the elements of two or more Interactive Objects are coincident you can apply the polygon offset. It is a sort of graphics computational offset, or depth buffer offset, that allows you to arrange elements (by modifying their depth value) without changing their coordinates. The graphical elements that accept this kind of offsets are solid polygons or displayed as boundary lines and points. The polygons could be displayed as lines or points by setting the appropriate interior style.
621 The methods *AIS_InteractiveObject::SetPolygonOffsets* and *AIS_InteractiveContext::SetPolygonOffsets* allow setting up the polygon offsets.
623 @subsubsection occt_visu_3_2_7 Object hierarchy
625 Each *PrsMgr_PresentableObject* has a list of objects called *myChildren*.
626 Any transformation of *PrsMgr_PresentableObject* is also applied to its children. This hierarchy does not propagate to *Graphic3d* level and below.
628 *PrsMgr_PresentableObject* sends its combined (according to the hierarchy) transformation down to *Graphic3d_Structure*.
629 The materials of structures are not affected by the hierarchy.
631 Object hierarchy can be controlled by the following API calls:
632 * *PrsMgr_PresentableObject::AddChild*;
633 * *PrsMgr_PresentableObject::RemoveChild*.
635 @subsubsection occt_visu_3_2_8 Instancing
637 The conception of instancing operates the object hierarchy as follows:
638 * Instances are represented by separated *AIS* objects.
639 * Instances do not compute any presentations.
641 Classes *AIS_ConnectedInteractive* and *AIS_MultipleConnectedInteractive* are used to implement this conception.
643 *AIS_ConnectedInteractive* is an object instance, which reuses the geometry of the connected object but has its own transformation and visibility flag. This connection is propagated down to *OpenGl* level, namely to *OpenGl_Structure*. *OpenGl_Structure* can be connected only to a single other structure.
645 *AIS_ConnectedInteractive* can be referenced to any *AIS_InteractiveObject* in general. When it is referenced to another *AIS_ConnectedInteractive*, it just copies the reference.
647 *AIS_MultipleConnectedInteractive* represents an assembly, which does not have its own presentation. The assemblies are able to participate in the object hierarchy and are intended to handle a grouped set of instanced objects. It behaves as a single object in terms of selection. It applies high level transformation to all sub-elements since it is located above in the hierarchy.
649 All *AIS_MultipleConnectedInteractive* are able to have child assemblies. Deep copy of object instances tree is performed if one assembly is attached to another.
651 Note that *AIS_ConnectedInteractive* cannot reference *AIS_MultipleConnectedInteractive*. *AIS_ConnectedInteractive* copies sensitive entities of the origin object for selection, unlike *AIS_MultipleConnectedInteractive* that re-uses the entities of the origin object.
653 Instances can be controlled by the following DRAW commands:
654 * *vconnect* : Creates and displays *AIS_MultipleConnectedInteractive* object from input objects and location.
655 * *vconnectto* : Makes an instance of object with the given position.
656 * *vdisconnect* : Disconnects all objects from an assembly or disconnects an object by name or number.
657 * *vaddconnected* : Adds an object to the assembly.
658 * *vlistconnected* : Lists objects in the assembly.
660 Have a look at the examples below:
666 vconnectto s2 3 0 0 s # make instance
670 See how proxy *OpenGl_Structure* is used to represent instance:
672 @figure{/user_guides/visualization/images/visualization_image029.png,"",240}
674 The original object does not have to be displayed in order to make instance. Also selection handles transformations of instances correctly:
681 vdisplay s # p is not displayed
683 vconnect x 3 0 0 s p # make assembly
687 @figure{/user_guides/visualization/images/visualization_image030.png,"",420}
689 Here is the example of a more complex hierarchy involving sub-assemblies:
697 vsetlocation s 0 2.5 0
702 vconnectto b1 -2 0 0 b
704 vconnect z2 4 0 0 d d2
705 vconnect z3 6 0 0 z z2
709 @subsection occt_visu_3_3 Interactive Context
711 @subsubsection occt_visu_3_3_1 Rules
713 The Interactive Context allows managing in a transparent way the graphic and **selectable** behavior of interactive objects in one or more viewers. Most functions which allow modifying the attributes of interactive objects, and which were presented in the preceding chapter, will be looked at again here.
715 There is one essential rule to follow: the modification of an interactive object, which is already known by the Context, must be done using Context functions. You can only directly call the functions available for an interactive object if it has not been loaded into an Interactive Context.
718 Handle(AIS_Shape) aShapePrs = new AIS_Shape (theShape);
719 myIntContext->Display (aShapePrs, AIS_Shaded, 0, false, aShapePrs->AcceptShapeDecomposition());
720 myIntContext->SetColor(aShapePrs, Quantity_NOC_RED);
726 Handle(AIS_Shape) aShapePrs = new AIS_Shape (theShape);
727 aShapePrs->SetColor (Quantity_NOC_RED);
728 aShapePrs->SetDisplayMode (AIS_Shaded);
729 myIntContext->Display (aShapePrs);
732 @subsubsection occt_visu_3_3_2 Groups of functions
734 **Neutral Point** and **Local Selection** constitute the two operating modes or states of the **Interactive Context**, which is the central entity which pilots visualizations and selections.
735 The **Neutral Point**, which is the default mode, allows easily visualizing and selecting interactive objects, which have been loaded into the context.
736 Activating **Local Selection** for specific Objects allows selecting of their sub-parts.
738 @subsubsection occt_visu_3_3_3 Management of the Interactive Context
740 An interactive object can have a certain number of specific graphic attributes, such as visualization mode, color, and material. Correspondingly, the interactive context has a set of graphic attributes, the *Drawer*, which is valid by default for the objects it controls.
741 When an interactive object is visualized, the required graphic attributes are first taken from the object's own *Drawer* if it exists, or from the context drawer if otherwise.
743 The following adjustable settings allow personalizing the behavior of presentations and selections:
744 * Default Drawer, containing all the color and line attributes which can be used by interactive objects, which do not have their own attributes.
745 * Default Visualization Mode for interactive objects. By default: *mode 0*;
746 * Highlight color of entities detected by mouse movement. By default: *Quantity_NOC_CYAN1*;
747 * Pre-selection color. By default: *Quantity_NOC_GREEN*;
748 * Selection color (when you click on a detected object). By default: *Quantity_NOC_GRAY80*;
750 All of these settings can be modified by functions proper to the Context.
751 When you change a graphic attribute pertaining to the Context (visualization mode, for example), all interactive objects, which do not have the corresponding appropriate attribute, are updated.
753 Let us examine the case of two interactive objects: *theObj1* and *theObj2*:
756 theCtx->Display (theObj1, false);
757 theCtx->Display (theObj2, true); // TRUE for viewer update
758 theCtx->SetDisplayMode (theObj1, 3, false);
759 theCtx->SetDisplayMode (2, true);
760 // theObj2 is visualised in mode 2 (if it accepts this mode)
761 // theObj1 stays visualised in its mode 3
764 *PresentationManager* and *Selector3D*, which manage the presentation and selection of present interactive objects, are associated to the main Viewer.
766 *WARNING!* Do NOT use integer values (like in sample above) in real code - use appropriate enumerations instead!
767 Each presentable object has independent list of supported display and selection modes; for instance, *AIS_DisplayMode* enumeration is applicable only to *AIS_Shape* presentations.
769 @subsection occt_visu_3_4 Local Selection
771 @subsubsection occt_visu_3_4_1 Selection Modes
773 The Local Selection is defined by index (Selection Mode). The Selection Modes implemented by a specific interactive object and their meaning should be checked within the documentation of this class.
774 See, for example, *MeshVS_SelectionModeFlags* for *MeshVS_Mesh* object.
776 *AIS_Shape* is the most used interactive object. It provides API to manage selection operations on the constituent elements of shapes (selection of vertices, edges, faces, etc.). The Selection Mode for a specific shape type (*TopAbs_ShapeEnum*) is returned by method *AIS_Shape::SelectionMode()*.
778 The method *AIS_InteractiveContext::Display()* without a Selection Mode argument activates the default Selection Mode of the object.
779 The methods *AIS_InteractiveContext::Activate()* and *AIS_InteractiveContext::Deactivate()* activate and deactivate a specific Selection Mode.
781 More than one Selection Mode can be activated at the same time (but default 0 mode for selecting entire object is exclusive - it cannot be combined with others).
782 The list of active modes can be retrieved using function *AIS_InteractiveContext::ActivatedModes*.
784 @subsubsection occt_visu_3_4_2 Filters
786 To define an environment of dynamic detection, you can use standard filter classes or create your own.
787 A filter questions the owner of the sensitive primitive to determine if it has the desired qualities. If it answers positively, it is kept. If not, it is rejected.
789 The root class of objects is *SelectMgr_Filter*. The principle behind it is straightforward: a filter tests to see whether the owners (*SelectMgr_EntityOwner*) detected in mouse position by selector answer *OK*. If so, it is kept, otherwise it is rejected.
790 You can create a custom class of filter objects by implementing the deferred function *SelectMgr_Filter::IsOk()*.
792 In *SelectMgr*, there are also Composition filters (AND Filters, OR Filters), which allow combining several filters. In Interactive Context, all filters that you add are stored in an OR filter (which answers *OK* if at least one filter answers *OK*).
794 There are Standard filters, which have already been implemented in several packages:
795 * *StdSelect_EdgeFilter* -- for edges, such as lines and circles;
796 * *StdSelect_FaceFilter* -- for faces, such as planes, cylinders and spheres;
797 * *StdSelect_ShapeTypeFilter* -- for shape types, such as compounds, solids, shells and wires;
798 * *AIS_TypeFilter* -- for types of interactive objects;
799 * *AIS_SignatureFilter* -- for types and signatures of interactive objects;
800 * *AIS_AttributeFilter* -- for attributes of Interactive Objects, such as color and width.
802 There are several functions to manipulate filters:
803 * *AIS_InteractiveContext::AddFilter* adds a filter passed as an argument.
804 * *AIS_InteractiveContext::RemoveFilter* removes a filter passed as an argument.
805 * *AIS_InteractiveContext::RemoveFilters* removes all present filters.
806 * *AIS_InteractiveContext::Filters* gets the list of filters active in a context.
811 // shading visualization mode, no specific mode, authorization for decomposition into sub-shapes
812 const TopoDS_Shape theShape;
813 Handle(AIS_Shape) aShapePrs = new AIS_Shape (theShape);
814 myContext->Display (aShapePrs, AIS_Shaded, -1, true, true);
816 // activates decomposition of shapes into faces
817 const int aSubShapeSelMode = AIS_Shape::SelectionMode (TopAbs_Face);
818 myContext->Activate (aShapePrs, aSubShapeSelMode);
820 Handle(StdSelect_FaceFilter) aFil1 = new StdSelect_FaceFilter (StdSelect_Revol);
821 Handle(StdSelect_FaceFilter) aFil2 = new StdSelect_FaceFilter (StdSelect_Plane);
822 myContext->AddFilter (aFil1);
823 myContext->AddFilter (aFil2);
825 // only faces of revolution or planar faces will be selected
826 myContext->MoveTo (thePixelX, thePixelY, myView, true);
829 @subsubsection occt_visu_3_4_6 Selection
831 Dynamic detection and selection are put into effect in a straightforward way. There are only a few conventions and functions to be familiar with:
832 * *AIS_InteractiveContext::MoveTo* -- passes mouse position to Interactive Context selectors.
833 * *AIS_InteractiveContext::Select* -- stores what has been detected at the last *MoveTo*. Replaces the previously selected object. Empties the stack if nothing has been detected at the last move.
834 * *AIS_InteractiveContext::ShiftSelect* -- if the object detected at the last move was not already selected, it is added to the list of the selected objects. If not, it is withdrawn. Nothing happens if you click on an empty area.
835 * *AIS_InteractiveContext::Select* -- selects everything found in the surrounding area.
836 * *AIS_InteractiveContext::ShiftSelect* -- selects what was not previously in the list of selected, deselects those already present.
838 Highlighting of detected and selected entities is automatically managed by the Interactive Context. The Highlight colors are those dealt with above. You can nonetheless disconnect this automatic mode if you want to manage this part yourself:
840 AIS_InteractiveContext::SetAutomaticHilight
841 AIS_InteractiveContext::AutomaticHilight
844 You can question the Interactive context by moving the mouse. The following functions can be used:
845 * *AIS_InteractiveContext::HasDetected* -- checks if there is a detected entity;
846 * *AIS_InteractiveContext::DetectedOwner* -- returns the (currently highlighted) detected entity.
848 After using the *Select* and *ShiftSelect* functions, you can explore the list of selections. The following functions can be used:
849 * *AIS_InteractiveContext::InitSelected* -- initializes an iterator;
850 * *AIS_InteractiveContext::MoreSelected* -- checks if the iterator is valid;
851 * *AIS_InteractiveContext::NextSelected* -- moves the iterator to the next position;
852 * *AIS_InteractiveContext::SelectedOwner* -- returns an entity at the current iterator position.
854 The owner object *SelectMgr_EntityOwner* is a key object identifying the selectable entity in the viewer (returned by methods *AIS_InteractiveContext::DetectedOwner* and *AIS_InteractiveContext::SelectedOwner*).
855 The Interactive Object itself can be retrieved by method *SelectMgr_EntityOwner::Selectable*, while identifying a sub-part depends on the type of Interactive Object.
856 In case of *AIS_Shape*, the (sub)shape is returned by method *StdSelect_BRepOwner::Shape*.
861 for (myAISCtx->InitSelected(); myAISCtx->MoreSelected(); myAISCtx->NextSelected())
863 Handle(SelectMgr_EntityOwner) anOwner = myAISCtx->SelectedOwner();
864 Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
865 if (Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast (anOwner))
867 // to be able to use the picked shape
868 TopoDS_Shape aShape = aBRepOwner->Shape();
873 @subsection occt_visu_3_5 Standard Interactive Object Classes
875 Interactive Objects are selectable and viewable objects connecting graphic representation and the underlying reference geometry.
877 They are divided into four types:
878 * the **Datum** -- a construction geometric element;
879 * the **Relation** -- a constraint on the interactive shape and the corresponding reference geometry;
880 * the **Object** -- a topological shape or connection between shapes;
881 * **None** -- a token, that instead of eliminating the object, tells the application to look further until it finds an acceptable object definition in its generation.
883 Inside these categories, there is a possibility of additional characterization by means of a signature. The signature provides an index to the further characterization. By default, the **Interactive Object** has a *None* type and a signature of 0 (equivalent to *None*).
884 If you want to give a particular type and signature to your interactive object, you must redefine the two virtual methods: *Type* and *Signature*.
886 @subsubsection occt_visu_3_5_1 Datum
888 The **Datum** groups together the construction elements such as lines, circles, points, trihedrons, plane trihedrons, planes and axes.
890 *AIS_Point, AIS_Axis, AIS_Line, AIS_Circle, AIS_Plane* and *AIS_Trihedron* have four selection modes:
891 * mode AIS_TrihedronSelectionMode_EntireObject : selection of a trihedron;
892 * mode AIS_TrihedronSelectionMode_Origin : selection of the origin of the trihedron;
893 * mode AIS_TrihedronSelectionMode_Axes : selection of the axes;
894 * mode AIS_TrihedronSelectionMode_MainPlanes : selection of the planes XOY, YOZ, XOZ.
896 when you activate one of modes, you pick AIS objects of type:
898 * *AIS_Axis* (and information on the type of axis);
899 * *AIS_Plane* (and information on the type of plane).
901 *AIS_PlaneTrihedron* offers three selection modes:
902 * mode 0 : selection of the whole trihedron;
903 * mode 1 : selection of the origin of the trihedron;
904 * mode 2 : selection of the axes -- same remarks as for the Trihedron.
906 For the presentation of planes and trihedra, the default length unit is millimeter and the default value for the representation of axes is 10. To modify these dimensions, you must temporarily recover the object **Drawer**. From it, take the *DatumAspect()* and change the value *FirstAxisLength*. Finally, recalculate the presentation.
908 @subsubsection occt_visu_3_5_2 Object
910 The **Object** type includes topological shapes, and connections between shapes.
912 *AIS_Shape* has two visualization modes:
913 * mode AIS_WireFrame : Line (default mode)
914 * mode AIS_Shaded : Shading (depending on the type of shape)
916 *AIS_ConnectedInteractive* is an Interactive Object connecting to another interactive object reference, and located elsewhere in the viewer makes it possible not to calculate presentation and selection, but to deduce them from your object reference.
917 *AIS_MultipleConnectedInteractive* is an object connected to a list of interactive objects (which can also be Connected objects. It does not require memory-hungry presentation calculations).
919 *MeshVS_Mesh* is an Interactive Object that represents meshes, it has a data source that provides geometrical information (nodes, elements) and can be built up from the source data with a custom presentation builder.
921 The class *AIS_ColoredShape* allows using custom colors and line widths for *TopoDS_Shape* objects and their sub-shapes.
924 AIS_ColoredShape aColoredShape = new AIS_ColoredShape (theShape);
926 // setup color of entire shape
927 aColoredShape->SetColor (Quantity_NOC_RED);
929 // setup line width of entire shape
930 aColoredShape->SetWidth (1.0);
932 // set transparency value
933 aColoredShape->SetTransparency (0.5);
935 // customize color of specified sub-shape
936 aColoredShape->SetCustomColor (theSubShape, Quantity_NOC_BLUE1);
938 // customize line width of specified sub-shape
939 aColoredShape->SetCustomWidth (theSubShape, 0.25);
942 The presentation class *AIS_PointCloud* can be used for efficient drawing of large arbitrary sets of colored points. It uses *Graphic3d_ArrayOfPoints* to pass point data into OpenGl graphic driver to draw a set points as an array of "point sprites". The point data is packed into vertex buffer object for performance.
943 - The type of point marker used to draw points can be specified as a presentation aspect.
944 - The presentation provides selection by a bounding box of the visualized set of points. It supports two display / highlighting modes: points or bounding box.
946 @figure{point_cloud.png,"A random colored cloud of points",240}
950 Handle(Graphic3d_ArrayOfPoints) aPoints = new Graphic3d_ArrayOfPoints (2000, Standard_True);
951 aPoints->AddVertex (gp_Pnt(-40.0, -40.0, -40.0), Quantity_Color (Quantity_NOC_BLUE1));
952 aPoints->AddVertex (gp_Pnt (40.0, 40.0, 40.0), Quantity_Color (Quantity_NOC_BLUE2));
954 Handle(AIS_PointCloud) aPntCloud = new AIS_PointCloud();
955 aPntCloud->SetPoints (aPoints);
958 The draw command *vpointcloud* builds a cloud of points from shape triangulation.
959 This command can also draw a sphere surface or a volume with a large amount of points (more than one million).
961 @subsubsection occt_visu_3_5_3 Relations
963 The **Relation** is made up of constraints on one or more interactive shapes and the corresponding reference geometry. For example, you might want to constrain two edges in a parallel relation. This constraint is considered as an object in its own right, and is shown as a sensitive primitive. This takes the graphic form of a perpendicular arrow marked with the || symbol and lying between the two edges.
965 The following relations are provided by *PrsDim*:
966 * *PrsDim_ConcentricRelation*
967 * *PrsDim_FixRelation*
968 * *PrsDim_IdenticRelation*
969 * *PrsDim_ParallelRelation*
970 * *PrsDim_PerpendicularRelation*
972 * *PrsDim_SymmetricRelation*
973 * *PrsDim_TangentRelation*
975 The list of relations is not exhaustive.
977 @subsubsection occt_visu_3_5_4 Dimensions
978 * *PrsDim_AngleDimension*
979 * *PrsDim_Chamf3dDimension*
980 * *PrsDim_DiameterDimension*
981 * *PrsDim_DimensionOwner*
982 * *PrsDim_LengthDimension*
983 * *PrsDim_OffsetDimension*
984 * *PrsDim_RadiusDimension*
986 @subsubsection occt_visu_3_5_5 MeshVS_Mesh
988 *MeshVS_Mesh* is an Interactive Object that represents meshes. This object differs from the *AIS_Shape* as its geometrical data is supported by the data source *MeshVS_DataSource* that describes nodes and elements of the object. As a result, you can provide your own data source.
990 However, the *DataSource* does not provide any information on attributes, for example nodal colors, but you can apply them in a special way -- by choosing the appropriate presentation builder.
992 The presentations of *MeshVS_Mesh* are built with the presentation builders *MeshVS_PrsBuilder*. You can choose between the builders to represent the object in a different way. Moreover, you can redefine the base builder class and provide your own presentation builder.
994 You can add/remove builders using the following methods:
996 MeshVS_Mesh::AddBuilder (const Handle(MeshVS_PrsBuilder)& theBuilder, Standard_Boolean theToTreatAsHilighter);
997 MeshVS_Mesh::RemoveBuilder (const Standard_Integer theIndex);
998 MeshVS_Mesh::RemoveBuilderById (const Standard_Integer theId);
1001 There is a set of reserved display and highlighting mode flags for *MeshVS_Mesh*. Mode value is a number of bits that allows selecting additional display parameters and combining the following mode flags, which allow displaying mesh in wireframe, shading and shrink modes:
1003 MeshVS_DMF_WireFrame
1008 It is also possible to display deformed mesh in wireframe, shading or shrink modes usung:
1010 MeshVS_DMF_DeformedPrsWireFrame
1011 MeshVS_DMF_DeformedPrsShading
1012 MeshVS_DMF_DeformedPrsShrink
1015 The following methods represent different kinds of data:
1017 MeshVS_DMF_VectorDataPrs
1018 MeshVS_DMF_NodalColorDataPrs
1019 MeshVS_DMF_ElementalColorDataPrs
1020 MeshVS_DMF_TextDataPrs
1021 MeshVS_DMF_EntitiesWithData
1024 The following methods provide selection and highlighting:
1026 MeshVS_DMF_SelectionPrs
1027 MeshVS_DMF_HilightPrs
1030 *MeshVS_DMF_User* is a user-defined mode.
1032 These values will be used by the presentation builder.
1033 There is also a set of selection modes flags that can be grouped in a combination of bits:
1037 * *MeshVS_SMF_Volume*
1038 * *MeshVS_SMF_Element* -- groups *0D, Link, Face* and *Volume* as a bit mask;
1040 * *MeshVS_SMF_All* -- groups *Element* and *Node* as a bit mask;
1042 * *MeshVS_SMF_Group*
1044 Such an object, for example, can be used for displaying the object and stored in the STL file format:
1047 // read the data and create a data source
1048 Handle(Poly_Triangulation) aSTLMesh = RWStl::ReadFile (aFileName);
1049 Handle(XSDRAWSTLVRML_DataSource) aDataSource = new XSDRAWSTLVRML_DataSource (aSTLMesh);
1052 Handle(MeshVS_Mesh) aMeshPrs = new MeshVS();
1053 aMeshPrs->SetDataSource (aDataSource);
1055 // use default presentation builder
1056 Handle(MeshVS_MeshPrsBuilder) aBuilder = new MeshVS_MeshPrsBuilder (aMeshPrs);
1057 aMeshPrs->AddBuilder (aBuilder, true);
1060 *MeshVS_NodalColorPrsBuilder* allows representing a mesh with a color scaled texture mapped on it.
1061 To do this you should define a color map for the color scale, pass this map to the presentation builder, and define an appropriate value in the range of 0.0 - 1.0 for every node.
1062 The following example demonstrates how you can do this (check if the view has been set up to display textures):
1065 // assign nodal builder to the mesh
1066 Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder (theMeshPrs, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask);
1067 aBuilder->UseTexture (true);
1069 // prepare color map
1070 Aspect_SequenceOfColor aColorMap;
1071 aColorMap.Append (Quantity_NOC_RED);
1072 aColorMap.Append (Quantity_NOC_BLUE1);
1074 // assign color scale map values (0..1) to nodes
1075 TColStd_DataMapOfIntegerReal aScaleMap;
1077 // iterate through the nodes and add an node id and an appropriate value to the map
1078 aScaleMap.Bind (anId, aValue);
1080 // pass color map and color scale values to the builder
1081 aBuilder->SetColorMap (aColorMap);
1082 aBuilder->SetInvalidColor (Quantity_NOC_BLACK);
1083 aBuilder->SetTextureCoords (aScaleMap);
1084 aMesh->AddBuilder (aBuilder, true);
1087 @subsection occt_visu_3_6 Dynamic Selection
1089 The dynamic selection represents the topological shape, which you want to select, by decomposition of *sensitive primitives* -- the sub-parts of the shape that will be detected and highlighted. The sets of these primitives are handled by the powerful three-level BVH tree selection algorithm.
1091 For more details on the algorithm and examples of usage, refer to @ref occt_visu_2_2 "Selection" chapter.
1093 @section occt_visu_4 3D Presentations
1095 @subsection occt_visu_4_1 Glossary of 3D terms
1097 * **Group** -- a set of primitives and attributes on those primitives. Primitives and attributes may be added to a group but cannot be removed from it, unless erased globally. A group can have a pick identity.
1098 * **Light** There are five kinds of light source -- ambient, headlight, directional, positional and spot. The light is only activated in a shading context in a view.
1099 * **Primitive** -- a drawable element. It has a definition in 3D space. Primitives can either be lines, faces, text, or markers. Once displayed markers and text remain the same size. Lines and faces can be modified e.g. zoomed. Primitives must be stored in a group.
1100 * **Structure** -- manages a set of groups. The groups are mutually exclusive. A structure can be edited, adding or removing groups. A structure can reference other structures to form a hierarchy. It has a default (identity) transformation and other transformations may be applied to it (rotation, translation, scale, etc). It has no default attributes for the primitive lines, faces, markers, and text. Attributes may be set in a structure but they are overridden by the attributes in each group. Each structure has a display priority associated with it, which rules the order in which it is redrawn in a 3D viewer. If the visualization mode is incompatible with the view it is not displayed in that view, e.g. a shading-only object is not visualized in a wireframe view.
1101 * **View** -- is defined by a view orientation, a view mapping, and a context view.
1102 * **Viewer** -- manages a set of views.
1103 * **View orientation** -- defines the manner in which the observer looks at the scene in terms of View Reference Coordinates.
1104 * **View mapping** -- defines the transformation from View Reference Coordinates to the Normalized Projection Coordinates. This follows the Phigs scheme.
1105 * **Z-Buffering** -- a form of hidden surface removal in shading mode only. This is always active for a view in the shading mode. It cannot be suppressed.
1107 @subsection occt_visu_4_2 Graphic primitives
1109 The *Graphic3d* package is used to create 3D graphic objects in a 3D viewer. These objects called **structures** are made up of groups of primitives and attributes, such as polylines, planar polygons with or without holes, text and markers, and attributes, such as color, transparency, reflection, line type, line width, and text font. A group is the smallest editable element of a structure. A transformation can be applied to a structure. Structures can be connected to form a tree of structures, composed by transformations. Structures are globally manipulated by the viewer.
1111 Graphic structures can be:
1116 * Connected to form a tree hierarchy of structures, created by transformations.
1118 There are classes for:
1119 * Visual attributes for lines, faces, markers, text, materials,
1120 * Vectors and vertices,
1121 * Graphic objects, groups, and structures.
1123 @subsubsection occt_visu_4_2_2 Structure hierarchies
1125 The root is the top of a structure hierarchy or structure network. The attributes of a parent structure are passed to its descendants. The attributes of the descendant structures do not affect the parent. Recursive structure networks are not supported.
1127 @subsubsection occt_visu_4_2_3 Graphic primitives
1129 * Have one or more vertices,
1130 * Have a type, a scale factor, and a color,
1131 * Have a size, shape, and orientation independent of transformations.
1133 * Has at least three vertices,
1134 * Has nodal normals defined for shading,
1135 * Has interior attributes -- style, color, front and back material, texture and reflection ratio.
1136 * **Polylines** or **Segments**
1137 * Have two or more vertices,
1138 * Have the following attributes -- type, width scale factor, color.
1140 * Has geometric and non-geometric attributes,
1141 * Geometric attributes -- character height, character up vector, text path, horizontal and vertical alignment, orientation, three-dimensional position, zoomable flag
1142 * Non-geometric attributes -- text font, character spacing, character expansion factor, color.
1144 @subsubsection occt_visu_4_2_4 Primitive arrays
1146 The different types of primitives could be presented with the following primitive arrays:
1147 * *Graphic3d_ArrayOfPoints,*
1148 * *Graphic3d_ArrayOfPolylines,*
1149 * *Graphic3d_ArrayOfSegments,*
1150 * *Graphic3d_ArrayOfTriangleFans,*
1151 * *Graphic3d_ArrayOfTriangles,*
1152 * *Graphic3d_ArrayOfTriangleStrips.*
1154 The *Graphic3d_ArrayOfPrimitives* is a base class for these primitive arrays.
1155 Method set *Graphic3d_ArrayOfPrimitives::AddVertex* allows adding vertices to the primitive array with their attributes (color, normal, texture coordinates).
1156 You can also modify the values assigned to the vertex or query these values by the vertex index.
1158 The following example shows how to define an array of points:
1162 Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints (theVerticiesMaxCount);
1164 // add vertices to the array
1165 anArray->AddVertex (10.0, 10.0, 10.0);
1166 anArray->AddVertex (0.0, 10.0, 10.0);
1168 // add the array to the structure
1169 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
1170 aGroup->AddPrimitiveArray (anArray);
1171 aGroup->SetGroupPrimitivesAspect (myDrawer->PointAspect()->Aspect());
1174 If the primitives share the same vertices (polygons, triangles, etc.) then you can define them as indices of the vertices array.
1175 The method *Graphic3d_ArrayOfPrimitives::AddEdge* allows defining the primitives by indices. This method adds an "edge" in the range *[1, VertexNumber()]* in the array.
1176 It is also possible to query the vertex defined by an edge using method *Graphic3d_ArrayOfPrimitives::Edge*.
1178 The following example shows how to define an array of triangles:
1182 Standard_Boolean hasNormals = false;
1183 Standard_Boolean hasColors = false;
1184 Standard_Boolean hasTextureCrds = false;
1185 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (theVerticesMaxCount, theEdgesMaxCount, hasNormals, hasColors, hasTextureCrds);
1186 // add vertices to the array
1187 anArray->AddVertex (-1.0, 0.0, 0.0); // vertex 1
1188 anArray->AddVertex ( 1.0, 0.0, 0.0); // vertex 2
1189 anArray->AddVertex ( 0.0, 1.0, 0.0); // vertex 3
1190 anArray->AddVertex ( 0.0,-1.0, 0.0); // vertex 4
1192 // add edges to the array
1193 anArray->AddEdge (1); // first triangle
1194 anArray->AddEdge (2);
1195 anArray->AddEdge (3);
1196 anArray->AddEdge (1); // second triangle
1197 anArray->AddEdge (2);
1198 anArray->AddEdge (4);
1200 // add the array to the structure
1201 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
1202 aGroup->AddPrimitiveArray (anArray);
1203 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
1206 @subsubsection occt_visu_4_2_5 Text primitive
1208 *TKOpenGl* toolkit renders text labels using texture fonts. *Graphic3d* text primitives have the following features:
1209 * fixed size (non-zoomable) or zoomable,
1210 * can be rotated to any angle in the view plane,
1211 * support unicode charset.
1213 The text attributes for the group could be defined with the *Graphic3d_AspectText3d* attributes group.
1214 To add any text to the graphic structure you can use the following methods:
1216 void Graphic3d_Group::AddText (const Handle(Graphic3d_Text)& theTextParams,
1217 const Standard_Boolean theToEvalMinMax);
1220 You can pass FALSE as *theToEvalMinMax* if you do not want the graphic3d structure boundaries to be affected by the text position.
1222 **Note** that the text orientation angle can be defined by *Graphic3d_AspectText3d* attributes.
1227 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
1229 // change the text aspect
1230 Handle(Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d();
1231 aTextAspect->SetTextZoomable (true);
1232 aTextAspect->SetTextAngle (45.0);
1233 aGroup->SetPrimitivesAspect (aTextAspect);
1235 // add a text primitive to the structure
1236 Handle(Graphic3d_Text) aText = new Graphic3d_Text (16.0f);
1237 aText->SetText ("Text");
1238 aText->SetPosition (gp_Pnt (1, 1, 1));
1239 aGroup->AddText (aText);
1242 @subsubsection occt_visu_4_2_6 Materials
1244 A *Graphic3d_MaterialAspect* is defined by:
1246 * Diffuse reflection -- a component of the object color;
1247 * Ambient reflection;
1248 * Specular reflection -- a component of the color of the light source;
1251 The following items are required to determine the three colors of reflection:
1253 * Coefficient of diffuse reflection;
1254 * Coefficient of ambient reflection;
1255 * Coefficient of specular reflection.
1257 @subsubsection occt_visu_4_2_7 Textures
1259 A *texture* is defined by a name.
1260 Three types of texture are available:
1263 * Environment mapping.
1265 @subsubsection occt_visu_4_2_8 Shaders
1267 OCCT visualization core supports GLSL shaders. Shaders can be assigned to a generic presentation by its drawer attributes (Graphic3d aspects). To enable custom shader for a specific AIS_Shape in your application, the following API functions can be used:
1270 // Create shader program
1271 Handle(Graphic3d_ShaderProgram) aProgram = new Graphic3d_ShaderProgram();
1273 // Attach vertex shader
1274 aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile (Graphic3d_TOS_VERTEX, "<Path to VS>"));
1276 // Attach fragment shader
1277 aProgram->AttachShader (Graphic3d_ShaderObject::CreateFromFile (Graphic3d_TOS_FRAGMENT, "<Path to FS>"));
1279 // Set values for custom uniform variables (if they are)
1280 aProgram->PushVariable ("MyColor", Graphic3d_Vec3 (0.0f, 1.0f, 0.0f));
1282 // Set aspect property for specific AIS_Shape
1283 theAISShape->Attributes()->ShadingAspect()->Aspect()->SetShaderProgram (aProgram);
1286 @subsection occt_visu_4_3 Graphic attributes
1288 @subsubsection occt_visu_4_3_1 Aspect package overview
1290 The *Aspect* package provides classes for the graphic elements in the viewer:
1291 * Groups of graphic attributes;
1292 * Edges, lines, background;
1295 * Enumerations for many of the above.
1297 @subsection occt_visu_4_4 3D view facilities
1299 @subsubsection occt_visu_4_4_1 Overview
1301 The *V3d* package provides the resources to define a 3D viewer and the views attached to this viewer (orthographic, perspective). This package provides the commands to manipulate the graphic scene of any 3D object visualized in a view on screen.
1303 A set of high-level commands allows the separate manipulation of parameters and the result of a projection (Rotations, Zoom, Panning, etc.) as well as the visualization attributes (Mode, Lighting, Clipping, etc.) in any particular view.
1305 The *V3d* package is basically a set of tools directed by commands from the viewer front-end. This tool set contains methods for creating and editing classes of the viewer such as:
1306 * Default parameters of the viewer,
1307 * Views (orthographic, perspective),
1308 * Lighting (positional, directional, ambient, spot, headlight),
1310 * Instantiated sequences of views, planes, light sources, graphic structures, and picks,
1311 * Various package methods.
1313 @subsubsection occt_visu_4_4_2 A programming example
1315 This sample TEST program for the *V3d* Package uses primary packages *Xw* and *Graphic3d* and secondary packages *Visual3d, Aspect, Quantity* and *math*.
1318 // create a default display connection
1319 Handle(Aspect_DisplayConnection) aDispConnection = new Aspect_DisplayConnection();
1320 // create a Graphic Driver
1321 Handle(OpenGl_GraphicDriver) aGraphicDriver = new OpenGl_GraphicDriver (aDispConnection);
1322 // create a Viewer to this Driver
1323 Handle(V3d_Viewer) VM = new V3d_Viewer (aGraphicDriver);
1324 VM->SetDefaultBackgroundColor (Quantity_NOC_DARKVIOLET);
1325 VM->SetDefaultViewProj (V3d_Xpos);
1326 // Create a structure in this Viewer
1327 Handle(Graphic3d_Structure) aStruct = new Graphic3d_Structure (VM->Viewer());
1329 // Type of structure
1330 aStruct->SetVisual (Graphic3d_TOS_SHADING);
1332 // Create a group of primitives in this structure
1333 Handle(Graphic3d_Group) aPrsGroup = new Graphic3d_Group (aStruct);
1335 // Fill this group with one quad of size 100
1336 Handle(Graphic3d_ArrayOfTriangleStrips) aTriangles = new Graphic3d_ArrayOfTriangleStrips (4);
1337 aTriangles->AddVertex (-100./2., -100./2., 0.0);
1338 aTriangles->AddVertex (-100./2., 100./2., 0.0);
1339 aTriangles->AddVertex ( 100./2., -100./2., 0.0);
1340 aTriangles->AddVertex ( 100./2., 100./2., 0.0);
1341 aPrsGroup->AddPrimitiveArray (aTriangles);
1342 aPrsGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectFillArea3d());
1344 // Create Ambient and Infinite Lights in this Viewer
1345 Handle(V3d_AmbientLight) aLight1 = new V3d_AmbientLight (VM, Quantity_NOC_GRAY50);
1346 Handle(V3d_DirectionalLight) aLight2 = new V3d_DirectionalLight (VM, V3d_XnegYnegZneg, Quantity_NOC_WHITE);
1348 // Create a 3D quality Window with the same DisplayConnection
1349 Handle(Xw_Window) aWindow = new Xw_Window (aDispConnection, "Test V3d", 0.5, 0.5, 0.5, 0.5);
1351 // Map this Window to this screen
1354 // Create a Perspective View in this Viewer
1355 Handle(V3d_View) aView = new V3d_View (VM);
1356 aView->Camera()->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
1357 // Associate this View with the Window
1358 aView ->SetWindow (aWindow);
1359 // Display ALL structures in this View
1360 VM->Viewer()->Display();
1361 // Finally update the Visualization in this View
1363 // Fit view to object size
1367 @subsubsection occt_visu_4_4_3 Define viewing parameters
1369 View projection and orientation in OCCT *V3d_View* are driven by camera. The camera calculates and supplies projection and view orientation matrices for rendering by OpenGL. The allows to the user to control all projection parameters. The camera is defined by the following properties:
1371 * **Eye** -- defines the observer (camera) position. Make sure the Eye point never gets between the Front and Back clipping planes.
1373 * **Center** -- defines the origin of View Reference Coordinates (where camera is aimed at).
1375 * **Direction** -- defines the direction of camera view (from the Eye to the Center).
1377 * **Distance** -- defines the distance between the Eye and the Center.
1379 * **Front** Plane -- defines the position of the front clipping plane in View Reference Coordinates system.
1381 * **Back** Plane -- defines the position of the back clipping plane in View Reference Coordinates system.
1383 * **ZNear** -- defines the distance between the Eye and the Front plane.
1385 * **ZFar** -- defines the distance between the Eye and the Back plane.
1387 Most common view manipulations (panning, zooming, rotation) are implemented as convenience methods of *V3d_View* class, however *Graphic3d_Camera* class can also be used directly by application developers:
1391 // rotate camera by X axis on 30.0 degrees
1393 aTrsf.SetRotation (gp_Ax1 (gp_Pnt (0.0, 0.0, 0.0), gp_Dir (1.0, 0.0, 0.0)), 30.0);
1394 aView->Camera()->Transform (aTrsf);
1397 @subsubsection occt_visu_4_4_4 Orthographic Projection
1399 @figure{view_frustum.png,"Perspective and orthographic projection",420}
1401 The following code configures the camera for orthographic rendering:
1404 // Create an orthographic View in this Viewer
1405 Handle(V3d_View) aView = new V3d_View (VM);
1406 aView->Camera()->SetProjectionType (Graphic3d_Camera::Projection_Orthographic);
1407 // update the Visualization in this View
1411 @subsubsection occt_visu_4_4_5 Perspective Projection
1413 **Field of view (FOVy)** -- defines the field of camera view by y axis in degrees (45° is default).
1415 @figure{camera_perspective.png,"Perspective frustum",420}
1417 The following code configures the camera for perspective rendering:
1420 // Create a perspective View in this Viewer
1421 Handle(V3d_View) aView = new V3d_View(VM);
1422 aView->Camera()->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
1427 @subsubsection occt_visu_4_4_6 Stereographic Projection
1429 **IOD** -- defines the intraocular distance (in world space units).
1431 There are two types of IOD:
1432 * _Graphic3d_Camera::IODType_Absolute_ : Intraocular distance is defined as an absolute value.
1433 * _Graphic3d_Camera::IODType_Relative_ : Intraocular distance is defined relative to the camera focal length (as its coefficient).
1435 **Field of view (FOV)** -- defines the field of camera view by y axis in degrees (45° is default).
1437 **ZFocus** -- defines the distance to the point of stereographic focus.
1439 @figure{stereo.png,"Stereographic projection",420}
1441 To enable stereo projection, your workstation should meet the following requirements:
1443 * The graphic card should support quad buffering.
1444 * You need active 3D glasses (LCD shutter glasses).
1445 * The graphic driver needs to be configured to impose quad buffering for newly created OpenGl contexts; the viewer and the view should be created after that.
1447 In stereographic projection mode the camera prepares two projection matrices to display different stereo-pictures for the left and for the right eye. In a non-stereo camera this effect is not visible because only the same projection is used for both eyes.
1449 To enable quad buffering support you should provide the following settings to the graphic driver *OpenGl_Caps*:
1452 Handle(OpenGl_GraphicDriver) aDriver = new OpenGl_GraphicDriver();
1453 OpenGl_Caps& aCaps = aDriver->ChangeOptions();
1454 aCaps.contextStereo = Standard_True;
1457 The following code configures the camera for stereographic rendering:
1460 // Create a Stereographic View in this Viewer
1461 Handle(V3d_View) aView = new V3d_View(VM);
1462 aView->Camera()->SetProjectionType (Graphic3d_Camera::Projection_Stereo);
1463 // Change stereo parameters
1464 aView->Camera()->SetIOD (IODType_Absolute, 5.0);
1465 // Finally update the Visualization in this View
1469 @subsubsection occt_visu_4_4_7 View frustum culling
1471 The algorithm of frustum culling on CPU-side is activated by default for 3D viewer. This algorithm allows skipping the presentation outside camera at the rendering stage, providing better performance. The following features support this method:
1472 * *Graphic3d_Structure::CalculateBoundBox()* is used to calculate axis-aligned bounding box of a presentation considering its transformation.
1473 * *V3d_View::SetFrustumCulling* enables or disables frustum culling for the specified view.
1474 * Classes *Graphic3d_BvhCStructureSet* and *Graphic3d_CullingTool* handle the detection of outer objects and usage of acceleration structure for frustum culling.
1475 * *BVH_BinnedBuilder* class splits several objects with null bounding box.
1477 @subsubsection occt_visu_4_4_9 View background styles
1478 There are three types of background styles available for *V3d_View*: solid color, gradient color and image.
1480 To set solid color for the background you can use the following method:
1482 void V3d_View::SetBackgroundColor (const Quantity_Color& theColor);
1485 The gradient background style could be set up with the following method:
1487 void V3d_View::SetBgGradientColors (const Quantity_Color& theColor1,
1488 const Quantity_Color& theColor2,
1489 const Aspect_GradientFillMethod theFillStyle,
1490 const Standard_Boolean theToUpdate = false);
1493 The *theColor1* and *theColor2* parameters define the boundary colors of interpolation, the *theFillStyle* parameter defines the direction of interpolation.
1495 To set the image as a background and change the background image style you can use the following method:
1497 void V3d_View::SetBackgroundImage (const Standard_CString theFileName,
1498 const Aspect_FillMethod theFillStyle,
1499 const Standard_Boolean theToUpdate = false);
1502 The *theFileName* parameter defines the image file name and the path to it, the *theFillStyle* parameter defines the method of filling the background with the image. The methods are:
1503 * *Aspect_FM_NONE* -- draws the image in the default position;
1504 * *Aspect_FM_CENTERED* -- draws the image at the center of the view;
1505 * *Aspect_FM_TILED* -- tiles the view with the image;
1506 * *Aspect_FM_STRETCH* -- stretches the image over the view.
1508 @subsubsection occt_visu_4_4_10 Dumping a 3D scene into an image file
1510 The 3D scene displayed in the view can be dumped into image file with resolution independent from window size (using offscreen buffer).
1511 The *V3d_View* has the following methods for dumping the 3D scene:
1513 Standard_Boolean V3d_View::Dump (const Standard_CString theFile,
1514 const Image_TypeOfImage theBufferType);
1516 Dumps the scene into an image file with the view dimensions.
1517 The raster image data handling algorithm is based on the *Image_AlienPixMap* class. The supported extensions are ".png", ".bmp", ".jpg" and others supported by **FreeImage** library.
1518 The value passed as *theBufferType* argument defines the type of the buffer for an output image (RGB, RGBA, floating-point, RGBF, RGBAF). Method returns TRUE if the scene has been successfully dumped.
1521 Standard_Boolean V3d_View::ToPixMap (Image_PixMap& theImage,
1522 const V3d_ImageDumpOptions& theParams);
1524 Dumps the displayed 3d scene into a pixmap with a width and height passed through parameters structure *theParams*.
1526 @subsubsection occt_visu_4_4_13 Ray tracing support
1528 OCCT visualization provides rendering by real-time ray tracing technique. It is allowed to switch easily between usual rasterization and ray tracing rendering modes. The core of OCCT ray tracing is written using GLSL shaders. The ray tracing has a wide list of features:
1534 * Support of non-polygon objects, such as lines, text, highlighting, selection.
1535 * Performance optimization using 2-level bounding volume hierarchy (BVH).
1537 The ray tracing algorithm is recursive (Whitted's algorithm). It uses BVH effective optimization structure. The structure prepares optimized data for a scene geometry for further displaying it in real-time. The time-consuming re-computation of the BVH is not necessary for view operations, selections, animation and even editing of the scene by transforming location of the objects. It is only necessary when the list of displayed objects or their geometry changes.
1538 To make the BVH reusable it has been added into an individual reusable OCCT package *TKMath/BVH*.
1540 There are several ray-tracing options that user can switch on/off:
1541 * Maximum ray tracing depth
1543 * Specular reflections
1544 * Adaptive anti aliasing
1545 * Transparency shadow effects
1549 Graphic3d_RenderingParams& aParams = aView->ChangeRenderingParams();
1550 // specifies rendering mode
1551 aParams.Method = Graphic3d_RM_RAYTRACING;
1552 // maximum ray-tracing depth
1553 aParams.RaytracingDepth = 3;
1554 // enable shadows rendering
1555 aParams.IsShadowEnabled = true;
1556 // enable specular reflections.
1557 aParams.IsReflectionEnabled = true;
1558 // enable adaptive anti-aliasing
1559 aParams.IsAntialiasingEnabled = true;
1560 // enable light propagation through transparent media.
1561 aParams.IsTransparentShadowEnabled = true;
1566 @subsubsection occt_visu_4_4_14 Display priorities
1568 Structure display priorities control the order, in which structures are drawn. When you display a structure you specify its priority. The lower is the value, the lower is the display priority. When the display is regenerated, the structures with the lowest priority are drawn first. The structures with the same display priority are drawn in the same order as they have been displayed. OCCT supports eleven structure display priorities.
1570 @subsubsection occt_visu_4_4_15 Z-layer support
1572 OCCT features depth-arranging functionality called z-layer. A graphical presentation can be put into a z-layer. In general, this function can be used for implementing "bring to front" functionality in a graphical application.
1577 // set z-layer to an interactive object
1578 Handle(AIS_InteractiveContext) theContext;
1579 Handle(AIS_InteractiveObject) theInterObj;
1580 Standard_Integer anId = 3;
1581 aViewer->AddZLayer (anId);
1582 theContext->SetZLayer (theInterObj, anId);
1585 For each z-layer, it is allowed to:
1586 * Enable / disable depth test for layer.
1587 * Enable / disable depth write for layer.
1588 * Enable / disable depth buffer clearing.
1589 * Enable / disable polygon offset.
1591 You can get the options using getter from *V3d_Viewer*. It returns *Graphic3d_ZLayerSettings* for a given *LayerId*.
1595 // change z-layer settings
1596 Graphic3d_ZLayerSettings aSettings = aViewer->ZLayerSettings (anId);
1597 aSettings.SetEnableDepthTest (true);
1598 aSettings.SetEnableDepthWrite(true);
1599 aSettings.SetClearDepth (true);
1600 aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
1601 aViewer->SetZLayerSettings (anId, aSettings);
1604 Another application for Z-Layer feature is treating visual precision issues when displaying objects far from the World Center.
1605 The key problem with such objects is that visualization data is stored and manipulated with single precision floating-point numbers (32-bit).
1606 Single precision 32-bit floating-point numbers give only 6-9 significant decimal digits precision,
1607 while double precision 64-bit numbers give 15-17 significant decimal digits precision, which is sufficient enough for most applications.
1609 When moving an Object far from the World Center, float number steadily eats precision.
1610 The camera Eye position adds leading decimal digits to the overall Object transformation, which discards smaller digits due to floating point number nature.
1611 For example, the object of size 0.0000123 moved to position 1000 has result transformation 1000.0000123,
1612 which overflows single precision floating point - considering the most optimistic scenario of 9 significant digits (but it is really not this case), the result number will be 1000.00001.
1614 This imprecision results in visual artifacts of two kinds in the 3D Viewer:
1616 * Overall per-vertex Object distortion.
1617 This happens when each vertex position has been defined within World Coordinate system.
1618 * The object itself is not distorted, but its position in the World is unstable and imprecise - the object jumps during camera manipulations.
1619 This happens when vertices have been defined within Local Coordinate system at the distance small enough to keep precision within single precision float,
1620 however Local Transformation applied to the Object is corrupted due to single precision float.
1622 The first issue cannot be handled without switching the entire presentation into double precision (for each vertex position).
1623 However, visualization hardware is much faster using single precision float number rather than double precision - so this is not an option in most cases.
1624 The second issue, however, can be negated by applying special rendering tricks.
1626 So, to apply this feature in OCCT, the application:
1628 * Defines Local Transformation for each object to fit the presentation data into single precision float without distortion.
1629 * Spatially splits the world into smaller areas/cells where single precision float will be sufficient.
1630 The size of such cell might vary and depends on the precision required by application (e.g. how much user is able to zoom in camera within application).
1631 * Defines a Z-Layer for each spatial cell containing any object.
1632 * Defines the Local Origin property of the Z-Layer according to the center of the cell.
1635 Graphic3d_ZLayerSettings aSettings = aViewer->ZLayerSettings (anId);
1636 aSettings.SetLocalOrigin (400.0, 0.0, 0.0);
1638 * Assigns a presentable object to the nearest Z-Layer.
1640 Note that Local Origin of the Layer is used only for rendering - everything outside will be still defined in the World Coordinate System,
1641 including Local Transformation of the Object and Detection results.
1642 E.g., while moving the presentation between Z-layers with different Local Origins, the Object will stay at the same place - only visualization quality will vary.
1644 @subsubsection occt_visu_4_4_16 Clipping planes
1646 The ability to define custom clipping planes could be very useful for some tasks. OCCT provides such an opportunity.
1648 The *Graphic3d_ClipPlane* class provides the services for clipping planes: it holds the plane equation coefficients and provides its graphical representation. To set and get plane equation coefficients you can use the following methods:
1651 Graphic3d_ClipPlane::Graphic3d_ClipPlane (const gp_Pln& thePlane)
1652 void Graphic3d_ClipPlane::SetEquation (const gp_Pln& thePlane)
1653 Graphic3d_ClipPlane::Graphic3d_ClipPlane (const Equation& theEquation)
1654 void Graphic3d_ClipPlane::SetEquation (const Equation& theEquation)
1655 gp_Pln Graphic3d_ClipPlane::ToPlane() const
1658 The clipping planes can be activated with the following method:
1660 void Graphic3d_ClipPlane::SetOn (const Standard_Boolean theIsOn)
1663 The number of clipping planes is limited. You can check the limit value via method *Graphic3d_GraphicDriver::InquireLimit()*;
1666 // get the limit of clipping planes for the current view
1667 Standard_Integer aMaxClipPlanes = aView->Viewer()->Driver()->InquireLimit (Graphic3d_TypeOfLimit_MaxNbClipPlanes);
1670 Let us see for example how to create a new clipping plane with custom parameters and add it to a view or to an object:
1672 // create a new clipping plane
1673 const Handle(Graphic3d_ClipPlane)& aClipPlane = new Graphic3d_ClipPlane();
1674 // change equation of the clipping plane
1675 Standard_Real aCoeffA = ...
1676 Standard_Real aCoeffB = ...
1677 Standard_Real aCoeffC = ...
1678 Standard_Real aCoeffD = ...
1679 aClipPlane->SetEquation (gp_Pln (aCoeffA, aCoeffB, aCoeffC, aCoeffD));
1681 aClipPlane->SetCapping (aCappingArg == "on");
1682 // set the material with red color of clipping plane
1683 Graphic3d_MaterialAspect aMat = aClipPlane->CappingMaterial();
1684 Quantity_Color aColor (1.0, 0.0, 0.0, Quantity_TOC_RGB);
1685 aMat.SetAmbientColor (aColor);
1686 aMat.SetDiffuseColor (aColor);
1687 aClipPlane->SetCappingMaterial (aMat);
1688 // set the texture of clipping plane
1689 Handle(Graphic3d_Texture2Dmanual) aTexture = ...
1690 aTexture->EnableModulate();
1691 aTexture->EnableRepeat();
1692 aClipPlane->SetCappingTexture (aTexture);
1693 // add the clipping plane to an interactive object
1694 Handle(AIS_InteractiveObject) aIObj = ...
1695 aIObj->AddClipPlane (aClipPlane);
1696 // or to the whole view
1697 aView->AddClipPlane (aClipPlane);
1698 // activate the clipping plane
1699 aClipPlane->SetOn(Standard_True);
1705 @subsubsection occt_visu_4_4_17 Automatic back face culling
1707 Back face culling reduces the rendered number of triangles (which improves the performance) and eliminates artifacts at shape boundaries. However, this option can be used only for solid objects, where the interior is actually invisible from any point of view. Automatic back-face culling mechanism is turned on by default, which is controlled by *V3d_View::SetBackFacingModel()*.
1709 The following features are applied in *StdPrs_ToolTriangulatedShape::IsClosed()*, which is used for definition of back face culling in *ShadingAspect*:
1710 * disable culling for free closed Shells (not inside the Solid) since reversed orientation of a free Shell is a valid case;
1711 * enable culling for Solids packed into a compound;
1712 * ignore Solids with incomplete triangulation.
1714 Back face culling is turned off at TKOpenGl level in the following cases:
1715 * clipping/capping planes are in effect;
1716 * for translucent objects;
1717 * with hatching presentation style.
1719 @subsection occt_visu_4_5 Examples: creating a 3D scene
1721 To create 3D graphic objects and display them in the screen, follow the procedure below:
1722 1. Create attributes.
1723 2. Create a 3D viewer.
1725 4. Create an interactive context.
1726 5. Create interactive objects.
1727 6. Create primitives in the interactive object.
1728 7. Display the interactive object.
1730 @subsubsection occt_visu_4_5_1 Create attributes
1735 Quantity_Color aBlack (Quantity_NOC_BLACK);
1736 Quantity_Color aBlue (Quantity_NOC_MATRABLUE);
1737 Quantity_Color aBrown (Quantity_NOC_BROWN4);
1738 Quantity_Color aFirebrick (Quantity_NOC_FIREBRICK);
1739 Quantity_Color aForest (Quantity_NOC_FORESTGREEN);
1740 Quantity_Color aGray (Quantity_NOC_GRAY70);
1741 Quantity_Color aMyColor (0.99, 0.65, 0.31, Quantity_TOC_RGB);
1742 Quantity_Color aBeet (Quantity_NOC_BEET);
1743 Quantity_Color aWhite (Quantity_NOC_WHITE);
1746 Create line attributes.
1749 Handle(Graphic3d_AspectLine3d) anAspectBrown = new Graphic3d_AspectLine3d();
1750 Handle(Graphic3d_AspectLine3d) anAspectBlue = new Graphic3d_AspectLine3d();
1751 Handle(Graphic3d_AspectLine3d) anAspectWhite = new Graphic3d_AspectLine3d();
1752 anAspectBrown->SetColor (aBrown);
1753 anAspectBlue ->SetColor (aBlue);
1754 anAspectWhite->SetColor (aWhite);
1757 Create marker attributes.
1759 Handle(Graphic3d_AspectMarker3d aFirebrickMarker = new Graphic3d_AspectMarker3d();
1760 // marker attributes
1761 aFirebrickMarker->SetColor (Firebrick);
1762 aFirebrickMarker->SetScale (1.0);
1763 aFirebrickMarker->SetType (Aspect_TOM_BALL);
1765 // it is a preferred way (supports full-color images on modern hardware).
1766 aFirebrickMarker->SetMarkerImage (theImage)
1769 Create facet attributes.
1771 Handle(Graphic3d_AspectFillArea3d) aFaceAspect = new Graphic3d_AspectFillArea3d();
1772 Graphic3d_MaterialAspect aBrassMaterial (Graphic3d_NOM_BRASS);
1773 Graphic3d_MaterialAspect aGoldMaterial (Graphic3d_NOM_GOLD);
1774 aFaceAspect->SetInteriorStyle (Aspect_IS_SOLID_WIREFRAME);
1775 aFaceAspect->SetInteriorColor (aMyColor);
1776 aFaceAspect->SetDistinguishOn ();
1777 aFaceAspect->SetFrontMaterial (aGoldMaterial);
1778 aFaceAspect->SetBackMaterial (aBrassMaterial);
1781 Create text attributes.
1783 Handle(Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d (aForest, Graphic3d_NOF_ASCII_MONO, 1.0, 0.0);
1786 @subsubsection occt_visu_4_5_2 Create a 3D Viewer (a Windows example)
1789 // create a default connection
1790 Handle(Aspect_DisplayConnection) aDisplayConnection;
1791 // create a graphic driver from default connection
1792 Handle(OpenGl_GraphicDriver) aGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection);
1794 myViewer = new V3d_Viewer (aGraphicDriver);
1795 // set parameters for V3d_Viewer
1796 // defines default lights -
1797 // positional-light 0.3 0.0 0.0
1798 // directional-light V3d_XnegYposZpos
1799 // directional-light V3d_XnegYneg
1801 a3DViewer->SetDefaultLights();
1802 // activates all the lights defined in this viewer
1803 a3DViewer->SetLightOn();
1804 // set background color to black
1805 a3DViewer->SetDefaultBackgroundColor (Quantity_NOC_BLACK);
1809 @subsubsection occt_visu_4_5_3 Create a 3D view (a Windows example)
1811 It is assumed that a valid Windows window may already be accessed via the method *GetSafeHwnd()* (as in case of MFC sample).
1813 Handle(WNT_Window) aWNTWindow = new WNT_Window (GetSafeHwnd());
1814 myView = myViewer->CreateView();
1815 myView->SetWindow (aWNTWindow);
1818 @subsubsection occt_visu_4_5_4 Create an interactive context
1821 myAISContext = new AIS_InteractiveContext (myViewer);
1824 You are now able to display interactive objects such as an *AIS_Shape*.
1827 TopoDS_Shape aShape = BRepAPI_MakeBox (10, 20, 30).Solid();
1828 Handle(AIS_Shape) anAISShape = new AIS_Shape (aShape);
1829 myAISContext->Display (anAISShape);
1832 @subsubsection occt_visu_4_5_5 Create your own interactive object
1834 Follow the procedure below to compute the presentable object:
1836 1. Build a presentable object inheriting from *AIS_InteractiveObject* (refer to the Chapter on @ref occt_visu_2_1 "Presentable Objects").
1837 2. Reuse the *Graphic3d_Structure* provided as an argument of the compute methods.
1839 **Note** that there are two compute methods: one for a standard representation, and the other for a degenerated representation, i.e. in hidden line removal and wireframe modes.
1841 Let us look at the example of compute methods
1844 void MyPresentableObject::Compute (const Handle(PrsMgr_PresentationManager3d)& thePrsManager,
1845 const Handle(Graphic3d_Structure)& thePrs,
1846 const Standard_Integer theMode)
1851 void MyPresentableObject::Compute (const Handle(Prs3d_Projector)& theProjector,
1852 const Handle(Graphic3d_Structure)& thePrs)
1858 @subsubsection occt_visu_4_5_6 Create primitives in the interactive object
1860 Get the group used in *Graphic3d_Structure*.
1863 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
1866 Update the group attributes.
1869 aGroup->SetGroupPrimitivesAspect (anAspectBlue);
1872 Create two triangles in *aGroup*.
1875 Standard_Integer aNbTria = 2;
1876 Handle(Graphic3d_ArrayOfTriangles) aTriangles = new Graphic3d_ArrayOfTriangles (3 * aNbTria, 0, true);
1877 for (Standard_Integer aTriIter = 1; aTriIter <= aNbTria; ++aTriIter)
1879 aTriangles->AddVertex (aTriIter * 5., 0., 0., 1., 1., 1.);
1880 aTriangles->AddVertex (aTriIter * 5 + 5, 0., 0., 1., 1., 1.);
1881 aTriangles->AddVertex (aTriIter * 5 + 2.5, 5., 0., 1., 1., 1.);
1883 aGroup->AddPrimitiveArray (aTriangles);
1884 aGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectFillArea3d());
1887 Use the polyline function to create a boundary box for the *thePrs* structure in group *aGroup*.
1890 Standard_Real Xm, Ym, Zm, XM, YM, ZM;
1891 thePrs->MinMaxValues (Xm, Ym, Zm, XM, YM, ZM);
1893 Handle(Graphic3d_ArrayOfPolylines) aPolylines = new Graphic3d_ArrayOfPolylines (16, 4);
1894 aPolylines->AddBound (4);
1895 aPolylines->AddVertex (Xm, Ym, Zm);
1896 aPolylines->AddVertex (Xm, Ym, ZM);
1897 aPolylines->AddVertex (Xm, YM, ZM);
1898 aPolylines->AddVertex (Xm, YM, Zm);
1899 aPolylines->AddBound (4);
1900 aPolylines->AddVertex (Xm, Ym, Zm);
1901 aPolylines->AddVertex (XM, Ym, Zm);
1902 aPolylines->AddVertex (XM, Ym, ZM);
1903 aPolylines->AddVertex (XM, YM, ZM);
1904 aPolylines->AddBound (4);
1905 aPolylines->AddVertex (XM, YM, Zm);
1906 aPolylines->AddVertex (XM, Ym, Zm);
1907 aPolylines->AddVertex (XM, YM, Zm);
1908 aPolylines->AddVertex (Xm, YM, Zm);
1909 aPolylines->AddBound (4);
1910 aPolylines->AddVertex (Xm, YM, ZM);
1911 aPolylines->AddVertex (XM, YM, ZM);
1912 aPolylines->AddVertex (XM, Ym, ZM);
1913 aPolylines->AddVertex (Xm, Ym, ZM);
1915 aGroup->AddPrimitiveArray(aPolylines);
1916 aGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d());
1919 Create text and markers in group *aGroup*.
1922 static char* texte[3] =
1924 "Application title",
1926 "My company address."
1928 Handle(Graphic3d_ArrayOfPoints) aPtsArr = new Graphic3d_ArrayOfPoints (2, 1);
1929 aPtsArr->AddVertex (-40.0, -40.0, -40.0);
1930 aPtsArr->AddVertex (40.0, 40.0, 40.0);
1931 aGroup->AddPrimitiveArray (aPtsArr);
1932 aGroup->SetGroupPrimitivesAspect (new Graphic3d_AspectText3d());
1934 Graphic3d_Vertex aMarker (0.0, 0.0, 0.0);
1935 for (int i = 0; i <= 2; i++)
1937 aMarker.SetCoord (-(Standard_Real )i * 4 + 30,
1938 (Standard_Real )i * 4,
1939 -(Standard_Real )i * 4);
1940 aGroup->Text (texte[i], Marker, 20.);
1945 @section occt_visu_5 Mesh Visualization Services
1947 *MeshVS* (Mesh Visualization Service) component extends 3D visualization capabilities of Open CASCADE Technology. It provides flexible means of displaying meshes along with associated pre- and post-processor data.
1949 From a developer's point of view, it is easy to integrate the *MeshVS* component into any mesh-related application with the following guidelines:
1951 * Derive a data source class from the *MeshVS_DataSource* class.
1952 * Re-implement its virtual methods, so as to give the *MeshVS* component access to the application data model. This is the most important part of the job, since visualization performance is affected by performance of data retrieval methods of your data source class.
1953 * Create an instance of *MeshVS_Mesh* class.
1954 * Create an instance of your data source class and pass it to a *MeshVS_Mesh* object through the *SetDataSource()* method.
1955 * Create one or several objects of *MeshVS_PrsBuilder*-derived classes (standard, included in the *MeshVS* package, or your custom ones).
1956 * Each *PrsBuilder* is responsible for drawing a *MeshVS_Mesh* presentation in a certain display mode(s) specified as a *PrsBuilder* constructor's argument. Display mode is treated by *MeshVS* classes as a combination of bit flags (two least significant bits are used to encode standard display modes: wireframe, shading and shrink).
1957 * Pass these objects to the *MeshVS_Mesh::AddBuilder()* method. *MeshVS_Mesh* takes advantage of improved selection highlighting mechanism: it highlights its selected entities itself, with the help of so called "highlighter" object. You can set one of *PrsBuilder* objects to act as a highlighter with the help of a corresponding argument of the *AddBuilder()* method.
1959 Visual attributes of the *MeshVS_Mesh* object (such as shading color, shrink coefficient and so on) are controlled through *MeshVS_Drawer* object. It maintains a map "Attribute ID --> attribute value" and can be easily extended with any number of custom attributes.
1961 In all other respects, *MeshVS_Mesh* is very similar to any other class derived from *AIS_InteractiveObject* and it should be used accordingly (refer to the description of *AIS package* in the documentation).