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 
53 {
54 public:
57  HighlightModeSelect = 0, // Active selection
58  HighlightModeLocate, // Rollover selection
59 
60  HighlightModeCount
61  };
62 
63  HD_API
64  virtual ~HdSelection();
65 
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.
103  struct PrimSelectionState {
104  PrimSelectionState() : fullySelected(false) {}
105 
106  bool fullySelected;
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 
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
129  SdfPathVector
130  GetAllSelectedPrimPaths() const;
131 
132  // Returns the selected rprim render index paths for the given mode.
133  HD_API
134  SdfPathVector
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
146  HdSelectionSharedPtr Merge(
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.
163  _PrimSelectionStateMap _selMap[HighlightModeCount];
164 
165  // Track all colors used for point selection highlighting.
166  std::vector<GfVec4f> _selectedPointColors;
167 };
168 
169 PXR_NAMESPACE_CLOSE_SCOPE
170 
171 #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:56
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
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
HD_API PrimSelectionState const * GetPrimSelectionState(HighlightMode const &mode, SdfPath const &renderIndexPath) const
-------------------------— Query API ------------------------------—