Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / basegfx / test / genericclipper.cxx
blobdde7afa4ad1d9bc1f425a7d90b947a4c331f1136
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <sal/types.h>
21 #include <cppunit/TestAssert.h>
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
25 #include <basegfx/matrix/b2dhommatrix.hxx>
26 #include <basegfx/curve/b2dcubicbezier.hxx>
27 #include <basegfx/curve/b2dbeziertools.hxx>
28 #include <basegfx/range/b2dpolyrange.hxx>
29 #include <basegfx/polygon/b2dpolygon.hxx>
30 #include <basegfx/polygon/b2dpolygontools.hxx>
31 #include <basegfx/polygon/b2dpolypolygontools.hxx>
32 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
33 #include <basegfx/polygon/b2dpolygonclipper.hxx>
34 #include <basegfx/polygon/b2dpolypolygon.hxx>
35 #include <basegfx/numeric/ftools.hxx>
37 using namespace ::basegfx;
39 namespace basegfx2d
42 class genericclipper : public CppUnit::TestFixture
44 private:
45 B2DPolygon aSelfIntersecting;
46 B2DPolygon aShiftedRectangle;
48 public:
49 // initialise your test code values here.
50 void setUp() override
52 aSelfIntersecting.append(B2DPoint(0, 0));
53 aSelfIntersecting.append(B2DPoint(0, 100));
54 aSelfIntersecting.append(B2DPoint(75, 100));
55 aSelfIntersecting.append(B2DPoint(75, 50));
56 aSelfIntersecting.append(B2DPoint(25, 50));
57 aSelfIntersecting.append(B2DPoint(25, 150));
58 aSelfIntersecting.append(B2DPoint(100,150));
59 aSelfIntersecting.append(B2DPoint(100,0));
60 aSelfIntersecting.setClosed(true);
62 aShiftedRectangle = tools::createPolygonFromRect(
63 B2DRange(0,90,20,150));
67 void validate(const char* pName,
68 const char* pValidSvgD,
69 B2DPolyPolygon (*pFunc)(const B2DPolyPolygon&, const B2DPolyPolygon&))
71 const B2DPolyPolygon aSelfIntersect(
72 tools::prepareForPolygonOperation(aSelfIntersecting));
73 const B2DPolyPolygon aRect(
74 tools::prepareForPolygonOperation(aShiftedRectangle));
75 #if OSL_DEBUG_LEVEL > 2
76 fprintf(stderr, "%s input LHS - svg:d=\"%s\"\n",
77 pName, OUStringToOString(
78 basegfx::tools::exportToSvgD(
79 aSelfIntersect, true, true, false),
80 RTL_TEXTENCODING_UTF8).getStr() );
81 fprintf(stderr, "%s input RHS - svg:d=\"%s\"\n",
82 pName, OUStringToOString(
83 basegfx::tools::exportToSvgD(
84 aRect, true, true, false),
85 RTL_TEXTENCODING_UTF8).getStr() );
86 #endif
88 const B2DPolyPolygon aRes=
89 pFunc(aSelfIntersect, aRect);
91 #if OSL_DEBUG_LEVEL > 2
92 fprintf(stderr, "%s - svg:d=\"%s\"\n",
93 pName, OUStringToOString(
94 basegfx::tools::exportToSvgD(aRes, true, true, false),
95 RTL_TEXTENCODING_UTF8).getStr() );
96 #endif
98 OUString aValid=OUString::createFromAscii(pValidSvgD);
100 CPPUNIT_ASSERT_MESSAGE(pName,
101 basegfx::tools::exportToSvgD(aRes, true, true, false) == aValid);
104 void validateOr()
106 const char* pValid="m0 0h100v150h-75v-50h-5v50h-20v-50-10zm75 100v-50h-50v50z";
107 validate("validateOr", pValid, &tools::solvePolygonOperationOr);
110 void validateXor()
112 const char* pValid="m0 0h100v150h-75v-50h-5v50h-20v-50-10zm0 100h20v-10h-20zm75 0v-50h-50v50z";
113 validate("validateXor", pValid, &tools::solvePolygonOperationXor);
116 void validateAnd()
118 const char* pValid="m0 100v-10h20v10z";
119 validate("validateAnd", pValid, &tools::solvePolygonOperationAnd);
122 void validateDiff()
124 const char* pValid="m0 90v-90h100v150h-75v-50h-5v-10zm75 10v-50h-50v50z";
125 validate("validateDiff", pValid, &tools::solvePolygonOperationDiff);
128 void validateCrossover(const char* pName,
129 const char* pInputSvgD,
130 const char* pValidSvgD)
132 OUString aInput=OUString::createFromAscii(pInputSvgD);
133 OUString aValid=OUString::createFromAscii(pValidSvgD);
134 B2DPolyPolygon aInputPoly, aValidPoly;
136 tools::importFromSvgD(aInputPoly, aInput, false, nullptr);
137 tools::importFromSvgD(aValidPoly, aValid, false, nullptr);
139 CPPUNIT_ASSERT_MESSAGE(
140 pName,
141 basegfx::tools::exportToSvgD(
142 tools::solveCrossovers(aInputPoly), true, true, false) == aValid);
145 void checkCrossoverSolver()
147 // partially intersecting polygons, with a common subsection
148 validateCrossover(
149 "partially intersecting",
150 "m0 0 v 5 h 3 h 1 h 1 h 1 v -2 v -3 z"
151 "m3 7 v -2 h 1 h 1 h 1 v -2 h 1 v 3 z",
152 "m0 0v5h3 1 1 1v-2-3zm3 7v-2h1 1 1v-2h1v3z");
154 // first polygon is identical to subset of second polygon
155 validateCrossover(
156 "full subset",
157 "m0 0 v 5 h 3 h 1 h 1 v -5 z"
158 "m3 10 v -5 h 1 h 1 v -5 h -5 v 5 h 3 z",
159 "m0 0v5h3 1 1v-5zm3 10v-5zm1-5h1v-5h-5v5h3z");
161 // first polygon is identical to subset of second polygon, but
162 // oriented in the opposite direction
163 validateCrossover(
164 "full subset, opposite direction",
165 "m0 0 v 5 h 3 h 1 h 1 v -5 z"
166 "m3 10 v -5 h -1 h -1 h -1 v -5 h 5 v 5 h 2 z",
167 "m0 0v5h1 1 1-1-1-1v-5h5v5-5zm4 5h1 2l-4 5v-5z");
169 // first polygon is identical to subset of second polygon, and
170 // has a curve segment (triggers different code path)
171 validateCrossover(
172 "full subset, plus curves",
173 "m0 0 v 5 h 3 h 1 h 1 c 2 0 2 0 0 -5 z"
174 "m3 10 v -5 h 1 h 1 c 2 0 2 0 0 -5 h -5 v 5 h 3 z",
175 "m0 0v5h3 1 1c2 0 2 0 0-5zm3 10v-5zm1-5h1c2 0 2 0 0-5h-5v5h3z");
178 // Change the following lines only, if you add, remove or rename
179 // member functions of the current class,
180 // because these macros are need by auto register mechanism.
182 CPPUNIT_TEST_SUITE(genericclipper);
183 CPPUNIT_TEST(validateOr);
184 CPPUNIT_TEST(validateXor);
185 CPPUNIT_TEST(validateAnd);
186 CPPUNIT_TEST(validateDiff);
187 CPPUNIT_TEST(checkCrossoverSolver);
188 CPPUNIT_TEST_SUITE_END();
191 CPPUNIT_TEST_SUITE_REGISTRATION(basegfx2d::genericclipper);
192 } // namespace basegfx2d
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */