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

4 5 6 7 8 9 10 11 12 13 14
#include <boost/regex.hpp>

#include <iostream>
#include <string>
#include <cstdlib>

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
15 16
struct base;

17
struct function {
kraus's avatar
kraus committed
18 19
    virtual ~function() {};

20
    virtual void print(int indent) = 0;
kraus's avatar
kraus committed
21
    virtual void apply(std::vector<base*> &bfuncs) = 0;
22 23 24 25 26 27
};

typedef std::string::iterator iterator;

bool parse(iterator &it, const iterator &end, function* &fun);

kraus's avatar
kraus committed
28 29 30 31 32 33 34 35 36 37 38
struct base: public function {
    CoordinateSystemTrafo trafo;
    base():
        trafo(Vector_t(0.0),
              Quaternion(0.0, 0.0, 0.0, 1.0))
    { }

    virtual base* clone() = 0;
};

struct rectangle: public base {
39 40 41
    double width;
    double height;

kraus's avatar
kraus committed
42 43 44 45 46 47 48 49
    rectangle():
        base(),
        width(0.0),
        height(0.0)
    { }

    virtual ~rectangle() { }

50 51 52
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
kraus's avatar
kraus committed
53 54
        Vector_t origin = trafo.getOrigin();
        double angle = trafo.getRotation()[0] * 2 * Physics::rad2deg;
55 56
        std::cout << indent << "rectangle, \n"
                  << indent2 << "w: " << width << ", \n"
kraus's avatar
kraus committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
                  << indent2 << "h: " << height << ", \n"
                  << indent2 << "origin: " << origin[0] << ", " << origin[1] << ",\n"
                  << indent2 << "angle: " << angle;
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        bfuncs.push_back(this->clone());
    }

    virtual base* clone() {
        rectangle *rect = new rectangle;
        rect->width = width;
        rect->height = height;
        rect->trafo = trafo;

        return rect;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* fun) {
        std::string str(it, end);
        boost::regex argumentList(UDouble + "," + UDouble + "(\\).*)");
        boost::smatch what;

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

        rectangle *rect = static_cast<rectangle*>(fun);
        rect->width = atof(std::string(what[1]).c_str());
        rect->height = atof(std::string(what[3]).c_str());

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

        return true;

    }
};

kraus's avatar
kraus committed
96
struct ellipse: public base {
97 98 99
    double width;
    double height;

kraus's avatar
kraus committed
100 101 102 103 104 105 106
    ellipse():
        width(0.0),
        height(0.0)
    { }

    virtual ~ellipse() { }

107 108 109
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
kraus's avatar
kraus committed
110 111
        Vector_t origin = trafo.getOrigin();
        double angle = trafo.getRotation()[0] * 2 * Physics::rad2deg;
112 113
        std::cout << indent << "ellipse, \n"
                  << indent2 << "w: " << width << ", \n"
kraus's avatar
kraus committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
                  << indent2 << "h: " << height << ", \n"
                  << indent2 << "origin: " << origin[0] << ", " << origin[1] << ",\n"
                  << indent2 << "angle: " << angle;
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        bfuncs.push_back(this->clone());
    }

    virtual base* clone() {
        ellipse *elps = new ellipse;
        elps->width = width;
        elps->height = height;
        elps->trafo = trafo;

        return elps;
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* fun) {
        std::string str(it, end);
        boost::regex argumentList(UDouble + "," + UDouble + "(\\).*)");
        boost::smatch what;

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

        ellipse *elps = static_cast<ellipse*>(fun);
        elps->width = atof(std::string(what[1]).c_str());
        elps->height = atof(std::string(what[3]).c_str());

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

        return true;

    }
};

struct repeat: public function {
    function* func;
    unsigned int N;
    double shiftx;
    double shifty;

kraus's avatar
kraus committed
159 160 161 162
    virtual ~repeat() {
        delete func;
    }

163 164 165 166 167 168 169 170 171
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "repeat, " << std::endl;
        func->print(indentwidth + 8);
        std::cout << ",\n"
                  << indent2 << "N: " << N << ", \n"
                  << indent2 << "dx: " << shiftx << ", \n"
                  << indent2 << "dy: " << shifty;
kraus's avatar
kraus committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        CoordinateSystemTrafo shift(Vector_t(shiftx, shifty, 0.0),
                                    Quaternion(0.0, 0.0, 0.0, 1.0));
        func->apply(bfuncs);
        const unsigned int size = bfuncs.size();

        CoordinateSystemTrafo current_shift = shift;
        for (unsigned int i = 0; i < N; ++ i) {
            for (unsigned int j = 0; j < size; ++ j) {
                base *obj = bfuncs[j]->clone();
                obj->trafo = current_shift * obj->trafo;
                bfuncs.push_back(obj);
            }

            current_shift *= shift;
        }
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* &fun) {
        repeat *rep = static_cast<repeat*>(fun);
        if (!parse(it, end, rep->func)) return false;

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

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

        rep->N = atof(std::string(what[1]).c_str());
        rep->shiftx = atof(std::string(what[2]).c_str());
        rep->shifty = atof(std::string(what[4]).c_str());

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

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

        return true;
    }
};

