annotate annotationCalculatorSrc/PlotTools.cpp @ 56:4394c9490716 tip

minor changes
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Mon, 24 Dec 2012 18:58:39 +0000
parents 5359e2c0b0fb
children
rev   line source
andrew@43 1 /*
andrew@43 2 * PlotTools.cpp
andrew@43 3 * annotationResultCalculator
andrew@43 4 *
andrew@43 5 * Created by Andrew on 04/05/2012.
andrew@43 6 * Copyright 2012 QMUL. All rights reserved.
andrew@43 7 *
andrew@43 8 */
andrew@43 9
andrew@48 10 //To DO
andrew@48 11 //need to fix axes
andrew@48 12
andrew@43 13 #include "PlotTools.h"
andrew@43 14
andrew@43 15 PlotTools::PlotTools(){
andrew@45 16 plotPoints = true;
andrew@43 17 }
andrew@43 18
andrew@43 19 void PlotTools::plotVector(const DoubleVector& d){
andrew@43 20 plotVector(d, 0, d.size()-1, *(std::min_element( d.begin(), d.end() )),*(std::max_element( d.begin(), d.end()) ));
andrew@43 21
andrew@43 22
andrew@43 23 }
andrew@43 24
andrew@43 25 void PlotTools::plotVector(const DoubleVector& d, const double& xStart, const double& xEnd, const double& yStart, const double& yEnd){
andrew@43 26 // printf("%f %f %f %f\n", xStart, xEnd, yStart, yEnd);
andrew@43 27
andrew@43 28 int startX, endX;//as indices not values
andrew@43 29
andrew@43 30 startX = max(0, (int) round(xStart));
andrew@43 31 endX = min((int)round(xEnd), (int)d.size()-1);
andrew@43 32 double yRange = yEnd - yStart;
andrew@43 33 double xRange = xEnd - xStart;
andrew@43 34
andrew@43 35 if (yRange > 0){//need this to plot
andrew@43 36
andrew@43 37 double heightRatio = (double)ofGetHeight() / (double)yRange;
andrew@43 38 double height = d[startX] - yStart;
andrew@43 39 height *= heightRatio;
andrew@43 40 screenHeight = ofGetHeight();
andrew@43 41 double xRatio = (double)ofGetWidth() / (double)xRange;
andrew@43 42 int xIndex = (int)(xRatio * (startX - xStart));
andrew@43 43 for (int i = startX+1; i < endX; i++){
andrew@43 44 double newHeight = (d[i] - yStart) * heightRatio;
andrew@43 45 double newXindex = (int)(xRatio * (i - xStart));
andrew@43 46 ofLine(xIndex, getY(height), xIndex + xRatio, getY(newHeight));
andrew@43 47 xIndex = newXindex;
andrew@43 48 height = newHeight;
andrew@43 49 }//end for
andrew@43 50
andrew@43 51 }//end if yRange > 0
andrew@43 52
andrew@43 53 }
andrew@43 54
andrew@43 55 void PlotTools::plotTwoVectors(const DoubleVector& d, const DoubleVector& h){
andrew@43 56 //just picks the min and max of each and plots with these as the width and height ranges.
andrew@43 57 plotTwoVectors(d, h, *(std::min_element( d.begin(), d.end() )),*(std::max_element( d.begin(), d.end()) ),
andrew@45 58 *(std::min_element( h.begin(), h.end() )),*(std::max_element( h.begin(), h.end()) ), false);
andrew@43 59
andrew@43 60 }
andrew@45 61 void PlotTools::plotTwoVectors(const DoubleVector& d, const DoubleVector& h, const double& xStart, const double& xEnd, const double& yStart, const double& yEnd, const bool drawAxes){
andrew@43 62 // printf("%f %f %f %f\n", xStart, xEnd, yStart, yEnd);
andrew@43 63 int startX, endX, startY, endY;
andrew@43 64 startX = 0;
andrew@43 65 while (d[startX] < xStart)
andrew@43 66 startX++;
andrew@43 67
andrew@45 68 startX--;
andrew@45 69
andrew@43 70 startX = max(0, (int) round(xStart));
andrew@43 71 endX = (int)d.size()-1;
andrew@43 72
andrew@43 73 while (d[endX] > xEnd) {
andrew@43 74 endX--;
andrew@43 75 }
andrew@45 76 if (endX < d.size()-1)
andrew@45 77 endX++;
andrew@43 78
andrew@43 79 //startX and endX are in terms of indices now...
andrew@43 80
andrew@43 81
andrew@43 82 double yRange = yEnd - yStart;
andrew@43 83 double xRange = xEnd - xStart;
andrew@43 84 screenWidth = ofGetWidth();
andrew@43 85
andrew@43 86 if (yRange > 0){//need this to plot
andrew@43 87 screenHeight = ofGetHeight();
andrew@43 88 double heightRatio = (double) screenHeight / (double)yRange;
andrew@43 89 double yIndex = (int)((h[startX] - yStart) * heightRatio);
andrew@43 90
andrew@43 91 double xRatio = (double)ofGetWidth() / (double)xRange;
andrew@43 92 double xIndex = (int)(xRatio * (d[startX] - xStart));
andrew@43 93
andrew@43 94 for (int i = startX+1; i <= endX && i < h.size(); i++){
andrew@43 95 // printf("Plotting x %f, y %f\n", d[i], h[i]);
andrew@43 96 double newYindex = (h[i] - yStart) * heightRatio;
andrew@43 97 double newXindex = (int)(xRatio * (d[i] - xStart));
andrew@43 98 ofLine(xIndex, getY(yIndex), newXindex, getY(newYindex));
andrew@45 99
andrew@45 100
andrew@45 101 if (plotPoints)
andrew@45 102 ofCircle(newXindex, getY(newYindex), 1);//then plots points too
andrew@45 103
andrew@43 104 xIndex = newXindex;
andrew@43 105 yIndex = newYindex;
andrew@43 106 }//end for
andrew@43 107
andrew@43 108
andrew@43 109 //do axis
andrew@45 110 if (drawAxes){
andrew@45 111
andrew@43 112 int wIndex, hIndex;
andrew@43 113 int numberOfPts = 5;
andrew@43 114 for (int x = 1; x < numberOfPts;x++){
andrew@43 115
andrew@43 116 wIndex = 0;
andrew@43 117 double val = xStart + (xRange * x/numberOfPts);
andrew@43 118 while (d[wIndex] < val)
andrew@43 119 wIndex++;
andrew@43 120
andrew@43 121 int markIndex = round(screenWidth * x/numberOfPts);
andrew@43 122 ofLine(markIndex, screenHeight, markIndex, screenHeight-10);
andrew@48 123 ofDrawBitmapString(ofToString(d[wIndex]*1000.0, 0), markIndex, screenHeight - 20);
andrew@43 124 }//end for x for axis
andrew@43 125
andrew@43 126
andrew@43 127 for (int y = 1; y < numberOfPts;y++){
andrew@43 128
andrew@43 129 hIndex = 0;
andrew@43 130 double val = yStart + (yRange * y/numberOfPts);
andrew@43 131 while (h[hIndex] < val)
andrew@43 132 hIndex++;
andrew@43 133
andrew@43 134 int markIndex = getY(round(screenHeight * y/numberOfPts));
andrew@43 135 ofLine(0, markIndex, 10, markIndex);
andrew@48 136 ofDrawBitmapString(ofToString(h[hIndex]*1000.0, 0), 2, markIndex);
andrew@43 137 }//end for y for axis
andrew@45 138 }//end draw axes
andrew@43 139
andrew@43 140 }//end if yRange > 0
andrew@43 141
andrew@43 142
andrew@43 143
andrew@43 144
andrew@43 145 }
andrew@43 146
andrew@43 147
andrew@45 148
andrew@45 149
andrew@45 150
andrew@43 151 int PlotTools::getY(const int& y){
andrew@43 152 return screenHeight - y;
andrew@43 153 }
andrew@43 154
andrew@43 155
andrew@45 156