Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
RayIntersector.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef RayIntersector_INCLUDE_ONCE
33 #define RayIntersector_INCLUDE_ONCE
34 
35 #include <vlCore/Ray.hpp>
36 #include <vlGraphics/Actor.hpp>
37 #include <vlGraphics/Geometry.hpp>
38 #include <vlCore/Vector4.hpp>
39 #include <vlCore/Matrix4.hpp>
40 #include <vlGraphics/Frustum.hpp>
41 
42 namespace vl
43 {
44  class SceneManager;
45  //-----------------------------------------------------------------------------
46  // RayIntersection
47  //-----------------------------------------------------------------------------
50  class RayIntersection: public Object
51  {
53 
54  public:
56  {
57  VL_DEBUG_SET_OBJECT_NAME()
58  }
59 
61  void setActor(Actor* a) { mActor = a; }
63  const Actor* actor() const { return mActor; }
65  Actor* actor() { return mActor; }
66 
68  const vec3& intersectionPoint() const { return mIntersectionPoint; }
71 
73  real distance() const { return mDistance; }
75  void setDistance(real dist) { mDistance = dist; }
76 
77  protected:
80  real mDistance;
81  };
82  //-----------------------------------------------------------------------------
83  // RayIntersectionGeometry
84  //-----------------------------------------------------------------------------
88  {
90 
91  public:
92  RayIntersectionGeometry(): mGeometry(NULL), mDrawCalls(NULL), mTriangleIndex(-1)
93  {
94  VL_DEBUG_SET_OBJECT_NAME()
95  memset(mTriangle, 0xFF, sizeof(mTriangle));
96  }
97 
99  Geometry* geometry() { return mGeometry; }
101  const Geometry* geometry() const { return mGeometry; }
103  DrawCall* drawCalls() { return mDrawCalls; }
105  const DrawCall* drawCalls() const { return mDrawCalls; }
107  int triangleIndex() const { return mTriangleIndex; }
109  const int* triangle() const { return mTriangle; }
110 
112  void setGeometry(Geometry* g) { mGeometry = g; }
114  void setPrimitives(DrawCall* p) { mDrawCalls = p; }
116  void setTriangleIndex(int t_idx) { mTriangleIndex = t_idx; }
118  void setTriangle(int a, int b, int c) { mTriangle[0] = a; mTriangle[1] = b; mTriangle[2] = c; }
119 
120  protected:
125  int mTriangle[3];
126  float mDistance;
127  };
128  //-----------------------------------------------------------------------------
129  // RayIntersector
130  //-----------------------------------------------------------------------------
134  {
136 
137  public:
139  {
140  VL_DEBUG_SET_OBJECT_NAME()
141  mActors = new ActorCollection;
142  }
143 
145  const ActorCollection* actors() const { return mActors.get(); }
147  ActorCollection* actors() { return mActors.get(); }
148 
150  const Ray& ray() const { return mRay; }
152  void setRay(const Ray& ray) { mRay = ray; }
153 
155  const Frustum& frustum() const { return mFrustum; }
157  void setFrustum(const Frustum& frustum) { mFrustum = frustum; }
158 
160  const std::vector< ref<RayIntersection> >& intersections() const { return mIntersections; }
161 
166  void intersect();
167 
177  void intersect(const Ray& ray, SceneManager* scene_manager);
178 
179  protected:
180  static bool sorter(const ref<RayIntersection>& a, const ref<RayIntersection>& b) { return a->distance() < b->distance(); }
181 
182  void intersect(Actor* act);
183  void intersectGeometry(Actor* act, Geometry* geom);
184 
185  // T should be either fvec3-4 or dvec3-4
186  template<class T>
187  void intersectTriangle(const T& a, const T& b, const T& c, int ia, int ib, int ic, Actor*, Geometry* geom, DrawCall* prim, int prim_idx);
188 
189  protected:
191  std::vector< ref<RayIntersection> > mIntersections;
194  };
195 }
196 
197 #endif
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
const int * triangle() const
An int[3] representing the indices of the intersected triangle.
DrawCall * drawCalls()
The intersected DrawCall.
The Ray class defines a ray as an origin and direction using real precision.
Definition: Ray.hpp:41
A set of planes defining a frustum used for culling purposes (frustum culling).
Definition: Frustum.hpp:49
void setDistance(real dist)
The intersection distance.
int triangleIndex() const
The starting index of the intersected primitive inside drawCalls()
static bool sorter(const ref< RayIntersection > &a, const ref< RayIntersection > &b)
const vec3 & intersectionPoint() const
The intersection point, in world coordinates.
const std::vector< ref< RayIntersection > > & intersections() const
The intersection points detected by the last intersect() call sorted according to their distance (the...
const DrawCall * drawCalls() const
The intersected DrawCall.
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
void setTriangleIndex(int t_idx)
The starting index of the intersected primitive inside drawCalls()
The RayIntersection encapsulates all the information relative to a Ray/Actor intersection.
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
Definition: Geometry.hpp:66
Actor * actor()
The intersected Actor.
Visualization Library main namespace.
void setActor(Actor *a)
The intersected Actor.
void setPrimitives(DrawCall *p)
The intersected DrawCall.
void setRay(const Ray &ray)
The ray in world coordinates to be intersected with the actors()
The base class for all the reference counted objects.
Definition: Object.hpp:158
The RayIntersectionGeometry encapsulates all the information relative to a Ray/Actor intersection...
Geometry * geometry()
The intersected Geometry.
void setFrustum(const Frustum &frustum)
The frustum in world coordinates used to cull the objects.
The SceneManager class is the base class for all the scene managers.
const Frustum & frustum() const
The frustum in world coordinates used to cull the objects.
const Ray & ray() const
The ray in world coordinates to be intersected with the actors()
#define NULL
Definition: OpenGLDefs.hpp:81
void setIntersectionPoint(const vec3 &v)
The intersection point, in world coordinates.
std::vector< ref< RayIntersection > > mIntersections
ActorCollection * actors()
The Actors against which the intersection test is performed.
Defined as a simple subclass of Collection<Actor>, see Collection for more information.
Definition: Actor.hpp:479
The base class of DrawArrays, DrawElements, MultiDrawElements and DrawRangeElements.
Definition: DrawCall.hpp:90
const Geometry * geometry() const
The intersected Geometry.
void setTriangle(int a, int b, int c)
An int[3] representing the indices of the intersected triangle.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
ref< ActorCollection > mActors
const ActorCollection * actors() const
The Actors against which the intersection test is performed.
const Actor * actor() const
The intersected Actor.
real distance() const
The intersection distance.
The RayIntersector class is used to detect the intersection points between a Ray and a set of Actor[s...
void setGeometry(Geometry *g)
The intersected Geometry.