From fef2e3ae68545183a4849a2c258f1e01cd954704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 13 Oct 2016 14:45:51 +0200 Subject: [PATCH] [apps/graph] use GraphContext to evaluate function at abscissa Change-Id: Id9832203bb550a72296d1b9396b6244f9bd8bcce --- apps/apps_container.cpp | 4 +++- apps/apps_container.h | 1 + apps/graph/function.cpp | 8 +++----- apps/graph/function.h | 3 ++- apps/graph/function_store.h | 4 ++-- apps/graph/graph/graph_controller.cpp | 4 ++-- apps/graph/graph/graph_controller.h | 3 ++- apps/graph/graph/graph_view.cpp | 8 ++++---- apps/graph/graph/graph_view.h | 4 +++- apps/graph/graph_app.cpp | 7 ++++--- apps/graph/graph_app.h | 4 +++- apps/graph/values/values_controller.cpp | 5 +++-- apps/graph/values/values_controller.h | 4 +++- 13 files changed, 35 insertions(+), 24 deletions(-) diff --git a/apps/apps_container.cpp b/apps/apps_container.cpp index e56c16e78..2cc765d4a 100644 --- a/apps/apps_container.cpp +++ b/apps/apps_container.cpp @@ -5,7 +5,9 @@ extern "C" { AppsContainer::AppsContainer() : Container(), - m_homeApp(this) + m_homeApp(this), + m_graphApp(&m_context), + m_context(Context()) { } diff --git a/apps/apps_container.h b/apps/apps_container.h index 4cca3d81e..d03e90b4e 100644 --- a/apps/apps_container.h +++ b/apps/apps_container.h @@ -26,6 +26,7 @@ private: #if USE_PIC_VIEW_APP PicViewApp m_picViewApp; #endif + Context m_context; }; #endif diff --git a/apps/graph/function.cpp b/apps/graph/function.cpp index f96a0c8e7..09e6055e8 100644 --- a/apps/graph/function.cpp +++ b/apps/graph/function.cpp @@ -59,9 +59,7 @@ void Graph::Function::setActive(bool active) { m_active = active; } -float Graph::Function::evaluateAtAbscissa(float x) { - Context plotContext; - Float xExp = Float(x); - plotContext.setExpressionForSymbolName(&xExp, "x"); - return m_expression->approximate(plotContext); +float Graph::Function::evaluateAtAbscissa(float x, Graph::EvaluateContext * context) { + context->setOverridenValueForSymbolX(x); + return m_expression->approximate(*context); } diff --git a/apps/graph/function.h b/apps/graph/function.h index 154fefeba..fc8ba57fc 100644 --- a/apps/graph/function.h +++ b/apps/graph/function.h @@ -3,6 +3,7 @@ #include #include +#include "evaluate_context.h" namespace Graph { @@ -21,7 +22,7 @@ public: void setActive(bool active); void setContent(const char * c); void setColor(KDColor m_color); - float evaluateAtAbscissa(float x); + float evaluateAtAbscissa(float x, EvaluateContext * context); private: constexpr static int k_bodyLength = 255; char m_text[k_bodyLength]; diff --git a/apps/graph/function_store.h b/apps/graph/function_store.h index db6418bc0..ce7380b5c 100644 --- a/apps/graph/function_store.h +++ b/apps/graph/function_store.h @@ -4,8 +4,8 @@ #include "function.h" namespace Graph { - /* FunctionStore is a dumb class. - * Its only job is to store functions and to give them a color. */ +/* FunctionStore is a dumb class. + * Its only job is to store functions and to give them a color. */ class FunctionStore { public: FunctionStore(); diff --git a/apps/graph/graph/graph_controller.cpp b/apps/graph/graph/graph_controller.cpp index 706bbb0eb..f2aeec265 100644 --- a/apps/graph/graph/graph_controller.cpp +++ b/apps/graph/graph/graph_controller.cpp @@ -1,9 +1,9 @@ #include "graph_controller.h" #include -GraphController::GraphController(Responder * parentResponder, Graph::FunctionStore * functionStore) : +GraphController::GraphController(Responder * parentResponder, Graph::FunctionStore * functionStore, Graph::EvaluateContext * evaluateContext) : HeaderViewController(parentResponder, &m_view), - m_view(GraphView(functionStore)), + m_view(GraphView(functionStore, evaluateContext)), m_headerSelected(false), m_windowButton(Button(this, "Fenetre", Invocation([](void * context, void * sender) {}, this))), m_displayButton(this, "Affichage", Invocation([](void * context, void * sender) {}, this)) diff --git a/apps/graph/graph/graph_controller.h b/apps/graph/graph/graph_controller.h index b1a5978c9..026fedb84 100644 --- a/apps/graph/graph/graph_controller.h +++ b/apps/graph/graph/graph_controller.h @@ -4,10 +4,11 @@ #include #include "graph_view.h" #include "../function_store.h" +#include "../evaluate_context.h" class GraphController : public HeaderViewController { public: - GraphController(Responder * parentResponder, Graph::FunctionStore * functionStore); + GraphController(Responder * parentResponder, Graph::FunctionStore * functionStore, Graph::EvaluateContext * evaluateContext); const char * title() const override; bool handleEvent(Ion::Events::Event event) override; diff --git a/apps/graph/graph/graph_view.cpp b/apps/graph/graph/graph_view.cpp index 52d602013..661ceb592 100644 --- a/apps/graph/graph/graph_view.cpp +++ b/apps/graph/graph/graph_view.cpp @@ -7,7 +7,7 @@ constexpr KDColor kSecondaryGridColor = KDColor(0xEEEEEE); constexpr int kNumberOfMainGridLines = 5; constexpr int kNumberOfSecondaryGridLines = 4; -GraphView::GraphView(Graph::FunctionStore * functionStore) : +GraphView::GraphView(Graph::FunctionStore * functionStore, Graph::EvaluateContext * evaluateContext) : #if GRAPH_VIEW_IS_TILED TiledView(), #else @@ -19,7 +19,8 @@ GraphView::GraphView(Graph::FunctionStore * functionStore) : m_xMax(2.0f), m_yMin(-2.0f), m_yMax(2.0f), - m_functionStore(functionStore) + m_functionStore(functionStore), + m_evaluateContext(evaluateContext) { } @@ -158,13 +159,12 @@ void GraphView::drawFunction(KDContext * ctx, KDRect rect) const { KDColor workingBuffer[stampSize*stampSize]; - Context plotContext; for (KDCoordinate px = rect.x()-stampSize; pxnumberOfFunctions(); i++) { Graph::Function * f = m_functionStore->functionAtIndex(i); if (f->isActive()) { - float y = f->evaluateAtAbscissa(x); + float y = f->evaluateAtAbscissa(x, m_evaluateContext); KDCoordinate py = floatToPixel(Axis::Vertical, y); KDRect stampRect(px, py, stampSize, stampSize); ctx->fillRectWithMask(stampRect, f->color(), mask, workingBuffer); diff --git a/apps/graph/graph/graph_view.h b/apps/graph/graph/graph_view.h index 7d397ba89..459933925 100644 --- a/apps/graph/graph/graph_view.h +++ b/apps/graph/graph/graph_view.h @@ -4,6 +4,7 @@ #include #include "cursor_view.h" #include "../function_store.h" +#include "../evaluate_context.h" #define GRAPH_VIEW_IS_TILED 1 @@ -15,7 +16,7 @@ class GraphView : public #endif { public: - GraphView(Graph::FunctionStore * functionStore); + GraphView(Graph::FunctionStore * functionStore, Graph::EvaluateContext * evaluateContext); #if GRAPH_VIEW_IS_TILED KDColor * tile() const override; @@ -67,6 +68,7 @@ private: float m_yMax; Graph::FunctionStore * m_functionStore; + Graph::EvaluateContext * m_evaluateContext; }; #endif diff --git a/apps/graph/graph_app.cpp b/apps/graph/graph_app.cpp index bcc68a5da..eee96079d 100644 --- a/apps/graph/graph_app.cpp +++ b/apps/graph/graph_app.cpp @@ -1,13 +1,14 @@ #include "graph_app.h" #include "graph_icon.h" -GraphApp::GraphApp() : +GraphApp::GraphApp(Context * context) : App("Graph", ImageStore::GraphIcon), m_functionStore(Graph::FunctionStore()), + m_evaluateContext(Graph::EvaluateContext(context)), m_listController(ListController(nullptr, &m_functionStore)), m_listStackViewController(StackViewController(&m_tabViewController, &m_listController)), - m_graphController(GraphController(nullptr, &m_functionStore)), - m_valuesController(ValuesController(nullptr, &m_functionStore)), + m_graphController(GraphController(nullptr, &m_functionStore, &m_evaluateContext)), + m_valuesController(ValuesController(nullptr, &m_functionStore, &m_evaluateContext)), m_valuesStackViewController(StackViewController(&m_tabViewController, &m_valuesController)), m_tabViewController(&m_inputViewController, &m_listStackViewController, &m_graphController, &m_valuesStackViewController), m_inputViewController(this, &m_tabViewController) diff --git a/apps/graph/graph_app.h b/apps/graph/graph_app.h index 979f5a872..341d70a6b 100644 --- a/apps/graph/graph_app.h +++ b/apps/graph/graph_app.h @@ -3,18 +3,20 @@ #include #include "function_store.h" +#include "evaluate_context.h" #include "graph/graph_controller.h" #include "list/list_controller.h" #include "values/values_controller.h" class GraphApp : public ::App { public: - GraphApp(); + GraphApp(Context * context); InputViewController * inputViewController(); protected: ViewController * rootViewController() override; private: Graph::FunctionStore m_functionStore; + Graph::EvaluateContext m_evaluateContext; ListController m_listController; StackViewController m_listStackViewController; GraphController m_graphController; diff --git a/apps/graph/values/values_controller.cpp b/apps/graph/values/values_controller.cpp index b51100e08..21754a448 100644 --- a/apps/graph/values/values_controller.cpp +++ b/apps/graph/values/values_controller.cpp @@ -1,12 +1,13 @@ #include "values_controller.h" #include -ValuesController::ValuesController(Responder * parentResponder, Graph::FunctionStore * functionStore) : +ValuesController::ValuesController(Responder * parentResponder, Graph::FunctionStore * functionStore, Graph::EvaluateContext * evaluateContext) : HeaderViewController(parentResponder, &m_tableView), m_tableView(TableView(this, k_topMargin, k_rightMargin, k_bottomMargin, k_leftMargin)), m_activeCellX(0), m_activeCellY(-1), m_functionStore(functionStore), + m_evaluateContext(evaluateContext), m_interval(Graph::Interval(-1.0f, 1.0f, 0.25f)), m_parameterController(ValuesParameterController(this)), m_setIntervalButton(Button(this, "Regler l'intervalle",Invocation([](void * context, void * sender) { @@ -215,7 +216,7 @@ void ValuesController::willDisplayCellAtLocation(View * cell, int i, int j) { } else { Graph::Function * function = m_functionStore->activeFunctionAtIndex(i-1); float x = m_interval.element(j-1); - myCell->setFloat(function->evaluateAtAbscissa(x)); + myCell->setFloat(function->evaluateAtAbscissa(x, m_evaluateContext)); } myCell->setEven(j%2 == 0); } diff --git a/apps/graph/values/values_controller.h b/apps/graph/values/values_controller.h index ecba806d3..b9ef3b33d 100644 --- a/apps/graph/values/values_controller.h +++ b/apps/graph/values/values_controller.h @@ -3,6 +3,7 @@ #include #include "../function_store.h" +#include "../evaluate_context.h" #include "value_cell.h" #include "title_cell.h" #include "interval.h" @@ -10,7 +11,7 @@ class ValuesController : public HeaderViewController, public TableViewDataSource { public: - ValuesController(Responder * parentResponder, Graph::FunctionStore * functionStore); + ValuesController(Responder * parentResponder, Graph::FunctionStore * functionStore, Graph::EvaluateContext * evaluateContext); void setActiveCell(int i, int j); @@ -54,6 +55,7 @@ private: int m_activeCellX; int m_activeCellY; Graph::FunctionStore * m_functionStore; + Graph::EvaluateContext * m_evaluateContext; Graph::Interval m_interval; ValuesParameterController m_parameterController; Button m_setIntervalButton;