1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 GFX_POINT_H
7 #define GFX_POINT_H
8 
9 #include "nsMathUtils.h"
10 #include "mozilla/gfx/BaseSize.h"
11 #include "mozilla/gfx/BasePoint.h"
12 #include "mozilla/gfx/Matrix.h"
13 #include "nsSize.h"
14 #include "nsPoint.h"
15 
16 #include "gfxTypes.h"
17 
18 struct gfxSize : public mozilla::gfx::BaseSize<gfxFloat, gfxSize> {
19     typedef mozilla::gfx::BaseSize<gfxFloat, gfxSize> Super;
20 
gfxSizegfxSize21     gfxSize() : Super() {}
gfxSizegfxSize22     gfxSize(gfxFloat aWidth, gfxFloat aHeight) : Super(aWidth, aHeight) {}
gfxSizegfxSize23     MOZ_IMPLICIT gfxSize(const mozilla::gfx::IntSize& aSize) : Super(aSize.width, aSize.height) {}
24 };
25 
26 struct gfxPoint : public mozilla::gfx::BasePoint<gfxFloat, gfxPoint> {
27     typedef mozilla::gfx::BasePoint<gfxFloat, gfxPoint> Super;
28 
gfxPointgfxPoint29     gfxPoint() : Super() {}
gfxPointgfxPoint30     gfxPoint(gfxFloat aX, gfxFloat aY) : Super(aX, aY) {}
gfxPointgfxPoint31     MOZ_IMPLICIT gfxPoint(const nsIntPoint& aPoint) : Super(aPoint.x, aPoint.y) {}
32 
WithinEpsilonOfgfxPoint33     bool WithinEpsilonOf(const gfxPoint& aPoint, gfxFloat aEpsilon) {
34         return fabs(aPoint.x - x) < aEpsilon && fabs(aPoint.y - y) < aEpsilon;
35     }
36 
TransformgfxPoint37     void Transform(const mozilla::gfx::Matrix4x4 &aMatrix)
38     {
39       // Transform this point with aMatrix
40       double px = x;
41       double py = y;
42 
43       x = px * aMatrix._11 + py * aMatrix._21 + aMatrix._41;
44       y = px * aMatrix._12 + py * aMatrix._22 + aMatrix._42;
45 
46       double w = px * aMatrix._14 + py * aMatrix._24 + aMatrix._44;
47       x /= w;
48       y /= w;
49     }
50 };
51 
52 inline gfxPoint
53 operator*(const gfxPoint& aPoint, const gfxSize& aSize)
54 {
55   return gfxPoint(aPoint.x * aSize.width, aPoint.y * aSize.height);
56 }
57 
58 inline gfxPoint
59 operator/(const gfxPoint& aPoint, const gfxSize& aSize)
60 {
61   return gfxPoint(aPoint.x / aSize.width, aPoint.y / aSize.height);
62 }
63 
64 inline gfxSize
65 operator/(gfxFloat aValue, const gfxSize& aSize)
66 {
67   return gfxSize(aValue / aSize.width, aValue / aSize.height);
68 }
69 
70 #endif /* GFX_POINT_H */
71