Skip to content

Commit 06e5f37

Browse files
committed
Reflecting range types
1 parent cf15b42 commit 06e5f37

File tree

4 files changed

+84
-107
lines changed

4 files changed

+84
-107
lines changed

source/Ranges/TRange.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
///
2+
/// Langulus::Math
3+
/// Copyright (c) 2014 Dimo Markov <[email protected]>
4+
/// Part of the Langulus framework, see https://langulus.com
5+
///
6+
/// SPDX-License-Identifier: GPL-3.0-or-later
7+
///
8+
#include "TRange.inl"
9+
10+
11+
namespace Langulus::Math
12+
{
13+
14+
/// Combines S and T... to form a vector type, and then a range from it
15+
/// @tparam S - size of the vector
16+
template<Count S>
17+
struct RangeTypeGenerator {
18+
template<class...T>
19+
static void Register(Types<T...>&&) {
20+
(((void) MetaOf<TRange<TVector<T, S>>>()), ...);
21+
}
22+
};
23+
24+
/// Register all commonly used vector types and constants, so they can be
25+
/// instantiated from scripts
26+
void RegisterRanges() {
27+
using AllTypes = Types<
28+
::std::uint8_t, ::std::uint16_t, ::std::uint32_t, ::std::uint64_t,
29+
::std::int8_t, ::std::int16_t, ::std::int32_t, ::std::int64_t,
30+
Float, Double
31+
>;
32+
33+
RangeTypeGenerator<1>::Register(AllTypes {});
34+
RangeTypeGenerator<2>::Register(AllTypes {});
35+
RangeTypeGenerator<3>::Register(AllTypes {});
36+
RangeTypeGenerator<4>::Register(AllTypes {});
37+
}
38+
39+
} // namespace Langulus::Math

source/Ranges/TRange.hpp

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
namespace Langulus::Math
1616
{
1717

18+
LANGULUS_API(MATH) extern void RegisterRanges();
19+
1820
TEMPLATE() struct TRange;
1921

2022
using Range1f = TRange<Vec1f>;
@@ -180,57 +182,42 @@ namespace Langulus::Math
180182
constexpr TRange(const PointType&, const PointType&) noexcept;
181183
constexpr TRange(const MemberType&, const MemberType&) noexcept;
182184

183-
#if LANGULUS_SIMD(128BIT)
184-
TRange(const simde__m128&) noexcept;
185-
TRange(const simde__m128d&) noexcept;
186-
TRange(const simde__m128i&) noexcept;
187-
#endif
188-
189-
#if LANGULUS_SIMD(256BIT)
190-
TRange(const simde__m256&) noexcept;
191-
TRange(const simde__m256d&) noexcept;
192-
TRange(const simde__m256i&) noexcept;
193-
#endif
194-
195-
#if LANGULUS_SIMD(512BIT)
196-
TRange(const simde__m512&) noexcept;
197-
TRange(const simde__m512d&) noexcept;
198-
TRange(const simde__m512i&) noexcept;
199-
#endif
200-
185+
TRange(const CT::SIMD auto&) noexcept;
201186
TRange(Describe&&);
202187

203188
///
204189
/// Assignment
205190
///
206-
constexpr TRange& operator = (const TRange&) noexcept = default;
207-
constexpr TRange& operator = (const CT::RangeBased auto&) noexcept;
208-
constexpr TRange& operator = (const CT::VectorBased auto&) noexcept;
209-
constexpr TRange& operator = (const CT::ScalarBased auto&) noexcept;
191+
constexpr auto operator = (const TRange&) noexcept -> TRange& = default;
192+
constexpr auto operator = (const CT::RangeBased auto&) noexcept -> TRange&;
193+
constexpr auto operator = (const CT::VectorBased auto&) noexcept -> TRange&;
194+
constexpr auto operator = (const CT::ScalarBased auto&) noexcept -> TRange&;
210195

211196
template<CT::ScalarBased N, CT::Dimension D>
212197
constexpr auto& operator = (const TVectorComponent<N, D>&) noexcept;
198+
213199
NOD() explicit operator Anyness::Text() const;
214200
NOD() explicit operator Flow::Code() const;
215201

216-
constexpr TRange& Embrace(const auto&) noexcept;
217-
constexpr TRange& ConstrainBy(const auto&) noexcept;
202+
constexpr auto Embrace(const auto&) noexcept -> TRange&;
203+
constexpr auto ConstrainBy(const auto&) noexcept -> TRange&;
204+
205+
NOD() auto GetMin() const noexcept -> const PointType&;
206+
NOD() auto GetMax() const noexcept -> const PointType&;
207+
NOD() auto Length() const noexcept -> PointType;
208+
NOD() auto Center() const noexcept -> PointType;
218209

219-
NOD() const PointType& GetMin() const noexcept;
220-
NOD() const PointType& GetMax() const noexcept;
221-
NOD() PointType Length() const noexcept;
222-
NOD() PointType Center() const noexcept;
223210
NOD() constexpr bool IsDegenerate() const noexcept;
224211
NOD() constexpr bool Inside(const PointType&) const noexcept;
225212
NOD() constexpr bool IsInsideHalfClosed(const PointType&) const noexcept;
226-
NOD() constexpr PointType ClampRev(const PointType&) const noexcept;
227-
NOD() constexpr PointType Clamp(const PointType&) const noexcept;
213+
NOD() constexpr auto ClampRev(const PointType&) const noexcept -> PointType;
214+
NOD() constexpr auto Clamp(const PointType&) const noexcept -> PointType;
228215

229-
NOD() constexpr TRange operator | (const TRange&) const noexcept;
230-
constexpr TRange& operator |= (const TRange&) noexcept;
216+
NOD() constexpr auto operator | (const TRange&) const noexcept -> TRange ;
217+
constexpr auto operator |= (const TRange&) noexcept -> TRange&;
231218

232-
NOD() constexpr MemberType& operator [] (Offset) noexcept;
233-
NOD() constexpr const MemberType& operator [] (Offset) const noexcept;
219+
NOD() constexpr auto operator [] (Offset) noexcept -> MemberType&;
220+
NOD() constexpr auto operator [] (Offset) const noexcept -> const MemberType&;
234221
};
235222
#pragma pack(pop)
236223

