Sketch.h 4.26 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 84
    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]; };

    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 87 88 89
    std::vector<ContinuousBoundaryPair> select_periodic_boundary_pairs(std::shared_ptr<Vertex> v0, double_t angle) const;

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

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

93 94 95
    std::vector<std::shared_ptr<Vertex>> Verticies;

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

97
    std::vector<std::shared_ptr<Constraint>> Constraints;
Jason Pries's avatar
Jason Pries committed
98

99
    std::vector<std::shared_ptr<Pattern>> Patterns;
Jason Pries's avatar
Jason Pries committed
100

101
    std::vector<std::shared_ptr<Contour>> Contours;
Jason Pries's avatar
Jason Pries committed
102

103
    std::shared_ptr<Contour> Boundary;
Jason Pries's avatar
Jason Pries committed
104 105

    size_t NumVariables;
106

Jason Pries's avatar
Jason Pries committed
107
    size_t NumEquations;
108 109 110 111 112 113

    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
114 115
};

116 117 118

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
119
    bool unique = true;
120 121
    for (size_t i = 0; i != elements.size() && unique; ++i) {
        unique = unique && (elements[i] != e);
Jason Pries's avatar
Jason Pries committed
122 123 124
    }

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

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

132 133
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
134
    bool unique = true;
135
    for (size_t i = 0; i != parameters.size() && unique; ++i) {
Jason Pries's avatar
Jason Pries committed
136 137 138 139 140 141 142 143 144 145
        unique = (parameters[i] != p);
    }

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


146 147 148 149 150 151
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
152

153
#endif //OERSTED_SKETCH_H