--- a/src/Types.h Sun Mar 09 14:04:06 2014 +0200 +++ b/src/Types.h Sun Mar 09 14:59:46 2014 +0200 @@ -16,15 +16,12 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef LDFORGE_TYPES_H -#define LDFORGE_TYPES_H - +#pragma once #include <QString> #include <QObject> #include <QStringList> #include <QMetaType> -#include <QVector> -#include "PropertyMacro.h" +#include "Macros.h" class LDObject; class QFile; @@ -39,11 +36,8 @@ using uint32 = quint32; using uint64 = quint64; -template<class T> -using initlist = std::initializer_list<T>; - -template<class T, class R> -using pair = std::pair<T, R>; +template<typename T, typename R> +using Pair = std::pair<T, R>; enum Axis { @@ -57,143 +51,230 @@ class LDObject; using LDObjectList = QList<LDObject*>; -// ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= -// matrix -// -// A mathematical 3 x 3 matrix -// ============================================================================= +//! +//! \brief A mathematical 3 x 3 matrix +//! class Matrix { public: + //! Constructs a matrix with undetermined values. Matrix() {} - Matrix (initlist<double> vals); + + //! Constructs a matrix with the given values. + //! \note \c vals is expected to have exactly 9 elements. + Matrix (const QList<double>& vals); + + //! Constructs a matrix all 9 elements initialized to the same value. + //! \param fillval the value to initialize the matrix coordinates as Matrix (double fillval); + + //! Constructs a matrix with a C-array. + //! \note \c vals is expected to have exactly 9 elements. Matrix (double vals[]); + //! Calculates the matrix's determinant. + //! \returns the calculated determinant. double getDeterminant() const; + + //! Multiplies this matrix with \c other + //! \param other the matrix to multiply with. + //! \returns the resulting matrix + //! \note a.mult(b) is not equivalent to b.mult(a)! Matrix mult (const Matrix& other) const; - void puts() const; + + //! Prints the matrix to stdout. + void dump() const; + + //! \returns a string representation of the matrix. QString toString() const; + + //! Zeroes the matrix out. void zero(); + + //! Assigns the matrix values to the values of \c other. + //! \param other the matrix to assign this to. + //! \returns a reference to self Matrix& operator= (const Matrix& other); - inline double& val (int idx) + //! \returns a mutable reference to a value by \c idx + inline double& value (int idx) { return m_vals[idx]; } - inline const double& val (int idx) const + //! An overload of \c value() for const matrices. + //! \returns a const reference to a value by \c idx + inline const double& value (int idx) const { return m_vals[idx]; } + //! An operator overload for \c mult(). + //! \returns the multiplied matrix. inline Matrix operator* (const Matrix& other) const { return mult (other); } + //! An operator overload for \c value(). + //! \returns a mutable reference to a value by \c idx inline double& operator[] (int idx) { - return val (idx); + return value (idx); } + //! An operator overload for \c value() const. + //! \returns a const reference to a value by \c idx inline const double& operator[] (int idx) const { - return val (idx); + return value (idx); } + //! \param other the matrix to check against + //! \returns whether the two matrices have the same values. bool operator== (const Matrix& other) const; private: double m_vals[9]; }; -// ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= -// Vertex -// -// Vertex class, contains a single point in 3D space. Not to be confused with -// LDVertex, which is a vertex used in an LDraw part file. -// ============================================================================= +//! +//! \brief A vertex in 3D space +//! +//! Contains a single point in 3D space. Not to be confused with +//! LDVertex, which is a vertex used in an LDraw part file. +//! +//! This also sees use as a position vector. +//! class Vertex { public: - Vertex() {} + //! Constructs a zero vertex + Vertex() : m_coords({0, 0, 0}) {} + + //! Constructs a vertex with the given \c x, \c y and \c z. Vertex (double x, double y, double z); + //! \returns the distance from this vertex to \c other double distanceTo (const Vertex& other) const; + + //! \returns the vertex at the midpoint between this and \c other Vertex midpoint (const Vertex& other); + + //! Moves this vertex using \param other as a position vector. void move (const Vertex& other); + + //! Yields a string representation of the vertex. The string returned + //! can possibly be mangled. + //! - As mangled: {1.5, 2.8, 3.14} + //! - Without mangling: 1.5 2.8 3.14 + //! + //! The mangled version is suitable for printing to the user, the + //! non-mangled one is used when writing the vertex to LDraw files. + //! + //! \returns a string representation of this vertex + //! \param mangled whether to return a mangled representation or not QString toString (bool mangled) const; + + //! Transforms this vertex with \c matr as transformation matrix + //! and \c pos as the position column of the 4x4 matrix. void transform (const Matrix& matr, const Vertex& pos); + //! An operator overload for \c move(). Vertex& operator+= (const Vertex& other); + + //! An operator overload for \c move(), using a temporary vertex. Vertex operator+ (const Vertex& other) const; + + //! Divides all values by \c d. Vertex operator/ (const double d) const; + + //! Divides all values by \c d. Vertex& operator/= (const double d); + + //! Checks whether this vertex has the same values as \c other. bool operator== (const Vertex& other) const; + + //! Checks whether this vertex has different values than \c other. bool operator!= (const Vertex& other) const; + + //! \returns a negated version the vertex Vertex operator-() const; + + //! \returns whether the vertex has lesser values than \c other. int operator< (const Vertex& other) const; + //! An operator overload for \c getCoordinate(). inline double& operator[] (const Axis ax) { return getCoordinate ((int) ax); } + //! An operator overload for \c getCoordinate() const. inline const double& operator[] (const Axis ax) const { return getCoordinate ((int) ax); } + //! An operator overload for \c getCoordinate(). inline double& operator[] (const int ax) { return getCoordinate (ax); } + //! An operator overload for \c getCoordinate() const. inline const double& operator[] (const int ax) const { return getCoordinate (ax); } + //! \returns a mutable reference for the coordinate designated by \param n. inline double& getCoordinate (int n) { return m_coords[n]; } + //! An overload of \c getCoordinate for const vertices. + //! \returns a const reference for the coordinate designated by \param n. inline const double& getCoordinate (int n) const { return m_coords[n]; } + //! \returns a mutable reference to X. inline double& x() { return m_coords[X]; } + //! An overload of \c x() for const vertices. + //! \returns a const reference to X. inline const double& x() const { return m_coords[X]; } + //! \returns a mutable reference to Y. inline double& y() { return m_coords[Y]; } + //! An overload of \c y() for const vertices. + //! \returns a const reference to Y. inline const double& y() const { return m_coords[Y]; } + //! \returns a mutable reference to Z. inline double& z() { return m_coords[Z]; } + //! An overload of \c z() for const vertices. + //! \returns a const reference to Z. inline const double& z() const { return m_coords[Z]; @@ -205,122 +286,49 @@ Q_DECLARE_METATYPE (Vertex) -// ============================================================================= -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -// ============================================================================= -// StringFormatArg +//! +//! Defines a bounding box that encompasses a given set of objects. +//! vertex0 is the minimum vertex, vertex1 is the maximum vertex. // -// Converts a given value into a string that can be retrieved with ::value(). -// Used as the argument type to the formatting functions, hence its name. -// ============================================================================= -class StringFormatArg -{ - public: - StringFormatArg (const QString& a) : m_val (a) {} - StringFormatArg (const char& a) : m_val (a) {} - StringFormatArg (const uchar& a) : m_val (a) {} - StringFormatArg (const QChar& a) : m_val (a) {} - StringFormatArg (int a) : m_val (QString::number (a)) {} - StringFormatArg (const float& a) : m_val (QString::number (a)) {} - StringFormatArg (const double& a) : m_val (QString::number (a)) {} - StringFormatArg (const Vertex& a) : m_val (a.toString (false)) {} - StringFormatArg (const Matrix& a) : m_val (a.toString()) {} - StringFormatArg (const char* a) : m_val (a) {} - - StringFormatArg (const void* a) - { - m_val.sprintf ("%p", a); - } - - template<class T, class R> void initFromList (const T& a) - { - m_val = "{ "; - - for (const R& it : a) - { - if (&it != &a.first()) - m_val += ", "; - - StringFormatArg arg (it); - m_val += arg.value(); - } - - if (!a.isEmpty()) - m_val += " "; - - m_val += "}"; - } - - template<class T> StringFormatArg (const QList<T>& a) - { - initFromList<QList<T>, T> (a); - } - - template<class T> StringFormatArg (const QVector<T>& a) - { - initFromList<QVector<T>, T> (a); - } - - inline QString value() const - { - return m_val; - } - - private: - QString m_val; -}; - -// ============================================================================= -// LDBoundingBox -// -// The bounding box is the box that encompasses a given set of objects. -// v0 is the minimum vertex, v1 is the maximum vertex. -// ============================================================================= class LDBoundingBox { - PROPERTY (private, bool, Empty, BOOL_OPS, STOCK_WRITE) - PROPERTY (private, Vertex, Vertex0, NO_OPS, STOCK_WRITE) - PROPERTY (private, Vertex, Vertex1, NO_OPS, STOCK_WRITE) + PROPERTY (private, bool, isEmpty, setEmpty, STOCK_WRITE) + PROPERTY (private, Vertex, vertex0, setVertex0, STOCK_WRITE) + PROPERTY (private, Vertex, vertex1, setVertex1, STOCK_WRITE) public: + //! Constructs an empty bounding box. LDBoundingBox(); + + //! Clears the bounding box void reset(); - void calculate(); - double size() const; + + //! Calculates the bounding box's values from the objects in the current + //! document. + void calculateFromCurrentDocument(); + + //! \returns the length of the bounding box on the longest measure. + double longestMeasurement() const; + + //! Calculates the given \c obj to the bounding box, adjusting + //! extremas if necessary. void calcObject (LDObject* obj); - void calcVertex (const Vertex& v); + + //! Calculates the given \c vertex to the bounding box, adjusting + //! extremas if necessary. + void calcVertex (const Vertex& vertex); + + //! \returns the center of the bounding box. Vertex center() const; + //! An operator overload for \c calcObject() LDBoundingBox& operator<< (LDObject* obj); + + //! An operator overload for \c calcVertex() LDBoundingBox& operator<< (const Vertex& v); }; -// Formatter function -QString DoFormat (QList<StringFormatArg> args); - -// printf replacement -void doPrint (QFile& f, QList<StringFormatArg> args); -void doPrint (FILE* fp, QList<StringFormatArg> args); - -// log() - universal access to the message log. Defined here so that I don't have -// to include MessageLog.h here and recompile everything every time that file changes. -void DoLog (std::initializer_list<StringFormatArg> args); - -// Macros to access these functions -# ifndef IN_IDE_PARSER -#define fmt(...) DoFormat ({__VA_ARGS__}) -# define fprint(F, ...) doPrint (F, {__VA_ARGS__}) -# define log(...) DoLog({ __VA_ARGS__ }) -#else -QString fmt (const char* fmtstr, ...); -void fprint (QFile& f, const char* fmtstr, ...); -void fprint (FILE* fp, const char* fmtstr, ...); -void log (const char* fmtstr, ...); -#endif - extern const Vertex g_origin; // Vertex at (0, 0, 0) extern const Matrix g_identity; // Identity matrix static const double pi = 3.14159265358979323846; - -#endif // LDFORGE_TYPES_H