有C或C ++函数做"饱和度"上一个整数 [英] is there a function in C or C++ to do "saturation" on an integer

查看:248
本文介绍了有C或C ++函数做"饱和度"上一个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些3D图形和我有一个开放的海洋。对于这个海洋,我有一个矩阵重新presenting海州(即浪高)为海的特定矩形款。海洋的其余部分是平的。我的问题是,我的控制海,那里有海浪,被定位在开平板海中,在我的网格的边缘上的不连续性引起一些不良的工件。究其原因,我只产生波浪的款,而不是整个海是因为我的隔音功能是计算对整个海昂贵(我知道最简单的解决方法是使用像单纯的噪音更便宜的噪音的功能,但是这不是一个选项)。

说了这么多,我的问题是真的很简单。如果说我有尺寸100x40组成的网格(又名又名矩阵二维数组),我想找到120x33位置的值,我只是想借此近邻,这将是100x33。因此,对于任意数量的处于一个给定的范围之外,我希望该号码到饱和趴在给定范围内。有没有做这个C或C ++函数?

编辑:位置参数都是浮点型

我知道我可以用一些简单的if语句做到这一点,但它只是似乎喜欢的事,标准库将包括。


解决方案

 模板< typename的T>
ŧ饱和(T VAL,T分,T最大){
    返回的std ::分钟(的std :: MAX(VAL,分钟),最大值);
}

I am doing some 3D graphics and I have an open ocean. For this ocean, I have a matrix representing the sea state (i.e. wave heights) for a particular rectangular subsection of the sea. The rest of the ocean is flat. My problem is that my controlled sea, where there are waves, is positioned in the middle of open flat sea, and the discontinuity at the edges of my grid causes some bad artifacts. The reason I am only generating waves for a subsection and not the entire sea is because my noise function is prohibitively expensive to compute on the entire sea (and I know the easiest solution is to use a cheaper noise function like simplex noise, but that's not an option).

Having said that my question is really rather simple. If say I have a grid (aka matrix aka 2d array) of size 100x40, and I want to find the value for position 120x33, I simply want to take the nearest neighbour, which would be 100x33. So for any number that lies outside a given range, I want that number to saturate to lie within the given range. Is there a function in C or C++ that does this?

Edit: the position parameters are of type float

I know I can do this with some simple if statements, but it just seems like something that the standard libraries would include.

解决方案

template<typename T>
T saturate(T val, T min, T max) {
    return std::min(std::max(val, min), max);
}

这篇关于有C或C ++函数做&QUOT;饱和度&QUOT;上一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