From 07742ff8268fed91bf913053758f6de9c7c62ea5 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Tue, 19 Jan 2021 12:13:57 +0100 Subject: [PATCH 01/24] Added capsule geometry Signed-off-by: ahcorde --- examples/simple_demo/Main.cc | 11 + include/ignition/rendering/Capsule.hh | 51 ++++ include/ignition/rendering/RenderTypes.hh | 5 + include/ignition/rendering/Scene.hh | 4 + .../ignition/rendering/base/BaseCapsule.hh | 278 ++++++++++++++++++ include/ignition/rendering/base/BaseScene.hh | 10 + .../ignition/rendering/ogre/OgreCapsule.hh | 80 +++++ .../rendering/ogre/OgreRenderTypes.hh | 2 + .../ignition/rendering/ogre/OgreScene.hh | 4 + ogre/src/OgreCapsule.cc | 150 ++++++++++ ogre/src/OgreScene.cc | 9 + .../ignition/rendering/ogre2/Ogre2Capsule.hh | 84 ++++++ .../rendering/ogre2/Ogre2RenderTypes.hh | 2 + .../ignition/rendering/ogre2/Ogre2Scene.hh | 4 + ogre2/src/Ogre2Capsule.cc | 131 +++++++++ ogre2/src/Ogre2Scene.cc | 10 + src/base/BaseScene.cc | 9 + 17 files changed, 844 insertions(+) create mode 100644 include/ignition/rendering/Capsule.hh create mode 100644 include/ignition/rendering/base/BaseCapsule.hh create mode 100644 ogre/include/ignition/rendering/ogre/OgreCapsule.hh create mode 100644 ogre/src/OgreCapsule.cc create mode 100644 ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh create mode 100644 ogre2/src/Ogre2Capsule.cc diff --git a/examples/simple_demo/Main.cc b/examples/simple_demo/Main.cc index 6f70ba2e8..fb1b6cf99 100644 --- a/examples/simple_demo/Main.cc +++ b/examples/simple_demo/Main.cc @@ -128,6 +128,17 @@ void buildScene(ScenePtr _scene) white->SetRenderOrder(0); //! [white material] + VisualPtr capsule_visual = _scene->CreateVisual(); + CapsulePtr capsule = _scene->CreateCapsule(); + capsule->SetLength(0.2); + capsule->SetRadius(0.2); + capsule_visual->AddGeometry(capsule); + capsule_visual->SetOrigin(0.0, 0.0, 0.0); + capsule_visual->SetLocalPosition(4, 2, 0); + capsule_visual->SetLocalScale(1, 1, 1); + capsule_visual->SetMaterial(red); + root->AddChild(capsule_visual); + // create plane visual VisualPtr plane = _scene->CreateVisual(); plane->AddGeometry(_scene->CreatePlane()); diff --git a/include/ignition/rendering/Capsule.hh b/include/ignition/rendering/Capsule.hh new file mode 100644 index 000000000..8d2dbabee --- /dev/null +++ b/include/ignition/rendering/Capsule.hh @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 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 IGNITION_RENDERING_CAPSULE_HH_ +#define IGNITION_RENDERING_CAPSULE_HH_ + +#include "ignition/rendering/config.hh" +#include "ignition/rendering/Geometry.hh" +#include "ignition/rendering/Object.hh" + +namespace ignition +{ + namespace rendering + { + inline namespace IGNITION_RENDERING_VERSION_NAMESPACE { + + /// \class CapsuleVisual CapsuleVisual.hh ignition/rendering/CapsuleVisual + /// \brief A CapsuleVisual geometry class. The visual appearance is based + /// on the type specified. + class IGNITION_RENDERING_VISIBLE Capsule : + public virtual Geometry + { + /// \brief Destructor + public: virtual ~Capsule() { } + + public: virtual void SetRadius(const double _radius) = 0; + + public: virtual void SetLength(const double _length) = 0; + + public: virtual double Radius() = 0; + + public: virtual double Length() = 0; + }; + } + } +} +#endif diff --git a/include/ignition/rendering/RenderTypes.hh b/include/ignition/rendering/RenderTypes.hh index e3e09c1cc..905a3ac9f 100644 --- a/include/ignition/rendering/RenderTypes.hh +++ b/include/ignition/rendering/RenderTypes.hh @@ -49,6 +49,7 @@ namespace ignition class ArrowVisual; class AxisVisual; class Camera; + class Capsule; class DepthCamera; class DirectionalLight; class GaussianNoisePass; @@ -125,6 +126,10 @@ namespace ignition /// \brief Shared pointer to GizmoVisual typedef shared_ptr GizmoVisualPtr; + /// \def CapsulePtr + /// \brief Shared pointer to Capsule + typedef shared_ptr CapsulePtr; + /// \def GridPtr /// \brief Shared pointer to Grid typedef shared_ptr GridPtr; diff --git a/include/ignition/rendering/Scene.hh b/include/ignition/rendering/Scene.hh index 9cef42044..781961aa6 100644 --- a/include/ignition/rendering/Scene.hh +++ b/include/ignition/rendering/Scene.hh @@ -826,6 +826,10 @@ namespace ignition /// \return The created box public: virtual GeometryPtr CreateBox() = 0; + /// \brief Create new capsule geometry + /// \return The created capsule + public: virtual CapsulePtr CreateCapsule() = 0; + /// \brief Create new cone geometry /// \return The created cone public: virtual GeometryPtr CreateCone() = 0; diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh new file mode 100644 index 000000000..7215b1912 --- /dev/null +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -0,0 +1,278 @@ +/* + * Copyright (C) 2021 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 IGNITION_RENDERING_BASECAPSULE_HH_ +#define IGNITION_RENDERING_BASECAPSULE_HH_ + +#include + +#include +#include + +#include "ignition/rendering/Capsule.hh" +#include "ignition/rendering/base/BaseObject.hh" + +namespace ignition +{ + namespace rendering + { + inline namespace IGNITION_RENDERING_VERSION_NAMESPACE { + + /// \brief Base implementation of a Capsule Visual + template + class BaseCapsule : + public virtual Capsule, + public virtual T + { + // Documentation inherited + protected: BaseCapsule(); + + // Documentation inherited + public: virtual ~BaseCapsule(); + + // Documentation inherited + public: virtual void PreRender() override; + + // Documentation inherited. + public: virtual void Destroy() override; + + // Documentation inherited + public: virtual void SetRadius(const double _radius) override; + + // Documentation inherited + public: virtual void SetLength(const double _length) override; + + // Documentation inherited + public: virtual double Radius() override; + + // Documentation inherited + public: virtual double Length() override; + + /// \brief return the list of triangles, _indexes and _uv coordinates + /// \param[inout] _positions list with the triangles + /// \param[inout] _indexes indexes of the position list + /// \param[inout] _uv UV coordinates + protected: void CapsuleMesh( + std::vector &_positions, + std::vector &_indexes, + std::vector &_uv); + + /// \brief Radius of the capsule + protected: double radius = 0.5; + + /// \brief Length of the capsule + protected: double length = 0.5; + + /// \brief Flag to indicate grid properties have changed + protected: bool capsuleDirty = false; + }; + + ///////////////////////////////////////////////// + // BaseCapsule + ///////////////////////////////////////////////// + template + BaseCapsule::BaseCapsule() + { + } + + ///////////////////////////////////////////////// + template + BaseCapsule::~BaseCapsule() + { + } + + ///////////////////////////////////////////////// + template + void BaseCapsule::PreRender() + { + T::PreRender(); + } + + ////////////////////////////////////////////////// + template + void BaseCapsule::Destroy() + { + T::Destroy(); + } + + ///////////////////////////////////////////////// + template + void BaseCapsule::SetRadius(const double _radius) + { + this->radius = _radius; + this->capsuleDirty = true; + } + + ///////////////////////////////////////////////// + template + double BaseCapsule::Radius() + { + return this->radius; + } + + ///////////////////////////////////////////////// + template + void BaseCapsule::SetLength(const double _length) + { + this->length = _length; + this->capsuleDirty = true; + } + + ///////////////////////////////////////////////// + template + double BaseCapsule::Length() + { + return this->length; + } + ///////////////////////////////////////////////// + template + void BaseCapsule::CapsuleMesh( + std::vector &_positions, + std::vector &_indexes, + std::vector &_uv) + { + // Based on https://github.com/godotengine/godot primitive_meshes.cpp + int prevRow, thisRow, point; + float x, y, z, u, v, w; + float oneThird = 1.0 / 3.0; + float twoThirds = 2.0 / 3.0; + unsigned int rings = 12; + unsigned int radialSegments = 32; + + point = 0; + + /* top hemisphere */ + thisRow = 0; + prevRow = 0; + for (unsigned int j = 0; j <= (rings + 1); j++) { + v = j; + + v /= (rings + 1); + w = sin(0.5 * M_PI * v); + y = this->radius * cos(0.5 * M_PI * v); + + for (unsigned int i = 0; i <= radialSegments; i++) { + u = i; + u /= radialSegments; + + x = -sin(u * (M_PI * 2.0)); + z = cos(u * (M_PI * 2.0)); + + ignition::math::Vector3d p( + x * this->radius * w, y, -z * this->radius * w); + _positions.push_back( + p + ignition::math::Vector3d(0.0, 0.5 * this->length, 0.0)); + _uv.emplace_back(ignition::math::Vector2d(u, v * oneThird)); + point++; + + if (i > 0 && j > 0) { + _indexes.emplace_back(thisRow + i - 1); + _indexes.emplace_back(prevRow + i); + _indexes.emplace_back(prevRow + i - 1); + + _indexes.emplace_back(thisRow + i - 1); + _indexes.emplace_back(thisRow + i); + _indexes.emplace_back(prevRow + i); + } + } + prevRow = thisRow; + thisRow = point; + } + + /* cylinder */ + thisRow = point; + prevRow = 0; + for (unsigned int j = 0; j <= (rings + 1); j++) { + v = j; + v /= (rings + 1); + + y = this->length * v; + y = (this->length * 0.5) - y; + + for (unsigned int i = 0; i <= radialSegments; i++) { + u = i; + u /= radialSegments; + + x = -sin(u * (M_PI * 2.0)); + z = cos(u * (M_PI * 2.0)); + + ignition::math::Vector3d p( + x * this->radius, y, -z * this->radius); + _positions.emplace_back(p); + _uv.emplace_back( + ignition::math::Vector2d(u, oneThird + (v * oneThird))); + point++; + + if (i > 0 && j > 0) { + _indexes.emplace_back(thisRow + i - 1); + _indexes.emplace_back(prevRow + i); + _indexes.emplace_back(prevRow + i - 1); + + _indexes.emplace_back(thisRow + i - 1); + _indexes.emplace_back(thisRow + i); + _indexes.emplace_back(prevRow + i); + } + } + prevRow = thisRow; + thisRow = point; + } + + /* bottom hemisphere */ + thisRow = point; + prevRow = 0; + for (unsigned int j = 0; j <= (rings + 1); j++) { + v = j; + + v /= (rings + 1); + v += 1.0; + w = sin(0.5 * M_PI * v); + y = this->radius * cos(0.5 * M_PI * v); + + for (unsigned int i = 0; i <= radialSegments; i++) { + float u2 = i; + u2 /= radialSegments; + + x = -sin(u2 * (M_PI * 2.0)); + z = cos(u2 * (M_PI * 2.0)); + + ignition::math::Vector3d p( + x * this->radius * w, y, -z * this->radius * w); + _positions.emplace_back( + p + ignition::math::Vector3d(0.0, -0.5 * this->length, 0.0)); + _uv.emplace_back( + ignition::math::Vector2d(u2, twoThirds + ((v - 1.0) * oneThird))); + point++; + + if (i > 0 && j > 0) { + _indexes.emplace_back(thisRow + i - 1); + _indexes.emplace_back(prevRow + i); + _indexes.emplace_back(prevRow + i - 1); + + _indexes.emplace_back(thisRow + i - 1); + _indexes.emplace_back(thisRow + i); + _indexes.emplace_back(prevRow + i); + } + } + + prevRow = thisRow; + thisRow = point; + } + } + } + } +} +#endif diff --git a/include/ignition/rendering/base/BaseScene.hh b/include/ignition/rendering/base/BaseScene.hh index d77e8671f..84a5696e5 100644 --- a/include/ignition/rendering/base/BaseScene.hh +++ b/include/ignition/rendering/base/BaseScene.hh @@ -384,6 +384,9 @@ namespace ignition public: virtual MeshPtr CreateMesh(const MeshDescriptor &_desc) override; + // Documentation inherited. + public: virtual CapsulePtr CreateCapsule() override; + // Documentation inherited. public: virtual GridPtr CreateGrid() override; @@ -538,6 +541,13 @@ namespace ignition const std::string &_name, const MeshDescriptor &_desc) = 0; + /// \brief Implementation for creating a capsule geometry object + /// \param[in] _id unique object id. + /// \param[in] _name unique object name. + /// \return Pointer to a capsule geometry object + protected: virtual CapsulePtr CreateCapsuleImpl(unsigned int _id, + const std::string &_name) = 0; + /// \brief Implementation for creating a grid geometry object /// \param[in] _id unique object id. /// \param[in] _name unique object name. diff --git a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh new file mode 100644 index 000000000..061a99b98 --- /dev/null +++ b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2021 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 IGNITION_RENDERING_OGRE_OGRECAPSULE_HH_ +#define IGNITION_RENDERING_OGRE_OGRECAPSULE_HH_ + +#include +#include "ignition/rendering/base/BaseCapsule.hh" +#include "ignition/rendering/ogre/OgreGeometry.hh" +#include "ignition/rendering/ogre/OgreIncludes.hh" + +namespace Ogre +{ + class MovableObject; +} + +namespace ignition +{ + namespace rendering + { + inline namespace IGNITION_RENDERING_VERSION_NAMESPACE { + // Forward declaration + class OgreCapsulePrivate; + + /// \brief Ogre 2.x implementation of a Capsule Visual. + class IGNITION_RENDERING_OGRE_VISIBLE OgreCapsule + : public BaseCapsule + { + /// \brief Constructor + protected: OgreCapsule(); + + /// \brief Destructor + public: virtual ~OgreCapsule(); + + // Documentation inherited. + public: virtual void Init() override; + + // Documentation inherited. + public: virtual Ogre::MovableObject *OgreObject() const; + + // Documentation inherited. + public: virtual void PreRender() override; + + // Documentation inherited. + public: virtual MaterialPtr Material() const; + + // Documentation inherited. + public: virtual void SetMaterial(MaterialPtr _material, bool _unique); + + /// \brief Set material to grid geometry. + /// \param[in] _material Ogre material. + protected: virtual void SetMaterialImpl(OgreMaterialPtr _material); + + /// \brief Create the Capsule Visual in ogre + private: void Create(); + + /// \brief Grid 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/ignition/rendering/ogre/OgreRenderTypes.hh b/ogre/include/ignition/rendering/ogre/OgreRenderTypes.hh index 3c1680ed8..5523222d3 100644 --- a/ogre/include/ignition/rendering/ogre/OgreRenderTypes.hh +++ b/ogre/include/ignition/rendering/ogre/OgreRenderTypes.hh @@ -29,6 +29,7 @@ namespace ignition class OgreArrowVisual; class OgreAxisVisual; class OgreCamera; + class OgreCapsule; class OgreDepthCamera; class OgreDirectionalLight; class OgreGeometry; @@ -73,6 +74,7 @@ namespace ignition typedef shared_ptr OgreArrowVisualPtr; typedef shared_ptr OgreAxisVisualPtr; typedef shared_ptr OgreCameraPtr; + typedef shared_ptr OgreCapsulePtr; typedef shared_ptr OgreDepthCameraPtr; typedef shared_ptr OgreDirectionalLightPtr; typedef shared_ptr OgreGeometryPtr; diff --git a/ogre/include/ignition/rendering/ogre/OgreScene.hh b/ogre/include/ignition/rendering/ogre/OgreScene.hh index a928e04a5..f7123bceb 100644 --- a/ogre/include/ignition/rendering/ogre/OgreScene.hh +++ b/ogre/include/ignition/rendering/ogre/OgreScene.hh @@ -134,6 +134,10 @@ namespace ignition protected: virtual MeshPtr CreateMeshImpl(unsigned int _id, const std::string &_name, const MeshDescriptor &_desc); + // Documentation inherited + protected: virtual CapsulePtr CreateCapsuleImpl(unsigned int _id, + const std::string &_name); + // Documentation inherited protected: virtual GridPtr CreateGridImpl(unsigned int _id, const std::string &_name); diff --git a/ogre/src/OgreCapsule.cc b/ogre/src/OgreCapsule.cc new file mode 100644 index 000000000..aa6dd97a3 --- /dev/null +++ b/ogre/src/OgreCapsule.cc @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2021 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 "ignition/rendering/ogre/OgreCapsule.hh" +#include "ignition/rendering/ogre/OgreMaterial.hh" +#include "ignition/rendering/ogre/OgreScene.hh" + +#include + +class ignition::rendering::OgreCapsulePrivate +{ + /// \brief Grid materal + public: OgreMaterialPtr material; + + public: Ogre::ManualObject *manualObject = nullptr; +}; + +using namespace ignition; +using namespace rendering; + +////////////////////////////////////////////////// +OgreCapsule::OgreCapsule() + : dataPtr(new OgreCapsulePrivate) +{ +} + +////////////////////////////////////////////////// +OgreCapsule::~OgreCapsule() +{ + // no ops +} + +////////////////////////////////////////////////// +void OgreCapsule::PreRender() +{ + if (this->capsuleDirty) + { + this->Create(); + this->capsuleDirty = false; + } +} + +////////////////////////////////////////////////// +Ogre::MovableObject *OgreCapsule::OgreObject() const +{ + return this->dataPtr->manualObject; +} + +////////////////////////////////////////////////// +void OgreCapsule::Init() +{ + this->Create(); +} + +////////////////////////////////////////////////// +void OgreCapsuleCapsule() +{ + // no ops +} + +////////////////////////////////////////////////// +void OgreCapsule::Create() +{ + if (!this->dataPtr->manualObject) + { + this->dataPtr->manualObject = + this->scene->OgreSceneManager()->createManualObject(this->name); + } + this->dataPtr->manualObject->clear(); + + this->dataPtr->manualObject->setCastShadows(false); + + std::string materialName = this->dataPtr->material ? + this->dataPtr->material->Name() : "Default/White"; + this->dataPtr->manualObject->begin(materialName, + Ogre::RenderOperation::OT_TRIANGLE_LIST); + + std::vector positions; + std::vector indexes; + std::vector uvs; + + this->CapsuleMesh(positions, indexes, uvs); + + for (unsigned int i = 0; i < positions.size(); i++) + { + this->dataPtr->manualObject->position( + Ogre::Vector3(positions[i].X(), positions[i].Y(), positions[i].Z())); + this->dataPtr->manualObject->textureCoord(Ogre::Vector2(uvs[i].X(), uvs[i].Y())); + this->dataPtr->manualObject->normal( + positions[i].Normalize().X(), + positions[i].Normalize().Y(), + positions[i].Normalize().Z()); + } + + for (auto index : indexes) + { + this->dataPtr->manualObject->index(index); + } + this->dataPtr->manualObject->end(); +} + +////////////////////////////////////////////////// +void OgreCapsule::SetMaterial(MaterialPtr _material, bool _unique) +{ + _material = (_unique) ? _material->Clone() : _material; + + OgreMaterialPtr derived = + std::dynamic_pointer_cast(_material); + + if (!derived) + { + ignerr << "Cannot assign material created by another render-engine" + << std::endl; + + return; + } + + this->SetMaterialImpl(derived); +} + +////////////////////////////////////////////////// +void OgreCapsule::SetMaterialImpl(OgreMaterialPtr _material) +{ + std::string materialName = _material->Name(); + Ogre::MaterialPtr ogreMaterial = _material->Material(); + this->dataPtr->manualObject->setMaterialName(0, materialName); + this->dataPtr->material = _material; +} + +////////////////////////////////////////////////// +MaterialPtr OgreCapsule::Material() const +{ + return this->dataPtr->material; +} diff --git a/ogre/src/OgreScene.cc b/ogre/src/OgreScene.cc index 7ed75d2f9..0d2bc84ff 100644 --- a/ogre/src/OgreScene.cc +++ b/ogre/src/OgreScene.cc @@ -20,6 +20,7 @@ #include "ignition/rendering/ogre/OgreArrowVisual.hh" #include "ignition/rendering/ogre/OgreAxisVisual.hh" #include "ignition/rendering/ogre/OgreCamera.hh" +#include "ignition/rendering/ogre/OgreCapsule.hh" #include "ignition/rendering/ogre/OgreDepthCamera.hh" #include "ignition/rendering/ogre/OgreConversions.hh" #include "ignition/rendering/ogre/OgreGeometry.hh" @@ -503,6 +504,14 @@ MeshPtr OgreScene::CreateMeshImpl(unsigned int _id, const std::string &_name, return (result) ? mesh : nullptr; } +////////////////////////////////////////////////// +CapsulePtr OgreScene::CreateCapsuleImpl(unsigned int _id, const std::string &_name) +{ + OgreCapsulePtr capsule(new OgreCapsule); + bool result = this->InitObject(capsule, _id, _name); + return (result) ? capsule : nullptr; +} + ////////////////////////////////////////////////// GridPtr OgreScene::CreateGridImpl(unsigned int _id, const std::string &_name) { diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh new file mode 100644 index 000000000..da41ac0f6 --- /dev/null +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2021 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 IGNITION_RENDERING_OGRE2_OGRECAPSULE_HH_ +#define IGNITION_RENDERING_OGRE2_OGRECAPSULE_HH_ + +#include +#include "ignition/rendering/base/BaseCapsule.hh" +#include "ignition/rendering/ogre2/Ogre2Geometry.hh" +#include "ignition/rendering/ogre2/Ogre2Includes.hh" + +namespace Ogre +{ + class MovableObject; +} + +namespace ignition +{ + namespace rendering + { + inline namespace IGNITION_RENDERING_VERSION_NAMESPACE { + // Forward declaration + class Ogre2CapsulePrivate; + + /// \brief Ogre 2.x implementation of a Capsule Visual. + class IGNITION_RENDERING_OGRE2_VISIBLE Ogre2Capsule + : public BaseCapsule + { + /// \brief Constructor + protected: Ogre2Capsule(); + + /// \brief Destructor + public: virtual ~Ogre2Capsule(); + + // Documentation inherited. + public: virtual void Init() override; + + // Documentation inherited. + public: virtual Ogre::MovableObject *OgreObject() const; + + // Documentation inherited. + public: virtual void PreRender() override; + + // Documentation inherited. + public: virtual MaterialPtr Material() const; + + // Documentation inherited. + public: virtual void SetMaterial(MaterialPtr _material, bool _unique); + + /// \brief Set material to grid geometry. + /// \param[in] _material Ogre material. + protected: virtual void SetMaterialImpl(Ogre2MaterialPtr _material); + + /// \brief Create the Capsule Visual in ogre + private: void Create(); + + /// \brief Grid should only be created by scene. + private: friend class Ogre2Scene; + + private: void calculateRing( + size_t segments, float radius, float y, float dy, + std::vector & positions); + + /// \brief Private data class + private: std::unique_ptr dataPtr; + }; + } + } +} +#endif diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2RenderTypes.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2RenderTypes.hh index 1c53db081..b4bcb2b3d 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2RenderTypes.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2RenderTypes.hh @@ -31,6 +31,7 @@ namespace ignition class Ogre2ArrowVisual; class Ogre2AxisVisual; class Ogre2Camera; + class Ogre2Capsule; class Ogre2DepthCamera; class Ogre2DirectionalLight; class Ogre2Geometry; @@ -74,6 +75,7 @@ namespace ignition typedef shared_ptr Ogre2ArrowVisualPtr; typedef shared_ptr Ogre2AxisVisualPtr; typedef shared_ptr Ogre2CameraPtr; + typedef shared_ptr Ogre2CapsulePtr; typedef shared_ptr Ogre2DepthCameraPtr; typedef shared_ptr Ogre2DirectionalLightPtr; typedef shared_ptr Ogre2GeometryPtr; diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Scene.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Scene.hh index f676c6190..8a0402415 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Scene.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Scene.hh @@ -184,6 +184,10 @@ namespace ignition const std::string &_name, const MeshDescriptor &_desc) override; + // Documentation inherited + protected: virtual CapsulePtr CreateCapsuleImpl(unsigned int _id, + const std::string &_name) override; + // Documentation inherited protected: virtual GridPtr CreateGridImpl(unsigned int _id, const std::string &_name) override; diff --git a/ogre2/src/Ogre2Capsule.cc b/ogre2/src/Ogre2Capsule.cc new file mode 100644 index 000000000..ce80ba43b --- /dev/null +++ b/ogre2/src/Ogre2Capsule.cc @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2021 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 "ignition/rendering/ogre2/Ogre2Capsule.hh" +#include "ignition/rendering/ogre2/Ogre2Material.hh" +#include "ignition/rendering/ogre2/Ogre2Scene.hh" +#include "ignition/rendering/ogre2/Ogre2DynamicRenderable.hh" + +#include + +class ignition::rendering::Ogre2CapsulePrivate +{ + /// \brief Grid materal + public: Ogre2MaterialPtr material; + + public: std::shared_ptr capsule = nullptr; +}; + +using namespace ignition; +using namespace rendering; + +////////////////////////////////////////////////// +Ogre2Capsule::Ogre2Capsule() + : dataPtr(new Ogre2CapsulePrivate) +{ +} + +////////////////////////////////////////////////// +Ogre2Capsule::~Ogre2Capsule() +{ + // no ops +} + +////////////////////////////////////////////////// +Ogre::MovableObject *Ogre2Capsule::OgreObject() const +{ + return this->dataPtr->capsule->OgreObject(); +} + +////////////////////////////////////////////////// +void Ogre2Capsule::PreRender() +{ + if (this->capsuleDirty) + { + this->Create(); + this->capsuleDirty = false; + } +} + +////////////////////////////////////////////////// +void Ogre2Capsule::Init() +{ + this->Create(); +} + +//////////////////////////////////////////////// +void Ogre2Capsule::Create() +{ + if (!this->dataPtr->capsule) + { + this->dataPtr->capsule.reset(new Ogre2DynamicRenderable(this->Scene())); + } + + this->dataPtr->capsule->Clear(); + this->dataPtr->capsule->Update(); + + this->dataPtr->capsule->SetOperationType(MT_TRIANGLE_LIST); + + std::vector positions; + std::vector indexes; + std::vector uvs; + + this->CapsuleMesh(positions, indexes, uvs); + + for (auto index : indexes) + { + this->dataPtr->capsule->AddPoint(positions[index]); + } + + this->dataPtr->capsule->Update(); +} + +////////////////////////////////////////////////// +void Ogre2Capsule::SetMaterial(MaterialPtr _material, bool _unique) +{ + _material = (_unique) ? _material->Clone() : _material; + + Ogre2MaterialPtr derived = + std::dynamic_pointer_cast(_material); + + if (!derived) + { + ignerr << "Cannot assign material created by another render-engine" + << std::endl; + + return; + } + + // Set material for the underlying dynamic renderable + this->dataPtr->capsule->SetMaterial(_material, false); + this->SetMaterialImpl(derived); +} + +////////////////////////////////////////////////// +void Ogre2Capsule::SetMaterialImpl(Ogre2MaterialPtr _material) +{ + Ogre::MaterialPtr ogreMaterial = _material->Material(); + this->dataPtr->material = _material; +} + +////////////////////////////////////////////////// +MaterialPtr Ogre2Capsule::Material() const +{ + return this->dataPtr->material; +} diff --git a/ogre2/src/Ogre2Scene.cc b/ogre2/src/Ogre2Scene.cc index 752b8ab9e..5f32d3aba 100644 --- a/ogre2/src/Ogre2Scene.cc +++ b/ogre2/src/Ogre2Scene.cc @@ -21,6 +21,7 @@ #include "ignition/rendering/ogre2/Ogre2ArrowVisual.hh" #include "ignition/rendering/ogre2/Ogre2AxisVisual.hh" #include "ignition/rendering/ogre2/Ogre2Camera.hh" +#include "ignition/rendering/ogre2/Ogre2Capsule.hh" #include "ignition/rendering/ogre2/Ogre2Conversions.hh" #include "ignition/rendering/ogre2/Ogre2DepthCamera.hh" #include "ignition/rendering/ogre2/Ogre2GizmoVisual.hh" @@ -373,6 +374,15 @@ MeshPtr Ogre2Scene::CreateMeshImpl(unsigned int _id, return (result) ? mesh : nullptr; } +////////////////////////////////////////////////// +CapsulePtr Ogre2Scene::CreateCapsuleImpl(unsigned int _id, + const std::string &_name) +{ + Ogre2CapsulePtr capsule(new Ogre2Capsule); + bool result = this->InitObject(capsule, _id, _name); + return (result) ? capsule : nullptr; +} + ////////////////////////////////////////////////// GridPtr Ogre2Scene::CreateGridImpl(unsigned int _id, const std::string &_name) diff --git a/src/base/BaseScene.cc b/src/base/BaseScene.cc index 9f86a7845..14c85661a 100644 --- a/src/base/BaseScene.cc +++ b/src/base/BaseScene.cc @@ -29,6 +29,7 @@ #include "ignition/rendering/AxisVisual.hh" #include "ignition/rendering/LidarVisual.hh" #include "ignition/rendering/Camera.hh" +#include "ignition/rendering/Capsule.hh" #include "ignition/rendering/DepthCamera.hh" #include "ignition/rendering/GizmoVisual.hh" #include "ignition/rendering/GpuRays.hh" @@ -978,6 +979,14 @@ GeometryPtr BaseScene::CreatePlane() return this->CreatePlaneImpl(objId, objName); } +////////////////////////////////////////////////// +CapsulePtr BaseScene::CreateCapsule() +{ + unsigned int objId = this->CreateObjectId(); + std::string objName = this->CreateObjectName(objId, "Capsule"); + return this->CreateCapsuleImpl(objId, objName); +} + ////////////////////////////////////////////////// GeometryPtr BaseScene::CreateSphere() { From d66ee331586119d135eb917633247cb56ea4ad6c Mon Sep 17 00:00:00 2001 From: ahcorde Date: Tue, 19 Jan 2021 15:42:24 +0100 Subject: [PATCH 02/24] make linters happy Signed-off-by: ahcorde --- .../ignition/rendering/base/BaseCapsule.hh | 154 +++++++++--------- ogre/src/OgreCapsule.cc | 3 +- ogre/src/OgreScene.cc | 3 +- 3 files changed, 81 insertions(+), 79 deletions(-) diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh index 7215b1912..fef9104e9 100644 --- a/include/ignition/rendering/base/BaseCapsule.hh +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -147,39 +147,39 @@ namespace ignition { // Based on https://github.com/godotengine/godot primitive_meshes.cpp int prevRow, thisRow, point; - float x, y, z, u, v, w; + float x, y, z, u, v, w; float oneThird = 1.0 / 3.0; - float twoThirds = 2.0 / 3.0; + float twoThirds = 2.0 / 3.0; unsigned int rings = 12; unsigned int radialSegments = 32; - point = 0; + point = 0; /* top hemisphere */ - thisRow = 0; - prevRow = 0; - for (unsigned int j = 0; j <= (rings + 1); j++) { - v = j; - - v /= (rings + 1); - w = sin(0.5 * M_PI * v); - y = this->radius * cos(0.5 * M_PI * v); - - for (unsigned int i = 0; i <= radialSegments; i++) { - u = i; - u /= radialSegments; - - x = -sin(u * (M_PI * 2.0)); - z = cos(u * (M_PI * 2.0)); - - ignition::math::Vector3d p( + thisRow = 0; + prevRow = 0; + for (unsigned int j = 0; j <= (rings + 1); j++) { + v = j; + gf + v /= (rings + 1); + w = sin(0.5 * M_PI * v); + y = this->radius * cos(0.5 * M_PI * v); + + for (unsigned int i = 0; i <= radialSegments; i++) { + u = i; + u /= radialSegments; + a + x = -sin(u * (M_PI * 2.0)); + z = cos(u * (M_PI * 2.0)); + + ignition::math::Vector3d p( x * this->radius * w, y, -z * this->radius * w); - _positions.push_back( + _positions.push_back( p + ignition::math::Vector3d(0.0, 0.5 * this->length, 0.0)); _uv.emplace_back(ignition::math::Vector2d(u, v * oneThird)); - point++; + point++; - if (i > 0 && j > 0) { + if (i > 0 && j > 0) { _indexes.emplace_back(thisRow + i - 1); _indexes.emplace_back(prevRow + i); _indexes.emplace_back(prevRow + i - 1); @@ -187,37 +187,37 @@ namespace ignition _indexes.emplace_back(thisRow + i - 1); _indexes.emplace_back(thisRow + i); _indexes.emplace_back(prevRow + i); - } - } - prevRow = thisRow; - thisRow = point; - } + } + } + prevRow = thisRow; + thisRow = point; + } /* cylinder */ - thisRow = point; - prevRow = 0; - for (unsigned int j = 0; j <= (rings + 1); j++) { - v = j; - v /= (rings + 1); + thisRow = point; + prevRow = 0; + for (unsigned int j = 0; j <= (rings + 1); j++) { + v = j; + v /= (rings + 1); - y = this->length * v; - y = (this->length * 0.5) - y; + y = this->length * v; + y = (this->length * 0.5) - y; - for (unsigned int i = 0; i <= radialSegments; i++) { - u = i; - u /= radialSegments; + for (unsigned int i = 0; i <= radialSegments; i++) { + u = i; + u /= radialSegments; - x = -sin(u * (M_PI * 2.0)); - z = cos(u * (M_PI * 2.0)); + x = -sin(u * (M_PI * 2.0)); + z = cos(u * (M_PI * 2.0)); - ignition::math::Vector3d p( + ignition::math::Vector3d p( x * this->radius, y, -z * this->radius); - _positions.emplace_back(p); + _positions.emplace_back(p); _uv.emplace_back( ignition::math::Vector2d(u, oneThird + (v * oneThird))); - point++; + point++; - if (i > 0 && j > 0) { + if (i > 0 && j > 0) { _indexes.emplace_back(thisRow + i - 1); _indexes.emplace_back(prevRow + i); _indexes.emplace_back(prevRow + i - 1); @@ -225,39 +225,39 @@ namespace ignition _indexes.emplace_back(thisRow + i - 1); _indexes.emplace_back(thisRow + i); _indexes.emplace_back(prevRow + i); - } - } - prevRow = thisRow; - thisRow = point; - } - - /* bottom hemisphere */ - thisRow = point; - prevRow = 0; - for (unsigned int j = 0; j <= (rings + 1); j++) { - v = j; - - v /= (rings + 1); - v += 1.0; - w = sin(0.5 * M_PI * v); - y = this->radius * cos(0.5 * M_PI * v); - - for (unsigned int i = 0; i <= radialSegments; i++) { - float u2 = i; - u2 /= radialSegments; - - x = -sin(u2 * (M_PI * 2.0)); - z = cos(u2 * (M_PI * 2.0)); - - ignition::math::Vector3d p( + } + } + prevRow = thisRow; + thisRow = point; + } + + /* bottom hemisphere */ + thisRow = point; + prevRow = 0; + for (unsigned int j = 0; j <= (rings + 1); j++) { + v = j; + + v /= (rings + 1); + v += 1.0; + w = sin(0.5 * M_PI * v); + y = this->radius * cos(0.5 * M_PI * v); + + for (unsigned int i = 0; i <= radialSegments; i++) { + float u2 = i; + u2 /= radialSegments; + + x = -sin(u2 * (M_PI * 2.0)); + z = cos(u2 * (M_PI * 2.0)); + + ignition::math::Vector3d p( x * this->radius * w, y, -z * this->radius * w); - _positions.emplace_back( + _positions.emplace_back( p + ignition::math::Vector3d(0.0, -0.5 * this->length, 0.0)); _uv.emplace_back( ignition::math::Vector2d(u2, twoThirds + ((v - 1.0) * oneThird))); - point++; + point++; - if (i > 0 && j > 0) { + if (i > 0 && j > 0) { _indexes.emplace_back(thisRow + i - 1); _indexes.emplace_back(prevRow + i); _indexes.emplace_back(prevRow + i - 1); @@ -265,12 +265,12 @@ namespace ignition _indexes.emplace_back(thisRow + i - 1); _indexes.emplace_back(thisRow + i); _indexes.emplace_back(prevRow + i); - } - } + } + } - prevRow = thisRow; - thisRow = point; - } + prevRow = thisRow; + thisRow = point; + } } } } diff --git a/ogre/src/OgreCapsule.cc b/ogre/src/OgreCapsule.cc index aa6dd97a3..753c335e3 100644 --- a/ogre/src/OgreCapsule.cc +++ b/ogre/src/OgreCapsule.cc @@ -101,7 +101,8 @@ void OgreCapsule::Create() { this->dataPtr->manualObject->position( Ogre::Vector3(positions[i].X(), positions[i].Y(), positions[i].Z())); - this->dataPtr->manualObject->textureCoord(Ogre::Vector2(uvs[i].X(), uvs[i].Y())); + this->dataPtr->manualObject->textureCoord( + Ogre::Vector2(uvs[i].X(), uvs[i].Y())); this->dataPtr->manualObject->normal( positions[i].Normalize().X(), positions[i].Normalize().Y(), diff --git a/ogre/src/OgreScene.cc b/ogre/src/OgreScene.cc index 0d2bc84ff..ee3ee1234 100644 --- a/ogre/src/OgreScene.cc +++ b/ogre/src/OgreScene.cc @@ -505,7 +505,8 @@ MeshPtr OgreScene::CreateMeshImpl(unsigned int _id, const std::string &_name, } ////////////////////////////////////////////////// -CapsulePtr OgreScene::CreateCapsuleImpl(unsigned int _id, const std::string &_name) +CapsulePtr OgreScene::CreateCapsuleImpl( + unsigned int _id, const std::string &_name) { OgreCapsulePtr capsule(new OgreCapsule); bool result = this->InitObject(capsule, _id, _name); From e30745f0877276b67b108f409cae2f9fa7bfcc22 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Tue, 19 Jan 2021 17:42:35 +0100 Subject: [PATCH 03/24] make linters happy Signed-off-by: ahcorde --- ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index da41ac0f6..8b52f0e25 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -19,6 +19,8 @@ #define IGNITION_RENDERING_OGRE2_OGRECAPSULE_HH_ #include +#include + #include "ignition/rendering/base/BaseCapsule.hh" #include "ignition/rendering/ogre2/Ogre2Geometry.hh" #include "ignition/rendering/ogre2/Ogre2Includes.hh" From 720a17840198284968b2c5546d0efb31e0da9fe9 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Tue, 19 Jan 2021 18:16:05 +0100 Subject: [PATCH 04/24] quick fix Signed-off-by: ahcorde --- include/ignition/rendering/base/BaseCapsule.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh index fef9104e9..20bb6d727 100644 --- a/include/ignition/rendering/base/BaseCapsule.hh +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -160,7 +160,7 @@ namespace ignition prevRow = 0; for (unsigned int j = 0; j <= (rings + 1); j++) { v = j; - gf + v /= (rings + 1); w = sin(0.5 * M_PI * v); y = this->radius * cos(0.5 * M_PI * v); @@ -168,7 +168,7 @@ namespace ignition for (unsigned int i = 0; i <= radialSegments; i++) { u = i; u /= radialSegments; - a + x = -sin(u * (M_PI * 2.0)); z = cos(u * (M_PI * 2.0)); From 02396d67adc7c5638f38bc481fc9f73816d82388 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Wed, 20 Jan 2021 15:36:10 +0100 Subject: [PATCH 05/24] Fixed typo Signed-off-by: ahcorde --- include/ignition/rendering/base/BaseCapsule.hh | 2 +- ogre/include/ignition/rendering/ogre/OgreCapsule.hh | 4 ++-- ogre/src/OgreCapsule.cc | 2 +- ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh | 4 ++-- ogre2/src/Ogre2Capsule.cc | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh index 20bb6d727..39135d990 100644 --- a/include/ignition/rendering/base/BaseCapsule.hh +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -77,7 +77,7 @@ namespace ignition /// \brief Length of the capsule protected: double length = 0.5; - /// \brief Flag to indicate grid properties have changed + /// \brief Flag to indicate capsule properties have changed protected: bool capsuleDirty = false; }; diff --git a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh index 061a99b98..079264ac4 100644 --- a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh +++ b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh @@ -61,14 +61,14 @@ namespace ignition // Documentation inherited. public: virtual void SetMaterial(MaterialPtr _material, bool _unique); - /// \brief Set material to grid geometry. + /// \brief Set material to capsule geometry. /// \param[in] _material Ogre material. protected: virtual void SetMaterialImpl(OgreMaterialPtr _material); /// \brief Create the Capsule Visual in ogre private: void Create(); - /// \brief Grid should only be created by scene. + /// \brief Capsule should only be created by scene. private: friend class OgreScene; /// \brief Private data class diff --git a/ogre/src/OgreCapsule.cc b/ogre/src/OgreCapsule.cc index 753c335e3..c6ef47aca 100644 --- a/ogre/src/OgreCapsule.cc +++ b/ogre/src/OgreCapsule.cc @@ -25,7 +25,7 @@ class ignition::rendering::OgreCapsulePrivate { - /// \brief Grid materal + /// \brief Capsule materal public: OgreMaterialPtr material; public: Ogre::ManualObject *manualObject = nullptr; diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index 8b52f0e25..c3168ec77 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -63,14 +63,14 @@ namespace ignition // Documentation inherited. public: virtual void SetMaterial(MaterialPtr _material, bool _unique); - /// \brief Set material to grid geometry. + /// \brief Set material to capsule geometry. /// \param[in] _material Ogre material. protected: virtual void SetMaterialImpl(Ogre2MaterialPtr _material); /// \brief Create the Capsule Visual in ogre private: void Create(); - /// \brief Grid should only be created by scene. + /// \brief Capsule should only be created by scene. private: friend class Ogre2Scene; private: void calculateRing( diff --git a/ogre2/src/Ogre2Capsule.cc b/ogre2/src/Ogre2Capsule.cc index ce80ba43b..508297feb 100644 --- a/ogre2/src/Ogre2Capsule.cc +++ b/ogre2/src/Ogre2Capsule.cc @@ -26,7 +26,7 @@ class ignition::rendering::Ogre2CapsulePrivate { - /// \brief Grid materal + /// \brief Capsule materal public: Ogre2MaterialPtr material; public: std::shared_ptr capsule = nullptr; From 7b952d3fc167d4f4f31ff08599d63642d69619db Mon Sep 17 00:00:00 2001 From: ahcorde Date: Wed, 20 Jan 2021 15:39:55 +0100 Subject: [PATCH 06/24] Added Capsule_TEST Signed-off-by: ahcorde --- src/Capsule_TEST.cc | 100 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/Capsule_TEST.cc diff --git a/src/Capsule_TEST.cc b/src/Capsule_TEST.cc new file mode 100644 index 000000000..fc3c9bdc4 --- /dev/null +++ b/src/Capsule_TEST.cc @@ -0,0 +1,100 @@ +/* * Copyright (C) 2021 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 + +#include "test_config.h" // NOLINT(build/include) +#include "ignition/rendering/RenderEngine.hh" +#include "ignition/rendering/RenderingIface.hh" +#include "ignition/rendering/Capsule.hh" +#include "ignition/rendering/Scene.hh" + +using namespace ignition; +using namespace rendering; + +class CapsuleTest : public testing::Test, + public testing::WithParamInterface +{ + public: void Capsule(const std::string &_renderEngine); +}; + +///////////////////////////////////////////////// +void CapsuleTest::Capsule(const std::string &_renderEngine) +{ + if (_renderEngine != "ogre" && _renderEngine != "ogre2") + { + igndbg << "Capsule not supported yet in rendering engine: " + << _renderEngine << std::endl; + return; + } + + RenderEngine *engine = rendering::engine(_renderEngine); + if (!engine) + { + igndbg << "Engine '" << _renderEngine + << "' is not supported" << std::endl; + return; + } + + ScenePtr scene = engine->CreateScene("scene"); + + CapsulePtr capsule = scene->CreateCapsule(); + ASSERT_NE(nullptr, capsule); + + EXPECT_DOUBLE_EQ(capsule->Radius(), 0.5); + EXPECT_DOUBLE_EQ(capsule->Length(), 0.5); + + capsule->Radius(0.7); + capsule->Length(1.9); + + EXPECT_DOUBLE_EQ(capsule->Radius(), 0.7); + EXPECT_DOUBLE_EQ(capsule->Length(), 1.9); + + // create material + MaterialPtr mat = scene->CreateMaterial(); + mat->SetAmbient(0.6, 0.7, 0.8); + mat->SetDiffuse(0.3, 0.8, 0.2); + mat->SetSpecular(0.4, 0.9, 1.0); + + capsule->SetMaterial(mat); + MaterialPtr capsuleMat = capsule->Material(); + ASSERT_NE(nullptr, capsuleMat); + EXPECT_EQ(math::Color(0.6, 0.7, 0.8), capsuleMat->Ambient()); + EXPECT_EQ(math::Color(0.3, 0.8, 0.2), capsuleMat->Diffuse()); + EXPECT_EQ(math::Color(0.4, 0.9, 1.0), capsuleMat->Specular()); + + // Clean up + engine->DestroyScene(scene); + rendering::unloadEngine(engine->Name()); +} + +///////////////////////////////////////////////// +TEST_P(CapsuleTest, Capsule) +{ + Capsule(GetParam()); +} + +INSTANTIATE_TEST_CASE_P(Capsule, CapsuleTest, + RENDER_ENGINE_VALUES, + ignition::rendering::PrintToStringParam()); + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} From 574f34ce20b8fc2ba0c62c6a43b58eb50f77d955 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Wed, 20 Jan 2021 15:56:27 +0100 Subject: [PATCH 07/24] Fixed test Signed-off-by: ahcorde --- src/Capsule_TEST.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Capsule_TEST.cc b/src/Capsule_TEST.cc index fc3c9bdc4..9c9a9af97 100644 --- a/src/Capsule_TEST.cc +++ b/src/Capsule_TEST.cc @@ -59,8 +59,8 @@ void CapsuleTest::Capsule(const std::string &_renderEngine) EXPECT_DOUBLE_EQ(capsule->Radius(), 0.5); EXPECT_DOUBLE_EQ(capsule->Length(), 0.5); - capsule->Radius(0.7); - capsule->Length(1.9); + capsule->SetRadius(0.7); + capsule->SetLength(1.9); EXPECT_DOUBLE_EQ(capsule->Radius(), 0.7); EXPECT_DOUBLE_EQ(capsule->Length(), 1.9); From fa1bd1601f09f38a6bbbdd17320aa8114bd8d8bb Mon Sep 17 00:00:00 2001 From: ahcorde Date: Thu, 21 Jan 2021 15:31:39 +0100 Subject: [PATCH 08/24] Updated capsule Signed-off-by: ahcorde --- .../ignition/rendering/base/BaseCapsule.hh | 166 ------------------ .../ignition/rendering/ogre/OgreCapsule.hh | 3 + ogre/src/OgreCapsule.cc | 99 +++++++---- .../ignition/rendering/ogre2/Ogre2Capsule.hh | 3 + ogre2/src/Ogre2Capsule.cc | 84 ++++++--- 5 files changed, 129 insertions(+), 226 deletions(-) diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh index 39135d990..587ae3e3a 100644 --- a/include/ignition/rendering/base/BaseCapsule.hh +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -20,9 +20,6 @@ #include -#include -#include - #include "ignition/rendering/Capsule.hh" #include "ignition/rendering/base/BaseObject.hh" @@ -44,12 +41,6 @@ namespace ignition // Documentation inherited public: virtual ~BaseCapsule(); - // Documentation inherited - public: virtual void PreRender() override; - - // Documentation inherited. - public: virtual void Destroy() override; - // Documentation inherited public: virtual void SetRadius(const double _radius) override; @@ -62,15 +53,6 @@ namespace ignition // Documentation inherited public: virtual double Length() override; - /// \brief return the list of triangles, _indexes and _uv coordinates - /// \param[inout] _positions list with the triangles - /// \param[inout] _indexes indexes of the position list - /// \param[inout] _uv UV coordinates - protected: void CapsuleMesh( - std::vector &_positions, - std::vector &_indexes, - std::vector &_uv); - /// \brief Radius of the capsule protected: double radius = 0.5; @@ -95,20 +77,6 @@ namespace ignition { } - ///////////////////////////////////////////////// - template - void BaseCapsule::PreRender() - { - T::PreRender(); - } - - ////////////////////////////////////////////////// - template - void BaseCapsule::Destroy() - { - T::Destroy(); - } - ///////////////////////////////////////////////// template void BaseCapsule::SetRadius(const double _radius) @@ -138,140 +106,6 @@ namespace ignition { return this->length; } - ///////////////////////////////////////////////// - template - void BaseCapsule::CapsuleMesh( - std::vector &_positions, - std::vector &_indexes, - std::vector &_uv) - { - // Based on https://github.com/godotengine/godot primitive_meshes.cpp - int prevRow, thisRow, point; - float x, y, z, u, v, w; - float oneThird = 1.0 / 3.0; - float twoThirds = 2.0 / 3.0; - unsigned int rings = 12; - unsigned int radialSegments = 32; - - point = 0; - - /* top hemisphere */ - thisRow = 0; - prevRow = 0; - for (unsigned int j = 0; j <= (rings + 1); j++) { - v = j; - - v /= (rings + 1); - w = sin(0.5 * M_PI * v); - y = this->radius * cos(0.5 * M_PI * v); - - for (unsigned int i = 0; i <= radialSegments; i++) { - u = i; - u /= radialSegments; - - x = -sin(u * (M_PI * 2.0)); - z = cos(u * (M_PI * 2.0)); - - ignition::math::Vector3d p( - x * this->radius * w, y, -z * this->radius * w); - _positions.push_back( - p + ignition::math::Vector3d(0.0, 0.5 * this->length, 0.0)); - _uv.emplace_back(ignition::math::Vector2d(u, v * oneThird)); - point++; - - if (i > 0 && j > 0) { - _indexes.emplace_back(thisRow + i - 1); - _indexes.emplace_back(prevRow + i); - _indexes.emplace_back(prevRow + i - 1); - - _indexes.emplace_back(thisRow + i - 1); - _indexes.emplace_back(thisRow + i); - _indexes.emplace_back(prevRow + i); - } - } - prevRow = thisRow; - thisRow = point; - } - - /* cylinder */ - thisRow = point; - prevRow = 0; - for (unsigned int j = 0; j <= (rings + 1); j++) { - v = j; - v /= (rings + 1); - - y = this->length * v; - y = (this->length * 0.5) - y; - - for (unsigned int i = 0; i <= radialSegments; i++) { - u = i; - u /= radialSegments; - - x = -sin(u * (M_PI * 2.0)); - z = cos(u * (M_PI * 2.0)); - - ignition::math::Vector3d p( - x * this->radius, y, -z * this->radius); - _positions.emplace_back(p); - _uv.emplace_back( - ignition::math::Vector2d(u, oneThird + (v * oneThird))); - point++; - - if (i > 0 && j > 0) { - _indexes.emplace_back(thisRow + i - 1); - _indexes.emplace_back(prevRow + i); - _indexes.emplace_back(prevRow + i - 1); - - _indexes.emplace_back(thisRow + i - 1); - _indexes.emplace_back(thisRow + i); - _indexes.emplace_back(prevRow + i); - } - } - prevRow = thisRow; - thisRow = point; - } - - /* bottom hemisphere */ - thisRow = point; - prevRow = 0; - for (unsigned int j = 0; j <= (rings + 1); j++) { - v = j; - - v /= (rings + 1); - v += 1.0; - w = sin(0.5 * M_PI * v); - y = this->radius * cos(0.5 * M_PI * v); - - for (unsigned int i = 0; i <= radialSegments; i++) { - float u2 = i; - u2 /= radialSegments; - - x = -sin(u2 * (M_PI * 2.0)); - z = cos(u2 * (M_PI * 2.0)); - - ignition::math::Vector3d p( - x * this->radius * w, y, -z * this->radius * w); - _positions.emplace_back( - p + ignition::math::Vector3d(0.0, -0.5 * this->length, 0.0)); - _uv.emplace_back( - ignition::math::Vector2d(u2, twoThirds + ((v - 1.0) * oneThird))); - point++; - - if (i > 0 && j > 0) { - _indexes.emplace_back(thisRow + i - 1); - _indexes.emplace_back(prevRow + i); - _indexes.emplace_back(prevRow + i - 1); - - _indexes.emplace_back(thisRow + i - 1); - _indexes.emplace_back(thisRow + i); - _indexes.emplace_back(prevRow + i); - } - } - - prevRow = thisRow; - thisRow = point; - } - } } } } diff --git a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh index 079264ac4..e96489db6 100644 --- a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh +++ b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh @@ -49,6 +49,9 @@ namespace ignition // Documentation inherited. public: virtual void Init() override; + // Documentation inherited. + public: virtual void Destroy() override; + // Documentation inherited. public: virtual Ogre::MovableObject *OgreObject() const; diff --git a/ogre/src/OgreCapsule.cc b/ogre/src/OgreCapsule.cc index c6ef47aca..f89a4eabb 100644 --- a/ogre/src/OgreCapsule.cc +++ b/ogre/src/OgreCapsule.cc @@ -17,9 +17,14 @@ #include +#include +#include + #include "ignition/rendering/ogre/OgreCapsule.hh" #include "ignition/rendering/ogre/OgreMaterial.hh" #include "ignition/rendering/ogre/OgreScene.hh" +#include "ignition/rendering/ogre/OgreMesh.hh" +#include "ignition/rendering/ogre/OgreVisual.hh" #include @@ -28,7 +33,8 @@ class ignition::rendering::OgreCapsulePrivate /// \brief Capsule materal public: OgreMaterialPtr material; - public: Ogre::ManualObject *manualObject = nullptr; + /// \brief Mesh Object for capsule shape + public: OgreMeshPtr ogreMesh = nullptr; }; using namespace ignition; @@ -59,7 +65,7 @@ void OgreCapsule::PreRender() ////////////////////////////////////////////////// Ogre::MovableObject *OgreCapsule::OgreObject() const { - return this->dataPtr->manualObject; + return this->dataPtr->ogreMesh->OgreObject(); } ////////////////////////////////////////////////// @@ -69,51 +75,68 @@ void OgreCapsule::Init() } ////////////////////////////////////////////////// -void OgreCapsuleCapsule() +void OgreCapsule::Destroy() { - // no ops -} + if (!this->Scene()) + return; -////////////////////////////////////////////////// -void OgreCapsule::Create() -{ - if (!this->dataPtr->manualObject) + if (this->dataPtr->ogreMesh) { - this->dataPtr->manualObject = - this->scene->OgreSceneManager()->createManualObject(this->name); + this->dataPtr->ogreMesh->Destroy(); + this->dataPtr->ogreMesh.reset(); } - this->dataPtr->manualObject->clear(); - - this->dataPtr->manualObject->setCastShadows(false); - - std::string materialName = this->dataPtr->material ? - this->dataPtr->material->Name() : "Default/White"; - this->dataPtr->manualObject->begin(materialName, - Ogre::RenderOperation::OT_TRIANGLE_LIST); - - std::vector positions; - std::vector indexes; - std::vector uvs; - this->CapsuleMesh(positions, indexes, uvs); - - for (unsigned int i = 0; i < positions.size(); i++) + if (this->dataPtr->material && this->Scene()) { - this->dataPtr->manualObject->position( - Ogre::Vector3(positions[i].X(), positions[i].Y(), positions[i].Z())); - this->dataPtr->manualObject->textureCoord( - Ogre::Vector2(uvs[i].X(), uvs[i].Y())); - this->dataPtr->manualObject->normal( - positions[i].Normalize().X(), - positions[i].Normalize().Y(), - positions[i].Normalize().Z()); + this->Scene()->DestroyMaterial(this->dataPtr->material); + this->dataPtr->material.reset(); } +} - for (auto index : indexes) +////////////////////////////////////////////////// +void OgreCapsuleCapsule() +{ + // no ops +} + +////////////////////////////////////////////////// +void OgreCapsule::Create() +{ + common::MeshManager *meshMgr = common::MeshManager::Instance(); + std::string capsuleMeshName = this->Name() + "_capsule_mesh" + + "_" + std::to_string(this->radius) + + "_" + std::to_string(this->length); + if (!meshMgr->HasMesh(capsuleMeshName)) { - this->dataPtr->manualObject->index(index); + meshMgr->CreateCapsule(capsuleMeshName, this->radius, this->length, 32, 32); + MeshDescriptor meshDescriptor; + meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); + if (meshDescriptor.mesh != nullptr) + { + auto visual = std::dynamic_pointer_cast(this->Parent()); + // clear geom if needed + if (this->dataPtr->ogreMesh) + { + if (visual) + { + visual->RemoveGeometry( + std::dynamic_pointer_cast(shared_from_this())); + } + this->dataPtr->ogreMesh->Destroy(); + } + this->dataPtr->ogreMesh = + std::dynamic_pointer_cast(this->Scene()->CreateMesh(meshDescriptor)); + if (this->dataPtr->material != nullptr) + { + this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); + } + if (visual) + { + visual->AddGeometry( + std::dynamic_pointer_cast(shared_from_this())); + } + } } - this->dataPtr->manualObject->end(); } ////////////////////////////////////////////////// @@ -140,7 +163,7 @@ void OgreCapsule::SetMaterialImpl(OgreMaterialPtr _material) { std::string materialName = _material->Name(); Ogre::MaterialPtr ogreMaterial = _material->Material(); - this->dataPtr->manualObject->setMaterialName(0, materialName); + this->dataPtr->ogreMesh->SetMaterial(_material, false); this->dataPtr->material = _material; } diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index c3168ec77..2f731df6d 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -51,6 +51,9 @@ namespace ignition // Documentation inherited. public: virtual void Init() override; + // Documentation inherited. + public: virtual void Destroy() override; + // Documentation inherited. public: virtual Ogre::MovableObject *OgreObject() const; diff --git a/ogre2/src/Ogre2Capsule.cc b/ogre2/src/Ogre2Capsule.cc index 508297feb..72c7209e7 100644 --- a/ogre2/src/Ogre2Capsule.cc +++ b/ogre2/src/Ogre2Capsule.cc @@ -17,10 +17,14 @@ #include +#include +#include + #include "ignition/rendering/ogre2/Ogre2Capsule.hh" #include "ignition/rendering/ogre2/Ogre2Material.hh" #include "ignition/rendering/ogre2/Ogre2Scene.hh" -#include "ignition/rendering/ogre2/Ogre2DynamicRenderable.hh" +#include "ignition/rendering/ogre2/Ogre2Mesh.hh" +#include "ignition/rendering/ogre2/Ogre2Visual.hh" #include @@ -29,7 +33,8 @@ class ignition::rendering::Ogre2CapsulePrivate /// \brief Capsule materal public: Ogre2MaterialPtr material; - public: std::shared_ptr capsule = nullptr; + /// \brief Mesh Object for capsule shape + public: Ogre2MeshPtr ogreMesh = nullptr; }; using namespace ignition; @@ -50,7 +55,7 @@ Ogre2Capsule::~Ogre2Capsule() ////////////////////////////////////////////////// Ogre::MovableObject *Ogre2Capsule::OgreObject() const { - return this->dataPtr->capsule->OgreObject(); + return this->dataPtr->ogreMesh->OgreObject(); } ////////////////////////////////////////////////// @@ -69,31 +74,66 @@ void Ogre2Capsule::Init() this->Create(); } -//////////////////////////////////////////////// -void Ogre2Capsule::Create() +////////////////////////////////////////////////// +void Ogre2Capsule::Destroy() { - if (!this->dataPtr->capsule) + if (!this->Scene()) + return; + + if (this->dataPtr->ogreMesh) { - this->dataPtr->capsule.reset(new Ogre2DynamicRenderable(this->Scene())); + this->dataPtr->ogreMesh->Destroy(); + this->dataPtr->ogreMesh.reset(); } - this->dataPtr->capsule->Clear(); - this->dataPtr->capsule->Update(); - - this->dataPtr->capsule->SetOperationType(MT_TRIANGLE_LIST); - - std::vector positions; - std::vector indexes; - std::vector uvs; - - this->CapsuleMesh(positions, indexes, uvs); - - for (auto index : indexes) + if (this->dataPtr->material && this->Scene()) { - this->dataPtr->capsule->AddPoint(positions[index]); + this->Scene()->DestroyMaterial(this->dataPtr->material); + this->dataPtr->material.reset(); } +} - this->dataPtr->capsule->Update(); +//////////////////////////////////////////////// +void Ogre2Capsule::Create() +{ + std::cerr << "Create" << '\n'; + common::MeshManager *meshMgr = common::MeshManager::Instance(); + std::string capsuleMeshName = this->Name() + "_capsule_mesh" + + "_" + std::to_string(this->radius) + + "_" + std::to_string(this->length); + std::cerr << "capsuleMeshName " << capsuleMeshName << '\n'; + if (!meshMgr->HasMesh(capsuleMeshName)) + { + meshMgr->CreateCapsule(capsuleMeshName, this->radius, this->length, 12, 32); + MeshDescriptor meshDescriptor; + meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); + if (meshDescriptor.mesh != nullptr) + { + auto visual = std::dynamic_pointer_cast(this->Parent()); + + // clear geom if needed + if (this->dataPtr->ogreMesh) + { + if (visual) + { + visual->RemoveGeometry( + std::dynamic_pointer_cast(shared_from_this())); + } + this->dataPtr->ogreMesh->Destroy(); + } + this->dataPtr->ogreMesh = + std::dynamic_pointer_cast(this->Scene()->CreateMesh(meshDescriptor)); + if (this->dataPtr->material != nullptr) + { + this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); + } + if (visual) + { + visual->AddGeometry( + std::dynamic_pointer_cast(shared_from_this())); + } + } + } } ////////////////////////////////////////////////// @@ -113,7 +153,7 @@ void Ogre2Capsule::SetMaterial(MaterialPtr _material, bool _unique) } // Set material for the underlying dynamic renderable - this->dataPtr->capsule->SetMaterial(_material, false); + this->dataPtr->ogreMesh->SetMaterial(_material, false); this->SetMaterialImpl(derived); } From 70554665bdcacff36b27a39f18c57ebba912c3ad Mon Sep 17 00:00:00 2001 From: ahcorde Date: Mon, 25 Jan 2021 23:43:44 +0100 Subject: [PATCH 09/24] updated code Signed-off-by: ahcorde --- examples/gazebo_scene_viewer/SceneManager.cc | 13 +++++++++++++ examples/gazebo_scene_viewer/SceneManagerPrivate.hh | 3 +++ include/ignition/rendering/Marker.hh | 5 ++++- ogre/src/OgreMarker.cc | 7 +++++++ ogre2/src/Ogre2Capsule.cc | 2 -- ogre2/src/Ogre2Marker.cc | 6 ++++++ src/Marker_TEST.cc | 3 +++ 7 files changed, 36 insertions(+), 3 deletions(-) diff --git a/examples/gazebo_scene_viewer/SceneManager.cc b/examples/gazebo_scene_viewer/SceneManager.cc index 9847e1050..d7c54855c 100644 --- a/examples/gazebo_scene_viewer/SceneManager.cc +++ b/examples/gazebo_scene_viewer/SceneManager.cc @@ -1465,6 +1465,19 @@ void SubSceneManager::ProcessCone( _parent->AddGeometry(cone); } +////////////////////////////////////////////////// +void SubSceneManager::ProcessCapsule( + const gazebo::msgs::Geometry & _geometryMsg, VisualPtr _parent) +{ + GeometryPtr capsule = this->activeScene->CreateCapsule(); + const gazebo::msgs::CapsuleGeom &capsuleMsg = _geometryMsg.capsule(); + double x = 2 * capsuleMsg.radius(); + double y = 2 * capsuleMsg.radius(); + double z = capsuleMsg.length(); + _parent->SetLocalScale(x, y, z); + _parent->AddGeometry(capsule); +} + ////////////////////////////////////////////////// //! [process cylinder] void SubSceneManager::ProcessCylinder( diff --git a/examples/gazebo_scene_viewer/SceneManagerPrivate.hh b/examples/gazebo_scene_viewer/SceneManagerPrivate.hh index 5b1dbc3d8..918b16693 100644 --- a/examples/gazebo_scene_viewer/SceneManagerPrivate.hh +++ b/examples/gazebo_scene_viewer/SceneManagerPrivate.hh @@ -324,6 +324,9 @@ namespace ignition protected: virtual void ProcessCone( const gazebo::msgs::Geometry &_geometryMsg, VisualPtr _parent); + protected: virtual void ProcessCapsule( + const gazebo::msgs::Geometry &_geometryMsg, VisualPtr _parent); + protected: virtual void ProcessCylinder( const gazebo::msgs::Geometry &_geometryMsg, VisualPtr _parent); diff --git a/include/ignition/rendering/Marker.hh b/include/ignition/rendering/Marker.hh index 93a639b67..585293773 100644 --- a/include/ignition/rendering/Marker.hh +++ b/include/ignition/rendering/Marker.hh @@ -65,7 +65,10 @@ namespace ignition MT_TRIANGLE_LIST = 9, /// \brief Triangle strip primitive - MT_TRIANGLE_STRIP = 10 + MT_TRIANGLE_STRIP = 10, + + /// \brief Capsule geometry + MT_CAPSULE = 11, }; /// \class Marker Marker.hh ignition/rendering/Marker diff --git a/ogre/src/OgreMarker.cc b/ogre/src/OgreMarker.cc index 3aae5b6a4..a548e9c49 100644 --- a/ogre/src/OgreMarker.cc +++ b/ogre/src/OgreMarker.cc @@ -17,6 +17,7 @@ #include +#include "ignition/rendering/ogre/OgreCapsule.hh" #include "ignition/rendering/ogre/OgreDynamicLines.hh" #include "ignition/rendering/ogre/OgreMarker.hh" #include "ignition/rendering/ogre/OgreMaterial.hh" @@ -81,6 +82,7 @@ Ogre::MovableObject *OgreMarker::OgreObject() const case MT_NONE: return nullptr; case MT_BOX: + case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: return this->dataPtr->mesh->OgreObject(); @@ -145,6 +147,7 @@ void OgreMarker::SetMaterial(MaterialPtr _material, bool _unique) case MT_NONE: break; case MT_BOX: + case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: this->dataPtr->mesh->SetMaterial(derived, false); @@ -205,6 +208,10 @@ void OgreMarker::SetType(MarkerType _markerType) this->dataPtr->mesh = std::dynamic_pointer_cast(this->scene->CreateBox()); break; + case MT_CAPSULE: + this->dataPtr->mesh = + std::dynamic_pointer_cast(this->scene->CreateCapsule()); + break; case MT_CYLINDER: this->dataPtr->mesh = std::dynamic_pointer_cast(this->scene->CreateCylinder()); diff --git a/ogre2/src/Ogre2Capsule.cc b/ogre2/src/Ogre2Capsule.cc index 72c7209e7..f51d34701 100644 --- a/ogre2/src/Ogre2Capsule.cc +++ b/ogre2/src/Ogre2Capsule.cc @@ -96,12 +96,10 @@ void Ogre2Capsule::Destroy() //////////////////////////////////////////////// void Ogre2Capsule::Create() { - std::cerr << "Create" << '\n'; common::MeshManager *meshMgr = common::MeshManager::Instance(); std::string capsuleMeshName = this->Name() + "_capsule_mesh" + "_" + std::to_string(this->radius) + "_" + std::to_string(this->length); - std::cerr << "capsuleMeshName " << capsuleMeshName << '\n'; if (!meshMgr->HasMesh(capsuleMeshName)) { meshMgr->CreateCapsule(capsuleMeshName, this->radius, this->length, 12, 32); diff --git a/ogre2/src/Ogre2Marker.cc b/ogre2/src/Ogre2Marker.cc index 82f2be376..54f793bb1 100644 --- a/ogre2/src/Ogre2Marker.cc +++ b/ogre2/src/Ogre2Marker.cc @@ -17,6 +17,7 @@ #include +#include "ignition/rendering/ogre2/Ogre2Capsule.hh" #include "ignition/rendering/ogre2/Ogre2DynamicRenderable.hh" #include "ignition/rendering/ogre2/Ogre2Marker.hh" #include "ignition/rendering/ogre2/Ogre2Material.hh" @@ -94,6 +95,7 @@ Ogre::MovableObject *Ogre2Marker::OgreObject() const case MT_NONE: return nullptr; case MT_BOX: + case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: return this->dataPtr->mesh->OgreObject(); @@ -158,6 +160,7 @@ void Ogre2Marker::SetMaterial(MaterialPtr _material, bool _unique) case MT_NONE: break; case MT_BOX: + case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: this->dataPtr->mesh->SetMaterial(derived, false); @@ -238,6 +241,9 @@ void Ogre2Marker::SetType(MarkerType _markerType) case MT_BOX: newMesh = this->scene->CreateBox(); break; + case MT_CAPSULE: + newMesh = this->scene->CreateCapsule(); + break; case MT_CYLINDER: newMesh = this->scene->CreateCylinder(); break; diff --git a/src/Marker_TEST.cc b/src/Marker_TEST.cc index 486d9064d..213ecc109 100644 --- a/src/Marker_TEST.cc +++ b/src/Marker_TEST.cc @@ -76,6 +76,9 @@ void MarkerTest::Marker(const std::string &_renderEngine) EXPECT_EQ(-2, marker->Layer()); // type + marker->SetType(MarkerType::MT_CAPSULE); + EXPECT_EQ(MarkerType::MT_CAPSULE, marker->Type()); + marker->SetType(MarkerType::MT_CYLINDER); EXPECT_EQ(MarkerType::MT_CYLINDER, marker->Type()); marker->SetType(MarkerType::MT_NONE); From d438660df82aea208ae474549776ba789e6fbfb2 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Fri, 12 Feb 2021 08:25:53 +0100 Subject: [PATCH 10/24] make linters happy Signed-off-by: ahcorde --- ogre/src/OgreCapsule.cc | 3 ++- ogre2/src/Ogre2Capsule.cc | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ogre/src/OgreCapsule.cc b/ogre/src/OgreCapsule.cc index f89a4eabb..607799c24 100644 --- a/ogre/src/OgreCapsule.cc +++ b/ogre/src/OgreCapsule.cc @@ -125,7 +125,8 @@ void OgreCapsule::Create() this->dataPtr->ogreMesh->Destroy(); } this->dataPtr->ogreMesh = - std::dynamic_pointer_cast(this->Scene()->CreateMesh(meshDescriptor)); + std::dynamic_pointer_cast(t + his->Scene()->CreateMesh(meshDescriptor)); if (this->dataPtr->material != nullptr) { this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); diff --git a/ogre2/src/Ogre2Capsule.cc b/ogre2/src/Ogre2Capsule.cc index f51d34701..eba9256e7 100644 --- a/ogre2/src/Ogre2Capsule.cc +++ b/ogre2/src/Ogre2Capsule.cc @@ -120,7 +120,8 @@ void Ogre2Capsule::Create() this->dataPtr->ogreMesh->Destroy(); } this->dataPtr->ogreMesh = - std::dynamic_pointer_cast(this->Scene()->CreateMesh(meshDescriptor)); + std::dynamic_pointer_cast( + this->Scene()->CreateMesh(meshDescriptor)); if (this->dataPtr->material != nullptr) { this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); From 26697aba41f915f141add49233cdc84df546bcb4 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Fri, 12 Feb 2021 08:30:02 +0100 Subject: [PATCH 11/24] Fixed merge Signed-off-by: ahcorde --- ogre/src/OgreScene.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/ogre/src/OgreScene.cc b/ogre/src/OgreScene.cc index c3f95bb0f..7fe8c4e44 100644 --- a/ogre/src/OgreScene.cc +++ b/ogre/src/OgreScene.cc @@ -21,7 +21,6 @@ #include "ignition/rendering/ogre/OgreAxisVisual.hh" #include "ignition/rendering/ogre/OgreCamera.hh" #include "ignition/rendering/ogre/OgreCapsule.hh" -#include "ignition/rendering/ogre/OgreDepthCamera.hh" #include "ignition/rendering/ogre/OgreConversions.hh" #include "ignition/rendering/ogre/OgreDepthCamera.hh" #include "ignition/rendering/ogre/OgreGeometry.hh" From 08ce78680360b16eb3e1d77ad29fdbe3422af337 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Fri, 12 Feb 2021 10:11:44 +0100 Subject: [PATCH 12/24] quick fix Signed-off-by: ahcorde --- ogre/src/OgreCapsule.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ogre/src/OgreCapsule.cc b/ogre/src/OgreCapsule.cc index 607799c24..5b1dd3345 100644 --- a/ogre/src/OgreCapsule.cc +++ b/ogre/src/OgreCapsule.cc @@ -125,8 +125,8 @@ void OgreCapsule::Create() this->dataPtr->ogreMesh->Destroy(); } this->dataPtr->ogreMesh = - std::dynamic_pointer_cast(t - his->Scene()->CreateMesh(meshDescriptor)); + std::dynamic_pointer_cast( + this->Scene()->CreateMesh(meshDescriptor)); if (this->dataPtr->material != nullptr) { this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); From bfabcd1785cdcf775191825f19bf6510cf5cd860 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Wed, 3 Mar 2021 08:18:17 +0100 Subject: [PATCH 13/24] Added feedback Signed-off-by: ahcorde --- examples/gazebo_scene_viewer/SceneManager.cc | 3 +++ include/ignition/rendering/Capsule.hh | 8 ++++++-- include/ignition/rendering/base/BaseCapsule.hh | 10 +++++----- ogre/include/ignition/rendering/ogre/OgreCapsule.hh | 2 +- ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh | 10 +++------- ogre2/src/Ogre2Camera.cc | 2 +- 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/examples/gazebo_scene_viewer/SceneManager.cc b/examples/gazebo_scene_viewer/SceneManager.cc index d7c54855c..89e4a84a9 100644 --- a/examples/gazebo_scene_viewer/SceneManager.cc +++ b/examples/gazebo_scene_viewer/SceneManager.cc @@ -1797,6 +1797,9 @@ void SubSceneManager::CreateGeometryFunctionMap() // this->geomFunctions[gazebo::msgs::Geometry::CONE] = // &SubSceneManager::ProcessCone; + this->geomFunctions[gazebo::msgs::Geometry::CAPSULE] = + &SubSceneManager::ProcessSphere; + this->geomFunctions[gazebo::msgs::Geometry::CYLINDER] = &SubSceneManager::ProcessCylinder; diff --git a/include/ignition/rendering/Capsule.hh b/include/ignition/rendering/Capsule.hh index 8d2dbabee..a72196a81 100644 --- a/include/ignition/rendering/Capsule.hh +++ b/include/ignition/rendering/Capsule.hh @@ -37,12 +37,16 @@ namespace ignition /// \brief Destructor public: virtual ~Capsule() { } - public: virtual void SetRadius(const double _radius) = 0; + /// \brief Set the radius of the capsule + public: virtual void SetRadius(double _radius) = 0; - public: virtual void SetLength(const double _length) = 0; + /// \brief Set the length of the capsule + public: virtual void SetLength(double _length) = 0; + /// \brief Get the radius of the capsule public: virtual double Radius() = 0; + /// \brief Get the length of the capsule public: virtual double Length() = 0; }; } diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh index 587ae3e3a..24a1c1659 100644 --- a/include/ignition/rendering/base/BaseCapsule.hh +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -29,7 +29,7 @@ namespace ignition { inline namespace IGNITION_RENDERING_VERSION_NAMESPACE { - /// \brief Base implementation of a Capsule Visual + /// \brief Base implementation of a Capsule Geometry template class BaseCapsule : public virtual Capsule, @@ -42,10 +42,10 @@ namespace ignition public: virtual ~BaseCapsule(); // Documentation inherited - public: virtual void SetRadius(const double _radius) override; + public: virtual void SetRadius(double _radius) override; // Documentation inherited - public: virtual void SetLength(const double _length) override; + public: virtual void SetLength(double _length) override; // Documentation inherited public: virtual double Radius() override; @@ -79,7 +79,7 @@ namespace ignition ///////////////////////////////////////////////// template - void BaseCapsule::SetRadius(const double _radius) + void BaseCapsule::SetRadius(double _radius) { this->radius = _radius; this->capsuleDirty = true; @@ -94,7 +94,7 @@ namespace ignition ///////////////////////////////////////////////// template - void BaseCapsule::SetLength(const double _length) + void BaseCapsule::SetLength(double _length) { this->length = _length; this->capsuleDirty = true; diff --git a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh index e96489db6..f2433a7e1 100644 --- a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh +++ b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh @@ -68,7 +68,7 @@ namespace ignition /// \param[in] _material Ogre material. protected: virtual void SetMaterialImpl(OgreMaterialPtr _material); - /// \brief Create the Capsule Visual in ogre + /// \brief Create the Capsule geometry in ogre private: void Create(); /// \brief Capsule should only be created by scene. diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index 2f731df6d..4e4fd36ff 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -15,8 +15,8 @@ * */ -#ifndef IGNITION_RENDERING_OGRE2_OGRECAPSULE_HH_ -#define IGNITION_RENDERING_OGRE2_OGRECAPSULE_HH_ +#ifndef IGNITION_RENDERING_OGRE2_OGRE2CAPSULE_HH_ +#define IGNITION_RENDERING_OGRE2_OGRE2CAPSULE_HH_ #include #include @@ -70,16 +70,12 @@ namespace ignition /// \param[in] _material Ogre material. protected: virtual void SetMaterialImpl(Ogre2MaterialPtr _material); - /// \brief Create the Capsule Visual in ogre + /// \brief Create the Capsule geometry in ogre private: void Create(); /// \brief Capsule should only be created by scene. private: friend class Ogre2Scene; - private: void calculateRing( - size_t segments, float radius, float y, float dy, - std::vector & positions); - /// \brief Private data class private: std::unique_ptr dataPtr; }; diff --git a/ogre2/src/Ogre2Camera.cc b/ogre2/src/Ogre2Camera.cc index 2d97eafaa..04a463085 100644 --- a/ogre2/src/Ogre2Camera.cc +++ b/ogre2/src/Ogre2Camera.cc @@ -220,7 +220,7 @@ VisualPtr Ogre2Camera::VisualAt(const ignition::math::Vector2i &_mousePos) } } - float ratio = screenScalingFactor(); + float ratio = 1;//screenScalingFactor(); ignition::math::Vector2i mousePos( static_cast(std::rint(ratio * _mousePos.X())), static_cast(std::rint(ratio * _mousePos.Y()))); From 7df5969bc581ed136575eea6169813962cd8e779 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Wed, 17 Mar 2021 19:01:17 +0100 Subject: [PATCH 14/24] Added feedback Signed-off-by: ahcorde --- .../ignition/rendering/base/BaseCapsule.hh | 2 -- .../ignition/rendering/ogre/OgreCapsule.hh | 10 +++----- ogre/src/OgreCapsule.cc | 25 ++++++------------- .../ignition/rendering/ogre2/Ogre2Capsule.hh | 10 +++----- ogre2/src/Ogre2Camera.cc | 2 +- ogre2/src/Ogre2Capsule.cc | 18 ++++++------- src/Capsule_TEST.cc | 3 ++- 7 files changed, 25 insertions(+), 45 deletions(-) diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh index 24a1c1659..ae1a905ad 100644 --- a/include/ignition/rendering/base/BaseCapsule.hh +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -18,8 +18,6 @@ #ifndef IGNITION_RENDERING_BASECAPSULE_HH_ #define IGNITION_RENDERING_BASECAPSULE_HH_ -#include - #include "ignition/rendering/Capsule.hh" #include "ignition/rendering/base/BaseObject.hh" diff --git a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh index f2433a7e1..722f7f8d3 100644 --- a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh +++ b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh @@ -53,20 +53,16 @@ namespace ignition public: virtual void Destroy() override; // Documentation inherited. - public: virtual Ogre::MovableObject *OgreObject() const; + public: virtual Ogre::MovableObject *OgreObject() const override; // Documentation inherited. public: virtual void PreRender() override; // Documentation inherited. - public: virtual MaterialPtr Material() const; + public: virtual MaterialPtr Material() const override; // Documentation inherited. - public: virtual void SetMaterial(MaterialPtr _material, bool _unique); - - /// \brief Set material to capsule geometry. - /// \param[in] _material Ogre material. - protected: virtual void SetMaterialImpl(OgreMaterialPtr _material); + public: virtual void SetMaterial(MaterialPtr _material, bool _unique) override; /// \brief Create the Capsule geometry in ogre private: void Create(); diff --git a/ogre/src/OgreCapsule.cc b/ogre/src/OgreCapsule.cc index 5b1dd3345..309355a77 100644 --- a/ogre/src/OgreCapsule.cc +++ b/ogre/src/OgreCapsule.cc @@ -93,12 +93,6 @@ void OgreCapsule::Destroy() } } -////////////////////////////////////////////////// -void OgreCapsuleCapsule() -{ - // no ops -} - ////////////////////////////////////////////////// void OgreCapsule::Create() { @@ -111,7 +105,12 @@ void OgreCapsule::Create() meshMgr->CreateCapsule(capsuleMeshName, this->radius, this->length, 32, 32); MeshDescriptor meshDescriptor; meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); - if (meshDescriptor.mesh != nullptr) + if (meshDescriptor.mesh == nullptr) + { + ignerr << "Capsule mesh is unavailable in the Mesh Manager" << std::endl; + return; + } + else { auto visual = std::dynamic_pointer_cast(this->Parent()); // clear geom if needed @@ -156,16 +155,8 @@ void OgreCapsule::SetMaterial(MaterialPtr _material, bool _unique) return; } - this->SetMaterialImpl(derived); -} - -////////////////////////////////////////////////// -void OgreCapsule::SetMaterialImpl(OgreMaterialPtr _material) -{ - std::string materialName = _material->Name(); - Ogre::MaterialPtr ogreMaterial = _material->Material(); - this->dataPtr->ogreMesh->SetMaterial(_material, false); - this->dataPtr->material = _material; + this->dataPtr->ogreMesh->SetMaterial(derived, false); + this->dataPtr->material = derived; } ////////////////////////////////////////////////// diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index 4e4fd36ff..90ab0c99f 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -55,20 +55,16 @@ namespace ignition public: virtual void Destroy() override; // Documentation inherited. - public: virtual Ogre::MovableObject *OgreObject() const; + public: virtual Ogre::MovableObject *OgreObject() const override; // Documentation inherited. public: virtual void PreRender() override; // Documentation inherited. - public: virtual MaterialPtr Material() const; + public: virtual MaterialPtr Material() const override; // Documentation inherited. - public: virtual void SetMaterial(MaterialPtr _material, bool _unique); - - /// \brief Set material to capsule geometry. - /// \param[in] _material Ogre material. - protected: virtual void SetMaterialImpl(Ogre2MaterialPtr _material); + public: virtual void SetMaterial(MaterialPtr _material, bool _unique) override; /// \brief Create the Capsule geometry in ogre private: void Create(); diff --git a/ogre2/src/Ogre2Camera.cc b/ogre2/src/Ogre2Camera.cc index 04a463085..2d97eafaa 100644 --- a/ogre2/src/Ogre2Camera.cc +++ b/ogre2/src/Ogre2Camera.cc @@ -220,7 +220,7 @@ VisualPtr Ogre2Camera::VisualAt(const ignition::math::Vector2i &_mousePos) } } - float ratio = 1;//screenScalingFactor(); + float ratio = screenScalingFactor(); ignition::math::Vector2i mousePos( static_cast(std::rint(ratio * _mousePos.X())), static_cast(std::rint(ratio * _mousePos.Y()))); diff --git a/ogre2/src/Ogre2Capsule.cc b/ogre2/src/Ogre2Capsule.cc index eba9256e7..c8a04e8eb 100644 --- a/ogre2/src/Ogre2Capsule.cc +++ b/ogre2/src/Ogre2Capsule.cc @@ -31,7 +31,7 @@ class ignition::rendering::Ogre2CapsulePrivate { /// \brief Capsule materal - public: Ogre2MaterialPtr material; + public: Ogre2MaterialPtr material{nullptr}; /// \brief Mesh Object for capsule shape public: Ogre2MeshPtr ogreMesh = nullptr; @@ -105,7 +105,12 @@ void Ogre2Capsule::Create() meshMgr->CreateCapsule(capsuleMeshName, this->radius, this->length, 12, 32); MeshDescriptor meshDescriptor; meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); - if (meshDescriptor.mesh != nullptr) + if (meshDescriptor.mesh == nullptr) + { + ignerr << "Capsule mesh is unavailable in the Mesh Manager" << std::endl; + return; + } + else { auto visual = std::dynamic_pointer_cast(this->Parent()); @@ -153,14 +158,7 @@ void Ogre2Capsule::SetMaterial(MaterialPtr _material, bool _unique) // Set material for the underlying dynamic renderable this->dataPtr->ogreMesh->SetMaterial(_material, false); - this->SetMaterialImpl(derived); -} - -////////////////////////////////////////////////// -void Ogre2Capsule::SetMaterialImpl(Ogre2MaterialPtr _material) -{ - Ogre::MaterialPtr ogreMaterial = _material->Material(); - this->dataPtr->material = _material; + this->dataPtr->material = derived; } ////////////////////////////////////////////////// diff --git a/src/Capsule_TEST.cc b/src/Capsule_TEST.cc index 9c9a9af97..f43172dfe 100644 --- a/src/Capsule_TEST.cc +++ b/src/Capsule_TEST.cc @@ -1,4 +1,5 @@ -/* * Copyright (C) 2021 Open Source Robotics Foundation +/* + * Copyright (C) 2021 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 7775b58c88f95bb0edf32ee82421d0021de59131 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Wed, 17 Mar 2021 19:06:40 +0100 Subject: [PATCH 15/24] make linters happy Signed-off-by: ahcorde --- ogre/include/ignition/rendering/ogre/OgreCapsule.hh | 3 ++- ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh index 722f7f8d3..b38387ec1 100644 --- a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh +++ b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh @@ -62,7 +62,8 @@ namespace ignition public: virtual MaterialPtr Material() const override; // Documentation inherited. - public: virtual void SetMaterial(MaterialPtr _material, bool _unique) override; + public: virtual void + SetMaterial(MaterialPtr _material, bool _unique) override; /// \brief Create the Capsule geometry in ogre private: void Create(); diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index 90ab0c99f..d692e9178 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -64,7 +64,8 @@ namespace ignition public: virtual MaterialPtr Material() const override; // Documentation inherited. - public: virtual void SetMaterial(MaterialPtr _material, bool _unique) override; + public: virtual void + SetMaterial(MaterialPtr _material, bool _unique) override; /// \brief Create the Capsule geometry in ogre private: void Create(); From 97b5bf713ff7bcb5396ffe13dbb95deb6c556e29 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Thu, 18 Mar 2021 13:35:29 +0100 Subject: [PATCH 16/24] Removed capsule from markers Signed-off-by: ahcorde --- ogre2/src/Ogre2Marker.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ogre2/src/Ogre2Marker.cc b/ogre2/src/Ogre2Marker.cc index 54f793bb1..314860340 100644 --- a/ogre2/src/Ogre2Marker.cc +++ b/ogre2/src/Ogre2Marker.cc @@ -95,7 +95,6 @@ Ogre::MovableObject *Ogre2Marker::OgreObject() const case MT_NONE: return nullptr; case MT_BOX: - case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: return this->dataPtr->mesh->OgreObject(); @@ -160,7 +159,6 @@ void Ogre2Marker::SetMaterial(MaterialPtr _material, bool _unique) case MT_NONE: break; case MT_BOX: - case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: this->dataPtr->mesh->SetMaterial(derived, false); @@ -241,9 +239,6 @@ void Ogre2Marker::SetType(MarkerType _markerType) case MT_BOX: newMesh = this->scene->CreateBox(); break; - case MT_CAPSULE: - newMesh = this->scene->CreateCapsule(); - break; case MT_CYLINDER: newMesh = this->scene->CreateCylinder(); break; From 09d7b20be2d97fb1a263ea780420b50c08a32f62 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Thu, 18 Mar 2021 14:22:20 +0100 Subject: [PATCH 17/24] Fix capsule material issue Signed-off-by: ahcorde --- ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh | 7 +++++-- ogre2/src/Ogre2Capsule.cc | 8 +++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index d692e9178..e7cdbbf67 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -64,8 +64,11 @@ namespace ignition public: virtual MaterialPtr Material() const override; // Documentation inherited. - public: virtual void - SetMaterial(MaterialPtr _material, bool _unique) override; + public: virtual void SetMaterial(MaterialPtr _material, bool _unique) override; + + /// \brief Set material to capsule geometry. + /// \param[in] _material Ogre material. + protected: virtual void SetMaterialImpl(Ogre2MaterialPtr _material); /// \brief Create the Capsule geometry in ogre private: void Create(); diff --git a/ogre2/src/Ogre2Capsule.cc b/ogre2/src/Ogre2Capsule.cc index c8a04e8eb..8a53e0118 100644 --- a/ogre2/src/Ogre2Capsule.cc +++ b/ogre2/src/Ogre2Capsule.cc @@ -158,7 +158,13 @@ void Ogre2Capsule::SetMaterial(MaterialPtr _material, bool _unique) // Set material for the underlying dynamic renderable this->dataPtr->ogreMesh->SetMaterial(_material, false); - this->dataPtr->material = derived; + this->SetMaterialImpl(derived); +} + +////////////////////////////////////////////////// +void Ogre2Capsule::SetMaterialImpl(Ogre2MaterialPtr _material) +{ + this->dataPtr->material = _material; } ////////////////////////////////////////////////// From e7e4597063004ce2e599abe1309018d13c28605d Mon Sep 17 00:00:00 2001 From: ahcorde Date: Thu, 18 Mar 2021 14:31:26 +0100 Subject: [PATCH 18/24] Fixed Ogre2Marker Signed-off-by: ahcorde --- ogre2/src/Ogre2Marker.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/ogre2/src/Ogre2Marker.cc b/ogre2/src/Ogre2Marker.cc index 314860340..8a0c07d39 100644 --- a/ogre2/src/Ogre2Marker.cc +++ b/ogre2/src/Ogre2Marker.cc @@ -17,6 +17,9 @@ #include +#include +#include + #include "ignition/rendering/ogre2/Ogre2Capsule.hh" #include "ignition/rendering/ogre2/Ogre2DynamicRenderable.hh" #include "ignition/rendering/ogre2/Ogre2Marker.hh" @@ -95,6 +98,7 @@ Ogre::MovableObject *Ogre2Marker::OgreObject() const case MT_NONE: return nullptr; case MT_BOX: + case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: return this->dataPtr->mesh->OgreObject(); @@ -159,6 +163,7 @@ void Ogre2Marker::SetMaterial(MaterialPtr _material, bool _unique) case MT_NONE: break; case MT_BOX: + case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: this->dataPtr->mesh->SetMaterial(derived, false); @@ -239,6 +244,40 @@ void Ogre2Marker::SetType(MarkerType _markerType) case MT_BOX: newMesh = this->scene->CreateBox(); break; + case MT_CAPSULE: + { + common::MeshManager *meshMgr = common::MeshManager::Instance(); + std::string capsuleMeshName = std::string("marker_capsule_mesh") + + "_" + std::to_string(0.5) + + "_" + std::to_string(1.0); + if (!meshMgr->HasMesh(capsuleMeshName)) + { + meshMgr->CreateCapsule(capsuleMeshName, 0.5, 1.0, 12, 32); + MeshDescriptor meshDescriptor; + meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); + if (meshDescriptor.mesh == nullptr) + { + ignerr << "Capsule mesh is unavailable in the Mesh Manager" << std::endl; + return; + } + else + { + this->dataPtr->mesh = + std::dynamic_pointer_cast( + this->Scene()->CreateMesh(meshDescriptor)); + if (this->dataPtr->material != nullptr) + { + this->dataPtr->mesh->SetMaterial(this->dataPtr->material, false); + } + if (visual) + { + visual->AddGeometry( + std::dynamic_pointer_cast(shared_from_this())); + } + } + } + } + return; case MT_CYLINDER: newMesh = this->scene->CreateCylinder(); break; From e8c9d0659f016b6f8392bad564957416788a3130 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Thu, 18 Mar 2021 14:34:52 +0100 Subject: [PATCH 19/24] make linters happy Signed-off-by: ahcorde --- ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index e7cdbbf67..ebb2d6616 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -64,7 +64,8 @@ namespace ignition public: virtual MaterialPtr Material() const override; // Documentation inherited. - public: virtual void SetMaterial(MaterialPtr _material, bool _unique) override; + public: virtual void + SetMaterial(MaterialPtr _material, bool _unique) override; /// \brief Set material to capsule geometry. /// \param[in] _material Ogre material. From decd32ed2f17320581d14630880cb6a3fdde50a1 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Thu, 18 Mar 2021 14:43:45 +0100 Subject: [PATCH 20/24] make linters happy Signed-off-by: ahcorde --- ogre2/src/Ogre2Marker.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ogre2/src/Ogre2Marker.cc b/ogre2/src/Ogre2Marker.cc index 8a0c07d39..8cab1570b 100644 --- a/ogre2/src/Ogre2Marker.cc +++ b/ogre2/src/Ogre2Marker.cc @@ -257,7 +257,8 @@ void Ogre2Marker::SetType(MarkerType _markerType) meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); if (meshDescriptor.mesh == nullptr) { - ignerr << "Capsule mesh is unavailable in the Mesh Manager" << std::endl; + ignerr << "Capsule mesh is unavailable in the Mesh Manager" + << std::endl; return; } else From d7791590eaeac3cd16a5dbd4ea3b74669eeca279 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Thu, 18 Mar 2021 17:44:16 +0100 Subject: [PATCH 21/24] Removed macos warning Signed-off-by: ahcorde --- ogre/include/ignition/rendering/ogre/OgreScene.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ogre/include/ignition/rendering/ogre/OgreScene.hh b/ogre/include/ignition/rendering/ogre/OgreScene.hh index 9bdf701ca..11145b2ac 100644 --- a/ogre/include/ignition/rendering/ogre/OgreScene.hh +++ b/ogre/include/ignition/rendering/ogre/OgreScene.hh @@ -149,7 +149,7 @@ namespace ignition // Documentation inherited protected: virtual CapsulePtr CreateCapsuleImpl(unsigned int _id, - const std::string &_name); + const std::string &_name) override; protected: virtual GridPtr CreateGridImpl( unsigned int _id, From 32c501e37f5531f39307433252aff51653c0bc79 Mon Sep 17 00:00:00 2001 From: ahcorde Date: Fri, 19 Mar 2021 09:48:06 +0100 Subject: [PATCH 22/24] Added feedback Signed-off-by: ahcorde --- examples/simple_demo/Main.cc | 14 +++++++------- include/ignition/rendering/Capsule.hh | 4 ++-- include/ignition/rendering/base/BaseCapsule.hh | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/simple_demo/Main.cc b/examples/simple_demo/Main.cc index 09977d1f2..6d8164181 100644 --- a/examples/simple_demo/Main.cc +++ b/examples/simple_demo/Main.cc @@ -137,16 +137,16 @@ void buildScene(ScenePtr _scene) white->SetRenderOrder(0); //! [white material] - VisualPtr capsule_visual = _scene->CreateVisual(); + VisualPtr capsuleVisual = _scene->CreateVisual(); CapsulePtr capsule = _scene->CreateCapsule(); capsule->SetLength(0.2); capsule->SetRadius(0.2); - capsule_visual->AddGeometry(capsule); - capsule_visual->SetOrigin(0.0, 0.0, 0.0); - capsule_visual->SetLocalPosition(4, 2, 0); - capsule_visual->SetLocalScale(1, 1, 1); - capsule_visual->SetMaterial(red); - root->AddChild(capsule_visual); + capsuleVisual->AddGeometry(capsule); + capsuleVisual->SetOrigin(0.0, 0.0, 0.0); + capsuleVisual->SetLocalPosition(4, 2, 0); + capsuleVisual->SetLocalScale(1, 1, 1); + capsuleVisual->SetMaterial(red); + root->AddChild(capsuleVisual); // create plane visual VisualPtr plane = _scene->CreateVisual(); diff --git a/include/ignition/rendering/Capsule.hh b/include/ignition/rendering/Capsule.hh index a72196a81..ddfc2a6cd 100644 --- a/include/ignition/rendering/Capsule.hh +++ b/include/ignition/rendering/Capsule.hh @@ -44,10 +44,10 @@ namespace ignition public: virtual void SetLength(double _length) = 0; /// \brief Get the radius of the capsule - public: virtual double Radius() = 0; + public: virtual double Radius() const = 0; /// \brief Get the length of the capsule - public: virtual double Length() = 0; + public: virtual double Length() const = 0; }; } } diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh index ae1a905ad..d4450d2c2 100644 --- a/include/ignition/rendering/base/BaseCapsule.hh +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -46,10 +46,10 @@ namespace ignition public: virtual void SetLength(double _length) override; // Documentation inherited - public: virtual double Radius() override; + public: virtual double Radius() const override; // Documentation inherited - public: virtual double Length() override; + public: virtual double Length() const override; /// \brief Radius of the capsule protected: double radius = 0.5; @@ -85,7 +85,7 @@ namespace ignition ///////////////////////////////////////////////// template - double BaseCapsule::Radius() + double BaseCapsule::Radius() const { return this->radius; } @@ -100,7 +100,7 @@ namespace ignition ///////////////////////////////////////////////// template - double BaseCapsule::Length() + double BaseCapsule::Length() const { return this->length; } From 83681f6705b98c30c0cd3b9089feaf7eca0bf044 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 19 Mar 2021 11:44:55 -0700 Subject: [PATCH 23/24] fixing windows warnings Signed-off-by: Ian Chen --- ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh | 2 +- src/Capsule_TEST.cc | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index ebb2d6616..d1f99cd2c 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -38,7 +38,7 @@ namespace ignition // Forward declaration class Ogre2CapsulePrivate; - /// \brief Ogre 2.x implementation of a Capsule Visual. + /// \brief Ogre 2.x implementation of a Capsule Geometry. class IGNITION_RENDERING_OGRE2_VISIBLE Ogre2Capsule : public BaseCapsule { diff --git a/src/Capsule_TEST.cc b/src/Capsule_TEST.cc index f43172dfe..99e2b7ad9 100644 --- a/src/Capsule_TEST.cc +++ b/src/Capsule_TEST.cc @@ -75,9 +75,9 @@ void CapsuleTest::Capsule(const std::string &_renderEngine) capsule->SetMaterial(mat); MaterialPtr capsuleMat = capsule->Material(); ASSERT_NE(nullptr, capsuleMat); - EXPECT_EQ(math::Color(0.6, 0.7, 0.8), capsuleMat->Ambient()); - EXPECT_EQ(math::Color(0.3, 0.8, 0.2), capsuleMat->Diffuse()); - EXPECT_EQ(math::Color(0.4, 0.9, 1.0), capsuleMat->Specular()); + EXPECT_EQ(math::Color(0.6f, 0.7f, 0.8f), capsuleMat->Ambient()); + EXPECT_EQ(math::Color(0.3f, 0.8f, 0.2f), capsuleMat->Diffuse()); + EXPECT_EQ(math::Color(0.4f, 0.9f, 1.0f), capsuleMat->Specular()); // Clean up engine->DestroyScene(scene); From 223ed97c649c52b3d755bfc49622b9089469a594 Mon Sep 17 00:00:00 2001 From: Louise Poubel Date: Fri, 19 Mar 2021 21:04:49 -0700 Subject: [PATCH 24/24] Suggestions to #200 (#276) Signed-off-by: Louise Poubel --- include/ignition/rendering/Capsule.hh | 6 +- .../ignition/rendering/base/BaseCapsule.hh | 1 - .../ignition/rendering/ogre/OgreCapsule.hh | 4 +- ogre/src/OgreCapsule.cc | 97 ++++++------- ogre/src/OgreMarker.cc | 62 +++++--- .../ignition/rendering/ogre2/Ogre2Capsule.hh | 8 +- ogre2/src/Ogre2Capsule.cc | 103 ++++++------- ogre2/src/Ogre2Marker.cc | 137 ++++++++++-------- src/Marker_TEST.cc | 1 + 9 files changed, 218 insertions(+), 201 deletions(-) diff --git a/include/ignition/rendering/Capsule.hh b/include/ignition/rendering/Capsule.hh index ddfc2a6cd..7423be1d7 100644 --- a/include/ignition/rendering/Capsule.hh +++ b/include/ignition/rendering/Capsule.hh @@ -27,10 +27,8 @@ namespace ignition namespace rendering { inline namespace IGNITION_RENDERING_VERSION_NAMESPACE { - - /// \class CapsuleVisual CapsuleVisual.hh ignition/rendering/CapsuleVisual - /// \brief A CapsuleVisual geometry class. The visual appearance is based - /// on the type specified. + /// \class Capsule Capsule.hh ignition/rendering/Capsule + /// \brief Geometry for a capsule shape. class IGNITION_RENDERING_VISIBLE Capsule : public virtual Geometry { diff --git a/include/ignition/rendering/base/BaseCapsule.hh b/include/ignition/rendering/base/BaseCapsule.hh index d4450d2c2..463c1a61c 100644 --- a/include/ignition/rendering/base/BaseCapsule.hh +++ b/include/ignition/rendering/base/BaseCapsule.hh @@ -26,7 +26,6 @@ namespace ignition namespace rendering { inline namespace IGNITION_RENDERING_VERSION_NAMESPACE { - /// \brief Base implementation of a Capsule Geometry template class BaseCapsule : diff --git a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh index b38387ec1..9cbb2a2ee 100644 --- a/ogre/include/ignition/rendering/ogre/OgreCapsule.hh +++ b/ogre/include/ignition/rendering/ogre/OgreCapsule.hh @@ -65,8 +65,8 @@ namespace ignition public: virtual void SetMaterial(MaterialPtr _material, bool _unique) override; - /// \brief Create the Capsule geometry in ogre - private: void Create(); + /// \brief Update the capsule geometry in ogre + private: void Update(); /// \brief Capsule should only be created by scene. private: friend class OgreScene; diff --git a/ogre/src/OgreCapsule.cc b/ogre/src/OgreCapsule.cc index 309355a77..c481149b1 100644 --- a/ogre/src/OgreCapsule.cc +++ b/ogre/src/OgreCapsule.cc @@ -26,15 +26,13 @@ #include "ignition/rendering/ogre/OgreMesh.hh" #include "ignition/rendering/ogre/OgreVisual.hh" -#include - class ignition::rendering::OgreCapsulePrivate { /// \brief Capsule materal - public: OgreMaterialPtr material; + public: OgreMaterialPtr material{nullptr}; /// \brief Mesh Object for capsule shape - public: OgreMeshPtr ogreMesh = nullptr; + public: OgreMeshPtr ogreMesh{nullptr}; }; using namespace ignition; @@ -42,14 +40,20 @@ using namespace rendering; ////////////////////////////////////////////////// OgreCapsule::OgreCapsule() - : dataPtr(new OgreCapsulePrivate) + : dataPtr(new OgreCapsulePrivate) { } ////////////////////////////////////////////////// -OgreCapsule::~OgreCapsule() +OgreCapsule::~OgreCapsule() = default; + +////////////////////////////////////////////////// +Ogre::MovableObject *OgreCapsule::OgreObject() const { - // no ops + if (this->dataPtr->ogreMesh) + return this->dataPtr->ogreMesh->OgreObject(); + else + return nullptr; } ////////////////////////////////////////////////// @@ -57,29 +61,20 @@ void OgreCapsule::PreRender() { if (this->capsuleDirty) { - this->Create(); + this->Update(); this->capsuleDirty = false; } } -////////////////////////////////////////////////// -Ogre::MovableObject *OgreCapsule::OgreObject() const -{ - return this->dataPtr->ogreMesh->OgreObject(); -} - ////////////////////////////////////////////////// void OgreCapsule::Init() { - this->Create(); + this->Update(); } ////////////////////////////////////////////////// void OgreCapsule::Destroy() { - if (!this->Scene()) - return; - if (this->dataPtr->ogreMesh) { this->dataPtr->ogreMesh->Destroy(); @@ -94,48 +89,49 @@ void OgreCapsule::Destroy() } ////////////////////////////////////////////////// -void OgreCapsule::Create() +void OgreCapsule::Update() { common::MeshManager *meshMgr = common::MeshManager::Instance(); std::string capsuleMeshName = this->Name() + "_capsule_mesh" + "_" + std::to_string(this->radius) + "_" + std::to_string(this->length); + + // Create new mesh if needed if (!meshMgr->HasMesh(capsuleMeshName)) { meshMgr->CreateCapsule(capsuleMeshName, this->radius, this->length, 32, 32); - MeshDescriptor meshDescriptor; - meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); - if (meshDescriptor.mesh == nullptr) - { - ignerr << "Capsule mesh is unavailable in the Mesh Manager" << std::endl; - return; - } - else + } + + MeshDescriptor meshDescriptor; + meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); + if (meshDescriptor.mesh == nullptr) + { + ignerr << "Capsule mesh is unavailable in the Mesh Manager" << std::endl; + return; + } + + auto visual = std::dynamic_pointer_cast(this->Parent()); + + // clear geom if needed + if (this->dataPtr->ogreMesh) + { + if (visual) { - auto visual = std::dynamic_pointer_cast(this->Parent()); - // clear geom if needed - if (this->dataPtr->ogreMesh) - { - if (visual) - { - visual->RemoveGeometry( - std::dynamic_pointer_cast(shared_from_this())); - } - this->dataPtr->ogreMesh->Destroy(); - } - this->dataPtr->ogreMesh = - std::dynamic_pointer_cast( - this->Scene()->CreateMesh(meshDescriptor)); - if (this->dataPtr->material != nullptr) - { - this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); - } - if (visual) - { - visual->AddGeometry( - std::dynamic_pointer_cast(shared_from_this())); - } + visual->RemoveGeometry( + std::dynamic_pointer_cast(shared_from_this())); } + this->dataPtr->ogreMesh->Destroy(); + } + this->dataPtr->ogreMesh = std::dynamic_pointer_cast( + this->Scene()->CreateMesh(meshDescriptor)); + if (this->dataPtr->material != nullptr) + { + this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); + } + if (visual) + { + visual->AddGeometry( + std::dynamic_pointer_cast(shared_from_this())); } } @@ -155,6 +151,7 @@ void OgreCapsule::SetMaterial(MaterialPtr _material, bool _unique) return; } + // Set material for the underlying dynamic renderable this->dataPtr->ogreMesh->SetMaterial(derived, false); this->dataPtr->material = derived; } diff --git a/ogre/src/OgreMarker.cc b/ogre/src/OgreMarker.cc index a548e9c49..c4f953e11 100644 --- a/ogre/src/OgreMarker.cc +++ b/ogre/src/OgreMarker.cc @@ -32,8 +32,8 @@ class ignition::rendering::OgreMarkerPrivate /// \brief DynamicLines Object to display public: std::shared_ptr dynamicRenderable; - /// \brief Mesh Object for primitive shapes - public: OgreMeshPtr mesh = nullptr; + /// \brief Geometry Object for primitive shapes + public: OgreGeometryPtr geom{nullptr}; }; using namespace ignition; @@ -60,10 +60,10 @@ void OgreMarker::PreRender() ////////////////////////////////////////////////// void OgreMarker::Destroy() { - if (this->dataPtr->mesh) + if (this->dataPtr->geom) { - this->dataPtr->mesh->Destroy(); - this->dataPtr->mesh.reset(); + this->dataPtr->geom->Destroy(); + this->dataPtr->geom.reset(); } if (this->dataPtr->material && this->Scene()) @@ -77,7 +77,7 @@ void OgreMarker::Destroy() ////////////////////////////////////////////////// Ogre::MovableObject *OgreMarker::OgreObject() const { - switch (markerType) + switch (this->markerType) { case MT_NONE: return nullptr; @@ -85,7 +85,14 @@ Ogre::MovableObject *OgreMarker::OgreObject() const case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: - return this->dataPtr->mesh->OgreObject(); + { + if (nullptr != this->dataPtr->geom) + { + return this->dataPtr->geom->OgreObject(); + } + return nullptr; + break; + } case MT_LINE_STRIP: case MT_LINE_LIST: case MT_POINTS: @@ -95,7 +102,7 @@ Ogre::MovableObject *OgreMarker::OgreObject() const return std::dynamic_pointer_cast (this->dataPtr->dynamicRenderable).get(); default: - ignerr << "Invalid Marker type " << markerType << "\n"; + ignerr << "Invalid Marker type " << this->markerType << "\n"; return nullptr; } } @@ -112,16 +119,22 @@ void OgreMarker::Create() this->markerType = MT_NONE; this->dataPtr->dynamicRenderable.reset(new OgreDynamicLines(MT_LINE_STRIP)); - if (!this->dataPtr->mesh) + if (!this->dataPtr->geom) { - this->dataPtr->mesh = - std::dynamic_pointer_cast(this->scene->CreateBox()); + this->dataPtr->geom = + std::dynamic_pointer_cast(this->scene->CreateBox()); } } ////////////////////////////////////////////////// void OgreMarker::SetMaterial(MaterialPtr _material, bool _unique) { + if (nullptr == _material) + { + ignerr << "Cannot assign null material" << std::endl; + return; + } + _material = (_unique) ? _material->Clone() : _material; OgreMaterialPtr derived = @@ -150,8 +163,17 @@ void OgreMarker::SetMaterial(MaterialPtr _material, bool _unique) case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: - this->dataPtr->mesh->SetMaterial(derived, false); + { + if (nullptr != this->dataPtr->geom) + { + this->dataPtr->geom->SetMaterial(derived, false); + } + else + { + ignerr << "Failed to set material, null geometry." << std::endl; + } break; + } case MT_LINE_STRIP: case MT_LINE_LIST: case MT_POINTS: @@ -205,20 +227,20 @@ void OgreMarker::SetType(MarkerType _markerType) case MT_NONE: break; case MT_BOX: - this->dataPtr->mesh = - std::dynamic_pointer_cast(this->scene->CreateBox()); + this->dataPtr->geom = + std::dynamic_pointer_cast(this->scene->CreateBox()); break; case MT_CAPSULE: - this->dataPtr->mesh = - std::dynamic_pointer_cast(this->scene->CreateCapsule()); + this->dataPtr->geom = + std::dynamic_pointer_cast(this->scene->CreateCapsule()); break; case MT_CYLINDER: - this->dataPtr->mesh = - std::dynamic_pointer_cast(this->scene->CreateCylinder()); + this->dataPtr->geom = + std::dynamic_pointer_cast(this->scene->CreateCylinder()); break; case MT_SPHERE: - this->dataPtr->mesh = - std::dynamic_pointer_cast(this->scene->CreateSphere()); + this->dataPtr->geom = + std::dynamic_pointer_cast(this->scene->CreateSphere()); break; case MT_LINE_STRIP: case MT_LINE_LIST: diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh index d1f99cd2c..2b0c5eea0 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Capsule.hh @@ -67,12 +67,8 @@ namespace ignition public: virtual void SetMaterial(MaterialPtr _material, bool _unique) override; - /// \brief Set material to capsule geometry. - /// \param[in] _material Ogre material. - protected: virtual void SetMaterialImpl(Ogre2MaterialPtr _material); - - /// \brief Create the Capsule geometry in ogre - private: void Create(); + /// \brief Update the capsule geometry in ogre + private: void Update(); /// \brief Capsule should only be created by scene. private: friend class Ogre2Scene; diff --git a/ogre2/src/Ogre2Capsule.cc b/ogre2/src/Ogre2Capsule.cc index 8a53e0118..8ad35ef35 100644 --- a/ogre2/src/Ogre2Capsule.cc +++ b/ogre2/src/Ogre2Capsule.cc @@ -26,15 +26,13 @@ #include "ignition/rendering/ogre2/Ogre2Mesh.hh" #include "ignition/rendering/ogre2/Ogre2Visual.hh" -#include - class ignition::rendering::Ogre2CapsulePrivate { /// \brief Capsule materal public: Ogre2MaterialPtr material{nullptr}; /// \brief Mesh Object for capsule shape - public: Ogre2MeshPtr ogreMesh = nullptr; + public: Ogre2MeshPtr ogreMesh{nullptr}; }; using namespace ignition; @@ -42,20 +40,20 @@ using namespace rendering; ////////////////////////////////////////////////// Ogre2Capsule::Ogre2Capsule() - : dataPtr(new Ogre2CapsulePrivate) + : dataPtr(new Ogre2CapsulePrivate) { } ////////////////////////////////////////////////// -Ogre2Capsule::~Ogre2Capsule() -{ - // no ops -} +Ogre2Capsule::~Ogre2Capsule() = default; ////////////////////////////////////////////////// Ogre::MovableObject *Ogre2Capsule::OgreObject() const { - return this->dataPtr->ogreMesh->OgreObject(); + if (this->dataPtr->ogreMesh) + return this->dataPtr->ogreMesh->OgreObject(); + else + return nullptr; } ////////////////////////////////////////////////// @@ -63,7 +61,7 @@ void Ogre2Capsule::PreRender() { if (this->capsuleDirty) { - this->Create(); + this->Update(); this->capsuleDirty = false; } } @@ -71,15 +69,12 @@ void Ogre2Capsule::PreRender() ////////////////////////////////////////////////// void Ogre2Capsule::Init() { - this->Create(); + this->Update(); } ////////////////////////////////////////////////// void Ogre2Capsule::Destroy() { - if (!this->Scene()) - return; - if (this->dataPtr->ogreMesh) { this->dataPtr->ogreMesh->Destroy(); @@ -93,50 +88,50 @@ void Ogre2Capsule::Destroy() } } -//////////////////////////////////////////////// -void Ogre2Capsule::Create() +////////////////////////////////////////////////// +void Ogre2Capsule::Update() { common::MeshManager *meshMgr = common::MeshManager::Instance(); std::string capsuleMeshName = this->Name() + "_capsule_mesh" + "_" + std::to_string(this->radius) + "_" + std::to_string(this->length); + + // Create new mesh if needed if (!meshMgr->HasMesh(capsuleMeshName)) { - meshMgr->CreateCapsule(capsuleMeshName, this->radius, this->length, 12, 32); - MeshDescriptor meshDescriptor; - meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); - if (meshDescriptor.mesh == nullptr) - { - ignerr << "Capsule mesh is unavailable in the Mesh Manager" << std::endl; - return; - } - else + meshMgr->CreateCapsule(capsuleMeshName, this->radius, this->length, 32, 32); + } + + MeshDescriptor meshDescriptor; + meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); + if (meshDescriptor.mesh == nullptr) + { + ignerr << "Capsule mesh is unavailable in the Mesh Manager" << std::endl; + return; + } + + auto visual = std::dynamic_pointer_cast(this->Parent()); + + // clear geom if needed + if (this->dataPtr->ogreMesh) + { + if (visual) { - auto visual = std::dynamic_pointer_cast(this->Parent()); - - // clear geom if needed - if (this->dataPtr->ogreMesh) - { - if (visual) - { - visual->RemoveGeometry( - std::dynamic_pointer_cast(shared_from_this())); - } - this->dataPtr->ogreMesh->Destroy(); - } - this->dataPtr->ogreMesh = - std::dynamic_pointer_cast( - this->Scene()->CreateMesh(meshDescriptor)); - if (this->dataPtr->material != nullptr) - { - this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); - } - if (visual) - { - visual->AddGeometry( - std::dynamic_pointer_cast(shared_from_this())); - } + visual->RemoveGeometry( + std::dynamic_pointer_cast(shared_from_this())); } + this->dataPtr->ogreMesh->Destroy(); + } + this->dataPtr->ogreMesh = std::dynamic_pointer_cast( + this->Scene()->CreateMesh(meshDescriptor)); + if (this->dataPtr->material != nullptr) + { + this->dataPtr->ogreMesh->SetMaterial(this->dataPtr->material, false); + } + if (visual) + { + visual->AddGeometry( + std::dynamic_pointer_cast(shared_from_this())); } } @@ -157,14 +152,8 @@ void Ogre2Capsule::SetMaterial(MaterialPtr _material, bool _unique) } // Set material for the underlying dynamic renderable - this->dataPtr->ogreMesh->SetMaterial(_material, false); - this->SetMaterialImpl(derived); -} - -////////////////////////////////////////////////// -void Ogre2Capsule::SetMaterialImpl(Ogre2MaterialPtr _material) -{ - this->dataPtr->material = _material; + this->dataPtr->ogreMesh->SetMaterial(derived, false); + this->dataPtr->material = derived; } ////////////////////////////////////////////////// diff --git a/ogre2/src/Ogre2Marker.cc b/ogre2/src/Ogre2Marker.cc index 8cab1570b..faa525191 100644 --- a/ogre2/src/Ogre2Marker.cc +++ b/ogre2/src/Ogre2Marker.cc @@ -37,8 +37,8 @@ class ignition::rendering::Ogre2MarkerPrivate /// responsible for destroying the material public: bool ownsMaterial = false; - /// \brief Mesh Object for primitive shapes - public: Ogre2MeshPtr mesh = nullptr; + /// \brief Geometry Object for primitive shapes + public: Ogre2GeometryPtr geom{nullptr}; /// \brief DynamicLines Object to display public: std::shared_ptr dynamicRenderable; @@ -68,13 +68,10 @@ void Ogre2Marker::PreRender() ////////////////////////////////////////////////// void Ogre2Marker::Destroy() { - if (!this->Scene()) - return; - - if (this->dataPtr->mesh) + if (this->dataPtr->geom) { - this->dataPtr->mesh->Destroy(); - this->dataPtr->mesh.reset(); + this->dataPtr->geom->Destroy(); + this->dataPtr->geom.reset(); } if (this->dataPtr->dynamicRenderable) @@ -83,7 +80,7 @@ void Ogre2Marker::Destroy() this->dataPtr->dynamicRenderable.reset(); } - if (this->dataPtr->material && this->dataPtr->ownsMaterial) + if (this->dataPtr->material && this->dataPtr->ownsMaterial && this->Scene()) { this->Scene()->DestroyMaterial(this->dataPtr->material); this->dataPtr->material.reset(); @@ -101,7 +98,14 @@ Ogre::MovableObject *Ogre2Marker::OgreObject() const case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: - return this->dataPtr->mesh->OgreObject(); + { + if (nullptr != this->dataPtr->geom) + { + return this->dataPtr->geom->OgreObject(); + } + return nullptr; + break; + } case MT_LINE_STRIP: case MT_LINE_LIST: case MT_POINTS: @@ -109,7 +113,12 @@ Ogre::MovableObject *Ogre2Marker::OgreObject() const case MT_TRIANGLE_LIST: case MT_TRIANGLE_STRIP: { - return this->dataPtr->dynamicRenderable->OgreObject(); + if (nullptr != this->dataPtr->dynamicRenderable) + { + return this->dataPtr->dynamicRenderable->OgreObject(); + } + return nullptr; + break; } default: ignerr << "Invalid Marker type " << this->markerType << "\n"; @@ -129,16 +138,22 @@ void Ogre2Marker::Create() this->markerType = MT_NONE; this->dataPtr->dynamicRenderable.reset(new Ogre2DynamicRenderable( this->scene)); - if (!this->dataPtr->mesh) + if (!this->dataPtr->geom) { - this->dataPtr->mesh = - std::dynamic_pointer_cast(this->scene->CreateBox()); + this->dataPtr->geom = + std::dynamic_pointer_cast(this->scene->CreateBox()); } } ////////////////////////////////////////////////// void Ogre2Marker::SetMaterial(MaterialPtr _material, bool _unique) { + if (nullptr == _material) + { + ignerr << "Cannot assign null material" << std::endl; + return; + } + _material = (_unique) ? _material->Clone() : _material; Ogre2MaterialPtr derived = @@ -166,16 +181,34 @@ void Ogre2Marker::SetMaterial(MaterialPtr _material, bool _unique) case MT_CAPSULE: case MT_CYLINDER: case MT_SPHERE: - this->dataPtr->mesh->SetMaterial(derived, false); + { + if (nullptr != this->dataPtr->geom) + { + this->dataPtr->geom->SetMaterial(derived, false); + } + else + { + ignerr << "Failed to set material, null geometry." << std::endl; + } break; + } case MT_LINE_STRIP: case MT_LINE_LIST: case MT_POINTS: case MT_TRIANGLE_FAN: case MT_TRIANGLE_LIST: case MT_TRIANGLE_STRIP: - this->dataPtr->dynamicRenderable->SetMaterial(derived, false); + { + if (nullptr != this->dataPtr->dynamicRenderable) + { + this->dataPtr->dynamicRenderable->SetMaterial(derived, false); + } + else + { + ignerr << "Failed to set material, null renderable." << std::endl; + } break; + } default: ignerr << "Invalid Marker type " << this->markerType << "\n"; break; @@ -225,65 +258,37 @@ void Ogre2Marker::SetType(MarkerType _markerType) auto visual = std::dynamic_pointer_cast(this->Parent()); // clear geom if needed - if (this->dataPtr->mesh) + if (this->dataPtr->geom) { if (visual) { visual->RemoveGeometry( std::dynamic_pointer_cast(shared_from_this())); } - this->dataPtr->mesh->Destroy(); + this->dataPtr->geom->Destroy(); } - - GeometryPtr newMesh; + bool isGeom{false}; + GeometryPtr newGeom; switch (_markerType) { case MT_NONE: break; case MT_BOX: - newMesh = this->scene->CreateBox(); + isGeom = true; + newGeom = this->scene->CreateBox(); break; case MT_CAPSULE: - { - common::MeshManager *meshMgr = common::MeshManager::Instance(); - std::string capsuleMeshName = std::string("marker_capsule_mesh") - + "_" + std::to_string(0.5) - + "_" + std::to_string(1.0); - if (!meshMgr->HasMesh(capsuleMeshName)) - { - meshMgr->CreateCapsule(capsuleMeshName, 0.5, 1.0, 12, 32); - MeshDescriptor meshDescriptor; - meshDescriptor.mesh = meshMgr->MeshByName(capsuleMeshName); - if (meshDescriptor.mesh == nullptr) - { - ignerr << "Capsule mesh is unavailable in the Mesh Manager" - << std::endl; - return; - } - else - { - this->dataPtr->mesh = - std::dynamic_pointer_cast( - this->Scene()->CreateMesh(meshDescriptor)); - if (this->dataPtr->material != nullptr) - { - this->dataPtr->mesh->SetMaterial(this->dataPtr->material, false); - } - if (visual) - { - visual->AddGeometry( - std::dynamic_pointer_cast(shared_from_this())); - } - } - } - } - return; + isGeom = true; + newGeom = this->scene->CreateCapsule(); + break; case MT_CYLINDER: - newMesh = this->scene->CreateCylinder(); + isGeom = true; + newGeom = this->scene->CreateCylinder(); break; case MT_SPHERE: - newMesh = this->scene->CreateSphere(); + isGeom = true; + newGeom = this->scene->CreateSphere(); break; case MT_LINE_STRIP: case MT_LINE_LIST: @@ -294,19 +299,29 @@ void Ogre2Marker::SetType(MarkerType _markerType) this->dataPtr->dynamicRenderable->SetOperationType(_markerType); break; default: - ignerr << "Invalid Marker type\n"; + ignerr << "Invalid Marker type [" << _markerType << "]" << std::endl; break; } - if (newMesh) + if (nullptr != newGeom) { - this->dataPtr->mesh = std::dynamic_pointer_cast(newMesh); - if (visual) + this->dataPtr->geom = std::dynamic_pointer_cast(newGeom); + if (nullptr == this->dataPtr->geom) + { + ignerr << "Failed to cast to [Ogre2Geom], type [" << _markerType << "]" + << std::endl; + } + else if (visual) { visual->AddGeometry( std::dynamic_pointer_cast(shared_from_this())); } } + else if (isGeom) + { + ignerr << "Failed to create geometry for marker type [" << _markerType + << "]" << std::endl; + } } ////////////////////////////////////////////////// diff --git a/src/Marker_TEST.cc b/src/Marker_TEST.cc index 213ecc109..9a6861020 100644 --- a/src/Marker_TEST.cc +++ b/src/Marker_TEST.cc @@ -81,6 +81,7 @@ void MarkerTest::Marker(const std::string &_renderEngine) marker->SetType(MarkerType::MT_CYLINDER); EXPECT_EQ(MarkerType::MT_CYLINDER, marker->Type()); + marker->SetType(MarkerType::MT_NONE); EXPECT_EQ(MarkerType::MT_NONE, marker->Type());