[apps/function] Factorize zoom in Function class

The new zoom implemented for ContinuousFunction is now factorized inside
Function to benefit the Sequence class. The same things is done to code
added to Graph::GraphController, which is moved into
FunctionGraphController.
This removes the reimplementation of several methods, most notably
computeYRange, as the implementation for function is general enough to
work on sequences.

Change-Id: I9b8211354064f46c3fa3dde3191dcb39d627a1d2
This commit is contained in:
Gabriel Ozouf
2020-09-02 12:09:21 +02:00
committed by Émilie Feral
parent 33f9bb50a3
commit a6db9688cd
14 changed files with 339 additions and 380 deletions

View File

@@ -2,6 +2,8 @@
#include <cmath>
#include <limits.h>
#include "../app.h"
#include <float.h>
#include <cmath>
#include <algorithm>
using namespace Shared;
@@ -43,8 +45,7 @@ float GraphController::interestingXMin() const {
return nmin;
}
float GraphController::interestingXHalfRange() const {
float standardRange = Shared::FunctionGraphController::interestingXHalfRange();
void GraphController::interestingRanges(InteractiveCurveViewRange * range) const {
int nmin = INT_MAX;
int nmax = 0;
int nbOfActiveModels = functionStore()->numberOfActiveFunctions();
@@ -52,10 +53,13 @@ float GraphController::interestingXHalfRange() const {
Sequence * s = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i));
int firstInterestingIndex = s->initialRank();
nmin = std::min(nmin, firstInterestingIndex);
nmax = std::max(nmax, firstInterestingIndex + static_cast<int>(standardRange));
nmax = std::max(nmax, firstInterestingIndex + static_cast<int>(k_defaultXHalfRange));
}
assert(nmax - nmin >= standardRange);
return nmax - nmin;
assert(nmax - nmin >= k_defaultXHalfRange);
range->setXMin(nmin);
range->setYAuto(true);
range->setXMax(nmax);
}
bool GraphController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) {
@@ -101,52 +105,4 @@ double GraphController::defaultCursorT(Ion::Storage::Record record) {
return std::fmax(0.0, std::round(Shared::FunctionGraphController::defaultCursorT(record)));
}
InteractiveCurveViewRangeDelegate::Range GraphController::computeYRange(InteractiveCurveViewRange * interactiveCurveViewRange) {
Poincare::Context * context = textFieldDelegateApp()->localContext();
float min = FLT_MAX;
float max = -FLT_MAX;
float xMin = interactiveCurveViewRange->xMin();
float xMax = interactiveCurveViewRange->xMax();
assert(functionStore()->numberOfActiveFunctions() > 0);
for (int i = 0; i < functionStore()->numberOfActiveFunctions(); i++) {
ExpiringPointer<Shared::Function> f = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i));
/* Scan x-range from the middle to the extrema in order to get balanced
* y-range for even functions (y = 1/x). */
double tMin = f->tMin();
if (std::isnan(tMin)) {
tMin = xMin;
} else if (f->shouldClipTRangeToXRange()) {
tMin = std::max<double>(tMin, xMin);
}
double tMax = f->tMax();
if (std::isnan(tMax)) {
tMax = xMax;
} else if (f->shouldClipTRangeToXRange()) {
tMax = std::min<double>(tMax, xMax);
}
/* In practice, a step smaller than a pixel's width is needed for sampling
* the values of a function. Otherwise some relevant extremal values may be
* missed. */
float rangeStep = f->rangeStep();
const float step = std::isnan(rangeStep) ? curveView()->pixelWidth() / 2.0f : rangeStep;
const int balancedBound = std::floor((tMax-tMin)/2/step);
for (int j = -balancedBound; j <= balancedBound ; j++) {
float t = (tMin+tMax)/2 + step * j;
Coordinate2D<float> xy = f->evaluateXYAtParameter(t, context);
float x = xy.x1();
if (!std::isnan(x) && !std::isinf(x) && x >= xMin && x <= xMax) {
float y = xy.x2();
if (!std::isnan(y) && !std::isinf(y)) {
min = std::min(min, y);
max = std::max(max, y);
}
}
}
}
InteractiveCurveViewRangeDelegate::Range range;
range.min = min;
range.max = max;
return range;
}
}