annotate trunk/src/Modules/Output/Graphics/GraphicsViewTime.cc @ 397:7a573750b186

- First add of a lot of graphics code from the old version. Not working yet, not even compiling yet.
author tomwalters
date Fri, 15 Oct 2010 05:40:53 +0000
parents
children 3ee03a6b95a0
rev   line source
tomwalters@397 1 // Copyright 2006-2010, Willem van Engen, Thomas Walters
tomwalters@397 2 //
tomwalters@397 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@397 4 // http://www.acousticscale.org/AIMC
tomwalters@397 5 //
tomwalters@397 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@397 7 // you may not use this file except in compliance with the License.
tomwalters@397 8 // You may obtain a copy of the License at
tomwalters@397 9 //
tomwalters@397 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@397 11 //
tomwalters@397 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@397 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@397 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@397 15 // See the License for the specific language governing permissions and
tomwalters@397 16 // limitations under the License.
tomwalters@397 17
tomwalters@397 18 /*!
tomwalters@397 19 * \file
tomwalters@397 20 * \brief Time-representation graphics view
tomwalters@397 21 *
tomwalters@397 22 * \author Willem van Engen <cnbh@willem.engen.nl>
tomwalters@397 23 * \date created 2006/09/26
tomwalters@397 24 * \version \$Id: GraphicsViewTime.cpp 607 2008-06-25 00:14:14Z tom $
tomwalters@397 25 */
tomwalters@397 26
tomwalters@397 27 #include "Support/Common.h"
tomwalters@397 28
tomwalters@397 29 #include <stdio.h>
tomwalters@397 30
tomwalters@397 31 #include "Support/SignalBank.h"
tomwalters@397 32 #include "Modules/Output/Graphics/GraphicsView.h"
tomwalters@397 33 #include "Modules/Output/Graphics/GraphicsViewTime.h"
tomwalters@397 34
tomwalters@397 35 GraphicsViewTime::GraphicsViewTime(Parameters *pParam)
tomwalters@397 36 : GraphicsView(pParam) {
tomwalters@397 37 }
tomwalters@397 38
tomwalters@397 39 GraphicsViewTime *GraphicsViewTime::Clone(GraphicsOutputDevice *pDev) {
tomwalters@397 40 GraphicsViewTime *pView = new GraphicsViewTime(m_pParam);
tomwalters@397 41 // Copy everything
tomwalters@397 42 pView->m_pAxisX->SetDisplayRange(m_pAxisX->m_fMax, m_pAxisX->m_fMin);
tomwalters@397 43 pView->m_pAxisX->SetDisplayScale(m_pAxisX->m_pScale->getType());
tomwalters@397 44 pView->m_pAxisY->SetDisplayRange(m_pAxisY->m_fMax, m_pAxisY->m_fMin);
tomwalters@397 45 pView->m_pAxisY->SetDisplayScale(m_pAxisY->m_pScale->getType());
tomwalters@397 46 pView->m_pAxisFreq->SetDisplayRange(m_pAxisFreq->m_fMax, m_pAxisFreq->m_fMin);
tomwalters@397 47 pView->m_pAxisFreq->SetDisplayScale(m_pAxisFreq->m_pScale->getType());
tomwalters@397 48 return pView;
tomwalters@397 49 }
tomwalters@397 50
tomwalters@397 51 void GraphicsViewTime::PlotAxes(const SignalBank &bank) {
tomwalters@397 52 m_pDev->gColor3f(0.0f, 0.7f, 0.7f);
tomwalters@397 53 // Vertical axis
tomwalters@397 54 m_pDev->gBeginLineStrip();
tomwalters@397 55 m_pDev->gVertex2f(m_fMarginLeft, m_fMarginBottom);
tomwalters@397 56 m_pDev->gVertex2f(m_fMarginLeft, 1.0f - m_fMarginTop);
tomwalters@397 57 m_pDev->gEnd();
tomwalters@397 58 // Horizontal axis
tomwalters@397 59 m_pDev->gBeginLineStrip();
tomwalters@397 60 m_pDev->gVertex2f(m_fMarginLeft, m_fMarginBottom);
tomwalters@397 61 m_pDev->gVertex2f(1.0f-m_fMarginRight, m_fMarginBottom);
tomwalters@397 62 m_pDev->gEnd();
tomwalters@397 63
tomwalters@397 64 if (!m_bPlotLabels)
tomwalters@397 65 return;
tomwalters@397 66
tomwalters@397 67 // Labels
tomwalters@397 68 char sTxt[80];
tomwalters@397 69 snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]),
tomwalters@397 70 _S("%s [%.0f..%.0f Hz, %s scale]"),
tomwalters@397 71 m_pAxisFreq->m_sLabel ? m_pAxisFreq->m_sLabel : "",
tomwalters@397 72 m_pAxisFreq->m_fMin, m_pAxisFreq->m_fMax,
tomwalters@397 73 m_pAxisFreq->m_pScale->getName());
tomwalters@397 74 m_pDev->gText2f(0.0025f, 0.35f, sTxt, true);
tomwalters@397 75 if (m_bPlotScaled) {
tomwalters@397 76 snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]),
tomwalters@397 77 _S("%s [cycles, %s scale]"),
tomwalters@397 78 m_pAxisX->m_sLabel ? m_pAxisX->m_sLabel : "",
tomwalters@397 79 m_pAxisX->m_pScale->getName());
tomwalters@397 80 } else {
tomwalters@397 81 snprintf(sTxt, sizeof(sTxt) / sizeof(sTxt[0]),
tomwalters@397 82 _S("%s [%.2f..%.2f ms, %s scale]"),
tomwalters@397 83 m_pAxisX->m_sLabel ? m_pAxisX->m_sLabel : "",
tomwalters@397 84 m_pAxisX->m_fMin,
tomwalters@397 85 m_pAxisX->m_fMax,
tomwalters@397 86 m_pAxisX->m_pScale->getName());
tomwalters@397 87 }
tomwalters@397 88 m_pDev->gText2f(m_fMarginLeft, 0.0025f, sTxt, false);
tomwalters@397 89
tomwalters@397 90 // Frame time
tomwalters@397 91 snprintf(sTxt, sizeof(sTxt)/sizeof(sTxt[0]), _S("t=%.0f ms"),
tomwalters@397 92 pBank->getSampleTime(0));
tomwalters@397 93 m_pDev->gText2f(0.8f, 0.0025f, sTxt, false);
tomwalters@397 94 }
tomwalters@397 95
tomwalters@397 96 void GraphicsViewTime::PlotData(const vector<float> &signal,
tomwalters@397 97 float sample_rate,
tomwalters@397 98 float yOffset,
tomwalters@397 99 float height,
tomwalters@397 100 float xScale) {
tomwalters@397 101 AIM_ASSERT(pSig);
tomwalters@397 102 AIM_ASSERT(xScale >= 0 && xScale <= 1);
tomwalters@397 103 AIM_ASSERT(height > 0 && height <= 1);
tomwalters@397 104 AIM_ASSERT(yOffset >= 0 && yOffset <= 1);
tomwalters@397 105 AIM_ASSERT(m_pAxisX && m_pAxisX->m_pScale);
tomwalters@397 106 AIM_ASSERT(m_pAxisY && m_pAxisY->m_pScale);
tomwalters@397 107
tomwalters@397 108 // Make sure we get time is ms as x value
tomwalters@397 109 xScale *= 1000.0 / sample_rate;
tomwalters@397 110 m_pDev->gColor3f(1.0f, 1.0f, 0.8f);
tomwalters@397 111 BeginDataStrip();
tomwalters@397 112
tomwalters@397 113 // Draw the signal.
tomwalters@397 114 float x = 0;
tomwalters@397 115 float y = 0;
tomwalters@397 116 for (int i = 0; i < signal.size(); i++) {
tomwalters@397 117 // Find out where to draw and do so
tomwalters@397 118 x = xScale * i;
tomwalters@397 119 y = signal[i];
tomwalters@397 120 x = m_pAxisX->m_pScale->FromLinearScaled(x) + 0.5f;
tomwalters@397 121 y = m_pAxisY->m_pScale->FromLinearScaled(y);
tomwalters@397 122
tomwalters@397 123 if (x < 0.0)
tomwalters@397 124 continue;
tomwalters@397 125
tomwalters@397 126 // Now fit it into the drawing area
tomwalters@397 127 x = x * (1.0f - m_fMarginLeft - m_fMarginRight) + m_fMarginLeft; // fit inside x-axis area
tomwalters@397 128 PlotDataPoint(x, yOffset, y, height, false);
tomwalters@397 129 /* There's no point in drawing anything when x>1.0, outside of view.
tomwalters@397 130 * x is continuously increasing, so we can just stop completely. We need to
tomwalters@397 131 * plot this point however, to finish the final line. */
tomwalters@397 132 if (x > 1.0)
tomwalters@397 133 break;
tomwalters@397 134 }
tomwalters@397 135 // Redraw the last point in case it's needed
tomwalters@397 136 PlotDataPoint(x, yOffset, y, height, true);
tomwalters@397 137
tomwalters@397 138 m_pDev->gEnd();
tomwalters@397 139 }