-
February 17th, 2015, 01:08 PM
#1
Converting JAVA for loop to C++ for loop
Hello
Two questions.
The second line m=field.length. What is the actual length?
The line n=field[j].length what is the second value?
I'm trying to convert some java code to c++.
Code:
if (field == null) field = new int[cwidth][cheight];
for (int j = 0, m = field.length; j < m; j++)
{
for (int k = 0, n = field[j].length; k < n; k++)
{
field[j][k] += (hashrandom(cx + j, cy + k, iteration) & (1 << (7 - iteration)));
}
}
I tried
Code:
/// Field
m=cwidth;
n=cheight;
if (field == NULL) field = new int[m*(n*m)];
for (int j = 0; j < m; j++)
{
for (int k = 0; k < n; k++)
{
field[j+(k*m)] += (hashrandom({cx + j, cy + k, iteration}) & (1 << (7 - iteration)));
}
}
but it does not work.
-
February 17th, 2015, 01:09 PM
#2
Re: Converting JAVA for loop to C++ for loop
I can give the full code if needed.
-
February 17th, 2015, 01:18 PM
#3
Re: Converting JAVA for loop to C++ for loop
Originally Posted by vivienneanthony
I can give the full code if needed.
Use cwidth to control the outer loop and cheight for the inner.
-
February 17th, 2015, 01:23 PM
#4
Re: Converting JAVA for loop to C++ for loop
Originally Posted by GCDEF
Use cwidth to control the outer loop and cheight for the inner.
The outer loop is controlled by j < m and m=cwidth;
Thats what you mean?
-
February 17th, 2015, 01:27 PM
#5
Re: Converting JAVA for loop to C++ for loop
Yeah. That should work. Sorry, didn't look closely. Your loop code looks okay, but field is a two dimensional array. You need to access it like one. The code in the first snippet should work.
field[j][k] += (hashrandom(cx + j, cy + k, iteration) & (1 << (7 - iteration)));
-
February 17th, 2015, 01:32 PM
#6
Re: Converting JAVA for loop to C++ for loop
Hmmm. Actually I made it one dimensional ...,....,... thats why i have j+(k*m)
The old code
http://pastebin.com/gh6P5zf3
My code is. I know its missing the memory deletion code.
http://pastebin.com/S037zuW0
-
February 17th, 2015, 01:34 PM
#7
Re: Converting JAVA for loop to C++ for loop
Sorry. I seem to be doing more harm than good. What do you mean by "doesn't work"?
It should work as a two dimensional array, but shouldn't the size be m*n?
-
February 17th, 2015, 01:46 PM
#8
Re: Converting JAVA for loop to C++ for loop
It should. I changed it. It was that code. I think I changed it to try to find out whats wrong with the code.
-
February 17th, 2015, 07:31 PM
#9
Re: Converting JAVA for loop to C++ for loop
Current not working full code.
This is the code so far I converted to C++ (Olsen 2D)
http://pastebin.com/qbgBL0Hq
This is the image produce
http://imgur.com/i6Lbr1P
Original source code
http://pastebin.com/gh6P5zf3
Demo Online(You should see what it should produce at 5 interations)
http://tatarize.nfshost.com/OlsenNoise.htm
-
February 18th, 2015, 03:13 AM
#10
Re: Converting JAVA for loop to C++ for loop
Originally Posted by vivienneanthony
I'm trying to convert some java code to c++.
Code:
if (field == null) field = new int[cwidth][cheight];
for (int j = 0, m = field.length; j < m; j++)
{
for (int k = 0, n = field[j].length; k < n; k++)
{
field[j][k] += (hashrandom(cx + j, cy + k, iteration) & (1 << (7 - iteration)));
}
}
Don't use new [] in C++, there is no need for it. Use std::vector instead.
And please post the code here, rather than providing some external link.
What do you mean by "doesn't work"?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
February 18th, 2015, 09:04 AM
#11
Re: Converting JAVA for loop to C++ for loop
Originally Posted by D_Drmmr
Don't use new [] in C++, there is no need for it. Use std::vector instead.
And please post the code here, rather than providing some external link.
What do you mean by "doesn't work"?
The noise isn't created correctly and it seems to be not shifting like it's jiggled.
Problems as also mentioned by the original author when my conversion code.
Tatarize said...
I can see some places where it bleeds and the numbers clearly are overflowing. Like the white goes from 0b11111111 to 0b100000000 which is basically 0b000000000 as far as the sample is concerned. And it really looks like half way down the damned thing loses a few bytes and suddenly it's filling in the scan lines with a bias. Like it's 1000x1000 for a while, then half way down it suddenly thinks it's only 993 stride and the pixel it takes to be 1 down from it is really 1 down and 7 to the left.
February 18, 2015 at 1:54:00 AM PST
Tatarize said...
Also, and less obviously, the top half has a patterned sweep. Subtly a few layers down even in the top half there's clearly a down right sweep. It's properly fractal noise. Which means there are no patterns. Any pattern is a bug.
Try jiggering with the number of iterations. It's recursive. So if you request 0 iterations it should give you real pure random noise. At 1 it should be a little less random but still very-all-over the place, and so on. Apparently some of it is working which obscures any good understanding of the bugs.
One thing about fractal noise is bugged fractal noise very often looks like fractal noise.
A image of the output is http://imgur.com/nb2pMjP
The code is
Code:
/*
* @author Tat
* c++ rewrite vivienne (WIP)
* verion .001
*/
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <png++/png.hpp>
using namespace std;
void SaveTerrFile(const int * image, int size, char * filename);
class OlsenNoise2D
{
public:
int * olsennoise(int x, int y, int width, int height);
private:
int hashrandom(std::vector<long long int> elements);
long long hash(long long v);
};
int * OlsenNoise2D::olsennoise(int x, int y, int width, int height)
{
int maxiterations =4;
int cx, cy;
int cxh, cyh;
int cwidth, cheight;
int xoff, yoff;
int nwidth, nheight;
int nx, ny;
int nxh, nyh;
int m=0;
int n=0;
int * field = NULL;
for (int iteration = 0; iteration < maxiterations; iteration++)
{
nx = x;
ny = y;
nxh = x + width;
nyh = y + width;
n = maxiterations - iteration;
for (int i = 1; i < n; i++)
{
nx = (nx / 2) - 1;
ny = (ny / 2) - 1;
nxh = 1 -(-nxh/2);
nyh = 1 -(-nyh/2);
}
xoff = -2*((nx/2)) + nx + 1;
yoff = -2*((ny/2)) + ny + 1;
cx = (nx / 2) - 1;
cy = (ny / 2) - 1;
cxh = 1 -(-nxh/2);
cyh = 1 -(-nyh/2);
nwidth = nxh - nx;
nheight = nyh - ny;
cwidth = cxh - cx;
cheight = cyh - cy;
/// Only happens once
if (field==NULL)
{
/// allocate memory
field = new int[height * width];
/// blank value
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
field[x+(y*width)]=0;
}
}
}
/// First loop
for (int j = 0, m=cwidth; j < m; j++)
{
for (int k = 0, n=cheight; k < n; k++)
{
field[j+(k*m)] += (hashrandom( {cx + j, ((cy + k)*m), iteration}) & (1 << (7 - iteration)));
}
}
/// Up sampled
int * upsampled = new int[(cwidth*2)*(cheight*2)];
long int upsampledsize=(cwidth*2)*(cheight*2);
for (int j = 0, m=cwidth*2; j < m; j++)
{
for (int k = 0,n=cheight*2; k < n; k++)
{
upsampled[j+(k*m)] = field[(j / 2)+((k / 2)*cwidth)];
}
}
memcpy((void *)field,(void *) upsampled,upsampledsize*sizeof(int));
delete upsampled;
/// Blur field
int * blurfield =new int[(cwidth-2)*(cheight-2)];
long int blurfieldsize = (cwidth-2)*(cheight-2);
for (int j = 0,m=cwidth-2; j < m; j++)
{
for (int k = 0, n=cheight-2; k < n; k++)
{
for (int h = 0; h < 9; h++)
{
//blurfield[j+(k*m)] += field[(j + (h % 3))+((k+(h/ 3))*(cwidth*2))];
blurfield[j+(k*m)] += field[(j + (h % 3))+((k+(h/ 3))*(cwidth*2))];
}
blurfield[j+(k*m)] /= 9;
}
}
memcpy((void *)field,(void *)blurfield,blurfieldsize*sizeof(int));
delete blurfield;
/// Trim field
int * trimfield = new int[nwidth*nheight];
long int trimfieldsize = nwidth*nheight;
for (int j = 0, m=nwidth; j < m; j++)
{
for (int k = 0, n=nheight; k < n; k++)
{
trimfield[j+(k*m)] = field[(j + xoff)+((k + yoff)*(cwidth-2))];
}
}
memcpy((void *)field,(void *)trimfield,trimfieldsize*sizeof(int));
delete trimfield;
}
SaveTerrFile(field, width, "rgbOlsena.png");
return field;
}
int OlsenNoise2D::hashrandom(std::vector<long long int> elements)
{
long long hashcalc = 0;
for (int i = 0; i < elements.size(); i++)
{
hashcalc ^= elements[i];
hashcalc = hash(hashcalc);
}
return (int) hashcalc;
};
long long OlsenNoise2D::hash(long long v)
{
long long hash = v;
long long h = hash;
switch ((int) hash & 3)
{
case 3:
hash += h;
hash ^= hash << 32;
hash ^= h << 36;
hash += hash >> 22;
break;
case 2:
hash += h;
hash ^= hash << 22;
hash += hash >> 34;
break;
case 1:
hash += h;
hash ^= hash << 20;
hash += hash >> 2;
}
hash ^= hash << 6;
hash += hash >> 10;
hash ^= hash << 8;
hash += hash >> 34;
hash ^= hash << 50;
hash += hash >> 12;
return hash;
};
int main()
{
/// Test
int ImageSize=2048;
int * imageInput = new int[ImageSize*ImageSize];
/// Image
OlsenNoise2D testingolsen;
imageInput=testingolsen.olsennoise(0,0,ImageSize,ImageSize);
// SaveTerrFile(imageInput, ImageSize, "rgbOlsen.png");
delete imageInput;
return 1;
}
void SaveTerrFile(const int * image, int size, char * filename)
{
png::image< png::rgb_pixel > newimage(size, size);
for (unsigned int y = 0; y < newimage.get_width(); ++y)
{
for (unsigned int x = 0; x < newimage.get_height(); ++x)
{
int col = int(image[x+(y*newimage.get_width())]);
newimage[y][x] = png::rgb_pixel(col,col,col);
// non-checking equivalent of image.set_pixel(x, y, ...);
}
}
newimage.write(filename);
}
The original JAVA code is
Code:
/*
* @author Tat
*/
public class OlsenNoise2D {
public int[][] olsennoise(int x, int y, int width, int height) {
int maxiterations = 7;
int cx, cy;
int cxh, cyh;
int cwidth, cheight;
int xoff, yoff;
int nwidth, nheight;
int nx, ny;
int nxh, nyh;
int[][] field = null;
for (int iteration = 0; iteration < maxiterations; iteration++) {
nx = x;
ny = y;
nxh = x + width;
nyh = y + width;
for (int i = 1, n = maxiterations - iteration; i < n; i++) {
nx = (nx / 2) - 1;
ny = (ny / 2) - 1;
nxh = 1 -(-nxh/2);
nyh = 1 -(-nyh/2);
}
xoff = -2*((nx/2)) + nx + 1;
yoff = -2*((ny/2)) + ny + 1;
cx = (nx / 2) - 1;
cy = (ny / 2) - 1;
cxh = 1 -(-nxh/2);
cyh = 1 -(-nyh/2);
nwidth = nxh - nx;
nheight = nyh - ny;
cwidth = cxh - cx;
cheight = cyh - cy;
if (field == null) field = new int[cwidth][cheight];
for (int j = 0, m = field.length; j < m; j++) {
for (int k = 0, n = field[j].length; k < n; k++) {
field[j][k] += (hashrandom(cx + j, cy + k, iteration) & (1 << (7 - iteration)));
}
}
int[][] upsampled = new int[field.length * 2][field[0].length * 2];
for (int j = 0, m = upsampled.length; j < m; j++) {
for (int k = 0, n = upsampled[0].length; k < n; k++) {
upsampled[j][k] = field[j / 2][k / 2];
}
}
field = upsampled;
int[][] blurfield = new int[field.length - 2][field[0].length - 2];
for (int j = 0, m = blurfield.length; j < m; j++) {
for (int k = 0, n = blurfield[0].length; k < n; k++) {
for (int h = 0; h < 9; h++) {
blurfield[j][k] += field[j + (h % 3)][k + (h / 3)];
}
blurfield[j][k] /= 9;
}
}
field = blurfield;
int[][] trimfield = new int[nwidth][nheight];
for (int j = 0, m = trimfield.length; j < m; j++) {
for (int k = 0, n = trimfield[0].length; k < n; k++) {
trimfield[j][k] = field[j + xoff][k + yoff];
}
}
field = trimfield;
}
return field;
}
public static int hashrandom(int... elements) {
long hash = 0;
for (int i = 0; i < elements.length; i++) {
hash ^= elements[i];
hash = hash(hash);
}
return (int) hash;
}
public static long hash(long v) {
long hash = v;
long h = hash;
switch ((int) hash & 3) {
case 3:
hash += h;
hash ^= hash << 32;
hash ^= h << 36;
hash += hash >> 22;
break;
case 2:
hash += h;
hash ^= hash << 22;
hash += hash >> 34;
break;
case 1:
hash += h;
hash ^= hash << 20;
hash += hash >> 2;
}
hash ^= hash << 6;
hash += hash >> 10;
hash ^= hash << 8;
hash += hash >> 34;
hash ^= hash << 50;
hash += hash >> 12;
return hash;
}
}
-
February 18th, 2015, 09:11 AM
#12
Re: Converting JAVA for loop to C++ for loop
This is the output from iteration 1-5. 7 is the max.
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|