LibreOffice Module svx (master)  1
helperhittest3d.cxx
Go to the documentation of this file.
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 
21 #include <svx/helperhittest3d.hxx>
23 #include <svx/svdpage.hxx>
24 #include <svx/scene3d.hxx>
25 #include <svx/svditer.hxx>
29 #include <com/sun/star/uno/Sequence.h>
30 
31 
32 using namespace com::sun::star;
33 
34 namespace {
35 
36 class ImplPairDephAndObject
37 {
38 private:
40  double mfDepth;
41 
42 public:
43  ImplPairDephAndObject(const E3dCompoundObject* pObject, double fDepth)
44  : mpObject(pObject),
45  mfDepth(fDepth)
46  {}
47 
48  // for ::std::sort
49  bool operator<(const ImplPairDephAndObject& rComp) const
50  {
51  return (mfDepth < rComp.mfDepth);
52  }
53 
54  // data read access
55  const E3dCompoundObject* getObject() const { return mpObject; }
56 };
57 
58 }
59 
61  const basegfx::B3DPoint& rFront,
62  const basegfx::B3DPoint& rBack,
63  const E3dCompoundObject& rObject,
64  const drawinglayer::geometry::ViewInformation3D& rObjectViewInformation3D,
65  ::std::vector< basegfx::B3DPoint >& o_rResult,
66  bool bAnyHit)
67 {
68  o_rResult.clear();
69 
70  if(!rFront.equal(rBack))
71  {
72  // rObject is an E3dCompoundObject, so it cannot be a scene (which is an E3dObject)
73  const sdr::contact::ViewContactOfE3d& rVCObject = static_cast< sdr::contact::ViewContactOfE3d& >(rObject.GetViewContact());
75 
76  if(!aPrimitives.empty())
77  {
78  // make BoundVolume empty and overlapping test for speedup
79  const basegfx::B3DRange aObjectRange(aPrimitives.getB3DRange(rObjectViewInformation3D));
80 
81  if(!aObjectRange.isEmpty())
82  {
83  const basegfx::B3DRange aFrontBackRange(rFront, rBack);
84 
85  if(aObjectRange.overlaps(aFrontBackRange))
86  {
87  // bound volumes hit, geometric cut tests needed
88  drawinglayer::processor3d::CutFindProcessor aCutFindProcessor(rObjectViewInformation3D, rFront, rBack, bAnyHit);
89  aCutFindProcessor.process(aPrimitives);
90  o_rResult = aCutFindProcessor.getCutPoints();
91  }
92  }
93  }
94  }
95 }
96 
97 
99 {
100  // Search for root scene (outmost scene) of the 3d object since e.g. in chart, multiple scenes may
101  // be placed between object and outmost scene. On that search, remember the in-between scene's
102  // transformation for the correct complete ObjectTransformation. For historical reasons, the
103  // root scene's own object transformation is part of the scene's ViewTransformation, o do not
104  // add it. For more details, see ViewContactOfE3dScene::createViewInformation3D.
105  E3dScene* pParentScene(rCandidate.getParentE3dSceneFromE3dObject());
106  E3dScene* pRootScene(nullptr);
107  basegfx::B3DHomMatrix aInBetweenSceneMatrix;
108 
109  while(pParentScene)
110  {
111  E3dScene* pParentParentScene(pParentScene->getParentE3dSceneFromE3dObject());
112 
113  if(pParentParentScene)
114  {
115  // pParentScene is an in-between scene
116  aInBetweenSceneMatrix = pParentScene->GetTransform() * aInBetweenSceneMatrix;
117  }
118  else
119  {
120  // pParentScene is the root scene
121  pRootScene = pParentScene;
122  }
123 
124  pParentScene = pParentParentScene;
125  }
126 
127  if(pRootScene)
128  {
129  const sdr::contact::ViewContactOfE3dScene& rVCScene = static_cast< sdr::contact::ViewContactOfE3dScene& >(pRootScene->GetViewContact());
130 
131  if(aInBetweenSceneMatrix.isIdentity())
132  {
133  o_rViewInformation3D = rVCScene.getViewInformation3D();
134  }
135  else
136  {
137  // build new ViewInformation containing all transforms for the candidate
139 
140  o_rViewInformation3D = drawinglayer::geometry::ViewInformation3D(
141  aViewInfo3D.getObjectTransformation() * aInBetweenSceneMatrix,
142  aViewInfo3D.getOrientation(),
143  aViewInfo3D.getProjection(),
144  aViewInfo3D.getDeviceToView(),
145  aViewInfo3D.getViewTime(),
146  aViewInfo3D.getExtendedInformationSequence());
147  }
148  }
149  else
150  {
151  const uno::Sequence< beans::PropertyValue > aEmptyParameters;
152  o_rViewInformation3D = drawinglayer::geometry::ViewInformation3D(aEmptyParameters);
153  }
154 
155  return pRootScene;
156 }
157 
158 
160  const basegfx::B2DPoint& rPoint,
161  const E3dScene& rScene,
162  ::std::vector< const E3dCompoundObject* >& o_rResult)
163 {
164  o_rResult.clear();
165  SdrObjList* pList = rScene.GetSubList();
166 
167  if(nullptr != pList && 0 != pList->GetObjCount())
168  {
169  // prepare relative HitPoint. To do so, get the VC of the 3DScene and from there
170  // the Scene's 2D transformation. Multiplying with the inverse transformation
171  // will create a point relative to the 3D scene as unit-2d-object
173  basegfx::B2DHomMatrix aInverseSceneTransform(rVCScene.getObjectTransformation());
174  aInverseSceneTransform.invert();
175  const basegfx::B2DPoint aRelativePoint(aInverseSceneTransform * rPoint);
176 
177  // check if test point is inside scene's area at all
178  if(aRelativePoint.getX() >= 0.0 && aRelativePoint.getX() <= 1.0 && aRelativePoint.getY() >= 0.0 && aRelativePoint.getY() <= 1.0)
179  {
180  SdrObjListIter aIterator(pList, SdrIterMode::DeepNoGroups);
181  ::std::vector< ImplPairDephAndObject > aDepthAndObjectResults;
182  const uno::Sequence< beans::PropertyValue > aEmptyParameters;
183  drawinglayer::geometry::ViewInformation3D aViewInfo3D(aEmptyParameters);
184 
185  while(aIterator.IsMore())
186  {
187  const E3dCompoundObject* pCandidate = dynamic_cast< const E3dCompoundObject* >(aIterator.Next());
188 
189  if(pCandidate)
190  {
191  fillViewInformation3DForCompoundObject(aViewInfo3D, *pCandidate);
192 
193  // create HitPoint Front and Back, transform to object coordinates
194  basegfx::B3DHomMatrix aViewToObject(aViewInfo3D.getObjectToView());
195  aViewToObject.invert();
196  const basegfx::B3DPoint aFront(aViewToObject * basegfx::B3DPoint(aRelativePoint.getX(), aRelativePoint.getY(), 0.0));
197  const basegfx::B3DPoint aBack(aViewToObject * basegfx::B3DPoint(aRelativePoint.getX(), aRelativePoint.getY(), 1.0));
198 
199  if(!aFront.equal(aBack))
200  {
201  // get all hit points with object
202  ::std::vector< basegfx::B3DPoint > aHitsWithObject;
203  getAllHit3DObjectWithRelativePoint(aFront, aBack, *pCandidate, aViewInfo3D, aHitsWithObject, false);
204 
205  for(const basegfx::B3DPoint & a : aHitsWithObject)
206  {
207  const basegfx::B3DPoint aPointInViewCoordinates(aViewInfo3D.getObjectToView() * a);
208  aDepthAndObjectResults.emplace_back(pCandidate, aPointInViewCoordinates.getZ());
209  }
210  }
211  }
212  }
213 
214  // fill nRetval
215  const sal_uInt32 nCount(aDepthAndObjectResults.size());
216 
217  if(nCount)
218  {
219  // sort aDepthAndObjectResults by depth
220  ::std::sort(aDepthAndObjectResults.begin(), aDepthAndObjectResults.end());
221 
222  // copy SdrObject pointers to return result set
223  for(const auto& rResult : aDepthAndObjectResults)
224  {
225  o_rResult.push_back(rResult.getObject());
226  }
227  }
228  }
229  }
230 }
231 
232 
234  const basegfx::B2DPoint& rPoint,
235  const E3dCompoundObject& rCandidate)
236 {
237  const uno::Sequence< beans::PropertyValue > aEmptyParameters;
238  drawinglayer::geometry::ViewInformation3D aViewInfo3D(aEmptyParameters);
239  E3dScene* pRootScene = fillViewInformation3DForCompoundObject(aViewInfo3D, rCandidate);
240 
241  if(pRootScene)
242  {
243  // prepare relative HitPoint. To do so, get the VC of the 3DScene and from there
244  // the Scene's 2D transformation. Multiplying with the inverse transformation
245  // will create a point relative to the 3D scene as unit-2d-object
246  const sdr::contact::ViewContactOfE3dScene& rVCScene = static_cast< sdr::contact::ViewContactOfE3dScene& >(pRootScene->GetViewContact());
247  basegfx::B2DHomMatrix aInverseSceneTransform(rVCScene.getObjectTransformation());
248  aInverseSceneTransform.invert();
249  const basegfx::B2DPoint aRelativePoint(aInverseSceneTransform * rPoint);
250 
251  // check if test point is inside scene's area at all
252  if(aRelativePoint.getX() >= 0.0 && aRelativePoint.getX() <= 1.0 && aRelativePoint.getY() >= 0.0 && aRelativePoint.getY() <= 1.0)
253  {
254  // create HitPoint Front and Back, transform to object coordinates
255  basegfx::B3DHomMatrix aViewToObject(aViewInfo3D.getObjectToView());
256  aViewToObject.invert();
257  const basegfx::B3DPoint aFront(aViewToObject * basegfx::B3DPoint(aRelativePoint.getX(), aRelativePoint.getY(), 0.0));
258  const basegfx::B3DPoint aBack(aViewToObject * basegfx::B3DPoint(aRelativePoint.getX(), aRelativePoint.getY(), 1.0));
259 
260  if(!aFront.equal(aBack))
261  {
262  // get all hit points with object
263  ::std::vector< basegfx::B3DPoint > aHitsWithObject;
264  getAllHit3DObjectWithRelativePoint(aFront, aBack, rCandidate, aViewInfo3D, aHitsWithObject, true);
265 
266  if(!aHitsWithObject.empty())
267  {
268  return true;
269  }
270  }
271  }
272  }
273 
274  return false;
275 }
276 
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double getX() const
size_t GetObjCount() const
Definition: svdpage.cxx:767
double getY() const
const drawinglayer::geometry::ViewInformation3D & getViewInformation3D(const ::basegfx::B3DRange &rContentRange) const
drawinglayer::primitive3d::Primitive3DContainer getViewIndependentPrimitive3DContainer() const
virtual SdrObjList * GetSubList() const override
Definition: scene3d.cxx:877
SbxObjectRef mpObject
sdr::contact::ViewContact & GetViewContact() const
Definition: svdobj.cxx:271
void process(const primitive3d::Primitive3DContainer &rSource)
bool IsMore() const
Definition: svditer.hxx:62
int nCount
E3dScene * getParentE3dSceneFromE3dObject() const
Definition: obj3d.cxx:281
uno_Any a
static void getAllHit3DObjectWithRelativePoint(const basegfx::B3DPoint &rFront, const basegfx::B3DPoint &rBack, const E3dCompoundObject &rObject, const drawinglayer::geometry::ViewInformation3D &rObjectViewInformation3D,::std::vector< basegfx::B3DPoint > &o_rResult, bool bAnyHit)
const basegfx::B3DHomMatrix & getObjectToView() const
bool isIdentity() const
bool checkHitSingle3DObject(const basegfx::B2DPoint &rPoint, const E3dCompoundObject &rCandidate)
support for checking if the single given 3d object is hit at position
bool equal(const B3DTuple &rTup) const
SdrObject * Next()
Definition: svditer.hxx:63
const basegfx::B2DHomMatrix & getObjectTransformation() const
bool operator<(const Subset &rLHS, const Subset &rRHS)
Definition: ucsubset.hxx:49
const ::std::vector< basegfx::B3DPoint > & getCutPoints() const
void getAllHit3DObjectsSortedFrontToBack(const basegfx::B2DPoint &rPoint, const E3dScene &rScene,::std::vector< const E3dCompoundObject * > &o_rResult)
support for getting all from a 2d position hit objects in a 3d scene in a depth sorted array ...
E3dScene * fillViewInformation3DForCompoundObject(drawinglayer::geometry::ViewInformation3D &o_rViewInformation3D, const E3dCompoundObject &rCandidate)
support for getting a ViewInformation3D for a given CompoudObject3D with correct ObjectTransformation...