Line data Source code
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 :
20 : #include <basegfx/raster/rasterconvert3d.hxx>
21 : #include <basegfx/polygon/b3dpolygon.hxx>
22 : #include <basegfx/polygon/b3dpolypolygon.hxx>
23 : #include <basegfx/point/b3dpoint.hxx>
24 :
25 : // implementations of the 3D raster converter
26 :
27 : namespace basegfx
28 : {
29 18402 : void RasterConverter3D::addArea(const B3DPolygon& rFill, const B3DHomMatrix* pViewToEye)
30 : {
31 18402 : const sal_uInt32 nPointCount(rFill.count());
32 :
33 88106 : for(sal_uInt32 a(0); a < nPointCount; a++)
34 : {
35 69704 : addEdge(rFill, a, (a + 1) % nPointCount, pViewToEye);
36 : }
37 18402 : }
38 :
39 18402 : void RasterConverter3D::addArea(const B3DPolyPolygon& rFill, const B3DHomMatrix* pViewToEye)
40 : {
41 18402 : const sal_uInt32 nPolyCount(rFill.count());
42 :
43 36804 : for(sal_uInt32 a(0); a < nPolyCount; a++)
44 : {
45 18402 : addArea(rFill.getB3DPolygon(a), pViewToEye);
46 : }
47 18402 : }
48 :
49 13 : RasterConverter3D::RasterConverter3D()
50 : : InterpolatorProvider3D(),
51 13 : maLineEntries()
52 13 : {}
53 :
54 13 : RasterConverter3D::~RasterConverter3D()
55 13 : {}
56 :
57 18747 : void RasterConverter3D::rasterconvertB3DArea(sal_Int32 nStartLine, sal_Int32 nStopLine)
58 : {
59 18747 : if(!maLineEntries.empty())
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 it's entries will be used in the following code.
65 12473 : ::std::sort(maLineEntries.begin(), maLineEntries.end());
66 :
67 : // local parameters
68 12473 : ::std::vector< RasterConversionLineEntry3D >::iterator aCurrentEntry(maLineEntries.begin());
69 12473 : ::std::vector< RasterConversionLineEntry3D* > aCurrentLine;
70 24946 : ::std::vector< RasterConversionLineEntry3D* > aNextLine;
71 12473 : ::std::vector< RasterConversionLineEntry3D* >::iterator aRasterConversionLineEntry3D;
72 12473 : sal_uInt32 nPairCount(0);
73 :
74 : // get scanlines first LineNumber as start
75 12473 : sal_Int32 nLineNumber(::std::max(aCurrentEntry->getY(), nStartLine));
76 :
77 201097 : while((aCurrentLine.size() || aCurrentEntry != maLineEntries.end()) && (nLineNumber < nStopLine))
78 : {
79 : // add all entries which start at current line to current scanline
80 381295 : while(aCurrentEntry != maLineEntries.end())
81 : {
82 94233 : const sal_Int32 nCurrentLineNumber(aCurrentEntry->getY());
83 :
84 94233 : if(nCurrentLineNumber > nLineNumber)
85 : {
86 : // line is below current one, done (since array is sorted)
87 65240 : break;
88 : }
89 : else
90 : {
91 : // less or equal. Line is above or at current one. Advance it exactly to
92 : // current line
93 28993 : const sal_uInt32 nStep(nLineNumber - nCurrentLineNumber);
94 :
95 28993 : if(!nStep || aCurrentEntry->decrementRasterConversionLineEntry3D(nStep))
96 : {
97 : // add when exactly on current line or when incremet to it did not
98 : // completely consume it
99 28993 : if(nStep)
100 : {
101 0 : aCurrentEntry->incrementRasterConversionLineEntry3D(nStep, *this);
102 : }
103 :
104 28993 : aCurrentLine.push_back(&(*(aCurrentEntry)));
105 : }
106 : }
107 :
108 28993 : ++aCurrentEntry;
109 : }
110 :
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 176151 : ::std::sort(aCurrentLine.begin(), aCurrentLine.end(), lineComparator());
116 :
117 : // process current scanline
118 176151 : aRasterConversionLineEntry3D = aCurrentLine.begin();
119 176151 : aNextLine.clear();
120 176151 : nPairCount = 0;
121 :
122 704604 : while(aRasterConversionLineEntry3D != aCurrentLine.end())
123 : {
124 352302 : RasterConversionLineEntry3D& rPrevScanRasterConversionLineEntry3D(**aRasterConversionLineEntry3D++);
125 :
126 : // look for 2nd span
127 352302 : if(aRasterConversionLineEntry3D != aCurrentLine.end())
128 : {
129 : // work on span from rPrevScanRasterConversionLineEntry3D to aRasterConversionLineEntry3D, fLineNumber is valid
130 176151 : processLineSpan(rPrevScanRasterConversionLineEntry3D, **aRasterConversionLineEntry3D, nLineNumber, nPairCount++);
131 : }
132 :
133 : // increment to next line
134 352302 : if(rPrevScanRasterConversionLineEntry3D.decrementRasterConversionLineEntry3D(1))
135 : {
136 323610 : rPrevScanRasterConversionLineEntry3D.incrementRasterConversionLineEntry3D(1, *this);
137 323610 : aNextLine.push_back(&rPrevScanRasterConversionLineEntry3D);
138 : }
139 : }
140 :
141 : // copy back next scanline if count has changed
142 176151 : if(aNextLine.size() != aCurrentLine.size())
143 : {
144 16222 : aCurrentLine = aNextLine;
145 : }
146 :
147 : // increment fLineNumber
148 176151 : nLineNumber++;
149 12473 : }
150 : }
151 18747 : }
152 :
153 69704 : void RasterConverter3D::addEdge(const B3DPolygon& rFill, sal_uInt32 a, sal_uInt32 b, const B3DHomMatrix* pViewToEye)
154 : {
155 69704 : B3DPoint aStart(rFill.getB3DPoint(a));
156 139408 : B3DPoint aEnd(rFill.getB3DPoint(b));
157 69704 : sal_Int32 nYStart(fround(aStart.getY()));
158 69704 : sal_Int32 nYEnd(fround(aEnd.getY()));
159 :
160 69704 : if(nYStart != nYEnd)
161 : {
162 28449 : if(nYStart > nYEnd)
163 : {
164 14262 : ::std::swap(aStart, aEnd);
165 14262 : ::std::swap(nYStart, nYEnd);
166 14262 : ::std::swap(a, b);
167 : }
168 :
169 28449 : const sal_uInt32 nYDelta(nYEnd - nYStart);
170 28449 : const double fInvYDelta(1.0 / nYDelta);
171 : maLineEntries.push_back(RasterConversionLineEntry3D(
172 56898 : aStart.getX(), (aEnd.getX() - aStart.getX()) * fInvYDelta,
173 56898 : aStart.getZ(), (aEnd.getZ() - aStart.getZ()) * fInvYDelta,
174 142245 : nYStart, nYDelta));
175 :
176 : // if extra interpolation data is used, add it to the last created entry
177 28449 : RasterConversionLineEntry3D& rEntry = maLineEntries[maLineEntries.size() - 1];
178 :
179 28449 : if(rFill.areBColorsUsed())
180 : {
181 656 : rEntry.setColorIndex(addColorInterpolator(rFill.getBColor(a), rFill.getBColor(b), fInvYDelta));
182 : }
183 :
184 28449 : if(rFill.areNormalsUsed())
185 : {
186 0 : rEntry.setNormalIndex(addNormalInterpolator(rFill.getNormal(a), rFill.getNormal(b), fInvYDelta));
187 : }
188 :
189 28449 : if(rFill.areTextureCoordinatesUsed())
190 : {
191 656 : if(pViewToEye)
192 : {
193 656 : const double fEyeA(((*pViewToEye) * aStart).getZ());
194 656 : const double fEyeB(((*pViewToEye) * aEnd).getZ());
195 :
196 : rEntry.setInverseTextureIndex(addInverseTextureInterpolator(
197 : rFill.getTextureCoordinate(a),
198 : rFill.getTextureCoordinate(b),
199 656 : fEyeA, fEyeB, fInvYDelta));
200 : }
201 : else
202 : {
203 : rEntry.setTextureIndex(addTextureInterpolator(
204 : rFill.getTextureCoordinate(a),
205 : rFill.getTextureCoordinate(b),
206 0 : fInvYDelta));
207 : }
208 : }
209 69704 : }
210 69704 : }
211 :
212 345 : void RasterConverter3D::rasterconvertB3DEdge(const B3DPolygon& rLine, sal_uInt32 nA, sal_uInt32 nB, sal_Int32 nStartLine, sal_Int32 nStopLine, sal_uInt16 nLineWidth)
213 : {
214 345 : B3DPoint aStart(rLine.getB3DPoint(nA));
215 690 : B3DPoint aEnd(rLine.getB3DPoint(nB));
216 345 : const double fZBufferLineAdd(0x00ff);
217 : static bool bForceToPolygon(false);
218 :
219 345 : if(nLineWidth > 1 || bForceToPolygon)
220 : {
221 : // this is not a hairline anymore, in most cases since it's an oversampled
222 : // hairline to get e.g. AA for Z-Buffering. Create fill geometry.
223 0 : if(!aStart.equal(aEnd))
224 : {
225 0 : reset();
226 0 : maLineEntries.clear();
227 :
228 0 : B2DVector aVector(aEnd.getX() - aStart.getX(), aEnd.getY() - aStart.getY());
229 0 : aVector.normalize();
230 0 : const B2DVector aPerpend(getPerpendicular(aVector) * ((static_cast<double>(nLineWidth) + 0.5) * 0.5));
231 0 : const double fZStartWithAdd(aStart.getZ() + fZBufferLineAdd);
232 0 : const double fZEndWithAdd(aEnd.getZ() + fZBufferLineAdd);
233 :
234 0 : B3DPolygon aPolygon;
235 0 : aPolygon.append(B3DPoint(aStart.getX() + aPerpend.getX(), aStart.getY() + aPerpend.getY(), fZStartWithAdd));
236 0 : aPolygon.append(B3DPoint(aEnd.getX() + aPerpend.getX(), aEnd.getY() + aPerpend.getY(), fZEndWithAdd));
237 0 : aPolygon.append(B3DPoint(aEnd.getX() - aPerpend.getX(), aEnd.getY() - aPerpend.getY(), fZEndWithAdd));
238 0 : aPolygon.append(B3DPoint(aStart.getX() - aPerpend.getX(), aStart.getY() - aPerpend.getY(), fZStartWithAdd));
239 0 : aPolygon.setClosed(true);
240 :
241 0 : addArea(aPolygon, 0);
242 0 : }
243 : }
244 : else
245 : {
246 : // it's a hairline. Use direct RasterConversionLineEntry creation to
247 : // rasterconvert lines as similar to areas as possible to avoid Z-Fighting
248 345 : sal_Int32 nYStart(fround(aStart.getY()));
249 345 : sal_Int32 nYEnd(fround(aEnd.getY()));
250 :
251 345 : if(nYStart == nYEnd)
252 : {
253 : // horizontal line, check X
254 0 : const sal_Int32 nXStart(static_cast<sal_Int32>(aStart.getX()));
255 0 : const sal_Int32 nXEnd(static_cast<sal_Int32>(aEnd.getX()));
256 :
257 0 : if(nXStart != nXEnd)
258 : {
259 0 : reset();
260 0 : maLineEntries.clear();
261 :
262 : // horizontal line, create vertical entries. These will be sorted by
263 : // X anyways, so no need to distinguish the case here
264 : maLineEntries.push_back(RasterConversionLineEntry3D(
265 0 : aStart.getX(), 0.0,
266 0 : aStart.getZ() + fZBufferLineAdd, 0.0,
267 0 : nYStart, 1));
268 : maLineEntries.push_back(RasterConversionLineEntry3D(
269 0 : aEnd.getX(), 0.0,
270 0 : aEnd.getZ() + fZBufferLineAdd, 0.0,
271 0 : nYStart, 1));
272 : }
273 : }
274 : else
275 : {
276 345 : reset();
277 345 : maLineEntries.clear();
278 :
279 345 : if(nYStart > nYEnd)
280 : {
281 230 : ::std::swap(aStart, aEnd);
282 230 : ::std::swap(nYStart, nYEnd);
283 : }
284 :
285 345 : const sal_uInt32 nYDelta(static_cast<sal_uInt32>(nYEnd - nYStart));
286 345 : const double fInvYDelta(1.0 / nYDelta);
287 :
288 : // non-horizontal line, create two parallell entries. These will be sorted by
289 : // X anyways, so no need to distinguish the case here
290 : maLineEntries.push_back(RasterConversionLineEntry3D(
291 690 : aStart.getX(), (aEnd.getX() - aStart.getX()) * fInvYDelta,
292 690 : aStart.getZ() + fZBufferLineAdd, (aEnd.getZ() - aStart.getZ()) * fInvYDelta,
293 1725 : nYStart, nYDelta));
294 :
295 345 : RasterConversionLineEntry3D& rEntry = maLineEntries[maLineEntries.size() - 1];
296 :
297 : // need to choose a X-Distance for the 2nd edge which guarantees all pixels
298 : // of the line to be set. This is exactly the X-Increment for one Y-Step.
299 : // Same is true for Z, so in both cases, add one increment to them. To also
300 : // guarantee one pixel per line, add a minimum of one for X.
301 345 : const double fDistanceX(fabs(rEntry.getX().getInc()) >= 1.0 ? rEntry.getX().getInc() : 1.0);
302 :
303 : maLineEntries.push_back(RasterConversionLineEntry3D(
304 690 : rEntry.getX().getVal() + fDistanceX, rEntry.getX().getInc(),
305 690 : rEntry.getZ().getVal() + rEntry.getZ().getInc(), rEntry.getZ().getInc(),
306 1725 : nYStart, nYDelta));
307 : }
308 : }
309 :
310 345 : if(!maLineEntries.empty())
311 : {
312 345 : rasterconvertB3DArea(nStartLine, nStopLine);
313 345 : }
314 345 : }
315 :
316 18402 : void RasterConverter3D::rasterconvertB3DPolyPolygon(const B3DPolyPolygon& rFill, const B3DHomMatrix* pViewToEye, sal_Int32 nStartLine, sal_Int32 nStopLine)
317 : {
318 18402 : reset();
319 18402 : maLineEntries.clear();
320 18402 : addArea(rFill, pViewToEye);
321 18402 : rasterconvertB3DArea(nStartLine, nStopLine);
322 18402 : }
323 :
324 115 : void RasterConverter3D::rasterconvertB3DPolygon(const B3DPolygon& rLine, sal_Int32 nStartLine, sal_Int32 nStopLine, sal_uInt16 nLineWidth)
325 : {
326 115 : const sal_uInt32 nPointCount(rLine.count());
327 :
328 115 : if(nPointCount)
329 : {
330 115 : const sal_uInt32 nEdgeCount(rLine.isClosed() ? nPointCount : nPointCount - 1);
331 :
332 460 : for(sal_uInt32 a(0); a < nEdgeCount; a++)
333 : {
334 345 : rasterconvertB3DEdge(rLine, a, (a + 1) % nPointCount, nStartLine, nStopLine, nLineWidth);
335 : }
336 : }
337 115 : }
338 : } // end of namespace basegfx
339 :
340 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|