annotate trunk/src/Modules/Output/Graphics/GraphicsView.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, Willem van Engen
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 #include "Support/Common.h"
tomwalters@397 19
tomwalters@397 20 #include "Output/GraphicsView.h"
tomwalters@397 21 #include "Output/GraphicsOutputDevice.h"
tomwalters@397 22
tomwalters@397 23 GraphicsView::GraphicsView(Parameters *parameters) : Module(parameters) {
tomwalters@397 24 module_description_ = "";
tomwalters@397 25 module_identifier_ = "graphics";
tomwalters@397 26 module_type_ = "output";
tomwalters@397 27 module_version_ = "$Id: $";
tomwalters@397 28
tomwalters@397 29 m_pDev = NULL;
tomwalters@397 30 m_bPlotLabels = false;
tomwalters@397 31 m_pAxisX = new GraphAxisSpec();
tomwalters@397 32 AIM_ASSERT(m_pAxisX);
tomwalters@397 33 m_pAxisY = new GraphAxisSpec();
tomwalters@397 34 AIM_ASSERT(m_pAxisY);
tomwalters@397 35 m_pAxisFreq = new GraphAxisSpec();
tomwalters@397 36 AIM_ASSERT(m_pAxisFreq);
tomwalters@397 37
tomwalters@397 38 m_pAxisY->Initialize(m_pParam,
tomwalters@397 39 _S("graph.y"),
tomwalters@397 40 -1,
tomwalters@397 41 1,
tomwalters@397 42 Scale::SCALE_LINEAR);
tomwalters@397 43 m_fMarginLeft = m_pParam->GetFloat(_S("graph.margin.left"));
tomwalters@397 44 m_fMarginRight = m_pParam->GetFloat(_S("graph.margin.right"));
tomwalters@397 45 m_fMarginTop = m_pParam->GetFloat(_S("graph.margin.top"));
tomwalters@397 46 m_fMarginBottom = m_pParam->GetFloat(_S("graph.margin.bottom"));
tomwalters@397 47 m_bPlotLabels = m_pParam->GetBool(_S("graph.plotlabels"));
tomwalters@397 48
tomwalters@397 49 const char *sGraphType = m_pParam->GetString(_S("graph.type"));
tomwalters@397 50 if (strcmp(sGraphType, _S("line"))==0)
tomwalters@397 51 m_iGraphType = GraphTypeLine;
tomwalters@397 52 else if (strcmp(sGraphType, _S("colormap"))==0)
tomwalters@397 53 m_iGraphType = GraphTypeColormap;
tomwalters@397 54 else if (strcmp(sGraphType, _S("none"))==0)
tomwalters@397 55 m_iGraphType = GraphTypeNone;
tomwalters@397 56 else {
tomwalters@397 57 ret = false;
tomwalters@397 58 AIM_ERROR(_T("Unrecognized graph type: '%s'"), sGraphType);
tomwalters@397 59 }
tomwalters@397 60
tomwalters@397 61 if (strcmp(m_pParam->GetString(_S("graph.mindistance")),"auto") == 0)
tomwalters@397 62 // -1 means detect later, based on type and Fire() argument
tomwalters@397 63 m_fMinPlotDistance = -1;
tomwalters@397 64 else
tomwalters@397 65 m_fMinPlotDistance = m_pParam->GetFloat(_S("graph.mindistance"));
tomwalters@397 66
tomwalters@397 67 }
tomwalters@397 68
tomwalters@397 69 GraphicsView::~GraphicsView() {
tomwalters@397 70 DELETE_IF_NONNULL(m_pAxisX);
tomwalters@397 71 DELETE_IF_NONNULL(m_pAxisY);
tomwalters@397 72 DELETE_IF_NONNULL(m_pAxisFreq);
tomwalters@397 73 }
tomwalters@397 74
tomwalters@397 75 bool
tomwalters@397 76
tomwalters@397 77 bool GraphicsView::InitializeInternal(const SignalBank &bank) {
tomwalters@397 78 if (!m_pDev) {
tomwalters@397 79 LOG_ERROR("Output device not connected");
tomwalters@397 80 return false;
tomwalters@397 81 }
tomwalters@397 82
tomwalters@397 83 float y_min = bank.centre_frequency(0);
tomwalters@397 84 float y_max = bank.centre_frequency(bank.channel_count() - 1);
tomwalters@397 85 if (!m_pAxisFreq->Initialize(m_pParam,
tomwalters@397 86 "graph.freq",
tomwalters@397 87 y_min,
tomwalters@397 88 y_max,
tomwalters@397 89 Scale::SCALE_ERB)) {
tomwalters@397 90 LOG_ERROR("");
tomwalters@397 91 return false;
tomwalters@397 92 }
tomwalters@397 93
tomwalters@397 94 float x_min = 0.0;
tomwalters@397 95 float x_max = 1000.0 * bank.buffer_length() / bank.sample_rate();
tomwalters@397 96 if (!m_pAxisX->Initialize(m_pParam,
tomwalters@397 97 "graph.x",
tomwalters@397 98 x_min,
tomwalters@397 99 x_max,
tomwalters@397 100 Scale::SCALE_LINEAR)) {
tomwalters@397 101 LOG_ERROR("");
tomwalters@397 102 return false;
tomwalters@397 103 }
tomwalters@397 104
tomwalters@397 105 /* Inform graphics output of maximum number of vertices between
tomwalters@397 106 * gBegin*() and gEnd(), for any type of plot. Colormap needs most.
tomwalters@397 107 */
tomwalters@397 108 if (!m_pDev->Initialize(MAX(10, bank.buffer_length() * 2 + 2))) {
tomwalters@397 109 LOG_ERROR("");
tomwalters@397 110 return false;
tomwalters@397 111 }
tomwalters@397 112 }
tomwalters@397 113
tomwalters@397 114 void GraphicsView::Process(const SignalBank &bank) {
tomwalters@397 115 float height = 1.0 / bank.channel_count();
tomwalters@397 116 float heightMinMargin = height * (1.0f - m_fMarginBottom - m_fMarginTop);
tomwalters@397 117 float xScaling = 1.0f;
tomwalters@397 118
tomwalters@397 119 m_pDev->gGrab();
tomwalters@397 120 PlotAxes(bank);
tomwalters@397 121 m_pDev->gColor3f(1.0f, 1.0f, 0.8f);
tomwalters@397 122 for (int i = 0; i < bank.channel_count(); i++) {
tomwalters@397 123 float yOffs = bank.centre_frequency(i);
tomwalters@397 124 yOffs = m_pAxisFreq->m_pScale->FromLinearScaled(yOffs) + 0.5f;
tomwalters@397 125 /* Don't plot below zero and stop when above 1, since yOffs is
tomwalters@397 126 * monotonically increasing. Because of rounding errors, we need
tomwalters@397 127 * to check for yOffs < -1e-6 instead of yOffs < 0. */
tomwalters@397 128 if (yOffs < -1e-6)
tomwalters@397 129 continue;
tomwalters@397 130 if (yOffs > 1)
tomwalters@397 131 break;
tomwalters@397 132
tomwalters@397 133 // Scale to single channel graphing.
tomwalters@397 134 yOffs = yOffs * (1.0f - height) + height / 2.0;
tomwalters@397 135 yOffs = yOffs * (1.0f - m_fMarginTop - m_fMarginBottom) + m_fMarginBottom;
tomwalters@397 136 PlotData(bank[i], yOffs, heightMinMargin, xScaling);
tomwalters@397 137 }
tomwalters@397 138 m_pDev->gRelease();
tomwalters@397 139 }
tomwalters@397 140
tomwalters@397 141 void GraphicsView::SetAxisScale(Scale::ScaleType iHori,
tomwalters@397 142 Scale::ScaleType iVert,
tomwalters@397 143 Scale::ScaleType iFreq) {
tomwalters@397 144 AIM_ASSERT(m_pAxisX);
tomwalters@397 145 AIM_ASSERT(m_pAxisY);
tomwalters@397 146 AIM_ASSERT(m_pAxisFreq);
tomwalters@397 147 m_pAxisX->SetDisplayScale(iHori);
tomwalters@397 148 m_pAxisY->SetDisplayScale(iVert);
tomwalters@397 149 m_pAxisFreq->SetDisplayScale(iFreq);
tomwalters@397 150 }
tomwalters@397 151
tomwalters@397 152 void GraphicsView::BeginDataStrip() {
tomwalters@397 153 m_iPrevValEqual=0;
tomwalters@397 154 m_bFirstPoint=true;
tomwalters@397 155 switch(m_iGraphType) {
tomwalters@397 156 case GraphTypeLine:
tomwalters@397 157 m_pDev->gBeginLineStrip();
tomwalters@397 158 break;
tomwalters@397 159 case GraphTypeColormap:
tomwalters@397 160 m_pDev->gBeginQuadStrip();
tomwalters@397 161 break;
tomwalters@397 162 case GraphTypeNone:
tomwalters@397 163 // Nothing: just for testing computation overhead of graphing
tomwalters@397 164 break;
tomwalters@397 165 }
tomwalters@397 166 }
tomwalters@397 167
tomwalters@397 168 void GraphicsView::PlotDataPoint(float x,
tomwalters@397 169 float y,
tomwalters@397 170 float val,
tomwalters@397 171 float height,
tomwalters@397 172 bool isLast) {
tomwalters@397 173 AIM_ASSERT(m_pDev);
tomwalters@397 174
tomwalters@397 175 /* Reduce the number of points plotted by eliminating duplicate values:
tomwalters@397 176 *
tomwalters@397 177 * oooo o--o
tomwalters@397 178 * o / o
tomwalters@397 179 * oooo ooo => o--o o--o
tomwalters@397 180 * oooo ooo o--o o-o
tomwalters@397 181 *
tomwalters@397 182 * with 'o' points that are plotted, and '-' by the graphics output
tomwalters@397 183 * device interpolated points. We could be smarter and include
tomwalters@397 184 * first-order interpolation, but we leave that as an exercise for
tomwalters@397 185 * the reader. Please send your patches :)
tomwalters@397 186 *
tomwalters@397 187 * So, we don't draw points that are too close to the previous value.
tomwalters@397 188 * But if the value changes (or it's the last point), we must draw the
tomwalters@397 189 * previous point too.
tomwalters@397 190 */
tomwalters@397 191 if (!m_bFirstPoint
tomwalters@397 192 && !isLast
tomwalters@397 193 && fabs(m_fPrevVal-val) < m_fMinPlotDistance) {
tomwalters@397 194 m_iPrevValEqual++;
tomwalters@397 195 // Don't set m_fPrevVal to avoid not catching slow changes
tomwalters@397 196 m_fPrevX = x;
tomwalters@397 197 m_fPrevY = y;
tomwalters@397 198 m_fPrevHeight = height;
tomwalters@397 199 return;
tomwalters@397 200 } else {
tomwalters@397 201 if (m_iPrevValEqual > 0) {
tomwalters@397 202 // Draw previous point
tomwalters@397 203 PlotDataPointDirect(m_fPrevX, m_fPrevY, m_fPrevVal, m_fPrevHeight);
tomwalters@397 204 }
tomwalters@397 205 m_iPrevValEqual = 0;
tomwalters@397 206 m_fPrevVal = val;
tomwalters@397 207 m_bFirstPoint = false;
tomwalters@397 208 }
tomwalters@397 209 PlotDataPointDirect(x, y, val, height);
tomwalters@397 210 }
tomwalters@397 211
tomwalters@397 212 void GraphicsView::PlotDataPointDirect(float x,
tomwalters@397 213 float y,
tomwalters@397 214 float val,
tomwalters@397 215 float height) {
tomwalters@397 216 // Draw it in the right way
tomwalters@397 217 switch(m_iGraphType) {
tomwalters@397 218 case GraphTypeLine:
tomwalters@397 219 m_pDev->gVertex2f(x, y + val * height);
tomwalters@397 220 break;
tomwalters@397 221 case GraphTypeColormap:
tomwalters@397 222 //! \todo make it a real colormap instead of grayscale
tomwalters@397 223 m_pDev->gColor3f(val + 0.5, val + 0.5, val + 0.5);
tomwalters@397 224 m_pDev->gVertex2f(x, y - height / 2);
tomwalters@397 225 m_pDev->gVertex2f(x, y + height / 2);
tomwalters@397 226 break;
tomwalters@397 227 case GraphTypeNone:
tomwalters@397 228 // Nothing: just for testing computation overhead of graphing
tomwalters@397 229 break;
tomwalters@397 230 default:
tomwalters@397 231 // Shouldn't happen
tomwalters@397 232 AIM_ASSERT(0);
tomwalters@397 233 }
tomwalters@397 234 }