feat: add polygon area calculate

This commit is contained in:
kradchen
2025-06-12 13:50:19 +08:00
parent cb7d4db7ef
commit 2eb6b1bb6b
2 changed files with 51 additions and 0 deletions

View File

@@ -8,6 +8,8 @@
#include <vtkActor2D.h> #include <vtkActor2D.h>
#include <vtkTextMapper.h> #include <vtkTextMapper.h>
#include <vtkTextProperty.h> #include <vtkTextProperty.h>
#include <vtkPolygon.h>
#include <vtkLine.h>
#include <QFile> #include <QFile>
#include <QCoreApplication> #include <QCoreApplication>
@@ -155,6 +157,7 @@ void OpenPolyAnnotationActor::controlPointCb(vtkObject *sender, unsigned long ev
double result[3] = {0, 0, 0}; double result[3] = {0, 0, 0};
MapScreenPointToWorld(pos[0], pos[1], this->Renderer, result); MapScreenPointToWorld(pos[0], pos[1], this->Renderer, result);
BaseDataPoints->SetPoint(index, result); BaseDataPoints->SetPoint(index, result);
UpdatePerimeterAndAreaText();
} }
void OpenPolyAnnotationActor::Highlight(int f) { void OpenPolyAnnotationActor::Highlight(int f) {
@@ -165,9 +168,55 @@ void OpenPolyAnnotationActor::Highlight(int f) {
} }
bool OpenPolyAnnotationActor::onMeasureDoubleClick(vtkRenderWindowInteractor *) { bool OpenPolyAnnotationActor::onMeasureDoubleClick(vtkRenderWindowInteractor *) {
UpdatePerimeterAndAreaText();
return false; return false;
} }
void OpenPolyAnnotationActor::UpdatePerimeterAndAreaText()
{
if (text)
{
double *rp = renderPoints->GetPoint(0);
text->SetDisplayPosition(rp[0] + 10, rp[1] - 20);
double distance = 0.0;
for (vtkIdType i = 1; i < BaseDataPoints->GetNumberOfPoints(); ++i)
{
double p1[3] = {0.};
double p2[3] = {0.};
BaseDataPoints->GetPoint(i, p1);
BaseDataPoints->GetPoint(i - 1, p2);
distance += vtkMath::Distance2BetweenPoints(p1, p2);
}
if(!Closed)
{
bool unitFlag = distance>100;
QString textValue = QString("%1: %2 %3").arg(mDistanceName)
.arg(unitFlag?distance/10:distance,0,'f',2).arg(unitFlag?mUnitcm:mUnitmm);
vtkTextMapper::SafeDownCast(text->GetMapper())->SetInput(textValue.toUtf8().constData());
text->SetVisibility(1);
measured = true;
}
else{
vtkNew<vtkPolygon> polygon;
polygon->Initialize(BaseDataPoints->GetNumberOfPoints(), BaseDataPoints);
double area = polygon->ComputeArea();
bool unitFlag1 = distance>100;
bool unitFlag2 = area>1000;
QString textValue = QString("%1: %2 %3, %4: %5 %6").arg(mDistanceName)
.arg(unitFlag1?distance/10:distance,0,'f',2).arg(unitFlag1?mUnitcm:mUnitmm)
.arg(mAreaName)
.arg(unitFlag2?area/100:area,0,'f',2).arg(unitFlag2?mUnitcm2:mUnitmm2);
vtkTextMapper::SafeDownCast(text->GetMapper())->SetInput(textValue.toUtf8().constData());
text->SetVisibility(1);
measured = true;
}
}
}
bool OpenPolyAnnotationActor::Valid() { bool OpenPolyAnnotationActor::Valid() {
return Closed == 0 ? BaseDataPoints->GetNumberOfPoints() > 1 : BaseDataPoints->GetNumberOfPoints() > 2; return Closed == 0 ? BaseDataPoints->GetNumberOfPoints() > 1 : BaseDataPoints->GetNumberOfPoints() > 2;
} }

View File

@@ -30,6 +30,8 @@ public:
bool onMeasureDoubleClick(vtkRenderWindowInteractor *) override; bool onMeasureDoubleClick(vtkRenderWindowInteractor *) override;
void UpdatePerimeterAndAreaText();
bool onMeasureLeftButtonDown(vtkRenderWindowInteractor *) override; bool onMeasureLeftButtonDown(vtkRenderWindowInteractor *) override;
void onMeasureMouseMove(vtkRenderWindowInteractor *) override; void onMeasureMouseMove(vtkRenderWindowInteractor *) override;