PD: simplify code to display add/sub shape

This commit is contained in:
wmayer
2021-10-09 11:38:25 +02:00
parent 66d3dd897f
commit f628050732

View File

@@ -116,9 +116,6 @@ void ViewProviderAddSub::updateAddSubShapeIndicator() {
return;
}
int numTriangles=0,numNodes=0,numNorms=0,numFaces=0;
std::set<int> faceEdges;
try {
// calculating the deflection value
Bnd_Box bounds;
@@ -126,16 +123,14 @@ void ViewProviderAddSub::updateAddSubShapeIndicator() {
bounds.SetGap(0.0);
Standard_Real xMin, yMin, zMin, xMax, yMax, zMax;
bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax);
Standard_Real deflection = ((xMax-xMin)+(yMax-yMin)+(zMax-zMin))/300.0 *
Deviation.getValue();
Standard_Real deflection = ((xMax-xMin)+(yMax-yMin)+(zMax-zMin))/300.0 * Deviation.getValue();
// create or use the mesh on the data structure
#if OCC_VERSION_HEX >= 0x060600
Standard_Real AngDeflectionRads = AngularDeflection.getValue() / 180.0 * M_PI;
BRepMesh_IncrementalMesh(cShape,deflection,Standard_False,
AngDeflectionRads,Standard_True);
BRepMesh_IncrementalMesh(cShape, deflection, Standard_False, AngDeflectionRads, Standard_True);
#else
BRepMesh_IncrementalMesh(cShape,deflection);
BRepMesh_IncrementalMesh(cShape, deflection);
#endif
// We must reset the location here because the transformation data
// are set in the placement property
@@ -143,6 +138,7 @@ void ViewProviderAddSub::updateAddSubShapeIndicator() {
cShape.Location(aLoc);
// count triangles and nodes in the mesh
int numTriangles=0,numNodes=0,numNorms=0,numFaces=0;
TopExp_Explorer Ex;
for (Ex.Init(cShape,TopAbs_FACE);Ex.More();Ex.Next()) {
Handle (Poly_Triangulation) mesh = BRep_Tool::Triangulation(TopoDS::Face(Ex.Current()), aLoc);
@@ -160,11 +156,12 @@ void ViewProviderAddSub::updateAddSubShapeIndicator() {
previewNorm ->vector .setNum(numNorms);
previewFaceSet ->coordIndex .setNum(numTriangles*4);
previewFaceSet ->partIndex .setNum(numFaces);
// get the raw memory for fast fill up
SbVec3f* verts = previewCoords ->point .startEditing();
SbVec3f* previewNorms = previewNorm ->vector .startEditing();
int32_t* index = previewFaceSet ->coordIndex .startEditing();
int32_t* parts = previewFaceSet ->partIndex .startEditing();
SbVec3f* verts = previewCoords ->point .startEditing();
SbVec3f* previewNorms = previewNorm ->vector .startEditing();
int32_t* index = previewFaceSet ->coordIndex .startEditing();
int32_t* parts = previewFaceSet ->partIndex .startEditing();
// preset the previewNormal vector with null vector
for (int i=0;i < numNorms;i++)
@@ -172,76 +169,46 @@ void ViewProviderAddSub::updateAddSubShapeIndicator() {
int ii = 0,faceNodeOffset=0,faceTriaOffset=0;
for (Ex.Init(cShape, TopAbs_FACE); Ex.More(); Ex.Next(),ii++) {
TopLoc_Location aLoc;
const TopoDS_Face &actFace = TopoDS::Face(Ex.Current());
// get the mesh of the shape
Handle (Poly_Triangulation) mesh = BRep_Tool::Triangulation(actFace,aLoc);
if (mesh.IsNull()) continue;
// getting the transformation of the shape/face
gp_Trsf myTransf;
Standard_Boolean identity = true;
if (!aLoc.IsIdentity()) {
identity = false;
myTransf = aLoc.Transformation();
}
TopLoc_Location loc;
Handle(Poly_Triangulation) mesh = BRep_Tool::Triangulation(actFace, loc);
if (mesh.IsNull())
continue;
// get triangulation
std::vector<gp_Pnt> points;
std::vector<Poly_Triangle> facets;
Part::Tools::getTriangulation(actFace, points, facets);
// get normal per vertex
std::vector<gp_Vec> vertexnormals;
Part::Tools::getPointNormals(actFace, mesh, vertexnormals);
Part::Tools::applyTransformationOnNormals(loc, vertexnormals);
// getting size of node and triangle array of this face
int nbNodesInFace = mesh->NbNodes();
int nbTriInFace = mesh->NbTriangles();
// check orientation
TopAbs_Orientation orient = actFace.Orientation();
std::size_t nbNodesInFace = points.size();
std::size_t nbTriInFace = facets.size();
for (std::size_t i = 0; i < points.size(); i++) {
verts[faceNodeOffset+i] = SbVec3f(points[i].X(), points[i].Y(), points[i].Z());
}
for (std::size_t i = 0; i < vertexnormals.size(); i++) {
previewNorms[faceNodeOffset+i] = SbVec3f(vertexnormals[i].X(), vertexnormals[i].Y(), vertexnormals[i].Z());
}
// cycling through the poly mesh
const Poly_Array1OfTriangle& Triangles = mesh->Triangles();
const TColgp_Array1OfPnt& Nodes = mesh->Nodes();
TColgp_Array1OfDir Normals (Nodes.Lower(), Nodes.Upper());
Part::Tools::getPointNormals(actFace, mesh, Normals);
for (int g=1;g<=nbTriInFace;g++) {
for (std::size_t g=0; g < nbTriInFace; g++) {
// Get the triangle
Standard_Integer N1,N2,N3;
Triangles(g).Get(N1,N2,N3);
// change orientation of the triangle if the face is reversed
if ( orient != TopAbs_FORWARD ) {
Standard_Integer tmp = N1;
N1 = N2;
N2 = tmp;
}
// get the 3 points of this triangle
gp_Pnt V1(Nodes(N1)), V2(Nodes(N2)), V3(Nodes(N3));
// get the 3 previewNormals of this triangle
gp_Dir NV1(Normals(N1)), NV2(Normals(N2)), NV3(Normals(N3));
// transform the vertices and previewNormals to the place of the face
if(!identity) {
V1.Transform(myTransf);
V2.Transform(myTransf);
V3.Transform(myTransf);
NV1.Transform(myTransf);
NV2.Transform(myTransf);
NV3.Transform(myTransf);
}
// add the previewNormals for all points of this triangle
previewNorms[faceNodeOffset+N1-1] += SbVec3f(NV1.X(),NV1.Y(),NV1.Z());
previewNorms[faceNodeOffset+N2-1] += SbVec3f(NV2.X(),NV2.Y(),NV2.Z());
previewNorms[faceNodeOffset+N3-1] += SbVec3f(NV3.X(),NV3.Y(),NV3.Z());
// set the vertices
verts[faceNodeOffset+N1-1].setValue((float)(V1.X()),(float)(V1.Y()),(float)(V1.Z()));
verts[faceNodeOffset+N2-1].setValue((float)(V2.X()),(float)(V2.Y()),(float)(V2.Z()));
verts[faceNodeOffset+N3-1].setValue((float)(V3.X()),(float)(V3.Y()),(float)(V3.Z()));
facets[g].Get(N1,N2,N3);
// set the index vector with the 3 point indexes and the end delimiter
index[faceTriaOffset*4+4*(g-1)] = faceNodeOffset+N1-1;
index[faceTriaOffset*4+4*(g-1)+1] = faceNodeOffset+N2-1;
index[faceTriaOffset*4+4*(g-1)+2] = faceNodeOffset+N3-1;
index[faceTriaOffset*4+4*(g-1)+3] = SO_END_FACE_INDEX;
index[faceTriaOffset*4+4*g] = faceNodeOffset+N1;
index[faceTriaOffset*4+4*g+1] = faceNodeOffset+N2;
index[faceTriaOffset*4+4*g+2] = faceNodeOffset+N3;
index[faceTriaOffset*4+4*g+3] = SO_END_FACE_INDEX;
}
parts[ii] = nbTriInFace; // new part