changeset 3:d346ddc50f70

Vectorised a few things. Fiddled with new mesh creator... not solved much.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 07 Dec 2012 19:20:57 +0000
parents 0aab7a1d3c77
children 79c7cf39a0a0
files button.mm mesh.h mesh.mm scanpath.h scanpath.mm spring.h spring.mm testApp.mm
diffstat 8 files changed, 186 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/button.mm	Thu Nov 22 17:58:34 2012 +0000
+++ b/button.mm	Fri Dec 07 19:20:57 2012 +0000
@@ -10,8 +10,11 @@
 #include "button.h"
 #include "globalUI.h"
 #include "globalForces.h"
+#include "scanpath.h"
+
 extern GlobalUI globalUI;
 extern GlobalForces globalForces;
+extern ScanPath scanPath;
 
 ControlButton::ControlButton(int abuttonId, int awidth,int aheight,int axpos,int aypos, string aname, Mesh *atheMeshPtr, ofColor acolor){
     width = awidth;
@@ -129,7 +132,7 @@
         globalForces.gravityAmt = 4.0;
     }
     if(name == "scanmode"){
-        theMesh->scanPath->scanMode = theMesh->scanPath->SPEED;
+        scanPath.scanMode = scanPath.SPEED;
     }
         pressed = true;
 }
@@ -149,7 +152,7 @@
         globalForces.gravityAmt = 0.0;
     }
     if(name == "scanmode"){
-        theMesh->scanPath->scanMode = theMesh->scanPath->DISPLACEMENT;
+        scanPath.scanMode = scanPath.DISPLACEMENT;
     }
     if (name == "freeAll"){
 		theMesh->constrain(.0,.0,Mesh::CONSTRAIN_EDGES);
--- a/mesh.h	Thu Nov 22 17:58:34 2012 +0000
+++ b/mesh.h	Fri Dec 07 19:20:57 2012 +0000
@@ -32,8 +32,9 @@
 	int numSprings;
     
 	Lump *lumps;
-	Spring *springs;
-    ScanPath* scanPath;
+    
+    vector<Spring> springs;
+	//Spring *springs;
     
     TwoVector centre;
 
--- a/mesh.mm	Thu Nov 22 17:58:34 2012 +0000
+++ b/mesh.mm	Fri Dec 07 19:20:57 2012 +0000
@@ -13,14 +13,13 @@
 #include <math.h>
 
 extern GlobalForces globalForces;
+extern ScanPath  scanPath; // now global
 
 //--------------------------------------------------------------
 Mesh::Mesh(){
 	// construct an empty structure
 	numSprings = 0;
 	numLumps = 0;
-    
-    scanPath = new ScanPath(); // in the end it was a bad idea - what if we're deleting the mesh?
 
 	syrup = false;
 	GRAB_RANGE = 0.040;
@@ -44,8 +43,8 @@
 Mesh::~Mesh(){
 	cout << "destroying mesh" << endl;
 	delete [] lumps; 
-	delete [] springs;
-    delete scanPath;
+
+    //delete scanPath;
 }
 ///////////
 // interface (called from testApp)
@@ -108,7 +107,7 @@
 	}	
     
     // scan the updated mesh
-    scanPath->updateWavetables();
+    scanPath.updateWavetables();
 }
 //--------------------------------------------------------------
 void Mesh::averagingFilter(double amt){
@@ -142,7 +141,7 @@
 		lumps[i].draw();
 	}
 
-    // scanPath->draw(); // uncomment if you want to see the output waveform
+    // scanPath.draw(); // uncomment if you want to see the output waveform
 
 
 }
