ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
SiftMatchCU.cpp
Go to the documentation of this file.
1 // 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 ccwu@cs.unc.edu
21 //
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  __max_sift = max_sift <= 0 ? 4096 : ((max_sift + 31) / 32 * 32);
52  _initialized = 0;
53 }
54 
55 bool SiftMatchCU::Allocate(int max_sift, int mbm) {
56  SetMaxSift(max_sift);
57 
58  for (int index = 0; index < 2; ++index) {
59  if (!_texDes[index].InitTexture(8 * __max_sift, 1, 4) ||
60  !_texLoc[index].InitTexture(__max_sift, 1, 2)) {
61  return false;
62  }
63  }
64 
65  if (!_texDot.InitTexture(__max_sift, __max_sift) ||
66  !_texMatch[0].InitTexture(__max_sift, 1)) {
67  return false;
68  }
69 
70  if (mbm) {
71  const int cols = (__max_sift + MULT_BLOCK_DIMY - 1) / MULT_BLOCK_DIMY;
72  if (!_texCRT.InitTexture(__max_sift, cols, 32) ||
73  !_texMatch[1].InitTexture(__max_sift, 1)) {
74  return false;
75  }
76  }
77 
78  _num_sift[0] = __max_sift;
79  _num_sift[1] = __max_sift;
80 
81  return true;
82 }
83 
84 void SiftMatchCU::SetMaxSift(int max_sift) {
85  max_sift = ((max_sift + 31) / 32) * 32;
86  __max_sift = max_sift;
87 }
88 
89 int SiftMatchCU::CheckCudaDevice(int device) {
90  return ProgramCU::CheckCudaDevice(device);
91 }
92 
93 void SiftMatchCU::InitSiftMatch() {
94  if (_initialized) return;
96  _initialized = 1;
97 }
98 
99 void SiftMatchCU::SetDescriptors(int index, int num,
100  const unsigned char* descriptors, int id) {
101  if (_initialized == 0) return;
102  if (index > 1) index = 1;
103  if (index < 0) index = 0;
104  _have_loc[index] = 0;
105  // the same feature is already set
106  if (id != -1 && id == _id_sift[index]) return;
107  _id_sift[index] = id;
108  if (num > __max_sift) num = __max_sift;
109  _num_sift[index] = num;
110  _texDes[index].InitTexture(8 * num, 1, 4);
111  _texDes[index].CopyFromHost((void*)descriptors);
112 }
113 
114 void SiftMatchCU::SetDescriptors(int index, int num, const float* descriptors,
115  int id) {
116  if (_initialized == 0) return;
117  if (index > 1) index = 1;
118  if (index < 0) index = 0;
119  if (num > __max_sift) num = __max_sift;
120 
121  sift_buffer.resize(num * 128 / 4);
122  unsigned char* pub = (unsigned char*)&sift_buffer[0];
123  for (int i = 0; i < 128 * num; ++i) {
124  pub[i] = int(512 * descriptors[i] + 0.5);
125  }
126  SetDescriptors(index, num, pub, id);
127 }
128 
129 void SiftMatchCU::SetFeautreLocation(int index, const float* locations,
130  int gap) {
131  if (_num_sift[index] <= 0) return;
132  _texLoc[index].InitTexture(_num_sift[index], 1, 2);
133  if (gap == 0) {
134  _texLoc[index].CopyFromHost(locations);
135  } else {
136  sift_buffer.resize(_num_sift[index] * 2);
137  float* pbuf = (float*)(&sift_buffer[0]);
138  for (int i = 0; i < _num_sift[index]; ++i) {
139  pbuf[i * 2] = *locations++;
140  pbuf[i * 2 + 1] = *locations++;
141  locations += gap;
142  }
143  _texLoc[index].CopyFromHost(pbuf);
144  }
145  _have_loc[index] = 1;
146 }
147 
148 int SiftMatchCU::GetGuidedSiftMatch(int max_match, uint32_t match_buffer[][2],
149  float* H, float* F, float distmax,
150  float ratiomax, float hdistmax,
151  float fdistmax, int mbm) {
152  if (_initialized == 0) return 0;
153  if (_num_sift[0] <= 0 || _num_sift[1] <= 0) return 0;
154  if (_have_loc[0] == 0 || _have_loc[1] == 0) return 0;
155  ProgramCU::MultiplyDescriptorG(_texDes, _texDes + 1, _texLoc, _texLoc + 1,
156  &_texDot, (mbm ? &_texCRT : NULL), H, hdistmax,
157  F, fdistmax);
158  return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
159 }
160 
161 int SiftMatchCU::GetSiftMatch(int max_match, uint32_t match_buffer[][2],
162  float distmax, float ratiomax, int mbm) {
163  if (_initialized == 0) return 0;
164  if (_num_sift[0] <= 0 || _num_sift[1] <= 0) return 0;
165  ProgramCU::MultiplyDescriptor(_texDes, _texDes + 1, &_texDot,
166  (mbm ? &_texCRT : NULL));
167  return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
168 }
169 
170 int SiftMatchCU::GetBestMatch(int max_match, uint32_t match_buffer[][2],
171  float distmax, float ratiomax, int mbm) {
172  sift_buffer.resize(_num_sift[0] + _num_sift[1]);
173  int *buffer1 = (int*)&sift_buffer[0],
174  *buffer2 = (int*)&sift_buffer[_num_sift[0]];
175  _texMatch[0].InitTexture(_num_sift[0], 1);
176  ProgramCU::GetRowMatch(&_texDot, _texMatch, distmax, ratiomax);
177  _texMatch[0].CopyToHost(buffer1);
178  if (mbm) {
179  _texMatch[1].InitTexture(_num_sift[1], 1);
180  ProgramCU::GetColMatch(&_texCRT, _texMatch + 1, distmax, ratiomax);
181  _texMatch[1].CopyToHost(buffer2);
182  }
183  int nmatch = 0, j;
184  for (int i = 0; i < _num_sift[0] && nmatch < max_match; ++i) {
185  j = int(buffer1[i]);
186  if (j >= 0 && (!mbm || int(buffer2[j]) == i)) {
187  match_buffer[nmatch][0] = i;
188  match_buffer[nmatch][1] = j;
189  nmatch++;
190  }
191  }
192 
193  cudaError_t error = cudaGetLastError();
194  if (error != cudaSuccess) {
195  return -1;
196  }
197 
198  return nmatch;
199 }
200 
201 #endif
#define NULL
static int _GoodOpenGL
Definition: GlobalUtil.h:83
matcher export
Definition: SiftGPU.h:269
int max(int a, int b)
Definition: cutil_math.h:48
static void error(char *msg)
Definition: lsd.c:159
Definition: Eigen.h:85
CorePointDescSet * descriptors