update dev300-m58
[ooovba.git] / basegfx / test / testtools.cxx
blob8b0aee1081baf61d7059ca1d5492edb3b9a8d5b5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testtools.cxx,v $
10 * $Revision: 1.3 $
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>
43 #include <algorithm>
46 namespace basegfx
48 namespace testtools
50 Plotter::Plotter( ::std::ostream& rOutputStream ) :
51 mrOutputStream(rOutputStream),
52 maPoints(),
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
58 // depending on t.
59 mrOutputStream << "#!/usr/bin/gnuplot -persist" << ::std::endl
60 << "#" << ::std::endl
61 << "# automatically generated by basegfx::testtools::Plotter, don't change!" << ::std::endl
62 << "#" << ::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,
86 // respectively)
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
91 << "plot [t=0:1] ";
94 namespace
96 class PointWriter
98 public:
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;
110 private:
111 ::std::ostream& mrOutputStream;
115 Plotter::~Plotter()
117 // End the plot line
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 <...>
122 // line
123 ::std::for_each( maPoints.begin(), maPoints.end(), PointWriter(mrOutputStream) );
126 void Plotter::plot( const B2DPolygon& rPoly )
128 const sal_uInt32 pointCount( rPoly.count() );
130 if( pointCount < 1 )
131 return;
133 if( pointCount == 1 )
135 plot( rPoly.getB2DPoint(0) );
136 return;
139 sal_uInt32 i;
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));
148 plot(aBezierPlot);
150 else
152 plot( rPoly.getB2DPoint(i), rPoly.getB2DPoint(i+1) );
157 void Plotter::plot( const B2DPolyPolygon& rPolyPoly )
159 const sal_uInt32 nPolyCount( rPolyPoly.count() );
161 sal_uInt32 i;
162 for( i=0; i<nPolyCount; ++i )
164 plot( rPolyPoly.getB2DPolygon(i) );
168 void Plotter::plot( const B2DPoint& rPoint )
170 maPoints.push_back( rPoint );
171 writeSeparator();
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(),
182 rRect.getMinY()),
183 B2DPoint(rRect.getMaxX(),
184 rRect.getMinY()) );
185 plot( B2DPoint(rRect.getMaxX(),
186 rRect.getMinY()),
187 B2DPoint(rRect.getMaxX(),
188 rRect.getMaxY()) );
189 plot( B2DPoint(rRect.getMaxX(),
190 rRect.getMaxY()),
191 B2DPoint(rRect.getMinX(),
192 rRect.getMaxY()) );
193 plot( B2DPoint(rRect.getMinX(),
194 rRect.getMaxY()),
195 B2DPoint(rRect.getMinX(),
196 rRect.getMinY()) );
199 void Plotter::plot( const B2DPoint& rStartPoint, const B2DPoint& rEndPoint )
201 writeSeparator();
202 mrOutputStream << "line(" << rStartPoint.getX()
203 << "," << rEndPoint.getX()
204 << ",t), "
205 << "line(" << rStartPoint.getY()
206 << "," << rEndPoint.getY()
207 << ",t)";
210 void Plotter::plot( const B2DCubicBezier& rCurve )
212 writeSeparator();
213 mrOutputStream << "cubicBezier(" << rCurve.getStartPoint().getX()
214 << "," << rCurve.getControlPointA().getX()
215 << "," << rCurve.getControlPointB().getX()
216 << "," << rCurve.getEndPoint().getX()
217 << ",t), "
218 << "cubicBezier(" << rCurve.getStartPoint().getY()
219 << "," << rCurve.getControlPointA().getY()
220 << "," << rCurve.getControlPointB().getY()
221 << "," << rCurve.getEndPoint().getY()
222 << ",t)";
225 void Plotter::writeSeparator()
227 if( mbFirstElement )
229 mbFirstElement = false;
231 else
233 mrOutputStream << ", ";