OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
Rectangle.cpp
Go to the documentation of this file.
4 #include "Physics/Units.h"
5 
6 #include <boost/regex.hpp>
7 
8 namespace 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) {
31  v = trafo_m.transformFrom(v);
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({Vector_t(0.5 * width_m, 0.5 * height_m, 0),
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  unsigned int width = out.precision() + 8;
72  for (unsigned int i = 0; i < 5; ++ i) {
73  Vector_t pt = pts[i % 4];
74  pt = trafo_m.transformFrom(pt);
75 
76  out << std::setw(width) << pt[0]
77  << std::setw(width) << pt[1]
78  << std::endl;
79  }
80  out << std::endl;
81 
82  for (auto item: divisor_m) {
83  item->writeGnuplot(out);
84  }
85 
86  // bb_m.writeGnuplot(out);
87  }
88 
89  void Rectangle::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
90  bfuncs.emplace_back(this->clone());
91  }
92 
93  std::shared_ptr<Base> Rectangle::clone() const {
94  std::shared_ptr<Rectangle> rect(new Rectangle);
95  rect->width_m = width_m;
96  rect->height_m = height_m;
97  rect->trafo_m = trafo_m;
98  rect->bb_m = bb_m;
99 
100  for (auto item: divisor_m) {
101  rect->divisor_m.emplace_back(item->clone());
102  }
103 
104  return std::static_pointer_cast<Base>(rect);
105  }
106 
108  std::string str(it, end);
109  boost::regex argumentList(UDouble + "," + UDouble + "(\\).*)");
110  boost::smatch what;
111 
112  Rectangle *rect = static_cast<Rectangle*>(fun);
113  ArgumentExtractor arguments(str);
114  try {
115  rect->width_m = parseMathExpression(arguments.get(0));
116  rect->height_m = parseMathExpression(arguments.get(1));
117  } catch (std::runtime_error &e) {
118  std::cout << e.what() << std::endl;
119  return false;
120  }
121 
122  if (rect->width_m < 0.0) {
123  std::cout << "Rectangle: a negative width provided '"
124  << arguments.get(0) << " = " << rect->width_m << "'"
125  << std::endl;
126  return false;
127  }
128  if (rect->height_m < 0.0) {
129  std::cout << "Rectangle: a negative height provided '"
130  << arguments.get(1) << " = " << rect->height_m << "'"
131  << std::endl;
132  return false;
133  }
134 
135  it += (arguments.getLengthConsumed() + 1);
136 
137  return true;
138  }
139 }
AffineTransformation trafo_m
Definition: MSLang.h:40
item[EANGLE] Entrance edge angle(radians).\item[ROTATION] Rotation of the magnet about its central axis(radians
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
double parseMathExpression(const std::string &str)
Definition: matheval.cpp:4
bool isInside(const Vector_t &X) const
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
std::vector< std::shared_ptr< Base > > divisor_m
Definition: MSLang.h:42
virtual bool isInside(const Vector_t &R) const
Definition: Rectangle.cpp:50
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
double width_m
Definition: Rectangle.h:8
BoundingBox2D bb_m
Definition: MSLang.h:41
virtual void computeBoundingBox()
Definition: Rectangle.cpp:24
Vector_t transformTo(const Vector_t &v) const
std::string::iterator iterator
Definition: MSLang.h:15
virtual void apply(std::vector< std::shared_ptr< Base > > &bfuncs)
Definition: Rectangle.cpp:89
unsigned int getLengthConsumed() const
virtual void print(int indentwidth)
Definition: Rectangle.cpp:9
#define X(arg)
Definition: fftpack.cpp:112
constexpr double rad2deg
Definition: Units.h:146
virtual void writeGnuplot(std::ofstream &out) const
Definition: Rectangle.cpp:66
Vector_t transformFrom(const Vector_t &v) const
std::string get(unsigned int i) const
double height_m
Definition: Rectangle.h:9
static const std::string UDouble
Definition: MSLang.h:33
static bool parse_detail(iterator &it, const iterator &end, Function *fun)
Definition: Rectangle.cpp:107
constexpr double e
The value of .
Definition: Physics.h:39
end
Definition: multipole_t.tex:9
virtual std::shared_ptr< Base > clone() const
Definition: Rectangle.cpp:93