Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / raster / rasterconvert3d.cxx
blob501bdf93f2e52cda39ad3240ba451c11d0638c62
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <basegfx/raster/rasterconvert3d.hxx>
21 #include <basegfx/polygon/b3dpolygon.hxx>
22 #include <basegfx/polygon/b3dpolypolygon.hxx>
23 #include <basegfx/point/b3dpoint.hxx>
25 //////////////////////////////////////////////////////////////////////////////
26 // implementations of the 3D raster converter
28 namespace basegfx
30 void RasterConverter3D::addArea(const B3DPolygon& rFill, const B3DHomMatrix* pViewToEye)
32 const sal_uInt32 nPointCount(rFill.count());
34 for(sal_uInt32 a(0); a < nPointCount; a++)
36 addEdge(rFill, a, (a + 1) % nPointCount, pViewToEye);
40 void RasterConverter3D::addArea(const B3DPolyPolygon& rFill, const B3DHomMatrix* pViewToEye)
42 const sal_uInt32 nPolyCount(rFill.count());
44 for(sal_uInt32 a(0); a < nPolyCount; a++)
46 addArea(rFill.getB3DPolygon(a), pViewToEye);
50 RasterConverter3D::RasterConverter3D()
51 : InterpolatorProvider3D(),
52 maLineEntries()
55 RasterConverter3D::~RasterConverter3D()
58 void RasterConverter3D::rasterconvertB3DArea(sal_Int32 nStartLine, sal_Int32 nStopLine)
60 if(!maLineEntries.empty())
62 OSL_ENSURE(nStopLine >= nStartLine, "nStopLine is bigger than nStartLine (!)");
64 // sort global entries by Y, X once. After this, the vector
65 // is seen as frozen. Pointers to it's entries will be used in the following code.
66 ::std::sort(maLineEntries.begin(), maLineEntries.end());
68 // local parameters
69 ::std::vector< RasterConversionLineEntry3D >::iterator aCurrentEntry(maLineEntries.begin());
70 ::std::vector< RasterConversionLineEntry3D* > aCurrentLine;
71 ::std::vector< RasterConversionLineEntry3D* > aNextLine;
72 ::std::vector< RasterConversionLineEntry3D* >::iterator aRasterConversionLineEntry3D;
73 sal_uInt32 nPairCount(0);
75 // get scanlines first LineNumber as start
76 sal_Int32 nLineNumber(::std::max(aCurrentEntry->getY(), nStartLine));
78 while((aCurrentLine.size() || aCurrentEntry != maLineEntries.end()) && (nLineNumber < nStopLine))
80 // add all entries which start at current line to current scanline
81 while(aCurrentEntry != maLineEntries.end())
83 const sal_Int32 nCurrentLineNumber(aCurrentEntry->getY());
85 if(nCurrentLineNumber > nLineNumber)
87 // line is below current one, done (since array is sorted)
88 break;
90 else
92 // less or equal. Line is above or at current one. Advance it exactly to
93 // current line
94 const sal_uInt32 nStep(nLineNumber - nCurrentLineNumber);
96 if(!nStep || aCurrentEntry->decrementRasterConversionLineEntry3D(nStep))
98 // add when exactly on current line or when incremet to it did not
99 // completely consume it
100 if(nStep)
102 aCurrentEntry->incrementRasterConversionLineEntry3D(nStep, *this);
105 aCurrentLine.push_back(&(*(aCurrentEntry)));
109 ++aCurrentEntry;
112 // sort current scanline using comparator. Only X is used there
113 // since all entries are already in one processed line. This needs to be done
114 // everytime since not only new spans may have benn added or old removed,
115 // but incrementing may also have changed the order
116 ::std::sort(aCurrentLine.begin(), aCurrentLine.end(), lineComparator());
118 // process current scanline
119 aRasterConversionLineEntry3D = aCurrentLine.begin();
120 aNextLine.clear();
121 nPairCount = 0;
123 while(aRasterConversionLineEntry3D != aCurrentLine.end())
125 RasterConversionLineEntry3D& rPrevScanRasterConversionLineEntry3D(**aRasterConversionLineEntry3D++);
127 // look for 2nd span
128 if(aRasterConversionLineEntry3D != aCurrentLine.end())
130 // work on span from rPrevScanRasterConversionLineEntry3D to aRasterConversionLineEntry3D, fLineNumber is valid
131 processLineSpan(rPrevScanRasterConversionLineEntry3D, **aRasterConversionLineEntry3D, nLineNumber, nPairCount++);
134 // increment to next line
135 if(rPrevScanRasterConversionLineEntry3D.decrementRasterConversionLineEntry3D(1))
137 rPrevScanRasterConversionLineEntry3D.incrementRasterConversionLineEntry3D(1, *this);
138 aNextLine.push_back(&rPrevScanRasterConversionLineEntry3D);
142 // copy back next scanline if count has changed
143 if(aNextLine.size() != aCurrentLine.size())
145 aCurrentLine = aNextLine;
148 // increment fLineNumber
149 nLineNumber++;
154 void RasterConverter3D::addEdge(const B3DPolygon& rFill, sal_uInt32 a, sal_uInt32 b, const B3DHomMatrix* pViewToEye)
156 B3DPoint aStart(rFill.getB3DPoint(a));
157 B3DPoint aEnd(rFill.getB3DPoint(b));
158 sal_Int32 nYStart(fround(aStart.getY()));
159 sal_Int32 nYEnd(fround(aEnd.getY()));
161 if(nYStart != nYEnd)
163 if(nYStart > nYEnd)
165 ::std::swap(aStart, aEnd);
166 ::std::swap(nYStart, nYEnd);
167 ::std::swap(a, b);
170 const sal_uInt32 nYDelta(nYEnd - nYStart);
171 const double fInvYDelta(1.0 / nYDelta);
172 maLineEntries.push_back(RasterConversionLineEntry3D(
173 aStart.getX(), (aEnd.getX() - aStart.getX()) * fInvYDelta,
174 aStart.getZ(), (aEnd.getZ() - aStart.getZ()) * fInvYDelta,
175 nYStart, nYDelta));
177 // if extra interpolation data is used, add it to the last created entry
178 RasterConversionLineEntry3D& rEntry = maLineEntries[maLineEntries.size() - 1];
180 if(rFill.areBColorsUsed())
182 rEntry.setColorIndex(addColorInterpolator(rFill.getBColor(a), rFill.getBColor(b), fInvYDelta));
185 if(rFill.areNormalsUsed())
187 rEntry.setNormalIndex(addNormalInterpolator(rFill.getNormal(a), rFill.getNormal(b), fInvYDelta));
190 if(rFill.areTextureCoordinatesUsed())
192 if(pViewToEye)
194 const double fEyeA(((*pViewToEye) * aStart).getZ());
195 const double fEyeB(((*pViewToEye) * aEnd).getZ());
197 rEntry.setInverseTextureIndex(addInverseTextureInterpolator(
198 rFill.getTextureCoordinate(a),
199 rFill.getTextureCoordinate(b),
200 fEyeA, fEyeB, fInvYDelta));
202 else
204 rEntry.setTextureIndex(addTextureInterpolator(
205 rFill.getTextureCoordinate(a),
206 rFill.getTextureCoordinate(b),
207 fInvYDelta));
213 void RasterConverter3D::rasterconvertB3DEdge(const B3DPolygon& rLine, sal_uInt32 nA, sal_uInt32 nB, sal_Int32 nStartLine, sal_Int32 nStopLine, sal_uInt16 nLineWidth)
215 B3DPoint aStart(rLine.getB3DPoint(nA));
216 B3DPoint aEnd(rLine.getB3DPoint(nB));
217 const double fZBufferLineAdd(0x00ff);
218 static bool bForceToPolygon(false);
220 if(nLineWidth > 1 || bForceToPolygon)
222 // this is not a hairline anymore, in most cases since it's an oversampled
223 // hairline to get e.g. AA for Z-Buffering. Create fill geometry.
224 if(!aStart.equal(aEnd))
226 reset();
227 maLineEntries.clear();
229 B2DVector aVector(aEnd.getX() - aStart.getX(), aEnd.getY() - aStart.getY());
230 aVector.normalize();
231 const B2DVector aPerpend(getPerpendicular(aVector) * ((static_cast<double>(nLineWidth) + 0.5) * 0.5));
232 const double fZStartWithAdd(aStart.getZ() + fZBufferLineAdd);
233 const double fZEndWithAdd(aEnd.getZ() + fZBufferLineAdd);
235 B3DPolygon aPolygon;
236 aPolygon.append(B3DPoint(aStart.getX() + aPerpend.getX(), aStart.getY() + aPerpend.getY(), fZStartWithAdd));
237 aPolygon.append(B3DPoint(aEnd.getX() + aPerpend.getX(), aEnd.getY() + aPerpend.getY(), fZEndWithAdd));
238 aPolygon.append(B3DPoint(aEnd.getX() - aPerpend.getX(), aEnd.getY() - aPerpend.getY(), fZEndWithAdd));
239 aPolygon.append(B3DPoint(aStart.getX() - aPerpend.getX(), aStart.getY() - aPerpend.getY(), fZStartWithAdd));
240 aPolygon.setClosed(true);
242 addArea(aPolygon, 0);
245 else
247 // it's a hairline. Use direct RasterConversionLineEntry creation to
248 // rasterconvert lines as similar to areas as possible to avoid Z-Fighting
249 sal_Int32 nYStart(fround(aStart.getY()));
250 sal_Int32 nYEnd(fround(aEnd.getY()));
252 if(nYStart == nYEnd)
254 // horizontal line, check X
255 const sal_Int32 nXStart(static_cast<sal_Int32>(aStart.getX()));
256 const sal_Int32 nXEnd(static_cast<sal_Int32>(aEnd.getX()));
258 if(nXStart != nXEnd)
260 reset();
261 maLineEntries.clear();
263 // horizontal line, create vertical entries. These will be sorted by
264 // X anyways, so no need to distinguish the case here
265 maLineEntries.push_back(RasterConversionLineEntry3D(
266 aStart.getX(), 0.0,
267 aStart.getZ() + fZBufferLineAdd, 0.0,
268 nYStart, 1));
269 maLineEntries.push_back(RasterConversionLineEntry3D(
270 aEnd.getX(), 0.0,
271 aEnd.getZ() + fZBufferLineAdd, 0.0,
272 nYStart, 1));
275 else
277 reset();
278 maLineEntries.clear();
280 if(nYStart > nYEnd)
282 ::std::swap(aStart, aEnd);
283 ::std::swap(nYStart, nYEnd);
286 const sal_uInt32 nYDelta(static_cast<sal_uInt32>(nYEnd - nYStart));
287 const double fInvYDelta(1.0 / nYDelta);
289 // non-horizontal line, create two parallell entries. These will be sorted by
290 // X anyways, so no need to distinguish the case here
291 maLineEntries.push_back(RasterConversionLineEntry3D(
292 aStart.getX(), (aEnd.getX() - aStart.getX()) * fInvYDelta,
293 aStart.getZ() + fZBufferLineAdd, (aEnd.getZ() - aStart.getZ()) * fInvYDelta,
294 nYStart, nYDelta));
296 RasterConversionLineEntry3D& rEntry = maLineEntries[maLineEntries.size() - 1];
298 // need to choose a X-Distance for the 2nd edge which guarantees all pixels
299 // of the line to be set. This is exactly the X-Increment for one Y-Step.
300 // Same is true for Z, so in both cases, add one increment to them. To also
301 // guarantee one pixel per line, add a minimum of one for X.
302 const double fDistanceX(fabs(rEntry.getX().getInc()) >= 1.0 ? rEntry.getX().getInc() : 1.0);
304 maLineEntries.push_back(RasterConversionLineEntry3D(
305 rEntry.getX().getVal() + fDistanceX, rEntry.getX().getInc(),
306 rEntry.getZ().getVal() + rEntry.getZ().getInc(), rEntry.getZ().getInc(),
307 nYStart, nYDelta));
311 if(!maLineEntries.empty())
313 rasterconvertB3DArea(nStartLine, nStopLine);
317 void RasterConverter3D::rasterconvertB3DPolyPolygon(const B3DPolyPolygon& rFill, const B3DHomMatrix* pViewToEye, sal_Int32 nStartLine, sal_Int32 nStopLine)
319 reset();
320 maLineEntries.clear();
321 addArea(rFill, pViewToEye);
322 rasterconvertB3DArea(nStartLine, nStopLine);
325 void RasterConverter3D::rasterconvertB3DPolygon(const B3DPolygon& rLine, sal_Int32 nStartLine, sal_Int32 nStopLine, sal_uInt16 nLineWidth)
327 const sal_uInt32 nPointCount(rLine.count());
329 if(nPointCount)
331 const sal_uInt32 nEdgeCount(rLine.isClosed() ? nPointCount : nPointCount - 1);
333 for(sal_uInt32 a(0); a < nEdgeCount; a++)
335 rasterconvertB3DEdge(rLine, a, (a + 1) % nPointCount, nStartLine, nStopLine, nLineWidth);
339 } // end of namespace basegfx
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */