LCOV - code coverage report
Current view: top level - gfx/2d - BasePoint.h (source / functions) Hit Total Coverage
Test: output.info Lines: 35 44 79.5 %
Date: 2017-07-14 16:53:18 Functions: 76 128 59.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       5             : 
       6             : #ifndef MOZILLA_GFX_BASEPOINT_H_
       7             : #define MOZILLA_GFX_BASEPOINT_H_
       8             : 
       9             : #include <cmath>
      10             : #include <ostream>
      11             : #include "mozilla/Attributes.h"
      12             : #include "mozilla/FloatingPoint.h"
      13             : #include "mozilla/TypeTraits.h"
      14             : 
      15             : namespace mozilla {
      16             : namespace gfx {
      17             : 
      18             : /**
      19             :  * Do not use this class directly. Subclass it, pass that subclass as the
      20             :  * Sub parameter, and only use that subclass. This allows methods to safely
      21             :  * cast 'this' to 'Sub*'.
      22             :  */
      23             : template <class T, class Sub, class Coord = T>
      24             : struct BasePoint {
      25             :   union {
      26             :     struct {
      27             :       T x, y;
      28             :     };
      29             :     T components[2];
      30             :   };
      31             : 
      32             :   // Constructors
      33       15732 :   constexpr BasePoint() : x(0), y(0) {}
      34      120039 :   constexpr BasePoint(Coord aX, Coord aY) : x(aX), y(aY) {}
      35             : 
      36          77 :   void MoveTo(T aX, T aY) { x = aX; y = aY; }
      37           0 :   void MoveBy(T aDx, T aDy) { x += aDx; y += aDy; }
      38             : 
      39             :   // Note that '=' isn't defined so we'll get the
      40             :   // compiler generated default assignment operator
      41             : 
      42        1989 :   bool operator==(const Sub& aPoint) const {
      43        1989 :     return x == aPoint.x && y == aPoint.y;
      44             :   }
      45        6244 :   bool operator!=(const Sub& aPoint) const {
      46        6244 :     return x != aPoint.x || y != aPoint.y;
      47             :   }
      48             : 
      49        2307 :   Sub operator+(const Sub& aPoint) const {
      50        2307 :     return Sub(x + aPoint.x, y + aPoint.y);
      51             :   }
      52        3911 :   Sub operator-(const Sub& aPoint) const {
      53        3911 :     return Sub(x - aPoint.x, y - aPoint.y);
      54             :   }
      55       22776 :   Sub& operator+=(const Sub& aPoint) {
      56       22776 :     x += aPoint.x;
      57       22776 :     y += aPoint.y;
      58       22776 :     return *static_cast<Sub*>(this);
      59             :   }
      60          82 :   Sub& operator-=(const Sub& aPoint) {
      61          82 :     x -= aPoint.x;
      62          82 :     y -= aPoint.y;
      63          82 :     return *static_cast<Sub*>(this);
      64             :   }
      65             : 
      66         169 :   Sub operator*(T aScale) const {
      67         169 :     return Sub(x * aScale, y * aScale);
      68             :   }
      69           7 :   Sub operator/(T aScale) const {
      70           7 :     return Sub(x / aScale, y / aScale);
      71             :   }
      72             : 
      73        6838 :   Sub operator-() const {
      74        6838 :     return Sub(-x, -y);
      75             :   }
      76             : 
      77          13 :   T DotProduct(const Sub& aPoint) const {
      78          13 :       return x * aPoint.x + y * aPoint.y;
      79             :   }
      80             : 
      81           0 :   Coord Length() const {
      82           0 :     return hypot(x, y);
      83             :   }
      84             : 
      85           0 :   T LengthSquare() const {
      86           0 :     return x * x + y * y;
      87             :   }
      88             : 
      89             :   // Round() is *not* rounding to nearest integer if the values are negative.
      90             :   // They are always rounding as floor(n + 0.5).
      91             :   // See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14
      92        1228 :   Sub& Round() {
      93        1228 :     x = Coord(floor(T(x) + T(0.5)));
      94        1228 :     y = Coord(floor(T(y) + T(0.5)));
      95        1228 :     return *static_cast<Sub*>(this);
      96             :   }
      97             : 
      98             :   // "Finite" means not inf and not NaN
      99         102 :   bool IsFinite() const
     100             :   {
     101             :     typedef typename mozilla::Conditional<mozilla::IsSame<T, float>::value, float, double>::Type FloatType;
     102         102 :     return (mozilla::IsFinite(FloatType(x)) && mozilla::IsFinite(FloatType(y)));
     103             :     return true;
     104             :   }
     105             : 
     106           0 :   void Clamp(T aMaxAbsValue)
     107             :   {
     108           0 :     x = std::max(std::min(x, aMaxAbsValue), -aMaxAbsValue);
     109           0 :     y = std::max(std::min(y, aMaxAbsValue), -aMaxAbsValue);
     110           0 :   }
     111             : 
     112           2 :   friend std::ostream& operator<<(std::ostream& stream, const BasePoint<T, Sub, Coord>& aPoint) {
     113           2 :     return stream << '(' << aPoint.x << ',' << aPoint.y << ')';
     114             :   }
     115             : 
     116             : };
     117             : 
     118             : } // namespace gfx
     119             : } // namespace mozilla
     120             : 
     121             : #endif /* MOZILLA_GFX_BASEPOINT_H_ */

Generated by: LCOV version 1.13