7573ec30574c6953b73ee7170856687e9f802feb
[platform/framework/web/crosswalk.git] / src / cc / base / tiling_data.cc
1 // Copyright 2010 The Chromium 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 "cc/base/tiling_data.h"
6
7 #include <algorithm>
8
9 #include "ui/gfx/rect.h"
10 #include "ui/gfx/vector2d.h"
11
12 namespace cc {
13
14 static int ComputeNumTiles(int max_texture_size,
15                            int total_size,
16                            int border_texels) {
17   if (max_texture_size - 2 * border_texels <= 0)
18     return total_size > 0 && max_texture_size >= total_size ? 1 : 0;
19
20   int num_tiles = std::max(1,
21                            1 + (total_size - 1 - 2 * border_texels) /
22                            (max_texture_size - 2 * border_texels));
23   return total_size > 0 ? num_tiles : 0;
24 }
25
26 TilingData::TilingData()
27     : border_texels_(0) {
28   RecomputeNumTiles();
29 }
30
31 TilingData::TilingData(
32     const gfx::Size& max_texture_size,
33     const gfx::Size& total_size,
34     bool has_border_texels)
35     : max_texture_size_(max_texture_size),
36       total_size_(total_size),
37       border_texels_(has_border_texels ? 1 : 0) {
38   RecomputeNumTiles();
39 }
40
41 TilingData::TilingData(
42     const gfx::Size& max_texture_size,
43     const gfx::Size& total_size,
44     int border_texels)
45     : max_texture_size_(max_texture_size),
46       total_size_(total_size),
47       border_texels_(border_texels) {
48   RecomputeNumTiles();
49 }
50
51 void TilingData::SetTotalSize(const gfx::Size& total_size) {
52   total_size_ = total_size;
53   RecomputeNumTiles();
54 }
55
56 void TilingData::SetMaxTextureSize(const gfx::Size& max_texture_size) {
57   max_texture_size_ = max_texture_size;
58   RecomputeNumTiles();
59 }
60
61 void TilingData::SetHasBorderTexels(bool has_border_texels) {
62   border_texels_ = has_border_texels ? 1 : 0;
63   RecomputeNumTiles();
64 }
65
66 void TilingData::SetBorderTexels(int border_texels) {
67   border_texels_ = border_texels;
68   RecomputeNumTiles();
69 }
70
71 int TilingData::TileXIndexFromSrcCoord(int src_position) const {
72   if (num_tiles_x_ <= 1)
73     return 0;
74
75   DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
76   int x = (src_position - border_texels_) /
77       (max_texture_size_.width() - 2 * border_texels_);
78   return std::min(std::max(x, 0), num_tiles_x_ - 1);
79 }
80
81 int TilingData::TileYIndexFromSrcCoord(int src_position) const {
82   if (num_tiles_y_ <= 1)
83     return 0;
84
85   DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
86   int y = (src_position - border_texels_) /
87       (max_texture_size_.height() - 2 * border_texels_);
88   return std::min(std::max(y, 0), num_tiles_y_ - 1);
89 }
90
91 int TilingData::FirstBorderTileXIndexFromSrcCoord(int src_position) const {
92   if (num_tiles_x_ <= 1)
93     return 0;
94
95   DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
96   int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
97   int x = (src_position - 2 * border_texels_) / inner_tile_size;
98   return std::min(std::max(x, 0), num_tiles_x_ - 1);
99 }
100
101 int TilingData::FirstBorderTileYIndexFromSrcCoord(int src_position) const {
102   if (num_tiles_y_ <= 1)
103     return 0;
104
105   DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
106   int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
107   int y = (src_position - 2 * border_texels_) / inner_tile_size;
108   return std::min(std::max(y, 0), num_tiles_y_ - 1);
109 }
110
111 int TilingData::LastBorderTileXIndexFromSrcCoord(int src_position) const {
112   if (num_tiles_x_ <= 1)
113     return 0;
114
115   DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
116   int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
117   int x = src_position / inner_tile_size;
118   return std::min(std::max(x, 0), num_tiles_x_ - 1);
119 }
120
121 int TilingData::LastBorderTileYIndexFromSrcCoord(int src_position) const {
122   if (num_tiles_y_ <= 1)
123     return 0;
124
125   DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
126   int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
127   int y = src_position / inner_tile_size;
128   return std::min(std::max(y, 0), num_tiles_y_ - 1);
129 }
130
131 gfx::Rect TilingData::TileBounds(int i, int j) const {
132   AssertTile(i, j);
133   int max_texture_size_x = max_texture_size_.width() - 2 * border_texels_;
134   int max_texture_size_y = max_texture_size_.height() - 2 * border_texels_;
135   int total_size_x = total_size_.width();
136   int total_size_y = total_size_.height();
137
138   int lo_x = max_texture_size_x * i;
139   if (i != 0)
140     lo_x += border_texels_;
141
142   int lo_y = max_texture_size_y * j;
143   if (j != 0)
144     lo_y += border_texels_;
145
146   int hi_x = max_texture_size_x * (i + 1) + border_texels_;
147   if (i + 1 == num_tiles_x_)
148     hi_x += border_texels_;
149
150   int hi_y = max_texture_size_y * (j + 1) + border_texels_;
151   if (j + 1 == num_tiles_y_)
152     hi_y += border_texels_;
153
154   hi_x = std::min(hi_x, total_size_x);
155   hi_y = std::min(hi_y, total_size_y);
156
157   int x = lo_x;
158   int y = lo_y;
159   int width = hi_x - lo_x;
160   int height = hi_y - lo_y;
161   DCHECK_GE(x, 0);
162   DCHECK_GE(y, 0);
163   DCHECK_GE(width, 0);
164   DCHECK_GE(height, 0);
165   DCHECK_LE(x, total_size_.width());
166   DCHECK_LE(y, total_size_.height());
167   return gfx::Rect(x, y, width, height);
168 }
169
170 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const {
171   AssertTile(i, j);
172   int max_texture_size_x = max_texture_size_.width() - 2 * border_texels_;
173   int max_texture_size_y = max_texture_size_.height() - 2 * border_texels_;
174   int total_size_x = total_size_.width();
175   int total_size_y = total_size_.height();
176
177   int lo_x = max_texture_size_x * i;
178   int lo_y = max_texture_size_y * j;
179
180   int hi_x = lo_x + max_texture_size_x + 2 * border_texels_;
181   int hi_y = lo_y + max_texture_size_y + 2 * border_texels_;
182
183   hi_x = std::min(hi_x, total_size_x);
184   hi_y = std::min(hi_y, total_size_y);
185
186   int x = lo_x;
187   int y = lo_y;
188   int width = hi_x - lo_x;
189   int height = hi_y - lo_y;
190   DCHECK_GE(x, 0);
191   DCHECK_GE(y, 0);
192   DCHECK_GE(width, 0);
193   DCHECK_GE(height, 0);
194   DCHECK_LE(x, total_size_.width());
195   DCHECK_LE(y, total_size_.height());
196   return gfx::Rect(x, y, width, height);
197 }
198
199 int TilingData::TilePositionX(int x_index) const {
200   DCHECK_GE(x_index, 0);
201   DCHECK_LT(x_index, num_tiles_x_);
202
203   int pos = (max_texture_size_.width() - 2 * border_texels_) * x_index;
204   if (x_index != 0)
205     pos += border_texels_;
206
207   return pos;
208 }
209
210 int TilingData::TilePositionY(int y_index) const {
211   DCHECK_GE(y_index, 0);
212   DCHECK_LT(y_index, num_tiles_y_);
213
214   int pos = (max_texture_size_.height() - 2 * border_texels_) * y_index;
215   if (y_index != 0)
216     pos += border_texels_;
217
218   return pos;
219 }
220
221 int TilingData::TileSizeX(int x_index) const {
222   DCHECK_GE(x_index, 0);
223   DCHECK_LT(x_index, num_tiles_x_);
224
225   if (!x_index && num_tiles_x_ == 1)
226     return total_size_.width();
227   if (!x_index && num_tiles_x_ > 1)
228     return max_texture_size_.width() - border_texels_;
229   if (x_index < num_tiles_x_ - 1)
230     return max_texture_size_.width() - 2 * border_texels_;
231   if (x_index == num_tiles_x_ - 1)
232     return total_size_.width() - TilePositionX(x_index);
233
234   NOTREACHED();
235   return 0;
236 }
237
238 int TilingData::TileSizeY(int y_index) const {
239   DCHECK_GE(y_index, 0);
240   DCHECK_LT(y_index, num_tiles_y_);
241
242   if (!y_index && num_tiles_y_ == 1)
243     return total_size_.height();
244   if (!y_index && num_tiles_y_ > 1)
245     return max_texture_size_.height() - border_texels_;
246   if (y_index < num_tiles_y_ - 1)
247     return max_texture_size_.height() - 2 * border_texels_;
248   if (y_index == num_tiles_y_ - 1)
249     return total_size_.height() - TilePositionY(y_index);
250
251   NOTREACHED();
252   return 0;
253 }
254
255 gfx::Vector2d TilingData::TextureOffset(int x_index, int y_index) const {
256   int left = (!x_index || num_tiles_x_ == 1) ? 0 : border_texels_;
257   int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_;
258
259   return gfx::Vector2d(left, top);
260 }
261
262 void TilingData::RecomputeNumTiles() {
263   num_tiles_x_ = ComputeNumTiles(
264       max_texture_size_.width(), total_size_.width(), border_texels_);
265   num_tiles_y_ = ComputeNumTiles(
266       max_texture_size_.height(), total_size_.height(), border_texels_);
267 }
268
269 TilingData::BaseIterator::BaseIterator(const TilingData* tiling_data)
270     : tiling_data_(tiling_data),
271       index_x_(-1),
272       index_y_(-1) {
273 }
274
275 TilingData::Iterator::Iterator() : BaseIterator(NULL) { done(); }
276
277 TilingData::Iterator::Iterator(const TilingData* tiling_data,
278                                const gfx::Rect& tiling_rect,
279                                bool include_borders)
280     : BaseIterator(tiling_data), left_(-1), right_(-1), bottom_(-1) {
281   if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
282     done();
283     return;
284   }
285
286   gfx::Rect rect(tiling_rect);
287   rect.Intersect(gfx::Rect(tiling_data_->total_size()));
288
289   gfx::Rect top_left_tile;
290   if (include_borders) {
291     index_x_ = tiling_data_->FirstBorderTileXIndexFromSrcCoord(rect.x());
292     index_y_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(rect.y());
293     right_ = tiling_data_->LastBorderTileXIndexFromSrcCoord(rect.right() - 1);
294     bottom_ = tiling_data_->LastBorderTileYIndexFromSrcCoord(rect.bottom() - 1);
295     top_left_tile = tiling_data_->TileBoundsWithBorder(index_x_, index_y_);
296   } else {
297     index_x_ = tiling_data_->TileXIndexFromSrcCoord(rect.x());
298     index_y_ = tiling_data_->TileYIndexFromSrcCoord(rect.y());
299     right_ = tiling_data_->TileXIndexFromSrcCoord(rect.right() - 1);
300     bottom_ = tiling_data_->TileYIndexFromSrcCoord(rect.bottom() - 1);
301     top_left_tile = tiling_data_->TileBounds(index_x_, index_y_);
302   }
303   left_ = index_x_;
304
305   // Index functions always return valid indices, so explicitly check
306   // for non-intersecting rects.
307   if (!top_left_tile.Intersects(rect))
308     done();
309 }
310
311 TilingData::Iterator& TilingData::Iterator::operator++() {
312   if (!*this)
313     return *this;
314
315   index_x_++;
316   if (index_x_ > right_) {
317     index_x_ = left_;
318     index_y_++;
319     if (index_y_ > bottom_)
320       done();
321   }
322
323   return *this;
324 }
325
326 TilingData::DifferenceIterator::DifferenceIterator(
327     const TilingData* tiling_data,
328     const gfx::Rect& consider_rect,
329     const gfx::Rect& ignore_rect)
330     : BaseIterator(tiling_data),
331       consider_left_(-1),
332       consider_top_(-1),
333       consider_right_(-1),
334       consider_bottom_(-1),
335       ignore_left_(-1),
336       ignore_top_(-1),
337       ignore_right_(-1),
338       ignore_bottom_(-1) {
339   if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
340     done();
341     return;
342   }
343
344   gfx::Rect bounds(tiling_data_->total_size());
345   gfx::Rect consider(consider_rect);
346   gfx::Rect ignore(ignore_rect);
347   consider.Intersect(bounds);
348   ignore.Intersect(bounds);
349   if (consider.IsEmpty()) {
350     done();
351     return;
352   }
353
354   consider_left_ =
355       tiling_data_->FirstBorderTileXIndexFromSrcCoord(consider.x());
356   consider_top_ =
357       tiling_data_->FirstBorderTileYIndexFromSrcCoord(consider.y());
358   consider_right_ =
359       tiling_data_->LastBorderTileXIndexFromSrcCoord(consider.right() - 1);
360   consider_bottom_ =
361       tiling_data_->LastBorderTileYIndexFromSrcCoord(consider.bottom() - 1);
362
363   if (!ignore.IsEmpty()) {
364     ignore_left_ =
365         tiling_data_->FirstBorderTileXIndexFromSrcCoord(ignore.x());
366     ignore_top_ =
367         tiling_data_->FirstBorderTileYIndexFromSrcCoord(ignore.y());
368     ignore_right_ =
369         tiling_data_->LastBorderTileXIndexFromSrcCoord(ignore.right() - 1);
370     ignore_bottom_ =
371         tiling_data_->LastBorderTileYIndexFromSrcCoord(ignore.bottom() - 1);
372
373     // Clamp ignore indices to consider indices.
374     ignore_left_ = std::max(ignore_left_, consider_left_);
375     ignore_top_ = std::max(ignore_top_, consider_top_);
376     ignore_right_ = std::min(ignore_right_, consider_right_);
377     ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
378   }
379
380   if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
381       ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
382     done();
383     return;
384   }
385
386   index_x_ = consider_left_;
387   index_y_ = consider_top_;
388
389   if (in_ignore_rect())
390     ++(*this);
391 }
392
393 TilingData::DifferenceIterator& TilingData::DifferenceIterator::operator++() {
394   if (!*this)
395     return *this;
396
397   index_x_++;
398   if (in_ignore_rect())
399     index_x_ = ignore_right_ + 1;
400
401   if (index_x_ > consider_right_) {
402     index_x_ = consider_left_;
403     index_y_++;
404
405     if (in_ignore_rect()) {
406       index_x_ = ignore_right_ + 1;
407       // If the ignore rect spans the whole consider rect horizontally, then
408       // ignore_right + 1 will be out of bounds.
409       if (in_ignore_rect() || index_x_ > consider_right_) {
410         index_y_ = ignore_bottom_ + 1;
411         index_x_ = consider_left_;
412       }
413     }
414
415     if (index_y_ > consider_bottom_)
416       done();
417   }
418
419   return *this;
420 }
421
422 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator()
423     : BaseIterator(NULL) {
424   done();
425 }
426
427 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator(
428     const TilingData* tiling_data,
429     const gfx::Rect& consider_rect,
430     const gfx::Rect& ignore_rect,
431     const gfx::Rect& center_rect)
432     : BaseIterator(tiling_data),
433       consider_left_(-1),
434       consider_top_(-1),
435       consider_right_(-1),
436       consider_bottom_(-1),
437       ignore_left_(-1),
438       ignore_top_(-1),
439       ignore_right_(-1),
440       ignore_bottom_(-1),
441       direction_(RIGHT),
442       delta_x_(1),
443       delta_y_(0),
444       current_step_(0),
445       horizontal_step_count_(0),
446       vertical_step_count_(0) {
447   if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
448     done();
449     return;
450   }
451
452   gfx::Rect bounds(tiling_data_->total_size());
453   gfx::Rect consider(consider_rect);
454   gfx::Rect ignore(ignore_rect);
455   gfx::Rect center(center_rect);
456   consider.Intersect(bounds);
457   ignore.Intersect(bounds);
458   if (consider.IsEmpty()) {
459     done();
460     return;
461   }
462
463   consider_left_ =
464       tiling_data_->FirstBorderTileXIndexFromSrcCoord(consider.x());
465   consider_top_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(consider.y());
466   consider_right_ =
467       tiling_data_->LastBorderTileXIndexFromSrcCoord(consider.right() - 1);
468   consider_bottom_ =
469       tiling_data_->LastBorderTileYIndexFromSrcCoord(consider.bottom() - 1);
470
471   if (!ignore.IsEmpty()) {
472     ignore_left_ = tiling_data_->FirstBorderTileXIndexFromSrcCoord(ignore.x());
473     ignore_top_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(ignore.y());
474     ignore_right_ =
475         tiling_data_->LastBorderTileXIndexFromSrcCoord(ignore.right() - 1);
476     ignore_bottom_ =
477         tiling_data_->LastBorderTileYIndexFromSrcCoord(ignore.bottom() - 1);
478
479     // Clamp ignore indices to consider indices.
480     ignore_left_ = std::max(ignore_left_, consider_left_);
481     ignore_top_ = std::max(ignore_top_, consider_top_);
482     ignore_right_ = std::min(ignore_right_, consider_right_);
483     ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
484   }
485
486   if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
487       ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
488     done();
489     return;
490   }
491
492   // Determine around left, such that it is between -1 and num_tiles_x.
493   int around_left = 0;
494   if (center.x() < 0 || center.IsEmpty())
495     around_left = -1;
496   else if (center.x() > tiling_data->total_size().width())
497     around_left = tiling_data->num_tiles_x();
498   else
499     around_left = tiling_data->FirstBorderTileXIndexFromSrcCoord(center.x());
500
501   // Determine around top, such that it is between -1 and num_tiles_y.
502   int around_top = 0;
503   if (center.y() < 0 || center.IsEmpty())
504     around_top = -1;
505   else if (center.y() > tiling_data->total_size().height())
506     around_top = tiling_data->num_tiles_y();
507   else
508     around_top = tiling_data->FirstBorderTileYIndexFromSrcCoord(center.y());
509
510   // Determine around right, such that it is between -1 and num_tiles_x.
511   int right_src_coord = center.right() - 1;
512   int around_right = 0;
513   if (right_src_coord < 0 || center.IsEmpty()) {
514     around_right = -1;
515   } else if (right_src_coord > tiling_data->total_size().width()) {
516     around_right = tiling_data->num_tiles_x();
517   } else {
518     around_right =
519         tiling_data->LastBorderTileXIndexFromSrcCoord(right_src_coord);
520   }
521
522   // Determine around bottom, such that it is between -1 and num_tiles_y.
523   int bottom_src_coord = center.bottom() - 1;
524   int around_bottom = 0;
525   if (bottom_src_coord < 0 || center.IsEmpty()) {
526     around_bottom = -1;
527   } else if (bottom_src_coord > tiling_data->total_size().height()) {
528     around_bottom = tiling_data->num_tiles_y();
529   } else {
530     around_bottom =
531         tiling_data->LastBorderTileYIndexFromSrcCoord(bottom_src_coord);
532   }
533
534   vertical_step_count_ = around_bottom - around_top + 1;
535   horizontal_step_count_ = around_right - around_left + 1;
536   current_step_ = horizontal_step_count_ - 1;
537
538   index_x_ = around_right;
539   index_y_ = around_bottom;
540
541   // The current index is the bottom right of the around rect, which is also
542   // ignored. So we have to advance.
543   ++(*this);
544 }
545
546 TilingData::SpiralDifferenceIterator& TilingData::SpiralDifferenceIterator::
547 operator++() {
548   int cannot_hit_consider_count = 0;
549   while (cannot_hit_consider_count < 4) {
550     if (needs_direction_switch())
551       switch_direction();
552
553     index_x_ += delta_x_;
554     index_y_ += delta_y_;
555     ++current_step_;
556
557     if (in_consider_rect()) {
558       cannot_hit_consider_count = 0;
559
560       if (!in_ignore_rect())
561         break;
562
563       // Steps needed to reach the very edge of the ignore rect, while remaining
564       // inside (so that the continue would take us outside).
565       int steps_to_edge = 0;
566       switch (direction_) {
567         case UP:
568           steps_to_edge = index_y_ - ignore_top_;
569           break;
570         case LEFT:
571           steps_to_edge = index_x_ - ignore_left_;
572           break;
573         case DOWN:
574           steps_to_edge = ignore_bottom_ - index_y_;
575           break;
576         case RIGHT:
577           steps_to_edge = ignore_right_ - index_x_;
578           break;
579       }
580
581       // We need to switch directions in |max_steps|.
582       int max_steps = current_step_count() - current_step_;
583
584       int steps_to_take = std::min(steps_to_edge, max_steps);
585       DCHECK_GE(steps_to_take, 0);
586
587       index_x_ += steps_to_take * delta_x_;
588       index_y_ += steps_to_take * delta_y_;
589       current_step_ += steps_to_take;
590     } else {
591       int max_steps = current_step_count() - current_step_;
592       int steps_to_take = max_steps;
593       bool can_hit_consider_rect = false;
594       switch (direction_) {
595         case UP:
596           if (valid_column() && consider_bottom_ < index_y_)
597             steps_to_take = index_y_ - consider_bottom_ - 1;
598           can_hit_consider_rect |= consider_right_ >= index_x_;
599           break;
600         case LEFT:
601           if (valid_row() && consider_right_ < index_x_)
602             steps_to_take = index_x_ - consider_right_ - 1;
603           can_hit_consider_rect |= consider_top_ <= index_y_;
604           break;
605         case DOWN:
606           if (valid_column() && consider_top_ > index_y_)
607             steps_to_take = consider_top_ - index_y_ - 1;
608           can_hit_consider_rect |= consider_left_ <= index_x_;
609           break;
610         case RIGHT:
611           if (valid_row() && consider_left_ > index_x_)
612             steps_to_take = consider_left_ - index_x_ - 1;
613           can_hit_consider_rect |= consider_bottom_ >= index_y_;
614           break;
615       }
616       steps_to_take = std::min(steps_to_take, max_steps);
617       DCHECK_GE(steps_to_take, 0);
618
619       index_x_ += steps_to_take * delta_x_;
620       index_y_ += steps_to_take * delta_y_;
621       current_step_ += steps_to_take;
622
623       if (can_hit_consider_rect)
624         cannot_hit_consider_count = 0;
625       else
626         ++cannot_hit_consider_count;
627     }
628   }
629
630   if (cannot_hit_consider_count >= 4)
631     done();
632   return *this;
633 }
634
635 bool TilingData::SpiralDifferenceIterator::needs_direction_switch() const {
636   return current_step_ >= current_step_count();
637 }
638
639 void TilingData::SpiralDifferenceIterator::switch_direction() {
640   int new_delta_x_ = delta_y_;
641   delta_y_ = -delta_x_;
642   delta_x_ = new_delta_x_;
643
644   current_step_ = 0;
645   direction_ = static_cast<Direction>((direction_ + 1) % 4);
646
647   if (direction_ == RIGHT || direction_ == LEFT) {
648     ++vertical_step_count_;
649     ++horizontal_step_count_;
650   }
651 }
652
653 }  // namespace cc