-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRansacMatchFilter.cpp
More file actions
136 lines (123 loc) · 6.8 KB
/
Copy pathRansacMatchFilter.cpp
File metadata and controls
136 lines (123 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright [2024] [Yao Yao]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "RansacMatchFilter.h"
#include "RansacMatchFilter.cuh"
#include "Runtime.hpp"
#include "FiberUtils.h"
namespace rsfm
{
RansacMatchFilter::~RansacMatchFilter() {
const auto stream = mRuntime.anyStream();
auto lock = mMutex.acquire(stream);
if (mDevScratchKey != Runtime::kInvalidKey) {
assert(mRuntime.storageManager().hasItem(mDevScratchKey));
mRuntime.storageManager().removeItem(mDevScratchKey);
mDevScratchKey = Runtime::kInvalidKey;
}
if (mPinnedScratchKey != Runtime::kInvalidKey) {
assert(mRuntime.storageManager().hasItem(mPinnedScratchKey));
mRuntime.storageManager().removeItem(mPinnedScratchKey);
mPinnedScratchKey = Runtime::kInvalidKey;
}
}
std::vector<bool> RansacMatchFilter::getInlierMask(Vec2<int> imgSize0, const std::vector<std::pair<Vec2f, Vec2f>>& matches, uint32_t minVotes, uint32_t nbRansacTests, float threshold, uint32_t cellCols, bool tryOtherAffines, cudaStream_t stream)
{
const uint32_t cellWidth = divUp(std::max(imgSize0.x, imgSize0.y), cellCols);
const uint32_t cols = divUp(imgSize0.x, cellWidth);
const uint32_t rows = divUp(imgSize0.y, cellWidth);
const uint32_t nbCells = cols * rows;
ASSERT(cols >= 1 && rows >= 1);
std::vector<std::vector<uint32_t>> cellIndices(cols*rows);
for (uint32_t n = 0; n < matches.size(); n++) {
const auto& [left, right] = matches.at(n);
const auto i = static_cast<uint32_t>(clamp(std::ceil(left.y / cellWidth), 0.f, rows - 1.f));
const auto j = static_cast<uint32_t>(clamp(std::ceil(left.x / cellWidth), 0.f, cols - 1.f));
const uint32_t idxCell = i * cols + j;
cellIndices.at(idxCell).push_back(n);
}
using namespace rmf;
std::vector<uint32_t> flatCellIndices;
std::vector<PtPair> reorderedMatches; reorderedMatches.reserve(matches.size());
std::vector<uint32_t> bounds({0}); bounds.reserve(nbCells + 1);
for (const auto& cell : cellIndices) {
for (const auto& idx : cell) {
const auto& [l, r] = matches.at(idx);
reorderedMatches.emplace_back(PtPair{{l.x, l.y}, {r.x, r.y}});
flatCellIndices.emplace_back(idx);
}
bounds.push_back(bounds.back() + cast32u(cell.size()));
}
const size_t alignment = 16;
const size_t voteBytes = roundUp(sizeof(uint8_t) * matches.size(), alignment);
const size_t affineBytes = roundUp(sizeof(kmat<float, 2, 3>) * nbCells, alignment);
const size_t matchBytes = roundUp(sizeof(rmf::PtPair) * matches.size(), alignment);
const size_t boundBytes = roundUp(sizeof(uint32_t) * (nbCells + 1), alignment);
const size_t devScratchBytes = voteBytes + affineBytes + matchBytes + boundBytes;
const size_t pinnedScratchBytes = std::max(matchBytes + boundBytes, voteBytes);
std::vector<uint8_t> votes(matches.size());
{
auto lock = mMutex.acquire(stream);
if (mDevScratchBytes < devScratchBytes) {
if (mDevScratchKey != Runtime::kInvalidKey) {
assert(mRuntime.storageManager().hasItem(mDevScratchKey));
mRuntime.storageManager().removeItem(mDevScratchKey);
mDevScratchKey = Runtime::kInvalidKey;
}
mDevScratchKey = mRuntime.allocCacheableScratch<std_byte, CudaMemType::kDevice>(devScratchBytes);
mDevScratchBytes = devScratchBytes;
}
if (mPinnedScratchBytes < pinnedScratchBytes) {
if (mPinnedScratchKey != Runtime::kInvalidKey) {
assert(mRuntime.storageManager().hasItem(mPinnedScratchKey));
mRuntime.storageManager().removeItem(mPinnedScratchKey);
mPinnedScratchKey = Runtime::kInvalidKey;
}
mPinnedScratchKey = mRuntime.allocCacheableScratch<std_byte, CudaMemType::kPinned>(pinnedScratchBytes);
mPinnedScratchBytes = pinnedScratchBytes;
}
const auto devScratchHolder = cudapp::storage::acquireScratch<std_byte, CudaMemType::kDevice>(mRuntime.storageManager(), mDevScratchKey, stream, true, true);
const auto devScratch = devScratchHolder.data();
const auto pVotes = reinterpret_cast<uint8_t*>(devScratch);
const auto pAffine = reinterpret_cast<kmat<float, 2, 3>*>(devScratch + voteBytes);
const auto pMatches = reinterpret_cast<rmf::PtPair*>(devScratch + voteBytes + affineBytes);
const auto pBounds = reinterpret_cast<uint32_t*>(devScratch + voteBytes + affineBytes + matchBytes);
const auto pinnedScratchHolder = cudapp::storage::acquireScratch<std_byte, CudaMemType::kPinned>(mRuntime.storageManager(), mPinnedScratchKey, stream, true, true);
const auto pinnedScratch = pinnedScratchHolder.data();
const auto pPinnedMatches = reinterpret_cast<rmf::PtPair*>(pinnedScratch);
const auto pPinnedBounds = reinterpret_cast<uint32_t*>(pinnedScratch + matchBytes);
const auto pPinnedVotes = reinterpret_cast<uint8_t*>(pinnedScratch);
assert(matches.size() == reorderedMatches.size());
launchCudaHostFunc(stream, [reorderedMatches{std::move(reorderedMatches)}, bounds{std::move(bounds)}, pPinnedMatches, pPinnedBounds]{
std::copy(reorderedMatches.begin(), reorderedMatches.end(), pPinnedMatches);
std::copy(bounds.begin(), bounds.end(), pPinnedBounds);
});
cudaCheck(cudaMemcpyAsync(pMatches, pPinnedMatches, sizeof(rmf::PtPair) * matches.size(), cudaMemcpyHostToDevice, stream));
cudaCheck(cudaMemcpyAsync(pBounds, pPinnedBounds, sizeof(uint32_t) * (nbCells + 1), cudaMemcpyHostToDevice, stream));
cudaRansacMatchFilter(pVotes, pAffine, pMatches, cast32u(matches.size()), pBounds, cols, rows, nbRansacTests, cellWidth * threshold, tryOtherAffines, stream);
cudaCheck(cudaMemcpyAsync(pPinnedVotes, pVotes, sizeof(pVotes[0]) * matches.size(), cudaMemcpyDeviceToHost, stream));
launchCudaHostFunc(stream, [&votes, pPinnedVotes]{
std::copy_n(pPinnedVotes, votes.size(), votes.begin());
});
}
#if 1
cudapp::fiberSyncCudaStream(stream);
#else
cudaCheck(cudaStreamSynchronize(stream));
#endif
std::vector<bool> results(votes.size());
for (uint32_t i = 0; i < votes.size(); i++) {
results.at(flatCellIndices.at(i)) = (votes.at(i) >= minVotes);
}
return results;
}
} // namespace rsfm