xref: /trunk/main/svx/source/engine3d/polygn3d.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 #include <svx/polygn3d.hxx>
31 #include <svx/svdpage.hxx>
32 #include "svx/globl3d.hxx"
33 #include <basegfx/point/b3dpoint.hxx>
34 #include <svx/sdr/contact/viewcontactofe3dpolygon.hxx>
35 #include <basegfx/polygon/b3dpolygon.hxx>
36 #include <basegfx/polygon/b3dpolygontools.hxx>
37 
38 TYPEINIT1(E3dPolygonObj, E3dCompoundObject);
39 
40 //////////////////////////////////////////////////////////////////////////////
41 // #110094# DrawContact section
42 
43 sdr::contact::ViewContact* E3dPolygonObj::CreateObjectSpecificViewContact()
44 {
45     return new sdr::contact::ViewContactOfE3dPolygon(*this);
46 }
47 
48 /*************************************************************************
49 |*
50 |* Konstruktor
51 |*
52 \************************************************************************/
53 
54 E3dPolygonObj::E3dPolygonObj(
55     E3dDefaultAttributes& rDefault,
56     const basegfx::B3DPolyPolygon& rPolyPoly3D,
57     sal_Bool bLinOnly)
58 :   E3dCompoundObject(rDefault),
59     bLineOnly(bLinOnly)
60 {
61     // Geometrie setzen
62     SetPolyPolygon3D(rPolyPoly3D);
63 
64     // Default-Normals erzeugen
65     CreateDefaultNormals();
66 
67     // Default-Texturkoordinaten erzeugen
68     CreateDefaultTexture();
69 }
70 
71 /*************************************************************************
72 |*
73 |* Konstruktor
74 |*
75 \************************************************************************/
76 
77 E3dPolygonObj::E3dPolygonObj(
78     E3dDefaultAttributes& rDefault,
79     const basegfx::B3DPolyPolygon& rPolyPoly3D,
80     const basegfx::B3DPolyPolygon& rPolyNormals3D,
81     sal_Bool bLinOnly)
82 :   E3dCompoundObject(rDefault),
83     bLineOnly(bLinOnly)
84 {
85     // Geometrie und Normalen setzen
86     SetPolyPolygon3D(rPolyPoly3D);
87     SetPolyNormals3D(rPolyNormals3D);
88 
89     // Default-Texturkoordinaten erzeugen
90     CreateDefaultTexture();
91 }
92 
93 /*************************************************************************
94 |*
95 |* Konstruktor
96 |*
97 \************************************************************************/
98 
99 E3dPolygonObj::E3dPolygonObj(
100     E3dDefaultAttributes& rDefault,
101     const basegfx::B3DPolyPolygon& rPolyPoly3D,
102     const basegfx::B3DPolyPolygon& rPolyNormals3D,
103     const basegfx::B2DPolyPolygon& rPolyTexture2D,
104     sal_Bool bLinOnly)
105 :   E3dCompoundObject(rDefault),
106     bLineOnly(bLinOnly)
107 {
108     SetPolyPolygon3D(rPolyPoly3D);
109     SetPolyNormals3D(rPolyNormals3D);
110     SetPolyTexture2D(rPolyTexture2D);
111 }
112 
113 /*************************************************************************
114 |*
115 |* Leer-Konstruktor
116 |*
117 \************************************************************************/
118 
119 E3dPolygonObj::E3dPolygonObj()
120 :   E3dCompoundObject(),
121     bLineOnly(false) // added missing initialisation
122 {
123     // Keine Geometrie erzeugen
124 }
125 
126 /*************************************************************************
127 |*
128 |* Default-Normalen erzeugen
129 |*
130 \************************************************************************/
131 
132 void E3dPolygonObj::CreateDefaultNormals()
133 {
134     basegfx::B3DPolyPolygon aPolyNormals;
135 
136     // Komplettes PolyPolygon mit den Ebenennormalen anlegen
137     for(sal_uInt32 a(0L); a < aPolyPoly3D.count(); a++)
138     {
139         // Quellpolygon finden
140         const basegfx::B3DPolygon aPolygon(aPolyPoly3D.getB3DPolygon(a));
141 
142         // Neues Polygon fuer Normalen anlegen
143         basegfx::B3DPolygon aNormals;
144 
145         // Normale holen (und umdrehen)
146         basegfx::B3DVector aNormal(-basegfx::tools::getNormal(aPolygon));
147 
148         // Neues Polygon fuellen
149         for(sal_uInt32 b(0L); b < aPolygon.count(); b++)
150         {
151             aNormals.append(aNormal);
152         }
153 
154         // Neues Polygon in PolyPolygon einfuegen
155         aPolyNormals.append(aNormals);
156     }
157 
158     // Default-Normalen setzen
159     SetPolyNormals3D(aPolyNormals);
160 }
161 
162 /*************************************************************************
163 |*
164 |* Default-Texturkoordinaten erzeugen
165 |*
166 \************************************************************************/
167 
168 void E3dPolygonObj::CreateDefaultTexture()
169 {
170     basegfx::B2DPolyPolygon aPolyTexture;
171 
172     // Komplettes PolyPolygon mit den Texturkoordinaten anlegen
173     // Die Texturkoordinaten erstrecken sich ueber X,Y und Z
174     // ueber die gesamten Extremwerte im Bereich 0.0 .. 1.0
175     for(sal_uInt32 a(0L); a < aPolyPoly3D.count(); a++)
176     {
177         // Quellpolygon finden
178         const basegfx::B3DPolygon& aPolygon(aPolyPoly3D.getB3DPolygon(a));
179 
180         // Gesamtgroesse des Objektes feststellen
181         basegfx::B3DRange aVolume(basegfx::tools::getRange(aPolygon));
182 
183         // Normale holen
184         basegfx::B3DVector aNormal(basegfx::tools::getNormal(aPolygon));
185         aNormal.setX(fabs(aNormal.getX()));
186         aNormal.setY(fabs(aNormal.getY()));
187         aNormal.setZ(fabs(aNormal.getZ()));
188 
189         // Entscheiden, welche Koordinaten als Source fuer das
190         // Mapping benutzt werden sollen
191         sal_uInt16 nSourceMode = 0;
192 
193         // Groessten Freiheitsgrad ermitteln
194         if(!(aNormal.getX() > aNormal.getY() && aNormal.getX() > aNormal.getZ()))
195         {
196             if(aNormal.getY() > aNormal.getZ())
197             {
198                 // Y ist am groessten, benutze X,Z als mapping
199                 nSourceMode = 1;
200             }
201             else
202             {
203                 // Z ist am groessten, benutze X,Y als mapping
204                 nSourceMode = 2;
205             }
206         }
207 
208         // Neues Polygon fuer Texturkoordinaten anlegen
209         basegfx::B2DPolygon aTexture;
210 
211         // Neues Polygon fuellen
212         for(sal_uInt32 b(0L); b < aPolygon.count(); b++)
213         {
214             basegfx::B2DPoint aTex;
215             const basegfx::B3DPoint aCandidate(aPolygon.getB3DPoint(b));
216 
217             switch(nSourceMode)
218             {
219                 case 0: // Quelle ist Y,Z
220                     if(aVolume.getHeight())
221                         aTex.setX((aCandidate.getY() - aVolume.getMinY()) / aVolume.getHeight());
222                     if(aVolume.getDepth())
223                         aTex.setY((aCandidate.getZ() - aVolume.getMinZ()) / aVolume.getDepth());
224                     break;
225 
226                 case 1: // Quelle ist X,Z
227                     if(aVolume.getWidth())
228                         aTex.setX((aCandidate.getX() - aVolume.getMinX()) / aVolume.getWidth());
229                     if(aVolume.getDepth())
230                         aTex.setY((aCandidate.getZ() - aVolume.getMinZ()) / aVolume.getDepth());
231                     break;
232 
233                 case 2: // Quelle ist X,Y
234                     if(aVolume.getWidth())
235                         aTex.setX((aCandidate.getX() - aVolume.getMinX()) / aVolume.getWidth());
236                     if(aVolume.getHeight())
237                         aTex.setY((aCandidate.getY() - aVolume.getMinY()) / aVolume.getHeight());
238                     break;
239             }
240 
241             aTexture.append(aTex);
242         }
243 
244         // Neues Polygon in PolyPolygon einfuegen
245         aPolyTexture.append(aTexture);
246     }
247 
248     // Default-Texturkoordinaten setzen
249     SetPolyTexture2D(aPolyTexture);
250 }
251 
252 /*************************************************************************
253 |*
254 |* Destruktor
255 |*
256 \************************************************************************/
257 
258 E3dPolygonObj::~E3dPolygonObj()
259 {
260 }
261 
262 /*************************************************************************
263 |*
264 |* Identifier zurueckgeben
265 |*
266 \************************************************************************/
267 
268 sal_uInt16 E3dPolygonObj::GetObjIdentifier() const
269 {
270     return E3D_POLYGONOBJ_ID;
271 }
272 
273 /*************************************************************************
274 |*
275 |* Polygon setzen
276 |*
277 \************************************************************************/
278 
279 void E3dPolygonObj::SetPolyPolygon3D(const basegfx::B3DPolyPolygon& rNewPolyPoly3D)
280 {
281     if ( aPolyPoly3D != rNewPolyPoly3D )
282     {
283         // Neues PolyPolygon; kopieren
284         aPolyPoly3D = rNewPolyPoly3D;
285 
286         // Geometrie neu erzeugen
287         ActionChanged();
288     }
289 }
290 
291 void E3dPolygonObj::SetPolyNormals3D(const basegfx::B3DPolyPolygon& rNewPolyNormals3D)
292 {
293     if ( aPolyNormals3D != rNewPolyNormals3D )
294     {
295         // Neue Normalen; kopieren
296         aPolyNormals3D = rNewPolyNormals3D;
297 
298         // Geometrie neu erzeugen
299         ActionChanged();
300     }
301 }
302 
303 void E3dPolygonObj::SetPolyTexture2D(const basegfx::B2DPolyPolygon& rNewPolyTexture2D)
304 {
305     if ( aPolyTexture2D != rNewPolyTexture2D )
306     {
307         // Neue Texturkoordinaten; kopieren
308         aPolyTexture2D = rNewPolyTexture2D;
309 
310         // Geometrie neu erzeugen
311         ActionChanged();
312     }
313 }
314 
315 /*************************************************************************
316 |*
317 |* Wandle das Objekt in ein Gruppenobjekt bestehend aus 6 Polygonen
318 |*
319 \************************************************************************/
320 
321 SdrObject *E3dPolygonObj::DoConvertToPolyObj(sal_Bool /*bBezier*/) const
322 {
323     return NULL;
324 }
325 
326 /*************************************************************************
327 |*
328 |* Zuweisungsoperator
329 |*
330 \************************************************************************/
331 
332 void E3dPolygonObj::operator=(const SdrObject& rObj)
333 {
334     // erstmal alle Childs kopieren
335     E3dCompoundObject::operator=(rObj);
336 
337     // weitere Parameter kopieren
338     const E3dPolygonObj& r3DObj = (const E3dPolygonObj&)rObj;
339 
340     aPolyPoly3D      = r3DObj.aPolyPoly3D;
341     aPolyNormals3D   = r3DObj.aPolyNormals3D;
342     aPolyTexture2D   = r3DObj.aPolyTexture2D;
343     bLineOnly        = r3DObj.bLineOnly;
344 }
345 
346 /*************************************************************************
347 |*
348 |* LineOnly setzen
349 |*
350 \************************************************************************/
351 
352 void E3dPolygonObj::SetLineOnly(sal_Bool bNew)
353 {
354     if(bNew != bLineOnly)
355     {
356         bLineOnly = bNew;
357         ActionChanged();
358     }
359 }
360 
361 // eof
362