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()
53 RasterConverter3D::~RasterConverter3D()
56 void RasterConverter3D::rasterconvertB3DArea(sal_Int32 nStartLine
, sal_Int32 nStopLine
)
58 if(maLineEntries
.empty())
61 OSL_ENSURE(nStopLine
>= nStartLine
, "nStopLine is bigger than nStartLine (!)");
63 // sort global entries by Y, X once. After this, the vector
64 // is seen as frozen. Pointers to its entries will be used in the following code.
65 std::sort(maLineEntries
.begin(), maLineEntries
.end());
68 std::vector
< RasterConversionLineEntry3D
>::iterator
aCurrentEntry(maLineEntries
.begin());
69 std::vector
< RasterConversionLineEntry3D
* > aCurrentLine
;
70 std::vector
< RasterConversionLineEntry3D
* > aNextLine
;
71 std::vector
< RasterConversionLineEntry3D
* >::iterator aRasterConversionLineEntry3D
;
73 // get scanlines first LineNumber as start
74 sal_Int32
nLineNumber(std::max(aCurrentEntry
->getY(), nStartLine
));
76 while((!aCurrentLine
.empty() || aCurrentEntry
!= maLineEntries
.end()) && (nLineNumber
< nStopLine
))
78 // add all entries which start at current line to current scanline
79 while(aCurrentEntry
!= maLineEntries
.end())
81 const sal_Int32
nCurrentLineNumber(aCurrentEntry
->getY());
83 if(nCurrentLineNumber
> nLineNumber
)
85 // line is below current one, done (since array is sorted)
90 // less or equal. Line is above or at current one. Advance it exactly to
92 const sal_uInt32
nStep(nLineNumber
- nCurrentLineNumber
);
94 if(!nStep
|| aCurrentEntry
->decrementRasterConversionLineEntry3D(nStep
))
96 // add when exactly on current line or when increment to it did not
97 // completely consume it
100 aCurrentEntry
->incrementRasterConversionLineEntry3D(nStep
, *this);
103 aCurrentLine
.push_back(&(*aCurrentEntry
));
110 // sort current scanline using comparator. Only X is used there
111 // since all entries are already in one processed line. This needs to be done
112 // every time since not only new spans may have benn added or old removed,
113 // but incrementing may also have changed the order
114 std::sort(aCurrentLine
.begin(), aCurrentLine
.end(), lineComparator());
116 // process current scanline
117 aRasterConversionLineEntry3D
= aCurrentLine
.begin();
119 sal_uInt32
nPairCount(0);
121 while(aRasterConversionLineEntry3D
!= aCurrentLine
.end())
123 RasterConversionLineEntry3D
& rPrevScanRasterConversionLineEntry3D(**aRasterConversionLineEntry3D
++);
126 if(aRasterConversionLineEntry3D
!= aCurrentLine
.end())
128 // work on span from rPrevScanRasterConversionLineEntry3D to aRasterConversionLineEntry3D, fLineNumber is valid
129 processLineSpan(rPrevScanRasterConversionLineEntry3D
, **aRasterConversionLineEntry3D
, nLineNumber
, nPairCount
++);
132 // increment to next line
133 if(rPrevScanRasterConversionLineEntry3D
.decrementRasterConversionLineEntry3D(1))
135 rPrevScanRasterConversionLineEntry3D
.incrementRasterConversionLineEntry3D(1, *this);
136 aNextLine
.push_back(&rPrevScanRasterConversionLineEntry3D
);
140 // copy back next scanline if count has changed
141 if(aNextLine
.size() != aCurrentLine
.size())
143 aCurrentLine
= aNextLine
;
146 // increment fLineNumber
151 void RasterConverter3D::addEdge(const B3DPolygon
& rFill
, sal_uInt32 a
, sal_uInt32 b
, const B3DHomMatrix
* pViewToEye
)
153 B3DPoint
aStart(rFill
.getB3DPoint(a
));
154 B3DPoint
aEnd(rFill
.getB3DPoint(b
));
155 sal_Int32
nYStart(fround(aStart
.getY()));
156 sal_Int32
nYEnd(fround(aEnd
.getY()));
163 std::swap(aStart
, aEnd
);
164 std::swap(nYStart
, nYEnd
);
168 const sal_uInt32
nYDelta(nYEnd
- nYStart
);
169 const double fInvYDelta(1.0 / nYDelta
);
170 maLineEntries
.emplace_back(
171 aStart
.getX(), (aEnd
.getX() - aStart
.getX()) * fInvYDelta
,
172 aStart
.getZ(), (aEnd
.getZ() - aStart
.getZ()) * fInvYDelta
,
175 // if extra interpolation data is used, add it to the last created entry
176 RasterConversionLineEntry3D
& rEntry
= maLineEntries
[maLineEntries
.size() - 1];
178 if(rFill
.areBColorsUsed())
180 rEntry
.setColorIndex(addColorInterpolator(rFill
.getBColor(a
), rFill
.getBColor(b
), fInvYDelta
));
183 if(rFill
.areNormalsUsed())
185 rEntry
.setNormalIndex(addNormalInterpolator(rFill
.getNormal(a
), rFill
.getNormal(b
), fInvYDelta
));
188 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
),
210 void RasterConverter3D::rasterconvertB3DEdge(const B3DPolygon
& rLine
, sal_uInt32 nA
, sal_uInt32 nB
, sal_Int32 nStartLine
, sal_Int32 nStopLine
, sal_uInt16 nLineWidth
)
212 B3DPoint
aStart(rLine
.getB3DPoint(nA
));
213 B3DPoint
aEnd(rLine
.getB3DPoint(nB
));
214 const double fZBufferLineAdd(0x00ff);
218 // this is not a hairline anymore, in most cases since it's an oversampled
219 // hairline to get e.g. AA for Z-Buffering. Create fill geometry.
220 if(!aStart
.equal(aEnd
))
223 maLineEntries
.clear();
225 B2DVector
aVector(aEnd
.getX() - aStart
.getX(), aEnd
.getY() - aStart
.getY());
227 const B2DVector
aPerpend(getPerpendicular(aVector
) * ((static_cast<double>(nLineWidth
) + 0.5) * 0.5));
228 const double fZStartWithAdd(aStart
.getZ() + fZBufferLineAdd
);
229 const double fZEndWithAdd(aEnd
.getZ() + fZBufferLineAdd
);
232 aPolygon
.append(B3DPoint(aStart
.getX() + aPerpend
.getX(), aStart
.getY() + aPerpend
.getY(), fZStartWithAdd
));
233 aPolygon
.append(B3DPoint(aEnd
.getX() + aPerpend
.getX(), aEnd
.getY() + aPerpend
.getY(), fZEndWithAdd
));
234 aPolygon
.append(B3DPoint(aEnd
.getX() - aPerpend
.getX(), aEnd
.getY() - aPerpend
.getY(), fZEndWithAdd
));
235 aPolygon
.append(B3DPoint(aStart
.getX() - aPerpend
.getX(), aStart
.getY() - aPerpend
.getY(), fZStartWithAdd
));
236 aPolygon
.setClosed(true);
238 addArea(aPolygon
, nullptr);
243 // it's a hairline. Use direct RasterConversionLineEntry creation to
244 // rasterconvert lines as similar to areas as possible to avoid Z-Fighting
245 sal_Int32
nYStart(fround(aStart
.getY()));
246 sal_Int32
nYEnd(fround(aEnd
.getY()));
250 // horizontal line, check X
251 const sal_Int32
nXStart(static_cast<sal_Int32
>(aStart
.getX()));
252 const sal_Int32
nXEnd(static_cast<sal_Int32
>(aEnd
.getX()));
257 maLineEntries
.clear();
259 // horizontal line, create vertical entries. These will be sorted by
260 // X anyways, so no need to distinguish the case here
261 maLineEntries
.emplace_back(
263 aStart
.getZ() + fZBufferLineAdd
, 0.0,
265 maLineEntries
.emplace_back(
267 aEnd
.getZ() + fZBufferLineAdd
, 0.0,
274 maLineEntries
.clear();
278 std::swap(aStart
, aEnd
);
279 std::swap(nYStart
, nYEnd
);
282 const sal_uInt32
nYDelta(static_cast<sal_uInt32
>(nYEnd
- nYStart
));
283 const double fInvYDelta(1.0 / nYDelta
);
285 // non-horizontal line, create two parallel entries. These will be sorted by
286 // X anyways, so no need to distinguish the case here
287 maLineEntries
.emplace_back(
288 aStart
.getX(), (aEnd
.getX() - aStart
.getX()) * fInvYDelta
,
289 aStart
.getZ() + fZBufferLineAdd
, (aEnd
.getZ() - aStart
.getZ()) * fInvYDelta
,
292 RasterConversionLineEntry3D
& rEntry
= maLineEntries
[maLineEntries
.size() - 1];
294 // need to choose a X-Distance for the 2nd edge which guarantees all pixels
295 // of the line to be set. This is exactly the X-Increment for one Y-Step.
296 // Same is true for Z, so in both cases, add one increment to them. To also
297 // guarantee one pixel per line, add a minimum of one for X.
298 const double fDistanceX(fabs(rEntry
.getX().getInc()) >= 1.0 ? rEntry
.getX().getInc() : 1.0);
300 maLineEntries
.emplace_back(
301 rEntry
.getX().getVal() + fDistanceX
, rEntry
.getX().getInc(),
302 rEntry
.getZ().getVal() + rEntry
.getZ().getInc(), rEntry
.getZ().getInc(),
307 if(!maLineEntries
.empty())
309 rasterconvertB3DArea(nStartLine
, nStopLine
);
313 void RasterConverter3D::rasterconvertB3DPolyPolygon(const B3DPolyPolygon
& rFill
, const B3DHomMatrix
* pViewToEye
, sal_Int32 nStartLine
, sal_Int32 nStopLine
)
316 maLineEntries
.clear();
317 addArea(rFill
, pViewToEye
);
318 rasterconvertB3DArea(nStartLine
, nStopLine
);
321 void RasterConverter3D::rasterconvertB3DPolygon(const B3DPolygon
& rLine
, sal_Int32 nStartLine
, sal_Int32 nStopLine
, sal_uInt16 nLineWidth
)
323 const sal_uInt32
nPointCount(rLine
.count());
327 const sal_uInt32
nEdgeCount(rLine
.isClosed() ? nPointCount
: nPointCount
- 1);
329 for(sal_uInt32
a(0); a
< nEdgeCount
; a
++)
331 rasterconvertB3DEdge(rLine
, a
, (a
+ 1) % nPointCount
, nStartLine
, nStopLine
, nLineWidth
);
335 } // end of namespace basegfx
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */