Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
transform.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "transform.h"
6
7#include "base/string_utils.h"
8
9namespace gfx {
10
12 : matrix_{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
13 is_identity_(true) {}
14
15Transform::Transform(float col1row1,
16 float col2row1,
17 float col3row1,
18 float col4row1,
19 float col1row2,
20 float col2row2,
21 float col3row2,
22 float col4row2,
23 float col1row3,
24 float col2row3,
25 float col3row3,
26 float col4row3,
27 float col1row4,
28 float col2row4,
29 float col3row4,
30 float col4row4)
31 : matrix_{col1row1, col2row1, col3row1, col4row1, col1row2, col2row2,
32 col3row2, col4row2, col1row3, col2row3, col3row3, col4row3,
33 col4row4, col2row4, col3row4, col4row4} {
34 UpdateIdentity();
35}
36
37Transform::Transform(float col1row1,
38 float col2row1,
39 float col1row2,
40 float col2row2,
41 float x_translation,
42 float y_translation)
43 : matrix_{col1row1,
44 col2row1,
45 x_translation,
46 0,
47 col1row2,
48 col2row2,
49 y_translation,
50 0,
51 0,
52 0,
53 1,
54 0,
55 0,
56 0,
57 0,
58 1} {
59 UpdateIdentity();
60}
61
62bool Transform::operator==(const Transform& rhs) const {
63 return matrix_[0] == rhs[0] && matrix_[1] == rhs[1] && matrix_[2] == rhs[2] &&
64 matrix_[3] == rhs[3] && matrix_[4] == rhs[4] && matrix_[5] == rhs[5] &&
65 matrix_[6] == rhs[6] && matrix_[7] == rhs[7] && matrix_[8] == rhs[8] &&
66 matrix_[9] == rhs[9] && matrix_[10] == rhs[10] &&
67 matrix_[11] == rhs[11] && matrix_[12] == rhs[12] &&
68 matrix_[13] == rhs[13] && matrix_[14] == rhs[14] &&
69 matrix_[15] == rhs[15];
70}
71
73 return is_identity_;
74}
75
76std::string Transform::ToString() const {
77 return base::StringPrintf(
78 "[ %+0.4f %+0.4f %+0.4f %+0.4f \n"
79 " %+0.4f %+0.4f %+0.4f %+0.4f \n"
80 " %+0.4f %+0.4f %+0.4f %+0.4f \n"
81 " %+0.4f %+0.4f %+0.4f %+0.4f ]\n",
82 matrix_[0], matrix_[1], matrix_[2], matrix_[3], matrix_[4], matrix_[5],
83 matrix_[6], matrix_[7], matrix_[8], matrix_[9], matrix_[10], matrix_[11],
84 matrix_[12], matrix_[13], matrix_[14], matrix_[15]);
85}
86
87void Transform::Scale(float x, float y) {
88 matrix_[0] *= x;
89 matrix_[5] *= y;
90 UpdateIdentity();
91}
92
94 if (IsIdentity())
95 return;
96 PointF origin = rect->origin();
97 PointF top_right = rect->top_right();
98 PointF bottom_left = rect->bottom_left();
99 TransformPoint(&origin);
100 TransformPoint(&top_right);
101 TransformPoint(&bottom_left);
102 rect->set_origin(origin);
103 rect->set_width(top_right.x() - origin.x());
104 rect->set_height(bottom_left.y() - origin.y());
105}
106
108 if (IsIdentity())
109 return;
110 float x = point->x();
111 float y = point->y();
112 point->SetPoint(x * matrix_[0] + y * matrix_[1] + matrix_[2],
113 x * matrix_[4] + y * matrix_[5] + matrix_[6]);
114 return;
115}
116
117void Transform::UpdateIdentity() {
118 for (size_t i = 0; i < 16; i++) {
119 if (i == 0 || i == 5 || i == 10 || i == 15) {
120 if (matrix_[i] != 1) {
121 is_identity_ = false;
122 return;
123 }
124 } else {
125 if (matrix_[i] != 0) {
126 is_identity_ = false;
127 return;
128 }
129 }
130 }
131 is_identity_ = true;
132}
133
134} // namespace gfx
constexpr float x() const
Definition point_f.h:27
void SetPoint(float x, float y)
Definition point_f.h:32
constexpr float y() const
Definition point_f.h:28
bool IsIdentity() const
Definition transform.cc:72
std::string ToString() const
Definition transform.cc:76
void TransformRect(RectF *rect) const
Definition transform.cc:93
void TransformPoint(PointF *point) const
Definition transform.cc:107
void Scale(float x, float y)
Definition transform.cc:87
bool operator==(const Transform &rhs) const
Definition transform.cc:62
double y
double x
std::string StringPrintf(const std::string &format, Args... args)
Definition insets.cc:10