vtk范围映射 -1 ~ +1 映射到 0 ~ 255

来源: https://www.ilikebigbits.com/2015_02_05_remap.html

 

inline float remap(float x, float in_min, float in_max,
                   float out_min, float out_max)
{
    float t = (x - in_min) / (in_max - in_min);
    return lerp(out_min, out_max, t);
}

inline float remap_clamp(float x, float in_min, float in_max,
                         float out_min, float out_max)
{
    float t = (x - in_min) / (in_max - in_min);
    t = clamp(t, 0, 1);
    return lerp(out_min, out_max, t);
}

//示例:
normal_encoded = remap(normal, -1, +1, 0, 255);
alpha = remap_clamp(time, fade_start, fade_stop, 1, 0);
//vtk 也有类似的方法 ,将距离标量 转为颜色的图表的。
vtkNew<vtkDistancePolyDataFilter> distanceFilter;

  distanceFilter->SetInputConnection(0, clean1->GetOutputPort());
  distanceFilter->SetInputConnection(1, clean2->GetOutputPort());
  distanceFilter->Update();

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(distanceFilter->GetOutputPort());
  mapper->SetScalarRange(
      distanceFilter->GetOutput()->GetPointData()->GetScalars()->GetRange()[0],
      distanceFilter->GetOutput()->GetPointData()->GetScalars()->GetRange()[1]);


  vtkNew<vtkScalarBarActor> scalarBar;
  scalarBar->SetLookupTable(mapper->GetLookupTable());
  scalarBar->SetTitle("Distance");
  scalarBar->SetNumberOfLabels(4);
  scalarBar->UnconstrainedFontSizeOn();

 

//距离转换为 颜色 越近的距离越绿色,距离远的越红色

float maxDist = 0;
float minDist = FLT_MAX;
for ( auto scalar : scalars )
{
    if ( maxDist < scalar )
    {
        maxDist = scalar;
    }
    if ( minDist > scalar )
    {
        minDist = scalar;
    }
}

MR::SaveSettings settings;
MR::VertColors colors;
colors.resize( waxMesh.topology.getValidVerts().size());


auto lerpColor = [] ( const Color& start, const Color& end, float t )
{
    return Color{ static_cast< int >( start.r + t * ( end.r - start.r ) ),
             static_cast< int >( start.g + t * ( end.g - start.g ) ),
             static_cast< int >( start.b + t * ( end.b - start.b ) ) };
};

auto distanceToColor = [=] ( float distance, float minDistance, float maxDistance )
{
    // 规范化距离
    float t = ( distance - minDistance ) / ( maxDistance - minDistance );
    // 限制 t 在 [0, 1] 范围内
    t = std::clamp( t, 0.0f, 1.0f );

    // 从绿色到红色
    Color green = { 0, 255, 0 }; // 绿色
    Color red = { 255, 0, 0 };   // 红色

    return lerpColor( green, red, t );
};


VertId id(0);
for ( auto scalar : scalars )
{
    colors[id++] = distanceToColor( scalar, minDist, maxDist );
}

 

posted @ 2024-08-19 09:59  lma_o  阅读(10)  评论(0编辑  收藏  举报