-
Notifications
You must be signed in to change notification settings - Fork 8
/
FormulaGen.cpp
172 lines (146 loc) · 5.65 KB
/
FormulaGen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
124
125
126
127
128
129
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "FormulaGen.h"
#include "ui_FormulaGen.h"
#include "exprtk/exprtk.hpp"
#include "stereomaker.h"
#include "mainwindow.h"
#include <QTableWidgetItem>
#include <QSettings>
#include <QListWidget>
typedef exprtk::symbol_table<float> symbol_table_t;
typedef exprtk::expression<float> expression_t;
typedef exprtk::parser<float> parser_t;
FormulaGen::FormulaGen(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FormulaGen)
{
ui->setupUi(this);
setBasicImageParent(this);
setSaveTitle("Formula Depth Image");
setFolderSettings("depthmaps");
QSettings settings;
//settings.remove("formula");
formulaList=settings.value("formula",QStringList()<<"0.5+0.5*sin( 100*(((x-0.5)*w/h)^2+(y-0.5)^2)^0.5)"<<
"var f :=sin(x*2*pi)*0.3;\n"
"var multip := 3.333;\n"
"(f>abs((y-0.5)))*(0.5+0.5*multip*sqrt(f*f-(y-0.5)*(y-0.5)))+(-f>abs((y-0.5)))*(0.5-0.5*multip*sqrt(f*f-(y-0.5)*(y-0.5)))"<<
"var x0 := (x*w/h-0.8)*4;\n"
"var y0 := (y-0.5)*4;\n"
"var tx := x0;\n"
"var ty := y0;\n"
"var temp := 0;\n"
"for(var i :=0; i <=8;i+=1)\n"
"{\n"
"temp := tx*tx-ty*ty+x0;\n"
"ty := 2*tx*ty+y0;\n"
"tx := temp;\n"
"}\n"
"(tx*tx+ty*ty)*0.1").toStringList();
ui->listWidget->addItems(formulaList);
ui->formulaPText->setPlainText(formulaList.last());
}
FormulaGen::~FormulaGen()
{
delete ui;
}
int FormulaGen::getOutputImageWidth()
{
return ui->widthSpin->value();
}
int FormulaGen::getOutputImageHeight()
{
return ui->heightSpin->value();
}
void FormulaGen::on_generatePb_clicked()
{
ui->errorTable->clearContents();
ui->errorTable->setRowCount(0);
int w=getOutputImageWidth();
int h=getOutputImageHeight();
QString formule_str=ui->formulaPText->toPlainText();
QByteArray formula_ba=formule_str.toLatin1();
const char* formula_buf=formula_ba.constData();
imdata=QImage(w,h,QImage::Format_RGB32);
float x_s, y_s;
float f_w=w, f_h=h;
symbol_table_t symbol_table;
symbol_table.add_pi();
symbol_table.add_constant("e", M_E);
symbol_table.add_variable("x", x_s);
symbol_table.add_variable("y", y_s);
symbol_table.add_variable("w", f_w);
symbol_table.add_variable("h", f_h);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t::settings_store sstore;
sstore.disable_all_logic_ops();
//sstore.disable_all_assignment_ops();
sstore.disable_all_control_structures();
sstore.enable_control_structure(parser_t::settings_store::e_ctrl_for_loop);
parser_t parser(sstore);
bool compile_valid=parser.compile(formula_buf, expression);
if (compile_valid)
{
int val;
for (int x=0;x<w;++x)
{
for (int y=0;y<h;++y)
{
x_s = (float)x/w;
y_s = (float)y/h;
val=expression.value()*255.99;
if (std::isnan(val) || std::isinf(val) || val<0)
val=0;
else if (val>255)
val=255;
imdata.setPixel(x,y,0xff000000|(val<<16)|(val<<8)|val);
}
}
//imdata=imdata.convertToFormat(QImage::Format_Indexed8,StereoMaker::getGrayScale());
ui->imageViewArea->setPixmap(QPixmap::fromImage(imdata));
if (!formulaList.contains(formule_str))
{
formulaList<<formule_str;
QSettings settings;
settings.setValue("formula",formulaList);
ui->listWidget->addItem(formule_str);
}
}
else {
ui->errorTable->setRowCount(parser.error_count());
ui->errorTable->setColumnCount(3);
QTextCursor cursor = ui->formulaPText->textCursor();
for (std::size_t i = 0; i < parser.error_count(); ++i)
{
typedef exprtk::parser_error::type error_t;
error_t error = parser.get_error(i);
/*printf("Error[%02d] Position: %02d Type: [%14s] Msg: %s\n",
(int)i,
(int)error.token.position,
exprtk::parser_error::to_str(error.mode).c_str(),
error.diagnostic.c_str());*/
cursor.setPosition(error.token.position);
ui->errorTable->setItem(i,0,new QTableWidgetItem(QString::number(error.token.position)+QString(" [%1,%2]").arg(cursor.blockNumber()+1).arg(cursor.positionInBlock())));
ui->errorTable->setItem(i,1,new QTableWidgetItem(tr(exprtk::parser_error::to_str(error.mode).c_str())));
ui->errorTable->setItem(i,2,new QTableWidgetItem(tr(error.diagnostic.c_str())));
}
cursor.setPosition(parser.get_error(0).token.position);
cursor.movePosition(QTextCursor::Right,QTextCursor::KeepAnchor);
ui->formulaPText->setTextCursor(cursor);
}
}
void FormulaGen::on_actionSave_Image_triggered()
{
saveAsImage();
}
void FormulaGen::on_actionPush_Image_triggered()
{
((MainWindow*)parentWidget())->setDepthImage(imdata);
}
void FormulaGen::on_actionPush_Image_as_Compose_Pattern_triggered()
{
((MainWindow*)parentWidget())->setComposePattern(imdata);
}
void FormulaGen::on_listWidget_itemActivated(QListWidgetItem *item)
{
ui->formulaPText->setPlainText(item->text());
}