libimgdoc2
Loading...
Searching...
No Matches
types.h
1// SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <cstdint>
8#include <stdexcept>
9#include <cmath>
10#include <iomanip>
11#include <sstream>
12
13namespace imgdoc2
14{
16 typedef std::int64_t dbIndex;
17
19 typedef char Dimension;
20
24 inline bool IsDimensionValid(Dimension dimension)
25 {
26 return ((dimension >= 'a' && dimension <= 'z') || (dimension >= 'A' && dimension <= 'Z'));
27 }
28
32 inline void ThrowIfDimensionInvalid(Dimension dimension)
33 {
34 if (!imgdoc2::IsDimensionValid(dimension))
35 {
36 std::ostringstream ss;
37 ss << "The character '" << (isprint(dimension) ? dimension : '?') << "'=0x" << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(dimension) << " is not a valid dimension specifier.";
38 throw std::invalid_argument(ss.str());
39 }
40 }
41
44 template <typename t>
45 struct PointT
46 {
48 PointT() :x(0), y(0) {}
49
53 PointT(t x, t y) :x(x), y(y) {}
54
55 t x;
56 t y;
57 };
58
60 struct PointF : PointT<float>
61 {
63 PointF() :PointT<float>() {}
64
68 PointF(float x, float y) :PointT<float>(x, y) {}
69 };
70
72 struct PointD : PointT<double>
73 {
75 PointD() :PointT<double>() {}
76
80 PointD(double x, double y) :PointT<double>(x, y) {}
81 };
82
85 template <typename t>
86 struct Point3dT
87 {
89 Point3dT() :x(0), y(0), z(0) {}
90
95 Point3dT(t x, t y, t z) :x(x), y(y), z(z) {}
96
97 t x;
98 t y;
99 t z;
100 };
101
103 struct Point3dF : Point3dT<float>
104 {
106
111 Point3dF(float x, float y, float z) :Point3dT<float>(x, y, z) {}
112 };
113
115 struct Point3dD : Point3dT<double>
116 {
118
123 Point3dD(double x, double y, double z) :Point3dT<double>(x, y, z) {}
124 };
125
128 template<typename t>
130 {
132 RectangleT() :x(0), y(0), w(0), h(0) {}
133
140 RectangleT(t x, t y, t w, t h)
141 {
142 if (w < 0 || h < 0)
143 {
144 throw std::invalid_argument("width and height must be non-negative");
145 }
146
147 this->x = x;
148 this->y = y;
149 this->w = w;
150 this->h = h;
151 }
152
153 t x;
154 t y;
155 t w;
156 t h;
157
161 inline bool IsPointInside(const PointT<t>& p) const
162 {
163 if (this->x <= p.x && (this->x + this->w) >= p.x && this->y <= p.y && (this->y + this->h) >= p.y)
164 {
165 return true;
166 }
167
168 return false;
169 }
170 };
171
173 struct RectangleF : RectangleT<float>
174 {
176
177 // Constructor.
183 RectangleF(float x, float y, float w, float h) :RectangleT<float>(x, y, w, h) {}
184 };
185
187 struct RectangleD : RectangleT<double>
188 {
190
191 // Constructor.
197 RectangleD(double x, double y, double w, double h) :RectangleT<double>(x, y, w, h) {}
198 };
199
203 {
206 };
207
211 {
214 };
215
216 template <typename t> struct Vector3dT;
217 template<typename T> struct Plane_NormalAndDist;
218
221 template<typename t>
222 struct CuboidT
223 {
225 CuboidT() :x(0), y(0), z(0), w(0), h(0), d(0) {}
226
235 CuboidT(t x, t y, t z, t w, t h, t d)
236 {
237 if (w < 0 || h < 0 || d < 0)
238 {
239 throw std::invalid_argument("width and height must be non-negative");
240 }
241
242 this->x = x;
243 this->y = y;
244 this->z = z;
245 this->w = w;
246 this->h = h;
247 this->d = d;
248 }
249
250 t x;
251 t y;
252 t z;
253 t w;
254 t h;
255 t d;
256
260 bool IsPointInside(const Point3dT<t>& p) const
261 {
262 if (this->x <= p.x && (this->x + this->w) >= p.x && this->y <= p.y && (this->y + this->h) >= p.y && this->z <= p.z && (this->z + this->d) >= p.z)
263 {
264 return true;
265 }
266
267 return false;
268 }
269
273 {
274 return Point3dT<t>(this->x + this->w / 2, this->y + this->h / 2, this->z + this->d / 2);
275 }
276
282
287 {
288 return DoIntersect(*this, plane);
289 }
290 };
291
293 struct CuboidF : CuboidT<float>
294 {
295 CuboidF() :CuboidT<float>() {}
296
305 CuboidF(float x, float y, float z, float w, float h, float d) :CuboidT<float>(x, y, z, w, h, d) {}
306 };
307
309 struct CuboidD : CuboidT<double>
310 {
312
321 CuboidD(double x, double y, double z, double w, double h, double d) :CuboidT<double>(x, y, z, w, h, d) {}
322 };
323
326 template <typename t>
328 {
330 Vector3dT() :x(0), y(0), z(0) {}
331
336 Vector3dT(t x, t y, t z) :x(x), y(y), z(z) {}
337
340 explicit Vector3dT(const Point3dT<t> p) : Vector3dT(p.x, p.y, p.z) {}
341
342 t x;
343 t y;
344 t z;
345
349 {
350 t absVal = this->AbsoluteValue();
351 return Vector3dT(this->x / absVal, this->y / absVal, this->z / absVal);
352 }
353
357 {
358 return this->x * this->x + this->y * this->y + this->z * this->z;
359 }
360
364 {
365 return std::sqrt(this->AbsoluteValueSquared());
366 }
367
372 static Vector3dT<t> Cross(const Vector3dT<t>& vectorA, const Vector3dT<t>& vectorB)
373 {
374 return Vector3dT
375 {
376 vectorA.y * vectorB.z - vectorA.z * vectorB.y,
377 vectorA.z * vectorB.x - vectorA.x * vectorB.z,
378 vectorA.x * vectorB.y - vectorA.y * vectorB.x
379 };
380 }
381
386 static t Dot(const Vector3dT<t>& vectorA, const Vector3dT<t>& vectorB)
387 {
388 return vectorA.x * vectorB.x + vectorA.y * vectorB.y + vectorA.z * vectorB.z;
389 }
390 };
391
393 struct Vector3dF : Vector3dT<float>
394 {
396
401 Vector3dF(float x, float y, float z) :Vector3dT<float>(x, y, z) {}
402 };
403
405 struct Vector3dD : Vector3dT<double>
406 {
408
413 Vector3dD(double x, double y, double z) :Vector3dT<double>(x, y, z) {}
414 };
415
419 template<typename T>
421 {
424
427
432
440 {
441 const auto n = Vector3dT<T>::Cross(Vector3dT<T>(b.x - a.x, b.y - a.y, b.z - a.z), Vector3dT<T>(c.x - a.x, c.y - a.y, c.z - a.z)).Normalize();
442 const auto dist = Vector3dT<T>::Dot(n, Vector3dT<T>(a));
443 return Plane_NormalAndDist<T>(n, dist);
444 }
445 };
446
449 typedef Plane_NormalAndDist<float> Plane_NormalAndDistF;
450
453 typedef Plane_NormalAndDist<double> Plane_NormalAndDistD;
454
455 /*static*/template<typename t> inline bool CuboidT<t>::DoIntersect(const imgdoc2::CuboidT<t>& aabb, const imgdoc2::Plane_NormalAndDist<t>& plane)
456 {
457 // -> https://gdbooks.gitbooks.io/3dcollisions/content/Chapter2/static_aabb_plane.html
458 const auto centerAabb = aabb.CenterPoint();
459 const Vector3dT<t> aabbExtents = Vector3dD(aabb.w / 2, aabb.h / 2, aabb.d / 2);
460
461 // Compute the projection interval radius of b onto L(t) = b.c + t * p.n
462 const auto r = aabbExtents.x * fabs(plane.normal.x) + aabbExtents.y * fabs(plane.normal.y) + aabbExtents.z * fabs(plane.normal.z);
463
464 // Compute distance of box center from plane
465 const auto s = Vector3dT<t>::Dot(plane.normal, Vector3dT<t>(centerAabb)) - plane.distance;
466
467 // Intersection occurs when distance s falls within [-r,+r] interval
468 return fabs(s) <= r;
469 }
470}
Structure defining an axis-aligned cuboid in three dimensions with doubles representing the coordinat...
Definition: types.h:310
CuboidD(double x, double y, double z, double w, double h, double d)
Definition: types.h:321
Structure defining an axis-aligned cuboid in three dimensions with floats representing the coordinate...
Definition: types.h:294
CuboidF(float x, float y, float z, float w, float h, float d)
Definition: types.h:305
Definition: types.h:223
Point3dT< t > CenterPoint() const
Definition: types.h:272
CuboidT(t x, t y, t z, t w, t h, t d)
Definition: types.h:235
static bool DoIntersect(const imgdoc2::CuboidT< t > &aabb, const imgdoc2::Plane_NormalAndDist< t > &plane)
Definition: types.h:455
t z
The z-coordinate of the edge point of the cuboid.
Definition: types.h:252
bool DoesIntersectWith(const imgdoc2::Plane_NormalAndDist< t > &plane) const
Definition: types.h:286
t x
The x-coordinate of the edge point of the cuboid.
Definition: types.h:250
bool IsPointInside(const Point3dT< t > &p) const
Definition: types.h:260
t w
The width of the cuboid (i.e. the extent in x-direction).
Definition: types.h:253
t y
The y-coordinate of the edge point of the cuboid.
Definition: types.h:251
t h
The height of the cuboid (i.e. the extent in y-direction).
Definition: types.h:254
t d
The depth of the cuboid (i.e. the extent in z-direction).
Definition: types.h:255
CuboidT()
Default constructor. All properties are set to zero.
Definition: types.h:225
Definition: types.h:211
PointD b
The second point on the line.
Definition: types.h:213
PointD a
The first point on the line.
Definition: types.h:212
Definition: types.h:203
PointF b
The second point on the line.
Definition: types.h:205
PointF a
The first point on the line.
Definition: types.h:204
Definition: types.h:421
Plane_NormalAndDist()
Default constructor. All properties are initialized to zero.
Definition: types.h:426
Plane_NormalAndDist(const Vector3dT< T > &n, T d)
Definition: types.h:431
T distance
The distance of the plane to the origin.
Definition: types.h:423
static Plane_NormalAndDist< T > FromThreePoints(Point3dT< T > a, Point3dT< T > b, Point3dT< T > c)
Definition: types.h:439
Vector3dT< T > normal
The normal of the plane.
Definition: types.h:422
Structure defining a point in three dimensions with doubles representing the coordinates.
Definition: types.h:116
Point3dD(double x, double y, double z)
Definition: types.h:123
Structure defining a point in three dimensions with floats representing the coordinates.
Definition: types.h:104
Point3dF(float x, float y, float z)
Definition: types.h:111
Definition: types.h:87
Point3dT()
Default constructor which sets all properties to zero.
Definition: types.h:89
t y
The y-coordinate of the point.
Definition: types.h:98
Point3dT(t x, t y, t z)
Definition: types.h:95
t x
The x-coordinate of the point.
Definition: types.h:97
t z
The z-coordinate of the point.
Definition: types.h:99
Structure defining a point in two dimensions with doubles representing the coordinates.
Definition: types.h:73
PointD()
Default constructor which sets all properties to zero.
Definition: types.h:75
PointD(double x, double y)
Definition: types.h:80
Structure defining a point in two dimensions with floats representing the coordinates.
Definition: types.h:61
PointF()
Default constructor which sets all properties to zero.
Definition: types.h:63
PointF(float x, float y)
Definition: types.h:68
Definition: types.h:46
PointT()
Default constructor which sets all properties to zero.
Definition: types.h:48
t y
The y-coordinate of the point.
Definition: types.h:56
t x
The x-coordinate of the point.
Definition: types.h:55
PointT(t x, t y)
Definition: types.h:53
Structure defining an axis-aligned rectangle in two dimensions with doubles representing the coordina...
Definition: types.h:188
RectangleD(double x, double y, double w, double h)
Definition: types.h:197
Structure defining an axis-aligned rectangle in two dimensions with floats representing the coordinat...
Definition: types.h:174
RectangleF(float x, float y, float w, float h)
Definition: types.h:183
Definition: types.h:130
t w
The width of the rectangle.
Definition: types.h:155
RectangleT(t x, t y, t w, t h)
Definition: types.h:140
t y
The y-coordinate of the edge point of the rectangle.
Definition: types.h:154
t h
The height of the rectangle.
Definition: types.h:156
bool IsPointInside(const PointT< t > &p) const
Definition: types.h:161
t x
The x-coordinate of the edge point of the rectangle.
Definition: types.h:153
RectangleT()
Default constructor which sets all properties to zero.
Definition: types.h:132
Structure defining a vector in three dimensions with doubles representing the coordinates.
Definition: types.h:406
Vector3dD(double x, double y, double z)
Definition: types.h:413
Structure defining a vector in three dimensions with floats representing the coordinates.
Definition: types.h:394
Vector3dF(float x, float y, float z)
Definition: types.h:401
Definition: types.h:328
t AbsoluteValueSquared() const
Definition: types.h:356
t x
The component of the vector in x-direction.
Definition: types.h:342
t y
The component of the vector in y-direction.
Definition: types.h:343
t AbsoluteValue() const
Definition: types.h:363
static t Dot(const Vector3dT< t > &vectorA, const Vector3dT< t > &vectorB)
Definition: types.h:386
Vector3dT< t > Normalize() const
Definition: types.h:348
static Vector3dT< t > Cross(const Vector3dT< t > &vectorA, const Vector3dT< t > &vectorB)
Definition: types.h:372
t z
The component of the vector in z-direction.
Definition: types.h:344
Vector3dT()
Default constructor. All properties are set to zero.
Definition: types.h:330
Vector3dT(t x, t y, t z)
Definition: types.h:336
Vector3dT(const Point3dT< t > p)
Definition: types.h:340