Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: cc/math_util.cc

Issue 11316043: Implement unit tests and temporary MathUtil wrappers for transform functionality (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressed reviewer feedback Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/math_util.h ('k') | cc/math_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/math_util.h" 5 #include "cc/math_util.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <limits> 8 #include <limits>
9 9
10 #include "ui/gfx/quad_f.h" 10 #include "ui/gfx/quad_f.h"
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 dotProduct = std::max(-1.0, std::min(1.0, dotProduct)); 388 dotProduct = std::max(-1.0, std::min(1.0, dotProduct));
389 return static_cast<float>(Rad2Deg(std::acos(dotProduct))); 389 return static_cast<float>(Rad2Deg(std::acos(dotProduct)));
390 } 390 }
391 391
392 gfx::Vector2dF MathUtil::projectVector(gfx::Vector2dF source, gfx::Vector2dF des tination) 392 gfx::Vector2dF MathUtil::projectVector(gfx::Vector2dF source, gfx::Vector2dF des tination)
393 { 393 {
394 float projectedLength = gfx::DotProduct(source, destination) / destination.L engthSquared(); 394 float projectedLength = gfx::DotProduct(source, destination) / destination.L engthSquared();
395 return gfx::Vector2dF(projectedLength * destination.x(), projectedLength * d estination.y()); 395 return gfx::Vector2dF(projectedLength * destination.x(), projectedLength * d estination.y());
396 } 396 }
397 397
398 bool MathUtil::isInvertible(const gfx::Transform& transform)
399 {
400 const SkMatrix44& matrix = transform.matrix();
401 double determinant = matrix.determinant();
402
403 // TODO (shawnsingh): we may need to check fuzzy against an epsilon here ins tead?
danakj 2012/11/17 02:14:17 Is something random going to break and cause a rev
404 return determinant != 0;
405 }
406
407 bool MathUtil::isBackFaceVisible(const gfx::Transform&)
408 {
409 DCHECK(false); // NOT YET IMPLEMENTED!!
danakj 2012/11/17 02:14:17 NOTREACHED()
410 return false;
411 }
412
413 bool MathUtil::isIdentity(const gfx::Transform& transform)
414 {
415 return transform.matrix().isIdentity();
416 }
417
418 bool MathUtil::isIdentityOrTranslation(const gfx::Transform& transform)
419 {
420 const SkMatrix44& matrix = transform.matrix();
421
422 bool hasNoPerspective = !matrix.getDouble(3, 0) && !matrix.getDouble(3, 1) & & !matrix.getDouble(3, 2) && (matrix.getDouble(3, 3) == 1);
423 bool hasNoRotationOrSkew = !matrix.getDouble(0, 1) && !matrix.getDouble(0, 2 ) && !matrix.getDouble(1, 0) &&
424 !matrix.getDouble(1, 2) && !matrix.getDouble(2, 0) && !matrix.getDouble( 2, 1);
425 bool hasNoScale = matrix.getDouble(0, 0) == 1
danakj 2012/11/17 02:14:17 nit: the line wrapping is inconsistent here.
426 && matrix.getDouble(1, 1) == 1
427 && matrix.getDouble(2, 2) == 1;
428
429 return hasNoPerspective && hasNoRotationOrSkew && hasNoScale;
430 }
431
432 bool MathUtil::hasPerspective(const gfx::Transform& transform)
433 {
434 // Mathematically it is a bit too strict to expect the 4th element to be
435 // equal to 1. However, the only non-perspective case where this element
436 // becomes non-1 is when it was explicitly initialized. In that case it
437 // still causes us to have a nontrivial divide-by-w, so we count it as
438 // being perspective here.
439 const SkMatrix44& matrix = transform.matrix();
440 return matrix.getDouble(3, 0) || matrix.getDouble(3, 1) || matrix.getDouble( 3, 2) || (matrix.getDouble(3, 3) != 1);
441 }
442
443 void MathUtil::makeIdentity(gfx::Transform* transform)
444 {
445 transform->matrix().setIdentity();
446 }
447
448 void MathUtil::rotateEulerAngles(gfx::Transform* transform, double eulerX, doubl e eulerY, double eulerZ)
449 {
450 // TODO (shawnsingh): make this implementation faster and more accurate by
451 // hard-coding each matrix instead of calling rotateAxisAngle().
452 gfx::Transform rotationAboutX;
453 gfx::Transform rotationAboutY;
454 gfx::Transform rotationAboutZ;
455
456 MathUtil::rotateAxisAngle(&rotationAboutX, 1, 0, 0, eulerX);
457 MathUtil::rotateAxisAngle(&rotationAboutY, 0, 1, 0, eulerY);
458 MathUtil::rotateAxisAngle(&rotationAboutZ, 0, 0, 1, eulerZ);
459
460 gfx::Transform composite = rotationAboutZ * rotationAboutY * rotationAboutX;
461 transform->PreconcatTransform(composite);
462 }
463
464 void MathUtil::rotateAxisAngle(gfx::Transform* transform, double i, double j, do uble k, double degrees)
465 {
466 // TODO (shawnsingh): fix gfx::Transform API to receive vector instead of
467 // point for the axis.
468 gfx::Point3F axis(i, j, k);
469 transform->PreconcatRotateAbout(axis, degrees);
470 }
471
472 gfx::Transform MathUtil::inverse(const gfx::Transform& transform)
473 {
474 gfx::Transform result;
475 bool invertedSuccessfully = transform.GetInverse(&result);
476
477 if (invertedSuccessfully)
478 return result;
479
480 // If transform was un-invertible, then just return identity.
481 return gfx::Transform();
482 }
483
484 gfx::Transform MathUtil::to2dTransform(const gfx::Transform& transform)
485 {
486 gfx::Transform result = transform;
487 SkMatrix44& matrix = result.matrix();
488 matrix.setDouble(0, 2, 0);
489 matrix.setDouble(1, 2, 0);
490 matrix.setDouble(2, 2, 1);
491 matrix.setDouble(3, 2, 0);
492
493 matrix.setDouble(2, 0, 0);
494 matrix.setDouble(2, 1, 0);
495 matrix.setDouble(2, 3, 0);
496
497 return result;
498 }
499
500 gfx::Transform MathUtil::toGfxTransform(const WebKit::WebTransformationMatrix&)
501 {
502 DCHECK(false); // NOT YET IMPLEMENTED!!
danakj 2012/11/17 02:14:17 NOTREACHED() but.. We're going to need this to ev
shawnsingh 2012/11/17 05:42:41 OK I guess you've landed by now, but why is it imp
danakj 2012/11/17 08:27:21 It's in and waiting for roll. Also, it's explicit
503 return gfx::Transform();
504 }
505
506 gfx::Transform MathUtil::createGfxTransform(double m11, double m12, double m13, double m14,
507 double m21, double m22, double m23, double m24,
508 double m31, double m32, double m33, double m34,
509 double m41, double m42, double m43, double m44)
510 {
511 gfx::Transform result;
512 SkMatrix44& matrix = result.matrix();
513
514 // Initialize column 1
515 matrix.setDouble(0, 0, m11);
516 matrix.setDouble(1, 0, m12);
517 matrix.setDouble(2, 0, m13);
518 matrix.setDouble(3, 0, m14);
519
520 // Initialize column 2
521 matrix.setDouble(0, 1, m21);
522 matrix.setDouble(1, 1, m22);
523 matrix.setDouble(2, 1, m23);
524 matrix.setDouble(3, 1, m24);
525
526 // Initialize column 3
527 matrix.setDouble(0, 2, m31);
528 matrix.setDouble(1, 2, m32);
529 matrix.setDouble(2, 2, m33);
530 matrix.setDouble(3, 2, m34);
531
532 // Initialize column 4
533 matrix.setDouble(0, 3, m41);
534 matrix.setDouble(1, 3, m42);
535 matrix.setDouble(2, 3, m43);
536 matrix.setDouble(3, 3, m44);
537
538 return result;
539 }
540
541 gfx::Transform MathUtil::createGfxTransform(double a, double b, double c,
542 double d, double e, double f)
543 {
544 gfx::Transform result;
545 SkMatrix44& matrix = result.matrix();
546 matrix.setDouble(0, 0, a);
547 matrix.setDouble(1, 0, b);
548 matrix.setDouble(0, 1, c);
549 matrix.setDouble(1, 1, d);
550 matrix.setDouble(0, 3, e);
551 matrix.setDouble(1, 3, f);
552
553 return result;
554 }
555
556 gfx::Transform operator*(const gfx::Transform& A, const gfx::Transform& B)
557 {
558 // Compute A * B.
559 gfx::Transform result = A;
560 result.PreconcatTransform(B);
561 return result;
562 }
563
398 } // namespace cc 564 } // namespace cc
OLDNEW
« no previous file with comments | « cc/math_util.h ('k') | cc/math_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698