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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2007-02-13
* Description : Layouting photos on a page
*
* SPDX-FileCopyrightText: 2007-2009 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
* SPDX-FileCopyrightText: 2006-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "atkinspagelayouttree.h"
// C++ includes
#include <cmath>
// Qt includes
#include <QList>
// Local includes
#include "atkinspagelayoutnode.h"
namespace DigikamGenericPrintCreatorPlugin
{
AtkinsPageLayoutTree::AtkinsPageLayoutTree(double aspectRatioPage,
double absoluteAreaPage)
: m_aspectRatioPage(aspectRatioPage),
m_absoluteAreaPage(absoluteAreaPage)
{
}
AtkinsPageLayoutTree::~AtkinsPageLayoutTree()
{
delete m_root;
}
AtkinsPageLayoutTree::AtkinsPageLayoutTree(const AtkinsPageLayoutTree& other)
{
(*this) = other;
}
AtkinsPageLayoutTree& AtkinsPageLayoutTree::operator=(const AtkinsPageLayoutTree& other)
{
if (this != &other)
{
delete m_root;
m_root = new AtkinsPageLayoutNode(*(other.m_root));
m_count = other.m_count;
m_aspectRatioPage = other.m_aspectRatioPage;
m_absoluteAreaPage = other.m_absoluteAreaPage;
}
return *this;
}
int AtkinsPageLayoutTree::addImage(double aspectRatio,
double relativeArea)
{
int index = m_count;
if (!m_root)
{
m_root = new AtkinsPageLayoutNode(aspectRatio,
relativeArea,
index);
m_count++;
return index;
}
// Section 2.1
AtkinsPageLayoutNode* bestTree = nullptr;
double highScore = 0;
for (int i = 0 ; i < m_count ; ++i)
{
for (int horizontal = 0 ; horizontal < 2 ; ++horizontal)
{
// create temporary tree
AtkinsPageLayoutNode* candidateTree = new AtkinsPageLayoutNode(*m_root);
// select the subtree which will be replace by a new internal node
AtkinsPageLayoutNode* const candidateSubtree = candidateTree->nodeForIndex(i);
// find parent node
AtkinsPageLayoutNode* const parentNode = candidateTree->parentOf(candidateSubtree);
// create new terminal node
AtkinsPageLayoutNode* const newTerminalNode = new AtkinsPageLayoutNode(aspectRatio,
relativeArea,
index);
// create new internal node
AtkinsPageLayoutNode* const newInternalNode = new AtkinsPageLayoutNode(candidateSubtree,
newTerminalNode,
horizontal,
index+1);
// replace in tree
if (parentNode)
{
// replace in tree
parentNode->takeAndSetChild(candidateSubtree, newInternalNode);
}
else
{
// candidateTree is candidateSubtree is root
candidateTree = newInternalNode;
}
// recompute sizes
candidateTree->computeRelativeSizes();
double candidateScore = score(candidateTree, m_count+2);
if (candidateScore > highScore)
{
highScore = candidateScore;
delete bestTree;
bestTree = candidateTree;
}
else
{
delete candidateTree;
}
}
}
delete m_root;
m_root = bestTree;
if (m_root)
{
m_root->computeDivisions();
}
m_count += 2;
return index;
}
/// Section 2.2.1
double AtkinsPageLayoutTree::score(AtkinsPageLayoutNode* const root,
int nodeCount)
{
if (!root)
{
return 0;
}
double areaSum = 0;
for (int i = 0 ; i < nodeCount ; ++i)
{
AtkinsPageLayoutNode* const node = root->nodeForIndex(i);<--- Variable 'node' can be declared as pointer to const
if (node->type() == AtkinsPageLayoutNode::TerminalNode)
{
areaSum += node->relativeArea();
}
}
double minRatioPage = root->aspectRatio() < m_aspectRatioPage ? root->aspectRatio()
: m_aspectRatioPage;
double maxRatioPage = root->aspectRatio() > m_aspectRatioPage ? root->aspectRatio()
: m_aspectRatioPage;
return (G() * (areaSum / root->relativeArea()) * (minRatioPage / maxRatioPage));
}
/// Section 2.2.2
double AtkinsPageLayoutTree::G() const
{
return (0.95 * 0.95);
}
/// Section 2.2.2
double AtkinsPageLayoutTree::absoluteArea(const AtkinsPageLayoutNode* const node)
{
// min(a_pbb, a_page), max(a_pbb, a_page)
double minRatioPage = m_root->aspectRatio() < m_aspectRatioPage ? m_root->aspectRatio()
: m_aspectRatioPage;
double maxRatioPage = m_root->aspectRatio() > m_aspectRatioPage ? m_root->aspectRatio()
: m_aspectRatioPage;
// A_pbb
double absoluteAreaRoot = m_absoluteAreaPage * minRatioPage / maxRatioPage;
if (node == m_root)
{
return absoluteAreaRoot;
}
// A_i
return (G() * node->relativeArea() / m_root->relativeArea() * absoluteAreaRoot);
}
QRectF AtkinsPageLayoutTree::drawingArea(int index, const QRectF& absoluteRectPage)
{
AtkinsPageLayoutNode* const node = m_root->nodeForIndex(index);
if (!node)
{
return QRectF();
}
// find out the "line of ancestry" of the node
QList<AtkinsPageLayoutNode*> treePath;
AtkinsPageLayoutNode* parent1 = node;
while (parent1)
{
treePath.prepend(parent1);
parent1 = m_root->parentOf(parent1);
}
// find out the rect of the page bounding box (the rect of the root node in the page rect)
QRectF absoluteRect = rectInRect(absoluteRectPage,
m_root->aspectRatio(),
absoluteArea(m_root));
// go along the line of ancestry and narrow down the bounding rectangle,
// as described in section 2.2.2
for (int i = 0 ; i < treePath.count() - 1 ; ++i)
{
AtkinsPageLayoutNode* const parent2 = treePath[i];<--- Variable 'parent2' can be declared as pointer to const
AtkinsPageLayoutNode* const child = treePath[i+1]; // only iterating to count-1<--- Variable 'child' can be declared as pointer to const
if (parent2->type() == AtkinsPageLayoutNode::VerticalDivision) // side by side
{
double leftWidth = absoluteRect.width() * parent2->division();
if (child == parent2->leftChild())
{
absoluteRect.setWidth(leftWidth);
}
else // right child
{
double rightWidth = absoluteRect.width() - leftWidth;
absoluteRect.setWidth(rightWidth);
absoluteRect.translate(leftWidth, 0);
}
}
else // horizontal division: one on top of the other
{
// left child is topmost
double upperHeight = absoluteRect.height() * parent2->division();
if (child == parent2->leftChild())
{
absoluteRect.setHeight(upperHeight);
}
else // right child
{
double lowerHeight = absoluteRect.height() - upperHeight;
absoluteRect.setHeight(lowerHeight);
absoluteRect.translate(0, upperHeight);
}
}
}
return rectInRect(absoluteRect, node->aspectRatio(), absoluteArea(node));
}
QRectF AtkinsPageLayoutTree::rectInRect(const QRectF &rect,
double aspectRatio,
double absoluteArea)
{
double width = std::sqrt(absoluteArea / aspectRatio);
double height = std::sqrt(absoluteArea * aspectRatio);
double x = rect.x() + (rect.width() - width) / 2;
double y = rect.y() + (rect.height() - height) / 2;
return QRectF(x, y, width, height);
}
} // Namespace DigikamGenericPrintCreatorPlugin
|