Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/types.h>
21 : #include <cppunit/TestAssert.h>
22 : #include <cppunit/TestFixture.h>
23 : #include <cppunit/extensions/HelperMacros.h>
24 :
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>
36 :
37 : using namespace ::basegfx;
38 :
39 : namespace basegfx2d
40 : {
41 :
42 30 : class genericclipper : public CppUnit::TestFixture
43 : {
44 : private:
45 : B2DPolygon aSelfIntersecting;
46 : B2DPolygon aShiftedRectangle;
47 :
48 : public:
49 : // initialise your test code values here.
50 10 : void setUp() SAL_OVERRIDE
51 : {
52 10 : aSelfIntersecting.append(B2DPoint(0, 0));
53 10 : aSelfIntersecting.append(B2DPoint(0, 100));
54 10 : aSelfIntersecting.append(B2DPoint(75, 100));
55 10 : aSelfIntersecting.append(B2DPoint(75, 50));
56 10 : aSelfIntersecting.append(B2DPoint(25, 50));
57 10 : aSelfIntersecting.append(B2DPoint(25, 150));
58 10 : aSelfIntersecting.append(B2DPoint(100,150));
59 10 : aSelfIntersecting.append(B2DPoint(100,0));
60 10 : aSelfIntersecting.setClosed(true);
61 :
62 20 : aShiftedRectangle = tools::createPolygonFromRect(
63 10 : B2DRange(0,90,20,150));
64 10 : }
65 :
66 10 : void tearDown() SAL_OVERRIDE
67 10 : {}
68 :
69 8 : void validate(const char* pName,
70 : const char* pValidSvgD,
71 : B2DPolyPolygon (*pFunc)(const B2DPolyPolygon&, const B2DPolyPolygon&))
72 : {
73 : const B2DPolyPolygon aSelfIntersect(
74 8 : tools::prepareForPolygonOperation(aSelfIntersecting));
75 : const B2DPolyPolygon aRect(
76 16 : tools::prepareForPolygonOperation(aShiftedRectangle));
77 : #if OSL_DEBUG_LEVEL > 2
78 : fprintf(stderr, "%s input LHS - svg:d=\"%s\"\n",
79 : pName, OUStringToOString(
80 : basegfx::tools::exportToSvgD(
81 : aSelfIntersect, true, true, false),
82 : RTL_TEXTENCODING_UTF8).getStr() );
83 : fprintf(stderr, "%s input RHS - svg:d=\"%s\"\n",
84 : pName, OUStringToOString(
85 : basegfx::tools::exportToSvgD(
86 : aRect, true, true, false),
87 : RTL_TEXTENCODING_UTF8).getStr() );
88 : #endif
89 :
90 : const B2DPolyPolygon aRes=
91 16 : pFunc(aSelfIntersect, aRect);
92 :
93 : #if OSL_DEBUG_LEVEL > 2
94 : fprintf(stderr, "%s - svg:d=\"%s\"\n",
95 : pName, OUStringToOString(
96 : basegfx::tools::exportToSvgD(aRes, true, true, false),
97 : RTL_TEXTENCODING_UTF8).getStr() );
98 : #endif
99 :
100 16 : OUString aValid=OUString::createFromAscii(pValidSvgD);
101 :
102 16 : CPPUNIT_ASSERT_MESSAGE(pName,
103 16 : basegfx::tools::exportToSvgD(aRes, true, true, false) == aValid);
104 8 : }
105 :
106 2 : void validateOr()
107 : {
108 2 : const char* pValid="m0 0h100v150h-75v-50h-5v50h-20v-50-10zm75 100v-50h-50v50z";
109 2 : validate("validateOr", pValid, &tools::solvePolygonOperationOr);
110 2 : }
111 :
112 2 : void validateXor()
113 : {
114 2 : const char* pValid="m0 0h100v150h-75v-50h-5v50h-20v-50-10zm0 100h20v-10h-20zm75 0v-50h-50v50z";
115 2 : validate("validateXor", pValid, &tools::solvePolygonOperationXor);
116 2 : }
117 :
118 2 : void validateAnd()
119 : {
120 2 : const char* pValid="m0 100v-10h20v10z";
121 2 : validate("validateAnd", pValid, &tools::solvePolygonOperationAnd);
122 2 : }
123 :
124 2 : void validateDiff()
125 : {
126 2 : const char* pValid="m0 90v-90h100v150h-75v-50h-5v-10zm75 10v-50h-50v50z";
127 2 : validate("validateDiff", pValid, &tools::solvePolygonOperationDiff);
128 2 : }
129 :
130 8 : void validateCrossover(const char* pName,
131 : const char* pInputSvgD,
132 : const char* pValidSvgD)
133 : {
134 8 : OUString aInput=OUString::createFromAscii(pInputSvgD);
135 16 : OUString aValid=OUString::createFromAscii(pValidSvgD);
136 16 : B2DPolyPolygon aInputPoly, aValidPoly;
137 :
138 8 : tools::importFromSvgD(aInputPoly, aInput, false, NULL);
139 8 : tools::importFromSvgD(aValidPoly, aValid, false, NULL);
140 :
141 16 : CPPUNIT_ASSERT_MESSAGE(
142 : pName,
143 : basegfx::tools::exportToSvgD(
144 16 : tools::solveCrossovers(aInputPoly), true, true, false) == aValid);
145 8 : }
146 :
147 2 : void checkCrossoverSolver()
148 : {
149 : // partially intersecting polygons, with a common subsection
150 : validateCrossover(
151 : "partially intersecting",
152 : "m0 0 v 5 h 3 h 1 h 1 h 1 v -2 v -3 z"
153 : "m3 7 v -2 h 1 h 1 h 1 v -2 h 1 v 3 z",
154 2 : "m0 0v5h3 1 1 1v-2-3zm3 7v-2h1 1 1v-2h1v3z");
155 :
156 : // first polygon is identical to subset of second polygon
157 : validateCrossover(
158 : "full subset",
159 : "m0 0 v 5 h 3 h 1 h 1 v -5 z"
160 : "m3 10 v -5 h 1 h 1 v -5 h -5 v 5 h 3 z",
161 2 : "m0 0v5h3 1 1v-5zm3 10v-5zm1-5h1v-5h-5v5h3z");
162 :
163 : // first polygon is identical to subset of second polygon, but
164 : // oriented in the opposite direction
165 : validateCrossover(
166 : "full subset, opposite direction",
167 : "m0 0 v 5 h 3 h 1 h 1 v -5 z"
168 : "m3 10 v -5 h -1 h -1 h -1 v -5 h 5 v 5 h 2 z",
169 2 : "m0 0v5h1 1 1-1-1-1v-5h5v5-5zm4 5h1 2l-4 5v-5z");
170 :
171 : // first polygon is identical to subset of second polygon, and
172 : // has a curve segment (triggers different code path)
173 : validateCrossover(
174 : "full subset, plus curves",
175 : "m0 0 v 5 h 3 h 1 h 1 c 2 0 2 0 0 -5 z"
176 : "m3 10 v -5 h 1 h 1 c 2 0 2 0 0 -5 h -5 v 5 h 3 z",
177 2 : "m0 0v5h3 1 1c2 0 2 0 0-5zm3 10v-5zm1-5h1c2 0 2 0 0-5h-5v5h3z");
178 2 : }
179 :
180 : // Change the following lines only, if you add, remove or rename
181 : // member functions of the current class,
182 : // because these macros are need by auto register mechanism.
183 :
184 4 : CPPUNIT_TEST_SUITE(genericclipper);
185 2 : CPPUNIT_TEST(validateOr);
186 2 : CPPUNIT_TEST(validateXor);
187 2 : CPPUNIT_TEST(validateAnd);
188 2 : CPPUNIT_TEST(validateDiff);
189 2 : CPPUNIT_TEST(checkCrossoverSolver);
190 4 : CPPUNIT_TEST_SUITE_END();
191 : };
192 :
193 2 : CPPUNIT_TEST_SUITE_REGISTRATION(basegfx2d::genericclipper);
194 6 : } // namespace basegfx2d
195 :
196 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|