闲来无聊,记录下最近几天测试的一些内容,最近突然想搞一个机械臂仿真测试,搜了下github,发现有个很好的例子,链接是:GitHub - junkle-wxt/Qt_Robots: 记录Qt关于机器人仿真的工具,主要使用Coin3D、msvc2008、Qt4.8.6记录Qt关于机器人仿真的工具,主要使用Coin3D、msvc2008、Qt4.8.6. Contribute to junkle-wxt/Qt_Robots development by creating an account on GitHub.https://github.com/junkle-wxt/Qt_Robots
编译也很简单,运行效果如下图:
拖动滚动条即可操作各个关节,因为读取的关节点是wri文件模型,后面想用OCC做个类似的功能,因为对这块实在没什么接触,咨询了下我的好朋友,看了写有关的知识点(也没这么看懂) ,后面看到github的案例时,本人觉得一个速成的方法就是为做个程序增加一个stp模型转换接口就好了,所以晚上脑补了下关于COIN3D的渲染,了解了他的基本流程,也做了一些模型绘制测试,效果都还可以,于是我根据OCC的一些接口(主要是离散接口)做了从OCC到COIN3D的转换接口,效果如下:
注意:模型不一样,DH设置也是不一样的。这里需要自己根据自己的模型进行设置即可。
接口我写的比较随意(主要是做测试使用),代码如下:
///
//occ to coin3d 以读取stp为例
SoSeparator* Robot::readSTP(const std::string& filename)
{
SoSeparator* robot = new SoSeparator;
//
STEPControl_Reader StepReader; //const char*
Standard_Integer status = StepReader.ReadFile(filename.c_str());
if (status == IFSelect_RetDone)
{
StepReader.TransferRoots();
TopoDS_Shape aShape = StepReader.OneShape();
if (!aShape.IsNull())
{
BRepMesh_IncrementalMesh(aShape, 0.01, Standard_False, 0.5, Standard_True);
TopExp_Explorer faceExplorer;
for (faceExplorer.Init(aShape, TopAbs_FACE); faceExplorer.More(); faceExplorer.Next())
{
SoCoordinate3* points = new SoCoordinate3();
SoMaterial* color = new SoMaterial();
color->diffuseColor.setValue(1, 1, 0);
color->transparency.setValue(0);
robot->addChild(color);
SoFaceSet* soface = new SoFaceSet;
robot->addChild(soface);
robot->addChild(points);
TopLoc_Location loc;
TopoDS_Face aFace = TopoDS::Face(faceExplorer.Current());
//2020-06-14
Handle_Poly_Triangulation triFace = BRep_Tool::Triangulation(aFace, loc);
gp_Pnt vertex1;
gp_Pnt vertex2;
gp_Pnt vertex3;
Standard_Integer nVertexIndex1 = 0;
Standard_Integer nVertexIndex2 = 0;
Standard_Integer nVertexIndex3 = 0;
if (triFace.IsNull())
{
continue;
}
Standard_Boolean hasNormal = triFace->HasNormals();
Standard_Boolean hasuvNormal = triFace->HasUVNodes();
Standard_Integer l = triFace->Nodes().Length();
Standard_Integer nTriangles = triFace->NbTriangles();
TColgp_Array1OfPnt nodes(1, triFace->NbNodes());
Poly_Array1OfTriangle triangles(1, triFace->NbTriangles());
nodes = triFace->Nodes();
triangles = triFace->Triangles();
points->point.setNum(l);
int verindex = 0;
for (Standard_Integer i = 1; i <= nTriangles; i++)
{
vertex1.SetCoord(0.0, 0.0, 0.0);
vertex2.SetCoord(0.0, 0.0, 0.0);
vertex3.SetCoord(0.0, 0.0, 0.0);
nVertexIndex1 = 0;
nVertexIndex2 = 0;
nVertexIndex3 = 0;
Poly_Triangle aTriangle = triangles.Value(i);
aTriangle.Get(nVertexIndex1, nVertexIndex2, nVertexIndex3);
//顶点
vertex1 = nodes.Value(nVertexIndex1).Transformed(loc.Transformation());
vertex2 = nodes.Value(nVertexIndex2).Transformed(loc.Transformation());
vertex3 = nodes.Value(nVertexIndex3).Transformed(loc.Transformation());
{
//
points->point.set1Value(verindex, vertex1.X(), vertex1.Y(), vertex1.Z());
points->point.set1Value(verindex+1, vertex2.X(), vertex2.Y(), vertex2.Z());
points->point.set1Value(verindex+2, vertex3.X(), vertex3.Y(), vertex3.Z());
verindex+=3;;
}
}
}
}
}
return robot;
}
//
以上就是测试的基本内容,请多多指教。