Skip to content

Commit

Permalink
DONOT COMMIT
Browse files Browse the repository at this point in the history
  • Loading branch information
casperlamboo committed Jun 5, 2023
1 parent fc95172 commit bac675d
Show file tree
Hide file tree
Showing 13 changed files with 672 additions and 120 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ set(engine_SRCS # Except main.cpp.
src/utils/ToolpathVisualizer.cpp
src/utils/VoronoiUtils.cpp
src/utils/VoxelUtils.cpp
)
include/utils/BoundedAreaHierarchy.h)

add_library(_CuraEngine STATIC ${engine_SRCS} ${engine_PB_SRCS})
use_threads(_CuraEngine)
Expand Down
6 changes: 6 additions & 0 deletions include/SkeletalTrapezoidation.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ class SkeletalTrapezoidation
*/
void constructFromPolygons(const Polygons& polys);

bool validate() const;


void filterCells(vd_t& voronoi_diagram, std::vector<Point>& points, std::vector<Segment>& segments);


/*!
* mapping each voronoi VD edge to the corresponding halfedge HE edge
* In case the result segment is discretized, we map the VD edge to the *last* HE edge
Expand Down
7 changes: 6 additions & 1 deletion include/SkeletalTrapezoidationGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "SkeletalTrapezoidationEdge.h"
#include "SkeletalTrapezoidationJoint.h"
#include "utils/HalfEdgeGraph.h"
#include "utils/PolygonsSegmentIndex.h"

namespace cura
{
Expand Down Expand Up @@ -71,6 +72,8 @@ class SkeletalTrapezoidationGraph: public HalfEdgeGraph<SkeletalTrapezoidationJo
using node_t = STHalfEdgeNode;
public:

std::vector<PolygonsSegmentIndex> segments;

/*!
* If an edge is too small, collapse it and its twin and fix the surrounding edges to ensure a consistent graph.
*
Expand All @@ -83,7 +86,7 @@ class SkeletalTrapezoidationGraph: public HalfEdgeGraph<SkeletalTrapezoidationJo
*/
void collapseSmallEdges(coord_t snap_dist = 5);

void makeRib(edge_t*& prev_edge, Point start_source_point, Point end_source_point, bool is_next_to_start_or_end);
void makeRib(edge_t*& prev_edge, Point start_source_point, Point end_source_point);

/*!
* Insert a node into the graph and connect it to the input polygon using ribs
Expand All @@ -92,6 +95,8 @@ class SkeletalTrapezoidationGraph: public HalfEdgeGraph<SkeletalTrapezoidationJo
*/
edge_t* insertNode(edge_t* edge, Point mid, coord_t mide_node_bead_count);

void drawGraph() const;

/*!
* Return the first and last edge of the edges replacing \p edge pointing to the same node
*/
Expand Down
10 changes: 10 additions & 0 deletions include/utils/AABB.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ class AABB
*/
bool hit(const AABB& other) const;

/*!
* Check whether this aabb overlaps with another.
*
* In the boundary case true is returned.
*
* \param other the aabb to check for overlaps with
* \return Whether the two aabbs overlap
*/
coord_t distanceSquared(const Point& p) const;

/*!
* \brief Includes the specified point in the bounding box.
*
Expand Down
210 changes: 210 additions & 0 deletions include/utils/BoundedAreaHierarchy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Copyright (c) 2023 Ultimaker B.V.
// CuraEngine is released under the terms of the AGPLv3 or higher

#include "AABB.h"
#include "utils/IntPoint.h"
#include <sstream>

#ifndef CURAENGINE_BOUNDEDAREAHIERARCHY_H
#define CURAENGINE_BOUNDEDAREAHIERARCHY_H

namespace cura
{
template<typename edge_data>
class BoundedAreaHierarchy
{
struct Edge
{
const Point start;
const Point end;

Edge(const Point start, const Point end) : start(start), end(end)
{
}

AABB getAABB() const
{
AABB aabb;
aabb.include(start);
aabb.include(end);
return aabb;
}
};

struct Node
{
AABB aabb;
// if child_a is 0, then this is a leaf node, child_b
// is the index of the edge in the edges vector
// this works as the first element of the bounded_area_hierarchy-tree
// is the root; no child points to the root in a tree data structure
int child_a;
int child_b;
};

std::vector<std::pair<Edge, edge_data>> edges;
std::vector<Node> bounded_area_hierarchy;

/*!
* \brief calculate_bounds
* \param start index of the first edge to include
* \param end index of the last edge to include (inclusive)
* \return the bounding box of the edges
*/
AABB calculate_bounds(const int start, const int end)
{
AABB aabb;
for (int i = start; i <= end; i++)
{
aabb.include(edges[i].first.getAABB());
}
return aabb;
}

