gmio/src/support/occ_libstl.cpp

116 lines
4.0 KiB
C++
Raw Normal View History

#include "occ_libstl.h"
#include <cstring>
#include <StlMesh_Mesh.hxx>
#include <StlMesh_MeshTriangle.hxx>
#include <StlMesh_SequenceOfMeshTriangle.hxx>
#include <TColgp_SequenceOfXYZ.hxx>
2014-01-27 22:03:50 +08:00
namespace internal {
/* Common */
2014-01-27 22:03:50 +08:00
static StlMesh_Mesh* occMeshPtr(const Handle_StlMesh_Mesh& mesh)
{
2014-01-27 22:03:50 +08:00
return mesh.operator->();
}
2014-01-27 22:03:50 +08:00
static void occmesh_add_triangle(void* cookie,
2014-01-29 18:33:55 +08:00
uint32_t tri_id,
const foug_stl_triangle_t* tri)
{
2014-01-27 22:03:50 +08:00
StlMesh_Mesh* mesh = static_cast<StlMesh_Mesh*>(cookie);
2014-01-29 18:33:55 +08:00
if (tri_id == 0)
mesh->AddDomain();
2014-01-27 22:03:50 +08:00
const int uId = mesh->AddOnlyNewVertex(tri->v1.x, tri->v1.y, tri->v1.z);
const int vId = mesh->AddOnlyNewVertex(tri->v2.x, tri->v2.y, tri->v2.z);
const int wId = mesh->AddOnlyNewVertex(tri->v3.x, tri->v3.y, tri->v3.z);
mesh->AddTriangle(uId, vId, wId, tri->normal.x, tri->normal.y, tri->normal.z);
}
2014-01-27 22:03:50 +08:00
static void occmesh_get_triangle(const void* cookie,
uint32_t tri_id,
foug_stl_triangle_t* triangle)
{
2014-01-27 22:03:50 +08:00
const foug_OccStlMeshDomain* meshCookie = static_cast<const foug_OccStlMeshDomain*>(cookie);
const StlMesh_SequenceOfMeshTriangle& occTriangles = meshCookie->mesh->Triangles(meshCookie->domainId);
const Handle_StlMesh_MeshTriangle& occTri = occTriangles.Value(tri_id + 1);
Standard_Integer v1;
Standard_Integer v2;
Standard_Integer v3;
Standard_Real xN;
Standard_Real yN;
Standard_Real zN;
occTri->GetVertexAndOrientation(v1, v2, v3, xN, yN, zN);
triangle->normal.x = float(xN);
triangle->normal.y = float(yN);
triangle->normal.z = float(zN);
2014-01-27 22:03:50 +08:00
const TColgp_SequenceOfXYZ& vertices = meshCookie->mesh->Vertices(meshCookie->domainId);
const gp_XYZ& coordsV1 = vertices.Value(v1);
const gp_XYZ& coordsV2 = vertices.Value(v2);
const gp_XYZ& coordsV3 = vertices.Value(v3);
triangle->v1.x = float(coordsV1.X());
triangle->v2.x = float(coordsV2.X());
triangle->v3.x = float(coordsV3.X());
2014-01-27 22:03:50 +08:00
triangle->v1.y = float(coordsV1.Y());
triangle->v2.y = float(coordsV2.Y());
triangle->v3.y = float(coordsV3.Y());
2014-01-27 22:03:50 +08:00
triangle->v1.z = float(coordsV1.Z());
triangle->v2.z = float(coordsV2.Z());
triangle->v3.z = float(coordsV3.Z());
}
2014-01-29 18:33:55 +08:00
static void occmesh_stlb_add_triangle(void* cookie,
uint32_t tri_id,
const foug_stl_triangle_t* triangle,
uint16_t /*attr_byte_count*/)
{
2014-01-29 18:33:55 +08:00
occmesh_add_triangle(cookie, tri_id, triangle);
}
2014-01-27 22:03:50 +08:00
} // namespace internal
void foug_stla_geom_input_set_occmesh(foug_stla_geom_input_t* input, const Handle_StlMesh_Mesh &mesh)
{
2014-01-27 22:03:50 +08:00
input->cookie = internal::occMeshPtr(mesh);
2014-01-29 18:33:55 +08:00
memset(input, 0, sizeof(foug_stla_geom_input_t));
input->process_triangle_func = internal::occmesh_add_triangle;
}
2014-01-27 22:03:50 +08:00
void foug_stla_geom_output_set_occmesh(foug_stla_geom_output_t *output,
const foug_OccStlMeshDomain &meshCookie)
{
2014-01-27 22:03:50 +08:00
output->cookie = &meshCookie;
2014-01-29 18:33:55 +08:00
memset(output, 0, sizeof(foug_stla_geom_output_t));
2014-01-27 22:03:50 +08:00
output->triangle_count = meshCookie.mesh->NbTriangles(meshCookie.domainId);
output->get_triangle_func = internal::occmesh_get_triangle;
}
2014-01-27 22:03:50 +08:00
void foug_stlb_geom_input_set_occmesh(foug_stlb_geom_input_t* input, const Handle_StlMesh_Mesh &mesh)
{
2014-01-27 22:03:50 +08:00
input->cookie = internal::occMeshPtr(mesh);
2014-01-29 18:33:55 +08:00
memset(input, 0, sizeof(foug_stlb_geom_input_t));
input->process_triangle_func = internal::occmesh_stlb_add_triangle;
}
2014-01-27 22:03:50 +08:00
void foug_stlb_geom_output_set_occmesh(foug_stlb_geom_output_t* output,
const foug_OccStlMeshDomain &meshCookie)
{
2014-01-27 22:03:50 +08:00
static const char occMeshBinaryHeader[] = "Generated by libfougdatax, occmesh geometry";
output->cookie = &meshCookie;
output->header = reinterpret_cast<const uint8_t*>(occMeshBinaryHeader);
output->triangle_count = meshCookie.mesh->NbTriangles(meshCookie.domainId);
output->get_triangle_func = internal::occmesh_get_triangle;
}
2014-01-27 22:03:50 +08:00
foug_OccStlMeshDomain::foug_OccStlMeshDomain(const Handle_StlMesh_Mesh &stlMesh, int stlDomainId)
: mesh(stlMesh),
domainId(stlDomainId)
{
}