NVIDIA DeepStream SDK API Reference

9.1 Release
sources/includes/ds3d/common/func_utils.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DS3D_COMMON_FUNC_UTILS__H
19 #define _DS3D_COMMON_FUNC_UTILS__H
20 
21 #include <ds3d/common/abi_frame.h>
22 #include <ds3d/common/common.h>
23 #include <ds3d/common/idatatype.h>
24 
25 #include <algorithm>
26 
27 namespace ds3d {
28 
29 using LockMutex = std::unique_lock<std::mutex>;
30 
31 inline bool
33 {
34  return c == ErrCode::kGood;
35 }
36 
37 inline bool
39 {
40  return c >= ErrCode::kGood;
41 }
42 
43 inline bool
45 {
46  return t == MemType::kCpu || t == MemType::kCpuPinned;
47 }
48 
49 inline bool
51 {
52  return t == MemType::kGpuCuda;
53 }
54 
55 inline const char*
57 {
58  static const std::unordered_map<ErrCode, const char*> kCodeTable = {
59 #define __DS3D_ERR_STR_DEF(code) {ErrCode::code, #code}
71 #undef __DS3D_ERR_STR_DEF
72  };
73  auto iter = kCodeTable.find(code);
74  if (iter == kCodeTable.cend()) {
75  return "undefined";
76  }
77  return iter->second;
78 };
79 
80 inline void
81 throwError(ErrCode code, const std::string& msg)
82 {
83  if (isGood(code)) {
84  return;
85  }
86  throw Exception(code, msg);
87 }
88 
89 template <class F, typename... Args>
90 inline ErrCode
91 CatchError(F f, Args... args)
92 {
93  ErrCode code = ErrCode::kGood;
94  DS3D_TRY { code = f(std::forward<Args>(args)...); }
95  DS3D_CATCH_ERROR(Exception, e.code(), "Catch ds3d error")
96  DS3D_CATCH_ERROR(std::exception, ErrCode::kUnknown, "Catch std exception")
97  DS3D_CATCH_ANY(ErrCode::kUnknown, "Catch unknown error")
98  return code;
99 }
100 
101 inline ErrCode
102 CatchVoidCall(std::function<void()> f)
103 {
104  DS3D_TRY { f(); }
105  DS3D_CATCH_ERROR(Exception, e.code(), "Catch ds3d error")
106  DS3D_CATCH_ERROR(std::exception, ErrCode::kUnknown, "Catch std exception")
107  DS3D_CATCH_ANY(ErrCode::kUnknown, "Catch unknown error")
108  return ErrCode::kGood;
109 }
110 
111 inline std::string
112 cppString(const char* str, size_t len = 0)
113 {
114  if (str) {
115  return (len ? std::string(str, len) : std::string(str));
116  }
117  return std::string("");
118 }
119 
120 template <typename T>
121 inline uint32_t
123 {
124  switch (f) {
125  case FrameType::kDepth:
126  return sizeof(T);
128  DS_ASSERT(sizeof(T) == 1);
129  return sizeof(T) * 4;
131  DS_ASSERT(sizeof(T) == 1);
132  return sizeof(T) * 3;
134  return sizeof(T) * 3;
136  return sizeof(T) * 4;
138  return sizeof(T) * 3;
139  default:
140  LOG_ERROR("bytesPerPixel with unsupported frametype: %d", static_cast<int>(f));
141  }
142  return 0;
143 }
144 
145 inline uint32_t
147 {
148  switch (t) {
149 #define __DS3D_DATATYPE_BYTES(t) \
150  case DataType::t: \
151  return sizeof(NativeData<DataType::t>::type)
162 #undef __DS3D_DATATYPE_BYTES
163  default:
164  assert(t);
165  return 0;
166  }
167 }
168 
169 inline size_t
170 ShapeSize(const Shape& shape)
171 {
172  if (!shape.numDims) {
173  return 0;
174  }
175  if (std::any_of(shape.d, shape.d + shape.numDims, [](int d) { return d < 0; })) {
176  assert(false);
177  return 0;
178  }
179  return (size_t)std::accumulate(
180  shape.d, shape.d + shape.numDims, 1, [](int s, int i) { return s * i; });
181 }
182 
183 inline bool
184 operator==(const Shape& a, const Shape& b)
185 {
186  if (a.numDims != b.numDims) {
187  return false;
188  }
189  for (uint32_t i = 0; i < a.numDims; ++i) {
190  if (a.d[i] != b.d[i]) {
191  return false;
192  }
193  }
194  return true;
195 }
196 
197 inline bool
198 operator!=(const Shape& a, const Shape& b)
199 {
200  return !(a == b);
201 }
202 
203 inline bool
204 readFile(const std::string& path, std::string& context)
205 {
206  std::ifstream fileIn(path, std::ios::in | std::ios::binary);
207  DS3D_FAILED_RETURN(fileIn, false, "open file %s failed", path.c_str());
208 
209  fileIn.seekg(0, std::ios::end);
210  size_t fileSize = fileIn.tellg();
211  context.resize(fileSize, 0);
212  fileIn.seekg(0, std::ios::beg);
213  fileIn.read(&context[0], fileSize);
214  fileIn.close();
215  return true;
216 }
217 
218 inline bool NvDs3dEnableDebug()
219 {
220  static bool debug = std::getenv("DS3D_ENABLE_DEBUG") ? true : false;
221  return debug;
222 }
223 
224 template <typename Data>
225 void
226 array2Vec3(Data* from, vec3<Data>& to)
227 {
228  for (int i = 0; i < 3; ++i) {
229  to.data[i] = from[i];
230  }
231 }
232 
233 inline bool
234 isNear(float a, float b)
235 {
236  if (fabs(a - b) <= std::numeric_limits<float>::epsilon())
237  return true;
238  return false;
239 }
240 
241 inline TransformMatrix
243 {
244  TransformMatrix mat = {{
245  {{param.fx, 0.0f, 0.0f, 0.0f}},
246  {{0.0f, param.fy, 0.0f, 0.0f}},
247  {{param.centerX, param.centerY, 1.0f, 0.0f}},
248  {{0.0f, 0.0f, 0.0f, 1.0f}},
249  }};
250  return mat;
251 }
252 
253 } // namespace ds3d
254 
258 
259 #endif // _DS3D_COMMON_FUNC_UTILS__H
ds3d::isGood
bool isGood(ErrCode c)
Definition: sources/includes/ds3d/common/func_utils.h:32
DS3D_TRY
#define DS3D_TRY
Definition: sources/includes/ds3d/common/defines.h:113
ds3d::ErrCode::kNvDsMeta
@ kNvDsMeta
ds3d::FrameType::kPointCoordUV
@ kPointCoordUV
DS3D_EXTERN_C_BEGIN
#define DS3D_EXTERN_C_BEGIN
Definition: sources/includes/ds3d/common/defines.h:130
ds3d::ErrCode::kNotFound
@ kNotFound
ds3d::DataType::kUint32
@ kUint32
ds3d::__xyz::data
T data[3]
Definition: sources/includes/ds3d/common/idatatype.h:40
ds3d::ShapeSize
size_t ShapeSize(const Shape &shape)
Definition: sources/includes/ds3d/common/func_utils.h:170
ds3d::IntrinsicsParam::centerX
float centerX
Definition: sources/includes/ds3d/common/idatatype.h:137
ds3d::MemType::kCpu
@ kCpu
ds3d::__xyz
Definition: sources/includes/ds3d/common/idatatype.h:39
ds3d::DataType::kFp32
@ kFp32
ds3d::ErrCode::kGL
@ kGL
ds3d::ErrCode::kUnsupported
@ kUnsupported
ds3d::ErrCode::kGst
@ kGst
ds3d::DataType
DataType
Definition: sources/includes/ds3d/common/idatatype.h:82
ds3d::CatchError
ErrCode CatchError(F f, Args... args)
Definition: sources/includes/ds3d/common/func_utils.h:91
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:69
ds3d::Exception
Definition: sources/includes/ds3d/common/common.h:148
ds3d::ErrCode::kNullPtr
@ kNullPtr
ds3d::ErrCode::kParam
@ kParam
ds3d::IntrinsicsParam
Definition: sources/includes/ds3d/common/idatatype.h:134
ds3d::DataType::kInt32
@ kInt32
ds3d::ErrCode::kGood
@ kGood
ds3d::ErrCode::kOutOfRange
@ kOutOfRange
ds3d::ErrCode::kCuda
@ kCuda
ds3d::ErrCode::kMem
@ kMem
ds3d::FrameType::kLidarXYZI
@ kLidarXYZI
ds3d::ErrCode::kRealSense
@ kRealSense
ds3d::readFile
bool readFile(const std::string &path, std::string &context)
Definition: sources/includes/ds3d/common/func_utils.h:204
ds3d::FrameType::kPointXYZ
@ kPointXYZ
ds3d::ErrCode::kLoadLib
@ kLoadLib
__DS3D_DATATYPE_BYTES
#define __DS3D_DATATYPE_BYTES(t)
DS3D_EXPORT_API
#define DS3D_EXPORT_API
Definition: sources/includes/ds3d/common/defines.h:129
ds3d::abiRefT
Definition: sources/includes/ds3d/common/abi_obj.h:43
DS_ASSERT
#define DS_ASSERT(...)
Definition: sources/includes/ds3d/common/defines.h:36
ds3d::ErrCode
ErrCode
Definition: sources/includes/ds3d/common/common.h:47
ds3d::bytesPerPixel
uint32_t bytesPerPixel(FrameType f)
Definition: sources/includes/ds3d/common/func_utils.h:122
ds3d::IntrinsicsParam::fy
float fy
Definition: sources/includes/ds3d/common/idatatype.h:140
ds3d::ErrCode::kTimeOut
@ kTimeOut
ds3d::Shape::numDims
uint32_t numDims
Definition: sources/includes/ds3d/common/idatatype.h:123
ds3d::NvDs3dEnableDebug
bool NvDs3dEnableDebug()
Definition: sources/includes/ds3d/common/func_utils.h:218
ds3d::ErrCode::kLockWakeup
@ kLockWakeup
ds3d::Exception::code
ErrCode code() const
Definition: sources/includes/ds3d/common/common.h:154
LOG_ERROR
#define LOG_ERROR(fmt,...)
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:60
ds3d::operator!=
bool operator!=(const Shape &a, const Shape &b)
Definition: sources/includes/ds3d/common/func_utils.h:198
DS3D_EXTERN_C_END
#define DS3D_EXTERN_C_END
Definition: sources/includes/ds3d/common/defines.h:131
ds3d::isNear
bool isNear(float a, float b)
Definition: sources/includes/ds3d/common/func_utils.h:234
__DS3D_ERR_STR_DEF
#define __DS3D_ERR_STR_DEF(code)
NvDs3d_CreateDataHashMap
DS3D_EXTERN_C_BEGIN DS3D_EXPORT_API ds3d::abiRefDataMap * NvDs3d_CreateDataHashMap()
ds3d::ErrCode::kByPass
@ kByPass
ds3d::operator==
bool operator==(const Shape &a, const Shape &b)
Definition: sources/includes/ds3d/common/func_utils.h:184
ds3d::TransformMatrix
Definition: sources/includes/ds3d/common/idatatype.h:150
ds3d::ErrCode::kTypeId
@ kTypeId
ds3d::MemType::kGpuCuda
@ kGpuCuda
ds3d::FrameType::kColorRGBA
@ kColorRGBA
ds3d::ErrCode::kConfig
@ kConfig
ds3d::DataType::kInt64
@ kInt64
ds3d::FrameType::kColorRGB
@ kColorRGB
ds3d::DataType::kUint16
@ kUint16
DS3D_CATCH_ERROR
#define DS3D_CATCH_ERROR(type, errcode, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:115
ds3d::IntrinsicsParam::centerY
float centerY
Definition: sources/includes/ds3d/common/idatatype.h:138
ds3d::array2Vec3
void array2Vec3(Data *from, vec3< Data > &to)
Definition: sources/includes/ds3d/common/func_utils.h:226
ds3d::isGpuMem
bool isGpuMem(MemType t)
Definition: sources/includes/ds3d/common/func_utils.h:50
ds3d::dataTypeBytes
uint32_t dataTypeBytes(DataType t)
Definition: sources/includes/ds3d/common/func_utils.h:146
ds3d::ErrCodeStr
const char * ErrCodeStr(ErrCode code)
Definition: sources/includes/ds3d/common/func_utils.h:56
ds3d::DataType::kDouble
@ kDouble
ds3d::DataType::kUint8
@ kUint8
ds3d::DataType::kFp16
@ kFp16
ds3d::LockMutex
std::unique_lock< std::mutex > LockMutex
Definition: sources/includes/ds3d/common/func_utils.h:29
ds3d::ErrCode::kUnknown
@ kUnknown
DS3D_CATCH_ANY
#define DS3D_CATCH_ANY(errcode, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:122
ds3d::FrameType::kDepth
@ kDepth
ds3d::isCpuMem
bool isCpuMem(MemType t)
Definition: sources/includes/ds3d/common/func_utils.h:44
ds3d::Shape::d
int32_t d[kMaxShapeDims]
Definition: sources/includes/ds3d/common/idatatype.h:124
ds3d::FrameType
FrameType
Definition: sources/includes/ds3d/common/idatatype.h:95
ds3d::DataType::kInt16
@ kInt16
ds3d::Shape
Definition: sources/includes/ds3d/common/idatatype.h:122
ds3d::cppString
std::string cppString(const char *str, size_t len=0)
Definition: sources/includes/ds3d/common/func_utils.h:112
ds3d::isNotBad
bool isNotBad(ErrCode c)
Definition: sources/includes/ds3d/common/func_utils.h:38
ds3d
Definition: sources/includes/ds3d/common/abi_dataprocess.h:25
ds3d::IntrinsicsParam::fx
float fx
Definition: sources/includes/ds3d/common/idatatype.h:139
ds3d::MemType::kCpuPinned
@ kCpuPinned
ds3d::getIntrinsicMat
TransformMatrix getIntrinsicMat(IntrinsicsParam &param)
Definition: sources/includes/ds3d/common/func_utils.h:242
ds3d::MemType
MemType
Definition: sources/includes/ds3d/common/idatatype.h:106
ds3d::ErrCode::kState
@ kState
ds3d::throwError
void throwError(ErrCode code, const std::string &msg)
Definition: sources/includes/ds3d/common/func_utils.h:81
ds3d::DataType::kInt8
@ kInt8
ds3d::ErrCode::kIncompatible
@ kIncompatible
ds3d::CatchVoidCall
ErrCode CatchVoidCall(std::function< void()> f)
Definition: sources/includes/ds3d/common/func_utils.h:102