Browse Source

working camera move & sort

tsns-map
cailean 2 months ago
parent
commit
0778d1134a
  1. 3
      onnx-test.code-workspace
  2. 113
      src/Map.cpp
  3. 15
      src/Map.h
  4. 18
      src/ofApp.cpp

3
onnx-test.code-workspace

@ -102,7 +102,8 @@
"__split_buffer": "cpp",
"__tree": "cpp",
"filesystem": "cpp",
"__mutex_base": "cpp"
"__mutex_base": "cpp",
"__node_handle": "cpp"
}
}
}

113
src/Map.cpp

@ -27,12 +27,19 @@ void Map::Setup(){
SetupNodes();
}
void Map::Update(){
void Map::Update(std::string& vp_img, bool& is_active){
time = ofGetElapsedTimef();
for(auto& n : nodes){
glm::vec2 offset = n.amplitude * sin(n.speed * time);
n.offset = glm::vec3(offset.x, offset.y, 0);
}
std::cout << cam.getPosition() << std::endl;
if(is_active){
has_reached = false;
}
if(!is_active)
MoveCamera(vp_img);
}
void Map::Draw(){
@ -46,17 +53,18 @@ void Map::Draw(){
ofMatrix4x4 viewMatrix = cam.getModelViewMatrix();
ofMatrix4x4 mvpMatrix = projectionMatrix * viewMatrix;
for (auto& n :nodes){
glm::vec3 node_position = n.position + n.offset;
for (auto& n :sortedNodes){
glm::vec3 node_position = n->position + n->offset;
if(isFrustum(node_position, 50)){
n.texture.getTexture().bind();
n->texture.getTexture().bind();
ofPushMatrix();
ofFill();
n.geom.setPosition(node_position);
n.geom.draw();
n->geom.setPosition(node_position);
n->geom.draw();
ofPopMatrix();
n.texture.getTexture().unbind();
n->texture.getTexture().unbind();
}
}
@ -80,10 +88,10 @@ void Map::SetupNodes(){
ofPlanePrimitive plane;
img.load("data/" + node.image_path);
int imageW = img.getWidth();
int imageH = img.getHeight();
int maxWidth = 100;
int maxHeight = 100;
int imageW = img.getWidth() * 0.1;
int imageH = img.getHeight() * 0.1;
int maxWidth = 1;
int maxHeight = 1;
float aspectRatio = (float)imageW / (float)imageH;
// Determine plane dimensions based on aspect ratio and constraints
@ -108,16 +116,16 @@ void Map::SetupNodes(){
planeH = maxWidth / aspectRatio;
}
plane.set(planeW, planeH);
plane.set(imageW, imageH);
plane.setPosition(node.position);
plane.setResolution(2, 2);
plane.setResolution(10, 10);
plane.mapTexCoordsFromTexture(img.getTexture());
img.getTexture().setTextureWrap(GL_REPEAT, GL_REPEAT);
plane.setScale(1);
node.geom = plane;
node.texture = img;
node.speed = glm::vec2(ofRandom(0.1, 2), ofRandom(0.1, 2));
node.speed = glm::vec2(ofRandom(0.1, 0.5), ofRandom(0.1, 0.5));
node.amplitude = glm::vec2(ofRandom(1, 5), ofRandom(1, 5));
node.phase = glm::vec2(ofRandom(0, TWO_PI), ofRandom(0, TWO_PI));
}
@ -159,11 +167,16 @@ void Map::SetupTSNE(){
for (size_t i = 0; i < points.size(); i++){
const auto& vec = points[i];
auto& n = nodes[i];
n.position = ( (glm::vec3(vec[0], vec[1], vec[2]) * scale) - (scale / 2) );
n.position = (glm::vec3(vec[0] * scale, vec[1] * scale, vec[2] * scale));
node_hash_map[n.image_path] = &n;
}
SortNodes();
// Instead of sorting nodes, just create a sorted reference list
for (auto& node : nodes) {
sortedNodes.push_back(&node); // Pointers to the original nodes
}
SortNodes(); // Sort the references for rendering
}
/*
@ -172,18 +185,72 @@ void Map::SetupTSNE(){
void Map::Setup3D(){
cam.setFov(20);
cam.removeAllInteractions();
cam.addInteraction(ofEasyCam::TRANSFORM_TRANSLATE_XY, OF_MOUSE_BUTTON_LEFT);
cam.addInteraction(ofEasyCam::TRANSFORM_TRANSLATE_Z, OF_MOUSE_BUTTON_MIDDLE);
}
/*
Query hashmap
*/
void Map::SearchMap(){
std::string t = "dataset/20230601_webexclusi-irelandsey_cl11536538/segmented_images/000000000_0.png";
bool Map::SearchMap(std::string& search_string){
std::string t = search_string;
if(node_hash_map.find(t) != node_hash_map.end()){
Node* n = node_hash_map[t];
std::cout << n->foldername << std::endl;
return true;
} else {
return false;
}
}
/* search if image is in the hashmap -> if true, move to its position */
void Map::MoveCamera(std::string& vp_img){
if(SearchMap(vp_img)){
Node* n = node_hash_map[vp_img];
glm::vec3 cur_pos(cam.getPosition().x, cam.getPosition().y, cam.getPosition().z);
glm::vec3 target_pos((n->position.x), (n->position.y), (n->position.z) + 100);
glm::vec3 dir = target_pos - cur_pos;
float dist = glm::length(dir);
dir = glm::normalize(dir);
accel = dir * 0.02f;
vel += accel;
// Define minimum and maximum velocities based on distance
const float minVelocity = 0.01f; // Minimum velocity when close to the target
const float maxVelocityDistance = 1000.0f; // Distance at which max velocity is applied
const float maxVelocity = 50.0f; // Maximum velocity
// Scale max velocity based on distance
float dynamicMaxVelocity = glm::clamp(dist / maxVelocityDistance * maxVelocity, minVelocity, maxVelocity);
// Clamp the velocity to the range [-dynamicMaxVelocity, dynamicMaxVelocity]
float velocityLength = glm::length(vel);
if (velocityLength > dynamicMaxVelocity) {
vel = glm::normalize(vel) * dynamicMaxVelocity;
}
// Update position
glm::vec3 newPosition = cur_pos + vel;
if(cam.getPosition() == target_pos){
has_reached = true;
}
if(!has_reached){
cam.setPosition(target_pos);
}
std::cout << "velocity " << vel << std::endl;
std::cout << "target " << target_pos << std::endl;
std::cout << "new position " << cam.getPosition() << std::endl;
}
}
void Map::buttonPressed(int key){
if(key==OF_KEY_LEFT){
z_adj -= 1;
} else {
z_adj += 1;
}
}
@ -191,11 +258,9 @@ void Map::SearchMap(){
Sort nodes by z position, for draw calls (lowest -> highest)
*/
void Map::SortNodes(){
std::cout << "Sorting Nodes.." << std::endl;
std::sort(nodes.begin(), nodes.end(), [](const Node& a, const Node&b){
return a.position.z < b.position.z;
std::sort(sortedNodes.begin(), sortedNodes.end(), [](const Node* a, const Node* b){
return a->position.z < b->position.z; // Sorting by z-position
});
std::cout << "Finished Sorting!" << std::endl;
}
bool Map::isFrustum(const glm::vec3& position, float radius){

15
src/Map.h

@ -28,17 +28,21 @@ class Map {
Map();
void Setup();
void Update();
void Update(std::string& vp_img, bool& is_active);
void Draw();
void SetupNodes();
void SetupTSNE();
void Setup3D();
void SearchMap();
bool SearchMap(std::string& search_string);
void SortNodes();
void MoveCamera(std::string& vp_img);
bool isFrustum(const glm::vec3& position, float radius);
void buttonPressed(int key);
/*
Variables
*/
@ -46,7 +50,8 @@ class Map {
float time;
ofFbo mapFbo;
vector<Node> nodes;
std::vector<Node> nodes;
std::vector<Node*> sortedNodes;
float scale;
ofxTSNE tsne;
@ -68,6 +73,10 @@ class Map {
ofLight light;
ofEasyCam cam;
float zoom;
glm::vec3 accel;
glm::vec3 vel;
float z_adj = 0;
bool has_reached = false;
ofImage fboImage;
ofPixels fboPixels;

18
src/ofApp.cpp

@ -7,6 +7,8 @@ void ofApp::setup(){
ofSetVerticalSync(true);
window_width = 1512;
window_height = ofGetWindowHeight();
/* setup pi_mapper */
ofx::piMapper::VideoSource::enableAudio = false;
ofx::piMapper::VideoSource::useHDMIForAudio = false;
projection_mapper.registerFboSource(quad_src);
@ -42,6 +44,8 @@ void ofApp::setup(){
/* Shader output */
rampedFbo.allocate(window_width, window_height);
/* pi_mapper set output fbo_src */
quad_src.setFbo(&rampedFbo);
emoteImage.allocate(260, 260);
@ -91,7 +95,7 @@ void ofApp::update(){
detected_faces.clear();
/* tsnemap fbo - first run to allocate fbo */
map.Update();
map.Update(server->vp_resp.image, server->is_active);
if(firstRun){
map.Draw();
@ -150,7 +154,6 @@ void ofApp::draw(){
ofSetBackgroundColor(0);
tf.drawString(std::to_string(ofGetFrameRate()), 10, 30);
ofPopMatrix();
// printEmotions();
// send final fbo (ramp.fbo) to pi_mapper
@ -208,12 +211,12 @@ void ofApp::inferEmotionalState(){
*/
void ofApp::renderDepthMap(){
rampedFbo.begin();
depthToColourShader.begin();
depthToColourShader.setUniformTexture("tex0", screen_fbo.getTexture(), 0);
depthToColourShader.setUniform1f("texW", rampedFbo.getWidth());
depthToColourShader.setUniform1f("texH", rampedFbo.getHeight());
// depthToColourShader.begin();
// depthToColourShader.setUniformTexture("tex0", screen_fbo.getTexture(), 0);
// depthToColourShader.setUniform1f("texW", rampedFbo.getWidth());
// depthToColourShader.setUniform1f("texH", rampedFbo.getHeight());
screen_fbo.draw(0, 0);
depthToColourShader.end();
// depthToColourShader.end();
rampedFbo.end();
rampedFbo.draw(0,0);
@ -242,6 +245,7 @@ void ofApp::displayFrame(){
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
projection_mapper.keyPressed(key);
map.buttonPressed(key);
}
//--------------------------------------------------------------

Loading…
Cancel
Save