mslang.cpp 24.6 KB
Newer Older
kraus's avatar
kraus committed
1 2 3
#include "Algorithms/CoordinateSystemTrafo.h"
#include "Physics/Physics.h"

4 5 6 7
#include <boost/regex.hpp>

#include <iostream>
#include <string>
8 9
#include <fstream>
#include <streambuf>
10
#include <cstdlib>
11
#include <cmath>
kraus's avatar
kraus committed
12
#include <tuple>
13 14 15 16 17 18

std::string UDouble("([0-9]+\\.?[0-9]*([Ee][+-]?[0-9]+)?)");
std::string Double("(-?[0-9]+\\.?[0-9]*([Ee][+-]?[0-9]+)?)");
std::string UInt("([0-9]+)");
std::string FCall("([a-z]*)\\((.*)");

kraus's avatar
kraus committed
19 20
typedef std::string::iterator iterator;

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
struct BoundingVolume {
    virtual bool isInside(const Vector_t &X) const = 0;
};

struct BoundingBox: public BoundingVolume {
    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 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])
    { }

    virtual 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;
    }

    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;
    }
};

struct BoundingSphere: public BoundingVolume {
    Vector_t center_m;
    double radiusSqr_m;

    BoundingSphere(const Vector_t &c,
                   double r):
        center_m(c),
        radiusSqr_m(r*r)
    { }

    virtual bool isInside(const Vector_t &X) const {
        if (std::pow(X[0] - center_m[0], 2) +
            std::pow(X[1] - center_m[1], 2) < radiusSqr_m)
            return true;

        return false;
    }

};

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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);
124 125 126
        Vector_t w = dot(A, b);

        return Vector_t(w[0], w[1], 0.0);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    }

    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;
    }
};

kraus's avatar
kraus committed
146
struct Base;
kraus's avatar
kraus committed
147

kraus's avatar
kraus committed
148 149
struct Function {
    virtual ~Function() {};
kraus's avatar
kraus committed
150

151
    virtual void print(int indent) = 0;
kraus's avatar
kraus committed
152
    virtual void apply(std::vector<Base*> &bfuncs) = 0;
153 154
};

kraus's avatar
kraus committed
155 156 157
bool parse(iterator &it, const iterator &end, Function* &fun);

struct Base: public Function {
158
    AffineTransformation trafo_m;
159
    BoundingBox bb_m;
kraus's avatar
kraus committed
160 161 162 163
    // std::tuple<unsigned int,
    //            double,
    //            double> repeat_m;
    Base():
164
        trafo_m()
kraus's avatar
kraus committed
165
        // , repeat_m(std::make_tuple(0u, 0.0, 0.0))
kraus's avatar
kraus committed
166 167
    { }

kraus's avatar
kraus committed
168
    virtual Base* clone() = 0;
169
    virtual void writeGnuplot(std::ofstream &out) const = 0;
170
    virtual void computeBoundingBox() = 0;
kraus's avatar
kraus committed
171 172
};

kraus's avatar
kraus committed
173 174 175
struct Rectangle: public Base {
    double width_m;
    double height_m;
176

kraus's avatar
kraus committed
177 178 179 180
    Rectangle():
        Base(),
        width_m(0.0),
        height_m(0.0)
kraus's avatar
kraus committed
181 182
    { }

kraus's avatar
kraus committed
183
    virtual ~Rectangle() { }
kraus's avatar
kraus committed
184

185 186 187
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
kraus's avatar
kraus committed
188
        Vector_t origin = trafo_m.getOrigin();
189
        double angle = trafo_m.getAngle() * Physics::rad2deg;
190
        std::cout << indent << "rectangle, \n"
kraus's avatar
kraus committed
191 192
                  << indent2 << "w: " << width_m << ", \n"
                  << indent2 << "h: " << height_m << ", \n"
kraus's avatar
kraus committed
193
                  << indent2 << "origin: " << origin[0] << ", " << origin[1] << ",\n"
194 195 196 197
                  << indent2 << "angle: " << angle << "\n"
                  << indent2 << trafo_m(0, 0) << "\t" << trafo_m(0, 1) << "\t" << trafo_m(0, 2) << "\n"
                  << indent2 << trafo_m(1, 0) << "\t" << trafo_m(1, 1) << "\t" << trafo_m(1, 2) << "\n"
                  << indent2 << trafo_m(2, 0) << "\t" << trafo_m(2, 1) << "\t" << trafo_m(2, 2) << std::endl;
kraus's avatar
kraus committed
198 199
    }

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    virtual void computeBoundingBox() {
        std::vector<Vector_t> corners({Vector_t(0.5 * width_m, 0.5 * height_m, 0),
                    Vector_t(-0.5 * width_m, 0.5 * height_m, 0),
                    Vector_t(-0.5 * width_m, -0.5 * height_m, 0),
                    Vector_t(0.5 * width_m, -0.5 * height_m, 0)});

        for (Vector_t &v: corners) {
            v = trafo_m.transformFrom(v);
        }

        Vector_t llc = corners[0], urc = corners[0];
        for (unsigned int i = 1; i < 4; ++ i) {
            if (corners[i][0] < llc[0]) llc[0] = corners[i][0];
            else if (corners[i][0] > urc[0]) urc[0] = corners[i][0];

            if (corners[i][1] < llc[1]) llc[1] = corners[i][1];
            else if (corners[i][1] > urc[1]) urc[1] = corners[i][1];
        }

        bb_m = BoundingBox(llc, urc);
    }

