OpenCASCADE Shape Location

OpenCASCADE Shape Location

eryar@163.com

Abstract. The TopLoc package of OpenCASCADE gives resources to handle 3D local coordinate systems called Locations. A Location is a composition of elementary coordinate systems, each one is called a Datum. The Location keeps track of this composition. The paper will use the Draw Test Harness to illustrate the Location concept.

Key Words. Transformation, Location, Local Coordinate Systems

1.Introduction

对于三维空间中的各种模型,总是想要摆放到合适的位置,最终形成一个工厂的模型,一艘船的模型,一个建筑物的模型,等等。目前来看,所有的几何相关的库对模型变换的实现一般都是使用了矩阵变换。有的可能只保留了最终变换的结果矩阵数据,而OpenCASCADE的TopoDS_Shape中保留了Location信息。从其文档可以看出,Location中保留了模型变换一系列的变换组合,并可以对这个变换进行Track追踪。如何来正确理解这个Location的意义呢?下面结合Draw Test Harness来进行说明。

2.Draw Test

在Draw Test Harness中对模型进行变换的命令有:ttranslate, trotate, tmove, reset, tmirror, tscale.其中ttranslate, trotate, tmove, reset命令只会对模型位置进行调整,并不能让模型发生变形,即是刚性变换。下面就通过对一个Box进行移动和旋转,来看看TopoDS_Shape中的Location是如何变化的。在Draw Test Harness中输入以下命令:

pload ALL 
box b 10 20 30 
vdisplay b 
vtrihedron vt 
dump b 

可以看到此时box的Location是没有的:

Figure 2.1 Box Location in Original

当将box沿X方向移动一段距离后:

# translate by x direction 
ttranslate b 10 0 0 
vdisplay b 
dump b

Figure 2.2 Location of the translation box

由上图可知,当对模型进行变换后,TopoDS_Shape中即有了Location数据,变换矩阵的平移部分(第4列数据)发生了变化。下面继续沿X轴方向移动10:

# translate by x direction 
ttranslate b 10 0 0 
vdisplay b 
dump b

Figure 2.3 Translate Box in X Direction

由图2.3可知,模型现在的位置是通过两个Elementary变换得来的。最后一个Complex变换是上述所有变换的复合。下面再对模型进行绕Y轴旋转45度:

# rotate by y axis 
trotate b 0 0 0 0 1 0 45 
vdisplay b 
dump b 

Figure 2.4 Rotate the Box

由上图可知,经过旋转变换后的模型有了4个Location:三个基本变换和一个复合变换,复合变换是所有基本变换的组合。

通过上面的示例,已经可以清晰理解OpenCASCADE中的Location的概念,虽然处理得有点复杂,通过Location可以对模型的变换轨迹有个详细的跟踪。这样处理的好处就是对模型的变换过程进行了记录,可以方便地回到历史上的任意一个时刻;不好的地方就是程序理解起来麻烦,而且还需要有额外的内存来保存这些数据。
以下为上述所有Tcl脚本:

#
# test TopoDS_Shape location.
# Shing Liu(eryar@163.com)
# 2016-09-06 22:50

pload ALL

# initialize 
box b 10 20 30
vdisplay b
vtrihedron vt
dump b

# translate by x direction
ttranslate b 10 0 0
vdisplay b
dump b

# translate by x direction
ttranslate b 10 0 0
vdisplay b
dump b

# rotate by y axis
trotate b 0 0 0 0 1 0 45
vdisplay b
dump b

3.Draw Code

每个Draw Test Harness命令都可以方便地找到其实现代码,其中模型变换命令的实现代码如下:

//=======================================================================
// transform
//=======================================================================

