gfx/2d/Point.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/Point.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_GFX_POINT_H_
    1.10 +#define MOZILLA_GFX_POINT_H_
    1.11 +
    1.12 +#include "mozilla/Attributes.h"
    1.13 +#include "Types.h"
    1.14 +#include "BasePoint.h"
    1.15 +#include "BasePoint3D.h"
    1.16 +#include "BasePoint4D.h"
    1.17 +#include "BaseSize.h"
    1.18 +
    1.19 +#include <cmath>
    1.20 +
    1.21 +namespace mozilla {
    1.22 +namespace gfx {
    1.23 +
    1.24 +// This should only be used by the typedefs below.
    1.25 +struct UnknownUnits {};
    1.26 +
    1.27 +template<class units>
    1.28 +struct IntPointTyped :
    1.29 +  public BasePoint< int32_t, IntPointTyped<units> >,
    1.30 +  public units {
    1.31 +  typedef BasePoint< int32_t, IntPointTyped<units> > Super;
    1.32 +
    1.33 +  MOZ_CONSTEXPR IntPointTyped() : Super() {}
    1.34 +  MOZ_CONSTEXPR IntPointTyped(int32_t aX, int32_t aY) : Super(aX, aY) {}
    1.35 +
    1.36 +  // XXX When all of the code is ported, the following functions to convert to and from
    1.37 +  // unknown types should be removed.
    1.38 +
    1.39 +  static IntPointTyped<units> FromUnknownPoint(const IntPointTyped<UnknownUnits>& aPoint) {
    1.40 +    return IntPointTyped<units>(aPoint.x, aPoint.y);
    1.41 +  }
    1.42 +
    1.43 +  IntPointTyped<UnknownUnits> ToUnknownPoint() const {
    1.44 +    return IntPointTyped<UnknownUnits>(this->x, this->y);
    1.45 +  }
    1.46 +};
    1.47 +typedef IntPointTyped<UnknownUnits> IntPoint;
    1.48 +
    1.49 +template<class units>
    1.50 +struct PointTyped :
    1.51 +  public BasePoint< Float, PointTyped<units> >,
    1.52 +  public units {
    1.53 +  typedef BasePoint< Float, PointTyped<units> > Super;
    1.54 +
    1.55 +  MOZ_CONSTEXPR PointTyped() : Super() {}
    1.56 +  MOZ_CONSTEXPR PointTyped(Float aX, Float aY) : Super(aX, aY) {}
    1.57 +  MOZ_CONSTEXPR PointTyped(const IntPointTyped<units>& point) : Super(float(point.x), float(point.y)) {}
    1.58 +
    1.59 +  // XXX When all of the code is ported, the following functions to convert to and from
    1.60 +  // unknown types should be removed.
    1.61 +
    1.62 +  static PointTyped<units> FromUnknownPoint(const PointTyped<UnknownUnits>& aPoint) {
    1.63 +    return PointTyped<units>(aPoint.x, aPoint.y);
    1.64 +  }
    1.65 +
    1.66 +  PointTyped<UnknownUnits> ToUnknownPoint() const {
    1.67 +    return PointTyped<UnknownUnits>(this->x, this->y);
    1.68 +  }
    1.69 +};
    1.70 +typedef PointTyped<UnknownUnits> Point;
    1.71 +
    1.72 +template<class units>
    1.73 +IntPointTyped<units> RoundedToInt(const PointTyped<units>& aPoint) {
    1.74 +  return IntPointTyped<units>(int32_t(floorf(aPoint.x + 0.5f)),
    1.75 +                              int32_t(floorf(aPoint.y + 0.5f)));
    1.76 +}
    1.77 +
    1.78 +template<class units>
    1.79 +struct Point3DTyped :
    1.80 +  public BasePoint3D< Float, Point3DTyped<units> > {
    1.81 +  typedef BasePoint3D< Float, Point3DTyped<units> > Super;
    1.82 +
    1.83 +  Point3DTyped() : Super() {}
    1.84 +  Point3DTyped(Float aX, Float aY, Float aZ) : Super(aX, aY, aZ) {}
    1.85 +
    1.86 +  // XXX When all of the code is ported, the following functions to convert to and from
    1.87 +  // unknown types should be removed.
    1.88 +
    1.89 +  static Point3DTyped<units> FromUnknownPoint(const Point3DTyped<UnknownUnits>& aPoint) {
    1.90 +    return Point3DTyped<units>(aPoint.x, aPoint.y, aPoint.z);
    1.91 +  }
    1.92 +
    1.93 +  Point3DTyped<UnknownUnits> ToUnknownPoint() const {
    1.94 +    return Point3DTyped<UnknownUnits>(this->x, this->y, this->z);
    1.95 +  }
    1.96 +};
    1.97 +typedef Point3DTyped<UnknownUnits> Point3D;
    1.98 +
    1.99 +template<class units>
   1.100 +struct Point4DTyped :
   1.101 +  public BasePoint4D< Float, Point4DTyped<units> > {
   1.102 +  typedef BasePoint4D< Float, Point4DTyped<units> > Super;
   1.103 +
   1.104 +  Point4DTyped() : Super() {}
   1.105 +  Point4DTyped(Float aX, Float aY, Float aZ, Float aW) : Super(aX, aY, aZ, aW) {}
   1.106 +
   1.107 +  // XXX When all of the code is ported, the following functions to convert to and from
   1.108 +  // unknown types should be removed.
   1.109 +
   1.110 +  static Point4DTyped<units> FromUnknownPoint(const Point4DTyped<UnknownUnits>& aPoint) {
   1.111 +    return Point4DTyped<units>(aPoint.x, aPoint.y, aPoint.z, aPoint.w);
   1.112 +  }
   1.113 +
   1.114 +  Point4DTyped<UnknownUnits> ToUnknownPoint() const {
   1.115 +    return Point4DTyped<UnknownUnits>(this->x, this->y, this->z, this->w);
   1.116 +  }
   1.117 +};
   1.118 +typedef Point4DTyped<UnknownUnits> Point4D;
   1.119 +
   1.120 +template<class units>
   1.121 +struct IntSizeTyped :
   1.122 +  public BaseSize< int32_t, IntSizeTyped<units> >,
   1.123 +  public units {
   1.124 +  typedef BaseSize< int32_t, IntSizeTyped<units> > Super;
   1.125 +
   1.126 +  MOZ_CONSTEXPR IntSizeTyped() : Super() {}
   1.127 +  MOZ_CONSTEXPR IntSizeTyped(int32_t aWidth, int32_t aHeight) : Super(aWidth, aHeight) {}
   1.128 +
   1.129 +  // XXX When all of the code is ported, the following functions to convert to and from
   1.130 +  // unknown types should be removed.
   1.131 +
   1.132 +  static IntSizeTyped<units> FromUnknownSize(const IntSizeTyped<UnknownUnits>& aSize) {
   1.133 +    return IntSizeTyped<units>(aSize.width, aSize.height);
   1.134 +  }
   1.135 +
   1.136 +  IntSizeTyped<UnknownUnits> ToUnknownSize() const {
   1.137 +    return IntSizeTyped<UnknownUnits>(this->width, this->height);
   1.138 +  }
   1.139 +};
   1.140 +typedef IntSizeTyped<UnknownUnits> IntSize;
   1.141 +
   1.142 +template<class units>
   1.143 +struct SizeTyped :
   1.144 +  public BaseSize< Float, SizeTyped<units> >,
   1.145 +  public units {
   1.146 +  typedef BaseSize< Float, SizeTyped<units> > Super;
   1.147 +
   1.148 +  MOZ_CONSTEXPR SizeTyped() : Super() {}
   1.149 +  MOZ_CONSTEXPR SizeTyped(Float aWidth, Float aHeight) : Super(aWidth, aHeight) {}
   1.150 +  explicit SizeTyped(const IntSizeTyped<units>& size) :
   1.151 +    Super(float(size.width), float(size.height)) {}
   1.152 +
   1.153 +  // XXX When all of the code is ported, the following functions to convert to and from
   1.154 +  // unknown types should be removed.
   1.155 +
   1.156 +  static SizeTyped<units> FromUnknownSize(const SizeTyped<UnknownUnits>& aSize) {
   1.157 +    return SizeTyped<units>(aSize.width, aSize.height);
   1.158 +  }
   1.159 +
   1.160 +  SizeTyped<UnknownUnits> ToUnknownSize() const {
   1.161 +    return SizeTyped<UnknownUnits>(this->width, this->height);
   1.162 +  }
   1.163 +};
   1.164 +typedef SizeTyped<UnknownUnits> Size;
   1.165 +
   1.166 +template<class units>
   1.167 +IntSizeTyped<units> RoundedToInt(const SizeTyped<units>& aSize) {
   1.168 +  return IntSizeTyped<units>(int32_t(floorf(aSize.width + 0.5f)),
   1.169 +                             int32_t(floorf(aSize.height + 0.5f)));
   1.170 +}
   1.171 +
   1.172 +}
   1.173 +}
   1.174 +
   1.175 +#endif /* MOZILLA_GFX_POINT_H_ */

mercurial