interactive_curve_view_range.cpp
8.27 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "interactive_curve_view_range.h"
#include <poincare.h>
#include <ion.h>
#include <cmath>
#include <stddef.h>
#include <assert.h>
using namespace Poincare;
namespace Shared {
InteractiveCurveViewRange::InteractiveCurveViewRange(CurveViewCursor * cursor, InteractiveCurveViewRangeDelegate * delegate) :
MemoizedCurveViewRange(),
m_yAuto(true),
m_delegate(delegate),
m_cursor(cursor)
{
}
void InteractiveCurveViewRange::setDelegate(InteractiveCurveViewRangeDelegate * delegate) {
m_delegate = delegate;
}
void InteractiveCurveViewRange::setCursor(CurveViewCursor * cursor) {
m_cursor = cursor;
}
uint32_t InteractiveCurveViewRange::rangeChecksum() {
float data[5] = {m_xMin, m_xMax, m_yMin, m_yMax, m_yAuto ? 1.0f : 0.0f};
size_t dataLengthInBytes = 5*sizeof(float);
assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4
return Ion::crc32((uint32_t *)data, dataLengthInBytes/sizeof(uint32_t));
}
bool InteractiveCurveViewRange::yAuto() {
return m_yAuto;
}
void InteractiveCurveViewRange::setXMin(float xMin) {
float newXMin = xMin;
MemoizedCurveViewRange::setXMin(clipped(newXMin, false));
if (m_xMax - newXMin < k_minFloat) {
newXMin = m_xMax - k_minFloat;
MemoizedCurveViewRange::setXMin(clipped(newXMin, false));
}
if (m_delegate) {
m_delegate->didChangeRange(this);
}
}
void InteractiveCurveViewRange::setXMax(float xMax) {
float newXMax = xMax;
MemoizedCurveViewRange::setXMax(clipped(newXMax, true));
if (newXMax - m_xMin < k_minFloat) {
newXMax = m_xMin + k_minFloat;
MemoizedCurveViewRange::setXMax(clipped(newXMax, true));
}
if (m_delegate) {
m_delegate->didChangeRange(this);
}
}
void InteractiveCurveViewRange::setYMin(float yMin) {
float newYMin = yMin;
MemoizedCurveViewRange::setYMin(clipped(newYMin, false));
if (m_yMax - newYMin < k_minFloat) {
newYMin = m_yMax - k_minFloat;
MemoizedCurveViewRange::setYMin(clipped(newYMin, false));
}
}
void InteractiveCurveViewRange::setYMax(float yMax) {
float newYMax = yMax;
MemoizedCurveViewRange::setYMax(clipped(newYMax, true));
if (newYMax - m_yMin < k_minFloat) {
newYMax = m_yMin + k_minFloat;
MemoizedCurveViewRange::setYMax(clipped(newYMax, true));
}
}
void InteractiveCurveViewRange::setYAuto(bool yAuto) {
m_yAuto = yAuto;
if (m_delegate) {
m_delegate->didChangeRange(this);
}
}
void InteractiveCurveViewRange::zoom(float ratio, float x, float y) {
float xMin = m_xMin;
float xMax = m_xMax;
float yMin = m_yMin;
float yMax = m_yMax;
if (ratio*std::fabs(xMax-xMin) < k_minFloat || ratio*std::fabs(yMax-yMin) < k_minFloat) {
return;
}
float centerX = std::isnan(x) || std::isinf(x) ? (xMax+xMin)/2: x;
float centerY = std::isnan(y) || std::isinf(y) ? (yMax+yMin)/2: y;
float newXMin = clipped(centerX*(1.0f-ratio)+ratio*xMin, false);
float newXMax = clipped(centerX*(1.0f-ratio)+ratio*xMax, true);
if (!std::isnan(newXMin) && !std::isnan(newXMax)) {
m_xMax = newXMax;
MemoizedCurveViewRange::setXMin(newXMin);
}
m_yAuto = false;
float newYMin = clipped(centerY*(1.0f-ratio)+ratio*yMin, false);
float newYMax = clipped(centerY*(1.0f-ratio)+ratio*yMax, true);
if (!std::isnan(newYMin) && !std::isnan(newYMax)) {
m_yMax = newYMax;
MemoizedCurveViewRange::setYMin(newYMin);
}
}
void InteractiveCurveViewRange::panWithVector(float x, float y) {
m_yAuto = false;
if (clipped(m_xMin + x, false) != m_xMin + x || clipped(m_xMax + x, true) != m_xMax + x || clipped(m_yMin + y, false) != m_yMin + y || clipped(m_yMax + y, true) != m_yMax + y || std::isnan(clipped(m_xMin + x, false)) || std::isnan(clipped(m_xMax + x, true)) || std::isnan(clipped(m_yMin + y, false)) || std::isnan(clipped(m_yMax + y, true))) {
return;
}
m_xMax = clipped(m_xMax + x, true);
MemoizedCurveViewRange::setXMin(clipped(m_xMin + x, false));
m_yMax = clipped(m_yMax + y, true);
MemoizedCurveViewRange::setYMin(clipped(m_yMin + y, false));
}
void InteractiveCurveViewRange::roundAbscissa() {
float xMin = m_xMin;
float xMax = m_xMax;
float newXMin = clipped(std::round((xMin+xMax)/2) - (float)Ion::Display::Width/2.0f, false);
float newXMax = clipped(std::round((xMin+xMax)/2) + (float)Ion::Display::Width/2.0f-1.0f, true);
if (std::isnan(newXMin) || std::isnan(newXMax)) {
return;
}
m_xMax = newXMax;
MemoizedCurveViewRange::setXMin(newXMin);
if (m_delegate) {
m_delegate->didChangeRange(this);
}
}
void InteractiveCurveViewRange::normalize() {
float xMin = m_xMin;
float xMax = m_xMax;
float yMin = m_yMin;
float yMax = m_yMax;
float newXMin = clipped((xMin+xMax)/2 - 5.3f, false);
float newXMax = clipped((xMin+xMax)/2 + 5.3f, true);
if (!std::isnan(newXMin) && !std::isnan(newXMax)) {
m_xMax = newXMax;
MemoizedCurveViewRange::setXMin(newXMin);
}
m_yAuto = false;
float newYMin = clipped((yMin+yMax)/2 - 3.1f, false);
float newYMax = clipped((yMin+yMax)/2 + 3.1f, true);
if (!std::isnan(newYMin) && !std::isnan(newYMax)) {
m_yMax = newYMax;
MemoizedCurveViewRange::setYMin(newYMin);
}
}
void InteractiveCurveViewRange::setTrigonometric() {
m_xMax = 10.5f;
MemoizedCurveViewRange::setXMin(-10.5f);
if (Preferences::sharedPreferences()->angleUnit() == Expression::AngleUnit::Degree) {
m_xMax = 600.0f;
MemoizedCurveViewRange::setXMin(-600.0f);
}
m_yAuto = false;
m_yMax = 1.6f;
MemoizedCurveViewRange::setYMin(-1.6f);
}
void InteractiveCurveViewRange::setDefault() {
if (m_delegate == nullptr) {
return;
}
m_xMax = m_delegate->interestingXRange();
MemoizedCurveViewRange::setXMin(-m_xMax);
setYAuto(true);
}
void InteractiveCurveViewRange::centerAxisAround(Axis axis, float position) {
if (std::isnan(position)) {
return;
}
if (axis == Axis::X) {
float range = m_xMax - m_xMin;
if (std::fabs(position/range) > k_maxRatioPositionRange) {
range = std::pow(10.0f, std::floor(std::log10(std::fabs(position)))-1.0f);
}
m_xMax = clipped(position + range/2.0f, true);
MemoizedCurveViewRange::setXMin(clipped(position - range/2.0f, false));
} else {
m_yAuto = false;
float range = m_yMax - m_yMin;
if (std::fabs(position/range) > k_maxRatioPositionRange) {
range = std::pow(10.0f, std::floor(std::log10(std::fabs(position)))-1.0f);
}
m_yMax = clipped(position + range/2.0f, true);
MemoizedCurveViewRange::setYMin(clipped(position - range/2.0f, false));
}
}
void InteractiveCurveViewRange::panToMakePointVisible(float x, float y, float topMarginRatio, float rightMarginRatio, float bottomMarginRation, float leftMarginRation) {
float xRange = m_xMax - m_xMin;
float yRange = m_yMax - m_yMin;
if (x < m_xMin + leftMarginRation*xRange && !std::isinf(x) && !std::isnan(x)) {
float newXMin = clipped(x - leftMarginRation*xRange, false);
m_xMax = clipped(newXMin + xRange, true);
MemoizedCurveViewRange::setXMin(newXMin);
m_yAuto = false;
}
if (x > m_xMax - rightMarginRatio*xRange && !std::isinf(x) && !std::isnan(x)) {
m_xMax = clipped(x + rightMarginRatio*xRange, true);
MemoizedCurveViewRange::setXMin(clipped(m_xMax - xRange, false));
m_yAuto = false;
}
if (y < m_yMin + bottomMarginRation*yRange && !std::isinf(y) && !std::isnan(y)) {
float newYMin = clipped(y - bottomMarginRation*yRange, false);
m_yMax = clipped(newYMin + yRange, true);
MemoizedCurveViewRange::setYMin(newYMin);
m_yAuto = false;
}
if (y > m_yMax - topMarginRatio*yRange && !std::isinf(y) && !std::isnan(y)) {
m_yMax = clipped(y + topMarginRatio*yRange, true);
MemoizedCurveViewRange::setYMin(clipped(m_yMax - yRange, false));
m_yAuto = false;
}
}
bool InteractiveCurveViewRange::isCursorVisible(float topMarginRatio, float rightMarginRatio, float bottomMarginRation, float leftMarginRation) {
float xRange = m_xMax - m_xMin;
float yRange = m_yMax - m_yMin;
return m_cursor->x() >= m_xMin + leftMarginRation*xRange && m_cursor->x() <= m_xMax - rightMarginRatio*xRange && m_cursor->y() >= m_yMin + bottomMarginRation*yRange && m_cursor->y() <= m_yMax - topMarginRatio*yRange;
}
float InteractiveCurveViewRange::clipped(float x, bool isMax) {
float max = isMax ? k_upperMaxFloat : k_lowerMaxFloat;
float min = isMax ? -k_lowerMaxFloat : -k_upperMaxFloat;
float clippedX = x > max ? max : x;
clippedX = clippedX < min ? min : clippedX;
return clippedX;
}
}