Skip to content

Commit

Permalink
avoid copy in traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Apr 3, 2024
1 parent 9984b66 commit e465778
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
11 changes: 8 additions & 3 deletions examples/libs/geo/lod/LODFrustum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
#include "threepp/math/Frustum.hpp"
#include "threepp/math/Matrix4.hpp"

#include "geo/lod/LODRadial.hpp"
#include "geo/MapView.hpp"
#include "geo/lod/LODRadial.hpp"

namespace threepp {

class LODFrustum: public LODRadial {

public:

explicit LODFrustum(float subdivideDistance = 150, float simplifyDistance = 400)
: LODRadial(subdivideDistance, simplifyDistance) {}

Expand All @@ -24,6 +23,7 @@ namespace threepp {
frustum.setFromProjectionMatrix(projection);
camera.getWorldPosition(pov);

std::vector<MapNode*> nodesToClear;
view.children[0]->traverseType<MapNode>([&](MapNode& node) {
node.getWorldPosition(position);

Expand All @@ -35,9 +35,14 @@ namespace threepp {
if (distance < this->subdivideDistance && inFrustum) {
node.subdivide();
} else if (distance > this->simplifyDistance && node.parentNode) {
node.parentNode->simplify();
if (node.parentNode->simplify()) {
nodesToClear.emplace_back(node.parentNode);
}
}
});
for (auto node : nodesToClear) {
node->clear();
}
}

private:
Expand Down
8 changes: 7 additions & 1 deletion examples/libs/geo/lod/LODRadial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace threepp {

camera.getWorldPosition(pov);

std::vector<MapNode*> nodesToClear;
view.children[0]->traverseType<MapNode>([&](MapNode& node) {
node.getWorldPosition(position);

Expand All @@ -36,9 +37,14 @@ namespace threepp {
if (distance < this->subdivideDistance) {
node.subdivide();
} else if (distance > this->simplifyDistance && node.parentNode) {
node.parentNode->simplify();
if (node.parentNode->simplify()) {
nodesToClear.emplace_back(node.parentNode);
}
}
});
for (auto node : nodesToClear) {
node->clear();
}
}

protected:
Expand Down
8 changes: 7 additions & 1 deletion examples/libs/geo/lod/LODRaycast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void LODRaycast::updateLOD(MapView& view, Camera& camera, const GLRenderer& rend
intersects = this->raycaster.intersectObjects(view.children, true);
}

std::vector<MapNode*> nodesToClear;
for (auto& intersect : intersects) {
auto* obj = intersect.object;

Expand All @@ -37,8 +38,13 @@ void LODRaycast::updateLOD(MapView& view, Camera& camera, const GLRenderer& rend
if (distance > this->thresholdUp) {
node->subdivide();
} else if (distance < this->thresholdDown && node->parentNode) {
node->parentNode->simplify();
if (node->parentNode->simplify()) {
nodesToClear.emplace_back(node->parentNode);
}
}
}
}
for (auto node : nodesToClear) {
node->clear();
}
}
7 changes: 4 additions & 3 deletions examples/libs/geo/nodes/MapNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ void MapNode::subdivide() {
this->subdivided = true;
}

void MapNode::simplify() {
bool MapNode::simplify() {
const auto minZoom = this->mapView->minZoom();
if (this->level - 1 < minZoom) {
return;
return false;
}

// Clear children and reset flags
this->subdivided = false;
this->layers.enable(0);
this->clear();
this->nodesLoaded = 0;

return true; //signals that node needs clearing. Can't do it while traversing
}

void MapNode::loadData() {
Expand Down
3 changes: 1 addition & 2 deletions examples/libs/geo/nodes/MapNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace threepp {

public:
MapNode* parentNode = nullptr;
bool remove = false;

MapNode(MapNode* parentNode, MapView* mapView,
int location = QuadTreePosition::root,
Expand All @@ -36,7 +35,7 @@ namespace threepp {

void subdivide();

void simplify();
bool simplify();

void loadData();

Expand Down
8 changes: 2 additions & 6 deletions src/threepp/core/Object3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ Object3D& Object3D::translateZ(float distance) {

void Object3D::localToWorld(Vector3& vector) {

this->updateWorldMatrix( true, false ); // https://github.com/mrdoob/three.js/pull/25097
this->updateWorldMatrix(true, false);// https://github.com/mrdoob/three.js/pull/25097

vector.applyMatrix4(*this->matrixWorld);
}

void Object3D::worldToLocal(Vector3& vector) {

this->updateWorldMatrix( true, false ); // https://github.com/mrdoob/three.js/pull/25097
this->updateWorldMatrix(true, false);// https://github.com/mrdoob/three.js/pull/25097

Matrix4 _m1{};

Expand Down Expand Up @@ -204,7 +204,6 @@ void Object3D::add(const std::shared_ptr<Object3D>& object) {

this->children_.emplace_back(object);
add(*object);

}

void Object3D::add(Object3D& object) {
Expand Down Expand Up @@ -323,7 +322,6 @@ void Object3D::traverse(const std::function<void(Object3D&)>& callback) {

callback(*this);

auto _childrenCopy = children_; // keep a copy because callback may delete children
for (auto& c : children) {

c->traverse(callback);
Expand All @@ -336,8 +334,6 @@ void Object3D::traverseVisible(const std::function<void(Object3D&)>& callback) {

callback(*this);

auto _childrenCopy = children_; // keep a copy because callback may delete children

for (auto& c : children) {

c->traverseVisible(callback);
Expand Down

0 comments on commit e465778

Please sign in to comment.