VPTissue Reference Manual
RegularTiling.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "RegularTiling.h"
21 
22 #include "tiles/Tile.h"
23 
24 #include <cassert>
25 #include <QPen>
26 
27 namespace SimPT_Editor {
28 
29 RegularTiling::RegularTiling(Tile *tile, const QRectF &rect, QGraphicsItem *parent)
30  : QGraphicsItemGroup(parent), m_tiles({ { tile } })
31 {
32  tile->setParentItem(this);
33 
34  SetRectangle(rect);
35 
36  setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
37 }
38 
40 {
41 }
42 
43 void RegularTiling::SetRectangle(const QRectF &rect)
44 {
45  m_parent_rect = rect;
46  m_rect = mapRectFromParent(m_parent_rect);
47  ResizeTiles();
48 }
49 
50 std::list<QPolygonF> RegularTiling::GetPolygons() const
51 {
52  std::list<QPolygonF> polygons;
53  for (auto row : m_tiles) {
54  for (Tile* tile : row) {
55  polygons.push_back(mapToParent(tile->GetPolygon()));
56  }
57  }
58  return polygons;
59 }
60 
61 QVariant RegularTiling::itemChange(GraphicsItemChange change, const QVariant &value)
62 {
63  if (change == QGraphicsItem::ItemTransformHasChanged || change == QGraphicsItem::ItemRotationHasChanged) {
64  m_rect = mapRectFromParent(m_parent_rect);
65  ResizeTiles();
66  }
67 
68  return QGraphicsItem::itemChange(change, value);
69 }
70 
71 void RegularTiling::ResizeTileRow(std::list<Tile*> &row)
72 {
73  auto tile_rect = [this] (Tile *tile) -> QRectF { return mapRectFromItem(tile, tile->GetPolygon().boundingRect()); };
74 
75  // Extend left and right side (first extend, we don't want to lose all our tiles of this row!)
76  while (tile_rect(row.front()).right() > m_rect.left()) {
77  row.push_front(row.front()->Left());
78  }
79  while(tile_rect(row.back()).left() < m_rect.right()) {
80  row.push_back(row.back()->Right());
81  }
82 
83  // Shrink left and right side
84  while(tile_rect(row.front()).right() < m_rect.left()) {
85  delete row.front();
86  row.pop_front();
87  }
88  while (tile_rect(row.back()).left() > m_rect.right()) {
89  delete row.back();
90  row.pop_back();
91  }
92 
93  assert( !row.empty() && "Number of tiles on a row should stay greater or equal to 1");
94 }
95 
96 void RegularTiling::ResizeTiles()
97 {
98  auto tile_rect = [this] (Tile *tile) -> QRectF { return mapRectFromItem(tile, tile->GetPolygon().boundingRect()); };
99  auto row_rect = [this, tile_rect] (const std::list<Tile*> &row) -> QRectF { QRectF rect = tile_rect(row.front()); for (Tile *tile : row) rect.united(tile_rect(tile)); return rect; };
100  auto delete_row = [] (Tile *tile) -> bool { delete tile; return true; };
101 
102  for (auto &row : m_tiles) {
103  ResizeTileRow(row);
104  }
105 
106  // Extend up and down (first extend, we don't want to lose all our rows!)
107  while (row_rect(m_tiles.front()).bottom() > m_rect.top()) {
108  m_tiles.push_front({ m_tiles.front().front()->Up() });
109  ResizeTileRow(m_tiles.front());
110  }
111  while(row_rect(m_tiles.back()).top() < m_rect.bottom()) {
112  m_tiles.push_back({ m_tiles.back().front()->Down() });
113  ResizeTileRow(m_tiles.back());
114  }
115 
116  // Shrink up and down direction
117  while(row_rect(m_tiles.front()).bottom() < m_rect.top()) {
118  m_tiles.front().remove_if(delete_row);
119  m_tiles.pop_front();
120  }
121  while (row_rect(m_tiles.back()).top() > m_rect.bottom()) {
122  m_tiles.back().remove_if(delete_row);
123  m_tiles.pop_back();
124  }
125 
126  assert( !m_tiles.empty() && "Number of tile rows should stay greater or equal to 1");
127 }
128 
129 } // namespace
Interface for Tile.
RegularTiling(Tile *seed, const QRectF &rect, QGraphicsItem *parent=nullptr)
Constructor.
Namespace for SimPT tissue editor package.
Definition: Cell.h:32
void SetRectangle(const QRectF &rect)
The rectangle the tiling should overlay.
Interface for RegularTiling.
std::list< QPolygonF > GetPolygons() const
Get the polygons associated with this tiling.
see the online Qt documentation
Abstract base class for cell pattern tiles.
Definition: Tile.h:29
virtual ~RegularTiling()
Destructor.
see the online Qt documentation