#include "Behaviors/StateMachine.h"

$nodeclass EllipsesPolygons : VisualRoutinesStateNode {

  $nodeclass Look : MapBuilderNode : constructor {
    mapreq.addObjectColor(ellipseDataType, "red");
    mapreq.addObjectColor(ellipseDataType, "green");
    mapreq.addObjectColor(ellipseDataType, "blue");
  }

  $nodeclass Report : VisualRoutinesStateNode : doStart {
    // First find all the ellipses.
    NEW_SHAPEVEC(allEllipses, EllipseData, select_type<EllipseData>(camShS));

    // We choose our polygon to be made of red ellipses.
    NEW_SHAPEVEC(redEllipses, EllipseData, subset(allEllipses, IsColor("red")));

    std::vector<Point> boundaryPoints;
    SHAPEVEC_ITERATE(redEllipses, EllipseData, e) {
        boundaryPoints.push_back(e->getCentroid());
    } END_ITERATE;

    // Make the polygon.
    NEW_SHAPE(myPolygon, PolygonData, new PolygonData(camShS, boundaryPoints, true));

    // Get some non-red ellipse.
    NEW_SHAPE(otherEllipse, EllipseData, find_if<EllipseData>(allEllipses, not1(IsColor("red"))));

    if (otherEllipse) {
        if (myPolygon->isInside(otherEllipse->getCentroid()))
            cout << "Ellipse is inside the polygon." << endl;
        else
            cout << "Ellipse is outside the polygon." << endl;
    } else
        cout << "No other ellipse found." << endl;
  }

  $setupmachine{
    look: Look =MAP=> Report
  }

}

REGISTER_BEHAVIOR(EllipsesPolygons);
