Make 3-D annotations independent of viewpoint
34 views (last 30 days)
I would like to annotate various straight lines in a 3-D plot in MATLAB, by showing the line number in the middle of the line. It serves as an identifier to users, so it should remain visible and readable when the plot is being rotated. Currently, I am using this:
plot3([0 1],[0 1],[0 1])
th = text(0.5,0.5,0.5,'1');
The label stays 'upright' when rotating (which is good), but depending on the viewpoint, the lines cross the number, making it hard to read.
Example 1: this viewpoint is OK:
Example 2: this viewpoint is not OK:
Do you have a suggestion for improving the annotations, such that the lines do not cross the line number visually? I cannot figure out how to deal with something like the z-index in these plots (e.g. uistack(th) does not seem to work).
Saurabh Gupta on 19 Jul 2017
Conceptually speaking, I think there is a fundamental difference between what you want to achieve and the way the plot is being rendered in 3D. I'm not an expert in this area, but I will try to explain as follows.
The concept of uistack applies to 2D UI design where you have to decide which visual elements appear at what part of visual ordering because there is exactly one viewpoint. On the other hand, 3D rendering depends upon the relative ordering of the specific points in the current viewpoint. A point is hidden if there is another point closer to the observer obscuring it.
Your text box is being rendered at point (0.5,0.5,0.5) in space with default (i.e. left) horizontal alignment, as per your design. During rotation, the text is re-rendered using the same logic based on the view-point to make sure it is consistently readable. But the text is very much a visual element in the 3D space/axes.
The part of the line that you are perceiving to be crossing the text is actually in front of the text-box in 3D space and is expected to be displayed. The part that gets cut-off is the part that is behind the text-box, and hence obscured. You may be able to observe this effect more clearly using a longer string of text with centered horizontal alignment.
In order for the text to always stay in front of the plot, it will need to be outside the axes. You could use text style 'uicontrol' object and keep it on top of the visual UI stack (on top of the axes), but it would affect your ability to interact with the plot and the position of the text will not correspond to a point on the axes.