Mercurial > hg > multitrack-audio-matcher
view 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 |
line wrap: on
line source
/* * PlotTools.cpp * annotationResultCalculator * * Created by Andrew on 04/05/2012. * Copyright 2012 QMUL. All rights reserved. * */ //To DO //need to fix axes #include "PlotTools.h" PlotTools::PlotTools(){ plotPoints = true; } void PlotTools::plotVector(const DoubleVector& d){ plotVector(d, 0, d.size()-1, *(std::min_element( d.begin(), d.end() )),*(std::max_element( d.begin(), d.end()) )); } void PlotTools::plotVector(const DoubleVector& d, const double& xStart, const double& xEnd, const double& yStart, const double& yEnd){ // printf("%f %f %f %f\n", xStart, xEnd, yStart, yEnd); int startX, endX;//as indices not values startX = max(0, (int) round(xStart)); endX = min((int)round(xEnd), (int)d.size()-1); double yRange = yEnd - yStart; double xRange = xEnd - xStart; if (yRange > 0){//need this to plot double heightRatio = (double)ofGetHeight() / (double)yRange; double height = d[startX] - yStart; height *= heightRatio; screenHeight = ofGetHeight(); double xRatio = (double)ofGetWidth() / (double)xRange; int xIndex = (int)(xRatio * (startX - xStart)); for (int i = startX+1; i < endX; i++){ double newHeight = (d[i] - yStart) * heightRatio; double newXindex = (int)(xRatio * (i - xStart)); ofLine(xIndex, getY(height), xIndex + xRatio, getY(newHeight)); xIndex = newXindex; height = newHeight; }//end for }//end if yRange > 0 } void PlotTools::plotTwoVectors(const DoubleVector& d, const DoubleVector& h){ //just picks the min and max of each and plots with these as the width and height ranges. plotTwoVectors(d, h, *(std::min_element( d.begin(), d.end() )),*(std::max_element( d.begin(), d.end()) ), *(std::min_element( h.begin(), h.end() )),*(std::max_element( h.begin(), h.end()) ), false); } void PlotTools::plotTwoVectors(const DoubleVector& d, const DoubleVector& h, const double& xStart, const double& xEnd, const double& yStart, const double& yEnd, const bool drawAxes){ // printf("%f %f %f %f\n", xStart, xEnd, yStart, yEnd); int startX, endX, startY, endY; startX = 0; while (d[startX] < xStart) startX++; startX--; startX = max(0, (int) round(xStart)); endX = (int)d.size()-1; while (d[endX] > xEnd) { endX--; } if (endX < d.size()-1) endX++; //startX and endX are in terms of indices now... double yRange = yEnd - yStart; double xRange = xEnd - xStart; screenWidth = ofGetWidth(); if (yRange > 0){//need this to plot screenHeight = ofGetHeight(); double heightRatio = (double) screenHeight / (double)yRange; double yIndex = (int)((h[startX] - yStart) * heightRatio); double xRatio = (double)ofGetWidth() / (double)xRange; double xIndex = (int)(xRatio * (d[startX] - xStart)); for (int i = startX+1; i <= endX && i < h.size(); i++){ // printf("Plotting x %f, y %f\n", d[i], h[i]); double newYindex = (h[i] - yStart) * heightRatio; double newXindex = (int)(xRatio * (d[i] - xStart)); ofLine(xIndex, getY(yIndex), newXindex, getY(newYindex)); if (plotPoints) ofCircle(newXindex, getY(newYindex), 1);//then plots points too xIndex = newXindex; yIndex = newYindex; }//end for //do axis if (drawAxes){ int wIndex, hIndex; int numberOfPts = 5; for (int x = 1; x < numberOfPts;x++){ wIndex = 0; double val = xStart + (xRange * x/numberOfPts); while (d[wIndex] < val) wIndex++; int markIndex = round(screenWidth * x/numberOfPts); ofLine(markIndex, screenHeight, markIndex, screenHeight-10); ofDrawBitmapString(ofToString(d[wIndex]*1000.0, 0), markIndex, screenHeight - 20); }//end for x for axis for (int y = 1; y < numberOfPts;y++){ hIndex = 0; double val = yStart + (yRange * y/numberOfPts); while (h[hIndex] < val) hIndex++; int markIndex = getY(round(screenHeight * y/numberOfPts)); ofLine(0, markIndex, 10, markIndex); ofDrawBitmapString(ofToString(h[hIndex]*1000.0, 0), 2, markIndex); }//end for y for axis }//end draw axes }//end if yRange > 0 } int PlotTools::getY(const int& y){ return screenHeight - y; }