blob: a95b548a5a71d4542794860c6d468b9feb7947b3 [file] [log] [blame]
Mike Reed4c79ecf2018-01-04 17:05:11 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCubicMap.h"
9#include "SkNx.h"
10#include "../../src/pathops/SkPathOpsCubic.h"
11
12void SkCubicMap::setPts(SkPoint p1, SkPoint p2) {
13 Sk2s s1 = Sk2s::Load(&p1) * 3;
14 Sk2s s2 = Sk2s::Load(&p2) * 3;
15
16 s1 = Sk2s::Min(Sk2s::Max(s1, 0), 3);
17 s2 = Sk2s::Min(Sk2s::Max(s2, 0), 3);
18
19 (Sk2s(1) + s1 - s2).store(&fCoeff[0]);
20 (s2 - s1 - s1).store(&fCoeff[1]);
21 s1.store(&fCoeff[2]);
22
23 this->buildXTable();
24}
25
26SkPoint SkCubicMap::computeFromT(float t) const {
27 Sk2s a = Sk2s::Load(&fCoeff[0]);
28 Sk2s b = Sk2s::Load(&fCoeff[1]);
29 Sk2s c = Sk2s::Load(&fCoeff[2]);
30
31 SkPoint result;
32 (((a * t + b) * t + c) * t).store(&result);
33 return result;
34}
35
36float SkCubicMap::computeYFromX(float x) const {
37 x = SkTPin<float>(x, 0, 0.99999f) * kTableCount;
38 float ix = sk_float_floor(x);
39 int index = (int)ix;
40 SkASSERT((unsigned)index < SK_ARRAY_COUNT(fXTable));
41 return this->computeFromT(fXTable[index].fT0 + fXTable[index].fDT * (x - ix)).fY;
42}
43
44float SkCubicMap::hackYFromX(float x) const {
45 x = SkTPin<float>(x, 0, 0.99999f) * kTableCount;
46 float ix = sk_float_floor(x);
47 int index = (int)ix;
48 SkASSERT((unsigned)index < SK_ARRAY_COUNT(fXTable));
49 return fXTable[index].fY0 + fXTable[index].fDY * (x - ix);
50}
51
52static float compute_t_from_x(float A, float B, float C, float x) {
53 double roots[3];
54 SkDEBUGCODE(int count =) SkDCubic::RootsValidT(A, B, C, -x, roots);
55 SkASSERT(count == 1);
56 return (float)roots[0];
57}
58
59void SkCubicMap::buildXTable() {
60 float prevT = 0;
61
62 const float dx = 1.0f / kTableCount;
63 float x = dx;
64
65 fXTable[0].fT0 = 0;
66 fXTable[0].fY0 = 0;
67 for (int i = 1; i < kTableCount; ++i) {
68 float t = compute_t_from_x(fCoeff[0].fX, fCoeff[1].fX, fCoeff[2].fX, x);
69 SkASSERT(t > prevT);
70
71 fXTable[i - 1].fDT = t - prevT;
72 fXTable[i].fT0 = t;
73
74 SkPoint p = this->computeFromT(t);
75 fXTable[i - 1].fDY = p.fY - fXTable[i - 1].fY0;
76 fXTable[i].fY0 = p.fY;
77
78 prevT = t;
79 x += dx;
80 }
81 fXTable[kTableCount - 1].fDT = 1 - prevT;
82 fXTable[kTableCount - 1].fDY = 1 - fXTable[kTableCount - 1].fY0;
83}