AliceVision
Photogrammetric Computer Vision Framework
ROI.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2022 AliceVision contributors.
3 // This Source Code Form is subject to the terms of the Mozilla Public License,
4 // v. 2.0. If a copy of the MPL was not distributed with this file,
5 // You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #pragma once
8 
9 // allows code sharing between NVCC and other compilers
10 #if defined(__NVCC__)
11  #define CUDA_HOST_DEVICE __host__ __device__
12  #define CUDA_HOST __host__
13  #define CUDA_CEIL(f) ceil(f)
14  #define CUDA_FLOOR(f) floor(f)
15  #define CUDA_MIN(a, b) min(a, b)
16  #define CUDA_MAX(a, b) max(a, b)
17 #elif defined(ACPP_LIBKERNEL_IS_DEVICE_PASS)
18  #include <aliceVision/depthMap_sycl/sycl/sycl.hpp>
19  #define CUDA_HOST_DEVICE
20  #define CUDA_HOST
21  #define CUDA_CEIL(f) sycl::ceil(f)
22  #define CUDA_FLOOR(f) sycl::floor(f)
23  #define CUDA_MIN(a, b) sycl::min(a, b)
24  #define CUDA_MAX(a, b) sycl::max(a, b)
25 #else
26  #define CUDA_HOST_DEVICE
27  #define CUDA_HOST
28  #define CUDA_CEIL(f) std::ceil(f)
29  #define CUDA_FLOOR(f) std::floor(f)
30  #define CUDA_MIN(a, b) std::min(a, b)
31  #define CUDA_MAX(a, b) std::max(a, b)
32  #include <algorithm>
33  #include <cmath>
34  #include <ostream>
35 #endif
36 
37 namespace aliceVision {
38 
39 /*
40  * @struct Range
41  * @brief Small CPU and GPU host / device struct describing a 1d range.
42  */
43 struct Range
44 {
45  unsigned int begin = 0;
46  unsigned int end = 0;
47 
48  // default constructor
49  Range() = default;
50 
56  CUDA_HOST_DEVICE Range(unsigned int in_begin, unsigned int in_end)
57  : begin(in_begin),
58  end(in_end)
59  {}
60 
66  CUDA_HOST_DEVICE inline unsigned int size() const { return end - begin; }
67 
68  CUDA_HOST_DEVICE inline bool isEmpty() const { return begin >= end; }
69 
75  CUDA_HOST inline bool contains(unsigned int i) const { return ((begin <= i) && (end > i)); }
76 };
77 
78 inline Range intersect(const Range& a, const Range& b) { return Range(CUDA_MAX(a.begin, b.begin), CUDA_MIN(a.end, b.end)); }
79 
80 /*
81  * @struct ROI
82  * @brief Small CPU and GPU host / device struct describing a rectangular 2d region of interest.
83  */
84 struct ROI
85 {
86  Range x, y;
87 
88  // default constructor
89  ROI() = default;
90 
98  CUDA_HOST_DEVICE ROI(unsigned int in_beginX, unsigned int in_endX, unsigned int in_beginY, unsigned int in_endY)
99  : x(in_beginX, in_endX),
100  y(in_beginY, in_endY)
101  {}
102 
108  CUDA_HOST_DEVICE ROI(const Range& in_rangeX, const Range& in_rangeY)
109  : x(in_rangeX),
110  y(in_rangeY)
111  {}
112 
117  CUDA_HOST_DEVICE inline unsigned int width() const { return x.size(); }
118 
123  CUDA_HOST_DEVICE inline unsigned int height() const { return y.size(); }
124 
125  CUDA_HOST_DEVICE inline bool isEmpty() const { return x.isEmpty() || y.isEmpty(); }
126 
133  CUDA_HOST inline bool contains(unsigned int in_x, unsigned int in_y) const { return (x.contains(in_x) && y.contains(in_y)); }
134 };
135 
143 CUDA_HOST inline bool checkImageROI(const ROI& roi, int width, int height)
144 {
145  return ((roi.x.end <= (unsigned int)(width)) && (roi.x.begin < roi.x.end) && (roi.y.end <= (unsigned int)(height)) && (roi.y.begin < roi.y.end));
146 }
147 
154 CUDA_HOST inline Range downscaleRange(const Range& range, float downscale)
155 {
156  return Range(CUDA_FLOOR(range.begin / downscale), CUDA_CEIL(range.end / downscale));
157 }
158 
165 CUDA_HOST inline Range upscaleRange(const Range& range, float upscale)
166 {
167  return Range(CUDA_FLOOR(range.begin * upscale), CUDA_CEIL(range.end * upscale));
168 }
169 
176 CUDA_HOST inline Range inflateRange(const Range& range, float factor)
177 {
178  const float midRange = range.begin + (range.size() * 0.5f);
179  const float inflateSize = range.size() * factor * 0.5f;
180  return Range(CUDA_FLOOR(CUDA_MAX(midRange - inflateSize, 0.f)), CUDA_CEIL(midRange + inflateSize));
181 }
182 
189 CUDA_HOST inline ROI downscaleROI(const ROI& roi, float downscale) { return ROI(downscaleRange(roi.x, downscale), downscaleRange(roi.y, downscale)); }
190 
197 CUDA_HOST inline ROI upscaleROI(const ROI& roi, float upscale) { return ROI(upscaleRange(roi.x, upscale), upscaleRange(roi.y, upscale)); }
198 
205 CUDA_HOST inline ROI inflateROI(const ROI& roi, float factor) { return ROI(inflateRange(roi.x, factor), inflateRange(roi.y, factor)); }
206 
207 inline ROI intersect(const ROI& a, const ROI& b) { return ROI(intersect(a.x, b.x), intersect(a.y, b.y)); }
208 
209 #if !defined(__NVCC__)
210 inline std::ostream& operator<<(std::ostream& os, const Range& range)
211 {
212  os << range.begin << "-" << range.end;
213  return os;
214 }
215 inline std::ostream& operator<<(std::ostream& os, const ROI& roi)
216 {
217  os << "x: " << roi.x << ", y: " << roi.y;
218  return os;
219 }
220 #endif
221 
222 } // namespace aliceVision
aliceVision::inflateRange
CUDA_HOST Range inflateRange(const Range &range, float factor)
Inflate the given Range with the given factor.
Definition: ROI.hpp:176
aliceVision::ROI::ROI
CUDA_HOST_DEVICE ROI(unsigned int in_beginX, unsigned int in_endX, unsigned int in_beginY, unsigned int in_endY)
ROI constructor.
Definition: ROI.hpp:98
aliceVision::downscaleROI
CUDA_HOST ROI downscaleROI(const ROI &roi, float downscale)
Downscale the given ROI with the given downscale factor.
Definition: ROI.hpp:189
aliceVision::ROI::contains
CUDA_HOST bool contains(unsigned int in_x, unsigned int in_y) const
Return true if the given 2d point is contained in the ROI.
Definition: ROI.hpp:133
aliceVision::Range::size
CUDA_HOST_DEVICE unsigned int size() const
Return true if the given index is contained in the Range.
Definition: ROI.hpp:66
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::Range::Range
CUDA_HOST_DEVICE Range(unsigned int in_begin, unsigned int in_end)
Range constructor.
Definition: ROI.hpp:56
aliceVision::Range
Definition: ROI.hpp:43
aliceVision::Range::contains
CUDA_HOST bool contains(unsigned int i) const
Return true if the given index is contained in the Range.
Definition: ROI.hpp:75
aliceVision::ROI::height
CUDA_HOST_DEVICE unsigned int height() const
Get the ROI height.
Definition: ROI.hpp:123
aliceVision::upscaleRange
CUDA_HOST Range upscaleRange(const Range &range, float upscale)
Upscale the given Range with the given upscale factor.
Definition: ROI.hpp:165
aliceVision::ROI::width
CUDA_HOST_DEVICE unsigned int width() const
Get the ROI width.
Definition: ROI.hpp:117
aliceVision::upscaleROI
CUDA_HOST ROI upscaleROI(const ROI &roi, float upscale)
Upscale the given ROI with the given upscale factor.
Definition: ROI.hpp:197
aliceVision::downscaleRange
CUDA_HOST Range downscaleRange(const Range &range, float downscale)
Downscale the given Range with the given downscale factor.
Definition: ROI.hpp:154
aliceVision::checkImageROI
CUDA_HOST bool checkImageROI(const ROI &roi, int width, int height)
check if a given ROI is valid and can be contained in a given image
Definition: ROI.hpp:143
aliceVision::ROI::ROI
CUDA_HOST_DEVICE ROI(const Range &in_rangeX, const Range &in_rangeY)
ROI constructor.
Definition: ROI.hpp:108
aliceVision::ROI
Definition: ROI.hpp:84
aliceVision::inflateROI
CUDA_HOST ROI inflateROI(const ROI &roi, float factor)
Inflate the given ROI with the given factor.
Definition: ROI.hpp:205