代码改造了vtkAffineReprentation2D, 使其在2d平面上可以画圆,和矩形。vtkAffineReprentation2D虽然也能画圆和矩形,但是不能放大缩小,也不能移动。本人改造了vtk代码使其可以放大缩小,更可以移动。
关键代码如下:
圆:
void vtkEllipseRepresent2D::BuildRepresentation()
{
if (this->GetMTime() > this->BuildTime ||
(this->Renderer && this->Renderer->GetVTKWindow() &&
this->Renderer->GetVTKWindow()->GetMTime() > this->BuildTime))
{
vtkInteractorObserver::ComputeWorldToDisplay(
this->Renderer, this->Origin[0], this->Origin[1], this->Origin[2], this->DisplayOrigin);
int* size = this->Renderer->GetRenderWindow()->GetSize();
this->CurrentWidth = this->CircleWidth;
this->CurrentWidth /= 2.0;
double p1[3], p2[3], p3[3], p4[3];
p1[0] = this->DisplayOrigin[0] - this->CurrentWidth;
p1[1] = this->DisplayOrigin[1] - this->CurrentWidth;
p1[2] = 0.0;
p2[0] = this->DisplayOrigin[0] + this->CurrentWidth;
p2[1] = this->DisplayOrigin[1] - this->CurrentWidth;
p2[2] = 0.0;
p3[0] = this->DisplayOrigin[0] + this->CurrentWidth;
p3[1] = this->DisplayOrigin[1] + this->CurrentWidth;
p3[2] = 0.0;
p4[0] = this->DisplayOrigin[0] - this->CurrentWidth;
p4[1] = this->DisplayOrigin[1] + this->CurrentWidth;
p4[2] = 0.0;
this->HBoxPoints->SetPoint(0, p1);
this->HBoxPoints->SetPoint(1, p2);
this->HBoxPoints->SetPoint(2, p3);
this->HBoxPoints->SetPoint(3, p4);
this->HBoxPoints->Modified();
// draw the circle
int i;
double theta, delTheta = 2.0 * vtkMath::Pi() / VTK_CIRCLE_RESOLUTION;
this->CurrentRadius = this->CurrentWidth ;//* 0.75
this->CircleCellArray->Reset();
this->CirclePoints->Reset();
this->CirclePoints->SetNumberOfPoints(VTK_CIRCLE_RESOLUTION);
//this->CircleCellArray->AllocateEstimate(1, VTK_CIRCLE_RESOLUTION + 1);
this->CircleCellArray->InsertNextCell(VTK_CIRCLE_RESOLUTION + 1);
for (i = 0; i < VTK_CIRCLE_RESOLUTION; i++)
{
theta = i * delTheta;
p1[0] = this->DisplayOrigin[0] + this->CurrentRadius * cos(theta);
p1[1] = this->DisplayOrigin[1] + this->CurrentRadius * sin(theta);
this->CirclePoints->SetPoint(i, p1);
this->CircleCellArray->InsertCellPoint(i);
}
this->CircleCellArray->InsertCellPoint(0);
this->CircleCellArray->Modified();
if (this->DisplayText)
{
char str[256];
int maxValue, minValue;
srand((unsigned)time(NULL));
for (int i = 0; i < 10; i++)
{
maxValue += rand();
minValue = rand();
}
maxValue = maxValue % 255;
minValue = minValue % 50;
if (maxValue < minValue)
maxValue += 180;
snprintf(str, sizeof(str), "max:%d\n, mean:%d\n, min:%d",
maxValue, (maxValue + minValue) / 2, minValue);
double eventPos[2] = { this->DisplayOrigin[0] + this->CurrentRadius ,this->DisplayOrigin[1] };
this->UpdateText(str, eventPos);
}
// draw the translation axes
this->CurrentAxesWidth = this->CurrentWidth * this->AxesWidth / this->CircleWidth;
p1[0] = this->DisplayOrigin[0] - this->CurrentAxesWidth;
p1[1] = this->DisplayOrigin[1];
this->XAxis->GetPositionCoordinate()->SetValue(p1);
p2[0] = this->DisplayOrigin[0] + this->CurrentAxesWidth;
p2[1] = this->DisplayOrigin[1];
this->XAxis->GetPosition2Coordinate()->SetValue(p2);
p1[0] = this->DisplayOrigin[0];
p1[1] = this->DisplayOrigin[1] - this->CurrentAxesWidth;
this->YAxis->GetPositionCoordinate()->SetValue(p1);
p2[0] = this->DisplayOrigin[0];
p2[1] = this->DisplayOrigin[1] + this->CurrentAxesWidth;
this->YAxis->GetPosition2Coordinate()->SetValue(p2);
this->BuildTime.Modified();
}
}