-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jenn Nguyen <[email protected]>
- Loading branch information
Showing
7 changed files
with
272 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters