annotate src/Modules/Output/Graphics/GraphicsView.cc @ 185:550d0d6bffe5

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