Zivid C++ API  1.8.1+6967bc1b-1
Defining the Future of 3D Machine Vision
Settings2D.h
Go to the documentation of this file.
1 
2 /*******************************************************************************
3  * This file is part of the Zivid 3D Camera API
4  *
5  * Copyright 2015-2020 (C) Zivid AS
6  * All rights reserved.
7  *
8  * Zivid Software License, v1.0
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * 3. Neither the name of Zivid AS nor the names of its contributors may be used
21  * to endorse or promote products derived from this software without specific
22  * prior written permission.
23  *
24  * 4. This software, with or without modification, must not be used with any
25  * other 3D camera than from Zivid AS.
26  *
27  * 5. Any software provided in binary form under this license must not be
28  * reverse engineered, decompiled, modified and/or disassembled.
29  *
30  * THIS SOFTWARE IS PROVIDED BY ZIVID AS "AS IS" AND ANY EXPRESS OR IMPLIED
31  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32  * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
33  * DISCLAIMED. IN NO EVENT SHALL ZIVID AS OR CONTRIBUTORS BE LIABLE FOR ANY
34  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Contact: Zivid Support <support@zivid.com>
42  * Info: http://www.zivid.com
43  ******************************************************************************/
44 
45 #pragma once
46 
47 #include <chrono>
48 #include <ctime>
49 #include <iomanip>
50 #include <memory>
51 #include <sstream>
52 #include <string>
53 #include <tuple>
54 #include <utility>
55 #include <vector>
56 
57 #include "Zivid/APIExport.h"
58 #include "Zivid/Range.h"
59 
60 #ifdef _MSC_VER
61 # pragma warning(push)
62 # pragma warning(disable : 4251) // "X needs to have dll-interface to be used by clients of class Y."
63 #endif
64 
65 namespace Zivid
66 {
68  class ZIVID_API_EXPORT Settings2D
69  {
70  public:
71  static constexpr size_t version{ 1 };
72 
73 #ifndef NO_DOC
74  template<size_t>
75  struct Version;
76 
77  using LatestVersion = Zivid::Settings2D;
78 #endif
79 
81  ~Settings2D();
82 
84  Settings2D(const Settings2D &other);
85 
87  Settings2D &operator=(const Settings2D &other);
88 
90  Settings2D(Settings2D &&other) noexcept;
91 
93  Settings2D &operator=(Settings2D &&other) noexcept;
94 
96  Settings2D() = default;
97 
99  static constexpr bool isContainer{ true };
100 
102  static constexpr const char *path{ "" };
103 
105  static constexpr const char *name{ "Settings2D" };
106 
108  static constexpr const char *description{ R"description(2D Capture settings)description" };
109 
111  void set(const std::string &fullPath, const std::string &value);
112 
114  std::string getString(const std::string &fullPath) const;
115 
129  class ZIVID_API_EXPORT Brightness
130  {
131  public:
133  using ValueType = double;
134 
136  static constexpr bool isContainer{ false };
137 
139  static constexpr const char *path{ "Brightness" };
140 
142  static constexpr const char *name{ "Brightness" };
143 
145  static constexpr const char *description{
146  R"description(Brightness controls the light output from the projector.
147 
148 Brightness above 1.0 may be needed when the distance between the camera and the
149 scene is large, or in case of high levels of ambient lighting. Brightness above 1.0
150 is only supported on Zivid One Plus.
151 
152 When brightness is above 1.0 the duty cycle of the camera (the percentage of time the
153 camera can capture frames) will be reduced. The lights in the projector can be lit for
154 at most 50% of the time during a 10 second period. This limitation is enforced automatically
155 by the camera. Calling capture2D when the duty cycle limit has been reached will cause
156 the camera to first wait (sleep) for a duration of time to cool down, before capture will
157 start.
158 )description"
159  };
160 
162  Brightness() = default;
163 
165  explicit constexpr Brightness(ValueType value) noexcept(
166  std::is_nothrow_copy_constructible<ValueType>::value)
167  : m_value{ value }
168  {}
169 
171  const ValueType &value() const
172  {
173  return m_value;
174  }
175 
177  Range<ValueType> range() const
178  {
179  return { 0, 1.8 };
180  }
181 
183  void setFromString(const std::string &value)
184  {
185  m_value = std::stod(value);
186  }
187 
189  std::string toString() const
190  {
191  return std::to_string(m_value);
192  }
193 
195  bool operator==(const Brightness &other) const
196  {
197  return m_value == other.m_value;
198  }
199 
201  bool operator!=(const Brightness &other) const
202  {
203  return m_value != other.m_value;
204  }
205 
207  bool operator<(const Brightness &other) const
208  {
209  return m_value < other.m_value;
210  }
211 
213  bool operator>(const Brightness &other) const
214  {
215  return m_value > other.m_value;
216  }
217 
219  friend std::ostream &operator<<(std::ostream &stream, const Brightness &value)
220  {
221  return stream << value.toString();
222  }
223 
225  friend std::istream &operator>>(std::istream &stream, Brightness &value)
226  {
227  value.setFromString(
228  std::string{ std::istreambuf_iterator<char>{ stream }, std::istreambuf_iterator<char>{} });
229  return stream;
230  }
231 
232  private:
233  ValueType m_value{ 1 };
234  };
235 
237  class ZIVID_API_EXPORT ExposureTime
238  {
239  public:
241  using ValueType = std::chrono::microseconds;
242 
244  static constexpr bool isContainer{ false };
245 
247  static constexpr const char *path{ "ExposureTime" };
248 
250  static constexpr const char *name{ "ExposureTime" };
251 
253  static constexpr const char *description{ R"description(Exposure time for the image.)description" };
254 
256  ExposureTime() = default;
257 
259  explicit constexpr ExposureTime(ValueType value) noexcept(
260  std::is_nothrow_copy_constructible<ValueType>::value)
261  : m_value{ value }
262  {}
263 
265  const ValueType &value() const
266  {
267  return m_value;
268  }
269 
271  Range<ValueType> range() const
272  {
273  return { ValueType{ 6500 }, ValueType{ 100000 } };
274  }
275 
277  void setFromString(const std::string &value)
278  {
279  m_value = ValueType{ std::stoll(value) };
280  }
281 
283  std::string toString() const
284  {
285  return std::to_string(m_value.count());
286  }
287 
289  bool operator==(const ExposureTime &other) const
290  {
291  return m_value == other.m_value;
292  }
293 
295  bool operator!=(const ExposureTime &other) const
296  {
297  return m_value != other.m_value;
298  }
299 
301  bool operator<(const ExposureTime &other) const
302  {
303  return m_value < other.m_value;
304  }
305 
307  bool operator>(const ExposureTime &other) const
308  {
309  return m_value > other.m_value;
310  }
311 
313  friend std::ostream &operator<<(std::ostream &stream, const ExposureTime &value)
314  {
315  return stream << value.toString();
316  }
317 
319  friend std::istream &operator>>(std::istream &stream, ExposureTime &value)
320  {
321  value.setFromString(
322  std::string{ std::istreambuf_iterator<char>{ stream }, std::istreambuf_iterator<char>{} });
323  return stream;
324  }
325 
326  private:
327  ValueType m_value{ 8333 };
328  };
329 
331  class ZIVID_API_EXPORT Gain
332  {
333  public:
335  using ValueType = double;
336 
338  static constexpr bool isContainer{ false };
339 
341  static constexpr const char *path{ "Gain" };
342 
344  static constexpr const char *name{ "Gain" };
345 
347  static constexpr const char *description{ R"description(Analog gain in the camera)description" };
348 
350  Gain() = default;
351 
353  explicit constexpr Gain(ValueType value) noexcept(std::is_nothrow_copy_constructible<ValueType>::value)
354  : m_value{ value }
355  {}
356 
358  const ValueType &value() const
359  {
360  return m_value;
361  }
362 
364  Range<ValueType> range() const
365  {
366  return { 1, 16 };
367  }
368 
370  void setFromString(const std::string &value)
371  {
372  m_value = std::stod(value);
373  }
374 
376  std::string toString() const
377  {
378  return std::to_string(m_value);
379  }
380 
382  bool operator==(const Gain &other) const
383  {
384  return m_value == other.m_value;
385  }
386 
388  bool operator!=(const Gain &other) const
389  {
390  return m_value != other.m_value;
391  }
392 
394  bool operator<(const Gain &other) const
395  {
396  return m_value < other.m_value;
397  }
398 
400  bool operator>(const Gain &other) const
401  {
402  return m_value > other.m_value;
403  }
404 
406  friend std::ostream &operator<<(std::ostream &stream, const Gain &value)
407  {
408  return stream << value.toString();
409  }
410 
412  friend std::istream &operator>>(std::istream &stream, Gain &value)
413  {
415  std::string{ std::istreambuf_iterator<char>{ stream }, std::istreambuf_iterator<char>{} });
416  return stream;
417  }
418 
419  private:
420  ValueType m_value{ 2 };
421  };
422 
424  class ZIVID_API_EXPORT Iris
425  {
426  public:
428  using ValueType = size_t;
429 
431  static constexpr bool isContainer{ false };
432 
434  static constexpr const char *path{ "Iris" };
435 
437  static constexpr const char *name{ "Iris" };
438 
440  static constexpr const char *description{
441  R"description(Iris (aperture) setting for the camera)description"
442  };
443 
445  Iris() = default;
446 
448  explicit constexpr Iris(ValueType value) noexcept(std::is_nothrow_copy_constructible<ValueType>::value)
449  : m_value{ value }
450  {}
451 
453  const ValueType &value() const
454  {
455  return m_value;
456  }
457 
459  Range<ValueType> range() const
460  {
461  return { 0, 72 };
462  }
463 
465  void setFromString(const std::string &value)
466  {
467  m_value = std::stoull(value);
468  }
469 
471  std::string toString() const
472  {
473  return std::to_string(m_value);
474  }
475 
477  bool operator==(const Iris &other) const
478  {
479  return m_value == other.m_value;
480  }
481 
483  bool operator!=(const Iris &other) const
484  {
485  return m_value != other.m_value;
486  }
487 
489  bool operator<(const Iris &other) const
490  {
491  return m_value < other.m_value;
492  }
493 
495  bool operator>(const Iris &other) const
496  {
497  return m_value > other.m_value;
498  }
499 
501  friend std::ostream &operator<<(std::ostream &stream, const Iris &value)
502  {
503  return stream << value.toString();
504  }
505 
507  friend std::istream &operator>>(std::istream &stream, Iris &value)
508  {
509  value.setFromString(
510  std::string{ std::istreambuf_iterator<char>{ stream }, std::istreambuf_iterator<char>{} });
511  return stream;
512  }
513 
514  private:
515  ValueType m_value{ 22 };
516  };
517 
518  template<typename T, typename std::enable_if<std::is_same<T, Settings2D::Brightness>::value, int>::type = 0>
519  const Settings2D::Brightness &get() const
520  {
521  return m_brightness;
522  }
523 
524  template<typename T, typename std::enable_if<std::is_same<T, Settings2D::ExposureTime>::value, int>::type = 0>
525  const Settings2D::ExposureTime &get() const
526  {
527  return m_exposureTime;
528  }
529 
530  template<typename T, typename std::enable_if<std::is_same<T, Settings2D::Gain>::value, int>::type = 0>
531  const Settings2D::Gain &get() const
532  {
533  return m_gain;
534  }
535 
536  template<typename T, typename std::enable_if<std::is_same<T, Settings2D::Iris>::value, int>::type = 0>
537  const Settings2D::Iris &get() const
538  {
539  return m_iris;
540  }
541 
542  template<size_t i, typename std::enable_if<i == 0, int>::type = 0>
543  const Settings2D::Brightness &get() const
544  {
545  return m_brightness;
546  }
547 
548  template<size_t i, typename std::enable_if<i == 1, int>::type = 0>
549  const Settings2D::ExposureTime &get() const
550  {
551  return m_exposureTime;
552  }
553 
554  template<size_t i, typename std::enable_if<i == 2, int>::type = 0>
555  const Settings2D::Gain &get() const
556  {
557  return m_gain;
558  }
559 
560  template<size_t i, typename std::enable_if<i == 3, int>::type = 0>
561  const Settings2D::Iris &get() const
562  {
563  return m_iris;
564  }
565 
567  explicit Settings2D(const Brightness &brightness,
568  const ExposureTime &exposureTime,
569  const Gain &gain,
570  const Iris &iris);
571 
572  private:
573  Brightness m_brightness{};
574 
575  public:
577  Settings2D &set(const Brightness &value)
578  {
579  if(value.value() < value.range().min() || value.value() > value.range().max())
580  {
581  throw std::out_of_range("Brightness{ " + std::to_string(value.value()) + " } is not in range ["
582  + std::to_string(value.range().min()) + ", "
583  + std::to_string(value.range().max()) + "]");
584  }
585 
586  m_brightness = value;
587  return *this;
588  }
590  const Brightness &brightness() const
591  {
592  return m_brightness;
593  }
594 
595  private:
596  ExposureTime m_exposureTime{};
597 
598  public:
600  Settings2D &set(const ExposureTime &value)
601  {
602  if(value.value().count() < value.range().min().count()
603  || value.value().count() > value.range().max().count())
604  {
605  throw std::out_of_range("ExposureTime{ " + std::to_string(value.value().count())
606  + " } is not in range [" + std::to_string(value.range().min().count()) + ", "
607  + std::to_string(value.range().max().count()) + "]");
608  }
609 
610  m_exposureTime = value;
611  return *this;
612  }
614  const ExposureTime &exposureTime() const
615  {
616  return m_exposureTime;
617  }
618 
619  private:
620  Gain m_gain{};
621 
622  public:
624  Settings2D &set(const Gain &value)
625  {
626  if(value.value() < value.range().min() || value.value() > value.range().max())
627  {
628  throw std::out_of_range("Gain{ " + std::to_string(value.value()) + " } is not in range ["
629  + std::to_string(value.range().min()) + ", "
630  + std::to_string(value.range().max()) + "]");
631  }
632 
633  m_gain = value;
634  return *this;
635  }
637  const Gain &gain() const
638  {
639  return m_gain;
640  }
641 
642  private:
643  Iris m_iris{};
644 
645  public:
647  Settings2D &set(const Iris &value)
648  {
649  if(value.value() < value.range().min() || value.value() > value.range().max())
650  {
651  throw std::out_of_range("Iris{ " + std::to_string(value.value()) + " } is not in range ["
652  + std::to_string(value.range().min()) + ", "
653  + std::to_string(value.range().max()) + "]");
654  }
655 
656  m_iris = value;
657  return *this;
658  }
660  const Iris &iris() const
661  {
662  return m_iris;
663  }
664 
666  template<typename F>
667  void forEach(const F &f) const
668  {
669  f(m_brightness);
670  f(m_exposureTime);
671  f(m_gain);
672  f(m_iris);
673  }
674 
676  template<typename F>
677  void forEach(const F &f)
678  {
679  f(m_brightness);
680  f(m_exposureTime);
681  f(m_gain);
682  f(m_iris);
683  }
684 
686  template<typename F>
687  void traverseValues(const F &f) const
688  {
689  f(m_brightness);
690  f(m_exposureTime);
691  f(m_gain);
692  f(m_iris);
693  }
694 
696  template<typename F>
697  void traverseValues(const F &f)
698  {
699  f(m_brightness);
700  f(m_exposureTime);
701  f(m_gain);
702  f(m_iris);
703  }
704 
706  std::string toString() const;
707 
709  friend std::ostream &operator<<(std::ostream &stream, const Settings2D &value)
710  {
711  return stream << value.toString();
712  }
713 
715  void setFromString(const std::string &value);
716 
718  friend std::istream &operator>>(std::istream &stream, Settings2D &value)
719  {
720  value.setFromString(
721  std::string{ std::istreambuf_iterator<char>{ stream }, std::istreambuf_iterator<char>{} });
722  return stream;
723  }
724 
726  bool operator==(const Settings2D &other) const;
727 
729  bool operator!=(const Settings2D &other) const;
730 
732  explicit Settings2D(const std::string &fileName);
733 
735  void save(const std::string &fileName) const;
736 
738  void load(const std::string &fileName);
739  };
740 
741 #ifndef NO_DOC
742  template<>
743  struct Settings2D::Version<1>
744  {
745  using Type = Settings2D;
746  };
747 #endif
748 
749 } // namespace Zivid
750 
751 #ifdef _MSC_VER
752 # pragma warning(pop)
753 #endif
754 
755 #ifndef NO_DOC
756 # if !(defined(_MSC_VER) && (_MSC_VER <= 1900))
757 namespace std // NOLINT
758 {
759  template<>
760  struct tuple_size<Zivid::Settings2D> : integral_constant<size_t, 4>
761  {};
762 
763  template<size_t i>
764  struct tuple_element<i, Zivid::Settings2D>
765  {
766  using type // NOLINT
767  = decltype(declval<Zivid::Settings2D>().get<i>());
768  };
769 
770 } // namespace std
771 # endif
772 #endif
Zivid::Settings2D::toString
std::string toString() const
Get the value as string
Zivid::toString
ZIVID_API_EXPORT std::string toString(const std::exception &exception)
Get string representation of the exception
Zivid::Settings2D::ExposureTime::ValueType
std::chrono::microseconds ValueType
The type of the underlying value
Definition: Settings2D.h:320
Zivid::Settings2D::Gain
Analog gain in the camera
Definition: Settings2D.h:410
Zivid::Settings2D::Settings2D
Settings2D()=default
Default constructor
Zivid::Settings2D::Brightness::range
Range< ValueType > range() const
The range of valid values
Definition: Settings2D.h:256
Zivid::Settings2D::Brightness::toString
std::string toString() const
Get the value as string
Definition: Settings2D.h:268
Zivid::Settings2D::Gain::range
Range< ValueType > range() const
The range of valid values
Definition: Settings2D.h:443
Zivid::Settings2D::Gain::value
const ValueType & value() const
Get the value
Definition: Settings2D.h:437
Range.h
Zivid::Settings2D::Brightness::ValueType
double ValueType
The type of the underlying value
Definition: Settings2D.h:212
Zivid::Settings2D::Iris::ValueType
size_t ValueType
The type of the underlying value
Definition: Settings2D.h:507
Zivid::Settings2D::setFromString
void setFromString(const std::string &value)
Set from the given string
APIExport.h
Zivid::Settings2D::Brightness::setFromString
void setFromString(const std::string &value)
Set the value from string
Definition: Settings2D.h:262
Zivid::Settings2D::Gain::ValueType
double ValueType
The type of the underlying value
Definition: Settings2D.h:414
Zivid::Settings2D::ExposureTime::value
const ValueType & value() const
Get the value
Definition: Settings2D.h:344
Zivid::Settings2D::Gain::toString
std::string toString() const
Get the value as string
Definition: Settings2D.h:455
Zivid::Settings2D::Brightness::value
const ValueType & value() const
Get the value
Definition: Settings2D.h:250
Zivid::Settings2D::ExposureTime
Exposure time for the image.
Definition: Settings2D.h:316
Zivid::Settings2D::Iris::setFromString
void setFromString(const std::string &value)
Set the value from string
Definition: Settings2D.h:544
Zivid::Settings2D
2D Capture settings
Definition: Settings2D.h:107
Zivid::Settings2D::ExposureTime::setFromString
void setFromString(const std::string &value)
Set the value from string
Definition: Settings2D.h:356
Zivid
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:52
Zivid::Settings2D::Gain::setFromString
void setFromString(const std::string &value)
Set the value from string
Definition: Settings2D.h:449
Zivid::Settings2D::ExposureTime::range
Range< ValueType > range() const
The range of valid values
Definition: Settings2D.h:350
ZIVID_API_EXPORT
#define ZIVID_API_EXPORT
Definition: APIExport.h:56
Zivid::Settings2D::Brightness
Brightness controls the light output from the projector.
Definition: Settings2D.h:208
Zivid::operator<<
ZIVID_API_EXPORT std::ostream & operator<<(std::ostream &stream, const Camera &camera)
Serialize the value to a stream
Zivid::Settings2D::Iris
Iris (aperture) setting for the camera
Definition: Settings2D.h:503
Zivid::Range
Class describing a range of values for a given type T The range boudaries for both minimum and maximu...
Definition: Range.h:95