Different behaviour if code is wrapped in method - FaceShift - ofxFaceshift

Hi,
I have this strange situation in which, if I wrap this code below

// doodle lines
        ofPushStyle();
        ofPushMatrix();
        meshBrows.setMode(OF_PRIMITIVE_LINE_LOOP);
        
        ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
    
    int scaleFactor = 10*browsFeatCount;
    for (int i=0; i<browsData.size(); i++) {
        meshBrows.addVertex(ofVec2f(browsData[i]*scaleFactor,browsData[i+1]*scaleFactor));
        meshBrows.addColor(255);
        
    }
    
    
        meshBrows.drawWireframe();
 
    
    meshBrows.clear();
    ofPopMatrix();
    ofPopStyle();

    
    
    /*---------------------------*/
    
    
    
    
    //clearing the vectors every cycle
    
    if (browsData.size()%browsFeatCount==0) {
       vector<float>().swap(browsData);
       vector<string>().swap(browsName);

        browsData.clear();
        browsName.clear();
    }

in this method

    void ofApp::doodle(ofMesh &featMesh, int &featCount, vector<float>&featData, vector<string>&featName, ofColor color, float scale, bool wireframe){
    
    
    // doodle lines
    ofPushStyle();
    ofPushMatrix();
    featMesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
    ofEnableAlphaBlending();
    ofEnableBlendMode(OF_BLENDMODE_ADD);
    
    ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
    
    int scaleFactor = scale*featCount;
    for (int i=0; i<featData.size(); i++) {
        
        
            featMesh.addVertex(ofVec2f(featData[i]*scaleFactor,featData[i+1]*scaleFactor));
            featMesh.addColor(color);
        
    }
    
    if (!wireframe) {
        
        featMesh.draw();
        
    } else {
 
        featMesh.drawWireframe();
    }
    
    featMesh.clear();
    ofPopMatrix();
    ofPopStyle();
    
    
    //clearing the vectors every cycle
    
    if (featData.size()%featCount==0) {
        vector<float>().swap(featData);
        vector<string>().swap(featName);

    }

    
    
    
}

/*---------------------------*/

I get a different behaviour.

check the attachments to see what I mean.

I don’t really get the reason for this.
Any help is good. I am stuck!

cheers

right_wrong.zip (124.1 KB)

Try a debug and look at the local variables, I have a feeling something isn’t being initialized in the method that is handled in the draw method. certainly might help identify the bug

hi @DomAmato
thanks for replying. I had a look at the debug but I have no idea of what I am seeing there.
I follow line after line and all seems ok to me. But I guess it is not.
I am updating the code with the method maybe it will be clearer?

cheers

so when you put a break point at this line and you look at all the points being fed into the vector there is no anomalies? That would be my guess since the videos you supplied would lead me to believe this is where the error is occuring

Hi @DomAmato
I never used this feature of xCode. it’s awesome. thanks.
I looked into that line. this is what happens.
when I first call the function with a specific feature, the values that I get are weird. they are negative floats. and this is not supposed to happen.
Funny thing though is that when I call the function just after with other features, this doesn’t happen.
I am trying to look into the code. Probably I will do that tonight. In the meanwhile thanks for helping me out and if you have other hints or thought about why this is happening, please share it!

cheers

this is what I mean: first function gets those awkward results. Others seems ok. But it’s not consistent. sometimes other gets funny results too.

    doodle(meshBrows, browsFeatCount, browsData, browsName, ofColor(255),10, false);
    doodle(meshEye, eyeFeatCount, eyeData, eyeName, ofColor(255,0,0),10, false);
    doodle(meshMouth, mouthFeatCount, mouthData, mouthName, ofColor(0,200,0),10, false);
    doodle(meshJaw, jawFeatCount, jawData,jawName, ofColor(100,200,0),10, false);
    doodle(meshCheek, cheekFeatCount, cheekData, cheekName, ofColor(0,100,200),10, false);
    doodle(meshChin, chinFeatCount, chinData, chinName, ofColor(200,200,0),10, false);
    doodle(meshPuff, puffFeatCount, puffData,puffName, ofColor(0,200,200),10, false);
    doodle(meshSneer, sneerFeatCount, sneerData, sneerName, ofColor(0,200,0),10, false);
    doodle(meshLips, lipsFeatCount, lipsData, lipsName, ofColor(0,100,0),10, false);

My guess is that the values aren’t being initialized and are defaulting to their default initialized value, which is usually the the largest or smallest number that the data type can be. If your values can only fall between a certain set of values like say the screen size you can just clamp them using ofClamp. Still that will not fix things thats only a fail safe, I would make sure the variables are initialized before ever passing them along in the function call, I don’t know how your program does this but ill give an example.

int i;
function_a(i);

That is a totally valid thing but the value of i is going to be like -2,147,483,647 because its value is never given.

further be careful with this

featMesh.addVertex(ofVec2f(featData[i]*scaleFactor,featData[i+1]*scaleFactor));

if featData has a size of 1 you will get an index out of bounds which usually throws an error but something might be preventing that which would give you bad values as well. Your for loop should be more like this:

 for (int i=0; i<featData.size()-1; i++) {

otherwise its likely you will get an error.

Hi @DomAmato,
thanks for the time you are putting in this.
As far as I understand my variables should be all initiated.
I declared them in .h and then I fill some of them in setup()

        for(int i = 0; i < addresses.size(); i++) {
                if (ofIsStringInString(faceShift.getBlendshapeName(i), "Brows") == true) {
                    browsFeatCount++;
                    meshBrows.addVertex(ofVec3f(0,0,0));

                }
//...same for the rest of the featuresCount and the other Mesh
    }

and this is what happens in update

for(int i = 0; i < addresses.size(); i++) {
                addMessage("/gesture" + addresses[i], faceShift.getBlendshapeWeight(i));
                
                if (ofIsStringInString(faceShift.getBlendshapeName(i), "Brows") == true) {
                    browsData.push_back(faceShift.getBlendshapeWeight(i));
                    browsName.push_back(faceShift.getBlendshapeName(i));
                }
    //...same for the rest of the featuresData and featureName
}

If I don’t get this wrong when draw() loop starts all these variables should be initialised. Last one out is the boolean but I don’t think it is important?

doodle(meshBrows, browsFeatCount, browsData, browsName, ofColor(255),10, false);

The difference from the code of the other day is only that I initialise the meshes in setup(). The rest is all the same.
However I get the same glitch.
I tried your option to add -1 to the for loop but I get an error, even though your logic makes sense to me.

I tried to condition the creation of the mesh with this

//        if (featData[i]>=0 && featData[i+1]>=0) { ... }

I get a reduction of the glitch but not entirely.
I might try to change approach maybe.

I don’t know. I need to clean up my mind…

cheers! :sunny:

if (featData[i]>=0 && featData[i+1]>=0) { ... }

You should probably do:

 if (featData.size() > 1) { ... } 

before the for loop since the former still can have index out of bounds errors. Generally speaking you never want to do i+1 on an array unless you know that i will never be greater than the size of the array-1. you should attach your code, I can look through it and comment and fix errors though from your other post it sounds like it may be a problem with the addon?

I tried both and I get a slightly better visual result with the first option. Even though it is more “limited”.
I am going to send you the file via pm.
thanks @DomAmato