box.hpp 24.9 KB
Newer Older
1
2
3
#pragma once

#include <mln/core/experimental/point.hpp>
4
#include <mln/core/rangev3/mdindex.hpp>
Michaël Roynard's avatar
Indent    
Michaël Roynard committed
5
6

#include <array>
7
#include <type_traits>
8
#include <algorithm>
9

Michaël Roynard's avatar
Indent    
Michaël Roynard committed
10

11
12
13
14
15
16
17
18
19
20
21
22
23
namespace mln::experimental
{
  // Forward declaration
  template <class Impl>
  class _box;

  namespace impl
  {
    template <int D, typename T = int>
    struct _bstorage;

    template <int D, class T>
    struct _bref;
Michaël Roynard's avatar
Indent    
Michaël Roynard committed
24
  }; // namespace impl
25
26
27
28
29
30
31
32
33
34
35
36
37
38

  /******************************************/
  /****   Box Types and Types Aliases    ****/
  /******************************************/

  using Box         = _box<impl::_bstorage<-1, int>>;
  using ConstBoxRef = _box<impl::_bref<-1, const int>>;

  template <int dim>
  using ndbox = _box<impl::_bstorage<dim, int>>;
  template <int dim>
  using ndboxref = _box<impl::_bref<dim, const int>>;


Michaël Roynard's avatar
Indent    
Michaël Roynard committed
39
40
41
  using box1d           = ndbox<1>;
  using box2d           = ndbox<2>;
  using box3d           = ndbox<3>;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  using const_box1d_ref = ndboxref<1>;
  using const_box2d_ref = ndboxref<2>;
  using const_box3d_ref = ndboxref<3>;


  /******************************************/
  /****          Box Interface          *****/
  /******************************************/

  /// \brief Box Public Interface shared by all "box" types
  template <class Impl>
  class _box : private Impl
  {
  public:
    template <class _i>
    friend class _box;

    template <int, class>
    friend struct impl::_bstorage;

    template <int, class>
    friend struct impl::_bref;


    using Impl::Impl;
    using Impl::ndim;
Michaël Roynard's avatar
Indent    
Michaël Roynard committed
68
    using typename Impl::coord_type;
69
    using point_type = ndpoint<ndim, coord_type>;
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

    /// Observers
    /// \{

    /// \brief Return the number of elements in the box
    constexpr std::size_t size() const noexcept;

    /// \brief Return the number of elements in the k-th dimension of the box
    constexpr int size(int k) const noexcept;

    /// \brief Return true if the box is empty
    constexpr bool empty() const noexcept;

    /// \brief Returns the width of the box
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 1>>
    constexpr int width() const noexcept
    {
      return size(0);
    }

    /// \brief Returns the height of the box
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 2>>
    constexpr int height() const noexcept
    {
      return size(1);
    }

    /// \brief Returns the depth of the box
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 3>>
    constexpr int depth() const noexcept
    {
      return size(2);
    }

Edwin Carlinet's avatar
Edwin Carlinet committed
104
105
    using Impl::cursor;
    using Impl::backward_cursor;
106
107
108
109
    using Impl::begin_cursor;
    using Impl::end_cursor;
    using Impl::rbegin_cursor;
    using Impl::rend_cursor;
110
111


112
    template <int d = ndim, class = std::enable_if_t<d != -1>>
113
    auto rows() const
114
115
116
117
118
    {
      return this->Impl::rows();
    }

    template <int d = ndim, class = std::enable_if_t<d != -1>>
119
    auto rrows() const
120
    {
121
      return this->Impl::rrows();
122
123
124
    }


125
126
127
    /// \brief Returns the top-left corner point
    using Impl::tl;

Edwin Carlinet's avatar
Edwin Carlinet committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    /// \brief Returns the x coordinate of the top-left corner point
    constexpr int x() const noexcept { return this->__begin(0); }

    /// \brief Returns the y coordinate of the top-left corner point
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 2>>
    constexpr int y() const noexcept { return this->__begin(1); }

    /// \brief Returns the z coordinate of the top-left corner point
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 3>>
    constexpr int z() const noexcept { return this->__begin(2); }