@@ -494,9 +493,10 @@
 
 void Mesh::zeroRestLength(){
     // sets springs rest length to zero - collapses structure
-    for(int i = 0; i < numSprings; i++){
-        springs[i].setRestLength(0.0);
+    for(vector<Spring>::iterator spriter = springs.begin(); spriter < springs.end(); spriter++){
+        (*spriter).setRestLength(0.0);
     }
+
 }
 //--------------------------------------------------------------
 int Mesh::getNearestLump(double ax, double ay){
@@ -521,7 +521,7 @@
 //--------------------------------------------------------------
 void Mesh::clearScanPath(){
 	// in between having clicked and drawn we can't play anything
-    scanPath->clear();
+    scanPath.clear();
     prevLump = -1;
 }
 //--------------------------------------------------------------
@@ -540,7 +540,7 @@
     
     while(!foundPath && count < 2048){
         count++;
-        if(prevLump == -1){ // (scanPath->howManyElements() == 0){ //
+        if(prevLump == -1){ // (scanPath.howManyElements() == 0){ //
             //first lump
             foundPath = true;
             prevLump = targetLump;
@@ -586,7 +586,7 @@
             // hurrah, add the spring and lump to the scanpath in the right order...
             // get pointer to lump and spring
             
-            scanPath->addElement(&lumps[targetLump], connectedToPrev);
+            scanPath.addElement(&lumps[targetLump], connectedToPrev);
             // and set prev lump to that one
             prevLump = targetLump;
             foundPath = true;
@@ -751,8 +751,6 @@
     zeroRestLength();
     
     radial = false;
-    
-    lumps[345].constrain();
 	
 	cout << "constructing SQUARE mesh\n";
 }
@@ -765,8 +763,7 @@
 // make componets for square
 void SquareCrossMesh::makeComponents(int aheight, int awidth){
     
-    // creating this makes mem error...
-    
+
 	height = aheight;
 	width = awidth;
 	numLumps = height * width;
@@ -775,7 +772,10 @@
 	
 	lumps = new Lump[numLumps];
 	cout << "made " << numLumps << " lumps\n";
-	springs = new Spring[numSprings];
+	
+    // one by one
+    springs = vector<Spring>(numSprings);
+    
 	cout << "made " << numSprings << " springs\n";
 }
 
@@ -968,7 +968,7 @@
     for(int i = 0; i < width - 2*hmarg; i++){
         if(lumpno >= numLumps) cout << "makeDefaultScanPath index error\n";
           if(springno >= numSprings) cout << "makeDefaultScanPath index error\n";
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
         lumpno++;
         springno++;
     }
@@ -980,7 +980,7 @@
     for(int i = 0; i < height - (2 * vmarg); i++){
         if(lumpno >= numLumps) cout << "makeDefaultScanPath index error\n";
         if(springno >= numSprings) cout << "makeDefaultScanPath index error\n";
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
         springno++; // jump to next row
         lumpno += width;   // ditto
     }
@@ -991,7 +991,7 @@
     for(int i = 0; i < width - 2*hmarg; i++){
         if(lumpno >= numLumps) cout << "makeDefaultScanPath index error\n";
         if(springno >= numSprings) cout << "makeDefaultScanPath index error\n";
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
         springno--; // jump to next row
         lumpno--;   // ditto
     } 
@@ -1001,7 +1001,7 @@
     for(int i = 0; i < height - 2 * vmarg; i++){
         if(lumpno >= numLumps) cout << "makeDefaultScanPath index error\n";
         if(springno >= numSprings) cout << "makeDefaultScanPath index error\n";
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
         springno--; // jump to next row
         lumpno -= width;   // ditto
     }
@@ -1044,7 +1044,8 @@
 	
 	lumps = new Lump[numLumps];
 	cout << "made " << numLumps << " lumps\n";
-	springs = new Spring[numSprings];
+	springs = vector<Spring>(numSprings);
+    
 	cout << "made " << numSprings << " springs\n";	
 	
 				
@@ -1177,7 +1178,7 @@
     for(int i = 0; i < numSpokes; i++){
         lumpno = numSpokes*ringNo + 1 + i;
         springno = numSpokes*ringNo + i;
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
     }
     cout << "!!spider scan path created on ring " << ringNo << "\n";
      
@@ -1189,7 +1190,7 @@
     for(int i = 0; i < numRings-1; i++){
         lumpno = numSpokes*i; // zero, numspokes etc
         springno = springsinrings + numSpokes*i; // starts from total rings, increments by numspokes etc
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
     }    
 */
  }
@@ -1227,7 +1228,7 @@
 	
 	lumps = new Lump[numLumps];
 	cout << "made " << numLumps << " lumps\n";
-	springs = new Spring[numSprings];
+	springs = vector<Spring>(numSprings);
 	cout << "made " << numSprings << " springs\n";	
 	
     
@@ -1354,7 +1355,7 @@
          lumpno = numSpokes*ringNo + i;
          springno = numSpokes*ringNo + i;
          
-         scanPath->addElement(&lumps[lumpno], &springs[springno]);
+         scanPath.addElement(&lumps[lumpno], &springs[springno]);
      }
      cout << "HOLED spider scan path created on ring " << ringNo << "\n";
      
@@ -1366,7 +1367,7 @@
     for(int i = 0; i < numRings-1; i++){
         lumpno = numSpokes*i; // zero, numspokes etc
         springno = springsinrings + numSpokes*i; // starts from total rings, increments by numspokes etc
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
     } 
      */
 }
@@ -1404,7 +1405,7 @@
 	
 	lumps = new Lump[numLumps];
 	cout << "made " << numLumps << " lumps\n";
-	springs = new Spring[numSprings];
+	springs = vector<Spring>(numSprings);
 	cout << "made " << numSprings << " springs\n";	
 	
     
@@ -1554,7 +1555,7 @@
     for(int i = 0; i < numSpokes; i++){
         lumpno = numSpokes*ringNo + i;
         springno = numSpokes*ringNo + i;
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
     }
     cout << "SpiderCrossMesh spider scan path created on ring " << ringNo << "\n";
     
@@ -1566,7 +1567,7 @@
      for(int i = 0; i < numRings-1; i++){
      lumpno = numSpokes*i; // zero, numspokes etc
      springno = springsinrings + numSpokes*i; // starts from total rings, increments by numspokes etc
-     scanPath->addElement(&lumps[lumpno], &springs[springno]);
+     scanPath.addElement(&lumps[lumpno], &springs[springno]);
      } 
      */
 }
