首页 > 代码库 > OCC 矩阵变换

OCC 矩阵变换

在OpenCADCADE中, 通过gp_Trsf类来进行矩阵变换操作,

采用矩阵在左的方式: 新点 = 变换矩阵 * 点

基本原理如下:

//! Defines a non-persistent transformation in 3D space.
//! The following transformations are implemented :
//! . Translation, Rotation, Scale
//! . Symmetry with respect to a point, a line, a plane.
//! Complex transformations can be obtained by combining the
//! previous elementary transformations using the method
//! Multiply.
//! The transformations can be represented as follow :
//!
//! V1   V2   V3    T       XYZ        XYZ
//! | a11  a12  a13   a14 |   | x |      | x‘|
//! | a21  a22  a23   a24 |   | y |      | y‘|
//! | a31  a32  a33   a34 |   | z |   =  | z‘|
//! |  0    0    0     1  |   | 1 |      | 1 |
//!
//! where {V1, V2, V3} defines the vectorial part of the
//! transformation and T defines the translation part of the
//! transformation.
//! This transformation never change the nature of the objects.

  gp_Trsf定义了单个平移, 旋转, 缩放, 对称等操作

复杂变换: 需要通过 gp_Trsf乘法来实现, 如:

  一个物体要经过 缩放, 旋转, 平移等一系列操作时, 必须定义为

  平移 * 旋转 * 缩放

Python示例:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import math
from OCC.gp import (gp_Pnt2d, gp_Vec2d, gp_Pnt, gp_Vec,
                    gp_Ax1, gp_OX, gp_OY, gp_OZ,
                    gp_Trsf)

atranslation = gp_Trsf()
atranslation.SetTranslation(gp_Vec(1, 1, 1))

arotate = gp_Trsf()
arotate.SetRotation(gp_OZ(), math.pi)

atransform = atranslation * arotate

def test1(): 
    print(测试1, 平移 -> 旋转)
    pt = gp_Pnt(-1, -1, -1)
    print(pt.Coord())
    pt2 = pt.Transform(atranslation)
    print(pt.Coord())
    pt2 = pt.Transform(arotate)
    print(pt.Coord())


def test2():
    print(测试2, 平移 * 旋转)
    pt = gp_Pnt(-1, -1, -1)
    print(pt.Coord())
    pt2 = pt.Transform(atranslation * arotate)
    print(pt.Coord())

def test3():
    print(测试3, 旋转 -> 平移)
    pt = gp_Pnt(-1, -1, -1)
    print(pt.Coord())
    pt2 = pt.Transform(arotate)
    print(pt.Coord())
    pt2 = pt.Transform(atranslation)
    print(pt.Coord())

def test4():
    print(测试4, 旋转 * 平移)
    pt = gp_Pnt(-1, -1, -1)
    print(pt.Coord())
    pt2 = pt.Transform(arotate * atranslation)
    print(pt.Coord())


if __name__ == __main__:
    test1()
    test2()
    test3()
    test4()
输出结果为:
测试1, 平移 -> 旋转 (-1.0, -1.0, -1.0) (0.0, 0.0, 0.0) (0.0, 0.0, 0.0) 测试2, 平移 * 旋转 (-1.0, -1.0, -1.0) (2.0, 2.0, 0.0) 测试3, 旋转 -> 平移 (-1.0, -1.0, -1.0) (1.0000000000000002, 0.9999999999999999, -1.0) (2.0, 2.0, 0.0) 测试4, 旋转 * 平移 (-1.0, -1.0, -1.0) (0.0, 0.0, 0.0)

 

OCC 矩阵变换