Commit 4c3f47fd authored by kraus's avatar kraus
Browse files

cleaning up the implementation of the multi slit language

parent 56d08bfc
......@@ -3,6 +3,7 @@
#include "AbsBeamline/Component.h"
#include "Utilities/MSLang.h"
#include "Utilities/MSLang/QuadTree.h"
class BeamlineVisitor;
class LossDataSink;
......
set (_SRCS
ArithmeticError.cpp
AttributeError.cpp
CLRangeError.cpp
ClassicException.cpp
ClassicField.cpp
CLRangeError.cpp
ClassicRandom.cpp
ComplexErrorFun.cpp
ConvergenceError.cpp
DivideError.cpp
......@@ -14,18 +15,32 @@ set (_SRCS
GeneralClassicException.cpp
InverseGauss.cpp
LogicalError.cpp
Mesher.cpp
MSLang.cpp
MSLang/BoundingBox.cpp
MSLang/Difference.cpp
MSLang/Ellipse.cpp
MSLang/Intersection.cpp
MSLang/Mask.cpp
MSLang/Polygon.cpp
MSLang/QuadTree.cpp
MSLang/Rectangle.cpp
MSLang/Repeat.cpp
MSLang/Rotation.cpp
MSLang/Shear.cpp
MSLang/SymmetricDifference.cpp
MSLang/Translation.cpp
MSLang/Triangle.cpp
MSLang/Union.cpp
NormalFormError.cpp
Options.cpp
OverflowError.cpp
ParseError.cpp
ClassicRandom.cpp
PortableBitmapReader.cpp
RingSection.cpp
SingularMatrixError.cpp
SizeError.cpp
SwitcherError.cpp
Mesher.cpp
Util.cpp
)
......@@ -38,10 +53,10 @@ ADD_OPAL_SOURCES(${_SRCS})
set (HDRS
ArithmeticError.h
AttributeError.h
CLRangeError.h
ClassicException.h
ClassicField.h
ClassicRandom.h
CLRangeError.h
ComplexErrorFun.h
ConvergenceError.h
DivideError.h
......@@ -52,9 +67,27 @@ set (HDRS
GeneralClassicException.h
InverseGauss.h
LogicalError.h
Mesher.h
MSLang.h
MSLang/AffineTransformation.h
MSLang/BoundingBox.h
MSLang/Difference.h
MSLang/Ellipse.h
MSLang/Intersection.h
MSLang/Mask.h
MSLang/Polygon.h
MSLang/QuadTree.h
MSLang/Rectangle.h
MSLang/Repeat.h
MSLang/Rotation.h
MSLang/Shear.h
MSLang/SymmetricDifference.h
MSLang/Translation.h
MSLang/Triangle.h
MSLang/Union.h
NormalFormError.h
Options.h
OptionTypes.h
Options.h
OverflowError.h
ParseError.h
PortableBitmapReader.h
......@@ -62,7 +95,6 @@ set (HDRS
SingularMatrixError.h
SizeError.h
SwitcherError.h
Mesher.h
Util.h
)
......
This diff is collapsed.
#ifndef MSLANG_H
#define MSLANG_H
#include "Utilities/MSLang/BoundingBox.h"
#include "Utilities/MSLang/AffineTransformation.h"
#include "Algorithms/Vektor.h"
#include "AppTypes/Tenzor.h"
......@@ -18,136 +20,9 @@ namespace mslang {
return euclidean_norm(v);
}
struct BoundingBox {
Vector_t center_m;
double width_m;
double height_m;
BoundingBox():
center_m(0.0),
width_m(0.0),
height_m(0.0)
{ }
BoundingBox(const BoundingBox &right):
center_m(right.center_m),
width_m(right.width_m),
height_m(right.height_m)
{ }
BoundingBox(const Vector_t &llc,
const Vector_t &urc):
center_m(0.5 * (llc + urc)),
width_m(urc[0] - llc[0]),
height_m(urc[1] - llc[1])
{ }
bool doesIntersect(const BoundingBox &bb) {
return ((center_m[0] - 0.5 * width_m <= bb.center_m[0] + 0.5 * bb.width_m) &&
(center_m[0] + 0.5 * width_m >= bb.center_m[0] - 0.5 * bb.width_m) &&
(center_m[1] - 0.5 * height_m <= bb.center_m[1] + 0.5 * bb.height_m) &&
(center_m[1] + 0.5 * height_m >= bb.center_m[1] - 0.5 * bb.height_m));
}
bool isInside(const Vector_t &X) const {
if (2 * std::abs(X[0] - center_m[0]) <= width_m &&
2 * std::abs(X[1] - center_m[1]) <= height_m)
return true;
return false;
}
bool isInside(const BoundingBox &b) const {
return (isInside(b.center_m + 0.5 * Vector_t( b.width_m, b.height_m, 0.0)) &&
isInside(b.center_m + 0.5 * Vector_t(-b.width_m, b.height_m, 0.0)) &&
isInside(b.center_m + 0.5 * Vector_t(-b.width_m, -b.height_m, 0.0)) &&
isInside(b.center_m + 0.5 * Vector_t( b.width_m, -b.height_m, 0.0)));
}
virtual void writeGnuplot(std::ofstream &out) const {
std::vector<Vector_t> pts({Vector_t(center_m[0] + 0.5 * width_m, center_m[1] + 0.5 * height_m, 0),
Vector_t(center_m[0] - 0.5 * width_m, center_m[1] + 0.5 * height_m, 0),
Vector_t(center_m[0] - 0.5 * width_m, center_m[1] - 0.5 * height_m, 0),
Vector_t(center_m[0] + 0.5 * width_m, center_m[1] - 0.5 * height_m, 0)});
unsigned int width = out.precision() + 8;
for (unsigned int i = 0; i < 5; ++ i) {
Vector_t & pt = pts[i % 4];
out << std::setw(width) << pt[0]
<< std::setw(width) << pt[1]
<< std::endl;
}
out << std::endl;
}
void print(std::ostream &out) const {
out << std::setw(18) << center_m[0] - 0.5 * width_m
<< std::setw(18) << center_m[1] - 0.5 * height_m
<< std::setw(18) << center_m[0] + 0.5 * width_m
<< std::setw(18) << center_m[1] + 0.5 * height_m
<< std::endl;
}
};
std::ostream & operator<< (std::ostream &out, const BoundingBox &bb);
struct AffineTransformation: public Tenzor<double, 3> {
AffineTransformation(const Vector_t& row0,
const Vector_t& row1):
Tenzor(row0[0], row0[1], row0[2], row1[0], row1[1], row1[2], 0.0, 0.0, 1.0) {
}
AffineTransformation():
Tenzor(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0) { }
AffineTransformation getInverse() const {
AffineTransformation Ret;
double det = (*this)(0, 0) * (*this)(1, 1) - (*this)(1, 0) * (*this)(0, 1);
Ret(0, 0) = (*this)(1, 1) / det;
Ret(1, 0) = -(*this)(1, 0) / det;
Ret(0, 1) = -(*this)(0, 1) / det;
Ret(1, 1) = (*this)(0, 0) / det;
Ret(0, 2) = - Ret(0, 0) * (*this)(0, 2) - Ret(0, 1) * (*this)(1, 2);
Ret(1, 2) = - Ret(1, 0) * (*this)(0, 2) - Ret(1, 1) * (*this)(1, 2);
Ret(2, 2) = 1.0;
return Ret;
}
Vector_t getOrigin() const {
return Vector_t(-(*this)(0, 2), -(*this)(1, 2), 0.0);
}
double getAngle() const {
return atan2((*this)(1, 0), (*this)(0, 0));
}
Vector_t transformTo(const Vector_t &v) const {
const Tenzor<double, 3> &A = *static_cast<const Tenzor<double, 3>* >(this);
Vector_t b(v[0], v[1], 1.0);
Vector_t w = dot(A, b);
return Vector_t(w[0], w[1], 0.0);
}
Vector_t transformFrom(const Vector_t &v) const {
AffineTransformation inv = getInverse();
return inv.transformTo(v);
}
AffineTransformation mult(const AffineTransformation &B) {
AffineTransformation Ret;
const Tenzor<double, 3> &A = *static_cast<const Tenzor<double, 3> *>(this);
const Tenzor<double, 3> &BTenz = *static_cast<const Tenzor<double, 3> *>(&B);
Tenzor<double, 3> &C = *static_cast<Tenzor<double, 3> *>(&Ret);
C = dot(A, BTenz);
return Ret;
}
};
struct Base;
......@@ -156,6 +31,13 @@ namespace mslang {
virtual void print(int indent) = 0;
virtual void apply(std::vector<Base*> &bfuncs) = 0;
static bool parse(iterator &it, const iterator &end, Function* &fun);
static const std::string UDouble;
static const std::string Double;
static const std::string UInt;
static const std::string FCall;
};
struct Base: public Function {
......@@ -184,83 +66,16 @@ namespace mslang {
virtual void writeGnuplot(std::ofstream &out) const = 0;
virtual void computeBoundingBox() = 0;
virtual bool isInside(const Vector_t &R) const = 0;
virtual void divideBy(std::vector<Base*> &divisors) = 0;
};
struct Triangle: public Base {
std::vector<Vector_t> nodes_m;
Triangle():
Base(),
nodes_m(std::vector<Vector_t>(3, Vector_t(0, 0, 1)))
{ }
Triangle(const Triangle &right):
Base(right),
nodes_m(right.nodes_m)
{ }
virtual ~Triangle()
{ }
virtual void print(int indentwidth);
virtual void apply(std::vector<Base*> &bfuncs);
virtual Base* clone() const;
virtual void writeGnuplot(std::ofstream &out) const;
virtual void computeBoundingBox();
double crossProduct(const Vector_t &pt, unsigned int nodeNum) const;
virtual bool isInside(const Vector_t &R) const;
virtual void divideBy(std::vector<Base*> &divisors);
void orientNodesCCW();
};
struct QuadTree {
int level_m;
std::list<Base*> objects_m;
BoundingBox bb_m;
QuadTree *nodes_m;
QuadTree():
level_m(0),
bb_m(),
nodes_m(0)
{ }
QuadTree(int l, const BoundingBox &b):
level_m(l),
bb_m(b),
nodes_m(0)
{ }
QuadTree(const QuadTree &right);
~QuadTree();
void operator=(const QuadTree &right);
void transferIfInside(std::list<Base*> &objs);
void buildUp();
void writeGnuplot(std::ofstream &out) const {
out << "# level: " << level_m << ", size: " << objects_m.size() << std::endl;
bb_m.writeGnuplot(out);
out << "# num holes: " << objects_m.size() << std::endl;
// for (const Base *obj: objects_m) {
// obj->writeGnuplot(out);
// }
out << std::endl;
if (nodes_m != 0) {
for (unsigned int i = 0; i < 4u; ++ i) {
nodes_m[i].writeGnuplot(out);
virtual void divideBy(std::vector<Base*> &divisors) {
for (auto item: divisors) {
if (bb_m.doesIntersect(item->bb_m)) {
divisor_m.push_back(item->clone());
}
}
}
bool isInside(const Vector_t &R) const;
};
bool parse(std::string str, Function* &fun);
}
......
#ifndef MSLANG_AFFINETRANSFORMATION_H
#define MSLANG_AFFINETRANSFORMATION_H
#include "Algorithms/Vektor.h"
#include "AppTypes/Tenzor.h"
#include <iostream>
#include <fstream>
namespace mslang {
struct AffineTransformation: public Tenzor<double, 3> {
AffineTransformation(const Vector_t& row0,
const Vector_t& row1):
Tenzor(row0[0], row0[1], row0[2], row1[0], row1[1], row1[2], 0.0, 0.0, 1.0) {
}
AffineTransformation():
Tenzor(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0) { }
AffineTransformation getInverse() const {
AffineTransformation Ret;
double det = (*this)(0, 0) * (*this)(1, 1) - (*this)(1, 0) * (*this)(0, 1);
Ret(0, 0) = (*this)(1, 1) / det;
Ret(1, 0) = -(*this)(1, 0) / det;
Ret(0, 1) = -(*this)(0, 1) / det;
Ret(1, 1) = (*this)(0, 0) / det;
Ret(0, 2) = - Ret(0, 0) * (*this)(0, 2) - Ret(0, 1) * (*this)(1, 2);
Ret(1, 2) = - Ret(1, 0) * (*this)(0, 2) - Ret(1, 1) * (*this)(1, 2);
Ret(2, 2) = 1.0;
return Ret;
}
Vector_t getOrigin() const {
return Vector_t(-(*this)(0, 2), -(*this)(1, 2), 0.0);
}
double getAngle() const {
return atan2((*this)(1, 0), (*this)(0, 0));
}
Vector_t transformTo(const Vector_t &v) const {
const Tenzor<double, 3> &A = *static_cast<const Tenzor<double, 3>* >(this);
Vector_t b(v[0], v[1], 1.0);
Vector_t w = dot(A, b);
return Vector_t(w[0], w[1], 0.0);
}
Vector_t transformFrom(const Vector_t &v) const {
AffineTransformation inv = getInverse();
return inv.transformTo(v);
}
AffineTransformation mult(const AffineTransformation &B) {
AffineTransformation Ret;
const Tenzor<double, 3> &A = *static_cast<const Tenzor<double, 3> *>(this);
const Tenzor<double, 3> &BTenz = *static_cast<const Tenzor<double, 3> *>(&B);
Tenzor<double, 3> &C = *static_cast<Tenzor<double, 3> *>(&Ret);
C = dot(A, BTenz);
return Ret;
}
};
}
#endif
\ No newline at end of file
#include "Utilities/MSLang/BoundingBox.h"
namespace mslang {
bool BoundingBox::doesIntersect(const BoundingBox &bb) const {
return ((center_m[0] - 0.5 * width_m <= bb.center_m[0] + 0.5 * bb.width_m) &&
(center_m[0] + 0.5 * width_m >= bb.center_m[0] - 0.5 * bb.width_m) &&
(center_m[1] - 0.5 * height_m <= bb.center_m[1] + 0.5 * bb.height_m) &&
(center_m[1] + 0.5 * height_m >= bb.center_m[1] - 0.5 * bb.height_m));
}
bool BoundingBox::isInside(const Vector_t &X) const {
if (2 * std::abs(X[0] - center_m[0]) <= width_m &&
2 * std::abs(X[1] - center_m[1]) <= height_m)
return true;
return false;
}
bool BoundingBox::isInside(const BoundingBox &b) const {
return (isInside(b.center_m + 0.5 * Vector_t( b.width_m, b.height_m, 0.0)) &&
isInside(b.center_m + 0.5 * Vector_t(-b.width_m, b.height_m, 0.0)) &&
isInside(b.center_m + 0.5 * Vector_t(-b.width_m, -b.height_m, 0.0)) &&
isInside(b.center_m + 0.5 * Vector_t( b.width_m, -b.height_m, 0.0)));
}
void BoundingBox::writeGnuplot(std::ofstream &out) const {
std::vector<Vector_t> pts({Vector_t(center_m[0] + 0.5 * width_m, center_m[1] + 0.5 * height_m, 0),
Vector_t(center_m[0] - 0.5 * width_m, center_m[1] + 0.5 * height_m, 0),
Vector_t(center_m[0] - 0.5 * width_m, center_m[1] - 0.5 * height_m, 0),
Vector_t(center_m[0] + 0.5 * width_m, center_m[1] - 0.5 * height_m, 0)});
unsigned int width = out.precision() + 8;
for (unsigned int i = 0; i < 5; ++ i) {
Vector_t & pt = pts[i % 4];
out << std::setw(width) << pt[0]
<< std::setw(width) << pt[1]
<< std::endl;
}
out << std::endl;
}
void BoundingBox::print(std::ostream &out) const {
out << std::setw(18) << center_m[0] - 0.5 * width_m
<< std::setw(18) << center_m[1] - 0.5 * height_m
<< std::setw(18) << center_m[0] + 0.5 * width_m
<< std::setw(18) << center_m[1] + 0.5 * height_m
<< std::endl;
}
}
\ No newline at end of file
#ifndef MSLANG_BOUNDINGBOX_H
#define MSLANG_BOUNDINGBOX_H
#include "Algorithms/Vektor.h"
#include <iostream>
#include <fstream>
namespace mslang {
struct BoundingBox {
Vector_t center_m;
double width_m;
double height_m;
BoundingBox():
center_m(0.0),
width_m(0.0),
height_m(0.0)
{ }
BoundingBox(const BoundingBox &right):
center_m(right.center_m),
width_m(right.width_m),
height_m(right.height_m)
{ }
BoundingBox(const Vector_t &llc,
const Vector_t &urc):
center_m(0.5 * (llc + urc)),
width_m(urc[0] - llc[0]),
height_m(urc[1] - llc[1])
{ }
bool doesIntersect(const BoundingBox &bb) const;
bool isInside(const Vector_t &X) const;
bool isInside(const BoundingBox &b) const;
virtual void writeGnuplot(std::ofstream &out) const;
void print(std::ostream &out) const;
};
}
#endif
\ No newline at end of file
#include "Utilities/MSLang/Difference.h"
#include <boost/regex.hpp>
namespace mslang {
void Difference::print(int indentwidth) {
std::string indent(' ', indentwidth);
std::cout << indent << "Difference\n"
<< indent << " nominators\n";
dividend_m->print(indentwidth + 8);
std::cout << indent << " denominators\n";
divisor_m->print(indentwidth + 8);
}
void Difference::apply(std::vector<Base*> &bfuncs) {
std::vector<Base*> nom, denom;
dividend_m->apply(nom);
divisor_m->apply(denom);
for (auto item: nom) {
item->divideBy(denom);
bfuncs.push_back(item->clone());
}
for (auto item: nom)
delete item;
for (auto item: denom)
delete item;
}
bool Difference::parse_detail(iterator &it, const iterator &end, Function* &fun) {
Difference *dif = static_cast<Difference*>(fun);
if (!parse(it, end, dif->dividend_m)) return false;
boost::regex argumentList("(,[a-z]+\\(.*)");
boost::regex endParenthesis("\\)(.*)");
boost::smatch what;
std::string str(it, end);
if (!boost::regex_match(str, what, argumentList)) return false;
iterator it2 = it + 1;
if (!parse(it2, end, dif->divisor_m)) return false;
it = it2;
str = std::string(it, end);
if (!boost::regex_match(str, what, endParenthesis)) return false;
std::string fullMatch = what[0];
std::string rest = what[1];
it += (fullMatch.size() - rest.size());
return true;
}
}
\ No newline at end of file
#ifndef MSLANG_DIFFERENCE_H
#define MSLANG_DIFFERENCE_H
#include "Utilities/MSLang.h"
namespace mslang {
struct Difference: public Function {
Function* dividend_m;
Function* divisor_m;
Difference()
{ }