1 | /*********************************************************************** |
---|
2 | * Software License Agreement (BSD License) |
---|
3 | * |
---|
4 | * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. |
---|
5 | * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. |
---|
6 | * |
---|
7 | * THE BSD LICENSE |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions |
---|
11 | * are met: |
---|
12 | * |
---|
13 | * 1. Redistributions of source code must retain the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer. |
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer in the |
---|
17 | * documentation and/or other materials provided with the distribution. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
29 | *************************************************************************/ |
---|
30 | |
---|
31 | #ifndef FLANN_DATASET_H_ |
---|
32 | #define FLANN_DATASET_H_ |
---|
33 | |
---|
34 | #include "flann/general.h" |
---|
35 | #include <stdio.h> |
---|
36 | |
---|
37 | namespace flann |
---|
38 | { |
---|
39 | |
---|
40 | typedef unsigned char uchar; |
---|
41 | |
---|
42 | class Matrix_ |
---|
43 | { |
---|
44 | public: |
---|
45 | |
---|
46 | Matrix_() : rows(0), cols(0), stride(0), data(NULL) |
---|
47 | { |
---|
48 | }; |
---|
49 | |
---|
50 | Matrix_(void* data_, size_t rows_, size_t cols_, flann_datatype_t type, size_t stride_ = 0) : |
---|
51 | rows(rows_), cols(cols_), stride(stride_) |
---|
52 | { |
---|
53 | data = static_cast<uchar*>(data_); |
---|
54 | if (stride==0) stride = flann_datatype_size(type)*cols; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Operator that returns a (pointer to a) row of the data. |
---|
59 | */ |
---|
60 | inline void* operator[](size_t index) const |
---|
61 | { |
---|
62 | return data+index*stride; |
---|
63 | } |
---|
64 | |
---|
65 | void* ptr() const |
---|
66 | { |
---|
67 | return data; |
---|
68 | } |
---|
69 | |
---|
70 | size_t rows; |
---|
71 | size_t cols; |
---|
72 | size_t stride; |
---|
73 | flann_datatype_t type; |
---|
74 | protected: |
---|
75 | uchar* data; |
---|
76 | |
---|
77 | }; |
---|
78 | |
---|
79 | |
---|
80 | /** |
---|
81 | * Class that implements a simple rectangular matrix stored in a memory buffer and |
---|
82 | * provides convenient matrix-like access using the [] operators. |
---|
83 | * |
---|
84 | * This class has the same memory structure as the un-templated class flann::Matrix_ and |
---|
85 | * it's directly convertible from it. |
---|
86 | */ |
---|
87 | template <typename T> |
---|
88 | class Matrix : public Matrix_ |
---|
89 | { |
---|
90 | public: |
---|
91 | typedef T type; |
---|
92 | |
---|
93 | Matrix() : Matrix_() |
---|
94 | { |
---|
95 | } |
---|
96 | |
---|
97 | Matrix(T* data_, size_t rows_, size_t cols_, size_t stride_ = 0) : |
---|
98 | Matrix_(data_, rows_, cols_, flann_datatype<T>::value, stride_) |
---|
99 | { |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Convenience function for deallocating the storage data. |
---|
104 | */ |
---|
105 | FLANN_DEPRECATED void free() |
---|
106 | { |
---|
107 | fprintf(stderr, "The flann::Matrix<T>::free() method is deprecated " |
---|
108 | "and it does not do any memory deallocation any more. You are" |
---|
109 | "responsible for deallocating the matrix memory (by doing" |
---|
110 | "'delete[] matrix.ptr()' for example)"); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Operator that returns a (pointer to a) row of the data. |
---|
115 | */ |
---|
116 | inline T* operator[](size_t index) const |
---|
117 | { |
---|
118 | return reinterpret_cast<T*>(static_cast<uchar*>(Matrix_::data)+index*stride); |
---|
119 | // return (T*)(Matrix_::operator [](index)); |
---|
120 | } |
---|
121 | |
---|
122 | T* ptr() const |
---|
123 | { |
---|
124 | return reinterpret_cast<T*>(Matrix_::data); |
---|
125 | } |
---|
126 | }; |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | #endif //FLANN_DATASET_H_ |
---|