// Quicksort algorithm based on https://www.geeksforgeeks.org/cpp-program-for-quicksort/
void quick_sort_primitives(const int axis, const int first, const int last) {
if (first < last) {
float pivot = get_axis(axis, vertices[indices[(first + last) / 2] * 3]);

int i = first - 1;

for (int j = first; j <= last - 1; j ++) {
if (get_axis(axis, centroids[indices[j]]) <= pivot) {
i++;
Swap(&indices[i], &indices[j]);
}
}
Swap(&indices[i + 1], &indices[last]);

int pivotIndex = i + 1;

QuickSortPrimitives(axis, first, pivotIndex - 1);
QuickSortPrimitives(axis, pivotIndex + 1, last);
}
}

void construct_search_structure(const int start, const int end)
{
if (end - start == 1)
{
bounded_area_hierarchy[start].child_a = 0;
bounded_area_hierarchy[start].child_b = start;
return;
}

edges.sort(start, end, [](const std::pair<Edge, edge_data>& a, const std::pair<Edge, edge_data>& b) {
return a.first.getAABB().getCenter().x < b.first.getAABB().getCenter().x;
});


const int middle = (start + end) / 2;
construct_search_structure(start, middle);
construct_search_structure(middle, end);
bounded_area_hierarchy[start].child_a = middle;
bounded_area_hierarchy[start].child_b = 0;
bounded_area_hierarchy[start].aabb = bounded_area_hierarchy[start].aabb.combine(bounded_area_hierarchy[middle].aabb);
}

public:
BoundedAreaHierarchy();

/*!
* \brief insert_edge inserts an edge into the bounded area hierarchy
* \param start the start point of the edge
* \param end the end point of the edge
* \param data the data associated with the edge
*/
void insert_edge(const Point start, const Point end, const edge_data data)
{
edges.emplace_back(Edge(start, end), data);
}

void draw_debug_svg(const std::string file_name)
{
if (! bounded_area_hierarchy.empty()) return;

AABB outer_aabb = bounded_area_hierarchy[0].aabb;
outer_aabb.expand(1000);
SVG svg(file_name, outer_aabb);

std::vector<int> stack;
stack.push_back(0);
while (!stack.empty())
{
const int node_index = stack.back();
stack.pop_back();
const Node& node = bounded_area_hierarchy[node_index];
const AABB aabb = node.aabb;
const Polygon polygons = aabb.toPolygon();

svg.writePolygon(polygons);

if (node.child_a == 0)
{
const int edge_index = node.child_b;
const Edge& edge = edges[edge_index].first;
svg.writeLine(edge.start, edge.end);
}
else
{
stack.push_back(node.child_a);
stack.push_back(node.child_b);
}
}
}

void construct_search_structure()
{
bounded_area_hierarchy.clear();
bounded_area_hierarchy.reserve(edges.size() * 2 - 1);
for (const auto& edge : edges)
{
bounded_area_hierarchy.emplace_back(Node{ edge.first.getAABB(), 0, 0 });
}
construct_search_structure(0, edges.size());
}

void radius_search(const Point point, const coord_t radius, std::vector<edge_data> result) const
{
std::vector<int> stack;
stack.push_back(0);
while (! stack.empty())
{
const int node_index = stack.back();
stack.pop_back();
const Node& node = bounded_area_hierarchy[node_index];
if (node.child_a == 0)
{
const int edge_index = node.child_b;
const Edge& edge = edges[edge_index].first;
if (edge.start.distance_to(point) < radius || edge.end.distance_to(point) < radius)
{
result.push_back(edges[edge_index].second);
}
}
else
{
const Node& child_a = bounded_area_hierarchy[node.child_a];
const Node& child_b = bounded_area_hierarchy[node.child_b];
if (child_a.aabb.distance_to(point) < radius)
{
stack.push_back(node.child_a);
}
if (child_b.aabb.distance_to(point) < radius)
{
stack.push_back(node.child_b);
}
}
}
}

std::vector<edge_data> radius_search(const Point point, const coord_t radius) const
{
std::vector<edge_data> result;
radius_search(point, radius, result);
return result;
}
};
} // namespace cura

#endif // CURAENGINE_BOUNDEDAREAHIERARCHY_H
16 changes: 16 additions & 0 deletions include/utils/polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ class PolygonRef : public ConstPolygonRef

void removeCollinearPoints(const AngleRadians max_deviation_angle);

void removeSmallEdges(const unsigned int max_dist);

/*!
* Removes consecutive line segments with same orientation and changes this polygon.
*
Expand Down Expand Up @@ -1158,6 +1160,20 @@ class Polygons
}
}
}

void removeSmallEdges(const unsigned int max_dist = 10)
{
Polygons& thiss = *this;
for (size_t p = 0; p < size(); p++)
{
thiss[p].removeSmallEdges(max_dist);
if (thiss[p].size() < 3)
{
remove(p);
p--;
}
}
}
public:

void scale(const Ratio& ratio)
Expand Down
Loading

0 comments on commit bac675d

Please sign in to comment.