NVIDIA DeepStream SDK API Reference

6.4 Release
func_utils.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 
14 #ifndef _DS3D_COMMON_FUNC_UTILS__H
15 #define _DS3D_COMMON_FUNC_UTILS__H
16 
17 #include <ds3d/common/abi_frame.h>
18 #include <ds3d/common/common.h>
19 #include <ds3d/common/idatatype.h>
20 
21 #include <algorithm>
22 
23 namespace ds3d {
24 
25 using LockMutex = std::unique_lock<std::mutex>;
26 
27 inline bool
29 {
30  return c == ErrCode::kGood;
31 }
32 
33 inline bool
35 {
36  return c >= ErrCode::kGood;
37 }
38 
39 inline bool
41 {
42  return t == MemType::kCpu || t == MemType::kCpuPinned;
43 }
44 
45 inline bool
47 {
48  return t == MemType::kGpuCuda;
49 }
50 
51 inline const char*
53 {
54  static const std::unordered_map<ErrCode, const char*> kCodeTable = {
55 #define __DS3D_ERR_STR_DEF(code) {ErrCode::code, #code}
67 #undef __DS3D_ERR_STR_DEF
68  };
69  auto iter = kCodeTable.find(code);
70  if (iter == kCodeTable.cend()) {
71  return "undefined";
72  }
73  return iter->second;
74 };
75 
76 inline void
77 throwError(ErrCode code, const std::string& msg)
78 {
79  if (isGood(code)) {
80  return;
81  }
82  throw Exception(code, msg);
83 }
84 
85 template <class F, typename... Args>
86 inline ErrCode
87 CatchError(F f, Args... args)
88 {
89  ErrCode code = ErrCode::kGood;
90  DS3D_TRY { code = f(std::forward<Args>(args)...); }
91  DS3D_CATCH_ERROR(Exception, e.code(), "Catch ds3d error")
92  DS3D_CATCH_ERROR(std::exception, ErrCode::kUnknown, "Catch std exception")
93  DS3D_CATCH_ANY(ErrCode::kUnknown, "Catch unknown error")
94  return code;
95 }
96 
97 inline ErrCode
98 CatchVoidCall(std::function<void()> f)
99 {
100  DS3D_TRY { f(); }
101  DS3D_CATCH_ERROR(Exception, e.code(), "Catch ds3d error")
102  DS3D_CATCH_ERROR(std::exception, ErrCode::kUnknown, "Catch std exception")
103  DS3D_CATCH_ANY(ErrCode::kUnknown, "Catch unknown error")
104  return ErrCode::kGood;
105 }
106 
107 inline std::string
108 cppString(const char* str, size_t len = 0)
109 {
110  if (str) {
111  return (len ? std::string(str, len) : std::string(str));
112  }
113  return std::string("");
114 }
115 
116 template <typename T>
117 inline uint32_t
119 {
120  switch (f) {
121  case FrameType::kDepth:
122  return sizeof(T);
124  DS_ASSERT(sizeof(T) == 1);
125  return sizeof(T) * 4;
127  DS_ASSERT(sizeof(T) == 1);
128  return sizeof(T) * 3;
130  return sizeof(T) * 3;
132  return sizeof(T) * 4;
134  return sizeof(T) * 3;
135  default:
136  LOG_ERROR("bytesPerPixel with unsupported frametype: %d", static_cast<int>(f));
137  }
138  return 0;
139 }
140 
141 inline uint32_t
143 {
144  switch (t) {
145 #define __DS3D_DATATYPE_BYTES(t) \
146  case DataType::t: \
147  return sizeof(NativeData<DataType::t>::type)
158 #undef __DS3D_DATATYPE_BYTES
159  default:
160  assert(t);
161  return 0;
162  }
163 }
164 
165 inline size_t
166 ShapeSize(const Shape& shape)
167 {
168  if (!shape.numDims) {
169  return 0;
170  }
171  if (std::any_of(shape.d, shape.d + shape.numDims, [](int d) { return d < 0; })) {
172  assert(false);
173  return 0;
174  }
175  return (size_t)std::accumulate(
176  shape.d, shape.d + shape.numDims, 1, [](int s, int i) { return s * i; });
177 }
178 
179 inline bool
180 operator==(const Shape& a, const Shape& b)
181 {
182  if (a.numDims != b.numDims) {
183  return false;
184  }
185  for (uint32_t i = 0; i < a.numDims; ++i) {
186  if (a.d[i] != b.d[i]) {
187  return false;
188  }
189  }
190  return true;
191 }
192 
193 inline bool
194 operator!=(const Shape& a, const Shape& b)
195 {
196  return !(a == b);
197 }
198 
199 inline bool
200 readFile(const std::string& path, std::string& context)
201 {
202  std::ifstream fileIn(path, std::ios::in | std::ios::binary);
203  DS3D_FAILED_RETURN(fileIn, false, "open file %s failed", path.c_str());
204 
205  fileIn.seekg(0, std::ios::end);
206  size_t fileSize = fileIn.tellg();
207  context.resize(fileSize, 0);
208  fileIn.seekg(0, std::ios::beg);
209  fileIn.read(&context[0], fileSize);
210  fileIn.close();
211  return true;
212 }
213 
214 inline bool NvDs3dEnableDebug()
215 {
216  static bool debug = std::getenv("DS3D_ENABLE_DEBUG") ? true : false;
217  return debug;
218 }
219 
220 template <typename Data>
221 void
222 array2Vec3(Data* from, vec3<Data>& to)
223 {
224  for (int i = 0; i < 3; ++i) {
225  to.data[i] = from[i];
226  }
227 }
228 
229 inline bool
230 isNear(float a, float b)
231 {
232  if (fabs(a - b) <= std::numeric_limits<float>::epsilon())
233  return true;
234  return false;
235 }
236 
237 inline TransformMatrix
239 {
240  TransformMatrix mat = {{
241  {{param.fx, 0.0f, 0.0f, 0.0f}},
242  {{0.0f, param.fy, 0.0f, 0.0f}},
243  {{param.centerX, param.centerY, 1.0f, 0.0f}},
244  {{0.0f, 0.0f, 0.0f, 1.0f}},
245  }};
246  return mat;
247 }
248 
249 } // namespace ds3d
250 
254 
255 #endif // _DS3D_COMMON_FUNC_UTILS__H
ds3d::isGood
bool isGood(ErrCode c)
Definition: func_utils.h:28
ds3d::ErrCode::kNvDsMeta
@ kNvDsMeta
nvdsinferserver::InferDataType::kInt32
@ kInt32
ds3d::FrameType::kPointCoordUV
@ kPointCoordUV
ds3d::ErrCode::kNotFound
@ kNotFound
ds3d::Shape::d
int32_t d[kMaxShapeDims]
Definition: idatatype.h:119
__DS3D_ERR_STR_DEF
#define __DS3D_ERR_STR_DEF(code)
NvDs3d_CreateDataHashMap
DS3D_EXTERN_C_BEGIN DS3D_EXPORT_API ds3d::abiRefDataMap * NvDs3d_CreateDataHashMap()
DS_ASSERT
#define DS_ASSERT(...)
Definition: defines.h:31
ds3d::ShapeSize
size_t ShapeSize(const Shape &shape)
Definition: func_utils.h:166
ds3d::IntrinsicsParam::centerX
float centerX
Definition: idatatype.h:132
ds3d::MemType::kCpu
@ kCpu
ds3d::__xyz
Definition: idatatype.h:34
ds3d::ErrCode::kGL
@ kGL
ds3d::ErrCode::kUnsupported
@ kUnsupported
ds3d::LockMutex
std::unique_lock< std::mutex > LockMutex
Definition: func_utils.h:25
ds3d::ErrCode::kGst
@ kGst
ds3d::DataType
DataType
Definition: idatatype.h:77
DS3D_CATCH_ANY
#define DS3D_CATCH_ANY(errcode, fmt,...)
Definition: defines.h:117
ds3d::CatchError
ErrCode CatchError(F f, Args... args)
Definition: func_utils.h:87
ds3d::Exception
Definition: common.h:138
ds3d::ErrCode::kNullPtr
@ kNullPtr
ds3d::ErrCode::kParam
@ kParam
ds3d::IntrinsicsParam
Definition: idatatype.h:129
DS3D_EXTERN_C_BEGIN
#define DS3D_EXTERN_C_BEGIN
Definition: defines.h:125
ds3d::ErrCode::kGood
@ kGood
ds3d::ErrCode::kOutOfRange
@ kOutOfRange
ds3d::ErrCode::kCuda
@ kCuda
nvdsinferserver::InferDataType::kUint8
@ kUint8
ds3d::ErrCode::kMem
@ kMem
ds3d::FrameType::kLidarXYZI
@ kLidarXYZI
ds3d::ErrCode::kRealSense
@ kRealSense
ds3d::readFile
bool readFile(const std::string &path, std::string &context)
Definition: func_utils.h:200
ds3d::FrameType::kPointXYZ
@ kPointXYZ
ds3d::ErrCode::kLoadLib
@ kLoadLib
ds3d::__xyz::data
T data[3]
Definition: idatatype.h:35
ds3d::abiRefT< abiDataMap >
ds3d::ErrCode
ErrCode
Definition: common.h:43
ds3d::bytesPerPixel
uint32_t bytesPerPixel(FrameType f)
Definition: func_utils.h:118
ds3d::IntrinsicsParam::fy
float fy
Definition: idatatype.h:135
ds3d::ErrCode::kTimeOut
@ kTimeOut
ds3d::Shape::numDims
uint32_t numDims
Definition: idatatype.h:118
ds3d::ErrCodeStr
const char * ErrCodeStr(ErrCode code)
Definition: func_utils.h:52
ds3d::NvDs3dEnableDebug
bool NvDs3dEnableDebug()
Definition: func_utils.h:214
ds3d::ErrCode::kLockWakeup
@ kLockWakeup
nvdsinferserver::InferDataType::kInt16
@ kInt16
ds3d::Exception::code
ErrCode code() const
Definition: common.h:144
DS3D_EXTERN_C_END
#define DS3D_EXTERN_C_END
Definition: defines.h:126
ds3d::operator!=
bool operator!=(const Shape &a, const Shape &b)
Definition: func_utils.h:194
ds3d::isNear
bool isNear(float a, float b)
Definition: func_utils.h:230
abi_frame.h
nvdsinferserver::InferDataType::kFp32
@ kFp32
DS3D_CATCH_ERROR
#define DS3D_CATCH_ERROR(type, errcode, fmt,...)
Definition: defines.h:110
LOG_ERROR
#define LOG_ERROR
Copyright (c) 2019, NVIDIA CORPORATION.
Definition: logging.h:17
ds3d::ErrCode::kByPass
@ kByPass
ds3d::operator==
bool operator==(const Shape &a, const Shape &b)
Definition: func_utils.h:180
nvdsinferserver::InferDataType::kInt64
@ kInt64
ds3d::TransformMatrix
Definition: idatatype.h:145
ds3d::ErrCode::kTypeId
@ kTypeId
ds3d::MemType::kGpuCuda
@ kGpuCuda
ds3d::FrameType::kColorRGBA
@ kColorRGBA
ds3d::ErrCode::kConfig
@ kConfig
ds3d::FrameType::kColorRGB
@ kColorRGB
ds3d::IntrinsicsParam::centerY
float centerY
Definition: idatatype.h:133
ds3d::array2Vec3
void array2Vec3(Data *from, vec3< Data > &to)
Definition: func_utils.h:222
ds3d::isGpuMem
bool isGpuMem(MemType t)
Definition: func_utils.h:46
nvdsinferserver::InferDataType::kFp16
@ kFp16
common.h
ds3d::dataTypeBytes
uint32_t dataTypeBytes(DataType t)
Definition: func_utils.h:142
ds3d::DataType::kDouble
@ kDouble
ds3d::ErrCode::kUnknown
@ kUnknown
DS3D_EXPORT_API
#define DS3D_EXPORT_API
Definition: defines.h:124
idatatype.h
ds3d::FrameType::kDepth
@ kDepth
nvdsinferserver::InferDataType::kUint32
@ kUint32
ds3d::isCpuMem
bool isCpuMem(MemType t)
Definition: func_utils.h:40
nvdsinferserver::InferDataType::kUint16
@ kUint16
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: defines.h:64
__DS3D_DATATYPE_BYTES
#define __DS3D_DATATYPE_BYTES(t)
ds3d::FrameType
FrameType
Definition: idatatype.h:90
ds3d::Shape
Definition: idatatype.h:117
ds3d::cppString
std::string cppString(const char *str, size_t len=0)
Definition: func_utils.h:108
nvdsinferserver::InferDataType::kInt8
@ kInt8
ds3d::isNotBad
bool isNotBad(ErrCode c)
Definition: func_utils.h:34
ds3d
Definition: lidar_3d_datatype.h:33
ds3d::IntrinsicsParam::fx
float fx
Definition: idatatype.h:134
ds3d::MemType::kCpuPinned
@ kCpuPinned
ds3d::getIntrinsicMat
TransformMatrix getIntrinsicMat(IntrinsicsParam &param)
Definition: func_utils.h:238
ds3d::MemType
MemType
Definition: idatatype.h:101
DS3D_TRY
#define DS3D_TRY
Definition: defines.h:108
ds3d::ErrCode::kState
@ kState
ds3d::throwError
void throwError(ErrCode code, const std::string &msg)
Definition: func_utils.h:77
nvdsinferserver::InferMediaFormat::kUnknown
@ kUnknown
ds3d::ErrCode::kIncompatible
@ kIncompatible
ds3d::CatchVoidCall
ErrCode CatchVoidCall(std::function< void()> f)
Definition: func_utils.h:98