Updated FLANN to version 1.5

This commit is contained in:
Marius Muja
2010-10-12 19:47:50 +00:00
parent 3230073b9b
commit 16b1f61c83
60 changed files with 2134 additions and 3685 deletions

View File

@@ -1,48 +0,0 @@
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef COMMOM_H
#define COMMOM_H
#define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0]))
#include <stdexcept>
namespace cvflann
{
class FLANNException : public std::runtime_error {
public:
FLANNException(const char* message) : std::runtime_error(message) { }
};
}
#endif //COMMOM_H

View File

@@ -1,68 +0,0 @@
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef CONSTANTS_H
#define CONSTANTS_H
const double FLANN_VERSION = 1.20;
/* Nearest neighbor index algorithms */
enum flann_algorithm_t {
LINEAR = 0,
KDTREE = 1,
KMEANS = 2,
COMPOSITE = 3,
SAVED = 254,
AUTOTUNED = 255,
};
enum flann_centers_init_t {
CENTERS_RANDOM = 0,
CENTERS_GONZALES = 1,
CENTERS_KMEANSPP = 2
};
enum flann_log_level_t {
LOG_NONE = 0,
LOG_FATAL = 1,
LOG_ERROR = 2,
LOG_WARN = 3,
LOG_INFO = 4
};
enum flann_distance_t {
EUCLIDEAN = 1,
MANHATTAN = 2,
MINKOWSKI = 3
};
#endif // CONSTANTS_H

View File

