Almond.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "Almond.h"
  2. #include <QIntValidator>
  3. #include <QFileDialog>
  4. #include <QMessageBox>
  5. #include <QGradient>
  6. #include "gradientchoosedialog.h"
  7. #include <cmath>
  8. mnd::MandelGenerator* generateTest()
  9. {
  10. mnd::Variable z{ "z" };
  11. mnd::Variable c{ "c" };
  12. mnd::Pow m{ std::make_unique<mnd::Expression>(z), std::make_unique<mnd::Expression>(mnd::Constant{ 2.01 }) };
  13. //mnd::Multiplication m2{ std::make_unique<mnd::Expression>(std::move(m)), std::make_unique<mnd::Expression>(z) };
  14. mnd::Addition a{ std::make_unique<mnd::Expression>(std::move(m)), std::make_unique<mnd::Expression>(c) };
  15. mnd::IterationFormula itf{ std::make_unique<mnd::Expression>(std::move(a)) };
  16. return new mnd::NaiveGenerator(std::move(itf), 1.0e-10);
  17. }
  18. Almond::Almond(QWidget* parent) :
  19. QMainWindow{ parent },
  20. mandelContext{ mnd::initializeContext() }
  21. {
  22. ui.setupUi(this);
  23. mw = std::make_unique<MandelWidget>(mandelContext,
  24. &mandelContext.getDefaultGenerator(),
  25. ui.centralWidget);
  26. on_maxIterations_editingFinished();
  27. mw->setSmoothColoring(ui.smooth->isChecked());
  28. currentView = MANDELBROT;
  29. mandelGeneratorSave = &mandelContext.getDefaultGenerator();
  30. mandelViewSave = mw->getViewport();
  31. mw->setGenerator(generateTest());
  32. QObject::connect(mw.get(), &MandelWidget::pointSelected, this, &Almond::pointSelected);
  33. ui.mainContainer->addWidget(mw.get());
  34. ui.maxIterations->setValidator(new QIntValidator(1, 1000000000, this));
  35. ui.backgroundProgress->setVisible(false);
  36. //ui.verticalLayout_left->addWidget(new MyGLWidget(ui.centralWidget));
  37. //mw->show();
  38. }
  39. Almond::~Almond(void)
  40. {
  41. }
  42. void Almond::submitBackgroundTask(BackgroundTask* task)
  43. {
  44. bool taken = QThreadPool::globalInstance()->tryTake(task->getRunnable());
  45. if (taken) {
  46. ui.backgroundProgress->setRange(0, 0);
  47. ui.backgroundProgress->setVisible(true);
  48. }
  49. }
  50. void Almond::backgroundTaskFinished(void)
  51. {
  52. }
  53. void Almond::on_zoom_out_clicked()
  54. {
  55. mw->zoom(2);
  56. }
  57. void Almond::on_zoom_in_clicked()
  58. {
  59. mw->zoom(0.5);
  60. }
  61. void Almond::on_maxIterations_editingFinished()
  62. {
  63. QString text = ui.maxIterations->text();
  64. int maxIter = text.toInt();
  65. mw->setMaxIterations(maxIter);
  66. }
  67. void Almond::on_chooseGradient_clicked()
  68. {
  69. auto response = gcd.exec();
  70. auto gradient = gcd.getGradient();
  71. if (gradient)
  72. mw->setGradient(std::move(*gradient));
  73. }
  74. void Almond::on_exportVideo_clicked()
  75. {
  76. ExportVideoInfo evi;
  77. evi.start = mnd::MandelViewport::standardView();
  78. evi.end = mw->getViewport();
  79. evi.gradient = mw->getGradient();
  80. ExportVideoDialog dialog(this, evi);
  81. //dialog.show();
  82. auto response = dialog.exec();
  83. printf("dialog executed\n"); fflush(stdout);
  84. if (response == 1) {
  85. mnd::MandelInfo mi;
  86. evi = dialog.getExportVideoInfo();
  87. //Video
  88. /*mi.maxIter = dialog.getMaxIterations();
  89. mi.view = mw->getViewport();
  90. mi.bWidth = dialog.getWidth();
  91. mi.bHeight = dialog.getHeight();
  92. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  93. mnd::Generator& g = mandelContext.getDefaultGenerator();
  94. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  95. g.generate(mi, fmap.pixels.get());
  96. auto bitmap = fmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  97. QImage img((unsigned char*)bitmap.pixels.get(), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  98. img.save(dialog.getPath());*/
  99. }
  100. }
  101. void Almond::on_smooth_stateChanged(int checked)
  102. {
  103. this->mw->setSmoothColoring(checked != Qt::Unchecked);
  104. }
  105. void Almond::on_exportImage_clicked()
  106. {
  107. ExportImageDialog dialog(this);
  108. dialog.setMaxIterations(mw->getMaxIterations());
  109. //dialog.show();
  110. auto response = dialog.exec();
  111. if (response == 1) {
  112. mnd::MandelInfo mi;
  113. mi.maxIter = dialog.getMaxIterations();
  114. mi.view = mw->getViewport();
  115. mi.bWidth = dialog.getWidth();
  116. mi.bHeight = dialog.getHeight();
  117. mi.view.adjustAspectRatio(mi.bWidth, mi.bHeight);
  118. mi.smooth = mw->getSmoothColoring();
  119. if (currentView == JULIA) {
  120. mi.julia = mw->getMandelInfo().julia;
  121. mi.juliaX = mw->getJuliaX();
  122. mi.juliaY = mw->getJuliaY();
  123. }
  124. mnd::MandelGenerator* currentGenerator = mw->getGenerator();
  125. mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
  126. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  127. g.generate(mi, fmap.pixels.get());
  128. auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
  129. return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
  130. });
  131. QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
  132. img.save(dialog.getPath());
  133. }
  134. }
  135. void Almond::on_resetZoom_clicked()
  136. {
  137. mw->setViewport(mnd::MandelViewport::standardView());
  138. }
  139. void Almond::on_displayInfo_stateChanged(int checked)
  140. {
  141. this->mw->setDisplayInfo(checked != Qt::Unchecked);
  142. }
  143. void Almond::on_chooseGenerator_clicked()
  144. {
  145. if (!generatorsDialog)
  146. generatorsDialog = std::make_unique<ChooseGenerators>(mandelContext, this);
  147. generatorsDialog->exec();
  148. if (generatorsDialog->getChosenGenerator()) {
  149. mandelGeneratorSave = generatorsDialog->getChosenGenerator();
  150. }
  151. else {
  152. mandelGeneratorSave = &mandelContext.getDefaultGenerator();
  153. }
  154. //this->currentView = MANDELBROT;
  155. this->mw->setGenerator(mandelGeneratorSave);
  156. //this->mw->getMandelInfo().julia = false;
  157. //printf("dialog executed\n"); fflush(stdout);
  158. }
  159. void Almond::on_selectPoint_clicked()
  160. {
  161. if (currentView == MANDELBROT) {
  162. emit this->mw->selectPoint();
  163. }
  164. }
  165. void Almond::pointSelected(mnd::Real x, mnd::Real y)
  166. {
  167. if (currentView != JULIA) {
  168. //auto& gen = mandelContext.getJuliaGenerator();
  169. mandelViewSave = mw->getViewport();
  170. //this->mw->setGenerator(&gen);
  171. this->mw->setViewport(mnd::MandelViewport::centerView());
  172. this->mw->setJuliaPos(x, y);
  173. this->mw->getMandelInfo().julia = true;
  174. this->mw->clearAll();
  175. }
  176. currentView = JULIA;
  177. }
  178. void Almond::on_viewMandelbrot_clicked()
  179. {
  180. if (currentView != MANDELBROT) {
  181. //this->mw->setGenerator(mandelGeneratorSave);
  182. this->mw->setViewport(mandelViewSave);
  183. this->mw->getMandelInfo().julia = false;
  184. this->mw->clearAll();
  185. currentView = MANDELBROT;
  186. }
  187. }