From cd688f608f9db3476072d34cb305f63d9f278d47 Mon Sep 17 00:00:00 2001 From: jmsgrogan Date: Fri, 13 Jan 2023 11:47:48 +0000 Subject: [PATCH] Start cleaning geometry module. --- .../src/visuals/BlochSphereNode.cpp | 4 ++ src/core/AbstractNamedItem.h | 12 ++++ src/core/CMakeLists.txt | 1 + src/geometry/AbstractGeometricItem.h | 24 ++----- src/geometry/Bounds.h | 21 ++++++ src/geometry/CMakeLists.txt | 62 +++++++++------- src/geometry/DiscretePoint.cpp | 4 +- src/geometry/DiscretePoint.h | 12 ++-- src/geometry/Grid.cpp | 33 ++++----- src/geometry/Grid.h | 18 ++--- src/geometry/Matrix.h | 0 src/geometry/Path.cpp | 0 src/geometry/Path.h | 0 src/geometry/Point.cpp | 6 +- src/geometry/Point.h | 2 - src/geometry/Triangle.h | 0 src/geometry/{ => math}/Linalg.cpp | 0 src/geometry/{ => math}/Linalg.h | 0 src/geometry/{ => math}/Matrix.cpp | 0 src/geometry/math/Matrix.h | 14 ++++ src/geometry/{ => math}/Vector.cpp | 0 src/geometry/{ => math}/Vector.h | 0 src/geometry/{Line.cpp => path/Curve.cpp} | 0 src/geometry/path/Curve.h | 13 ++++ src/geometry/path/Line.cpp | 53 ++++++++++++++ src/geometry/path/Line.h | 23 ++++++ src/geometry/{ => path}/LineSegment.cpp | 12 +++- src/geometry/{ => path}/LineSegment.h | 12 ++-- src/geometry/path/Path.cpp | 6 ++ src/geometry/path/Path.h | 17 +++++ src/geometry/{Line.h => path/PathElement.cpp} | 0 src/geometry/path/PathElement.h | 9 +++ src/geometry/{ => primitives}/Circle.cpp | 19 +++-- src/geometry/{ => primitives}/Circle.h | 12 ++-- src/geometry/{ => primitives}/Quad.cpp | 0 src/geometry/{ => primitives}/Quad.h | 0 src/geometry/{ => primitives}/Rectangle.cpp | 25 ++++--- src/geometry/{ => primitives}/Rectangle.h | 15 +--- src/geometry/{ => primitives}/Triangle.cpp | 0 src/geometry/primitives/Triangle.h | 8 +++ src/publishing/plotting/SvgConverter.cpp | 9 ++- .../basic_shapes/CircleNode.cpp | 62 ++-------------- src/visual_elements/basic_shapes/CircleNode.h | 14 +--- src/visual_elements/basic_shapes/LineNode.h | 28 +++++++- .../basic_shapes/RectangleNode.cpp | 58 +-------------- .../basic_shapes/RectangleNode.h | 15 ++-- src/visual_elements/nodes/GeometryNode.cpp | 72 ++++++++++++++++++- src/visual_elements/nodes/GeometryNode.h | 25 +++++-- src/visual_elements/scene/SceneItem.cpp | 10 +++ src/visual_elements/scene/SceneItem.h | 5 ++ src/web/svg/elements/SvgShapeElements.cpp | 23 +++++- src/web/svg/elements/SvgShapeElements.h | 12 +++- 52 files changed, 493 insertions(+), 277 deletions(-) create mode 100644 src/core/AbstractNamedItem.h create mode 100644 src/geometry/Bounds.h delete mode 100644 src/geometry/Matrix.h delete mode 100644 src/geometry/Path.cpp delete mode 100644 src/geometry/Path.h delete mode 100644 src/geometry/Triangle.h rename src/geometry/{ => math}/Linalg.cpp (100%) rename src/geometry/{ => math}/Linalg.h (100%) rename src/geometry/{ => math}/Matrix.cpp (100%) create mode 100644 src/geometry/math/Matrix.h rename src/geometry/{ => math}/Vector.cpp (100%) rename src/geometry/{ => math}/Vector.h (100%) rename src/geometry/{Line.cpp => path/Curve.cpp} (100%) create mode 100644 src/geometry/path/Curve.h create mode 100644 src/geometry/path/Line.cpp create mode 100644 src/geometry/path/Line.h rename src/geometry/{ => path}/LineSegment.cpp (61%) rename src/geometry/{ => path}/LineSegment.h (60%) create mode 100644 src/geometry/path/Path.cpp create mode 100644 src/geometry/path/Path.h rename src/geometry/{Line.h => path/PathElement.cpp} (100%) create mode 100644 src/geometry/path/PathElement.h rename src/geometry/{ => primitives}/Circle.cpp (52%) rename src/geometry/{ => primitives}/Circle.h (80%) rename src/geometry/{ => primitives}/Quad.cpp (100%) rename src/geometry/{ => primitives}/Quad.h (100%) rename src/geometry/{ => primitives}/Rectangle.cpp (57%) rename src/geometry/{ => primitives}/Rectangle.h (62%) rename src/geometry/{ => primitives}/Triangle.cpp (100%) create mode 100644 src/geometry/primitives/Triangle.h diff --git a/plugins/quantum_computing/src/visuals/BlochSphereNode.cpp b/plugins/quantum_computing/src/visuals/BlochSphereNode.cpp index ae4c08a..741c2ff 100644 --- a/plugins/quantum_computing/src/visuals/BlochSphereNode.cpp +++ b/plugins/quantum_computing/src/visuals/BlochSphereNode.cpp @@ -36,9 +36,13 @@ void BlochSphereNode::update(SceneInfo* sceneInfo) auto loc = Point(mSize / 2.0, mSize / 2.0); mInnerCircle = std::make_unique(loc, mSize / 2.0); mInnerCircle->setMinorRadius(mSize / 4.0); + mInnerCircle->setStrokeThickness(0.5); mOuterCircle = std::make_unique(loc, mSize/2.0); + const auto end_point_x = Point(loc.getX() + 1.2 * mSize / 2.0, loc.getY()); + mXAxis = std::make_unique(loc, { end_point_x }); + mCentreCircle = std::make_unique(loc, mSize / 50.0); mCentreCircle->setFillColor(Color(0, 0, 0)); mCentreCircle->setHasStrokeColor(false); diff --git a/src/core/AbstractNamedItem.h b/src/core/AbstractNamedItem.h new file mode 100644 index 0000000..e68dca8 --- /dev/null +++ b/src/core/AbstractNamedItem.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +class AbstractNamedItem +{ +public: + ~AbstractNamedItem() = default; +protected: + std::string mName; + std::string mSymbol; +}; \ No newline at end of file diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 1f4dd5b..9acd7c2 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -2,6 +2,7 @@ set(MODULE_NAME core) list(APPEND core_HEADERS AbstractApp.h + AbstractNamedItem.h AbstractWebApp.h Dictionary.h Event.h diff --git a/src/geometry/AbstractGeometricItem.h b/src/geometry/AbstractGeometricItem.h index 723a37a..1c30503 100644 --- a/src/geometry/AbstractGeometricItem.h +++ b/src/geometry/AbstractGeometricItem.h @@ -1,9 +1,8 @@ #pragma once -class Rectangle; -class Point; +#include "Bounds.h" +#include "Point.h" -template class Grid; class AbstractGeometricItem @@ -13,6 +12,8 @@ public: enum class Type { LINE, + LINE_SEGMENT, + CURVE, RECTANGLE, TRIANGLE, POINT, @@ -21,26 +22,13 @@ public: UNKNOWN }; - struct Bounds - { - Bounds(double width, double height) - : mWidth(width), - mHeight(height) - { - - } - - double mWidth{0.0}; - double mHeight{0.0}; - }; - virtual ~AbstractGeometricItem() = default; - virtual Bounds getSize() const = 0; + virtual Bounds getBounds() const = 0; virtual const Point& getLocation() const = 0; - virtual void sample(Grid* grid) const = 0; + virtual void sample(Grid* grid) const = 0; virtual Type getType() const { return Type::UNKNOWN; }; }; diff --git a/src/geometry/Bounds.h b/src/geometry/Bounds.h new file mode 100644 index 0000000..790e194 --- /dev/null +++ b/src/geometry/Bounds.h @@ -0,0 +1,21 @@ +#pragma once + +struct Bounds +{ + Bounds(double minX, double maxX, double minY, double maxY, double minZ = 0.0, double maxZ = 0.0) + : mMinX(minX), + mMaxX(maxX), + mMinY(minY), + mMaxY(maxY), + mMinZ(minZ), + mMaxZ(maxZ) + { + + } + double mMinX{ 0.0 }; + double mMaxX{ 0.0 }; + double mMinY{ 0.0 }; + double mMaxY{ 0.0 }; + double mMinZ{ 0.0 }; + double mMaxZ{ 0.0 }; +}; \ No newline at end of file diff --git a/src/geometry/CMakeLists.txt b/src/geometry/CMakeLists.txt index 8b0cbf8..d450add 100644 --- a/src/geometry/CMakeLists.txt +++ b/src/geometry/CMakeLists.txt @@ -1,36 +1,48 @@ +set(MODULE_NAME geometry) + list(APPEND geometry_LIB_INCLUDES AbstractGeometricItem.h - Circle.h - Circle.cpp - DiscretePoint.h + Bounds.h + DiscretePoint.h DiscretePoint.cpp Grid.h Grid.cpp - Line.h - Linalg.h - Linalg.cpp - Line.cpp - LineSegment.cpp - Matrix.h - Matrix.cpp - Path.h - Path.cpp Point.h Point.cpp - Quad.cpp - Rectangle.h - Rectangle.cpp - Triangle.h - Triangle.cpp Transform.cpp - Vector.h - Vector.cpp) + math/Linalg.h + math/Linalg.cpp + math/Matrix.h + math/Matrix.cpp + math/Vector.h + math/Vector.cpp + path/Curve.h + path/Curve.cpp + path/Line.h + path/Line.cpp + path/LineSegment.h + path/LineSegment.cpp + path/Path.h + path/Path.cpp + path/PathElement.h + primitives/Circle.h + primitives/Circle.cpp + primitives/Quad.h + primitives/Quad.cpp + primitives/Rectangle.h + primitives/Rectangle.cpp + primitives/Triangle.h + primitives/Triangle.cpp + ) +add_library(${MODULE_NAME} SHARED ${geometry_LIB_INCLUDES}) -# add the library -add_library(geometry SHARED ${geometry_LIB_INCLUDES}) +target_include_directories(${MODULE_NAME} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/math + ${CMAKE_CURRENT_SOURCE_DIR}/path + ${CMAKE_CURRENT_SOURCE_DIR}/primitives + ) -target_include_directories(geometry PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") - -set_target_properties( geometry PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) -set_property(TARGET geometry PROPERTY FOLDER src) +set_target_properties( ${MODULE_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) +set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER src) diff --git a/src/geometry/DiscretePoint.cpp b/src/geometry/DiscretePoint.cpp index 7758a3b..dc5f8eb 100644 --- a/src/geometry/DiscretePoint.cpp +++ b/src/geometry/DiscretePoint.cpp @@ -17,12 +17,12 @@ std::shared_ptr DiscretePoint::Create(unsigned x, unsigned y) return std::make_shared(x, y); } -unsigned DiscretePoint::GetX() const +unsigned DiscretePoint::getX() const { return mX; } -unsigned DiscretePoint::GetY() const +unsigned DiscretePoint::getY() const { return mY; } diff --git a/src/geometry/DiscretePoint.h b/src/geometry/DiscretePoint.h index 997c5bf..f87185c 100644 --- a/src/geometry/DiscretePoint.h +++ b/src/geometry/DiscretePoint.h @@ -4,20 +4,16 @@ class DiscretePoint { - unsigned mX; - unsigned mY; - public: - DiscretePoint(unsigned x = 0, unsigned y = 0); ~DiscretePoint(); std::shared_ptr Create(unsigned x, unsigned y); - unsigned GetX() const; + unsigned getX() const; - unsigned GetY() const; + unsigned getY() const; bool operator==(const DiscretePoint& rhs) const { @@ -28,6 +24,10 @@ public: { return !operator==(rhs); } + +private: + unsigned mX{ 0 }; + unsigned mY{ 0 }; }; using Pixel = DiscretePoint; diff --git a/src/geometry/Grid.cpp b/src/geometry/Grid.cpp index f5260aa..b405986 100644 --- a/src/geometry/Grid.cpp +++ b/src/geometry/Grid.cpp @@ -1,50 +1,43 @@ #include "Grid.h" -template -Grid::Grid(const ntk::Rectangle& bounds) +Grid::Grid(const Bounds& bounds) : mBounds(bounds) { - mValues = std::vector(mNumX*mNumY, T()); + mValues = std::vector(mNumX*mNumY, 0.0); } -template -const ntk::Rectangle& Grid::getBounds() const +const Bounds& Grid::getBounds() const { return mBounds; } -template -double Grid::getXSpacing() const +double Grid::getXSpacing() const { - return mBounds.getWidth()/double(mNumX); + const auto width = mBounds.mMaxX - mBounds.mMinX; + return width/double(mNumX); } -template -double Grid::getYSpacing() const +double Grid::getYSpacing() const { - return mBounds.getHeight()/double(mNumY); + const auto height = mBounds.mMaxY - mBounds.mMinY; + return height/double(mNumY); } -template -const std::vector& Grid::getValues() const +const std::vector& Grid::getValues() const { return mValues; } -template -void Grid::resetBounds(const ntk::Rectangle& bounds) +void Grid::resetBounds(const Bounds& bounds) { mBounds = bounds; - mValues = std::vector(mNumX*mNumY, T()); + mValues = std::vector(mNumX*mNumY, 0.0); } -template -void Grid::setValues(const std::vector& indices, T value) +void Grid::setValues(const std::vector& indices, double value) { for (auto index : indices) { mValues[index] = value; } } - -template class Grid; diff --git a/src/geometry/Grid.h b/src/geometry/Grid.h index 0edc9cf..3577b19 100644 --- a/src/geometry/Grid.h +++ b/src/geometry/Grid.h @@ -1,29 +1,29 @@ #pragma once -#include "Rectangle.h" +#include "Bounds.h" + #include -template class Grid { public: - Grid(const ntk::Rectangle& bounds); + Grid(const Bounds& bounds); - const ntk::Rectangle& getBounds() const; + const Bounds& getBounds() const; double getXSpacing() const; double getYSpacing() const; - const std::vector& getValues() const; + const std::vector& getValues() const; - void resetBounds(const ntk::Rectangle& bounds); + void resetBounds(const Bounds& bounds); - void setValues(const std::vector& indices, T value); + void setValues(const std::vector& indices, double value); private: - ntk::Rectangle mBounds; - std::vector mValues; + Bounds mBounds; + std::vector mValues; unsigned mNumX{5}; unsigned mNumY{5}; }; diff --git a/src/geometry/Matrix.h b/src/geometry/Matrix.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/geometry/Path.cpp b/src/geometry/Path.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/geometry/Path.h b/src/geometry/Path.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/geometry/Point.cpp b/src/geometry/Point.cpp index c743725..f9f1088 100644 --- a/src/geometry/Point.cpp +++ b/src/geometry/Point.cpp @@ -1,5 +1,7 @@ #include "Point.h" +#include + Point::Point(double x, double y, double z) : mX(x), mY(y), @@ -8,8 +10,8 @@ Point::Point(double x, double y, double z) } Point::Point(const DiscretePoint& point) - : mX(point.GetX()), - mY(point.GetY()), + : mX(static_cast(point.getX())), + mY(static_cast(point.getY())), mZ(0) { diff --git a/src/geometry/Point.h b/src/geometry/Point.h index 79df9f9..b9f204a 100644 --- a/src/geometry/Point.h +++ b/src/geometry/Point.h @@ -4,12 +4,10 @@ #include "Vector.h" #include -#include class Point { public: - Point(double x = 0, double y = 0, double z = 0); Point(const DiscretePoint& point); diff --git a/src/geometry/Triangle.h b/src/geometry/Triangle.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/geometry/Linalg.cpp b/src/geometry/math/Linalg.cpp similarity index 100% rename from src/geometry/Linalg.cpp rename to src/geometry/math/Linalg.cpp diff --git a/src/geometry/Linalg.h b/src/geometry/math/Linalg.h similarity index 100% rename from src/geometry/Linalg.h rename to src/geometry/math/Linalg.h diff --git a/src/geometry/Matrix.cpp b/src/geometry/math/Matrix.cpp similarity index 100% rename from src/geometry/Matrix.cpp rename to src/geometry/math/Matrix.cpp diff --git a/src/geometry/math/Matrix.h b/src/geometry/math/Matrix.h new file mode 100644 index 0000000..96e247f --- /dev/null +++ b/src/geometry/math/Matrix.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +class Matrix +{ +public: + Matrix(unsigned numRows, unsigned numColumns) + { + mData = std::vector(numRows * numColumns, 0.0); + } +private: + std::vector mData; +}; \ No newline at end of file diff --git a/src/geometry/Vector.cpp b/src/geometry/math/Vector.cpp similarity index 100% rename from src/geometry/Vector.cpp rename to src/geometry/math/Vector.cpp diff --git a/src/geometry/Vector.h b/src/geometry/math/Vector.h similarity index 100% rename from src/geometry/Vector.h rename to src/geometry/math/Vector.h diff --git a/src/geometry/Line.cpp b/src/geometry/path/Curve.cpp similarity index 100% rename from src/geometry/Line.cpp rename to src/geometry/path/Curve.cpp diff --git a/src/geometry/path/Curve.h b/src/geometry/path/Curve.h new file mode 100644 index 0000000..b3f19d1 --- /dev/null +++ b/src/geometry/path/Curve.h @@ -0,0 +1,13 @@ +#pragma once + +#include "PathElement.h" + +class Curve : public PathElement +{ +public: + enum class CurveType + { + ARC, + CUBIC_BEZIER + }; +}; \ No newline at end of file diff --git a/src/geometry/path/Line.cpp b/src/geometry/path/Line.cpp new file mode 100644 index 0000000..7cedca2 --- /dev/null +++ b/src/geometry/path/Line.cpp @@ -0,0 +1,53 @@ +#include "Line.h" + +Line::Line(const Point& start, const std::vector& points) + : mStartPoint(start), + mPoints(points) +{ + +} + +const std::vector& Line::getPoints() const +{ + return mPoints; +} + +Line::Type Line::getType() const +{ + return Line::Type::LINE; +} + +const Point& Line::getLocation() const +{ + return mStartPoint; +} + +Bounds Line::getBounds() const +{ + double minX = mStartPoint.getX(); + double maxX = mStartPoint.getX(); + + double minY = mStartPoint.getY(); + double maxY = mStartPoint.getY(); + + for (const auto& point : mPoints) + { + if (point.getX() > maxX) + { + maxX = point.getX(); + } + if (point.getX() < minX) + { + minX = point.getX(); + } + if (point.getY() > maxY) + { + maxY = point.getX(); + } + if (point.getY() < minY) + { + minY = point.getY(); + } + } + return { minX, maxX, minY, maxY }; +} \ No newline at end of file diff --git a/src/geometry/path/Line.h b/src/geometry/path/Line.h new file mode 100644 index 0000000..23cce25 --- /dev/null +++ b/src/geometry/path/Line.h @@ -0,0 +1,23 @@ +#pragma once + +#include "PathElement.h" + +#include + +class Line : public PathElement +{ +public: + Line(const Point& start, const std::vector& points); + + const std::vector& getPoints() const; + + Line::Type getType() const override; + + const Point& getLocation() const override; + + Bounds getBounds() const override; + +private: + Point mStartPoint; + std::vector mPoints; +}; \ No newline at end of file diff --git a/src/geometry/LineSegment.cpp b/src/geometry/path/LineSegment.cpp similarity index 61% rename from src/geometry/LineSegment.cpp rename to src/geometry/path/LineSegment.cpp index 8f607b9..d8ebab3 100644 --- a/src/geometry/LineSegment.cpp +++ b/src/geometry/path/LineSegment.cpp @@ -27,14 +27,20 @@ const Point& LineSegment::getPoint1() const return mP1; } -void LineSegment::sample(Grid* grid) const +void LineSegment::sample(Grid* grid) const { } -LineSegment::Bounds LineSegment::getSize() const +Bounds LineSegment::getBounds() const { - return {mP0.getDeltaX(mP1), mP0.getDeltaY(mP1)}; + const auto minX = std::min(mP0.getX(), mP1.getX()); + const auto maxX = std::max(mP0.getX(), mP1.getX()); + + const auto minY = std::min(mP0.getY(), mP1.getY()); + const auto maxY = std::max(mP0.getY(), mP1.getY()); + + return {minX, maxX, minY, maxY}; } const Point& LineSegment::getLocation() const diff --git a/src/geometry/LineSegment.h b/src/geometry/path/LineSegment.h similarity index 60% rename from src/geometry/LineSegment.h rename to src/geometry/path/LineSegment.h index 330db38..369d017 100644 --- a/src/geometry/LineSegment.h +++ b/src/geometry/path/LineSegment.h @@ -1,12 +1,8 @@ #pragma once -#include "AbstractGeometricItem.h" -#include "Point.h" +#include "PathElement.h" -template -class Grid; - -class LineSegment : public AbstractGeometricItem +class LineSegment : public PathElement { public: LineSegment(const Point& p0, const Point& p1); @@ -19,9 +15,9 @@ public: const Point& getPoint1() const; - void sample(Grid* grid) const override; + void sample(Grid* grid) const override; - Bounds getSize() const override; + Bounds getBounds() const override; const Point& getLocation() const override; diff --git a/src/geometry/path/Path.cpp b/src/geometry/path/Path.cpp new file mode 100644 index 0000000..f299982 --- /dev/null +++ b/src/geometry/path/Path.cpp @@ -0,0 +1,6 @@ +#include "Path.h" + +const std::vector Path::getElements() const +{ + return mElements; +} \ No newline at end of file diff --git a/src/geometry/path/Path.h b/src/geometry/path/Path.h new file mode 100644 index 0000000..5f28825 --- /dev/null +++ b/src/geometry/path/Path.h @@ -0,0 +1,17 @@ +#pragma once + +#include "PathElement.h" + +#include +#include + +using PathElementPtr = std::unique_ptr; + +class Path : public AbstractGeometricItem +{ +public: + const std::vector getElements() const; + +private: + std::vector mElements; +}; \ No newline at end of file diff --git a/src/geometry/Line.h b/src/geometry/path/PathElement.cpp similarity index 100% rename from src/geometry/Line.h rename to src/geometry/path/PathElement.cpp diff --git a/src/geometry/path/PathElement.h b/src/geometry/path/PathElement.h new file mode 100644 index 0000000..42aad66 --- /dev/null +++ b/src/geometry/path/PathElement.h @@ -0,0 +1,9 @@ +#pragma once + +#include "AbstractGeometricItem.h" + +class PathElement : public AbstractGeometricItem +{ +public: + ~PathElement() = default; +}; \ No newline at end of file diff --git a/src/geometry/Circle.cpp b/src/geometry/primitives/Circle.cpp similarity index 52% rename from src/geometry/Circle.cpp rename to src/geometry/primitives/Circle.cpp index be208ac..640cdd9 100644 --- a/src/geometry/Circle.cpp +++ b/src/geometry/primitives/Circle.cpp @@ -1,8 +1,8 @@ #include "Circle.h" Circle::Circle(const Point& centre, double radius) - : mCentre(centre), - mRadius(radius) + : mCentre(centre), + mRadius(radius) { mMinorRadius = mRadius; } @@ -27,14 +27,23 @@ void Circle::setMinorRadius(double radius) mMinorRadius = radius; } -void Circle::sample(Grid* grid) const +void Circle::sample(Grid* grid) const { } -Circle::Bounds Circle::getSize() const +bool Circle::isEllipse() const { - return Bounds{ 2.0 * mRadius, 2.0 * mMinorRadius }; + return mRadius != mMinorRadius; +} + +Bounds Circle::getBounds() const +{ + double minX = mCentre.getX() - mRadius; + double maxX = mCentre.getX() + mRadius; + double minY = mCentre.getY() - mMinorRadius; + double maxY = mCentre.getY() + mMinorRadius; + return { minX, maxX, minY, maxY }; } Circle::Type Circle::getType() const diff --git a/src/geometry/Circle.h b/src/geometry/primitives/Circle.h similarity index 80% rename from src/geometry/Circle.h rename to src/geometry/primitives/Circle.h index 41666fa..91d5142 100644 --- a/src/geometry/Circle.h +++ b/src/geometry/primitives/Circle.h @@ -14,14 +14,16 @@ public: double getMinorRadius() const; - void setMinorRadius(double radius); - - void sample(Grid* grid) const override; - - Bounds getSize() const override; + Bounds getBounds() const override; Type getType() const override; + bool isEllipse() const; + + void setMinorRadius(double radius); + + void sample(Grid* grid) const override; + private: double mMinorRadius{ 0.5 }; double mRadius{ 0.5 }; diff --git a/src/geometry/Quad.cpp b/src/geometry/primitives/Quad.cpp similarity index 100% rename from src/geometry/Quad.cpp rename to src/geometry/primitives/Quad.cpp diff --git a/src/geometry/Quad.h b/src/geometry/primitives/Quad.h similarity index 100% rename from src/geometry/Quad.h rename to src/geometry/primitives/Quad.h diff --git a/src/geometry/Rectangle.cpp b/src/geometry/primitives/Rectangle.cpp similarity index 57% rename from src/geometry/Rectangle.cpp rename to src/geometry/primitives/Rectangle.cpp index 8e3fac8..b2747a6 100644 --- a/src/geometry/Rectangle.cpp +++ b/src/geometry/primitives/Rectangle.cpp @@ -10,18 +10,28 @@ namespace ntk { Rectangle::Rectangle(const Point& bottomLeft, double width, double height) : mBottomLeft(bottomLeft), - mHeight(height), - mWidth(width) + mHeight(height), + mWidth(width) { } - Rectangle Rectangle::getBounds() const + Rectangle::Type Rectangle::getType() const { - return Rectangle(mBottomLeft, Point(mBottomLeft, mWidth, mHeight)); + return AbstractGeometricItem::Type::RECTANGLE; } - void Rectangle::sample(Grid* grid) const + Bounds Rectangle::getBounds() const + { + const auto minX = mBottomLeft.getX(); + const auto maxX = mBottomLeft.getX() + mWidth; + + const auto minY = mBottomLeft.getY(); + const auto maxY = mBottomLeft.getY() + mHeight; + return { minX , maxX , minY , maxY }; + } + + void Rectangle::sample(Grid* grid) const { } @@ -40,9 +50,4 @@ namespace ntk { { return mBottomLeft; } - - Rectangle::Bounds Rectangle::getSize() const - { - return { mWidth, mHeight }; - } } diff --git a/src/geometry/Rectangle.h b/src/geometry/primitives/Rectangle.h similarity index 62% rename from src/geometry/Rectangle.h rename to src/geometry/primitives/Rectangle.h index e821d32..ee9c5c1 100644 --- a/src/geometry/Rectangle.h +++ b/src/geometry/primitives/Rectangle.h @@ -1,10 +1,6 @@ #pragma once #include "AbstractGeometricItem.h" -#include "Point.h" - -template -class Grid; namespace ntk{ class Rectangle : public AbstractGeometricItem @@ -19,16 +15,11 @@ public: const Point& getLocation() const override; - Bounds getSize() const override; + Bounds getBounds() const override; - Rectangle getBounds() const; + Type getType() const override; - Type getType() const - { - return AbstractGeometricItem::Type::RECTANGLE; - } - - void sample(Grid* grid) const override; + void sample(Grid* grid) const override; private: Point mBottomLeft; diff --git a/src/geometry/Triangle.cpp b/src/geometry/primitives/Triangle.cpp similarity index 100% rename from src/geometry/Triangle.cpp rename to src/geometry/primitives/Triangle.cpp diff --git a/src/geometry/primitives/Triangle.h b/src/geometry/primitives/Triangle.h new file mode 100644 index 0000000..f20ff74 --- /dev/null +++ b/src/geometry/primitives/Triangle.h @@ -0,0 +1,8 @@ +#pragma once + +#include "AbstractGeometricItem.h" + +class Triangle : public AbstractGeometricItem +{ + +}; \ No newline at end of file diff --git a/src/publishing/plotting/SvgConverter.cpp b/src/publishing/plotting/SvgConverter.cpp index 08f5eae..72b9d3f 100644 --- a/src/publishing/plotting/SvgConverter.cpp +++ b/src/publishing/plotting/SvgConverter.cpp @@ -102,7 +102,7 @@ void SvgConverter::setStyle(SceneModel* model, SvgShapeElement* element) const if (model->hasOutlineColor()) { element->setStrokeColor(model->getOutlineColor()); - element->setStrokeWidth(1.0 / transform.getScaleX()); + element->setStrokeWidth(model->getOutlineThickness() / transform.getScaleX()); } else { @@ -126,8 +126,13 @@ void SvgConverter::convertPrimitive(SvgDocument* document, SceneModel* model) co { auto model_circle = dynamic_cast(model->getGeometry()); - auto circle = std::make_unique(); + auto is_ellipse = model_circle->getMinorRadius() != model_circle->getRadius(); + auto circle = std::make_unique(model_circle->isEllipse() ? SvgCircle::Type::ELLIPSE : SvgCircle::Type::REGULAR); circle->setRadius(model_circle->getRadius()); + if (model_circle->isEllipse()) + { + circle->setMinorRadius(model_circle->getMinorRadius()); + } setStyle(model, circle.get()); document->getRoot()->addChild(std::move(circle)); diff --git a/src/visual_elements/basic_shapes/CircleNode.cpp b/src/visual_elements/basic_shapes/CircleNode.cpp index 87c351a..795cb2a 100644 --- a/src/visual_elements/basic_shapes/CircleNode.cpp +++ b/src/visual_elements/basic_shapes/CircleNode.cpp @@ -1,6 +1,5 @@ #include "CircleNode.h" -#include "FontsManager.h" #include "SceneModel.h" #include "AbstractMesh.h" #include "MeshPrimitives.h" @@ -11,7 +10,7 @@ CircleNode::CircleNode(const Point& location, double radius) : GeometryNode(location), mRadius(radius) { - + mMinorRadius = mRadius; } CircleNode::Type CircleNode::getType() @@ -24,23 +23,6 @@ double CircleNode::getRadius() const return mRadius; } -SceneItem* CircleNode::getSceneItem(std::size_t idx) const -{ - if (idx == 0) - { - return mBackgroundItem.get(); - } - else - { - return nullptr; - } -} - -std::size_t CircleNode::getNumSceneItems() const -{ - return 1; -} - void CircleNode::setRadius(double radius) { if (mRadius != radius) @@ -63,8 +45,9 @@ void CircleNode::createOrUpdateGeometry(SceneInfo* sceneInfo) { if (sceneInfo->mSupportsGeometryPrimitives) { - auto circle = std::make_unique(Point{ 0, 0 }, 0.5); - circle->setMinorRadius(mMinorRadius); + double unit_circle_radius = 0.5; + auto circle = std::make_unique(Point{ 0, 0 }, unit_circle_radius); + circle->setMinorRadius(unit_circle_radius*mMinorRadius/mRadius); mBackgroundItem = std::make_unique(std::move(circle)); } else @@ -75,40 +58,7 @@ void CircleNode::createOrUpdateGeometry(SceneInfo* sceneInfo) mBackgroundItem->setName(mName + "_Model"); } -void CircleNode::updateMaterial() +void CircleNode::updateTransform() { - if (!mBackgroundItem) - { - return; - } - - if (mHasFillColor) - { - mBackgroundItem->setFillColor(mFillColor); - } - if (mHasStrokeColor) - { - mBackgroundItem->setOutlineColor(mStrokeColor); - } -} - -void CircleNode::update(SceneInfo* sceneInfo) -{ - if (!mBackgroundItem || mGeometryIsDirty) - { - createOrUpdateGeometry(sceneInfo); - mGeometryIsDirty = false; - } - - if (mTransformIsDirty) - { - mBackgroundItem->updateTransform({mLocation, 2*mRadius, 2*mRadius}); - mTransformIsDirty = false; - } - - if (mMaterialIsDirty) - { - updateMaterial(); - mMaterialIsDirty = false; - } + mBackgroundItem->updateTransform({ mLocation, 2 * mRadius, 2 * mRadius }); } diff --git a/src/visual_elements/basic_shapes/CircleNode.h b/src/visual_elements/basic_shapes/CircleNode.h index 640096b..546ac5f 100644 --- a/src/visual_elements/basic_shapes/CircleNode.h +++ b/src/visual_elements/basic_shapes/CircleNode.h @@ -8,22 +8,14 @@ public: CircleNode(const Point& location, double radius); Type getType() override; - double getRadius() const; - SceneItem* getSceneItem(std::size_t idx) const override; - std::size_t getNumSceneItems() const override; - void setMinorRadius(double radius); void setRadius(double radius); - - void update(SceneInfo* sceneInfo) override; private: - void createOrUpdateGeometry(SceneInfo* sceneInfo); - void updateMaterial(); + void createOrUpdateGeometry(SceneInfo* sceneInfo) override; + void updateTransform() override; + double mRadius{1.0}; double mMinorRadius{ 1.0 }; - - std::unique_ptr mBackgroundItem; - std::unique_ptr mOutlineItem; }; diff --git a/src/visual_elements/basic_shapes/LineNode.h b/src/visual_elements/basic_shapes/LineNode.h index f830cbb..c863212 100644 --- a/src/visual_elements/basic_shapes/LineNode.h +++ b/src/visual_elements/basic_shapes/LineNode.h @@ -1,12 +1,18 @@ #pragma once #include "GeometryNode.h" +#include "SceneInfo.h" + +#include "Line.h" + +#include class LineNode : public GeometryNode { public: - LineNode(const Point& location) - : GeometryNode(location) + LineNode(const Point& location, const std::vector& points) + : GeometryNode(location), + mPoints(points) { } @@ -15,4 +21,22 @@ public: { return Type::Line; } + + void createOrUpdateGeometry(SceneInfo* sceneInfo) + { + if (sceneInfo->mSupportsGeometryPrimitives) + { + auto line = std::make_unique(Point{ 0, 0 }, 1, 1); + mBackgroundItem = std::make_unique(std::move(line)); + } + mBackgroundItem->setName(mName + "_Model"); + } + + void updateTransform() + { + mBackgroundItem->updateTransform({ mLocation }); + } + +private: + std::vector mPoints; }; \ No newline at end of file diff --git a/src/visual_elements/basic_shapes/RectangleNode.cpp b/src/visual_elements/basic_shapes/RectangleNode.cpp index 49140f4..b9852cc 100644 --- a/src/visual_elements/basic_shapes/RectangleNode.cpp +++ b/src/visual_elements/basic_shapes/RectangleNode.cpp @@ -5,8 +5,6 @@ #include "MeshPrimitives.h" #include "SceneInfo.h" -#include - RectangleNode::RectangleNode(const Point& loc, double width, double height) : GeometryNode(loc), mWidth(width), @@ -25,23 +23,6 @@ GeometryNode::Type RectangleNode::getType() return GeometryNode::Type::Rectangle; } -SceneItem* RectangleNode::getSceneItem(std::size_t idx) const -{ - if (idx == 0) - { - return mBackgroundItem.get(); - } - else - { - return nullptr; - } -} - -std::size_t RectangleNode::getNumSceneItems() const -{ - return 1; -} - double RectangleNode::getWidth() const { return mWidth; @@ -86,40 +67,7 @@ void RectangleNode::createOrUpdateGeometry(SceneInfo* sceneInfo) mBackgroundItem->setName(mName + "_Model"); } -void RectangleNode::updateMaterial() +void RectangleNode::updateTransform() { - if (!mBackgroundItem) - { - return; - } - - if (mHasFillColor) - { - mBackgroundItem->setFillColor(mFillColor); - } - if (mHasStrokeColor) - { - mBackgroundItem->setOutlineColor(mStrokeColor); - } -} - -void RectangleNode::update(SceneInfo* sceneInfo) -{ - if (!mBackgroundItem || mGeometryIsDirty) - { - createOrUpdateGeometry(sceneInfo); - mGeometryIsDirty = false; - } - - if (mTransformIsDirty) - { - mBackgroundItem->updateTransform({mLocation, mWidth, mHeight}); - mTransformIsDirty = false; - } - - if (mMaterialIsDirty) - { - updateMaterial(); - mMaterialIsDirty = false; - } -} + mBackgroundItem->updateTransform({ mLocation, mWidth, mHeight }); +} \ No newline at end of file diff --git a/src/visual_elements/basic_shapes/RectangleNode.h b/src/visual_elements/basic_shapes/RectangleNode.h index 1fe65ad..2fff6fc 100644 --- a/src/visual_elements/basic_shapes/RectangleNode.h +++ b/src/visual_elements/basic_shapes/RectangleNode.h @@ -9,27 +9,20 @@ class RectangleNode : public GeometryNode public: RectangleNode(const Point& loc, double width, double height); static std::unique_ptr Create(const Point& loc, double width, double height); + GeometryNode::Type getType() override; double getWidth() const; double getHeight() const; - SceneItem* getSceneItem(std::size_t idx) const override; - std::size_t getNumSceneItems() const override; - void setWidth(double width); void setHeight(double height); - - void update(SceneInfo* sceneInfo) override; - private: - void createOrUpdateGeometry(SceneInfo* sceneInfo); - void updateMaterial(); + void createOrUpdateGeometry(SceneInfo* sceneInfo) override; + void updateTransform() override; + double mWidth{1}; double mHeight{1}; - - std::unique_ptr mBackgroundItem; - std::unique_ptr mOutlineItem; }; using RectangleNodePtr = std::unique_ptr; diff --git a/src/visual_elements/nodes/GeometryNode.cpp b/src/visual_elements/nodes/GeometryNode.cpp index 03a024c..cb0fb09 100644 --- a/src/visual_elements/nodes/GeometryNode.cpp +++ b/src/visual_elements/nodes/GeometryNode.cpp @@ -1,19 +1,45 @@ #include "GeometryNode.h" +#include "SceneInfo.h" +#include "SceneModel.h" + GeometryNode::GeometryNode(const Point& location) : MaterialNode(location), - mStrokeThickness(1), + mStrokeThickness(1.0), mType(Type::Path) { } -unsigned GeometryNode::getStrokeThickness() const +GeometryNode::~GeometryNode() +{ + +} + +SceneItem* GeometryNode::getSceneItem(std::size_t idx) const +{ + if (idx == 0) + { + return mBackgroundItem.get(); + } + else + { + return nullptr; + } +} + +std::size_t GeometryNode::getNumSceneItems() const +{ + return 1; +} + + +double GeometryNode::getStrokeThickness() const { return mStrokeThickness; } -void GeometryNode::setStrokeThickness(unsigned thickness) +void GeometryNode::setStrokeThickness(double thickness) { if (mStrokeThickness != thickness) { @@ -21,3 +47,43 @@ void GeometryNode::setStrokeThickness(unsigned thickness) mStrokeThickness = thickness; } } + +void GeometryNode::updateMaterial() +{ + if (!mBackgroundItem) + { + return; + } + + if (mHasFillColor) + { + mBackgroundItem->setFillColor(mFillColor); + } + + if (mHasStrokeColor) + { + mBackgroundItem->setOutlineColor(mStrokeColor); + mBackgroundItem->setOutlineThickness(mStrokeThickness); + } +} + +void GeometryNode::update(SceneInfo* sceneInfo) +{ + if (!mBackgroundItem || mGeometryIsDirty) + { + createOrUpdateGeometry(sceneInfo); + mGeometryIsDirty = false; + } + + if (mTransformIsDirty) + { + updateTransform(); + mTransformIsDirty = false; + } + + if (mMaterialIsDirty) + { + updateMaterial(); + mMaterialIsDirty = false; + } +} diff --git a/src/visual_elements/nodes/GeometryNode.h b/src/visual_elements/nodes/GeometryNode.h index 1514c4d..2a04cc4 100644 --- a/src/visual_elements/nodes/GeometryNode.h +++ b/src/visual_elements/nodes/GeometryNode.h @@ -2,6 +2,9 @@ #include "MaterialNode.h" +struct SceneInfo; +class SceneModel; + class GeometryNode : public MaterialNode { public: @@ -11,23 +14,33 @@ public: Rectangle, Circle, Arc, - Line, - Polyline + Line }; public: GeometryNode(const Point& location); - virtual ~GeometryNode() = default; + virtual ~GeometryNode(); virtual Type getType() = 0; - unsigned getStrokeThickness() const; - void setStrokeThickness(unsigned thickness); + SceneItem* getSceneItem(std::size_t idx) const override; + std::size_t getNumSceneItems() const override; + + double getStrokeThickness() const; + void setStrokeThickness(double thickness); + + void update(SceneInfo* sceneInfo) override; protected: - unsigned mStrokeThickness{0}; + virtual void createOrUpdateGeometry(SceneInfo* sceneInfo) = 0; + virtual void updateMaterial(); + virtual void updateTransform() = 0; + + double mStrokeThickness{0}; Type mType; bool mGeometryIsDirty{true}; + + std::unique_ptr mBackgroundItem; }; using GeometryNodePtr = std::unique_ptr; diff --git a/src/visual_elements/scene/SceneItem.cpp b/src/visual_elements/scene/SceneItem.cpp index aa6cf8c..dbd70e9 100644 --- a/src/visual_elements/scene/SceneItem.cpp +++ b/src/visual_elements/scene/SceneItem.cpp @@ -20,6 +20,11 @@ const Transform& SceneItem::getTransform() const return mTransform; } +double SceneItem::getOutlineThickness() const +{ + return mOutlineThickness; +} + bool SceneItem::isVisible() const { return mIsVisible; @@ -40,6 +45,11 @@ void SceneItem::setIsVisible(bool isVisible) mIsVisible = isVisible; } +void SceneItem::setOutlineThickness(double thickness) +{ + mOutlineThickness = thickness; +} + void SceneItem::setFillColor(const Color& color) { if (!mHasFillColor || mFillColor != color) diff --git a/src/visual_elements/scene/SceneItem.h b/src/visual_elements/scene/SceneItem.h index fef5fe5..42592be 100644 --- a/src/visual_elements/scene/SceneItem.h +++ b/src/visual_elements/scene/SceneItem.h @@ -28,6 +28,8 @@ public: virtual Type getType() const = 0; + double getOutlineThickness() const; + bool hasFillColor() const; bool hasOutlineColor() const; @@ -38,6 +40,8 @@ public: void setOutlineColor(const Color& color); + void setOutlineThickness(double thickness); + void setFillColor(const Color& color); void updateTransform(const Transform& transform); @@ -60,6 +64,7 @@ protected: bool mHasOutlineColor{ false }; Color mOutlineColor; + double mOutlineThickness{ 1.0 }; bool mColorIsDirty{true}; bool mTransformIsDirty{true}; diff --git a/src/web/svg/elements/SvgShapeElements.cpp b/src/web/svg/elements/SvgShapeElements.cpp index a4961b1..9522f7b 100644 --- a/src/web/svg/elements/SvgShapeElements.cpp +++ b/src/web/svg/elements/SvgShapeElements.cpp @@ -2,8 +2,9 @@ #include -SvgCircle::SvgCircle() - : SvgShapeElement("circle") +SvgCircle::SvgCircle(Type type) + : SvgShapeElement(type == Type::REGULAR ? "circle" : "ellipse"), + mType(type) { } @@ -22,7 +23,23 @@ void SvgCircle::setLocation(const Point& loc) void SvgCircle::setRadius(double rad) { - auto r = std::make_unique("r"); + if (mType == Type::REGULAR) + { + auto r = std::make_unique("r"); + r->setValue(std::to_string(rad)); + addAttribute(std::move(r)); + } + else + { + auto r = std::make_unique("rx"); + r->setValue(std::to_string(rad)); + addAttribute(std::move(r)); + } +} + +void SvgCircle::setMinorRadius(double rad) +{ + auto r = std::make_unique("ry"); r->setValue(std::to_string(rad)); addAttribute(std::move(r)); } diff --git a/src/web/svg/elements/SvgShapeElements.h b/src/web/svg/elements/SvgShapeElements.h index 1e72e0f..728f1d2 100644 --- a/src/web/svg/elements/SvgShapeElements.h +++ b/src/web/svg/elements/SvgShapeElements.h @@ -8,11 +8,21 @@ class SvgCircle : public SvgShapeElement { public: - SvgCircle(); + enum class Type + { + REGULAR, + ELLIPSE + }; + + SvgCircle(Type type = Type::REGULAR); void setLocation(const Point& loc); void setRadius(double rad); + + void setMinorRadius(double rad); +private: + Type mType{ Type::REGULAR }; }; class SvgRectangle : public SvgShapeElement