20#ifndef LIBREPCB_CORE_LENGTH_H
21#define LIBREPCB_CORE_LENGTH_H
26#include "../exceptions.h"
28#include <type_safe/constrained_type.hpp>
58#ifdef USE_32BIT_LENGTH_UNITS
83 Q_DECLARE_TR_FUNCTIONS(
Length)
606 static bool checkRange(qreal nanometers,
bool doThrow =
false);
620 const Length& gridInterval)
noexcept;
653 stream << length.
toMm();
658 stream << QString(
"Length(%1mm)").arg(length.
toMm());
662inline std::size_t
qHash(
const Length& key, std::size_t seed = 0) noexcept {
663 return ::qHash(key.
toNm(), seed);
671 template <
typename Value,
typename Predicate>
672 static constexpr auto verify(Value&& val,
const Predicate& p) ->
673 typename std::decay<Value>::type {
674 return p(val) ? std::forward<Value>(val)
676 Length::tr(
"Value must be >= 0!")),
677 std::forward<Value>(val));
736 return (*lhs) >= rhs;
739 return (*lhs) >= rhs;
748 return (*lhs) <= rhs;
751 return (*lhs) <= rhs;
754 return (*lhs) == rhs;
757 return (*lhs) == rhs;
760 return (*lhs) != rhs;
763 return (*lhs) != rhs;
768 stream << length->
toMm();
773 stream << QString(
"UnsignedLength(%1mm)").arg(length->toMm());
778 std::size_t seed = 0) noexcept {
779 return ::qHash(key->toNm(), seed);
787 template <
typename Value,
typename Predicate>
788 static constexpr auto verify(Value&& val,
const Predicate& p) ->
789 typename std::decay<Value>::type {
790 return p(val) ? std::forward<Value>(val)
792 Length::tr(
"Value must be > 0!")),
793 std::forward<Value>(val));
884 return (*lhs) > (*rhs);
888 return (*lhs) > (*rhs);
898 return (*lhs) >= (*rhs);
902 return (*lhs) >= (*rhs);
905 return (*lhs) >= rhs;
908 return (*lhs) >= rhs;
912 return (*lhs) < (*rhs);
916 return (*lhs) < (*rhs);
926 return (*lhs) <= (*rhs);
930 return (*lhs) <= (*rhs);
933 return (*lhs) <= rhs;
936 return (*lhs) <= rhs;
940 return (*lhs) == (*rhs);
944 return (*lhs) == (*rhs);
947 return (*lhs) == rhs;
950 return (*lhs) == rhs;
954 return (*lhs) != (*rhs);
958 return (*lhs) != (*rhs);
961 return (*lhs) != rhs;
964 return (*lhs) != rhs;
969 stream << length->
toMm();
974 stream << QString(
"PositiveLength(%1mm)").arg(length->toMm());
979 std::size_t seed = 0) noexcept {
980 return ::qHash(key->toNm(), seed);
987using Point3D = std::tuple<Length, Length, Length>;
The Length class is used to represent a length (for example 12.75 millimeters)
Definition length.h:82
constexpr bool operator!=(const Length &rhs) const
Definition length.h:569
void setLengthMil(qreal mils)
Set the length in mils (1/1000 inch)
Definition length.h:187
Length & operator/=(const Length &rhs)
Definition length.h:517
Length & mapToGrid(const Length &gridInterval) noexcept
Map this Length object to a specific grid interval.
Definition length.cpp:75
Length & makeAbs() noexcept
Make the length absolute (mNanometers >= 0)
Definition length.cpp:61
void setLengthMm(const QString &millimeters)
Set the length in millimeters, represented in a QString.
Definition length.h:155
Length & operator*=(LengthBase_t rhs)
Definition length.h:513
constexpr bool operator==(LengthBase_t rhs) const
Definition length.h:566
Length & operator-=(const Length &rhs)
Definition length.h:505
Length operator-() const
Definition length.h:528
static constexpr LengthBase_t sNmPerMil
1 inch = 25.4mm
Definition length.h:641
Length operator/(const Length &rhs) const
Definition length.h:536
constexpr bool operator!=(LengthBase_t rhs) const
Definition length.h:572
constexpr bool operator<=(const Length &rhs) const
Definition length.h:557
Length abs() const noexcept
Get a Length object with absolute value (mNanometers >= 0)
Definition length.cpp:55
constexpr bool operator<(LengthBase_t rhs) const
Definition length.h:550
Length mappedToGrid(const Length &gridInterval) const noexcept
Get a Length object which is mapped to a specific grid interval.
Definition length.cpp:70
constexpr bool operator==(const Length &rhs) const
Definition length.h:563
static LengthBase_t mmStringToNm(const QString &millimeters)
Convert a length from a QString (in millimeters) to an integer (in nanometers)
Definition length.cpp:200
constexpr bool operator>=(const Length &rhs) const
Definition length.h:551
void setLengthPx(qreal pixels)
Set the length in pixels (from QGraphics* objects)
Definition length.h:204
QString toMmString() const noexcept
Get the length in millimeters as a QString.
Definition length.cpp:47
Length & operator+=(const Length &rhs)
Definition length.h:501
Length operator*(LengthBase_t rhs) const
Definition length.h:535
qreal toMm() const noexcept
Get the length in millimeters.
Definition length.h:239
static constexpr LengthBase_t sNmPerInch
1 inch = 25.4mm
Definition length.h:640
constexpr Length() noexcept
Default Constructor.
Definition length.h:93
qreal toInch() const noexcept
Get the length in inches.
Definition length.h:261
Length & scale(qreal factor) noexcept
Scale this Length object with a specific factor.
Definition length.cpp:85
void setLengthFromFloat(qreal nanometers)
Set the length from a floating point number in nanometers.
Definition length.cpp:136
static Length fromMm(qreal millimeters, const Length &gridInterval=Length(0))
Get a Length object with a specific length and map it to a specific grid.
Definition length.cpp:94
constexpr bool operator>=(LengthBase_t rhs) const
Definition length.h:554
Length & operator/=(LengthBase_t rhs)
Definition length.h:521
static Length fromInch(qreal inches, const Length &gridInterval=Length(0))
Get a Length object with a specific length and map it to a specific grid.
Definition length.cpp:106
Length & operator*=(const Length &rhs)
Definition length.h:509
qreal toMicrometers() const noexcept
Get the length in micrometers.
Definition length.h:230
Length operator/(LengthBase_t rhs) const
Definition length.h:539
static bool isValidMm(qreal millimeters) noexcept
Check if a float value in millimeters is in the allowed range.
Definition length.h:366
void setLengthNm(LengthBase_t nanometers) noexcept
Set the length in nanometers.
Definition length.h:123
static LengthBase_t mapNmToGrid(LengthBase_t nanometers, const Length &gridInterval) noexcept
Map a length in nanometers to a grid interval in nanometers.
Definition length.cpp:160
static constexpr LengthBase_t sPixelsPerInch
72 dpi for the QGraphics* objects
Definition length.h:642
Length operator-(const Length &rhs) const
Definition length.h:529
constexpr bool operator>(const Length &rhs) const
Definition length.h:543
static Length min() noexcept
Get the smallest possible length value.
Definition length.cpp:124
static bool checkRange(qreal nanometers, bool doThrow=false)
Check if a float value in nanometers is in the allowed range.
Definition length.cpp:147
void setLengthInch(qreal inches)
Set the length in inches.
Definition length.h:172
Length operator*(const Length &rhs) const
Definition length.h:532
constexpr bool operator<(const Length &rhs) const
Definition length.h:547
constexpr bool operator>(LengthBase_t rhs) const
Definition length.h:546
qreal toPx() const noexcept
Get the length in pixels (for QGraphics* objects)
Definition length.h:282
LengthBase_t mNanometers
the length in nanometers
Definition length.h:637
QString toNmString() const noexcept
Get the length in nanometers as a QString.
Definition length.h:221
static constexpr qreal sNmPerPixel
Definition length.h:644
constexpr Length(LengthBase_t nanometers) noexcept
Constructor with length in nanometers.
Definition length.h:108
static Length fromMil(qreal mils, const Length &gridInterval=Length(0))
Get a Length object with a specific length and map it to a specific grid.
Definition length.cpp:112
void setLengthMm(qreal millimeters)
Set the length in millimeters.
Definition length.h:140
Length scaled(qreal factor) const noexcept
Get a Length object which is scaled with a specific factor.
Definition length.cpp:80
constexpr Length(const Length &length) noexcept
Copy Constructor.
Definition length.h:100
qreal toMil() const noexcept
Get the length in mils (1/1000 inches)
Definition length.h:270
LengthBase_t toNm() const noexcept
Get the length in nanometers.
Definition length.h:213
static Length fromPx(qreal pixels, const Length &gridInterval=Length(0))
Get a Length object with a specific length and map it to a specific grid.
Definition length.cpp:118
~Length()=default
Destructor.
static Length max() noexcept
Get the highest possible length value.
Definition length.cpp:128
static constexpr qreal sPixelsPerNm
Definition length.h:645
Length operator%(const Length &rhs) const
Definition length.h:540
Length operator+(const Length &rhs) const
Definition length.h:525
constexpr bool operator<=(LengthBase_t rhs) const
Definition length.h:560
The RuntimeError class.
Definition exceptions.h:218
Definition occmodel.cpp:76
std::size_t qHash(const AttributeKey &key, std::size_t seed=0) noexcept
Definition attributekey.h:118
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition length.h:810
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:85
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition length.h:694
bool operator>=(const UnsignedLength &lhs, const Length &rhs) noexcept
Definition length.h:735
std::tuple< Length, Length, Length > Point3D
Definition length.h:987
qint64 LengthBase_t
This type is the ONLY base type to store all lengths (always in nanometers)!
Definition length.h:61
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition attributekey.h:91
static bool operator<(const BoardGerberExport::LayerPair &lhs, const BoardGerberExport::LayerPair &rhs) noexcept
Definition boardgerberexport.cpp:62
Length operator-(const Length &lhs, const UnsignedLength &rhs) noexcept
Definition length.h:720
Length operator*(const UnsignedLength &lhs, LengthBase_t rhs) noexcept
Definition length.h:708
UnsignedLength positiveToUnsigned(const PositiveLength &l) noexcept
Definition length.h:812
UnsignedLength operator+(const UnsignedLength &lhs, const UnsignedLength &rhs) noexcept
Definition length.h:696
Length operator/(const UnsignedLength &lhs, LengthBase_t rhs) noexcept
Definition length.h:711
UnsignedLength & operator+=(UnsignedLength &lhs, const UnsignedLength &rhs) noexcept
Definition length.h:702
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition attributekey.h:108
bool operator<=(const UnsignedLength &lhs, const Length &rhs) noexcept
Definition length.h:747
bool operator>(const UnsignedLength &lhs, const Length &rhs) noexcept
Definition length.h:729
constexpr bool operator()(const Length &l) const noexcept
Definition length.h:798
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition length.h:788
constexpr bool operator()(const Length &l) const noexcept
Definition length.h:682
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition length.h:672