222
    virtual void writeGnuplot(std::ofstream &out) const {
kraus's avatar
kraus committed
223 224 225 226
        std::vector<Vector_t> pts({Vector_t(0.5 * width_m, 0.5 * height_m, 0),
                                   Vector_t(-0.5 * width_m, 0.5 * height_m, 0),
                                   Vector_t(-0.5 * width_m, -0.5 * height_m, 0),
                                   Vector_t(0.5 * width_m, -0.5 * height_m, 0)});
227 228 229
        unsigned int width = out.precision() + 8;
        for (unsigned int i = 0; i < 5; ++ i) {
            Vector_t pt = pts[i % 4];
kraus's avatar
kraus committed
230
            pt = trafo_m.transformFrom(pt);
231 232 233 234 235 236

            out << std::setw(width) << pt[0]
                << std::setw(width) << pt[1]
                << std::endl;
        }
        out << std::endl;
237 238

        bb_m.writeGnuplot(out);
239 240
    }

kraus's avatar
kraus committed
241
    virtual void apply(std::vector<Base*> &bfuncs) {
kraus's avatar
kraus committed
242 243 244
        bfuncs.push_back(this->clone());
    }

kraus's avatar
kraus committed
245 246 247 248 249
    virtual Base* clone() {
        Rectangle *rect = new Rectangle;
        rect->width_m = width_m;
        rect->height_m = height_m;
        rect->trafo_m = trafo_m;
kraus's avatar
kraus committed
250 251

        return rect;
252 253 254
    }

    static
kraus's avatar
kraus committed
255
    bool parse_detail(iterator &it, const iterator &end, Function* fun) {
256 257 258 259 260 261
        std::string str(it, end);
        boost::regex argumentList(UDouble + "," + UDouble + "(\\).*)");
        boost::smatch what;

        if (!boost::regex_match(str, what, argumentList)) return false;

kraus's avatar
kraus committed
262 263 264
        Rectangle *rect = static_cast<Rectangle*>(fun);
        rect->width_m = atof(std::string(what[1]).c_str());
        rect->height_m = atof(std::string(what[3]).c_str());
265 266 267 268 269 270 271 272 273 274

        std::string fullMatch = what[0];
        std::string rest = what[5];
        it += (fullMatch.size() - rest.size() + 1);

        return true;

    }
};

