Commit e19111ce authored by kraus's avatar kraus
Browse files

replace plain pointers with std::shared_ptr in MSLang

parent 65dbadd3
......@@ -36,8 +36,8 @@ FlexibleCollimator::FlexibleCollimator(const FlexibleCollimator &right):
lossDs_m(nullptr),
parmatint_m(NULL)
{
for (const mslang::Base *obj: right.holes_m) {
holes_m.push_back(obj->clone());
for (const std::shared_ptr<mslang::Base> obj: right.holes_m) {
holes_m.emplace_back(obj->clone());
}
tree_m.bb_m = bb_m;
......@@ -61,9 +61,9 @@ FlexibleCollimator::~FlexibleCollimator() {
if (online_m)
goOffline();
for (mslang::Base *obj: holes_m) {
delete obj;
}
// for (mslang::Base *obj: holes_m) {
// delete obj;
// }
}
......@@ -214,7 +214,7 @@ void FlexibleCollimator::setDescription(const std::string &desc) {
it->computeBoundingBox();
}
mslang::Base *first = holes_m.front();
std::shared_ptr<mslang::Base> &first = holes_m.front();
const mslang::BoundingBox &bb = first->bb_m;
Vector_t llc(bb.center_m[0] - 0.5 * bb.width_m,
......@@ -246,8 +246,10 @@ void FlexibleCollimator::setDescription(const std::string &desc) {
tree_m.objects_m.insert(tree_m.objects_m.end(), holes_m.begin(), holes_m.end());
tree_m.buildUp();
if (Ippl::myNode() == 0) {
std::ofstream out("data/quadtree.gpl");
tree_m.writeGnuplot(out);
}
// if (Ippl::myNode() == 0) {
// std::ofstream out("data/quadtree.gpl");
// tree_m.writeGnuplot(out);
// }
delete fun;
}
\ No newline at end of file
......@@ -5,6 +5,8 @@
#include "Utilities/MSLang.h"
#include "Utilities/MSLang/QuadTree.h"
#include <memory>
class BeamlineVisitor;
class LossDataSink;
// Class FlexibleCollimator
......@@ -66,7 +68,7 @@ private:
void operator=(const FlexibleCollimator &);
std::string description_m;
std::vector<mslang::Base*> holes_m;
std::vector<std::shared_ptr<mslang::Base> > holes_m;
mslang::BoundingBox bb_m;
mslang::QuadTree tree_m;
......
......@@ -10,6 +10,7 @@
#include <iostream>
#include <fstream>
#include <list>
#include <memory>
namespace mslang {
typedef std::string::iterator iterator;
......@@ -26,7 +27,7 @@ namespace mslang {
virtual ~Function() {};
virtual void print(int indent) = 0;
virtual void apply(std::vector<Base*> &bfuncs) = 0;
virtual void apply(std::vector<std::shared_ptr<Base>> &bfuncs) = 0;
static bool parse(iterator &it, const iterator &end, Function* &fun);
......@@ -39,7 +40,7 @@ namespace mslang {
struct Base: public Function {
AffineTransformation trafo_m;
BoundingBox bb_m;
std::vector<Base*> divisor_m;
std::vector<std::shared_ptr<Base> > divisor_m;
Base():
trafo_m()
......@@ -51,21 +52,20 @@ namespace mslang {
{ }
virtual ~Base() {
for (auto item: divisor_m) {
delete item;
item = NULL;
}
// for (auto item: divisor_m) {
// item.reset();
// }
divisor_m.clear();
}
virtual Base* clone() const = 0;
virtual std::shared_ptr<Base> clone() const = 0;
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) {
virtual void divideBy(std::vector<std::shared_ptr<Base> > &divisors) {
for (auto item: divisors) {
if (bb_m.doesIntersect(item->bb_m)) {
divisor_m.push_back(item->clone());
divisor_m.emplace_back(std::move(item->clone()));
}
}
}
......
......@@ -23,7 +23,7 @@ namespace mslang {
isInside(b.center_m + 0.5 * Vector_t( b.width_m, -b.height_m, 0.0)));
}
void BoundingBox::writeGnuplot(std::ofstream &out) const {
void BoundingBox::writeGnuplot(std::ostream &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),
......
......@@ -34,7 +34,7 @@ namespace mslang {
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;
virtual void writeGnuplot(std::ostream &out) const;
void print(std::ostream &out) const;
};
......
......@@ -13,20 +13,20 @@ namespace mslang {
divisor_m->print(indentwidth + 8);
}
void Difference::apply(std::vector<Base*> &bfuncs) {
std::vector<Base*> nom, denom;
void Difference::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
std::vector<std::shared_ptr<Base> > nom, denom;
dividend_m->apply(nom);
divisor_m->apply(denom);
for (auto item: nom) {
item->divideBy(denom);
bfuncs.push_back(item->clone());
bfuncs.emplace_back(std::move(item->clone()));
}
for (auto item: nom)
delete item;
item.reset();
for (auto item: denom)
delete item;
item.reset();
}
bool Difference::parse_detail(iterator &it, const iterator &end, Function* &fun) {
......
......@@ -22,9 +22,9 @@ namespace mslang {
}
virtual void print(int indentwidth);
virtual void apply(std::vector<Base*> &bfuncs);
virtual void apply(std::vector<std::shared_ptr<Base> > &bfuncs);
static bool parse_detail(iterator &it, const iterator &end, Function* &fun);
};
}
#endif
#endif
\ No newline at end of file
......@@ -50,22 +50,22 @@ namespace mslang {
// bb_m.writeGnuplot(out);
}
void Ellipse::apply(std::vector<Base*> &bfuncs) {
bfuncs.push_back(this->clone());
void Ellipse::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
bfuncs.emplace_back(std::move(this->clone()));
}
Base* Ellipse::clone() const{
Ellipse *elps = new Ellipse;
std::shared_ptr<Base> Ellipse::clone() const{
std::shared_ptr<Ellipse> elps(new Ellipse);
elps->width_m = width_m;
elps->height_m = height_m;
elps->trafo_m = trafo_m;
elps->bb_m = bb_m;
for (auto item: divisor_m) {
elps->divisor_m.push_back(item->clone());
elps->divisor_m.emplace_back(std::move(item->clone()));
}
return elps;
return std::static_pointer_cast<Base>(elps);
}
void Ellipse::computeBoundingBox() {
......
......@@ -24,12 +24,12 @@ namespace mslang {
virtual void print(int indentwidth);
virtual void writeGnuplot(std::ofstream &out) const;
virtual void apply(std::vector<Base*> &bfuncs);
virtual Base* clone() const;
virtual void apply(std::vector<std::shared_ptr<Base> > &bfuncs);
virtual std::shared_ptr<Base> clone() const;
virtual void computeBoundingBox();
virtual bool isInside(const Vector_t &R) const;
static bool parse_detail(iterator &it, const iterator &end, Function* fun);
};
}
#endif
#endif
\ No newline at end of file
......@@ -13,8 +13,8 @@ namespace mslang {
secondOperand_m->print(indentwidth + 8);
}
void Intersection::apply(std::vector<Base*> &bfuncs) {
std::vector<Base*> first, firstrep, second;
void Intersection::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
std::vector<std::shared_ptr<Base> > first, firstrep, second;
firstOperand_m->apply(first);
firstOperand_m->apply(firstrep);
......@@ -26,15 +26,15 @@ namespace mslang {
for (auto item: first) {
item->divideBy(firstrep);
bfuncs.push_back(item->clone());
bfuncs.emplace_back(std::move(item->clone()));
}
for (auto item: first)
delete item;
item.reset();
for (auto item: firstrep)
delete item;
item.reset();
for (auto item: second)
delete item;
item.reset();
}
bool Intersection::parse_detail(iterator &it, const iterator &end, Function* &fun) {
......
......@@ -22,9 +22,9 @@ namespace mslang {
}
virtual void print(int indentwidth);
virtual void apply(std::vector<Base*> &bfuncs);
virtual void apply(std::vector<std::shared_ptr<Base> > &bfuncs);
static bool parse_detail(iterator &it, const iterator &end, Function* &fun);
};
}
#endif
#endif
\ No newline at end of file
......@@ -73,7 +73,7 @@ namespace mslang {
for (auto pix: pixels_m) pix.print(ident);
}
void Mask::apply(std::vector<Base*> &bfuncs) {
void Mask::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
for (auto pix: pixels_m) pix.apply(bfuncs);
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ namespace mslang {
struct Mask: public Function {
static bool parse_detail(iterator &it, const iterator &end, Function* &fun);
virtual void print(int ident);
virtual void apply(std::vector<Base*> &bfuncs);
virtual void apply(std::vector<std::shared_ptr<Base> > &bfuncs);
std::vector<Rectangle> pixels_m;
};
......
......@@ -41,8 +41,8 @@ namespace mslang {
// for (auto pix: pixels_m) pix.print(ident);
}
void Polygon::apply(std::vector<Base*> &bfuncs) {
void Polygon::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
for (Triangle &tri: triangles_m)
bfuncs.push_back(tri.clone());
bfuncs.push_back(std::shared_ptr<Base>(tri.clone()));
}
}
\ No newline at end of file
......@@ -11,8 +11,8 @@ namespace mslang {
void triangulize(std::vector<Vector_t> &nodes);
static bool parse_detail(iterator &it, const iterator &end, Function* &fun);
virtual void print(int ident);
virtual void apply(std::vector<Base*> &bfuncs);
virtual void apply(std::vector<std::shared_ptr<Base> > &bfuncs);
};
}
#endif
#endif
\ No newline at end of file
#include "Utilities/MSLang/QuadTree.h"
#include <utility>
namespace mslang {
QuadTree::QuadTree(const QuadTree &right):
......@@ -6,24 +7,22 @@ namespace mslang {
objects_m(right.objects_m.begin(),
right.objects_m.end()),
bb_m(right.bb_m),
nodes_m(0)
nodes_m()
{
if (right.nodes_m != 0) {
nodes_m = new QuadTree[4];
if (right.nodes_m.size() != 0) {
nodes_m.resize(4);
for (unsigned int i = 0; i < 4u; ++ i) {
nodes_m[i] = right.nodes_m[i];
*nodes_m[i] = *right.nodes_m[i];
}
}
}
QuadTree::~QuadTree() {
for (Base *&obj: objects_m)
obj = 0; // memory isn't handled by QuadTree class
// for (std::shared_ptr<Base> &obj: objects_m)
// obj.reset();
if (nodes_m != 0) {
delete[] nodes_m;
}
nodes_m = 0;
objects_m.clear();
nodes_m.clear();
}
void QuadTree::operator=(const QuadTree &right) {
......@@ -33,81 +32,57 @@ namespace mslang {
right.objects_m.end());
bb_m = right.bb_m;
if (nodes_m != 0) delete[] nodes_m;
nodes_m = 0;
if (nodes_m.size() != 0) nodes_m.clear();
if (right.nodes_m != 0) {
nodes_m = new QuadTree[4];
if (right.nodes_m.size() != 0) {
nodes_m.resize(4);
for (unsigned int i = 0; i < 4u; ++ i) {
nodes_m[i] = right.nodes_m[i];
*nodes_m[i] = *right.nodes_m[i];
}
}
}
void QuadTree::transferIfInside(std::list<Base*> &objs) {
for (Base* &obj: objs) {
void QuadTree::transferIfInside(std::list<std::shared_ptr<Base> > &objs) {
for (std::shared_ptr<Base> &obj: objs) {
if (bb_m.isInside(obj->bb_m)) {
objects_m.push_back(obj);
obj = 0;
objects_m.emplace_back(std::move(obj));
}
}
objs.remove_if([](const Base *obj) { return obj == 0; });
objs.remove_if([](const std::shared_ptr<Base> obj) { return !obj; });
}
void QuadTree::buildUp() {
QuadTree *next = new QuadTree[4];
next[0] = QuadTree(level_m + 1,
BoundingBox(bb_m.center_m,
Vector_t(bb_m.center_m[0] + 0.5 * bb_m.width_m,
bb_m.center_m[1] + 0.5 * bb_m.height_m,
0.0)));
next[1] = QuadTree(level_m + 1,
BoundingBox(Vector_t(bb_m.center_m[0],
bb_m.center_m[1] - 0.5 * bb_m.height_m,
0.0),
Vector_t(bb_m.center_m[0] + 0.5 * bb_m.width_m,
bb_m.center_m[1],
0.0)));
next[2] = QuadTree(level_m + 1,
BoundingBox(Vector_t(bb_m.center_m[0] - 0.5 * bb_m.width_m,
bb_m.center_m[1],
0.0),
Vector_t(bb_m.center_m[0],
bb_m.center_m[1] + 0.5 * bb_m.height_m,
0.0)));
next[3] = QuadTree(level_m + 1,
BoundingBox(Vector_t(bb_m.center_m[0] - 0.5 * bb_m.width_m,
bb_m.center_m[1] - 0.5 * bb_m.height_m,
0.0),
bb_m.center_m));
for (unsigned int i = 0; i < 4u; ++ i) {
next[i].transferIfInside(objects_m);
}
double X[] = {bb_m.center_m[0] - 0.5 * bb_m.width_m,
bb_m.center_m[0],
bb_m.center_m[0] + 0.5 * bb_m.width_m};
double Y[] = {bb_m.center_m[1] - 0.5 * bb_m.height_m,
bb_m.center_m[1],
bb_m.center_m[1] + 0.5 * bb_m.height_m};
bool allEmpty = true;
nodes_m.reserve(4);
for (unsigned int i = 0; i < 4u; ++ i) {
if (next[i].objects_m.size() != 0) {
nodes_m.emplace_back(new QuadTree(level_m + 1,
BoundingBox(Vector_t(X[i / 2], Y[i % 2], 0.0),
Vector_t(X[i / 2 + 1], Y[i % 2 + 1], 0.0))));
nodes_m.back()->transferIfInside(objects_m);
if (nodes_m.back()->objects_m.size() != 0) {
allEmpty = false;
break;
}
}
if (allEmpty) {
for (unsigned int i = 0; i < 4u; ++ i) {
objects_m.merge(next[i].objects_m);
}
delete[] next;
nodes_m.clear();
return;
}
for (unsigned int i = 0; i < 4u; ++ i) {
next[i].buildUp();
nodes_m[i]->buildUp();
}
nodes_m = next;
}
void QuadTree::writeGnuplot(std::ofstream &out) const {
......@@ -119,25 +94,25 @@ namespace mslang {
// }
out << std::endl;
if (nodes_m != 0) {
if (nodes_m.size() != 0) {
for (unsigned int i = 0; i < 4u; ++ i) {
nodes_m[i].writeGnuplot(out);
nodes_m[i]->writeGnuplot(out);
}
}
}
bool QuadTree::isInside(const Vector_t &R) const {
if (nodes_m != 0) {
if (nodes_m.size() != 0) {
Vector_t X = R - bb_m.center_m;
unsigned int idx = (X[1] >= 0.0 ? 0: 1);
idx += (X[0] >= 0.0 ? 0: 2);
unsigned int idx = (X[1] < 0.0 ? 0: 1);
idx += (X[0] < 0.0 ? 0: 2);
if (nodes_m[idx].isInside(R)) {
if (nodes_m[idx]->isInside(R)) {
return true;
}
}
for (Base* obj: objects_m) {
for (const std::shared_ptr<Base> & obj: objects_m) {
if (obj->isInside(R)) {
return true;
}
......@@ -145,4 +120,14 @@ namespace mslang {
return false;
}
void QuadTree::getDepth(unsigned int &d) const {
if (nodes_m.size() > 0) {
for (const auto & node: nodes_m) {
node->getDepth(d);
}
} else {
if ((unsigned int)level_m > d) d = level_m;
}
}
}
\ No newline at end of file
......@@ -3,23 +3,25 @@
#include "Utilities/MSLang.h"
#include <memory>
namespace mslang {
struct QuadTree {
int level_m;
std::list<Base*> objects_m;
std::list<std::shared_ptr<Base> > objects_m;
BoundingBox bb_m;
QuadTree *nodes_m;
std::vector<std::shared_ptr<QuadTree> > nodes_m;
QuadTree():
level_m(0),
bb_m(),
nodes_m(0)
nodes_m()
{ }
QuadTree(int l, const BoundingBox &b):
level_m(l),
bb_m(b),
nodes_m(0)
nodes_m()
{ }
QuadTree(const QuadTree &right);
......@@ -28,12 +30,14 @@ namespace mslang {
void operator=(const QuadTree &right);
void transferIfInside(std::list<Base*> &objs);
void transferIfInside(std::list<std::shared_ptr<Base> > &objs);
void buildUp();
void writeGnuplot(std::ofstream &out) const;
bool isInside(const Vector_t &R) const;
void getDepth(unsigned int &d) const;
};
}
......
......@@ -86,22 +86,22 @@ namespace mslang {
// bb_m.writeGnuplot(out);
}
void Rectangle::apply(std::vector<Base*> &bfuncs) {
bfuncs.push_back(this->clone());
void Rectangle::apply(std::vector<std::shared_ptr<Base> > &bfuncs) {
bfuncs.emplace_back(std::move(this->clone()));
}
Base* Rectangle::clone() const {
Rectangle *rect = new Rectangle;
std::shared_ptr<Base> Rectangle::clone() const {
std::shared_ptr<Rectangle> rect(new Rectangle);
rect->width_m = width_m;
rect->height_m = height_m;
rect->trafo_m = trafo_m;
rect->bb_m = bb_m;
for (auto item: divisor_m) {
rect->divisor_m.push_back(item->clone());
rect->divisor_m.emplace_back(std::move(item->clone()));
}
return rect;
return std::static_pointer_cast<Base>(rect);
}
bool Rectangle::parse_detail(iterator &it, const iterator &end, Function* fun) {
......
......@@ -26,8 +26,8 @@ namespace mslang {
virtual void computeBoundingBox();