Skip to content

Commit

Permalink
Merge pull request #46 from Yvee1/SimpleSets
Browse files Browse the repository at this point in the history
SimpleSets: basic implementation
  • Loading branch information
Willem3141 authored Oct 8, 2024
2 parents 30ddc8f + 8395d51 commit 3c80a8a
Show file tree
Hide file tree
Showing 72 changed files with 5,763 additions and 14 deletions.
1 change: 1 addition & 0 deletions cartocrow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_subdirectory(necklace_map)
add_subdirectory(simplification)
add_subdirectory(flow_map)
add_subdirectory(isoline_simplification)
add_subdirectory(simplesets)

add_library(cartocrow_lib INTERFACE)
target_link_libraries(
Expand Down
1 change: 1 addition & 0 deletions cartocrow/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace cartocrow {

Color::Color() : r(0), g(0), b(0) {}
Color::Color(int r, int g, int b) : r(r), g(g), b(b) {}
Color::Color(int rgb) : r((rgb & 0xff0000) >> 16), g((rgb & 0x00ff00) >> 8), b(rgb & 0x0000ff) {}

Number<Inexact> wrapAngle(Number<Inexact> alpha, Number<Inexact> beta) {
return wrap<Inexact>(alpha, beta, beta + M_2xPI);
Expand Down
5 changes: 5 additions & 0 deletions cartocrow/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Created by tvl (t.vanlankveld@esciencecenter.nl) on 07-11-2019
#include <CGAL/Bbox_2.h>
#include <CGAL/Circle_2.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Line_2.h>
#include <CGAL/Point_2.h>
Expand Down Expand Up @@ -60,6 +61,8 @@ template <class K> using Line = CGAL::Line_2<K>;
template <class K> using Segment = CGAL::Segment_2<K>;
/// A ray in the plane. See \ref CGAL::Ray_2.
template <class K> using Ray = CGAL::Ray_2<K>;
/// An axis-aligned rectangle in the plane. See \ref CGAL::Iso_rectangle_2.
template <class K> using Rectangle = CGAL::Iso_rectangle_2<K>;

/// A polygon in the plane. See \ref CGAL::Polygon_2.
template <class K> using Polygon = CGAL::Polygon_2<K>;
Expand Down Expand Up @@ -174,6 +177,8 @@ struct Color {
Color();
/// Constructs a color.
Color(int r, int g, int b);
/// Constructs a color from a single integer (useful combined with hexadecimal literals, e.g. 0xFFFFFF).
Color(int rgb);
};

/// Wraps the given number \f$n\f$ to the interval \f$[a, b)\f$.
Expand Down
10 changes: 5 additions & 5 deletions cartocrow/isoline_simplification/symmetric_difference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum Side {
TOP = 3,
};

Side closest_side(const Point<Exact>& point, const Exact::Iso_rectangle_2& bb) {
Side closest_side(const Point<Exact>& point, const Rectangle<Exact>& bb) {
std::vector<double> dist({ to_double(point.x()) - to_double(bb.xmin()), to_double(point.y()) - to_double(bb.ymin()),
to_double(bb.xmax()) - to_double(point.x()), to_double(bb.ymax()) - to_double(point.y()) });
auto it = std::min_element(dist.begin(), dist.end());
Expand All @@ -50,7 +50,7 @@ Vector<Exact> side_direction(const Side& side) {
}
}

Point<Exact> proj_on_side(Point<Exact> p, Side side, const Exact::Iso_rectangle_2& rect) {
Point<Exact> proj_on_side(Point<Exact> p, Side side, const Rectangle<Exact>& rect) {
switch (side) {
case LEFT:
return { rect.xmin(), p.y() };
Expand All @@ -63,7 +63,7 @@ Point<Exact> proj_on_side(Point<Exact> p, Side side, const Exact::Iso_rectangle_
}
}

Point<Exact> corner(const Side& side1, const Side& side2, const Exact::Iso_rectangle_2& rect) {
Point<Exact> corner(const Side& side1, const Side& side2, const Rectangle<Exact>& rect) {
if (side1 > side2) return corner(side2, side1, rect);
auto dist = abs(side1 - side2);
if (dist == 1) {
Expand All @@ -77,7 +77,7 @@ Side next_side(const Side& side) {
return static_cast<Side>((side + 1) % 4);
}

Polygon<Exact> close_isoline(const Isoline<K>& isoline, Exact::Iso_rectangle_2& bb, Side source_side, Side target_side) {
Polygon<Exact> close_isoline(const Isoline<K>& isoline, Rectangle<Exact>& bb, Side source_side, Side target_side) {
std::vector<Point<Exact>> points;
std::transform(isoline.m_points.begin(), isoline.m_points.end(), std::back_inserter(points),
[](Point<Inexact> p) { return Point<Exact>(p.x(), p.y()); });
Expand Down Expand Up @@ -133,7 +133,7 @@ double symmetric_difference(const Isoline<K>& original, const Isoline<K>& simpli
std::transform(simplified.m_points.begin(), simplified.m_points.end(), std::back_inserter(all_points),
[](Point<Inexact> p) { return Point<Exact>(p.x(), p.y()); });

Exact::Iso_rectangle_2 bb = CGAL::bounding_box(all_points.begin(), all_points.end());
Rectangle<Exact> bb = CGAL::bounding_box(all_points.begin(), all_points.end());

auto source_side = closest_side(
Point<Exact>(original.m_points.front().x(), original.m_points.front().y()), bb);
Expand Down
2 changes: 2 additions & 0 deletions cartocrow/renderer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(SOURCES
geometry_widget.cpp
ipe_renderer.cpp
painting_renderer.cpp
function_painting.cpp
render_path.cpp
)
set(HEADERS
Expand All @@ -11,6 +12,7 @@ set(HEADERS
geometry_widget.h
ipe_renderer.h
painting_renderer.h
function_painting.h
render_path.h
)

Expand Down
10 changes: 10 additions & 0 deletions cartocrow/renderer/function_painting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "function_painting.h"

namespace cartocrow::renderer {
FunctionPainting::FunctionPainting(const std::function<void(GeometryRenderer&)>& draw_function)
: m_draw_function(draw_function) {}

void FunctionPainting::paint(GeometryRenderer& renderer) const {
m_draw_function(renderer);
}
}
18 changes: 18 additions & 0 deletions cartocrow/renderer/function_painting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CARTOCROW_FUNCTION_PAINTING_H
#define CARTOCROW_FUNCTION_PAINTING_H

#include "geometry_renderer.h"
#include "geometry_painting.h"

namespace cartocrow::renderer {
class FunctionPainting : public GeometryPainting {
public:
FunctionPainting(const std::function<void(GeometryRenderer&)>& draw_function);
void paint(renderer::GeometryRenderer& renderer) const override;

private:
const std::function<void(GeometryRenderer&)> m_draw_function;
};
}

#endif //CARTOCROW_FUNCTION_PAINTING_H
9 changes: 8 additions & 1 deletion cartocrow/renderer/geometry_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "geometry_renderer.h"
#include "ipe_renderer.h"

#include "function_painting.h"

#include <QFileDialog>
#include <QGuiApplication>
#include <QPainterPath>
Expand Down Expand Up @@ -541,7 +543,7 @@ void GeometryWidget::draw(const BezierSpline& s) {

void GeometryWidget::draw(const Ray<Inexact>& r) {
Box bounds = inverseConvertBox(rect());
auto result = intersection(r, CGAL::Iso_rectangle_2<Inexact>(Point<Inexact>(bounds.xmin(), bounds.ymin()), Point<Inexact>(bounds.xmax(), bounds.ymax())));
auto result = intersection(r, Rectangle<Inexact>(Point<Inexact>(bounds.xmin(), bounds.ymin()), Point<Inexact>(bounds.xmax(), bounds.ymax())));
if (result) {
if (const Segment<Inexact>* s = boost::get<Segment<Inexact>>(&*result)) {
int oldMode = m_style.m_mode;
Expand Down Expand Up @@ -680,6 +682,11 @@ void GeometryWidget::addPainting(std::shared_ptr<GeometryPainting> painting, con
updateLayerList();
}

void GeometryWidget::addPainting(const std::function<void(renderer::GeometryRenderer&)>& draw_function, const std::string& name) {
auto painting = std::make_shared<FunctionPainting>(draw_function);
addPainting(painting, name);
}

void GeometryWidget::clear() {
m_paintings.clear();
updateLayerList();
Expand Down
1 change: 1 addition & 0 deletions cartocrow/renderer/geometry_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class GeometryWidget : public QWidget, public GeometryRenderer {

/// Adds a new painting to this widget.
void addPainting(std::shared_ptr<GeometryPainting> painting, const std::string& name);
void addPainting(const std::function<void(renderer::GeometryRenderer&)>& draw_function, const std::string& name);
/// Removes all paintings from this widget.
void clear();

Expand Down
37 changes: 33 additions & 4 deletions cartocrow/renderer/ipe_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

#include "ipe_renderer.h"

#include "function_painting.h"
#include "geometry_renderer.h"

#include <ipeattributes.h>
Expand Down Expand Up @@ -74,9 +75,18 @@ void IpeRenderer::save(const std::filesystem::path& file) {
m_alphaSheet->setName("alpha-values");
document.cascade()->insert(2, m_alphaSheet);
setFillOpacity(255); // add default alpha to style sheet
setStrokeOpacity(255); // add default alpha to style sheet

m_page = new ipe::Page();
for (auto painting : m_paintings) {
document.push_back(m_page);

int current_page = 0;

for (auto painting : m_paintings) { // Assumes m_paintings are ordered in increasing page_index
while (painting.page_index > current_page) {
m_page = new ipe::Page();
document.push_back(m_page);
}
pushStyle();
if (auto name = painting.name) {
m_page->addLayer(name->c_str());
Expand All @@ -88,7 +98,6 @@ void IpeRenderer::save(const std::filesystem::path& file) {
popStyle();
}

document.push_back(m_page);
document.save(file.string().c_str(), ipe::FileFormat::Xml, 0);
}

Expand Down Expand Up @@ -320,17 +329,37 @@ ipe::AllAttributes IpeRenderer::getAttributesForStyle() const {
attributes.iStroke = ipe::Attribute(ipe::Color(m_style.m_strokeColor));
attributes.iFill = ipe::Attribute(ipe::Color(m_style.m_fillColor));
attributes.iOpacity = m_style.m_fillOpacity;
attributes.iStrokeOpacity = m_style.m_strokeOpacity;
return attributes;
}


void IpeRenderer::addPainting(const std::function<void(renderer::GeometryRenderer&)>& draw_function) {
auto painting = std::make_shared<FunctionPainting>(draw_function);
addPainting(painting);
}

void IpeRenderer::addPainting(const std::function<void(renderer::GeometryRenderer&)>& draw_function, const std::string& name) {
auto painting = std::make_shared<FunctionPainting>(draw_function);
addPainting(painting, name);
}

void IpeRenderer::addPainting(const std::shared_ptr<GeometryPainting>& painting) {
m_paintings.push_back(DrawnPainting{painting});
m_paintings.push_back(DrawnPainting{painting, std::nullopt, m_page_index});
}

void IpeRenderer::addPainting(const std::shared_ptr<GeometryPainting>& painting, const std::string& name) {
std::string spaceless;
std::replace_copy(name.begin(), name.end(), std::back_inserter(spaceless), ' ', '_');
m_paintings.push_back(DrawnPainting{painting, spaceless});
m_paintings.push_back(DrawnPainting{painting, spaceless, m_page_index});
}

void IpeRenderer::nextPage() {
++m_page_index;
}

int IpeRenderer::currentPage() {
return m_page_index;
}

std::string IpeRenderer::escapeForLaTeX(const std::string& text) const {
Expand Down
11 changes: 11 additions & 0 deletions cartocrow/renderer/ipe_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,16 @@ class IpeRenderer : public GeometryRenderer {
void setFill(Color color) override;
void setFillOpacity(int alpha) override;

void addPainting(const std::function<void(renderer::GeometryRenderer&)>& draw_function);
void addPainting(const std::function<void(renderer::GeometryRenderer&)>& draw_function, const std::string& name);
void addPainting(const std::shared_ptr<GeometryPainting>& painting);
void addPainting(const std::shared_ptr<GeometryPainting>& painting, const std::string& name);

/// Paintings will be added to a new page.
void nextPage();
/// Returns the current page index.
int currentPage();

private:
/// Converts a polygon to an Ipe curve.
ipe::Curve* convertPolygonToCurve(const Polygon<Inexact>& p) const;
Expand All @@ -119,6 +126,8 @@ class IpeRenderer : public GeometryRenderer {
std::shared_ptr<GeometryPainting> m_painting;
/// The name of the painting displayed as a layer name in ipe.
std::optional<std::string> name;
/// The Ipe page the painting will be drawn onto.
int page_index;
};

/// The paintings we're drawing.
Expand All @@ -138,6 +147,8 @@ class IpeRenderer : public GeometryRenderer {
ipe::StyleSheet* m_alphaSheet;
/// The index of the Ipe layer we are currently drawing to.
int m_layer;
/// The index of the Ipe page a painting will get drawn to.
int m_page_index = 0;
};

} // namespace cartocrow::renderer
Expand Down
55 changes: 55 additions & 0 deletions cartocrow/simplesets/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
set(SOURCES
cat_point.cpp
types.cpp
parse_input.cpp
patterns/pattern.cpp
patterns/single_point.cpp
patterns/matching.cpp
patterns/island.cpp
patterns/bank.cpp
dilated/dilated_poly.cpp
helpers/approximate_convex_hull.cpp
helpers/cs_curve_helpers.cpp
helpers/cs_polygon_helpers.cpp
helpers/cs_polyline_helpers.cpp
helpers/poly_line_gon_intersection.cpp
partition_algorithm.cpp
partition_painting.cpp
drawing_algorithm.cpp
grow_circles.cpp
)
set(HEADERS
types.h
settings.h
cat_point.h
parse_input.h
patterns/pattern.h
patterns/poly_pattern.h
patterns/single_point.h
patterns/matching.h
patterns/island.h
patterns/bank.h
dilated/dilated_poly.h
helpers/approximate_convex_hull.h
helpers/arrangement_helpers.h
helpers/point_voronoi_helpers.h
helpers/poly_line_gon_intersection.h
helpers/cropped_voronoi.h
helpers/cs_curve_helpers.h
helpers/cs_polygon_helpers.h
helpers/cs_polyline_helpers.h
partition_algorithm.h
partition_painting.h
partition.h
drawing_algorithm.h
general_polyline.h
grow_circles.h
)

add_library(simplesets ${SOURCES})
target_link_libraries(simplesets
PUBLIC core
)

cartocrow_install_module(simplesets)
install(FILES ${HEADERS} DESTINATION ${CARTOCROW_INSTALL_DIR}/simplesets)
7 changes: 7 additions & 0 deletions cartocrow/simplesets/cat_point.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "cat_point.h"

namespace cartocrow::simplesets {
std::ostream& operator<<(std::ostream& os, CatPoint const& catPoint) {
return os << catPoint.category << " " << catPoint.point;
}
}
18 changes: 18 additions & 0 deletions cartocrow/simplesets/cat_point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CARTOCROW_CAT_POINT_H
#define CARTOCROW_CAT_POINT_H

#include "types.h"

namespace cartocrow::simplesets {
/// Categorical point
struct CatPoint {
unsigned int category;
Point<Inexact> point;
CatPoint(unsigned int category, Point<Inexact> point) : category(category), point(point) {};
bool operator==(const CatPoint&) const = default;
};

std::ostream& operator<<(std::ostream& os, CatPoint const& catPoint);
}

#endif //CARTOCROW_CAT_POINT_H
Loading

0 comments on commit 3c80a8a

Please sign in to comment.