Reverse this string

edited April 2015 in Kinect

Hi everyone, I have this code that saves a formatted txt file, containing the depth values coming from a kinect, I wanna "reverse" the content of the string kinectDEM, to obtain "an image" rotated of 180 degrees, so to do that I wanna append the last value of depthValues as the first of kinectDEM, the second to last of depthValues as the second of kinectDEM an so on. I wanna do that before the "sb.toString" command. Here's the code:

import java.io.File;
import SimpleOpenNI.*;

SimpleOpenNI kinect;

void setup()
{
  //size(640, 480);
  kinect = new SimpleOpenNI(this);
  kinect.enableDepth();
}

int precedente = millis();

void draw()
{
  kinect.update();
  PImage depthImage = kinect.depthImage();
  image(depthImage, 0, 0);
  int[] depthValues = kinect.depthMap();
  StringBuilder sb = new StringBuilder();

  int kinectheight = 1200; // kinect distance from the baselevel
  int pixelsPerRow = 640;
  int pixelsToSkip = 40;
  int rowNum = 0;
  for (int i = 0; i < depthValues.length; i++) {
    if (i > 0 && i == (rowNum + 1) * pixelsPerRow) {
      rowNum++;
      sb.append("\n");
    }
    if (i >= (rowNum * pixelsPerRow) + pixelsToSkip) {
      sb.append(kinectheight - depthValues[i]);
      if (i < depthValues.length - 1) {
        sb.append(" ");
      }
    }
  }

  String kinectDEM = sb.toString();

  final String[] txt= new String[1]; //creates a string array of 2 elements
  int savingtimestep = 2000;  // time step in millisec between each saving
  if (millis() > precedente + savingtimestep) {
    txt[0] = "ncols         600\nnrows         480\nxllcorner     0\nyllcorner     0\ncellsize      91.6667\nNODATA_value  -9999\n" +kinectDEM;
    saveStrings("kinectDEM.tmp", txt);
    precedente = millis();

    // delete the old .txt file
    File f = new File ("C:/Users/Nicola/Desktop/KinectDEMlas_rename/kinectDEM.txt");
    boolean success = f.delete();

    // rename the .tmp file to .txt
    File oldName = new File("C:/Users/Nicola/Desktop/KinectDEMlas_rename/kinectDEM.tmp");
    File newName = new File("C:/Users/Nicola/Desktop/KinectDEMlas_rename/kinectDEM.txt");
    oldName.renameTo(newName);

  }
}

Thanks

Answers

  • Okay, and which part of this is giving you trouble? Have you googled "java reverse String"? What did you find? What exactly are you confused about?

  • edited April 2015

    Hi Kevin, thanks for the answer, I tried using:

    StringBuffer reversed = new StringBuffer(kinectDEM);
    reversed.reverse();
    

    but it mix things up in the string "reversed" after the reverse command, because, the string kinectDEM as you can see is formatted with a new line after each 600 elements, it looks like this:

      0      1      2 ......... 597    598    599
    600  601  601 ...... 1198  1199  1200
    ..........................................................
    ............................................. 288000
    

    after the reverse command the data are mixed up, so instead of reverse the string after its creation, I'd like to change the way the string "kinectDEM" is created with StringBuilder(), modifying the for loop in line 27, I guess with a loop that iterate backwards so that the last value of depthValues ( int[]) will be the first of kinectDEM, the second to last of depthValues will be the second of kinectDEM an so on. The doubt is, how to write the for loop to do that.

    Thanks

  • the string kinectDEM as you can see

    the problem is that we can't see. we don't have kinect hardware. we certainly don't have the file that only exists on your c drive.

    we need a few examples of the input string and the desired output string. using a word like "reverse" isn't enough.

  • "As you can see" is referred to the insertion of a new line after each 600 elements, lines 28 to 30 in the code above:

    if (i > 0 && i == (rowNum + 1) * pixelsPerRow) {
          rowNum++;
          sb.append("\n");
    

    Sorry if I was unclear, but you don't need to have a kinect to undestand the problem, it's not about the kinect, it's about string. Here the problem is this: starting from the variable " int[] depthValues = kinect.depthMap() " which is a list of numbers returning the distances of the target from the kinect for each pixel (640 x 480) (let's say, it's a list of integers) I already transform it into a string using the append method of StringBuilder, and in the same time I start formatting it like an ESRI ASCII .txt file, so with the append method of StringBuilder I convert a int variable into a string and I also split the list of integers with a new line after each 600 elements and remove the first 40 elements of each row. I don't need anything else, I just want to change the order by means the StringBuilder append the values, here's a figure that will explain better:

    Untitled

    the difference respect to the figure is that DepthValues has 307200 elements (not 10) and each row of the output string will be of 600 elements (not 5).

    Thanks guys

  • I can't get it why numbers would need to be converted to String in order to reverse their order? :-/

  • At the end I need a .txt file to be saved, can I do that using the numbers?

  • If we got elements in some index-based container like an array for example, it's just a matter of calling reverse(): https://processing.org/reference/reverse_.html

  • Are you suggesting me to use reverse() on the DepthValues variable before the conversion into a string? And then leave the StringBuilder modifications as they are?

  • edited April 2015

    If you gonna reverse() the int[] itself, why would you need a StringBuilder anyways? O:-)

    If you need a String[] after everything is done, just call str():
    https://processing.org/reference/strconvert_.html

    Or even nf() or 1 of its 3 other siblings: *-:)
    https://processing.org/reference/nf_.html
    https://processing.org/reference/nfc_.html
    https://processing.org/reference/nfp_.html
    https://processing.org/reference/nfs_.html

Sign In or Register to comment.