All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
selection.h
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 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 using HdSelectionSharedPtr = std::shared_ptr<class HdSelection>;
41 
52 class HdSelection {
53 public:
56  HighlightModeSelect = 0, // Active selection
57  HighlightModeLocate, // Rollover selection
58 
59  HighlightModeCount
60  };
61 
62  HdSelection() = default;
63 
64  HD_API
65  virtual ~HdSelection();
66 
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.
104  struct PrimSelectionState {
105  PrimSelectionState() : fullySelected(false) {}
106 
107  bool fullySelected;
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 
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
130  SdfPathVector
131  GetAllSelectedPrimPaths() const;
132 
133  // Returns the selected rprim render index paths for the given mode.
134  HD_API
135  SdfPathVector
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>
156  _PrimSelectionStateMap;
157  // Keep track of selection per selection mode.
158  _PrimSelectionStateMap _selMap[HighlightModeCount];
159 
160  // Track all colors used for point selection highlighting.
161  std::vector<GfVec4f> _selectedPointColors;
162 };
163 
164 PXR_NAMESPACE_CLOSE_SCOPE
165 
166 #endif //PXR_IMAGING_HD_SELECTION_H
HD_API void AddRprim(HighlightMode const &mode, SdfPath const &renderIndexPath)
---------------------— Population API -----------------------------—
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:55
HD_API PrimSelectionState const * GetPrimSelectionState(HighlightMode const &mode, SdfPath const &renderIndexPath) const
-------------------------— Query API ------------------------------—
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
HdSelection holds a collection of selected items per selection mode.
Definition: selection.h:52