@@ -1,278 +0,0 @@
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef FLANN_H
#define FLANN_H
#include "constants.h"
#ifdef WIN32
/* win32 dll export/import directives */
#ifdef flann_EXPORTS
#define LIBSPEC __declspec(dllexport)
#else
#define LIBSPEC __declspec(dllimport)
#endif
#else
/* unix needs nothing */
#define LIBSPEC
#endif
struct FLANNParameters {
flann_algorithm_t algorithm; // the algorithm to use (see constants.h)
int checks; // how many leafs (features) to check in one search
float cb_index; // cluster boundary index. Used when searching the kmeans tree
int trees; // number of randomized trees to use (for kdtree)
int branching; // branching factor (for kmeans tree)
int iterations; // max iterations to perform in one kmeans cluetering (kmeans tree)
flann_centers_init_t centers_init; // algorithm used for picking the initial cluetr centers for kmeans tree
float target_precision; // precision desired (used for autotuning, -1 otherwise)
float build_weight; // build tree time weighting factor
float memory_weight; // index memory weigthing factor
float sample_fraction; // what fraction of the dataset to use for autotuning
flann_log_level_t log_level; // determines the verbosity of each flann function
char* log_destination; // file where the output should go, NULL for the console
long random_seed; // random seed to use
};
typedef void* FLANN_INDEX; // deprecated
typedef void* flann_index_t;
#ifdef __cplusplus
extern "C" {
#endif
/**
Sets the log level used for all flann functions (unless
specified in FLANNParameters for each call
Params:
level = verbosity level (defined in constants.h)
*/
LIBSPEC void flann_log_verbosity(int level);
/**
* Sets the distance type to use throughout FLANN.
* If distance type specified is MINKOWSKI, the second argument
* specifies which order the minkowski distance should have.
*/
LIBSPEC void flann_set_distance_type(flann_distance_t distance_type, int order);
/**
Builds and returns an index. It uses autotuning if the target_precision field of index_params
is between 0 and 1, or the parameters specified if it's -1.
Params:
dataset = pointer to a data set stored in row major order
rows = number of rows (features) in the dataset
cols = number of columns in the dataset (feature dimensionality)
speedup = speedup over linear search, estimated if using autotuning, output parameter
index_params = index related parameters
flann_params = generic flann parameters
Returns: the newly created index or a number <0 for error
*/
LIBSPEC FLANN_INDEX flann_build_index(float* dataset,
int rows,
int cols,
float* speedup,
struct FLANNParameters* flann_params);
/**
* Saves the index to a file. Only the index is saved into the file, the dataset corresponding to the index is not saved.
*
* @param index_id The index that should be saved
* @param filename The filename the index should be saved to
* @return Returns 0 on success, negative value on error.
*/
LIBSPEC int flann_save_index(FLANN_INDEX index_id,
char* filename);
/**
* Loads an index from a file.
*
* @param filename File to load the index from.
* @param dataset The dataset corresponding to the index.
* @param rows Dataset tors
* @param cols Dataset columns
* @return
*/
LIBSPEC FLANN_INDEX flann_load_index(char* filename,
float* dataset,
int rows,
int cols);
/**
Builds an index and uses it to find nearest neighbors.
Params:
dataset = pointer to a data set stored in row major order
rows = number of rows (features) in the dataset
cols = number of columns in the dataset (feature dimensionality)
testset = pointer to a query set stored in row major order
trows = number of rows (features) in the query dataset (same dimensionality as features in the dataset)
indices = pointer to matrix for the indices of the nearest neighbors of the testset features in the dataset
(must have trows number of rows and nn number of columns)
nn = how many nearest neighbors to return
index_params = index related parameters
flann_params = generic flann parameters
Returns: zero or -1 for error
*/
LIBSPEC int flann_find_nearest_neighbors(float* dataset,
int rows,
int cols,
float* testset,
int trows,
int* indices,
float* dists,
int nn,
struct FLANNParameters* flann_params);
/**
Searches for nearest neighbors using the index provided
Params:
index_id = the index (constructed previously using flann_build_index).
testset = pointer to a query set stored in row major order
trows = number of rows (features) in the query dataset (same dimensionality as features in the dataset)
indices = pointer to matrix for the indices of the nearest neighbors of the testset features in the dataset
(must have trows number of rows and nn number of columns)
nn = how many nearest neighbors to return
checks = number of checks to perform before the search is stopped
flann_params = generic flann parameters
Returns: zero or a number <0 for error
*/
LIBSPEC int flann_find_nearest_neighbors_index(FLANN_INDEX index_id,
float* testset,
int trows,
int* indices,
float* dists,
int nn,
int checks,
struct FLANNParameters* flann_params);
/**
* Performs an radius search using an already constructed index.
*
* In case of radius search, instead of always returning a predetermined
* number of nearest neighbours (for example the 10 nearest neighbours), the
* search will return all the neighbours found within a search radius
* of the query point.
*
* The check parameter in the function below sets the level of approximation
* for the search by only visiting "checks" number of features in the index
* (the same way as for the KNN search). A lower value for checks will give
* a higher search speedup at the cost of potentially not returning all the
* neighbours in the specified radius.
*/
LIBSPEC int flann_radius_search(FLANN_INDEX index_ptr, /* the index */
float* query, /* query point */
int* indices, /* array for storing the indices found (will be modified) */
float* dists, /* similar, but for storing distances */
int max_nn, /* size of arrays indices and dists */
float radius, /* search radius (squared radius for euclidian metric) */
int checks, /* number of features to check, sets the level of approximation */
FLANNParameters* flann_params);
/**
Deletes an index and releases the memory used by it.
Params:
index_id = the index (constructed previously using flann_build_index).
flann_params = generic flann parameters
Returns: zero or a number <0 for error
*/
LIBSPEC int flann_free_index(FLANN_INDEX index_id,
struct FLANNParameters* flann_params);
/**
Clusters the features in the dataset using a hierarchical kmeans clustering approach.
This is significantly faster than using a flat kmeans clustering for a large number
of clusters.
Params:
dataset = pointer to a data set stored in row major order
rows = number of rows (features) in the dataset
cols = number of columns in the dataset (feature dimensionality)
clusters = number of cluster to compute
result = memory buffer where the output cluster centers are storred
index_params = used to specify the kmeans tree parameters (branching factor, max number of iterations to use)
flann_params = generic flann parameters
Returns: number of clusters computed or a number <0 for error. This number can be different than the number of clusters requested, due to the
way hierarchical clusters are computed. The number of clusters returned will be the highest number of the form
(branch_size-1)*K+1 smaller than the number of clusters requested.
*/
LIBSPEC int flann_compute_cluster_centers(float* dataset,
int rows,
int cols,
int clusters,
float* result,
struct FLANNParameters* flann_params);
#ifdef __cplusplus
};
#include "flann.hpp"
#endif
#endif /*FLANN_H*/

View File

