User avatar
krupitskas
Topic Author
Posts: 23
Joined: 16 Apr 2021, 17:42

Custom bounding box hit test

20 Sep 2021, 13:41

Hello!
Im trying to find a way to receive Bounding Box information.
We need it for parenting in 3D space to automatically understand, does bb fit into the object or not and for other math caluclation.
First object we are working with is the TextBlock. Is it possible?

Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Custom bounding box hit test

20 Sep 2021, 17:12

VisualTreeHelper.GetDescendantBounds() will give you the 2D bounding box of any UI element in the tree. You can also obtain the min/max Z of the descendant if you need to calculate the 3D bounds.

Is this what you are looking for?
 
User avatar
krupitskas
Topic Author
Posts: 23
Joined: 16 Apr 2021, 17:42

Re: Custom bounding box hit test

26 Sep 2021, 18:16

Feel like not exactly.
I have
Noesis::Canvas
which is a parent of all texts.
If I use
GetDescendantBounds
, then it's aggregate all the text and gives incorrect bounding box.
If I call same function for just added text, then it returns always 0,0 as xy and some correct size, so it's a local values.
How can I get relative bounding box of added text to the canvas? Even if its 100 of them
For example when I add 1 text and call GetDescendantBounds on Canvas, then I have nice correct bounding box.
 
User avatar
krupitskas
Topic Author
Posts: 23
Joined: 16 Apr 2021, 17:42

Re: Custom bounding box hit test

26 Sep 2021, 18:17

Also if text is rotated, how can I get it via VisualTreeHelper?
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Custom bounding box hit test

29 Sep 2021, 18:42

Hi,

If you call TransformToAncestor() for the TextBlock you will get the matrix required to transform the local bounds to be relative to the Canvas.
Matrix4 toCanvasMtx = textBlock->TransformToAncestor(parentCanvas);
Then it depends if you need the bounding box of the text bbox projected into the canvas space:
Rect projectedBounds = bounds.Transform(toCanvasMtx);
Or if you need the bbox transformed in 3D, then you'll have to transform each of the vertices (this is what we do in our Inspector tool to render the 3D bounding boxes of the elements):
float txtNear = VisualTreeHelper::GetDescendantBoundsMinZ(textBlock);
float txtFar = VisualTreeHelper::GetDescendantBoundsMaxZ(textBlock);
Vector4 topLeftNear = Vector4(bounds.GetLeft(), bounds.GetTop(), txtNear, 1.0f) * toCanvasMtx;
Vector4 topLeftFar = Vector4(bounds.GetLeft(), bounds.GetTop(), txtFar, 1.0f) * toCanvasMtx;
Vector4 topRightNear = Vector4(bounds.GetRight(), bounds.GetTop(), txtNear, 1.0f) * toCanvasMtx;
Vector4 topRightFar = Vector4(bounds.GetRight(), bounds.GetTop(), txtFar, 1.0f) * toCanvasMtx;
..

Who is online

Users browsing this forum: Google [Bot] and 73 guests