Commit dc0aee72 authored by JasonPries's avatar JasonPries
Browse files

Fix memory leaks

parent 8e5011bb
...@@ -363,7 +363,7 @@ bool Mesh::is_protruding(size_t ei) const { ...@@ -363,7 +363,7 @@ bool Mesh::is_protruding(size_t ei) const {
size_t nxt = next(next(ei)); size_t nxt = next(next(ei));
while (nxt != prev(ei)) { while (nxt != prev(ei)) {
Point const p4 = point(nxt); Point const p4 = base(nxt);
double v2x = p2.X - p4.X; double v2x = p2.X - p4.X;
double v2y = p2.Y - p4.Y; double v2y = p2.Y - p4.Y;
...@@ -566,7 +566,7 @@ void Mesh::create_boundary_polygon() { ...@@ -566,7 +566,7 @@ void Mesh::create_boundary_polygon() {
bool any_split = true; bool any_split = true;
while (any_split) { while (any_split) {
any_split = false; any_split = false;
for (size_t i = 0; i != Edges.size(); ++i) { for (size_t i = 0; i != Edges.size() - 1; ++i) {
for (size_t j = i + 1; j != Edges.size(); ++j) { for (size_t j = i + 1; j != Edges.size(); ++j) {
if (are_intersecting(i, j)) { if (are_intersecting(i, j)) {
any_split = true; any_split = true;
......
...@@ -100,7 +100,13 @@ public: ...@@ -100,7 +100,13 @@ public:
Point const base(size_t ei) const { return Points[node(ei)]; }; Point const base(size_t ei) const { return Points[node(ei)]; };
Point const point(size_t i) const { return Points[i]; }; Point const point(size_t i) const {
if (i >= Points.size()) {
throw;
} else {
return Points[i];
}
}; //return Points[i]; };
Point const point(Edge const e) const { return Points[e.Node]; }; Point const point(Edge const e) const { return Points[e.Node]; };
......
...@@ -199,7 +199,7 @@ bool CircularArc::on_manifold(const double x, const double y) const { ...@@ -199,7 +199,7 @@ bool CircularArc::on_manifold(const double x, const double y) const {
} }
} }
bool CircularArc::is_identical(std::shared_ptr<Curve> c) const { bool CircularArc::is_identical(std::shared_ptr<Curve> const &c) const {
//const CircularArc *cc = dynamic_cast<const CircularArc *>(c); //const CircularArc *cc = dynamic_cast<const CircularArc *>(c);
auto cc = std::dynamic_pointer_cast<CircularArc>(c); auto cc = std::dynamic_pointer_cast<CircularArc>(c);
...@@ -218,7 +218,7 @@ bool CircularArc::is_identical(std::shared_ptr<Curve> c) const { ...@@ -218,7 +218,7 @@ bool CircularArc::is_identical(std::shared_ptr<Curve> c) const {
} }
} }
bool CircularArc::is_identical(std::shared_ptr<Curve> c, std::shared_ptr<Vertex> origin, const double angle) const { bool CircularArc::is_identical(std::shared_ptr<Curve> const &c, std::shared_ptr<Vertex> const &origin, double const angle) const {
//const CircularArc *cc = dynamic_cast<const CircularArc *>(c); //const CircularArc *cc = dynamic_cast<const CircularArc *>(c);
auto cc = std::dynamic_pointer_cast<CircularArc>(c); auto cc = std::dynamic_pointer_cast<CircularArc>(c);
...@@ -249,7 +249,7 @@ bool CircularArc::is_identical(const double r, const double xc, const double yc, ...@@ -249,7 +249,7 @@ bool CircularArc::is_identical(const double r, const double xc, const double yc,
&& abs(end()->y() - ye) < tol; && abs(end()->y() - ye) < tol;
} }
bool CircularArc::is_coincident(std::shared_ptr<Curve> c) const { bool CircularArc::is_coincident(std::shared_ptr<Curve> const &c) const {
//const CircularArc *cc = dynamic_cast<const CircularArc *>(c); //const CircularArc *cc = dynamic_cast<const CircularArc *>(c);
auto cc = std::dynamic_pointer_cast<CircularArc>(c); auto cc = std::dynamic_pointer_cast<CircularArc>(c);
......
...@@ -60,11 +60,11 @@ public: ...@@ -60,11 +60,11 @@ public:
using Curve::on_segment; using Curve::on_segment;
bool is_identical(std::shared_ptr<Curve> c) const override; bool is_identical(std::shared_ptr<Curve> const &c) const override;
bool is_identical(std::shared_ptr<Curve> c, std::shared_ptr<Vertex> origin, const double angle) const override; bool is_identical(std::shared_ptr<Curve> const &c, std::shared_ptr<Vertex> const &origin, double const angle) const override;
bool is_coincident(std::shared_ptr<Curve> c) const override; bool is_coincident(std::shared_ptr<Curve> const &c) const override;
std::shared_ptr<Curve> clone() const override { return std::make_shared<CircularArc>(this); }; std::shared_ptr<Curve> clone() const override { return std::make_shared<CircularArc>(this); };
......
#include "Sketch.hpp" #include "Sketch.hpp"
Constellation::Constellation(const Sketch *s) { Constellation::Constellation(Sketch const *s) {
for (size_t i = 0; i != s->size_verticies(); ++i) { for (size_t i = 0; i != s->size_verticies(); ++i) {
Stars.push_back(Star{s->vertex(i), s}); Stars.push_back(Star{s->vertex(i), s});
} }
...@@ -8,8 +8,6 @@ Constellation::Constellation(const Sketch *s) { ...@@ -8,8 +8,6 @@ Constellation::Constellation(const Sketch *s) {
} }
bool Constellation::twin(std::list<Star>::iterator &s_out, std::list<Branch>::iterator &b_out) { bool Constellation::twin(std::list<Star>::iterator &s_out, std::list<Branch>::iterator &b_out) {
std::shared_ptr<Curve> c = b_out->Path; // #TODO: Local variable 'c' is only assigned but never accessed
for (auto s = Stars.begin(); s != Stars.end(); ++s) { for (auto s = Stars.begin(); s != Stars.end(); ++s) {
if (s != s_out) { if (s != s_out) {
for (auto b = s->begin(); b != s->end(); ++b) { for (auto b = s->begin(); b != s->end(); ++b) {
...@@ -75,7 +73,7 @@ void Constellation::pop(std::shared_ptr<Curve> c) { ...@@ -75,7 +73,7 @@ void Constellation::pop(std::shared_ptr<Curve> c) {
} }
} }
bool Constellation::boundary(std::shared_ptr<Contour> c) { std::shared_ptr<Contour> Constellation::boundary() {
std::vector<std::shared_ptr<Curve>> curves; std::vector<std::shared_ptr<Curve>> curves;
std::vector<bool> orientation; std::vector<bool> orientation;
...@@ -95,24 +93,23 @@ bool Constellation::boundary(std::shared_ptr<Contour> c) { ...@@ -95,24 +93,23 @@ bool Constellation::boundary(std::shared_ptr<Contour> c) {
bool has_twin = twin(s, b); bool has_twin = twin(s, b);
if (!has_twin) { if (!has_twin) {
return false; return std::make_shared<Contour>();
} else { } else {
b = s->prev(b); b = s->prev(b);
angle += 2.0 * M_PI - b->Angle; angle += 2.0 * M_PI - b->Angle;
} }
if (b->Path == curves.back()) { //excludes self-closed contours if (b->Path == curves.back()) { //excludes self-closed contours
return false; return std::make_shared<Contour>();
} else if (b->Path == curves[0]) { } else if (b->Path == curves[0]) {
double tol = (FLT_EPSILON * M_PI * (curves.size() - 1)); double tol = (FLT_EPSILON * M_PI * (curves.size() - 1));
double expected = (curves.size() - 2) * M_PI; double expected = (curves.size() - 2) * M_PI;
double diff = abs(angle - expected); double diff = abs(angle - expected);
if (diff <= tol) { if (diff <= tol) {
c->initialize(curves, orientation); return std::make_shared<Contour>(curves,orientation);
return true;
} else { } else {
return false; return std::make_shared<Contour>();
} }
} else { } else {
curves.push_back(b->Path); curves.push_back(b->Path);
...@@ -121,20 +118,20 @@ bool Constellation::boundary(std::shared_ptr<Contour> c) { ...@@ -121,20 +118,20 @@ bool Constellation::boundary(std::shared_ptr<Contour> c) {
} }
} }
bool Constellation::contours(std::vector<std::shared_ptr<Contour>> &contours) { std::vector<std::shared_ptr<Contour>> Constellation::contours() {
std::vector<std::shared_ptr<Contour>> contours;
std::vector<std::shared_ptr<Curve>> contour_curves; std::vector<std::shared_ptr<Curve>> contour_curves;
std::vector<bool> orientation; std::vector<bool> orientation;
while (size() > 0) { while (size() > 0) {
bool success = find_closed_contour(contour_curves, orientation); if (find_closed_contour(contour_curves, orientation)) {
if (success) {
contours.push_back(std::make_shared<Contour>(contour_curves, orientation)); contours.push_back(std::make_shared<Contour>(contour_curves, orientation));
} else { } else {
return false; return std::vector<std::shared_ptr<Contour>>(); // TODO: Ugly multiple return points
} }
} }
return true; return contours;
} }
bool Constellation::find_closed_contour(std::vector<std::shared_ptr<Curve>> &curves, std::vector<bool> &orientation) { bool Constellation::find_closed_contour(std::vector<std::shared_ptr<Curve>> &curves, std::vector<bool> &orientation) {
......
...@@ -8,18 +8,18 @@ public: ...@@ -8,18 +8,18 @@ public:
// Constructors // Constructors
Constellation() {}; Constellation() {};
Constellation(const Sketch *s); Constellation(Sketch const *s);
size_t size() { return Stars.size(); }; size_t size() { return Stars.size(); };
bool contours(std::vector<std::shared_ptr<Contour>> &contours); std::vector<std::shared_ptr<Contour>> contours();
bool boundary(std::shared_ptr<Contour>); std::shared_ptr<Contour> boundary();
private: private:
std::list<Star> Stars; std::list<Star> Stars;
void pop(std::shared_ptr<Curve> c = nullptr); void pop(std::shared_ptr<Curve> c = std::shared_ptr<Curve>());
bool twin(std::list<Star>::iterator &s_out, std::list<Branch>::iterator &b_out); bool twin(std::list<Star>::iterator &s_out, std::list<Branch>::iterator &b_out);
......
...@@ -65,10 +65,13 @@ Contour::Contour(std::vector<std::shared_ptr<Curve>> const &c, std::vector<bool> ...@@ -65,10 +65,13 @@ Contour::Contour(std::vector<std::shared_ptr<Curve>> const &c, std::vector<bool>
} }
bool Contour::initialize(std::vector<std::shared_ptr<Curve>> const &c, std::vector<bool> const &dir) { bool Contour::initialize(std::vector<std::shared_ptr<Curve>> const &c, std::vector<bool> const &dir) {
Curves.resize(0); Curves = std::vector<std::shared_ptr<Curve>>();
Orientation.resize(0); Curves.reserve(c.size());
Orientation = std::vector<bool>();
Orientation.reserve(c.size());
for (size_t i = 0; i < c.size(); ++i) { for (size_t i = 0; i != c.size(); ++i) {
Curves.push_back(c[i]); Curves.push_back(c[i]);
Orientation.push_back(dir[i]); Orientation.push_back(dir[i]);
......
#include "Sketch.hpp" #include "Sketch.hpp"
bool Curve::on_segment(std::shared_ptr<Vertex> v) const { bool Curve::on_segment(std::shared_ptr<Vertex> const &v) const {
return on_segment(v->x(), v->y()); return on_segment(v->x(), v->y());
} }
bool Curve::on_segment(std::shared_ptr<Vertex> v, std::shared_ptr<Vertex> origin, const double angle) const { bool Curve::on_segment(std::shared_ptr<Vertex> const &v, std::shared_ptr<Vertex> const &origin, double const angle) const {
double x, y; double x, y;
std::tie(x, y) = v->rotate(origin, angle); std::tie(x, y) = v->rotate(origin, angle);
return on_segment(x, y); return on_segment(x, y);
} }
bool Curve::on_manifold(std::shared_ptr<Vertex> v) const { bool Curve::on_manifold(std::shared_ptr<Vertex> const &v) const {
return on_manifold(v->x(), v->y()); return on_manifold(v->x(), v->y());
} }
bool Curve::on_manifold(std::shared_ptr<Vertex> v, std::shared_ptr<Vertex> origin, const double angle) const { bool Curve::on_manifold(std::shared_ptr<Vertex> const &v, std::shared_ptr<Vertex> const &origin, double const angle) const {
double x, y; double x, y;
std::tie(x, y) = v->rotate(origin, angle); std::tie(x, y) = v->rotate(origin, angle);
......
...@@ -8,9 +8,9 @@ public: ...@@ -8,9 +8,9 @@ public:
friend class MirrorCopy; friend class MirrorCopy;
// Constructors // Constructors
Curve() : Start(nullptr), End(nullptr) {}; Curve() : Start(std::make_shared<Vertex>()), End(std::make_shared<Vertex>()), ForConstruction(false) {};
Curve(std::shared_ptr<Vertex> v0, std::shared_ptr<Vertex>v1, bool fc = false) : Start(v0), End(v1), ForConstruction(fc) {}; Curve(std::shared_ptr<Vertex> v0, std::shared_ptr<Vertex> v1, bool fc = false) : Start(v0), End(v1), ForConstruction(fc) {};
//Curve(Vertex *v0, Vertex *v1, bool fc = false) : Start(v0), End(v1), ForConstruction(fc) {}; //Curve(Vertex *v0, Vertex *v1, bool fc = false) : Start(v0), End(v1), ForConstruction(fc) {};
...@@ -39,20 +39,20 @@ public: ...@@ -39,20 +39,20 @@ public:
virtual std::pair<double, double> supremum() const = 0; // maximum length of vector between origin and point on curve virtual std::pair<double, double> supremum() const = 0; // maximum length of vector between origin and point on curve
// Curve-Vertex Comparison // Curve-Vertex Comparison
virtual bool on_manifold(std::shared_ptr<Vertex> v) const final; // true if vertex is on manifold defined by curve virtual bool on_manifold(std::shared_ptr<Vertex> const &v) const final; // true if vertex is on manifold defined by curve
virtual bool on_manifold(std::shared_ptr<Vertex> v, std::shared_ptr<Vertex> origin, const double angle) const final; virtual bool on_manifold(std::shared_ptr<Vertex> const &v, std::shared_ptr<Vertex> const &origin, double const angle) const final;
virtual bool on_segment(std::shared_ptr<Vertex> v) const final; // true if vertex is on curve segment virtual bool on_segment(std::shared_ptr<Vertex> const &v) const final; // true if vertex is on curve segment
virtual bool on_segment(std::shared_ptr<Vertex> v, std::shared_ptr<Vertex> origin, const double angle) const final; virtual bool on_segment(std::shared_ptr<Vertex> const &v, std::shared_ptr<Vertex> const &origin, double const angle) const final;
// Curve-Curve Comparison // Curve-Curve Comparison
virtual bool is_identical(std::shared_ptr<Curve> c) const = 0; // true if (input curve) XOR (object curve) is a set with measure < tol virtual bool is_identical(std::shared_ptr<Curve> const &c) const = 0; // true if (input curve) XOR (object curve) is a set with measure < tol
virtual bool is_identical(std::shared_ptr<Curve> c, std::shared_ptr<Vertex> origin, const double angle) const = 0; virtual bool is_identical(std::shared_ptr<Curve> const &c, std::shared_ptr<Vertex> const &origin, double const angle) const = 0;
// #TODO: virtual bool is_overlapping(const Curve* c) const = 0; // true if (input curve) AND (object curve) is a set with measure > tol // #TODO: virtual bool is_overlapping(const Curve* c) const = 0; // true if (input curve) AND (object curve) is a set with measure > tol
// #TODO: virtual bool is_overlapping(const Curve* c, const Vertex* origin, const double_t angle) const = 0; // #TODO: virtual bool is_overlapping(const Curve* c, const Vertex* origin, const double_t angle) const = 0;
virtual bool is_coincident(std::shared_ptr<Curve> c) const = 0; // true if (input curve) AND (object curve + parametric extension) is a set with measure > tol virtual bool is_coincident(std::shared_ptr<Curve> const &c) const = 0; // true if (input curve) AND (object curve + parametric extension) is a set with measure > tol
// #TODO: virtual bool is_coincident(const Curve* c, const Vertex* origin, const double_t angle) const = 0; // #TODO: virtual bool is_coincident(const Curve* c, const Vertex* origin, const double_t angle) const = 0;
// Modification // Modification
......
...@@ -105,7 +105,7 @@ bool LineSegment::on_segment(const double x, const double y) const { ...@@ -105,7 +105,7 @@ bool LineSegment::on_segment(const double x, const double y) const {
} }
} }
bool LineSegment::is_identical(std::shared_ptr<Curve> c) const { bool LineSegment::is_identical(std::shared_ptr<Curve> const &c) const {
auto l = std::dynamic_pointer_cast<LineSegment>(c); auto l = std::dynamic_pointer_cast<LineSegment>(c);
if (l.get() == nullptr) { if (l.get() == nullptr) {
...@@ -115,7 +115,7 @@ bool LineSegment::is_identical(std::shared_ptr<Curve> c) const { ...@@ -115,7 +115,7 @@ bool LineSegment::is_identical(std::shared_ptr<Curve> c) const {
} }
} }
bool LineSegment::is_identical(std::shared_ptr<Curve> c, std::shared_ptr<Vertex> origin, const double angle) const { bool LineSegment::is_identical(std::shared_ptr<Curve> const &c, std::shared_ptr<Vertex> const &origin, double const angle) const {
auto l = std::dynamic_pointer_cast<LineSegment>(c); auto l = std::dynamic_pointer_cast<LineSegment>(c);
if (l.get() == nullptr) { if (l.get() == nullptr) {
...@@ -143,7 +143,7 @@ bool LineSegment::is_identical(const double x0, const double y0, const double x1 ...@@ -143,7 +143,7 @@ bool LineSegment::is_identical(const double x0, const double y0, const double x1
|| (abs(xs - x1) < tol && abs(ys - y1) < tol && abs(xe - x0) < tol && abs(ye - y0) < tol); || (abs(xs - x1) < tol && abs(ys - y1) < tol && abs(xe - x0) < tol && abs(ye - y0) < tol);
} }
bool LineSegment::is_coincident(std::shared_ptr<Curve> c) const { bool LineSegment::is_coincident(std::shared_ptr<Curve> const &c) const {
auto l = std::dynamic_pointer_cast<LineSegment>(c); auto l = std::dynamic_pointer_cast<LineSegment>(c);
if (l.get() == nullptr) { if (l.get() == nullptr) {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include "Curve.h" #include "Curve.h"
class LineSegment final : public Curve { class LineSegment final : public Curve { // TODO: Use interface instead of friend class
public: public:
friend class Angle; friend class Angle;
...@@ -22,7 +22,7 @@ public: ...@@ -22,7 +22,7 @@ public:
//Constructors //Constructors
LineSegment() : Curve() {}; LineSegment() : Curve() {};
LineSegment(const LineSegment *l) : Curve(l->Start, l->End, l->ForConstruction) {}; LineSegment(LineSegment const *l) : Curve(l->Start, l->End, l->ForConstruction) {};
LineSegment(std::shared_ptr<Vertex> v0, std::shared_ptr<Vertex> v1, bool fc = false) : Curve(v0, v1, fc) {}; LineSegment(std::shared_ptr<Vertex> v0, std::shared_ptr<Vertex> v1, bool fc = false) : Curve(v0, v1, fc) {};
...@@ -62,11 +62,11 @@ public: ...@@ -62,11 +62,11 @@ public:
using Curve::on_segment; using Curve::on_segment;
// Curve-Curve Comparison // Curve-Curve Comparison
bool is_identical(std::shared_ptr<Curve> c) const override; bool is_identical(std::shared_ptr<Curve> const &c) const override;
bool is_identical(std::shared_ptr<Curve> c, std::shared_ptr<Vertex> origin, const double angle) const override; bool is_identical(std::shared_ptr<Curve> const &c, std::shared_ptr<Vertex> const &origin, double const angle) const override;
bool is_coincident(std::shared_ptr<Curve> c) const override; bool is_coincident(std::shared_ptr<Curve> const &c) const override;
// Modification // Modification
std::shared_ptr<Curve> clone() const override { return std::make_shared<LineSegment>(this); }; std::shared_ptr<Curve> clone() const override { return std::make_shared<LineSegment>(this); };
......
#include "Sketch.hpp" #include "Sketch.hpp"
RotateCopy::RotateCopy(std::vector<std::shared_ptr<Curve>> &input, std::shared_ptr<Vertex> center, double angle, size_t copies, bool remove_internal) { RotateCopy::RotateCopy(std::vector<std::shared_ptr<Curve>> input, std::shared_ptr<Vertex> center, double angle, size_t copies, bool remove_internal) {
// Creates rotated copies of the input curves about an vertex // Creates rotated copies of the input curves about an vertex
// #TODO: Need to rearrange code and reserve vector sizes in a way that makes more sense (much code copied from MirrorCopy constructor) // #TODO: Need to rearrange code and reserve vector sizes in a way that makes more sense (much code copied from MirrorCopy constructor)
// #TODO: Restructure to obviate the need for local_curves and local_verticies // #TODO: Restructure to obviate the need for local_curves and local_verticies
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
class RotateCopy : public Pattern { class RotateCopy : public Pattern {
public: public:
RotateCopy(std::vector<std::shared_ptr<Curve>> &input, std::shared_ptr<Vertex> center, double angle, size_t copies, bool remove_internal = false); RotateCopy(std::vector<std::shared_ptr<Curve>> input, std::shared_ptr<Vertex> center, double angle, size_t copies, bool remove_internal = false);
private: private:
std::shared_ptr<Vertex> Center; std::shared_ptr<Vertex> Center;
......
#include "Sketch.hpp" #include "Sketch.hpp"
Sketch::Sketch() { /*
* Sketch::Sketch() {
NumEquations = 0; NumEquations = 0;
NumVariables = 0; NumVariables = 0;
Boundary = std::make_shared<Contour>(); Boundary = std::make_shared<Contour>();
} }
*/
void Sketch::delete_me() { void Sketch::delete_me() {
Variables.clear();
Verticies.clear();
Curves.clear();
Constraints.clear();
Patterns.clear();
Contours.clear();
}; };
void Sketch::solve() { double Sketch::solve() {
// #TODO: Tolerance based iteration convergence monitoring // #TODO: Tolerance based iteration convergence monitoring
// #TODO: Use sparse matrix representation for Jacobian // #TODO: Use sparse matrix representation for Jacobian
...@@ -32,22 +23,22 @@ void Sketch::solve() { ...@@ -32,22 +23,22 @@ void Sketch::solve() {
Eigen::FullPivLU<Eigen::MatrixXd> LU; Eigen::FullPivLU<Eigen::MatrixXd> LU;
for (size_t j = 0; j < 32; ++j) { for (size_t j = 0; j < 32; ++j) {
// #TODO: Use QR or LDLT decomposition instead // TODO: Use QR or LDLT decomposition instead
// #TODO: Check for over/underdetermined system // TODO: Check for over/underdetermined system
// #TODO: Try extracting sub matrix and solving // TODO: Try extracting sub matrix and solving
// #TODO: Extract newton_update over Verticies, Curves, and Constraints into a private method // TODO: Extract newton_update over Verticies, Curves, and Constraints into a private method
J.setZero(); J.setZero();
for (size_t i = 0; i < Verticies.size(); ++i) { for (size_t i = 0; i != Verticies.size(); ++i) {
Verticies[i]->update(J, r); Verticies[i]->update(J, r);
} }
for (size_t i = 0; i < Curves.size(); ++i) { for (size_t i = 0; i != Curves.size(); ++i) {
Curves[i]->update(J, r); Curves[i]->update(J, r);
} }
for (size_t i = 0; i < Constraints.size(); ++i) { for (size_t i = 0; i != Constraints.size(); ++i) {
Constraints[i]->update(J, r); Constraints[i]->update(J, r);
} }
...@@ -59,15 +50,18 @@ void Sketch::solve() { ...@@ -59,15 +50,18 @@ void Sketch::solve() {
Variables[i]->update(delta); Variables[i]->update(delta);
} }
} }
return r.no