static Standard_Integer transform(Draw_Interpretor& ,Standard_Integer n,const char** a)
{
  if (n <= 1) return 1;

  gp_Trsf T;
  Standard_Integer last = n;
  const char* aName = a[0];

  Standard_Boolean isBasic = Standard_False;

  if (!strcmp(aName,"reset")) {
  }
  else {
    isBasic = (aName[0] == 'b');
    aName++;

    if (!strcmp(aName,"move")) {
      if (n < 3) return 1;
      TopoDS_Shape SL = DBRep::Get(a[n-1]);
      if (SL.IsNull()) return 0;
      T = SL.Location().Transformation();
      last = n-1;
    }
    else if (!strcmp(aName,"translate")) {
      if (n < 5) return 1;
      T.SetTranslation(gp_Vec(Draw::Atof(a[n-3]),Draw::Atof(a[n-2]),Draw::Atof(a[n-1])));
      last = n-3;
    }
    else if (!strcmp(aName,"rotate")) {
      if (n < 9) return 1;
      T.SetRotation(gp_Ax1(gp_Pnt(Draw::Atof(a[n-7]),Draw::Atof(a[n-6]),Draw::Atof(a[n-5])),
                    gp_Vec(Draw::Atof(a[n-4]),Draw::Atof(a[n-3]),Draw::Atof(a[n-2]))),
                    Draw::Atof(a[n-1])* (M_PI / 180.0));
      last = n-7;
    }
    else if (!strcmp(aName,"mirror")) {
      if (n < 8) return 1;
      T.SetMirror(gp_Ax2(gp_Pnt(Draw::Atof(a[n-6]),Draw::Atof(a[n-5]),Draw::Atof(a[n-4])),
                  gp_Vec(Draw::Atof(a[n-3]),Draw::Atof(a[n-2]),Draw::Atof(a[n-1]))));
      last = n-6;
    }
    else if (!strcmp(aName,"scale")) {
      if (n < 6) return 1;
      T.SetScale(gp_Pnt(Draw::Atof(a[n-4]),Draw::Atof(a[n-3]),Draw::Atof(a[n-2])),Draw::Atof(a[n-1]));
      last = n-4;
    }
  }

  if (T.Form() == gp_Identity || isBasic) {
    TopLoc_Location L(T);
    for (Standard_Integer i = 1; i < last; i++) {
      TopoDS_Shape S = DBRep::Get(a[i]);
      if (S.IsNull())
      {
        std::cerr << "Error: " << a[i] << " is not a valid shape\n";
        return 1;
      }
      else
        DBRep::Set(a[i],S.Located(L));
    }
  }
  else {
    BRepBuilderAPI_Transform trf(T);
    for (Standard_Integer i = 1; i < last; i++) {
      TopoDS_Shape S = DBRep::Get(a[i]);
      if (S.IsNull()) {
        std::cerr << "Error: " << a[i] << " is not a valid shape\n";
        return 1;
      }
      else {
        trf.Perform(S);
        if (!trf.IsDone())
          return 1;
        DBRep::Set(a[i],trf.Shape());
      }
    }
  }
  return 0;
}

对模型的变换主要使用类BRepBuilderAPI_Transform来完成。

4.Conclusion

通过上面的示例,已经可以清晰理解OpenCASCADE中的Location的概念,虽然处理得有点复杂,通过Location可以对模型的变换轨迹有个详细的跟踪。这样处理的好处就是对模型的变换过程进行了记录,可以方便地回到历史上的任意一个时刻;不好的地方就是程序理解起来麻烦,而且还需要有额外的内存来保存这些数据。

理解了Location的概念,也就理解了OpenCASCADE的Brep文件中的一项基本内容,方便开发一些转换接口。

关于矩阵变换在图形学中的应用可以参考《3D数学基础:图形与游戏开发》。

5.References

1.Fletcher Dunn, Ian Parberry. 3D Math Primer for Graphics and Game Development. 清华大学出版社. 2005

2.OpenCASCADE Draw Test Harness User Guide.

 

PDF Version: OpenCASCADE Shape Location

时间: 2024-09-20 09:00:03

OpenCASCADE Shape Location的相关文章

