summaryrefslogtreecommitdiff
path: root/svx/source/engine3d/sphere3d.cxx
blob: ec7b2807ae0c8c93f0cec8914b170578738daa7c (plain)
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
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: sphere3d.cxx,v $
 *
 *  $Revision: 1.18 $
 *
 *  last change: $Author: hr $ $Date: 2007-06-27 18:05:27 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_svx.hxx"

#include "svdstr.hrc"
#include "svdglob.hxx"

#ifndef _SVDMODEL_HXX
#include <svx/svdmodel.hxx>
#endif

#ifndef _SVDPAGE_HXX
#include <svx/svdpage.hxx>
#endif

#ifndef _E3D_GLOBL3D_HXX
#include "globl3d.hxx"
#endif

#ifndef _E3D_SPHERE3D_HXX
#include <svx/sphere3d.hxx>
#endif

#ifndef _SVX_SVXIDS_HRC
#include <svx/svxids.hrc>
#endif

#ifndef _SVX3DITEMS_HXX
#include <svx/svx3ditems.hxx>
#endif

#ifndef _SDR_PROPERTIES_E3DSPHEREPROPERTIES_HXX
#include <svx/sdr/properties/e3dsphereproperties.hxx>
#endif

#ifndef _BGFX_VECTOR_B3DVECTOR_HXX
#include <basegfx/vector/b3dvector.hxx>
#endif

#ifndef _BGFX_POINT_B3DPOINT_HXX
#include <basegfx/point/b3dpoint.hxx>
#endif

#ifndef _BGFX_POLYGON_B3DPOLYGON_HXX
#include <basegfx/polygon/b3dpolygon.hxx>
#endif

//////////////////////////////////////////////////////////////////////////////

sdr::properties::BaseProperties* E3dSphereObj::CreateObjectSpecificProperties()
{
    return new sdr::properties::E3dSphereProperties(*this);
}

//////////////////////////////////////////////////////////////////////////////

TYPEINIT1(E3dSphereObj, E3dCompoundObject);

/*************************************************************************
|*
|* Kugel aus Polygonfacetten nach Laengen und Breitengrad aufbauen
|*
\************************************************************************/

E3dSphereObj::E3dSphereObj(E3dDefaultAttributes& rDefault, const basegfx::B3DPoint& rCenter, const basegfx::B3DVector& r3DSize)
:   E3dCompoundObject(rDefault)
{
    // Defaults setzen
    SetDefaultAttributes(rDefault);

    // Uebergebene drueberbuegeln
    aCenter = rCenter;
    aSize = r3DSize;

    // Geometrie erzeugen
    CreateGeometry();
}

E3dSphereObj::E3dSphereObj()
:   E3dCompoundObject()
{
    // Defaults setzen
    E3dDefaultAttributes aDefault;
    SetDefaultAttributes(aDefault);
}

/*************************************************************************
|*
|* Kugel erzeugen ohne die Polygone darin zu erzeugen
|*
\************************************************************************/

// FG: Dieser Aufruf erfolgt von der 3D-Object Factory (objfac3d.cxx) und zwar ausschliesslich beim
//     laden von Dokumenten. Hier braucht man keinen CreateSphere-Aufruf, denn die wirkliche
//     Anzahl Segmente ist ja noch nicht bekannt. Dies war bis zum 10.2.97 ein (kleines)
//     Speicherleck.
E3dSphereObj::E3dSphereObj(int /*dummy*/) // den Parameter braucht es um unterscheiden zu koennen, welcher
{                                     // der beiden Konstruktoren gemeint ist. Der obige halt per Default
    // Defaults setzen
    E3dDefaultAttributes aDefault;
    SetDefaultAttributes(aDefault);
}

void E3dSphereObj::SetDefaultAttributes(E3dDefaultAttributes& rDefault)
{
    // Defaults setzen
    aCenter = rDefault.GetDefaultSphereCenter();
    aSize = rDefault.GetDefaultSphereSize();
}

/*************************************************************************
|*
|* Give out simple line geometry
|*
\************************************************************************/

