Ignition Rendering

API Reference

0.1.0
BaseRayQuery.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #ifndef IGNITION_RENDERING_BASE_BASERAYQUERY_HH_
18 #define IGNITION_RENDERING_BASE_BASERAYQUERY_HH_
19 
20 #include <ignition/math/Matrix4.hh>
21 #include <ignition/math/Vector3.hh>
22 
25 
26 namespace ignition
27 {
28  namespace rendering
29  {
33  template <class T>
34  class IGNITION_RENDERING_VISIBLE BaseRayQuery :
35  public virtual RayQuery,
36  public T
37  {
39  protected: BaseRayQuery();
40 
42  public: virtual ~BaseRayQuery() override;
43 
44  // Documentation inherited
45  public: virtual void SetOrigin(const math::Vector3d &_origin) override;
46 
47  // Documentation inherited
48  public: virtual math::Vector3d Origin() const override;
49 
50  // Documentation inherited
51  public: virtual void SetDirection(const math::Vector3d &_dir) override;
52 
53  // Documentation inherited
54  public: virtual math::Vector3d Direction() const override;
55 
56  // Documentation inherited
57  public: virtual void SetFromCamera(const CameraPtr &_camera,
58  const math::Vector2d &_coord) override;
59 
60  // Documentation inherited
61  public: virtual RayQueryResult ClosestPoint() override;
62 
64  protected: math::Vector3d origin;
65 
67  protected: math::Vector3d direction;
68  };
69 
71  template <class T>
73  {
74  }
75 
77  template <class T>
79  {
80  }
81 
83  template <class T>
84  void BaseRayQuery<T>::SetOrigin(const math::Vector3d &_origin)
85  {
86  this->origin = _origin;
87  }
88 
90  template <class T>
91  ignition::math::Vector3d BaseRayQuery<T>::Origin() const
92  {
93  return this->origin;
94  }
95 
97  template <class T>
98  void BaseRayQuery<T>::SetDirection(const math::Vector3d &_dir)
99  {
100  this->direction = _dir;
101  }
102 
104  template <class T>
105  ignition::math::Vector3d BaseRayQuery<T>::Direction() const
106  {
107  return this->direction;
108  }
109 
111  template <class T>
113  const ignition::math::Vector2d &_coord)
114  {
115  math::Matrix4d projectionMatrix = _camera->ProjectionMatrix();
116  math::Matrix4d viewMatrix = _camera->ViewMatrix();
117  math::Vector3d start(_coord.X(), _coord.Y(), -1.0);
118  math::Vector3d end(_coord.X(), _coord.Y(), 0.0);
119  math::Matrix4d viewProjInv = (projectionMatrix * viewMatrix).Inverse();
120 
121  // rotate start and end
122  // ign math does not support matrix4 * vec4
123  // so calc homogeneous coordinate w ourselves
124  double startw = viewProjInv(3, 0) * start[0] +
125  viewProjInv(3, 1) * start[1] +
126  viewProjInv(3, 2) * start[2] + viewProjInv(3, 3);
127  double endw = viewProjInv(3, 0) * end[0] +
128  viewProjInv(3, 1) * end[1] +
129  viewProjInv(3, 2) * end[2] + viewProjInv(3, 3);
130  start = viewProjInv * start;
131  end = viewProjInv * end;
132  // normalize
133  start = start / startw;
134  end = end / endw;
135  math::Vector3d dir = (end - start).Normalize();
136 
137  this->origin = start;
138  this->direction = dir;
139  }
140 
142  template <class T>
144  {
145  // TODO implement a generic ray query here?
147  result.distance = -1;
148  return result;
149  }
150 
151  }
152 }
153 #endif
virtual RayQueryResult ClosestPoint() override
Compute intersections.
Definition: BaseRayQuery.hh:143
virtual void SetDirection(const math::Vector3d &_dir) override
Set ray direction.
Definition: BaseRayQuery.hh:98
A Ray Query class used for computing ray object intersections.
Definition: BaseRayQuery.hh:34
double distance
Intersection distance.
Definition: RayQuery.hh:34
math::Vector3d direction
Ray direction.
Definition: BaseRayQuery.hh:67
A Ray Query class used for computing ray object intersections.
Definition: RayQuery.hh:51
A class that stores ray query intersection results.
Definition: RayQuery.hh:31
virtual math::Vector3d Direction() const override
Get ray direction.
Definition: BaseRayQuery.hh:105
math::Vector3d origin
Ray origin.
Definition: BaseRayQuery.hh:64
bool result
virtual math::Vector3d Origin() const override
Get ray origin.
Definition: BaseRayQuery.hh:91
virtual ~BaseRayQuery() override
Destructor.
Definition: BaseRayQuery.hh:78
virtual void SetOrigin(const math::Vector3d &_origin) override
Set ray origin.
Definition: BaseRayQuery.hh:84
Definition: ArrowVisual.hh:22
virtual void SetFromCamera(const CameraPtr &_camera, const math::Vector2d &_coord) override
Create the ray query from camera.
Definition: BaseRayQuery.hh:112
BaseRayQuery()
Constructor.
Definition: BaseRayQuery.hh:72