1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testtools.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basegfx.hxx"
34 #include "testtools.hxx"
36 #include <basegfx/point/b2dpoint.hxx>
37 #include <basegfx/vector/b2dvector.hxx>
38 #include <basegfx/range/b2drange.hxx>
39 #include <basegfx/curve/b2dcubicbezier.hxx>
40 #include <basegfx/polygon/b2dpolygon.hxx>
41 #include <basegfx/polygon/b2dpolypolygon.hxx>
50 Plotter::Plotter( ::std::ostream
& rOutputStream
) :
51 mrOutputStream(rOutputStream
),
53 mbFirstElement( true )
55 // output gnuplot setup. We switch gnuplot to parametric
56 // mode, therefore every plot has at least _two_
57 // functions: one for the x and one for the y value, both
59 mrOutputStream
<< "#!/usr/bin/gnuplot -persist" << ::std::endl
61 << "# automatically generated by basegfx::testtools::Plotter, don't change!" << ::std::endl
63 << "set parametric" << ::std::endl
64 // This function plots a cubic bezier curve. P,q,r,s
65 // are the control point elements of the corresponding
66 // output coordinate component (i.e. x components for
67 // the x plot, and y components for the y plot)
68 << "cubicBezier(p,q,r,s,t) = p*(1-t)**3+q*3*(1-t)**2*t+r*3*(1-t)*t**2+s*t**3" << ::std::endl
70 // This function plots the derivative of a cubic
71 // bezier curve. P,q,r,s are the control point
72 // components of the _original_ curve
73 << "cubicBezDerivative(p,q,r,s,t) = 3*(q-p)*(1-t)**2+6*(r-q)*(1-t)*t+3*(s-r)*t**2" << ::std::endl
75 // Plot a line's x component of a line in implicit
76 // form ax + by + c = 0
77 << "implicitLineX(a,b,c,t) = a*-c + t*-b" << ::std::endl
79 // Plot a line's y component of a line in implicit
80 // form ax + by + c = 0
81 << "implicitLineY(a,b,c,t) = b*-c + t*a" << ::std::endl
83 // Plot a line's component of a line between a and b
84 // (where a and b should be the corresponding
85 // components of the line's start and end point,
87 << "line(a,b,t) = a*(1-t) + b*t" << ::std::endl
<< ::std::endl
88 << "# end of setup" << ::std::endl
<< ::std::endl
90 // Start the actual plot line
99 PointWriter( ::std::ostream
& rOutputStream
) :
100 mrOutputStream( rOutputStream
)
104 void operator()( const B2DPoint
& rPoint
) const
106 mrOutputStream
<< rPoint
.getX() << "\t" << rPoint
.getY() << ::std::endl
;
107 mrOutputStream
<< "e" << ::std::endl
;
111 ::std::ostream
& mrOutputStream
;
118 mrOutputStream
<< ::std::endl
;
120 // write stored data points. Cannot write before, since
121 // this is an inline dataset, which must be after the plot <...>
123 ::std::for_each( maPoints
.begin(), maPoints
.end(), PointWriter(mrOutputStream
) );
126 void Plotter::plot( const B2DPolygon
& rPoly
)
128 const sal_uInt32
pointCount( rPoly
.count() );
133 if( pointCount
== 1 )
135 plot( rPoly
.getB2DPoint(0) );
140 for( i
=0; i
<pointCount
-1; ++i
)
142 if(rPoly
.isNextControlPointUsed(i
) || rPoly
.isPrevControlPointUsed(i
+ 1))
144 const B2DCubicBezier
aBezierPlot(
145 rPoly
.getB2DPoint(i
), rPoly
.getNextControlPoint(i
),
146 rPoly
.getPrevControlPoint(i
+ 1), rPoly
.getB2DPoint(i
+ 1));
152 plot( rPoly
.getB2DPoint(i
), rPoly
.getB2DPoint(i
+1) );
157 void Plotter::plot( const B2DPolyPolygon
& rPolyPoly
)
159 const sal_uInt32
nPolyCount( rPolyPoly
.count() );
162 for( i
=0; i
<nPolyCount
; ++i
)
164 plot( rPolyPoly
.getB2DPolygon(i
) );
168 void Plotter::plot( const B2DPoint
& rPoint
)
170 maPoints
.push_back( rPoint
);
172 mrOutputStream
<< "'-' using ($1):($2) title \"Point " << maPoints
.size() << "\" with points";
175 void Plotter::plot( const B2DRange
& rRect
)
177 // TODO: do that also as a data file plot. maPoints must
178 // then become polymorph, but WTF.
180 // decompose into four lines
181 plot( B2DPoint(rRect
.getMinX(),
183 B2DPoint(rRect
.getMaxX(),
185 plot( B2DPoint(rRect
.getMaxX(),
187 B2DPoint(rRect
.getMaxX(),
189 plot( B2DPoint(rRect
.getMaxX(),
191 B2DPoint(rRect
.getMinX(),
193 plot( B2DPoint(rRect
.getMinX(),
195 B2DPoint(rRect
.getMinX(),
199 void Plotter::plot( const B2DPoint
& rStartPoint
, const B2DPoint
& rEndPoint
)
202 mrOutputStream
<< "line(" << rStartPoint
.getX()
203 << "," << rEndPoint
.getX()
205 << "line(" << rStartPoint
.getY()
206 << "," << rEndPoint
.getY()
210 void Plotter::plot( const B2DCubicBezier
& rCurve
)
213 mrOutputStream
<< "cubicBezier(" << rCurve
.getStartPoint().getX()
214 << "," << rCurve
.getControlPointA().getX()
215 << "," << rCurve
.getControlPointB().getX()
216 << "," << rCurve
.getEndPoint().getX()
218 << "cubicBezier(" << rCurve
.getStartPoint().getY()
219 << "," << rCurve
.getControlPointA().getY()
220 << "," << rCurve
.getControlPointB().getY()
221 << "," << rCurve
.getEndPoint().getY()
225 void Plotter::writeSeparator()
229 mbFirstElement
= false;
233 mrOutputStream
<< ", ";