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