1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cppunit/TestAssert.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
24 #include <basegfx/polygon/b2dpolygon.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <basegfx/polygon/b2dpolypolygontools.hxx>
27 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
28 #include <basegfx/polygon/b2dpolypolygon.hxx>
30 using namespace ::basegfx
;
35 class genericclipper
: public CppUnit::TestFixture
38 B2DPolygon aSelfIntersecting
;
39 B2DPolygon aShiftedRectangle
;
42 // initialise your test code values here.
45 aSelfIntersecting
.append(B2DPoint(0, 0));
46 aSelfIntersecting
.append(B2DPoint(0, 100));
47 aSelfIntersecting
.append(B2DPoint(75, 100));
48 aSelfIntersecting
.append(B2DPoint(75, 50));
49 aSelfIntersecting
.append(B2DPoint(25, 50));
50 aSelfIntersecting
.append(B2DPoint(25, 150));
51 aSelfIntersecting
.append(B2DPoint(100,150));
52 aSelfIntersecting
.append(B2DPoint(100,0));
53 aSelfIntersecting
.setClosed(true);
55 aShiftedRectangle
= utils::createPolygonFromRect(
56 B2DRange(0,90,20,150));
60 void validate(const char* pName
,
61 const char* pValidSvgD
,
62 B2DPolyPolygon (*pFunc
)(const B2DPolyPolygon
&, const B2DPolyPolygon
&)) const
64 const B2DPolyPolygon
aSelfIntersect(
65 utils::prepareForPolygonOperation(aSelfIntersecting
));
66 const B2DPolyPolygon
aRect(
67 utils::prepareForPolygonOperation(aShiftedRectangle
));
68 #if OSL_DEBUG_LEVEL > 2
69 fprintf(stderr
, "%s input LHS - svg:d=\"%s\"\n",
70 pName
, OUStringToOString(
71 basegfx::utils::exportToSvgD(
72 aSelfIntersect
, true, true, false),
73 RTL_TEXTENCODING_UTF8
).getStr() );
74 fprintf(stderr
, "%s input RHS - svg:d=\"%s\"\n",
75 pName
, OUStringToOString(
76 basegfx::utils::exportToSvgD(
77 aRect
, true, true, false),
78 RTL_TEXTENCODING_UTF8
).getStr() );
81 const B2DPolyPolygon aRes
=
82 pFunc(aSelfIntersect
, aRect
);
84 #if OSL_DEBUG_LEVEL > 2
85 fprintf(stderr
, "%s - svg:d=\"%s\"\n",
86 pName
, OUStringToOString(
87 basegfx::utils::exportToSvgD(aRes
, true, true, false),
88 RTL_TEXTENCODING_UTF8
).getStr() );
91 OUString aValid
=OUString::createFromAscii(pValidSvgD
);
93 CPPUNIT_ASSERT_EQUAL_MESSAGE(pName
,
94 aValid
, basegfx::utils::exportToSvgD(aRes
, true, true, false));
99 const char* const pValid
="m0 0h100v150h-75v-50h-5v50h-20v-50-10zm75 100v-50h-50v50z";
100 validate("validateOr", pValid
, &utils::solvePolygonOperationOr
);
105 const char* const pValid
="m0 0h100v150h-75v-50h-5v50h-20v-50-10zm0 100h20v-10h-20zm75 0v-50h-50v50z";
106 validate("validateXor", pValid
, &utils::solvePolygonOperationXor
);
111 const char* const pValid
="m0 100v-10h20v10z";
112 validate("validateAnd", pValid
, &utils::solvePolygonOperationAnd
);
117 const char* const pValid
="m0 90v-90h100v150h-75v-50h-5v-10zm75 10v-50h-50v50z";
118 validate("validateDiff", pValid
, &utils::solvePolygonOperationDiff
);
121 void validateCrossover(const char* pName
,
122 const char* pInputSvgD
,
123 const char* pValidSvgD
) const
125 OUString aInput
=OUString::createFromAscii(pInputSvgD
);
126 OUString aValid
=OUString::createFromAscii(pValidSvgD
);
127 B2DPolyPolygon aInputPoly
, aValidPoly
;
129 CPPUNIT_ASSERT(utils::importFromSvgD(aInputPoly
, aInput
, false, nullptr));
130 CPPUNIT_ASSERT(utils::importFromSvgD(aValidPoly
, aValid
, false, nullptr));
132 CPPUNIT_ASSERT_EQUAL_MESSAGE(
135 basegfx::utils::exportToSvgD(
136 utils::solveCrossovers(aInputPoly
), true, true, false));
139 void checkCrossoverSolver()
141 // partially intersecting polygons, with a common subsection
143 "partially intersecting",
144 "m0 0 v 5 h 3 h 1 h 1 h 1 v -2 v -3 z"
145 "m3 7 v -2 h 1 h 1 h 1 v -2 h 1 v 3 z",
146 "m0 0v5h3 1 1 1v-2-3zm3 7v-2h1 1 1v-2h1v3z");
148 // first polygon is identical to subset of second polygon
151 "m0 0 v 5 h 3 h 1 h 1 v -5 z"
152 "m3 10 v -5 h 1 h 1 v -5 h -5 v 5 h 3 z",
153 "m0 0v5h3 1 1v-5zm3 10v-5zm1-5h1v-5h-5v5h3z");
155 // first polygon is identical to subset of second polygon, but
156 // oriented in the opposite direction
158 "full subset, opposite direction",
159 "m0 0 v 5 h 3 h 1 h 1 v -5 z"
160 "m3 10 v -5 h -1 h -1 h -1 v -5 h 5 v 5 h 2 z",
161 "m0 0v5h1 1 1-1-1-1v-5h5v5-5zm4 5h1 2l-4 5v-5z");
163 // first polygon is identical to subset of second polygon, and
164 // has a curve segment (triggers different code path)
166 "full subset, plus curves",
167 "m0 0 v 5 h 3 h 1 h 1 c 2 0 2 0 0 -5 z"
168 "m3 10 v -5 h 1 h 1 c 2 0 2 0 0 -5 h -5 v 5 h 3 z",
169 "m0 0v5h3 1 1c2 0 2 0 0-5zm3 10v-5zm1-5h1c2 0 2 0 0-5h-5v5h3z");
172 // Change the following lines only, if you add, remove or rename
173 // member functions of the current class,
174 // because these macros are need by auto register mechanism.
176 CPPUNIT_TEST_SUITE(genericclipper
);
177 CPPUNIT_TEST(validateOr
);
178 CPPUNIT_TEST(validateXor
);
179 CPPUNIT_TEST(validateAnd
);
180 CPPUNIT_TEST(validateDiff
);
181 CPPUNIT_TEST(checkCrossoverSolver
);
182 CPPUNIT_TEST_SUITE_END();
185 CPPUNIT_TEST_SUITE_REGISTRATION(basegfx2d::genericclipper
);
186 } // namespace basegfx2d
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */