From 1f4def63aefcf788a427cd9bb4354617490be67c Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 31 Oct 2024 17:00:37 -0700 Subject: [PATCH 1/9] Check valid camera near far clip distances (#1082) Signed-off-by: Ian Chen --- include/gz/rendering/base/BaseCamera.hh | 13 +++++++++++++ ogre/src/OgreCamera.cc | 4 ++-- ogre2/src/Ogre2Camera.cc | 4 ++-- test/common_test/Camera_TEST.cc | 8 ++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/gz/rendering/base/BaseCamera.hh b/include/gz/rendering/base/BaseCamera.hh index c1771b2a6..5201a5e88 100644 --- a/include/gz/rendering/base/BaseCamera.hh +++ b/include/gz/rendering/base/BaseCamera.hh @@ -17,6 +17,7 @@ #ifndef GZ_RENDERING_BASE_BASECAMERA_HH_ #define GZ_RENDERING_BASE_BASECAMERA_HH_ +#include #include #include @@ -709,6 +710,12 @@ namespace gz template void BaseCamera::SetFarClipPlane(const double _far) { + if (_far <= 0 || !std::isfinite(_far)) + { + gzerr << "Far clip distance must be a finite number greater than 0." + << std::endl; + return; + } this->farClip = _far; } @@ -723,6 +730,12 @@ namespace gz template void BaseCamera::SetNearClipPlane(const double _near) { + if (_near <= 0 || !std::isfinite(_near)) + { + gzerr << "Near clip distance must be a finite number greater than 0." + << std::endl; + return; + } this->nearClip = _near; } diff --git a/ogre/src/OgreCamera.cc b/ogre/src/OgreCamera.cc index 3b7af1ef8..03a652a6e 100644 --- a/ogre/src/OgreCamera.cc +++ b/ogre/src/OgreCamera.cc @@ -340,14 +340,14 @@ void OgreCamera::SetNearClipPlane(const double _near) { // this->nearClip = _near; BaseCamera::SetNearClipPlane(_near); - this->ogreCamera->setNearClipDistance(_near); + this->ogreCamera->setNearClipDistance(this->nearClip); } ////////////////////////////////////////////////// void OgreCamera::SetFarClipPlane(const double _far) { BaseCamera::SetFarClipPlane(_far); - this->ogreCamera->setFarClipDistance(_far); + this->ogreCamera->setFarClipDistance(this->farClip); } ////////////////////////////////////////////////// diff --git a/ogre2/src/Ogre2Camera.cc b/ogre2/src/Ogre2Camera.cc index aa3024431..61cca096b 100644 --- a/ogre2/src/Ogre2Camera.cc +++ b/ogre2/src/Ogre2Camera.cc @@ -403,14 +403,14 @@ void Ogre2Camera::SetProjectionType(CameraProjectionType _type) void Ogre2Camera::SetNearClipPlane(const double _near) { BaseCamera::SetNearClipPlane(_near); - this->ogreCamera->setNearClipDistance(_near); + this->ogreCamera->setNearClipDistance(this->nearClip); } ////////////////////////////////////////////////// void Ogre2Camera::SetFarClipPlane(const double _far) { BaseCamera::SetFarClipPlane(_far); - this->ogreCamera->setFarClipDistance(_far); + this->ogreCamera->setFarClipDistance(this->farClip); } ////////////////////////////////////////////////// diff --git a/test/common_test/Camera_TEST.cc b/test/common_test/Camera_TEST.cc index d42aabe9c..87cb4ef29 100644 --- a/test/common_test/Camera_TEST.cc +++ b/test/common_test/Camera_TEST.cc @@ -61,10 +61,18 @@ TEST_F(CameraTest, ViewProjectionMatrix) camera->SetNearClipPlane(0.1); EXPECT_DOUBLE_EQ(0.1, camera->NearClipPlane()); + // set invalid clip distance and verify it is not set + camera->SetNearClipPlane(0.0); + EXPECT_DOUBLE_EQ(0.1, camera->NearClipPlane()); + EXPECT_GT(camera->FarClipPlane(), 0); camera->SetFarClipPlane(800); EXPECT_DOUBLE_EQ(800, camera->FarClipPlane()); + // set invalid clip distance and verify it is not set + camera->SetFarClipPlane(-10.0); + EXPECT_DOUBLE_EQ(800, camera->FarClipPlane()); + EXPECT_NE(projMatrix, camera->ProjectionMatrix()); // view matrix From 9e874c0483d3c3cd34477baa59db8f7d9341a14b Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 6 Nov 2024 09:36:23 -0800 Subject: [PATCH 2/9] Do not create wirebox with empty AABB (ogre) (#1083) Signed-off-by: Ian Chen --- ogre/src/OgreWireBox.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ogre/src/OgreWireBox.cc b/ogre/src/OgreWireBox.cc index e44c064ba..c071af58d 100644 --- a/ogre/src/OgreWireBox.cc +++ b/ogre/src/OgreWireBox.cc @@ -84,6 +84,9 @@ void OgreWireBox::Create() this->scene->OgreSceneManager()->createManualObject(this->name); } + if (this->box == math::AxisAlignedBox()) + return; + this->dataPtr->manualObject->clear(); this->dataPtr->manualObject->setCastShadows(false); @@ -169,6 +172,10 @@ void OgreWireBox::SetMaterial(MaterialPtr _material, bool _unique) ////////////////////////////////////////////////// void OgreWireBox::SetMaterialImpl(OgreMaterialPtr _material) { + if (!this->dataPtr->manualObject || + this->dataPtr->manualObject->getNumSections() == 0u) + return; + std::string materialName = _material->Name(); Ogre::MaterialPtr ogreMaterial = _material->Material(); this->dataPtr->manualObject->setMaterialName(0, materialName); From 9cc0e9eadd9e10f548c87763b9e7f8acb537d9dd Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Tue, 13 Aug 2024 22:39:29 +0530 Subject: [PATCH 3/9] Visualize Frustum Signed-off-by: Utkarsh --- include/gz/rendering/FrustumVisual.hh | 180 +++++++++++ include/gz/rendering/RenderTypes.hh | 9 + include/gz/rendering/Scene.hh | 28 ++ .../gz/rendering/base/BaseFrustumVisual.hh | 265 ++++++++++++++++ include/gz/rendering/base/BaseScene.hh | 22 ++ .../gz/rendering/ogre/OgreFrustumVisual.hh | 77 +++++ .../gz/rendering/ogre/OgreRenderTypes.hh | 2 + ogre/include/gz/rendering/ogre/OgreScene.hh | 4 + ogre/src/OgreFrustumVisual.cc | 102 ++++++ ogre/src/OgreScene.cc | 10 + .../gz/rendering/ogre2/Ogre2FrustumVisual.hh | 77 +++++ .../gz/rendering/ogre2/Ogre2RenderTypes.hh | 2 + .../include/gz/rendering/ogre2/Ogre2Scene.hh | 4 + ogre2/src/Ogre2FrustumVisual.cc | 290 ++++++++++++++++++ ogre2/src/Ogre2Scene.cc | 10 + src/FrustumVisual.cc | 28 ++ src/base/BaseScene.cc | 31 ++ 17 files changed, 1141 insertions(+) create mode 100644 include/gz/rendering/FrustumVisual.hh create mode 100644 include/gz/rendering/base/BaseFrustumVisual.hh create mode 100644 ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh create mode 100644 ogre/src/OgreFrustumVisual.cc create mode 100644 ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh create mode 100644 ogre2/src/Ogre2FrustumVisual.cc create mode 100644 src/FrustumVisual.cc diff --git a/include/gz/rendering/FrustumVisual.hh b/include/gz/rendering/FrustumVisual.hh new file mode 100644 index 000000000..bd1771a30 --- /dev/null +++ b/include/gz/rendering/FrustumVisual.hh @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2015 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ +#ifndef GZ_RENDERING_FRUSTUMVISUAL_HH_ +#define GZ_RENDERING_FRUSTUMVISUAL_HH_ + +#include +#include +#include +#include +#include +#include "gz/rendering/config.hh" +#include "gz/rendering/Visual.hh" +#include "gz/rendering/Object.hh" +#include "gz/rendering/RenderTypes.hh" +#include "gz/rendering/Marker.hh" + +namespace gz +{ + namespace rendering + { + inline namespace GZ_RENDERING_VERSION_NAMESPACE { + // + /// \brief Planes that define the boundaries of the frustum. + enum GZ_RENDERING_VISIBLE FrustumVisualPlane + { + /// \brief Near plane + FRUSTUM_PLANE_NEAR = 0, + + /// \brief Far plane + FRUSTUM_PLANE_FAR = 1, + + /// \brief Left plane + FRUSTUM_PLANE_LEFT = 2, + + /// \brief Right plane + FRUSTUM_PLANE_RIGHT = 3, + + /// \brief Top plane + FRUSTUM_PLANE_TOP = 4, + + /// \brief Bottom plane + FRUSTUM_PLANE_BOTTOM = 5 + }; + + /// \brief Mathematical representation of a frustum and related functions. + /// This is also known as a view frustum. + class GZ_RENDERING_VISIBLE FrustumVisual : public virtual Visual + { + /// \brief Default constructor. With the following default values: + /// + /// * near: 0.0 + /// * far: 1.0 + /// * fov: 0.78539 radians (45 degrees) + /// * aspect ratio: 1.0 + /// * pose: Pose3d::Zero + protected: FrustumVisual(); + + /// \brief Destructor + public: virtual ~FrustumVisual(); + + /// \brief Constructor + /// \param[in] _near Near plane distance. This is the distance from + /// the frustum's vertex to the closest plane + /// \param[in] _far Far plane distance. This is the distance from the + /// frustum's vertex to the farthest plane. + /// \param[in] _fov Field of view. The field of view is the + /// angle between the frustum's vertex and the edges of the near or far + /// plane. This value represents the horizontal angle. + /// \param[in] _aspectRatio The aspect ratio, which is the width divided + /// by height of the near or far planes. + /// \param[in] _pose Pose of the frustum, which is the vertex (top of + /// the pyramid). + /*protected: FrustumVisual(double _near, + double _far, + const math::Angle &_fov, + double _aspectRatio, + const gz::math::Pose3d &_pose = gz::math::Pose3d::Zero);*/ + + public: virtual void Update() = 0; + + /// \brief Get the near distance. This is the distance from the + /// frustum's vertex to the closest plane. + /// \return Near distance. + /// \sa SetNear + public: virtual double Near() const = 0; + + /// \brief Set the near distance. This is the distance from the + /// frustum's vertex to the closest plane. + /// \param[in] _near Near distance. + /// \sa Near + public: virtual void SetNear(double _near) = 0; + + /// \brief Get the far distance. This is the distance from the + /// frustum's vertex to the farthest plane. + /// \return Far distance. + /// \sa SetFar + public: virtual double Far() const = 0; + + /// \brief Set the far distance. This is the distance from the + /// frustum's vertex to the farthest plane. + /// \param[in] _far Far distance. + /// \sa Far + public: virtual void SetFar(double _far) = 0; + + /// \brief Get the horizontal field of view. The field of view is the + /// angle between the frustum's vertex and the edges of the near or far + /// plane. This value represents the horizontal angle. + /// \return The field of view. + /// \sa SetFOV + public: virtual gz::math::Angle FOV() const = 0; + + /// \brief Set the horizontal field of view. The field of view is the + /// angle between the frustum's vertex and the edges of the near or far + /// plane. This value represents the horizontal angle. + /// \param[in] _fov The field of view. + /// \sa FOV + public: virtual void SetFOV(const gz::math::Angle &_fov) = 0; + + /// \brief Get the aspect ratio, which is the width divided by height + /// of the near or far planes. + /// \return The frustum's aspect ratio. + /// \sa SetAspectRatio + public: virtual double AspectRatio() const = 0; + + /// \brief Set the aspect ratio, which is the width divided by height + /// of the near or far planes. + /// \param[in] _aspectRatio The frustum's aspect ratio. + /// \sa AspectRatio + public: virtual void SetAspectRatio(double _aspectRatio) = 0; + + /// \brief Get a plane of the frustum. + /// \param[in] _plane The plane to return. + /// \return Plane of the frustum. + public: virtual gz::math::Planed Plane(const FrustumVisualPlane _plane) const = 0; + + /// \brief Check if a box lies inside the pyramid frustum. + /// \param[in] _b Box to check. + /// \return True if the box is inside the pyramid frustum. + //public: virtual bool Contains(const gz::math::AxisAlignedBox &_b) const = 0; //TO-DO + + /// \brief Check if a point lies inside the pyramid frustum. + /// \param[in] _p Point to check. + /// \return True if the point is inside the pyramid frustum. + //public: virtual bool Contains(const gz::math::Vector3d &_p) const = 0; //TO-DO + + /// \brief Get the pose of the frustum + /// \return Pose of the frustum + /// \sa SetPose + public: virtual gz::math::Pose3d Pose() const = 0; + + /// \brief Set the pose of the frustum + /// \param[in] _pose Pose of the frustum, top vertex. + /// \sa Pose + public: virtual void SetPose(const gz::math::Pose3d &_pose) = 0; + + /// \brief Compute the planes of the frustum. This is called whenever + /// a property of the frustum is changed. + private: void ComputePlanes(); + + /// \brief Private data pointer + //GZ_UTILS_IMPL_PTR(dataPtr) + }; + } + } +} +#endif diff --git a/include/gz/rendering/RenderTypes.hh b/include/gz/rendering/RenderTypes.hh index 2a7c5e0e8..addac9636 100644 --- a/include/gz/rendering/RenderTypes.hh +++ b/include/gz/rendering/RenderTypes.hh @@ -72,6 +72,7 @@ namespace gz class LightVisual; class JointVisual; class LidarVisual; + class FrustumVisual; class Light; class Marker; class Material; @@ -206,6 +207,10 @@ namespace gz /// \brief Shared pointer to LidarVisual typedef shared_ptr LidarVisualPtr; + /// \typedef FrustumVisualPtr + /// \brief Shared pointer to FrustumVisual + typedef shared_ptr FrustumVisualPtr; + /// \typedef MaterialPtr /// \brief Shared pointer to Material typedef shared_ptr MaterialPtr; @@ -384,6 +389,10 @@ namespace gz /// \brief Shared pointer to const LidarVisual typedef shared_ptr ConstLidarVisualPtr; + /// \typedef const FrustumVisualPtr + /// \brief Shared pointer to const FrustumVisual + typedef shared_ptr ConstFrustumVisualPtr; + /// \typedef const MaterialPtr /// \brief Shared pointer to const Material typedef shared_ptr ConstMaterialPtr; diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index fdaa6436d..bf6b11395 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1108,6 +1108,34 @@ namespace gz public: virtual LidarVisualPtr CreateLidarVisual( unsigned int _id, const std::string &_name) = 0; + /// \brief Create new frusum visual. A unique ID and name will + /// automatically be assigned to the frustum visual. + /// \return The created frustum visual + public: virtual FrustumVisualPtr CreateFrustumVisual() = 0; + + /// \brief Create new frustum visual with the given ID. A unique name + /// will automatically be assigned to the frustum visual. If the given + /// ID is already in use, NULL will be returned. + /// \param[in] _id ID of the new frustum visual + /// \return The created frustum visual + public: virtual FrustumVisualPtr CreateFrustumVisual(unsigned int _id) = 0; + + /// \brief Create new frustum visual with the given name. A unique ID + /// will automatically be assigned to the frustum visual. If the given + /// name is already in use, NULL will be returned. + /// \param[in] _name Name of the new frustum visual + /// \return The created frustum visual + public: virtual FrustumVisualPtr CreateFrustumVisual( + const std::string &_name) = 0; + + /// \brief Create new frustum visual with the given name. If either + /// the given ID or name is already in use, NULL will be returned. + /// \param[in] _id ID of the frustum visual. + /// \param[in] _name Name of the new frustum visual. + /// \return The created frustum visual + public: virtual FrustumVisualPtr CreateFrustumVisual( + unsigned int _id, const std::string &_name) = 0; + /// \brief Create new heightmap geomerty. The rendering::Heightmap will be /// created from the given HeightmapDescriptor. /// \param[in] _desc Data about the heightmap diff --git a/include/gz/rendering/base/BaseFrustumVisual.hh b/include/gz/rendering/base/BaseFrustumVisual.hh new file mode 100644 index 000000000..875156ec4 --- /dev/null +++ b/include/gz/rendering/base/BaseFrustumVisual.hh @@ -0,0 +1,265 @@ +/* + * Copyright (C) 2020 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ +#ifndef GZ_RENDERING_BASEFRUSTUMVISUAL_HH_ +#define GZ_RENDERING_BASEFRUSTUMVISUAL_HH_ + +#include + +#include "gz/rendering/FrustumVisual.hh" +#include "gz/rendering/base/BaseObject.hh" +#include "gz/rendering/base/BaseRenderTypes.hh" +#include "gz/rendering/Scene.hh" + +namespace gz +{ + namespace rendering + { + inline namespace GZ_RENDERING_VERSION_NAMESPACE { + /// \brief Base implementation of a Frustum Visual + template + class BaseFrustumVisual : + public virtual FrustumVisual, + public virtual T + { + // Documentation inherited + protected: BaseFrustumVisual(); + + // Documentation inherited + public: virtual ~BaseFrustumVisual(); + + // Documentation inherited + public: virtual void PreRender() override; + + // Documentation inherited + public: virtual void Destroy() override; + + // Documentation inherited + public: virtual void Update() override; + + // Documentation inherited + public: virtual void Init() override; + + // Documentation inherited + public: virtual double Near() const override; + + // Documentation inherited + public: virtual void SetNear(double _near) override; + + // Documentation inherited + public: virtual double Far() const override; + + // Documentation inherited + public: virtual void SetFar(double _far) override; + + // Documentation inherited + public: virtual math::Angle FOV() const override; + + // Documentation inherited + public: virtual void SetFOV(const math::Angle &_fov) override; + + // Documentation inherited + public: virtual double AspectRatio() const override; + + // Documentation inherited + public: virtual void SetAspectRatio(double _aspectRatio) override; + + // Documentation inherited + public: virtual gz::math::Planed Plane(const FrustumVisualPlane _plane) const override; + + // Documentation inherited + //public: virtual bool Contains(const gz::math::AxisAlignedBox &_b) const override; + + // Documentation inherited + //public: virtual bool Contains(const gz::math::Vector3d &_p) const override; + + // Documentation inherited + public: virtual gz::math::Pose3d Pose() const override; + + // Documentation inherited + public: virtual void SetPose(const gz::math::Pose3d &_pose) override; + + /// \brief Create predefined materials for lidar visual + public: virtual void CreateMaterials(); + + /// \brief near value + protected: double near = 0.0; + + /// \brief far value + protected: double far = 1.0; + + /// \brief fov value + protected: gz::math::Angle fov = 0.78539; + + /// \brief aspect ratio value + protected: double aspectRatio = 1.0; + + /// \brief array of plane + protected: std::array planes; + + /// \brief pose of visual + protected: gz::math::Pose3d pose = gz::math::Pose3d::Zero; + }; + + ///////////////////////////////////////////////// + // BaseFrustumVisual + ///////////////////////////////////////////////// + template + BaseFrustumVisual::BaseFrustumVisual() + { + } + + ///////////////////////////////////////////////// + template + BaseFrustumVisual::~BaseFrustumVisual() + { + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::PreRender() + { + T::PreRender(); + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::Destroy() + { + T::Destroy(); + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::Update() + { + // no op + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::Init() + { + T::Init(); + this->CreateMaterials(); + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::SetNear( + double _near) + { + this->near = _near; + } + + ///////////////////////////////////////////////// + template + double BaseFrustumVisual::Near() const + { + return this->near; + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::SetFar( + double _far) + { + this->far = _far; + } + + ///////////////////////////////////////////////// + template + double BaseFrustumVisual::Far() const + { + return this->far; + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::SetFOV( + const gz::math::Angle &_fov) + { + this->fov = _fov; + } + + ///////////////////////////////////////////////// + template + gz::math::Angle BaseFrustumVisual::FOV() const + { + return this->fov; + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::SetAspectRatio( + double _aspectRatio) + { + this->aspectRatio = _aspectRatio; + } + + ///////////////////////////////////////////////// + template + double BaseFrustumVisual::AspectRatio() const + { + return this->aspectRatio; + } + + ///////////////////////////////////////////////// + template + gz::math::Planed BaseFrustumVisual::Plane(const FrustumVisualPlane _plane) const + { + return this->planes[_plane]; + } + + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::SetPose(const gz::math::Pose3d &_pose) + { + this->pose = _pose; + } + + ///////////////////////////////////////////////// + template + gz::math::Pose3d BaseFrustumVisual::Pose() const + { + return this->pose; + } + ///////////////////////////////////////////////// + template + void BaseFrustumVisual::CreateMaterials() + { + MaterialPtr mtl; + + if (!this->Scene()->MaterialRegistered("Frustum/BlueRay")) + { + mtl = this->Scene()->CreateMaterial("Frustum/BlueRay"); + mtl->SetAmbient(0.0, 0.0, 1.0); + mtl->SetDiffuse(0.0, 0.0, 1.0); + mtl->SetEmissive(0.0, 0.0, 1.0); + mtl->SetSpecular(0.0, 0.0, 1.0); + mtl->SetTransparency(0.0); + mtl->SetCastShadows(false); + mtl->SetReceiveShadows(false); + mtl->SetLightingEnabled(false); + mtl->SetMetalness(0.1f); + mtl->SetReflectivity(0.2); + } + return; + } + } + } +} +#endif diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index 200246a2b..b92fd1449 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -537,6 +537,21 @@ namespace gz public: virtual LidarVisualPtr CreateLidarVisual(unsigned int _id, const std::string &_name) override; + // Documentation inherited. + public: virtual FrustumVisualPtr CreateFrustumVisual() override; + + // Documentation inherited. + public: virtual FrustumVisualPtr CreateFrustumVisual( + unsigned int _id) override; + + // Documentation inherited. + public: virtual FrustumVisualPtr CreateFrustumVisual( + const std::string &_name) override; + + // Documentation inherited. + public: virtual FrustumVisualPtr CreateFrustumVisual(unsigned int _id, + const std::string &_name) override; + // Documentation inherited. public: virtual HeightmapPtr CreateHeightmap( const HeightmapDescriptor &_desc) override; @@ -828,6 +843,13 @@ namespace gz protected: virtual LidarVisualPtr CreateLidarVisualImpl(unsigned int _id, const std::string &_name) = 0; + /// \brief Implementation for creating a frustum visual + /// \param[in] _id unique object id. + /// \param[in] _name unique object name. + /// \return Pointer to a frustum visual + protected: virtual FrustumVisualPtr CreateFrustumVisualImpl(unsigned int _id, + const std::string &_name) = 0; + /// \brief Implementation for creating a heightmap geometry /// \param[in] _id Unique object id. /// \param[in] _name Unique object name. diff --git a/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh b/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh new file mode 100644 index 000000000..ede578b37 --- /dev/null +++ b/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2020 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#ifndef GZ_RENDERING_OGRE_OGREFRUSTUMVISUAL_HH_ +#define GZ_RENDERING_OGRE_OGREFRUSTUMVISUAL_HH_ + +#include +#include +#include "gz/rendering/base/BaseFrustumVisual.hh" +#include "gz/rendering/ogre/OgreVisual.hh" +#include "gz/rendering/ogre/OgreIncludes.hh" +#include "gz/rendering/ogre/OgreScene.hh" + +namespace gz +{ + namespace rendering + { + inline namespace GZ_RENDERING_VERSION_NAMESPACE { + // + // Forward declaration + class OgreFrustumVisualPrivate; + + /// \brief Ogre implementation of a Frustum Visual. + class GZ_RENDERING_OGRE_VISIBLE OgreFrustumVisual + : public BaseFrustumVisual + { + /// \brief Constructor + protected: OgreFrustumVisual(); + + /// \brief Destructor + public: virtual ~OgreFrustumVisual(); + + // Documentation inherited. + public: virtual void Init() override; + + // Documentation inherited. + public: virtual void PreRender() override; + + // Documentation inherited. + public: virtual void Destroy() override; + + // Documentation inherited + public: virtual void Update() override; + + /// \brief Create the Frustum Visual in ogre + private: void Create(); + + /// \brief Clear data stored by dynamiclines + private: void ClearVisualData(); + + // Documentation inherited + public: virtual void SetVisible(bool _visible) override; + + /// \brief Frustum Visual should only be created by scene. + private: friend class OgreScene; + + /// \brief Private data class + private: std::unique_ptr dataPtr; + }; + } + } +} +#endif diff --git a/ogre/include/gz/rendering/ogre/OgreRenderTypes.hh b/ogre/include/gz/rendering/ogre/OgreRenderTypes.hh index 06fabd6f8..031caa34d 100644 --- a/ogre/include/gz/rendering/ogre/OgreRenderTypes.hh +++ b/ogre/include/gz/rendering/ogre/OgreRenderTypes.hh @@ -33,6 +33,7 @@ namespace gz class OgreCOMVisual; class OgreDepthCamera; class OgreDirectionalLight; + class OgreFrustumVisual; class OgreGeometry; class OgreGizmoVisual; class OgreGpuRays; @@ -86,6 +87,7 @@ namespace gz typedef shared_ptr OgreCOMVisualPtr; typedef shared_ptr OgreDepthCameraPtr; typedef shared_ptr OgreDirectionalLightPtr; + typedef shared_ptr OgreFrustumVisualPtr; typedef shared_ptr OgreGeometryPtr; typedef shared_ptr OgreGeometryStorePtr; typedef shared_ptr OgreGizmoVisualPtr; diff --git a/ogre/include/gz/rendering/ogre/OgreScene.hh b/ogre/include/gz/rendering/ogre/OgreScene.hh index a55c8de66..19f14f368 100644 --- a/ogre/include/gz/rendering/ogre/OgreScene.hh +++ b/ogre/include/gz/rendering/ogre/OgreScene.hh @@ -180,6 +180,10 @@ namespace gz protected: virtual LidarVisualPtr CreateLidarVisualImpl(unsigned int _id, const std::string &_name) override; + // Documentation inherited + protected: virtual FrustumVisualPtr CreateFrustumVisualImpl(unsigned int _id, + const std::string &_name) override; + // Documentation inherited protected: virtual WireBoxPtr CreateWireBoxImpl(unsigned int _id, const std::string &_name) override; diff --git a/ogre/src/OgreFrustumVisual.cc b/ogre/src/OgreFrustumVisual.cc new file mode 100644 index 000000000..abe7c1b79 --- /dev/null +++ b/ogre/src/OgreFrustumVisual.cc @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2020 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include "gz/rendering/ogre/OgreDynamicLines.hh" +#include "gz/rendering/ogre/OgreFrustumVisual.hh" +#include "gz/rendering/ogre/OgreScene.hh" +#include "gz/rendering/ogre/OgreMarker.hh" +#include "gz/rendering/ogre/OgreGeometry.hh" + + +class gz::rendering::OgreFrustumVisualPrivate +{ + /// \brief Frustum Ray DynamicLines Object to display + public: std::vector> rayLines; + + /// \brief Frustum visual type + //public: FrustumVisualPlane frustumVisPlane = + //FrustumVisualPlane::FRUSTUM_PLANE_TOP; + + /// \brief The visibility of the visual + public: bool visible = true; + + /// \brief Each corner of the frustum. + public: std::array points; + + /// \brief each edge of the frustum. + public: std::array, 12> edges; +}; + +using namespace gz; +using namespace rendering; + +////////////////////////////////////////////////// +OgreFrustumVisual::OgreFrustumVisual() + : dataPtr(new OgreFrustumVisualPrivate) +{ +} + +////////////////////////////////////////////////// +OgreFrustumVisual::~OgreFrustumVisual() +{ + // no ops +} + +////////////////////////////////////////////////// +void OgreFrustumVisual::PreRender() +{ + // no ops +} + +////////////////////////////////////////////////// +void OgreFrustumVisual::Destroy() +{ + BaseFrustumVisual::Destroy(); +} + +////////////////////////////////////////////////// +void OgreFrustumVisual::Init() +{ + BaseFrustumVisual::Init(); + this->Create(); +} + +////////////////////////////////////////////////// +void OgreFrustumVisual::Create() +{ + // no ops +} + +////////////////////////////////////////////////// +void OgreFrustumVisual::ClearVisualData() +{ + this->dataPtr->rayLines.clear(); +} + +////////////////////////////////////////////////// +void OgreFrustumVisual::Update() +{ + //no ops FIX-ME +} + +////////////////////////////////////////////////// +void OgreFrustumVisual::SetVisible(bool _visible) +{ + this->dataPtr->visible = _visible; + this->ogreNode->setVisible(this->dataPtr->visible); +} diff --git a/ogre/src/OgreScene.cc b/ogre/src/OgreScene.cc index 36531eb21..981c64fe4 100644 --- a/ogre/src/OgreScene.cc +++ b/ogre/src/OgreScene.cc @@ -35,6 +35,7 @@ #include "gz/rendering/ogre/OgreInertiaVisual.hh" #include "gz/rendering/ogre/OgreJointVisual.hh" #include "gz/rendering/ogre/OgreLidarVisual.hh" +#include "gz/rendering/ogre/OgreFrustumVisual.hh" #include "gz/rendering/ogre/OgreLightVisual.hh" #include "gz/rendering/ogre/OgreMarker.hh" #include "gz/rendering/ogre/OgreMaterial.hh" @@ -622,6 +623,15 @@ LidarVisualPtr OgreScene::CreateLidarVisualImpl(unsigned int _id, return (result) ? lidar: nullptr; } +////////////////////////////////////////////////// +FrustumVisualPtr OgreScene::CreateFrustumVisualImpl(unsigned int _id, + const std::string &_name) +{ + OgreFrustumVisualPtr frustum(new OgreFrustumVisual); + bool result = this->InitObject(frustum, _id, _name); + return (result) ? frustum: nullptr; +} + ////////////////////////////////////////////////// TextPtr OgreScene::CreateTextImpl(unsigned int _id, const std::string &_name) { diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh b/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh new file mode 100644 index 000000000..f0c63cec0 --- /dev/null +++ b/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2020 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#ifndef GZ_RENDERING_OGRE2_OGREFRUSTUMVISUAL_HH_ +#define GZ_RENDERING_OGRE2_OGREFRUSTUMVISUAL_HH_ + +#include +#include +#include "gz/rendering/base/BaseFrustumVisual.hh" +#include "gz/rendering/ogre2/Ogre2Visual.hh" +#include "gz/rendering/ogre2/Ogre2Includes.hh" +#include "gz/rendering/ogre2/Ogre2Scene.hh" + +namespace gz +{ + namespace rendering + { + inline namespace GZ_RENDERING_VERSION_NAMESPACE { + // + // Forward declaration + class Ogre2FrustumVisualPrivate; + + /// \brief Ogre 2.x implementation of a Frustum Visual. + class GZ_RENDERING_OGRE2_VISIBLE Ogre2FrustumVisual + : public BaseFrustumVisual + { + /// \brief Constructor + protected: Ogre2FrustumVisual(); + + /// \brief Destructor + public: virtual ~Ogre2FrustumVisual(); + + // Documentation inherited. + public: virtual void Init() override; + + // Documentation inherited. + public: virtual void PreRender() override; + + // Documentation inherited. + public: virtual void Destroy() override; + + // Documentation inherited + public: virtual void Update() override; + + /// \brief Create the Frustum Visual in ogre + private: void Create(); + + /// \brief Clear data stored by dynamiclines + private: void ClearVisualData(); + + // Documentation inherited + public: virtual void SetVisible(bool _visible) override; + + /// \brief Frustum Visual should only be created by scene. + private: friend class Ogre2Scene; + + /// \brief Private data class + private: std::unique_ptr dataPtr; + }; + } + } +} +#endif diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2RenderTypes.hh b/ogre2/include/gz/rendering/ogre2/Ogre2RenderTypes.hh index b2c0fd6dd..d8c5ef4eb 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2RenderTypes.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2RenderTypes.hh @@ -36,6 +36,7 @@ namespace gz class Ogre2COMVisual; class Ogre2DepthCamera; class Ogre2DirectionalLight; + class Ogre2FrustumVisual; class Ogre2Geometry; class Ogre2GizmoVisual; class Ogre2GlobalIlluminationCiVct; @@ -92,6 +93,7 @@ namespace gz typedef shared_ptr Ogre2COMVisualPtr; typedef shared_ptr Ogre2DepthCameraPtr; typedef shared_ptr Ogre2DirectionalLightPtr; + typedef shared_ptr Ogre2FrustumVisualPtr; typedef shared_ptr Ogre2GeometryPtr; typedef shared_ptr Ogre2GizmoVisualPtr; typedef shared_ptr Ogre2GpuRaysPtr; diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index 323c2b30b..4c2e5d1ef 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -343,6 +343,10 @@ namespace gz protected: virtual LidarVisualPtr CreateLidarVisualImpl(unsigned int _id, const std::string &_name) override; + // Documentation inherited + protected: virtual FrustumVisualPtr CreateFrustumVisualImpl(unsigned int _id, + const std::string &_name) override; + // Documentation inherited protected: virtual WireBoxPtr CreateWireBoxImpl(unsigned int _id, const std::string &_name) override; diff --git a/ogre2/src/Ogre2FrustumVisual.cc b/ogre2/src/Ogre2FrustumVisual.cc new file mode 100644 index 000000000..7669bcd8f --- /dev/null +++ b/ogre2/src/Ogre2FrustumVisual.cc @@ -0,0 +1,290 @@ +/* + * Copyright (C) 2020 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifdef __APPLE__ + #define GL_SILENCE_DEPRECATION + #include + #include +#else +#ifndef _WIN32 + #include +#endif +#endif + +#include + +#include "gz/rendering/ogre2/Ogre2Conversions.hh" +#include "gz/rendering/ogre2/Ogre2DynamicRenderable.hh" +#include "gz/rendering/ogre2/Ogre2FrustumVisual.hh" +#include "gz/rendering/ogre2/Ogre2RenderEngine.hh" +#include "gz/rendering/ogre2/Ogre2Scene.hh" +#include "gz/rendering/ogre2/Ogre2Marker.hh" +#include "gz/rendering/ogre2/Ogre2Geometry.hh" + +#ifdef _MSC_VER + #pragma warning(push, 0) +#endif +#include +#include +#include +#include +#include +#ifdef _MSC_VER + #pragma warning(pop) +#endif + +class gz::rendering::Ogre2FrustumVisualPrivate +{ + /// \brief Frustum Ray DynamicLines Object to display + public: std::vector> rayLines; + + /// \brief Frustum visual type + //public: FrustumVisualPlane frustumVisPlane = + //FrustumVisualPlane::FRUSTUM_PLANE_TOP; + + /// \brief The visibility of the visual + public: bool visible = true; + + /// \brief Each corner of the frustum. + public: std::array points; + + /// \brief each edge of the frustum. + public: std::array, 12> edges; +}; + +using namespace gz; +using namespace rendering; + +////////////////////////////////////////////////// +Ogre2FrustumVisual::Ogre2FrustumVisual() + : dataPtr(new Ogre2FrustumVisualPrivate) +{ +} + +////////////////////////////////////////////////// +Ogre2FrustumVisual::~Ogre2FrustumVisual() +{ + // no ops +} + +////////////////////////////////////////////////// +void Ogre2FrustumVisual::PreRender() +{ + // no ops +} + +////////////////////////////////////////////////// +void Ogre2FrustumVisual::Destroy() +{ + BaseFrustumVisual::Destroy(); +} + +////////////////////////////////////////////////// +void Ogre2FrustumVisual::Init() +{ + BaseFrustumVisual::Init(); + this->Create(); +} + +////////////////////////////////////////////////// +void Ogre2FrustumVisual::Create() +{ + // enable GL_PROGRAM_POINT_SIZE so we can set gl_PointSize in vertex shader + auto engine = Ogre2RenderEngine::Instance(); + std::string renderSystemName = + engine->OgreRoot()->getRenderSystem()->getFriendlyName(); + if (renderSystemName.find("OpenGL") != std::string::npos) + { +#ifdef __APPLE__ + glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); +#else +#ifndef _WIN32 + glEnable(GL_PROGRAM_POINT_SIZE); +#endif +#endif + } +} + +////////////////////////////////////////////////// +void Ogre2FrustumVisual::ClearVisualData() +{ + this->dataPtr->rayLines.clear(); +} + +////////////////////////////////////////////////// +void Ogre2FrustumVisual::Update() +{ + std::shared_ptr renderable = + std::shared_ptr( + new Ogre2DynamicRenderable(this->Scene())); + this->ogreNode->attachObject(renderable->OgreObject()); + + #if (!(OGRE_VERSION <= ((1 << 16) | (10 << 8) | 7))) + // the Materials are assigned here to avoid repetitive search for materials + Ogre::MaterialPtr rayLineMat = + Ogre::MaterialManager::getSingleton().getByName( + "Frustum/BlueRay"); + #endif + + #if (OGRE_VERSION <= ((1 << 16) | (10 << 8) | 7)) + MaterialPtr mat = this->Scene()->Material("Frustum/BlueRay"); + #else + //MaterialPtr mat = this->Scene()->Material(rayLineMat); //FIX-ME + MaterialPtr mat = this->Scene()->Material("Frustum/BlueRay"); + #endif + + renderable->SetMaterial(mat, false); + renderable->SetOperationType(MT_LINE_LIST); + this->dataPtr->rayLines.push_back(renderable); + + // Tangent of half the field of view. + double tanFOV2 = std::tan(this->fov() * 0.5); + + // Width of near plane + double nearWidth = 2.0 * tanFOV2 * this->near; + + // Height of near plane + double nearHeight = nearWidth / this->aspectRatio; + + // Width of far plane + double farWidth = 2.0 * tanFOV2 * this->far; + + // Height of far plane + double farHeight = farWidth / this->aspectRatio; + + // Up, right, and forward unit vectors. + gz::math::Vector3d forward = this->pose.Rot().RotateVector(gz::math::Vector3d::UnitX); + gz::math::Vector3d up = this->pose.Rot().RotateVector(gz::math::Vector3d::UnitZ); + gz::math::Vector3d right = this->pose.Rot().RotateVector(-gz::math::Vector3d::UnitY); + + // Near plane center + gz::math::Vector3d nearCenter = this->pose.Pos() + forward * this->near; + + // Far plane center + gz::math::Vector3d farCenter = this->pose.Pos() + forward * this->far; + + // These four variables are here for convenience. + gz::math::Vector3d upNearHeight2 = up * (nearHeight * 0.5); + gz::math::Vector3d rightNearWidth2 = right * (nearWidth * 0.5); + gz::math::Vector3d upFarHeight2 = up * (farHeight * 0.5); + gz::math::Vector3d rightFarWidth2 = right * (farWidth * 0.5); + + // Compute the vertices of the near plane + gz::math::Vector3d nearTopLeft = nearCenter + upNearHeight2 - rightNearWidth2; + gz::math::Vector3d nearTopRight = nearCenter + upNearHeight2 + rightNearWidth2; + gz::math::Vector3d nearBottomLeft = nearCenter - upNearHeight2 - rightNearWidth2; + gz::math::Vector3d nearBottomRight = nearCenter - upNearHeight2 + rightNearWidth2; + + // Compute the vertices of the far plane + gz::math::Vector3d farTopLeft = farCenter + upFarHeight2 - rightFarWidth2; + gz::math::Vector3d farTopRight = farCenter + upFarHeight2 + rightFarWidth2; + gz::math::Vector3d farBottomLeft = farCenter - upFarHeight2 - rightFarWidth2; + gz::math::Vector3d farBottomRight = farCenter - upFarHeight2 + rightFarWidth2; + + // Save these vertices + this->dataPtr->points[0] = nearTopLeft; + this->dataPtr->points[1] = nearTopRight; + this->dataPtr->points[2] = nearBottomLeft; + this->dataPtr->points[3] = nearBottomRight; + this->dataPtr->points[4] = farTopLeft; + this->dataPtr->points[5] = farTopRight; + this->dataPtr->points[6] = farBottomLeft; + this->dataPtr->points[7] = farBottomRight; + + // Save the edges + this->dataPtr->edges[0] = {nearTopLeft, nearTopRight}; + this->dataPtr->edges[1] = {nearTopLeft, nearBottomLeft}; + this->dataPtr->edges[2] = {nearTopLeft, farTopLeft}; + this->dataPtr->edges[3] = {nearTopRight, nearBottomRight}; + this->dataPtr->edges[4] = {nearTopRight, farTopRight}; + this->dataPtr->edges[5] = {nearBottomLeft, nearBottomRight}; + this->dataPtr->edges[6] = {nearBottomLeft, farBottomLeft}; + this->dataPtr->edges[7] = {farTopLeft, farTopRight}; + this->dataPtr->edges[8] = {farTopLeft, farBottomLeft}; + this->dataPtr->edges[9] = {farTopRight, farBottomRight}; + this->dataPtr->edges[10] = {farBottomLeft, farBottomRight}; + this->dataPtr->edges[11] = {farBottomRight, nearBottomRight}; + + gz::math::Vector3d leftCenter = + (farTopLeft + nearTopLeft + farBottomLeft + nearBottomLeft) / 4.0; + + gz::math::Vector3d rightCenter = + (farTopRight + nearTopRight + farBottomRight + nearBottomRight) / 4.0; + + gz::math::Vector3d topCenter = + (farTopRight + nearTopRight + farTopLeft + nearTopLeft) / 4.0; + + gz::math::Vector3d bottomCenter = + (farBottomRight + nearBottomRight + farBottomLeft + nearBottomLeft) / 4.0; + + // For creating the frustum visuals + renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, -nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, -nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, -nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, -nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, -farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, -farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, -farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, -farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, -nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, -farHeight)); + renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, -nearHeight)); + renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, -farHeight)); + + // Compute plane offsets + // Set the planes, where the first value is the plane normal and the + // second the plane offset + gz::math::Vector3d norm = gz::math::Vector3d::Normal(nearTopLeft, nearTopRight, nearBottomLeft); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_NEAR].Set(norm, nearCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal(farTopRight, farTopLeft, farBottomLeft); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_FAR].Set(norm, farCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal(farTopLeft, nearTopLeft, nearBottomLeft); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_LEFT].Set(norm, leftCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal(nearTopRight, farTopRight, farBottomRight); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_RIGHT].Set(norm, rightCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal(nearTopLeft, farTopLeft, nearTopRight); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_TOP].Set(norm, topCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal(nearBottomLeft, nearBottomRight, farBottomRight); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_BOTTOM].Set(norm, bottomCenter.Dot(norm)); + + renderable->Update(); + this->SetVisible(this->dataPtr->visible); +} + +////////////////////////////////////////////////// +void Ogre2FrustumVisual::SetVisible(bool _visible) +{ + this->dataPtr->visible = _visible; + this->ogreNode->setVisible(this->dataPtr->visible); +} diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 2d164e235..22d172593 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -53,6 +53,7 @@ #include "gz/rendering/ogre2/Ogre2Light.hh" #include "gz/rendering/ogre2/Ogre2LightVisual.hh" #include "gz/rendering/ogre2/Ogre2LidarVisual.hh" +#include "gz/rendering/ogre2/Ogre2FrustumVisual.hh" #include "gz/rendering/ogre2/Ogre2Marker.hh" #include "gz/rendering/ogre2/Ogre2Material.hh" #include "gz/rendering/ogre2/Ogre2MeshFactory.hh" @@ -1344,6 +1345,15 @@ LidarVisualPtr Ogre2Scene::CreateLidarVisualImpl(unsigned int _id, return (result) ? lidar: nullptr; } +////////////////////////////////////////////////// +FrustumVisualPtr Ogre2Scene::CreateFrustumVisualImpl(unsigned int _id, + const std::string &_name) +{ + Ogre2FrustumVisualPtr frustum(new Ogre2FrustumVisual); + bool result = this->InitObject(frustum, _id, _name); + return (result) ? frustum: nullptr; +} + ////////////////////////////////////////////////// TextPtr Ogre2Scene::CreateTextImpl(unsigned int /*_id*/, const std::string &/*_name*/) diff --git a/src/FrustumVisual.cc b/src/FrustumVisual.cc new file mode 100644 index 000000000..a1021142f --- /dev/null +++ b/src/FrustumVisual.cc @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2020 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +#include "gz/rendering/FrustumVisual.hh" + +using namespace gz; +using namespace rendering; + +////////////////////////////////////////////////// +FrustumVisual::FrustumVisual() = default; + +////////////////////////////////////////////////// +FrustumVisual::~FrustumVisual() = default; diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index b65112fb3..2a03743c7 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -30,6 +30,7 @@ #include "gz/rendering/InstallationDirectories.hh" #include "gz/rendering/JointVisual.hh" #include "gz/rendering/LidarVisual.hh" +#include "gz/rendering/FrustumVisual.hh" #include "gz/rendering/LightVisual.hh" #include "gz/rendering/Camera.hh" #include "gz/rendering/Capsule.hh" @@ -1262,6 +1263,36 @@ LidarVisualPtr BaseScene::CreateLidarVisual(unsigned int _id, return (result) ? lidar : nullptr; } +////////////////////////////////////////////////// +FrustumVisualPtr BaseScene::CreateFrustumVisual() +{ + unsigned int objId = this->CreateObjectId(); + return this->CreateFrustumVisual(objId); +} + +////////////////////////////////////////////////// +FrustumVisualPtr BaseScene::CreateFrustumVisual(unsigned int _id) +{ + const std::string objName = this->CreateObjectName(_id, "FrustumVisual"); + return this->CreateFrustumVisual(_id, objName); +} + +////////////////////////////////////////////////// +FrustumVisualPtr BaseScene::CreateFrustumVisual(const std::string &_name) +{ + unsigned int objId = this->CreateObjectId(); + return this->CreateFrustumVisual(objId, _name); +} + +////////////////////////////////////////////////// +FrustumVisualPtr BaseScene::CreateFrustumVisual(unsigned int _id, + const std::string &_name) +{ + FrustumVisualPtr frustum = this->CreateFrustumVisualImpl(_id, _name); + bool result = this->RegisterVisual(frustum); + return (result) ? frustum : nullptr; +} + ////////////////////////////////////////////////// WireBoxPtr BaseScene::CreateWireBox() { From bdc9edf6b109d1da5452309747bdd7635e6c8132 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Fri, 27 Dec 2024 14:49:32 +0530 Subject: [PATCH 4/9] Update FrustumVisual Signed-off-by: Utkarsh --- include/gz/rendering/FrustumVisual.hh | 34 ++----------------- .../gz/rendering/base/BaseFrustumVisual.hh | 2 +- ogre2/src/Ogre2FrustumVisual.cc | 5 +++ src/FrustumVisual.cc | 2 +- 4 files changed, 9 insertions(+), 34 deletions(-) diff --git a/include/gz/rendering/FrustumVisual.hh b/include/gz/rendering/FrustumVisual.hh index bd1771a30..0d49eec3f 100644 --- a/include/gz/rendering/FrustumVisual.hh +++ b/include/gz/rendering/FrustumVisual.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Open Source Robotics Foundation + * Copyright (C) 2024 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,24 +72,7 @@ namespace gz /// \brief Destructor public: virtual ~FrustumVisual(); - /// \brief Constructor - /// \param[in] _near Near plane distance. This is the distance from - /// the frustum's vertex to the closest plane - /// \param[in] _far Far plane distance. This is the distance from the - /// frustum's vertex to the farthest plane. - /// \param[in] _fov Field of view. The field of view is the - /// angle between the frustum's vertex and the edges of the near or far - /// plane. This value represents the horizontal angle. - /// \param[in] _aspectRatio The aspect ratio, which is the width divided - /// by height of the near or far planes. - /// \param[in] _pose Pose of the frustum, which is the vertex (top of - /// the pyramid). - /*protected: FrustumVisual(double _near, - double _far, - const math::Angle &_fov, - double _aspectRatio, - const gz::math::Pose3d &_pose = gz::math::Pose3d::Zero);*/ - + /// \brief Update the Visual public: virtual void Update() = 0; /// \brief Get the near distance. This is the distance from the @@ -147,16 +130,6 @@ namespace gz /// \return Plane of the frustum. public: virtual gz::math::Planed Plane(const FrustumVisualPlane _plane) const = 0; - /// \brief Check if a box lies inside the pyramid frustum. - /// \param[in] _b Box to check. - /// \return True if the box is inside the pyramid frustum. - //public: virtual bool Contains(const gz::math::AxisAlignedBox &_b) const = 0; //TO-DO - - /// \brief Check if a point lies inside the pyramid frustum. - /// \param[in] _p Point to check. - /// \return True if the point is inside the pyramid frustum. - //public: virtual bool Contains(const gz::math::Vector3d &_p) const = 0; //TO-DO - /// \brief Get the pose of the frustum /// \return Pose of the frustum /// \sa SetPose @@ -170,9 +143,6 @@ namespace gz /// \brief Compute the planes of the frustum. This is called whenever /// a property of the frustum is changed. private: void ComputePlanes(); - - /// \brief Private data pointer - //GZ_UTILS_IMPL_PTR(dataPtr) }; } } diff --git a/include/gz/rendering/base/BaseFrustumVisual.hh b/include/gz/rendering/base/BaseFrustumVisual.hh index 875156ec4..fbacb04e9 100644 --- a/include/gz/rendering/base/BaseFrustumVisual.hh +++ b/include/gz/rendering/base/BaseFrustumVisual.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Open Source Robotics Foundation + * Copyright (C) 2024 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/ogre2/src/Ogre2FrustumVisual.cc b/ogre2/src/Ogre2FrustumVisual.cc index 7669bcd8f..cae4dae21 100644 --- a/ogre2/src/Ogre2FrustumVisual.cc +++ b/ogre2/src/Ogre2FrustumVisual.cc @@ -25,6 +25,11 @@ #endif #endif +#include +#include +#include +#include + #include #include "gz/rendering/ogre2/Ogre2Conversions.hh" diff --git a/src/FrustumVisual.cc b/src/FrustumVisual.cc index a1021142f..825f2851f 100644 --- a/src/FrustumVisual.cc +++ b/src/FrustumVisual.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Open Source Robotics Foundation + * Copyright (C) 2024 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 5c86f0682c1880feb1ff6c73b9a7a625189f904b Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Sat, 11 Jan 2025 15:51:18 +0530 Subject: [PATCH 5/9] Code formatting and Copyright updates Signed-off-by: Utkarsh --- include/gz/rendering/FrustumVisual.hh | 5 +- include/gz/rendering/Scene.hh | 3 +- .../gz/rendering/base/BaseFrustumVisual.hh | 16 ++-- include/gz/rendering/base/BaseScene.hh | 4 +- .../gz/rendering/ogre/OgreFrustumVisual.hh | 2 +- ogre/include/gz/rendering/ogre/OgreScene.hh | 4 +- ogre/src/OgreFrustumVisual.cc | 11 +-- .../gz/rendering/ogre2/Ogre2FrustumVisual.hh | 2 +- .../include/gz/rendering/ogre2/Ogre2Scene.hh | 4 +- ogre2/src/Ogre2FrustumVisual.cc | 79 ++++++++++++------- src/FrustumVisual.cc | 2 +- 11 files changed, 75 insertions(+), 57 deletions(-) diff --git a/include/gz/rendering/FrustumVisual.hh b/include/gz/rendering/FrustumVisual.hh index 0d49eec3f..25519a11a 100644 --- a/include/gz/rendering/FrustumVisual.hh +++ b/include/gz/rendering/FrustumVisual.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Open Source Robotics Foundation + * Copyright (C) 2025 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -128,7 +128,8 @@ namespace gz /// \brief Get a plane of the frustum. /// \param[in] _plane The plane to return. /// \return Plane of the frustum. - public: virtual gz::math::Planed Plane(const FrustumVisualPlane _plane) const = 0; + public: virtual gz::math::Planed Plane( + const FrustumVisualPlane _plane) const = 0; /// \brief Get the pose of the frustum /// \return Pose of the frustum diff --git a/include/gz/rendering/Scene.hh b/include/gz/rendering/Scene.hh index bf6b11395..71f2467da 100644 --- a/include/gz/rendering/Scene.hh +++ b/include/gz/rendering/Scene.hh @@ -1118,7 +1118,8 @@ namespace gz /// ID is already in use, NULL will be returned. /// \param[in] _id ID of the new frustum visual /// \return The created frustum visual - public: virtual FrustumVisualPtr CreateFrustumVisual(unsigned int _id) = 0; + public: virtual FrustumVisualPtr CreateFrustumVisual( + unsigned int _id) = 0; /// \brief Create new frustum visual with the given name. A unique ID /// will automatically be assigned to the frustum visual. If the given diff --git a/include/gz/rendering/base/BaseFrustumVisual.hh b/include/gz/rendering/base/BaseFrustumVisual.hh index fbacb04e9..9fef37b72 100644 --- a/include/gz/rendering/base/BaseFrustumVisual.hh +++ b/include/gz/rendering/base/BaseFrustumVisual.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Open Source Robotics Foundation + * Copyright (C) 2025 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -78,13 +78,8 @@ namespace gz public: virtual void SetAspectRatio(double _aspectRatio) override; // Documentation inherited - public: virtual gz::math::Planed Plane(const FrustumVisualPlane _plane) const override; - - // Documentation inherited - //public: virtual bool Contains(const gz::math::AxisAlignedBox &_b) const override; - - // Documentation inherited - //public: virtual bool Contains(const gz::math::Vector3d &_p) const override; + public: virtual gz::math::Planed Plane( + const FrustumVisualPlane _plane) const override; // Documentation inherited public: virtual gz::math::Pose3d Pose() const override; @@ -219,7 +214,8 @@ namespace gz ///////////////////////////////////////////////// template - gz::math::Planed BaseFrustumVisual::Plane(const FrustumVisualPlane _plane) const + gz::math::Planed BaseFrustumVisual::Plane( + const FrustumVisualPlane _plane) const { return this->planes[_plane]; } @@ -235,7 +231,7 @@ namespace gz template gz::math::Pose3d BaseFrustumVisual::Pose() const { - return this->pose; + return this->pose; } ///////////////////////////////////////////////// template diff --git a/include/gz/rendering/base/BaseScene.hh b/include/gz/rendering/base/BaseScene.hh index b92fd1449..7ec3b8688 100644 --- a/include/gz/rendering/base/BaseScene.hh +++ b/include/gz/rendering/base/BaseScene.hh @@ -847,8 +847,8 @@ namespace gz /// \param[in] _id unique object id. /// \param[in] _name unique object name. /// \return Pointer to a frustum visual - protected: virtual FrustumVisualPtr CreateFrustumVisualImpl(unsigned int _id, - const std::string &_name) = 0; + protected: virtual FrustumVisualPtr CreateFrustumVisualImpl( + unsigned int _id, const std::string &_name) = 0; /// \brief Implementation for creating a heightmap geometry /// \param[in] _id Unique object id. diff --git a/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh b/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh index ede578b37..3b7ea484a 100644 --- a/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh +++ b/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Open Source Robotics Foundation + * Copyright (C) 2025 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/ogre/include/gz/rendering/ogre/OgreScene.hh b/ogre/include/gz/rendering/ogre/OgreScene.hh index 19f14f368..bebc39dd9 100644 --- a/ogre/include/gz/rendering/ogre/OgreScene.hh +++ b/ogre/include/gz/rendering/ogre/OgreScene.hh @@ -181,8 +181,8 @@ namespace gz const std::string &_name) override; // Documentation inherited - protected: virtual FrustumVisualPtr CreateFrustumVisualImpl(unsigned int _id, - const std::string &_name) override; + protected: virtual FrustumVisualPtr CreateFrustumVisualImpl( + unsigned int _id, const std::string &_name) override; // Documentation inherited protected: virtual WireBoxPtr CreateWireBoxImpl(unsigned int _id, diff --git a/ogre/src/OgreFrustumVisual.cc b/ogre/src/OgreFrustumVisual.cc index abe7c1b79..ed76b1067 100644 --- a/ogre/src/OgreFrustumVisual.cc +++ b/ogre/src/OgreFrustumVisual.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Open Source Robotics Foundation + * Copyright (C) 2025 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,8 +29,8 @@ class gz::rendering::OgreFrustumVisualPrivate public: std::vector> rayLines; /// \brief Frustum visual type - //public: FrustumVisualPlane frustumVisPlane = - //FrustumVisualPlane::FRUSTUM_PLANE_TOP; + // public: FrustumVisualPlane frustumVisPlane = + // FrustumVisualPlane::FRUSTUM_PLANE_TOP; /// \brief The visibility of the visual public: bool visible = true; @@ -39,7 +39,8 @@ class gz::rendering::OgreFrustumVisualPrivate public: std::array points; /// \brief each edge of the frustum. - public: std::array, 12> edges; + public: std::array, 12> edges; }; using namespace gz; @@ -91,7 +92,7 @@ void OgreFrustumVisual::ClearVisualData() ////////////////////////////////////////////////// void OgreFrustumVisual::Update() { - //no ops FIX-ME + // no ops } ////////////////////////////////////////////////// diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh b/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh index f0c63cec0..ad67cf85b 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Open Source Robotics Foundation + * Copyright (C) 2025 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh index 4c2e5d1ef..5be78d9c1 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2Scene.hh @@ -344,8 +344,8 @@ namespace gz const std::string &_name) override; // Documentation inherited - protected: virtual FrustumVisualPtr CreateFrustumVisualImpl(unsigned int _id, - const std::string &_name) override; + protected: virtual FrustumVisualPtr CreateFrustumVisualImpl( + unsigned int _id, const std::string &_name) override; // Documentation inherited protected: virtual WireBoxPtr CreateWireBoxImpl(unsigned int _id, diff --git a/ogre2/src/Ogre2FrustumVisual.cc b/ogre2/src/Ogre2FrustumVisual.cc index cae4dae21..8b93d3a49 100644 --- a/ogre2/src/Ogre2FrustumVisual.cc +++ b/ogre2/src/Ogre2FrustumVisual.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Open Source Robotics Foundation + * Copyright (C) 2025 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,8 +58,8 @@ class gz::rendering::Ogre2FrustumVisualPrivate public: std::vector> rayLines; /// \brief Frustum visual type - //public: FrustumVisualPlane frustumVisPlane = - //FrustumVisualPlane::FRUSTUM_PLANE_TOP; + // public: FrustumVisualPlane frustumVisPlane = + // FrustumVisualPlane::FRUSTUM_PLANE_TOP; /// \brief The visibility of the visual public: bool visible = true; @@ -68,7 +68,8 @@ class gz::rendering::Ogre2FrustumVisualPrivate public: std::array points; /// \brief each edge of the frustum. - public: std::array, 12> edges; + public: std::array, 12> edges; }; using namespace gz; @@ -148,10 +149,9 @@ void Ogre2FrustumVisual::Update() #if (OGRE_VERSION <= ((1 << 16) | (10 << 8) | 7)) MaterialPtr mat = this->Scene()->Material("Frustum/BlueRay"); #else - //MaterialPtr mat = this->Scene()->Material(rayLineMat); //FIX-ME MaterialPtr mat = this->Scene()->Material("Frustum/BlueRay"); #endif - + renderable->SetMaterial(mat, false); renderable->SetOperationType(MT_LINE_LIST); this->dataPtr->rayLines.push_back(renderable); @@ -172,9 +172,12 @@ void Ogre2FrustumVisual::Update() double farHeight = farWidth / this->aspectRatio; // Up, right, and forward unit vectors. - gz::math::Vector3d forward = this->pose.Rot().RotateVector(gz::math::Vector3d::UnitX); - gz::math::Vector3d up = this->pose.Rot().RotateVector(gz::math::Vector3d::UnitZ); - gz::math::Vector3d right = this->pose.Rot().RotateVector(-gz::math::Vector3d::UnitY); + gz::math::Vector3d forward = + this->pose.Rot().RotateVector(gz::math::Vector3d::UnitX); + gz::math::Vector3d up = + this->pose.Rot().RotateVector(gz::math::Vector3d::UnitZ); + gz::math::Vector3d right = + this->pose.Rot().RotateVector(-gz::math::Vector3d::UnitY); // Near plane center gz::math::Vector3d nearCenter = this->pose.Pos() + forward * this->near; @@ -189,10 +192,14 @@ void Ogre2FrustumVisual::Update() gz::math::Vector3d rightFarWidth2 = right * (farWidth * 0.5); // Compute the vertices of the near plane - gz::math::Vector3d nearTopLeft = nearCenter + upNearHeight2 - rightNearWidth2; - gz::math::Vector3d nearTopRight = nearCenter + upNearHeight2 + rightNearWidth2; - gz::math::Vector3d nearBottomLeft = nearCenter - upNearHeight2 - rightNearWidth2; - gz::math::Vector3d nearBottomRight = nearCenter - upNearHeight2 + rightNearWidth2; + gz::math::Vector3d nearTopLeft = + nearCenter + upNearHeight2 - rightNearWidth2; + gz::math::Vector3d nearTopRight = + nearCenter + upNearHeight2 + rightNearWidth2; + gz::math::Vector3d nearBottomLeft = + nearCenter - upNearHeight2 - rightNearWidth2; + gz::math::Vector3d nearBottomRight = + nearCenter - upNearHeight2 + rightNearWidth2; // Compute the vertices of the far plane gz::math::Vector3d farTopLeft = farCenter + upFarHeight2 - rightFarWidth2; @@ -265,23 +272,35 @@ void Ogre2FrustumVisual::Update() // Compute plane offsets // Set the planes, where the first value is the plane normal and the // second the plane offset - gz::math::Vector3d norm = gz::math::Vector3d::Normal(nearTopLeft, nearTopRight, nearBottomLeft); - this->planes[FrustumVisualPlane::FRUSTUM_PLANE_NEAR].Set(norm, nearCenter.Dot(norm)); - - norm = gz::math::Vector3d::Normal(farTopRight, farTopLeft, farBottomLeft); - this->planes[FrustumVisualPlane::FRUSTUM_PLANE_FAR].Set(norm, farCenter.Dot(norm)); - - norm = gz::math::Vector3d::Normal(farTopLeft, nearTopLeft, nearBottomLeft); - this->planes[FrustumVisualPlane::FRUSTUM_PLANE_LEFT].Set(norm, leftCenter.Dot(norm)); - - norm = gz::math::Vector3d::Normal(nearTopRight, farTopRight, farBottomRight); - this->planes[FrustumVisualPlane::FRUSTUM_PLANE_RIGHT].Set(norm, rightCenter.Dot(norm)); - - norm = gz::math::Vector3d::Normal(nearTopLeft, farTopLeft, nearTopRight); - this->planes[FrustumVisualPlane::FRUSTUM_PLANE_TOP].Set(norm, topCenter.Dot(norm)); - - norm = gz::math::Vector3d::Normal(nearBottomLeft, nearBottomRight, farBottomRight); - this->planes[FrustumVisualPlane::FRUSTUM_PLANE_BOTTOM].Set(norm, bottomCenter.Dot(norm)); + gz::math::Vector3d norm = gz::math::Vector3d::Normal( + nearTopLeft, nearTopRight, nearBottomLeft); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_NEAR].Set( + norm, nearCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal( + farTopRight, farTopLeft, farBottomLeft); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_FAR].Set( + norm, farCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal( + farTopLeft, nearTopLeft, nearBottomLeft); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_LEFT].Set( + norm, leftCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal( + nearTopRight, farTopRight, farBottomRight); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_RIGHT].Set( + norm, rightCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal( + nearTopLeft, farTopLeft, nearTopRight); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_TOP].Set( + norm, topCenter.Dot(norm)); + + norm = gz::math::Vector3d::Normal( + nearBottomLeft, nearBottomRight, farBottomRight); + this->planes[FrustumVisualPlane::FRUSTUM_PLANE_BOTTOM].Set( + norm, bottomCenter.Dot(norm)); renderable->Update(); this->SetVisible(this->dataPtr->visible); diff --git a/src/FrustumVisual.cc b/src/FrustumVisual.cc index 825f2851f..9c057dec2 100644 --- a/src/FrustumVisual.cc +++ b/src/FrustumVisual.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Open Source Robotics Foundation + * Copyright (C) 2025 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 08ebe929f83f727b8921362423e8088ba5be3553 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 15 Jan 2025 18:32:08 +0000 Subject: [PATCH 6/9] Update function and variable naming, add demo Signed-off-by: Ian Chen --- examples/frustum_visual/CMakeLists.txt | 28 ++ examples/frustum_visual/GlutWindow.cc | 393 ++++++++++++++++++ examples/frustum_visual/GlutWindow.hh | 31 ++ examples/frustum_visual/Main.cc | 180 ++++++++ include/gz/rendering/FrustumVisual.hh | 38 +- .../gz/rendering/base/BaseFrustumVisual.hh | 60 +-- .../gz/rendering/ogre/OgreFrustumVisual.hh | 2 +- ogre/src/OgreFrustumVisual.cc | 3 + .../gz/rendering/ogre2/Ogre2FrustumVisual.hh | 2 +- ogre2/src/Ogre2FrustumVisual.cc | 3 +- 10 files changed, 675 insertions(+), 65 deletions(-) create mode 100644 examples/frustum_visual/CMakeLists.txt create mode 100644 examples/frustum_visual/GlutWindow.cc create mode 100644 examples/frustum_visual/GlutWindow.hh create mode 100644 examples/frustum_visual/Main.cc diff --git a/examples/frustum_visual/CMakeLists.txt b/examples/frustum_visual/CMakeLists.txt new file mode 100644 index 000000000..675e5081a --- /dev/null +++ b/examples/frustum_visual/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.22.1 FATAL_ERROR) +project(gz-rendering-frustum-visual) +find_package(gz-rendering10 REQUIRED) + +find_package(GLUT REQUIRED) +include_directories(SYSTEM ${GLUT_INCLUDE_DIRS}) +link_directories(${GLUT_LIBRARY_DIRS}) + +find_package(OpenGL REQUIRED) +include_directories(SYSTEM ${OpenGL_INCLUDE_DIRS}) +link_directories(${OpenGL_LIBRARY_DIRS}) + +if (NOT APPLE) + find_package(GLEW REQUIRED) + include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) + link_directories(${GLEW_LIBRARY_DIRS}) +endif() + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations") + +add_executable(frustum_visual Main.cc GlutWindow.cc) + +target_link_libraries(frustum_visual + ${GLUT_LIBRARIES} + ${OPENGL_LIBRARIES} + ${GLEW_LIBRARIES} + ${GZ-RENDERING_LIBRARIES} +) diff --git a/examples/frustum_visual/GlutWindow.cc b/examples/frustum_visual/GlutWindow.cc new file mode 100644 index 000000000..4b1a93de3 --- /dev/null +++ b/examples/frustum_visual/GlutWindow.cc @@ -0,0 +1,393 @@ +/* + * Copyright (C) 2025 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#if __APPLE__ + #include + #include + #include +#else + #include + #include + #include +#endif + +#if !defined(__APPLE__) && !defined(_WIN32) + #include +#endif + +#include + +#include +#include +#include +#include +#include +#include + +#include "GlutWindow.hh" + +#define KEY_ESC 27 +#define KEY_TAB 9 + +////////////////////////////////////////////////// +unsigned int imgw = 0; +unsigned int imgh = 0; + +std::vector g_cameras; +ir::CameraPtr g_camera; +ir::CameraPtr g_currCamera; +unsigned int g_cameraIndex = 0; +ir::ImagePtr g_image; + + +std::vector g_nodes; + +bool g_initContext = false; + +#if __APPLE__ + CGLContextObj g_context; + CGLContextObj g_glutContext; +#elif _WIN32 +#else + GLXContext g_context; + Display *g_display; + GLXDrawable g_drawable; + GLXContext g_glutContext; + Display *g_glutDisplay; + GLXDrawable g_glutDrawable; +#endif + +// view control variables +ir::RayQueryPtr g_rayQuery; +ir::OrbitViewController g_viewControl; +ir::RayQueryResult g_target; +struct mouseButton +{ + int button = 0; + int state = GLUT_UP; + int x = 0; + int y = 0; + int motionX = 0; + int motionY = 0; + int dragX = 0; + int dragY = 0; + int scroll = 0; + bool buttonDirty = false; + bool motionDirty = false; +}; +struct mouseButton g_mouse; +std::mutex g_mouseMutex; + +////////////////////////////////////////////////// +void mouseCB(int _button, int _state, int _x, int _y) +{ + // ignore unknown mouse button numbers + if (_button >= 5) + return; + + std::lock_guard lock(g_mouseMutex); + g_mouse.button = _button; + g_mouse.state = _state; + g_mouse.x = _x; + g_mouse.y = _y; + g_mouse.motionX = _x; + g_mouse.motionY = _y; + g_mouse.buttonDirty = true; +} + +////////////////////////////////////////////////// +void motionCB(int _x, int _y) +{ + std::lock_guard lock(g_mouseMutex); + int deltaX = _x - g_mouse.motionX; + int deltaY = _y - g_mouse.motionY; + g_mouse.motionX = _x; + g_mouse.motionY = _y; + + if (g_mouse.motionDirty) + { + g_mouse.dragX += deltaX; + g_mouse.dragY += deltaY; + } + else + { + g_mouse.dragX = deltaX; + g_mouse.dragY = deltaY; + } + g_mouse.motionDirty = true; +} + +////////////////////////////////////////////////// +void handleMouse() +{ + std::lock_guard lock(g_mouseMutex); + // only ogre supports ray query for now so use + // ogre camera located at camera index = 0. + ir::CameraPtr rayCamera = g_cameras[0]; + if (!g_rayQuery) + { + g_rayQuery = rayCamera->Scene()->CreateRayQuery(); + if (!g_rayQuery) + { + gzerr << "Failed to create Ray Query" << std::endl; + return; + } + } + if (g_mouse.buttonDirty) + { + g_mouse.buttonDirty = false; + double nx = + 2.0 * g_mouse.x / static_cast(rayCamera->ImageWidth()) - 1.0; + double ny = 1.0 - + 2.0 * g_mouse.y / static_cast(rayCamera->ImageHeight()); + g_rayQuery->SetFromCamera(rayCamera, gz::math::Vector2d(nx, ny)); + g_target = g_rayQuery->ClosestPoint(); + if (!g_target) + { + // set point to be 10m away if no intersection found + g_target.point = g_rayQuery->Origin() + g_rayQuery->Direction() * 10; + return; + } + + // mouse wheel scroll zoom + if ((g_mouse.button == 3 || g_mouse.button == 4) && + g_mouse.state == GLUT_UP) + { + double scroll = (g_mouse.button == 3) ? -1.0 : 1.0; + double distance = rayCamera->WorldPosition().Distance( + g_target.point); + int factor = 1; + double amount = -(scroll * factor) * (distance / 5.0); + for (ir::CameraPtr camera : g_cameras) + { + g_viewControl.SetCamera(camera); + g_viewControl.SetTarget(g_target.point); + g_viewControl.Zoom(amount); + } + } + } + + if (g_mouse.motionDirty) + { + g_mouse.motionDirty = false; + auto drag = gz::math::Vector2d(g_mouse.dragX, g_mouse.dragY); + + // left mouse button pan + if (g_mouse.button == GLUT_LEFT_BUTTON && g_mouse.state == GLUT_DOWN) + { + for (ir::CameraPtr camera : g_cameras) + { + g_viewControl.SetCamera(camera); + g_viewControl.SetTarget(g_target.point); + g_viewControl.Pan(drag); + } + } + else if (g_mouse.button == GLUT_MIDDLE_BUTTON && g_mouse.state == GLUT_DOWN) + { + for (ir::CameraPtr camera : g_cameras) + { + g_viewControl.SetCamera(camera); + g_viewControl.SetTarget(g_target.point); + g_viewControl.Orbit(drag); + } + } + // right mouse button zoom + else if (g_mouse.button == GLUT_RIGHT_BUTTON && g_mouse.state == GLUT_DOWN) + { + double hfov = rayCamera->HFOV().Radian(); + double vfov = 2.0f * atan(tan(hfov / 2.0f) / + rayCamera->AspectRatio()); + double distance = rayCamera->WorldPosition().Distance( + g_target.point); + double amount = ((-g_mouse.dragY / + static_cast(rayCamera->ImageHeight())) + * distance * tan(vfov/2.0) * 6.0); + for (ir::CameraPtr camera : g_cameras) + { + g_viewControl.SetCamera(camera); + g_viewControl.SetTarget(g_target.point); + g_viewControl.Zoom(amount); + } + } + } +} + + +////////////////////////////////////////////////// +void displayCB() +{ +#if __APPLE__ + CGLSetCurrentContext(g_context); +#elif _WIN32 +#else + if (g_display) + { + glXMakeCurrent(g_display, g_drawable, g_context); + } +#endif + + g_cameras[g_cameraIndex]->Capture(*g_image); + handleMouse(); + +#if __APPLE__ + CGLSetCurrentContext(g_glutContext); +#elif _WIN32 +#else + glXMakeCurrent(g_glutDisplay, g_glutDrawable, g_glutContext); +#endif + + unsigned char *data = g_image->Data(); + + glClearColor(0.5, 0.5, 0.5, 1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glPixelZoom(1, -1); + glRasterPos2f(-1, 1); + glDrawPixels(imgw, imgh, GL_RGB, GL_UNSIGNED_BYTE, data); + + glutSwapBuffers(); +} + +////////////////////////////////////////////////// +void idleCB() +{ + glutPostRedisplay(); +} + +////////////////////////////////////////////////// +void keyboardCB(unsigned char _key, int, int) +{ + if (_key == KEY_ESC || _key == 'q' || _key == 'Q') + { + exit(0); + } + else if (_key == KEY_TAB) + { + g_cameraIndex = (g_cameraIndex + 1) % g_cameras.size(); + } + + // main node movement control + double posIncr = 0.03; + double yawIncr = 0.03; + for (ir::NodePtr node : g_nodes) + { + if (!node) + { + std::cerr << "Main node not found! " << std::endl; + return; + } + if (_key == 'w' || _key == 'W') + { + node->SetWorldPosition(node->WorldPosition() + + node->WorldRotation() * gz::math::Vector3d(posIncr, 0, 0)); + } + else if (_key == 's' || _key == 'S') + { + node->SetWorldPosition(node->WorldPosition() + + node->WorldRotation() * gz::math::Vector3d(-posIncr, 0, 0)); + } + else if (_key == 'a' || _key == 'A') + { + node->SetWorldRotation(gz::math::Quaterniond(0, 0, + node->WorldRotation().Yaw() + yawIncr)); + } + else if (_key == 'd' || _key == 'D') + { + node->SetWorldRotation(gz::math::Quaterniond(0, 0, + node->WorldRotation().Yaw() - yawIncr)); + } + } +} + +////////////////////////////////////////////////// +void initCamera(ir::CameraPtr _camera) +{ + g_camera = _camera; + imgw = g_camera->ImageWidth(); + imgh = g_camera->ImageHeight(); + ir::Image image = g_camera->CreateImage(); + g_image = std::make_shared(image); + g_camera->Capture(*g_image); +} + +////////////////////////////////////////////////// +void initContext() +{ + glutInitDisplayMode(GLUT_DOUBLE); + glutInitWindowPosition(0, 0); + glutInitWindowSize(imgw, imgh); + glutCreateWindow("Frustum Visual"); + glutDisplayFunc(displayCB); + glutIdleFunc(idleCB); + glutKeyboardFunc(keyboardCB); + + glutMouseFunc(mouseCB); + glutMotionFunc(motionCB); +} + +////////////////////////////////////////////////// +void printUsage() +{ + std::cout << "===============================" << std::endl; + std::cout << " TAB - Switch render engines " << std::endl; + std::cout << " ESC - Exit " << std::endl; + std::cout << " " << std::endl; + std::cout << " W: Move box forward " << std::endl; + std::cout << " S: Move box backward " << std::endl; + std::cout << " A: Rotate box to the left " << std::endl; + std::cout << " D: Rotate box to the right " << std::endl; + std::cout << "===============================" << std::endl; +} + +////////////////////////////////////////////////// +void run(std::vector _cameras, + const std::vector &_nodes) +{ + if (_cameras.empty()) + { + gzerr << "No cameras found. Scene will not be rendered" << std::endl; + return; + } + +#if __APPLE__ + g_context = CGLGetCurrentContext(); +#elif _WIN32 +#else + g_context = glXGetCurrentContext(); + g_display = glXGetCurrentDisplay(); + g_drawable = glXGetCurrentDrawable(); +#endif + + g_cameras = _cameras; + initCamera(_cameras[0]); + + // main node to track + g_nodes = _nodes; + + initContext(); + printUsage(); + +#if __APPLE__ + g_glutContext = CGLGetCurrentContext(); +#elif _WIN32 +#else + g_glutDisplay = glXGetCurrentDisplay(); + g_glutDrawable = glXGetCurrentDrawable(); + g_glutContext = glXGetCurrentContext(); +#endif + + glutMainLoop(); +} diff --git a/examples/frustum_visual/GlutWindow.hh b/examples/frustum_visual/GlutWindow.hh new file mode 100644 index 000000000..8a4abcb52 --- /dev/null +++ b/examples/frustum_visual/GlutWindow.hh @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2025 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef GZ_RENDERING_EXAMPLES_FRUSTUM_VISUAL_GLUTWINDOW_HH_ +#define GZ_RENDERING_EXAMPLES_FRUSTUM_VISUAL_GLUTWINDOW_HH_ + +#include +#include + +namespace ir = gz::rendering; + +/// \brief Run the demo and render the scene from the cameras +/// \param[in] _cameras Cameras in the scene +/// \param[in] _nodes Nodes being tracked / followed in the scene +void run(std::vector _cameras, + const std::vector &_nodes); + +#endif diff --git a/examples/frustum_visual/Main.cc b/examples/frustum_visual/Main.cc new file mode 100644 index 000000000..f05e8edef --- /dev/null +++ b/examples/frustum_visual/Main.cc @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2025 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#if defined(__APPLE__) + #include + #include +#elif not defined(_WIN32) + #include + #include + #include +#endif + +#include +#include + +#include +#include +#include "GlutWindow.hh" + +using namespace gz; +using namespace rendering; + +////////////////////////////////////////////////// +void buildScene(ScenePtr _scene) +{ + // initialize _scene + _scene->SetAmbientLight(0.3, 0.3, 0.3); + VisualPtr root = _scene->RootVisual(); + + // create directional light + DirectionalLightPtr light0 = _scene->CreateDirectionalLight(); + light0->SetDirection(-0.5, 0.5, -1); + light0->SetDiffuseColor(0.5, 0.5, 0.5); + light0->SetSpecularColor(0.5, 0.5, 0.5); + root->AddChild(light0); + + // create grid visual + VisualPtr grid = _scene->CreateVisual(); + GridPtr gridGeom = _scene->CreateGrid(); + gridGeom->SetCellCount(20); + gridGeom->SetCellLength(1); + gridGeom->SetVerticalCellCount(0); + grid->AddGeometry(gridGeom); + grid->SetLocalPosition(3, 0, 0.0); + root->AddChild(grid); + + // create camera + CameraPtr camera = _scene->CreateCamera("camera"); + camera->SetLocalPosition(0.0, 0.0, 3.0); + camera->SetLocalRotation(0.0, 0.0, 0.0); + camera->SetImageWidth(800); + camera->SetImageHeight(600); + camera->SetAntiAliasing(2); + camera->SetAspectRatio(1.333); + camera->SetHFOV(GZ_PI / 2); + root->AddChild(camera); +} + + +////////////////////////////////////////////////// +NodePtr createMainNode(ScenePtr _scene) +{ + // create green material + MaterialPtr green = _scene->CreateMaterial(); + green->SetAmbient(0.0, 0.5, 0.0); + green->SetDiffuse(0.0, 0.7, 0.0); + green->SetSpecular(0.5, 0.5, 0.5); + green->SetShininess(50); + green->SetReflectivity(0); + + // create box visual + VisualPtr box = _scene->CreateVisual(); + box->AddGeometry(_scene->CreateBox()); + box->SetLocalPosition(3, 0, 0); + box->SetMaterial(green); + + VisualPtr root = _scene->RootVisual(); + root->AddChild(box); + + // create frustum visual and attach to main node + FrustumVisualPtr frustumVisual = _scene->CreateFrustumVisual(); + frustumVisual->SetNearClipPlane(1); + frustumVisual->SetFarClipPlane(5); + frustumVisual->SetHFOV(0.7); + frustumVisual->Update(); + box->AddChild(frustumVisual); + + return std::dynamic_pointer_cast(box); +} + +////////////////////////////////////////////////// +CameraPtr createCamera(const std::string &_engineName, + const std::map& _params) +{ + // create and populate scene + RenderEngine *engine = rendering::engine(_engineName, _params); + if (!engine) + { + std::cout << "Engine '" << _engineName + << "' is not supported" << std::endl; + return CameraPtr(); + } + ScenePtr scene = engine->CreateScene("scene"); + buildScene(scene); + + // return camera sensor + SensorPtr sensor = scene->SensorByName("camera"); + return std::dynamic_pointer_cast(sensor); +} + +////////////////////////////////////////////////// +int main(int _argc, char** _argv) +{ + glutInit(&_argc, _argv); + + // Expose engine name to command line because we can't instantiate both + // ogre and ogre2 at the same time + std::string ogreEngineName("ogre2"); + if (_argc > 1) + { + ogreEngineName = _argv[1]; + } + + GraphicsAPI graphicsApi = defaultGraphicsAPI(); + if (_argc > 2) + { + graphicsApi = GraphicsAPIUtils::Set(std::string(_argv[2])); + } + + common::Console::SetVerbosity(4); + std::vector engineNames; + std::vector cameras; + std::vector nodes; + + engineNames.push_back(ogreEngineName); + + for (auto engineName : engineNames) + { + try + { + std::map params; + if (engineName.compare("ogre2") == 0 + && graphicsApi == GraphicsAPI::METAL) + { + params["metal"] = "1"; + } + + CameraPtr camera = createCamera(engineName, params); + if (camera) + { + cameras.push_back(camera); + NodePtr node = createMainNode(camera->Scene()); + if (node) + nodes.push_back(node); + camera->SetTrackTarget(node, math::Vector3d(0.5, 0, 0)); + } + } + catch (...) + { + // std::cout << ex.what() << std::endl; + std::cerr << "Error starting up: " << engineName << std::endl; + } + } + run(cameras, nodes); + return 0; +} diff --git a/include/gz/rendering/FrustumVisual.hh b/include/gz/rendering/FrustumVisual.hh index 25519a11a..59552bbe5 100644 --- a/include/gz/rendering/FrustumVisual.hh +++ b/include/gz/rendering/FrustumVisual.hh @@ -64,7 +64,7 @@ namespace gz /// /// * near: 0.0 /// * far: 1.0 - /// * fov: 0.78539 radians (45 degrees) + /// * hfov: 0.78539 radians (45 degrees) /// * aspect ratio: 1.0 /// * pose: Pose3d::Zero protected: FrustumVisual(); @@ -78,40 +78,40 @@ namespace gz /// \brief Get the near distance. This is the distance from the /// frustum's vertex to the closest plane. /// \return Near distance. - /// \sa SetNear - public: virtual double Near() const = 0; + /// \sa SetNearClipPlane + public: virtual double NearClipPlane() const = 0; /// \brief Set the near distance. This is the distance from the /// frustum's vertex to the closest plane. /// \param[in] _near Near distance. - /// \sa Near - public: virtual void SetNear(double _near) = 0; + /// \sa NearClipPlane + public: virtual void SetNearClipPlane(double _near) = 0; /// \brief Get the far distance. This is the distance from the /// frustum's vertex to the farthest plane. /// \return Far distance. - /// \sa SetFar - public: virtual double Far() const = 0; + /// \sa SetFarClipPlane + public: virtual double FarClipPlane() const = 0; /// \brief Set the far distance. This is the distance from the /// frustum's vertex to the farthest plane. /// \param[in] _far Far distance. - /// \sa Far - public: virtual void SetFar(double _far) = 0; + /// \sa FarClipPlane + public: virtual void SetFarClipPlane(double _far) = 0; /// \brief Get the horizontal field of view. The field of view is the /// angle between the frustum's vertex and the edges of the near or far /// plane. This value represents the horizontal angle. /// \return The field of view. - /// \sa SetFOV - public: virtual gz::math::Angle FOV() const = 0; + /// \sa SetHFOV + public: virtual gz::math::Angle HFOV() const = 0; /// \brief Set the horizontal field of view. The field of view is the /// angle between the frustum's vertex and the edges of the near or far /// plane. This value represents the horizontal angle. - /// \param[in] _fov The field of view. - /// \sa FOV - public: virtual void SetFOV(const gz::math::Angle &_fov) = 0; + /// \param[in] _hfov The field of view. + /// \sa HFOV + public: virtual void SetHFOV(const gz::math::Angle &_hfov) = 0; /// \brief Get the aspect ratio, which is the width divided by height /// of the near or far planes. @@ -131,16 +131,6 @@ namespace gz public: virtual gz::math::Planed Plane( const FrustumVisualPlane _plane) const = 0; - /// \brief Get the pose of the frustum - /// \return Pose of the frustum - /// \sa SetPose - public: virtual gz::math::Pose3d Pose() const = 0; - - /// \brief Set the pose of the frustum - /// \param[in] _pose Pose of the frustum, top vertex. - /// \sa Pose - public: virtual void SetPose(const gz::math::Pose3d &_pose) = 0; - /// \brief Compute the planes of the frustum. This is called whenever /// a property of the frustum is changed. private: void ComputePlanes(); diff --git a/include/gz/rendering/base/BaseFrustumVisual.hh b/include/gz/rendering/base/BaseFrustumVisual.hh index 9fef37b72..51e31d5b9 100644 --- a/include/gz/rendering/base/BaseFrustumVisual.hh +++ b/include/gz/rendering/base/BaseFrustumVisual.hh @@ -17,7 +17,7 @@ #ifndef GZ_RENDERING_BASEFRUSTUMVISUAL_HH_ #define GZ_RENDERING_BASEFRUSTUMVISUAL_HH_ -#include +#include #include "gz/rendering/FrustumVisual.hh" #include "gz/rendering/base/BaseObject.hh" @@ -54,22 +54,22 @@ namespace gz public: virtual void Init() override; // Documentation inherited - public: virtual double Near() const override; + public: virtual double NearClipPlane() const override; // Documentation inherited - public: virtual void SetNear(double _near) override; + public: virtual void SetNearClipPlane(double _near) override; // Documentation inherited - public: virtual double Far() const override; + public: virtual double FarClipPlane() const override; // Documentation inherited - public: virtual void SetFar(double _far) override; + public: virtual void SetFarClipPlane(double _far) override; // Documentation inherited - public: virtual math::Angle FOV() const override; + public: virtual math::Angle HFOV() const override; // Documentation inherited - public: virtual void SetFOV(const math::Angle &_fov) override; + public: virtual void SetHFOV(const math::Angle &_hfov) override; // Documentation inherited public: virtual double AspectRatio() const override; @@ -81,11 +81,11 @@ namespace gz public: virtual gz::math::Planed Plane( const FrustumVisualPlane _plane) const override; - // Documentation inherited - public: virtual gz::math::Pose3d Pose() const override; - - // Documentation inherited - public: virtual void SetPose(const gz::math::Pose3d &_pose) override; +// // Documentation inherited +// public: virtual gz::math::Pose3d Pose() const override; +// +// // Documentation inherited +// public: virtual void SetPose(const gz::math::Pose3d &_pose) override; /// \brief Create predefined materials for lidar visual public: virtual void CreateMaterials(); @@ -97,7 +97,7 @@ namespace gz protected: double far = 1.0; /// \brief fov value - protected: gz::math::Angle fov = 0.78539; + protected: gz::math::Angle hfov = 0.78539; /// \brief aspect ratio value protected: double aspectRatio = 1.0; @@ -154,47 +154,45 @@ namespace gz ///////////////////////////////////////////////// template - void BaseFrustumVisual::SetNear( - double _near) + void BaseFrustumVisual::SetNearClipPlane(double _near) { this->near = _near; } ///////////////////////////////////////////////// template - double BaseFrustumVisual::Near() const + double BaseFrustumVisual::NearClipPlane() const { return this->near; } ///////////////////////////////////////////////// template - void BaseFrustumVisual::SetFar( - double _far) + void BaseFrustumVisual::SetFarClipPlane(double _far) { this->far = _far; } ///////////////////////////////////////////////// template - double BaseFrustumVisual::Far() const + double BaseFrustumVisual::FarClipPlane() const { return this->far; } ///////////////////////////////////////////////// template - void BaseFrustumVisual::SetFOV( - const gz::math::Angle &_fov) + void BaseFrustumVisual::SetHFOV( + const gz::math::Angle &_hfov) { - this->fov = _fov; + this->hfov = _hfov; } ///////////////////////////////////////////////// template - gz::math::Angle BaseFrustumVisual::FOV() const + gz::math::Angle BaseFrustumVisual::HFOV() const { - return this->fov; + return this->hfov; } ///////////////////////////////////////////////// @@ -220,19 +218,6 @@ namespace gz return this->planes[_plane]; } - ///////////////////////////////////////////////// - template - void BaseFrustumVisual::SetPose(const gz::math::Pose3d &_pose) - { - this->pose = _pose; - } - - ///////////////////////////////////////////////// - template - gz::math::Pose3d BaseFrustumVisual::Pose() const - { - return this->pose; - } ///////////////////////////////////////////////// template void BaseFrustumVisual::CreateMaterials() @@ -253,7 +238,6 @@ namespace gz mtl->SetMetalness(0.1f); mtl->SetReflectivity(0.2); } - return; } } } diff --git a/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh b/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh index 3b7ea484a..8985484ac 100644 --- a/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh +++ b/ogre/include/gz/rendering/ogre/OgreFrustumVisual.hh @@ -18,8 +18,8 @@ #ifndef GZ_RENDERING_OGRE_OGREFRUSTUMVISUAL_HH_ #define GZ_RENDERING_OGRE_OGREFRUSTUMVISUAL_HH_ -#include #include + #include "gz/rendering/base/BaseFrustumVisual.hh" #include "gz/rendering/ogre/OgreVisual.hh" #include "gz/rendering/ogre/OgreIncludes.hh" diff --git a/ogre/src/OgreFrustumVisual.cc b/ogre/src/OgreFrustumVisual.cc index ed76b1067..a2e8572a7 100644 --- a/ogre/src/OgreFrustumVisual.cc +++ b/ogre/src/OgreFrustumVisual.cc @@ -15,6 +15,9 @@ * */ +#include +#include + #include #include "gz/rendering/ogre/OgreDynamicLines.hh" #include "gz/rendering/ogre/OgreFrustumVisual.hh" diff --git a/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh b/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh index ad67cf85b..efe37afee 100644 --- a/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh +++ b/ogre2/include/gz/rendering/ogre2/Ogre2FrustumVisual.hh @@ -19,7 +19,7 @@ #define GZ_RENDERING_OGRE2_OGREFRUSTUMVISUAL_HH_ #include -#include + #include "gz/rendering/base/BaseFrustumVisual.hh" #include "gz/rendering/ogre2/Ogre2Visual.hh" #include "gz/rendering/ogre2/Ogre2Includes.hh" diff --git a/ogre2/src/Ogre2FrustumVisual.cc b/ogre2/src/Ogre2FrustumVisual.cc index 8b93d3a49..75a67a062 100644 --- a/ogre2/src/Ogre2FrustumVisual.cc +++ b/ogre2/src/Ogre2FrustumVisual.cc @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -157,7 +158,7 @@ void Ogre2FrustumVisual::Update() this->dataPtr->rayLines.push_back(renderable); // Tangent of half the field of view. - double tanFOV2 = std::tan(this->fov() * 0.5); + double tanFOV2 = std::tan(this->hfov() * 0.5); // Width of near plane double nearWidth = 2.0 * tanFOV2 * this->near; From bf688489e12ff117be90394e53df2e80d231cfbe Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 16 Jan 2025 18:45:58 +0000 Subject: [PATCH 7/9] fixing windows Signed-off-by: Ian Chen --- include/gz/rendering/base/BaseFrustumVisual.hh | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/include/gz/rendering/base/BaseFrustumVisual.hh b/include/gz/rendering/base/BaseFrustumVisual.hh index 51e31d5b9..d213dacf5 100644 --- a/include/gz/rendering/base/BaseFrustumVisual.hh +++ b/include/gz/rendering/base/BaseFrustumVisual.hh @@ -81,12 +81,6 @@ namespace gz public: virtual gz::math::Planed Plane( const FrustumVisualPlane _plane) const override; -// // Documentation inherited -// public: virtual gz::math::Pose3d Pose() const override; -// -// // Documentation inherited -// public: virtual void SetPose(const gz::math::Pose3d &_pose) override; - /// \brief Create predefined materials for lidar visual public: virtual void CreateMaterials(); @@ -97,7 +91,7 @@ namespace gz protected: double far = 1.0; /// \brief fov value - protected: gz::math::Angle hfov = 0.78539; + protected: gz::math::Angle hfov = gz::math::Angle(0.78539); /// \brief aspect ratio value protected: double aspectRatio = 1.0; From 76b28d0e661b492f2253eae6d90270cccb6b5b0d Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 16 Jan 2025 19:07:47 +0000 Subject: [PATCH 8/9] Fix window var naming Signed-off-by: Ian Chen --- .../gz/rendering/base/BaseFrustumVisual.hh | 12 ++-- ogre2/src/Ogre2FrustumVisual.cc | 56 +++++++++---------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/include/gz/rendering/base/BaseFrustumVisual.hh b/include/gz/rendering/base/BaseFrustumVisual.hh index d213dacf5..a26fb7f6e 100644 --- a/include/gz/rendering/base/BaseFrustumVisual.hh +++ b/include/gz/rendering/base/BaseFrustumVisual.hh @@ -85,10 +85,10 @@ namespace gz public: virtual void CreateMaterials(); /// \brief near value - protected: double near = 0.0; + protected: double nearClip = 0.0; /// \brief far value - protected: double far = 1.0; + protected: double farClip = 1.0; /// \brief fov value protected: gz::math::Angle hfov = gz::math::Angle(0.78539); @@ -150,28 +150,28 @@ namespace gz template void BaseFrustumVisual::SetNearClipPlane(double _near) { - this->near = _near; + this->nearClip = _near; } ///////////////////////////////////////////////// template double BaseFrustumVisual::NearClipPlane() const { - return this->near; + return this->nearClip; } ///////////////////////////////////////////////// template void BaseFrustumVisual::SetFarClipPlane(double _far) { - this->far = _far; + this->farClip = _far; } ///////////////////////////////////////////////// template double BaseFrustumVisual::FarClipPlane() const { - return this->far; + return this->farClip; } ///////////////////////////////////////////////// diff --git a/ogre2/src/Ogre2FrustumVisual.cc b/ogre2/src/Ogre2FrustumVisual.cc index 75a67a062..361f8d29f 100644 --- a/ogre2/src/Ogre2FrustumVisual.cc +++ b/ogre2/src/Ogre2FrustumVisual.cc @@ -161,13 +161,13 @@ void Ogre2FrustumVisual::Update() double tanFOV2 = std::tan(this->hfov() * 0.5); // Width of near plane - double nearWidth = 2.0 * tanFOV2 * this->near; + double nearWidth = 2.0 * tanFOV2 * this->nearClip; // Height of near plane double nearHeight = nearWidth / this->aspectRatio; // Width of far plane - double farWidth = 2.0 * tanFOV2 * this->far; + double farWidth = 2.0 * tanFOV2 * this->farClip; // Height of far plane double farHeight = farWidth / this->aspectRatio; @@ -181,10 +181,10 @@ void Ogre2FrustumVisual::Update() this->pose.Rot().RotateVector(-gz::math::Vector3d::UnitY); // Near plane center - gz::math::Vector3d nearCenter = this->pose.Pos() + forward * this->near; + gz::math::Vector3d nearCenter = this->pose.Pos() + forward * this->nearClip; // Far plane center - gz::math::Vector3d farCenter = this->pose.Pos() + forward * this->far; + gz::math::Vector3d farCenter = this->pose.Pos() + forward * this->farClip; // These four variables are here for convenience. gz::math::Vector3d upNearHeight2 = up * (nearHeight * 0.5); @@ -245,30 +245,30 @@ void Ogre2FrustumVisual::Update() (farBottomRight + nearBottomRight + farBottomLeft + nearBottomLeft) / 4.0; // For creating the frustum visuals - renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, -nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, -nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, -nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, -nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, -farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, -farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, -farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, -farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, -nearWidth, -nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, -farWidth, -farHeight)); - renderable->AddPoint(gz::math::Vector3d(this->near, nearWidth, -nearHeight)); - renderable->AddPoint(gz::math::Vector3d(this->far, farWidth, -farHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, nearWidth, nearHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, nearWidth, -nearHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, nearWidth, -nearHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, -nearWidth, -nearHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, -nearWidth, -nearHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, -nearWidth, nearHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, -nearWidth, nearHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, nearWidth, nearHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, farWidth, farHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, farWidth, -farHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, farWidth, -farHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, -farWidth, -farHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, -farWidth, -farHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, -farWidth, farHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, -farWidth, farHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, farWidth, farHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, nearWidth, nearHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, farWidth, farHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, -nearWidth, nearHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, -farWidth, farHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, -nearWidth, -nearHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, -farWidth, -farHeight)); + renderable->AddPoint(math::Vector3d(this->nearClip, nearWidth, -nearHeight)); + renderable->AddPoint(math::Vector3d(this->farClip, farWidth, -farHeight)); // Compute plane offsets // Set the planes, where the first value is the plane normal and the From c8ee90ed8bfe5bcf53b2421ca8a312fd7328b012 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 16 Jan 2025 19:10:11 +0000 Subject: [PATCH 9/9] remove unused var Signed-off-by: Ian Chen --- ogre/src/OgreFrustumVisual.cc | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ogre/src/OgreFrustumVisual.cc b/ogre/src/OgreFrustumVisual.cc index a2e8572a7..0e3594842 100644 --- a/ogre/src/OgreFrustumVisual.cc +++ b/ogre/src/OgreFrustumVisual.cc @@ -31,10 +31,6 @@ class gz::rendering::OgreFrustumVisualPrivate /// \brief Frustum Ray DynamicLines Object to display public: std::vector> rayLines; - /// \brief Frustum visual type - // public: FrustumVisualPlane frustumVisPlane = - // FrustumVisualPlane::FRUSTUM_PLANE_TOP; - /// \brief The visibility of the visual public: bool visible = true;