Browse Source

added text & tsne positions &i ds

master
cailean 1 month ago
parent
commit
beee785fc7
  1. BIN
      bin/data/MaziusDisplay-Bold.otf
  2. BIN
      bin/data/new_tree.bin
  3. BIN
      bin/image-to-mesh
  4. 41
      src/Bullet.cpp
  5. 5
      src/Bullet.h
  6. 6
      src/network/Server.cpp
  7. 156
      src/ofApp.cpp
  8. 11
      src/ofApp.h

BIN
bin/data/MaziusDisplay-Bold.otf

Binary file not shown.

BIN
bin/data/new_tree.bin

Binary file not shown.

BIN
bin/image-to-mesh

Binary file not shown.

41
src/Bullet.cpp

@ -21,7 +21,6 @@ void Bullet::setup(){
world.setCamera(&camera);
world.setGravity( ofVec3f(0, 0, 0) );
for (int i = 0; i < numThreads; ++i) {
workerThreads.emplace_back(&Bullet::workerThreadFunction, this, i);
}
@ -34,17 +33,24 @@ void Bullet::setup(){
void Bullet::update(bool& is_controller_active, Node& chosen_node, vector<Node*> nn) {
std::lock_guard<std::mutex> lock(shapeMutex);
static float t = 0;
t += ofGetElapsedTimef();
// Cycle throught tsne_list
if( t > ITER_DURATION){
t = 0;
tsne_iter = (tsne_iter + 1) % 3;
}
nn_nodes = nn;
contr_active = is_controller_active;
static float blend_factor = 0.0f;
const float MIN_ZOOM = 0.04f;
const float MAX_ZOOM = 0.3f;
const float ZOOM_OUT_START_TIME = 40.0f;
float d_time = ofGetLastFrameTime();
contr_active = is_controller_active;
float d_time = std::min(static_cast<float>(ofGetLastFrameTime()), 1.0f / 30.0f);
if(chosen_node.tsne_position != last_chosen_node.tsne_position){
if(chosen_node.id != last_chosen_node.id){
ofLog() << "new target!";
new_chosen_node = true;
is_random_walking = false;
@ -118,10 +124,12 @@ void Bullet::update(bool& is_controller_active, Node& chosen_node, vector<Node*>
}
}
// Smoothed zoom based on velocity
// Smoothed zoom based on velocity
float vel_magnitude = glm::length(camera_velocity);
static float current_zoom = 0.02f;
float target_zoom = ofMap(vel_magnitude, 0, MAX_VELOCITY, 0.02, 0.3, true);
// CHANGE MIN/MAX ZOOM HERE
static float current_zoom = 0.04f;
float target_zoom = ofMap(vel_magnitude, 0, MAX_VELOCITY, 0.04, 0.3, true);
current_zoom = current_zoom + (target_zoom - current_zoom) * 0.05f;
// Calculate scale-out factor if we're in random walk mode
@ -184,13 +192,12 @@ void Bullet::draw(){
ofNoFill();
for(const auto& n : nodes){
if(checkNodeVisibility(n)){
// Compare pointers directly
bool is_nearest = false;
for(const auto* nn : nn_nodes) {
if(nn->tsne_position == n.tsne_position) { // or if you store the address differently, adjust this comparison
if(nn->id == n.id) { // or if you store the address differently, adjust this comparison
is_nearest = true;
break;
}
@ -217,7 +224,7 @@ void Bullet::draw(){
if(checkNodeVisibility(n)){
bool is_nearest = false;
for(const auto* nn : nn_nodes) {
if(nn->tsne_position == n.tsne_position) { // or if you store the address differently, adjust this comparison
if(nn->id == n.id) { // or if you store the address differently, adjust this comparison
is_nearest = true;
break;
}
@ -270,7 +277,7 @@ void Bullet::addMesh(ofMesh _mesh, ofMesh _simple_mesh, Node& _node){
ofQuaternion startRot = ofQuaternion(0., 0., 0., PI);
ofVec3f start_location = ofVec3f( ofRandom(_node.tsne_position.x - 50, _node.tsne_position.x + 50), ofRandom(_node.tsne_position.y - 50, _node.tsne_position.y + 50) , -5 );
ofVec3f start_location = ofVec3f( ofRandom(_node.tsne_list[0].x - 50, _node.tsne_list[0].x + 50), ofRandom(_node.tsne_list[0].y - 50, _node.tsne_list[0].y + 50) , -5 );
ofxBulletCustomShape* s = new ofxBulletCustomShape();
s->addMesh(_simple_mesh, _node.scale * 1.4, true);
@ -310,7 +317,7 @@ void Bullet::workerThreadFunction(int threadId) {
void Bullet::updateShapeBatch(size_t start, size_t end) {
for (size_t i = start; i < end; ++i) {
std::lock_guard<std::mutex> lock(shapeMutex);
nodes[i].tsne_position = nodes[i].tsne_list[tsne_iter];
glm::vec3 pos = nodes[i].collider->getPosition();
glm::vec3 target_pos(nodes[i].tsne_position.x, nodes[i].tsne_position.y, -5);
glm::vec3 direction = target_pos - pos;
@ -393,3 +400,7 @@ void Bullet::updateNodeActivation(Node& node) {
}
}
float Bullet::getRandomWalkTime(){
return random_walk_time;
}

5
src/Bullet.h

@ -15,6 +15,7 @@ struct Node {
ofTexture tex;
glm::vec3 scale;
glm::vec3 tsne_position;
vector<glm::vec3> tsne_list{3};
vector<float> raw_embedding;
int id;
int error;
@ -50,6 +51,7 @@ class Bullet{
void setNodes(vector<Node>& _nodes);
void updateTSNEPosition(vector<Node>& _nodes);
void updateNodeActivation(Node& node);
float getRandomWalkTime();
ofxBulletWorldRigid world;
vector <ofxBulletBox*> bounds;
ofxBulletCustomShape* boundsShape;
@ -195,6 +197,9 @@ class Bullet{
float random_walk_time = 0.0f;
float scale_out_factor = 0.0f;
int tsne_iter = 0;
const int ITER_DURATION = 120;
// Generate new random target within bounds
glm::vec3 generateRandomTarget() {
return glm::vec3(

6
src/network/Server.cpp

@ -23,12 +23,6 @@ void Server::start(){
void Server::update(ofFbo& esp_comp){
/* set all past clients to false, to check connection */
// if(ofGetElapsedTimef() > r_time){
// server.disconnectAllClients();
// r_time = refresh_time + ofGetElapsedTimef();
// ofLog() << "restart";
// }
for(auto& c : clients){
c.second.connected = false;
}

156
src/ofApp.cpp

@ -32,6 +32,8 @@ void ofApp::setup(){
alpha_demo.allocate(portrait_pre_fbo_alpha.getWidth(), portrait_pre_fbo_alpha.getHeight(), OF_IMAGE_COLOR_ALPHA);
text_display.allocate(ofGetWindowWidth(), map_h, GL_RGBA);
/* black pi mapper bg */
mapper_black.allocate(300, 1080, GL_RGB);
mapper_black.begin();
@ -63,6 +65,9 @@ void ofApp::setup(){
p_depth.load("shaders/p_depth");
map_depth.load("shaders/map_depth");
font.load("MaziusDisplay-Bold.otf", 180);
ofLog() << font.getLineHeight();
ORTCHAR_T* modelPath = "/home/cailean/Desktop/openframeworks/of_v0.12.0_linux64gcc6_release/apps/myApps/image-to-mesh/bin/data/models/depth_anything_v2_vits.onnx";
ORTCHAR_T* modelPath_Small = "/home/cailean/Desktop/openframeworks/of_v0.12.0_linux64gcc6_release/apps/myApps/image-to-mesh/bin/data/models/depth_anything_v2_vitb.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";
@ -119,8 +124,6 @@ void ofApp::setup(){
portrait_camera.setOrientation(current_cp.rotation);
portrait_camera.setScale(current_cp.scale);
updateCurrentCameraMode();
// portrait_camera.removeAllInteractions();
// portrait_camera.disableMouseInput();
createNodes("data/json/sv_embeddings.json");
@ -140,13 +143,12 @@ void ofApp::setup(){
//--------------------------------------------------------------
void ofApp::update(){
float current_time = ofGetElapsedTimef();
Node n = server->getChosenNode();
// Check if node has changed
if(n.tsne_position != last_chosen_node.tsne_position) {
if(n.id != last_chosen_node.id) {
portrait_needs_update = true;
last_chosen_node = n;
current_cp = cam_positions[ofRandom(cam_positions.size())];
@ -201,21 +203,18 @@ void ofApp::update(){
mapper.update();
bullet.update(server->is_active, n, 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) % 3;
// updateTSNEPositions(nodes);
// }
updateCurrentCameraMode();
if(bullet.getRandomWalkTime() > 60){
display_text = true;
}
}
//--------------------------------------------------------------
void ofApp::draw(){
//drawText();
ofPushStyle();
map_fbo_alpha.begin();
ofClear(0);
@ -267,7 +266,8 @@ void ofApp::draw(){
shaders.setUniform1f("time", ofGetElapsedTimef());
shaders.setUniform1i("frame", ofGetFrameNum());
map_fbo_post.draw(0, 0);
map_fbo_alpha.draw(0,0);
map_fbo_alpha.draw(0, 0);
//text_display.draw(0, 0);
shaders.end();
comp_fbo.end();
@ -277,6 +277,7 @@ void ofApp::draw(){
comp_fbo.draw(0, 0);
mapper.draw();
if(bullet.print_debug)
server->print();
}
@ -549,12 +550,12 @@ void ofApp::createNodes(std::string json_path){
/* setup nodes */
bool addNode = true; // Toggle flag to track every second node
int _id = 0;
int id = 0;
for(const auto& j : json) {
if(j.contains("vector") && j["vector"].is_array() && j["error"] == 0) {
if(addNode) { // Only process when flag is true
Node n;
n.id = _id;
n.id = id;
n.img.load(j["image"]);
n.img_path = (j["image"]);
n.tex = n.img.getTexture();
@ -571,7 +572,7 @@ void ofApp::createNodes(std::string json_path){
}
n.raw_embedding = t_embedding;
nodes.push_back(n);
_id++;
id++;
}
addNode = !addNode; // Toggle the flag after each valid node
}
@ -601,40 +602,13 @@ void ofApp::createNodes(std::string json_path){
point_iterations.push_back(tsne_points);
}
for(size_t i = 0; i < tsne_points.size(); i++){
const auto& vec = point_iterations[1][i];
auto& n = nodes[i];
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
);
for(size_t x = 0; x < point_iterations.size(); x++){
for(size_t y = 0; y < tsne_points.size(); y++){
const auto& vec = point_iterations[x][y];
auto& n = nodes[y];
n.tsne_list[x] = (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) {
@ -732,15 +706,6 @@ void ofApp::keyPressed(int 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){
mapper.keyReleased(key);
}
@ -835,3 +800,76 @@ ofMesh ofApp::createCustomPlane(float width, float height, int numX, int numY) {
return mesh;
}
void ofApp::drawText(){
text_display.begin();
ofPushStyle();
ofClear(0, 0, 0, 0);
float random_walk_time = bullet.getRandomWalkTime();
if (random_walk_time > 10) {
// Fade in and oscillate
fade_time += ofGetLastFrameTime();
if (!is_oscillating) {
// Linear fade in
t_alpha = ofClamp(fade_time, 0, 1);
if (fade_time >= 1) {
is_oscillating = true;
oscillate_time = 0;
}
} else {
// Oscillating fade
oscillate_time += ofGetLastFrameTime();
t_alpha = 1 - (glm::sin(oscillate_time) + 1) / 2;
}
} else {
// Fade out
fade_time -= ofGetLastFrameTime();
t_alpha = ofClamp(fade_time, 0, 1);
if (fade_time <= 0) {
is_oscillating = false;
is_fading_in = true;
fade_time = 0;
}
}
ofSetColor(ofColor::black, t_alpha * 255);
vector<std::string> sentence{
"HOW ARE",
"YOU",
"FEELING?"
};
// Calculate total text height
float line_height = font.getLineHeight();
int num_lines = sentence.size();
// Calculate vertical center of the screen
float screen_center = text_display.getHeight() / 2;
// Calculate total text block height
float total_text_height = line_height * num_lines;
// Add ascender height to adjust baseline
float ascender_height = font.getAscenderHeight();
// Starting Y position to center the text block
float start_y = screen_center - (total_text_height / 2) + ascender_height;
for(size_t idx = 0; idx < sentence.size(); ++idx){
const auto& t = sentence[idx];
float text_width = font.stringWidth(t);
float text_x = text_display.getWidth()/2 - text_width/2;
float text_y = start_y + (idx * line_height);
font.drawString(t, text_x, text_y);
}
ofPopStyle();
text_display.end();
}

11
src/ofApp.h

@ -37,6 +37,7 @@ class ofApp : public ofBaseApp{
void mouseDragged(int x, int y, int button);
void onTSNEUpdateComplete();
void updateCurrentCameraMode();
void drawText();
void exit();
@ -70,6 +71,9 @@ class ofApp : public ofBaseApp{
ofImage model_image_portrait;
ofImage model_image_portrait_cropped;
ofFbo text_display;
ofTrueTypeFont font;
ofShader shaders;
ofShader esp_shader;
ofShader vert_snap;
@ -167,6 +171,13 @@ class ofApp : public ofBaseApp{
bool draw_face;
/* text display */
bool display_text = false;
float t_alpha = 0;
float fade_time = 0;
float oscillate_time = 0;
bool is_fading_in = true; // Start with fading in
bool is_oscillating = false;
private:

Loading…
Cancel
Save