@@ -1602,7 +1603,7 @@
     numLumps = numSegments+1;
     numSprings = numSegments;
     lumps = new Lump[numLumps];
-	springs = new Spring[numSprings];
+	springs = vector<Spring>(numSprings);
     
 }
 //--------------------------------------------------------------
@@ -1650,7 +1651,7 @@
 	
 	cout << "LineMesh class makeDefaultScanPath\n";
     for(int i = 0; i < numSegments; i++){
-        scanPath->addElement(&lumps[i], &springs[i]);
+        scanPath.addElement(&lumps[i], &springs[i]);
     }
 }
 //--------------------------------------------------------------
@@ -1686,7 +1687,7 @@
     numLumps = (numSegments+1)*2;
     numSprings = numSegments*2;
     lumps = new Lump[numLumps];
-	springs = new Spring[numSprings];
+	springs = vector<Spring>(numSprings);
     
 }
 //--------------------------------------------------------------
@@ -1764,7 +1765,7 @@
 	
 	cout << "LineMesh class makeDefaultScanPath\n";
     for(int i = 0; i < numSegments; i++){
-        scanPath->addElement(&lumps[i], &springs[i]);
+        scanPath.addElement(&lumps[i], &springs[i]);
     }
 }
 //--------------------------------------------------------------
@@ -1805,8 +1806,10 @@
     numLumps = numSegments;
     numSprings = numSegments;
     lumps = new Lump[numLumps];
-	springs = new PressureSpring[numSprings];
+	//springs = new PressureSpring[numSprings];
     
+    //TODO PressureSpring  shoudl BE a spring ?
+    springs = vector<Spring>(numSprings);
 }
 //--------------------------------------------------------------
 void DropletMesh::setLumpPositions(){
@@ -1881,7 +1884,7 @@
 	
 	cout << "DropletMesh class makeDefaultScanPath\n";
     for(int i = 0; i < numSegments; i++){
-        scanPath->addElement(&lumps[i], &springs[i]);
+        scanPath.addElement(&lumps[i], &springs[i]);
     }
     cout << "DropletMesh class makeDefaultScanPath\n";
 }
