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