Quantcast
Channel: openFrameworks - Latest posts
Viewing all articles
Browse latest Browse all 40524

Update Mesh slow-down

$
0
0

the way you are getting the color from the image in the first loop is relatively slow. a faster way to do it is:

        float intensityThreshold = drawMyMesh;
        for (auto line: image.getPixels().getLines()) {
            auto x = 0;
            for (auto pixel: line.getPixels()) {
                ofColor c = pixel.getColor();
                float intensity = c.getLightness();
                if (intensity <= intensityThreshold) {
                    float saturation = c.getSaturation();
                    float z = ofMap(saturation, 0, 255, -100, 100);
                    ofVec3f pos(x * 5, line.getLineNum() * 5, z);
                    mesh.addVertex(pos);
                    mesh.addColor(c);
                }
                ++x;
            }
        }

in the second loop you could optimize a bit by using the squaredDistance instead of the plain distance which avoids a square root which is really expensive in terms of cpu usage.

but the problem really is in the second loop, you are going through all the vertices for every vertex, depending on what the first loop does you can easily get thousands of millions of iterations which there's no way to optimize. you could try going through one in 2 or 3 vertices or pixels instead of going through all of them.

also if you are just planing to work with static images you can regenerate the mesh only when the parameters change instead of in every update by adding a listener to the parameters:

drawMyMesh.addListener(this, &ofApp::parametersChanged);
actionRadius.addListener(this, &ofApp::parametersChanged);

then:

void ofApp::parametersChanged(float & v){
    // update the mesh here instead of in update
}

Viewing all articles
Browse latest Browse all 40524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>