@@ -1961,7 +1964,7 @@
 	
 	lumps = new Lump[numLumps];
 	cout << "made " << numLumps << " lumps\n";
-	springs = new Spring[numSprings];
+	springs = vector<Spring>(numSprings);
 	cout << "made " << numSprings << " springs\n";
 }
 
@@ -2098,7 +2101,7 @@
     int springno = vmarg * (width - 1) + hmarg;
     // do top horz
     for(int i = 0; i < width - 2*hmarg; i++){
-        scanPath->addElement(&lumps[lumpno], &springs[springno]);
+        scanPath.addElement(&lumps[lumpno], &springs[springno]);
         lumpno++;
         springno++;
     }
--- a/scanpath.h	Thu Nov 22 17:58:34 2012 +0000
+++ b/scanpath.h	Fri Dec 07 19:20:57 2012 +0000
@@ -12,7 +12,16 @@
 #include "lump.h"
 #include "spring.h"
 #include <vector>
-
+struct Element{
+    Lump * eLump;
+    Spring * eSpring;
+    Element(Lump * aLump, Spring * aSpring){
+        eLump = aLump;
+        eSpring = aSpring;
+        
+    };
+    
+};
 
 class ScanPath{
 private:
@@ -24,10 +33,9 @@
 
 	//Spring ** springPath;
 	// springpath - an array of pointers to the springs in the path
-	// lump path - ditto should be same number 
-	Lump ** lumpPath;
-    Spring ** springPath;
-    
+	// lump path - ditto should be same number
+    vector<Element> pathElements;
+
     double * wavetableNew;
     double * wavetableOld;
     double * wavetableUpdate;
@@ -48,7 +56,7 @@
     
 	ScanPath();
 	~ScanPath();
-    
+    void init();
     
 	void inscribe(double ax,double ay);
 	void clear();
@@ -67,7 +75,7 @@
     double getNextSampleCubic(double aPhasor);
     // redone stuff
     void initWavetables();
-    void addElement(Lump* aLump, Spring * aSpring);
+    void addElement(Lump* const aLump, Spring * const aSpring);
 
     void updateWavetables();
 
--- a/scanpath.mm	Thu Nov 22 17:58:34 2012 +0000
+++ b/scanpath.mm	Fri Dec 07 19:20:57 2012 +0000
@@ -10,30 +10,36 @@
 #include "scanpath.h"
 #include "testApp.h"
 
+ScanPath scanPath;
+
+// audio thread does not touch mesh lumps/springs
 //----------------------------------------------------------------
-ScanPath::ScanPath() : maxElements(2048) {
+ScanPath::ScanPath() : maxElements(10000) {
 	// construct an empty scanpath
 	// an ' element ' consists of 1 lump and one spring
 
     
 	numElements = 0; // numElements is a dynamic running count of elements when building - a bit crap
-    springPath = new Spring*[maxElements];
-    lumpPath = new Lump*[maxElements];
+ 
     
-    framesPerSample = ofGetFrameRate()/SAMPLE_RATE;
     frameInterpolator = 0.0;
     currentLength = 0.0;
     restLength = 1.0; // ?
 
     scanMode = DISPLACEMENT;
+    initWavetables();
+    framesPerSample = 60.0/SAMPLE_RATE;
+
+}
+void ScanPath::init(){
+    framesPerSample = ofGetFrameRate()/SAMPLE_RATE;
     
-    initWavetables();
-
 }
 //----------------------------------------------------------------
 ScanPath::~ScanPath(){
-	delete [] springPath;
-    delete [] lumpPath;
+    
+    //VECTORISE
+
     delete [] wavetableNew;
     delete [] wavetableOld;
     delete [] wavetableUpdate;
@@ -41,14 +47,17 @@
 }
 
 void ScanPath::clear(){
-    for(int i = 0; i<numElements; i++){
-        springPath[i]->removeFromScanPath();
-        lumpPath[i]->removeFromScanPath();
+    //VECTORISE
+    for(vector<Element>::iterator elitr = pathElements.begin(); elitr < pathElements.end(); elitr++){
+        (*elitr).eLump->removeFromScanPath();
+        (*elitr).eSpring->removeFromScanPath();
     }
+    pathElements.clear();
     numElements = 0;
-    // cant work?
+
 }
 int ScanPath::howManyElements(){
+    //return pathElements.size();
     return numElements;
 }
 //----------------------------------------------------------------
@@ -57,6 +66,8 @@
 	// if we're further away from current lump
 	
 	// check end points of connecting springs, pick closest
+    
+    // NOW IN MESH
 }
 void ScanPath::draw(){
     // draw the actual waveform in the corner
@@ -103,8 +114,10 @@
 double ScanPath::getTotalLength(){
     // for interesting modulations...
     currentLength = 0.0;
-    for(int i = 0; i < numElements; i++){
-        currentLength += springPath[i]->getLength();
+
+    for(vector<Element>::iterator elitr = pathElements.begin(); elitr < pathElements.end(); elitr++){
+
+        currentLength = (*elitr).eSpring->getLength();
     }
     return currentLength;
     
@@ -123,19 +136,23 @@
     
 }
 //----------------------------------------------------------------
-void ScanPath::addElement(Lump* aLump, Spring * aSpring){
+void ScanPath::addElement(Lump* const aLump, Spring * const aSpring){
     // insert ptr to the lump and spring into array
-    if(numElements >= maxElements){
-        cerr << "cannot add any more to scanpath - max elements: 2048\n";
+
+    
+    if(numElements + 1 >= maxElements){
+        cout << " max elements reached!\n";
         return;
     }
-    lumpPath[numElements] = aLump;
-    springPath[numElements] = aSpring;
+
+    pathElements.push_back(Element(aLump,aSpring));
     
+    // maybe do this in mesh?
     aLump->addToScanPath();
     aSpring->addToScanPath();
+    numElements++;
     
-    numElements++;
+
 }
 //----------------------------------------------------------------
 void ScanPath::updateWavetables(){
@@ -143,29 +160,48 @@
     double * temp;
 
     // 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";
+    updateAccessing = true;
+     
+     */
     switch(scanMode){
         case DISPLACEMENT:
             // now fill with new values
             for(int i = 0; i < numElements; i++){
-
-                wavetableUpdate[i] = lumpPath[i]->scanRadialDisplacement()/1.5;
+                // double check
+                if(pathElements[i].eLump->isInScanPath){
+                    wavetableUpdate[i] = pathElements[i].eLump->scanRadialDisplacement()/1.5;
+                }
                 
             }
             break;
         case SPEED:
             for(int i = 0; i < numElements; i++){
-                wavetableUpdate[i] = lumpPath[i]->scanLumpSpeed();
+                if(pathElements[i].eLump->isInScanPath){
+                    wavetableUpdate[i] = pathElements[i].eLump->scanLumpSpeed();
+                }
             }
             break;
         case SPRING_FORCE:
             for(int i = 0; i < numElements; i++){
-                wavetableUpdate[i] = springPath[i]->getForceMag();
+                if(pathElements[i].eSpring->isInScanPath){
+                    wavetableUpdate[i] = pathElements[i].eSpring->getForceMag();
+                }
             }
             break;
         case YPOS:
             for(int i = 0; i < numElements; i++){
-                wavetableUpdate[i] = lumpPath[i]->scanYPos();
+                if(pathElements[i].eLump->isInScanPath){
+                    wavetableUpdate[i] = pathElements[i].eLump->scanYPos();
+                }
             }
             break;
         default:
@@ -174,17 +210,15 @@
           
     }
     
-    // END THREAD MUTEX
+    
+    
  
-    // reset the interp between frames
-    if(audioAccessing){
-        cout << "buffers swapped while update!\n";
-    }
-    updateAccessing = true;   
     temp = wavetableOld;
     wavetableOld = wavetableNew;
     wavetableNew = wavetableUpdate;
     wavetableUpdate = temp;
+    
+    // END THREAD MUTEX
     updateAccessing = false;
     
     frameInterpolator = 0.0;
@@ -192,7 +226,11 @@
     
 }
 //----------------------------------------------------------------
+//----------------------------------------------------------------
+// AUDIO THREAD STUFF
+//----------------------------------------------------------------
 // get next sample
+
 double ScanPath::getNextSample(double aPhasor){
 	// move along path, interpolating between points
     // move between frames too
@@ -207,12 +245,17 @@
 
     double frac = alongPath - double(n0);
     
+    // TODO THRED MUTEX HERE!??
+    // this is called from audio thread
+    /*
+    int i = 0;
+    while(updateAccessing){
+        i++;
+    }
     audioAccessing = true;
-    if (updateAccessing){
-        cout << "update is accessing while audio is\n";
-    }
-    
-    // TODO THRED MUTEX HERE!??
+    cout << "update is accessing while audio is" << i << "times\n";
+     
+     */
     
     double oldsample = (1 - frac) * wavetableOld[n0] + frac * wavetableOld[n1];
     
--- a/spring.h	Thu Nov 22 17:58:34 2012 +0000
+++ b/spring.h	Fri Dec 07 19:20:57 2012 +0000
@@ -37,10 +37,11 @@
 	Lump * startLumpPtr;
 	Lump * endLumpPtr;
 	
-	bool isInScanPath;
+	
 
 	
 public:
+    bool isInScanPath;
 	static bool forcesOn;
 	
 	Spring();
--- a/spring.mm	Thu Nov 22 17:58:34 2012 +0000
+++ b/spring.mm	Fri Dec 07 19:20:57 2012 +0000
@@ -24,13 +24,11 @@
 //---------------------------------------------------------------
 Spring::Spring(){
 	//cout << "constructing a default spring" << endl;
+
+    isInScanPath = false;
 	
-	startPoint.setCoord(0.0,0.0); // dummy values - shouldn't get used
-	endPoint.setCoord(0.0,0.0);
-	
-	springConst = 0.3; // not needed - assume it's one and use 1/c^2 for mass
-    
-	isInScanPath = false;
+	springConst = 0.3; 
+
 	
 	startLumpPtr = 0;
 	endLumpPtr = 0;
--- a/testApp.mm	Thu Nov 22 17:58:34 2012 +0000
+++ b/testApp.mm	Fri Dec 07 19:20:57 2012 +0000
@@ -2,6 +2,7 @@
 
 extern GlobalForces globalForces;
 extern GlobalUI globalUI;
+extern ScanPath scanPath;
 //--------------------------------------------------------------
 void testApp::setup(){	
     theMesh = NULL;
@@ -34,7 +35,7 @@
     
     // if we want things to start immediately
     paused = false;
-    audioOn = true;
+    //audioOn = true;
 	drawingOn = true;
     audioAccessFlag = false;
     meshConstructionFlag = false;
@@ -42,7 +43,10 @@
     
     
     setupMesh();
-
+    scanPath.init();
+    
+    globalUI.touchMode = globalUI.GRAB;
+    scanPath.scanMode = scanPath.DISPLACEMENT;
 }
 
 void testApp::setupMesh(){
@@ -50,8 +54,10 @@
     if(audioAccessFlag) return; // or rather wait !
     meshConstructionFlag = TRUE;
     static int type = 0;
-    int numTypes = 5;
-    delete theMesh;
+    int numTypes = 6;
+    
+    // TODO: Just empty vectors
+
     
     if (type % numTypes == 0){
     // different for iphone
@@ -70,10 +76,13 @@
         theMesh = new SpiderCrossMesh(6,6);
     }else if(type % numTypes ==3){
         
-        theMesh = new TriangleMesh(12,12);
+        theMesh = new TriangleMesh(34,34);
     }else if(type % numTypes ==4){
 
         theMesh = new SquareCrossMesh(28,28);
+    }else if(type % numTypes ==5){
+        
+        theMesh = new DropletMesh(128);
     }
     type++;
     //theMesh = new LineMesh(123);
@@ -86,10 +95,14 @@
     theMesh->setMass(1);
     theMesh->setFriction(0.99991);
     
-    globalUI.touchMode = globalUI.GRAB;
-    theMesh->scanPath->scanMode = theMesh->scanPath->DISPLACEMENT;
+
     numTouches = 0;
     meshConstructionFlag = FALSE;
+    ofSoundStreamStart();
+    drawingOn = true;
+    paused = false;
+    audioOn = true;
+    cout << "MESH REGENERATED\n";
 }
 //--------------------------------------------------------------
 //--------------------------------------------------------------
@@ -267,9 +280,15 @@
         numTouches = 0;
     }else if(e.widget->getName() == "NEW"){
         // ????
+        
         cout << ((ofxUIButton *)e.widget)->getValue();
         
-        if( ((ofxUIButton *)e.widget)->getValue() == 1) setupMesh();
+        if( ((ofxUIButton *)e.widget)->getValue() == 1) {
+            cout << "new down";
+            deleteMesh();
+            
+        }
+        if( ((ofxUIButton *)e.widget)->getValue() == 0) setupMesh();
     }else if(e.widget->getName() == "PAUSE"){
         paused = !paused;
     }else if(e.widget->getName() == "TOUCH AMT"){
@@ -338,6 +357,7 @@
     saveLogXML();
     
     if(theMesh != NULL){
+        scanPath.clear();
         delete theMesh;
     }
     
@@ -403,7 +423,7 @@
     audioOn = false;
     drawingOn = false;
     
-    
+    scanPath.clear();
     // trash it
     delete theMesh;
     theMesh = NULL;
@@ -792,17 +812,17 @@
     static double phasor;
     static double sinePhasor;
     if(meshConstructionFlag) return;
-    
+    // if no mesh , scanpath will still be there, but empty
 
     phasorIncr = pitch/SAMPLE_RATE;
 
-	if(audioOn && theMesh != NULL){
+	if(audioOn){
         audioAccessFlag = TRUE;
 		for (int i = 0; i < bufferSize; i++){
             sinePhasor = sin(2*PI*phasor);
 
-			sample  = float(theMesh->scanPath->getNextSample(phasor));
-            //sample  = float(theMesh->scanPath->getNextSample());
+			sample  = float(scanPath.getNextSample(phasor));
+            //sample  = float(scanPath.getNextSample());
             // BASIC STRING sample = float(basicString->getNextSample(phasor));
             
             // hipass to get rid of DC
@@ -873,6 +893,7 @@
                 }else{
                     dim2 = 3;
                 }
+                
                 regenerateMesh(meshType,dim1,dim2);
 
                 
@@ -1014,17 +1035,17 @@
             // set scan mode
             if (m.getArgAsString(0) == "DISPLACEMENT"){
                 cout << "touchMode = DISPLACEMENT" << endl;
-                theMesh->scanPath->scanMode = theMesh->scanPath->DISPLACEMENT;
+                scanPath.scanMode = scanPath.DISPLACEMENT;
             }else if (m.getArgAsString(0) == "SPEED"){
                 
                 cout << "scanMode = SPEED" << endl;
-                theMesh->scanPath->scanMode = theMesh->scanPath->SPEED;
+                scanPath.scanMode = scanPath.SPEED;
             }else if (m.getArgAsString(0) == "SPRING_FORCE"){
                 cout << "scanMode = SPRING_FORCE" << endl;
-                theMesh->scanPath->scanMode = theMesh->scanPath->SPRING_FORCE;
+                scanPath.scanMode = scanPath.SPRING_FORCE;
             }else if (m.getArgAsString(0) == "YPOS"){
                 cout << "scanMode = YPOS" << endl;
-                theMesh->scanPath->scanMode = theMesh->scanPath->YPOS;
+                scanPath.scanMode = scanPath.YPOS;
             }else if (m.getArgAsString(0) == "OTHER"){
                 cout << "scanMode = OTHER" << endl;
                 //scanMode = OTHER;