    /// \brief Returns the w coordinate of the top-left corner point
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 4>>
    constexpr int w() const noexcept { return this->__begin(3); }


144
145
146
147
148
149
150
151
152
    /// \brief Returns the k-th coordinate of the top-left corner point
    constexpr int tl(int k) const noexcept { return this->__begin(k); }

    /// \brief Returns the k-th coordinate of the bottom-right corner point (past-the-end)
    constexpr int br(int k) const noexcept { return this->__end(k); }

    /// \brief Returns the bottom-right (past-the-end) corner point
    using Impl::br;

Michaël Roynard's avatar
Indent    
Michaël Roynard committed
153
154
155
    /// \brief Returns the shape of the domain
    using Impl::shape;

156
157
158
    /// \brief Returns the number of dimensions
    using Impl::dim;

Michaël Roynard's avatar
Indent    
Michaël Roynard committed
159
160
161
    /// \brief Returns the size of each dimension
    using Impl::extents;

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    /// \brief Returns a pointer to the coordinate array
    using Impl::data;
    /// \}

    /// Modifiers
    /// \{

    /// \brief Set the size of the box in the k-th dimension to a new size (does not move top-left corner)
    constexpr void set_size(int k, int size) noexcept { this->__end(k) = this->__begin(k) + size; }

    /// \brief Set the width of the box to a new width (does not move top-left corner)
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 1>>
    constexpr void set_width(int width) noexcept
    {
      this->set_size(0, width);
    }

    /// \brief Set the height of the box to a new height (does not move top-left corner)
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 2>>
    constexpr void set_height(int height) noexcept
    {
      this->set_size(1, height);
    }

    /// \brief Set the depth of the box to a new depth (does not move top-left corner)
    template <int d = ndim, class = std::enable_if_t<d == -1 || d >= 3>>
    constexpr void set_depth(int depth) noexcept
    {
      this->set_size(2, depth);
    }

    /// \brief Inflate (or deflate) the box
    constexpr void inflate(int k) noexcept;
    /// \}


    /// Set operations
    /// \{
    template <class _i>
    constexpr bool operator==(const _box<_i>& B) const noexcept;

    template <class _i>
    constexpr bool operator!=(const _box<_i>& B) const noexcept;

    template <class _p>
    constexpr bool has(_point<_p> x) const noexcept;

    template <class _i>
    constexpr bool includes(const _box<_i>& B) const noexcept;

    template <class _i>
    constexpr bool intersects(const _box<_i>& B) const noexcept;

    /// \brief Clip the box to a given region (compute the intersection)
    template <class _i>
    constexpr void clip(const _box<_i>& B) noexcept;
    /// \}

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    /// Projection operations
    /// \{

    /// \brief Clamp a point to box boundaries
    ///
    /// yᵢ = clamp(xᵢ, aᵢ, bᵢ)
    /// \postcondition b.has(p)
    template <class _p>
    point_type clamp(_point<_p> x) const noexcept;


    /// \brief Project a point into a box by mirroring
    ///
    /// yᵢ = clamp(xᵢ, aᵢ, bᵢ)
    /// \postcondition box.has(p)
    template <class _p>
    point_type mirror(_point<_p> x) const noexcept;

    /// \brief Project a point into a box by mirroring
    /// \postcondition b.has(p)
    template <class _p>
    point_type periodize(_point<_p> x) const noexcept;
    /// \}


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

  private:
    template <int K>
    constexpr int __size() const noexcept;
  };


  /******************************************/
  /****          Implementation          ****/
  /******************************************/


  namespace impl
  {
    template <class _i1, class _i2>
    using is_impl_compatible_with =
        std::integral_constant<bool, (_i1::ndim == -1 || _i2::ndim == -1 || _i1::ndim == _i2::ndim) &&
                                         std::is_convertible_v<typename _i1::coord_type, typename _i2::coord_type>>;


