Sketch.h 4.46 KB
Newer Older
1 2
#ifndef OERSTED_SKETCH_H
#define OERSTED_SKETCH_H
Jason Pries's avatar
Jason Pries committed
3 4

#include <cfloat>
5 6 7 8 9 10 11
#include <fstream>
#include <list>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
Jason Pries's avatar
Jason Pries committed
12

13
#include <boost/filesystem.hpp>
Jason Pries's avatar
Jason Pries committed
14

15
#include "Eigen"
Jason Pries's avatar
Jason Pries committed
16

17 18
#include "ContinuousBoundaryPair.h"

19
class Variable;
Jason Pries's avatar
Jason Pries committed
20 21 22 23 24 25 26 27 28 29 30 31
class Vertex;
class Curve;
class Constraint;
class Pattern;
class Contour;

enum class SaveMethod {
    Rasterize
};

class Sketch {
public:
32
    Sketch() : NumVariables(0), NumEquations(0), Boundary() {};
Jason Pries's avatar
Jason Pries committed
33

34
    size_t size() const { return (Verticies.size() + Curves.size() + Constraints.size()); };
Jason Pries's avatar
Jason Pries committed
35

36
    size_t size_constraints() const { return Constraints.size(); };
Jason Pries's avatar
Jason Pries committed
37

38
    size_t size_contours() const { return Contours.size(); };
Jason Pries's avatar
Jason Pries committed
39

40
    size_t size_curves() const { return Curves.size(); };
Jason Pries's avatar
Jason Pries committed
41

42
    size_t size_verticies() const { return Verticies.size(); };
Jason Pries's avatar
Jason Pries committed
43

44
    bool build(); // TODO: Detailed return enum
Jason Pries's avatar
Jason Pries committed
45

46
    double characteristic_length() const;
Jason Pries's avatar
Jason Pries committed
47

48
    double solve(); // TODO: Detailed return enum
Jason Pries's avatar
Jason Pries committed
49

50
    void add_element(std::shared_ptr<Constraint> c);
Jason Pries's avatar
Jason Pries committed
51

52
    void add_element(std::shared_ptr<Curve> c);
Jason Pries's avatar
Jason Pries committed
53

54
    void add_element(std::shared_ptr<Pattern> p);
Jason Pries's avatar
Jason Pries committed
55

56
    void add_element(std::shared_ptr<Vertex> v);
Jason Pries's avatar
Jason Pries committed
57

58
    void add_parameter(std::shared_ptr<Variable> v) { add_parameter(Variables, v); };
Jason Pries's avatar
Jason Pries committed
59

60
    void perturb(Eigen::VectorXd &delta, double scale) const;
Jason Pries's avatar
Jason Pries committed
61

62
    void update_linearization(Eigen::MatrixXd &J, Eigen::VectorXd &r) const;
Jason Pries's avatar
Jason Pries committed
63

64
    void update_variables(Eigen::VectorXd &delta) const;
Jason Pries's avatar
Jason Pries committed
65

66 67
    template<SaveMethod S>
    void save_as(std::string path, std::string file_name) const;
Jason Pries's avatar
Jason Pries committed
68

69 70
    template<class T, class...ArgT>
    std::shared_ptr<T> new_element(ArgT &&... args);
Jason Pries's avatar
Jason Pries committed
71

72
    std::shared_ptr<Curve const> curve(size_t i) const { return Curves[i]; };
Jason Pries's avatar
Jason Pries committed
73

74
    std::shared_ptr<Constraint const> constraint(size_t i) const { return Constraints[i]; };
Jason Pries's avatar
Jason Pries committed
75

76 77 78 79 80 81 82 83
    std::shared_ptr<Contour const> contour(size_t i) const { return Contours[i]; };

    std::shared_ptr<Contour const> boundary() const { return Boundary; };

    std::shared_ptr<Variable const> variable(size_t i) const { return Variables[i]; };

    std::shared_ptr<Vertex const> vertex(size_t i) const { return Verticies[i]; };

84
    std::vector<std::shared_ptr<Curve const>> const curves() { return std::vector<std::shared_ptr<Curve const>>{Curves.begin(), Curves.end()}; };
Jason Pries's avatar
Jason Pries committed
85

86
    std::vector<PeriodicBoundaryPair> select_periodic_boundary_pairs(std::shared_ptr<Vertex const> const &v0, double_t angle) const;
87

88 89 90
    std::vector<std::shared_ptr<Curve const>> select_radial_boundary(std::shared_ptr<Vertex const> const &v0, double_t radius) const;

    std::shared_ptr<Vertex const> select_periodic_vertex(std::shared_ptr<Vertex const> const &v1, std::shared_ptr<Vertex const> const &origin, double_t angle) const;
91

Jason Pries's avatar
Jason Pries committed
92
private:
93
    std::vector<std::shared_ptr<Variable>> Variables;
Jason Pries's avatar
Jason Pries committed
94

95 96 97
    std::vector<std::shared_ptr<Vertex>> Verticies;

    std::vector<std::shared_ptr<Curve>> Curves;
Jason Pries's avatar
Jason Pries committed
98

99
    std::vector<std::shared_ptr<Constraint>> Constraints;
Jason Pries's avatar
Jason Pries committed
100

101
    std::vector<std::shared_ptr<Pattern>> Patterns;
Jason Pries's avatar
Jason Pries committed
102

103
    std::vector<std::shared_ptr<Contour>> Contours;
Jason Pries's avatar
Jason Pries committed
104

105
    std::shared_ptr<Contour> Boundary;
Jason Pries's avatar
Jason Pries committed
106 107

    size_t NumVariables;
108

Jason Pries's avatar
Jason Pries committed
109
    size_t NumEquations;
110 111 112 113 114 115

    template<class T>
    void add_element(std::vector<std::shared_ptr<T>> &elements, std::shared_ptr<T> e);

    template<class T>
    void add_parameter(std::vector<std::shared_ptr<T>> &parameters, std::shared_ptr<T> p);
Jason Pries's avatar
Jason Pries committed
116 117
};

118 119 120

template<class T>
void Sketch::add_element(std::vector<std::shared_ptr<T>> &elements, std::shared_ptr<T> e) {
Jason Pries's avatar
Jason Pries committed
121
    bool unique = true;
122 123
    for (size_t i = 0; i != elements.size() && unique; ++i) {
        unique = unique && (elements[i] != e);
Jason Pries's avatar
Jason Pries committed
124 125 126
    }

    if (unique) {
127 128
        NumEquations += e->set_equation_index(NumEquations);
        elements.push_back(e);
Jason Pries's avatar
Jason Pries committed
129 130
    }

131
    e->register_parameters(this);
Jason Pries's avatar
Jason Pries committed
132 133
}

134 135
template<class T>
void Sketch::add_parameter(std::vector<std::shared_ptr<T>> &parameters, std::shared_ptr<T> p) {
Jason Pries's avatar
Jason Pries committed
136
    bool unique = true;
137
    for (size_t i = 0; i != parameters.size() && unique; ++i) {
Jason Pries's avatar
Jason Pries committed
138 139 140 141 142 143 144 145 146 147
        unique = (parameters[i] != p);
    }

    if (unique) {
        NumVariables += p->set_index(NumVariables);
        parameters.push_back(p);
    }
}


148 149 150 151 152 153
template<class T, class...ArgT>
std::shared_ptr<T> Sketch::new_element(ArgT &&... args) {
    std::shared_ptr<T> e = std::make_shared<T>(std::forward<ArgT>(args)...);
    add_element(e);
    return e;
};
Jason Pries's avatar
Jason Pries committed
154

155
#endif //OERSTED_SKETCH_H