annotate src/Modules/Output/Graphics/GraphicsViewTime.cc @ 118:18237d55e346

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