source/Ranges/TRange.inl

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -53,56 +53,11 @@ namespace Langulus::Math
5353
: mMin {min}
5454
, mMax {max} {}
5555

56-
#if LANGULUS_SIMD(128BIT)
56+
/// Create from registers
5757
TEMPLATE() LANGULUS(INLINED)
58-
TME()::TRange(const simde__m128& source) noexcept {
58+
TME()::TRange(const CT::SIMD auto& source) noexcept {
5959
SIMD::Store(source, mArray);
6060
}
61-
62-
TEMPLATE() LANGULUS(INLINED)
63-
TME()::TRange(const simde__m128d& source) noexcept {
64-
SIMD::Store(source, mArray);
65-
}
66-
67-
TEMPLATE() LANGULUS(INLINED)
68-
TME()::TRange(const simde__m128i& source) noexcept {
69-
SIMD::Store(source, mArray);
70-
}
71-
#endif
72-
73-
#if LANGULUS_SIMD(256BIT)
74-
TEMPLATE() LANGULUS(INLINED)
75-
TME()::TRange(const simde__m256& source) noexcept {
76-
SIMD::Store(source, mArray);
77-
}
78-
79-
TEMPLATE() LANGULUS(INLINED)
80-
TME()::TRange(const simde__m256d& source) noexcept {
81-
SIMD::Store(source, mArray);
82-
}
83-
84-
TEMPLATE() LANGULUS(INLINED)
85-
TME()::TRange(const simde__m256i& source) noexcept {
86-
SIMD::Store(source, mArray);
87-
}
88-
#endif
89-
90-
#if LANGULUS_SIMD(512BIT)
91-
TEMPLATE() LANGULUS(INLINED)
92-
TME()::TRange(const simde__m512& source) noexcept {
93-
SIMD::Store(source, mArray);
94-
}
95-
96-
TEMPLATE() LANGULUS(INLINED)
97-
TME()::TRange(const simde__m512d& source) noexcept {
98-
SIMD::Store(source, mArray);
99-
}
100-
101-
TEMPLATE() LANGULUS(INLINED)
102-
TME()::TRange(const simde__m512i& source) noexcept {
103-
SIMD::Store(source, mArray);
104-
}
105-
#endif
10661

10762
/// Construct from a descriptor
10863
/// @param describe - the descriptor to scan
@@ -145,32 +100,30 @@ namespace Langulus::Math
145100
/// @param r - the range to copy
146101
/// @return a reference to this range
147102
TEMPLATE() LANGULUS(INLINED)
148-
constexpr TME()& TME()::operator = (const CT::RangeBased auto& r) noexcept {
103+
constexpr auto TME()::operator = (const CT::RangeBased auto& r) noexcept -> TRange& {
149104
return *new (this) TRange {DeintCast(r)};
150105
}
151106

152107
/// Copy vector
153108
/// @param v - the vector to copy
154109
/// @return a reference to this range
155110
TEMPLATE() LANGULUS(INLINED)
156-
constexpr TME()& TME()::operator = (const CT::VectorBased auto& v) noexcept {
111+
constexpr auto TME()::operator = (const CT::VectorBased auto& v) noexcept -> TRange& {
157112
return *new (this) TRange {DeintCast(v)};
158113
}
159114

160115
/// Copy scalar
161116
/// @param s - the scalar value
162117
/// @return a reference to this range
163118
TEMPLATE() LANGULUS(INLINED)
164-
constexpr TME()& TME()::operator = (const CT::ScalarBased auto& s) noexcept {
119+
constexpr auto TME()::operator = (const CT::ScalarBased auto& s) noexcept -> TRange& {
165120
return *new (this) TRange {DeintCast(s)};
166121
}
167122

168123
/// Set only a specific component
169124
/// @param c - the component to overwrite
170125
/// @return a reference to this vector
171-
TEMPLATE()
172-
template<CT::ScalarBased N, CT::Dimension D>
173-
LANGULUS(INLINED)
126+
TEMPLATE() template<CT::ScalarBased N, CT::Dimension D> LANGULUS(INLINED)
174127
constexpr auto& TME()::operator = (const TVectorComponent<N, D>& c) noexcept {
175128
return *new (this) TRange {PointType {c}};
176129
}
@@ -195,36 +148,36 @@ namespace Langulus::Math
195148
}
196149

197150
TEMPLATE() LANGULUS(INLINED)
198-
constexpr TME()& TME()::Embrace(const auto& other) noexcept {
151+
constexpr auto TME()::Embrace(const auto& other) noexcept -> TRange& {
199152
mMin = Min(mMin, other);
200153
mMax = Max(mMax, other);
201154
return *this;
202155
}
203156

204157
TEMPLATE() LANGULUS(INLINED)
205-
constexpr TME()& TME()::ConstrainBy(const auto& limits) noexcept {
158+
constexpr auto TME()::ConstrainBy(const auto& limits) noexcept -> TRange& {
206159
mMin = Clamp(mMin, limits.mMin, limits.mMax);
207160
mMax = Clamp(mMax, limits.mMin, limits.mMax);
208161
return *this;
209162
}
210163

211164
TEMPLATE() LANGULUS(INLINED)
212-
const typename TME()::PointType& TME()::GetMin() const noexcept {
165+
auto TME()::GetMin() const noexcept -> const PointType& {
213166
return mMin;
214167
}
215168

216169
TEMPLATE() LANGULUS(INLINED)
217-
const typename TME()::PointType& TME()::GetMax() const noexcept {
170+
auto TME()::GetMax() const noexcept -> const PointType& {
218171
return mMax;
219172
}
220173

221174
TEMPLATE() LANGULUS(INLINED)
222-
typename TME()::PointType TME()::Length() const noexcept {
175+
auto TME()::Length() const noexcept -> PointType {
223176
return mMax - mMin;
224177
}
225178

226179
TEMPLATE() LANGULUS(INLINED)
227-
typename TME()::PointType TME()::Center() const noexcept {
180+
auto TME()::Center() const noexcept -> PointType {
228181
return mMin + Length() * 0.5f;
229182
}
230183

@@ -244,25 +197,25 @@ namespace Langulus::Math
244197
}
245198

246199
TEMPLATE() LANGULUS(INLINED)
247-
constexpr typename TME()::PointType TME()::ClampRev(const PointType& pos) const noexcept {
200+
constexpr auto TME()::ClampRev(const PointType& pos) const noexcept -> PointType {
248201
return pos.ClampRev(mMin, mMax);
249202
}
250203

251204
TEMPLATE() LANGULUS(INLINED)
252-
constexpr typename TME()::PointType TME()::Clamp(const PointType& pos) const noexcept {
205+
constexpr auto TME()::Clamp(const PointType& pos) const noexcept -> PointType {
253206
return pos.Clamp(mMin, mMax);
254207
}
255208

256209
TEMPLATE() LANGULUS(INLINED)
257-
constexpr TME() TME()::operator | (const TME()& a) const noexcept {
210+
constexpr auto TME()::operator | (const TME()& a) const noexcept -> TRange {
258211
return {
259212
mMin.Clamp(a.mMin, a.mMax),
260213
mMax.Clamp(a.mMin, a.mMax)
261214
};
262215
}
263216

264217
TEMPLATE() LANGULUS(INLINED)
265-
constexpr TME()& TME()::operator |= (const TME()& a) noexcept {
218+
constexpr auto TME()::operator |= (const TME()& a) noexcept -> TRange& {
266219
*this = *this | a;
267220
}
268221

@@ -273,12 +226,12 @@ namespace Langulus::Math
273226
/// minX minY minZ ... maxX maxY maxZ ...
274227
/// @returns a reference to the component
275228
TEMPLATE() LANGULUS(INLINED)
276-
constexpr TypeOf<T>& TME()::operator [] (const Offset a) noexcept {
229+
constexpr auto TME()::operator [] (const Offset a) noexcept -> MemberType& {
277230
return mArray[a];
278231
}
279232

280233
TEMPLATE() LANGULUS(INLINED)
281-
constexpr const TypeOf<T>& TME()::operator [] (const Offset a) const noexcept {
234+
constexpr auto TME()::operator [] (const Offset a) const noexcept -> const MemberType& {
282235
return mArray[a];
283236
}
284237

@@ -434,35 +387,31 @@ namespace Langulus::Math
434387
///
435388

436389
/// Add
437-
template<class T1, class T2>
438-
LANGULUS(INLINED)
390+
template<class T1, class T2> LANGULUS(INLINED)
439391
auto& operator += (TRange<T1>& me, const TRange<T2>& other) noexcept {
440392
me.mMin += other.mMin;
441393
me.mMax += other.mMax;
442394
return me;
443395
}
444396

445397
/// Subtract
446-
template<class T1, class T2>
447-
LANGULUS(INLINED)
398+
template<class T1, class T2> LANGULUS(INLINED)
448399
auto& operator -= (TRange<T1>& me, const TRange<T2>& other) noexcept {
449400
me.mMin -= other.mMin;
450401
me.mMax -= other.mMax;
451402
return me;
452403
}
453404

454405
/// Multiply
455-
template<class T1, class T2>
456-
LANGULUS(INLINED)
406+
template<class T1, class T2> LANGULUS(INLINED)
457407
auto& operator *= (TRange<T1>& me, const TRange<T2>& other) noexcept {
458408
me.mMin *= other.mMin;
459409
me.mMax *= other.mMax;
460410
return me;
461411
}
462412

463413
/// Divide
464-
template<class T1, class T2>
465-
LANGULUS(INLINED)
414+
template<class T1, class T2> LANGULUS(INLINED)
466415
auto& operator /= (TRange<T1>& me, const TRange<T2>& other) {
467416
me.mMin /= other.mMin;
468417
me.mMax /= other.mMax;

test/Main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <Math/Config.hpp>
1818
#include <Math/Vector.hpp>
19+
#include <Math/Range.hpp>
1920
#include <Math/Number.hpp>
2021

2122
#define CATCH_CONFIG_RUNNER
@@ -28,6 +29,7 @@ int main(int argc, char* argv[]) {
2829
Math::RegisterTraits();
2930
Math::RegisterVerbs();
3031
Math::RegisterVectors();
32+
Math::RegisterRanges();
3133

3234
Catch::Session session;
3335
return session.run(argc, argv);

0 commit comments

Comments
 (0)