    /// Storage for a box
    template <int D, typename T>
267
    struct _bstorage : mln::ranges::mdindex_facade<D, T, ndpoint<D, T>, _bstorage<D, T>>
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
    {
    private:
      using self_t     = _bstorage;
      using point_type = ndpoint<D, T>;

    public:
      static constexpr int ndim = D;
      using coord_type          = T;

      constexpr void _zeros() noexcept
      {
        for (int k = 0; k < D; ++k)
        {
          m_begin[k] = 0;
          m_end[k]   = 0;
        }
      }

      constexpr _bstorage() = default;

      template <class _i>
      constexpr _bstorage(const _box<_i>& b) noexcept
      {
        static_assert(is_impl_compatible_with<_i, self_t>::value,
                      "Number of dimension and/or coordinate type mismatch.");
        assert(this->dim() == b.dim() && "Incompatible number of dimensions");

        for (int i = 0; i < D; ++i)
        {
          m_begin[i] = b.__begin(i);
          m_end[i]   = b.__end(i);
        }
      }


      constexpr _bstorage(ConstPointRef p1, ConstPointRef p2)
      {
        assert(p1.dim() == D && "Invalid number of dimensions");
        assert(p2.dim() == D && "Invalid number of dimensions");

        bool empty = false;
        for (int k = 0; k < D; ++k)
        {
          if (p1[k] < p2[k])
          {
            m_begin[k] = p1[k];
            m_end[k]   = p2[k];
          }
          else if (p1[k] > p2[k])
          {
            m_begin[k] = p2[k];
            m_end[k]   = p1[k];
          }
          else
          {
            empty = true;
            break;
          }
        }
        if (empty)
          _zeros();
      }

      template <int d = D, class = std::enable_if_t<d == 1>>
      constexpr _bstorage(int width)
        : m_begin{0}
        , m_end{width}
      {
        assert(width >= 0);
      }

Edwin Carlinet's avatar
Edwin Carlinet committed
339
340
341
342
343
344
345
346
347
348

      constexpr _bstorage(int x, int width) requires(D == 1)
        : m_begin{x}
        , m_end{x + width}
      {
        assert(width >= 0);
      }


      constexpr _bstorage(int width, int height) requires (D == 2)
349
350
351
352
353
354
        : m_begin{0, 0}
        , m_end{width, height}
      {
        assert(width >= 0 && height >= 0);
      }

Edwin Carlinet's avatar
Edwin Carlinet committed
355
356
357
358
359
360
361
362
      constexpr _bstorage(int x, int y, int width, int height) requires(D == 2)
        : m_begin{x, y}
        , m_end{x + width, y + height}
      {
        assert(width >= 0 && height >= 0);
      }


363
364
365
366
367
368
369
370
      template <int d = D, class = std::enable_if_t<d == 3>>
      constexpr _bstorage(int width, int height, int depth)
        : m_begin{0, 0, 0}
        , m_end{width, height, depth}
      {
        assert(width >= 0 && height >= 0 && depth >= 0);
      }

Edwin Carlinet's avatar
Edwin Carlinet committed
371
372
373
374
375
376
377
378
379
380
      template <int d = D, class = std::enable_if_t<d == 3>>
      constexpr _bstorage(int x, int y, int z, int width, int height, int depth)
        : m_begin{x, y, z}
        , m_end{x + width, y + height, z + depth}
      {
        assert(width >= 0 && height >= 0 && depth >= 0);
      }


      constexpr _bstorage(int width, int height, int depth, int duration) requires (D == 4)
381
382
383
384
385
386
        : m_begin{0, 0, 0, 0}
        , m_end{width, height, depth, duration}
      {
        assert(width >= 0 && height >= 0 && depth >= 0 && duration >= 0);
      }

Edwin Carlinet's avatar
Edwin Carlinet committed
387
388
389
390
391
392
393
394
395
      template <int d = D, class = std::enable_if_t<d == 4>>
      constexpr _bstorage(int x, int y, int z, int w, int width, int height, int depth, int duration)
        : m_begin{x, y, z, w}
        , m_end{x + width, y + height, z + depth, w + duration}
      {
        assert(width >= 0 && height >= 0 && depth >= 0 && duration >= 0);
      }


396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432

      static constexpr int dim() noexcept { return D; }
      constexpr const T*   data() const noexcept { return m_begin.data(); }
      constexpr T*         data() noexcept { return m_begin.data(); }

      constexpr const T* __begin() const noexcept { return m_begin.data(); }
      constexpr T*       __begin() noexcept { return m_begin.data(); }
      constexpr const T* __end() const noexcept { return m_end.data(); }
      constexpr T*       __end() noexcept { return m_end.data(); }

      constexpr T __begin(int k) const noexcept
      {
        assert(k < D);
        return m_begin[k];
      }
      constexpr T __end(int k) const noexcept
      {
        assert(k < D);
        return m_end[k];
      }
      constexpr T& __begin(int k) noexcept
      {
        assert(k < D);
        return m_begin[k];
      }
      constexpr T& __end(int k) noexcept
      {
        assert(k < D);
        return m_end[k];
      }


      constexpr point_type  tl() const noexcept { return m_begin; }
      constexpr point_type& tl() noexcept { return m_begin; }
      constexpr point_type  br() const noexcept { return m_end; }
      constexpr point_type& br() noexcept { return m_end; }

433
      constexpr auto shape() const noexcept { return br() - tl(); }
Michaël Roynard's avatar
Indent    
Michaël Roynard committed
434
435
436
437
438
439
440
441
442
443
      constexpr auto extents() const noexcept
      {
        auto pmin = tl();
        auto pmax = br();
        auto ret  = std::array<std::size_t, D>{};
        for (int i = 0; i < D; i++)
          ret[i] = pmax[i] - pmin[i];
        return ret;
      }

444
445
446
447
      using base = mln::ranges::mdindex_facade<D, T, point_type, _bstorage<D, T>>;
      typename base::cursor          begin_cursor() const { return {m_begin.data(), m_end.data(), false}; }
      typename base::backward_cursor rbegin_cursor() const { return {m_begin.data(), m_end.data(), true}; }

448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566

      point_type m_begin = {};
      point_type m_end   = {};
    };


    /// Storage for a dynamic box
    template <typename T>
    struct _bstorage<-1, T>
    {
      static constexpr int ndim = -1;
      using coord_type          = T;

      constexpr void _zeros() noexcept
      {
        for (int i = 0; i < 8; i++)
          m_coords[i] = 0;
      }

      constexpr _bstorage() = default;

      // Converting constructors
      template <class _i>
      constexpr _bstorage(const _box<_i>& b)
        : m_dim(b.dim())
      {
        assert(m_dim <= 4 && "Cannot handle dyn dimension greater than 4");
        for (int i = 0; i < m_dim; ++i)
        {
          m_coords[i]         = b.__begin(i);
          m_coords[m_dim + i] = b.__end(i);
        }
      }

      constexpr _bstorage(T w) noexcept
        : m_dim{1}
        , m_coords{0, w, 0, 1, 0, 1, 0, 1}
      {
      }
      constexpr _bstorage(T w, T h) noexcept
        : m_dim{2}
        , m_coords{0, 0, w, h, 0, 1, 0, 1}
      {
      }
      constexpr _bstorage(T w, T h, T depth) noexcept
        : m_dim{3}
        , m_coords{0, 0, 0, w, h, depth, 0, 1}
      {
      }
      constexpr _bstorage(T w, T h, T depth, T duration) noexcept
        : m_dim{4}
        , m_coords{0, 0, 0, 0, w, h, depth, duration}
      {
      }
      constexpr _bstorage(ConstPointRef tl, ConstPointRef br) noexcept
        : m_dim{tl.dim()}
        , m_coords{}
      {
        assert(tl.dim() == br.dim());

        bool empty = false;
        for (int i = 0; i < m_dim; i++)
        {
          if (tl[i] < br[i])
          {
            m_coords[i]         = tl[i];
            m_coords[m_dim + i] = br[i];
          }
          else if (br[i] < tl[i])
          {
            m_coords[i]         = tl[i];
            m_coords[m_dim + i] = br[i];
          }
          else
          {
            empty = true;
            break;
          }
        }
        if (empty)
          _zeros();
      }

      constexpr int      dim() const noexcept { return m_dim; }
      constexpr T*       data() noexcept { return m_coords; }
      constexpr const T* data() const noexcept { return m_coords; }