kraus's avatar
kraus committed
275 276 277
struct Ellipse: public Base {
    double width_m;
    double height_m;
278

kraus's avatar
kraus committed
279 280 281
    Ellipse():
        width_m(0.0),
        height_m(0.0)
kraus's avatar
kraus committed
282 283
    { }

kraus's avatar
kraus committed
284
    virtual ~Ellipse() { }
kraus's avatar
kraus committed
285

286 287 288
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
kraus's avatar
kraus committed
289
        Vector_t origin = trafo_m.getOrigin();
290
        double angle = trafo_m.getAngle() * Physics::rad2deg;
291
        std::cout << indent << "ellipse, \n"
kraus's avatar
kraus committed
292 293
                  << indent2 << "w: " << width_m << ", \n"
                  << indent2 << "h: " << height_m << ", \n"
kraus's avatar
kraus committed
294
                  << indent2 << "origin: " << origin[0] << ", " << origin[1] << ",\n"
295
                  << indent2 << "angle: " << angle << "\n"
296 297 298 299
                  // << indent2 << trafo_m(0, 0) << "\t" << trafo_m(0, 1) << "\t" << trafo_m(0, 2) << "\n"
                  // << indent2 << trafo_m(1, 0) << "\t" << trafo_m(1, 1) << "\t" << trafo_m(1, 2) << "\n"
                  // << indent2 << trafo_m(2, 0) << "\t" << trafo_m(2, 1) << "\t" << trafo_m(2, 2)
                  << std::endl;
kraus's avatar
kraus committed
300 301
    }

302 303 304 305 306 307 308 309
    virtual void writeGnuplot(std::ofstream &out) const {
        const unsigned int N = 101;
        const double dp = Physics::two_pi / (N - 1);
        const unsigned int colwidth = out.precision() + 8;

        double phi = 0;
        for (unsigned int i = 0; i < N; ++ i, phi += dp) {
            Vector_t pt(0.0);
kraus's avatar
kraus committed
310 311 312
            pt[0] = std::copysign(sqrt(std::pow(height_m * width_m * 0.25, 2) /
                                       (std::pow(height_m * 0.5, 2) +
                                        std::pow(width_m * 0.5 * tan(phi), 2))),
313 314
                                  cos(phi));
            pt[1] = pt[0] * tan(phi);
kraus's avatar
kraus committed
315
            pt = trafo_m.transformFrom(pt);
316 317 318 319 320 321 322

            out << std::setw(colwidth) << pt[0]
                << std::setw(colwidth) << pt[1]
                << std::endl;
        }
        out << std::endl;

323
        bb_m.writeGnuplot(out);
324 325
    }

kraus's avatar
kraus committed
326
    virtual void apply(std::vector<Base*> &bfuncs) {
kraus's avatar
kraus committed
327 328 329
        bfuncs.push_back(this->clone());
    }

kraus's avatar
kraus committed
330 331 332 333 334
    virtual Base* clone() {
        Ellipse *elps = new Ellipse;
        elps->width_m = width_m;
        elps->height_m = height_m;
        elps->trafo_m = trafo_m;
kraus's avatar
kraus committed
335 336

        return elps;
337 338
    }

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    virtual void computeBoundingBox() {
        // TODO: Currently doesn't take into account shear operation!!
        Vector_t llc(0.0), urc(0.0);
        const Vector_t e_x(1.0, 0.0, 0.0), e_y(0.0, 1.0, 0.0);
        const Vector_t center = trafo_m.transformFrom(Vector_t(0.0));
        const Vector_t e_xp = trafo_m.transformFrom(e_x) - center;
        const Vector_t e_yp = trafo_m.transformFrom(e_y) - center;
        const double &M11 = e_xp[0];
        const double &M12 = e_yp[0];
        const double &M21 = e_xp[1];
        const double &M22 = e_yp[1];

        double t = atan2(height_m * M12, width_m * M11);
        double halfwidth = 0.5 * (M11 * width_m * cos(t) +
                                  M12 * height_m * sin(t));
        llc[0] = center[0] - std::abs(halfwidth);
        urc[0] = center[0] + std::abs(halfwidth);

        t = atan2(height_m * M22, width_m * M21);

        double halfheight = 0.5 * (M21 * width_m * cos(t) +
                                   M22 * height_m * sin(t));

        llc[1] = center[1] - std::abs(halfheight);
        urc[1] = center[1] + std::abs(halfheight);

        bb_m = BoundingBox(llc, urc);
    }

368
    static
kraus's avatar
kraus committed
369
    bool parse_detail(iterator &it, const iterator &end, Function* fun) {
370 371 372 373 374 375
        std::string str(it, end);
        boost::regex argumentList(UDouble + "," + UDouble + "(\\).*)");
        boost::smatch what;

        if (!boost::regex_match(str, what, argumentList)) return false;

kraus's avatar
kraus committed
376 377 378
        Ellipse *elps = static_cast<Ellipse*>(fun);
        elps->width_m = atof(std::string(what[1]).c_str());
        elps->height_m = atof(std::string(what[3]).c_str());
379 380 381 382 383 384 385 386 387 388

        std::string fullMatch = what[0];
        std::string rest = what[5];
        it += (fullMatch.size() - rest.size() + 1);

        return true;

    }
};

