通過一個平面擺正視圖

先說下開發思路:
在PRO/E中,視圖矩陣是4X4的矩陣。其構成如下圖所示:

Rotation(3x3)        |    Projection(3x1)
translation(1x3)    |    scaling/zoom(1x1)
控制PRO/E中視圖的方法,就是需要將Rotation中的3X3矩陣設置好。現將Rotation(3x3)矩陣寫成Rotation[3][3],則Rotation[0]為X向量,指向屏幕左邊;Rotation[1]為Y向量,指向屏幕上邊,Rotation[2]為Z向量,垂直屏幕指向外面,其中X,Y,Z三向量滿足右手法則。所以如果要將一個平面正面朝向用戶,則只要將平面的法向方向成為視圖矩陣的Z向即可。

平面的方向如下圖:


以下是源代碼:

TKView.cpp

#include "stdafx.h"
#include "TKView.h"

#include
#include
#include

#include ".\TKFiles\UtilMatrix.h"

//
// TKView的默認構造函數
//
TKView::TKView(void)
{
// 將視圖矩陣置成單位矩陣
InitUnitMatrix(frontViewMatrix);
InitUnitMatrix(bottomViewMatrix);
}

//
// TKView的默認析構函數
//
TKView::~TKView(void)
{
}


//------------------------------------------------------------
// 名稱: SetViewReferencePlane
// 描述: 設置視圖參考平面
//
// 輸入參數:
// surfPlane - 視力參考平面
//
// 輸出參數:
//
//
// 返回值:
// PRO_TK_NO_ERROR - 設置參考平面成功
// PRO_TK_GENERAL_ERROR - 設置參考平面失敗
//
// 版本:1.0
// 作者:Aaron
// 時間:2010.09.03
//------------------------------------------------------------
ProError TKView::SetViewReferencePlane(ProSurface surfPlane)
{
ProError status;

// 獲取平面的幾何信息
ProGeomitemdata *geomData;
status = ProSurfaceDataGet(surfPlane, &geomData);
if (status != PRO_TK_NO_ERROR)
{
return PRO_TK_GENERAL_ERROR;
}

// 判斷給定的面是否為平面
if (geomData->data.p_surface_data->type != PRO_SRF_PLANE)
{
return PRO_TK_GENERAL_ERROR;
}

// 初始化前視圖矩陣
InitFrontViewMatrix(geomData->data.p_surface_data);

// 初始化底視圖矩陣
InitBottomViewMatrix(geomData->data.p_surface_data);

// 釋放內存
status = ProGeomitemdataFree(&geomData);

return PRO_TK_NO_ERROR;
}



//------------------------------------------------------------
// 名稱: InitFrontViewMatrix
// 描述: 初始化前視圖對應矩陣
//
// 輸入參數:
// surfData - 平面相關信息
//
// 輸出參數:
//
//
// 返回值:
// PRO_TK_NO_ERROR - 初始化成功
// PRO_TK_GENERAL_ERROR - 初始化失敗
//
// 版本:1.0
// 作者:Aaron
// 時間:2010.09.03
//------------------------------------------------------------
ProError TKView::InitFrontViewMatrix(ProSurfacedata* surfData)
{
// 設置方法
// 視圖矩陣的第一行表示X(朝屏幕左),第二行表示Y(朝屏幕右),第三行表示Z(垂直屏幕朝外)
// 即如果要使參考面朝向用戶,需使平面的法向方向成為視圖矩陣的Z方向
// 所以先確定視圖矩陣的Z向量,然後根據Y,Z向量使用右手法則確定X向量(也可由Z,X向量確定Y向量)

ProError status;

// Y向量
frontViewMatrix[1][0] = surfData->srf_shape.plane.e2[0];
frontViewMatrix[1][1] = surfData->srf_shape.plane.e2[1];
frontViewMatrix[1][2] = surfData->srf_shape.plane.e2[2];

// Z向量
int orient = surfData->orient;
frontViewMatrix[2][0] = orient * surfData->srf_shape.plane.e3[0];
frontViewMatrix[2][1] = orient * surfData->srf_shape.plane.e3[1];
frontViewMatrix[2][2] = orient * surfData->srf_shape.plane.e3[2];

// X向量由Y,Z向量運用右手法則確定,即X~ = Y~ X Z~
frontViewMatrix[0][0] = frontViewMatrix[1][1] * frontViewMatrix[2][2] - frontViewMatrix[1][2] * frontViewMatrix[2][1];
frontViewMatrix[0][1] = -1 *(frontViewMatrix[1][0] * frontViewMatrix[2][2] - frontViewMatrix[1][2] * frontViewMatrix[2][0]);
frontViewMatrix[0][2] = frontViewMatrix[1][0] * frontViewMatrix[2][1] - frontViewMatrix[1][1] * frontViewMatrix[2][0];

// 求矩陣的逆,使其成為視圖矩陣
ProMatrix viewMatrix;
ProUtilMatrixInvert(frontViewMatrix, viewMatrix);
ProUtilMatrixCopy(viewMatrix, frontViewMatrix);

// 將矩陣單位化
ProUtilMatrixNormalize(frontViewMatrix);

return PRO_TK_NO_ERROR;
}


