SkCamera.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #include "include/utils/SkCamera.h"
  8. static SkScalar SkScalarDotDiv(int count, const SkScalar a[], int step_a,
  9. const SkScalar b[], int step_b,
  10. SkScalar denom) {
  11. SkScalar prod = 0;
  12. for (int i = 0; i < count; i++) {
  13. prod += a[0] * b[0];
  14. a += step_a;
  15. b += step_b;
  16. }
  17. return prod / denom;
  18. }
  19. static SkScalar SkScalarDot(int count, const SkScalar a[], int step_a,
  20. const SkScalar b[], int step_b) {
  21. SkScalar prod = 0;
  22. for (int i = 0; i < count; i++) {
  23. prod += a[0] * b[0];
  24. a += step_a;
  25. b += step_b;
  26. }
  27. return prod;
  28. }
  29. ///////////////////////////////////////////////////////////////////////////////
  30. SkScalar SkPoint3D::normalize(SkUnit3D* unit) const {
  31. SkScalar mag = SkScalarSqrt(fX*fX + fY*fY + fZ*fZ);
  32. if (mag) {
  33. SkScalar scale = SkScalarInvert(mag);
  34. unit->fX = fX * scale;
  35. unit->fY = fY * scale;
  36. unit->fZ = fZ * scale;
  37. } else {
  38. unit->fX = unit->fY = unit->fZ = 0;
  39. }
  40. return mag;
  41. }
  42. SkScalar SkUnit3D::Dot(const SkUnit3D& a, const SkUnit3D& b) {
  43. return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ;
  44. }
  45. void SkUnit3D::Cross(const SkUnit3D& a, const SkUnit3D& b, SkUnit3D* cross) {
  46. SkASSERT(cross);
  47. // use x,y,z, in case &a == cross or &b == cross
  48. SkScalar x = a.fY * b.fZ - a.fZ * b.fY;
  49. SkScalar y = a.fZ * b.fX - a.fX * b.fY;
  50. SkScalar z = a.fX * b.fY - a.fY * b.fX;
  51. cross->set(x, y, z);
  52. }
  53. ///////////////////////////////////////////////////////////////////////////////
  54. SkPatch3D::SkPatch3D() {
  55. this->reset();
  56. }
  57. void SkPatch3D::reset() {
  58. fOrigin.set(0, 0, 0);
  59. fU.set(SK_Scalar1, 0, 0);
  60. fV.set(0, -SK_Scalar1, 0);
  61. }
  62. void SkPatch3D::transform(const SkMatrix3D& m, SkPatch3D* dst) const {
  63. if (dst == nullptr) {
  64. dst = (SkPatch3D*)this;
  65. }
  66. m.mapVector(fU, &dst->fU);
  67. m.mapVector(fV, &dst->fV);
  68. m.mapPoint(fOrigin, &dst->fOrigin);
  69. }
  70. SkScalar SkPatch3D::dotWith(SkScalar dx, SkScalar dy, SkScalar dz) const {
  71. SkScalar cx = fU.fY * fV.fZ - fU.fZ * fV.fY;
  72. SkScalar cy = fU.fZ * fV.fX - fU.fX * fV.fY;
  73. SkScalar cz = fU.fX * fV.fY - fU.fY * fV.fX;
  74. return cx * dx + cy * dy + cz * dz;
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////
  77. void SkMatrix3D::reset() {
  78. memset(fMat, 0, sizeof(fMat));
  79. fMat[0][0] = fMat[1][1] = fMat[2][2] = SK_Scalar1;
  80. }
  81. void SkMatrix3D::setTranslate(SkScalar x, SkScalar y, SkScalar z) {
  82. memset(fMat, 0, sizeof(fMat));
  83. fMat[0][0] = x;
  84. fMat[1][1] = y;
  85. fMat[2][2] = z;
  86. }
  87. void SkMatrix3D::setRotateX(SkScalar degX) {
  88. SkScalar r = SkDegreesToRadians(degX),
  89. s = SkScalarSin(r),
  90. c = SkScalarCos(r);
  91. this->setRow(0, SK_Scalar1, 0, 0);
  92. this->setRow(1, 0, c, -s);
  93. this->setRow(2, 0, s, c);
  94. }
  95. void SkMatrix3D::setRotateY(SkScalar degY) {
  96. SkScalar r = SkDegreesToRadians(degY),
  97. s = SkScalarSin(r),
  98. c = SkScalarCos(r);
  99. this->setRow(0, c, 0, -s);
  100. this->setRow(1, 0, SK_Scalar1, 0);
  101. this->setRow(2, s, 0, c);
  102. }
  103. void SkMatrix3D::setRotateZ(SkScalar degZ) {
  104. SkScalar r = SkDegreesToRadians(degZ),
  105. s = SkScalarSin(r),
  106. c = SkScalarCos(r);
  107. this->setRow(0, c, -s, 0);
  108. this->setRow(1, s, c, 0);
  109. this->setRow(2, 0, 0, SK_Scalar1);
  110. }
  111. void SkMatrix3D::preTranslate(SkScalar x, SkScalar y, SkScalar z) {
  112. SkScalar col[3] = { x, y, z};
  113. for (int i = 0; i < 3; i++) {
  114. fMat[i][3] += SkScalarDot(3, &fMat[i][0], 1, col, 1);
  115. }
  116. }
  117. void SkMatrix3D::preRotateX(SkScalar degX) {
  118. SkMatrix3D m;
  119. m.setRotateX(degX);
  120. this->setConcat(*this, m);
  121. }
  122. void SkMatrix3D::preRotateY(SkScalar degY) {
  123. SkMatrix3D m;
  124. m.setRotateY(degY);
  125. this->setConcat(*this, m);
  126. }
  127. void SkMatrix3D::preRotateZ(SkScalar degZ) {
  128. SkMatrix3D m;
  129. m.setRotateZ(degZ);
  130. this->setConcat(*this, m);
  131. }
  132. void SkMatrix3D::setConcat(const SkMatrix3D& a, const SkMatrix3D& b) {
  133. SkMatrix3D tmp;
  134. SkMatrix3D* c = this;
  135. if (this == &a || this == &b) {
  136. c = &tmp;
  137. }
  138. for (int i = 0; i < 3; i++) {
  139. for (int j = 0; j < 3; j++) {
  140. c->fMat[i][j] = SkScalarDot(3, &a.fMat[i][0], 1, &b.fMat[0][j], 4);
  141. }
  142. c->fMat[i][3] = SkScalarDot(3, &a.fMat[i][0], 1,
  143. &b.fMat[0][3], 4) + a.fMat[i][3];
  144. }
  145. if (c == &tmp) {
  146. *this = tmp;
  147. }
  148. }
  149. void SkMatrix3D::mapPoint(const SkPoint3D& src, SkPoint3D* dst) const {
  150. SkScalar x = SkScalarDot(3, &fMat[0][0], 1, &src.fX, 1) + fMat[0][3];
  151. SkScalar y = SkScalarDot(3, &fMat[1][0], 1, &src.fX, 1) + fMat[1][3];
  152. SkScalar z = SkScalarDot(3, &fMat[2][0], 1, &src.fX, 1) + fMat[2][3];
  153. dst->set(x, y, z);
  154. }
  155. void SkMatrix3D::mapVector(const SkVector3D& src, SkVector3D* dst) const {
  156. SkScalar x = SkScalarDot(3, &fMat[0][0], 1, &src.fX, 1);
  157. SkScalar y = SkScalarDot(3, &fMat[1][0], 1, &src.fX, 1);
  158. SkScalar z = SkScalarDot(3, &fMat[2][0], 1, &src.fX, 1);
  159. dst->set(x, y, z);
  160. }
  161. ///////////////////////////////////////////////////////////////////////////////
  162. SkCamera3D::SkCamera3D() {
  163. this->reset();
  164. }
  165. void SkCamera3D::reset() {
  166. fLocation.set(0, 0, -SkIntToScalar(576)); // 8 inches backward
  167. fAxis.set(0, 0, SK_Scalar1); // forward
  168. fZenith.set(0, -SK_Scalar1, 0); // up
  169. fObserver.set(0, 0, fLocation.fZ);
  170. fNeedToUpdate = true;
  171. }
  172. void SkCamera3D::update() {
  173. fNeedToUpdate = true;
  174. }
  175. void SkCamera3D::doUpdate() const {
  176. SkUnit3D axis, zenith, cross;
  177. // construct a orthonormal basis of cross (x), zenith (y), and axis (z)
  178. fAxis.normalize(&axis);
  179. {
  180. SkScalar dot = SkUnit3D::Dot(SkUnit3D{fZenith.fX, fZenith.fY, fZenith.fZ}, axis);
  181. zenith.fX = fZenith.fX - dot * axis.fX;
  182. zenith.fY = fZenith.fY - dot * axis.fY;
  183. zenith.fZ = fZenith.fZ - dot * axis.fZ;
  184. SkPoint3D{zenith.fX, zenith.fY, zenith.fZ}.normalize(&zenith);
  185. }
  186. SkUnit3D::Cross(axis, zenith, &cross);
  187. {
  188. SkMatrix* orien = &fOrientation;
  189. SkScalar x = fObserver.fX;
  190. SkScalar y = fObserver.fY;
  191. SkScalar z = fObserver.fZ;
  192. // Looking along the view axis we have:
  193. //
  194. // /|\ zenith
  195. // |
  196. // |
  197. // | * observer (projected on XY plane)
  198. // |
  199. // |____________\ cross
  200. // /
  201. //
  202. // So this does a z-shear along the view axis based on the observer's x and y values,
  203. // and scales in x and y relative to the negative of the observer's z value
  204. // (the observer is in the negative z direction).
  205. orien->set(SkMatrix::kMScaleX, x * axis.fX - z * cross.fX);
  206. orien->set(SkMatrix::kMSkewX, x * axis.fY - z * cross.fY);
  207. orien->set(SkMatrix::kMTransX, x * axis.fZ - z * cross.fZ);
  208. orien->set(SkMatrix::kMSkewY, y * axis.fX - z * zenith.fX);
  209. orien->set(SkMatrix::kMScaleY, y * axis.fY - z * zenith.fY);
  210. orien->set(SkMatrix::kMTransY, y * axis.fZ - z * zenith.fZ);
  211. orien->set(SkMatrix::kMPersp0, axis.fX);
  212. orien->set(SkMatrix::kMPersp1, axis.fY);
  213. orien->set(SkMatrix::kMPersp2, axis.fZ);
  214. }
  215. }
  216. void SkCamera3D::patchToMatrix(const SkPatch3D& quilt, SkMatrix* matrix) const {
  217. if (fNeedToUpdate) {
  218. this->doUpdate();
  219. fNeedToUpdate = false;
  220. }
  221. const SkScalar* mapPtr = (const SkScalar*)(const void*)&fOrientation;
  222. const SkScalar* patchPtr;
  223. SkPoint3D diff;
  224. SkScalar dot;
  225. diff.fX = quilt.fOrigin.fX - fLocation.fX;
  226. diff.fY = quilt.fOrigin.fY - fLocation.fY;
  227. diff.fZ = quilt.fOrigin.fZ - fLocation.fZ;
  228. dot = SkUnit3D::Dot(SkUnit3D{diff.fX, diff.fY, diff.fZ},
  229. SkUnit3D{mapPtr[6], mapPtr[7], mapPtr[8]});
  230. // This multiplies fOrientation by the matrix [quilt.fU quilt.fV diff] -- U, V, and diff are
  231. // column vectors in the matrix -- then divides by the length of the projection of diff onto
  232. // the view axis (which is 'dot'). This transforms the patch (which transforms from local path
  233. // space to world space) into view space (since fOrientation transforms from world space to
  234. // view space).
  235. //
  236. // The divide by 'dot' isn't strictly necessary as the homogeneous divide would do much the
  237. // same thing (it's just scaling the entire matrix by 1/dot). It looks like it's normalizing
  238. // the matrix into some canonical space.
  239. patchPtr = (const SkScalar*)&quilt;
  240. matrix->set(SkMatrix::kMScaleX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
  241. matrix->set(SkMatrix::kMSkewY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
  242. matrix->set(SkMatrix::kMPersp0, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
  243. patchPtr += 3;
  244. matrix->set(SkMatrix::kMSkewX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
  245. matrix->set(SkMatrix::kMScaleY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
  246. matrix->set(SkMatrix::kMPersp1, SkScalarDotDiv(3, patchPtr, 1, mapPtr+6, 1, dot));
  247. patchPtr = (const SkScalar*)(const void*)&diff;
  248. matrix->set(SkMatrix::kMTransX, SkScalarDotDiv(3, patchPtr, 1, mapPtr, 1, dot));
  249. matrix->set(SkMatrix::kMTransY, SkScalarDotDiv(3, patchPtr, 1, mapPtr+3, 1, dot));
  250. matrix->set(SkMatrix::kMPersp2, SK_Scalar1);
  251. }
  252. ///////////////////////////////////////////////////////////////////////////////
  253. Sk3DView::Sk3DView() {
  254. fInitialRec.fMatrix.reset();
  255. fRec = &fInitialRec;
  256. }
  257. Sk3DView::~Sk3DView() {
  258. Rec* rec = fRec;
  259. while (rec != &fInitialRec) {
  260. Rec* next = rec->fNext;
  261. delete rec;
  262. rec = next;
  263. }
  264. }
  265. void Sk3DView::save() {
  266. Rec* rec = new Rec;
  267. rec->fNext = fRec;
  268. rec->fMatrix = fRec->fMatrix;
  269. fRec = rec;
  270. }
  271. void Sk3DView::restore() {
  272. SkASSERT(fRec != &fInitialRec);
  273. Rec* next = fRec->fNext;
  274. delete fRec;
  275. fRec = next;
  276. }
  277. #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
  278. void Sk3DView::setCameraLocation(SkScalar x, SkScalar y, SkScalar z) {
  279. // the camera location is passed in inches, set in pt
  280. SkScalar lz = z * 72.0f;
  281. fCamera.fLocation.set(x * 72.0f, y * 72.0f, lz);
  282. fCamera.fObserver.set(0, 0, lz);
  283. fCamera.update();
  284. }
  285. SkScalar Sk3DView::getCameraLocationX() const {
  286. return fCamera.fLocation.fX / 72.0f;
  287. }
  288. SkScalar Sk3DView::getCameraLocationY() const {
  289. return fCamera.fLocation.fY / 72.0f;
  290. }
  291. SkScalar Sk3DView::getCameraLocationZ() const {
  292. return fCamera.fLocation.fZ / 72.0f;
  293. }
  294. #endif
  295. void Sk3DView::translate(SkScalar x, SkScalar y, SkScalar z) {
  296. fRec->fMatrix.preTranslate(x, y, z);
  297. }
  298. void Sk3DView::rotateX(SkScalar deg) {
  299. fRec->fMatrix.preRotateX(deg);
  300. }
  301. void Sk3DView::rotateY(SkScalar deg) {
  302. fRec->fMatrix.preRotateY(deg);
  303. }
  304. void Sk3DView::rotateZ(SkScalar deg) {
  305. fRec->fMatrix.preRotateZ(deg);
  306. }
  307. SkScalar Sk3DView::dotWithNormal(SkScalar x, SkScalar y, SkScalar z) const {
  308. SkPatch3D patch;
  309. patch.transform(fRec->fMatrix);
  310. return patch.dotWith(x, y, z);
  311. }
  312. void Sk3DView::getMatrix(SkMatrix* matrix) const {
  313. if (matrix != nullptr) {
  314. SkPatch3D patch;
  315. patch.transform(fRec->fMatrix);
  316. fCamera.patchToMatrix(patch, matrix);
  317. }
  318. }
  319. #include "include/core/SkCanvas.h"
  320. void Sk3DView::applyToCanvas(SkCanvas* canvas) const {
  321. SkMatrix matrix;
  322. this->getMatrix(&matrix);
  323. canvas->concat(matrix);
  324. }