kraus's avatar
kraus committed
389 390 391 392 393
struct Repeat: public Function {
    Function* func_m;
    unsigned int N_m;
    double shiftx_m;
    double shifty_m;
394

kraus's avatar
kraus committed
395 396
    virtual ~Repeat() {
        delete func_m;
kraus's avatar
kraus committed
397 398
    }

399 400 401 402
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "repeat, " << std::endl;
kraus's avatar
kraus committed
403
        func_m->print(indentwidth + 8);
404
        std::cout << ",\n"
kraus's avatar
kraus committed
405 406 407
                  << indent2 << "N: " << N_m << ", \n"
                  << indent2 << "dx: " << shiftx_m << ", \n"
                  << indent2 << "dy: " << shifty_m;
kraus's avatar
kraus committed
408 409
    }

kraus's avatar
kraus committed
410
    virtual void apply(std::vector<Base*> &bfuncs) {
411 412 413 414 415
        AffineTransformation shift(Vector_t(1.0, 0.0, -shiftx_m),
                                   Vector_t(0.0, 1.0, -shifty_m));
        std::cout << shiftx_m << "\t" << shift(0, 2) << std::endl;
        // CoordinateSystemTrafo shift(Vector_t(shiftx_m, shifty_m, 0.0),
        //                             Quaternion(1.0, 0.0, 0.0, 0.0));
kraus's avatar
kraus committed
416
        func_m->apply(bfuncs);
kraus's avatar
kraus committed
417 418
        const unsigned int size = bfuncs.size();

419
        AffineTransformation current_shift = shift;
kraus's avatar
kraus committed
420
        for (unsigned int i = 0; i < N_m; ++ i) {
kraus's avatar
kraus committed
421
            for (unsigned int j = 0; j < size; ++ j) {
kraus's avatar
kraus committed
422
                Base *obj = bfuncs[j]->clone();
423
                obj->trafo_m = obj->trafo_m.mult(current_shift);
kraus's avatar
kraus committed
424 425 426
                bfuncs.push_back(obj);
            }

427
            current_shift = current_shift.mult(shift);
kraus's avatar
kraus committed
428
        }
429 430 431
    }

    static
kraus's avatar
kraus committed
432 433 434
    bool parse_detail(iterator &it, const iterator &end, Function* &fun) {
        Repeat *rep = static_cast<Repeat*>(fun);
        if (!parse(it, end, rep->func_m)) return false;
435 436 437 438 439 440 441

        boost::regex argumentList("," + UInt + "," + Double + "," + Double + "\\)(.*)");
        boost::smatch what;

        std::string str(it, end);
        if (!boost::regex_match(str, what, argumentList)) return false;

kraus's avatar
kraus committed
442 443 444
        rep->N_m = atof(std::string(what[1]).c_str());
        rep->shiftx_m = atof(std::string(what[2]).c_str());
        rep->shifty_m = atof(std::string(what[4]).c_str());
445 446 447 448 449 450 451 452 453 454

        std::string fullMatch = what[0];
        std::string rest = what[6];

        it += (fullMatch.size() - rest.size());

        return true;
    }
};

kraus's avatar
kraus committed
455 456 457 458
struct Translate: public Function {
    Function* func_m;
    double shiftx_m;
    double shifty_m;
459

kraus's avatar
kraus committed
460 461
    virtual ~Translate() {
        delete func_m;
kraus's avatar
kraus committed
462 463
    }

464 465 466 467
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "translate, " << std::endl;
kraus's avatar
kraus committed
468
        func_m->print(indentwidth + 8);
469
        std::cout << ",\n"
kraus's avatar
kraus committed
470 471
                  << indent2 << "dx: " << shiftx_m << ", \n"
                  << indent2 << "dy: " << shifty_m;
kraus's avatar
kraus committed
472 473
    }