@@ -1,246 +0,0 @@
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef FLANN_HPP_
#define FLANN_HPP_
#include <vector>
#include <string>
#include "constants.h"
#include "common.h"
#include "matrix.h"
#include "flann.h"
namespace cvflann
{
class NNIndex;
class IndexFactory
{
public:
virtual ~IndexFactory() {}
virtual NNIndex* createIndex(const Matrix<float>& dataset) const = 0;
};
struct IndexParams : public IndexFactory {
protected:
IndexParams() {};
public:
static IndexParams* createFromParameters(const FLANNParameters& p);
void fromParameters(const FLANNParameters&) {};
void toParameters(FLANNParameters&) { };
};
struct LinearIndexParams : public IndexParams {
LinearIndexParams() {};
NNIndex* createIndex(const Matrix<float>& dataset) const;
};
struct KDTreeIndexParams : public IndexParams {
KDTreeIndexParams(int trees_ = 4) : trees(trees_) {};
int trees; // number of randomized trees to use (for kdtree)
NNIndex* createIndex(const Matrix<float>& dataset) const;
void fromParameters(const FLANNParameters& p)
{
trees = p.trees;
}
void toParameters(FLANNParameters& p)
{
p.algorithm = KDTREE;
p.trees = trees;
};
};
struct KMeansIndexParams : public IndexParams {
KMeansIndexParams(int branching_ = 32, int iterations_ = 11,
flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
branching(branching_),
iterations(iterations_),
centers_init(centers_init_),
cb_index(cb_index_) {};
int branching; // branching factor (for kmeans tree)
int iterations; // max iterations to perform in one kmeans clustering (kmeans tree)
flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree
float cb_index; // cluster boundary index. Used when searching the kmeans tree
NNIndex* createIndex(const Matrix<float>& dataset) const;
void fromParameters(const FLANNParameters& p)
{
branching = p.branching;
iterations = p.iterations;
centers_init = p.centers_init;
cb_index = p.cb_index;
}
void toParameters(FLANNParameters& p)
{
p.algorithm = KMEANS;
p.branching = branching;
p.iterations = iterations;
p.centers_init = centers_init;
p.cb_index = cb_index;
};
};
struct CompositeIndexParams : public IndexParams {
CompositeIndexParams(int trees_ = 4, int branching_ = 32, int iterations_ = 11,
flann_centers_init_t centers_init_ = CENTERS_RANDOM, float cb_index_ = 0.2 ) :
trees(trees_),
branching(branching_),
iterations(iterations_),
centers_init(centers_init_),
cb_index(cb_index_) {};
int trees; // number of randomized trees to use (for kdtree)
int branching; // branching factor (for kmeans tree)
int iterations; // max iterations to perform in one kmeans clustering (kmeans tree)
flann_centers_init_t centers_init; // algorithm used for picking the initial cluster centers for kmeans tree
float cb_index; // cluster boundary index. Used when searching the kmeans tree
NNIndex* createIndex(const Matrix<float>& dataset) const;
void fromParameters(const FLANNParameters& p)
{
trees = p.trees;
branching = p.branching;
iterations = p.iterations;
centers_init = p.centers_init;
cb_index = p.cb_index;
}
void toParameters(FLANNParameters& p)
{
p.algorithm = COMPOSITE;
p.trees = trees;
p.branching = branching;
p.iterations = iterations;
p.centers_init = centers_init;
p.cb_index = cb_index;
};
};
struct AutotunedIndexParams : public IndexParams {
AutotunedIndexParams( float target_precision_ = 0.9, float build_weight_ = 0.01,
float memory_weight_ = 0, float sample_fraction_ = 0.1) :
target_precision(target_precision_),
build_weight(build_weight_),
memory_weight(memory_weight_),
sample_fraction(sample_fraction_) {};
float target_precision; // precision desired (used for autotuning, -1 otherwise)
float build_weight; // build tree time weighting factor
float memory_weight; // index memory weighting factor
float sample_fraction; // what fraction of the dataset to use for autotuning
NNIndex* createIndex(const Matrix<float>& dataset) const;
void fromParameters(const FLANNParameters& p)
{
target_precision = p.target_precision;
build_weight = p.build_weight;
memory_weight = p.memory_weight;
sample_fraction = p.sample_fraction;
}
void toParameters(FLANNParameters& p)
{
p.algorithm = AUTOTUNED;
p.target_precision = target_precision;
p.build_weight = build_weight;
p.memory_weight = memory_weight;
p.sample_fraction = sample_fraction;
};
};
struct SavedIndexParams : public IndexParams {
SavedIndexParams() {
throw FLANNException("I don't know which index to load");
}
SavedIndexParams(std::string filename_) : filename(filename_) {}
std::string filename; // filename of the stored index
NNIndex* createIndex(const Matrix<float>& dataset) const;
};
struct SearchParams {
SearchParams(int checks_ = 32) :
checks(checks_) {};
int checks;
};
class Index {
NNIndex* nnIndex;
public:
Index(const Matrix<float>& features, const IndexParams& params);
~Index();
void knnSearch(const Matrix<float>& queries, Matrix<int>& indices, Matrix<float>& dists, int knn, const SearchParams& params);
int radiusSearch(const Matrix<float>& query, Matrix<int> indices, Matrix<float> dists, float radius, const SearchParams& params);
void save(std::string filename);
int veclen() const;
int size() const;
};
int hierarchicalClustering(const Matrix<float>& features, Matrix<float>& centers, const KMeansIndexParams& params);
}
#endif /* FLANN_HPP_ */

View File

@@ -1,163 +0,0 @@
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef DATASET_H
#define DATASET_H
#include <stdio.h>
#include "random.h"
namespace cvflann
{
/**
* Class implementing a generic rectangular dataset.
*/
template <typename T>
class Matrix {
/**
* Flag showing if the class owns its data storage.
*/
bool ownData;
void shallow_copy(const Matrix& rhs)
{
data = rhs.data;
rows = rhs.rows;
cols = rhs.cols;
ownData = false;
}
public:
long rows;
long cols;
T* data;
Matrix(long rows_, long cols_, T* data_ = NULL) :
ownData(false), rows(rows_), cols(cols_), data(data_)
{
if (data_==NULL) {
data = new T[rows*cols];
ownData = true;
}
}
Matrix(const Matrix& d)
{
shallow_copy(d);
}
const Matrix& operator=(const Matrix& rhs)
{
if (this!=&rhs) {
shallow_copy(rhs);
}
return *this;
}
~Matrix()
{
if (ownData) {
delete[] data;
}
}
/**
* Operator that return a (pointer to a) row of the data.
*/
T* operator[](long index)
{
return data+index*cols;
}
T* operator[](long index) const
{
return data+index*cols;
}
Matrix<T>* sample(long size, bool remove = false)
{
UniqueRandom rand(rows);
Matrix<T> *newSet = new Matrix<T>(size,cols);
T *src,*dest;
for (long i=0;i<size;++i) {
long r = rand.next();
dest = (*newSet)[i];
src = (*this)[r];
for (long j=0;j<cols;++j) {
dest[j] = src[j];
}
if (remove) {
dest = (*this)[rows-i-1];
src = (*this)[r];
for (long j=0;j<cols;++j) {
swap(*src,*dest);
src++;
dest++;
}
}
}
if (remove) {
rows -= size;
}
return newSet;
}
Matrix<T>* sample(long size) const
{
UniqueRandom rand(rows);
Matrix<T> *newSet = new Matrix<T>(size,cols);
T *src,*dest;
for (long i=0;i<size;++i) {
long r = rand.next();
dest = (*newSet)[i];
src = (*this)[r];
for (long j=0;j<cols;++j) {
dest[j] = src[j];
}
}
return newSet;
}
};
}
#endif //DATASET_H

View File

@@ -1,134 +0,0 @@
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef RANDOM_H
#define RANDOM_H
#include <algorithm>
#include <cstdlib>
#include <cassert>
using namespace std;
namespace cvflann
{
/**
* Seeds the random number generator
*/
void seed_random(unsigned int seed);
/*
* Generates a random double value.
*/
double rand_double(double high = 1.0, double low=0);
/*
* Generates a random integer value.
*/
int rand_int(int high = RAND_MAX, int low = 0);
/**
* Random number generator that returns a distinct number from
* the [0,n) interval each time.
*
* TODO: improve on this to use a generator function instead of an
* array of randomly permuted numbers
*/
class UniqueRandom
{
int* vals;
int size;
int counter;
public:
/**
* Constructor.
* Params:
* n = the size of the interval from which to generate
* random numbers.
*/
UniqueRandom(int n) : vals(NULL) {
init(n);
}
~UniqueRandom()
{
delete[] vals;
}
/**
* Initializes the number generator.
* Params:
* n = the size of the interval from which to generate
* random numbers.
*/
void init(int n)
{
// create and initialize an array of size n
if (vals == NULL || n!=size) {
delete[] vals;
size = n;
vals = new int[size];
}
for(int i=0;i<size;++i) {
vals[i] = i;
}
// shuffle the elements in the array
// Fisher-Yates shuffle
for (int i=size;i>0;--i) {
// int rand = cast(int) (drand48() * n);
int rnd = rand_int(i);
assert(rnd >=0 && rnd < i);
swap(vals[i-1], vals[rnd]);
}
counter = 0;
}
/**
* Return a distinct random integer in greater or equal to 0 and less
* than 'n' on each call. It should be called maximum 'n' times.
* Returns: a random integer
*/
int next() {
if (counter==size) {
return -1;
} else {
return vals[counter++];
}
}
};
}
#endif //RANDOM_H