SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSCaptureProps Class Reference

#include <SENSCamera.h>

Inherits std::vector< SENSCameraDeviceProps >.

Public Member Functions

bool containsDeviceId (const std::string &deviceId) const
 
bool supportsCameraFacing (const SENSCameraFacing &facing) const
 
const SENSCameraDevicePropscamPropsForDeviceId (const std::string &deviceId) const
 
std::pair< const SENSCameraDeviceProps *const, const SENSCameraStreamConfig *const > findBestMatchingConfig (SENSCameraFacing facing, const float horizFov, const int width, const int height) const
 Helper function to find a camera device and stream configuration with certain characteristics. More...
 

Detailed Description

Definition at line 136 of file SENSCamera.h.

Member Function Documentation

◆ camPropsForDeviceId()

const SENSCameraDeviceProps * SENSCaptureProps::camPropsForDeviceId ( const std::string &  deviceId) const

Definition at line 73 of file SENSCamera.cpp.

74 {
75  auto camPropsIt = std::find_if(begin(), end(), [&](const SENSCameraDeviceProps& comp)
76  { return comp.deviceId() == deviceId; });
77  if (camPropsIt != end())
78  return &*camPropsIt;
79  else
80  return nullptr;
81 }
const std::string & deviceId() const
Definition: SENSCamera.h:85

◆ containsDeviceId()

bool SENSCaptureProps::containsDeviceId ( const std::string &  deviceId) const

Definition at line 61 of file SENSCamera.cpp.

62 {
63  return std::find_if(begin(), end(), [&](const SENSCameraDeviceProps& comp)
64  { return comp.deviceId() == deviceId; }) != end();
65 }

◆ findBestMatchingConfig()

std::pair< const SENSCameraDeviceProps *const, const SENSCameraStreamConfig *const > SENSCaptureProps::findBestMatchingConfig ( SENSCameraFacing  facing,
const float  horizFov,
const int  width,
const int  height 
) const

Helper function to find a camera device and stream configuration with certain characteristics.

Returns a pair on nullptrs if characteristics were not fulfilled. Possible reasons: the facing is not available or if the transferred width and height would require an extrapolation.

Definition at line 89 of file SENSCamera.cpp.