basegfx::B3DPolyPolygon E3dSphereObj::Get3DLineGeometry() const
{
    basegfx::B3DPolyPolygon aRetval;

    // add geometry describing polygons to rLinePolyPolygon
    const sal_uInt32 nCntHor((sal_uInt32)GetHorizontalSegments());
    const sal_uInt32 nCntVer((sal_uInt32)GetVerticalSegments());
    const sal_Bool bCreateHorizontal(sal_True);
    const sal_Bool bCreateVertical(sal_True);

    if(nCntHor && nCntVer && (bCreateHorizontal || bCreateVertical))
    {
        const double fHInc((double)DEG2RAD(360) / nCntHor);
        const double fVInc((double)DEG2RAD(180) / nCntVer);
        const basegfx::B3DVector aRadius(aSize / 2.0);
        const basegfx::B3DPoint aCenterPos(aCenter);
        double fHAng(0.0);
        basegfx::B3DPolygon aAllPoints;

        // create all sphere points
        for(sal_uInt32 nH(0L); nH < nCntHor; nH++)
        {
            const double fHSin(sin(fHAng));
            const double fHCos(cos(fHAng));
            fHAng += fHInc;
            double fVAng(DEG2RAD(90) - fVInc);

            for(sal_uInt32 nV(1L); nV < nCntVer; nV++)
            {
                const double fVSin(sin(fVAng));
                const double fVCos(cos(fVAng));
                fVAng -= fVInc;

                basegfx::B3DPoint aNewPos(
                    aCenterPos.getX() + (aRadius.getX() * fVCos) * fHCos,
                    aCenterPos.getY() + (aRadius.getY() * fVSin),
                    aCenterPos.getZ() + (aRadius.getZ() * fVCos) * fHSin);

                aAllPoints.append(aNewPos);
            }
        }

        // create horizontal lines
        if(bCreateHorizontal)
        {
            for(sal_uInt32 a(1L); a < nCntVer; a++)
            {
                basegfx::B3DPolygon aNewHor;
                sal_uInt32 nStartIndex(a - 1L);

                for(sal_uInt32 b(0L); b < nCntHor; b++)
                {
                    aNewHor.append(aAllPoints.getB3DPoint(nStartIndex));
                    nStartIndex += (nCntVer - 1L);
                }

                aNewHor.append(aNewHor.getB3DPoint(0L));
                aRetval.append(aNewHor);
            }
        }

        // create vertical lines
        if(bCreateVertical)
        {
            const basegfx::B3DPoint aTopPos(aCenterPos.getX(), aCenterPos.getY() + aRadius.getY(), aCenterPos.getZ());
            const basegfx::B3DPoint aBottomPos(aCenterPos.getX(), aCenterPos.getY() - aRadius.getY(), aCenterPos.getZ());

            for(sal_uInt32 a(0L); a < nCntHor; a++)
            {
                basegfx::B3DPolygon aNewVer;
                aNewVer.append(aTopPos);
                sal_uInt32 nStartIndex(a * (nCntVer - 1L));

                for(sal_uInt32 b(1L); b < nCntVer; b++, nStartIndex++)
                {
                    aNewVer.append(aAllPoints.getB3DPoint(nStartIndex));
                }

                aNewVer.append(aBottomPos);
                aRetval.append(aNewVer);
            }
        }
    }

    return aRetval;
}

/*************************************************************************
|*
|* Geometrieerzeugung
|*
\************************************************************************/

