示例#1
0
void contourToConvexHull(ofPolyline &src, ofPolyline &dst) {

    dst.clear();

    vector<hPoint> P(src.size());
    for(int i = 0; i < src.size(); i++) {
        P[i].x = src[i].x;
        P[i].y = src[i].y;
    }

    int n = src.size(), k = 0;
    vector<hPoint> H(2*n);

    // Sort points lexicographically
    sort(P.begin(), P.end());

    // Build lower hull
    for (int i = 0; i < n; i++) {
        while (k >= 2 && cross(H[k-2], H[k-1], P[i]) <= 0) k--;
        H[k++] = P[i];
    }

    // Build upper hull
    for (int i = n-2, t = k+1; i >= 0; i--) {
        while (k >= t && cross(H[k-2], H[k-1], P[i]) <= 0) k--;
        H[k++] = P[i];
    }

    H.resize(k);

    for(int i = 0; i < H.size(); i++) {
        dst.addVertex(H[i].x + 500, H[i].y);
    }
}
void ofxPolylineLoad(ofPolyline & poly, string xmlPath) {
    ofXml xml;
    bool bLoaded = xml.load(xmlPath);
    if(bLoaded == false) {
        return;
    }
    
    xml.setTo("poly");
    bool bClosed = ofToInt(xml.getAttribute("closed"));
    
    poly.clear();
    
    int numOfPoints = xml.getNumChildren();
    for(int i=0; i<numOfPoints; i++) {
        xml.setToChild(i);
        float x = ofToFloat(xml.getAttribute("x"));
        float y = ofToFloat(xml.getAttribute("y"));
        
        poly.addVertex(x, y);
    }
    
    if(bClosed == true) {
        poly.close();
    }
}
示例#3
0
void phdGimbal2d::getKeyPoints(ofPolyline & _keys) {
	_keys.clear();
	_keys.addVertex( 1.00, 0.0);
	_keys.addVertex( 0.75, 0.0);
	_keys.addVertex( 0.00, 0.0);
	_keys.addVertex( 0.00,-1.0);
	getTransformedPolyline(_keys, _keys, getOTSMatrix());
}
示例#4
0
void update()
{
    // Update our little offset thingy.
    offset += 0.01;

    if (offset > 1) 
    {
        offset = 0;   
    }
    
    // Update our camera.
    grabber.update();
    
    // If the camera has a new frame to offer us ...
    if (grabber.isFrameNew())
    {
        // Make a copy of our grabber pixels in the colorImage.
        colorImage.setFromPixels(grabber.getPixelsRef());

        // When we assign a color image to a grayscale image, it is converted automatically.
        grayscaleImage = colorImage;
		
	    // If we set learnBackground to true using the keyboard, we'll take a snapshot of 
	    // the background and use it to create a clean foreground image.
		if (learnBackground == true)
		{
		    // We assign the grayscaleImage to the grayscaleBackgroundImage.
			grayscaleBackgroundImage = grayscaleImage;	
			
			// Now we set learnBakground so we won't set a background unless
			// explicitly directed to with a keyboard command.
			learnBackground = false;
		}

        // Create a difference image by comparing the background and the current grayscale images.
		grayscaleAbsoluteDifference.absDiff(grayscaleBackgroundImage, grayscaleImage);

        // Assign grayscaleAbsoluteDifference to the grayscaleBinary image.
        grayscaleBinary = grayscaleAbsoluteDifference;
        
        // Then threshold the grayscale image to create a binary image.
        grayscaleBinary.threshold(threshold, invert);

        // Find contours (blobs) that are between the size of 20 pixels and 
        // 1 / 3 * (width * height) of the camera. Also find holes.
		contourFinder.findContours(grayscaleBinary, 100, (width * height) / 3.0, 10, true);

        // Get the biggest blob and use it to draw.
        if (contourFinder.nBlobs > 0)
        {
            holePositions.addVertex(contourFinder.blobs[0].boundingRect.getCenter());
        }
        else 
        {
		   holePositions.clear(); 
        }
    }
}
//--------------------------------------------------------------
void testApp::newPath() {
  path.clear();
  // A path is a series of connected points
  // A more sophisticated path might be a curve
  path.addVertex(0, ofGetHeight()/2);
  path.addVertex(ofRandom(0, ofGetWidth()/2), ofRandom(0, ofGetHeight()));
  path.addVertex(ofRandom(ofGetWidth()/2, ofGetWidth()), ofRandom(0, ofGetHeight()));
  path.addVertex(ofGetWidth(), ofGetHeight()/2);
}
示例#6
0
void testApp::update(){
    ofSetWindowTitle(ofToString(ofGetFrameRate()));
    mesh.clear();
    for (int i = 0; i < balls.size(); i++) {
        balls[i].update();
	float z = 0;
	ofPoint sp = ofPoint(balls[i].getPos().x, balls[i].getPos().y, z);
	mesh.curveTo(sp*1.0);
    }
}
示例#7
0
void testApp::interpolatePolyLine(ofPolyline& a, ofPolyline& b, ofPolyline& out, float delta){
    if(a.getVertices().size() != b.getVertices().size()){
        ofLogError("Polylines did not match in size");
        return;
    }
    
    out.clear();
    
    for(int i = 0; i < a.getVertices().size(); i++){
        out.addVertex( a.getVertices()[i].getInterpolated(b.getVertices()[i], delta) );
    }
}
示例#8
0
void WaveformForBuffer(Float32 * begin, size_t length, float w, float h, ofPolyline &outLine, unsigned rate) {
	const size_t size = length / rate;
	
	if(size == 0) {
		outLine.clear();
		return;
	}
	
	if(outLine.size() != size) {
		outLine.resize(size);
	}
	
	float * v = (float *)&outLine[0];
	float zero = 0;
	float half = h / 2.;
	vDSP_vsmsa(begin, rate, &half, &half, v + 1, 3, size); // multiply and add "y"s
	vDSP_vgen(&zero, &w, v, 3, size); // generate "x"s
}
示例#9
0
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    switch(key) {
        case 'R': bRotate ^= true; if(!bRotate) rotAngle = 0; break;
        case 'r': poly.clear(); break;
        case 'c': poly.curveTo(mouseX, mouseY); break;
        case 'a': poly.arc(mouseX, mouseY, 50, 50, 0, 180); break;
        case 'o': poly.setClosed(!poly.isClosed()); break;
            
        case 'F': poly.simplify(10); break;
        case 'M': poly = poly.getSmoothed(5); break;
        case 'S': poly = poly.getResampledBySpacing(30); break;
        case 'C': poly = poly.getResampledByCount(50); break;
            
        case 'l': poly.setClosed(!poly.isClosed());
            
        case 'i': poly.insertVertex(ofPoint(mouseX, mouseY, 0), nearestIndex); break;
    }
}
示例#10
0
// ----------------------------------------------------------
void WaveformForBuffer(const ofxAudioUnitTap::MonoSamples &buffer, float width, float height, ofPolyline &outLine, unsigned sampleRate)
// ----------------------------------------------------------
{	
	outLine.clear();
	
	const float xStep = width / (buffer.size() / sampleRate);
	float x = 0;
	
	for (int i = 0; i < buffer.size(); i += sampleRate, x += xStep)
	{
#if TARGET_OS_IPHONE
		SInt16 s = SInt16(buffer[i] >> 9);
		float y = ofMap(s, -32768, 32767, height, 0, true);
#else
		float y = ofMap(buffer[i], -1, 1, height, 0, true);
#endif
		outLine.addVertex(ofPoint(x, y));
	}
}
示例#11
0
//--------------------------------------------------------------
void Path3D::parsePts(string filename, ofPolyline &polyline){
    ofFile file = ofFile(ofToDataPath(filename));
    polyline.clear();
    if(!file.exists()){
        ofLogError("The file " + filename + " is missing");
    }
    ofBuffer buffer(file);
    
    //Read file
    for (ofBuffer::Line it = buffer.getLines().begin(); it != buffer.getLines().end(); it++) {
        string line = *it;
        
        float scalar = 10;
        
        ofVec3f offset;
        if (filename == "path_XZ.txt"){
            offset = ofVec3f(0, 0, 0);
            scalar = 3;
        }
        else if (filename == "path_YZ.txt"){
            offset = ofVec3f(0, 0, 0);
            scalar = 3;
        }
        else{
            offset = ofVec3f(0, 0, .25);
            scalar = 3;
        }
        
        ofStringReplace(line, "{", "");
        ofStringReplace(line, "}", "");
        cout<<line<<endl;
        vector<string> coords = ofSplitString(line, ", ");  // get x y z coordinates
        
        ofVec3f p = ofVec3f(ofToFloat(coords[0])*scalar,ofToFloat(coords[1])*scalar,ofToFloat(coords[2])*scalar);
        p += offset;
        
        polyline.addVertex(p);
    }
}
示例#12
0
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

    temp.clear();
}