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

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: 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
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 // TODO (shawnsingh): improve this function. We don't need to perform a
401 // full matrix inversion when the matrix was actually invertible.
Ian Vollick 2012/11/16 13:44:46 Now that SkMatrix44::determinant is public, would
402 gfx::Transform inverse;
403 bool invertible = transform.GetInverse(&inverse);
404 return invertible;
405 }
406
407 bool MathUtil::isBackFaceVisible(const gfx::Transform&)
408 {
409 DCHECK(false); // NOT YET IMPLEMENTED!!
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
Ian Vollick 2012/11/16 13:44:46 SkMatrix44 now has getDouble for accessing matrix
422 bool hasNoPerspective = !matrix.get(3, 0) && !matrix.get(3, 1) && !matrix.ge t(3, 2) && (matrix.get(3, 3) == 1);
423 bool hasNoRotationOrSkew = !matrix.get(0, 1) && !matrix.get(0, 2) && !matrix .get(1, 0) &&
424 !matrix.get(1, 2) && !matrix.get(2, 0) && !matrix.get(2, 1);
425 bool hasNoScale = matrix.get(0, 0) == 1
426 && matrix.get(1, 1) == 1
427 && matrix.get(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.get(3, 0) || matrix.get(3, 1) || matrix.get(3, 2) || (matrix.g et(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, float eulerX, float eulerY, float eulerZ)
449 {
450 // TODO (shawnsingh): make this implementation faster and more accurate by
451 // hard-coding each matrix instead of calling rotateAxisAngle().
Ian Vollick 2012/11/16 13:44:46 Actually, it would be nice if SkMatrix44 noticed w
shawnsingh 2012/11/17 00:38:31 I think I understand you're saying that we should
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, float i, float j, floa t k, float degrees)
465 {
466 // TODO (shawnsingh): fix gfx::Transform API to receive vector instead of
467 // point for the axis.
Ian Vollick 2012/11/16 13:44:46 This is a good point, but this would ideally be a
shawnsingh 2012/11/17 00:38:31 Sounds like musings for follow-up patch, right? a
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 transform.GetInverse(&result);
Ian Vollick 2012/11/16 13:44:46 GetInverse should be decorated with WARN_UNUSED_RE
476 return result;
477 }
478
479 gfx::Transform MathUtil::to2dTransform(const gfx::Transform& transform)
480 {
481 gfx::Transform result = transform;
482 SkMatrix44& matrix = result.matrix();
483 matrix.set(0, 2, 0);
Ian Vollick 2012/11/16 13:44:46 setDouble
484 matrix.set(1, 2, 0);
485 matrix.set(2, 2, 1);
486 matrix.set(3, 2, 0);
487
488 matrix.set(2, 0, 0);
489 matrix.set(2, 1, 0);
490 matrix.set(2, 3, 0);
491
492 return result;
493 }
494
495 gfx::Transform MathUtil::toGfxTransform(const WebKit::WebTransformationMatrix&)
496 {
497 DCHECK(false); // NOT YET IMPLEMENTED!!
498 return gfx::Transform();
499 }
500
501 gfx::Transform MathUtil::createGfxTransform(float m11, float m12, float m13, flo at m14,
502 float m21, float m22, float m23, flo at m24,
503 float m31, float m32, float m33, flo at m34,
504 float m41, float m42, float m43, flo at m44)
505 {
506 gfx::Transform result;
507 SkMatrix44& matrix = result.matrix();
508
509 // Initialize column 1
510 matrix.set(0, 0, m11);
Ian Vollick 2012/11/16 13:44:46 setDouble.
511 matrix.set(1, 0, m12);
512 matrix.set(2, 0, m13);
513 matrix.set(3, 0, m14);
514
515 // Initialize column 2
516 matrix.set(0, 1, m21);
517 matrix.set(1, 1, m22);
518 matrix.set(2, 1, m23);
519 matrix.set(3, 1, m24);
520
521 // Initialize column 2
Ian Vollick 2012/11/16 13:44:46 Copy pasta -- column 3. Here and below.
522 matrix.set(0, 2, m31);
523 matrix.set(1, 2, m32);
524 matrix.set(2, 2, m33);
525 matrix.set(3, 2, m34);
526
527 // Initialize column 2
528 matrix.set(0, 3, m41);
529 matrix.set(1, 3, m42);
530 matrix.set(2, 3, m43);
531 matrix.set(3, 3, m44);
532
533 return result;
534 }
535
536 gfx::Transform MathUtil::createGfxTransform(float a, float b, float c,
537 float d, float e, float f)
538 {
539 gfx::Transform result;
540 SkMatrix44& matrix = result.matrix();
541 matrix.set(0, 0, a);
Ian Vollick 2012/11/16 13:44:46 setDouble
542 matrix.set(1, 0, b);
543 matrix.set(0, 1, c);
544 matrix.set(1, 1, d);
545 matrix.set(0, 3, e);
546 matrix.set(1, 3, f);
547
548 return result;
549 }
550
551 gfx::Transform operator*(const gfx::Transform& A, const gfx::Transform& B)
552 {
553 // Compute A * B.
554 gfx::Transform result = A;
555 result.PreconcatTransform(B);
556 return result;
557 }
558
398 } // namespace cc 559 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698