| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <cstddef> |
| 4 |
|
| 5 |
#include "math/FloatTools.h" |
| 6 |
#include "inode.h" |
| 7 |
#include "iselection.h" |
| 8 |
#include <memory> |
| 9 |
#include <functional> |
| 10 |
|
| 11 |
class SelectionIntersection |
| 12 |
{ |
| 13 |
private: |
| 14 |
float _depth; |
| 15 |
float _distance; |
| 16 |
public: |
| 17 |
SelectionIntersection() : |
| 18 |
_depth(1), |
| 19 |
_distance(2) |
| 20 |
{} |
| 21 |
|
| 22 |
SelectionIntersection(float depth, float distance) : |
| 23 |
_depth(depth), |
| 24 |
_distance(distance) |
| 25 |
{} |
| 26 |
|
| 27 |
bool operator<(const SelectionIntersection& other) const |
| 28 |
{ |
| 29 |
if (_distance != other._distance) |
| 30 |
{ |
| 31 |
return _distance < other._distance; |
| 32 |
} |
| 33 |
|
| 34 |
if (_depth != other._depth) |
| 35 |
{ |
| 36 |
return _depth < other._depth; |
| 37 |
} |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
bool equalEpsilon(const SelectionIntersection& other, float distanceEpsilon, float depthEpsilon) const |
| 43 |
{ |
| 44 |
return float_equal_epsilon(_distance, other._distance, distanceEpsilon) && |
| 45 |
float_equal_epsilon(_depth, other._depth, depthEpsilon); |
| 46 |
} |
| 47 |
|
| 48 |
float depth() const |
| 49 |
{ |
| 50 |
return _depth; |
| 51 |
} |
| 52 |
|
| 53 |
bool isValid() const |
| 54 |
{ |
| 55 |
return depth() < 1; |
| 56 |
} |
| 57 |
|
| 58 |
// returns true if self is closer than other |
| 59 |
bool isCloserThan(const SelectionIntersection& other) const |
| 60 |
{ |
| 61 |
return *this < other; |
| 62 |
} |
| 63 |
|
| 64 |
// assigns other to *this if other is closer than *this |
| 65 |
void assignIfCloser(const SelectionIntersection& other) |
| 66 |
{ |
| 67 |
if (other.isCloserThan(*this)) |
| 68 |
{ |
| 69 |
*this = other; |
| 70 |
} |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
/** |
| 75 |
* greebo: A helper class allowing to traverse a sequence of Vector3 |
| 76 |
* objects in memory. The Vector3 objects can have a certain distance |
| 77 |
* (stride) which is passed to the constructor. Incrementing the contained |
| 78 |
* iterator object moves from one Vector3 to the next in memory. |
| 79 |
*/ |
| 80 |
class VertexPointer |
| 81 |
{ |
| 82 |
typedef const unsigned char* byte_pointer; |
| 83 |
public: |
| 84 |
typedef const Vector3* vector_pointer; |
| 85 |
typedef const Vector3& vector_reference; |
| 86 |
|
| 87 |
class iterator |
| 88 |
{ |
| 89 |
public: |
| 90 |
iterator() {} |
| 91 |
iterator(byte_pointer vertices, std::size_t stride) |
| 92 |
: m_iter(vertices), m_stride(stride) {} |
| 93 |
|
| 94 |
bool operator==(const iterator& other) const |
| 95 |
{ |
| 96 |
return m_iter == other.m_iter; |
| 97 |
} |
| 98 |
bool operator!=(const iterator& other) const |
| 99 |
{ |
| 100 |
return !operator==(other); |
| 101 |
} |
| 102 |
|
| 103 |
iterator operator+(std::size_t i) |
| 104 |
{ |
| 105 |
return iterator(m_iter + i * m_stride, m_stride); |
| 106 |
} |
| 107 |
iterator operator+=(std::size_t i) |
| 108 |
{ |
| 109 |
m_iter += i * m_stride; |
| 110 |
return *this; |
| 111 |
} |
| 112 |
iterator& operator++() |
| 113 |
{ |
| 114 |
m_iter += m_stride; |
| 115 |
return *this; |
| 116 |
} |
| 117 |
iterator operator++(int) |
| 118 |
{ |
| 119 |
iterator tmp = *this; |
| 120 |
m_iter += m_stride; |
| 121 |
return tmp; |
| 122 |
} |
| 123 |
vector_reference operator*() const |
| 124 |
{ |
| 125 |
return *reinterpret_cast<vector_pointer>(m_iter); |
| 126 |
} |
| 127 |
private: |
| 128 |
byte_pointer m_iter; |
| 129 |
std::size_t m_stride; |
| 130 |
}; |
| 131 |
|
| 132 |
VertexPointer(vector_pointer vertices, std::size_t stride) |
| 133 |
: m_vertices(reinterpret_cast<byte_pointer>(vertices)), m_stride(stride) {} |
| 134 |
|
| 135 |
iterator begin() const |
| 136 |
{ |
| 137 |
return iterator(m_vertices, m_stride); |
| 138 |
} |
| 139 |
|
| 140 |
vector_reference operator[](std::size_t i) const |
| 141 |
{ |
| 142 |
return *reinterpret_cast<vector_pointer>(m_vertices + m_stride*i); |
| 143 |
} |
| 144 |
|
| 145 |
private: |
| 146 |
// The address of the first Vector3 object |
| 147 |
byte_pointer m_vertices; |
| 148 |
|
| 149 |
// The distance (in bytes) to the next object in memory |
| 150 |
std::size_t m_stride; |
| 151 |
}; |
| 152 |
|
| 153 |
class IndexPointer |
| 154 |
{ |
| 155 |
public: |
| 156 |
typedef unsigned int index_type; |
| 157 |
typedef const index_type* pointer; |
| 158 |
|
| 159 |
class iterator |
| 160 |
{ |
| 161 |
public: |
| 162 |
iterator(pointer iter) : m_iter(iter) {} |
| 163 |
|
| 164 |
bool operator==(const iterator& other) const |
| 165 |
{ |
| 166 |
return m_iter == other.m_iter; |
| 167 |
} |
| 168 |
bool operator!=(const iterator& other) const |
| 169 |
{ |
| 170 |
return !operator==(other); |
| 171 |
} |
| 172 |
|
| 173 |
iterator operator+(std::size_t i) |
| 174 |
{ |
| 175 |
return m_iter + i; |
| 176 |
} |
| 177 |
iterator operator+=(std::size_t i) |
| 178 |
{ |
| 179 |
return m_iter += i; |
| 180 |
} |
| 181 |
iterator operator++() |
| 182 |
{ |
| 183 |
return ++m_iter; |
| 184 |
} |
| 185 |
iterator operator++(int) |
| 186 |
{ |
| 187 |
return m_iter++; |
| 188 |
} |
| 189 |
const index_type& operator*() const |
| 190 |
{ |
| 191 |
return *m_iter; |
| 192 |
} |
| 193 |
private: |
| 194 |
void increment() |
| 195 |
{ |
| 196 |
++m_iter; |
| 197 |
} |
| 198 |
pointer m_iter; |
| 199 |
}; |
| 200 |
|
| 201 |
IndexPointer(pointer indices, std::size_t count) |
| 202 |
: m_indices(indices), m_finish(indices + count) {} |
| 203 |
|
| 204 |
iterator begin() const |
| 205 |
{ |
| 206 |
return m_indices; |
| 207 |
} |
| 208 |
iterator end() const |
| 209 |
{ |
| 210 |
return m_finish; |
| 211 |
} |
| 212 |
|
| 213 |
private: |
| 214 |
pointer m_indices; |
| 215 |
pointer m_finish; |
| 216 |
}; |
| 217 |
|
| 218 |
template<typename Element> class BasicVector3; |
| 219 |
typedef BasicVector3<double> Vector3; |
| 220 |
class Matrix4; |
| 221 |
class VolumeTest; |
| 222 |
|
| 223 |
class SelectionTest |
| 224 |
{ |
| 225 |
public: |
| 226 |
virtual ~SelectionTest() {} |
| 227 |
virtual void BeginMesh(const Matrix4& localToWorld, bool twoSided = false) = 0; |
| 228 |
virtual const VolumeTest& getVolume() const = 0; |
| 229 |
virtual const Vector3& getNear() const = 0; |
| 230 |
virtual const Vector3& getFar() const = 0; |
| 231 |
virtual void TestPoint(const Vector3& point, SelectionIntersection& best) = 0; |
| 232 |
virtual void TestPolygon(const VertexPointer& vertices, std::size_t count, SelectionIntersection& best) = 0; |
| 233 |
virtual void TestLineStrip(const VertexPointer& vertices, std::size_t count, SelectionIntersection& best) = 0; |
| 234 |
virtual void TestTriangles(const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best) = 0; |
| 235 |
virtual void TestQuads(const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best) = 0; |
| 236 |
virtual void TestQuadStrip(const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best) = 0; |
| 237 |
}; |
| 238 |
typedef std::shared_ptr<SelectionTest> SelectionTestPtr; |
| 239 |
|
| 240 |
/** |
| 241 |
* @brief Abstract interface for an object which collects possibly-selected |
| 242 |
* objects, keeping track of the best intersection which typically determines |
| 243 |
* which candidate object should be selected when more than one is possible. |
| 244 |
* |
| 245 |
* This object exposes a stateful interface. A call to pushSelectable() sets the |
| 246 |
* given ISelectable object as the "current" selectable. Subsequent calls to |
| 247 |
* addIntersection() apply to this current selectable, replacing the current |
| 248 |
* intersection if the newly-submitted one is better. The operation is completed |
| 249 |
* when popSelectable() is called, and the current selectable along with its |
| 250 |
* best intersection is added to the internal list(s). |
| 251 |
* |
| 252 |
* Despite the usage of "push" and "pop" terminology, there is only one current |
| 253 |
* selectable, and no internal stack. |
| 254 |
*/ |
| 255 |
class Selector |
| 256 |
{ |
| 257 |
public: |
| 258 |
virtual ~Selector() {} |
| 259 |
|
| 260 |
/// Set the given object as the current selectable |
| 261 |
virtual void pushSelectable(ISelectable& selectable) = 0; |
| 262 |
|
| 263 |
/// Commit the current selectable, storing it along with its best intersection |
| 264 |
virtual void popSelectable() = 0; |
| 265 |
|
| 266 |
/** |
| 267 |
* @brief Add a candidate intersection for the current selectable. |
| 268 |
* |
| 269 |
* The candidate intersection is only stored if it is a better fit than the |
| 270 |
* best intersection seen so far. |
| 271 |
*/ |
| 272 |
virtual void addIntersection(const SelectionIntersection& intersection) = 0; |
| 273 |
|
| 274 |
// Returns true if no selectable has been chosen |
| 275 |
virtual bool empty() const = 0; |
| 276 |
|
| 277 |
// Iterate over every selectable in the pool |
| 278 |
virtual void foreachSelectable(const std::function<void(ISelectable*)>& functor) = 0; |
| 279 |
|
| 280 |
/// Add a selectable object and immediately commit it with a null intersection |
| 281 |
void addWithNullIntersection(ISelectable& selectable) |
| 282 |
{ |
| 283 |
pushSelectable(selectable); |
| 284 |
addIntersection(SelectionIntersection(0, 0)); |
| 285 |
popSelectable(); |
| 286 |
} |
| 287 |
|
| 288 |
/// Add a selectable object and immediately commit it with the given intersection |
| 289 |
void addWithIntersection(ISelectable& selectable, const SelectionIntersection& intersection) |
| 290 |
{ |
| 291 |
pushSelectable(selectable); |
| 292 |
addIntersection(intersection); |
| 293 |
popSelectable(); |
| 294 |
} |
| 295 |
}; |
| 296 |
|
| 297 |
class VolumeTest; |
| 298 |
class SelectionTestable |
| 299 |
{ |
| 300 |
public: |
| 301 |
virtual ~SelectionTestable() {} |
| 302 |
virtual void testSelect(Selector& selector, SelectionTest& test) = 0; |
| 303 |
}; |
| 304 |
typedef std::shared_ptr<SelectionTestable> SelectionTestablePtr; |
| 305 |
|
| 306 |
inline SelectionTestablePtr Node_getSelectionTestable(const scene::INodePtr& node) { |
| 307 |
return std::dynamic_pointer_cast<SelectionTestable>(node); |
| 308 |
} |
| 309 |
|
| 310 |
class ComponentSelectionTestable { |
| 311 |
public: |
| 312 |
virtual ~ComponentSelectionTestable() {} |
| 313 |
virtual bool isSelectedComponents() const = 0; |
| 314 |
virtual void setSelectedComponents(bool select, selection::ComponentSelectionMode mode) = 0; |
| 315 |
virtual void invertSelectedComponents(selection::ComponentSelectionMode mode) = 0; |
| 316 |
virtual void testSelectComponents(Selector& selector, SelectionTest& test, selection::ComponentSelectionMode mode) = 0; |
| 317 |
}; |
| 318 |
typedef std::shared_ptr<ComponentSelectionTestable> ComponentSelectionTestablePtr; |
| 319 |
|
| 320 |
inline ComponentSelectionTestablePtr Node_getComponentSelectionTestable(const scene::INodePtr& node) { |
| 321 |
return std::dynamic_pointer_cast<ComponentSelectionTestable>(node); |
| 322 |
} |
| 323 |
|
| 324 |
class Plane3; |
| 325 |
typedef std::function<void (const Plane3&)> PlaneCallback; |
| 326 |
|
| 327 |
class SelectedPlanes |
| 328 |
{ |
| 329 |
public: |
| 330 |
virtual ~SelectedPlanes() {} |
| 331 |
virtual bool contains(const Plane3& plane) const = 0; |
| 332 |
}; |
| 333 |
|
| 334 |
class PlaneSelectable |
| 335 |
{ |
| 336 |
public: |
| 337 |
virtual ~PlaneSelectable() {} |
| 338 |
virtual void selectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback) = 0; |
| 339 |
virtual void selectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes) = 0; |
| 340 |
}; |
| 341 |
typedef std::shared_ptr<PlaneSelectable> PlaneSelectablePtr; |
| 342 |
|
| 343 |
inline PlaneSelectablePtr Node_getPlaneSelectable(const scene::INodePtr& node) { |
| 344 |
return std::dynamic_pointer_cast<PlaneSelectable>(node); |
| 345 |
} |
| 346 |
|
| 347 |
namespace selection |
| 348 |
{ |
| 349 |
|
| 350 |
/** |
| 351 |
* Interface of a scene walker that can be used to test |
| 352 |
* some or all nodes of the scene for selection. |
| 353 |
* |
| 354 |
* All qualified scene nodes are tested for selection and |
| 355 |
* stored internally. Use the foreachSelectable() method |
| 356 |
* to iterate over the selectables. |
| 357 |
*/ |
| 358 |
class ISceneSelectionTester |
| 359 |
{ |
| 360 |
public: |
| 361 |
using Ptr = std::shared_ptr<ISceneSelectionTester>; |
| 362 |
|
| 363 |
virtual ~ISceneSelectionTester() {} |
| 364 |
|
| 365 |
// Tests all qualified nodes in the scene for selection |
| 366 |
// and stores the ones passing the test internally |
| 367 |
virtual void testSelectScene(const VolumeTest& view, SelectionTest& test) = 0; |
| 368 |
|
| 369 |
// Tests all qualified nodes in the scene for selection |
| 370 |
// all passing selectables are stored internally if they pass the given predicate |
| 371 |
virtual void testSelectSceneWithFilter(const VolumeTest& view, SelectionTest& test, |
| 372 |
const std::function<bool(ISelectable*)>& predicate) = 0; |
| 373 |
|
| 374 |
// Returns true if the tester found one or more selectables passing the test |
| 375 |
virtual bool hasSelectables() const = 0; |
| 376 |
|
| 377 |
// Iterates over the selectables that were passing the test |
| 378 |
virtual void foreachSelectable(const std::function<void(ISelectable*)>& functor) = 0; |
| 379 |
}; |
| 380 |
|
| 381 |
/** |
| 382 |
* Factory interface used to acquire ISceneSelectionTester |
| 383 |
* instances suitable for a given purpose. |
| 384 |
*/ |
| 385 |
class ISceneSelectionTesterFactory |
| 386 |
{ |
| 387 |
public: |
| 388 |
virtual ~ISceneSelectionTesterFactory() {} |
| 389 |
|
| 390 |
/** |
| 391 |
* Returns an instance of a selection tester suitable for testing |
| 392 |
* scene nodes according to the given purpose/selection mode. |
| 393 |
*/ |
| 394 |
virtual ISceneSelectionTester::Ptr createSceneSelectionTester(SelectionMode mode) = 0; |
| 395 |
}; |
| 396 |
|
| 397 |
} // namespace |