view annotationCalculatorSrc/PlotTools.cpp @ 43:b7ad807c9cde

Added annotation writing and the src for the result calculator
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Fri, 04 May 2012 15:33:36 +0100
parents
children d23685b9e766
line wrap: on
line source
/*
 *  PlotTools.cpp
 *  annotationResultCalculator
 *
 *  Created by Andrew on 04/05/2012.
 *  Copyright 2012 QMUL. All rights reserved.
 *
 */

#include "PlotTools.h"

PlotTools::PlotTools(){

}

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()) ) );
	
}
void PlotTools::plotTwoVectors(const DoubleVector& d, const DoubleVector& h, 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, startY, endY;
	startX = 0;
	while (d[startX] < xStart)
		startX++;
	
	startX = max(0, (int) round(xStart));
	endX = (int)d.size()-1;
	
	while (d[endX] > xEnd) {
		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));
			xIndex = newXindex;
			yIndex = newYindex;
		}//end for
	
		
	//do axis
		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], 1), 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], 1), 2, markIndex);
		}//end for y for axis
		
	}//end if yRange > 0
	
	
	
	
}


int PlotTools::getY(const int& y){
	return screenHeight - y;
}