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

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

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

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

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifdef PYLENE_CONCEPT_TS_ENABLED
#include <stl2/type_traits.hpp>
#endif

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
27
  }; // namespace impl
28
29
30
31
32
33
34
35
36
37
38
39
40
41

  /******************************************/
  /****   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
42
43
44
  using box1d           = ndbox<1>;
  using box2d           = ndbox<2>;
  using box3d           = ndbox<3>;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  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
71
    using typename Impl::coord_type;
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139


    /// 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);
    }

    /// \brief Returns the begin iterator of the range
    template <int d = ndim, class = std::enable_if_t<d != -1>>
    auto begin() const
    {
      return Impl::begin();
    }

    /// \brief Returns the end iterator of the range
    template <int d = ndim, class = std::enable_if_t<d != -1>>
    auto end() const
    {
      return Impl::end();
    }

    /// \brief Returns the reversed range
    template <int d = ndim, class = std::enable_if_t<d != -1>>
    auto reversed() const
    {
      return this->Impl::reversed();
    }

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

    /// \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
140
141
142
    /// \brief Returns the shape of the domain
    using Impl::shape;

143
144
145
    /// \brief Returns the number of dimensions
    using Impl::dim;

Michaël Roynard's avatar
Indent    
Michaël Roynard committed
146
147
148
    /// \brief Returns the size of each dimension
    using Impl::extents;

149
150
151
152
153
154
155
156
157
158
159
160
161
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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
    /// \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;
    /// \}


  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>
    struct _bstorage : ranges::details::multi_indices_facade<D, _bstorage<D, T>, false /* Col-Major */>
    {
    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);
      }

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

      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);
      }

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


      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; }

Michaël Roynard's avatar
Indent    
Michaël Roynard committed
362
363
364
365
366
367
368
369
370
371
372
      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;
      }

373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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
      constexpr point_type __from() const noexcept { return m_begin; }
      constexpr point_type __to() const noexcept { return m_end; }

      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
494
495
496
497
498
499
500
501
502
503
504
      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;
      }

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
      constexpr int begin() const noexcept { return 0; }
      constexpr int end() const noexcept { return 1; }

      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>
    struct _bref : ranges::details::multi_indices_facade<D, _bref<D, T>, false /* Col-Major */>
    {
      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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
      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;
      }


547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
      constexpr ndpoint<D, std::remove_const_t<T>> __from() const noexcept { return tl(); }
      constexpr ndpoint<D, std::remove_const_t<T>> __to() const noexcept { return br(); }

      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
583
584
585
586
587
588
589
590
591
592
593
      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;
      }

594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
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
      int begin() const { return 0; }
      int end() const { return 1; }


      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();
  }

} // namespace mln::experimental


#ifdef PYLENE_CONCEPT_TS_ENABLED
// Specialization of std::common_reference
STL2_OPEN_NAMESPACE
{
  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>;
  };
}
STL2_CLOSE_NAMESPACE
#endif