kraus's avatar
kraus committed
474
    virtual void apply(std::vector<Base*> &bfuncs) {
475 476 477
        AffineTransformation shift(Vector_t(1.0, 0.0, -shiftx_m),
                                   Vector_t(0.0, 1.0, -shifty_m));

kraus's avatar
kraus committed
478
        func_m->apply(bfuncs);
kraus's avatar
kraus committed
479 480 481
        const unsigned int size = bfuncs.size();

        for (unsigned int j = 0; j < size; ++ j) {
kraus's avatar
kraus committed
482
            Base *obj = bfuncs[j];
483
            obj->trafo_m = obj->trafo_m.mult(shift);
kraus's avatar
kraus committed
484
        }
485 486 487
    }

    static
kraus's avatar
kraus committed
488 489 490
    bool parse_detail(iterator &it, const iterator &end, Function* &fun) {
        Translate *trans = static_cast<Translate*>(fun);
        if (!parse(it, end, trans->func_m)) return false;
491 492 493 494 495 496 497

        boost::regex argumentList("," + Double + "," + Double + "\\)(.*)");
        boost::smatch what;

        std::string str(it, end);
        if (!boost::regex_match(str, what, argumentList)) return false;

kraus's avatar
kraus committed
498 499
        trans->shiftx_m = atof(std::string(what[1]).c_str());
        trans->shifty_m = atof(std::string(what[3]).c_str());
500 501 502 503 504 505 506 507 508 509 510

        std::string fullMatch = what[0];
        std::string rest = what[5];

        it += (fullMatch.size() - rest.size());

        return true;
    }
};


kraus's avatar
kraus committed
511 512 513
struct Rotate: public Function {
    Function* func_m;
    double angle_m;
514

kraus's avatar
kraus committed
515 516
    virtual ~Rotate() {
        delete func_m;
kraus's avatar
kraus committed
517 518
    }

519 520 521 522
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "rotate, " << std::endl;
kraus's avatar
kraus committed
523
        func_m->print(indentwidth + 8);
524
        std::cout << ",\n"
kraus's avatar
kraus committed
525
                  << indent2 << "angle: " << angle_m;
kraus's avatar
kraus committed
526 527
    }

kraus's avatar
kraus committed
528
    virtual void apply(std::vector<Base*> &bfuncs) {
529 530 531
        AffineTransformation rotation(Vector_t(cos(angle_m), sin(angle_m), 0.0),
                                      Vector_t(-sin(angle_m), cos(angle_m), 0.0));

kraus's avatar
kraus committed
532
        func_m->apply(bfuncs);
kraus's avatar
kraus committed
533 534 535
        const unsigned int size = bfuncs.size();

        for (unsigned int j = 0; j < size; ++ j) {
kraus's avatar
kraus committed
536
            Base *obj = bfuncs[j];
537
            obj->trafo_m = obj->trafo_m.mult(rotation);
kraus's avatar
kraus committed
538
        }
539 540 541
    }

    static
kraus's avatar
kraus committed
542 543 544
    bool parse_detail(iterator &it, const iterator &end, Function* &fun) {
        Rotate *rot = static_cast<Rotate*>(fun);
        if (!parse(it, end, rot->func_m)) return false;
545

kraus's avatar
kraus committed
546
        boost::regex argumentList("," + Double + "\\)(.*)");
547 548 549 550 551
        boost::smatch what;

        std::string str(it, end);
        if (!boost::regex_match(str, what, argumentList)) return false;

kraus's avatar
kraus committed
552
        rot->angle_m = atof(std::string(what[1]).c_str());
553 554 555 556 557 558 559 560 561 562

        std::string fullMatch = what[0];
        std::string rest = what[3];

        it += (fullMatch.size() - rest.size());

        return true;
    }
};

kraus's avatar
kraus committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
struct Shear: public Function {
    Function* func_m;
    double angleX_m;
    double angleY_m;

