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 37447 : void RasterConverter3D::addArea(const B3DPolygon& rFill, const B3DHomMatrix* pViewToEye)
30 : {
31 37447 : const sal_uInt32 nPointCount(rFill.count());
32 :
33 179283 : for(sal_uInt32 a(0); a < nPointCount; a++)
34 : {
35 141836 : addEdge(rFill, a, (a + 1) % nPointCount, pViewToEye);
36 : }
37 37447 : }
38 :
39 37447 : void RasterConverter3D::addArea(const B3DPolyPolygon& rFill, const B3DHomMatrix* pViewToEye)
40 : {
41 37447 : const sal_uInt32 nPolyCount(rFill.count());
42 :
43 74894 : for(sal_uInt32 a(0); a < nPolyCount; a++)
44 : {
45 37447 : addArea(rFill.getB3DPolygon(a), pViewToEye);
46 : }
47 37447 : }
48 :
49 21 : RasterConverter3D::RasterConverter3D()
50 : : InterpolatorProvider3D(),
51 21 : maLineEntries()
52 21 : {}
53 :
54 21 : RasterConverter3D::~RasterConverter3D()
55 21 : {}
56 :
57 38095 : void RasterConverter3D::rasterconvertB3DArea(sal_Int32 nStartLine, sal_Int32 nStopLine)
58 : {
59 38095 : 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 25873 : ::std::sort(maLineEntries.begin(), maLineEntries.end());
66 :
67 : // local parameters
68 25873 : ::std::vector< RasterConversionLineEntry3D >::iterator aCurrentEntry(maLineEntries.begin());
69 25873 : ::std::vector< RasterConversionLineEntry3D* > aCurrentLine;
70 51746 : ::std::vector< RasterConversionLineEntry3D* > aNextLine;
71 25873 : ::std::vector< RasterConversionLineEntry3D* >::iterator aRasterConversionLineEntry3D;
72 25873 : sal_uInt32 nPairCount(0);
73 :
74 : // get scanlines first LineNumber as start
75 25873 : sal_Int32 nLineNumber(::std::max(aCurrentEntry->getY(), nStartLine));
76 :
77 449775 : while((aCurrentLine.size() || aCurrentEntry != maLineEntries.end()) && (nLineNumber < nStopLine))
78 : {
79 : // add all entries which start at current line to current scanline
80 857578 : while(aCurrentEntry != maLineEntries.end())
81 : {
82 174456 : const sal_Int32 nCurrentLineNumber(aCurrentEntry->getY());
83 :
84 174456 : if(nCurrentLineNumber > nLineNumber)
85 : {
86 : // line is below current one, done (since array is sorted)
87 112936 : break;
88 : }
89 : else
90 : {
91 : // less or equal. Line is above or at current one. Advance it exactly to
92 : // current line
93 61520 : const sal_uInt32 nStep(nLineNumber - nCurrentLineNumber);
94 :
95 61520 : 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 61520 : if(nStep)
100 : {
101 0 : aCurrentEntry->incrementRasterConversionLineEntry3D(nStep, *this);
102 : }
103 :
104 61520 : aCurrentLine.push_back(&(*(aCurrentEntry)));
105 : }
106 : }
107 :
108 61520 : ++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 398029 : ::std::sort(aCurrentLine.begin(), aCurrentLine.end(), lineComparator());
116 :
117 : // process current scanline
118 398029 : aRasterConversionLineEntry3D = aCurrentLine.begin();
119 398029 : aNextLine.clear();
120 398029 : nPairCount = 0;
121 :
122 1592116 : while(aRasterConversionLineEntry3D != aCurrentLine.end())
123 : {
124 796058 : RasterConversionLineEntry3D& rPrevScanRasterConversionLineEntry3D(**aRasterConversionLineEntry3D++);
125 :
126 : // look for 2nd span
127 796058 : if(aRasterConversionLineEntry3D != aCurrentLine.end())
128 : {
129 : // work on span from rPrevScanRasterConversionLineEntry3D to aRasterConversionLineEntry3D, fLineNumber is valid
130 398029 : processLineSpan(rPrevScanRasterConversionLineEntry3D, **aRasterConversionLineEntry3D, nLineNumber, nPairCount++);
131 : }
132 :
133 : // increment to next line
134 796058 : if(rPrevScanRasterConversionLineEntry3D.decrementRasterConversionLineEntry3D(1))
135 : {
136 734654 : rPrevScanRasterConversionLineEntry3D.incrementRasterConversionLineEntry3D(1, *this);
137 734654 : aNextLine.push_back(&rPrevScanRasterConversionLineEntry3D);
138 : }
139 : }
140 :
141 : // copy back next scanline if count has changed
142 398029 : if(aNextLine.size() != aCurrentLine.size())
143 : {
144 35008 : aCurrentLine = aNextLine;
145 : }
146 :
147 : // increment fLineNumber
148 398029 : nLineNumber++;
149 25873 : }
150 : }
151 38095 : }
152 :
153 141836 : void RasterConverter3D::addEdge(const B3DPolygon& rFill, sal_uInt32 a, sal_uInt32 b, const B3DHomMatrix* pViewToEye)
154 : {
155 141836 : B3DPoint aStart(rFill.getB3DPoint(a));
156 283672 : B3DPoint aEnd(rFill.getB3DPoint(b));
157 141836 : sal_Int32 nYStart(fround(aStart.getY()));
158 141836 : sal_Int32 nYEnd(fround(aEnd.getY()));
159 :
160 141836 : if(nYStart != nYEnd)
161 : {
162 60262 : if(nYStart > nYEnd)
163 : {
164 30274 : ::std::swap(aStart, aEnd);
165 30274 : ::std::swap(nYStart, nYEnd);
166 30274 : ::std::swap(a, b);
167 : }
168 :
169 60262 : const sal_uInt32 nYDelta(nYEnd - nYStart);
170 60262 : const double fInvYDelta(1.0 / nYDelta);
171 : maLineEntries.push_back(RasterConversionLineEntry3D(
172 120524 : aStart.getX(), (aEnd.getX() - aStart.getX()) * fInvYDelta,
173 120524 : aStart.getZ(), (aEnd.getZ() - aStart.getZ()) * fInvYDelta,
174 301310 : nYStart, nYDelta));
175 :
176 : // if extra interpolation data is used, add it to the last created entry
177 60262 : RasterConversionLineEntry3D& rEntry = maLineEntries[maLineEntries.size() - 1];
178 :
179 60262 : if(rFill.areBColorsUsed())
180 : {
181 132 : rEntry.setColorIndex(addColorInterpolator(rFill.getBColor(a), rFill.getBColor(b), fInvYDelta));
182 : }
183 :
184 60262 : if(rFill.areNormalsUsed())
185 : {
186 0 : rEntry.setNormalIndex(addNormalInterpolator(rFill.getNormal(a), rFill.getNormal(b), fInvYDelta));
187 : }
188 :
189 60262 : if(rFill.areTextureCoordinatesUsed())
190 : {
191 132 : if(pViewToEye)
192 : {
193 132 : const double fEyeA(((*pViewToEye) * aStart).getZ());
194 132 : const double fEyeB(((*pViewToEye) * aEnd).getZ());
195 :
196 : rEntry.setInverseTextureIndex(addInverseTextureInterpolator(
197 : rFill.getTextureCoordinate(a),
198 : rFill.getTextureCoordinate(b),
199 132 : 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 141836 : }
210 141836 : }
211 :
212 648 : void RasterConverter3D::rasterconvertB3DEdge(const B3DPolygon& rLine, sal_uInt32 nA, sal_uInt32 nB, sal_Int32 nStartLine, sal_Int32 nStopLine, sal_uInt16 nLineWidth)
213 : {
214 648 : B3DPoint aStart(rLine.getB3DPoint(nA));
215 1296 : B3DPoint aEnd(rLine.getB3DPoint(nB));
216 648 : const double fZBufferLineAdd(0x00ff);
217 : static bool bForceToPolygon(false);
218 :
219 648 : 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 648 : sal_Int32 nYStart(fround(aStart.getY()));
249 648 : sal_Int32 nYEnd(fround(aEnd.getY()));
250 :
251 648 : 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 648 : reset();
277 648 : maLineEntries.clear();
278 :
279 648 : if(nYStart > nYEnd)
280 : {
281 432 : ::std::swap(aStart, aEnd);
282 432 : ::std::swap(nYStart, nYEnd);
283 : }
284 :
285 648 : const sal_uInt32 nYDelta(static_cast<sal_uInt32>(nYEnd - nYStart));
286 648 : 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 1296 : aStart.getX(), (aEnd.getX() - aStart.getX()) * fInvYDelta,
292 1296 : aStart.getZ() + fZBufferLineAdd, (aEnd.getZ() - aStart.getZ()) * fInvYDelta,
293 3240 : nYStart, nYDelta));
294 :
295 648 : 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 648 : const double fDistanceX(fabs(rEntry.getX().getInc()) >= 1.0 ? rEntry.getX().getInc() : 1.0);
302 :
303 : maLineEntries.push_back(RasterConversionLineEntry3D(
304 1296 : rEntry.getX().getVal() + fDistanceX, rEntry.getX().getInc(),
305 1296 : rEntry.getZ().getVal() + rEntry.getZ().getInc(), rEntry.getZ().getInc(),
306 3240 : nYStart, nYDelta));
307 : }
308 : }
309 :
310 648 : if(!maLineEntries.empty())
311 : {
312 648 : rasterconvertB3DArea(nStartLine, nStopLine);
313 648 : }
314 648 : }
315 :
316 37447 : void RasterConverter3D::rasterconvertB3DPolyPolygon(const B3DPolyPolygon& rFill, const B3DHomMatrix* pViewToEye, sal_Int32 nStartLine, sal_Int32 nStopLine)
317 : {
318 37447 : reset();
319 37447 : maLineEntries.clear();
320 37447 : addArea(rFill, pViewToEye);
321 37447 : rasterconvertB3DArea(nStartLine, nStopLine);
322 37447 : }
323 :
324 216 : void RasterConverter3D::rasterconvertB3DPolygon(const B3DPolygon& rLine, sal_Int32 nStartLine, sal_Int32 nStopLine, sal_uInt16 nLineWidth)
325 : {
326 216 : const sal_uInt32 nPointCount(rLine.count());
327 :
328 216 : if(nPointCount)
329 : {
330 216 : const sal_uInt32 nEdgeCount(rLine.isClosed() ? nPointCount : nPointCount - 1);
331 :
332 864 : for(sal_uInt32 a(0); a < nEdgeCount; a++)
333 : {
334 648 : rasterconvertB3DEdge(rLine, a, (a + 1) % nPointCount, nStartLine, nStopLine, nLineWidth);
335 : }
336 : }
337 216 : }
338 : } // end of namespace basegfx
339 :
340 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|