struct translate: public function {
    function* func;
    double shiftx;
    double shifty;

kraus's avatar
kraus committed
221 222 223 224
    virtual ~translate() {
        delete func;
    }

225 226 227 228 229 230 231 232
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "translate, " << std::endl;
        func->print(indentwidth + 8);
        std::cout << ",\n"
                  << indent2 << "dx: " << shiftx << ", \n"
                  << indent2 << "dy: " << shifty;
kraus's avatar
kraus committed
233 234 235 236 237 238 239 240 241 242 243 244
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        CoordinateSystemTrafo shift(Vector_t(shiftx, shifty, 0.0),
                                    Quaternion(0.0, 0.0, 0.0, 1.0));
        func->apply(bfuncs);
        const unsigned int size = bfuncs.size();

        for (unsigned int j = 0; j < size; ++ j) {
            base *obj = bfuncs[j];
            obj->trafo = shift * obj->trafo;
        }
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* &fun) {
        translate *trans = static_cast<translate*>(fun);
        if (!parse(it, end, trans->func)) return false;

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

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

        trans->shiftx = atof(std::string(what[1]).c_str());
        trans->shifty = atof(std::string(what[3]).c_str());

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

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

        return true;
    }
};


struct rotate: public function {
    function* func;
    double angle;

kraus's avatar
kraus committed
275 276 277 278
    virtual ~rotate() {
        delete func;
    }

279 280 281 282 283 284 285
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "rotate, " << std::endl;
        func->print(indentwidth + 8);
        std::cout << ",\n"
                  << indent2 << "angle: " << angle;
kraus's avatar
kraus committed
286 287 288 289 290 291 292 293 294 295 296 297
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        CoordinateSystemTrafo rotation(Vector_t(0.0, 0.0, 0.0),
                                       Quaternion(0.5 * angle, 0.0, 0.0, 1.0));
        func->apply(bfuncs);
        const unsigned int size = bfuncs.size();

        for (unsigned int j = 0; j < size; ++ j) {
            base *obj = bfuncs[j];
            obj->trafo = rotation * obj->trafo;
        }
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* &fun) {
        rotate *rot = static_cast<rotate*>(fun);
        if (!parse(it, end, rot->func)) return false;

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

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

        rot->angle = atof(std::string(what[1]).c_str());

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

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

        return true;
    }
};

struct unionf: public function {
    std::vector<function*> funcs;

kraus's avatar
kraus committed
325 326 327 328 329 330
    virtual ~unionf () {
        for (function* func: funcs) {
            delete func;
        }
    }

331 332 333 334 335 336 337 338
    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";
        funcs.front()->print(indentwidth + 16);
        for (unsigned int i = 1; i < funcs.size(); ++ i) {
kraus's avatar
kraus committed
339 340
            std::cout << "\n"
                      << indent3 << "," << std::endl;
341 342 343
            funcs[i]->print(indentwidth + 16);
        }
        std::cout << "\n"
kraus's avatar
kraus committed
344 345 346 347 348 349 350 351
                  << indent2 << "} ";
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        for (unsigned int i = 0; i < funcs.size(); ++ i) {
            function *func = funcs[i];
            func->apply(bfuncs);
        }
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* &fun) {
        unionf *unin = static_cast<unionf*>(fun);
        unin->funcs.push_back(NULL);
        if (!parse(it, end, unin->funcs.back())) return false;

        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;
            unin->funcs.push_back(NULL);

            if (!parse(it2, end, unin->funcs.back())) return false;

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

bool parse(std::string str, function* &fun) {
    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;
}

bool parse(iterator &it, const iterator &end, function* &fun) {
    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") {
        fun = new rectangle;
        iterator it2 = it + shift;
        if (!rectangle::parse_detail(it2, end, fun)) return false;

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

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

        it = it2;

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

        it = it2;

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

        it = it2;

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

        it = it2;

        return true;
    }


    return (it == end);

}

int
main()
{
    function *fun;
    //std::string str("rectangle(0.1, 0.01)");
    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)");

    if (parse(str, fun)) {
        fun->print(0);
kraus's avatar
kraus committed
472 473 474 475 476 477 478 479 480 481 482 483 484
        std::cout << "\n" << std::endl;

        std::vector<base*> baseBlocks;
        fun->apply(baseBlocks);

        for (base* bfun: baseBlocks) {
            bfun->print(0);
            std::cout << std::endl;
        }

        for (base* func: baseBlocks) {
            delete func;
        }
485
    }
kraus's avatar
kraus committed
486 487 488

    delete fun;

489 490
    return 0;
}