1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_svx.hxx" 30*cdf0e10cSrcweir #include <svx/viewpt3d.hxx> 31*cdf0e10cSrcweir #include <svx/volume3d.hxx> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir /************************************************************************* 34*cdf0e10cSrcweir |* 35*cdf0e10cSrcweir |* Konstruktor 36*cdf0e10cSrcweir |* 37*cdf0e10cSrcweir \************************************************************************/ 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir Viewport3D::Viewport3D() : 40*cdf0e10cSrcweir aVRP(0, 0, 5), 41*cdf0e10cSrcweir aVPN(0, 0, 1), 42*cdf0e10cSrcweir aVUV(0, 1, 1), 43*cdf0e10cSrcweir aPRP(0, 0, 2), 44*cdf0e10cSrcweir fVPD(-3), 45*cdf0e10cSrcweir fNearClipDist (0.0), 46*cdf0e10cSrcweir fFarClipDist (0.0), 47*cdf0e10cSrcweir eProjection(PR_PERSPECTIVE), 48*cdf0e10cSrcweir eAspectMapping(AS_NO_MAPPING), 49*cdf0e10cSrcweir aDeviceRect(Point(0,0), Size(-1,-1)), 50*cdf0e10cSrcweir aViewPoint (0, 0, 5000), 51*cdf0e10cSrcweir bTfValid(0), 52*cdf0e10cSrcweir fWRatio (1.0), 53*cdf0e10cSrcweir fHRatio (1.0) 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir aViewWin.X = -1; aViewWin.Y = -1; 56*cdf0e10cSrcweir aViewWin.W = 2; aViewWin.H = 2; 57*cdf0e10cSrcweir } 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir /************************************************************************* 60*cdf0e10cSrcweir |* 61*cdf0e10cSrcweir |* ViewWindow (in View-Koordinaten) setzen 62*cdf0e10cSrcweir |* 63*cdf0e10cSrcweir \************************************************************************/ 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir void Viewport3D::SetViewWindow(double fX, double fY, double fW, double fH) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir aViewWin.X = fX; 68*cdf0e10cSrcweir aViewWin.Y = fY; 69*cdf0e10cSrcweir if ( fW > 0 ) aViewWin.W = fW; 70*cdf0e10cSrcweir else aViewWin.W = 1.0; 71*cdf0e10cSrcweir if ( fH > 0 ) aViewWin.H = fH; 72*cdf0e10cSrcweir else aViewWin.H = 1.0; 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir fWRatio = aDeviceRect.GetWidth() / aViewWin.W; 75*cdf0e10cSrcweir fHRatio = aDeviceRect.GetHeight() / aViewWin.H; 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir /************************************************************************* 79*cdf0e10cSrcweir |* 80*cdf0e10cSrcweir |* ViewWindow zurueckgeben 81*cdf0e10cSrcweir |* 82*cdf0e10cSrcweir \************************************************************************/ 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir void Viewport3D::GetViewWindow(double& rX, double& rY, 85*cdf0e10cSrcweir double& rW, double& rH) const 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir rX = aViewWin.X; 88*cdf0e10cSrcweir rY = aViewWin.Y; 89*cdf0e10cSrcweir rW = aViewWin.W; 90*cdf0e10cSrcweir rH = aViewWin.H; 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir /************************************************************************* 94*cdf0e10cSrcweir |* 95*cdf0e10cSrcweir |* Beobachterposition (PRP) in Weltkoordinaten zurueckgeben 96*cdf0e10cSrcweir |* 97*cdf0e10cSrcweir \************************************************************************/ 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir const basegfx::B3DPoint& Viewport3D::GetViewPoint() 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir MakeTransform(); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir return aViewPoint; 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir /************************************************************************* 107*cdf0e10cSrcweir |* 108*cdf0e10cSrcweir |* Transformationsmatrix zurueckgeben 109*cdf0e10cSrcweir |* 110*cdf0e10cSrcweir \************************************************************************/ 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir const basegfx::B3DHomMatrix& Viewport3D::GetViewTransform() 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir MakeTransform(); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir return aViewTf; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir /************************************************************************* 126*cdf0e10cSrcweir |* 127*cdf0e10cSrcweir |* View-Transformationsmatrix berechnen 128*cdf0e10cSrcweir |* 129*cdf0e10cSrcweir \************************************************************************/ 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir void Viewport3D::MakeTransform(void) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir if ( !bTfValid ) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir double fV, fXupVp, fYupVp; 136*cdf0e10cSrcweir aViewPoint = aVRP + aVPN * aPRP.getZ(); 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir // auf Einheitsmatrix zuruecksetzen 139*cdf0e10cSrcweir aViewTf.identity(); 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir // in den Ursprung verschieben 142*cdf0e10cSrcweir aViewTf.translate(-aVRP.getX(), -aVRP.getY(), -aVRP.getZ()); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // fV = Laenge der Projektion von aVPN auf die yz-Ebene: 145*cdf0e10cSrcweir fV = aVPN.getYZLength(); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir if ( fV != 0 ) 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir basegfx::B3DHomMatrix aTemp; 150*cdf0e10cSrcweir const double fSin(aVPN.getY() / fV); 151*cdf0e10cSrcweir const double fCos(aVPN.getZ() / fV); 152*cdf0e10cSrcweir aTemp.set(2, 2, fCos); 153*cdf0e10cSrcweir aTemp.set(1, 1, fCos); 154*cdf0e10cSrcweir aTemp.set(2, 1, fSin); 155*cdf0e10cSrcweir aTemp.set(1, 2, -fSin); 156*cdf0e10cSrcweir aViewTf *= aTemp; 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir basegfx::B3DHomMatrix aTemp; 161*cdf0e10cSrcweir const double fSin(-aVPN.getX()); 162*cdf0e10cSrcweir const double fCos(fV); 163*cdf0e10cSrcweir aTemp.set(2, 2, fCos); 164*cdf0e10cSrcweir aTemp.set(0, 0, fCos); 165*cdf0e10cSrcweir aTemp.set(0, 2, fSin); 166*cdf0e10cSrcweir aTemp.set(2, 0, -fSin); 167*cdf0e10cSrcweir aViewTf *= aTemp; 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir // X- und Y-Koordinaten des View Up Vektors in das (vorlaeufige) 171*cdf0e10cSrcweir // View-Koordinatensytem umrechnen 172*cdf0e10cSrcweir fXupVp = aViewTf.get(0, 0) * aVUV.getX() + aViewTf.get(0, 1) * aVUV.getY() + aViewTf.get(0, 2) * aVUV.getZ(); 173*cdf0e10cSrcweir fYupVp = aViewTf.get(1, 0) * aVUV.getX() + aViewTf.get(1, 1) * aVUV.getY() + aViewTf.get(1, 2) * aVUV.getZ(); 174*cdf0e10cSrcweir fV = sqrt(fXupVp * fXupVp + fYupVp * fYupVp); 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir if ( fV != 0 ) 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir basegfx::B3DHomMatrix aTemp; 179*cdf0e10cSrcweir const double fSin(fXupVp / fV); 180*cdf0e10cSrcweir const double fCos(fYupVp / fV); 181*cdf0e10cSrcweir aTemp.set(1, 1, fCos); 182*cdf0e10cSrcweir aTemp.set(0, 0, fCos); 183*cdf0e10cSrcweir aTemp.set(1, 0, fSin); 184*cdf0e10cSrcweir aTemp.set(0, 1, -fSin); 185*cdf0e10cSrcweir aViewTf *= aTemp; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir bTfValid = sal_True; 189*cdf0e10cSrcweir } 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir /************************************************************************* 193*cdf0e10cSrcweir |* 194*cdf0e10cSrcweir |* DeviceWindow des Ausgabegeraetes setzen 195*cdf0e10cSrcweir |* 196*cdf0e10cSrcweir \************************************************************************/ 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir void Viewport3D::SetDeviceWindow(const Rectangle& rRect) 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir long nNewW = rRect.GetWidth(); 201*cdf0e10cSrcweir long nNewH = rRect.GetHeight(); 202*cdf0e10cSrcweir long nOldW = aDeviceRect.GetWidth(); 203*cdf0e10cSrcweir long nOldH = aDeviceRect.GetHeight(); 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir switch ( eAspectMapping ) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir double fRatio, fTmp; 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir // Mapping, ohne die reale Groesse der Objekte im Device-Window 210*cdf0e10cSrcweir // zu aendern 211*cdf0e10cSrcweir case AS_HOLD_SIZE: 212*cdf0e10cSrcweir // Wenn Device ungueltig (w, h = -1), zunaechst 213*cdf0e10cSrcweir // View mit AsHoldX anpassen 214*cdf0e10cSrcweir if ( nOldW > 0 && nOldH > 0 ) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir fRatio = (double) nNewW / nOldW; 217*cdf0e10cSrcweir aViewWin.X *= fRatio; 218*cdf0e10cSrcweir aViewWin.W *= fRatio; 219*cdf0e10cSrcweir fRatio = (double) nNewH / nOldH; 220*cdf0e10cSrcweir aViewWin.Y *= fRatio; 221*cdf0e10cSrcweir aViewWin.H *= fRatio; 222*cdf0e10cSrcweir break; 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir case AS_HOLD_X: 225*cdf0e10cSrcweir // View-Hoehe an -Breite anpassen 226*cdf0e10cSrcweir fRatio = (double) nNewH / nNewW; 227*cdf0e10cSrcweir fTmp = aViewWin.H; 228*cdf0e10cSrcweir aViewWin.H = aViewWin.W * fRatio; 229*cdf0e10cSrcweir aViewWin.Y = aViewWin.Y * aViewWin.H / fTmp; 230*cdf0e10cSrcweir break; 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir case AS_HOLD_Y: 233*cdf0e10cSrcweir // View-Breite an -Hoehe anpassen 234*cdf0e10cSrcweir fRatio = (double) nNewW / nNewH; 235*cdf0e10cSrcweir fTmp = aViewWin.W; 236*cdf0e10cSrcweir aViewWin.W = aViewWin.H * fRatio; 237*cdf0e10cSrcweir aViewWin.X = aViewWin.X * aViewWin.W / fTmp; 238*cdf0e10cSrcweir break; 239*cdf0e10cSrcweir default: break; 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir fWRatio = nNewW / aViewWin.W; 242*cdf0e10cSrcweir fHRatio = nNewH / aViewWin.H; 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir aDeviceRect = rRect; 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir /************************************************************************* 257*cdf0e10cSrcweir |* 258*cdf0e10cSrcweir |* 3D-Punkt auf Viewplane projizieren 259*cdf0e10cSrcweir |* 260*cdf0e10cSrcweir \************************************************************************/ 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir basegfx::B3DPoint Viewport3D::DoProjection(const basegfx::B3DPoint& rVec) const 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir basegfx::B3DPoint aVec(rVec); 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir if ( eProjection == PR_PERSPECTIVE ) 267*cdf0e10cSrcweir { 268*cdf0e10cSrcweir double fPrDist = fVPD - aPRP.getZ(); 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir if ( aPRP.getZ() == rVec.getZ() ) 271*cdf0e10cSrcweir { 272*cdf0e10cSrcweir aVec.setX(0.0); 273*cdf0e10cSrcweir aVec.setY(0.0); 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir else 276*cdf0e10cSrcweir { 277*cdf0e10cSrcweir // Das ist die Version fuer beliebigen PRP, wird aber 278*cdf0e10cSrcweir // aus Performancegruenden nicht verwendet 279*cdf0e10cSrcweir fPrDist /= aVec.getZ() - aPRP.getZ(); 280*cdf0e10cSrcweir aVec.setX(aVec.getX() * fPrDist); 281*cdf0e10cSrcweir aVec.setY(aVec.getY() * fPrDist); 282*cdf0e10cSrcweir } 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir return aVec; 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir /************************************************************************* 289*cdf0e10cSrcweir |* 290*cdf0e10cSrcweir |* 3D-Punkt auf Geraetekoordinaten mappen 291*cdf0e10cSrcweir |* 292*cdf0e10cSrcweir \************************************************************************/ 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir basegfx::B3DPoint Viewport3D::MapToDevice(const basegfx::B3DPoint& rVec) const 295*cdf0e10cSrcweir { 296*cdf0e10cSrcweir basegfx::B3DPoint aRetval; 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir // Y-Koordinate subtrahieren, da die Device-Y-Achse von oben 299*cdf0e10cSrcweir // nach unten verlaeuft 300*cdf0e10cSrcweir aRetval.setX((double)aDeviceRect.Left() + ((rVec.getX() - aViewWin.X) * fWRatio)); 301*cdf0e10cSrcweir aRetval.setY((double)aDeviceRect.Bottom() - ((rVec.getY() - aViewWin.Y) * fHRatio)); 302*cdf0e10cSrcweir aRetval.setZ(rVec.getZ()); 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir return aRetval; 305*cdf0e10cSrcweir } 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir /************************************************************************* 308*cdf0e10cSrcweir |* 309*cdf0e10cSrcweir |* View Reference Point setzen 310*cdf0e10cSrcweir |* 311*cdf0e10cSrcweir \************************************************************************/ 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir void Viewport3D::SetVRP(const basegfx::B3DPoint& rNewVRP) 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir aVRP = rNewVRP; 316*cdf0e10cSrcweir bTfValid = sal_False; 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir 319*cdf0e10cSrcweir /************************************************************************* 320*cdf0e10cSrcweir |* 321*cdf0e10cSrcweir |* View Plane Normal setzen 322*cdf0e10cSrcweir |* 323*cdf0e10cSrcweir \************************************************************************/ 324*cdf0e10cSrcweir 325*cdf0e10cSrcweir void Viewport3D::SetVPN(const basegfx::B3DVector& rNewVPN) 326*cdf0e10cSrcweir { 327*cdf0e10cSrcweir aVPN = rNewVPN; 328*cdf0e10cSrcweir aVPN.normalize(); 329*cdf0e10cSrcweir bTfValid = sal_False; 330*cdf0e10cSrcweir } 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir /************************************************************************* 333*cdf0e10cSrcweir |* 334*cdf0e10cSrcweir |* View Up Vector setzen 335*cdf0e10cSrcweir |* 336*cdf0e10cSrcweir \************************************************************************/ 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir void Viewport3D::SetVUV(const basegfx::B3DVector& rNewVUV) 339*cdf0e10cSrcweir { 340*cdf0e10cSrcweir aVUV = rNewVUV; 341*cdf0e10cSrcweir bTfValid = sal_False; 342*cdf0e10cSrcweir } 343*cdf0e10cSrcweir 344*cdf0e10cSrcweir /************************************************************************* 345*cdf0e10cSrcweir |* 346*cdf0e10cSrcweir |* Center Of Projection setzen 347*cdf0e10cSrcweir |* 348*cdf0e10cSrcweir \************************************************************************/ 349*cdf0e10cSrcweir 350*cdf0e10cSrcweir void Viewport3D::SetPRP(const basegfx::B3DPoint& rNewPRP) 351*cdf0e10cSrcweir { 352*cdf0e10cSrcweir aPRP = rNewPRP; 353*cdf0e10cSrcweir aPRP.setX(0.0); 354*cdf0e10cSrcweir aPRP.setY(0.0); 355*cdf0e10cSrcweir bTfValid = sal_False; 356*cdf0e10cSrcweir } 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir /************************************************************************* 359*cdf0e10cSrcweir |* 360*cdf0e10cSrcweir |* View Plane Distance setzen 361*cdf0e10cSrcweir |* 362*cdf0e10cSrcweir \************************************************************************/ 363*cdf0e10cSrcweir 364*cdf0e10cSrcweir void Viewport3D::SetVPD(double fNewVPD) 365*cdf0e10cSrcweir { 366*cdf0e10cSrcweir fVPD = fNewVPD; 367*cdf0e10cSrcweir bTfValid = sal_False; 368*cdf0e10cSrcweir } 369*cdf0e10cSrcweir 370*cdf0e10cSrcweir /************************************************************************* 371*cdf0e10cSrcweir |* 372*cdf0e10cSrcweir |* Abstand der vorderen Clippingebene setzen 373*cdf0e10cSrcweir |* 374*cdf0e10cSrcweir \************************************************************************/ 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir void Viewport3D::SetNearClipDist(double fNewNCD) 377*cdf0e10cSrcweir { 378*cdf0e10cSrcweir fNearClipDist = fNewNCD; 379*cdf0e10cSrcweir bTfValid = sal_False; 380*cdf0e10cSrcweir } 381*cdf0e10cSrcweir 382*cdf0e10cSrcweir /************************************************************************* 383*cdf0e10cSrcweir |* 384*cdf0e10cSrcweir |* Abstand der hinteren Clippingebene setzen 385*cdf0e10cSrcweir |* 386*cdf0e10cSrcweir \************************************************************************/ 387*cdf0e10cSrcweir 388*cdf0e10cSrcweir void Viewport3D::SetFarClipDist(double fNewFCD) 389*cdf0e10cSrcweir { 390*cdf0e10cSrcweir fFarClipDist = fNewFCD; 391*cdf0e10cSrcweir bTfValid = sal_False; 392*cdf0e10cSrcweir } 393*cdf0e10cSrcweir 394*cdf0e10cSrcweir // eof 395