LibreOffice Module basegfx (master) 1
rasterconvert3d.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
25
26// implementations of the 3D raster converter
27
28namespace basegfx
29{
30 void RasterConverter3D::addArea(const B3DPolygon& rFill, const B3DHomMatrix* pViewToEye)
31 {
32 const sal_uInt32 nPointCount(rFill.count());
33
34 for(sal_uInt32 a(0); a < nPointCount; a++)
35 {
36 addEdge(rFill, a, (a + 1) % nPointCount, pViewToEye);
37 }
38 }
39
40 void RasterConverter3D::addArea(const B3DPolyPolygon& rFill, const B3DHomMatrix* pViewToEye)
41 {
42 const sal_uInt32 nPolyCount(rFill.count());
43
44 for(sal_uInt32 a(0); a < nPolyCount; a++)
45 {
46 addArea(rFill.getB3DPolygon(a), pViewToEye);
47 }
48 }
49
50 RasterConverter3D::RasterConverter3D()
51 {}
52
53 RasterConverter3D::~RasterConverter3D()
54 {}
55
56 void RasterConverter3D::rasterconvertB3DArea(sal_Int32 nStartLine, sal_Int32 nStopLine)
57 {
58 if(maLineEntries.empty())
59 return;
60
61 OSL_ENSURE(nStopLine >= nStartLine, "nStopLine is bigger than nStartLine (!)");
62
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());
66
67 // local parameters
68 std::vector< RasterConversionLineEntry3D >::iterator aCurrentEntry(maLineEntries.begin());
69 std::vector< RasterConversionLineEntry3D* > aCurrentLine;
70 std::vector< RasterConversionLineEntry3D* > aNextLine;
71 std::vector< RasterConversionLineEntry3D* >::iterator aRasterConversionLineEntry3D;
72
73 // get scanlines first LineNumber as start
74 sal_Int32 nLineNumber(std::max(aCurrentEntry->getY(), nStartLine));
75
76 while((!aCurrentLine.empty() || aCurrentEntry != maLineEntries.end()) && (nLineNumber < nStopLine))
77 {
78 // add all entries which start at current line to current scanline
79 while(aCurrentEntry != maLineEntries.end())
80 {
81 const sal_Int32 nCurrentLineNumber(aCurrentEntry->getY());
82
83 if(nCurrentLineNumber > nLineNumber)
84 {
85 // line is below current one, done (since array is sorted)
86 break;
87 }
88 else
89 {
90 // less or equal. Line is above or at current one. Advance it exactly to
91 // current line
92 const sal_uInt32 nStep(nLineNumber - nCurrentLineNumber);
93
94 if(!nStep || aCurrentEntry->decrementRasterConversionLineEntry3D(nStep))
95 {
96 // add when exactly on current line or when increment to it did not
97 // completely consume it
98 if(nStep)
99 {
100 aCurrentEntry->incrementRasterConversionLineEntry3D(nStep, *this);
101 }
102
103 aCurrentLine.push_back(&(*aCurrentEntry));
104 }
105 }
106
107 ++aCurrentEntry;
108 }
109
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());
115
116 // process current scanline
117 aRasterConversionLineEntry3D = aCurrentLine.begin();
118 aNextLine.clear();
119 sal_uInt32 nPairCount(0);
120
121 while(aRasterConversionLineEntry3D != aCurrentLine.end())
122 {
123 RasterConversionLineEntry3D& rPrevScanRasterConversionLineEntry3D(**aRasterConversionLineEntry3D++);
124
125 // look for 2nd span
126 if(aRasterConversionLineEntry3D != aCurrentLine.end())
127 {
128 // work on span from rPrevScanRasterConversionLineEntry3D to aRasterConversionLineEntry3D, fLineNumber is valid
129 processLineSpan(rPrevScanRasterConversionLineEntry3D, **aRasterConversionLineEntry3D, nLineNumber, nPairCount++);
130 }
131
132 // increment to next line
133 if(rPrevScanRasterConversionLineEntry3D.decrementRasterConversionLineEntry3D(1))
134 {
135 rPrevScanRasterConversionLineEntry3D.incrementRasterConversionLineEntry3D(1, *this);
136 aNextLine.push_back(&rPrevScanRasterConversionLineEntry3D);
137 }
138 }
139
140 // copy back next scanline if count has changed
141 if(aNextLine.size() != aCurrentLine.size())
142 {
143 aCurrentLine = aNextLine;
144 }
145
146 // increment fLineNumber
147 nLineNumber++;
148 }
149 }
150
151 void RasterConverter3D::addEdge(const B3DPolygon& rFill, sal_uInt32 a, sal_uInt32 b, const B3DHomMatrix* pViewToEye)
152 {
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()));
157
158 if(nYStart == nYEnd)
159 return;
160
161 if(nYStart > nYEnd)
162 {
163 std::swap(aStart, aEnd);
164 std::swap(nYStart, nYEnd);
165 std::swap(a, b);
166 }
167
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,
173 nYStart, nYDelta);
174
175 // if extra interpolation data is used, add it to the last created entry
176 RasterConversionLineEntry3D& rEntry = maLineEntries[maLineEntries.size() - 1];
177
178 if(rFill.areBColorsUsed())
179 {
180 rEntry.setColorIndex(addColorInterpolator(rFill.getBColor(a), rFill.getBColor(b), fInvYDelta));
181 }
182
183 if(rFill.areNormalsUsed())
184 {
185 rEntry.setNormalIndex(addNormalInterpolator(rFill.getNormal(a), rFill.getNormal(b), fInvYDelta));
186 }
187
188 if(!rFill.areTextureCoordinatesUsed())
189 return;
190
191 if(pViewToEye)
192 {
193 const double fEyeA(((*pViewToEye) * aStart).getZ());
194 const double fEyeB(((*pViewToEye) * aEnd).getZ());
195
196 rEntry.setInverseTextureIndex(addInverseTextureInterpolator(
197 rFill.getTextureCoordinate(a),
198 rFill.getTextureCoordinate(b),
199 fEyeA, fEyeB, fInvYDelta));
200 }
201 else
202 {
203 rEntry.setTextureIndex(addTextureInterpolator(
204 rFill.getTextureCoordinate(a),
205 rFill.getTextureCoordinate(b),
206 fInvYDelta));
207 }
208 }
209
210 void RasterConverter3D::rasterconvertB3DEdge(const B3DPolygon& rLine, sal_uInt32 nA, sal_uInt32 nB, sal_Int32 nStartLine, sal_Int32 nStopLine, sal_uInt16 nLineWidth)
211 {
212 B3DPoint aStart(rLine.getB3DPoint(nA));
213 B3DPoint aEnd(rLine.getB3DPoint(nB));
214 const double fZBufferLineAdd(0x00ff);
215
216 if(nLineWidth > 1)
217 {
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))
221 {
222 reset();
223 maLineEntries.clear();
224
225 B2DVector aVector(aEnd.getX() - aStart.getX(), aEnd.getY() - aStart.getY());
226 aVector.normalize();
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);
230
231 B3DPolygon aPolygon;
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);
237
238 addArea(aPolygon, nullptr);
239 }
240 }
241 else
242 {
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()));
247
248 if(nYStart == nYEnd)
249 {
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()));
253
254 if(nXStart != nXEnd)
255 {
256 reset();
257 maLineEntries.clear();
258
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(
262 aStart.getX(), 0.0,
263 aStart.getZ() + fZBufferLineAdd, 0.0,
264 nYStart, 1);
265 maLineEntries.emplace_back(
266 aEnd.getX(), 0.0,
267 aEnd.getZ() + fZBufferLineAdd, 0.0,
268 nYStart, 1);
269 }
270 }
271 else
272 {
273 reset();
274 maLineEntries.clear();
275
276 if(nYStart > nYEnd)
277 {
278 std::swap(aStart, aEnd);
279 std::swap(nYStart, nYEnd);
280 }
281
282 const sal_uInt32 nYDelta(static_cast<sal_uInt32>(nYEnd - nYStart));
283 const double fInvYDelta(1.0 / nYDelta);
284
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,
290 nYStart, nYDelta);
291
292 RasterConversionLineEntry3D& rEntry = maLineEntries[maLineEntries.size() - 1];
293
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);
299
300 maLineEntries.emplace_back(
301 rEntry.getX().getVal() + fDistanceX, rEntry.getX().getInc(),
302 rEntry.getZ().getVal() + rEntry.getZ().getInc(), rEntry.getZ().getInc(),
303 nYStart, nYDelta);
304 }
305 }
306
307 if(!maLineEntries.empty())
308 {
309 rasterconvertB3DArea(nStartLine, nStopLine);
310 }
311 }
312
313 void RasterConverter3D::rasterconvertB3DPolyPolygon(const B3DPolyPolygon& rFill, const B3DHomMatrix* pViewToEye, sal_Int32 nStartLine, sal_Int32 nStopLine)
314 {
315 reset();
316 maLineEntries.clear();
317 addArea(rFill, pViewToEye);
318 rasterconvertB3DArea(nStartLine, nStopLine);
319 }
320
321 void RasterConverter3D::rasterconvertB3DPolygon(const B3DPolygon& rLine, sal_Int32 nStartLine, sal_Int32 nStopLine, sal_uInt16 nLineWidth)
322 {
323 const sal_uInt32 nPointCount(rLine.count());
324
325 if(nPointCount)
326 {
327 const sal_uInt32 nEdgeCount(rLine.isClosed() ? nPointCount : nPointCount - 1);
328
329 for(sal_uInt32 a(0); a < nEdgeCount; a++)
330 {
331 rasterconvertB3DEdge(rLine, a, (a + 1) % nPointCount, nStartLine, nStopLine, nLineWidth);
332 }
333 }
334 }
335} // end of namespace basegfx
336
337/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno_Any a
B2DVector getPerpendicular(const B2DVector &rNormalizedVec)
Calculate a perpendicular 2D Vector to the given one.
Definition: b2dvector.cxx:138
B2IRange fround(const B2DRange &rRange)
Round double to nearest integer for 2D range.
Definition: b2drange.cxx:64