Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Point.h « polygon « include - github.com/Ultimaker/CuraEngine.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 119b2ce77fa1bbe01a0138a272748cf1c3bcbeac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
68
69
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
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
140
141
142
143
144
145
146
147
148
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


#ifndef CURAENGINE_POINT_H
#define CURAENGINE_POINT_H

#include <array>
#include <type_traits>

#include "polygon/Unit.h"
#include "utils/concepts/Polygon.h"

#include <gsl/gsl-lite.hpp>
#include <range/v3/all.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/view/zip.hpp>
#include <range/v3/view/zip_with.hpp>

namespace cura::poly
{

enum struct Axis
{
    X = 0,
    Y = 1,
    Z = 2
};

template<class Tp, std::size_t Nm = 2> // TODO: figure out how to set a max size (of 3 x,y,z)
class Point
{
public:
    // types
    using value_type = Tp;
    using pointer = value_type*;
    using const_pointer = const value_type*;
    using reference = value_type&;
    using const_reference = const value_type&;
    using iterator = value_type*;
    using const_iterator = const value_type*;
    using size_type = std::size_t;
    using difference_type = std::ptrdiff_t;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    template<typename... Ts>
    constexpr explicit Point(Ts&&... args) noexcept : data_{ std::forward<Ts>(args)... }
    {
    }
    //
    //    constexpr explicit Point(ranges::any_view<Tp> rng) noexcept
    //    {
    //        for (const auto& [idx, point] : rng | ranges::views::enumerate)
    //        {
    //            data_[idx] = point;
    //        }
    //    }


    constexpr void fill(const value_type& value)
    {
        std::fill_n(begin(), size(), value);
    }

    constexpr void swap(Point& other) noexcept(std::is_nothrow_swappable_v<Point&>)
    {
        std::swap_ranges(begin(), end(), other.begin());
    }

    // Iterators.
    [[nodiscard]] constexpr iterator begin() noexcept
    {
        return std::begin(data_);
    }

    [[nodiscard]] constexpr const_iterator begin() const noexcept
    {
        return std::begin(data_);
    }

    [[nodiscard]] constexpr iterator end() noexcept
    {
        return std::end(data_);
    }

    [[nodiscard]] constexpr const_iterator end() const noexcept
    {
        return std::end(data_);
    }

    [[nodiscard]] constexpr reverse_iterator rbegin() noexcept
    {
        return std::rbegin(data_);
    }

    [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept
    {
        return std::rbegin(data_);
    }

    [[nodiscard]] constexpr reverse_iterator rend() noexcept
    {
        return std::rend(data_);
    }

    [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept
    {
        return std::rend(data_);
    }

    [[nodiscard]] constexpr const_iterator cbegin() const noexcept
    {
        return std::cbegin(data_);
    }

    [[nodiscard]] constexpr const_iterator cend() const noexcept
    {
        return std::cend(data_);
    }

    [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept
    {
        return std::crbegin(data_);
    }

    [[nodiscard]] const_reverse_iterator crend() const noexcept
    {
        return std::crend(data_);
    }

    // Capacity.
    [[nodiscard]] constexpr size_type size() const noexcept
    {
        return Nm;
    }

    [[nodiscard]] constexpr size_type max_size() const noexcept
    {
        return Nm;
    }

    [[nodiscard]] constexpr bool empty() const noexcept
    {
        return size() == 0;
    }

    // Element access.
    [[nodiscard]] constexpr reference operator[](size_type idx) noexcept
    {
        gsl_Expects(idx < size());
        return data_[idx];
    }

    [[nodiscard]] constexpr reference operator[](const Axis& axis) noexcept
    {
        return data_[static_cast<size_type>(axis)];
    }

    [[nodiscard]] constexpr const_reference operator[](size_type idx) const noexcept
    {
        gsl_Expects(idx < Nm);
        return data_[idx];
    }

    [[nodiscard]] constexpr const_reference operator[](const Axis& axis) const noexcept
    {
        return data_[static_cast<size_type>(axis)];
    }

    [[nodiscard]] constexpr reference at(size_type idx)
    {
        gsl_Expects(idx < Nm);
        return data_.at(idx);
    }

    [[nodiscard]] constexpr reference at(const Axis& axis)
    {
        return data_.at(static_cast<size_type>(axis));
    }

    [[nodiscard]] constexpr const_reference at(size_type idx) const
    {
        gsl_Expects(idx < Nm);
        return data_.at(idx);
    }

    [[nodiscard]] constexpr const_reference at(const Axis& axis) const
    {
        return data_.at(static_cast<size_type>(axis));
    }

    [[nodiscard]] constexpr reference front() noexcept
    {
        return data_.front();
    }

    [[nodiscard]] constexpr const_reference front() const noexcept
    {
        return data_.front();
    }

    [[nodiscard]] constexpr reference back() noexcept
    {
        return data_.back();
    }

    [[nodiscard]] constexpr const_reference back() const noexcept
    {
        return data_.back();
    }

    [[nodiscard]] constexpr pointer data() noexcept
    {
        return data_.data();
    }

    [[nodiscard]] const_pointer data() const noexcept
    {
        return data_.data();
    }

    // Arithmetic operations
    [[nodiscard]] constexpr Point operator+(const Vector auto& other) const
    {
        // FIXME: initialize Point with a rng
        Point<Tp, Nm> p;
        for (const auto& [idx, point] : ranges::views::zip_with([](const auto& lhs, const auto& rhs) { return lhs + rhs; }, *this, other) | ranges::views::enumerate)
        {
            p[idx] = point;
        }
        return p;
    }

    [[nodiscard]] constexpr Point operator+(const Scalar auto& magnitude) const
    {
        return *this | ranges::actions::transform([magnitude](const auto& lhs) { return lhs + magnitude; }) | ranges::to<std::array<Tp, Nm>>;
    }

private:
    std::array<Tp, Nm> data_;
};

// TODO: Add free function swap

// Length unit specialization of Point
template<Scalar Tp, std::size_t Nm = 2>
using Position = Point<units::isq::si::length<unit::base_length, Tp>, Nm>;

// CTAD
template<typename Tp, typename... Up>
Point(Tp, Up...) -> Point<std::enable_if_t<(std::is_same_v<Tp, Up> && ...), Tp>, 1 + sizeof...(Up)>;

// TODO: CTAD for the Position type

} // namespace cura::poly

#endif // CURAENGINE_POINT_H