我建议稍微改变一下你对变换的看法。
创建一个类,transform
这样它就不需要从它派生其他类。转换一个点所需的所有数据都可以保存在这个类中。
添加构造缩放变换、平移变换和旋转变换的函数。
添加函数以乘以变换,并将变换和点相乘。这些可以成为转换其他形状的构建块。
根据需要添加用于转换其他形状的功能。
以骨骼的形式,
class transform { ... };
class position { ... };
// tag structs
struct rotation_about_x {};
struct rotation_about_y {};
struct rotation_about_z {};
// Functions to construct transforms
transform construct_scale_transform(double scale_factor) { ... };
transform construct_translation_transform(position pos) { ... };
transform construct_rotation_transform(double angle, rotation_about_x tag) { ... };
transform construct_rotation_transform(double angle, rotation_about_y tag) { ... };
transform construct_rotation_transform(double angle, rotation_about_z tag) { ... };
// Function to transform a point.
position operator*(transform const& t, position const& p) { ... }
// Function to multiply transforms.
transform operator*(transform const& t1, transform const& t2) { ... }
// Functions to apply transforms to other objects.
triangle operator*(transform const& tr, triangle const& t) { ... }
...
用法:
transform t1 = construct_rotation_transform(10, rotation_about_x{});
transform t2 = construct_translation_transform({20, 10, 0});
position p1{100, 200, 30};
position p2 = t1*t2*p1;
triangle tr1{ ... }
triangle tr2 = t1*t2*tr1;
如果要多次使用相同的组合变换,请先计算组合变换并将其用于所有变换。
transform t1 = construct_rotation_transform(10, rotation_about_x{});
transform t2 = construct_rotation_transform(5, rotation_about_y{});
transform t3 = construct_translation_transform({20, 10, 0});
tranform tc = t1 * t2 * t3;
position p1{100, 200, 30};
position p2 = tc*p1;
triangle tr1{ ... }
triangle tr2 = tc*tr1;