Page 1 of 1

Custom bounding box hit test

Posted: 20 Sep 2021, 13:41
by krupitskas
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.

Re: Custom bounding box hit test

Posted: 20 Sep 2021, 17:12
by sfernandez
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?

Re: Custom bounding box hit test

Posted: 26 Sep 2021, 18:16
by krupitskas
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.

Re: Custom bounding box hit test

Posted: 26 Sep 2021, 18:17
by krupitskas
Also if text is rotated, how can I get it via VisualTreeHelper?

Re: Custom bounding box hit test

Posted: 29 Sep 2021, 18:42
by sfernandez
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;
..