c++ - 确定将一个平面转换为与另一个平面平行的仿射变换

标签 c++ cgal affinetransform

我如何确定将一个平面 (plane1) 转换为与另一个平面 (plane2) 平行的平面的 CGAL 仿射变换 (Aff_transformation_3)?

假设我有两个物体平面:

Plane_3  pl1;
Plane_3  pl2;

而且它们不是平行线,这种仿射变换如何确定?

Aff_transformation_3 t3 = ??? (pl1, pl2);

我咨询了这个问题和你的回答:CGAL: Transformation Matrix for Rotation given two lines/vectors/directions ,但我不知道它对我有什么帮助。我有两个平面,但是是 3d 维度的。

谢谢。

最佳答案

我不知道 2d 仿射变换 (Aff_transformation_2) 如何帮助我应用 3d 仿射变换 (Aff_transformation_3)。

但是,我找到了问题的解决方案。这是我希望能帮助别人的代码。

typedef CGAL::Cartesian<double>         KC;

typedef KC::Line_3                      Line3;
typedef KC::Vector_3                    Vector3;
typedef KC::Plane_3                     Plane3;
typedef CGAL::Aff_transformation_3<KC>  Transform3;

// forwards
struct axis_angle;

typedef boost::shared_ptr<axis_angle>   RAxisAngle;

struct axis_angle
{
    axis_angle()
    {
        angle = 0;
        axis = Vector3(0.0, 0.0, 0.0);
    }

    double  angle;
    Vector3 axis;
};

Vector3 normalize(const Vector3 &v)
{
    ldouble len = ::sqrt(v.squared_length());

    if (len == 0.0)
        return v;

    return v / len;
}

// return the angle and axis from two planes that there are not parallels
RAxisAngle axis_angle_from_planes(const Plane3 &pln1, const Plane3 &pln2)
{
    RAxisAngle result = RAxisAngle(new axis_angle());

    Vector3 norm1 = pln1.orthogonal_vector();
    Vector3 norm2 = pln2.orthogonal_vector();

    double dot_r = norm1 * norm2;
    double len_r = ::sqrt(norm1.squared_length() * norm2.squared_length());

    if (len_r)
        result->angle = ::acos(dot_r / len_r);
    else
        result->angle = 0.0;

    Line3 l1;
    CGAL::Object obj_cgal = CGAL::intersection(pln1, pln2);
    if (CGAL::assign(l1, obj_cgal))
    {
        result->axis = normalize(l1.to_vector());
    }
    else
    {
        // when planes are parallels, then use some basic axis
        result->axis = Vector3(1.0, 0.0, 0.0);
    }

    return result;
}

// return a CGAL affine transformation that is builded from a 3x3 matrix
// this transformation is for rotate an object from axis and angle
// http://en.wikipedia.org/wiki/Transformation_matrix
// http://en.wikipedia.org/wiki/Rotation_matrix
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm
Transform3 axis_angle_to_matrix(const RAxisAngle &aa)
{
    double tmp1, tmp2;

    double c = ::cos(aa->angle);
    double s = ::sin(aa->angle);
    double t = 1.0 - c;

    double m00 = c + aa->axis.x() * aa->axis.x() * t;
    double m11 = c + aa->axis.y() * aa->axis.y() * t;
    double m22 = c + aa->axis.z() * aa->axis.z() * t;

    tmp1 = aa->axis.x() * aa->axis.y() * t;
    tmp2 = aa->axis.z() * s;
    double m10 = tmp1 + tmp2;
    double m01 = tmp1 - tmp2;

    tmp1 = aa->axis.x() * aa->axis.z() * t;
    tmp2 = aa->axis.y() * s;
    double m20 = tmp1 - tmp2;
    double m02 = tmp1 + tmp2;

    tmp1 = aa->axis.y() * aa->axis.z() * t;
    tmp2 = aa->axis.x() * s;
    double m21 = tmp1 + tmp2;
    double m12 = tmp1 - tmp2;

    return Transform3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
}

然后,我可以像这样使用:

RAxisAngle aa = axis_angle_from_planes(plane1, plane2);
Transform3 t3 = axis_angle_to_matrix(aa);

Plane2 new_transform_plane = plane1.transform(t3);

或者这个平面的一个点:

Point3 new_transform_point = point_of_plane1.transform(t3);

感谢您给我发布我的小解决方案的机会。

关于c++ - 确定将一个平面转换为与另一个平面平行的仿射变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923948/

相关文章:

c++ - Linux 中的未知类型名称 uint64_t 和 uint16_t uint8_t

c++ - 抽象类的堆分配

c++ - 具有 std::function 值的 std::map 调用析构函数 4 次但仅构造一个对象

c++ - libcurl - 5 秒后出现奇怪的超时

c++ - CGAL Constrained_Delaunay_Triangulation 和 Triangulation_vertex_base_with_info

java - 如何在 Java 中旋转 BufferedImage?

java围绕中心旋转矩形

Java游戏旋转图像和轴

visual-studio - Visual Studio/C++ : How to turn off certain first-chance exception debug messages?

centos - 如何在 CentOS 7(或 CentOS 6)上安装 cgal?