void E3dSphereObj::CreateGeometry()
{
    if ( GetHorizontalSegments() <  3L  )
    {
        GetProperties().SetObjectItemDirect(Svx3DHorizontalSegmentsItem(3L));
    }

    if ( GetHorizontalSegments() > 100L )
    {
        GetProperties().SetObjectItemDirect(Svx3DHorizontalSegmentsItem(100L));
    }

    if ( GetVerticalSegments() <  2L  )
    {
        GetProperties().SetObjectItemDirect(Svx3DVerticalSegmentsItem(2L));
    }

    if ( GetVerticalSegments() > 100L )
    {
        GetProperties().SetObjectItemDirect(Svx3DVerticalSegmentsItem(100L));
    }

    // Start der Geometrieerzeugung ankuendigen
    StartCreateGeometry();

    basegfx::B3DVector aRadius(aSize / 2.0);
    double fHSin1, fHSin2, fHCos1, fHCos2;
    double fHAng = 0;
    double fHInc = DEG2RAD(360) / GetHorizontalSegments();
    double fVInc = DEG2RAD(180) / GetVerticalSegments();

    fHSin2 = 0.0; // sin(0)
    fHCos2 = 1.0; // cos(0)
    sal_uInt16 nUpperBound = (sal_uInt16)GetHorizontalSegments();

    for (USHORT nH = 0; nH < nUpperBound; nH++)
    {
        fHAng += fHInc;
        fHSin1 = fHSin2;
        fHSin2 = sin(fHAng);
        fHCos1 = fHCos2;
        fHCos2 = cos(fHAng);

        double fRx, fRz;
        double fVAng = DEG2RAD(90);
        double fVSin1, fVSin2, fVCos1, fVCos2;
        fVSin2 = 1.0; // sin(90)
        fVCos2 = 0.0; // cos(90)
        sal_uInt16 nUpperVert = (sal_uInt16)GetVerticalSegments();

        for (USHORT nV = 0; nV < nUpperVert; nV++)
        {
            fVAng -= fVInc;
            fVSin1 = fVSin2;
            fVSin2 = sin(fVAng);
            fVCos1 = fVCos2;
            fVCos2 = cos(fVAng);

            basegfx::B3DPoint aPos1(aCenter);
            fRx = aRadius.getX() * fVCos1;
            fRz = aRadius.getZ() * fVCos1;
            aPos1.setX(aPos1.getX() + (fRx * fHCos1));
            aPos1.setY(aPos1.getY() + (aRadius.getY() * fVSin1));
            aPos1.setZ(aPos1.getZ() + (fRz * fHSin1));

            basegfx::B3DPoint aPos2(aPos1);
            aPos2.setX(aCenter.getX() + fRx * fHCos2);
            aPos2.setZ(aCenter.getZ() + fRz * fHSin2);

            basegfx::B3DPoint aPos0(aCenter);
            fRx = aRadius.getX() * fVCos2;
            fRz = aRadius.getZ() * fVCos2;
            aPos0.setX(aPos0.getX() + (fRx * fHCos1));
            aPos0.setY(aPos0.getY() + (aRadius.getY() * fVSin2));
            aPos0.setZ(aPos0.getZ() + (fRz * fHSin1));

            basegfx::B3DPoint aPos3(aPos0);
            aPos3.setX(aCenter.getX() + (fRx * fHCos2));
            aPos3.setZ(aCenter.getZ() + (fRz * fHSin2));

            basegfx::B3DPolygon aRect3D;
            basegfx::B2DPolygon aTexture2D;

            aRect3D.append(aPos0);
            aRect3D.append(aPos1);
            aRect3D.append(aPos2);
            aRect3D.append(aPos3);

            if(GetCreateTexture())
            {
                basegfx::B2DPoint aTex1;
                aTex1.setX((double)(nUpperBound - (nH + 1)) / (double)nUpperBound);
                aTex1.setY((double)nV / (double)nUpperVert);

                basegfx::B2DPoint aTex2;
                aTex2.setX((double)((nUpperBound - (nH + 1)) - 1) / (double)nUpperBound);
                aTex2.setY(aTex1.getY());

                basegfx::B2DPoint aTex3;
                aTex3.setX(aTex2.getX());
                aTex3.setY((double)(nV+1) / (double)nUpperVert);

                basegfx::B2DPoint aTex0;
                aTex0.setX(aTex1.getX());
                aTex0.setY(aTex3.getY());

                aTexture2D.append(aTex0);
                aTexture2D.append(aTex1);
                aTexture2D.append(aTex2);
                aTexture2D.append(aTex3);
            }

            if(GetCreateNormals())
            {
                basegfx::B3DPolygon aNormal3D;
                basegfx::B3DVector aNorZwi;

                aNorZwi = aRect3D.getB3DPoint(0L); aNorZwi.normalize(); aNormal3D.append(aNorZwi);
                aNorZwi = aRect3D.getB3DPoint(1L); aNorZwi.normalize(); aNormal3D.append(aNorZwi);
                aNorZwi = aRect3D.getB3DPoint(2L); aNorZwi.normalize(); aNormal3D.append(aNorZwi);
                aNorZwi = aRect3D.getB3DPoint(3L); aNorZwi.normalize(); aNormal3D.append(aNorZwi);

                if(GetCreateTexture())
                {
                    AddGeometry(basegfx::B3DPolyPolygon(aRect3D), basegfx::B3DPolyPolygon(aNormal3D), basegfx::B2DPolyPolygon(aTexture2D), FALSE);
                }
                else
                {
                    AddGeometry(basegfx::B3DPolyPolygon(aRect3D), basegfx::B3DPolyPolygon(aNormal3D), FALSE);
                }
            }
            else
            {
                AddGeometry(basegfx::B3DPolyPolygon(aRect3D), FALSE);
            }
        }
    }

    // call parent
    E3dCompoundObject::CreateGeometry();
}

