【VTKExamples::Visualization】第二期 TestAssignCellColorsFromLUT

本文主要是介绍【VTKExamples::Visualization】第二期 TestAssignCellColorsFromLUT,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

很高兴在雪易的CSDN遇见你 

VTK技术爱好者 QQ:870202403


前言

本文分享vtkPolyData渲染时如何为每个Cell指定一个颜色,希望对各位小伙伴有所帮助!

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的点赞就是我的动力(^U^)ノ~YO


1. 指定Cell颜色

void ztInteractionApplication::MakeLUT(size_t const& tableSize, vtkLookupTable* lut)
{vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();lut->SetNumberOfTableValues(static_cast<vtkIdType>(tableSize));lut->Build();// Fill in a few known colors, the rest will be generated if neededlut->SetTableValue(0, nc->GetColor4d("Black").GetData());lut->SetTableValue(1, nc->GetColor4d("Banana").GetData());lut->SetTableValue(2, nc->GetColor4d("Tomato").GetData());lut->SetTableValue(3, nc->GetColor4d("Wheat").GetData());lut->SetTableValue(4, nc->GetColor4d("Lavender").GetData());lut->SetTableValue(5, nc->GetColor4d("Flesh").GetData());lut->SetTableValue(6, nc->GetColor4d("Raspberry").GetData());lut->SetTableValue(7, nc->GetColor4d("Salmon").GetData());lut->SetTableValue(8, nc->GetColor4d("Mint").GetData());lut->SetTableValue(9, nc->GetColor4d("Peacock").GetData());
}//! Use a color transfer Function to generate the colors in the lookup table.
void ztInteractionApplication::MakeLUTFromCTF(size_t const& tableSize, vtkLookupTable* lut)
{vtkSmartPointer<vtkColorTransferFunction> ctf =vtkSmartPointer<vtkColorTransferFunction>::New();ctf->SetColorSpaceToDiverging();// Green to tan.ctf->AddRGBPoint(0.0, 0.085, 0.532, 0.201);ctf->AddRGBPoint(0.5, 0.865, 0.865, 0.865);ctf->AddRGBPoint(1.0, 0.677, 0.492, 0.093);lut->SetNumberOfTableValues(static_cast<vtkIdType>(tableSize));lut->Build();for (size_t i = 0; i < tableSize; ++i){double* rgb;rgb = ctf->GetColor(static_cast<double>(i) / tableSize);lut->SetTableValue(static_cast<vtkIdType>(i), rgb);}
}//! Create the cell data using the colors from the lookup table.
void ztInteractionApplication::MakeCellData(size_t const& tableSize, vtkLookupTable* lut, vtkUnsignedCharArray* colors)
{for (size_t i = 1; i < tableSize; i++){double rgb[3];unsigned char ucrgb[3];// Get the interpolated color.// Of course you can use any function whose range is [0...1]// to get the required color and assign it to a cell Id.// In this case we are just using the cell (Id + 1)/(tableSize - 1)// to get the interpolated color.lut->GetColor(static_cast<double>(i) / (tableSize - 1), rgb);for (size_t j = 0; j < 3; ++j){ucrgb[j] = static_cast<unsigned char>(rgb[j] * 255);}colors->InsertNextTuple3(ucrgb[0], ucrgb[1], ucrgb[2]);// Print out what we have.std::cout << "(";PrintColour<double[3]>(rgb);std::cout << ") (";PrintColour<unsigned char[3]>(ucrgb);std::cout << ")" << std::endl;}
}template <typename T>
void ztInteractionApplication::PrintColour(T& rgb)
{// Don't do this in real code! Range checking etc. is needed.for (size_t i = 0; i < 3; ++i){if (i < 2){std::cout << static_cast<double>(rgb[i]) << " ";}else{std::cout << static_cast<double>(rgb[i]);}}
}void ztInteractionApplication::on_assignCellColorsFromLUTAct_triggered()
{vtkSmartPointer<vtkNamedColors> nc = vtkSmartPointer<vtkNamedColors>::New();// Provide some geometryint resolution = 3;vtkSmartPointer<vtkPlaneSource> plane11 =vtkSmartPointer<vtkPlaneSource>::New();plane11->SetXResolution(resolution);plane11->SetYResolution(resolution);vtkSmartPointer<vtkPlaneSource> plane12 =vtkSmartPointer<vtkPlaneSource>::New();plane12->SetXResolution(resolution);plane12->SetYResolution(resolution);// Create a lookup table to map cell data to colorsvtkSmartPointer<vtkLookupTable> lut1 = vtkSmartPointer<vtkLookupTable>::New();vtkSmartPointer<vtkLookupTable> lut2 = vtkSmartPointer<vtkLookupTable>::New();int tableSize = std::max(resolution * resolution + 1, 10);// Force an update so we can set cell dataplane11->Update();plane12->Update();MakeLUT(tableSize, lut1);MakeLUTFromCTF(tableSize, lut2);vtkSmartPointer<vtkUnsignedCharArray> colorData1 =vtkSmartPointer<vtkUnsignedCharArray>::New();colorData1->SetName("colors"); // Any name will work here.colorData1->SetNumberOfComponents(3);std::cout << "Using a lookup table from a set of named colors." << std::endl;MakeCellData(tableSize, lut1, colorData1);// Then use SetScalars() to add it to the vtkPolyData structure,// this will then be interpreted as a color table.plane11->GetOutput()->GetCellData()->SetScalars(colorData1);vtkSmartPointer<vtkUnsignedCharArray> colorData2 =vtkSmartPointer<vtkUnsignedCharArray>::New();colorData2->SetName("colors");colorData2->SetNumberOfComponents(3);std::cout << "Using a lookup table created from a color transfer function."<< std::endl;MakeCellData(tableSize, lut2, colorData2);plane12->GetOutput()->GetCellData()->SetScalars(colorData2);// Setup actor and mappervtkSmartPointer<vtkPolyDataMapper> mapper11 =vtkSmartPointer<vtkPolyDataMapper>::New();mapper11->SetInputConnection(plane11->GetOutputPort());// Now, instead of doing this:// mapper11->SetScalarRange(0, tableSize - 1);// mapper11->SetLookupTable(lut1);// We can just use the color data that we created from the lookup table and// assigned to the cells:mapper11->SetScalarModeToUseCellData();mapper11->Update();vtkSmartPointer<vtkPolyDataMapper> mapper12 =vtkSmartPointer<vtkPolyDataMapper>::New();mapper12->SetInputConnection(plane12->GetOutputPort());mapper12->SetScalarModeToUseCellData();mapper12->Update();vtkSmartPointer<vtkXMLPolyDataWriter> writer =vtkSmartPointer<vtkXMLPolyDataWriter>::New();writer->SetFileName("pdlut.vtp");writer->SetInputData(mapper11->GetInput());// This is set so we can see the data in a text editor.writer->SetDataModeToAscii();writer->Write();writer->SetFileName("pdctf.vtp");writer->SetInputData(mapper12->GetInput());writer->Write();vtkSmartPointer<vtkActor> actor11 = vtkSmartPointer<vtkActor>::New();actor11->SetMapper(mapper11);vtkSmartPointer<vtkActor> actor12 = vtkSmartPointer<vtkActor>::New();actor12->SetMapper(mapper12);// Let's read in the data we wrote out.vtkSmartPointer<vtkXMLPolyDataReader> reader1 =vtkSmartPointer<vtkXMLPolyDataReader>::New();reader1->SetFileName("pdlut.vtp");vtkSmartPointer<vtkXMLPolyDataReader> reader2 =vtkSmartPointer<vtkXMLPolyDataReader>::New();reader2->SetFileName("pdctf.vtp");vtkSmartPointer<vtkPolyDataMapper> mapper21 =vtkSmartPointer<vtkPolyDataMapper>::New();mapper21->SetInputConnection(reader1->GetOutputPort());mapper21->SetScalarModeToUseCellData();mapper21->Update();vtkSmartPointer<vtkActor> actor21 = vtkSmartPointer<vtkActor>::New();actor21->SetMapper(mapper11);vtkSmartPointer<vtkPolyDataMapper> mapper22 =vtkSmartPointer<vtkPolyDataMapper>::New();mapper22->SetInputConnection(reader2->GetOutputPort());mapper22->SetScalarModeToUseCellData();mapper22->Update();vtkSmartPointer<vtkActor> actor22 = vtkSmartPointer<vtkActor>::New();actor22->SetMapper(mapper22);// Define viewport ranges.// (xmin, ymin, xmax, ymax)double viewport11[4] = { 0.0, 0.0, 0.5, 0.5 };double viewport12[4] = { 0.0, 0.5, 0.5, 1.0 };double viewport21[4] = { 0.5, 0.0, 1.0, 0.5 };double viewport22[4] = { 0.5, 0.5, 1.0, 1.0 };// Set up the renderers.vtkSmartPointer<vtkRenderer> ren11 = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderer> ren12 = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderer> ren21 = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderer> ren22 = vtkSmartPointer<vtkRenderer>::New();// Setup the render windowsvtkSmartPointer<vtkRenderWindow> renWin =vtkSmartPointer<vtkRenderWindow>::New();renWin->SetSize(600, 600);renWin->AddRenderer(ren11);renWin->AddRenderer(ren12);renWin->AddRenderer(ren21);renWin->AddRenderer(ren22);ren11->SetViewport(viewport11);ren12->SetViewport(viewport12);ren21->SetViewport(viewport21);ren22->SetViewport(viewport22);ren11->SetBackground(nc->GetColor3d("MidnightBlue").GetData());ren12->SetBackground(nc->GetColor3d("MidnightBlue").GetData());ren21->SetBackground(nc->GetColor3d("MidnightBlue").GetData());ren22->SetBackground(nc->GetColor3d("MidnightBlue").GetData());ren11->AddActor(actor11);ren12->AddActor(actor12);ren21->AddActor(actor21);ren22->AddActor(actor22);vtkSmartPointer<vtkRenderWindowInteractor> iren =vtkSmartPointer<vtkRenderWindowInteractor>::New();iren->SetRenderWindow(renWin);renWin->Render();iren->Start();
}

结论:

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的赞赏是我的最最最最大的动力(^U^)ノ~YO

这篇关于【VTKExamples::Visualization】第二期 TestAssignCellColorsFromLUT的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/586099

相关文章

【ArcGIS Pro实操第二期】最小成本路径(Least-cost path)原理及实操案例

ArcGIS Pro实操第一期:最小成本路径原理及实操案例 概述(Creating the least-cost path)1.1 原理介绍1.2 实现步骤1.3 应用案例 2 GIS实操2.1 工具箱简介2.1.1 成本路径(Cost path)2.1.2 成本距离(Cost distance)2.1.2 路径距离(Path Distance) 2.2 案例: 参考 概述(Cre

各厂内推整理 | 第二期

点击上方“朱小厮的博客”,选择“设为星标” 从去年开始,整个互联网行业的态势就不容乐观,很多公司都停止了招聘甚至出现了大面积的裁员潮,找工作变得越来越困难。 皮皮应几位道友相邀,在几个月前建立了一个内推群,主要是想让大家可以互通信息,希望各位求职者在这个寒冬里能够找到满意的工作,也希望内推者可以找到合适的候选人。 内推群里已经发出过很多JD,不过群聊信息一多就被一一淹没了,很多求职者都

杭州城市开发者社区活动:《聚力AI,共创共荣》第二期线下AI论坛活动等你来!

由中也AI共荣社主办,COC杭州城市开发者社区等多家协办的《聚力AI,共创共荣》AI论坛活动来袭! 活动围绕AI技术的创新应用,以及在实际应用落地的场景为主题展开,聚力AI,共同探讨。有机会与行业大咖进行面对面交流分享和学习交流,有料有趣的探索之旅,你值得拥有。 目录 🚩主题📅时间📍 地点💐中也AI共荣社🌞活动嘉宾介绍李健朋 中也AI共荣社CMO中也资本投资/中也智能科技联

【论文阅读第二期】The Wisdom of the few

论文阅读第二期的文章《The Wisdom of the few》讲的是基于专家观点的协同过滤推荐算法,是一篇2009年的文章。作者Xavier Amatriain是推荐系统领域的一位大牛,最早主导了Netflix的推荐系统,后来去了Quora,现在自己创办公司Curai研究AI。因为本人对推荐系统了解比较少,所以只是略读了文章,做一个简单的总结:1. 文章摘要;2.什么是协同过滤 1. 文章摘

书生·浦语大模型实战营第二期作业六

1、安装环境: 2、安装legent和agentlego: 3、部署apiserver: 4、legent web demo: 5、没搜到,很尴尬: 6、自定义工具: 7、智能体“乐高”: 8、智能体工具,识别图片: 9、自定义工具:

【InternLM实战营第二期笔记】06:Lagent AgentLego 智能体应用搭建

文章目录 讲解为什么要有智能体什么是 Agent智能体的组成智能体框架AutoGPTReWooReAct Lagent & Agent LegoAgentLego 实操Lagent Web Demo自定义工具 AgentLego:组装智能体“乐高”直接使用作为智能体,WebUI文生图测试 Agent 工具能力微调 讲解 为什么要有智能体 什么是 Agent 智能

【InternLM实战营第二期笔记】07:OpenCompass :是骡子是马,拉出来溜溜

文章目录 课程实操 课程 评测的意义是什么呢?我最近也在想。看到这节开头的内容后忽然有个顿悟:如果大模型最终也会变成一种基础工具(类比软件),稳定或可预期的效果需要先于用户感知构建出来,评测 case 就需要变成用例的相对充分抽样。 除了提高效率本身,最近还有一个很好的工作 MixEval,把标准、静态的 benchmarks 跟 elo 表现做了充分关联,使得只测试少量

大数据面试题第二期*6

题1、Namenode挂了怎么办? 方法一:将SecondaryNameNode中数据拷贝到namenode存储数据的目录。 方法二:使用importCheckpoint选项启动namenode守护进程,从而将SecondaryNameNode中数据拷贝到namenode目录中。 题2、Hadoop 的namenode 宕机怎么解决? 先分析宕机后的损失,宕机后直接导致client无

【WRF理论第二期】运行模型的基础知识

WRF理论第二期:运行模型的基础知识 1 Basics for Running the Model2 Geogrid程序2.1 Geogrid2.2 Terrestrial Input Data 3 Ungrid程序3.1 Ungrid3.2 Intermediate Files3.3 Required Fields 4 Metgrid程序参考 官方介绍-Basics for Ru

【WRF理论第二期】模型目录介绍

WRF理论第二期:模型目录介绍 1 WRF主目录2 WPS主目录3 编译后的可执行文件4 运行目录参考 了解 WRF 模型的目录结构有助于有效地管理和操作模型,从而确保模拟和分析工作的顺利进行。以下分解介绍WRF主目录、WPS主目录等。 Github-wrf-model/WRF 1 WRF主目录 安装 WRF 模型后,通常会在特定目录下形成一系列文件和子目录,这些目录结构帮