From a4c60f6c3743aac8167858d67020db37dd19b599 Mon Sep 17 00:00:00 2001 From: Nicholas Milef Date: Tue, 30 Apr 2019 12:29:47 -0400 Subject: [PATCH] ENH: added Entity class --- Source/Core/imstkEntity.cpp | 39 +++++++++++++ Source/Core/imstkEntity.h | 57 +++++++++++++++++++ Source/SceneElements/Light/imstkLight.h | 7 ++- .../Objects/imstkSceneObject.cpp | 10 ++++ .../SceneElements/Objects/imstkSceneObject.h | 13 ++--- 5 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 Source/Core/imstkEntity.cpp create mode 100644 Source/Core/imstkEntity.h diff --git a/Source/Core/imstkEntity.cpp b/Source/Core/imstkEntity.cpp new file mode 100644 index 000000000..aa693aecf --- /dev/null +++ b/Source/Core/imstkEntity.cpp @@ -0,0 +1,39 @@ +/*========================================================================= + + Library: iMSTK + + Copyright (c) Kitware, Inc. & Center for Modeling, Simulation, + & Imaging in Medicine, Rensselaer Polytechnic Institute. + + 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.txt + + 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 "imstkEntity.h" + +namespace imstk +{ +std::atomic Entity::s_count {0}; + +Entity::Entity() +{ + s_count++; + m_ID = s_count; +} + +EntityID +Entity::getID() const +{ + return m_ID; +} +} diff --git a/Source/Core/imstkEntity.h b/Source/Core/imstkEntity.h new file mode 100644 index 000000000..c86d2fad6 --- /dev/null +++ b/Source/Core/imstkEntity.h @@ -0,0 +1,57 @@ +/*========================================================================= + + Library: iMSTK + + Copyright (c) Kitware, Inc. & Center for Modeling, Simulation, + & Imaging in Medicine, Rensselaer Polytechnic Institute. + + 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.txt + + 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 imstkEntity_h +#define imstkEntity_h + +#include + +namespace imstk +{ +using EntityID = unsigned long; + +/// +/// \class Entity +/// \brief Top-level class for iMSTK objects +/// +class Entity +{ +public: + /// + /// \brief Get ID (ALWAYS query the ID in your code, DO NOT hardcode it) + /// \returns ID of entity + /// + EntityID getID() const; + +protected: + /// + /// \brief Constructor + /// + Entity(); + + // Not the best design pattern + static std::atomic s_count; ///< current count of entities + + EntityID m_ID; ///< unique ID of entity +}; +} + +#endif \ No newline at end of file diff --git a/Source/SceneElements/Light/imstkLight.h b/Source/SceneElements/Light/imstkLight.h index 25728f667..f7127b6c3 100644 --- a/Source/SceneElements/Light/imstkLight.h +++ b/Source/SceneElements/Light/imstkLight.h @@ -27,6 +27,7 @@ // imstk #include "imstkMath.h" #include "imstkColor.h" +#include "imstkEntity.h" namespace imstk { @@ -45,7 +46,7 @@ enum class LightType /// /// \brief Abstract base class for lights /// -class Light +class Light : public Entity { public: /// @@ -117,8 +118,8 @@ public: void setName(const std::string&& name) { m_name = std::move(name); }; protected: - Light(const std::string& name) : m_name(name){}; - Light(std::string&& name) : m_name(std::move(name)){}; + Light(const std::string& name) : m_name(name), Entity() {}; + Light(std::string&& name) : m_name(std::move(name)), Entity() {}; // properties with defaults float m_intensity = 100.; diff --git a/Source/SceneElements/Objects/imstkSceneObject.cpp b/Source/SceneElements/Objects/imstkSceneObject.cpp index 12efbad15..1ab5faa5f 100644 --- a/Source/SceneElements/Objects/imstkSceneObject.cpp +++ b/Source/SceneElements/Objects/imstkSceneObject.cpp @@ -26,6 +26,16 @@ namespace imstk { +SceneObject::SceneObject(const std::string& name) : m_name(name), Entity() +{ + m_type = Type::Visual; +} + +SceneObject::SceneObject(std::string&& name) : m_name(std::move(name)), Entity() +{ + m_type = Type::Visual; +} + std::shared_ptr SceneObject::getVisualGeometry() const { diff --git a/Source/SceneElements/Objects/imstkSceneObject.h b/Source/SceneElements/Objects/imstkSceneObject.h index 35532d248..eeb05bc29 100644 --- a/Source/SceneElements/Objects/imstkSceneObject.h +++ b/Source/SceneElements/Objects/imstkSceneObject.h @@ -26,6 +26,7 @@ #include #include "imstkVisualModel.h" +#include "imstkEntity.h" namespace imstk { @@ -38,7 +39,7 @@ class DeviceClient; /// \brief Base class for all scene objects. A scene object can optionally be visible and /// collide with other scene objects. A object of the class is static. /// -class SceneObject +class SceneObject : public Entity { public: enum class Type @@ -54,14 +55,8 @@ public: /// /// \brief Constructor /// - SceneObject(const std::string& name) : m_name(name) - { - m_type = Type::Visual; - } - SceneObject(std::string&& name) : m_name(std::move(name)) - { - m_type = Type::Visual; - } + SceneObject(const std::string& name); + SceneObject(std::string&& name); /// /// \brief Destructor -- GitLab