Commit a601879a authored by Sabrina Templeton's avatar Sabrina Templeton
Browse files

added some c++ side code for nearest neighbors

parent 94ab4563
......@@ -14,6 +14,10 @@
#include "graphics/colormap.h"
#include "common/tools.h"
#include <geogram/nn_search.h>
using namespace avro;
UT_TEST_SUITE( voronoi_toy )
......@@ -73,7 +77,8 @@ UT_TEST_CASE( test1 )
GL_CALL( glBindVertexArray(vertex_array) );
//generate random seeds desired times 3
int size = 7*3;
int num_points = 7.;
int size = num_points*3;
std::vector<graphics::gl_float> seeds;
srand (time(NULL));
......@@ -82,19 +87,39 @@ UT_TEST_CASE( test1 )
//printf("loop ran, %d point added", i);
};
//compute the nearest neighbors here
//GEO::NearestNeighborSearch* nns = GEO::NearestNeighborSearch::create(2,"BNN");
GEO::NearestNeighborSearch* nns = GEO::NearestNeighborSearch::create(3,"BNN"); // number of dimensions is really 2, but we have been using three
index_t nb_points = 7;
std::vector<real_t> x( nb_points*3 ); //3 even tho its 2d, equivalent to the seeds
//std::vector<graphics::gl_float> seeds(nb_points*3); // the actual seeds
index_t nb_points = 50;
std::vector<real_t> x( nb_points*2 ); //2 for 2d
for (index_t k=0;k<nb_points*2;k++)
x[k] = random_within(0.,1.);
for (index_t k=0;k<nb_points*3;k++)
//float y = random_within(0.,1.);
x[k] = random_within(0.,1.); //we will substitute seeds for this code chunk
//x[k] = random_within(0.,1.);
nns->set_points( nb_points , x.data() );
index_t nb_neighbors = 50;
index_t nb_neighbors = 7; // since there are only seven points right now, this number will probably be more like 50 later
std::vector<index_t> neighbors(nb_neighbors);
std::vector<double> neighbors_sq_dist(nb_neighbors);
for (index_t k=0;k<nb_points;k++)
{
nns->get_nearest_neighbors( nb_neighbors , k , neighbors.data() , neighbors_sq_dist.data() );
//nns->get_nearest_neighbors( nb_neighbors , &seeds[k*3] , neighbors.data() , neighbors_sq_dist.data() , GEO::NearestNeighborSearch::KeepInitialValues() );
UT_ASSERT_EQUALS( neighbors[0] , k );
//print_inline(neighbors);
}
// std::vector<graphics::gl_float> seeds = {
// 0.0, 0.0 , 0.0,
// 1.0, 0.0 , 0.0,
......@@ -111,6 +136,14 @@ UT_TEST_CASE( test1 )
//bind the nearest neighbor information to the buffer
graphics::gl_index nn_buffer;
GL_CALL( glGenBuffers( 1 , &nn_buffer) );
GL_CALL( glBindBuffer( GL_ARRAY_BUFFER , nn_buffer ) );
GL_CALL( glBufferData( GL_ARRAY_BUFFER , sizeof(graphics::gl_float) * neighbors.size() , neighbors.data() , GL_STATIC_DRAW ) );
GL_CALL( glBindBuffer( GL_ARRAY_BUFFER , 0 ) );
shader.setUniform("u_ModelViewProjectionMatrix",mvp);
graphics::gl_index colormap_buffer;
......@@ -130,6 +163,7 @@ UT_TEST_CASE( test1 )
GL_CALL( glBindTexture( GL_TEXTURE_BUFFER , seeds_texture) );
GL_CALL( glTexBuffer( GL_TEXTURE_BUFFER , GL_RGB32F , points_buffer ) );
// generate a texture to hold the colormap
GLuint colormap_texture;
GL_CALL( glGenTextures( 1 , &colormap_texture) );
......@@ -137,7 +171,15 @@ UT_TEST_CASE( test1 )
GL_CALL( glBindTexture( GL_TEXTURE_BUFFER , colormap_texture) );
GL_CALL( glTexBuffer( GL_TEXTURE_BUFFER , GL_R32F , colormap_buffer ) );
//generate a texture to hold the nearest neighbors maybe here
// generate a texture to hold the nearest neighbor information
graphics::gl_index nn_texture;
GL_CALL( glGenTextures( 1, &nn_texture) );
GL_CALL( glActiveTexture( GL_TEXTURE0 + 2) );
GL_CALL( glBindTexture( GL_TEXTURE_BUFFER, nn_texture) );
GL_CALL( glTexBuffer( GL_TEXTURE_BUFFER, GL_R32I, nn_texture) );
......@@ -172,6 +214,10 @@ UT_TEST_CASE( test1 )
GLint colormap_location = glGetUniformLocation(shader.handle() , "colormap");
glUniform1i(colormap_location, 1);
//enable the nn search texture
glActiveTexture(GL_TEXTURE0 + 2);
GLint nn_location = glGetUniformLocation(shader.handle() , "nn");
glUniform1i(nn_location, 2);
// draw
GL_CALL( glBindBuffer(GL_ARRAY_BUFFER, points_buffer ) ); // it doesn't really matter which buffer we bind here since it isn't used
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment