• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
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 "include/utils/SkCamera.h"
9 
SkScalarDotDiv(int count,const SkScalar a[],int step_a,const SkScalar b[],int step_b,SkScalar denom)10 static SkScalar SkScalarDotDiv(int count, const SkScalar a[], int step_a,
11                                const SkScalar b[], int step_b,
12                                SkScalar denom) {
13     SkScalar prod = 0;
14     for (int i = 0; i < count; i++) {
15         prod += a[0] * b[0];
16         a += step_a;
17         b += step_b;
18     }
19     return prod / denom;
20 }
21 
22 ///////////////////////////////////////////////////////////////////////////////
23 
SkPatch3D()24 SkPatch3D::SkPatch3D() {
25     this->reset();
26 }
27 
reset()28 void SkPatch3D::reset() {
29     fOrigin = {0, 0, 0};
30     fU = {SK_Scalar1, 0, 0};
31     fV = {0, -SK_Scalar1, 0};
32 }
33 
transform(const SkM44 & m,SkPatch3D * dst) const34 void SkPatch3D::transform(const SkM44& m, SkPatch3D* dst) const {
35     if (dst == nullptr) {
36         dst = (SkPatch3D*)this;
37     }
38     dst->fU = m * fU;
39     dst->fV = m * fV;
40     auto [x,y,z,_] = m.map(fOrigin.x, fOrigin.y, fOrigin.z, 1);
41     dst->fOrigin = {x, y, z};
42 }
43 
dotWith(SkScalar dx,SkScalar dy,SkScalar dz) const44 SkScalar SkPatch3D::dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const {
45     SkScalar cx = fU.y * fV.z - fU.z * fV.y;
46     SkScalar cy = fU.z * fV.x - fU.x * fV.y;
47     SkScalar cz = fU.x * fV.y - fU.y * fV.x;
48 
49     return cx * dx + cy * dy + cz * dz;
50 }
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 
SkCamera3D()54 SkCamera3D::SkCamera3D() {
55     this->reset();
56 }
57 
reset()58 void SkCamera3D::reset() {
59     fLocation = {0, 0, -SkIntToScalar(576)};   // 8 inches backward
60     fAxis = {0, 0, SK_Scalar1};                // forward
61     fZenith = {0, -SK_Scalar1, 0};             // up
62 
63     fObserver = {0, 0, fLocation.z};
64 
65     fNeedToUpdate = true;
66 }
67 
update()68 void SkCamera3D::update() {
69     fNeedToUpdate = true;
70 }
71 
doUpdate() const72 void SkCamera3D::doUpdate() const {
73     SkV3    axis, zenith, cross;
74 
75     // construct a orthonormal basis of cross (x), zenith (y), and axis (z)
76     axis = fAxis.normalize();
77 
78     zenith = fZenith - (axis * fZenith) * axis;
79     zenith = zenith.normalize();
80 
81     cross = axis.cross(zenith);
82 
83     {
84         SkMatrix* orien = &fOrientation;
85         auto [x, y, z] = fObserver;
86 
87         // Looking along the view axis we have:
88         //
89         //   /|\ zenith
90         //    |
91         //    |
92         //    |  * observer (projected on XY plane)
93         //    |
94         //    |____________\ cross
95         //                 /
96         //
97         // So this does a z-shear along the view axis based on the observer's x and y values,
98         // and scales in x and y relative to the negative of the observer's z value
99         // (the observer is in the negative z direction).
100 
101         orien->set(SkMatrix::kMScaleX, x * axis.x - z * cross.x);
102         orien->set(SkMatrix::kMSkewX,  x * axis.y - z * cross.y);
103         orien->set(SkMatrix::kMTransX, x * axis.z - z * cross.z);
104         orien->set(SkMatrix::kMSkewY,  y * axis.x - z * zenith.x);
105         orien->set(SkMatrix::kMScaleY, y * axis.y - z * zenith.y);
106         orien->set(SkMatrix::kMTransY, y * axis.z - z * zenith.z);
107         orien->set(SkMatrix::kMPersp0, axis.x);
108         orien->set(SkMatrix::kMPersp1, axis.y);
109         orien->set(SkMatrix::kMPersp2, axis.z);
110     }
111 }
112 
patchToMatrix(const SkPatch3D & quilt,SkMatrix * matrix) const113 void SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const {
114     if (fNeedToUpdate) {
115         this->doUpdate();
116         fNeedToUpdate = false;
117     }
118 
119     const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation;
120     const SkScalar* patchPtr;
121 
122     SkV3 diff = quilt.fOrigin - fLocation;
123     SkScalar dot = diff.dot({mapPtr[6], mapPtr[7], mapPtr[8]});
124 
125     // This multiplies fOrientation by the matrix [quilt.fU quilt.fV diff] -- U, V, and diff are
126     // column vectors in the matrix -- then divides by the length of the projection of diff onto
127     // the view axis (which is 'dot'). This transforms the patch (which transforms from local path
128     // space to world space) into view space (since fOrientation transforms from world space to
129     // view space).
130     //
131     // The divide by 'dot' isn't strictly necessary as the homogeneous divide would do much the
132     // same thing (it's just scaling the entire matrix by 1/dot). It looks like it's normalizing
133     // the matrix into some canonical space.
134     patchPtr = (const SkScalar*)&quilt;
135     matrix->set(SkMatrix::kMScaleX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
136     matrix->set(SkMatrix::kMSkewY,  SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
137     matrix->set(SkMatrix::kMPersp0, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
138 
139     patchPtr += 3;
140     matrix->set(SkMatrix::kMSkewX,  SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
141     matrix->set(SkMatrix::kMScaleY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
142     matrix->set(SkMatrix::kMPersp1, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
143 
144     patchPtr = (const SkScalar*)(const void*)&diff;
145     matrix->set(SkMatrix::kMTransX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
146     matrix->set(SkMatrix::kMTransY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
147     matrix->set(SkMatrix::kMPersp2, SK_Scalar1);
148 }
149 
150 ///////////////////////////////////////////////////////////////////////////////
151 
Sk3DView()152 Sk3DView::Sk3DView() {
153     fRec = &fInitialRec;
154 }
155 
~Sk3DView()156 Sk3DView::~Sk3DView() {
157     Rec* rec = fRec;
158     while (rec != &fInitialRec) {
159         Rec* next = rec->fNext;
160         delete rec;
161         rec = next;
162     }
163 }
164 
save()165 void Sk3DView::save() {
166     Rec* rec = new Rec;
167     rec->fNext = fRec;
168     rec->fMatrix = fRec->fMatrix;
169     fRec = rec;
170 }
171 
restore()172 void Sk3DView::restore() {
173     SkASSERT(fRec != &fInitialRec);
174     Rec* next = fRec->fNext;
175     delete fRec;
176     fRec = next;
177 }
178 
179 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
setCameraLocation(SkScalar x,SkScalar y,SkScalar z)180 void Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) {
181     // the camera location is passed in inches, set in pt
182     SkScalar lz = z * 72.0f;
183     fCamera.fLocation = {x * 72.0f, y * 72.0f, lz};
184     fCamera.fObserver = {0, 0, lz};
185     fCamera.update();
186 
187 }
188 
getCameraLocationX() const189 SkScalar Sk3DView::getCameraLocationX() const {
190     return fCamera.fLocation.x / 72.0f;
191 }
192 
getCameraLocationY() const193 SkScalar Sk3DView::getCameraLocationY() const {
194     return fCamera.fLocation.y / 72.0f;
195 }
196 
getCameraLocationZ() const197 SkScalar Sk3DView::getCameraLocationZ() const {
198     return fCamera.fLocation.z / 72.0f;
199 }
200 #endif
201 
translate(SkScalar x,SkScalar y,SkScalar z)202 void Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z) {
203     fRec->fMatrix.preTranslate(x, y, z);
204 }
205 
rotateX(SkScalar deg)206 void Sk3DView::rotateX(SkScalar deg) {
207     fRec->fMatrix.preConcat(SkM44::Rotate({1, 0, 0}, deg * SK_ScalarPI / 180));
208 }
209 
rotateY(SkScalar deg)210 void Sk3DView::rotateY(SkScalar deg) {
211     fRec->fMatrix.preConcat(SkM44::Rotate({0,-1, 0}, deg * SK_ScalarPI / 180));
212 }
213 
rotateZ(SkScalar deg)214 void Sk3DView::rotateZ(SkScalar deg) {
215     fRec->fMatrix.preConcat(SkM44::Rotate({0, 0, 1}, deg * SK_ScalarPI / 180));
216 }
217 
dotWithNormal(SkScalar x,SkScalar y,SkScalar z) const218 SkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const {
219     SkPatch3D   patch;
220     patch.transform(fRec->fMatrix);
221     return patch.dotWith(x, y, z);
222 }
223 
getMatrix(SkMatrix * matrix) const224 void Sk3DView::getMatrix(SkMatrix* matrix) const {
225     if (matrix != nullptr) {
226         SkPatch3D   patch;
227         patch.transform(fRec->fMatrix);
228         fCamera.patchToMatrix(patch, matrix);
229     }
230 }
231 
232 #include "include/core/SkCanvas.h"
233 
applyToCanvas(SkCanvas * canvas) const234 void Sk3DView::applyToCanvas(SkCanvas* canvas) const {
235     SkMatrix    matrix;
236 
237     this->getMatrix(&matrix);
238     canvas->concat(matrix);
239 }
240