OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Ellipse.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 Ellipse::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 << "ellipse, \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 << std::setw(14) << trafo_m(0, 0) << std::setw(14) << trafo_m(0, 1) << std::setw(14) << trafo_m(0, 2) << "\n"
20  << indent2 << std::setw(14) << trafo_m(1, 0) << std::setw(14) << trafo_m(1, 1) << std::setw(14) << trafo_m(1, 2) << "\n"
21  << indent2 << std::setw(14) << trafo_m(2, 0) << std::setw(14) << trafo_m(2, 1) << std::setw(14) << trafo_m(2, 2)
22  << std::endl;
23  }
24 
25  void Ellipse::writeGnuplot(std::ofstream &out) const {
26  const unsigned int N = 101;
27  const double dp = Physics::two_pi / (N - 1);
28  const unsigned int colwidth = out.precision() + 8;
29 
30  double phi = 0;
31  for (unsigned int i = 0; i < N; ++ i, phi += dp) {
32  Vector_t pt(0.0);
33  pt[0] = std::copysign(sqrt(std::pow(height_m * width_m * 0.25, 2) /
34  (std::pow(height_m * 0.5, 2) +
35  std::pow(width_m * 0.5 * tan(phi), 2))),
36  cos(phi));
37  pt[1] = pt[0] * tan(phi);
38  pt = trafo_m.transformFrom(pt);
39 
40  out << std::setw(colwidth) << pt[0]
41  << std::setw(colwidth) << pt[1]
42  << std::endl;
43  }
44  out << std::endl;
45 
46  for (auto item: divisor_m) {
47  item->writeGnuplot(out);
48  }
49 
50  // bb_m.writeGnuplot(out);
51  }
52 
53  void Ellipse::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
54  bfuncs.emplace_back(this->clone());
55  }
56 
57  std::shared_ptr<Base> Ellipse::clone() const{
58  std::shared_ptr<Ellipse> elps(new Ellipse);
59  elps->width_m = width_m;
60  elps->height_m = height_m;
61  elps->trafo_m = trafo_m;
62  elps->bb_m = bb_m;
63 
64  for (auto item: divisor_m) {
65  elps->divisor_m.emplace_back(item->clone());
66  }
67 
68  return std::static_pointer_cast<Base>(elps);
69  }
70 
72  Vector_t llc(0.0), urc(0.0);
73  const Vector_t e_x(1.0, 0.0, 0.0), e_y(0.0, 1.0, 0.0);
74  const Vector_t center = trafo_m.transformFrom(Vector_t(0.0));
75  const Vector_t e_xp = trafo_m.transformFrom(e_x) - center;
76  const Vector_t e_yp = trafo_m.transformFrom(e_y) - center;
77  const double &M11 = e_xp[0];
78  const double &M12 = e_yp[0];
79  const double &M21 = e_xp[1];
80  const double &M22 = e_yp[1];
81 
82  double t = atan2(height_m * M12, width_m * M11);
83  double halfwidth = 0.5 * (M11 * width_m * cos(t) +
84  M12 * height_m * sin(t));
85  llc[0] = center[0] - std::abs(halfwidth);
86  urc[0] = center[0] + std::abs(halfwidth);
87 
88  t = atan2(height_m * M22, width_m * M21);
89 
90  double halfheight = 0.5 * (M21 * width_m * cos(t) +
91  M22 * height_m * sin(t));
92 
93  llc[1] = center[1] - std::abs(halfheight);
94  urc[1] = center[1] + std::abs(halfheight);
95 
96  bb_m = BoundingBox(llc, urc);
97 
98  for (auto item: divisor_m) {
99  item->computeBoundingBox();
100  }
101  }
102 
103  bool Ellipse::isInside(const Vector_t &R) const {
104  if (!bb_m.isInside(R)) return false;
105 
107  if (4 * (std::pow(X[0] / width_m, 2) + std::pow(X[1] / height_m, 2)) <= 1) {
108 
109  for (auto item: divisor_m) {
110  if (item->isInside(R)) return false;
111  }
112 
113  return true;
114  }
115 
116  return false;
117  }
118 
119  bool Ellipse::parse_detail(iterator &it, const iterator &end, Function* fun) {
120 
121  ArgumentExtractor arguments(std::string(it, end));
122  double width, height;
123  try {
124  width = parseMathExpression(arguments.get(0));
125  height = parseMathExpression(arguments.get(1));
126  } catch (std::runtime_error &e) {
127  std::cout << e.what() << std::endl;
128  return false;
129  }
130 
131  Ellipse *elps = static_cast<Ellipse*>(fun);
132  elps->width_m = width;
133  elps->height_m = height;
134 
135  if (elps->width_m < 0.0) {
136  std::cout << "Ellipse: a negative width provided '"
137  << arguments.get(0) << " = " << elps->width_m << "'"
138  << std::endl;
139  return false;
140  }
141  if (elps->height_m < 0.0) {
142  std::cout << "Ellipse: a negative height provided '"
143  << arguments.get(1) << " = " << elps->height_m << "'"
144  << std::endl;
145  return false;
146  }
147 
148  it += (arguments.getLengthConsumed() + 1);
149 
150  return true;
151  }
152 }
unsigned int getLengthConsumed() const
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
constexpr double e
The value of .
Definition: Physics.h:40
double width_m
Definition: Ellipse.h:8
virtual std::shared_ptr< Base > clone() const
Definition: Ellipse.cpp:57
Definition: rbendmap.h:8
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
PETE_TBTree< FnCopysign, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > copysign(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
constexpr double two_pi
The value of .
Definition: Physics.h:34
BoundingBox bb_m
Definition: MSLang.h:42
bool isInside(const Vector_t &X) const
Definition: BoundingBox.cpp:11
Tps< T > tan(const Tps< T > &x)
Tangent.
Definition: TpsMath.h:147
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
static bool parse_detail(iterator &it, const iterator &end, Function *fun)
Definition: Ellipse.cpp:119
virtual void print(int indentwidth)
Definition: Ellipse.cpp:9
Vector_t transformFrom(const Vector_t &v) const
virtual void computeBoundingBox()
Definition: Ellipse.cpp:71
virtual void apply(std::vector< std::shared_ptr< Base > > &bfuncs)
Definition: Ellipse.cpp:53
virtual bool isInside(const Vector_t &R) const
Definition: Ellipse.cpp:103
PETE_TBTree< FnArcTan2, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > atan2(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
Vector_t transformTo(const Vector_t &v) const
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
virtual void writeGnuplot(std::ofstream &out) const
Definition: Ellipse.cpp:25
AffineTransformation trafo_m
Definition: MSLang.h:41
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
std::string get(unsigned int i) const
std::string::iterator iterator
Definition: MSLang.h:16
double parseMathExpression(const std::string &str)
Definition: matheval.cpp:4
double height_m
Definition: Ellipse.h:9
Inform & endl(Inform &inf)
Definition: Inform.cpp:42