Zivid C++ API 2.18.0+1b44dbef-1
CameraCalibration.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of the Zivid API
3 *
4 * Copyright 2015-2026 (C) Zivid AS
5 * All rights reserved.
6 *
7 * Zivid Software License, v1.0
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of Zivid AS nor the names of its contributors may be used
20 * to endorse or promote products derived from this software without specific
21 * prior written permission.
22 *
23 * 4. This software, with or without modification, must not be used with any
24 * other 3D camera than from Zivid AS.
25 *
26 * 5. Any software provided in binary form under this license must not be
27 * reverse engineered, decompiled, modified and/or disassembled.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ZIVID AS "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL ZIVID AS OR CONTRIBUTORS BE LIABLE FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Contact: Zivid Customer Success Team <customersuccess@zivid.com>
41 * Info: http://www.zivid.com
42 ******************************************************************************/
43
44#pragma once
45
46#include <array>
47#include <chrono>
48#include <cmath>
49#include <ctime>
50#include <iomanip>
51#include <memory>
52#include <set>
53#include <sstream>
54#include <string>
55#include <tuple>
56#include <utility>
57#include <vector>
58
66#include "Zivid/Range.h"
67
68#ifdef _MSC_VER
69# pragma warning(push)
70# pragma warning(disable : 4251) // "X needs to have dll-interface to be used by clients of class Y."
71#endif
72
73namespace Zivid
74{
75
77
78 // NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding)
80 {
81 public:
84
86 static constexpr const char *path{ "" };
87
89 static constexpr const char *name{ "CameraCalibration" };
90
92 static constexpr const char *description{ R"description(Camera Calibration data model)description" };
93
94 static constexpr size_t version{ 1 };
95
96#ifndef NO_DOC
97 template<size_t>
98 struct Version;
99
100 using LatestVersion = Zivid::CameraCalibration;
101
102 // Short identifier. This value is not guaranteed to be universally unique
103 // Todo(ZIVID-2808): Move this to internal DataModelExt header
104 static constexpr std::array<uint8_t, 3> binaryId{ 'c', 'a', 'c' };
105
106#endif
107
109
110 // NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding)
112 {
113 public:
116
118 static constexpr const char *path{ "Extrinsics" };
119
121 static constexpr const char *name{ "Extrinsics" };
122
124 static constexpr const char *description{ R"description(Camera Extrinsics)description" };
125
128
130 Extrinsics() = default;
131
134 : m_value{ std::move(value) }
135 {}
136
139
142
144 std::string toString() const;
145
147 bool operator==(const Extrinsics &other) const
148 {
149 return m_value == other.m_value;
150 }
151
153 bool operator!=(const Extrinsics &other) const
154 {
155 return m_value != other.m_value;
156 }
157
159 friend std::ostream &operator<<(std::ostream &stream, const Extrinsics &value)
160 {
161 return stream << value.toString();
162 }
163
164 private:
165 void setFromString(const std::string &value);
166
168
169 friend struct DataModel::Detail::Befriend<Extrinsics>;
170 };
171
173
174 // NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding)
176 {
177 public:
180
182 static constexpr const char *path{ "Intrinsics" };
183
185 static constexpr const char *name{ "Intrinsics" };
186
188 static constexpr const char *description{ R"description(Camera Intrinsics)description" };
189
192
194 Intrinsics() = default;
195
198 : m_value{ std::move(value) }
199 {}
200
203
206
208 std::string toString() const;
209
211 bool operator==(const Intrinsics &other) const
212 {
213 return m_value == other.m_value;
214 }
215
217 bool operator!=(const Intrinsics &other) const
218 {
219 return m_value != other.m_value;
220 }
221
223 friend std::ostream &operator<<(std::ostream &stream, const Intrinsics &value)
224 {
225 return stream << value.toString();
226 }
227
228 private:
229 void setFromString(const std::string &value);
230
232
233 friend struct DataModel::Detail::Befriend<Intrinsics>;
234 };
235
236 using Descendants = std::tuple<CameraCalibration::Extrinsics, CameraCalibration::Intrinsics>;
237
240
242 explicit CameraCalibration(const std::string &fileName);
243
249 [[nodiscard]] static CameraCalibration fromSerialized(const std::string &value);
250
256 std::string serialize() const;
257
270#ifndef NO_DOC
271 template<
272 typename... Args,
273 typename std::enable_if<sizeof...(Args) >= 1, int>::type = 0,
274 typename std::enable_if<
275 Zivid::Detail::TypeTraits::AllArgsAreInTuple<Descendants, typename std::decay<Args>::type...>::value,
276 int>::type = 0>
277#else
278 template<typename... Args>
279#endif
280 explicit CameraCalibration(Args &&...args)
281 {
282 using namespace Zivid::Detail::TypeTraits;
283
284 static_assert(
285 AllArgsDecayedAreUnique<Args...>::value,
286 "Found duplicate types among the arguments passed to CameraCalibration(...). "
287 "Types should be listed at most once.");
288
289 set(std::forward<Args>(args)...);
290 }
291
303#ifndef NO_DOC
304 template<typename... Args, typename std::enable_if<sizeof...(Args) >= 2, int>::type = 0>
305#else
306 template<typename... Args>
307#endif
308 void set(Args &&...args)
309 {
310 using namespace Zivid::Detail::TypeTraits;
311
312 using AllArgsAreDescendantNodes = AllArgsAreInTuple<Descendants, typename std::decay<Args>::type...>;
313 static_assert(
314 AllArgsAreDescendantNodes::value, "All arguments passed to set(...) must be descendant nodes.");
315
316 static_assert(
317 AllArgsDecayedAreUnique<Args...>::value,
318 "Found duplicate types among the arguments passed to set(...). "
319 "Types should be listed at most once.");
320
321 Zivid::DataModel::Detail::invokeSetWithEachArgument(*this, std::forward<Args>(args)...);
322 }
323
336#ifndef NO_DOC
337 template<typename... Args, typename std::enable_if<sizeof...(Args) >= 1, int>::type = 0>
338#else
339 template<typename... Args>
340#endif
341 CameraCalibration copyWith(Args &&...args) const
342 {
343 using namespace Zivid::Detail::TypeTraits;
344
345 using AllArgsAreDescendantNodes = AllArgsAreInTuple<Descendants, typename std::decay<Args>::type...>;
346 static_assert(
347 AllArgsAreDescendantNodes::value, "All arguments passed to copyWith(...) must be descendant nodes.");
348
349 static_assert(
350 AllArgsDecayedAreUnique<Args...>::value,
351 "Found duplicate types among the arguments passed to copyWith(...). "
352 "Types should be listed at most once.");
353
354 auto copy{ *this };
355 copy.set(std::forward<Args>(args)...);
356 return copy;
357 }
358
360 const Extrinsics &extrinsics() const
361 {
362 return m_extrinsics;
363 }
364
367 {
368 return m_extrinsics;
369 }
370
373 {
374 m_extrinsics = value;
375 return *this;
376 }
377
379 const Intrinsics &intrinsics() const
380 {
381 return m_intrinsics;
382 }
383
386 {
387 return m_intrinsics;
388 }
389
392 {
393 m_intrinsics = value;
394 return *this;
395 }
396
397 template<
398 typename T,
399 typename std::enable_if<std::is_same<T, CameraCalibration::Extrinsics>::value, int>::type = 0>
401 {
402 return m_extrinsics;
403 }
404
405 template<
406 typename T,
407 typename std::enable_if<std::is_same<T, CameraCalibration::Intrinsics>::value, int>::type = 0>
409 {
410 return m_intrinsics;
411 }
412
413 template<size_t i, typename std::enable_if<i == 0, int>::type = 0>
415 {
416 return m_extrinsics;
417 }
418
419 template<size_t i, typename std::enable_if<i == 1, int>::type = 0>
421 {
422 return m_intrinsics;
423 }
424
426 template<typename F>
427 void forEach(const F &f) const
428 {
429 f(m_extrinsics);
430 f(m_intrinsics);
431 }
432
434 template<typename F>
435 void forEach(const F &f)
436 {
437 f(m_extrinsics);
438 f(m_intrinsics);
439 }
440
442 bool operator==(const CameraCalibration &other) const;
443
445 bool operator!=(const CameraCalibration &other) const;
446
448 std::string toString() const;
449
451 friend std::ostream &operator<<(std::ostream &stream, const CameraCalibration &value)
452 {
453 return stream << value.toString();
454 }
455
457 void save(const std::string &fileName) const;
458
460 void load(const std::string &fileName);
461
462 private:
463 void setFromString(const std::string &value);
464
465 void setFromString(const std::string &fullPath, const std::string &value);
466
467 std::string getString(const std::string &fullPath) const;
468
469 Extrinsics m_extrinsics;
470 Intrinsics m_intrinsics;
471
472 friend struct DataModel::Detail::Befriend<CameraCalibration>;
473 };
474
475#ifndef NO_DOC
476 template<>
477 struct CameraCalibration::Version<1>
478 {
479 using Type = CameraCalibration;
480 };
481#endif
482
483} // namespace Zivid
484
485#ifndef NO_DOC
487namespace Zivid::Detail
488{
489
490 ZIVID_CORE_EXPORT void save(const Zivid::CameraCalibration &dataModel, std::ostream &ostream);
491 ZIVID_CORE_EXPORT void load(Zivid::CameraCalibration &dataModel, std::istream &istream);
492
493} // namespace Zivid::Detail
494#endif
495
496#ifdef _MSC_VER
497# pragma warning(pop)
498#endif
499
500#ifndef NO_DOC
501# if !(defined(_MSC_VER) && (_MSC_VER <= 1900))
502namespace std // NOLINT
503{
504
505 template<>
506 struct tuple_size<Zivid::CameraCalibration> : integral_constant<size_t, 2>
507 {};
508
509 template<size_t i>
510 struct tuple_element<i, Zivid::CameraCalibration>
511 {
512 static_assert(i < tuple_size<Zivid::CameraCalibration>::value, "Index must be less than 2");
513
514 using type // NOLINT
515 = decltype(declval<Zivid::CameraCalibration>().get<i>());
516 };
517
518} // namespace std
519# endif
520#endif
521
522// If we have access to the DataModel library, automatically include internal DataModel
523// header. This header is necessary for serialization and deserialization.
524#if defined(__has_include) && !defined(NO_DOC)
525# if __has_include("Zivid/CameraCalibrationInternal.h") && __has_include("Zivid/DataModelNodeMetaData.h")
526# include "Zivid/CameraCalibrationInternal.h"
527# endif
528#endif
#define ZIVID_CORE_EXPORT
Definition CoreExport.h:56
Camera Extrinsics.
Definition CameraCalibration.h:112
static constexpr DataModel::NodeType nodeType
The type of this node.
Definition CameraCalibration.h:115
Extrinsics(Zivid::CameraExtrinsics value)
Constructor.
Definition CameraCalibration.h:133
const Zivid::CameraExtrinsics & value() const
Get the value.
static constexpr const char * path
The full path for this value.
Definition CameraCalibration.h:118
static constexpr const char * description
The description for this value.
Definition CameraCalibration.h:124
Extrinsics()=default
Default constructor.
Zivid::CameraExtrinsics ValueType
The type of the underlying value.
Definition CameraCalibration.h:127
Zivid::CameraExtrinsics & value()
Get a mutable reference to the value.
std::string toString() const
Get the value as string.
bool operator!=(const Extrinsics &other) const
Comparison operator.
Definition CameraCalibration.h:153
static constexpr const char * name
The name of this value.
Definition CameraCalibration.h:121
friend std::ostream & operator<<(std::ostream &stream, const Extrinsics &value)
Operator to serialize the value to a stream.
Definition CameraCalibration.h:159
bool operator==(const Extrinsics &other) const
Comparison operator.
Definition CameraCalibration.h:147
Camera Intrinsics.
Definition CameraCalibration.h:176
Zivid::CameraIntrinsics & value()
Get a mutable reference to the value.
const Zivid::CameraIntrinsics & value() const
Get the value.
static constexpr const char * description
The description for this value.
Definition CameraCalibration.h:188
std::string toString() const
Get the value as string.
bool operator!=(const Intrinsics &other) const
Comparison operator.
Definition CameraCalibration.h:217
static constexpr const char * name
The name of this value.
Definition CameraCalibration.h:185
Zivid::CameraIntrinsics ValueType
The type of the underlying value.
Definition CameraCalibration.h:191
static constexpr const char * path
The full path for this value.
Definition CameraCalibration.h:182
bool operator==(const Intrinsics &other) const
Comparison operator.
Definition CameraCalibration.h:211
static constexpr DataModel::NodeType nodeType
The type of this node.
Definition CameraCalibration.h:179
Intrinsics()=default
Default constructor.
Intrinsics(Zivid::CameraIntrinsics value)
Constructor.
Definition CameraCalibration.h:197
friend std::ostream & operator<<(std::ostream &stream, const Intrinsics &value)
Operator to serialize the value to a stream.
Definition CameraCalibration.h:223
Camera Calibration data model.
Definition CameraCalibration.h:80
const CameraCalibration::Intrinsics & get() const
Definition CameraCalibration.h:408
void forEach(const F &f)
Run the given function on each direct member with the value of the member as parameter.
Definition CameraCalibration.h:435
const Extrinsics & extrinsics() const
Get Extrinsics.
Definition CameraCalibration.h:360
std::string toString() const
Get the value as string.
void save(const std::string &fileName) const
Save to the given file.
const Intrinsics & intrinsics() const
Get Intrinsics.
Definition CameraCalibration.h:379
void forEach(const F &f) const
Run the given function on each direct member with the value of the member as parameter.
Definition CameraCalibration.h:427
Extrinsics & extrinsics()
Get Extrinsics.
Definition CameraCalibration.h:366
static constexpr size_t version
Definition CameraCalibration.h:94
CameraCalibration()
Default constructor.
friend std::ostream & operator<<(std::ostream &stream, const CameraCalibration &value)
Operator to send the value as string to a stream.
Definition CameraCalibration.h:451
void set(Args &&...args)
Set multiple arguments.
Definition CameraCalibration.h:308
CameraCalibration(Args &&...args)
Constructor taking variadic number of arguments.
Definition CameraCalibration.h:280
std::string serialize() const
Serialize to a string.
static constexpr DataModel::NodeType nodeType
The type of this node.
Definition CameraCalibration.h:83
Intrinsics & intrinsics()
Get Intrinsics.
Definition CameraCalibration.h:385
CameraCalibration & set(const Extrinsics &value)
Set Extrinsics.
Definition CameraCalibration.h:372
void load(const std::string &fileName)
Load from the given file.
static constexpr const char * name
The name of this value.
Definition CameraCalibration.h:89
CameraCalibration copyWith(Args &&...args) const
Returns a copy of this object with the given argument(s) set to the new value(s)
Definition CameraCalibration.h:341
std::tuple< CameraCalibration::Extrinsics, CameraCalibration::Intrinsics > Descendants
Definition CameraCalibration.h:236
bool operator==(const CameraCalibration &other) const
Equality operator.
bool operator!=(const CameraCalibration &other) const
Inequality operator.
const CameraCalibration::Extrinsics & get() const
Definition CameraCalibration.h:400
CameraCalibration(const std::string &fileName)
Construct CameraCalibration by loading from file.
static constexpr const char * description
The description for this value.
Definition CameraCalibration.h:92
CameraCalibration & set(const Intrinsics &value)
Set Intrinsics.
Definition CameraCalibration.h:391
static CameraCalibration fromSerialized(const std::string &value)
Construct a new CameraCalibration instance from a previously serialized string.
static constexpr const char * path
The full path for this value.
Definition CameraCalibration.h:86
Extrinsic parameters describing the pose of a camera in 3D space, expressed as a rotation quaternion ...
Definition CameraExtrinsics.h:79
Information about the intrinsic parameters of the camera (OpenCV model)
Definition CameraIntrinsics.h:78
NodeType
Definition NodeType.h:49
@ leafValue
Definition NodeType.h:52
@ group
Definition NodeType.h:50
Definition EnvironmentInfo.h:74
Get version information for the library.
Definition Version.h:58
The main Zivid namespace. All Zivid code is found here.
Definition Application.h:85