1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <basegfx/polygon/b3dpolypolygontools.hxx>
#include <basegfx/range/b3drange.hxx>
#include <basegfx/polygon/b3dpolypolygon.hxx>
#include <basegfx/polygon/b3dpolygon.hxx>
#include <basegfx/polygon/b3dpolygontools.hxx>
#include <basegfx/matrix/b3dhommatrix.hxx>
#include <basegfx/numeric/ftools.hxx>
#include <com/sun/star/drawing/DoubleSequence.hpp>
#include <com/sun/star/drawing/PolyPolygonShape3D.hpp>
// predefines
#define nMinSegments sal_uInt32(1)
#define nMaxSegments sal_uInt32(512)
namespace basegfx::utils
{
// B3DPolyPolygon tools
B3DRange getRange(const B3DPolyPolygon& rCandidate)
{
B3DRange aRetval;
for(const auto& rPolygon : rCandidate )
{
aRetval.expand(getRange(rPolygon));
}
return aRetval;
}
B3DPolyPolygon const & createUnitCubePolyPolygon()
{
static auto const singleton = [] {
B3DPolyPolygon aRetval;
B3DPolygon aTemp;
aTemp.append(B3DPoint(0.0, 0.0, 1.0));
aTemp.append(B3DPoint(0.0, 1.0, 1.0));
aTemp.append(B3DPoint(1.0, 1.0, 1.0));
aTemp.append(B3DPoint(1.0, 0.0, 1.0));
aTemp.setClosed(true);
aRetval.append(aTemp);
aTemp.clear();
aTemp.append(B3DPoint(0.0, 0.0, 0.0));
aTemp.append(B3DPoint(0.0, 1.0, 0.0));
aTemp.append(B3DPoint(1.0, 1.0, 0.0));
aTemp.append(B3DPoint(1.0, 0.0, 0.0));
aTemp.setClosed(true);
aRetval.append(aTemp);
aTemp.clear();
aTemp.append(B3DPoint(0.0, 0.0, 0.0));
aTemp.append(B3DPoint(0.0, 0.0, 1.0));
aRetval.append(aTemp);
aTemp.clear();
aTemp.append(B3DPoint(0.0, 1.0, 0.0));
aTemp.append(B3DPoint(0.0, 1.0, 1.0));
aRetval.append(aTemp);
aTemp.clear();
aTemp.append(B3DPoint(1.0, 1.0, 0.0));
aTemp.append(B3DPoint(1.0, 1.0, 1.0));
aRetval.append(aTemp);
aTemp.clear();
aTemp.append(B3DPoint(1.0, 0.0, 0.0));
aTemp.append(B3DPoint(1.0, 0.0, 1.0));
aRetval.append(aTemp);
return aRetval;
}();
return singleton;
}
B3DPolyPolygon const & createUnitCubeFillPolyPolygon()
{
static auto const singleton = [] {
B3DPolyPolygon aRetval;
B3DPolygon aTemp;
// all points
const B3DPoint A(0.0, 0.0, 0.0);
const B3DPoint B(0.0, 1.0, 0.0);
const B3DPoint C(1.0, 1.0, 0.0);
const B3DPoint D(1.0, 0.0, 0.0);
const B3DPoint E(0.0, 0.0, 1.0);
const B3DPoint F(0.0, 1.0, 1.0);
const B3DPoint G(1.0, 1.0, 1.0);
const B3DPoint H(1.0, 0.0, 1.0);
// create bottom
aTemp.append(D);
aTemp.append(A);
aTemp.append(E);
aTemp.append(H);
aTemp.setClosed(true);
aRetval.append(aTemp);
// create front
aTemp.clear();
aTemp.append(B);
aTemp.append(A);
aTemp.append(D);
aTemp.append(C);
aTemp.setClosed(true);
aRetval.append(aTemp);
// create left
aTemp.clear();
aTemp.append(E);
aTemp.append(A);
aTemp.append(B);
aTemp.append(F);
aTemp.setClosed(true);
aRetval.append(aTemp);
// create top
aTemp.clear();
aTemp.append(C);
aTemp.append(G);
aTemp.append(F);
aTemp.append(B);
aTemp.setClosed(true);
aRetval.append(aTemp);
// create right
aTemp.clear();
aTemp.append(H);
aTemp.append(G);
aTemp.append(C);
aTemp.append(D);
aTemp.setClosed(true);
aRetval.append(aTemp);
// create back
aTemp.clear();
aTemp.append(F);
aTemp.append(G);
aTemp.append(H);
aTemp.append(E);
aTemp.setClosed(true);
aRetval.append(aTemp);
return aRetval;
}();
return singleton;
}
B3DPolyPolygon createCubePolyPolygonFromB3DRange( const B3DRange& rRange)
{
B3DPolyPolygon aRetval;
if(!rRange.isEmpty())
{
aRetval = createUnitCubePolyPolygon();
B3DHomMatrix aTrans;
aTrans.scale(rRange.getWidth(), rRange.getHeight(), rRange.getDepth());
aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
aRetval.transform(aTrans);
aRetval.removeDoublePoints();
}
return aRetval;
}
B3DPolyPolygon createCubeFillPolyPolygonFromB3DRange( const B3DRange& rRange)
{
B3DPolyPolygon aRetval;
if(!rRange.isEmpty())
{
aRetval = createUnitCubeFillPolyPolygon();
B3DHomMatrix aTrans;
aTrans.scale(rRange.getWidth(), rRange.getHeight(), rRange.getDepth());
aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
aRetval.transform(aTrans);
aRetval.removeDoublePoints();
}
return aRetval;
}
// helper for getting the 3D Point from given cartesian coordinates. fHor is defined from
// [M_PI_2 .. -M_PI_2], fVer from [0.0 .. 2PI]
static B3DPoint getPointFromCartesian(double fHor, double fVer)
{
const double fCosVer(cos(fVer));
return B3DPoint(fCosVer * cos(fHor), sin(fVer), fCosVer * -sin(fHor));
}
B3DPolyPolygon createUnitSpherePolyPolygon(
sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
double fVerStart, double fVerStop,
double fHorStart, double fHorStop)
{
B3DPolyPolygon aRetval;
sal_uInt32 a, b;
if(!nHorSeg)
{
nHorSeg = fround(fabs(fHorStop - fHorStart) / (M_PI / 12.0));
}
// min/max limitations
nHorSeg = std::clamp(nHorSeg, nMinSegments, nMaxSegments);
if(!nVerSeg)
{
nVerSeg = fround(fabs(fVerStop - fVerStart) / (M_PI / 12.0));
}
// min/max limitations
nVerSeg = std::clamp(nVerSeg, nMinSegments, nMaxSegments);
// create constants
const double fVerDiffPerStep((fVerStop - fVerStart) / static_cast<double>(nVerSeg));
const double fHorDiffPerStep((fHorStop - fHorStart) / static_cast<double>(nHorSeg));
bool bHorClosed(fTools::equal(fHorStop - fHorStart, 2 * M_PI));
bool bVerFromTop(fTools::equal(fVerStart, M_PI_2));
bool bVerToBottom(fTools::equal(fVerStop, -M_PI_2));
// create horizontal rings
const sal_uInt32 nLoopVerInit(bVerFromTop ? 1 : 0);
const sal_uInt32 nLoopVerLimit(bVerToBottom ? nVerSeg : nVerSeg + 1);
const sal_uInt32 nLoopHorLimit(bHorClosed ? nHorSeg : nHorSeg + 1);
for(a = nLoopVerInit; a < nLoopVerLimit; a++)
{
const double fVer(fVerStart + (static_cast<double>(a) * fVerDiffPerStep));
B3DPolygon aNew;
for(b = 0; b < nLoopHorLimit; b++)
{
const double fHor(fHorStart + (static_cast<double>(b) * fHorDiffPerStep));
aNew.append(getPointFromCartesian(fHor, fVer));
}
aNew.setClosed(bHorClosed);
aRetval.append(aNew);
}
// create vertical half-rings
for(a = 0; a < nLoopHorLimit; a++)
{
const double fHor(fHorStart + (static_cast<double>(a) * fHorDiffPerStep));
B3DPolygon aNew;
if(bVerFromTop)
{
aNew.append(B3DPoint(0.0, 1.0, 0.0));
}
for(b = nLoopVerInit; b < nLoopVerLimit; b++)
{
const double fVer(fVerStart + (static_cast<double>(b) * fVerDiffPerStep));
aNew.append(getPointFromCartesian(fHor, fVer));
}
if(bVerToBottom)
{
aNew.append(B3DPoint(0.0, -1.0, 0.0));
}
aRetval.append(aNew);
}
return aRetval;
}
B3DPolyPolygon createSpherePolyPolygonFromB3DRange( const B3DRange& rRange,
sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
double fVerStart, double fVerStop,
double fHorStart, double fHorStop)
{
B3DPolyPolygon aRetval(createUnitSpherePolyPolygon(nHorSeg, nVerSeg, fVerStart, fVerStop, fHorStart, fHorStop));
if(aRetval.count())
{
// move and scale whole construct which is now in [-1.0 .. 1.0] in all directions
B3DHomMatrix aTrans;
aTrans.translate(1.0, 1.0, 1.0);
aTrans.scale(rRange.getWidth() / 2.0, rRange.getHeight() / 2.0, rRange.getDepth() / 2.0);
aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
aRetval.transform(aTrans);
}
return aRetval;
}
B3DPolyPolygon createUnitSphereFillPolyPolygon(
sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
bool bNormals,
double fVerStart, double fVerStop,
double fHorStart, double fHorStop)
{
B3DPolyPolygon aRetval;
if(!nHorSeg)
{
nHorSeg = fround(fabs(fHorStop - fHorStart) / (M_PI / 12.0));
}
// min/max limitations
nHorSeg = std::clamp(nHorSeg, nMinSegments, nMaxSegments);
if(!nVerSeg)
{
nVerSeg = fround(fabs(fVerStop - fVerStart) / (M_PI / 12.0));
}
// min/max limitations
nVerSeg = std::clamp(nVerSeg, nMinSegments, nMaxSegments);
// vertical loop
for(sal_uInt32 a(0); a < nVerSeg; a++)
{
const double fVer1(fVerStart + (((fVerStop - fVerStart) * a) / nVerSeg));
const double fVer2(fVerStart + (((fVerStop - fVerStart) * (a + 1)) / nVerSeg));
// horizontal loop
for(sal_uInt32 b(0); b < nHorSeg; b++)
{
const double fHor1(fHorStart + (((fHorStop - fHorStart) * b) / nHorSeg));
const double fHor2(fHorStart + (((fHorStop - fHorStart) * (b + 1)) / nHorSeg));
B3DPolygon aNew;
aNew.append(getPointFromCartesian(fHor1, fVer1));
aNew.append(getPointFromCartesian(fHor2, fVer1));
aNew.append(getPointFromCartesian(fHor2, fVer2));
aNew.append(getPointFromCartesian(fHor1, fVer2));
if(bNormals)
{
for(sal_uInt32 c(0); c < aNew.count(); c++)
{
aNew.setNormal(c, ::basegfx::B3DVector(aNew.getB3DPoint(c)));
}
}
aNew.setClosed(true);
aRetval.append(aNew);
}
}
return aRetval;
}
B3DPolyPolygon createSphereFillPolyPolygonFromB3DRange( const B3DRange& rRange,
sal_uInt32 nHorSeg, sal_uInt32 nVerSeg,
bool bNormals,
double fVerStart, double fVerStop,
double fHorStart, double fHorStop)
{
B3DPolyPolygon aRetval(createUnitSphereFillPolyPolygon(nHorSeg, nVerSeg, bNormals, fVerStart, fVerStop, fHorStart, fHorStop));
if(aRetval.count())
{
// move and scale whole construct which is now in [-1.0 .. 1.0] in all directions
B3DHomMatrix aTrans;
aTrans.translate(1.0, 1.0, 1.0);
aTrans.scale(rRange.getWidth() / 2.0, rRange.getHeight() / 2.0, rRange.getDepth() / 2.0);
aTrans.translate(rRange.getMinX(), rRange.getMinY(), rRange.getMinZ());
aRetval.transform(aTrans);
}
return aRetval;
}
B3DPolyPolygon applyDefaultNormalsSphere( const B3DPolyPolygon& rCandidate, const B3DPoint& rCenter)
{
B3DPolyPolygon aRetval;
for( const auto& rB3DPolygon : rCandidate)
{
aRetval.append(applyDefaultNormalsSphere(rB3DPolygon, rCenter));
}
return aRetval;
}
B3DPolyPolygon invertNormals( const B3DPolyPolygon& rCandidate)
{
B3DPolyPolygon aRetval;
for( const auto& rB3DPolygon : rCandidate )
{
aRetval.append(invertNormals(rB3DPolygon));
}
return aRetval;
}
B3DPolyPolygon applyDefaultTextureCoordinatesParallel( const B3DPolyPolygon& rCandidate, const B3DRange& rRange, bool bChangeX, bool bChangeY)
{
B3DPolyPolygon aRetval;
for( const auto& rB3DPolygon : rCandidate)
{
aRetval.append(applyDefaultTextureCoordinatesParallel(rB3DPolygon, rRange, bChangeX, bChangeY));
}
return aRetval;
}
B3DPolyPolygon applyDefaultTextureCoordinatesSphere( const B3DPolyPolygon& rCandidate, const B3DPoint& rCenter, bool bChangeX, bool bChangeY)
{
B3DPolyPolygon aRetval;
for( const auto& rB3DPolygon : rCandidate )
{
aRetval.append(applyDefaultTextureCoordinatesSphere(rB3DPolygon, rCenter, bChangeX, bChangeY));
}
return aRetval;
}
bool isInside(const B3DPolyPolygon& rCandidate, const B3DPoint& rPoint)
{
const sal_uInt32 nPolygonCount(rCandidate.count());
if(nPolygonCount == 1)
{
return isInside(rCandidate.getB3DPolygon(0), rPoint, false/*bWithBorder*/);
}
else
{
sal_Int32 nInsideCount(0);
for(const auto& rPolygon : rCandidate )
{
const bool bInside(isInside(rPolygon, rPoint, false/*bWithBorder*/));
if(bInside)
{
nInsideCount++;
}
}
return (nInsideCount % 2);
}
}
/// converters for css::drawing::PolyPolygonShape3D
B3DPolyPolygon UnoPolyPolygonShape3DToB3DPolyPolygon(
const css::drawing::PolyPolygonShape3D& rPolyPolygonShape3DSource)
{
B3DPolyPolygon aRetval;
const sal_Int32 nOuterSequenceCount(rPolyPolygonShape3DSource.SequenceX.getLength());
if(nOuterSequenceCount)
{
assert(nOuterSequenceCount == rPolyPolygonShape3DSource.SequenceY.getLength()
&& nOuterSequenceCount
== rPolyPolygonShape3DSource.SequenceZ.getLength()&&
"UnoPolyPolygonShape3DToB3DPolygon: Not all double sequences have the same "
"length (!)" );
const css::drawing::DoubleSequence* pInnerSequenceX = rPolyPolygonShape3DSource.SequenceX.getConstArray();
const css::drawing::DoubleSequence* pInnerSequenceY = rPolyPolygonShape3DSource.SequenceY.getConstArray();
const css::drawing::DoubleSequence* pInnerSequenceZ = rPolyPolygonShape3DSource.SequenceZ.getConstArray();
for(sal_Int32 a(0); a < nOuterSequenceCount; a++)
{
basegfx::B3DPolygon aNewPolygon;
const sal_Int32 nInnerSequenceCount(pInnerSequenceX->getLength());
assert(nInnerSequenceCount == pInnerSequenceY->getLength()
&& nInnerSequenceCount == pInnerSequenceZ->getLength()
&& "UnoPolyPolygonShape3DToB3DPolygon: Not all double sequences have "
"the same length (!)");
const double* pArrayX = pInnerSequenceX->getConstArray();
const double* pArrayY = pInnerSequenceY->getConstArray();
const double* pArrayZ = pInnerSequenceZ->getConstArray();
for(sal_Int32 b(0); b < nInnerSequenceCount; b++)
{
aNewPolygon.append(basegfx::B3DPoint(*pArrayX++,*pArrayY++,*pArrayZ++));
}
pInnerSequenceX++;
pInnerSequenceY++;
pInnerSequenceZ++;
// #i101520# correction is needed for imported polygons of old format,
// see callers
basegfx::utils::checkClosed(aNewPolygon);
aRetval.append(aNewPolygon);
}
}
return aRetval;
}
void B3DPolyPolygonToUnoPolyPolygonShape3D(
const B3DPolyPolygon& rPolyPolygonSource,
css::drawing::PolyPolygonShape3D& rPolyPolygonShape3DRetval)
{
const sal_uInt32 nPolygonCount(rPolyPolygonSource.count());
if(nPolygonCount)
{
rPolyPolygonShape3DRetval.SequenceX.realloc(nPolygonCount);
rPolyPolygonShape3DRetval.SequenceY.realloc(nPolygonCount);
rPolyPolygonShape3DRetval.SequenceZ.realloc(nPolygonCount);
css::drawing::DoubleSequence* pOuterSequenceX = rPolyPolygonShape3DRetval.SequenceX.getArray();
css::drawing::DoubleSequence* pOuterSequenceY = rPolyPolygonShape3DRetval.SequenceY.getArray();
css::drawing::DoubleSequence* pOuterSequenceZ = rPolyPolygonShape3DRetval.SequenceZ.getArray();
for(sal_uInt32 a(0); a < nPolygonCount; a++)
{
const basegfx::B3DPolygon& aPoly(rPolyPolygonSource.getB3DPolygon(a));
const sal_uInt32 nPointCount(aPoly.count());
if(nPointCount)
{
const bool bIsClosed(aPoly.isClosed());
const sal_uInt32 nTargetCount(bIsClosed ? nPointCount + 1 : nPointCount);
pOuterSequenceX->realloc(nTargetCount);
pOuterSequenceY->realloc(nTargetCount);
pOuterSequenceZ->realloc(nTargetCount);
double* pInnerSequenceX = pOuterSequenceX->getArray();
double* pInnerSequenceY = pOuterSequenceY->getArray();
double* pInnerSequenceZ = pOuterSequenceZ->getArray();
for(sal_uInt32 b(0); b < nPointCount; b++)
{
const basegfx::B3DPoint aPoint(aPoly.getB3DPoint(b));
*pInnerSequenceX++ = aPoint.getX();
*pInnerSequenceY++ = aPoint.getY();
*pInnerSequenceZ++ = aPoint.getZ();
}
if(bIsClosed)
{
const basegfx::B3DPoint aPoint(aPoly.getB3DPoint(0));
*pInnerSequenceX++ = aPoint.getX();
*pInnerSequenceY++ = aPoint.getY();
*pInnerSequenceZ++ = aPoint.getZ();
}
}
else
{
pOuterSequenceX->realloc(0);
pOuterSequenceY->realloc(0);
pOuterSequenceZ->realloc(0);
}
pOuterSequenceX++;
pOuterSequenceY++;
pOuterSequenceZ++;
}
}
else
{
rPolyPolygonShape3DRetval.SequenceX.realloc(0);
rPolyPolygonShape3DRetval.SequenceY.realloc(0);
rPolyPolygonShape3DRetval.SequenceZ.realloc(0);
}
}
} // end of namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|