OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
Rectangle.cpp
Go to the documentation of this file.
4#include "Physics/Units.h"
5
6#include <regex>
7
8namespace mslang {
9 void Rectangle::print(int indentwidth) {
10 std::string indent(indentwidth, ' ');
11 std::string indent2(indentwidth + 8, ' ');
12 Vector_t origin = trafo_m.getOrigin();
13 double angle = trafo_m.getAngle() * Units::rad2deg;
14 std::cout << indent << "rectangle, \n"
15 << indent2 << "w: " << width_m << ", \n"
16 << indent2 << "h: " << height_m << ", \n"
17 << indent2 << "origin: " << origin[0] << ", " << origin[1] << ",\n"
18 << indent2 << "angle: " << angle << "\n"
19 << indent2 << trafo_m(0, 0) << "\t" << trafo_m(0, 1) << "\t" << trafo_m(0, 2) << "\n"
20 << indent2 << trafo_m(1, 0) << "\t" << trafo_m(1, 1) << "\t" << trafo_m(1, 2) << "\n"
21 << indent2 << trafo_m(2, 0) << "\t" << trafo_m(2, 1) << "\t" << trafo_m(2, 2) << std::endl;
22 }
23
25 std::vector<Vector_t> corners({Vector_t({0.5 * width_m, 0.5 * height_m, 0}),
26 Vector_t({-0.5 * width_m, 0.5 * height_m, 0}),
27 Vector_t({-0.5 * width_m, -0.5 * height_m, 0}),
28 Vector_t({0.5 * width_m, -0.5 * height_m, 0})});
29
30 for (Vector_t &v: corners) {
32 }
33
34 Vector_t llc = corners[0], urc = corners[0];
35 for (unsigned int i = 1; i < 4; ++ i) {
36 if (corners[i][0] < llc[0]) llc[0] = corners[i][0];
37 else if (corners[i][0] > urc[0]) urc[0] = corners[i][0];
38
39 if (corners[i][1] < llc[1]) llc[1] = corners[i][1];
40 else if (corners[i][1] > urc[1]) urc[1] = corners[i][1];
41 }
42
43 bb_m = BoundingBox2D(llc, urc);
44
45 for (auto item: divisor_m) {
46 item->computeBoundingBox();
47 }
48 }
49
50 bool Rectangle::isInside(const Vector_t &R) const {
51 if (!bb_m.isInside(R)) return false;
52
54 if (2 * std::abs(X[0]) <= width_m &&
55 2 * std::abs(X[1]) <= height_m) {
56 for (auto item: divisor_m) {
57 if (item->isInside(R))
58 return false;
59 }
60 return true;
61 }
62
63 return false;
64 }
65
66 void Rectangle::writeGnuplot(std::ofstream &out) const {
67 std::vector<Vector_t> pts({
68 Vector_t({ 0.5 * width_m, 0.5 * height_m, 0}),
69 Vector_t({-0.5 * width_m, 0.5 * height_m, 0}),
70 Vector_t({-0.5 * width_m, -0.5 * height_m, 0}),
71 Vector_t({ 0.5 * width_m, -0.5 * height_m, 0})
72 });
73 unsigned int width = out.precision() + 8;
74 for (unsigned int i = 0; i < 5; ++ i) {
75 Vector_t pt = pts[i % 4];
76 pt = trafo_m.transformFrom(pt);
77
78 out << std::setw(width) << pt[0]
79 << std::setw(width) << pt[1]
80 << std::endl;
81 }
82 out << std::endl;
83
84 for (auto item: divisor_m) {
85 item->writeGnuplot(out);
86 }
87
88 // bb_m.writeGnuplot(out);
89 }
90
91 void Rectangle::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
92 bfuncs.emplace_back(this->clone());
93 }
94
95 std::shared_ptr<Base> Rectangle::clone() const {
96 std::shared_ptr<Rectangle> rect(new Rectangle);
97 rect->width_m = width_m;
98 rect->height_m = height_m;
99 rect->trafo_m = trafo_m;
100 rect->bb_m = bb_m;
101
102 for (auto item: divisor_m) {
103 rect->divisor_m.emplace_back(item->clone());
104 }
105
106 return std::static_pointer_cast<Base>(rect);
107 }
108
110 std::string str(it, end);
111 std::regex argumentList(UDouble + "," + UDouble + "(\\).*)");
112 std::smatch what;
113
114 Rectangle *rect = static_cast<Rectangle*>(fun);
115 ArgumentExtractor arguments(str);
116 try {
117 rect->width_m = parseMathExpression(arguments.get(0));
118 rect->height_m = parseMathExpression(arguments.get(1));
119 } catch (std::runtime_error &e) {
120 std::cout << e.what() << std::endl;
121 return false;
122 }
123
124 if (rect->width_m < 0.0) {
125 std::cout << "Rectangle: a negative width provided '"
126 << arguments.get(0) << " = " << rect->width_m << "'"
127 << std::endl;
128 return false;
129 }
130 if (rect->height_m < 0.0) {
131 std::cout << "Rectangle: a negative height provided '"
132 << arguments.get(1) << " = " << rect->height_m << "'"
133 << std::endl;
134 return false;
135 }
136
137 it += (arguments.getLengthConsumed() + 1);
138
139 return true;
140 }
141}
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
#define X(arg)
Definition fftpack.cpp:106
constexpr double rad2deg
Definition Units.h:146
double parseMathExpression(const std::string &str)
Definition matheval.cpp:4
std::string::iterator iterator
Definition MSLang.h:13
static const std::string UDouble
Definition MSLang.h:31
std::vector< std::shared_ptr< Base > > divisor_m
Definition MSLang.h:40
AffineTransformation trafo_m
Definition MSLang.h:38
BoundingBox2D bb_m
Definition MSLang.h:39
Vector_t transformTo(const Vector_t &v) const
Vector_t transformFrom(const Vector_t &v) const
std::string get(unsigned int i) const
unsigned int getLengthConsumed() const
bool isInside(const Vector_t &X) const
virtual bool isInside(const Vector_t &R) const
Definition Rectangle.cpp:50
virtual void computeBoundingBox()
Definition Rectangle.cpp:24
virtual void apply(std::vector< std::shared_ptr< Base > > &bfuncs)
Definition Rectangle.cpp:91
static bool parse_detail(iterator &it, const iterator &end, Function *fun)
virtual void writeGnuplot(std::ofstream &out) const
Definition Rectangle.cpp:66
virtual std::shared_ptr< Base > clone() const
Definition Rectangle.cpp:95
virtual void print(int indentwidth)
Definition Rectangle.cpp:9
Vektor< double, 3 > Vector_t
Definition Vektor.h:6