OpenCascade Shape Representation in OpenSceneGraph

OpenCascade Shape Representation in OpenSceneGraph eryar@163.com 摘要Abstract:本文通过程序实例,将OpenCascade中的拓朴数据(边.面)离散化后在OpenSceneGraph中进行显示.有了这些离散数据,就可以不用OpenCascade的显示模块了,可以使用其他显示引擎对形状进行显示.即若要线框模式显示形状时,就绘制离散形状拓朴边后得到的多段线:若要实体渲染模式显示形状时,就绘制离散形状拓朴面得到的三角网格.理解这些

OpenCASCADE Outline

OpenCASCADE Outline eryar@163.com      有网友反映blog中关于OpenCASCADE的文章比较杂乱,不太好找,最好能提供一个大纲,这样方便查找.于是决定将这些学习时写的文章整理下,方便对OpenCASCADE的学习理解.其实在http://www.cnblogs.com/opencascade中,已经将文章按目录重新发表了一遍.可以按OpenCASCADE的模块的顺序来学习,也可以挑选自己感兴趣的部分来学习.      由于本人水平所限,文中的错误不妥之处

Locations Section of OpenCascade BRep

Locations Section of OpenCascade BRep eryar@163.com 摘要Abstract:本文结合OpenCascade的BRep格式描述文档和源程序,对BRep格式进行分析,详细说明BRep的数据组织形式.本文主要通过对BRep文件中的Locations部分的读写代码进行分析,来完全理解OpenCascade中的Location部分. 关键字Key Words:OpenCascade, BRep Format, Location, Location Set

Topology and Geometry in OpenCascade-Location and Orientaion

Topology and Geometry in OpenCascade Location and Orientaion eryar@163.com 摘要Abstract:本文简要介绍了几何造型中的边界表示法(BRep),并结合程序说明OpenCascade中的边界表示的具体实现,即拓朴与几何的联系.拓朴结构中的位置(Location)和朝向(Orientation)进行了详细说明. 关键字Key Words:OpenCascade.BRep.Topology.Geometry.Location

Open Cascade DataExchange IGES

Open Cascade DataExchange IGES eryar@163.com 摘要Abstract:本文结合OpenCascade和Initial Graphics Exchange Specification来对IGES格式进行详细说明,理解IGES格式的结构,进而方便对OpenCascade中IGES格式文件读写的实现进行理解. 关键字Key Words:IGES.Open Cascade.Data Exchange 一.引言 Introduction 目前市面上的CAD系统都有

Open CASCADE Modeling Data &amp;ndash; Topology

Open CASCADE Modeling Data – Topology eryar@163.com 一.概述 Overview Open CASCADE拓朴数据结构可以在几何对象不显示的情况下访问或控制对象的数据.拓朴数据结构在参数空间描述了几何对象,而Geometry用坐标和参数值来定义几何对象.为了提供这种描述,Open CASCADE抽象的拓朴结构提供以下功能: u 跟踪形状的位置: u 对形状.子形状.状态进行命名: u 操纵形状和子形状: u 遍历拓朴数据结构: u 使用形状的列表

OpenCASCADE BRepTools

OpenCASCADE BRepTools eryar@163.com Abstract. OpenCASCADE BRepTools provides utilities for BRep data structure. OuterWire method to find the outer wire of a face. Dump method to dump a BRep object. It also can be used as the data exchange for OpenCAS

OpenCASCADE General Transformation

OpenCASCADE General Transformation eryar@163.com Abstract. OpenCASCADE provides a general transformation class: gp_GTrsf. It can be a transformation from gp, an affinity, or you can define your own transformation giving the matrix of transformation.

Make Helix Shape in occQt

Make Helix Shape in occQt PCurve is a powerful modeling method, to make helix shapes in occQt to help you understand pcurve better, then you can also can model some intereting shape based on pcurve. OpenCASCADE does not provide helix curve directly,