00001 /*********************************************************************** 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. 00005 * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * 1. Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 *************************************************************************/ 00028 00029 00030 #ifndef RTABMAP_FLANN_SAMPLING_H_ 00031 #define RTABMAP_FLANN_SAMPLING_H_ 00032 00033 #include "rtflann/util/matrix.h" 00034 #include "rtflann/util/random.h" 00035 00036 namespace rtflann 00037 { 00038 00039 template<typename T> 00040 Matrix<T> random_sample(Matrix<T>& srcMatrix, size_t size, bool remove = false) 00041 { 00042 UniqueRandom rand_unique(srcMatrix.rows); 00043 Matrix<T> newSet(new T[size * srcMatrix.cols], size,srcMatrix.cols); 00044 00045 T* src,* dest; 00046 for (size_t i=0; i<size; ++i) { 00047 size_t r; 00048 if (remove) { 00049 r = static_cast<size_t>(rand_int(srcMatrix.rows-i)); 00050 } 00051 else { 00052 r = static_cast<size_t>(rand_unique.next()); 00053 } 00054 dest = newSet[i]; 00055 src = srcMatrix[r]; 00056 std::copy(src, src+srcMatrix.cols, dest); 00057 if (remove) { 00058 src = srcMatrix[srcMatrix.rows-i-1]; 00059 dest = srcMatrix[r]; 00060 std::copy(src, src+srcMatrix.cols, dest); 00061 } 00062 } 00063 if (remove) { 00064 srcMatrix.rows -= size; 00065 } 00066 return newSet; 00067 } 00068 00069 } // namespace 00070 00071 00072 #endif /* FLANN_SAMPLING_H_ */