Compare commits

..

No commits in common. "82938b4f526d84c86f82f7131abf897c81ee6d79" and "f7c0d0a844810f38bc99a022f8a602b97602374a" have entirely different histories.

8 changed files with 325 additions and 442 deletions

View File

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

View File

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

View File

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

View File

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

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

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

View File

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