20#ifndef LIBREPCB_CORE_LENGTH_H
21#define LIBREPCB_CORE_LENGTH_H
26#include "../exceptions.h"
27#include "../qtcompat.h"
29#include <type_safe/constrained_type.hpp>
59#ifdef USE_32BIT_LENGTH_UNITS
84 Q_DECLARE_TR_FUNCTIONS(
Length)
607 static bool checkRange(qreal nanometers,
bool doThrow =
false);
621 const Length& gridInterval)
noexcept;
654 stream << length.
toMm();
659 stream << QString(
"Length(%1mm)").arg(length.
toMm());
673 template <
typename Value,
typename Predicate>
674 static constexpr auto verify(Value&& val,
const Predicate& p) ->
675 typename std::decay<Value>::type {
676 return p(val) ? std::forward<Value>(val)
678 Length::tr(
"Value must be >= 0!")),
679 std::forward<Value>(val));
738 return (*lhs) >= rhs;
741 return (*lhs) >= rhs;
750 return (*lhs) <= rhs;
753 return (*lhs) <= rhs;
756 return (*lhs) == rhs;
759 return (*lhs) == rhs;
762 return (*lhs) != rhs;
765 return (*lhs) != rhs;
770 stream << length->toMm();
775 stream << QString(
"UnsignedLength(%1mm)").arg(length->toMm());
789 template <
typename Value,
typename Predicate>
790 static constexpr auto verify(Value&& val,
const Predicate& p) ->
791 typename std::decay<Value>::type {
792 return p(val) ? std::forward<Value>(val)
794 Length::tr(
"Value must be > 0!")),
795 std::forward<Value>(val));
886 return (*lhs) > (*rhs);
890 return (*lhs) > (*rhs);
900 return (*lhs) >= (*rhs);
904 return (*lhs) >= (*rhs);
907 return (*lhs) >= rhs;
910 return (*lhs) >= rhs;
914 return (*lhs) < (*rhs);
918 return (*lhs) < (*rhs);
928 return (*lhs) <= (*rhs);
932 return (*lhs) <= (*rhs);
935 return (*lhs) <= rhs;
938 return (*lhs) <= rhs;
942 return (*lhs) == (*rhs);
946 return (*lhs) == (*rhs);
949 return (*lhs) == rhs;
952 return (*lhs) == rhs;
956 return (*lhs) != (*rhs);
960 return (*lhs) != (*rhs);
963 return (*lhs) != rhs;
966 return (*lhs) != rhs;
971 stream << length->toMm();
976 stream << QString(
"PositiveLength(%1mm)").arg(length->toMm());
989using Point3D = std::tuple<Length, Length, Length>;
The Length class is used to represent a length (for example 12.75 millimeters)
Definition: length.h:83
constexpr bool operator!=(const Length &rhs) const
Definition: length.h:570
void setLengthMil(qreal mils)
Set the length in mils (1/1000 inch)
Definition: length.h:188
Length & operator/=(const Length &rhs)
Definition: length.h:518
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:156
Length & operator*=(LengthBase_t rhs)
Definition: length.h:514
constexpr bool operator==(LengthBase_t rhs) const
Definition: length.h:567
Length & operator-=(const Length &rhs)
Definition: length.h:506
Length operator-() const
Definition: length.h:529
static constexpr LengthBase_t sNmPerMil
1 inch = 25.4mm
Definition: length.h:642
Length operator/(const Length &rhs) const
Definition: length.h:537
constexpr bool operator!=(LengthBase_t rhs) const
Definition: length.h:573
constexpr bool operator<=(const Length &rhs) const
Definition: length.h:558
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:551
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:564
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:552
void setLengthPx(qreal pixels)
Set the length in pixels (from QGraphics* objects)
Definition: length.h:205
QString toMmString() const noexcept
Get the length in millimeters as a QString.
Definition: length.cpp:47
Length & operator+=(const Length &rhs)
Definition: length.h:502
Length operator*(LengthBase_t rhs) const
Definition: length.h:536
qreal toMm() const noexcept
Get the length in millimeters.
Definition: length.h:240
static constexpr LengthBase_t sNmPerInch
1 inch = 25.4mm
Definition: length.h:641
constexpr Length() noexcept
Default Constructor.
Definition: length.h:94
qreal toInch() const noexcept
Get the length in inches.
Definition: length.h:262
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:555
Length & operator/=(LengthBase_t rhs)
Definition: length.h:522
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:510
qreal toMicrometers() const noexcept
Get the length in micrometers.
Definition: length.h:231
Length operator/(LengthBase_t rhs) const
Definition: length.h:540
static bool isValidMm(qreal millimeters) noexcept
Check if a float value in millimeters is in the allowed range.
Definition: length.h:367
void setLengthNm(LengthBase_t nanometers) noexcept
Set the length in nanometers.
Definition: length.h:124
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:643
Length operator-(const Length &rhs) const
Definition: length.h:530
constexpr bool operator>(const Length &rhs) const
Definition: length.h:544
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:173
Length operator*(const Length &rhs) const
Definition: length.h:533
constexpr bool operator<(const Length &rhs) const
Definition: length.h:548
constexpr bool operator>(LengthBase_t rhs) const
Definition: length.h:547
qreal toPx() const noexcept
Get the length in pixels (for QGraphics* objects)
Definition: length.h:283
LengthBase_t mNanometers
the length in nanometers
Definition: length.h:638
QString toNmString() const noexcept
Get the length in nanometers as a QString.
Definition: length.h:222
static constexpr qreal sNmPerPixel
Definition: length.h:645
constexpr Length(LengthBase_t nanometers) noexcept
Constructor with length in nanometers.
Definition: length.h:109
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:141
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:101
qreal toMil() const noexcept
Get the length in mils (1/1000 inches)
Definition: length.h:271
LengthBase_t toNm() const noexcept
Get the length in nanometers.
Definition: length.h:214
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:646
Length operator%(const Length &rhs) const
Definition: length.h:541
Length operator+(const Length &rhs) const
Definition: length.h:526
constexpr bool operator<=(LengthBase_t rhs) const
Definition: length.h:561
uint Hash
Return type of Qt's qHash() function.
Definition: qtcompat.h:58
The RuntimeError class.
Definition: exceptions.h:218
Definition: occmodel.cpp:77
type_safe::constrained_type< Length, PositiveLengthConstraint, PositiveLengthVerifier > PositiveLength
Definition: length.h:812
bool operator==(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:86
QtCompat::Hash qHash(const AttributeKey &key, QtCompat::Hash seed=0) noexcept
Definition: attributekey.h:119
type_safe::constrained_type< Length, UnsignedLengthConstraint, UnsignedLengthVerifier > UnsignedLength
Definition: length.h:696
bool operator>=(const UnsignedLength &lhs, const Length &rhs) noexcept
Definition: length.h:737
std::tuple< Length, Length, Length > Point3D
Definition: length.h:989
qint64 LengthBase_t
This type is the ONLY base type to store all lengths (always in nanometers)!
Definition: length.h:62
bool operator!=(const AttributeKey &lhs, const QString &rhs) noexcept
Definition: attributekey.h:92
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:722
Length operator*(const UnsignedLength &lhs, LengthBase_t rhs) noexcept
Definition: length.h:710
UnsignedLength positiveToUnsigned(const PositiveLength &l) noexcept
Definition: length.h:814
UnsignedLength operator+(const UnsignedLength &lhs, const UnsignedLength &rhs) noexcept
Definition: length.h:698
Length operator/(const UnsignedLength &lhs, LengthBase_t rhs) noexcept
Definition: length.h:713
UnsignedLength & operator+=(UnsignedLength &lhs, const UnsignedLength &rhs) noexcept
Definition: length.h:704
QDataStream & operator<<(QDataStream &stream, const AttributeKey &obj)
Definition: attributekey.h:109
bool operator<=(const UnsignedLength &lhs, const Length &rhs) noexcept
Definition: length.h:749
QtCompat::Hash qHash(const PositiveLength &key, QtCompat::Hash seed=0) noexcept
Definition: length.h:980
bool operator>(const UnsignedLength &lhs, const Length &rhs) noexcept
Definition: length.h:731
constexpr bool operator()(const Length &l) const noexcept
Definition: length.h:800
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: length.h:790
constexpr bool operator()(const Length &l) const noexcept
Definition: length.h:684
static constexpr auto verify(Value &&val, const Predicate &p) -> typename std::decay< Value >::type
Definition: length.h:674