Debian Codegraph @

src/thirdparty/SiftGPU/SiftMatchCU.cpp

raw
1 ////////////////////////////////////////////////////////////////////////////
2 // File: SiftMatchCU.cpp
3 // Author: Changchang Wu
4 // Description : implementation of the SiftMatchCU class.
5 // CUDA-based implementation of SiftMatch
6 //
7 // Copyright (c) 2007 University of North Carolina at Chapel Hill
8 // All Rights Reserved
9 //
10 // Permission to use, copy, modify and distribute this software and its
11 // documentation for educational, research and non-profit purposes, without
12 // fee, and without a written agreement is hereby granted, provided that
13 // the above copyright notice and the following paragraph appear in all
14 // copies.
15 //
16 // The University of North Carolina at Chapel Hill make no representations
17 // about the suitability of this software for any purpose. It is provided
18 // 'as is' without express or implied warranty.
19 //
20 // Please send BUG REPORTS to [email protected]
21 //
22 ////////////////////////////////////////////////////////////////////////////
23
24 #if defined(SIFTGPU_CUDA_ENABLED)
25
26 #include "GL/glew.h"
27 #include <algorithm>
28 #include <iostream>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <vector>
32 using namespace std;
33
34 #include <cuda_runtime.h>
35
36 #include "CuTexImage.h"
37 #include "GlobalUtil.h"
38 #include "ProgramCU.h"
39 #include "SiftGPU.h"
40 #include "SiftMatchCU.h"
41
42 #define MULT_TBLOCK_DIMX 128
43 #define MULT_TBLOCK_DIMY 1
44 #define MULT_BLOCK_DIMX (MULT_TBLOCK_DIMX)
45 #define MULT_BLOCK_DIMY (8 * MULT_TBLOCK_DIMY)
46
47 SiftMatchCU::SiftMatchCU(int max_sift) : SiftMatchGPU() {
48 _num_sift[0] = _num_sift[1] = 0;
49 _id_sift[0] = _id_sift[1] = 0;
50 _have_loc[0] = _have_loc[1] = 0;
51 _loc_ncomp[0] = _loc_ncomp[1] = 2;
52 __max_sift = max_sift <= 0 ? 4096 : ((max_sift + 31) / 32 * 32);
53 _initialized = 0;
54 }
55
56 bool SiftMatchCU::Allocate(int max_sift, int mbm) {
57 SetMaxSift(max_sift);
58
59 for (int index = 0; index < 2; ++index) {
60 if (!_texDes[index].InitTexture(8 * __max_sift, 1, 4) ||
61 !_texLoc[index].InitTexture(__max_sift, 1, 2)) {
62 return false;
63 }
64 }
65
66 if (!_texDot.InitTexture(__max_sift, __max_sift) ||
67 !_texMatch[0].InitTexture(__max_sift, 1)) {
68 return false;
69 }
70
71 if (mbm) {
72 const int cols = (__max_sift + MULT_BLOCK_DIMY - 1) / MULT_BLOCK_DIMY;
73 if (!_texCRT.InitTexture(__max_sift, cols, 32) ||
74 !_texMatch[1].InitTexture(__max_sift, 1)) {
75 return false;
76 }
77 }
78
79 _num_sift[0] = __max_sift;
80 _num_sift[1] = __max_sift;
81
82 return true;
83 }
84
85 void SiftMatchCU::SetMaxSift(int max_sift) {
86 max_sift = ((max_sift + 31) / 32) * 32;
87 __max_sift = max_sift;
88 }
89
90 int SiftMatchCU::CheckCudaDevice(int device) {
91 return ProgramCU::CheckCudaDevice(device);
92 }
93
94 void SiftMatchCU::InitSiftMatch() {
95 if (_initialized) return;
96 GlobalUtil::_GoodOpenGL = max(GlobalUtil::_GoodOpenGL, 1);
97 _initialized = 1;
98 }
99
100 void SiftMatchCU::SetDescriptors(int index, int num,
101 const unsigned char* descriptors, int id) {
102 if (_initialized == 0) return;
103 if (index > 1) index = 1;
104 if (index < 0) index = 0;
105 _have_loc[index] = 0;
106 // the same feature is already set
107 if (id != -1 && id == _id_sift[index]) return;
108 _id_sift[index] = id;
109 if (num > __max_sift) num = __max_sift;
110 _num_sift[index] = num;
111 _texDes[index].InitTexture(8 * num, 1, 4);
112 _texDes[index].CopyFromHost((void*)descriptors);
113 }
114
115 void SiftMatchCU::SetDescriptors(int index, int num, const float* descriptors,
116 int id) {
117 if (_initialized == 0) return;
118 if (index > 1) index = 1;
119 if (index < 0) index = 0;
120 if (num > __max_sift) num = __max_sift;
121
122 sift_buffer.resize(num * 128 / 4);
123 unsigned char* pub = (unsigned char*)&sift_buffer[0];
124 for (int i = 0; i < 128 * num; ++i) {
125 pub[i] = int(512 * descriptors[i] + 0.5);
126 }
127 SetDescriptors(index, num, pub, id);
128 }
129
130 void SiftMatchCU::SetFeatureLocation(int index, const float* locations,
131 int gap, int ncomp) {
132 if (_num_sift[index] <= 0) return;
133 if (ncomp < 2) ncomp = 2;
134 if (ncomp != 2 && ncomp != 3 && ncomp != 9) ncomp = 2;
135 if (ncomp == 9) {
136 // Bearing vector plus unprojection Jacobian, padded to 12 floats so that
137 // the kernel can fetch three float4 per feature. CUDA has no float3 or
138 // float9 channel descriptor.
139 _texLoc[index].InitTexture(_num_sift[index] * 3, 1, 4);
140 sift_buffer.resize(_num_sift[index] * 12);
141 float* pbuf = (float*)(&sift_buffer[0]);
142 for (int i = 0; i < _num_sift[index]; ++i) {
143 for (int k = 0; k < 9; ++k) {
144 pbuf[i * 12 + k] = *locations++;
145 }
146 pbuf[i * 12 + 9] = 0.0f;
147 pbuf[i * 12 + 10] = 0.0f;
148 pbuf[i * 12 + 11] = 0.0f;
149 locations += gap;
150 }
151 _texLoc[index].CopyFromHost(pbuf);
152 } else if (ncomp == 2) {
153 _texLoc[index].InitTexture(_num_sift[index], 1, 2);
154 if (gap == 0) {
155 _texLoc[index].CopyFromHost(locations);
156 } else {
157 sift_buffer.resize(_num_sift[index] * 2);
158 float* pbuf = (float*)(&sift_buffer[0]);
159 for (int i = 0; i < _num_sift[index]; ++i) {
160 pbuf[i * 2] = *locations++;
161 pbuf[i * 2 + 1] = *locations++;
162 locations += gap;
163 }
164 _texLoc[index].CopyFromHost(pbuf);
165 }
166 } else {
167 // CUDA has no float3 channel descriptor, so general homogeneous locations
168 // are padded to four floats per feature.
169 _texLoc[index].InitTexture(_num_sift[index], 1, 4);
170 sift_buffer.resize(_num_sift[index] * 4);
171 float* pbuf = (float*)(&sift_buffer[0]);
172 for (int i = 0; i < _num_sift[index]; ++i) {
173 pbuf[i * 4] = *locations++;
174 pbuf[i * 4 + 1] = *locations++;
175 pbuf[i * 4 + 2] = *locations++;
176 pbuf[i * 4 + 3] = 0.0f;
177 locations += gap;
178 }
179 _texLoc[index].CopyFromHost(pbuf);
180 }
181 _loc_ncomp[index] = ncomp;
182 _have_loc[index] = 1;
183 }
184
185 int SiftMatchCU::GetGuidedSiftMatch(int max_match, uint32_t match_buffer[][2],
186 float* H, float* F, float distmax,
187 float ratiomax, float hdistmax,
188 float fdistmax, int mbm, int use_h,
189 int use_f) {
190 if (_initialized == 0) return 0;
191 if (_num_sift[0] <= 0 || _num_sift[1] <= 0) return 0;
192 if (_have_loc[0] == 0 || _have_loc[1] == 0) return 0;
193 if (_loc_ncomp[0] == 9 && _loc_ncomp[1] == 9) {
194 // Bearing vectors with Jacobians: score the essential matrix in pixels.
195 // This layout carries no image plane points, so the homography branch is
196 // not available and F is interpreted as E.
197 ProgramCU::MultiplyDescriptorGRay(_texDes, _texDes + 1, _texLoc,
198 _texLoc + 1, &_texDot,
199 (mbm ? &_texCRT : NULL), F, fdistmax);
200 } else {
201 ProgramCU::MultiplyDescriptorG(_texDes, _texDes + 1, _texLoc, _texLoc + 1,
202 &_texDot, (mbm ? &_texCRT : NULL), H,
203 hdistmax, F, fdistmax, use_h, use_f);
204 }
205 return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
206 }
207
208 int SiftMatchCU::GetSiftMatch(int max_match, uint32_t match_buffer[][2],
209 float distmax, float ratiomax, int mbm) {
210 if (_initialized == 0) return 0;
211 if (_num_sift[0] <= 0 || _num_sift[1] <= 0) return 0;
212 ProgramCU::MultiplyDescriptor(_texDes, _texDes + 1, &_texDot,
213 (mbm ? &_texCRT : NULL));
214 return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
215 }
216
217 int SiftMatchCU::GetBestMatch(int max_match, uint32_t match_buffer[][2],
218 float distmax, float ratiomax, int mbm) {
219 sift_buffer.resize(_num_sift[0] + _num_sift[1]);
220 int *buffer1 = (int*)&sift_buffer[0],
221 *buffer2 = (int*)&sift_buffer[_num_sift[0]];
222 _texMatch[0].InitTexture(_num_sift[0], 1);
223 ProgramCU::GetRowMatch(&_texDot, _texMatch, distmax, ratiomax);
224 _texMatch[0].CopyToHost(buffer1);
225 if (mbm) {
226 _texMatch[1].InitTexture(_num_sift[1], 1);
227 ProgramCU::GetColMatch(&_texCRT, _texMatch + 1, distmax, ratiomax);
228 _texMatch[1].CopyToHost(buffer2);
229 }
230 int nmatch = 0, j;
231 for (int i = 0; i < _num_sift[0] && nmatch < max_match; ++i) {
232 j = int(buffer1[i]);
233 if (j >= 0 && (!mbm || int(buffer2[j]) == i)) {
234 match_buffer[nmatch][0] = i;
235 match_buffer[nmatch][1] = j;
236 nmatch++;
237 }
238 }
239
240 cudaError_t error = cudaGetLastError();
241 if (error != cudaSuccess) {
242 return -1;
243 }
244
245 return nmatch;
246 }
247
248 #endif