Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <sal/types.h>
30 : : #include "cppunit/TestAssert.h"
31 : : #include "cppunit/TestFixture.h"
32 : : #include "cppunit/extensions/HelperMacros.h"
33 : :
34 : : #include <basegfx/matrix/b2dhommatrix.hxx>
35 : : #include <basegfx/curve/b2dcubicbezier.hxx>
36 : : #include <basegfx/curve/b2dbeziertools.hxx>
37 : : #include <basegfx/range/b2dpolyrange.hxx>
38 : : #include <basegfx/polygon/b2dpolygon.hxx>
39 : : #include <basegfx/polygon/b2dpolygontools.hxx>
40 : : #include <basegfx/polygon/b2dpolypolygontools.hxx>
41 : : #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
42 : : #include <basegfx/polygon/b2dpolygonclipper.hxx>
43 : : #include <basegfx/polygon/b2dpolypolygon.hxx>
44 : : #include <basegfx/numeric/ftools.hxx>
45 : :
46 : : using namespace ::basegfx;
47 : :
48 : :
49 : : namespace basegfx2d
50 : : {
51 : :
52 [ + - ][ + - ]: 75 : class genericclipper : public CppUnit::TestFixture
[ - + ][ + - ]
[ + - ]
53 : : {
54 : : private:
55 : : B2DPolygon aSelfIntersecting;
56 : : B2DPolygon aShiftedRectangle;
57 : :
58 : : public:
59 : : // initialise your test code values here.
60 : 25 : void setUp()
61 : : {
62 [ + - ]: 25 : aSelfIntersecting.append(B2DPoint(0, 0));
63 [ + - ]: 25 : aSelfIntersecting.append(B2DPoint(0, 100));
64 [ + - ]: 25 : aSelfIntersecting.append(B2DPoint(75, 100));
65 [ + - ]: 25 : aSelfIntersecting.append(B2DPoint(75, 50));
66 [ + - ]: 25 : aSelfIntersecting.append(B2DPoint(25, 50));
67 [ + - ]: 25 : aSelfIntersecting.append(B2DPoint(25, 150));
68 [ + - ]: 25 : aSelfIntersecting.append(B2DPoint(100,150));
69 [ + - ]: 25 : aSelfIntersecting.append(B2DPoint(100,0));
70 : 25 : aSelfIntersecting.setClosed(true);
71 : :
72 : : aShiftedRectangle = tools::createPolygonFromRect(
73 [ + - ][ + - ]: 25 : B2DRange(0,90,20,150));
[ + - ]
74 : 25 : }
75 : :
76 : 25 : void tearDown()
77 : 25 : {}
78 : :
79 : 20 : void validate(const char* pName,
80 : : const char* pValidSvgD,
81 : : B2DPolyPolygon (*pFunc)(const B2DPolyPolygon&, const B2DPolyPolygon&))
82 : : {
83 : : const B2DPolyPolygon aSelfIntersect(
84 [ + - ]: 20 : tools::prepareForPolygonOperation(aSelfIntersecting));
85 : : const B2DPolyPolygon aRect(
86 [ + - ]: 20 : tools::prepareForPolygonOperation(aShiftedRectangle));
87 : : #if OSL_DEBUG_LEVEL > 2
88 : : fprintf(stderr, "%s input LHS - svg:d=\"%s\"\n",
89 : : pName, rtl::OUStringToOString(
90 : : basegfx::tools::exportToSvgD(
91 : : aSelfIntersect),
92 : : RTL_TEXTENCODING_UTF8).getStr() );
93 : : fprintf(stderr, "%s input RHS - svg:d=\"%s\"\n",
94 : : pName, rtl::OUStringToOString(
95 : : basegfx::tools::exportToSvgD(
96 : : aRect),
97 : : RTL_TEXTENCODING_UTF8).getStr() );
98 : : #endif
99 : :
100 : : const B2DPolyPolygon aRes=
101 [ + - ]: 20 : pFunc(aSelfIntersect, aRect);
102 : :
103 : : #if OSL_DEBUG_LEVEL > 2
104 : : fprintf(stderr, "%s - svg:d=\"%s\"\n",
105 : : pName, rtl::OUStringToOString(
106 : : basegfx::tools::exportToSvgD(aRes),
107 : : RTL_TEXTENCODING_UTF8).getStr() );
108 : : #endif
109 : :
110 : 20 : rtl::OUString aValid=rtl::OUString::createFromAscii(pValidSvgD);
111 : :
112 [ + - ][ + - ]: 40 : CPPUNIT_ASSERT_MESSAGE(pName,
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
113 [ + - ][ + - ]: 40 : basegfx::tools::exportToSvgD(aRes) == aValid);
[ + - + - ]
114 : 20 : }
115 : :
116 : 5 : void validateOr()
117 : : {
118 : 5 : const char* pValid="m0 0h100v150h-75v-50h-5v50h-20v-50-10zm75 100v-50h-50v50z";
119 : 5 : validate("validateOr", pValid, &tools::solvePolygonOperationOr);
120 : 5 : }
121 : :
122 : 5 : void validateXor()
123 : : {
124 : 5 : const char* pValid="m0 0h100v150h-75v-50h-5v50h-20v-50-10zm0 100h20v-10h-20zm75 0v-50h-50v50z";
125 : 5 : validate("validateXor", pValid, &tools::solvePolygonOperationXor);
126 : 5 : }
127 : :
128 : 5 : void validateAnd()
129 : : {
130 : 5 : const char* pValid="m0 100v-10h20v10z";
131 : 5 : validate("validateAnd", pValid, &tools::solvePolygonOperationAnd);
132 : 5 : }
133 : :
134 : 5 : void validateDiff()
135 : : {
136 : 5 : const char* pValid="m0 90v-90h100v150h-75v-50h-5v-10zm75 10v-50h-50v50z";
137 : 5 : validate("validateDiff", pValid, &tools::solvePolygonOperationDiff);
138 : 5 : }
139 : :
140 : 20 : void validateCrossover(const char* pName,
141 : : const char* pInputSvgD,
142 : : const char* pValidSvgD)
143 : : {
144 : 20 : rtl::OUString aInput=rtl::OUString::createFromAscii(pInputSvgD);
145 : 20 : rtl::OUString aValid=rtl::OUString::createFromAscii(pValidSvgD);
146 [ + - ][ + - ]: 20 : B2DPolyPolygon aInputPoly, aValidPoly;
147 : :
148 [ + - ]: 20 : tools::importFromSvgD(aInputPoly, aInput);
149 [ + - ]: 20 : tools::importFromSvgD(aValidPoly, aValid);
150 : :
151 [ + - ][ + - ]: 40 : CPPUNIT_ASSERT_MESSAGE(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
152 : : pName,
153 : : basegfx::tools::exportToSvgD(
154 [ + - ]: 40 : tools::solveCrossovers(aInputPoly)) == aValid);
[ + - + - ]
155 : 20 : }
156 : :
157 : 5 : void checkCrossoverSolver()
158 : : {
159 : : // partially intersecting polygons, with a common subsection
160 : : validateCrossover(
161 : : "partially intersecting",
162 : : "m0 0 v 5 h 3 h 1 h 1 h 1 v -2 v -3 z"
163 : : "m3 7 v -2 h 1 h 1 h 1 v -2 h 1 v 3 z",
164 : 5 : "m0 0v5h3 1 1 1v-2-3zm3 7v-2h1 1 1v-2h1v3z");
165 : :
166 : : // first polygon is identical to subset of second polygon
167 : : validateCrossover(
168 : : "full subset",
169 : : "m0 0 v 5 h 3 h 1 h 1 v -5 z"
170 : : "m3 10 v -5 h 1 h 1 v -5 h -5 v 5 h 3 z",
171 : 5 : "m0 0v5h3 1 1v-5zm3 10v-5zm1-5h1v-5h-5v5h3z");
172 : :
173 : : // first polygon is identical to subset of second polygon, but
174 : : // oriented in the opposite direction
175 : : validateCrossover(
176 : : "full subset, opposite direction",
177 : : "m0 0 v 5 h 3 h 1 h 1 v -5 z"
178 : : "m3 10 v -5 h -1 h -1 h -1 v -5 h 5 v 5 h 2 z",
179 : 5 : "m0 0v5h1 1 1-1-1-1v-5h5v5-5zm4 5h1 2l-4 5v-5z");
180 : :
181 : : // first polygon is identical to subset of second polygon, and
182 : : // has a curve segment (triggers different code path)
183 : : validateCrossover(
184 : : "full subset, plus curves",
185 : : "m0 0 v 5 h 3 h 1 h 1 c 2 0 2 0 0 -5 z"
186 : : "m3 10 v -5 h 1 h 1 c 2 0 2 0 0 -5 h -5 v 5 h 3 z",
187 : 5 : "m0 0v5h3 1 1c2 0 2 0 0-5zm3 10v-5zm1-5h1c2 0 2 0 0-5h-5v5h3z");
188 : 5 : }
189 : :
190 : : // Change the following lines only, if you add, remove or rename
191 : : // member functions of the current class,
192 : : // because these macros are need by auto register mechanism.
193 : :
194 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(genericclipper);
[ + - ][ + - ]
[ # # ]
195 [ + - ][ + - ]: 5 : CPPUNIT_TEST(validateOr);
[ + - ][ + - ]
[ + - ][ + - ]
196 [ + - ][ + - ]: 5 : CPPUNIT_TEST(validateXor);
[ + - ][ + - ]
[ + - ][ + - ]
197 [ + - ][ + - ]: 5 : CPPUNIT_TEST(validateAnd);
[ + - ][ + - ]
[ + - ][ + - ]
198 [ + - ][ + - ]: 5 : CPPUNIT_TEST(validateDiff);
[ + - ][ + - ]
[ + - ][ + - ]
199 [ + - ][ + - ]: 5 : CPPUNIT_TEST(checkCrossoverSolver);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
200 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
201 : : };
202 : :
203 : : // -----------------------------------------------------------------------------
204 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(basegfx2d::genericclipper);
205 [ + - ][ + - ]: 15 : } // namespace basegfx2d
206 : :
207 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|