DiscussionsIssue archiveOCCT:Visualization

Archived issue #0026195

Visualization - optimize selection algorithms

Open CASCADEOCCT:Visualizationclosed42 public notes

Search issues

Description

Current implementation of selection algorithms has some inefficiencies which can lead to performance degradation.

1) Select3D_SensitiveTriangulation::overlapsElement(...) performs transformation of every triangle in the set. Instead of this, it is necessary to transform frustum (only once) before processing triangles set.

2) Frustum construction is inefficient. For example, in the SelectMgr_RectangularFrustum::Build(...) method, frustum normals are computed, and after that frustum edges are computed. Therefore, edges are computed twice. It is better to compute edges and use them for computation of normals. Another point is the use of

  SelectMgr_Vec3 aDimensions[3] =
  {
    SelectMgr_Vec3 (1.0, 0.0, 0.0),
    SelectMgr_Vec3 (0.0, 1.0, 0.0),
    SelectMgr_Vec3 (0.0, 0.0, 1.0)
  };

.....

  for (Standard_Integer aVertIdx = 0; aVertIdx < 8; ++aVertIdx)
  {
    Standard_Real aProjection = DOT (aDimensions[aDim], myVertices[aVertIdx]);
    aMax = Max (aProjection, aMax);
    aMin = Min (aProjection, aMin);
  }

This code is overkilled because in this case:

  DOT (aDimensions[aDim], myVertices[aVertIdx]) == myVertices[aVertIdx][aDim]

Projecting frustum on plane normals is also inefficient:

  for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < 6; ++aPlaneIdx)
  {
    Standard_Real aMax = -DBL_MAX;
    Standard_Real aMin = DBL_MAX;
    const SelectMgr_Vec3 aPlane = myPlanes[aPlaneIdx];
    for (Standard_Integer aVertIdx = 0; aVertIdx < 8; ++aVertIdx)
    {
      Standard_Real aProjection = DOT (aPlane, myVertices[aVertIdx]);
      aMax = Max (aMax, aProjection);
      aMin = Min (aMin, aProjection);
    }
    myMaxVertsProjections[aPlaneIdx] = aMax;
    myMinVertsProjections[aPlaneIdx] = aMin;
  }

In case of ortho frustum checking all 6 normals is redundant. Moreover, instead of projecting all 8 vertices we can use only 2 vertices which lie on appropriate diagonal of the box.

3) In SelectMgr_RectangularFrustum::Build (const gp_Pnt2d &thePoint) and SelectMgr_RectangularFrustum::Build (const gp_Pnt2d& theMinPnt, const gp_Pnt2d& theMaxPnt) the code is duplicated. Please redesign it to share common build function.

4) The above remaks also applicable to SelectMgr_RectangularFrustum::Transform (const gp_Trsf& theTrsf). This method also uses redundant SelectMgr_Vec3 aDimensions[3] and computes normals before edges.

5) Another point in SelectMgr_RectangularFrustum::Transform(...) is related to normal computation. It is not necessary to re-compute them from scratch. You simply need to transform normals into new space. Please note, that normals are transformed with inversed transposed matrix. These optimization are very important because frustum transformatioans are used widely. Please check current performance of this method and its performance after optimizations (e.g., by calling this single method 1000 000 times). Please check another possible performance issues by using profiler.

6) Remove macroses like DISTANCE (...), DOT(...) and use member fucntions of SelectMgr_Vec3 class.

7) Revise a set of SelectMgr_RectangularFrustum::Overlaps(...) methods. Some of them look redundant, and some of them are not compatible. E.g.
  
  Overlaps (const SelectMgr_Vec3& theBoxMin,
            const SelectMgr_Vec3& theBoxMax,
            Standard_Boolean* theInside)

  Overlaps (const BVH_Box<Standard_Real, 3>& theBox,
            Standard_Real& theDepth)

The following method should have analog without theDepth parameter (can be used in many cases):

   Overlaps (const gp_Pnt& thePnt,
             Standard_Real& theDepth)

8) Consider the use of BVH binned builder only along main axis in Select3D_SensitiveSet. And is absolutely necessary to decrease a number of bins in both Select3D_SensitiveSet and SelectMgr_SensitiveEntitySet. Consider 4-8 bins per node. Compare performance and build times.

9) Use NCollection_IndexedMap::Swap method instead of its emulation via 3 Substitute() methods.

10) Switch from double tolerances to integer pixel tolerance and eliminate dependence from type of sensitive entity in frustum cache map in SelectMgr_ViewerSelector.

Steps to reproduce

not needed

Public activity

42 archived notes

Participants are labeled by their role within this record.

01Commenter 1
Dear vpa,

please rebase your patch on 26364. It should solve remaining performance issues.
02Commenter 2
Branch [archived branch] has been created by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Mon Aug 17 20:25:43 2015 +0300

    Refactoring of triangular and rectangular frustum build procedure:
    - DOT/DOTp/DISTANCEp macros were eliminated;
    - normals calculation uses previously calculated edges;
    - added vertex projection calculation using closest diagonal;
    - common calculations were moved to separate functions;
    - Scale and Transform methods were replaced by single ScaleAndTransform.
03Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Thu Aug 20 18:12:03 2015 +0300

    Switch to integer pixel tolerances, part 1

Author: vpa
Date: Thu Aug 20 17:57:33 2015 +0300

    Corrected vertex projection for orthographic case;
    3 substitute operations were replaced by single swap.

04Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Fri Aug 21 19:48:12 2015 +0300

    Switch to integer tolerances, part 2;
    Added debug function to draw selecting frustum and display picked point data

05Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Tue Aug 25 17:42:46 2015 +0300

    SelectMgr_SelectableObjectSet BVH builder now splits only along main axis

Author: vpa
Date: Tue Aug 25 17:38:59 2015 +0300

    Switch to gp points and vectors instead of NCollection_Vec3 to eliminate macros and simplify calculations

Author: vpa
Date: Mon Aug 24 18:28:47 2015 +0300

    Unnecessary loop in SelectMgr_Frustum::hasOverlap for AABB was eliminated;
    Removed comparison in SelectMgr_Frustum::hasOverlap for planar convex polygin

06Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Wed Aug 26 19:34:14 2015 +0300

    Draft implementation of inilial location transformation application at SelectMgr_ViewerSelector level

Author: vpa
Date: Wed Aug 26 16:07:59 2015 +0300

    Refactoring of Overlaps methods

07Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Thu Aug 27 12:00:07 2015 +0300

    Error with transformation is fixed

08Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
09Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Fri Aug 28 15:17:49 2015 +0300

    Added possibility to parallelize 2nd level BVH tree build;
    Removed unnecessary variables from 1st and 2nd level BVH data sets

10Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
11Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
12Commenter 2
Branch [archived branch] has been created by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Fri Aug 28 16:46:15 2015 +0300

    0026195: Visualization - optimize selection algorithms
    
    - initial transformation of triangulation is now applied to selecting frustum;
    - switched from NCollection_Vec3 to gp collections to avoid conversions and usage of macros;
    - calculation of frustum was refactored to reduce its build time;
    - double pixel tolerances for selection were replaced by integer ones;
    - switched to splitting along the main axis only in SelectMgr BVH selection primitive sets.
13Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Fri Aug 28 17:01:43 2015 +0300

    Missing comments were added

14Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
15Commenter 15
Dear Danila,

could you please take a look at patch in branch CR26195_1?
16Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Fri Aug 28 18:58:26 2015 +0300

    Remarks from DUV

17Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
18Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Fri Aug 28 20:44:43 2015 +0300

    Regressions were fixed

19Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
20Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Sun Aug 30 22:17:59 2015 +0300

    Type-cast corrected

21Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Mon Aug 31 00:04:37 2015 +0300

    Debug function was improved

22Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
23Commenter 2
Branch [archived branch] has been updated by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Mon Aug 31 01:32:31 2015 +0300

    Cosmetics

24Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
25Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
26Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
27Commenter 27
Dear Kirill,

please review patch in branch CR26195.
Performance tests results are in attached file Attachment 1 (XLS). Please note that there is a regression in comparison of display times on test case with fine-tessellated MeshVS mesh. Such timings are more likely caused by performance of compute part; 2nd level BVH build time shows performance boost in comparison to v6.9.0.
28Commenter 3
Dear Varvara,

+
+    //! Returns a const ptr to coordinates location.
+    //! Is useful for algorithms, but DOES NOT PERFORM
+    //! ANY CHECKS!
+    operator const Standard_Real*() const { return (&x); }
+
+    //! Returns a ptr to coordinates location.
+    //! Is useful for algorithms, but DOES NOT PERFORM
+    //! ANY CHECKS!
+    operator Standard_Real*()             { return (&x); }

please replace these operators with named methods (like Coords() or GetData()/ChangeData()).
29Commenter 2
Branch [archived branch] has been created by Participant.

[revision removed]


Detailed log of new commits:

Author: vpa
Date: Mon Aug 31 00:07:01 2015 +0300

    0026195: Visualization - optimize selection algorithms
    
    - initial transformation of triangulation is now applied to selecting frustum;
    - switched from NCollection_Vec3 to gp collections to avoid conversions and usage of macros;
    - calculation of frustum was refactored to reduce its build time;
    - double pixel tolerances for selection were replaced by integer ones;
    - switched to splitting along the main axis only in SelectMgr BVH selection primitive sets.
30Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
31Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
32Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
33Commenter 33
Dear Kirill,

branch CR26195_1 was updated according to your remarks. Please, review.
Note that branch CR26195_2 is ported for integration to v6.9.1.
34Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
35Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
36Commenter 3
Please test patch in branch CR26195_1.
37Commenter 2
Branch [archived branch] has been updated forcibly by Participant.

[revision removed]
38Commenter 38
Branch [archived branch] has been rebased on the current master
39Commenter 39
Dear Commenter 1,

Branch CR26195_1 from occt git-repository (and master from products git-repository) was compiled on Linux, MacOS and Windows platforms and tested.
[revision removed]

Number of compiler warnings:
occt component:
   Linux: 15 (15 on master)
   Windows: 0 (0 on master)
products component :
   Linux: 39 (39 on master)
   Windows: 0 (0 on master)

Regressions/Differences:
Not detected

Testing cases:
Not needed

Testing on Linux:
Total MEMORY difference: 91284324 / 91941125 [-0.71%]
Total CPU difference: 17719.189999998875 / 17715.669999998972 [+0.02%]

Testing on Windows:
Total MEMORY difference: 57157329 / 57146693 [+0.02%]
Total CPU difference: 16407.249173999135 / 16584.80951219933 [-1.07%]
40Commenter 2
Branch [archived branch] has been deleted by Commenter 3.

[revision removed]
41Commenter 2
Branch [archived branch] has been deleted by Commenter 3.

[revision removed]
42Commenter 2
Branch [archived branch] has been deleted by Commenter 3.

[revision removed]

Related records