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