- krupitskas
- Posts: 23
- Joined:
Custom bounding box hit test
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.
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.
-
sfernandez
Site Admin
- Posts: 2993
- Joined:
Re: Custom bounding box hit test
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?
Is this what you are looking for?
- krupitskas
- Posts: 23
- Joined:
Re: Custom bounding box hit test
Feel like not exactly.
I have which is a parent of all texts.
If I use, 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.
I have
Code: Select all
Noesis::Canvas
If I use
Code: Select all
GetDescendantBounds
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.
- krupitskas
- Posts: 23
- Joined:
Re: Custom bounding box hit test
Also if text is rotated, how can I get it via VisualTreeHelper?
-
sfernandez
Site Admin
- Posts: 2993
- Joined:
Re: Custom bounding box hit test
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.
Then it depends if you need the bounding box of the text bbox projected into the canvas space:
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):
If you call TransformToAncestor() for the TextBlock you will get the matrix required to transform the local bounds to be relative to the Canvas.
Code: Select all
Matrix4 toCanvasMtx = textBlock->TransformToAncestor(parentCanvas);
Code: Select all
Rect projectedBounds = bounds.Transform(toCanvasMtx);
Code: Select all
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;
..