GenVect PerlinNoise2D::generate(size_t xn, size_t yn) { GenVect generation = GenVect(xn); GenVect result = GenVect(xn); for (size_t x = 0; x < xn; x++) { generation[x] = randomNoise(yn); result[x] = generation[x]; } for (size_t i = 0; i < depth; i++) { GenVect nextGen = GenVect(xn); for (size_t x = 0; x < xn; x++) { nextGen[x] = std::vector<float>(yn); for (size_t y = 0; y < yn; y++) { int x1 = _perlin_noise2d::floor(x / 2.0f); int x2 = _perlin_noise2d::ceil(x / 2.0f); int y1 = _perlin_noise2d::floor(y / 2.0f); int y2 = _perlin_noise2d::ceil(y / 2.0f); nextGen[x][y] = generation[x1][y1] + generation[x2][y2]; result[x][y] += nextGen[x][y]; } } generation = nextGen; } return result; }
int main(void) { cv :: Mat randomNoise(numTrainingPoints, numVariables, CV_32FC1 ); cv :: randu (randomNoise, 0., 1.) ; //the Mat file randomNoise is now filled with uniformly //distributed random numbers between 0 and 1. cv::imshow("random noise", randomNoise); cv::waitKey(0); }
void NoiseProcessor::perlinNoise(Image *img) { auto size = nextPow2(std::max(size_.get().x, size_.get().y)); std::vector<std::unique_ptr<Image>> levels; std::vector<TemplateImageSampler<float,float>> samplers; auto currentSize = std::pow(2, levels_.get().x); auto iterations = levels_.get().y - levels_.get().x + 1; float currentPersistance = 1; while (currentSize <= size && iterations--) { size2_t imgsize{static_cast<size_t>(currentSize)}; auto img1 = util::make_unique<Image>(imgsize, DataFLOAT32::get()); randomNoise(img1.get(), -currentPersistance, currentPersistance); samplers.push_back(TemplateImageSampler<float,float>(img1.get())); levels.push_back(std::move(img1)); currentSize *= 2; currentPersistance *= persistence_.get(); } auto data = static_cast<float *>( img->getColorLayer()->getEditableRepresentation<LayerRAM>()->getData()); float repri = 1.0 / size; // size_t index = 0; util::IndexMapper2D index(size_.get()); #pragma omp parallel for for (long long y = 0; y < size_.get().y; y++) { for (long long x = 0; x < size_.get().x; x++) { float v = 0; float X = x * repri; float Y = y * repri; for (auto &sampler : samplers) { v += sampler.sample(X, Y); } v = (v + 1.0f) / 2.0f; data[index(x, size_.get().y - 1 - y)] = glm::clamp(v, 0.0f, 1.0f); } } }