1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <drawinglayer/processor2d/hittestprocessor2d.hxx>
21 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
22 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
23 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
24 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <basegfx/polygon/b2dpolypolygontools.hxx>
27 #include <drawinglayer/primitive2d/transparenceprimitive2d.hxx>
28 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
29 #include <drawinglayer/primitive2d/sceneprimitive2d.hxx>
30 #include <drawinglayer/primitive2d/pointarrayprimitive2d.hxx>
31 #include <basegfx/matrix/b3dhommatrix.hxx>
32 #include <drawinglayer/processor3d/cutfindprocessor3d.hxx>
33 #include <drawinglayer/primitive2d/hiddengeometryprimitive2d.hxx>
34 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
35 #include <comphelper/lok.hxx>
37 namespace drawinglayer
41 HitTestProcessor2D::HitTestProcessor2D(const geometry::ViewInformation2D
& rViewInformation
,
42 const basegfx::B2DPoint
& rLogicHitPosition
,
43 double fLogicHitTolerance
,
45 : BaseProcessor2D(rViewInformation
),
46 maDiscreteHitPosition(),
47 mfDiscreteHitTolerance(0.0),
49 mbCollectHitStack(false),
51 mbHitTextOnly(bHitTextOnly
)
54 mfDiscreteHitTolerance
= fLogicHitTolerance
;
56 if(basegfx::fTools::less(mfDiscreteHitTolerance
, 0.0))
58 // ensure input parameter for hit tolerance is >= 0.0
59 mfDiscreteHitTolerance
= 0.0;
61 else if(basegfx::fTools::more(mfDiscreteHitTolerance
, 0.0))
63 // generate discrete hit tolerance
64 mfDiscreteHitTolerance
= (getViewInformation2D().getObjectToViewTransformation()
65 * basegfx::B2DVector(mfDiscreteHitTolerance
, 0.0)).getLength();
68 // generate discrete hit position
69 maDiscreteHitPosition
= getViewInformation2D().getObjectToViewTransformation() * rLogicHitPosition
;
72 HitTestProcessor2D::~HitTestProcessor2D()
76 bool HitTestProcessor2D::checkHairlineHitWithTolerance(
77 const basegfx::B2DPolygon
& rPolygon
,
78 double fDiscreteHitTolerance
) const
80 basegfx::B2DPolygon
aLocalPolygon(rPolygon
);
81 aLocalPolygon
.transform(getViewInformation2D().getObjectToViewTransformation());
84 basegfx::B2DRange
aPolygonRange(aLocalPolygon
.getB2DRange());
86 if(basegfx::fTools::more(fDiscreteHitTolerance
, 0.0))
88 aPolygonRange
.grow(fDiscreteHitTolerance
);
91 // do rough range test first
92 if(aPolygonRange
.isInside(getDiscreteHitPosition()))
94 // check if a polygon edge is hit
95 return basegfx::utils::isInEpsilonRange(
97 getDiscreteHitPosition(),
98 fDiscreteHitTolerance
);
104 bool HitTestProcessor2D::checkFillHitWithTolerance(
105 const basegfx::B2DPolyPolygon
& rPolyPolygon
,
106 double fDiscreteHitTolerance
) const
109 basegfx::B2DPolyPolygon
aLocalPolyPolygon(rPolyPolygon
);
110 aLocalPolyPolygon
.transform(getViewInformation2D().getObjectToViewTransformation());
112 // get discrete range
113 basegfx::B2DRange
aPolygonRange(aLocalPolyPolygon
.getB2DRange());
114 const bool bDiscreteHitToleranceUsed(basegfx::fTools::more(fDiscreteHitTolerance
, 0.0));
116 if(bDiscreteHitToleranceUsed
)
118 aPolygonRange
.grow(fDiscreteHitTolerance
);
121 // do rough range test first
122 if(aPolygonRange
.isInside(getDiscreteHitPosition()))
124 // if a HitTolerance is given, check for polygon edge hit in epsilon first
125 if(bDiscreteHitToleranceUsed
&&
126 basegfx::utils::isInEpsilonRange(
128 getDiscreteHitPosition(),
129 fDiscreteHitTolerance
))
134 // check for hit in filled polyPolygon
135 if(!bRetval
&& basegfx::utils::isInside(
137 getDiscreteHitPosition(),
147 void HitTestProcessor2D::check3DHit(const primitive2d::ScenePrimitive2D
& rCandidate
)
149 // calculate relative point in unified 2D scene
150 const basegfx::B2DPoint
aLogicHitPosition(getViewInformation2D().getInverseObjectToViewTransformation() * getDiscreteHitPosition());
152 // use bitmap check in ScenePrimitive2D
153 bool bTryFastResult(false);
155 if(rCandidate
.tryToCheckLastVisualisationDirectHit(aLogicHitPosition
, bTryFastResult
))
157 mbHit
= bTryFastResult
;
161 basegfx::B2DHomMatrix
aInverseSceneTransform(rCandidate
.getObjectTransformation());
162 aInverseSceneTransform
.invert();
163 const basegfx::B2DPoint
aRelativePoint(aInverseSceneTransform
* aLogicHitPosition
);
165 // check if test point is inside scene's unified area at all
166 if(aRelativePoint
.getX() >= 0.0 && aRelativePoint
.getX() <= 1.0
167 && aRelativePoint
.getY() >= 0.0 && aRelativePoint
.getY() <= 1.0)
169 // get 3D view information
170 const geometry::ViewInformation3D
& rObjectViewInformation3D
= rCandidate
.getViewInformation3D();
172 // create HitPoint Front and Back, transform to object coordinates
173 basegfx::B3DHomMatrix
aViewToObject(rObjectViewInformation3D
.getObjectToView());
174 aViewToObject
.invert();
175 const basegfx::B3DPoint
aFront(aViewToObject
* basegfx::B3DPoint(aRelativePoint
.getX(), aRelativePoint
.getY(), 0.0));
176 const basegfx::B3DPoint
aBack(aViewToObject
* basegfx::B3DPoint(aRelativePoint
.getX(), aRelativePoint
.getY(), 1.0));
178 if(!aFront
.equal(aBack
))
180 const primitive3d::Primitive3DContainer
& rPrimitives
= rCandidate
.getChildren3D();
182 if(!rPrimitives
.empty())
184 // make BoundVolume empty and overlapping test for speedup
185 const basegfx::B3DRange
aObjectRange(
186 rPrimitives
.getB3DRange(rObjectViewInformation3D
));
188 if(!aObjectRange
.isEmpty())
190 const basegfx::B3DRange
aFrontBackRange(aFront
, aBack
);
192 if(aObjectRange
.overlaps(aFrontBackRange
))
194 // bound volumes hit, geometric cut tests needed
195 drawinglayer::processor3d::CutFindProcessor
aCutFindProcessor(
196 rObjectViewInformation3D
,
200 aCutFindProcessor
.process(rPrimitives
);
202 mbHit
= (!aCutFindProcessor
.getCutPoints().empty());
211 // empty 3D scene; Check for border hit
212 basegfx::B2DPolygon
aOutline(basegfx::utils::createUnitPolygon());
213 aOutline
.transform(rCandidate
.getObjectTransformation());
215 mbHit
= checkHairlineHitWithTolerance(aOutline
, getDiscreteHitTolerance());
220 void HitTestProcessor2D::processBasePrimitive2D(const primitive2d::BasePrimitive2D
& rCandidate
)
224 // stop processing as soon as a hit was recognized
228 switch(rCandidate
.getPrimitive2DID())
230 case PRIMITIVE2D_ID_TRANSFORMPRIMITIVE2D
:
232 // remember current ViewInformation2D
233 const primitive2d::TransformPrimitive2D
& rTransformCandidate(static_cast< const primitive2d::TransformPrimitive2D
& >(rCandidate
));
234 const geometry::ViewInformation2D
aLastViewInformation2D(getViewInformation2D());
236 // create new local ViewInformation2D containing transformation
237 const geometry::ViewInformation2D
aViewInformation2D(
238 getViewInformation2D().getObjectTransformation() * rTransformCandidate
.getTransformation(),
239 getViewInformation2D().getViewTransformation(),
240 getViewInformation2D().getViewport(),
241 getViewInformation2D().getVisualizedPage(),
242 getViewInformation2D().getViewTime(),
243 getViewInformation2D().getExtendedInformationSequence());
244 updateViewInformation(aViewInformation2D
);
246 // process child content recursively
247 process(rTransformCandidate
.getChildren());
249 // restore transformations
250 updateViewInformation(aLastViewInformation2D
);
254 case PRIMITIVE2D_ID_POLYGONHAIRLINEPRIMITIVE2D
:
256 if(!getHitTextOnly())
258 // create hairline in discrete coordinates
259 const primitive2d::PolygonHairlinePrimitive2D
& rPolygonCandidate(static_cast< const primitive2d::PolygonHairlinePrimitive2D
& >(rCandidate
));
262 mbHit
= checkHairlineHitWithTolerance(rPolygonCandidate
.getB2DPolygon(), getDiscreteHitTolerance());
267 case PRIMITIVE2D_ID_POLYGONMARKERPRIMITIVE2D
:
269 if(!getHitTextOnly())
271 // handle marker like hairline; no need to decompose in dashes
272 const primitive2d::PolygonMarkerPrimitive2D
& rPolygonCandidate(static_cast< const primitive2d::PolygonMarkerPrimitive2D
& >(rCandidate
));
275 mbHit
= checkHairlineHitWithTolerance(rPolygonCandidate
.getB2DPolygon(), getDiscreteHitTolerance());
280 case PRIMITIVE2D_ID_POLYGONSTROKEPRIMITIVE2D
:
282 if(!getHitTextOnly())
284 // handle stroke evtl. directly; no need to decompose to filled polygon outlines
285 const primitive2d::PolygonStrokePrimitive2D
& rPolygonCandidate(static_cast< const primitive2d::PolygonStrokePrimitive2D
& >(rCandidate
));
286 const attribute::LineAttribute
& rLineAttribute
= rPolygonCandidate
.getLineAttribute();
288 if(basegfx::fTools::more(rLineAttribute
.getWidth(), 0.0))
290 if(basegfx::B2DLineJoin::Miter
== rLineAttribute
.getLineJoin())
292 // if line is mitered, use decomposition since mitered line
293 // geometry may use more space than the geometry grown by half line width
298 // for all other B2DLINEJOIN_* do a hairline HitTest with expanded tolerance
299 const basegfx::B2DVector
aDiscreteHalfLineVector(getViewInformation2D().getObjectToViewTransformation()
300 * basegfx::B2DVector(rLineAttribute
.getWidth() * 0.5, 0.0));
301 mbHit
= checkHairlineHitWithTolerance(
302 rPolygonCandidate
.getB2DPolygon(),
303 getDiscreteHitTolerance() + aDiscreteHalfLineVector
.getLength());
308 // hairline; fallback to hairline test. Do not decompose
309 // since this may decompose the hairline to dashes
310 mbHit
= checkHairlineHitWithTolerance(rPolygonCandidate
.getB2DPolygon(), getDiscreteHitTolerance());
316 case PRIMITIVE2D_ID_POLYGONWAVEPRIMITIVE2D
:
318 if(!getHitTextOnly())
320 // do not use decompose; just handle like a line with width
321 const primitive2d::PolygonWavePrimitive2D
& rPolygonCandidate(static_cast< const primitive2d::PolygonWavePrimitive2D
& >(rCandidate
));
322 double fLogicHitTolerance(0.0);
324 // if WaveHeight, grow by it
325 if(basegfx::fTools::more(rPolygonCandidate
.getWaveHeight(), 0.0))
327 fLogicHitTolerance
+= rPolygonCandidate
.getWaveHeight();
330 // if line width, grow by it
331 if(basegfx::fTools::more(rPolygonCandidate
.getLineAttribute().getWidth(), 0.0))
333 fLogicHitTolerance
+= rPolygonCandidate
.getLineAttribute().getWidth() * 0.5;
336 const basegfx::B2DVector
aDiscreteHalfLineVector(getViewInformation2D().getObjectToViewTransformation()
337 * basegfx::B2DVector(fLogicHitTolerance
, 0.0));
339 mbHit
= checkHairlineHitWithTolerance(
340 rPolygonCandidate
.getB2DPolygon(),
341 getDiscreteHitTolerance() + aDiscreteHalfLineVector
.getLength());
346 case PRIMITIVE2D_ID_POLYPOLYGONCOLORPRIMITIVE2D
:
348 if(!getHitTextOnly())
350 // create filled polyPolygon in discrete coordinates
351 const primitive2d::PolyPolygonColorPrimitive2D
& rPolygonCandidate(static_cast< const primitive2d::PolyPolygonColorPrimitive2D
& >(rCandidate
));
354 mbHit
= checkFillHitWithTolerance(rPolygonCandidate
.getB2DPolyPolygon(), getDiscreteHitTolerance());
359 case PRIMITIVE2D_ID_TRANSPARENCEPRIMITIVE2D
:
361 // sub-transparence group
362 const primitive2d::TransparencePrimitive2D
& rTransCandidate(static_cast< const primitive2d::TransparencePrimitive2D
& >(rCandidate
));
364 // Currently the transparence content is not taken into account; only
365 // the children are recursively checked for hit. This may be refined for
366 // parts where the content is completely transparent if needed.
367 process(rTransCandidate
.getChildren());
371 case PRIMITIVE2D_ID_MASKPRIMITIVE2D
:
373 // create mask in discrete coordinates; only recursively continue
374 // with content when HitTest position is inside the mask
375 const primitive2d::MaskPrimitive2D
& rMaskCandidate(static_cast< const primitive2d::MaskPrimitive2D
& >(rCandidate
));
378 if(checkFillHitWithTolerance(rMaskCandidate
.getMask(), getDiscreteHitTolerance()))
380 // recursively HitTest children
381 process(rMaskCandidate
.getChildren());
386 case PRIMITIVE2D_ID_SCENEPRIMITIVE2D
:
388 if(!getHitTextOnly())
390 const primitive2d::ScenePrimitive2D
& rScenePrimitive2D(
391 static_cast< const primitive2d::ScenePrimitive2D
& >(rCandidate
));
392 check3DHit(rScenePrimitive2D
);
397 case PRIMITIVE2D_ID_WRONGSPELLPRIMITIVE2D
:
398 case PRIMITIVE2D_ID_MARKERARRAYPRIMITIVE2D
:
399 case PRIMITIVE2D_ID_GRIDPRIMITIVE2D
:
400 case PRIMITIVE2D_ID_HELPLINEPRIMITIVE2D
:
402 // ignorable primitives
405 case PRIMITIVE2D_ID_SHADOWPRIMITIVE2D
:
407 // Ignore shadows; we do not want to have shadows hittable.
408 // Remove this one to make shadows hittable on demand.
411 case PRIMITIVE2D_ID_TEXTSIMPLEPORTIONPRIMITIVE2D
:
412 case PRIMITIVE2D_ID_TEXTDECORATEDPORTIONPRIMITIVE2D
:
414 // for text use the BoundRect of the primitive itself
415 const basegfx::B2DRange
aRange(rCandidate
.getB2DRange(getViewInformation2D()));
417 if(!aRange
.isEmpty())
419 const basegfx::B2DPolygon
aOutline(basegfx::utils::createPolygonFromRect(aRange
));
420 mbHit
= checkFillHitWithTolerance(basegfx::B2DPolyPolygon(aOutline
), getDiscreteHitTolerance());
425 case PRIMITIVE2D_ID_BITMAPPRIMITIVE2D
:
427 if(!getHitTextOnly())
429 // The recently added BitmapEx::GetTransparency() makes it easy to extend
430 // the BitmapPrimitive2D HitTest to take the contained BitmapEx and it's
431 // transparency into account
432 const basegfx::B2DRange
aRange(rCandidate
.getB2DRange(getViewInformation2D()));
434 if(!aRange
.isEmpty())
436 const primitive2d::BitmapPrimitive2D
& rBitmapCandidate(static_cast< const primitive2d::BitmapPrimitive2D
& >(rCandidate
));
437 const BitmapEx
& rBitmapEx
= rBitmapCandidate
.getBitmapEx();
438 const Size
& rSizePixel(rBitmapEx
.GetSizePixel());
440 // When tiled rendering, don't bother with the pixel size of the candidate.
441 if(rSizePixel
.Width() && rSizePixel
.Height() && !comphelper::LibreOfficeKit::isActive())
443 basegfx::B2DHomMatrix
aBackTransform(
444 getViewInformation2D().getObjectToViewTransformation() *
445 rBitmapCandidate
.getTransform());
446 aBackTransform
.invert();
448 const basegfx::B2DPoint
aRelativePoint(aBackTransform
* getDiscreteHitPosition());
449 const basegfx::B2DRange
aUnitRange(0.0, 0.0, 1.0, 1.0);
451 if(aUnitRange
.isInside(aRelativePoint
))
453 const sal_Int32
nX(basegfx::fround(aRelativePoint
.getX() * rSizePixel
.Width()));
454 const sal_Int32
nY(basegfx::fround(aRelativePoint
.getY() * rSizePixel
.Height()));
456 mbHit
= (0xff != rBitmapEx
.GetTransparency(nX
, nY
));
461 // fallback to standard HitTest
462 const basegfx::B2DPolygon
aOutline(basegfx::utils::createPolygonFromRect(aRange
));
463 mbHit
= checkFillHitWithTolerance(basegfx::B2DPolyPolygon(aOutline
), getDiscreteHitTolerance());
470 case PRIMITIVE2D_ID_METAFILEPRIMITIVE2D
:
471 case PRIMITIVE2D_ID_CONTROLPRIMITIVE2D
:
472 case PRIMITIVE2D_ID_FILLGRADIENTPRIMITIVE2D
:
473 case PRIMITIVE2D_ID_FILLHATCHPRIMITIVE2D
:
474 case PRIMITIVE2D_ID_PAGEPREVIEWPRIMITIVE2D
:
475 case PRIMITIVE2D_ID_MEDIAPRIMITIVE2D
:
477 if(!getHitTextOnly())
479 // Class of primitives for which just the BoundRect of the primitive itself
480 // will be used for HitTest currently.
482 // This may be refined in the future, e.g:
483 // - For Bitmaps, the mask and/or transparence information may be used
484 // - For MetaFiles, the MetaFile content may be used
485 const basegfx::B2DRange
aRange(rCandidate
.getB2DRange(getViewInformation2D()));
487 if(!aRange
.isEmpty())
489 const basegfx::B2DPolygon
aOutline(basegfx::utils::createPolygonFromRect(aRange
));
490 mbHit
= checkFillHitWithTolerance(basegfx::B2DPolyPolygon(aOutline
), getDiscreteHitTolerance());
496 case PRIMITIVE2D_ID_HIDDENGEOMETRYPRIMITIVE2D
:
498 // HiddenGeometryPrimitive2D; the default decomposition would return an empty sequence,
499 // so force this primitive to process its children directly if the switch is set
500 // (which is the default). Else, ignore invisible content
501 const primitive2d::HiddenGeometryPrimitive2D
& rHiddenGeometry(static_cast< const primitive2d::HiddenGeometryPrimitive2D
& >(rCandidate
));
502 const primitive2d::Primitive2DContainer
& rChildren
= rHiddenGeometry
.getChildren();
504 if(!rChildren
.empty())
511 case PRIMITIVE2D_ID_POINTARRAYPRIMITIVE2D
:
513 if(!getHitTextOnly())
515 const primitive2d::PointArrayPrimitive2D
& rPointArrayCandidate(static_cast< const primitive2d::PointArrayPrimitive2D
& >(rCandidate
));
516 const std::vector
< basegfx::B2DPoint
>& rPositions
= rPointArrayCandidate
.getPositions();
517 const sal_uInt32
nCount(rPositions
.size());
519 for(sal_uInt32
a(0); !getHit() && a
< nCount
; a
++)
521 const basegfx::B2DPoint
aPosition(getViewInformation2D().getObjectToViewTransformation() * rPositions
[a
]);
522 const basegfx::B2DVector
aDistance(aPosition
- getDiscreteHitPosition());
524 if(aDistance
.getLength() <= getDiscreteHitTolerance())
535 // process recursively
542 if (getHit() && getCollectHitStack())
544 /// push candidate to HitStack to create it. This only happens when a hit is found and
545 /// creating the HitStack was requested (see collectHitStack)
546 maHitStack
.append(primitive2d::Primitive2DReference(const_cast< primitive2d::BasePrimitive2D
* >(&rCandidate
)));
550 } // end of namespace processor2d
551 } // end of namespace drawinglayer
553 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */