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 ///
53 {
54 public:
55  /// Selection modes allow differentiation in selection highlight behavior.
57  HighlightModeSelect = 0, // Active selection
58  HighlightModeLocate, // Rollover selection
59 
61  };
62 
63  HD_API
64  virtual ~HdSelection();
65 
66  /// ------------------------ Population API --------------------------------
67  HD_API
68  void AddRprim(HighlightMode const &mode,
69  SdfPath const &renderIndexPath);
70 
71  HD_API
72  void AddInstance(HighlightMode const &mode,
73  SdfPath const &renderIndexPath,
74  VtIntArray const &instanceIndex=VtIntArray());
75 
76  HD_API
77  void AddElements(HighlightMode const &mode,
78  SdfPath const &renderIndexPath,
79  VtIntArray const &elementIndices);
80 
81  HD_API
82  void AddEdges(HighlightMode const &mode,
83  SdfPath const &renderIndexPath,
84  VtIntArray const &edgeIndices);
85 
86  HD_API
87  void AddPoints(HighlightMode const &mode,
88  SdfPath const &renderIndexPath,
89  VtIntArray const &pointIndices);
90 
91  // Special handling for points: we allow a set of selected point indices to
92  // also specify a color to use for highlighting.
93  HD_API
94  void AddPoints(HighlightMode const &mode,
95  SdfPath const &renderIndexPath,
96  VtIntArray const &pointIndices,
97  GfVec4f const &pointColor);
98 
99  // XXX: Ideally, this should be per instance, if we want to support
100  // selection of subprims (faces/edges/points) per instance of an rprim.
101  // By making this per rprim, all selected instances of the rprim will share
102  // the same subprim highlighting.
105 
107  // Use a vector of VtIntArray to avoid copying the indices data.
108  // This way, we support multiple Add<Subprim> operations without
109  // having to consolidate the indices each time.
110  std::vector<VtIntArray> instanceIndices;
111  std::vector<VtIntArray> elementIndices;
112  std::vector<VtIntArray> edgeIndices;
113  std::vector<VtIntArray> pointIndices;
114  std::vector<int> pointColorIndices;
115  };
116 
117  /// ---------------------------- Query API ---------------------------------
118 
119  // Returns a pointer to the selection state for the rprim path if selected.
120  // Returns nullptr otherwise.
121  HD_API
122  PrimSelectionState const *
124  SdfPath const &renderIndexPath) const;
125 
126  // Returns the selected rprim render index paths for all the selection
127  // modes. The vector returned may contain duplicates.
128  HD_API
130  GetAllSelectedPrimPaths() const;
131 
132  // Returns the selected rprim render index paths for the given mode.
133  HD_API
135  GetSelectedPrimPaths(HighlightMode const &mode) const;
136 
137  HD_API
138  std::vector<GfVec4f> const& GetSelectedPointColors() const;
139 
140  // Returns true if nothing is selected.
141  HD_API
142  bool IsEmpty() const;
143 
144  HD_API
145  static
147  HdSelectionSharedPtr const &,
148  HdSelectionSharedPtr const &);
149 
150 private:
151  void _AddPoints(HighlightMode const &mode,
152  SdfPath const &renderIndexPath,
153  VtIntArray const &pointIndices,
154  int pointColorIndex);
155 
156  void _GetSelectionPrimPathsForMode(HighlightMode const &mode,
157  SdfPathVector *paths) const;
158 
159 protected:
160  using _PrimSelectionStateMap =
161  std::unordered_map<SdfPath, PrimSelectionState, SdfPath::Hash>;
162  // Keep track of selection per selection mode.
164 
165  // Track all colors used for point selection highlighting.
166  std::vector<GfVec4f> _selectedPointColors;
167 };
168 
170 
171 #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::unordered_map< SdfPath, PrimSelectionState, SdfPath::Hash > _PrimSelectionStateMap
Definition: selection.h:161
std::vector< GfVec4f > _selectedPointColors
Definition: selection.h:166
HD_API SdfPathVector GetSelectedPrimPaths(HighlightMode const &mode) const
std::vector< VtIntArray > elementIndices
Definition: selection.h:111
_PrimSelectionStateMap _selMap[HighlightModeCount]
Definition: selection.h:163
HD_API std::vector< GfVec4f > const & GetSelectedPointColors() const
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:56
Definition: path.h:290
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:211
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:113
Definition: vec4f.h:62
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
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:114
std::shared_ptr< class HdSelection > HdSelectionSharedPtr
Definition: selection.h:40
HD_API bool IsEmpty() const
std::vector< VtIntArray > edgeIndices
Definition: selection.h:112
std::vector< VtIntArray > instanceIndices
Definition: selection.h:110