File: object_t.cpp

package info (click to toggle)
openorienteering-mapper 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 67,132 kB
  • sloc: cpp: 114,710; ansic: 1,455; sh: 430; java: 240; xml: 140; sed: 64; makefile: 28
file content (193 lines) | stat: -rw-r--r-- 6,240 bytes parent folder | download
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
/*
 *    Copyright 2025 Matthias Kühlewein
 *    Copyright 2025 Kai Pastor
 *
 *    This file is part of OpenOrienteering.
 *
 *    OpenOrienteering is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    OpenOrienteering is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QtTest>
#include <QObject>

#include "core/map.h"
#include "core/map_coord.h"
#include "core/objects/object.h"
#include "core/objects/text_object.h"
#include "core/symbols/area_symbol.h"
#include "core/symbols/line_symbol.h"
#include "core/symbols/symbol.h"
#include "core/symbols/text_symbol.h"


using namespace OpenOrienteering;

Q_DECLARE_METATYPE(MapCoordVector)

/**
 * @test Tests object functions.
 */
class ObjectTest : public QObject
{
	Q_OBJECT
	
private slots:
	void objectLengthTest_data()
	{
		QTest::addColumn<int>("map_scale");
		QTest::addColumn<MapCoordVector>("coords");
		QTest::addColumn<float>("paper_length");
		QTest::addColumn<float>("paper_area");
		
		auto coords = MapCoordVector{
		    MapCoord{0.0, 0.0}, MapCoord{0.0, 5.0}, MapCoord{10.0, 5.0}, MapCoord{10.0, 0.0}, MapCoord{0.0, 0.0, MapCoord::ClosePoint}
		};
		
		QTest::newRow("Line object 1 at 1:10000") << 10000 << coords << 30.0f << 50.0f;
		QTest::newRow("Line object 1 at 1:15000") << 15000 << coords << 30.0f << 50.0f;
	}
	
	void objectLengthTest()
	{
		QFETCH(int, map_scale);
		QFETCH(MapCoordVector, coords);
		QFETCH(float, paper_length);
		QFETCH(float, paper_area);
		
		Map map;
		map.setScaleDenominator(map_scale);
		
		PathObject obj;
		QCOMPARE(obj.getPaperLength(), 0.0f);
		QCOMPARE(obj.calculatePaperArea(), 0.0f);
		QVERIFY(obj.isAreaTooSmall() == false);
		QVERIFY(obj.isLineTooShort() == false);
		
		auto line_symbol = new LineSymbol();
		map.addSymbol(line_symbol, 0);
		PathObject rectangle{line_symbol, coords, &map};
		rectangle.updatePathCoords();
		
		QCOMPARE(rectangle.getPaperLength(), paper_length);
		QCOMPARE(rectangle.getRealLength(), paper_length*map_scale/1000.0f);
		
		line_symbol->setMinimumLength(paper_length*1000 - 1);
		QVERIFY(rectangle.isLineTooShort() == false);
		
		line_symbol->setMinimumLength(paper_length*1000 + 1);
		QVERIFY(rectangle.isLineTooShort() == true);
		
		QCOMPARE(rectangle.calculatePaperArea(), paper_area);
		QCOMPARE(rectangle.calculateRealArea(), paper_area*(map_scale/1000.0f)*(map_scale/1000.0f));
	}
	
	
	void objectAreaTest_data()
	{
		QTest::addColumn<int>("map_scale");
		QTest::addColumn<MapCoordVector>("coords");
		QTest::addColumn<float>("paper_length");
		QTest::addColumn<float>("paper_area");
		
		auto coords1 = MapCoordVector{
		    MapCoord{0.0, 0.0}, MapCoord{0.0, 5.0}, MapCoord{10.0, 5.0}, MapCoord{10.0, 0.0}, MapCoord{0.0, 0.0, MapCoord::ClosePoint}
		};
		
		QTest::newRow("Area object 1 at 1:10000") << 10000 << coords1 << 30.0f << 50.0f;
		QTest::newRow("Area object 1 at 1:15000") << 15000 << coords1 << 30.0f << 50.0f;
		
		auto coords2 = MapCoordVector{
		    MapCoord{0.0, 0.0}, MapCoord{0.0, 5.0}, MapCoord{10.0, 5.0}, MapCoord{10.0, 0.0}, MapCoord{0.0, 0.0, MapCoord::ClosePoint|MapCoord::HolePoint},
		    MapCoord{5.0, 2.0}, MapCoord{5.0, 4.0}, MapCoord{8.0, 4.0}, MapCoord{8.0, 2.0}, MapCoord{5.0, 2.0, MapCoord::ClosePoint}
		};
		
		QTest::newRow("Area object 2 at 1:10000") << 10000 << coords2 << 30.0f << 44.0f;
	}
	