/*************************************************************************
|*
|* Identifier zurueckgeben
|*
\************************************************************************/

UINT16 E3dSphereObj::GetObjIdentifier() const
{
    return E3D_SPHEREOBJ_ID;
}

/*************************************************************************
|*
|* Wandle das Objekt in ein Gruppenobjekt bestehend aus n Polygonen
|*
\************************************************************************/

SdrObject *E3dSphereObj::DoConvertToPolyObj(BOOL /*bBezier*/) const
{
    return NULL;
}

/*************************************************************************
|*
|* Leer-Konstruktor
|*
\************************************************************************/

void E3dSphereObj::ReSegment(sal_uInt32 nHSegs, sal_uInt32 nVSegs)
{
    if((nHSegs != GetHorizontalSegments() || nVSegs != GetVerticalSegments()) && (nHSegs != 0 || nVSegs != 0))
    {
        GetProperties().SetObjectItemDirect(Svx3DHorizontalSegmentsItem(nHSegs));
        GetProperties().SetObjectItemDirect(Svx3DVerticalSegmentsItem(nVSegs));

        bGeometryValid = FALSE;
    }
}

/*************************************************************************
|*
|* Zuweisungsoperator
|*
\************************************************************************/

void E3dSphereObj::operator=(const SdrObject& rObj)
{
    // erstmal alle Childs kopieren
    E3dCompoundObject::operator=(rObj);

    // weitere Parameter kopieren
    const E3dSphereObj& r3DObj = (const E3dSphereObj&) rObj;

    aCenter       = r3DObj.aCenter;
    aSize         = r3DObj.aSize;
}

/*************************************************************************
|*
|* Lokale Parameter setzen mit Geometrieneuerzeugung
|*
\************************************************************************/

void E3dSphereObj::SetCenter(const basegfx::B3DPoint& rNew)
{
    if(aCenter != rNew)
    {
        aCenter = rNew;
        bGeometryValid = FALSE;
    }
}

void E3dSphereObj::SetSize(const basegfx::B3DVector& rNew)
{
    if(aSize != rNew)
    {
        aSize = rNew;
        bGeometryValid = FALSE;
    }
}

/*************************************************************************
|*
|* Get the name of the object (singular)
|*
\************************************************************************/

void E3dSphereObj::TakeObjNameSingul(XubString& rName) const
{
    rName=ImpGetResStr(STR_ObjNameSingulSphere3d);

    String aName( GetName() );
    if(aName.Len())
    {
        rName += sal_Unicode(' ');
        rName += sal_Unicode('\'');
        rName += aName;
        rName += sal_Unicode('\'');
    }
}

/*************************************************************************
|*
|* Get the name of the object (plural)
|*
\************************************************************************/

void E3dSphereObj::TakeObjNamePlural(XubString& rName) const
{
    rName=ImpGetResStr(STR_ObjNamePluralSphere3d);
}

// eof