12 #include "chaiscript/chaiscript.hpp"
14 #include "geometrize/bitmap/bitmap.h"
15 #include "geometrize/commonutil.h"
16 #include "geometrize/core.h"
17 #include "geometrize/model.h"
18 #include "geometrize/shape/circle.h"
19 #include "geometrize/shape/ellipse.h"
20 #include "geometrize/shape/line.h"
21 #include "geometrize/shape/polyline.h"
22 #include "geometrize/shape/quadraticbezier.h"
23 #include "geometrize/rasterizer/rasterizer.h"
24 #include "geometrize/rasterizer/scanline.h"
25 #include "geometrize/shape/rectangle.h"
26 #include "geometrize/shape/rotatedellipse.h"
27 #include "geometrize/shape/rotatedrectangle.h"
28 #include "geometrize/shape/shape.h"
29 #include "geometrize/shape/shapefactory.h"
30 #include "geometrize/shape/shapetypes.h"
31 #include "geometrize/shape/triangle.h"
57 class GeometrizerEngine :
public QObject,
public std::enable_shared_from_this<GeometrizerEngine>
68 void installScripts(
const std::map<std::string, std::string>& scripts);
76 auto self(shared_from_this());
77 geometrize::core::EnergyFunction f = [
self,
this](
const std::vector<geometrize::Scanline>& lines,
78 const std::uint32_t alpha,
79 const geometrize::Bitmap& target,
80 const geometrize::Bitmap& current,
81 geometrize::Bitmap& buffer,
95 std::function<std::shared_ptr<geometrize::Shape>()>
makeShapeCreator(geometrize::ShapeTypes types, std::int32_t xMin, std::int32_t yMin, std::int32_t xMax, std::int32_t yMax)
const
97 auto self(shared_from_this());
98 auto f = [
self,
this, types, xMin, yMin, xMax, yMax]() {
99 std::shared_ptr<geometrize::Shape> s = geometrize::randomShapeOf(types);
101 switch(s->getType()) {
102 case geometrize::ShapeTypes::RECTANGLE: {
103 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::Rectangle&
>(s)); };
104 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::Rectangle&
>(s)); };
105 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::Rectangle&
>(s), xMin, yMin, xMax, yMax); };
108 case geometrize::ShapeTypes::ROTATED_RECTANGLE: {
109 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::RotatedRectangle&
>(s)); };
110 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::RotatedRectangle&
>(s)); };
111 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::RotatedRectangle&
>(s), xMin, yMin, xMax, yMax); };
114 case geometrize::ShapeTypes::TRIANGLE: {
115 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::Triangle&
>(s)); };
116 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::Triangle&
>(s)); };
117 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::Triangle&
>(s), xMin, yMin, xMax, yMax); };
120 case geometrize::ShapeTypes::ELLIPSE: {
121 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::Ellipse&
>(s)); };
122 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::Ellipse&
>(s)); };
123 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::Ellipse&
>(s), xMin, yMin, xMax, yMax); };
126 case geometrize::ShapeTypes::ROTATED_ELLIPSE: {
127 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::RotatedEllipse&
>(s)); };
128 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::RotatedEllipse&
>(s)); };
129 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::RotatedEllipse&
>(s), xMin, yMin, xMax, yMax); };
132 case geometrize::ShapeTypes::CIRCLE: {
133 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::Circle&
>(s)); };
134 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::Circle&
>(s)); };
135 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::Circle&
>(s), xMin, yMin, xMax, yMax); };
138 case geometrize::ShapeTypes::LINE: {
139 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::Line&
>(s)); };
140 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::Line&
>(s)); };
141 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::Line&
>(s), xMin, yMin, xMax, yMax); };
144 case geometrize::ShapeTypes::QUADRATIC_BEZIER: {
145 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::QuadraticBezier&
>(s)); };
146 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::QuadraticBezier&
>(s)); };
147 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::QuadraticBezier&
>(s), xMin, yMin, xMax, yMax); };
150 case geometrize::ShapeTypes::POLYLINE: {
151 s->setup = [
this](geometrize::Shape& s) {
return setup(
static_cast<geometrize::Polyline&
>(s)); };
152 s->mutate = [
this](geometrize::Shape& s) {
mutate(
static_cast<geometrize::Polyline&
>(s)); };
153 s->rasterize = [xMin, yMin, xMax, yMax](
const geometrize::Shape& s) {
return geometrize::rasterize(
static_cast<const geometrize::Polyline&
>(s), xMin, yMin, xMax, yMax); };
157 assert(0 &&
"Bad shape type");
190 void installShapeScript(
const std::string& functionName,
const std::map<std::string, std::string>& scripts)
193 const auto it{scripts.find(functionName)};
194 assert(it != scripts.end());
202 const auto f{
m_engine->eval<std::function<void(T&)>>(functionName)};
204 const auto g = [f](T& s) {
207 }
catch(std::exception& e) {
208 std::cout << e.what() << std::endl;
212 const std::string setupPrefix{
"setup"};
213 const std::string mutatePrefix{
"mutate"};
214 if(functionName.find(setupPrefix) == 0) {
217 }
else if(functionName.find(mutatePrefix) == 0) {
221 }
catch(std::exception& e) {
222 std::cout << e.what() << std::endl;
225 assert(0 &&
"Checking for unrecognized required function, will ignore it");
238 const auto f{
m_engine->eval<geometrize::core::EnergyFunction>(functionName)};
240 const geometrize::core::EnergyFunction g = [f](
241 const std::vector<geometrize::Scanline>& lines,
242 const std::uint32_t alpha,
243 const geometrize::Bitmap& target,
244 const geometrize::Bitmap& current,
245 geometrize::Bitmap& buffer,
246 const double score) {
248 return f(lines, alpha, target, current, buffer, score);
249 }
catch(std::exception& e) {
250 std::cout << e.what() << std::endl;
257 }
catch(std::exception& e) {
258 std::cout << e.what() << std::endl;
261 assert(0 &&
"Checking for unrecognized required function, will ignore it");
264 void setup(geometrize::Circle& shape)
const
269 void mutate(geometrize::Circle& shape)
const
284 void setup(geometrize::Ellipse& shape)
const
289 void mutate(geometrize::Ellipse& shape)
const
304 void setup(geometrize::Line& shape)
const
309 void mutate(geometrize::Line& shape)
const
324 void setup(geometrize::Polyline& shape)
const
329 void mutate(geometrize::Polyline& shape)
const
344 void setup(geometrize::QuadraticBezier& shape)
const
349 void mutate(geometrize::QuadraticBezier& shape)
const
364 void setup(geometrize::Rectangle& shape)
const
369 void mutate(geometrize::Rectangle& shape)
const
384 void setup(geometrize::RotatedEllipse& shape)
const
389 void mutate(geometrize::RotatedEllipse& shape)
const
404 void setup(geometrize::RotatedRectangle& shape)
const
409 void mutate(geometrize::RotatedRectangle& shape)
const
424 void setup(geometrize::Triangle& shape)
const
429 void mutate(geometrize::Triangle& shape)
const