diff options
| author | Tim Redfern <tim@getdrop.com> | 2018-05-22 00:00:06 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@getdrop.com> | 2018-05-22 00:00:06 +0100 |
| commit | 459d1daf71dd8991c60dc8c84d1154802eec331b (patch) | |
| tree | 0a8407a7aad9d4e8fbbd51287bb921f69ae1d073 /ofxHelios | |
| parent | 880f710768391dc3a3399fc1896447a9e6c34fa4 (diff) | |
OE started
Diffstat (limited to 'ofxHelios')
| -rw-r--r-- | ofxHelios/src/lineSegmenter.cpp | 71 | ||||
| -rw-r--r-- | ofxHelios/src/lineSegmenter.h | 22 | ||||
| -rw-r--r-- | ofxHelios/src/lineTransformer.cpp | 127 | ||||
| -rw-r--r-- | ofxHelios/src/lineTransformer.h | 19 | ||||
| -rw-r--r-- | ofxHelios/src/ofxHelios.h | 1 |
5 files changed, 240 insertions, 0 deletions
diff --git a/ofxHelios/src/lineSegmenter.cpp b/ofxHelios/src/lineSegmenter.cpp new file mode 100644 index 0000000..14f6e24 --- /dev/null +++ b/ofxHelios/src/lineSegmenter.cpp @@ -0,0 +1,71 @@ + #include "lineSegmenter.h" + +const vector <ofPolyline> & lineSegmenter::getSegments(int num,float coverage, float phase){ + //num - number of segments + //coverage - amount that each segment fills it's slot from 0-1 + //phase - from 0-1 + + //if the path is closed, we can make a segment that crosses the end/beginning + //however we want to be able to deal with open paths + +/* + +segments 0...n - 1 +phase 0...1 + +phase 0 + +segment 0 is (0 -> coverage) / n +segment n - 1 is ((0 -> coverage) + (n-1)) /n + +phase 1: has to be the loop target, it has to look identical + +segment 0 is (1 -> coverage) / n +segment n - 1 is (1 - > coverage) + (n-1) + +*/ + + + segments.clear(); + + for (int i=0;i<num;i++){ + float startIndex=line.getIndexAtPercent((phase+i)/num); //always <1 + float endPoint=(phase+i+coverage)/num; //can be >1 + float endIndex=line.getIndexAtPercent(endPoint>1.0f?endPoint-1.0f:endPoint); + ofPolyline segment; + segment.addVertex(line.getPointAtIndexInterpolated(startIndex)); + for (int j=(int)ceil(startIndex);j<(endPoint>1?line.size():(int)ceil(endIndex));j++){ + segment.addVertex(line[j]); + } + if (endPoint>1){ + segments.push_back(segment); + segment.clear(); + for (int j=0;j<(int)ceil(endIndex);j++){ + segment.addVertex(line[j]); + } + segment.addVertex(line.getPointAtIndexInterpolated(endIndex)); + } + else { + segment.addVertex(line.getPointAtIndexInterpolated(endIndex)); + } + segments.push_back(segment); + } + + return segments; +} + +void lineSegmenter::draw(){ + line.draw(); + return; +} +int lineSegmenter::size(){ + return line.size(); +} + + +/* + + + + +*/
\ No newline at end of file diff --git a/ofxHelios/src/lineSegmenter.h b/ofxHelios/src/lineSegmenter.h new file mode 100644 index 0000000..d858ba2 --- /dev/null +++ b/ofxHelios/src/lineSegmenter.h @@ -0,0 +1,22 @@ +#pragma once + +#include "ofMain.h" + +class lineSegmenter{ + public: + lineSegmenter(ofPolyline &_line){ + line=_line; + if (line.isClosed()){ + line.addVertex(line[0]); + } + } + const vector <ofPolyline> &getSegments(int num,float coverage, float phase); + ofPolyline getPoly(){ + return line; + } + void draw(); + int size(); + private: + ofPolyline line; + vector <ofPolyline> segments; +};
\ No newline at end of file diff --git a/ofxHelios/src/lineTransformer.cpp b/ofxHelios/src/lineTransformer.cpp new file mode 100644 index 0000000..c405569 --- /dev/null +++ b/ofxHelios/src/lineTransformer.cpp @@ -0,0 +1,127 @@ +#include "lineTransformer.h" + + +void lineTransformer::drawWarpFrame(glm::vec2 warpframe[4]){ + ofSetColor(255,255,255); + ofNoFill(); + for (int i=0;i<4;i++){ + ofDrawCircle(warpframe[i],25); + ofDrawLine(warpframe[i],warpframe[(i+1)%4]); + } +} + +void lineTransformer::gaussianElimination(float * input, int n) +{ + auto i = 0; + auto j = 0; + auto m = n - 1; + + while (i < m && j < n) + { + auto iMax = i; + for (auto k = i + 1; k < m; ++k) + { + if (fabs(input[k * n + j]) > fabs(input[iMax * n + j])) + { + iMax = k; + } + } + + if (input[iMax * n + j] != 0) + { + if (i != iMax) + { + for (auto k = 0; k < n; ++k) + { + auto ikIn = input[i * n + k]; + input[i * n + k] = input[iMax * n + k]; + input[iMax * n + k] = ikIn; + } + } + + float ijIn = input[i * n + j]; + for (auto k = 0; k < n; ++k) + { + input[i * n + k] /= ijIn; + } + + for (auto u = i + 1; u < m; ++u) + { + auto ujIn = input[u * n + j]; + for (auto k = 0; k < n; ++k) + { + input[u * n + k] -= ujIn * input[i * n + k]; + } + } + + ++i; + } + ++j; + } + + for (auto i = m - 2; i >= 0; --i) + { + for (auto j = i + 1; j < n - 1; ++j) + { + input[i * n + m] -= input[i * n + j] * input[j * n + m]; + } + } +} + +glm::mat4 lineTransformer::getPerspectiveTransformMatrix(const glm::vec2 src[4], const glm::vec2 dst[4]) +{ + float p[8][9] = + { + { -src[0][0], -src[0][1], -1, 0, 0, 0, src[0][0] * dst[0][0], src[0][1] * dst[0][0], -dst[0][0] }, // h11 + { 0, 0, 0, -src[0][0], -src[0][1], -1, src[0][0] * dst[0][1], src[0][1] * dst[0][1], -dst[0][1] }, // h12 + { -src[1][0], -src[1][1], -1, 0, 0, 0, src[1][0] * dst[1][0], src[1][1] * dst[1][0], -dst[1][0] }, // h13 + { 0, 0, 0, -src[1][0], -src[1][1], -1, src[1][0] * dst[1][1], src[1][1] * dst[1][1], -dst[1][1] }, // h21 + { -src[2][0], -src[2][1], -1, 0, 0, 0, src[2][0] * dst[2][0], src[2][1] * dst[2][0], -dst[2][0] }, // h22 + { 0, 0, 0, -src[2][0], -src[2][1], -1, src[2][0] * dst[2][1], src[2][1] * dst[2][1], -dst[2][1] }, // h23 + { -src[3][0], -src[3][1], -1, 0, 0, 0, src[3][0] * dst[3][0], src[3][1] * dst[3][0], -dst[3][0] }, // h31 + { 0, 0, 0, -src[3][0], -src[3][1], -1, src[3][0] * dst[3][1], src[3][1] * dst[3][1], -dst[3][1] }, // h32 + }; + + gaussianElimination(&p[0][0], 9); + + return glm::mat4(p[0][8], p[3][8], 0, p[6][8], + p[1][8], p[4][8], 0, p[7][8], + 0, 0, 1, 0, + p[2][8], p[5][8], 0, 1); +} + +ofPolyline lineTransformer::polyLineTransform(const ofMatrix4x4 xform, const ofPolyline& poly){ + ofPolyline tempPoly; + for (auto& p:poly){ + tempPoly.addVertex(ofVec3f(p)*xform); + } + return tempPoly; +} + +colourPolyline lineTransformer::polyLineTransform(const ofMatrix4x4 xform,colourPolyline& poly,float colourFade){ + colourPolyline tempPoly; + for (int i=0;i<poly.size();i++){ + tempPoly.addVertex(ofVec3f(poly[i])*xform,poly.getColourAt(i)*colourFade); + } + return tempPoly; +} + + +ofPolyline lineTransformer::makePolygon(int num,float diam){ + ofPolyline poly; + float step=PI*2/num; + for (int i=0;i<=num;i++){ + poly.addVertex(cos(step*i)*diam,sin(step*i)*diam); + } + return poly; +} + +void lineTransformer::drawPoly(ofPolyline poly,float x,float y){ + glPushMatrix(); + ofTranslate(x,y); + poly.draw(); + for (int i=0;i<poly.size();i++){ + ofDrawBitmapString(poly.getDegreesAtIndex(i),poly[i].x+10,poly[i].y+10,0); + } + glPopMatrix(); +} diff --git a/ofxHelios/src/lineTransformer.h b/ofxHelios/src/lineTransformer.h new file mode 100644 index 0000000..0c1ff3b --- /dev/null +++ b/ofxHelios/src/lineTransformer.h @@ -0,0 +1,19 @@ +#pragma once + +#include "ofMain.h" +#include "colourPolyline.h" + +class lineTransformer { + + public: + lineTransformer(){ + } + void static drawWarpFrame(glm::vec2 warpframe[4]); + void static gaussianElimination(float * input, int n); + glm::mat4 static getPerspectiveTransformMatrix(const glm::vec2 src[4], const glm::vec2 dst[4]); + ofPolyline static polyLineTransform(const ofMatrix4x4 xform,const ofPolyline& poly); + colourPolyline static polyLineTransform(const ofMatrix4x4 xform,colourPolyline& poly,float colourFade=1.0f); + ofPolyline static makePolygon(int num,float diam); + void static drawPoly(ofPolyline poly,float x,float y); + +};
\ No newline at end of file diff --git a/ofxHelios/src/ofxHelios.h b/ofxHelios/src/ofxHelios.h index 280906f..8493cd4 100644 --- a/ofxHelios/src/ofxHelios.h +++ b/ofxHelios/src/ofxHelios.h @@ -5,6 +5,7 @@ // Created by Tim Redfern Nov 2017 // // it would be good if ofxHelios could inherit the current transform +// it would good if it could be resoution independent // #ifndef ofxHelios_h |