//------------------------------------------------------------
// 名稱: InitBottomViewMatrix
// 描述: 初始底視圖對應矩陣
//
// 輸入參數:
// surfData - 平面相關信息
//
// 輸出參數:
//
//
// 返回值:
// PRO_TK_NO_ERROR - 初始化成功
// PRO_TK_GENERAL_ERROR - 初始化失敗
//
// 版本:1.0
// 作者:Aaron
// 時間:2010.09.03
//------------------------------------------------------------
ProError TKView::InitBottomViewMatrix(ProSurfacedata* surfData)
{
// 設置方法
// 視圖矩陣的第一行表示X(朝屏幕左),第二行表示Y(朝屏幕右),第三行表示Z(垂直屏幕朝外)
// 即如果要使參考面朝向BOTTOM方向,需使平面的法向方向成為視圖矩陣的-Y方向
// 所以先確定視圖矩陣的Z向量,然後根據Y,Z向量使用右手法則確定X向量(也可由Z,X向量確定Y向量)

ProError status;

// Z向量
bottomViewMatrix[2][0] = surfData->srf_shape.plane.e2[0];
bottomViewMatrix[2][1] = surfData->srf_shape.plane.e2[1];
bottomViewMatrix[2][2] = surfData->srf_shape.plane.e2[2];

// Y向量
int orient = -1 * surfData->orient;
bottomViewMatrix[1][0] = orient * surfData->srf_shape.plane.e3[0];
bottomViewMatrix[1][1] = orient * surfData->srf_shape.plane.e3[1];
bottomViewMatrix[1][2] = orient * surfData->srf_shape.plane.e3[2];

// X向量由Y,Z向量運用右手法則確定,即X~ = Y~ X Z~
bottomViewMatrix[0][0] = bottomViewMatrix[1][1] * bottomViewMatrix[2][2] - bottomViewMatrix[1][2] * bottomViewMatrix[2][1];
bottomViewMatrix[0][1] = -1 *(bottomViewMatrix[1][0] * bottomViewMatrix[2][2] - bottomViewMatrix[1][2] * bottomViewMatrix[2][0]);
bottomViewMatrix[0][2] = bottomViewMatrix[1][0] * bottomViewMatrix[2][1] - bottomViewMatrix[1][1] * bottomViewMatrix[2][0];

// 求矩陣的逆,使其成為視圖矩陣
ProMatrix viewMatrix;
ProUtilMatrixInvert(bottomViewMatrix, viewMatrix);
ProUtilMatrixCopy(viewMatrix, bottomViewMatrix);

// 將矩陣單位化
ProUtilMatrixNormalize(bottomViewMatrix);

return PRO_TK_NO_ERROR;
}


//------------------------------------------------------------
// 名稱: SetFrontView
// 描述: 將視圖設成前視圖
//
// 輸入參數:
// refit - 是否放縮視圖使整個檔案適合於窗口,默認為true
//
// 輸出參數:
//
//
// 返回值:
// PRO_TK_NO_ERROR - 設置視圖成功
// PRO_TK_GENERAL_ERROR - 設置視力失敗
//
// 版本:1.0
// 作者:Aaron
// 時間:2010.09.03
//------------------------------------------------------------
ProError TKView::SetFrontView(bool refit)
{
ProError status;

// 擺正視圖
status = ProViewMatrixSet(NULL, NULL, frontViewMatrix);

// 縮放視窗
if (refit)
{
status = ProViewRefit(NULL, NULL);
}

// 刷新窗口
status = ProWindowRepaint(-1);

return PRO_TK_NO_ERROR;
}


//------------------------------------------------------------
// 名稱: SetBottomView
// 描述: 將視圖設成底視圖
//
// 輸入參數:
// refit - 是否放縮視圖使整個檔案適合於窗口,默認為true
//
// 輸出參數:
//
//
// 返回值:
// PRO_TK_NO_ERROR - 設置視圖成功
// PRO_TK_GENERAL_ERROR - 設置視力失敗
//
// 版本:1.0
// 作者:Aaron
// 時間:2010.09.03
//------------------------------------------------------------
ProError TKView::SetBottomView(bool refit)
{
ProError status;

// 擺正視圖
status = ProViewMatrixSet(NULL, NULL, bottomViewMatrix);

// 縮放視窗
if (refit)
{
status = ProViewRefit(NULL, NULL);
}

// 刷新窗口
status = ProWindowRepaint(-1);

return PRO_TK_NO_ERROR;
}

TKView.h

#pragma once

#include
#include

class TKView
{
public:
TKView(void);
~TKView(void);

public:
// 設置視圖參考平面
ProError SetViewReferencePlane(ProSurface surfPlane);
// 將視圖設成前視圖
ProError SetFrontView(bool refit = true);
// 將視圖設成底視圖
ProError SetBottomView(bool refit = true);

private:
// 初始化前視圖對應矩陣
ProError InitFrontViewMatrix(ProSurfacedata* surfData);
// 初始底視圖對應矩陣
ProError InitBottomViewMatrix(ProSurfacedata* surfData);

private:
// 前視圖對應矩陣
ProMatrix frontViewMatrix;
// 底視圖對應矩陣
ProMatrix bottomViewMatrix;
};

調用

ProError TestViewSet()
{
ProError status;

// 選擇參考面
ProSurface surfPlane;
ProPoint3d position;
status = SelectSurfacePlane(surfPlane, position);
if (status != PRO_TK_NO_ERROR)
{
return NULL;
}

// 自動擺正視圖
TKView viewManager;
status = viewManager.SetViewReferencePlane(surfPlane);
if (status != PRO_TK_NO_ERROR)
{
return NULL;
}
status = viewManager.SetFrontView();

return PRO_TK_NO_ERROR;
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24494482/viewspace-672526/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24494482/viewspace-672526/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值