//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
    
    // resetting lastX and lastY so line will separate when unclick the mouse
    if ( !bIsDrawing ) {
        lastX = mouseX;
        lastY = mouseY;
        bIsDrawing = true;
    }
    
    // change local pen color and notify remote
    if ( ofDistSquared(mouseX, mouseY, 50, 450) < 225 ) {
        penColor.set(0, 0, 0); // black
        spacebrew.sendString("color", "black");
    } else if ( ofDistSquared(mouseX, mouseY, 100, 450) < 225 ) {
        penColor.set(255, 0, 0); // red
        spacebrew.sendString("color", "red");
    } else if ( ofDistSquared(mouseX, mouseY, 150, 450) < 225 ) {
        penColor.set(0, 255, 0); // green
        spacebrew.sendString("color", "green");
    } else if ( ofDistSquared(mouseX, mouseY, 200, 450) < 225 ) {
        penColor.set(0, 0, 255); // blue
        spacebrew.sendString("color", "blue");
    }
    
    if ( ofDistSquared(mouseX, mouseY, 400, 450) < 1600 ) {
        bSaveRequestSent = true;
        spacebrew.sendBoolean("button", true);
    }
}
//---------------------------------------------------------------------
void ofxSimpleGuiQuadWarp::onPress(int x, int y, int button) {
	curPoint = NULL;
	for(int i=0; i<4; i++) {
		if(ofDistSquared(x - this->x, y - this->y, pts[i].x, pts[i].y) < MOUSE_DISTANCE * MOUSE_DISTANCE) {
			curPoint = pts + i;
		}
	}
	
	// if doubleclick, reset
	if(ofDistSquared(x, y, lastPressPlace.x, lastPressPlace.y) < MOUSE_DISTANCE * MOUSE_DISTANCE && ofGetElapsedTimef() - lastPressTime < 0.25f) {
//		pts[0].set(0, 0);
//		pts[1].set(width, 0);
//		pts[2].set(width, height);
//		pts[3].set(0, height);
		if(curPoint) {
			switch((int)curPoint->z) {		// stores index of point 
				case 0: pts[0].set(0, 0); break;
				case 1:	pts[1].set(width, 0); break;
				case 2:	pts[2].set(width, height); break;
				case 3:	pts[3].set(0, height); break;
			}			
		}
	}
	
	lastPressPlace.set(x, y);
	lastPressTime = ofGetElapsedTimef();
}
ofPoint ArmContourFinder::refitTip(int n)
{	
	unsigned int l = getLabel(n);
	ofPoint midWrist = ofPoint( (wrists[l][0].x + wrists[l][1].x)/2, (wrists[l][0].y + wrists[l][1].y)/2 );

	unsigned int start, end;
	polylines[n].getClosestPoint(wrists[l][1], &start);
	polylines[n].getClosestPoint(wrists[l][0], &end);

	ofPoint newTip;
	float maxDist = 0;
	int i = start;
	while( i != end ) {
		float dist = ofDistSquared(midWrist.x, midWrist.y, polylines[n][i].x, polylines[n][i].y);
		if(dist > maxDist) {
			maxDist = dist;
			newTip = polylines[n][i];
		}
		i++;
		if( i == polylines[n].size() )
			i = 0;
	}

	ofPoint closestTip = polylines[n].getClosestPoint(tips[getLabel(n)]);
	float dist = ofDistSquared(closestTip.x, closestTip.y, newTip.x, newTip.y);
	if(dist < 100) { //TODO change magic number
		return closestTip;
	}
	
	return newTip;
}
Example #4
0
void GLOW::Creature::FindCreatures(GLOW::PixelGrid* PG,vector<Creature> & Creatures){
	this->Kleur=ofColor(255,255,255);
	for (vector<NearCreatureInfo*>::iterator pObj = NearCreatures.begin();pObj != NearCreatures.end(); ++pObj) {
      delete *pObj;
	}
	this->NearCreatures.clear();
	NearCreatureInfo* Nci = NULL;	
	float distance ;
	for(int i=0;i<Creatures.size();i++){	
		distance=ofDistSquared(this->x,this->y,Creatures[i].x,Creatures[i].y);
		if(distance<3 && distance>-3){
			Nci = new NearCreatureInfo();
			Nci->ID=Creatures[i].ID;
			Nci->DistanceToCreature=distance;
			this->NearCreatures.push_back(Nci);	
		}
	}
	if(this->NearCreatures.size()>1){
		this->Kleur=ofColor::green;
	}
	if(this->NearCreatures.size()>2){
	this->Kleur=ofColor::yellow;
	}
	if(this->NearCreatures.size()>3){
		this->Kleur=ofColor(255,0,0);		
	}
	
}
Example #5
0
void effectFive::builtNextBranch(){
printf("on built next branche!!!%i",indexLine);
	if(indexLine<lines.size()){
	
		int posx1 = lines[indexLine].x1;
		int posy1 = lines[indexLine].y1;
		int posx2 = lines[indexLine].x2;
		int posy2 = lines[indexLine].y2;
		float dist = sqrt(ofDistSquared(posx1, posy1, posx2, posy2 ));
		//branches.clear();
		int nbBranch = int(dist/20);
		if (indexBranch<nbBranch) {
			branch tempBranch;
			tempBranch.create((dist/20)*indexBranch,noise);
			branches.push_back(tempBranch);
			indexBranch++;
		}else {
			indexBranch = 0;
			indexLine++;
		}

		

		
	}
	


}
Example #6
0
void Boid::update(float _max){

    af = MIN(MAX(-1000.0f/MIN(ofDistSquared(x, y, 0, 0),5.0f),-_max),_max);
    angle = atan2(y,x);
    ay = sin(angle)*af;
    ax = cos(angle)*af;
    aangle =atan2(ay,ax);
    
    
    vx += ax*0.9f;
    vy += ay*0.9f;
    
    //vx = MAX(MIN(vx,5.0f),-5.0f);
    //vy = MAX(MIN(vy,5.0f),-5.0f);
    
    vangle = atan2(vy,vx);
    
    vf = sqrt(vx*vx+vy*vy);
    /*
    float maxvf = 3.0f;
    if(vf>maxvf){
        float divf = maxvf/vf;
        vx*=divf;
        vy*=divf;
    }
    */
    x+= vx*0.9f;
    y+= vy*0.9f;

}
Example #7
0
void HexagonParticle::separation(vector<HexagonParticle*>* connectedParticles)
{
	ofVec2f steerSep = ofVec2f(0.f, 0.f);

	int count = 0;
	for (vector<HexagonParticle*>::iterator c = connectedParticles->begin(); c != connectedParticles->end(); ++c){
		float distSqrd = ofDistSquared((*c)->pos.x, (*c)->pos.y, pos.x, pos.y);

		if (distSqrd < s_neighDistSqrd){
		
			if (distSqrd < s_desiredSepSqrd && distSqrd > 0){
				steerSep += (pos - (*c)->pos);
			}
		
			++count;
		}

		if (count > 0){
		
			steerSep /= (float)count;
			//steerSep = steerSep.normalize() * maxSpeed;
			//steerSep -= vel;
			steerSep.limit(maxForce*0.2);
			accel += steerSep;
			//accel.limit(maxSpeed);
		}
	}

}
void testApp::newParticleField(int res){
    int particleResolution = res;
    float randomScatter = 30;
    
    randomPerl = ofRandom(1000);
    
    float perlinScale = 0.005;
    
    for(int y = 0; y < ofGetWindowHeight(); y += particleResolution){
        for(int x = ofGetWindowWidth()/2 - ofGetWindowHeight()/2; x < ofGetWindowWidth()/2 + ofGetWindowHeight()/2; x += particleResolution){
            
            //calculate point distance from attractor
            float distSq = ofDistSquared(x, y, attractorPos.x, attractorPos.y);
            
            //create new points only if outside attractor
            if(distSq > (attractorSize + attractionRad) * (attractorSize + attractionRad)){
                float noiseVal = ofNoise(x * perlinScale + randomPerl, y * perlinScale + randomPerl);
                
                if(noiseVal > 0.5){
                    Particle p;
                    p.pos.set(x + ofRandom(-randomScatter, randomScatter), y + ofRandom(-randomScatter, randomScatter));
                    
                    p.setup(&pImg);
                    
                    //add to x pos to center them in window
//                    p.pos.x += ofGetWindowWidth()/2 - ofGetWindowHeight()/2;
                    pList.push_back(p);
                }
            }
        }
    }
}
Example #9
0
void tmGuiQuadWarp::mousePressed(ofMouseEventArgs &e) {
	curPoint = NULL;
	for(int i=0; i<4; i++) {
		if(ofDistSquared(e.x - this->x, e.y - this->y, points[i].x, points[i].y) < MOUSE_DISTANCE * MOUSE_DISTANCE) {
			curPoint = points + i;
		}
	}
}
Example #10
0
//--------------------------------------------------------------
float MyString::minimum_distance_Squared(ofVec2f v, ofVec2f w, ofVec2f p) {


    // Return minimum distance between line segment vw and point p
    const float l2 = ofDistSquared(v.x, v.y, w.x, w.y);  // i.e. |w-v|^2 -  avoid a sqrt
    if (l2 == 0.0) return ofDistSquared(p.x, p.y, v.x, v.y);   // v == w case
    // Consider the line extending the segment, parameterized as v + t (w - v).
    // We find projection of point p onto the line.
    // It falls where t = [(p-v) . (w-v)] / |w-v|^2
    const float t =(p - v).dot(w - v) / l2;

    if (t < 0.0) return ofDistSquared(p.x, p.y, v.x, v.y);       // Beyond the 'v' end of the segment
    else if (t > 1.0) return ofDistSquared(p.x, p.y, w.x, w.y);  // Beyond the 'w' end of the segment
    const ofVec2f projection = v + t * (w - v);  // Projection falls on the segment
    return ofDistSquared(p.x, p.y, projection.x, projection.y);


}
Example #11
0
//---------------------------------------------------------------------
void ofxSimpleGuiQuadWarp::onPress(int x, int y, int button) {
	curPoint = NULL;
	for(int i=0; i<4; i++) {
		if(ofDistSquared(x - this->x, y - this->y, pts[i].x, pts[i].y) < MOUSE_DISTANCE * MOUSE_DISTANCE) {
			curPoint = pts + i;
		}
	}
	
	if(ofDistSquared(x, y, lastPressPlace.x, lastPressPlace.y) < MOUSE_DISTANCE * MOUSE_DISTANCE && ofGetElapsedTimef() - lastPressTime < 0.5f) {
		pts[0].set(0, 0);
		pts[1].set(width, 0);
		pts[2].set(width, height);
		pts[3].set(0, height);
	}
	
	lastPressPlace.set(x, y);
	lastPressTime = ofGetElapsedTimef();
}
Example #12
0
void BirdParticle::update(vector<BirdParticle*>* angryParticles, float timeScale)
{
	ofVec2f steerVecSep = ofVec2f(0.f, 0.f);
	ofVec2f sumCoh = ofVec2f(0.f, 0.f);
	ofVec2f sumAlign = ofVec2f(0.f, 0.f);

	int count = 0;
	BirdParticle* angry;
	for (vector<BirdParticle*>::iterator p = angryParticles->begin(); p != angryParticles->end(); ++p)
	{
		angry = (*p);
		//look for close particles
		float distSqrd = ofDistSquared(angry->pos.x, angry->pos.y, pos.x, pos.y);
		if (distSqrd < s_neighbourDistSqrd)
		{
			//do flocking calculations
			//seperation
			if (distSqrd < s_desiredSeperationSqrd && distSqrd > 0)
			{
				steerVecSep += ((pos - angry->pos).normalize() / sqrt(distSqrd));
			}

			//cohesion
			sumCoh += angry->pos;

			//alignment
			sumAlign += angry->vel;

			++count;
		}
	}

	if (count > 0)
	{
		//seperation
		steerVecSep /= (float)count;
		steerVecSep = steerVecSep.normalize() * maxSpeed;
		steerVecSep -= vel;
		steerVecSep.limit(maxForce * 0.2f);

		//cohesion
		sumCoh /= (float)count;
		Particle::seek(sumCoh, 0.1f);

		//alingnment
		sumAlign /= (float)count;
		sumAlign.normalize();

		//add them all
		accel += steerVecSep + sumAlign;
		accel.limit(maxSpeed * (0.1f + 0.9f * homeDistRatio));
	}

	Particle::update(timeScale);
}
Example #13
0
//--------------------------------------------------------------------------------------------
//make sure our colleciton of towers matches up with cups in the real world
void Scene::checkCups(){
    //first, mark each tower as having not yet been checked this frame
    for (int i=0; i<towers.size(); i++){
        towers[i]->hasBeenCheckedThisFrame = false;
    }
    
    //go through the list of real world cups
    for (int i=0; i<cupTracker->activeCups.size(); i++){
        CupInfo thisCup = cupTracker->activeCups[i];
        
        bool foundTower = false;
        
        //check if there is a coresponding tower
        for (int k=0; k<towers.size(); k++){
            if (towers[k]->uniqueID == thisCup.uniqueID){
                //store the old values
                float prevAngle = towers[k]->targetAngle;
                ofVec2f prevPos = towers[k]->pos;
                
                //update
                towers[k]->setFromCupInfo(thisCup);
                towers[k]->hasBeenCheckedThisFrame = true;    //mark that we checked it so it doesn't get killed
                foundTower = true;                          //and mark that this cup is accounted for
                
                //check if it moved enough to not be considered idle
                if ( ofDistSquared(prevPos.x, prevPos.y, towers[k]->pos.x, towers[k]->pos.y) > idleTowerMoveThreshold*idleTowerMoveThreshold){
                    //cout<<"big move"<<endl;
                    idleTimer = 0;
                }
                if ( abs(prevAngle-towers[k]->targetAngle) > idleTowerRotateThreshold){
                    //cout<<"big angle"<<endl;
                    idleTimer = 0;
                }
                
                break;
            }
        }
        
        //if no tower was found, it is a new cup, and we need a tower for it!
        if ( !foundTower ){
            idleTimer = 0;
            addTower(thisCup);
        }
        
    }
    
    //now that we've gone through all cups, go through and remove any towers not accounted for
    for (int i=towers.size()-1; i>=0; i--){
        if ( !towers[i]->hasBeenCheckedThisFrame && !towers[i]->ignoreAutoRemove){
            idleTimer = 0;
            removeTower(i);
        }
    }
    
}
bool ArmContourFinder::findHand(int n) {

	unsigned int l = getLabel(n);
	//First, find ends
	ends[l] = findEnd(n);
	if( ends[l].x == -1 and ends[l].y == -1)
		return false;

	//Now, get the tip
	tips[l] = findTip(n);
	//See if it's far enough away
	float d1 = ofDistSquared(tips[l].x, tips[l].y, ends[l].x, ends[l].y);
	float d2 = ofDistSquared(tips[l].x, tips[l].y, ends[l].x, ends[l].y);
	if( d1 < MIN_HAND_SIZE * MIN_HAND_SIZE and d2 < MIN_HAND_SIZE * MIN_HAND_SIZE )
		return false; // Too small!
	// if( d1 > MAX_HAND_SIZE * MAX_HAND_SIZE and d2 > MAX_HAND_SIZE * MAX_HAND_SIZE )
	// 	return false; // Too big!

	//Now find the wrists
	wrists[l] = findWrists(n);
	if( wrists[l].size() != 2 ) return false;

	tips[l] = refitTip(n);

	d1 = ofDistSquared(wrists[l][0].x, wrists[l][0].y, tips[l].x, tips[l].y);
	d2 = ofDistSquared(wrists[l][1].x, wrists[l][1].y, tips[l].x, tips[l].y);
	if(d1 <= MIN_HAND_SIZE * MIN_HAND_SIZE or d1 >= MAX_HAND_SIZE * MAX_HAND_SIZE 
		or d2 <= MIN_HAND_SIZE * MIN_HAND_SIZE or d2 >= MAX_HAND_SIZE * MAX_HAND_SIZE) {
		wrists[l] = findWrists(n);
		if(wrists[l].size() != 2)
			return false;
	}

	return true;

}
vector< ofPoint > utility::findClosestPoints(ofPolyline one, ofPolyline two)
{
	float shortestDist = INFINITY;
	vector< ofPoint > closest;
	closest.resize(2);
	for (int i = 0; i < one.size(); ++i)
	{
		for (int j = i; j < two.size(); ++j)
		{
			float dsquared = ofDistSquared(one[i].x, one[i].y, two[j].x, two[j].y);
			if(dsquared <= shortestDist) {
				closest[0] = one[i];
				closest[1] = two[j];
			}
		}
	}

	return closest;
}
float Timeline::mousePressed(int x, int y, int button){
    if (x > offset.x-nodeCircleSize && x < offset.x+drawW+nodeCircleSize && y > offset.y && y < offset.y+drawH){
        mouseStartedInside = true;
        
        //did they click a node?
        for (int i=0; i<nodes.size(); i++){
            float nodePrc = nodes[i].time/maxTime;
            float nodeX = offset.x+drawW*nodePrc;
            if ( ofDistSquared(x, y, nodeX, offset.y+drawH/2) < nodeCircleSize*nodeCircleSize){
                selectedNode = i;
                nodeBeingDragged = i!=0 && i!=nodes.size()-1;   //no dragging the first or last node
                dragFrameTimer = 0;
                return nodes[i].time;   //set the playhead to this position
            }
        }
        
    }else{
        mouseStartedInside = false;
    }
    
    return mouseDragged(x, y, button);
}
ofPoint ArmContourFinder::findTip(int n) {

	//Create a line connecting the center of the base of the arm to the center of the bounding box
	ofPoint boxCenter = ofxCv::toOf(getCenter(n));
	unsigned int l = getLabel(n);

	float xn = 3*boxCenter.x - 2*ends[l].x;
	float yn = 3*boxCenter.y - 2*ends[l].y;
	
	ofPoint mark = ofPoint(xn, yn);

	ofPoint newTip = polylines[n].getClosestPoint(mark);

	//If our old tip is still good, keep it
	ofPoint closestTip = polylines[n].getClosestPoint(tips[getLabel(n)]);
	float dist = ofDistSquared(closestTip.x, closestTip.y, newTip.x, newTip.y);
	if(dist < 100) { //TODO change magic number
		return closestTip;
	}

	return newTip;

}
Example #18
0
void LightScene::Render()
{
	ofClear(0);

	//draw lights for background
	ofSetColor(255);
	ofSetRectMode(OF_RECTMODE_CORNER);
	ofEnableBlendMode(OF_BLENDMODE_ADD);
	for(vector<Light>::iterator l = m_lights.begin(); l != m_lights.end(); ++l){
		if(l->isOn == true){
			m_lightImg.draw(l->x - m_lightImg.width * 0.5, 0, 0);
		}
	}
	ofDisableBlendMode();

	ofEnableBlendMode(OF_BLENDMODE_MULTIPLY);
	m_backgroundImg.draw(0, 0);
	ofDisableBlendMode();

	ofEnableAlphaBlending();

	//draw light tubes
	for (vector<Light>::iterator l = m_lights.begin(); l != m_lights.end(); ++l){
		if (l->isOn == true){
			m_lightTube.draw(l->x - m_lightImg.width * 0.5, 0, 0);
		}
	}


	HexagonParticle* hp;
	//draw a hexagon for each person in people
	for(vector<Particle*>::iterator p = pPeople->begin(); p != pPeople->end(); ++p)
	{
		hp = (HexagonParticle*) (*p);
		ofPushMatrix();
			ofTranslate(hp->pos);
			ofSetRectMode(OF_RECTMODE_CENTER);
			ofRotate(hp->hexRotation);
			ofScale(hp->hexSize, hp->hexSize);
			ofSetColor(hp->hexColor);
			m_hexImgInner.draw(0,0);

			ofSetColor(255);
			m_hexImgBorder.draw(0,0);
		ofPopMatrix();
		
		ofSetRectMode(OF_RECTMODE_CORNER);

		float distSqrd = std::numeric_limits<float>::max();

		for(vector<Particle*>::iterator hands = pHandPositions->begin(); hands != pHandPositions->end(); ++hands){
			
			distSqrd = ofDistSquared( (*hands)->pos.x, (*hands)->pos.y, (*p)->pos.x, (*p)->pos.y);
			
			hp->hexToHands = (*hands)->pos - hp->pos;

			float offsetMap = ofMap(distSqrd, 22500, 90000, 0, 1);
			ofClamp(offsetMap, 0,1);
			ofVec2f offsetAccel = hp->hexToHands*offsetMap;	

			if (distSqrd < 250000){				
			
				closestHand.push_back((*hands));	

				//hp->seek((*hands)->pos, 1.f, true);
				
				m_connectedParticles.push_back(hp);	
				//hp->separation(&m_connectedParticles);

				if(distSqrd > 90000){
					//hp->seek((*p)->pos, 1.0, true);
					hp->accel += offsetAccel;
				}
				else if(distSqrd < 90000){
					hp->accel -= offsetAccel*0.0025;
				}
				m_connectedParticles.clear();

			}
		}

		ofNoFill();
		ofSetColor(255, 255);
		ofSetLineWidth(6);

		if(closestHand.size() != 0){
			for(vector<Particle*>::iterator connectedhands = closestHand.begin(); connectedhands != closestHand.end(); ++connectedhands){

				//draws line from hands to connected particle
				ofLine( (*connectedhands)->pos.x, (*connectedhands)->pos.y, hp->pos.x, hp->pos.y);
				
				//increments the alpha and the size
				hp->hexAlpha += 5;
				if(hp->hexAlpha > 200){
					hp->hexAlpha = 200;
				}
				
				hp->hexSize += hp->hexGrowthRate;
				if (hp->hexSize  > 1){  
					hp->hexSize = 1; 
				}
			}
			closestHand.clear();
		}
		else
		{
			//decrements the alpha and the size
			hp->hexAlpha -= 5;
			if(hp->hexAlpha < 0){
				hp->hexAlpha = 0;
			}	

			hp->hexSize -= hp->hexGrowthRate;
				if (hp->hexSize  < 0.3){  
					hp->hexSize = 0.3; 
				}
			m_connectedToHands = false;
		}
		hp->hexColor.a = hp->hexAlpha;	

	}

	//FOG 
	ofSetColor(255);
	ofSetRectMode(OF_RECTMODE_CORNER);

	m_fogAlphaMask.begin();
	//ofEnableAlphaBlending();
	ofClear(0, 0);
	int dif = m_lightAlpha.width * 0.5;
	for (vector<Light>::iterator l = m_lights.begin(); l != m_lights.end(); ++l){
		if (l->isOn == true){
			m_lightAlpha.draw(l->x - dif, 0, 0);
		}
	}
	m_fogAlphaMask.end();

	//draw fog with shader
	m_fogShader.begin();

		m_fogImg.getTextureReference().bind();
		m_fogShader.setUniformTexture("imageMask", m_fogAlphaMask.getTextureReference(), 1);
		m_fogVbo.bind();

		glDrawArrays(GL_QUADS, 0, 4);

		m_fogImg.getTextureReference().unbind();
		m_fogVbo.unbind();

	m_fogShader.end();
	
	ofSetRectMode(OF_RECTMODE_CENTER);
	for (vector<Particle*>::iterator hands = pHandPositions->begin(); hands != pHandPositions->end(); ++hands){
		ofPushMatrix();
		ofTranslate((*hands)->pos);
		m_handsImage.draw(0,0);
		ofPopMatrix();
	}
	ofSetRectMode(OF_RECTMODE_CORNER);
}
Example #19
0
void testApp::houghCircles( ofxCvGrayscaleImage sourceImg) {
	IplImage* gray = sourceImg.getCvImage();	
	cvSmooth( gray, gray, CV_GAUSSIAN, 5, 5 ); // smooth it, otherwise a lot of false circles may be detected
	CvMemStorage* storage = cvCreateMemStorage(0);	
	circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 2, gray->width/8, 300, 200 );
	circCount = circles->total;
    
	// find positions of CV circles
	
	for (int i = 0; i < circles->total; i++) 
	{
		float* p = (float*)cvGetSeqElem( circles, i );
		ofPoint pos;
		pos.x = cvPoint( cvRound(p[0]),cvRound(p[1]) ).x;  
		pos.y = cvPoint( cvRound(p[0]),cvRound(p[1]) ).y;
		pos.x = (int)pos.x;
		pos.y = (int)pos.y;
		int radius = cvRound( p[2] );
		
		bool cFound = false;
		
		for (int circs = 0; circs < myCircles.size(); circs++) 
		{
			ofPoint& posCirc = myCircles[circs].pos;
			float dist = ofDistSquared(pos.x, pos.y, posCirc.x, posCirc.y);
			//cout << "distance is: " << dist << endl;
			
			// check to see if there is a circle near an existing tracked circle
			if ( dist < 1000 ) 
			{
				myCircles[circs].lastSeen = ofGetFrameNum();
				myCircles[circs].radius = radius;
				myCircles[circs].pos = pos;
				myCircles[circs].isAlive += 1;
				cFound = true;
			}
		}
		
		if (!cFound) 
		{
            radius *=1.05;
			CircleTrack c = CircleTrack( pos );
			c.pos = pos;
			c.radius = radius;
			c.lastSeen = ofGetFrameNum();
			c.isAlive = 0;
			c.drawMe = false;
			c.isSetUp = false;
			c.iD = circID;
			
			if (c.radius) {
				myCircles.push_back(c);
				circID++;
                
                ofPixels pixCopy = pix;
                pixCopy.crop( pos.x-radius, (pos.y-radius), radius*2 , radius*2 );
                tex.loadData( pixCopy );
                
                ofTexture T;
                T.allocate(radius*2,radius*2, GL_RGB);
                T.loadData(pixCopy);
                particle P = particle( pos,circID,radius,T  );
                punched.push_back( P );
                printf("punched %f,%f\n", pos.x,pos.y);
                
			}
		}
	}	
	
	cvReleaseMemStorage(&storage);
	
	for ( int x = 0; x<myCircles.size(); x++ ) 
    {
		bool isSetupNow = myCircles[x].isSetUp;
		if ( !isSetupNow ) {
			myCircles[x].setup();
		}
	}
	

	vector<CircleTrack>::iterator iter = myCircles.begin();
	while (iter != myCircles.end()) {
		
		double life = iter->lastSeen;
		int isAlive = iter->isAlive;
    
        // kill old particles;
        
		if ( (ofGetFrameNum()-life) > 50 ) 
        {
			
			ofPoint tracePos = iter->pos;
			iter = myCircles.erase(iter);
			cout << "shape deleted at: "<< tracePos.x << ", " << tracePos.y << endl;
			
			
		} else {
			
			if (iter->isAlive > 0) // used to be 3? 
            {
				iter->drawMe = true;
			} else 
            {
				iter->drawMe = false;
			}
			iter++;
		}
	}
}
bool phdControlLink::pointInside(float _x, float _y) {
	return (ofDistSquared(_x, _y, x, y) <= ofDistSquared(0, 0, (float)(linkSize*0.5), (float)(linkSize*0.5) ) );
}
vector < ofPoint > ArmContourFinder::findWrists(int n) {

	unsigned int l = getLabel(n);
	//Square our distances now, because premature optimization
	int minSquared = MIN_HAND_SIZE * MIN_HAND_SIZE;
	int maxSquared = MAX_HAND_SIZE * MAX_HAND_SIZE;
	int maxWrist = MAX_WRIST_WIDTH * MAX_WRIST_WIDTH;
	int minWrist = MIN_WRIST_WIDTH * MIN_WRIST_WIDTH;
	float distSquared;
	if(wrists[l].size() == 2) {
		//If the old wrists still work, keep em
		vector< ofPoint > closestWrists;
		closestWrists.push_back(polylines[n].getClosestPoint(wrists[l][0]));
		closestWrists.push_back(polylines[n].getClosestPoint(wrists[l][1]));
		distSquared = ofDistSquared(closestWrists[0].x, closestWrists[0].y, closestWrists[1].x, closestWrists[1].y);
		if(distSquared <= maxWrist and distSquared >= minWrist) {
			float d1 = ofDistSquared(closestWrists[0].x, closestWrists[0].y, tips[l].x, tips[l].y);
			float d2 = ofDistSquared(closestWrists[1].x, closestWrists[1].y, tips[l].x, tips[l].y);
			if(d1 >= minSquared and d1 <= maxSquared and d2 >= minSquared and d2 <= maxSquared)
				return closestWrists;
		}
	}

	//One polyline for each side of the hand
	ofPolyline sideOne, sideTwo;

	//The indeces at which the split will occur
	unsigned int start, end;

	//Find those indeces, not sure if this is computationally expensive (premature optimization, anyone?)
	polylines[n].getClosestPoint(tips[l], &start);
	polylines[n].getClosestPoint(ends[l], &end);


	//Put all verteces within the right distance in one set
	int i = start;
	while(i != end) {
		distSquared = ofDistSquared(tips[l].x, tips[l].y, polylines[n][i].x, polylines[n][i].y);
		if(distSquared <= maxSquared and distSquared >= minSquared)
			sideOne.addVertex( polylines[n][i] );
		i++;
		if( i == polylines[n].size() )
			i = 0;
	}

	//Now grab all the suitable ones on the other side
	i = start;
	while(i != end) {
		distSquared = ofDistSquared(tips[l].x, tips[l].y, polylines[n][i].x, polylines[n][i].y);
		if(distSquared <= maxSquared and distSquared >= minSquared)
			sideTwo.addVertex( polylines[n][i] );
		i--;
		if( i < 0 )
			i = polylines[n].size() - 1;
	}

	// Now find the closest two points on these lines
	float shortestDist = maxWrist + 1;
	vector< ofPoint > possibleWrists;
	for (int i = 0; i < sideOne.size(); ++i)
	{
		for (int j = 0; j < sideTwo.size(); ++j)
		{
			distSquared = ofDistSquared(sideOne[i].x, sideOne[i].y, sideTwo[j].x, sideTwo[j].y);
			if(distSquared < shortestDist and distSquared <= maxWrist and distSquared >= minWrist) {
				possibleWrists.resize(2);
				possibleWrists[0] = sideOne[i];
				possibleWrists[1] = sideTwo[j];
				shortestDist = distSquared;
			}
		}
	}

	return possibleWrists;

}
ofPoint ArmContourFinder::findEnd(int n) {

	vector< ofPoint > pts = polylines[n].getVertices();
	vector< ofPoint > endPoints;
	ofPoint center = ofxCv::toOf(getCenter(n));
	unsigned int l = getLabel(n);

	int sides [4] = {0,0,0,0};
	for (int i = 0; i < pts.size(); ++i)
	{
		if(pts[i].x <= bounds[0] + 2 || pts[i].y <= bounds[1] + 2
 			|| pts[i].x >= bounds[2] - 2 || pts[i].y >=  bounds[3] - 2) {
			endPoints.push_back(pts[i]);
		}
		if(pts[i].x <= bounds[0] + 2)
			sides[0]++;
		if(pts[i].y <= bounds[1] + 2)
			sides[1]++;
		if(pts[i].x >= bounds[2] - 2)
			sides[2]++;
		if(pts[i].y >= bounds[3] - 2)
			sides[3]++;
	}
	int maxSide = 0;
	for (int i = 0; i < 4; ++i)
	{
		if(sides[i] > maxSide) {
			maxSide = sides[i];
			side[l] = i;
		}
	}
	if(endPoints.size() > 0) {
		// Just take the one that's the farthest from the center of the box
		float maxDist = 0;
		for (int i = 0; i < endPoints.size(); ++i)
		{
			float dist = ofDistSquared(center.x, center.y, endPoints[i].x, endPoints[i].y);
			if(dist > maxDist) {
				maxDist = dist;
				endPoints[0] = endPoints[i];
			}
		}

		endPoints.resize(1);

		// if(endPoints[0].x <= bounds[0] + 2) side[l] = 0;
		// else if(endPoints[0].y <= bounds[1] + 2) side[l] = 1;
		// else if(endPoints[0].x >= bounds[2] - 2) side[l] = 2;
		// else if(endPoints[0].y >= bounds[3] - 2) side[l] = 3;
	}


	if(endPoints.size() == 0) {
		if(handFound[l]) {
			ofPoint centroid = polylines[n].getCentroid2D();
			int thisSide = side[l];
			// assume they're still on the same side
			ofPoint mark;
			if(thisSide == 0 or thisSide == 2)
				mark = ofPoint(bounds[thisSide], centroid.y);
			else
				mark = ofPoint(centroid.x, bounds[thisSide]);
			endPoints.push_back(polylines[n].getClosestPoint(mark));
		}
		else return ofPoint(-1, -1);
	}



	// New tactic!
	ofPolyline rotatedRect = ofxCv::toOf(getMinAreaRect(n));
	vector< ofPoint > verts = rotatedRect.getVertices();

	// Remove two farthest from endpoint
	for (int i = 0; i < 2; ++i)
	{
		float maxDist = 0;			
		int indexToRemove;
		for (int i = 0; i < verts.size(); ++i)
		{
			float dist = ofDistSquared(endPoints[0].x, endPoints[0].y, verts[i].x, verts[i].y);
			if(dist > maxDist) {
				maxDist = dist;
				indexToRemove = i;
			}
		}
		verts.erase(verts.begin() + indexToRemove);
	}


	ofPoint end;

	float maxDist = 0;
	vector< float > distances;
	for (int i = 0; i < verts.size(); ++i)
	{
		float dist = ofDistSquared(verts[i].x, verts[i].y, center.x, center.y);
		distances.push_back(dist);
		if(dist > maxDist) {
			maxDist = dist;
			end = verts[i];
		}
	}

	if(distances.size() == 2) {
		if( abs(distances[0] - distances[1]) < 400 ) {
			float d1 = ofDistSquared(ends[l].x, ends[l].y, verts[0].x, verts[0].y);
			float d2 = ofDistSquared(ends[l].x, ends[l].y, verts[1].x, verts[1].y);
			if(d1 < d2)
				end = verts[0];
			else
				end = verts[1];
		}
	}


	return end;

}
Example #23
0
void LightScene::Render()
{
	//FOG 
	m_fogShader.begin();
	m_fogImg.getTextureReference().bind();
	m_fogVbo.bind();

	//draw fog shader
	m_fogShader.setUniformTexture("lightsOnPix", m_lightsOnTexture, 1);
	m_fogShader.setUniform1f("numLights", m_lights.size());
	glDrawArrays(GL_QUADS, 0, 4);

	m_fogImg.getTextureReference().unbind();
	m_fogShader.end();
	m_fogVbo.unbind();


	//draw lights
	ofSetColor(255);
	ofSetRectMode(OF_RECTMODE_CORNER);
	for(vector<Light>::iterator l = m_lights.begin(); l != m_lights.end(); ++l){
		if(l->isOn == true){
			m_lightImg.draw(l->x, 0, 0);
		}
	}


	HexagonParticle* hp;
	//draw a hexagon for each person in people
	for(vector<Particle*>::iterator p = pPeople->begin(); p != pPeople->end(); ++p)
	{
		hp = (HexagonParticle*) (*p);
		ofPushMatrix();
			ofTranslate(hp->pos);
			ofSetRectMode(OF_RECTMODE_CENTER);
			ofRotate(hp->hexRotation);
			ofScale(hp->hexSize, hp->hexSize);

			ofSetColor(hp->hexColor);
			m_hexImgInner.draw(0,0);

			ofSetColor(255);
			m_hexImgBorder.draw(0,0);
		ofPopMatrix();
		
		ofNoFill();
		ofSetLineWidth(5);
		float distSqrd = std::numeric_limits<float>::max();

		for(vector<Particle*>::iterator hands = pHandPositions->begin(); hands != pHandPositions->end(); ++hands){
			distSqrd = ofDistSquared( (*hands)->pos.x, (*hands)->pos.y, (*p)->pos.x, (*p)->pos.y);
			
			if (distSqrd < 90000){				
				closestHand.push_back((*hands));							
			}
		}

		if(closestHand.size() != 0){
			for(vector<Particle*>::iterator connectedhands = closestHand.begin(); connectedhands != closestHand.end(); ++connectedhands){

				ofLine( (*connectedhands)->pos.x, (*connectedhands)->pos.y, (*p)->pos.x, (*p)->pos.y);
				
				hp->hexAlpha += 5;
				if(hp->hexAlpha > 200){
					hp->hexAlpha = 200;
				}
				
				hp->hexSize += hp->hexGrowthRate;
				if (hp->hexSize  > 4){  
					hp->hexSize = 4; 
				}
			}
			closestHand.clear();

		}
		else
		{

			hp->hexAlpha -= 5;
			if(hp->hexAlpha < 0){
				hp->hexAlpha = 0;
			}	

			hp->hexSize -= hp->hexGrowthRate;
				if (hp->hexSize  < 1){  
					hp->hexSize = 1; 
				}
		}
		hp->hexColor.a = hp->hexAlpha;	
	}

}
Example #24
0
void effectFive::draw(float sound,bool defromPerlin,float left[256]){
	offcetTime++;
	if(offcetTime > period){
	
		builtNextBranch();
			offcetTime = 0;
	}
	
	
	
	//ofSetLineWidth(1);
	ofNoFill();
	ofSetColor(255, 255, 255);
	int index = 0;
	
	//Applique l'effet sur toutes les lignes
	
	for(int i=0; i<lines.size(); i++){
		
		int div = 10;
		int posx1 = lines[i].x1;
		int posy1 = lines[i].y1;
		int posx2 = lines[i].x2;
		int posy2 = lines[i].y2;
		
		if(defromPerlin){
			
			float simplexNoiseX1 = (noise->noise3((512.0-(float)posx1)/div,(float)posy1/div,ofGetFrameNum()/3))*sound*15000;
			float simplexNoiseY1 = (noise->noise3((350-(float)posy1)/div,(float)posx1/div,ofGetFrameNum()/3))*sound*15000;
			float simplexNoiseX2 = (noise->noise3((512-(float)posx2)/div,(float)posy2/div,ofGetFrameNum()/3))*sound*15000;
			float simplexNoiseY2 = (noise->noise3((350-(float)posy2)/div,(float)posx2/div,ofGetFrameNum()/3))*sound*15000;
			
			posx1Voulu = posx1 + simplexNoiseX1;
			posy1Voulu = posy1 + simplexNoiseY1;
			posx2Voulu = posx2 + simplexNoiseX2;
			posy2Voulu = posy2 + simplexNoiseY2;
			
			float diffx1 =  posx1Voulu - posx1;
			float diffy1 =  posy1Voulu - posy1;
			float diffx2 =  posx2Voulu - posx2;
			float diffy2 =  posy2Voulu - posy2;
			
			posx1 += diffx1/150;
			posy1 += diffy1/150;
			posx2 += diffx2/150;
			posy2 += diffy2/150;
			
		}
		
		float dist = sqrt(ofDistSquared(posx1, posy1, posx2, posy2 ));
		float coeff = 256.0 / dist;
		float hight = 200.0 / coeff;
		float angle = atan2(double(posy2-posy1) , double(posx2-posx1));
		float mydegreangle = ofRadToDeg(angle);			
		
		glPushMatrix();
		ofTranslate(posx1, posy1,0);
		ofRotateZ(mydegreangle);
	//	glBegin(GL_LINES);

		for(int x = 1; x < dist; x +=20) {
			if(branches.size()>index){
			branches[index].draw();
			index++;
			}
			//ofCircle(x,0, 10);
		
		}
		
		glPopMatrix();
	}
}
Example #25
0
//--------------------------------------------------------------
void ofApp::update() {


    //kinect stuff
    kinect.update();
    if(kinect.isNewSkeleton()) {
        for( int i = 0; i < kinect.getSkeletons().size(); i++)
        {
            // has a head? probably working ok then :)
            if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD) != kinect.getSkeletons().at(i).end())
            {
                // just get the first one
                //SkeletonBone headBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD)->second;
                //ofVec2f headScrenPosition( headBone.getScreenPosition().x, headBone.getScreenPosition().y);
                SkeletonBone lHand = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HAND_LEFT)->second;
                ofVec2f lHandScrenPosition( lHand.getScreenPosition().x, lHand.getScreenPosition().y);

                SkeletonBone rHand = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HAND_RIGHT)->third;
                ofVec2f rHandScreenPosition( rHand.getScreenPosition().x, rHand.getScreenPosition().y);
                //cerr << headBone;
                handDist = ofDistSquared(lHand.getScreenPosition().x, lHand.getScreenPosition().y, rHand.getScreenPosition().x, rHand.getScreenPosition().y);

                return;
            }
        }
    }

    vidGrabber.update();
    float time = ofGetElapsedTimef();         //Get time
    float oldValue3d = ofNoise(meshRadius, time);


    //Change vertices

    for (int i=0; i<mesh.getNumVertices(); i++) {
        //---------------------------------//
        //set mesh color from video (even though it's not working right now)
        //---------------------------------//

        // Map i to the width and height of the image ( or video ) so that color can be sampled from the corresponding pixel
        int widthSampleIndex = ofMap(i, 0, mesh.getNumVertices(), 0, vidGrabber.getWidth());
        int heightSampleIndex = ofMap(i, 0, mesh.getNumVertices(), 0, vidGrabber.getHeight());

        if(vidGrabber.isFrameNew()) {
            vidPixels = vidGrabber.getPixelsRef();

            //Grab color from pixel and set vertex color to it
            //c = vidPixels.getColor(widthSampleIndex, heightSampleIndex);
            image.setFromPixels(vidGrabber.getPixelsRef());
            c = image.getColor(widthSampleIndex, heightSampleIndex);
            cerr << ( c )<< endl ;
        }
        mesh.addColor(c);

        //---------------------------------//
        //displace vertice here using prelin noise ( After attempting this approach I think I would go with GLSL shaders next time.  While each point follows the noise with this there's no continuity from point to point so it looks random.
        //---------------------------------//

        ofVec3f p = mesh.getVertex( i );

        //Get Perlin noise value ( not sure which is best
        float value = ofNoise(100*i);
        float value2d = ofNoise(100 * i, time);
        float value3d = ofNoise(100 * i, i, time);

        ofVec3f center = mesh.getCentroid();
        float dist = center.distance(p);

        //find scale value so that the vertice can be moved
        float scaleValue = (dist*(oldValue3d))/dist;
        float oldValue3d = value3d;

        //Change coordinate of vertex in relation to the center of the sphere
        ofVec3f newP = p.scale(scaleValue*(10*handDist));
        //mesh.setVertex(i, newP);
    }
}