    virtual ~Shear() {
        delete func_m;
    }

    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "shear, " << std::endl;
        func_m->print(indentwidth + 8);
        if (std::abs(angleX_m) > 0.0) {
            std::cout << ",\n"
                      << indent2 << "angle X: " << angleX_m;
        } else {
            std::cout << ",\n"
                      << indent2 << "angle Y: " << angleY_m;
        }
    }

    virtual void apply(std::vector<Base*> &bfuncs) {
        AffineTransformation shear(Vector_t(1.0, tan(angleX_m), 0.0),
                                   Vector_t(-tan(angleY_m), 1.0, 0.0));

        func_m->apply(bfuncs);
        const unsigned int size = bfuncs.size();

        for (unsigned int j = 0; j < size; ++ j) {
            Base *obj = bfuncs[j];
            obj->trafo_m = obj->trafo_m.mult(shear);
        }
    }

    static
    bool parse_detail(iterator &it, const iterator &end, Function* &fun) {
        Shear *shr = static_cast<Shear*>(fun);
        if (!parse(it, end, shr->func_m)) return false;

        boost::regex argumentList("," + Double + "," + Double + "\\)(.*)");
        boost::smatch what;

        std::string str(it, end);
        if (!boost::regex_match(str, what, argumentList)) return false;

        shr->angleX_m = atof(std::string(what[1]).c_str());
        shr->angleY_m = atof(std::string(what[3]).c_str());

        if (std::abs(shr->angleX_m) > 0.0 && std::abs(shr->angleY_m) > 0.0)
            return false;

        std::string fullMatch = what[0];
        std::string rest = what[5];

        it += (fullMatch.size() - rest.size());

        return true;
    }
};

kraus's avatar
kraus committed
625 626
struct Union: public Function {
    std::vector<Function*> funcs_m;
627

kraus's avatar
kraus committed
628 629
    virtual ~Union () {
        for (Function* func: funcs_m) {
kraus's avatar
kraus committed
630 631 632 633
            delete func;
        }
    }

634 635 636 637 638 639
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::string indent3(indentwidth + 16, ' ');
        std::cout << indent << "union, " << std::endl;
        std::cout << indent2 << "funcs: {\n";
kraus's avatar
kraus committed
640 641
        funcs_m.front()->print(indentwidth + 16);
        for (unsigned int i = 1; i < funcs_m.size(); ++ i) {
kraus's avatar
kraus committed
642 643
            std::cout << "\n"
                      << indent3 << "," << std::endl;
kraus's avatar
kraus committed
644
            funcs_m[i]->print(indentwidth + 16);
645 646
        }
        std::cout << "\n"
kraus's avatar
kraus committed
647 648 649
                  << indent2 << "} ";
    }

kraus's avatar
kraus committed
650 651 652 653
    virtual void apply(std::vector<Base*> &bfuncs) {
        for (unsigned int i = 0; i < funcs_m.size(); ++ i) {
            std::vector<Base*> children;
            Function *func = funcs_m[i];
654
            func->apply(children);
kraus's avatar
kraus committed
655
            bfuncs.insert(bfuncs.end(), children.begin(), children.end());
kraus's avatar
kraus committed
656
        }
657 658 659
    }

    static
kraus's avatar
kraus committed
660 661 662 663
    bool parse_detail(iterator &it, const iterator &end, Function* &fun) {
        Union *unin = static_cast<Union*>(fun);
        unin->funcs_m.push_back(NULL);
        if (!parse(it, end, unin->funcs_m.back())) return false;
664 665 666 667 668 669 670 671

        boost::regex argumentList("(,[a-z]+\\(.*)");
        boost::regex endParenthesis("\\)(.*)");
        boost::smatch what;

        std::string str(it, end);
        while (boost::regex_match(str, what, argumentList)) {
            iterator it2 = it + 1;
kraus's avatar
kraus committed
672
            unin->funcs_m.push_back(NULL);
673

kraus's avatar
kraus committed
674
            if (!parse(it2, end, unin->funcs_m.back())) return false;
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

            it = it2;
            str = std::string(it, end);
        }

        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;
    }
};

kraus's avatar
kraus committed
692 693

bool parse(std::string str, Function* &fun) {
694 695 696 697 698 699 700 701 702 703 704
    str = boost::regex_replace(str, boost::regex("\\s"), std::string(""), boost::match_default | boost::format_all);
    iterator it = str.begin();
    iterator end = str.end();
    if (!parse(it, end, fun)) {
        std::cout << "parsing failed here:" << std::string(it, end) << std::endl;
        return false;
    }

    return true;
}

