Some simple svg output.

This commit is contained in:
James Grogan 2022-12-08 13:52:37 +00:00
parent 1fc730d413
commit b101a5f87c
11 changed files with 87 additions and 6 deletions

View file

@ -32,7 +32,7 @@ public:
virtual std::vector<unsigned> getEdgeIds() const = 0; virtual std::vector<unsigned> getEdgeIds() const = 0;
//virtual std::vector<Point> getNodeLocations() const = 0; virtual std::vector<Point> getNodeLocations() const = 0;
protected: protected:
unsigned mId{0}; unsigned mId{0};

View file

@ -1,6 +1,7 @@
#include "TriFace.h" #include "TriFace.h"
#include "Edge.h" #include "Edge.h"
#include "Node.h"
TriFace::TriFace(Edge* edge0, Edge* edge1, Edge* edge2, unsigned id) TriFace::TriFace(Edge* edge0, Edge* edge1, Edge* edge2, unsigned id)
: AbstractFace(id), : AbstractFace(id),
@ -73,3 +74,8 @@ void TriFace::associateWidthEdges()
mEdge1->associateFace(mId); mEdge1->associateFace(mId);
mEdge2->associateFace(mId); mEdge2->associateFace(mId);
} }
std::vector<Point> TriFace::getNodeLocations() const
{
return {mEdge0->getNode0()->getPoint(), mEdge0->getNode1()->getPoint(), mEdge1->getNode1()->getPoint()};
}

View file

@ -26,6 +26,8 @@ public:
unsigned getEdge2Id () const; unsigned getEdge2Id () const;
std::vector<unsigned> getEdgeIds() const override; std::vector<unsigned> getEdgeIds() const override;
std::vector<Point> getNodeLocations() const override;
private: private:
Edge* mEdge0{nullptr}; Edge* mEdge0{nullptr};
Edge* mEdge1{nullptr}; Edge* mEdge1{nullptr};

View file

@ -8,6 +8,7 @@
#include "AbstractMesh.h" #include "AbstractMesh.h"
#include "TriMesh.h" #include "TriMesh.h"
#include "AbstractFace.h"
#include "SvgShapeElements.h" #include "SvgShapeElements.h"
@ -16,22 +17,44 @@ std::unique_ptr<SvgDocument> SvgConverter::convert(Scene* scene)
auto doc = std::make_unique<SvgDocument>(); auto doc = std::make_unique<SvgDocument>();
scene->update(); scene->update();
for(auto item : scene->getItems()) for(auto item : scene->getItems())
{ {
if (item->getType() == SceneItem::Type::MODEL) if (item->getType() == SceneItem::Type::MODEL)
{ {
auto mesh = dynamic_cast<SceneModel*>(item)->getMesh(); auto mesh = dynamic_cast<SceneModel*>(item)->getMesh();
auto transform = item->getTransform();
if (mesh->getType() == AbstractMesh::MeshType::TRI) if (mesh->getType() == AbstractMesh::MeshType::TRI)
{ {
for(const auto& face : dynamic_cast<TriMesh*>(mesh)->getFaces()) for(const auto& face : dynamic_cast<TriMesh*>(mesh)->getFaces())
{ {
auto svg_tri = std::make_unique<SvgPolygon>(); auto svg_tri = std::make_unique<SvgPolygon>();
// Get node locs for face
// Convert to pixel std::vector<DiscretePoint> points(3);
unsigned count = 0;
// add polygon to doc auto face_locs = face->getNodeLocations();
for(auto& loc : face_locs)
{
const auto x = loc.getX() * transform.getScaleX() + transform.getLocation().getX();
const auto y = loc.getY() * transform.getScaleY() + transform.getLocation().getY();
points[count] = {static_cast<unsigned>(x), static_cast<unsigned>(y)};
count++;
}
svg_tri->setPoints(points);
svg_tri->setFill(item->getColor());
doc->getRoot()->addChild(std::move(svg_tri));
if (scene->shouldShowMeshOutline())
{
auto mesh_outline = std::make_unique<SvgPolyline>();
points.push_back(points[0]);
mesh_outline->setPoints(points);
mesh_outline->setStrokeColor({0, 0, 0});
mesh_outline->setStrokeWidth(0.1);
doc->getRoot()->addChild(std::move(mesh_outline));
}
} }
} }
} }

View file

@ -23,9 +23,21 @@ public:
bool isEmpty() const; bool isEmpty() const;
bool shouldShowMeshOutline() const
{
return mShowMeshOutline;
}
void setShowMeshOutline(bool shouldShow)
{
mShowMeshOutline = shouldShow;
}
void update(FontsManager* fontsManager = nullptr); void update(FontsManager* fontsManager = nullptr);
private: private:
void updateNode(AbstractVisualNode* node, FontsManager* fontsManager); void updateNode(AbstractVisualNode* node, FontsManager* fontsManager);
std::unique_ptr<RootNode> mRootNode; std::unique_ptr<RootNode> mRootNode;
std::vector<SceneItem*> mSceneItems; std::vector<SceneItem*> mSceneItems;
bool mShowMeshOutline{false};
}; };

