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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_SELECTION_H
25 #define PXR_IMAGING_HD_SELECTION_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/usd/sdf/path.h"
31 #include "pxr/base/gf/vec4f.h"
32 #include "pxr/base/vt/array.h"
33 
34 #include <memory>
35 #include <vector>
36 #include <unordered_map>
37 
39 
40 using HdSelectionSharedPtr = std::shared_ptr<class HdSelection>;
41 
42 /// \class HdSelection
43 ///
44 /// HdSelection holds a collection of selected items per selection mode.
45 /// The items may be rprims, instances of an rprim and subprimitives of an
46 /// rprim, such as elements (faces for meshes, individual curves for basis
47 /// curves), edges & points. Each item is referred to by the render index path.
48 ///
49 /// It current supports active and rollover selection modes, and may be
50 /// inherited for customization.
51 ///
52 class HdSelection {
53 public:
54  /// Selection modes allow differentiation in selection highlight behavior.
56  HighlightModeSelect = 0, // Active selection
57  HighlightModeLocate, // Rollover selection
58 
60  };
61 
62  HdSelection() = default;
63 
64  HD_API
65  virtual ~HdSelection();
66 
67  /// ------------------------ Population API --------------------------------
68  HD_API
69  void AddRprim(HighlightMode const &mode,
70  SdfPath const &renderIndexPath);
71 
72  HD_API
73  void AddInstance(HighlightMode const &mode,
74  SdfPath const &renderIndexPath,
75  VtIntArray const &instanceIndex=VtIntArray());
76 
77  HD_API
78  void AddElements(HighlightMode const &mode,
79  SdfPath const &renderIndexPath,
80  VtIntArray const &elementIndices);
81 
82  HD_API
83  void AddEdges(HighlightMode const &mode,
84  SdfPath const &renderIndexPath,
85  VtIntArray const &edgeIndices);
86 
87  HD_API
88  void AddPoints(HighlightMode const &mode,
89  SdfPath const &renderIndexPath,
90  VtIntArray const &pointIndices);
91 
92  // Special handling for points: we allow a set of selected point indices to
93  // also specify a color to use for highlighting.
94  HD_API
95  void AddPoints(HighlightMode const &mode,
96  SdfPath const &renderIndexPath,
97  VtIntArray const &pointIndices,
98  GfVec4f const &pointColor);
99 
100  // XXX: Ideally, this should be per instance, if we want to support
101  // selection of subprims (faces/edges/points) per instance of an rprim.
102  // By making this per rprim, all selected instances of the rprim will share
103  // the same subprim highlighting.
106 
108  // Use a vector of VtIntArray to avoid copying the indices data.
109  // This way, we support multiple Add<Subprim> operations without
110  // having to consolidate the indices each time.
111  std::vector<VtIntArray> instanceIndices;
112  std::vector<VtIntArray> elementIndices;
113  std::vector<VtIntArray> edgeIndices;
114  std::vector<VtIntArray> pointIndices;
115  std::vector<int> pointColorIndices;
116  };
117 
118  /// ---------------------------- Query API ---------------------------------
119 
120  // Returns a pointer to the selection state for the rprim path if selected.
121  // Returns nullptr otherwise.
122  HD_API
123  PrimSelectionState const *
125  SdfPath const &renderIndexPath) const;
126 
127  // Returns the selected rprim render index paths for all the selection
128  // modes. The vector returned may contain duplicates.
129  HD_API
131  GetAllSelectedPrimPaths() const;
132 
133  // Returns the selected rprim render index paths for the given mode.
134  HD_API
136  GetSelectedPrimPaths(HighlightMode const &mode) const;
137 
138  HD_API
139  std::vector<GfVec4f> const& GetSelectedPointColors() const;
140 
141  // Returns true if nothing is selected.
142  HD_API
143  bool IsEmpty() const;
144 
145 private:
146  void _AddPoints(HighlightMode const &mode,
147  SdfPath const &renderIndexPath,
148  VtIntArray const &pointIndices,
149  int pointColorIndex);
150 
151  void _GetSelectionPrimPathsForMode(HighlightMode const &mode,
152  SdfPathVector *paths) const;
153 
154 protected:
155  typedef std::unordered_map<SdfPath, PrimSelectionState, SdfPath::Hash>
157  // Keep track of selection per selection mode.
159 
160  // Track all colors used for point selection highlighting.
161  std::vector<GfVec4f> _selectedPointColors;
162 };
163 
165 
166 #endif //PXR_IMAGING_HD_SELECTION_H
HD_API PrimSelectionState const * GetPrimSelectionState(HighlightMode const &mode, SdfPath const &renderIndexPath) const
-------------------------— Query API ------------------------------—
virtual HD_API ~HdSelection()
#define HD_API
Definition: api.h:40
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::vector< GfVec4f > _selectedPointColors
Definition: selection.h:161
HD_API SdfPathVector GetSelectedPrimPaths(HighlightMode const &mode) const
std::vector< VtIntArray > elementIndices
Definition: selection.h:112
_PrimSelectionStateMap _selMap[HighlightModeCount]
Definition: selection.h:158
HD_API std::vector< GfVec4f > const & GetSelectedPointColors() const
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:55
Definition: path.h:288
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:209
HdSelection()=default
GLenum mode
Definition: glcorearb.h:98
HD_API SdfPathVector GetAllSelectedPrimPaths() const
std::vector< VtIntArray > pointIndices
Definition: selection.h:114
Definition: vec4f.h:63
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
HD_API void AddRprim(HighlightMode const &mode, SdfPath const &renderIndexPath)
---------------------— Population API -----------------------------—
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
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:115
std::shared_ptr< class HdSelection > HdSelectionSharedPtr
Definition: selection.h:40
std::unordered_map< SdfPath, PrimSelectionState, SdfPath::Hash > _PrimSelectionStateMap
Definition: selection.h:156
HD_API bool IsEmpty() const
std::vector< VtIntArray > edgeIndices
Definition: selection.h:113
GLenum const void * paths
Definition: glew.h:13872
std::vector< VtIntArray > instanceIndices
Definition: selection.h:111