Matlab绘图中的对象容器(Object Containers)

对象容器(Object Containers)


概述

Grouping objects and operating on the group as a whole.


Functions

函数 功能
hggroup 创建组对象
hgtransform 创建图形变换对象
makehgtform 创建4x4的变换矩阵
eye 创建4x4单位矩阵

Properties

属性 含义
Group Properties 控制组对象的外观和行为
Transform Properties 控制图形变换对象的外观和行为

Examples and How To

Create Object Groups
Create an object group by parenting objects to a group or transform object.

Rotate About an Arbitrary Axis
This example shows how to rotate an object about an arbitrary axis.

Nest Transforms for Complex Movements
This example creates a nested hierarchy of transform objects, which are then transformed in sequence to create a cube from six squares.(创建一个嵌套的层次变换对象, 然后按顺序依次转换来从6个正方形创建一个立方体)


Concepts

Object Groups
Group objects are invisible containers for graphics objects. Use group objects to form a collection of objects that can behave as one object in certain respects.(组对象是不可见的图形对象,使用组对象在某些情况下可以将多个对象当作一个对象处理)

Transforms Supported by hgtransform
The transform object’s Matrix property applies a transform to all the object’s children in unison.(图形变换的矩阵属性表示将一个变换应用于该对象的每一个子对象,也就是左乘)



Create Object Groups

Create an object group by parenting objects to a group or transform object. For example, call hggroup to create a group object and save its handle. Assign this group object as the parent of subsequently created objects:
(生成一个组对象的方式是将“组对象成员的 Parent 属设置为组对象的句柄”。)

hg = hggroup;
plot(rand(5),'Parent',hg);
text(3,0.5,'Random lines','Parent',hg);

Setting the visibility of the group to off makes the line and text objects it contains invisible.
(将组对象的的可见性设置为off,以使线和字符都不可见)

hg.Visible = 'off';

You can add objects to a group selectively. For example, the following call to the bar function returns the handles to five separate bar objects:
(你可以有选择性的将一个对象添加到组对象中。例如,下面的代码是先生成一个含有含有5个bar句柄的数组,然后使用 set 函数将其中的句柄 3-5添加到组对象中)

hb = bar(randn(5))
hg = hggroup;
set(hb(3:5),'Parent',hg) 
hb = 
	1x5 Bar array:
		Bar    Bar    Bar    Bar    Bar

Group objects can be the parent of any number of axes children, including other group objects. For examples, see Rotate About an Arbitrary Axis and Nest Transforms for Complex Movements. (组对象的 children 可以使任意数量的axes 对象的 children——也就是一个坐标轴的子对象,包含其它的组对象也是可以的。详细参见后面的Rotate About an Arbitrary Axis Nest Transforms for Complex Movements. )


Parent Specification

Plotting functions clear the axes before generating their graph. However, if you assign a group or transform as the Parent in the plotting function, the group or transform object is not cleared.
(当你再 绘图函数(plot、patch、line等) 中指定 Parent 属性时,大致相当于添加了hold on。普通绘图时,绘图函数(plot、patch、line等)会清除之前的图形对象(相当于 cla),所以如果使用set,则必须绘制完图形之后再创建组对象/变换对象)

For example:

hg = hggroup;
hb = bar(randn(5));
set(hb,'Parent',hg)
Error using matlab.graphics.chart.primitive.Bar/set Cannot set property to a deleted object

The bar function cleared the axes. However, if you set the Parent property as a name/value pair in the bar function arguments, the bar function does not delete the group:

hg = hggroup;
hb = bar(randn(5),'Parent',hg);

Visible and Selected Properties of Group Children

Setting the Visible property of a group or transform object controls whether all the objects in the group are visible or not visible. However, changing the state of the Visible property for the group object does not change the state of this property for the individual objects. The values of the Visible property for the individual objects are preserved.
(通过设置一个group/transform objectVisible 属性来控制组中所有对象的可见性。此时,并不改变组中个体的Visible 属性。)

For example, if the Visible property of the group is set to off and subsequently set to on, only the objects that were originally visible are displayed.
(例如,当组的 Visible属性设置为 on-->off-->on , 则仅显式个体属性为 on 的个体)

The same behavior applies to the Selected and SelectionHighlight properties. The children of the group or transform object show the state of the containing object properties without actually changing their own property values.



Rotate About an Arbitrary Axis

This example shows how to rotate an object about an arbitrary axis.

  • Translate to Origin Before Rotating
  • Rotate Surface

Translate to Origin Before Rotating

Rotations are performed about the origin. Therefore, you need to perform a translation so that the intended axis of rotation is temporarily at the origin. After applying the rotation transform matrix, you then translate the object back to its original position.

Rotate Surface

This example shows how to rotate a surface about the y-axis.

Create Surface and Transform

Parent the surface to the transform object.

t = hgtransform;
surf(peaks(40),'Parent',t)
view(-20,30)
axis manual

这里写图片描述

Create Transform

上面是原始图像,要对其进行变换,首先需要使用makehgtform生成变换矩阵,具体使用方法参见makehgtform的函数说明
Set a y-axis rotation matrix to rotate the surface by -15 degrees.

ry_angle = -15*pi/180;
Ry = makehgtform('yrotate',ry_angle);
t.Matrix = Ry;

The surface rotated -15 degrees about the y-axis that passes through the origin.
这里写图片描述

Translate the Surface and Rotate

Now rotate the surface about the y-axis that passes through the point x = 20.

Create two translation matrices, one to translate the surface -20 units in x and another to translate 20 units back. Concatenate the two translation matrices with the rotation matrix in the correct order and set the transform.

Tx1 = makehgtform('translate',[-20 0 0]);
Tx2 = makehgtform('translate',[20 0 0]);
t.Matrix = Tx2*Ry*Tx1;
% 或者
t.Matrix = Tx2*Ry*Tx2*Tx1*Tx1

这里写图片描述

可以看到:

  • 可以将makehgtform的输出看作是 元变换,所有其它的变换都是通过元变换 组合生成。而元变换的参考是(0,0,0)点。
  • t.Matrix 仅仅由一个 元变换组成时,则直接做相应的变换即可。
  • t.Matrix 由多个 元变换组成时,可以选择从其中任意一个元变换开始,然后左乘等价于在原坐标系做相应的变换,右乘等价于在变换后的坐标系中做相应的变换。

比如:
t.Matrix = Tx2*Ry*Tx1;
可以看作是,先将t在元坐标系中左移20(Tx1)
这里写图片描述
然后再以 x=0处 的Y轴为旋转轴旋转 Ry
这里写图片描述
最后再沿x轴右移 20 (Tx2);
或者看作,先旋转,然后整体沿着原坐标系的x方向右移20(Tx2),最后沿着绕y轴旋转-15°后(逆时针)的x轴负方向移动20.

猜你喜欢

转载自blog.csdn.net/fdcp123/article/details/74330515