10 #include <vtkActor2D.h>
12 #include <vtkObjectFactory.h>
13 #include <vtkPoints.h>
14 #include <vtkPolyData.h>
15 #include <vtkRenderer.h>
16 #include <vtkTextActor.h>
17 #include <vtkTextProperty.h>
23 : LabelSuffix(nullptr), ShowLabel(1) {
28 vtkTextProperty* textProp = this->LabelActor->GetTextProperty();
29 textProp->SetFontSize(20);
31 textProp->SetColor(1.0, 1.0, 1.0);
32 textProp->SetShadow(1);
33 textProp->SetJustificationToCentered();
34 textProp->SetVerticalJustificationToCentered();
37 this->LabelActor->SetInput(
"Contour");
40 this->LabelActor->SetVisibility(1);
46 if (this->Renderer && this->LabelActor) {
47 this->Renderer->RemoveActor2D(this->LabelActor);
50 if (this->LabelSuffix) {
51 delete[] this->LabelSuffix;
52 this->LabelSuffix =
nullptr;
58 if (this->LabelSuffix) {
59 delete[] this->LabelSuffix;
60 this->LabelSuffix =
nullptr;
64 size_t len = strlen(suffix);
65 this->LabelSuffix =
new char[len + 1];
66 strcpy(this->LabelSuffix, suffix);
75 return this->LabelSuffix;
80 if (this->ShowLabel != show) {
81 this->ShowLabel = show;
82 if (this->LabelActor) {
83 this->LabelActor->SetVisibility(show && this->GetVisibility());
92 if (this->Renderer && this->LabelActor) {
93 this->Renderer->RemoveActor2D(this->LabelActor);
97 this->Superclass::SetRenderer(ren);
100 if (this->Renderer && this->LabelActor) {
101 this->Renderer->AddActor2D(this->LabelActor);
108 this->Superclass::SetVisibility(visible);
111 if (this->LabelActor) {
112 this->LabelActor->SetVisibility(visible && this->ShowLabel);
119 this->Superclass::BuildRepresentation();
127 if (!this->LabelActor) {
132 std::string labelText =
"Contour";
133 if (this->LabelSuffix) {
135 labelText += this->LabelSuffix;
137 this->LabelActor->SetInput(labelText.c_str());
140 int numNodes = this->GetNumberOfNodes();
141 if (numNodes > 0 && this->Renderer) {
142 double centroid[3] = {0.0, 0.0, 0.0};
145 for (
int i = 0; i < numNodes; ++i) {
147 this->GetNthNodeWorldPosition(i, pos);
148 centroid[0] += pos[0];
149 centroid[1] += pos[1];
150 centroid[2] += pos[2];
153 centroid[0] /= numNodes;
154 centroid[1] /= numNodes;
155 centroid[2] /= numNodes;
158 this->Renderer->SetWorldPoint(centroid[0], centroid[1], centroid[2],
160 this->Renderer->WorldToDisplay();
161 double* displayCoord = this->Renderer->GetDisplayPoint();
164 this->LabelActor->SetPosition(displayCoord[0], displayCoord[1] + 20);
167 this->LabelActor->SetPosition(50, 50);
Extended contour representation with instance label support.
const char * GetLabelSuffix() const
Get the current label suffix.
void SetShowLabel(int show)
Show or hide the instance label.
~cvConstrainedContourRepresentation() override
void SetRenderer(vtkRenderer *ren) override
Set the renderer (override to add label actor)
void UpdateLabel()
Update the label text and position.
void SetLabelSuffix(const char *suffix)
Set the label suffix to identify this contour instance.
void BuildRepresentation() override
Build the representation (override to update label position)
cvConstrainedContourRepresentation()
void SetVisibility(vtkTypeBool visible) override
Set visibility (override to control label visibility)
vtkStandardNewMacro(cvConstrainedContourRepresentation)