示例#1
0
文件: flann.cpp 项目: brics/brics_3d
EXPORTED int flann_find_nearest_neighbors_index(FLANN_INDEX index_ptr, float* testset, int tcount, int* result, float* dists, int nn, int checks, FLANNParameters* flann_params)
{
	try {
		init_flann_parameters(flann_params);

        if (index_ptr==NULL) {
            throw FLANNException("Invalid index");
        }
        NNIndexPtr index = NNIndexPtr(index_ptr);

        int length = index->veclen();
        StartStopTimer t;
        t.start();
        Params searchParams;
        searchParams["checks"] = checks;
        Dataset<int> result_set(tcount, nn, result);
        Dataset<float> dists_set(tcount, nn, dists);
        search_for_neighbors(*index, Dataset<float>(tcount, length, testset), result_set, dists_set, searchParams);
        t.stop();
        logger.info("Searching took %g seconds\n",t.value);

		return 0;
	}
	catch(runtime_error& e) {
		logger.error("Caught exception: %s\n",e.what());
		return -1;
	}

}
示例#2
0
文件: flann.cpp 项目: brics/brics_3d
EXPORTED FLANN_INDEX flann_build_index(float* dataset, int rows, int cols, float* speedup, FLANNParameters* flann_params)
{
	try {
		if (flann_params == NULL) {
			throw FLANNException("The index_params agument must be non-null");
		}
		init_flann_parameters(flann_params);

		DatasetPtr inputData = new Dataset<float>(rows,cols,dataset);
		float target_precision = flann_params->target_precision;

		NNIndex* index = NULL;
		if (flann_params->target_precision < 0) {
			Params params = parametersToParams(*flann_params);
			logger.info("Building index\n");
			index = create_index((flann_algorithm_t)(int)params["algorithm"],*inputData,params);
            StartStopTimer t;
            t.start();
            index->buildIndex();
            t.stop();
            logger.info("Building index took: %g\n",t.value);
		}
		else {
            if (flann_params->build_weight < 0) {
                throw FLANNException("The index_params.build_weight must be positive.");
            }

            if (flann_params->memory_weight < 0) {
                throw FLANNException("The index_params.memory_weight must be positive.");
            }
            Autotune autotuner(flann_params->build_weight, flann_params->memory_weight, flann_params->sample_fraction);
			Params params = autotuner.estimateBuildIndexParams(*inputData, target_precision);
			index = create_index((flann_algorithm_t)(int)params["algorithm"],*inputData,params);
			index->buildIndex();
			autotuner.estimateSearchParams(*index,*inputData,target_precision,params);
			paramsToParameters(params, flann_params);

			if (speedup != NULL) {
				*speedup = float(params["speedup"]);
			}
		}

		return index;
	}
	catch (runtime_error& e) {
		logger.error("Caught exception: %s\n",e.what());
		return NULL;
	}
}
示例#3
0
文件: flann.cpp 项目: brics/brics_3d
EXPORTED int flann_find_nearest_neighbors(float* dataset,  int rows, int cols, float* testset, int tcount, int* result, float* dists, int nn, FLANNParameters* flann_params)
{
	try {
		init_flann_parameters(flann_params);

        DatasetPtr inputData = new Dataset<float>(rows,cols,dataset);
		float target_precision = flann_params->target_precision;

        StartStopTimer t;
		NNIndexPtr index;
		if (target_precision < 0) {
			Params params = parametersToParams(*flann_params);
			logger.info("Building index\n");
            index = create_index((flann_algorithm_t)(int)params["algorithm"],*inputData,params);
            t.start();
 			index->buildIndex();
            t.stop();
            logger.info("Building index took: %g\n",t.value);
		}
		else {
            logger.info("Build index: %g\n", flann_params->build_weight);
            Autotune autotuner(flann_params->build_weight, flann_params->memory_weight, flann_params->sample_fraction);
            Params params = autotuner.estimateBuildIndexParams(*inputData, target_precision);
            index = create_index((flann_algorithm_t)(int)params["algorithm"],*inputData,params);
            index->buildIndex();
            autotuner.estimateSearchParams(*index,*inputData,target_precision,params);
			paramsToParameters(params, flann_params);
		}
		logger.info("Finished creating the index.\n");

		logger.info("Searching for nearest neighbors.\n");
        Params searchParams;
        searchParams["checks"] = flann_params->checks;
        Dataset<int> result_set(tcount, nn, result);
        Dataset<float> dists_set(tcount, nn, dists);
        search_for_neighbors(*index, Dataset<float>(tcount, cols, testset), result_set, dists_set, searchParams);

		delete index;
		delete inputData;

		return 0;
	}
	catch(runtime_error& e) {
		logger.error("Caught exception: %s\n",e.what());
		return -1;
	}
}
示例#4
0
float search_with_ground_truth(NNIndex& index, const Matrix<float>& inputData, const Matrix<float>& testData, const Matrix<int>& matches, int nn, int checks, float& time, float& dist, int skipMatches)
{
    if (matches.cols<nn) {
        logger.info("matches.cols=%d, nn=%d\n",matches.cols,nn);

        throw FLANNException("Ground truth is not computed for as many neighbors as requested");
    }

    KNNResultSet resultSet(nn+skipMatches);
    SearchParams searchParams(checks);

    int correct = 0;
    float distR = 0;
    StartStopTimer t;
    int repeats = 0;
    while (t.value<0.2) {
        repeats++;
        t.start();
        correct = 0;
        distR = 0;
        for (int i = 0; i < testData.rows; i++) {
            float* target = testData[i];
            resultSet.init(target, testData.cols);
            index.findNeighbors(resultSet,target, searchParams);
            int* neighbors = resultSet.getNeighbors();
            neighbors = neighbors+skipMatches;

            correct += countCorrectMatches(neighbors,matches[i], nn);
            distR += computeDistanceRaport(inputData, target,neighbors,matches[i], testData.cols, nn);
        }
        t.stop();
    }
    time = (float)(t.value/repeats);


    float precicion = (float)correct/(nn*testData.rows);

    dist = distR/(testData.rows*nn);

    logger.info("%8d %10.4g %10.5g %10.5g %10.5g\n",
            checks, precicion, time, 1000.0 * time / testData.rows, dist);

    return precicion;
}
示例#5
0
    DemoScene( unsigned int windowWidth,
               unsigned int windowHeight,
               bool         fullscreen,
               unsigned int multisample ) :
        time(0.0f),
        fps(0.0f),
        dt(0.0f),
        drawHelp(true),
        needToTakeScreenshot(false)
    {
        Engine* engine = Engine::Instance();

        // setup graphics
        {
            using namespace slon::scene;
            using namespace slon::animation;

            // setup logging
            {
                log::currentLogManager().redirectOutput("database", "database_log.txt");
                log::currentLogManager().redirectOutput("graphics", "graphics_log.txt");
            }

            GraphicsManager& graphicsManager = engine->getGraphicsManager();
            graphicsManager.setVideoMode(windowWidth, windowHeight, 32, fullscreen, false, multisample);

            // create renderer
            {
                ForwardRendererDesc desc;
                desc.useDepthPass   = true;
                desc.makeDepthMap   = true;
                desc.useDebugRender = true;

                Renderer* renderer = graphicsManager.initRenderer(desc);
                renderer->connectPostRenderCallback( boost::bind(&DemoScene::OnPostRender, this, _1) );
            }
        #ifdef WIN32
            FreeConsole();
        #endif

            // create world
            realm::World*       world = realm::currentWorld();
            realm::location_ptr location(new realm::BVHLocation);
            world->addLocation(location);

            // Create skybox
            SkyBox* skyBox = new SkyBox();
            {
                const std::string SKY_BOX_MAPS[6] =
                {
                    "Data/SkyBox/sunset_west.jpg",
                    "Data/SkyBox/sunset_east.jpg",
                    "Data/SkyBox/sunset_up.jpg",
                    "Data/SkyBox/sunset_down.jpg",
                    "Data/SkyBox/sunset_south.jpg",
                    "Data/SkyBox/sunset_north.jpg"
                };
                skyBox->MakeFromSideTextures(SKY_BOX_MAPS);
            }
            world->addInfiniteNode(skyBox);

            // create scene
            {
                database::library_ptr library = database::loadLibrary("Data/Models/troll.DAE");
                location->add(library->visualScenes.begin()->second.get());

				database::Library::key_animation_map animations = library->animations;
				if ( !animations.empty() )
				{
					StartStopTimer* timer = new StartStopTimer();
					timer->start();
					animations.begin()->second->play(timer);
				}

                // create light
                scene::DirectionalLight* light = new DirectionalLight();
                light->setDirection( normalize( Vector3f(-1.5f, -0.1f, 0.85f) ) );
                light->setColor( Vector4f(0.8f, 0.8f, 0.8f, 1.0f) );
                light->setAmbient(0.3f);
                light->setIntensity(1.5f);
                world->addInfiniteNode(light);
            }

            // Create camera
            {
                sgl::rectangle viewport(0, 0, windowWidth, windowHeight);
                camera.reset( createMainCamera(viewport) );
                graphicsManager.addCamera( camera.get() );
            }

            camera->setPosition( Vector3f(60.0f, 20.0f, 0.0f) );
            camera->setDirection( Vector3f(-1.0f, -1.0f, 1.0f) );
            camera->setUp( Vector3f(0.0f, 1.0f, 0.0f) );

            // create font
            font.reset( currentDevice()->CreateFont() );
            {
                sgl::Image* image = currentDevice()->CreateImage();
                image->LoadFromFile("Data/Fonts/font.png");
	            font->SetTexture( image->CreateTexture2D() );
            }
        }

        // setup input
        {
            using boost::bind;
            using namespace slon::input;
            using namespace slon::thread;

            InputManager& inputManager = engine->getInputManager();
            inputManager.showCursor(false);

            // fix cursor in the window center
            Vector2ui cursorPosition = screenSize / 2 ;
            inputManager.setCursorPosition(cursorPosition.x, cursorPosition.y);
            inputManager.fixCursorPosition(cursorPosition.x, cursorPosition.y);

            keyboardHandler.reset( new KeyboardHandler() );
            inputManager.addInputHandler( keyboardHandler.get() );

            keyboardHandler->connectKeyPressEventHandler( input::KEY_F1,     bind(&DemoScene::toggleHelpText,  this) );
            keyboardHandler->connectKeyPressEventHandler( input::KEY_F2,     bind(&DemoScene::toggleWireframe, this) );
            keyboardHandler->connectKeyPressEventHandler( input::KEY_F9,     bind(&DemoScene::takeScreenShot,  this) );
            keyboardHandler->connectKeyPressEventHandler( input::KEY_ESCAPE, bind(&DemoScene::stopDemo,        this) );

            keyboardHandler->connectKeyDownHandler( input::KEY_w,   bind(&DemoScene::flyCamera, this, math::Vector3f(  0.0f,   0.0f,  10.0f)) );
            keyboardHandler->connectKeyDownHandler( input::KEY_s,   bind(&DemoScene::flyCamera, this, math::Vector3f(  0.0f,   0.0f, -10.0f)) );
            keyboardHandler->connectKeyDownHandler( input::KEY_d,   bind(&DemoScene::flyCamera, this, math::Vector3f( 10.0f,   0.0f,   0.0f)) );
            keyboardHandler->connectKeyDownHandler( input::KEY_a,   bind(&DemoScene::flyCamera, this, math::Vector3f(-10.0f,   0.0f,   0.0f)) );
            keyboardHandler->connectKeyDownHandler( input::KEY_q,   bind(&DemoScene::flyCamera, this, math::Vector3f(  0.0f,  10.0f,   0.0f)) );
            keyboardHandler->connectKeyDownHandler( input::KEY_e,   bind(&DemoScene::flyCamera, this, math::Vector3f(  0.0f, -10.0f,   0.0f)) );

            // setup mouse input
            MouseHandler* mouseHandler = new MouseHandler();
            inputManager.addInputHandler(mouseHandler);

            mouseHandler->connectRelativeMouseMotionEventHandler( bind(&DemoScene::turnCamera, this, _1, _2) );
        }

        Engine::DESC desc;
        desc.multithreaded = false;
        desc.grabInput     = true;
        engine->run(desc);
    }