summaryrefslogtreecommitdiffstats
path: root/ui/gfx/transform.cc
blob: 53981450964b38141f29b06aabc1747ec4147189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/transform.h"
#include "ui/gfx/point3.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/skia_util.h"

namespace {

static int SymmetricRound(float x) {
  return static_cast<int>(
    x > 0
      ? std::floor(x + 0.5f)
      : std::ceil(x - 0.5f));
}

} // namespace

namespace ui {

Transform::Transform() {
  matrix_.reset();
}

Transform::~Transform() {}

bool Transform::operator==(const Transform& rhs) const {
  return matrix_ == rhs.matrix_;
}

bool Transform::operator!=(const Transform& rhs) const {
  return !(*this == rhs);
}

void Transform::SetRotate(float degree) {
  matrix_.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree));
}

void Transform::SetScaleX(float x) {
  matrix_.set(0, 0, SkFloatToScalar(x));
}

void Transform::SetScaleY(float y) {
  matrix_.set(1, 1, SkFloatToScalar(y));
}

void Transform::SetScale(float x, float y) {
  matrix_.setScale(
    SkFloatToScalar(x),
    SkFloatToScalar(y),
    matrix_.get(2, 2));
}

void Transform::SetTranslateX(float x) {
  matrix_.set(0, 3, SkFloatToScalar(x));
}

void Transform::SetTranslateY(float y) {
  matrix_.set(1, 3, SkFloatToScalar(y));
}

void Transform::SetTranslate(float x, float y) {
  matrix_.setTranslate(
    SkFloatToScalar(x),
    SkFloatToScalar(y),
    matrix_.get(2, 3));
}

void Transform::ConcatRotate(float degree) {
  SkMatrix44 rot;
  rot.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree));
  matrix_.postConcat(rot);
}

void Transform::ConcatScale(float x, float y) {
  SkMatrix44 scale;
  scale.setScale(SkFloatToScalar(x), SkFloatToScalar(y), 1);
  matrix_.postConcat(scale);
}

void Transform::ConcatTranslate(float x, float y) {
  SkMatrix44 translate;
  translate.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y), 0);
  matrix_.postConcat(translate);
}

void Transform::PreconcatTransform(const Transform& transform) {
  if (!transform.matrix_.isIdentity()) {
    matrix_.preConcat(transform.matrix_);
  }
}

void Transform::ConcatTransform(const Transform& transform) {
  if (!transform.matrix_.isIdentity()) {
    matrix_.postConcat(transform.matrix_);
  }
}

bool Transform::HasChange() const {
  return !matrix_.isIdentity();
}

void Transform::TransformPoint(gfx::Point& point) const {
  TransformPointInternal(matrix_, point);
}

void Transform::TransformPoint(gfx::Point3f& point) const {
  TransformPointInternal(matrix_, point);
}

bool Transform::TransformPointReverse(gfx::Point& point) const {
  // TODO(sad): Try to avoid trying to invert the matrix.
  SkMatrix44 inverse;
  if (!matrix_.invert(&inverse))
    return false;

  TransformPointInternal(inverse, point);
  return true;
}

bool Transform::TransformPointReverse(gfx::Point3f& point) const {
  // TODO(sad): Try to avoid trying to invert the matrix.
  SkMatrix44 inverse;
  if (!matrix_.invert(&inverse))
    return false;

  TransformPointInternal(inverse, point);
  return true;
}

void Transform::TransformRect(gfx::Rect* rect) const {
  SkRect src = gfx::RectToSkRect(*rect);
  const SkMatrix& matrix = matrix_;
  matrix.mapRect(&src);
  *rect = gfx::SkRectToRect(src);
}

bool Transform::TransformRectReverse(gfx::Rect* rect) const {
  SkMatrix44 inverse;
  if (!matrix_.invert(&inverse))
    return false;
  const SkMatrix& matrix = inverse;
  SkRect src = gfx::RectToSkRect(*rect);
  matrix.mapRect(&src);
  *rect = gfx::SkRectToRect(src);
  return true;
}

void Transform::TransformPointInternal(const SkMatrix44& xform,
                                       gfx::Point3f& point) const {
  SkScalar p[4] = {
    SkFloatToScalar(point.x()),
    SkFloatToScalar(point.y()),
    SkFloatToScalar(point.z()),
    1 };

  xform.map(p);

  if (p[3] != 1 && abs(p[3]) > 0) {
    point.SetPoint(p[0] / p[3], p[1] / p[3], p[2]/ p[3]);
  } else {
    point.SetPoint(p[0], p[1], p[2]);
  }
}

void Transform::TransformPointInternal(const SkMatrix44& xform,
                                       gfx::Point& point) const {
  SkScalar p[4] = {
    SkIntToScalar(point.x()),
    SkIntToScalar(point.y()),
    0,
    1 };

  xform.map(p);

  point.SetPoint(SymmetricRound(p[0]),
                 SymmetricRound(p[1]));
}

}  // namespace ui