1. 程式人生 > >OCC 矩陣變換

OCC 矩陣變換

where win plane env 輸出結果 clas 對稱 ane efi

在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 矩陣變換