# HG changeset patch # User Robert Tubb # Date 1355144403 0 # Node ID 79c7cf39a0a0abb797dd4231a6f22a38fc6db9ba # Parent d346ddc50f70c7fa0b238dd697451cc8c93ccfbf Fixed new mesh crash - static array bounds. Made home made mutex for wavetable access. Less clicks? diff -r d346ddc50f70 -r 79c7cf39a0a0 lump.h --- a/lump.h Fri Dec 07 19:20:57 2012 +0000 +++ b/lump.h Mon Dec 10 13:00:03 2012 +0000 @@ -18,22 +18,23 @@ private: double mass, inverseMass; bool grabbed, highlighted; - const int maxSprings; - + TwoVector velocity; TwoVector accel; double friction; public: bool constrained; - Spring** attachedSprings; // pointers to all attached springs + vector attachedSprings; // pointers to all attached springs int numAttachedSprings; enum ConstrainMode {NOT_CONSTRAINED,CONSTRAIN_X,CONSTRAIN_Y,CONSTRAIN_XY}; ConstrainMode constrainMode; bool isInScanPath; - + bool isScanPathEnd; + bool isScanPathStart; + TwoVector position; TwoVector previousPosition; double totalForceMag; diff -r d346ddc50f70 -r 79c7cf39a0a0 lump.mm --- a/lump.mm Fri Dec 07 19:20:57 2012 +0000 +++ b/lump.mm Mon Dec 10 13:00:03 2012 +0000 @@ -16,7 +16,7 @@ //int Lump::numLumps = 0; //-------------------------------------------------------------- // default constr -Lump::Lump() : maxSprings(100){ +Lump::Lump(){ //cout << "constructing a default lump" << endl; mass = 10.0; inverseMass = 1.0/mass; // not needed - used csquared for force @@ -34,45 +34,40 @@ totalForceMag = 0.0; size = 3; //sqrt(mass/3.0); isInScanPath = false; + isScanPathStart = false; + isScanPathEnd = false; + previousPosition.setCoord(0.5,0.5); zeroRefPos.setCoord(0.0,0.0); constrainMode = NOT_CONSTRAINED; - - attachedSprings = new Spring*[maxSprings]; + grabID = -1; - //myIndex = Lump::numLumps++; - //cout << numAttachedSprings << endl; } //-------------------------------------------------------------- // arg constructor -Lump::Lump(double aMass,double aFriction, double positionX, double positionY) : maxSprings(100){ +Lump::Lump(double aMass,double aFriction, double positionX, double positionY){ // set members } //-------------------------------------------------------------- Lump::~Lump(){ - - delete [] attachedSprings; + } //-------------------------------------------------------------- void Lump::attachSpring(Spring* aSpring){ - // stick pointer in array - if(numAttachedSprings < maxSprings){ - attachedSprings[numAttachedSprings] = aSpring; - numAttachedSprings++; - }else{ - cout << "cant attach another spring as mass already has " << maxSprings << endl; - return; - } + + attachedSprings.push_back(aSpring); + numAttachedSprings++; + } //-------------------------------------------------------------- Spring * Lump::checkConnectedTo(Lump * otherLump){ // loop thru all attached springs looking at other end - for(int i = 0; igetLumpOnOtherEnd(this) == otherLump){ return attachedSprings[i]; } @@ -122,7 +117,18 @@ int xpos = position.x * ofGetHeight() + globalUI.borderSize; int ypos = position.y * ofGetHeight(); ofEllipse(xpos,ypos, 6, 6); - + if(isScanPathEnd){ + ofSetColor(255, 255, 0); + ofNoFill(); + ofCircle(xpos, ypos, 6.0); + ofFill(); + } + if(isScanPathStart){ + ofSetColor(0, 255, 255); + ofNoFill(); + ofCircle(xpos, ypos, 7.0); + ofFill(); + } // code to display restpos and displacement /* ofSetColor(0, 0, 0); @@ -141,7 +147,10 @@ int ypos = position.y * ofGetHeight(); ofEllipse(xpos,ypos, 2, 2); }else if (constrained){ - ofSetColor(200,23,23); + ofSetColor(200,5,5); + int xpos = position.x * ofGetHeight() + globalUI.borderSize; + int ypos = position.y * ofGetHeight(); + ofEllipse(xpos,ypos, 2, 2); }else{ // dont draw 'normal ' lumps return; @@ -166,7 +175,7 @@ TwoVector totalForce(0.0,0.0); // sum up force from each attached spring - for(int i = 0;igetForce(this); @@ -260,14 +269,14 @@ if (constrained || grabbed) return; double avx = 0.0, avy = 0.0; // average the position of all the attached lumps - for(int i = 0;igetLumpOnOtherEnd(this); avx += otherLump->position.x; avy += otherLump->position.y; } - avx /= numAttachedSprings; - avy /= numAttachedSprings; + avx /= attachedSprings.size(); + avy /= attachedSprings.size(); // mix in the average with the 'real' position.x = (1 - amt)*position.x + amt*avx; @@ -281,14 +290,14 @@ //TODO what if edges unconstrained? this is why filtered unconstrained just ends up as a line... // average the position of all the attached lumps - for(int i = 0;igetLumpOnOtherEnd(this); av.x += otherLump->position.x; av.y += otherLump->position.y; } - av.x /= numAttachedSprings; - av.y /= numAttachedSprings; + av.x /= attachedSprings.size(); + av.y /= attachedSprings.size(); return av; diff -r d346ddc50f70 -r 79c7cf39a0a0 mesh.h --- a/mesh.h Fri Dec 07 19:20:57 2012 +0000 +++ b/mesh.h Mon Dec 10 13:00:03 2012 +0000 @@ -31,8 +31,8 @@ int numLumps; int numSprings; - Lump *lumps; - + //Lump *lumps; + vector lumps; vector springs; //Spring *springs; diff -r d346ddc50f70 -r 79c7cf39a0a0 mesh.mm --- a/mesh.mm Fri Dec 07 19:20:57 2012 +0000 +++ b/mesh.mm Mon Dec 10 13:00:03 2012 +0000 @@ -42,9 +42,8 @@ Mesh::~Mesh(){ cout << "destroying mesh" << endl; - delete [] lumps; - //delete scanPath; + } /////////// // interface (called from testApp) @@ -59,13 +58,13 @@ static int updown = 0; updown++; if(updown > 24){ - for(int i=0;i avLumpPos = vector(lumps.size()); - for(int i=0;i(numLumps); cout << "made " << numLumps << " lumps\n"; // one by one @@ -787,7 +793,7 @@ // set positions for(int row = 0; row < height; row++){ // go down column for(int col = 0;col < width; col++){ // go along row - if(i >= numLumps){ + if(i >= lumps.size()){ cout << " setLumpPosisions index error!\n"; } lumps[i].setPosition(hspacing*col,vspacing*row); @@ -811,7 +817,7 @@ if(springnum >= numSprings){ cout << " makeConnections index error!\n"; } - if(lumpnum >= numLumps){ + if(lumpnum >= lumps.size()){ cout << " makeConnections index error!\n"; } connect(springnum,lumpnum); @@ -830,7 +836,7 @@ if(springnum >= numSprings){ cout << " makeConnections index error!\n"; } - if(lumpnum >= numLumps){ + if(lumpnum >= lumps.size()){ cout << " makeConnections index error!\n"; } connect(springnum,lumpnum); @@ -848,7 +854,7 @@ if(springnum >= numSprings){ cout << " makeConnections index error!\n"; } - if(lumpnum >= numLumps){ + if(lumpnum >= lumps.size()){ cout << " makeConnections index error!\n"; } connect(springnum,lumpnum+1); @@ -866,7 +872,7 @@ if(springnum >= numSprings){ cout << " makeConnections index error!\n"; } - if(lumpnum >= numLumps){ + if(lumpnum >= lumps.size()){ cout << " makeConnections index error!\n"; } connect(springnum,lumpnum-1); @@ -886,12 +892,12 @@ // check if lump is within grab range switch (aMode) { case CONSTRAIN_GRAB_REGION: - for(i = 0; i= numLumps){ + if(i >= lumps.size()){ cout << "constrain index error\n"; } lumps[i].constrain(); @@ -903,7 +909,7 @@ for(int row = 0; row < height; row++){ // go down column for(int col = 0;col < width; col++){ // go along row if(row == 0 || row == height-1 || col == 0 || col == width-1){ - if(i >= numLumps){ + if(i >= lumps.size()){ cout << "constrain index error\n"; } lumps[i].constrain(); @@ -917,12 +923,12 @@ for(int row = 0; row < height; row++){ // go down column for(int col = 0;col < width; col++){ // go along row if(row == 0 || row == height-1){ - if(i >= numLumps){ + if(i >= lumps.size()){ cout << "constrain index error\n"; } lumps[i].constrain(Lump::CONSTRAIN_Y); }else if( col == 0 || col == width-1){ - if(i >= numLumps){ + if(i >= lumps.size()){ cout << "constrain index error\n"; } lumps[i].constrain(Lump::CONSTRAIN_X); @@ -939,7 +945,7 @@ || (row == height-1 && col == 0) || (row == 0 && col == width-1) || (row == height-1 && col == width-1)){ - if(i >= numLumps){ + if(i >= lumps.size()){ cout << "constrain index error\n"; } lumps[i].constrain(); @@ -966,7 +972,7 @@ int springno = vmarg * (width - 1) + hmarg; // do top horz for(int i = 0; i < width - 2*hmarg; i++){ - if(lumpno >= numLumps) cout << "makeDefaultScanPath index error\n"; + if(lumpno >= lumps.size()) cout << "makeDefaultScanPath index error\n"; if(springno >= numSprings) cout << "makeDefaultScanPath index error\n"; scanPath.addElement(&lumps[lumpno], &springs[springno]); lumpno++; @@ -978,7 +984,7 @@ springno = height*(width-1) + ((height-1) * (width - hmarg)) + vmarg; for(int i = 0; i < height - (2 * vmarg); i++){ - if(lumpno >= numLumps) cout << "makeDefaultScanPath index error\n"; + if(lumpno >= lumps.size()) cout << "makeDefaultScanPath index error\n"; if(springno >= numSprings) cout << "makeDefaultScanPath index error\n"; scanPath.addElement(&lumps[lumpno], &springs[springno]); springno++; // jump to next row @@ -989,7 +995,7 @@ // do bottom horz right to left springno = (height - vmarg) * (width - 1) + (width - hmarg - 1); for(int i = 0; i < width - 2*hmarg; i++){ - if(lumpno >= numLumps) cout << "makeDefaultScanPath index error\n"; + if(lumpno >= lumps.size()) cout << "makeDefaultScanPath index error\n"; if(springno >= numSprings) cout << "makeDefaultScanPath index error\n"; scanPath.addElement(&lumps[lumpno], &springs[springno]); springno--; // jump to next row @@ -999,7 +1005,7 @@ // all horz spr // left marg rows // top margin springno = height*(width-1) + ((height-1) * hmarg) + height - vmarg - 1; for(int i = 0; i < height - 2 * vmarg; i++){ - if(lumpno >= numLumps) cout << "makeDefaultScanPath index error\n"; + if(lumpno >= lumps.size()) cout << "makeDefaultScanPath index error\n"; if(springno >= numSprings) cout << "makeDefaultScanPath index error\n"; scanPath.addElement(&lumps[lumpno], &springs[springno]); springno--; // jump to next row @@ -1042,8 +1048,9 @@ numLumps = numSpokes * numRings + 1; // +1 cos one in the middle numSprings = 2 * numSpokes * numRings; - lumps = new Lump[numLumps]; - cout << "made " << numLumps << " lumps\n"; + //lumps = new Lump[numLumps]; + lumps = vector(numLumps); + cout << "made " << numLumps << " lumps\n"; springs = vector(numSprings); cout << "made " << numSprings << " springs\n"; @@ -1136,7 +1143,7 @@ // check if lump is within grab range switch (aMode) { case CONSTRAIN_GRAB_REGION: - for(i = 0; i(numLumps); + cout << "made " << numLumps << " lumps\n"; springs = vector(numSprings); cout << "made " << numSprings << " springs\n"; @@ -1310,7 +1318,7 @@ // check if lump is within grab range switch (aMode) { case CONSTRAIN_GRAB_REGION: - for(i = 0; i(numLumps); cout << "made " << numLumps << " lumps\n"; springs = vector(numSprings); cout << "made " << numSprings << " springs\n"; @@ -1511,7 +1519,7 @@ // check if lump is within grab range switch (aMode) { case CONSTRAIN_GRAB_REGION: - for(i = 0; i(numLumps); springs = vector(numSprings); } @@ -1611,7 +1619,7 @@ cout << "LineMesh class setLumpPositions\n"; // double hspacing = (1.0 - 0.05)/numSegments; - for(int i = 0; i < numLumps; i++){ + for(int i = 0; i < lumps.size(); i++){ lumps[i].setPosition(0.025+hspacing*i, 0.5); } } @@ -1632,10 +1640,10 @@ cout << "LineMesh class constrain\n"; if (aMode == CONSTRAIN_EDGES || aMode == CONSTRAIN_CORNERS){ lumps[0].constrain(); - lumps[numLumps-1].constrain(); + lumps[lumps.size()-1].constrain(); }else if (aMode == CONSTRAIN_GRAB_REGION){ TwoVector diff; - for(int i = 0; i(numLumps); springs = vector(numSprings); } @@ -1726,16 +1734,16 @@ cout << "LineMesh class constrain\n"; if (aMode == CONSTRAIN_EDGES || aMode == CONSTRAIN_CORNERS){ lumps[0].constrain(); - lumps[numLumps-1].constrain(); + lumps[lumps.size()-1].constrain(); - for(int i = numSegments; i < numLumps; i++){ + for(int i = numSegments; i < lumps.size(); i++){ lumps[i].constrain(); } } if (aMode == CONSTRAIN_GRAB_REGION){ TwoVector diff; - for(int i = 0; i(numLumps); //springs = new PressureSpring[numSprings]; //TODO PressureSpring shoudl BE a spring ? @@ -1865,10 +1873,10 @@ cout << "DropletMesh class constrain\n"; if (aMode == CONSTRAIN_EDGES || aMode == CONSTRAIN_CORNERS){ lumps[0].constrain(); - lumps[numLumps-1].constrain(); + lumps[lumps.size()-1].constrain(); } else if (aMode == CONSTRAIN_GRAB_REGION){ TwoVector diff; - for(int i = 0; i(numLumps); cout << "made " << numLumps << " lumps\n"; springs = vector(numSprings); cout << "made " << numSprings << " springs\n"; @@ -2050,7 +2058,7 @@ // check if lump is within grab range switch (aMode) { case CONSTRAIN_GRAB_REGION: - for(i = 0; i::iterator elitr = pathElements.begin(); elitr < pathElements.end(); elitr++){ (*elitr).eLump->removeFromScanPath(); (*elitr).eSpring->removeFromScanPath(); @@ -162,16 +163,18 @@ // TODO THRED MUTEX HERE!?? // this is called from graphics thread // reset the interp between frames - - /* + int i = 0; while(audioAccessing){ i++; } - cout << "buffers swapped while update!" << i << "times\n"; + if(i > 0){ + cout << "Update wavetables had to wait for audio access " << i << " times\n"; + // hardly ever happens + } updateAccessing = true; - */ + switch(scanMode){ case DISPLACEMENT: // now fill with new values @@ -206,13 +209,9 @@ break; default: break; - - + } - - - - + temp = wavetableOld; wavetableOld = wavetableNew; wavetableNew = wavetableUpdate; @@ -247,15 +246,14 @@ // TODO THRED MUTEX HERE!?? // this is called from audio thread - /* + int i = 0; while(updateAccessing){ i++; } audioAccessing = true; - cout << "update is accessing while audio is" << i << "times\n"; - - */ + if(i>0) cout << "Audio thread had to wait for wavetable update " << i << " times\n"; + double oldsample = (1 - frac) * wavetableOld[n0] + frac * wavetableOld[n1]; diff -r d346ddc50f70 -r 79c7cf39a0a0 testApp.mm --- a/testApp.mm Fri Dec 07 19:20:57 2012 +0000 +++ b/testApp.mm Mon Dec 10 13:00:03 2012 +0000 @@ -48,7 +48,7 @@ globalUI.touchMode = globalUI.GRAB; scanPath.scanMode = scanPath.DISPLACEMENT; } - +//-------------------------------------------------------------- void testApp::setupMesh(){ // SET UP THE MESH STRUCTURE if(audioAccessFlag) return; // or rather wait ! @@ -810,7 +810,7 @@ void testApp::audioRequested (float * output, int bufferSize, int nChannels){ float sample; static double phasor; - static double sinePhasor; + if(meshConstructionFlag) return; // if no mesh , scanpath will still be there, but empty @@ -819,11 +819,8 @@ if(audioOn){ audioAccessFlag = TRUE; for (int i = 0; i < bufferSize; i++){ - sinePhasor = sin(2*PI*phasor); sample = float(scanPath.getNextSample(phasor)); - //sample = float(scanPath.getNextSample()); - // BASIC STRING sample = float(basicString->getNextSample(phasor)); // hipass to get rid of DC //sample = mydspTools.highpass1(sample);