2

我有一个名为 的类transform,以及它的子类和translation,它们应该对三角形应用变换。rotationscaling

每个子类都覆盖该apply_transform()方法:

class transform
{
protected:
    virtual triangle apply_transform(const triangle&) const = 0;
public:
    static triangle apply_transforms(const triangle&, const std::initializer_list<const transform*>&);
};

class scaling : public transform
{
...
public:
    triangle apply_transform(const triangle&) const override;
};

//same for rotation and translation

我还有一个名为 的函数apply_transforms,它应该可供外界访问,我使用它来应用多个转换。我将一个列表传递给它transform*以启用多态性。

我唯一的问题是,现在子类也知道这种方法。这让我很困扰,因为子类不应该能够应用所有其他转换。

有一个优雅的解决方案吗?

4

3 回答 3

4

创建一个非成员函数,apply_transforms该函数不包含在实现transform.

于 2017-05-31T16:05:46.503 回答
2

我建议稍微改变一下你对变换的看法。

  1. 创建一个类,transform这样它就不需要从它派生其他类。转换一个点所需的所有数据都可以保存在这个类中。

  2. 添加构造缩放变换、平移变换和旋转变换的函数。

  3. 添加函数以乘以变换,并将变换和点相乘。这些可以成为转换其他形状的构建块。

  4. 根据需要添加用于转换其他形状的功能。


以骨骼的形式,

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;
于 2017-05-31T16:27:35.503 回答
1

事实是,由于您的apply_transforms(...)方法是公开的,因此所有潜在的调用者都可以使用它。考虑到这一点,您不能也不应该阻止子类看到这些方法。

如果您知道您的方法将从特定的类中调用,您可以将您的方法设为私有并将这些调用类声明为朋友。

否则,将该方法封装在不同的类中,以防止子类transform包含它。

于 2017-05-31T17:09:29.977 回答