00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined(INCLUDED_SELECTABLE_H)
00023 #define INCLUDED_SELECTABLE_H
00024
00025 #include <cstddef>
00026
00027 #include "math/Vector3.h"
00028 #include "scenelib.h"
00029 #include "generic/callbackfwd.h"
00030
00031 class SelectionIntersection
00032 {
00033 float m_depth;
00034 float m_distance;
00035 public:
00036 SelectionIntersection () :
00037 m_depth(1), m_distance(2)
00038 {
00039 }
00040 SelectionIntersection (float depth, float distance) :
00041 m_depth(depth), m_distance(distance)
00042 {
00043 }
00044 bool operator< (const SelectionIntersection& other) const
00045 {
00046 if (m_distance != other.m_distance) {
00047 return m_distance < other.m_distance;
00048 }
00049 if (m_depth != other.m_depth) {
00050 return m_depth < other.m_depth;
00051 }
00052 return false;
00053 }
00054 bool equalEpsilon (const SelectionIntersection& other, float distanceEpsilon, float depthEpsilon) const
00055 {
00056 return float_equal_epsilon(m_distance, other.m_distance, distanceEpsilon) && float_equal_epsilon(m_depth,
00057 other.m_depth, depthEpsilon);
00058 }
00059 float depth () const
00060 {
00061 return m_depth;
00062 }
00063 bool valid () const
00064 {
00065 return depth() < 1;
00066 }
00067 };
00068
00069
00070 inline bool SelectionIntersection_closer (const SelectionIntersection& self, const SelectionIntersection& other)
00071 {
00072 return self < other;
00073 }
00074
00075
00076 inline void assign_if_closer (SelectionIntersection& best, const SelectionIntersection& other)
00077 {
00078 if (SelectionIntersection_closer(other, best)) {
00079 best = other;
00080 }
00081 }
00082
00083 class VertexPointer
00084 {
00085 typedef const unsigned char* byte_pointer;
00086 public:
00087 typedef float elem_type;
00088 typedef const elem_type* pointer;
00089 typedef const elem_type& reference;
00090
00091 class iterator
00092 {
00093 public:
00094 iterator ()
00095 {
00096 }
00097 iterator (byte_pointer vertices, std::size_t stride) :
00098 m_iter(vertices), m_stride(stride)
00099 {
00100 }
00101
00102 bool operator== (const iterator& other) const
00103 {
00104 return m_iter == other.m_iter;
00105 }
00106 bool operator!= (const iterator& other) const
00107 {
00108 return !operator==(other);
00109 }
00110
00111 iterator operator+ (std::size_t i)
00112 {
00113 return iterator(m_iter + i * m_stride, m_stride);
00114 }
00115 iterator operator+= (std::size_t i)
00116 {
00117 m_iter += i * m_stride;
00118 return *this;
00119 }
00120 iterator& operator++ ()
00121 {
00122 m_iter += m_stride;
00123 return *this;
00124 }
00125 iterator operator++ (int)
00126 {
00127 iterator tmp = *this;
00128 m_iter += m_stride;
00129 return tmp;
00130 }
00131 reference operator* () const
00132 {
00133 return *reinterpret_cast<pointer> (m_iter);
00134 }
00135 private:
00136 byte_pointer m_iter;
00137 std::size_t m_stride;
00138 };
00139
00140 VertexPointer (pointer vertices, std::size_t stride) :
00141 m_vertices(reinterpret_cast<byte_pointer> (vertices)), m_stride(stride)
00142 {
00143 }
00144
00145 iterator begin () const
00146 {
00147 return iterator(m_vertices, m_stride);
00148 }
00149
00150 reference operator[] (std::size_t i) const
00151 {
00152 return *reinterpret_cast<pointer> (m_vertices + m_stride * i);
00153 }
00154
00155 private:
00156 byte_pointer m_vertices;
00157 std::size_t m_stride;
00158 };
00159
00160 class IndexPointer
00161 {
00162 public:
00163 typedef unsigned int index_type;
00164 typedef const index_type* pointer;
00165
00166 class iterator
00167 {
00168 public:
00169 iterator (pointer iter) :
00170 m_iter(iter)
00171 {
00172 }
00173
00174 bool operator== (const iterator& other) const
00175 {
00176 return m_iter == other.m_iter;
00177 }
00178 bool operator!= (const iterator& other) const
00179 {
00180 return !operator==(other);
00181 }
00182
00183 iterator operator+ (std::size_t i)
00184 {
00185 return m_iter + i;
00186 }
00187 iterator operator+= (std::size_t i)
00188 {
00189 return m_iter += i;
00190 }
00191 iterator operator++ ()
00192 {
00193 return ++m_iter;
00194 }
00195 iterator operator++ (int)
00196 {
00197 return m_iter++;
00198 }
00199 const index_type& operator* () const
00200 {
00201 return *m_iter;
00202 }
00203 private:
00204 void increment ()
00205 {
00206 ++m_iter;
00207 }
00208 pointer m_iter;
00209 };
00210
00211 IndexPointer (pointer indices, std::size_t count) :
00212 m_indices(indices), m_finish(indices + count)
00213 {
00214 }
00215
00216 iterator begin () const
00217 {
00218 return m_indices;
00219 }
00220 iterator end () const
00221 {
00222 return m_finish;
00223 }
00224
00225 private:
00226 pointer m_indices;
00227 pointer m_finish;
00228 };
00229
00230 template<typename Element> class BasicVector3;
00231 typedef BasicVector3<float> Vector3;
00232 class Matrix4;
00233 class VolumeTest;
00234
00235 class SelectionTest
00236 {
00237 public:
00238 virtual ~SelectionTest ()
00239 {
00240 }
00241 virtual void BeginMesh (const Matrix4& localToWorld, bool twoSided = false) = 0;
00242 virtual const VolumeTest& getVolume () const = 0;
00243 virtual const Vector3& getNear () const = 0;
00244 virtual const Vector3& getFar () const = 0;
00245 virtual void TestPoint (const Vector3& point, SelectionIntersection& best) = 0;
00246 virtual void TestPolygon (const VertexPointer& vertices, std::size_t count, SelectionIntersection& best) = 0;
00247 virtual void TestLineLoop (const VertexPointer& vertices, std::size_t count, SelectionIntersection& best) = 0;
00248 virtual void TestLineStrip (const VertexPointer& vertices, std::size_t count, SelectionIntersection& best) = 0;
00249 virtual void TestLines (const VertexPointer& vertices, std::size_t count, SelectionIntersection& best) = 0;
00250 virtual void TestTriangles (const VertexPointer& vertices, const IndexPointer& indices,
00251 SelectionIntersection& best) = 0;
00252 virtual void
00253 TestQuads (const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best) = 0;
00254 virtual void TestQuadStrip (const VertexPointer& vertices, const IndexPointer& indices,
00255 SelectionIntersection& best) = 0;
00256 };
00257
00258 class Selectable;
00259
00260 class Selector
00261 {
00262 public:
00263 virtual ~Selector ()
00264 {
00265 }
00266 virtual void pushSelectable (Selectable& selectable) = 0;
00267 virtual void popSelectable () = 0;
00268 virtual void addIntersection (const SelectionIntersection& intersection) = 0;
00269 };
00270
00271 inline void Selector_add (Selector& selector, Selectable& selectable)
00272 {
00273 selector.pushSelectable(selectable);
00274 selector.addIntersection(SelectionIntersection(0, 0));
00275 selector.popSelectable();
00276 }
00277
00278 inline void Selector_add (Selector& selector, Selectable& selectable, const SelectionIntersection& intersection)
00279 {
00280 selector.pushSelectable(selectable);
00281 selector.addIntersection(intersection);
00282 selector.popSelectable();
00283 }
00284
00285 class VolumeTest;
00286 class SelectionTestable
00287 {
00288 public:
00289 STRING_CONSTANT(Name, "SelectionTestable");
00290
00291 virtual ~SelectionTestable ()
00292 {
00293 }
00294 virtual void testSelect (Selector& selector, SelectionTest& test) = 0;
00295 };
00296
00297 inline SelectionTestable* Instance_getSelectionTestable (scene::Instance& instance)
00298 {
00299 return dynamic_cast<SelectionTestable*> (&instance);
00300 }
00301
00302 class Plane3;
00303 typedef Callback1<const Plane3&> PlaneCallback;
00304
00305 class SelectedPlanes
00306 {
00307 public:
00308 virtual ~SelectedPlanes ()
00309 {
00310 }
00311 virtual bool contains (const Plane3& plane) const = 0;
00312 };
00313
00314 class PlaneSelectable
00315 {
00316 public:
00317 STRING_CONSTANT(Name, "PlaneSelectable");
00318
00319 virtual ~PlaneSelectable ()
00320 {
00321 }
00322 virtual void
00323 selectPlanes (Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback) = 0;
00324 virtual void selectReversedPlanes (Selector& selector, const SelectedPlanes& selectedPlanes) = 0;
00325 };
00326
00327 #endif