      constexpr const T* __begin() const noexcept { return m_coords; }
      constexpr T*       __begin() noexcept { return m_coords; }
      constexpr const T* __end() const noexcept { return m_coords + m_dim; }
      constexpr T*       __end() noexcept { return m_coords + m_dim; }


      constexpr T __begin(int k) const noexcept
      {
        assert(k < m_dim);
        return m_coords[k];
      }
      constexpr T __end(int k) const noexcept
      {
        assert(k < m_dim);
        return m_coords[m_dim + k];
      }
      constexpr T& __begin(int k) noexcept
      {
        assert(k < m_dim);
        return m_coords[k];
      }
      constexpr T& __end(int k) noexcept
      {
        assert(k < m_dim);
        return m_coords[m_dim + k];
      }

      constexpr ConstPointRef tl() const noexcept { return {m_dim, m_coords}; }
      constexpr PointRef      tl() noexcept { return {m_dim, m_coords}; }
      constexpr ConstPointRef br() const noexcept { return {m_dim, m_coords + m_dim}; }
      constexpr PointRef      br() noexcept { return {m_dim, m_coords + m_dim}; }

Michaël Roynard's avatar
Indent    
Michaël Roynard committed
567
568
569
570
571
572
573
574
575
576
577
      constexpr auto shape() const noexcept { return tl() - br(); }
      auto           extents() const noexcept
      {
        auto pmin = tl();
        auto pmax = br();
        auto ret  = std::vector<std::size_t>{m_dim, 0};
        for (int i = 0; i < m_dim; i++)
          ret[i] = pmax[i] - pmin[i];
        return ret;
      }

578
579
580
      constexpr int begin() const noexcept { return 0; }
      constexpr int end() const noexcept { return 1; }

581
582
583
584
      void begin_cursor() const {}
      void rbegin_cursor() const {};
      void end_cursor() const {}
      void rend_cursor() const {};
Edwin Carlinet's avatar
Edwin Carlinet committed
585
586
      using cursor = void;
      using backward_cursor = void;
587
588


589
590
591
592
593
594
      int m_dim       = 0;
      T   m_coords[8] = {0}; // (x1,y1,z1,w1) -- (x2,y2,z2,w2)
    };

    /// Reference to box wrapper
    template <int D, class T>
595
596
    struct _bref
      : mln::ranges::mdindex_facade<D, std::remove_const_t<T>, ndpoint<D, std::remove_const_t<T>>, _bref<D, T>>
597
    {
598
599
600
      using base =
          mln::ranges::mdindex_facade<D, std::remove_const_t<T>, ndpoint<D, std::remove_const_t<T>>, _bref<D, T>>;

601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
      static constexpr int ndim = D;
      using coord_type          = T;

      template <class Impl>
      constexpr _bref(const _box<Impl>& b) noexcept
        : m_coords{b.data()}
      {
        assert(b.dim() == D && "Incompatible number of dimension.");
      }

      constexpr int dim() const noexcept { return D; }
      constexpr T*  data() const noexcept { return m_coords; }

      constexpr T& __begin(int k) const noexcept { return assert(k < D), m_coords[k]; }
      constexpr T& __end(int k) const noexcept { return assert(k < D), m_coords[D + k]; }

Michaël Roynard's avatar
Indent    
Michaël Roynard committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
      constexpr ndpointref<D, T> tl() const noexcept { return {m_coords}; }
      constexpr ndpointref<D, T> br() const noexcept { return {m_coords + D}; }

      constexpr auto shape() const noexcept { return tl() - br(); }
      constexpr auto extents() const noexcept
      {
        auto pmin = tl();
        auto pmax = br();
        auto ret  = std::array<std::size_t, D>{};
        for (int i = 0; i < D; i++)
          ret[i] = pmax[i] - pmin[i];
        return ret;
      }

631
632
      typename base::cursor          begin_cursor() const { return {m_coords, m_coords + D, false}; }
      typename base::backward_cursor rbegin_cursor() const { return {m_coords, m_coords + D, true}; }
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666

      T* m_coords;
    };

