我正在编写一个计算机图形应用程序,它利用了几个不同的库,这些库提供了许多所需的功能。
例如,在大学里,我编写了四元数、优化的3向量和优化的4x4矩阵类,我喜欢使用这些类,因为它们很高效,我对它们非常熟悉。我还编写了一个仅依赖于STL的K-D树实现。
FixedPointType closestPointCoords;
KdTree::HyperPoint target(3);
target[0] = transformedPoint[0];
target[1] = transformedPoint[1];
target[2] = transformedPoint[2];
KdTree::Neighbor result = FixedPointTree.FindNearestNeighbor(target);
minimumDistance = result.GetDistance();
closestPointCoords[0] = result.GetPoint().Get(0);
closestPointCoords[1] = result.GetPoint().Get(1);
closestPointCoords[2] = result.GetPoint().Get(2);
FixedPointType ConvertHyperPointToFixedPointType(const KdTree::HyperPoint &p)
{
FixedPointType fixedPoint;
fixedPoint[0] = p[0];
fixedPoint[1] = p[1];
fixedPoint[2] = p[2];
return fixedPoint;
}
class HyperPoint
{
...
operator FixedPointType()
{
// Same content as ConvertHyperPointToFixedPointType above
}
...
};
也许我应该翻一页。NET书籍并创建静态Convert类?
是
Adapter
更新
TypeConverter
paper
了解更多信息。
class VectorConverter
{
public:
VectorConverter(const VectorImp1 &v1)
{
_data[0] = v1[0];
_data[1] = v1[1];
_data[2] = v1[2];
}
VectorConverter(const VectorImp2 &v2)
{
_data[0] = v2.Get(0);
_data[1] = v2.Get(1);
_data[2] = v2.Get(2);
}
operator VectorImp1()
{
return VectorImp1(_data[0], _data[1], _data[2]);
}
operator VectorImp2()
{
VectorImp2 v2;
v2.Set(0, _data[0]);
v2.Set(1, _data[1]);
v2.Set(2, _data[2]);
return v2;
}
// actual vector implementation goes here or a wrapper around an existing one
};
// example usage
VectorImp1 closestPointCoords, target;
// for the sake of illustration I included the cast, but you could implicitly cast
KdTree::Neighbor result = FixedPointTree.FindNearestNeighbor((VectorImp2)VectorConverter(target));
minimumDistance = result.GetDistance();
closestPointCoords = (VectorImp1)VectorConverter(result.GetPoint());