OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Rectangle.cpp
Go to the documentation of this file.
4 #include "Physics/Physics.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() * Physics::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 = BoundingBox(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 
107  bool Rectangle::parse_detail(iterator &it, const iterator &end, Function* fun) {
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 }
unsigned int getLengthConsumed() const
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
static const std::string UDouble
Definition: MSLang.h:34
constexpr double e
The value of .
Definition: Physics.h:40
Definition: rbendmap.h:8
BoundingBox bb_m
Definition: MSLang.h:42
bool isInside(const Vector_t &X) const
Definition: BoundingBox.cpp:11
std::vector< std::shared_ptr< Base > > divisor_m
Definition: MSLang.h:43
constexpr double rad2deg
The conversion factor from radians to degrees.
Definition: Physics.h:46
virtual void print(int indentwidth)
Definition: Rectangle.cpp:9
virtual void apply(std::vector< std::shared_ptr< Base > > &bfuncs)
Definition: Rectangle.cpp:89
static bool parse_detail(iterator &it, const iterator &end, Function *fun)
Definition: Rectangle.cpp:107
Vector_t transformFrom(const Vector_t &v) const
virtual std::shared_ptr< Base > clone() const
Definition: Rectangle.cpp:93
virtual void computeBoundingBox()
Definition: Rectangle.cpp:24
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
Vector_t transformTo(const Vector_t &v) const
AffineTransformation trafo_m
Definition: MSLang.h:41
std::string get(unsigned int i) const
double height_m
Definition: Rectangle.h:9
std::string::iterator iterator
Definition: MSLang.h:16
double parseMathExpression(const std::string &str)
Definition: matheval.cpp:4
double width_m
Definition: Rectangle.h:8
virtual bool isInside(const Vector_t &R) const
Definition: Rectangle.cpp:50
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
virtual void writeGnuplot(std::ofstream &out) const
Definition: Rectangle.cpp:66