Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6 ➡️ 7 #524

Merged
merged 4 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function(gz_gui_add_plugin plugin_name)
endfunction()

# Plugins
add_subdirectory(camera_fps)
add_subdirectory(camera_tracking)
add_subdirectory(grid_config)
add_subdirectory(image_display)
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/camera_fps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
gz_gui_add_plugin(CameraFps
SOURCES
CameraFps.cc
QT_HEADERS
CameraFps.hh
PUBLIC_LINK_LIBS
gz-rendering${GZ_RENDERING_VER}::gz-rendering${GZ_RENDERING_VER}
)

131 changes: 131 additions & 0 deletions src/plugins/camera_fps/CameraFps.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2023 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 <list>
#include <string>

#include <gz/common/Console.hh>
#include <gz/plugin/Register.hh>
#include <gz/rendering/RenderingIface.hh>
#include <gz/rendering/Scene.hh>

#include "gz/gui/Application.hh"
#include "gz/gui/GuiEvents.hh"
#include "gz/gui/MainWindow.hh"

#include "CameraFps.hh"

/// \brief Private data class for CameraFps
class gz::gui::plugins::CameraFpsPrivate
{
/// \brief Previous camera update time
public: std::optional<std::chrono::steady_clock::time_point>
prevCameraUpdateTime;

/// \brief A moving window of camera update times
public: std::list<std::chrono::duration<double>> cameraUpdateTimes;

/// \brief Sum of all update times in the moving window
public: std::chrono::duration<double> cameraUpdateTimeSum;

/// \brief Size of camera update time window
/// \todo(anyone) make this configurable
public: unsigned int cameraFPSWindowSize = 20u;

/// \brief Camera FPS string value
public: QString cameraFPSValue;
};

using namespace gz;
using namespace gui;
using namespace plugins;

/////////////////////////////////////////////////
void CameraFps::OnRender()
{
auto now = std::chrono::steady_clock::now();
if (!this->dataPtr->prevCameraUpdateTime.has_value())
{
this->dataPtr->prevCameraUpdateTime = now;
return;
}

const std::chrono::duration<double> dt =
std::chrono::steady_clock::now() - *this->dataPtr->prevCameraUpdateTime;
this->dataPtr->prevCameraUpdateTime = now;
this->dataPtr->cameraUpdateTimeSum += dt;
if (this->dataPtr->cameraUpdateTimes.size() >=
this->dataPtr->cameraFPSWindowSize)
{
auto first = this->dataPtr->cameraUpdateTimes.front();
this->dataPtr->cameraUpdateTimes.pop_front();
this->dataPtr->cameraUpdateTimeSum -= first;
double sum = this->dataPtr->cameraUpdateTimeSum.count();
double avg = sum /
static_cast<double>(this->dataPtr->cameraFPSWindowSize);
this->SetCameraFpsValue(QString::fromStdString(std::to_string(1.0/avg)));
}
this->dataPtr->cameraUpdateTimes.push_back(dt);
}

/////////////////////////////////////////////////
CameraFps::CameraFps()
: Plugin(), dataPtr(new CameraFpsPrivate)
{
}

/////////////////////////////////////////////////
CameraFps::~CameraFps()
{
}

/////////////////////////////////////////////////
void CameraFps::LoadConfig(const tinyxml2::XMLElement *)
{
if (this->title.empty())
this->title = "Camera FPS";

App()->findChild<MainWindow *>()->installEventFilter(this);
}

/////////////////////////////////////////////////
bool CameraFps::eventFilter(QObject *_obj, QEvent *_event)
{
if (_event->type() == events::Render::kType)
{
this->OnRender();
}
// Standard event processing
return QObject::eventFilter(_obj, _event);
}

/////////////////////////////////////////////////
QString CameraFps::CameraFpsValue() const
{
return this->dataPtr->cameraFPSValue;
}

/////////////////////////////////////////////////
void CameraFps::SetCameraFpsValue(const QString &_value)
{
this->dataPtr->cameraFPSValue = _value;
this->CameraFpsValueChanged();
}

// Register this plugin
GZ_ADD_PLUGIN(gz::gui::plugins::CameraFps,
gz::gui::Plugin)
80 changes: 80 additions & 0 deletions src/plugins/camera_fps/CameraFps.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef GZ_GUI_PLUGINS_CAMERAFPS_HH_
#define GZ_GUI_PLUGINS_CAMERAFPS_HH_

#include <memory>

#include "gz/gui/Plugin.hh"

namespace gz
{
namespace gui
{
namespace plugins
{
class CameraFpsPrivate;

/// \brief This plugin displays the GUI camera's Framerate Per Second (FPS)
class CameraFps : public Plugin
{
Q_OBJECT

/// \brief Camera frames per second
Q_PROPERTY(
QString cameraFPSValue
READ CameraFpsValue
WRITE SetCameraFpsValue
NOTIFY CameraFpsValueChanged
)

/// \brief Constructor
public: CameraFps();

/// \brief Destructor
public: virtual ~CameraFps();

// Documentation inherited
public: virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem)
override;

/// \brief Set the camera FPS value string
/// \param[in] _value Camera FPS value string
public: Q_INVOKABLE void SetCameraFpsValue(const QString &_value);

/// \brief Get the camera FPS value string
/// \return Camera FPS value string
public: Q_INVOKABLE QString CameraFpsValue() const;

/// \brief Notify that camera FPS value has changed
signals: void CameraFpsValueChanged();

/// \brief Perform rendering calls in the rendering thread.
private: void OnRender();

// Documentation inherited
private: bool eventFilter(QObject *_obj, QEvent *_event) override;

/// \internal
/// \brief Pointer to private data.
private: std::unique_ptr<CameraFpsPrivate> dataPtr;
};
}
}
}
#endif
45 changes: 45 additions & 0 deletions src/plugins/camera_fps/CameraFps.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2023 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.
*
*/

import QtQuick 2.9
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3

Rectangle {
id: cameraFps
color: "transparent"
Layout.minimumWidth: 150
Layout.minimumHeight: 80

RowLayout {
id: cameraFpsLayout
anchors.fill: parent
anchors.margins: 10

Label {
ToolTip.text: qsTr("Camera FPS")
font.weight: Font.DemiBold
text: "FPS"
}

Label {
objectName: "cameraFps"
text: CameraFps.cameraFPSValue
Layout.alignment: Qt.AlignRight
}
}
}
5 changes: 5 additions & 0 deletions src/plugins/camera_fps/CameraFps.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="CameraFps/">
<file>CameraFps.qml</file>
</qresource>
</RCC>
2 changes: 1 addition & 1 deletion test/integration/camera_tracking.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ TEST(MinimalSceneTest, GZ_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Config))
EXPECT_TRUE(rep.data());

// Many update loops to process many events
maxSleep = 300;
maxSleep = 600;
for (auto it : {150.0, 200.0})
{
// Move target
Expand Down