Browse Source

after-install

master
cailean 1 month ago
parent
commit
6bb4fa75cf
  1. BIN
      bin/data/new_tree.bin
  2. BIN
      bin/image-to-mesh
  3. BIN
      bin/image-to-mesh_debug
  4. 139
      src/Bullet.cpp
  5. 14
      src/Bullet.h
  6. 23
      src/network/Server.cpp
  7. 6
      src/network/Server.h
  8. 110
      src/ofApp.cpp
  9. 2
      src/ofApp.h

BIN
bin/data/new_tree.bin

Binary file not shown.

BIN
bin/image-to-mesh

Binary file not shown.

BIN
bin/image-to-mesh_debug

Binary file not shown.

139
src/Bullet.cpp

@ -2,7 +2,7 @@
void Bullet::setup(){
/* setup camera & world */
camera_start_pos = ofVec3f(0, -3.f, -40.f);
camera_start_pos = ofVec3f(0, 0, -40.f);
camera_end_position = glm::vec3(ofRandom(-100, 100), ofRandom(-100, 100), ofRandom(0, 0));
camera_move_duration = 20.f;
camera_move_start_time = ofGetElapsedTimef();
@ -35,7 +35,12 @@ void Bullet::update(bool& is_controller_active, Node& chosen_node, vector<Node*>
nn_nodes = nn;
static float blend_factor = 0.0f;
float current_zoom = camera.getScale().x;
const float MIN_ZOOM = 0.04f;
const float MAX_ZOOM = 0.3f;
const float ZOOM_OUT_START_TIME = 40.0f;
float d_time = ofGetLastFrameTime();
float target_zoom = 0;
contr_active = is_controller_active;
@ -46,129 +51,49 @@ void Bullet::update(bool& is_controller_active, Node& chosen_node, vector<Node*>
time_at_target = 0;
random_walk_time = 0;
scale_out_factor = 0;
camera_velocity = glm::vec3(0.0f);
} else {
new_chosen_node = false;
time_at_target += d_time;
}
cameraBounds = getCameraBounds(camera);
world.update();
float d_time = ofGetLastFrameTime();
glm::vec3 target_pos = chosen_node.collider->getPosition();
glm::vec3 current_pos = camera.getPosition();
glm::vec3 dir = current_pos - target_pos;
float dist = glm::length(dir);
// Check if we're close to target
if (dist < TARGET_REACHED_DISTANCE) {
time_at_target += d_time;
// Start random walk after delay
if (time_at_target >= RANDOM_WALK_DELAY && !is_random_walking) {
is_random_walking = true;
random_target = generateRandomTarget();
}
} else {
time_at_target = 0.0f;
// Start random walk after delay
if (time_at_target >= RANDOM_WALK_DELAY && !is_random_walking) {
is_random_walking = true;
}
// Handle random walk mode
if (is_random_walking) {
target_zoom = MAX_ZOOM;
random_walk_time += d_time;
glm::vec3 to_random_target = random_target - current_pos;
float random_dist = glm::length(to_random_target);
// Generate new random target when reached current one
if (random_dist < 10.0f) {
random_target = generateRandomTarget();
}
// Smooth movement towards random target
if (random_dist > 0.0001f) {
glm::vec3 random_dir = glm::normalize(to_random_target);
camera_velocity = random_dir * RANDOM_WALK_SPEED * d_time;
}
} else {
target_zoom = MIN_ZOOM;
}
// Regular movement towards chosen node
else if (!std::isnan(dist) && dist >= 0.0001f) {
glm::vec3 norm_dir = glm::normalize(dir);
if(!std::isnan(norm_dir.x) && !std::isnan(norm_dir.y) && !std::isnan(norm_dir.z)) {
float distance_factor = glm::min(dist / 50.0f, 1.0f);
float current_acceleration = ACCELERATION * distance_factor;
glm::vec3 acceleration = -norm_dir * current_acceleration * d_time;
if(!std::isnan(acceleration.x) && !std::isnan(acceleration.y) && !std::isnan(acceleration.z)) {
camera_velocity += acceleration;
}
/* decelerate if close to target */
if (dist < 5.0f) {
float decel_factor = dist / 5.0f;
camera_velocity *= (1.0f - (1.0f - decel_factor) * d_time * 10.0f);
}
float mag = glm::length(camera_velocity);
current_zoom += (target_zoom - camera.getScale().x) * 0.005f;
/* clamp vel */
if (mag > MAX_VELOCITY) {
camera_velocity = glm::normalize(camera_velocity) * MAX_VELOCITY;
}
}
}
camera.setScale(current_zoom);
// 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);
current_zoom = current_zoom + (target_zoom - current_zoom) * 0.05f;
// Calculate scale-out factor if we're in random walk mode
if (random_walk_time > SCALE_OUT_START_TIME) {
float scale_progress = (random_walk_time - SCALE_OUT_START_TIME) / SCALE_OUT_DURATION;
scale_progress = ofClamp(scale_progress, 0.0f, 1.0f);
// Smoother easing function (quintic ease in/out)
float t = scale_progress;
if (t < 0.5f) {
scale_out_factor = 16 * t * t * t * t * t;
} else {
t = t - 1;
scale_out_factor = 1 + 16 * t * t * t * t * t;
}
if(!glm::any(glm::isnan((target_pos)))){
glm::vec3 new_pos = current_pos + (target_pos - current_pos) * 0.01f;
new_pos.z = current_pos.z;
camera.setPosition(new_pos);
}
// Blend between velocity-based zoom and scale-out with smooth transitions both ways
float final_scale = current_zoom;
// Calculate target blend based on whether we're random walking
float target_blend = is_random_walking ? scale_out_factor : 0.0f;
blend_factor += (target_blend - blend_factor) * 0.05f; // Smooth transition both ways
if (blend_factor > 0.001f) { // Apply blended scale if we have any blend factor
final_scale = ofLerp(current_zoom, 0.3, blend_factor);
if(glm::any(glm::isnan(camera.getPosition()))){
camera.setPosition(0, 0, -40);
}
camera.setScale(final_scale);
// Update camera position
glm::vec3 new_pos = current_pos + camera_velocity * d_time;
// Clamp position within bounds
new_pos.x = ofClamp(new_pos.x, -RANDOM_WALK_RADIUS, RANDOM_WALK_RADIUS);
new_pos.y = ofClamp(new_pos.y, -RANDOM_WALK_RADIUS, RANDOM_WALK_RADIUS);
camera.setPosition(new_pos);
// Always update these regardless of camera movement
for(auto& node : nodes) {
updateNodeActivation(node);
}
last_chosen_node = chosen_node;
}
@ -193,14 +118,13 @@ void Bullet::draw(){
if(checkNodeVisibility(n)){
// Compare pointers directly
bool is_nearest = false;
for(const auto* nn : nn_nodes) {
for(const auto* nn : nn_nodes) {
if(nn->tsne_position == n.tsne_position) { // or if you store the address differently, adjust this comparison
is_nearest = true;
break;
}
}
ofPushStyle();
ofSetColor(ofColor::orange); // Yellow for others
ofPushMatrix();
n.collider->transformGL();
@ -275,12 +199,13 @@ 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) + 20, ofRandom(_node.tsne_position.y - 50, _node.tsne_position.y + 50) + 20, -5 );
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 );
ofxBulletCustomShape* s = new ofxBulletCustomShape();
s->addMesh(_simple_mesh, _node.scale * 1.4, true);
s->create( world.world, start_location, startRot, 3.);
s->add();
s->getRigidBody()->setActivationState(DISABLE_DEACTIVATION);
s->getRigidBody()->setAngularFactor(btVector3(0, 0, 1));
s->getRigidBody()->setDamping(btScalar(0.0), btScalar(0.0));
s->getRigidBody()->setLinearFactor(btVector3(1, 1, 0));
@ -318,7 +243,7 @@ void Bullet::updateShapeBatch(size_t start, size_t end) {
float dist_sq = glm::length(direction);
glm::vec3 norm_dir = glm::normalize(direction);
glm::vec3 rand_vel(ofRandom(-1, 1));
nodes[i].collider->applyCentralForce((1.0f * norm_dir) + rand_vel);
nodes[i].collider->applyCentralForce((1.0f * norm_dir));
}
}
@ -379,10 +304,10 @@ void Bullet::updateTSNEPosition(vector<Node>& _nodes){
/* 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) {
glm::vec3 node_pos = node.collider->getPosition();
bool should_be_active = (node_pos.x < cameraBounds.x + 20 &&
node_pos.x > cameraBounds.y - 20 &&
node_pos.y < cameraBounds.z + 20 &&
node_pos.y > cameraBounds.w - 20);
bool should_be_active = (node_pos.x < cameraBounds.x + 100 &&
node_pos.x > cameraBounds.y -100 &&
node_pos.y < cameraBounds.z + 100 &&
node_pos.y > cameraBounds.w - 100);
btRigidBody* body = node.collider->getRigidBody();
if (should_be_active && !body->isActive()) {

14
src/Bullet.h

@ -16,6 +16,7 @@ struct Node {
glm::vec3 scale;
glm::vec3 tsne_position;
vector<float> raw_embedding;
int id;
int error;
};
@ -80,8 +81,8 @@ class Bullet{
float current_zoom = 0.03f;
glm::vec3 transition_start_pos;
glm::vec3 camera_velocity = glm::vec3(0.0f);
const float ACCELERATION = 100.0f; // Adjust this value
const float DECELERATION = 20.0f; // Adjust this value
const float ACCELERATION = 70.0f; // Adjust this value
const float DECELERATION = 40.0f; // Adjust this value
const float MAX_VELOCITY = 50.0f; // Your velocity cap
@ -181,10 +182,11 @@ class Bullet{
const float TARGET_REACHED_DISTANCE = 5.0f;
const float RANDOM_WALK_DELAY = 30.0f; // 5 seconds delay
const float SCALE_OUT_START_TIME = 10.0f; // When to start scaling out
const float SCALE_OUT_DURATION = 20.0f; // How long to scale out
const float INITIAL_SCALE = 0.05f; // Starting scale
const float TARGET_SCALE = 0.2f; // Final scale
const float SCALE_OUT_START_TIME = 0.0f; // When to start scaling out
const float SCALE_OUT_DURATION = 40.0f; // How long to scale out
const float INITIAL_SCALE = 0.05f;
const float TARGET_SCALE_MED = 0.1f; // Starting scale
const float TARGET_SCALE = 0.1f; // Final scale
bool is_random_walking = false;
float time_at_target = 0.0f;

23
src/network/Server.cpp

@ -8,6 +8,7 @@ void Server::start(){
previous_embedding = embedding;
last_change_time = std::chrono::steady_clock::now();
lastUpdateTime = std::chrono::steady_clock::now();
r_time = refresh_time;
/* allocate pixels in vector */
esp_pixels.resize(4);
@ -21,14 +22,21 @@ 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;
}
ofPixels fboPixels;
esp_comp.readToPixels(fboPixels);
vector<ofImage> extractedImages;
extractedImages.resize(4);
for (int i = 0; i < 4; i++) {
@ -143,7 +151,11 @@ void Server::checkActivity(){
vector<vector<float>> vp_queries;
vp_queries.push_back(embedding.to_vector());
vector<int> ids = tree.query(vp_queries, 1);
chosen_node = &nodes[ids[0]];
if(ids.size() > 0){
if(ids[0] >= 0 && ids[0] <= nodes.size() - 1){
chosen_node = &nodes[ids[0]];
}
}
// ofLog() << chosen_node->collider->getPosition();
} else {
@ -234,10 +246,7 @@ void Server::sendImagesToClients(vector<ofImage>& imgs){
}
int num_images = imgs.size();
if (num_images != 4) {
// No images to send, return early
return;
}
ofSetLogLevel(OF_LOG_FATAL_ERROR);

6
src/network/Server.h

@ -34,6 +34,7 @@ class Server{
void print();
void sendImagesToClients(vector<ofImage>& imgs);
void disconnectAllClients();
void help(ofFbo& esp_comp);
void close();
std::vector<std::vector<double>> generateRandomVectors(int count, int dimension);
@ -49,7 +50,8 @@ class Server{
VP& tree;
vector<Node>& nodes;
Node* chosen_node;
float refresh_time = 30;
float r_time = 0;
Request http;
std::string http_ip;
int http_port;
@ -59,6 +61,8 @@ class Server{
int num_of_clients = 0;
vector<ofPixels> esp_pixels;
ofPixels fboPixels;
vector<ofImage> extractedImages;
private:
Embedding previous_embedding;

110
src/ofApp.cpp

@ -140,7 +140,7 @@ void ofApp::setup(){
//--------------------------------------------------------------
void ofApp::update(){
server->update(esp_comp_fbo);
float current_time = ofGetElapsedTimef();
@ -150,22 +150,24 @@ void ofApp::update(){
if(n.tsne_position != last_chosen_node.tsne_position) {
portrait_needs_update = true;
last_chosen_node = n;
//current_cp = cam_positions[ofRandom(cam_positions.size())];
current_cp = cam_positions[ofRandom(cam_positions.size())];
} else {
portrait_needs_update = false;
}
if(current_time - last_updated_time >= 1.0){
if(current_time - last_updated_time >= 0.5){
buildKDTree();
getNearestImages();
last_updated_time = current_time;
server->update(esp_comp_fbo);
}
if(current_time - last_updated_time_esp >= 4.0){
if(current_time - last_updated_time_esp >= 1.0){
depth_esp.update();
depth_onnx_esp.SetPixels(model_esp_out_fbo);
last_updated_time_esp = current_time;
}
@ -198,19 +200,19 @@ void ofApp::update(){
}
mapper.update();
bullet.update(server->is_active, server->getChosenNode(), nn_nodes);
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);
}
// if(ofGetElapsedTimef() > tsne_start_time + TNSE_DURATION){
// tsne_iter_idx = (tsne_iter_idx + 1) % 3;
// updateTSNEPositions(nodes);
// }
//updateCurrentCameraMode();
updateCurrentCameraMode();
}
//--------------------------------------------------------------
@ -267,7 +269,6 @@ void ofApp::draw(){
shaders.setUniform1i("frame", ofGetFrameNum());
map_fbo_post.draw(0, 0);
map_fbo_alpha.draw(0,0);
//alpha_demo.draw(0, 0);
shaders.end();
comp_fbo.end();
@ -421,7 +422,12 @@ void ofApp::drawPortrait(){
}
void ofApp::drawPortraitZ(){
ofBackgroundGradient(ofColor::white, ofColor::orange, OF_GRADIENT_LINEAR);
if(current_cp.mode == CameraMode::ORTHO){
ofBackgroundGradient(ofColor::white, ofColor::black, OF_GRADIENT_LINEAR);
} else {
ofBackgroundGradient(ofColor::white, ofColor::orange, OF_GRADIENT_LINEAR);
}
/* if no faces found use full portrait texture */
ofTexture tex_color;
@ -457,43 +463,6 @@ void ofApp::drawPortraitZ(){
float p_noise_x = ofSignedNoise(time, time) * 20;
float p_noise_y = ofSignedNoise(time + 100, time + 100) * 20;
// // Second plane with offset
// ofPushMatrix();
// p_depth.begin();
// p_depth.setUniform1f("gridSize", g_size);
// p_depth.setUniform2f("resolution", glm::vec2(portrait_fbo.getWidth(), portrait_fbo.getHeight()));
// p_depth.setUniform2f("threshold", glm::vec2(minDepth, max_r));
// p_depth.setUniformTexture("tex0", tex_color, 0);
// p_depth.setUniformTexture("tex1", tex_depth, 1);
// ofFill();
// // Offset the second plane - adjust these values as needed
// ofTranslate(p_noise_x - 50, p_noise_y - 0, -150); // moved right (+200), up (+100) and back (-300)
// ofRotateDeg(60, 0, 1, 0);
// plane.draw();
// p_depth.end();
// ofPopMatrix();
// ofDisableDepthTest();
// // Second plane with offset
// ofPushMatrix();
// p_depth.begin();
// p_depth.setUniform1f("gridSize", g_size);
// p_depth.setUniform2f("resolution", glm::vec2(portrait_fbo.getWidth(), portrait_fbo.getHeight()));
// p_depth.setUniform2f("threshold", glm::vec2(minDepth, max_r));
// p_depth.setUniformTexture("tex0", tex_color, 0);
// p_depth.setUniformTexture("tex1", tex_depth, 1);
// ofFill();
// // Offset the second plane - adjust these values as needed
// ofTranslate(p_noise_x - 140, p_noise_y - 0, -150); // moved right (+200), up (+100) and back (-300)
// ofRotateDeg(-60, 0, 1, 0);
// plane.draw();
// p_depth.end();
// ofPopMatrix();
// ofDisableDepthTest();
ofPushMatrix();
p_depth.begin();
p_depth.setUniform1f("gridSize", g_size);
@ -525,6 +494,7 @@ void ofApp::getNearestImages(){
int imageIndex = 0;
for(auto& img : esp_images){
if (imageIndex >= 4) break;
/* add to nn_nodes array */
nn_nodes.push_back(&nodes[kd_result[imageIndex].second]);
@ -557,7 +527,7 @@ void ofApp::getNearestImages(){
selected_image.drawSubsection(drawX, drawY, cropWidth, cropHeight, cropX, cropY);
imageIndex++;
if (imageIndex >= 4) break; // Stop after drawing 4 images
// Stop after drawing 4 images
}
kd_out.end();
@ -580,10 +550,12 @@ void ofApp::createNodes(std::string json_path){
/* setup nodes */
bool addNode = true; // Toggle flag to track every second node
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.img.load(j["image"]);
n.img_path = (j["image"]);
n.tex = n.img.getTexture();
@ -600,6 +572,7 @@ void ofApp::createNodes(std::string json_path){
}
n.raw_embedding = t_embedding;
nodes.push_back(n);
_id++;
}
addNode = !addNode; // Toggle the flag after each valid node
}
@ -630,7 +603,7 @@ void ofApp::createNodes(std::string json_path){
}
for(size_t i = 0; i < tsne_points.size(); i++){
const auto& vec = point_iterations[0][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));
}
@ -742,29 +715,6 @@ void ofApp::queryKD(glm::vec3& _position, int k){
void ofApp::keyPressed(int key) {
switch(key) {
case OF_KEY_UP:
g_size += 5;
ofLog() << "Grid Size: " << g_size;
break;
case OF_KEY_DOWN:
g_size -= 5;
ofLog() << "Grid Size: " << g_size;
break;
case 'q':
plane_size += 5;
ofLog() << "Plane Size: " << plane_size;
break;
case 'w':
plane_size -=5;
ofLog() << "Plane Size: " << plane_size;
break;
case 'c':
// cam_pos_idx++;
// if(cam_pos_idx > cam_positions.size() - 1)
// cam_pos_idx = 0;
portrait_camera.setPosition(cam_positions[cam_pos_idx].position);
portrait_camera.setOrientation(cam_positions[cam_pos_idx].rotation);
portrait_camera.setScale(cam_positions[cam_pos_idx].scale);
case 'n':
if(bullet.print_debug){
bullet.print_debug = false;
@ -773,19 +723,9 @@ void ofApp::keyPressed(int key) {
}
std::cout << portrait_camera.getPosition() << " : " <<portrait_camera.getOrientationQuat()<< " : " << portrait_camera.getScale() << std::endl;
break;
case 't':
tsne_iter_idx++;
updateTSNEPositions(nodes);
case 'v':
ofToggleFullscreen();
case 'o':
if(draw_face){
draw_face = false;
} else {
draw_face = true;
}
case 'e':
current_cp = cam_positions[ofRandom(cam_positions.size())];
break;
}
mapper.keyPressed(key);
}

2
src/ofApp.h

@ -161,7 +161,7 @@ class ofApp : public ofBaseApp{
Bullet bullet;
/* simulation update timer */
const float TNSE_DURATION = 360.0f;
const float TNSE_DURATION = 180.0f;
float tsne_start_time;
int tsne_iter_idx = 0;

Loading…
Cancel
Save