    /// Reference to box wrapper
    template <class T>
    struct _bref<-1, T>
    {
      static constexpr int ndim = -1;
      using coord_type          = T;

      template <class Impl>
      constexpr _bref(const _box<Impl>& b) noexcept
        : m_dim{b.dim()}
        , m_coords(b.data())
      {
      }

      constexpr int dim() const noexcept { return m_dim; }
      constexpr T*  data() const noexcept { return m_coords; }

      constexpr T& __begin(int k) const noexcept
      {
        assert(k < m_dim);
        return m_coords[k];
      }
      constexpr T& __end(int k) const noexcept
      {
        assert(k < m_dim);
        return m_coords[m_dim + k];
      }
      constexpr ConstPointRef tl() const noexcept { return {m_dim, m_coords}; }
      constexpr ConstPointRef br() const noexcept { return {m_dim, m_coords + m_dim}; }

Michaël Roynard's avatar
Indent    
Michaël Roynard committed
667
668
669
670
671
672
673
674
675
676
677
      constexpr auto shape() const noexcept { return tl() - br(); }
      auto           extents() const noexcept
      {
        auto pmin = tl();
        auto pmax = br();
        auto ret  = std::vector<std::size_t>{m_dim, 0};
        for (int i = 0; i < m_dim; i++)
          ret[i] = pmax[i] - pmin[i];
        return ret;
      }

678
679
      int begin() const { return 0; }
      int end() const { return 1; }
680
681
682
683
      void begin_cursor() const {}
      void rbegin_cursor() const {};
      void end_cursor() const {}
      void rend_cursor() const {};
Edwin Carlinet's avatar
Edwin Carlinet committed
684
685
      using cursor = void;
      using backward_cursor = void;
686

687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822


      int m_dim;
      T*  m_coords;
    };

  } // namespace impl

  template <class Impl>
  constexpr void _box<Impl>::inflate(int k) noexcept
  {
    auto&& pmin = this->tl();
    auto&& pmax = this->br();

    bool empty = false;
    for (int i = 0; i < this->dim(); ++i)
    {
      pmin[i] -= k;
      pmax[i] += k;
      if (pmax[i] <= pmin[i])
        empty = true;
    }
    if (empty)
      this->_zeros();
  }

  template <class Impl>
  constexpr int _box<Impl>::size(int k) const noexcept
  {
    assert(k < this->dim() && "Invalid dimension");
    return this->__end(k) - this->__begin(k);
  }

  template <class Impl>
  constexpr std::size_t _box<Impl>::size() const noexcept
  {
    std::size_t sz = 1;
    for (int k = 0; k < this->dim(); ++k)
      sz *= (this->__end(k) - this->__begin(k));
    if (sz < 0)
      sz = 0;
    return sz;
  }

  template <class Impl>
  constexpr bool _box<Impl>::empty() const noexcept
  {
    for (int k = 0; k < this->dim(); ++k)
      if (this->__begin(k) == 0 && this->__end(k) == 0)
        return true;
    return this->dim() == 0;
  }


  template <class Impl>
  template <class _p>
  constexpr bool _box<Impl>::has(_point<_p> x) const noexcept
  {
    assert(x.dim() == this->dim() && "Point dimension mismatch.");

    for (int k = 0; k < this->dim(); ++k)
      if (x[k] < this->__begin(k) || x[k] >= this->__end(k))
        return false;
    return true;
  }

  template <class Impl>
  template <class _i>
  constexpr bool _box<Impl>::operator==(const _box<_i>& B) const noexcept
  {
    assert(this->dim() == B.dim() && "Boxes must be of the same dimensions");

    for (int k = 0; k < this->dim(); ++k)
      if (B.__begin(k) != this->__begin(k) || B.__end(k) != this->__end(k))
        return false;
    return true;
  }

  template <class Impl>
  template <class _i>
  constexpr bool _box<Impl>::operator!=(const _box<_i>& B) const noexcept
  {
    return !(*this == B);
  }


