Gazebo Rendering

API Reference

8.2.1
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 GZ_RENDERING_BASE_BASERAYQUERY_HH_
18 #define GZ_RENDERING_BASE_BASERAYQUERY_HH_
19 
20 #include <gz/math/Matrix4.hh>
21 #include <gz/math/Vector3.hh>
22 
23 #include "gz/rendering/RayQuery.hh"
24 #include "gz/rendering/Scene.hh"
25 
26 namespace gz
27 {
28  namespace rendering
29  {
30  inline namespace GZ_RENDERING_VERSION_NAMESPACE {
31  //
35  template <class T>
36  class BaseRayQuery :
37  public virtual RayQuery,
38  public T
39  {
41  protected: BaseRayQuery();
42 
44  public: virtual ~BaseRayQuery() override;
45 
46  // Documentation inherited
47  public: virtual void SetOrigin(const math::Vector3d &_origin) override;
48 
49  // Documentation inherited
50  public: virtual math::Vector3d Origin() const override;
51 
52  // Documentation inherited
53  public: virtual void SetDirection(const math::Vector3d &_dir) override;
54 
55  // Documentation inherited
56  public: virtual math::Vector3d Direction() const override;
57 
58  // Documentation inherited
59  public: virtual void SetFromCamera(const CameraPtr &_camera,
60  const math::Vector2d &_coord) override;
61 
62  // Documentation inherited
63  public: void SetPreferGpu(bool _preferGpu) override;
64 
65  // Documentation inherited
66  public: bool UsesGpu() const override;
67 
68  // Documentation inherited
69  public: virtual RayQueryResult ClosestPoint(
70  bool _forceSceneUpdate = true) override;
71 
73  protected: math::Vector3d origin;
74 
77  };
78 
80  template <class T>
82  {
83  }
84 
86  template <class T>
88  {
89  }
90 
92  template <class T>
94  {
95  this->origin = _origin;
96  }
97 
99  template <class T>
101  {
102  return this->origin;
103  }
104 
106  template <class T>
108  {
109  this->direction = _dir;
110  }
111 
113  template <class T>
115  {
116  return this->direction;
117  }
118 
120  template <class T>
122  const gz::math::Vector2d &_coord)
123  {
124  math::Matrix4d projectionMatrix = _camera->ProjectionMatrix();
125  math::Matrix4d viewMatrix = _camera->ViewMatrix();
126  math::Vector3d start(_coord.X(), _coord.Y(), -1.0);
127  math::Vector3d end(_coord.X(), _coord.Y(), 0.0);
128  math::Matrix4d viewProjInv = (projectionMatrix * viewMatrix).Inverse();
129 
130  // rotate start and end
131  // gz math does not support matrix4 * vec4
132  // so calc homogeneous coordinate w ourselves
133  double startw = viewProjInv(3, 0) * start[0] +
134  viewProjInv(3, 1) * start[1] +
135  viewProjInv(3, 2) * start[2] + viewProjInv(3, 3);
136  double endw = viewProjInv(3, 0) * end[0] +
137  viewProjInv(3, 1) * end[1] +
138  viewProjInv(3, 2) * end[2] + viewProjInv(3, 3);
139  start = viewProjInv * start;
140  end = viewProjInv * end;
141  // normalize
142  start = start / startw;
143  end = end / endw;
144  math::Vector3d dir = (end - start).Normalize();
145 
146  this->origin = start;
147  this->direction = dir;
148  }
149 
151  template <class T>
152  void BaseRayQuery<T>::SetPreferGpu(bool /*_preferGpu*/) // NOLINT
153  {
154  }
155 
157  template <class T>
159  {
160  return false;
161  }
162 
164  template <class T>
166  bool /*_forceSceneUpdate*/) // NOLINT
167  {
168  // TODO(anyone): implement a generic ray query here?
169  RayQueryResult result;
170  result.distance = -1;
171  return result;
172  }
173  }
174  }
175 }
176 #endif