kraus's avatar
kraus committed
705
bool parse(iterator &it, const iterator &end, Function* &fun) {
706 707 708 709 710 711 712 713 714 715 716
    boost::regex functionCall(FCall);
    boost::smatch what;

    std::string str(it, end);
    if( !boost::regex_match(str , what, functionCall ) ) return false;

    std::string identifier = what[1];
    std::string arguments = what[2];
    unsigned int shift = identifier.size() + 1;

    if (identifier == "rectangle") {
kraus's avatar
kraus committed
717
        fun = new Rectangle;
kraus's avatar
kraus committed
718 719
        /*iterator it2 = */it += shift;
        if (!Rectangle::parse_detail(it, end, fun)) return false;
720

kraus's avatar
kraus committed
721
        // it = it2;
722 723
        return true;
    } else if (identifier == "ellipse") {
kraus's avatar
kraus committed
724
        fun = new Ellipse;
kraus's avatar
kraus committed
725 726
        /*iterator it2 = */it += shift;
        if (!Ellipse::parse_detail(it, end, fun)) return false;
727

kraus's avatar
kraus committed
728
        // it = it2;
729 730
        return true;
    } else if (identifier == "repeat") {
kraus's avatar
kraus committed
731
        fun = new Repeat;
kraus's avatar
kraus committed
732 733
        /*iterator it2 = */it += shift;
        if (!Repeat::parse_detail(it, end, fun)) return false;
734

kraus's avatar
kraus committed
735
        // it = it2;
736 737 738

        return true;
    } else if (identifier == "rotate") {
kraus's avatar
kraus committed
739
        fun = new Rotate;
kraus's avatar
kraus committed
740 741
        // iterator it2 =
            it += shift;
kraus's avatar
kraus committed
742
        if (!Rotate::parse_detail(it, end, fun)) return false;
743

kraus's avatar
kraus committed
744
        // // it = it2;
745 746 747

        return true;
    } else if (identifier == "translate") {
kraus's avatar
kraus committed
748
        fun = new Translate;
kraus's avatar
kraus committed
749 750
        /*iterator it2 = */it += shift;
        if (!Translate::parse_detail(it, end, fun)) return false;
751

kraus's avatar
kraus committed
752 753 754 755 756 757 758 759 760
        // it = it2;

        return true;
    } else if (identifier == "shear") {
        fun = new Shear;
        /*iterator it2 = */it += shift;
        if (!Shear::parse_detail(it, end, fun)) return false;

        // it = it2;
761 762 763

        return true;
    } else if (identifier == "union") {
kraus's avatar
kraus committed
764
        fun = new Union;
kraus's avatar
kraus committed
765 766
        /*iterator it2 = */it += shift;
        if (!Union::parse_detail(it, end, fun)) return false;
767

kraus's avatar
kraus committed
768
        // it = it2;
769 770 771 772 773 774 775 776 777

        return true;
    }


    return (it == end);

}

778
int main(int argc, char *argv[])
779
{
780 781 782 783
    if (argc < 2) {
        std::cout << "please provide the name of the file that contains your code" << std::endl;
        return 1;
    }
kraus's avatar
kraus committed
784
    Function *fun;
785 786 787 788 789 790

    std::ifstream t(argv[1]);
    std::string str((std::istreambuf_iterator<char>(t)),
                    std::istreambuf_iterator<char>());

    // std::string str("repeat( translate(union(rectangle(0.1, 0.1), ellipse(0.1, 0.1)), -0.01, -0.02), 2, 0.1, 0.2)");
791 792 793

    if (parse(str, fun)) {
        fun->print(0);
kraus's avatar
kraus committed
794 795
        std::cout << "\n" << std::endl;

kraus's avatar
kraus committed
796
        std::vector<Base*> baseBlocks;
kraus's avatar
kraus committed
797 798
        fun->apply(baseBlocks);

799
        std::ofstream out("test.gpl");
kraus's avatar
kraus committed
800
        for (Base* bfun: baseBlocks) {
kraus's avatar
kraus committed
801
            bfun->print(0);
802
            bfun->computeBoundingBox();
kraus's avatar
kraus committed
803
            std::cout << std::endl;
804
            bfun->writeGnuplot(out);
kraus's avatar
kraus committed
805 806
        }

kraus's avatar
kraus committed
807
        for (Base* func: baseBlocks) {
kraus's avatar
kraus committed
808 809
            delete func;
        }
810
    }
kraus's avatar
kraus committed
811 812 813

    delete fun;

814 815
    return 0;
}