  template <class Impl>
  template <class _i>
  constexpr bool _box<Impl>::includes(const _box<_i>& B) const noexcept
  {
    assert(this->dim() == B.dim() && "Boxes must be of the same dimensions");

    for (int k = 0; k < this->dim(); ++k)
      if (B.__begin(k) < this->__begin(k) || B.__end(k) > this->__end(k))
        return false;
    return true;
  }

  template <class Impl>
  template <class _i>
  constexpr bool _box<Impl>::intersects(const _box<_i>& B) const noexcept
  {
    assert(this->dim() == B.dim() && "Boxes must be of the same dimensions");

    for (int k = 0; k < this->dim(); ++k)
    {
      bool disjoint = this->__end(k) <= B.__begin(k) || B.__end(k) <= this->__begin(k);
      if (disjoint)
        return false;
    }
    return true;
  }

  template <class Impl>
  template <class _i>
  constexpr void _box<Impl>::clip(const _box<_i>& B) noexcept
  {
    assert(this->dim() == B.dim() && "Boxes must be of the same dimensions");

    bool empty = false;
    for (int k = 0; k < this->dim(); ++k)
    {
      if (this->__begin(k) < B.__begin(k))
        this->__begin(k) = B.__begin(k);
      if (this->__end(k) > B.__end(k))
        this->__end(k) = B.__end(k);
      if (this->__end(k) <= this->__begin(k))
      {
        empty = true;
        break;
      }
    }
    if (empty)
      this->_zeros();
  }

823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
  template <class Impl>
  template <class _p>
  auto _box<Impl>::clamp(_point<_p> x) const noexcept -> point_type
  {
    assert(this->dim() == x.dim() && "Objects must be of the same dimensions");
    assert(!this->empty() && "Cannot clamp to an empty box.");

    point_type q;
    for (int k = 0; k < this->dim(); ++k)
      q[k] = std::clamp(x[k], this->__begin(k), this->__end(k) - 1);
    return q;
  }

  template <class Impl>
  template <class _p>
  auto _box<Impl>::mirror(_point<_p> x) const noexcept -> point_type
  {
    assert(this->dim() == x.dim() && "Objects must be of the same dimensions");
    assert(!this->empty() && "Cannot clamp to an empty box.");

    point_type q;
    for (int k = 0; k < this->dim(); ++k)
    {
      auto a = this->__begin(k);
      auto b = this->__end(k);
      auto n = b - a;
      auto s = 2 * n;
      auto v = x[k] - a;
      v      = v % s;
      v      = v < 0 ? v + s : v; // ensure v>0
      v      = v < n ? v : s - v - 1;
      q[k]   = a + v;
    }
    return q;
  }

  template <class Impl>
  template <class _p>
  auto _box<Impl>::periodize(_point<_p> x) const noexcept -> point_type
  {
    assert(this->dim() == x.dim() && "Objects must be of the same dimensions");
    assert(!this->empty() && "Cannot clamp to an empty box.");

    point_type q;
    for (int k = 0; k < this->dim(); ++k)
    {
      auto a = this->__begin(k);
      auto b = this->__end(k);
      auto n = b - a;
      auto v = x[k] - a;
      v      = v % n;
      v      = v < 0 ? v + n : v;
      q[k]   = a + (v % n);
    }
    return q;
  }

880
881
882
883
884
} // namespace mln::experimental


#ifdef PYLENE_CONCEPT_TS_ENABLED
// Specialization of std::common_reference
885
namespace concepts
886
887
888
889
890
891
892
893
894
895
{
  template <class UImpl, class VImpl, template <class> class TQual, template <class> class UQual>
  struct basic_common_reference<mln::experimental::_box<UImpl>, mln::experimental::_box<VImpl>, TQual, UQual>
  {
    static_assert((UImpl::ndim == -1) || (VImpl::ndim == -1) || (UImpl::ndim == UImpl::ndim),
                  "Incompatible number of dimensions.");
    using type = mln::experimental::ndbox<(UImpl::ndim == VImpl::ndim) ? UImpl::ndim : -1>;
  };
}
#endif
896
897
898

namespace ranges
{
899
900
901
  template <typename Impl>
  struct range_cardinality<mln::experimental::_box<Impl>, void> :
    std::integral_constant<cardinality, finite>
902
  {
903
  };
904
905
}