[work 40] Untitled

[work 40] Untitled

Movie

Source code

about

  • 等間隔の点とウィンドウ内を動く点でドロネー図を描く

file

  • 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
  • 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"

//================================
int main( ){

    // 4K:4096x2160
    // 2K:2048x1080
    // FullHD:1920x1080
    // HD:1440x1080
    // HD720p:1280x720
    // DVD:720x480
    // setup the GL context
    ofSetupOpenGL(1280,720, OF_WINDOW);

	// this kicks off the running of my app
	// can be OF_WINDOW or OF_FULLSCREEN
	// pass in width and height too:
	ofRunApp( new ofApp());

}
#pragma once

#include "ofMain.h"

#include "Delaunay.hpp"


class ofApp : public ofBaseApp{
public:
    ofApp();
    ~ofApp();
    
    void setup();
    void update();
    void draw();
    
    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y);
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void mouseEntered(int x, int y);
    void mouseExited(int x, int y);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
    
private:
    int cellSize;
    int colSize, rowSize;
    float tx, ty;
    
    shared_ptr<Delaunay2d> d2d;
    std::vector<ofVec2f> points;
    std::vector<ofVec2f> movers;
    ofRectangle rect;
    std::vector<std::array<ofVec2f, 3>> triangles;
    ofMesh frame;
};
#include "ofApp.h"


#define BG_COLOR 218,214,200
#define FR_COLOR 255
#define MV_COLOR 89,41,5

ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    
    ofSetFrameRate(fps);
    ofBackground(BG_COLOR);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    cellSize = 80;
    colSize = ofGetWidth() / cellSize;
    rowSize = ofGetHeight() / cellSize;
    
    tx = 0;
    ty = 10000;
    
    rect = ofRectangle(-1, -1, ofGetWidth()+2, ofGetHeight()+2);
}

//--------------------------------------------------------------
void ofApp::update(){
    points.clear();
    for (int row = 0; row <= rowSize; row++) {
        for (int col = 0; col <= colSize; col++) {
            float x = col * cellSize;
            float y = row * cellSize;
            ofVec2f p = ofVec2f(x, y);
            points.push_back(p);
        }
    }
    
    movers.clear();
    for (int i = 0; i < 100; i++) {
        float x = ofNoise(tx + i * 100) * ofGetWidth();
        float y = ofNoise(ty + i * 100) * ofGetHeight();
        movers.push_back(ofVec2f(x, y));
        points.push_back(movers.back());
    }
    tx += 0.02;
    ty += 0.02;
    
    d2d.reset(new Delaunay2d());
    d2d->setup(rect, points);
    d2d->getTriangle(triangles);
    
    frame.clear();
    frame.setMode(OF_PRIMITIVE_TRIANGLES);
    for (std::array<ofVec2f, 3> t : triangles) {
        frame.addVertex(ofVec3f(t[0].x, t[0].y));
        frame.addVertex(ofVec3f(t[1].x, t[1].y));
        frame.addVertex(ofVec3f(t[2].x, t[2].y));
        
        frame.addColor(ofColor(FR_COLOR));
        frame.addColor(ofColor(FR_COLOR));
        frame.addColor(ofColor(FR_COLOR));
    }

}

//--------------------------------------------------------------
void ofApp::draw(){
    frame.drawWireframe();
    
    for (ofVec2f p : points) {
        ofFill();
        ofSetColor(FR_COLOR);
        ofDrawCircle(p, 4);
    }
    
    for (ofVec2f m : movers) {
        ofNoFill();
        ofSetColor(MV_COLOR);
        ofDrawCircle(m, 10);
    }
}
#ifndef Delaunay_hpp
#define Delaunay_hpp

#include <stdio.h>
#include "ofMain.h"
#include <opencv2/opencv.hpp>



class Delaunay2d {
public:
    Delaunay2d();
    ~Delaunay2d();
    void setup(ofRectangle rect, std::vector<ofVec2f> points);
    void getTriangle(std::vector<std::array<ofVec2f, 3>> &tlist);
    
private:
    cv::Subdiv2D sd;
    std::vector<cv::Vec6f> triangleList;
    cv::Rect area;
};
#endif /* Delaunay_hpp */
#include "Delaunay.hpp"


Delaunay2d::Delaunay2d()
{
    
}

Delaunay2d::~Delaunay2d()
{
    
}


void Delaunay2d::setup(ofRectangle rect, std::vector<ofVec2f> points)
{
    area.x = (int)rect.x;
    area.y = (int)rect.y;
    area.width = (int)rect.width;
    area.height = (int)rect.height;
    sd.initDelaunay(area);
    
    for (ofVec2f ofxp : points) {
        cv::Point2f pt;
        pt.x = ofxp.x;
        pt.y = ofxp.y;
        
        if(rect.inside(ofxp.x, ofxp.y)) {
            sd.insert(pt);
        } else {
            std::cout << "[Warning] out of area:" << pt.x << "," << pt.y << std::endl;
        }
    }
    sd.getTriangleList(triangleList);
}


void Delaunay2d::getTriangle(std::vector<std::array<ofVec2f, 3>> &tlist)
{
    std::array<ofVec2f, 3> a;
    ofRectangle r = ofRectangle(area.x, area.y, area.width, area.height);
    
    tlist.clear();
    
    for (cv::Vec6f v6f : triangleList) {
        a[0].x = v6f[0];
        a[0].y = v6f[1];
        a[1].x = v6f[2];
        a[1].y = v6f[3];
        a[2].x = v6f[4];
        a[2].y = v6f[5];
        
        if (r.inside(a[0]) && r.inside(a[1]) && r.inside(a[2])) {
            tlist.push_back(a);
        }
    }
}

Link to the reference page

ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。

categoryAPI/Lib
openframeworksofNoise
openframeworksofMesh
openCVSubdiv2D

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode
  • openCV3