Browse Source

last update (tsne nn)

master
cailean 2 months ago
parent
commit
21917be6f1
  1. BIN
      bin/data/new_tree.bin
  2. BIN
      bin/image-to-mesh
  3. 11
      src/Bullet.cpp
  4. 8
      src/Bullet.h
  5. 76
      src/ofApp.cpp
  6. 28
      src/ofApp.h

BIN
bin/data/new_tree.bin

Binary file not shown.

BIN
bin/image-to-mesh

Binary file not shown.

11
src/Bullet.cpp

@ -1,6 +1,6 @@
#include "Bullet.h" #include "Bullet.h"
void Bullet::setup(vector<Node>& _nodes){ void Bullet::setup(){
/* setup camera & world */ /* setup camera & world */
camera_start_pos = ofVec3f(0, -3.f, -40.f); camera_start_pos = ofVec3f(0, -3.f, -40.f);
camera_end_position = glm::vec3(ofRandom(-100, 100), ofRandom(-100, 100), ofRandom(0, 0)); camera_end_position = glm::vec3(ofRandom(-100, 100), ofRandom(-100, 100), ofRandom(0, 0));
@ -31,6 +31,7 @@ void Bullet::setup(vector<Node>& _nodes){
} }
void Bullet::update(bool& is_controller_active, Node& chosen_node, vector<Node*> nn) { void Bullet::update(bool& is_controller_active, Node& chosen_node, vector<Node*> nn) {
std::lock_guard<std::mutex> lock(shapeMutex);
nn_nodes = nn; nn_nodes = nn;
@ -173,7 +174,6 @@ void Bullet::update(bool& is_controller_active, Node& chosen_node, vector<Node*>
} }
void Bullet::draw(){ void Bullet::draw(){
std::lock_guard<std::mutex> lock(shapeMutex);
float shapes_drawn = 0; float shapes_drawn = 0;
@ -369,6 +369,13 @@ void Bullet::setNodes(vector<Node>& _nodes){
nodes = _nodes; nodes = _nodes;
} }
void Bullet::updateTSNEPosition(vector<Node>& _nodes){
for(size_t i = 0; i < _nodes.size(); i++){
nodes[i].tsne_position = _nodes[i].tsne_position;
}
}
/* check if a node is inside a given radius, deactivate & stall if not. if it drifts too far reset position*/ /* check if a node is inside a given radius, deactivate & stall if not. if it drifts too far reset position*/
void Bullet::updateNodeActivation(Node& node) { void Bullet::updateNodeActivation(Node& node) {
glm::vec3 node_pos = node.collider->getPosition(); glm::vec3 node_pos = node.collider->getPosition();

8
src/Bullet.h

@ -27,7 +27,7 @@ struct CameraPosition {
class Bullet{ class Bullet{
public: public:
void setup(vector<Node>& _nodes); void setup();
void update(bool& is_camera_active, Node& chosen_node, vector<Node*> nn); void update(bool& is_camera_active, Node& chosen_node, vector<Node*> nn);
void draw(); void draw();
void addMesh(ofMesh _mesh, ofMesh _simple_mesh, Node& _node); void addMesh(ofMesh _mesh, ofMesh _simple_mesh, Node& _node);
@ -38,6 +38,7 @@ class Bullet{
bool checkNodeVisibility(const Node& n); bool checkNodeVisibility(const Node& n);
glm::vec3 getCameraPosition(); glm::vec3 getCameraPosition();
void setNodes(vector<Node>& _nodes); void setNodes(vector<Node>& _nodes);
void updateTSNEPosition(vector<Node>& _nodes);
void updateNodeActivation(Node& node); void updateNodeActivation(Node& node);
ofxBulletWorldRigid world; ofxBulletWorldRigid world;
vector <ofxBulletBox*> bounds; vector <ofxBulletBox*> bounds;
@ -145,6 +146,7 @@ class Bullet{
private: private:
std::vector<std::thread> workerThreads; std::vector<std::thread> workerThreads;
std::mutex shapeMutex; std::mutex shapeMutex;
std::mutex nodesMutex;
std::atomic<bool> shouldTerminate{false}; std::atomic<bool> shouldTerminate{false};
std::atomic<size_t> repulsionNodeIndex{0}; std::atomic<size_t> repulsionNodeIndex{0};
@ -165,8 +167,8 @@ class Bullet{
Node* current_target_node = nullptr; Node* current_target_node = nullptr;
/* random walk vars */ /* random walk vars */
const float RANDOM_WALK_RADIUS = 700.0f; const float RANDOM_WALK_RADIUS = 450.0f;
const float RANDOM_WALK_SPEED = 25.0f; // Adjust for slower/faster movement const float RANDOM_WALK_SPEED = 40.0f; // Adjust for slower/faster movement
const float TARGET_REACHED_DISTANCE = 5.0f; const float TARGET_REACHED_DISTANCE = 5.0f;
const float RANDOM_WALK_DELAY = 10.0f; // 5 seconds delay const float RANDOM_WALK_DELAY = 10.0f; // 5 seconds delay

76
src/ofApp.cpp

@ -61,8 +61,7 @@ void ofApp::setup(){
ORTCHAR_T* modelPath_Yolo = "/home/cailean/Desktop/openframeworks/of_v0.12.0_linux64gcc6_release/apps/myApps/image-to-mesh/bin/data/models/yolov5s-face.onnx"; ORTCHAR_T* modelPath_Yolo = "/home/cailean/Desktop/openframeworks/of_v0.12.0_linux64gcc6_release/apps/myApps/image-to-mesh/bin/data/models/yolov5s-face.onnx";
ORTCHAR_T* modelPath_Indoor_Dynamic = "/home/cailean/Desktop/openframeworks/of_v0.12.0_linux64gcc6_release/apps/myApps/image-to-mesh/bin/data/models/depth_anything_v2_vitl.onnx"; ORTCHAR_T* modelPath_Indoor_Dynamic = "/home/cailean/Desktop/openframeworks/of_v0.12.0_linux64gcc6_release/apps/myApps/image-to-mesh/bin/data/models/depth_anything_v2_vitl.onnx";
/* bullet setup */ bullet.setup();
bullet.setup(nodes);
/* pi mapper setup */ /* pi mapper setup */
// mapper.registerFboSource(projection_src); // mapper.registerFboSource(projection_src);
@ -118,6 +117,7 @@ void ofApp::setup(){
server->start(); server->start();
last_updated_time = ofGetElapsedTimef(); last_updated_time = ofGetElapsedTimef();
last_updated_time_esp = ofGetElapsedTimef();
} }
//-------------------------------------------------------------- //--------------------------------------------------------------
@ -137,14 +137,17 @@ void ofApp::update(){
} }
if(current_time - last_updated_time >= 1){ if(current_time - last_updated_time >= 0.1){
buildKDTree(); buildKDTree();
getNearestImages(); getNearestImages();
depth_esp.update();
depth_onnx_esp.SetPixels(model_esp_out_fbo);
last_updated_time = current_time; last_updated_time = current_time;
} }
if(current_time - last_updated_time_esp >= 4.0){
depth_esp.update();
depth_onnx_esp.SetPixels(model_esp_out_fbo);
last_updated_time_esp = current_time;
}
if(ofGetFrameNum() < 1){ if(ofGetFrameNum() < 1){
@ -177,6 +180,16 @@ void ofApp::update(){
//mapper.update(); //mapper.update();
bullet.update(server->is_active, server->getChosenNode(), nn_nodes); bullet.update(server->is_active, server->getChosenNode(), nn_nodes);
if(tsne_update_complete) {
tsne_update_complete = false; // Reset flag
onTSNEUpdateComplete(); // Call your main thread function
}
if(ofGetElapsedTimef() > tsne_start_time + TNSE_DURATION){
tsne_iter_idx = (tsne_iter_idx + 1) % 9;
updateTSNEPositions(nodes);
}
} }
//-------------------------------------------------------------- //--------------------------------------------------------------
@ -386,8 +399,6 @@ void ofApp::drawPortrait(){
} }
} }
void ofApp::drawPortraitZ(){ void ofApp::drawPortraitZ(){
ofBackgroundGradient(ofColor::white, ofColor::orange, OF_GRADIENT_LINEAR); ofBackgroundGradient(ofColor::white, ofColor::orange, OF_GRADIENT_LINEAR);
@ -586,13 +597,50 @@ void ofApp::createNodes(std::string json_path){
tsne_points = tsne.run(tsne_input, dims, perplexity, theta, normalise, runManually); tsne_points = tsne.run(tsne_input, dims, perplexity, theta, normalise, runManually);
// Option 1: Using nested loops
for(size_t i = 0; i < 9; i++) {
for(size_t t = 0; t <= 100; t++) {
tsne_points = tsne.iterate();
}
point_iterations.push_back(tsne_points);
}
for(size_t i = 0; i < tsne_points.size(); i++){ for(size_t i = 0; i < tsne_points.size(); i++){
const auto& vec = tsne_points[i]; const auto& vec = point_iterations[0][i];
auto& n = nodes[i]; auto& n = nodes[i];
n.tsne_position = (glm::vec3(((vec[0] * 2) - 1) * tsne_scale, ((vec[1] * 2) - 1) * tsne_scale, -5.0f)); n.tsne_position = (glm::vec3(((vec[0] * 2) - 1) * tsne_scale, ((vec[1] * 2) - 1) * tsne_scale, -5.0f));
} }
} }
void ofApp::updateTSNEPositions(vector<Node>& _nodes){
if(tsne_updating) {
ofLog() << "TSNE update already in progress";
return;
}
tsne_updating = true;
tsne_update_complete = false; // Reset flag
tsne_thread = std::thread(&ofApp::updateTSNEPositionsThreaded, this, std::ref(_nodes));
tsne_thread.detach();
}
void ofApp::updateTSNEPositionsThreaded(vector<Node>& _nodes){
ofLog() << "Starting TSNE update in thread";
{
std::lock_guard<std::mutex> lock(nodes_mutex);
for(size_t i = 0; i < point_iterations[tsne_iter_idx].size(); i++) {
const auto& vec = point_iterations[tsne_iter_idx][i];
nodes[i].tsne_position = glm::vec3(
((vec[0] * 2) - 1) * tsne_scale,
((vec[1] * 2) - 1) * tsne_scale,
-5.0f
);
}
}
tsne_updating = false;
tsne_update_complete = true; // Set completion flag
ofLog() << "TSNE update complete";
}
std::vector<std::vector<double>> ofApp::createDoubleVectorFromNodes(const std::vector<Node>& nodes) { std::vector<std::vector<double>> ofApp::createDoubleVectorFromNodes(const std::vector<Node>& nodes) {
std::vector<std::vector<double>> result; std::vector<std::vector<double>> result;
result.reserve(nodes.size()); result.reserve(nodes.size());
@ -701,10 +749,22 @@ void ofApp::keyPressed(int key) {
} }
std::cout << portrait_camera.getPosition() << " : " <<portrait_camera.getOrientationQuat()<< " : " << portrait_camera.getScale() << std::endl; std::cout << portrait_camera.getPosition() << " : " <<portrait_camera.getOrientationQuat()<< " : " << portrait_camera.getScale() << std::endl;
break; break;
case 't':
tsne_iter_idx++;
updateTSNEPositions(nodes);
} }
//mapper.keyPressed(key); //mapper.keyPressed(key);
} }
// Your main thread function:
void ofApp::onTSNEUpdateComplete() {
// Do whatever you need to do after TSNE update
// This will run on the main thread
bullet.updateTSNEPosition(nodes);
tsne_start_time = ofGetElapsedTimef();
ofLog() << "Handling TSNE update completion on main thread";
}
void ofApp::keyReleased(int key){ void ofApp::keyReleased(int key){
//mapper.keyReleased(key); //mapper.keyReleased(key);
} }

28
src/ofApp.h

@ -35,13 +35,16 @@ class ofApp : public ofBaseApp{
void mousePressed(int x, int y, int button); void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button); void mouseReleased(int x, int y, int button);
void mouseDragged(int x, int y, int button); void mouseDragged(int x, int y, int button);
void onTSNEUpdateComplete();
void exit(); void exit();
ofEasyCam portrait_camera; ofEasyCam portrait_camera;
ofImage img; ofImage img;
ofMesh mesh; ofMesh mesh;
MeshGenerator mesh_generator; MeshGenerator mesh_generator;
Bullet bullet; //Bullet bullet;
vector<ofImage> images; vector<ofImage> images;
ofFbo map_fbo; ofFbo map_fbo;
@ -103,6 +106,7 @@ class ofApp : public ofBaseApp{
vector<ofFbo> esp_images; vector<ofFbo> esp_images;
ofPixels esp_comp_pixels; ofPixels esp_comp_pixels;
float last_updated_time; float last_updated_time;
float last_updated_time_esp;
/* nodes */ /* nodes */
ofJson json; ofJson json;
@ -113,8 +117,9 @@ class ofApp : public ofBaseApp{
float perplexity = 20; float perplexity = 20;
float theta = 0.2; float theta = 0.2;
bool normalise = true; bool normalise = true;
bool runManually = false; bool runManually = true;
float tsne_scale = 500; float tsne_scale = 500;
vector<vector<vector<double>>> point_iterations;
/* kd tree */ /* kd tree */
std::unique_ptr<KDTree> kd_tree; std::unique_ptr<KDTree> kd_tree;
@ -148,4 +153,23 @@ class ofApp : public ofBaseApp{
/* n-neighbour */ /* n-neighbour */
vector<Node*> nn_nodes; vector<Node*> nn_nodes;
Bullet bullet;
/* simulation update timer */
const float TNSE_DURATION = 360.0f;
float tsne_start_time;
int tsne_iter_idx = 0;
private:
std::thread tsne_thread;
std::atomic<bool> tsne_updating{false};
std::mutex nodes_mutex;
bool tsne_update_complete = false;
void updateTSNEPositions(vector<Node>& _nodes);
void updateTSNEPositionsThreaded(vector<Node>& _nodes);
}; };

Loading…
Cancel
Save