背景:
有时需要在屏幕上绘制一个像素框,并拾取框框内的图元或者vtkCell, 然后翻阅了VTK源码,做了摘抄并且提取了关键绘制代码
结果以及关键代码:
{
#if 0
//获取整个窗口的宽高。。。。 pixels 开辟的内存大小,是整个屏幕宽高的大小。 然后将 start-end 之间的矩形内容进行渲染....
if (!PixelArray) PixelArray = vtkUnsignedCharArray::New();
PixelArray->Initialize();
PixelArray->SetNumberOfComponents(4);
int* size = Interactor->GetRenderWindow()->GetSize();
PixelArray->SetNumberOfTuples(size[0] * size[1]);
//GetRGBACharPixelData 获取该像素范围内的RGBA值?
int StartPosition[2]{ 0,0 };
//int EndPosition[2]{ size[0]*0.7, size[1] * 0.7 };
int EndPosition[2]{ 150,120 };
vtkUnsignedCharArray* tmpPixelArray = vtkUnsignedCharArray::New();
tmpPixelArray->DeepCopy(PixelArray);
unsigned char* pixels = tmpPixelArray->GetPointer(0);
int min[2], max[2];
min[0] = StartPosition[0] <= EndPosition[0] ?
StartPosition[0] : EndPosition[0];
if (min[0] < 0) { min[0] = 0; }
if (min[0] >= size[0]) { min[0] = size[0] - 1; }
min[1] = StartPosition[1] <= EndPosition[1] ?
StartPosition[1] : EndPosition[1];
if (min[1] < 0) { min[1] = 0; }
if (min[1] >= size[1]) { min[1] = size[1] - 1; }
max[0] = EndPosition[0] > StartPosition[0] ?
EndPosition[0] : StartPosition[0];
if (max[0] < 0) { max[0] = 0; }
if (max[0] >= size[0]) { max[0] = size[0] - 1; }
max[1] = EndPosition[1] > StartPosition[1] ?
EndPosition[1] : StartPosition[1];
if (max[1] < 0) { max[1] = 0; }
if (max[1] >= size[1]) { max[1] = size[1] - 1; }
int i;
//防止数据超过255 0 200 两个循环,刚好给 RGBA 赋值..... 这里是绘制了四条边
int centY = (min[0] + max[0]) / 2;
for (i = min[0]; i <= max[0]; i++)
{
//下边
pixels[4 * (min[1] * size[0] + i)] = 255 ^ pixels[4 * (min[1] * size[0] + i)];
pixels[4 * (min[1] * size[0] + i) + 1] = 1 ^ pixels[4 * (min[1] * size[0] + i) + 1];
pixels[4 * (min[1] * size[0] + i) + 2] = 1 ^ pixels[4 * (min[1] * size[0] + i) + 2];
//中间一条直线.
pixels[4 * (centY * size[0]) + i] = 255 ^ pixels[4 * (centY * size[0]) + i];
pixels[4 * (centY * size[0] + i) + 1] = 255 ^ pixels[4 * (centY * size[0] + i) + 1];
pixels[4 * (centY * size[0] + i) + 2] = 255 ^ pixels[4 * (centY * size[0] + i) + 2];
//上边
pixels[4 * (max[1] * size[0] + i)] = 1 ^ pixels[4 * (max[1] * size[0] + i)];
pixels[4 * (max[1] * size[0] + i) + 1] = 255 ^ pixels[4 * (max[1] * size[0] + i) + 1];
pixels[4 * (max[1] * size[0] + i) + 2] = 1 ^ pixels[4 * (max[1] * size[0] + i) + 2];
}
std::cout << "x i: " << i << endl;
for (i = min[1] + 1; i < max[1]; i++)
{
pixels[4 * (i * size[0] + min[0])] = 1 ^ pixels[4 * (i * size[0] + min[0])];
pixels[4 * (i * size[0] + min[0]) + 1] = 1 ^ pixels[4 * (i * size[0] + min[0]) + 1];
pixels[4 * (i * size[0] + min[0]) + 2] = 255 ^ pixels[4 * (i * size[0] + min[0]) + 2];
pixels[4 * (i * size[0] + max[0])] = 255 ^ pixels[4 * (i * size[0] + max[0])];
pixels[4 * (i * size[0] + max[0]) + 1] = 1 ^ pixels[4 * (i * size[0] + max[0]) + 1];
pixels[4 * (i * size[0] + max[0]) + 2] = 255 ^ pixels[4 * (i * size[0] + max[0]) + 2];
}
std::cout << "y i: " << i << " " << min[0] << " " << min[1] << " " << max[0] << " " << max[1] << endl;
Interactor->GetRenderWindow()->SetRGBACharPixelData(0, 0, size[0] - 1, size[1] - 1, pixels, 0);
Interactor->GetRenderWindow()->Frame();
#elif 0
/// <summary>
/// 这里就这样吧。。不就结了..
/// </summary>
if (!PixelArray) PixelArray = vtkUnsignedCharArray::New();
int w = 150;
int h = 120;
auto length = [=]() {
return w * h;
};
int length2 = w * h;
int componets = 4;
PixelArray->SetNumberOfTuples(length2);
PixelArray->SetNumberOfComponents(componets);
vtkUnsignedCharArray* tmpPixelArray = vtkUnsignedCharArray::New();
tmpPixelArray->DeepCopy(PixelArray);
unsigned char* pixels = tmpPixelArray->GetPointer(0);
//赋值给数组pixels
int controls = length2;
//std::cout << "总大小: " << sizeof(pixels)/ sizeof(char) << endl;
std::cout << "验证了Pixels大小是获取正确的..." << "controls: " << controls << " tmpPixelArray: " << tmpPixelArray->GetNumberOfTuples() << endl;
//正确的找到 X Y 的坐标数值.
/*int stripStep = w ;
for (size_t i = 0; i < controls - stripStep; i+=stripStep)
{
for (size_t j = 0; j < stripStep-4; j += 4)
{
pixels[i+j] = 255;
pixels[i+j + 1] = 255;
pixels[i+j + 2] = 0;
}
}*/
/*for (size_t i = 0; i < controls; i += 4)
{
pixels[i ] = 255;
pixels[i + 1] = 0;
pixels[i + 2] = 0;
pixels[i+4] = 255;
pixels[i+4 + 1] = 0;
pixels[i+4 + 2] = 0;
}*/
for (size_t i = 0; i < w; i++)
{
pixels[i] = 255;
pixels[i + 1] = 0;
pixels[i + 2] = 0;
//第二行而已...
pixels[i * w] = 255;
pixels[i * w + 1] = 0;
pixels[i * w + 2] = 0;
pixels[i * w * 2] = 255;
pixels[i * w * 2 + 1] = 0;
pixels[i * w * 2 + 2] = 0;
/* pixels[i * w] = 255;
pixels[i * w + 1] = 0;
pixels[i * w + 2] = 0;*/
}
//或者这个.
Interactor->GetRenderWindow()->SetRGBACharPixelData(0, 0, w - 1, h - 1, pixels, 0);//注意这里的RGBA
Interactor->GetRenderWindow()->Frame();
delete pixels;
pixels = nullptr;
#endif // 0
}
我们可以实现的功能效果: 在屏幕上绘制任意的闭合多边形,并拾取内部的图元
版权声明:本文为sinat_36564005原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。