View file

@ -61,6 +61,7 @@ void CircleNode::update(FontsManager* fontsManager)
{ {
mBackgroundItem = std::make_unique<SceneModel>(std::move(mesh)); mBackgroundItem = std::make_unique<SceneModel>(std::move(mesh));
mBackgroundItem->setName(mName + "_Model"); mBackgroundItem->setName(mName + "_Model");
mBackgroundItem->updateUniformColor(mFillColor);
} }
else else
{ {

View file

@ -21,7 +21,7 @@ void SvgShapeElement::setFill(const Color& fill)
addAttribute(std::move(attr)); addAttribute(std::move(attr));
} }
void SvgShapeElement::setStrokeWidth(unsigned width) void SvgShapeElement::setStrokeWidth(double width)
{ {
auto attr = std::make_unique<XmlAttribute>("stroke-width"); auto attr = std::make_unique<XmlAttribute>("stroke-width");
attr->setValue(std::to_string(width)); attr->setValue(std::to_string(width));

View file

@ -10,7 +10,7 @@ public:
void setFill(const Color& fill); void setFill(const Color& fill);
void setStrokeWidth(unsigned width); void setStrokeWidth(double width);
void setStrokeColor(const Color& stroke); void setStrokeColor(const Color& stroke);

View file

@ -83,3 +83,24 @@ void SvgPolygon::setPoints(const std::vector<DiscretePoint>& locs)
addAttribute(std::move(points)); addAttribute(std::move(points));
} }
SvgPolyline::SvgPolyline()
: SvgShapeElement("polyline")
{
auto fill = std::make_unique<XmlAttribute>("fill");
fill->setValue("none");
addAttribute(std::move(fill));
}
void SvgPolyline::setPoints(const std::vector<DiscretePoint>& locs)
{
auto points = std::make_unique<XmlAttribute>("points");
std::stringstream sstr;
for (const auto& loc : locs)
{
sstr << loc.GetX() << "," << loc.GetY() << " ";
}
points->setValue(sstr.str());
addAttribute(std::move(points));
}

View file

@ -34,3 +34,11 @@ public:
void setPoints(const std::vector<DiscretePoint>& loc); void setPoints(const std::vector<DiscretePoint>& loc);
}; };
class SvgPolyline : public SvgShapeElement
{
public:
SvgPolyline();
void setPoints(const std::vector<DiscretePoint>& loc);
};

View file

@ -4,6 +4,7 @@
#include "Scene.h" #include "Scene.h"
#include "CircleNode.h" #include "CircleNode.h"
#include "RectangleNode.h"
#include "File.h" #include "File.h"
@ -13,12 +14,19 @@
TEST_CASE(TestSvgConverter, "[publishing]") TEST_CASE(TestSvgConverter, "[publishing]")
{ {
Scene scene; Scene scene;
scene.setShowMeshOutline(true);
CircleNode circle({10, 10}, 20); CircleNode circle({10, 10}, 20);
circle.setFillColor({255, 0, 0});
scene.addNode(&circle); scene.addNode(&circle);
//RectangleNode rectangle({10, 10}, 20, 20);
//rectangle.setFillColor({255, 0, 0});
//scene.addNode(&rectangle);
SvgConverter converter; SvgConverter converter;
auto svg_document = converter.convert(&scene); auto svg_document = converter.convert(&scene);
svg_document->setViewBox(0, 0, 100, 100);
SvgWriter writer; SvgWriter writer;
auto content = writer.toString(svg_document.get()); auto content = writer.toString(svg_document.get());