93 {
94  struct SortElem
95  {
96  // corresponding camera characteristics
97  const SENSCameraDeviceProps* camChars;
98  // corresponding index in stream configurations
99  const SENSCameraStreamConfig* streamConfig;
100  int streamConfigIdx;
101  // cropped width (using stream configuration referenced by streamConfigIdx)
102  int widthCropped;
103  // cropped height (using stream configuration referenced by streamConfigIdx)
104  int heightCropped;
105  // horizontal and vertical field of view using cropped width
106  float horizFov = -1.f;
107  // scale factor between stream config and target resolutions
108  float scale;
109 
110  // difference to target field of view
111  float fovScore = 0.f;
112  // summed crop (used as later as score to make decision)
113  int cropScore;
114  // score that respecs necessary scale complexity
115  int scaleScore;
116 
117  void print(int idx) const
118  {
119  std::stringstream ss;
120  ss << "Sort Elem " << idx;
121  ss << " fovV: " << horizFov;
122  ss << " fovV score: " << fovScore;
123  ss << " crop score: " << cropScore;
124  ss << " scale score " << scaleScore;
125  ss << " widthCropped: " << widthCropped;
126  ss << " heightCropped: " << heightCropped;
127  ss << " scale: " << scale;
128  ss << " width orig " << camChars->streamConfigs()[streamConfigIdx].widthPix;
129  ss << " height orig " << camChars->streamConfigs()[streamConfigIdx].heightPix;
130  Utils::log("SENSCaptureProps", ss.str().c_str());
131  }
132  };
133  auto printSortElems = [](const std::vector<SortElem>& sortElems, std::string id)
134  {
135  std::cout << id << std::endl;
136  for (int i = 0; i < sortElems.size(); ++i)
137  sortElems.at(i).print(i);
138  };
139 
140  std::vector<SortElem> sortElems;
141 
142  // make cropped versions of all stream configurations
143  for (auto itChars = begin(); itChars != end(); ++itChars)
144  {
145  if (facing != itChars->facing())
146  continue;
147 
148  const auto& streams = itChars->streamConfigs();
149  float targetWidthDivHeight = (float)width / (float)height;
150 
151  for (auto itStream = streams.begin(); itStream != streams.end(); ++itStream)
152  {
153  const SENSCameraStreamConfig& config = *itStream;
154 
155  SortElem sortElem;
156  int cropW, cropH;
157  SENS::calcCrop({config.widthPix,
158  config.heightPix},
159  targetWidthDivHeight,
160  cropW,
161  cropH,
162  sortElem.widthCropped,
163  sortElem.heightCropped);
164  sortElem.scale = (float)width / (float)sortElem.widthCropped;
165 
166  if (sortElem.scale <= 1.f)
167  {
168  sortElem.camChars = &*itChars;
169  sortElem.streamConfigIdx = (int)(&*itStream - &*streams.begin());
170  sortElem.streamConfig = &*itStream;
171  if (config.focalLengthPix > 0)
172  {
173  // we have to use the cropped and unscaled width of the stream config because of the resolution of the focalLengthPix
174  sortElem.horizFov = SENS::calcFOVDegFromFocalLengthPix(config.focalLengthPix,
175  sortElem.widthCropped);
176  sortElem.fovScore = std::abs(sortElem.horizFov - horizFov);
177  }
178  sortElem.cropScore = cropW + cropH;
179  // if we have to scale add a score for it
180  if (!isEqualToOne(sortElem.scale))
181  sortElem.scaleScore = width * height;
182  else
183  sortElem.scaleScore = 0.f;
184 
185  sortElems.push_back(sortElem);
186  }
187  }
188  }
189 
190  if (sortElems.size())
191  {
192  // sort by difference to target fovV
193  std::sort(sortElems.begin(), sortElems.end(), [](const SortElem& lhs, const SortElem& rhs) -> bool
194  { return lhs.fovScore < rhs.fovScore; });
195  // printSortElems(sortElems, "sortElems");
196 
197  // extract all in a range of +-3 degree compared to the closest to target fovV
198  std::vector<SortElem> closeFovSortElems;
199  float maxFovDiff = sortElems.front().fovScore + 3;
200  for (SortElem elem : sortElems)
201  {
202  if (elem.fovScore < maxFovDiff)
203  closeFovSortElems.push_back(elem);
204  }
205 
206  // now extract the one with the best scale score from the remaining
207  std::sort(closeFovSortElems.begin(), closeFovSortElems.end(), [](const SortElem& lhs, const SortElem& rhs) -> bool
208  { return lhs.scale > rhs.scale; });
209  // printSortElems(closeFovSortElems, "closeFovSortElems");
210 
211  const SortElem& bestSortElem = closeFovSortElems.front();
212  return std::make_pair(bestSortElem.camChars, bestSortElem.streamConfig);
213  }
214  else
215  {
216  return std::make_pair(nullptr, nullptr);
217  }
218 }
bool isEqualToOne(float value)
Definition: SENSCamera.cpp:7
const std::vector< SENSCameraStreamConfig > & streamConfigs() const
Definition: SENSCamera.h:84
Config config
The configuration set in App::run.
Definition: AppAndroid.cpp:34
float calcFOVDegFromFocalLengthPix(const float focalLengthPix, const int imgLength)
Definition: SENSUtils.cpp:192
bool calcCrop(cv::Size inputSize, float targetWdivH, int &cropW, int &cropH, int &width, int &height)
Definition: SENSUtils.cpp:9
T abs(T a)
Definition: Utils.h:249
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1100

◆ supportsCameraFacing()

bool SENSCaptureProps::supportsCameraFacing ( const SENSCameraFacing facing) const

Definition at line 67 of file SENSCamera.cpp.

68 {
69  return std::find_if(begin(), end(), [&](const SENSCameraDeviceProps& comp)
70  { return comp.facing() == facing; }) != end();
71 }
const SENSCameraFacing & facing() const
Definition: SENSCamera.h:86

The documentation for this class was generated from the following files: