Can my messy 3D scanner code be optimized?

edited December 2015 in Library Questions

Hello, we built a 3D scanner and used processing to calculate the cartesian coordinates from the image and output to .asc -file. My code is here:

The code and markings are written in Finnish, but I will try to clarify the code below a bit:

My questions:

  1. So about NoLoop - function, where should I put it?

  2. In my function pyorita(), which sends serial data to Arduino, which turns the scanner platter, I must have 1000ms delay, otherwise the program will crash. This makes my program run slow, though. Could this be optimized anyhow? (If I remove the delay, it will just print me "-1" "-1" "-1" in the console with red font (error))s

  3. Could I anyhow see the latest picture taken by scanner? Now the screen is black until the scan is fully complete. In the end, it shows the LAST (200th) raw picture it took.

here is my code as it seems impossible for me to properly send it here: http://hietanen.xyz/paste/?paste=8

Answers

  • images only show when draw() finishes. yours only finishes when you've done scanning...

    https://processing.org/reference/draw_.html

  • edited December 2015

    Thanks @koogs ! This clarifies a lot.

    Can I put the draw() function inside the loop? :) Should I make a new function which calls the draw function with the image as a parameter (is this even possible) or how should I edit this? What do you think?

  • I didn't test your code, but there are some things that i noticed.

    • noLoop() stops draw() from executing continously, so it can be called once, multiple calls don't make any sense, unless you call loop() somewhere.

    • As koogs mentioned, the display-window updates at the end of draw(), so when you use noLoop(), it will only be updated once, after all your code finished.

    • So if you want to show different images, you could remove the for-loop that you have in 81 and could just calculate one image in each frame.

    • Lines 87-90 don't make sense, you just assign all pixels to themselfes. You can remove that.

  • edited December 2015

    Thank you @benja

    How could I make the code calculate one image per frame? Now the framerate is 30 fps, or do you think the code executed in draw() would stop if there is delay in the pyorita() , which is called from draw () ?

    I only need one image per one rotation of platter.

  • Yes, this is how delay() works. It's usually better to use some kind of timer instead, but i think it should work with your delay like it does now.

    The code inside of your loop could stay like it is, but you would have to replace the variable k with frameCount i.e..

    Then when frameCount reaches the number of images that you want to process, you can call a function that saves the output file and call noLoop(). (line 97+98)

  • Thank you again for very detailed answer. I will test with modifications tomorrow/on Thursday when I can reach our scanner.

  • edited December 2015

    Hello @benja !

    The framCount way to handle iteration works! Thanks a lot. I still wonder how to get rid of the delays, if I just remove then, the program crashes... Why does this happen?

    edit: Seems like if a loop does nothing it will crash the program. I added a small amount of sleep (10ms) to the loop where the program crashed and it works like a charm now

Sign In or Register to comment.