HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
selection.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_SELECTION_H
8 #define PXR_IMAGING_HD_SELECTION_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/api.h"
12 #include "pxr/imaging/hd/version.h"
13 #include "pxr/usd/sdf/path.h"
14 #include "pxr/base/gf/vec4f.h"
15 #include "pxr/base/vt/array.h"
16 
17 #include <memory>
18 #include <vector>
19 #include <unordered_map>
20 
22 
23 using HdSelectionSharedPtr = std::shared_ptr<class HdSelection>;
24 
25 /// \class HdSelection
26 ///
27 /// HdSelection holds a collection of selected items per selection mode.
28 /// The items may be rprims, instances of an rprim and subprimitives of an
29 /// rprim, such as elements (faces for meshes, individual curves for basis
30 /// curves), edges & points. Each item is referred to by the render index path.
31 ///
32 /// It current supports active and rollover selection modes, and may be
33 /// inherited for customization.
34 ///
36 {
37 public:
38  /// Selection modes allow differentiation in selection highlight behavior.
39  enum HighlightMode : uint32_t {
40  HighlightModeSelect = 0, // Active selection
41  HighlightModeLocate, // Rollover selection
42 
44  };
45 
46  HD_API
47  virtual ~HdSelection();
48 
49  /// ------------------------ Population API --------------------------------
50  HD_API
51  void AddRprim(HighlightMode const &mode,
52  SdfPath const &renderIndexPath);
53 
54  HD_API
55  void AddInstance(HighlightMode const &mode,
56  SdfPath const &renderIndexPath,
57  VtIntArray const &instanceIndex=VtIntArray());
58 
59  HD_API
60  void AddElements(HighlightMode const &mode,
61  SdfPath const &renderIndexPath,
62  VtIntArray const &elementIndices);
63 
64  HD_API
65  void AddEdges(HighlightMode const &mode,
66  SdfPath const &renderIndexPath,
67  VtIntArray const &edgeIndices);
68 
69  HD_API
70  void AddPoints(HighlightMode const &mode,
71  SdfPath const &renderIndexPath,
72  VtIntArray const &pointIndices);
73 
74  // Special handling for points: we allow a set of selected point indices to
75  // also specify a color to use for highlighting in the render's working
76  // color space.
77  HD_API
78  void AddPoints(HighlightMode const &mode,
79  SdfPath const &renderIndexPath,
80  VtIntArray const &pointIndices,
81  GfVec4f const &pointColor);
82 
83  // XXX: Ideally, this should be per instance, if we want to support
84  // selection of subprims (faces/edges/points) per instance of an rprim.
85  // By making this per rprim, all selected instances of the rprim will share
86  // the same subprim highlighting.
89 
91  // Use a vector of VtIntArray to avoid copying the indices data.
92  // This way, we support multiple Add<Subprim> operations without
93  // having to consolidate the indices each time.
94  std::vector<VtIntArray> instanceIndices;
95  std::vector<VtIntArray> elementIndices;
96  std::vector<VtIntArray> edgeIndices;
97  std::vector<VtIntArray> pointIndices;
98  std::vector<int> pointColorIndices;
99  };
100 
101  /// ---------------------------- Query API ---------------------------------
102 
103  // Returns a pointer to the selection state for the rprim path if selected.
104  // Returns nullptr otherwise.
105  HD_API
106  PrimSelectionState const *
108  SdfPath const &renderIndexPath) const;
109 
110  // Returns the selected rprim render index paths for all the selection
111  // modes. The vector returned may contain duplicates.
112  HD_API
114  GetAllSelectedPrimPaths() const;
115 
116  // Returns the selected rprim render index paths for the given mode.
117  HD_API
119  GetSelectedPrimPaths(HighlightMode const &mode) const;
120 
121  HD_API
122  std::vector<GfVec4f> const& GetSelectedPointColors() const;
123 
124  // Returns true if nothing is selected.
125  HD_API
126  bool IsEmpty() const;
127 
128  HD_API
129  static
131  HdSelectionSharedPtr const &,
132  HdSelectionSharedPtr const &);
133 
134 private:
135  void _AddPoints(HighlightMode const &mode,
136  SdfPath const &renderIndexPath,
137  VtIntArray const &pointIndices,
138  int pointColorIndex);
139 
140  void _GetSelectionPrimPathsForMode(HighlightMode const &mode,
141  SdfPathVector *paths) const;
142 
143 protected:
144  using _PrimSelectionStateMap =
145  std::unordered_map<SdfPath, PrimSelectionState, SdfPath::Hash>;
146  // Keep track of selection per selection mode.
148 
149  // Track all colors used for point selection highlighting.
150  std::vector<GfVec4f> _selectedPointColors;
151 };
152 
154 
155 #endif //PXR_IMAGING_HD_SELECTION_H
HD_API PrimSelectionState const * GetPrimSelectionState(HighlightMode const &mode, SdfPath const &renderIndexPath) const
-------------------------— Query API ------------------------------—
virtual HD_API ~HdSelection()
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:39
#define HD_API
Definition: api.h:23
HD_API void AddElements(HighlightMode const &mode, SdfPath const &renderIndexPath, VtIntArray const &elementIndices)
HD_API void AddEdges(HighlightMode const &mode, SdfPath const &renderIndexPath, VtIntArray const &edgeIndices)
std::unordered_map< SdfPath, PrimSelectionState, SdfPath::Hash > _PrimSelectionStateMap
Definition: selection.h:145
std::vector< GfVec4f > _selectedPointColors
Definition: selection.h:150
HD_API SdfPathVector GetSelectedPrimPaths(HighlightMode const &mode) const
std::vector< class SdfPath > SdfPathVector
std::vector< VtIntArray > elementIndices
Definition: selection.h:95
_PrimSelectionStateMap _selMap[HighlightModeCount]
Definition: selection.h:147
HD_API std::vector< GfVec4f > const & GetSelectedPointColors() const
Definition: path.h:273
static HD_API HdSelectionSharedPtr Merge(HdSelectionSharedPtr const &, HdSelectionSharedPtr const &)
GLenum mode
Definition: glcorearb.h:99
HD_API SdfPathVector GetAllSelectedPrimPaths() const
std::vector< VtIntArray > pointIndices
Definition: selection.h:97
Definition: vec4f.h:45
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HD_API void AddRprim(HighlightMode const &mode, SdfPath const &renderIndexPath)
---------------------— Population API -----------------------------—
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HD_API void AddPoints(HighlightMode const &mode, SdfPath const &renderIndexPath, VtIntArray const &pointIndices)
HD_API void AddInstance(HighlightMode const &mode, SdfPath const &renderIndexPath, VtIntArray const &instanceIndex=VtIntArray())
std::vector< int > pointColorIndices
Definition: selection.h:98
std::shared_ptr< class HdSelection > HdSelectionSharedPtr
Definition: selection.h:23
HD_API bool IsEmpty() const
std::vector< VtIntArray > edgeIndices
Definition: selection.h:96
std::vector< VtIntArray > instanceIndices
Definition: selection.h:94