Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
Box.h
1// trax track library
2// AD 2014
3//
4// "She's got a box full [?]"
5//
6// Jade Bird
7//
8// Copyright (c) 2025 Trend Redaktions- und Verlagsgesellschaft mbH
9// Copyright (c) 2019 Marc-Michael Horstmann
10//
11// Permission is hereby granted to any person obtaining a copy of this software
12// and associated source code (the "Software"), to use, view, and study the
13// Software for personal or internal business purposes, subject to the following
14// conditions:
15//
16// 1. Redistribution, modification, sublicensing, or commercial use of the
17// Software is NOT permitted without prior written consent from the copyright
18// holder.
19//
20// 2. The Software is provided "AS IS", without warranty of any kind, express
21// or implied.
22//
23// 3. All copies of the Software must retain this license notice.
24//
25// For further information, please contact: horstmann.marc@trendverlag.de
26
27#pragma once
28
29#include "common/Interval.h"
30
31
32namespace spat{
33
34 template<typename> struct Position;
35 template<typename> struct Sphere;
36 template<typename> struct Vector;
37
39 template<typename Valtype>
40 struct Box
41 {
42 typedef Valtype value_type;
43
47
48
63 Box() noexcept = default;
64 Box( Valtype xext, Valtype yext, Valtype zext ) noexcept;
65 explicit Box( const Vector<Valtype>& cuboid ) noexcept;
66 Box( Valtype xnear, Valtype ynear, Valtype znear, Valtype xfar, Valtype yfar, Valtype zfar ) noexcept;
67 Box( const Position<Valtype>& nearCorner, const Position<Valtype>& farCorner ) noexcept;
69
70
72 Box& Init() noexcept;
73
74
76 bool Normal() const noexcept;
77
78
82 Box& Normalize() noexcept;
83
84
86 const Position<Valtype> Near() const noexcept;
87
88
90 const Position<Valtype> Far() const noexcept;
91
92
94 Valtype NearX() const noexcept;
95
96
98 Valtype NearY() const noexcept;
99
100
102 Valtype NearZ() const noexcept;
103
104
106 Valtype FarX() const noexcept;
107
108
110 Valtype FarY() const noexcept;
111
112
114 Valtype FarZ() const noexcept;
115
116
118 Box& NearX( Valtype val ) noexcept;
119
120
122 Box& NearY( Valtype val ) noexcept;
123
124
126 Box& NearZ( Valtype val ) noexcept;
127
128
130 Box& FarX( Valtype val ) noexcept;
131
132
134 Box& FarY( Valtype val ) noexcept;
135
136
138 Box& FarZ( Valtype val ) noexcept;
139
140
142 Valtype EdgeX() const noexcept;
143
144
147 Box& EdgeX( Valtype width ) noexcept;
148
149
151 Valtype EdgeY() const noexcept;
152
153
156 Box& EdgeY( Valtype width ) noexcept;
157
158
160 Valtype EdgeZ() const noexcept;
161
162
165 Box& EdgeZ( Valtype width ) noexcept;
166
167
169 auto Volume() const noexcept -> decltype(Valtype{}*Valtype{}*Valtype{});
170
171
173 Vector<Valtype> Diagonal() const noexcept;
174
175
177 Position<Valtype> Center() const noexcept;
178
179
181 Box& Union( const Box& b1, const Box& b2 ) noexcept;
182
183
185 Box& Union( const Box& box ) noexcept;
186
187
192 bool Includes( Valtype x, Valtype y, Valtype z ) const noexcept;
193
194
199 bool Includes( const Position<Valtype>& pt ) const noexcept;
200
201
206 Box& Expand( Valtype x, Valtype y, Valtype z ) noexcept;
207
208
213 Box& Expand( const Position<Valtype>& pt ) noexcept;
214
215
220 Box& Inflate( Valtype dx, Valtype dy, Valtype dz ) noexcept;
221
222
227 Box& Deflate( Valtype dx, Valtype dy, Valtype dz ) noexcept;
228
229
231 Box& Move( const Vector<Valtype>& dPos ) noexcept;
232
233
235 Box& Round( int toDigit ) noexcept;
236 };
237
238
242 template<typename Valtype>
243 bool Intersecting( const Box<Valtype>& box, const Sphere<Valtype>& sphere ) noexcept;
244
245 template<typename Valtype>
246 bool Intersecting( const Box<Valtype>& boxA, const Box<Valtype>& boxB ) noexcept;
248
251 template<typename Valtype> constexpr
252 bool operator==( const Box<Valtype>& b1, const Box<Valtype>& b2 ) noexcept;
253 template<typename Valtype> constexpr
254 bool operator!=( const Box<Valtype>& b1, const Box<Valtype>& b2 ) noexcept;
256
257
258// /// Transform Box by homogenous transformation matrix
259// template<typename Valtype>
260// Box<Valtype> operator*( const Transformation<Valtype>& mat, const Box<Valtype>& sp );
261//
262// /// Smallest geom that contains several points.
263// /// Take an array of vertices. The size counts
264// /// in vertices and the stride is in bytes.
265// template<typename Valtype>
266// Box<Valtype>& SmallestContainingAll( Box<Valtype>& Result,
267// const void* pPositions,
268// unsigned int numPositions,
269// unsigned short offset = 0,
270// unsigned short stride = sizeof(Position<Valtype>) );
271//
272//
274template<typename Valtype> inline
275Box<Valtype>::Box( Valtype xext, Valtype yext, Valtype zext ) noexcept
276 : m_WidthX{-xext/2,xext/2},
277 m_WidthY{-yext/2,yext/2},
278 m_WidthZ{-zext/2,zext/2}
279{}
280
281template<typename Valtype> inline
282Box<Valtype>::Box( const Vector<Valtype>& diagonal ) noexcept
283 : m_WidthX{-diagonal.dx/2,diagonal.dx/2},
284 m_WidthY{-diagonal.dy/2,diagonal.dy/2},
285 m_WidthZ{-diagonal.dz/2,diagonal.dz/2}
286{}
287
288template<typename Valtype> inline
289Box<Valtype>::Box( Valtype xnear, Valtype ynear, Valtype znear, Valtype xfar, Valtype yfar, Valtype zfar ) noexcept
290 : m_WidthX{xnear,xfar},
291 m_WidthY{ynear,yfar},
292 m_WidthZ{znear,zfar}
293{}
294
295template<typename Valtype> inline
296Box<Valtype>::Box( const Position<Valtype>& nearCorner, const Position<Valtype>& farCorner ) noexcept
297 : m_WidthX{nearCorner.x,farCorner.x},
298 m_WidthY{nearCorner.y,farCorner.y},
299 m_WidthZ{nearCorner.z,farCorner.z}
300{}
301
302template<typename Valtype> inline
304 m_WidthX.Init();
305 m_WidthY.Init();
306 m_WidthZ.Init();
307 return *this;
308}
309
310template<typename Valtype> inline
311bool Box<Valtype>::Normal() const noexcept{
312 return m_WidthX.Normal() && m_WidthY.Normal() && m_WidthZ.Normal();
313}
314
315template<typename Valtype> inline
317 m_WidthX.Normalize();
318 m_WidthY.Normalize();
319 m_WidthZ.Normalize();
320 return *this;
321}
322
323template<typename Valtype> inline
325 return { NearX(), NearY(), NearZ() };
326}
327
328template<typename Valtype> inline
330 return { FarX(), FarY(), FarZ() };
331}
332
333template<typename Valtype> inline
334Valtype Box<Valtype>::NearX() const noexcept{
335 return m_WidthX.Near();
336}
337
338template<typename Valtype> inline
339Valtype Box<Valtype>::NearY() const noexcept{
340 return m_WidthY.Near();
341}
342
343template<typename Valtype> inline
344Valtype Box<Valtype>::NearZ() const noexcept{
345 return m_WidthZ.Near();
346}
347
348template<typename Valtype> inline
349Valtype Box<Valtype>::FarX() const noexcept{
350 return m_WidthX.Far();
351}
352
353template<typename Valtype> inline
354Valtype Box<Valtype>::FarY() const noexcept{
355 return m_WidthY.Far();
356}
357
358template<typename Valtype> inline
359Valtype Box<Valtype>::FarZ() const noexcept{
360 return m_WidthZ.Far();
361}
362
363template<typename Valtype> inline
364Box<Valtype>& Box<Valtype>::NearX( Valtype val ) noexcept{
365 m_WidthX.Near(val);
366 return *this;
367}
368
369template<typename Valtype> inline
370Box<Valtype>& Box<Valtype>::NearY( Valtype val ) noexcept{
371 m_WidthY.Near(val);
372 return *this;
373}
374
375template<typename Valtype> inline
376Box<Valtype>& Box<Valtype>::NearZ( Valtype val ) noexcept{
377 m_WidthZ.Near(val);
378 return *this;
379}
380
381template<typename Valtype> inline
382Box<Valtype>& Box<Valtype>::FarX( Valtype val ) noexcept{
383 m_WidthX.Far(val);
384 return *this;
385}
386
387template<typename Valtype> inline
388Box<Valtype>& Box<Valtype>::FarY( Valtype val ) noexcept{
389 m_WidthY.Far(val);
390 return *this;
391}
392
393template<typename Valtype> inline
394Box<Valtype>& Box<Valtype>::FarZ( Valtype val ) noexcept{
395 m_WidthZ.Far(val);
396 return *this;
397}
398
399template<typename Valtype> inline
400Valtype Box<Valtype>::EdgeX() const noexcept{
401 return m_WidthX.Length();
402}
403
404template<typename Valtype> inline
405Box<Valtype>& Box<Valtype>::EdgeX( Valtype width ) noexcept{
406 m_WidthX.Length(width);
407 return *this;
408}
409
410template<typename Valtype> inline
411Valtype Box<Valtype>::EdgeY() const noexcept{
412 return m_WidthY.Length();
413}
414
415template<typename Valtype> inline
416Box<Valtype>& Box<Valtype>::EdgeY( Valtype width ) noexcept{
417 m_WidthY.Length(width);
418 return *this;
419}
420
421template<typename Valtype> inline
422Valtype Box<Valtype>::EdgeZ() const noexcept{
423 return m_WidthZ.Length();
424}
425
426template<typename Valtype> inline
427Box<Valtype>& Box<Valtype>::EdgeZ( Valtype width ) noexcept{
428 m_WidthZ.Length(width);
429 return *this;
430}
431
432template<typename Valtype> inline
433auto Box<Valtype>::Volume() const noexcept -> decltype(Valtype{}*Valtype{}*Valtype{}){
434 return EdgeX() * EdgeY() * EdgeZ();
435}
436
437template<typename Valtype> inline
439 return { EdgeX(), EdgeY(), EdgeZ() };
440}
441
442template<typename Valtype> inline
444 return { m_WidthX.Center(), m_WidthY.Center(), m_WidthZ.Center() };
445}
446
447template<typename Valtype> inline
448Box<Valtype>& Box<Valtype>::Union( const Box& b1, const Box& b2 ) noexcept{
449 m_WidthX.Union( b1.m_WidthX, b2.m_WidthX );
450 m_WidthY.Union( b1.m_WidthY, b2.m_WidthY );
451 m_WidthZ.Union( b1.m_WidthZ, b2.m_WidthZ );
452 return *this;
453}
454
455
456template<typename Valtype> inline
457Box<Valtype>& Box<Valtype>::Union( const Box& box ) noexcept{
458 m_WidthX.Union( box.m_WidthX );
459 m_WidthY.Union( box.m_WidthY );
460 m_WidthZ.Union( box.m_WidthZ );
461 return *this;
462}
463
464template<typename Valtype> inline
465bool Box<Valtype>::Includes( Valtype x, Valtype y, Valtype z ) const noexcept{
466 return m_WidthX.Includes(x) && m_WidthY.Includes(y) && m_WidthZ.Includes(z);
467}
468
469template<typename Valtype> inline
470bool Box<Valtype>::Includes( const Position<Valtype>& pt ) const noexcept{
471 return Includes( pt.x, pt.y, pt.z );
472}
473
474template<typename Valtype> inline
475Box<Valtype>& Box<Valtype>::Expand( Valtype x, Valtype y, Valtype z ) noexcept{
476 m_WidthX.Expand(x);
477 m_WidthY.Expand(y);
478 m_WidthZ.Expand(z);
479 return *this;
480}
481
482template<typename Valtype> inline
484 return Expand( pt.x, pt.y, pt.z );
485}
486
487template<typename Valtype> inline
488Box<Valtype>& Box<Valtype>::Inflate( Valtype dx, Valtype dy, Valtype dz ) noexcept
489{
490 m_WidthX.Inflate( dx );
491 m_WidthY.Inflate( dy );
492 m_WidthZ.Inflate( dz );
493 return *this;
494}
495
496template<typename Valtype> inline
497Box<Valtype>& Box<Valtype>::Deflate( Valtype dx, Valtype dy, Valtype dz ) noexcept
498{
499 m_WidthX.Deflate( dx );
500 m_WidthY.Deflate( dy );
501 m_WidthZ.Deflate( dz );
502 return *this;
503}
504
505template<typename Valtype> inline
507 m_WidthX.Move( dPos.dx );
508 m_WidthY.Move( dPos.dy );
509 m_WidthZ.Move( dPos.dz );
510 return *this;
511}
512
513template<typename Valtype> inline
514Box<Valtype>& Box<Valtype>::Round( int toDigit ) noexcept{
515 m_WidthX.Round( toDigit );
516 m_WidthY.Round( toDigit );
517 m_WidthZ.Round( toDigit );
518 return *this;
519}
520
521template<typename Valtype> inline
522bool Intersecting( const Box<Valtype>& box, const Sphere<Valtype>& sphere ) noexcept{
523 // not quite entirely correct
524 return Intersecting( box, sphere.ExBox() );
525}
526
527template<typename Valtype> inline
528bool Intersecting( const Box<Valtype>& boxA, const Box<Valtype>& boxB ) noexcept{
529 return Intersecting( boxA.m_WidthX, boxB.m_WidthX ) &&
530 Intersecting( boxA.m_WidthY, boxB.m_WidthY ) &&
531 Intersecting( boxA.m_WidthZ, boxB.m_WidthZ );
532}
533
534template<typename Valtype> constexpr
535inline bool operator==( const Box<Valtype>& b1, const Box<Valtype>& b2 ) noexcept{
536 return b1.m_WidthX == b2.m_WidthX && b1.m_WidthY == b2.m_WidthY && b1.m_WidthZ == b2.m_WidthZ;
537}
538
539template<typename Valtype> constexpr
540inline bool operator!=( const Box<Valtype>& b1, const Box<Valtype>& b2 ) noexcept{
541 return !(b1 == b2);
542}
543
544} // namespace spat
545
constexpr bool operator!=(const Interval< Valtype > &i1, const Interval< Valtype > &i2) noexcept
Interval operator.
Definition Interval.h:701
constexpr bool operator==(const Interval< Valtype > &i1, const Interval< Valtype > &i2) noexcept
Interval operator.
Definition Interval.h:696
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
An interval describes the area between two numbers. It is understood to contain the near one and exlu...
Definition Interval.h:42
Axis aligned box.
Definition Box.h:41
dim::Value< Dimension< 1, 0, 0 > > EdgeZ() const noexcept
Definition Box.h:422
auto Volume() const noexcept -> decltype(dim::Value< Dimension< 1, 0, 0 > >{} *dim::Value< Dimension< 1, 0, 0 > >{} *dim::Value< Dimension< 1, 0, 0 > >{})
Definition Box.h:433
const Position< dim::Value< Dimension< 1, 0, 0 > > > Far() const noexcept
Definition Box.h:329
Box & Inflate(dim::Value< Dimension< 1, 0, 0 > > dx, dim::Value< Dimension< 1, 0, 0 > > dy, dim::Value< Dimension< 1, 0, 0 > > dz) noexcept
Definition Box.h:488
Vector< Valtype > Diagonal() const noexcept
Definition Box.h:438
common::Interval< Valtype > m_WidthZ
height
Definition Box.h:46
bool Normal() const noexcept
Definition Box.h:311
common::Interval< Valtype > m_WidthX
width
Definition Box.h:44
dim::Value< Dimension< 1, 0, 0 > > NearY() const noexcept
Definition Box.h:339
dim::Value< Dimension< 1, 0, 0 > > NearX() const noexcept
Definition Box.h:334
Box & Deflate(dim::Value< Dimension< 1, 0, 0 > > dx, dim::Value< Dimension< 1, 0, 0 > > dy, dim::Value< Dimension< 1, 0, 0 > > dz) noexcept
Definition Box.h:497
dim::Value< Dimension< 1, 0, 0 > > FarX() const noexcept
Definition Box.h:349
Box & Union(const Box &b1, const Box &b2) noexcept
Definition Box.h:448
dim::Value< Dimension< 1, 0, 0 > > FarY() const noexcept
Definition Box.h:354
Box & Normalize() noexcept
Definition Box.h:316
Box & Expand(dim::Value< Dimension< 1, 0, 0 > > x, dim::Value< Dimension< 1, 0, 0 > > y, dim::Value< Dimension< 1, 0, 0 > > z) noexcept
Definition Box.h:475
dim::Value< Dimension< 1, 0, 0 > > EdgeY() const noexcept
Definition Box.h:411
const Position< dim::Value< Dimension< 1, 0, 0 > > > Near() const noexcept
Definition Box.h:324
Box() noexcept=default
Does not initialize the members.
dim::Value< Dimension< 1, 0, 0 > > NearZ() const noexcept
Definition Box.h:344
Box & Init() noexcept
Definition Box.h:303
dim::Value< Dimension< 1, 0, 0 > > FarZ() const noexcept
Definition Box.h:359
Box & Round(int toDigit) noexcept
Definition Box.h:514
Box & Move(const Vector< dim::Value< Dimension< 1, 0, 0 > > > &dPos) noexcept
Definition Box.h:506
dim::Value< Dimension< 1, 0, 0 > > EdgeX() const noexcept
Definition Box.h:400
Position< dim::Value< Dimension< 1, 0, 0 > > > Center() const noexcept
Definition Box.h:443
bool Includes(dim::Value< Dimension< 1, 0, 0 > > x, dim::Value< Dimension< 1, 0, 0 > > y, dim::Value< Dimension< 1, 0, 0 > > z) const noexcept
Definition Box.h:465
common::Interval< Valtype > m_WidthY
depth
Definition Box.h:45
Implements a 3D - position in cartesian coordinates.
Definition Position.h:46
Sphere with center and radius.
Definition Sphere.h:39
Implements a 3D - vector in cartesian coordinates.
Definition Vector.h:48