Compare commits

...

3 Commits

Author SHA1 Message Date
82938b4f52 合并代码 2026-03-21 20:48:06 +08:00
48028183e5 修改组件获取的问题,同时更新翻译文件 2026-03-21 20:44:38 +08:00
d57591e3dc 修改轨迹组件 2026-03-21 19:48:08 +08:00
8 changed files with 445 additions and 328 deletions

View File

@ -9,7 +9,6 @@
#include "entities/Entity.h" #include "entities/Entity.h"
#include "common/SpdLogger.h" #include "common/SpdLogger.h"
//#include "scutcheon/osgScutcheon.h"
LabelComponent::LabelComponent(SceneComponent* parent) LabelComponent::LabelComponent(SceneComponent* parent)
: SceneComponent(parent) : SceneComponent(parent)

View File

@ -38,7 +38,7 @@ public:
template<class T> template<class T>
T* GetComponent() { T* GetComponent() {
for (auto& componet : children_) { for (auto& componet : children_) {
if (componet->GetTypeName() == T::GetTypeName()) { if (componet->GetSelfTypeName() == T::GetTypeName()) {
return reinterpret_cast<T*>(componet); return reinterpret_cast<T*>(componet);
} }
} }

View File

@ -1,6 +1,7 @@
#include "entities/TrajectoryTraceComponent.h" #include "entities/TrajectoryTraceComponent.h"
#include <algorithm> #include <algorithm>
#include <cmath>
#include <osg/BlendFunc> #include <osg/BlendFunc>
#include <osg/Depth> #include <osg/Depth>
@ -90,6 +91,7 @@ void TrajectoryTraceComponent::Begin() {
sampleAccum_ = 0.0; sampleAccum_ = 0.0;
elapsedTime_ = 0.0; elapsedTime_ = 0.0;
hasLastSample_ = false; hasLastSample_ = false;
traceRestartPending_ = false;
attachedToScene_ = false; attachedToScene_ = false;
ClearTrace(); ClearTrace();
} }
@ -107,6 +109,14 @@ void TrajectoryTraceComponent::Update(double dt) {
TrimExpiredPoints(); TrimExpiredPoints();
const osg::Vec3d currentPos = entity->GetTransform()->GetLocation(); const osg::Vec3d currentPos = entity->GetTransform()->GetLocation();
if (traceRestartPending_) {
lastSamplePos_ = currentPos;
hasLastSample_ = true;
sampleAccum_ = 0.0;
traceRestartPending_ = false;
return;
}
if (!hasLastSample_) { if (!hasLastSample_) {
osg::Vec3d initialPos = currentPos; osg::Vec3d initialPos = currentPos;
if (PathComponent* pathComponent = entity->GetComponent<PathComponent>()) { if (PathComponent* pathComponent = entity->GetComponent<PathComponent>()) {
@ -210,11 +220,8 @@ double TrajectoryTraceComponent::GetMinMoveDistance() const {
void TrajectoryTraceComponent::SetMaxPoints(int maxPoints) { void TrajectoryTraceComponent::SetMaxPoints(int maxPoints) {
maxPoints_ = std::max(2, maxPoints); maxPoints_ = std::max(2, maxPoints);
if (vertices_.valid() && static_cast<int>(vertices_->size()) > maxPoints_) { if (vertices_.valid() && static_cast<int>(vertices_->size()) > maxPoints_) {
const int overflow = static_cast<int>(vertices_->size()) - maxPoints_; TrimOverflowPoints();
vertices_->erase(vertices_->begin(), vertices_->begin() + overflow); DirtyGeometry();
drawArrays_->setCount(static_cast<GLsizei>(vertices_->size()));
vertices_->dirty();
geometry_->dirtyBound();
} }
} }
@ -271,6 +278,7 @@ double TrajectoryTraceComponent::GetGapLength() const {
void TrajectoryTraceComponent::SetDashScrollSpeed(double speed) { void TrajectoryTraceComponent::SetDashScrollSpeed(double speed) {
dashScrollSpeed_ = speed; dashScrollSpeed_ = speed;
DirtyGeometry();
} }
double TrajectoryTraceComponent::GetDashScrollSpeed() const { double TrajectoryTraceComponent::GetDashScrollSpeed() const {
@ -280,17 +288,18 @@ double TrajectoryTraceComponent::GetDashScrollSpeed() const {
void TrajectoryTraceComponent::ClearTrace() { void TrajectoryTraceComponent::ClearTrace() {
InitializeGeometry(); InitializeGeometry();
Entity* entity = GetEntity();
if (nullptr != entity && nullptr != entity->GetTransform()) {
lastSamplePos_ = entity->GetTransform()->GetLocation();
}
vertices_->clear(); vertices_->clear();
renderVertices_->clear();
sampleTimes_.clear(); sampleTimes_.clear();
drawArrays_->setCount(0); hasLastSample_ = true;
vertices_->dirty(); sampleAccum_ = 0.0;
geometry_->dirtyBound(); traceRestartPending_ = true;
if (geode_.valid()) { DirtyGeometry();
geode_->dirtyBound();
}
if (mt_.valid()) {
mt_->dirtyBound();
}
} }
void TrajectoryTraceComponent::InitializeGeometry() { void TrajectoryTraceComponent::InitializeGeometry() {
@ -302,15 +311,15 @@ void TrajectoryTraceComponent::InitializeGeometry() {
geode_ = new osg::Geode; geode_ = new osg::Geode;
geometry_ = new osg::Geometry; geometry_ = new osg::Geometry;
vertices_ = new osg::Vec3Array; vertices_ = new osg::Vec3Array;
renderVertices_ = new osg::Vec3Array;
colors_ = new osg::Vec4Array; colors_ = new osg::Vec4Array;
drawArrays_ = new osg::DrawArrays(GL_LINE_STRIP, 0, 0); drawArrays_ = new osg::DrawArrays(GL_LINE_STRIP, 0, 0);
lineWidthState_ = new osg::LineWidth(lineWidth_); lineWidthState_ = new osg::LineWidth(lineWidth_);
lineStippleState_ = new osg::LineStipple(1, 0xFFFF);
geometry_->setDataVariance(osg::Object::DYNAMIC); geometry_->setDataVariance(osg::Object::DYNAMIC);
geometry_->setUseDisplayList(false); geometry_->setUseDisplayList(false);
geometry_->setUseVertexBufferObjects(true); geometry_->setUseVertexBufferObjects(true);
geometry_->setVertexArray(vertices_.get()); geometry_->setVertexArray(renderVertices_.get());
geometry_->addPrimitiveSet(drawArrays_.get()); geometry_->addPrimitiveSet(drawArrays_.get());
colors_->push_back(color_); colors_->push_back(color_);
@ -323,7 +332,6 @@ void TrajectoryTraceComponent::InitializeGeometry() {
stateSet->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); stateSet->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
stateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL, 0.0, 1.0, false)); stateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL, 0.0, 1.0, false));
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
stateSet->setAttributeAndModes(lineStippleState_.get(), osg::StateAttribute::OFF);
geode_->addDrawable(geometry_.get()); geode_->addDrawable(geometry_.get());
mt_->addChild(geode_.get()); mt_->addChild(geode_.get());
@ -349,23 +357,8 @@ bool TrajectoryTraceComponent::AppendPoint(const osg::Vec3d& geoPoint) {
vertices_->push_back(worldPoint); vertices_->push_back(worldPoint);
sampleTimes_.push_back(elapsedTime_); sampleTimes_.push_back(elapsedTime_);
if (static_cast<int>(vertices_->size()) > maxPoints_) { TrimOverflowPoints();
const int overflow = static_cast<int>(vertices_->size()) - maxPoints_; DirtyGeometry();
vertices_->erase(vertices_->begin(), vertices_->begin() + overflow);
for (int i = 0; i < overflow && !sampleTimes_.empty(); ++i) {
sampleTimes_.pop_front();
}
}
drawArrays_->setCount(static_cast<GLsizei>(vertices_->size()));
vertices_->dirty();
geometry_->dirtyBound();
if (geode_.valid()) {
geode_->dirtyBound();
}
if (mt_.valid()) {
mt_->dirtyBound();
}
if (attachedToScene_ && mt_->getNumParents() == 0) { if (attachedToScene_ && mt_->getNumParents() == 0) {
AttachTraceToScene(); AttachTraceToScene();
@ -389,15 +382,7 @@ bool TrajectoryTraceComponent::UpdateTailPoint(const osg::Vec3d& geoPoint) {
if (!sampleTimes_.empty()) { if (!sampleTimes_.empty()) {
sampleTimes_.back() = elapsedTime_; sampleTimes_.back() = elapsedTime_;
} }
drawArrays_->setCount(static_cast<GLsizei>(vertices_->size())); DirtyGeometry();
vertices_->dirty();
geometry_->dirtyBound();
if (geode_.valid()) {
geode_->dirtyBound();
}
if (mt_.valid()) {
mt_->dirtyBound();
}
if (attachedToScene_ && mt_->getNumParents() == 0) { if (attachedToScene_ && mt_->getNumParents() == 0) {
AttachTraceToScene(); AttachTraceToScene();
@ -463,15 +448,7 @@ void TrajectoryTraceComponent::TrimExpiredPoints() {
} }
if (removed) { if (removed) {
drawArrays_->setCount(static_cast<GLsizei>(vertices_->size())); DirtyGeometry();
vertices_->dirty();
geometry_->dirtyBound();
if (geode_.valid()) {
geode_->dirtyBound();
}
if (mt_.valid()) {
mt_->dirtyBound();
}
} }
} }
@ -487,34 +464,101 @@ void TrajectoryTraceComponent::UpdateStyle() {
lineWidthState_->setWidth(lineWidth_); lineWidthState_->setWidth(lineWidth_);
} }
if (lineStippleState_.valid() && geode_.valid()) { DirtyGeometry();
const double cycle = std::max(1.0, dashLength_ + gapLength_); }
double factorValue = cycle / std::max(1.0f, lineWidth_);
if (factorValue < 1.0) { void TrajectoryTraceComponent::DirtyGeometry() {
factorValue = 1.0; if (!geometry_.valid() || !drawArrays_.valid() || !renderVertices_.valid()) {
} else if (factorValue > 255.0) { return;
factorValue = 255.0; }
}
int factor = static_cast<int>(factorValue); RebuildRenderGeometry();
int onBits = static_cast<int>(std::round(16.0 * dashLength_ / cycle)); renderVertices_->dirty();
if (onBits < 1) { geometry_->dirtyDisplayList();
onBits = 1; geometry_->dirtyBound();
} else if (onBits > 15) { if (geode_.valid()) {
onBits = 15; geode_->dirtyBound();
} }
unsigned short pattern = 0; if (mt_.valid()) {
for (int i = 0; i < 16; ++i) { mt_->dirtyBound();
if (i < onBits) {
pattern |= static_cast<unsigned short>(1u << i);
}
}
if (!dashed_) {
pattern = 0xFFFF;
geode_->getOrCreateStateSet()->setAttributeAndModes(lineStippleState_.get(), osg::StateAttribute::OFF);
} else {
geode_->getOrCreateStateSet()->setAttributeAndModes(lineStippleState_.get(), osg::StateAttribute::ON);
}
lineStippleState_->setFactor(factor);
lineStippleState_->setPattern(pattern);
} }
} }
void TrajectoryTraceComponent::TrimOverflowPoints() {
if (!vertices_.valid()) {
return;
}
if (static_cast<int>(vertices_->size()) <= maxPoints_) {
return;
}
const int overflow = static_cast<int>(vertices_->size()) - maxPoints_;
vertices_->erase(vertices_->begin(), vertices_->begin() + overflow);
for (int i = 0; i < overflow && !sampleTimes_.empty(); ++i) {
sampleTimes_.pop_front();
}
}
void TrajectoryTraceComponent::RebuildRenderGeometry() {
if (!renderVertices_.valid()) {
return;
}
renderVertices_->clear();
if (!vertices_.valid() || vertices_->empty()) {
drawArrays_->setMode(GL_LINE_STRIP);
drawArrays_->setCount(0);
return;
}
if (!dashed_ || vertices_->size() < 2) {
drawArrays_->setMode(GL_LINE_STRIP);
renderVertices_->assign(vertices_->begin(), vertices_->end());
drawArrays_->setCount(static_cast<GLsizei>(renderVertices_->size()));
return;
}
drawArrays_->setMode(GL_LINES);
const double cycle = std::max(0.001, dashLength_ + gapLength_);
double dashOffset = std::fmod(elapsedTime_ * dashScrollSpeed_, cycle);
if (dashOffset < 0.0) {
dashOffset += cycle;
}
double accumulatedDistance = 0.0;
for (std::size_t i = 1; i < vertices_->size(); ++i) {
const osg::Vec3d& start = (*vertices_)[i - 1];
const osg::Vec3d& end = (*vertices_)[i];
const osg::Vec3d segment = end - start;
const double segmentLength = segment.length();
if (segmentLength <= 1e-6) {
continue;
}
const osg::Vec3d direction = segment / segmentLength;
double localDistance = 0.0;
while (localDistance < segmentLength) {
double phase = std::fmod(accumulatedDistance + localDistance + dashOffset, cycle);
if (phase < 0.0) {
phase += cycle;
}
if (phase < dashLength_) {
const double visibleLength = std::min(dashLength_ - phase, segmentLength - localDistance);
const osg::Vec3d visibleStart = start + direction * localDistance;
const osg::Vec3d visibleEnd = start + direction * (localDistance + visibleLength);
renderVertices_->push_back(visibleStart);
renderVertices_->push_back(visibleEnd);
localDistance += visibleLength;
} else {
localDistance += std::min(cycle - phase, segmentLength - localDistance);
}
}
accumulatedDistance += segmentLength;
}
drawArrays_->setCount(static_cast<GLsizei>(renderVertices_->size()));
}

View File

@ -3,7 +3,6 @@
#include "entities/SceneComponent.h" #include "entities/SceneComponent.h"
#include <osg/Geometry> #include <osg/Geometry>
#include <osg/LineStipple>
#include <osg/LineWidth> #include <osg/LineWidth>
#include <osg/Vec4> #include <osg/Vec4>
#include <deque> #include <deque>
@ -71,15 +70,18 @@ private:
void AttachTraceToScene(); void AttachTraceToScene();
void TrimExpiredPoints(); void TrimExpiredPoints();
void UpdateStyle(); void UpdateStyle();
void DirtyGeometry();
void TrimOverflowPoints();
void RebuildRenderGeometry();
private: private:
osg::ref_ptr<osg::Geode> geode_; osg::ref_ptr<osg::Geode> geode_;
osg::ref_ptr<osg::Geometry> geometry_; osg::ref_ptr<osg::Geometry> geometry_;
osg::ref_ptr<osg::Vec3Array> vertices_; osg::ref_ptr<osg::Vec3Array> vertices_;
osg::ref_ptr<osg::Vec3Array> renderVertices_;
osg::ref_ptr<osg::Vec4Array> colors_; osg::ref_ptr<osg::Vec4Array> colors_;
osg::ref_ptr<osg::DrawArrays> drawArrays_; osg::ref_ptr<osg::DrawArrays> drawArrays_;
osg::ref_ptr<osg::LineWidth> lineWidthState_; osg::ref_ptr<osg::LineWidth> lineWidthState_;
osg::ref_ptr<osg::LineStipple> lineStippleState_;
osg::Vec4 color_{1.0f, 0.8f, 0.1f, 1.0f}; osg::Vec4 color_{1.0f, 0.8f, 0.1f, 1.0f};
float lineWidth_{2.0f}; float lineWidth_{2.0f};
@ -97,6 +99,7 @@ private:
double elapsedTime_{0.0}; double elapsedTime_{0.0};
osg::Vec3d lastSamplePos_{0.0, 0.0, 0.0}; osg::Vec3d lastSamplePos_{0.0, 0.0, 0.0};
bool hasLastSample_{false}; bool hasLastSample_{false};
bool traceRestartPending_{false};
bool attachedToScene_{false}; bool attachedToScene_{false};
std::deque<double> sampleTimes_; std::deque<double> sampleTimes_;
}; };

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -483,7 +483,8 @@ void QTrajectoryTraceComponentAttribute::SetColor(const QColor& c) {
} }
OsgUtils::QColorToVec4(c, &vColor); OsgUtils::QColorToVec4(c, &vColor);
object_->SetColor(vColor); const std::string value = StringUtils::Vec4ToString(vColor);
object_->SetAttribute("color", value.c_str());
} }
QColor QTrajectoryTraceComponentAttribute::GetColor() const { QColor QTrajectoryTraceComponentAttribute::GetColor() const {
@ -502,7 +503,8 @@ void QTrajectoryTraceComponentAttribute::SetLineWidth(double width) {
return; return;
} }
object_->SetLineWidth(static_cast<float>(width)); const std::string value = std::to_string(width);
object_->SetAttribute("lineWidth", value.c_str());
} }
double QTrajectoryTraceComponentAttribute::GetLineWidth() const { double QTrajectoryTraceComponentAttribute::GetLineWidth() const {
@ -518,7 +520,8 @@ void QTrajectoryTraceComponentAttribute::SetSampleInterval(double interval) {
return; return;
} }
object_->SetSampleInterval(interval); const std::string value = std::to_string(interval);
object_->SetAttribute("sampleInterval", value.c_str());
} }
double QTrajectoryTraceComponentAttribute::GetSampleInterval() const { double QTrajectoryTraceComponentAttribute::GetSampleInterval() const {
@ -534,7 +537,8 @@ void QTrajectoryTraceComponentAttribute::SetMinMoveDistance(double distance) {
return; return;
} }
object_->SetMinMoveDistance(distance); const std::string value = std::to_string(distance);
object_->SetAttribute("minMoveDistance", value.c_str());
} }
double QTrajectoryTraceComponentAttribute::GetMinMoveDistance() const { double QTrajectoryTraceComponentAttribute::GetMinMoveDistance() const {
@ -550,7 +554,8 @@ void QTrajectoryTraceComponentAttribute::SetMaxPoints(int maxPoints) {
return; return;
} }
object_->SetMaxPoints(maxPoints); const std::string value = std::to_string(maxPoints);
object_->SetAttribute("maxPoints", value.c_str());
} }
int QTrajectoryTraceComponentAttribute::GetMaxPoints() const { int QTrajectoryTraceComponentAttribute::GetMaxPoints() const {
@ -566,7 +571,8 @@ void QTrajectoryTraceComponentAttribute::SetTailDuration(double duration) {
return; return;
} }
object_->SetTailDuration(duration); const std::string value = std::to_string(duration);
object_->SetAttribute("tailDuration", value.c_str());
} }
double QTrajectoryTraceComponentAttribute::GetTailDuration() const { double QTrajectoryTraceComponentAttribute::GetTailDuration() const {
@ -582,7 +588,7 @@ void QTrajectoryTraceComponentAttribute::SetVisible(bool visible) {
return; return;
} }
object_->SetTraceVisible(visible); object_->SetAttribute("visible", visible ? "true" : "false");
} }
bool QTrajectoryTraceComponentAttribute::IsVisible() const { bool QTrajectoryTraceComponentAttribute::IsVisible() const {
@ -598,7 +604,7 @@ void QTrajectoryTraceComponentAttribute::SetDashed(bool dashed) {
return; return;
} }
object_->SetDashed(dashed); object_->SetAttribute("dashed", dashed ? "true" : "false");
} }
bool QTrajectoryTraceComponentAttribute::IsDashed() const { bool QTrajectoryTraceComponentAttribute::IsDashed() const {
@ -614,7 +620,8 @@ void QTrajectoryTraceComponentAttribute::SetDashLength(double length) {
return; return;
} }
object_->SetDashLength(length); const std::string value = std::to_string(length);
object_->SetAttribute("dashLength", value.c_str());
} }
double QTrajectoryTraceComponentAttribute::GetDashLength() const { double QTrajectoryTraceComponentAttribute::GetDashLength() const {
@ -630,7 +637,8 @@ void QTrajectoryTraceComponentAttribute::SetGapLength(double length) {
return; return;
} }
object_->SetGapLength(length); const std::string value = std::to_string(length);
object_->SetAttribute("gapLength", value.c_str());
} }
double QTrajectoryTraceComponentAttribute::GetGapLength() const { double QTrajectoryTraceComponentAttribute::GetGapLength() const {
@ -646,7 +654,8 @@ void QTrajectoryTraceComponentAttribute::SetDashScrollSpeed(double speed) {
return; return;
} }
object_->SetDashScrollSpeed(speed); const std::string value = std::to_string(speed);
object_->SetAttribute("dashScrollSpeed", value.c_str());
} }
double QTrajectoryTraceComponentAttribute::GetDashScrollSpeed() const { double QTrajectoryTraceComponentAttribute::GetDashScrollSpeed() const {

View File

@ -23,7 +23,6 @@
#include "scene/OEScene.h" #include "scene/OEScene.h"
#include "workspace/WorkSpaceManager.h" #include "workspace/WorkSpaceManager.h"
#include "workspace/WorkSpace.h" #include "workspace/WorkSpace.h"
//#include "scutcheon/osgScutcheon.h"
#include "workspace/PresetModelConfigParser.h" #include "workspace/PresetModelConfigParser.h"
#include "entities/EntitiesManager.h" #include "entities/EntitiesManager.h"