← Back to team overview

kicad-developers team mailing list archive

[RFC 1/2] New coordinate framework

 

---
 include/length.h | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/point.h  |  46 ++++++++++++++++++++++++
 2 files changed, 151 insertions(+)
 create mode 100644 include/length.h
 create mode 100644 include/point.h

diff --git a/include/length.h b/include/length.h
new file mode 100644
index 000000000..b6a910cfc
--- /dev/null
+++ b/include/length.h
@@ -0,0 +1,105 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2018 Simon Richter
+ * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+
+#include <cstdint>
+
+
+/** Length in real-world units (metric or imperial) */
+class length
+{
+public:
+    typedef int64_t value_type;
+    typedef double factor_type;
+
+    /// @todo Valgrind: mark uninitialized
+    constexpr length() : value(0) { }
+
+    // Addition
+    constexpr length operator+(length const &rhs) const { return length(value + rhs.value); }
+    length &operator+=(length const &rhs) { value += rhs.value; return *this; }
+
+    // Subtraction
+    constexpr length operator-(length const &rhs) const { return length(value - rhs.value); }
+    length &operator-=(length const &rhs) { value -= rhs.value; return *this; }
+
+    // Multiplication with a scalar
+    constexpr length operator*(factor_type const &rhs) const { return length(value * rhs); }
+    length &operator*=(factor_type const &rhs) { value *= rhs; return *this; }
+
+    // Division by a scalar
+    constexpr length operator/(factor_type const &rhs) const { return length(value / rhs); }
+    length &operator/=(factor_type const &rhs) { value /= rhs; return *this; }
+
+    // Division by a length (returning a scalar)
+    constexpr factor_type operator/(length const &rhs) const { return factor_type(value) / factor_type(rhs.value); }
+
+private:
+    constexpr explicit length(value_type v) : value(v) { }
+
+    friend constexpr length operator""_nm(unsigned long long);
+
+    value_type value;
+};
+
+
+/** Commutative multiplication */
+template<typename T>
+constexpr auto operator*(T const &lhs, length const &rhs) -> decltype(rhs * lhs)
+{
+    return rhs * lhs;
+}
+
+
+/** Length in nanometers */
+constexpr length operator""_nm(unsigned long long value)
+{
+    return length(value);
+}
+
+
+/** Length in micrometers */
+constexpr length operator""_um(unsigned long long value)
+{
+    return 1000_nm * value;
+}
+
+
+/** Length in millimeters */
+constexpr length operator""_mm(unsigned long long value)
+{
+    return 1000_um * value;
+}
+
+
+/** Lenght in mil */
+constexpr length operator""_mil(unsigned long long value)
+{
+    return 25400_um * value;
+}
+
+
+/** Length in inches */
+constexpr length operator""_in(unsigned long long value)
+{
+    return 1000_mil * value;
+}
diff --git a/include/point.h b/include/point.h
new file mode 100644
index 000000000..06e1f9e56
--- /dev/null
+++ b/include/point.h
@@ -0,0 +1,46 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2018 Simon Richter
+ * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+
+#include "length.h"
+
+
+class point
+{
+public:
+    constexpr point() : x(), y() { }
+    constexpr point(length nx, length ny) : x(nx), y(ny) { }
+
+    length x, y;
+};
+
+
+class vector
+{
+public:
+    constexpr vector() : x(), y() { }
+    constexpr vector(length nx, length ny) : x(nx), y(ny) { }
+
+    constexpr explicit operator bool() const { return x || y; }
+
+    length x, y;
+};

Follow ups

References