1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/vector/b2dvector.hxx>
22 #include <basegfx/polygon/b3dpolygon.hxx>
23 #include <basegfx/polygon/b3dpolypolygon.hxx>
24 #include <basegfx/point/b3dpoint.hxx>
26 // implementations of the 3D raster converter
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(),
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 its entries will be used in the following code.
66 std::sort(maLineEntries
.begin(), maLineEntries
.end());
69 std::vector
< RasterConversionLineEntry3D
>::iterator
aCurrentEntry(maLineEntries
.begin());
70 std::vector
< RasterConversionLineEntry3D
* > aCurrentLine
;
71 std::vector
< RasterConversionLineEntry3D
* > aNextLine
;
72 std::vector
< RasterConversionLineEntry3D
* >::iterator aRasterConversionLineEntry3D
;
74 // get scanlines first LineNumber as start
75 sal_Int32
nLineNumber(std::max(aCurrentEntry
->getY(), nStartLine
));
77 while((!aCurrentLine
.empty() || aCurrentEntry
!= maLineEntries
.end()) && (nLineNumber
< nStopLine
))
79 // add all entries which start at current line to current scanline
80 while(aCurrentEntry
!= maLineEntries
.end())
82 const sal_Int32
nCurrentLineNumber(aCurrentEntry
->getY());
84 if(nCurrentLineNumber
> nLineNumber
)
86 // line is below current one, done (since array is sorted)
91 // less or equal. Line is above or at current one. Advance it exactly to
93 const sal_uInt32
nStep(nLineNumber
- nCurrentLineNumber
);
95 if(!nStep
|| aCurrentEntry
->decrementRasterConversionLineEntry3D(nStep
))
97 // add when exactly on current line or when increment to it did not
98 // completely consume it
101 aCurrentEntry
->incrementRasterConversionLineEntry3D(nStep
, *this);
104 aCurrentLine
.push_back(&(*aCurrentEntry
));
111 // sort current scanline using comparator. Only X is used there
112 // since all entries are already in one processed line. This needs to be done
113 // every time since not only new spans may have benn added or old removed,
114 // but incrementing may also have changed the order
115 std::sort(aCurrentLine
.begin(), aCurrentLine
.end(), lineComparator());
117 // process current scanline
118 aRasterConversionLineEntry3D
= aCurrentLine
.begin();
120 sal_uInt32
nPairCount(0);
122 while(aRasterConversionLineEntry3D
!= aCurrentLine
.end())
124 RasterConversionLineEntry3D
& rPrevScanRasterConversionLineEntry3D(**aRasterConversionLineEntry3D
++);
127 if(aRasterConversionLineEntry3D
!= aCurrentLine
.end())
129 // work on span from rPrevScanRasterConversionLineEntry3D to aRasterConversionLineEntry3D, fLineNumber is valid
130 processLineSpan(rPrevScanRasterConversionLineEntry3D
, **aRasterConversionLineEntry3D
, nLineNumber
, nPairCount
++);
133 // increment to next line
134 if(rPrevScanRasterConversionLineEntry3D
.decrementRasterConversionLineEntry3D(1))
136 rPrevScanRasterConversionLineEntry3D
.incrementRasterConversionLineEntry3D(1, *this);
137 aNextLine
.push_back(&rPrevScanRasterConversionLineEntry3D
);
141 // copy back next scanline if count has changed
142 if(aNextLine
.size() != aCurrentLine
.size())
144 aCurrentLine
= aNextLine
;
147 // increment fLineNumber
153 void RasterConverter3D::addEdge(const B3DPolygon
& rFill
, sal_uInt32 a
, sal_uInt32 b
, const B3DHomMatrix
* pViewToEye
)
155 B3DPoint
aStart(rFill
.getB3DPoint(a
));
156 B3DPoint
aEnd(rFill
.getB3DPoint(b
));
157 sal_Int32
nYStart(fround(aStart
.getY()));
158 sal_Int32
nYEnd(fround(aEnd
.getY()));
164 std::swap(aStart
, aEnd
);
165 std::swap(nYStart
, nYEnd
);
169 const sal_uInt32
nYDelta(nYEnd
- nYStart
);
170 const double fInvYDelta(1.0 / nYDelta
);
171 maLineEntries
.emplace_back(
172 aStart
.getX(), (aEnd
.getX() - aStart
.getX()) * fInvYDelta
,
173 aStart
.getZ(), (aEnd
.getZ() - aStart
.getZ()) * fInvYDelta
,
176 // if extra interpolation data is used, add it to the last created entry
177 RasterConversionLineEntry3D
& rEntry
= maLineEntries
[maLineEntries
.size() - 1];
179 if(rFill
.areBColorsUsed())
181 rEntry
.setColorIndex(addColorInterpolator(rFill
.getBColor(a
), rFill
.getBColor(b
), fInvYDelta
));
184 if(rFill
.areNormalsUsed())
186 rEntry
.setNormalIndex(addNormalInterpolator(rFill
.getNormal(a
), rFill
.getNormal(b
), fInvYDelta
));
189 if(rFill
.areTextureCoordinatesUsed())
193 const double fEyeA(((*pViewToEye
) * aStart
).getZ());
194 const double fEyeB(((*pViewToEye
) * aEnd
).getZ());
196 rEntry
.setInverseTextureIndex(addInverseTextureInterpolator(
197 rFill
.getTextureCoordinate(a
),
198 rFill
.getTextureCoordinate(b
),
199 fEyeA
, fEyeB
, fInvYDelta
));
203 rEntry
.setTextureIndex(addTextureInterpolator(
204 rFill
.getTextureCoordinate(a
),
205 rFill
.getTextureCoordinate(b
),
212 void RasterConverter3D::rasterconvertB3DEdge(const B3DPolygon
& rLine
, sal_uInt32 nA
, sal_uInt32 nB
, sal_Int32 nStartLine
, sal_Int32 nStopLine
, sal_uInt16 nLineWidth
)
214 B3DPoint
aStart(rLine
.getB3DPoint(nA
));
215 B3DPoint
aEnd(rLine
.getB3DPoint(nB
));
216 const double fZBufferLineAdd(0x00ff);
220 // this is not a hairline anymore, in most cases since it's an oversampled
221 // hairline to get e.g. AA for Z-Buffering. Create fill geometry.
222 if(!aStart
.equal(aEnd
))
225 maLineEntries
.clear();
227 B2DVector
aVector(aEnd
.getX() - aStart
.getX(), aEnd
.getY() - aStart
.getY());
229 const B2DVector
aPerpend(getPerpendicular(aVector
) * ((static_cast<double>(nLineWidth
) + 0.5) * 0.5));
230 const double fZStartWithAdd(aStart
.getZ() + fZBufferLineAdd
);
231 const double fZEndWithAdd(aEnd
.getZ() + fZBufferLineAdd
);
234 aPolygon
.append(B3DPoint(aStart
.getX() + aPerpend
.getX(), aStart
.getY() + aPerpend
.getY(), fZStartWithAdd
));
235 aPolygon
.append(B3DPoint(aEnd
.getX() + aPerpend
.getX(), aEnd
.getY() + aPerpend
.getY(), fZEndWithAdd
));
236 aPolygon
.append(B3DPoint(aEnd
.getX() - aPerpend
.getX(), aEnd
.getY() - aPerpend
.getY(), fZEndWithAdd
));
237 aPolygon
.append(B3DPoint(aStart
.getX() - aPerpend
.getX(), aStart
.getY() - aPerpend
.getY(), fZStartWithAdd
));
238 aPolygon
.setClosed(true);
240 addArea(aPolygon
, nullptr);
245 // it's a hairline. Use direct RasterConversionLineEntry creation to
246 // rasterconvert lines as similar to areas as possible to avoid Z-Fighting
247 sal_Int32
nYStart(fround(aStart
.getY()));
248 sal_Int32
nYEnd(fround(aEnd
.getY()));
252 // horizontal line, check X
253 const sal_Int32
nXStart(static_cast<sal_Int32
>(aStart
.getX()));
254 const sal_Int32
nXEnd(static_cast<sal_Int32
>(aEnd
.getX()));
259 maLineEntries
.clear();
261 // horizontal line, create vertical entries. These will be sorted by
262 // X anyways, so no need to distinguish the case here
263 maLineEntries
.emplace_back(
265 aStart
.getZ() + fZBufferLineAdd
, 0.0,
267 maLineEntries
.emplace_back(
269 aEnd
.getZ() + fZBufferLineAdd
, 0.0,
276 maLineEntries
.clear();
280 std::swap(aStart
, aEnd
);
281 std::swap(nYStart
, nYEnd
);
284 const sal_uInt32
nYDelta(static_cast<sal_uInt32
>(nYEnd
- nYStart
));
285 const double fInvYDelta(1.0 / nYDelta
);
287 // non-horizontal line, create two parallel entries. These will be sorted by
288 // X anyways, so no need to distinguish the case here
289 maLineEntries
.emplace_back(
290 aStart
.getX(), (aEnd
.getX() - aStart
.getX()) * fInvYDelta
,
291 aStart
.getZ() + fZBufferLineAdd
, (aEnd
.getZ() - aStart
.getZ()) * fInvYDelta
,
294 RasterConversionLineEntry3D
& rEntry
= maLineEntries
[maLineEntries
.size() - 1];
296 // need to choose a X-Distance for the 2nd edge which guarantees all pixels
297 // of the line to be set. This is exactly the X-Increment for one Y-Step.
298 // Same is true for Z, so in both cases, add one increment to them. To also
299 // guarantee one pixel per line, add a minimum of one for X.
300 const double fDistanceX(fabs(rEntry
.getX().getInc()) >= 1.0 ? rEntry
.getX().getInc() : 1.0);
302 maLineEntries
.emplace_back(
303 rEntry
.getX().getVal() + fDistanceX
, rEntry
.getX().getInc(),
304 rEntry
.getZ().getVal() + rEntry
.getZ().getInc(), rEntry
.getZ().getInc(),
309 if(!maLineEntries
.empty())
311 rasterconvertB3DArea(nStartLine
, nStopLine
);
315 void RasterConverter3D::rasterconvertB3DPolyPolygon(const B3DPolyPolygon
& rFill
, const B3DHomMatrix
* pViewToEye
, sal_Int32 nStartLine
, sal_Int32 nStopLine
)
318 maLineEntries
.clear();
319 addArea(rFill
, pViewToEye
);
320 rasterconvertB3DArea(nStartLine
, nStopLine
);
323 void RasterConverter3D::rasterconvertB3DPolygon(const B3DPolygon
& rLine
, sal_Int32 nStartLine
, sal_Int32 nStopLine
, sal_uInt16 nLineWidth
)
325 const sal_uInt32
nPointCount(rLine
.count());
329 const sal_uInt32
nEdgeCount(rLine
.isClosed() ? nPointCount
: nPointCount
- 1);
331 for(sal_uInt32
a(0); a
< nEdgeCount
; a
++)
333 rasterconvertB3DEdge(rLine
, a
, (a
+ 1) % nPointCount
, nStartLine
, nStopLine
, nLineWidth
);
337 } // end of namespace basegfx
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */