andrew@0
|
1 /*
|
andrew@0
|
2 * ofxPlotFunction.cpp
|
andrew@0
|
3 *
|
andrew@0
|
4 *
|
andrew@0
|
5 * Created by Andrew on 14/11/2011.
|
andrew@0
|
6 * Copyright 2011 QMUL. All rights reserved.
|
andrew@0
|
7 *
|
andrew@0
|
8 */
|
andrew@0
|
9
|
andrew@0
|
10 #include "ofxPlotFunction.h"
|
andrew@0
|
11
|
andrew@0
|
12
|
andrew@0
|
13 ofxPlotFunction :: ofxPlotFunction(){
|
andrew@0
|
14 fullScreen.resetToFullScreen();
|
andrew@0
|
15 }
|
andrew@0
|
16
|
andrew@0
|
17 ofxPlotFunction :: ~ofxPlotFunction(){
|
andrew@0
|
18
|
andrew@0
|
19 }
|
andrew@0
|
20
|
andrew@0
|
21
|
andrew@0
|
22 void ofxPlotFunction::drawVector(DoubleVector& energyVec, int minIndex, int maxIndex, const ofxWindowRegion& window, const double& maxNumberOfIndexPoints, const double& maxValue){
|
andrew@0
|
23
|
andrew@0
|
24 float screenHeight = window.height;
|
andrew@0
|
25 float screenWidth = window.width;
|
andrew@0
|
26
|
andrew@0
|
27 double numberOfIndexPoints = min(maxNumberOfIndexPoints, (double) maxIndex - minIndex);
|
andrew@0
|
28 double indicesPerStep = (maxIndex - minIndex) / numberOfIndexPoints;
|
andrew@0
|
29 double pixelsPerStep = window.width / numberOfIndexPoints;
|
andrew@0
|
30
|
andrew@0
|
31 int i, j;
|
andrew@0
|
32
|
andrew@0
|
33 double heightScalar = window.height / (1.0*maxValue);
|
andrew@0
|
34
|
andrew@0
|
35 int lastHeight = window.y + screenHeight - (energyVec[minIndex]*heightScalar);;
|
andrew@0
|
36 int newHeight;
|
andrew@0
|
37 int xPosition;
|
andrew@0
|
38 int lastXposition = window.x;
|
andrew@0
|
39
|
andrew@0
|
40 double exactIndex;
|
andrew@0
|
41 for (exactIndex = minIndex; exactIndex < maxIndex; exactIndex += indicesPerStep){
|
andrew@0
|
42 j = round(exactIndex);
|
andrew@0
|
43 i = j - minIndex;
|
andrew@0
|
44
|
andrew@0
|
45 if (j < energyVec.size()){
|
andrew@0
|
46 xPosition = window.x + i*pixelsPerStep;
|
andrew@0
|
47 newHeight = window.y + screenHeight - (energyVec[j]*heightScalar);
|
andrew@0
|
48
|
andrew@0
|
49 ofLine(lastXposition, lastHeight, xPosition, newHeight);
|
andrew@0
|
50
|
andrew@0
|
51 lastHeight = newHeight;
|
andrew@0
|
52 lastXposition = xPosition;
|
andrew@0
|
53
|
andrew@0
|
54 }
|
andrew@0
|
55 }
|
andrew@0
|
56 }
|
andrew@0
|
57
|
andrew@0
|
58
|
andrew@0
|
59 void ofxPlotFunction::drawVector(DoubleVector& energyVec, int minIndex, int maxIndex, const ofxWindowRegion& window, const double& maxResolution){
|
andrew@0
|
60
|
andrew@0
|
61
|
andrew@0
|
62 minIndex = max(0, minIndex);
|
andrew@0
|
63 maxIndex = min((int)energyVec.size()-1, maxIndex);
|
andrew@0
|
64 int tmpTwenty = 20;
|
andrew@0
|
65 double maximumValue = 1.1*getMaximum(energyVec, minIndex, maxIndex, tmpTwenty);
|
andrew@0
|
66 drawVector(energyVec, minIndex, maxIndex, window, maxResolution, maximumValue);
|
andrew@0
|
67
|
andrew@0
|
68 }
|
andrew@0
|
69
|
andrew@0
|
70
|
andrew@0
|
71
|
andrew@0
|
72 void ofxPlotFunction::drawArray(float* energyArray, int minIndex, int maxIndex, const ofxWindowRegion& window, const double& maxResolution){
|
andrew@0
|
73
|
andrew@0
|
74 minIndex = max(0, minIndex);
|
andrew@0
|
75 // maxIndex = min((int)sizeof(energyArray)-1, maxIndex);
|
andrew@0
|
76 int tmpStepVal = 100;
|
andrew@0
|
77 double maximumValue = 1;
|
andrew@0
|
78 if (maxIndex > minIndex)
|
andrew@0
|
79 maximumValue = getMaximumArray(energyArray, minIndex, maxIndex, tmpStepVal);
|
andrew@0
|
80
|
andrew@0
|
81 drawArray(energyArray, minIndex, maxIndex, window, maxResolution, maximumValue);
|
andrew@0
|
82 }
|
andrew@0
|
83
|
andrew@0
|
84 void ofxPlotFunction::drawArray(float* energyArray, int minIndex, int maxIndex, const ofxWindowRegion& window, const double& maxNumberOfIndexPoints, const double& maximumValue){
|
andrew@0
|
85
|
andrew@0
|
86
|
andrew@0
|
87 float screenHeight = window.height;
|
andrew@0
|
88 float screenWidth = window.width;
|
andrew@0
|
89
|
andrew@0
|
90 double numberOfIndexPoints= min(maxNumberOfIndexPoints, (double) maxIndex - minIndex);
|
andrew@0
|
91 int indicesPerStep = (int) round((maxIndex - minIndex) / numberOfIndexPoints);
|
andrew@0
|
92 indicesPerStep = max(1, indicesPerStep);
|
andrew@0
|
93 double pixelsPerStep = window.width / numberOfIndexPoints;
|
andrew@0
|
94
|
andrew@0
|
95 string pixelstring = ofToString(indicesPerStep);
|
andrew@0
|
96 ofDrawBitmapString("max val "+pixelstring, 500,100);
|
andrew@0
|
97
|
andrew@0
|
98 int i, j;
|
andrew@0
|
99
|
andrew@0
|
100 double heightScalar = window.height / (1.2*maximumValue);
|
andrew@0
|
101
|
andrew@0
|
102 int lastHeight = window.y + screenHeight - (energyArray[minIndex]*heightScalar);;
|
andrew@0
|
103 int newHeight;
|
andrew@0
|
104 int xPosition;
|
andrew@0
|
105 int lastXposition = window.x;
|
andrew@0
|
106
|
andrew@0
|
107 int exactIndex;
|
andrew@0
|
108 for (exactIndex = minIndex - minIndex%indicesPerStep; exactIndex < maxIndex; exactIndex += indicesPerStep){
|
andrew@0
|
109 j = exactIndex;// - exactIndex % indicesPerStep ;
|
andrew@0
|
110 i = j - minIndex;
|
andrew@0
|
111
|
andrew@0
|
112 xPosition = window.x + i*pixelsPerStep;
|
andrew@0
|
113 if (j >= 0)
|
andrew@0
|
114 newHeight = window.y + screenHeight - (energyArray[j]*heightScalar);
|
andrew@0
|
115
|
andrew@0
|
116 if (j < 16)
|
andrew@0
|
117 cout << "j is " << j << ", i is " << i << endl;
|
andrew@0
|
118
|
andrew@0
|
119 ofLine(lastXposition, lastHeight, xPosition, newHeight);
|
andrew@0
|
120 // window.x+screenWidth*(i+1)/scrollWidth, window.y + screenHeight - (energyVec[j]*screenHeight/heightFactor));
|
andrew@0
|
121
|
andrew@0
|
122 lastHeight = newHeight;
|
andrew@0
|
123 lastXposition = xPosition;
|
andrew@0
|
124
|
andrew@0
|
125
|
andrew@0
|
126 }
|
andrew@0
|
127
|
andrew@0
|
128
|
andrew@0
|
129 }
|
andrew@0
|
130
|
andrew@0
|
131
|
andrew@0
|
132 double ofxPlotFunction::getMaximum(DoubleVector& energyVec, const int& minIndex, const int& maxIndex, int& numberOfSteps){
|
andrew@0
|
133
|
andrew@0
|
134 int step = max(1, (int)((maxIndex - minIndex) / (float) numberOfSteps));
|
andrew@0
|
135 double maximumValue = energyVec[minIndex];
|
andrew@0
|
136 int index = minIndex;
|
andrew@0
|
137 while (index < maxIndex && index < energyVec.size()){
|
andrew@0
|
138 index += step;
|
andrew@0
|
139 if (energyVec[index] > maximumValue){
|
andrew@0
|
140 maximumValue = energyVec[index];
|
andrew@0
|
141 }
|
andrew@0
|
142 }
|
andrew@0
|
143 return maximumValue;
|
andrew@0
|
144
|
andrew@0
|
145 }
|
andrew@0
|
146
|
andrew@0
|
147
|
andrew@0
|
148
|
andrew@0
|
149 double ofxPlotFunction::getMaximumArray(float* energyArray, const int& minIndex, const int& maxIndex, int& numberOfSteps){
|
andrew@0
|
150
|
andrew@0
|
151 int step = max(1, (int)((maxIndex - minIndex) / (float) numberOfSteps));
|
andrew@0
|
152 double maximumValue = energyArray[minIndex];
|
andrew@0
|
153 int index = minIndex;
|
andrew@0
|
154 while (index < maxIndex ){//&& index < sizeof(energyArray)
|
andrew@0
|
155 index += step;
|
andrew@0
|
156 if (energyArray[index] > maximumValue){
|
andrew@0
|
157 maximumValue = energyArray[index];
|
andrew@0
|
158 }
|
andrew@0
|
159 }
|
andrew@0
|
160 return maximumValue;
|
andrew@0
|
161
|
andrew@0
|
162 }
|
andrew@0
|
163
|