	void objectAreaTest()
	{
		QFETCH(int, map_scale);
		QFETCH(MapCoordVector, coords);
		QFETCH(float, paper_length);
		QFETCH(float, paper_area);
		
		Map map;
		map.setScaleDenominator(map_scale);
		
		PathObject obj;
		QCOMPARE(obj.getPaperLength(), 0.0f);
		QCOMPARE(obj.calculatePaperArea(), 0.0f);
		QVERIFY(obj.isAreaTooSmall() == false);
		QVERIFY(obj.isLineTooShort() == false);
		
		auto area_symbol = new AreaSymbol();
		map.addSymbol(area_symbol, 0);
		PathObject rectangle_area{area_symbol, coords, &map};
		rectangle_area.updatePathCoords();
		
		QCOMPARE(rectangle_area.getPaperLength(), paper_length);
		QCOMPARE(rectangle_area.getRealLength(), paper_length*map_scale/1000.0f);
		
		QCOMPARE(rectangle_area.calculatePaperArea(), paper_area);
		QCOMPARE(rectangle_area.calculateRealArea(), paper_area*(map_scale/1000.0f)*(map_scale/1000.0f));
		
		area_symbol->setMinimumArea(paper_area*1000 - 1);
		QVERIFY(rectangle_area.isAreaTooSmall() == false);
		
		area_symbol->setMinimumArea(paper_area*1000 + 1);
		QVERIFY(rectangle_area.isAreaTooSmall() == true);
	}
	
	void calcTextCoordTransformTest()
	{
		struct RotatableTextSymbol : public TextSymbol {
			RotatableTextSymbol() : TextSymbol() { setRotatable (true); }
		} text_symbol;
		QVERIFY(text_symbol.isRotatable());
		QVERIFY(text_symbol.calculateInternalScaling() > 0);
		
		TextObject object(&text_symbol);
		object.setText(QLatin1String("ABC"));
		object.setRotation(1.5);
		
		// The anchor on the map is 0,0 on the text, regardless of rotation.
		auto const anchor_map = QPointF{10.0, 10.0};
		auto const anchor_text = QPointF{0.0, 0.0};
		object.setAnchorPosition(MapCoordF(anchor_map));
		
		auto to_text = object.calcMapToTextTransform();
		QVERIFY(!to_text.isIdentity());
		
		auto to_map = object.calcTextToMapTransform();
		QVERIFY(!to_map.isIdentity());
		
		QCOMPARE(to_text.map(anchor_map), anchor_text);
		QCOMPARE(to_map.map(anchor_text), anchor_map);
		
		// Another point in the rotated text, and subject to rotation.
		auto const anchor_text_2 = QPointF{10.0, 5.0};
		QCOMPARE(to_text.map(to_map.map(anchor_text_2)), anchor_text_2);
	}
	
};  // class ObjectTest

/*
 * We don't need a real GUI window.
 */
namespace {
	auto const Q_DECL_UNUSED qpa_selected = qputenv("QT_QPA_PLATFORM", "minimal");  // clazy:exclude=non-pod-global-static
}

QTEST_MAIN(ObjectTest)
#include "object_t.moc"  // IWYU pragma: keep