Gazebo Rendering

API Reference

7.4.2
gz/rendering/base/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: virtual RayQueryResult ClosestPoint(
64  bool _forceSceneUpdate = true) override;
65 
67  protected: math::Vector3d origin;
68 
71  };
72 
74  template <class T>
76  {
77  }
78 
80  template <class T>
82  {
83  }
84 
86  template <class T>
88  {
89  this->origin = _origin;
90  }
91 
93  template <class T>
95  {
96  return this->origin;
97  }
98 
100  template <class T>
102  {
103  this->direction = _dir;
104  }
105 
107  template <class T>
109  {
110  return this->direction;
111  }
112 
114  template <class T>
116  const gz::math::Vector2d &_coord)
117  {
118  math::Matrix4d projectionMatrix = _camera->ProjectionMatrix();
119  math::Matrix4d viewMatrix = _camera->ViewMatrix();
120  math::Vector3d start(_coord.X(), _coord.Y(), -1.0);
121  math::Vector3d end(_coord.X(), _coord.Y(), 0.0);
122  math::Matrix4d viewProjInv = (projectionMatrix * viewMatrix).Inverse();
123 
124  // rotate start and end
125  // gz math does not support matrix4 * vec4
126  // so calc homogeneous coordinate w ourselves
127  double startw = viewProjInv(3, 0) * start[0] +
128  viewProjInv(3, 1) * start[1] +
129  viewProjInv(3, 2) * start[2] + viewProjInv(3, 3);
130  double endw = viewProjInv(3, 0) * end[0] +
131  viewProjInv(3, 1) * end[1] +
132  viewProjInv(3, 2) * end[2] + viewProjInv(3, 3);
133  start = viewProjInv * start;
134  end = viewProjInv * end;
135  // normalize
136  start = start / startw;
137  end = end / endw;
138  math::Vector3d dir = (end - start).Normalize();
139 
140  this->origin = start;
141  this->direction = dir;
142  }
143 
145  template <class T>
147  bool /*_forceSceneUpdate*/) // NOLINT
148  {
149  // TODO(anyone): implement a generic ray query here?
150  RayQueryResult result;
151  result.distance = -1;
152  return result;
153  }
154  }
155  }
156 }
157 #endif