annotate data/model/EditableDenseThreeDimensionalModel.cpp @ 535:3ccf48fb81d6

* make compression optional in editable dense 3d model, and some tweaks
author Chris Cannam
date Fri, 23 Jan 2009 14:00:29 +0000
parents 6038cb6fcd30
children beb51f558e9c
rev   line source
Chris@152 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@152 2
Chris@152 3 /*
Chris@152 4 Sonic Visualiser
Chris@152 5 An audio file viewer and annotation editor.
Chris@152 6 Centre for Digital Music, Queen Mary, University of London.
Chris@202 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@152 8
Chris@152 9 This program is free software; you can redistribute it and/or
Chris@152 10 modify it under the terms of the GNU General Public License as
Chris@152 11 published by the Free Software Foundation; either version 2 of the
Chris@152 12 License, or (at your option) any later version. See the file
Chris@152 13 COPYING included with this distribution for more information.
Chris@152 14 */
Chris@152 15
Chris@152 16 #include "EditableDenseThreeDimensionalModel.h"
Chris@152 17
Chris@478 18 #include "base/LogRange.h"
Chris@478 19
Chris@152 20 #include <QTextStream>
Chris@387 21 #include <QStringList>
Chris@387 22
Chris@181 23 #include <iostream>
Chris@181 24
Chris@256 25 #include <cmath>
Chris@534 26 #include <cassert>
Chris@256 27
Chris@152 28 EditableDenseThreeDimensionalModel::EditableDenseThreeDimensionalModel(size_t sampleRate,
Chris@152 29 size_t resolution,
Chris@152 30 size_t yBinCount,
Chris@535 31 CompressionType compression,
Chris@152 32 bool notifyOnAdd) :
Chris@152 33 m_sampleRate(sampleRate),
Chris@152 34 m_resolution(resolution),
Chris@152 35 m_yBinCount(yBinCount),
Chris@535 36 m_compression(compression),
Chris@152 37 m_minimum(0.0),
Chris@152 38 m_maximum(0.0),
Chris@256 39 m_haveExtents(false),
Chris@152 40 m_notifyOnAdd(notifyOnAdd),
Chris@152 41 m_sinceLastNotifyMin(-1),
Chris@152 42 m_sinceLastNotifyMax(-1),
Chris@152 43 m_completion(100)
Chris@152 44 {
Chris@152 45 }
Chris@152 46
Chris@152 47 bool
Chris@152 48 EditableDenseThreeDimensionalModel::isOK() const
Chris@152 49 {
Chris@152 50 return true;
Chris@152 51 }
Chris@152 52
Chris@152 53 size_t
Chris@152 54 EditableDenseThreeDimensionalModel::getSampleRate() const
Chris@152 55 {
Chris@152 56 return m_sampleRate;
Chris@152 57 }
Chris@152 58
Chris@152 59 size_t
Chris@152 60 EditableDenseThreeDimensionalModel::getStartFrame() const
Chris@152 61 {
Chris@152 62 return 0;
Chris@152 63 }
Chris@152 64
Chris@152 65 size_t
Chris@152 66 EditableDenseThreeDimensionalModel::getEndFrame() const
Chris@152 67 {
Chris@152 68 return m_resolution * m_data.size() + (m_resolution - 1);
Chris@152 69 }
Chris@152 70
Chris@152 71 Model *
Chris@152 72 EditableDenseThreeDimensionalModel::clone() const
Chris@152 73 {
Chris@534 74 QMutexLocker locker(&m_mutex);
Chris@534 75
Chris@152 76 EditableDenseThreeDimensionalModel *model =
Chris@152 77 new EditableDenseThreeDimensionalModel
Chris@535 78 (m_sampleRate, m_resolution, m_yBinCount, m_compression);
Chris@152 79
Chris@152 80 model->m_minimum = m_minimum;
Chris@152 81 model->m_maximum = m_maximum;
Chris@256 82 model->m_haveExtents = m_haveExtents;
Chris@152 83
Chris@152 84 for (size_t i = 0; i < m_data.size(); ++i) {
Chris@533 85 model->setColumn(i, m_data.at(i));
Chris@152 86 }
Chris@152 87
Chris@152 88 return model;
Chris@152 89 }
Chris@152 90
Chris@152 91 size_t
Chris@152 92 EditableDenseThreeDimensionalModel::getResolution() const
Chris@152 93 {
Chris@152 94 return m_resolution;
Chris@152 95 }
Chris@152 96
Chris@152 97 void
Chris@152 98 EditableDenseThreeDimensionalModel::setResolution(size_t sz)
Chris@152 99 {
Chris@152 100 m_resolution = sz;
Chris@152 101 }
Chris@152 102
Chris@152 103 size_t
Chris@182 104 EditableDenseThreeDimensionalModel::getWidth() const
Chris@182 105 {
Chris@182 106 return m_data.size();
Chris@182 107 }
Chris@182 108
Chris@182 109 size_t
Chris@182 110 EditableDenseThreeDimensionalModel::getHeight() const
Chris@152 111 {
Chris@152 112 return m_yBinCount;
Chris@152 113 }
Chris@152 114
Chris@152 115 void
Chris@182 116 EditableDenseThreeDimensionalModel::setHeight(size_t sz)
Chris@152 117 {
Chris@152 118 m_yBinCount = sz;
Chris@152 119 }
Chris@152 120
Chris@152 121 float
Chris@152 122 EditableDenseThreeDimensionalModel::getMinimumLevel() const
Chris@152 123 {
Chris@152 124 return m_minimum;
Chris@152 125 }
Chris@152 126
Chris@152 127 void
Chris@152 128 EditableDenseThreeDimensionalModel::setMinimumLevel(float level)
Chris@152 129 {
Chris@152 130 m_minimum = level;
Chris@152 131 }
Chris@152 132
Chris@152 133 float
Chris@152 134 EditableDenseThreeDimensionalModel::getMaximumLevel() const
Chris@152 135 {
Chris@152 136 return m_maximum;
Chris@152 137 }
Chris@152 138
Chris@152 139 void
Chris@152 140 EditableDenseThreeDimensionalModel::setMaximumLevel(float level)
Chris@152 141 {
Chris@152 142 m_maximum = level;
Chris@152 143 }
Chris@152 144
Chris@533 145 EditableDenseThreeDimensionalModel::Column
Chris@533 146 EditableDenseThreeDimensionalModel::getColumn(size_t index) const
Chris@152 147 {
Chris@152 148 QMutexLocker locker(&m_mutex);
Chris@534 149 if (index >= m_data.size()) return Column();
Chris@534 150 return expandAndRetrieve(index);
Chris@152 151 }
Chris@152 152
Chris@152 153 float
Chris@182 154 EditableDenseThreeDimensionalModel::getValueAt(size_t index, size_t n) const
Chris@152 155 {
Chris@534 156 Column c = getColumn(index);
Chris@535 157 if (n < c.size()) return c.at(n);
Chris@534 158 return m_minimum;
Chris@534 159 }
Chris@152 160
Chris@535 161 //static int given = 0, stored = 0;
Chris@534 162
Chris@534 163 void
Chris@534 164 EditableDenseThreeDimensionalModel::truncateAndStore(size_t index,
Chris@534 165 const Column &values)
Chris@534 166 {
Chris@534 167 assert(index < m_data.size());
Chris@534 168
Chris@534 169 //std::cout << "truncateAndStore(" << index << ", " << values.size() << ")" << std::endl;
Chris@534 170
Chris@535 171 // The default case is to store the entire column at m_data[index]
Chris@535 172 // and place 0 at m_trunc[index] to indicate that it has not been
Chris@535 173 // truncated. We only do clever stuff if one of the clever-stuff
Chris@535 174 // tests works out.
Chris@535 175
Chris@534 176 m_trunc[index] = 0;
Chris@535 177 if (index == 0 ||
Chris@535 178 m_compression == NoCompression ||
Chris@535 179 values.size() != m_yBinCount) {
Chris@535 180 // given += values.size();
Chris@535 181 // stored += values.size();
Chris@534 182 m_data[index] = values;
Chris@534 183 return;
Chris@152 184 }
Chris@152 185
Chris@535 186 // Maximum distance between a column and the one we refer to as
Chris@535 187 // the source of its truncated values. Limited by having to fit
Chris@535 188 // in a signed char, but in any case small values are usually
Chris@535 189 // better
Chris@535 190 static int maxdist = 6;
Chris@534 191
Chris@535 192 bool known = false; // do we know whether to truncate at top or bottom?
Chris@535 193 bool top = false; // if we do know, will we truncate at top?
Chris@534 194
Chris@535 195 // If the previous column is not truncated, then it is the only
Chris@535 196 // candidate for comparison. If it is truncated, then the column
Chris@535 197 // that it refers to is the only candidate. Either way, we only
Chris@535 198 // have one possible column to compare against here, and we are
Chris@535 199 // being careful to ensure it is not a truncated one (to avoid
Chris@535 200 // doing more work recursively when uncompressing).
Chris@534 201 int tdist = 1;
Chris@534 202 int ptrunc = m_trunc[index-1];
Chris@534 203 if (ptrunc < 0) {
Chris@534 204 top = false;
Chris@534 205 known = true;
Chris@534 206 tdist = -ptrunc + 1;
Chris@534 207 } else if (ptrunc > 0) {
Chris@534 208 top = true;
Chris@534 209 known = true;
Chris@534 210 tdist = ptrunc + 1;
Chris@534 211 }
Chris@534 212
Chris@534 213 Column p = expandAndRetrieve(index - tdist);
Chris@534 214 int h = m_yBinCount;
Chris@534 215
Chris@534 216 if (p.size() == h && tdist <= maxdist) {
Chris@534 217
Chris@534 218 int bcount = 0, tcount = 0;
Chris@534 219 if (!known || !top) {
Chris@535 220 // count how many identical values there are at the bottom
Chris@534 221 for (int i = 0; i < h; ++i) {
Chris@534 222 if (values.at(i) == p.at(i)) ++bcount;
Chris@534 223 else break;
Chris@534 224 }
Chris@534 225 }
Chris@534 226 if (!known || top) {
Chris@535 227 // count how many identical values there are at the top
Chris@534 228 for (int i = h; i > 0; --i) {
Chris@534 229 if (values.at(i-1) == p.at(i-1)) ++tcount;
Chris@534 230 else break;
Chris@534 231 }
Chris@534 232 }
Chris@534 233 if (!known) top = (tcount > bcount);
Chris@534 234
Chris@535 235 int limit = h / 4; // don't bother unless we have at least this many
Chris@534 236 if ((top ? tcount : bcount) > limit) {
Chris@534 237
Chris@534 238 if (!top) {
Chris@535 239 // create a new column with h - bcount values from bcount up
Chris@534 240 Column tcol(h - bcount);
Chris@535 241 // given += values.size();
Chris@535 242 // stored += h - bcount;
Chris@534 243 for (int i = bcount; i < h; ++i) {
Chris@534 244 tcol[i - bcount] = values.at(i);
Chris@534 245 }
Chris@534 246 m_data[index] = tcol;
Chris@534 247 m_trunc[index] = -tdist;
Chris@534 248 return;
Chris@534 249 } else {
Chris@535 250 // create a new column with h - tcount values from 0 up
Chris@534 251 Column tcol(h - tcount);
Chris@535 252 // given += values.size();
Chris@535 253 // stored += h - tcount;
Chris@534 254 for (int i = 0; i < h - tcount; ++i) {
Chris@534 255 tcol[i] = values.at(i);
Chris@534 256 }
Chris@534 257 m_data[index] = tcol;
Chris@534 258 m_trunc[index] = tdist;
Chris@534 259 return;
Chris@534 260 }
Chris@534 261 }
Chris@534 262 }
Chris@534 263
Chris@535 264 // given += values.size();
Chris@535 265 // stored += values.size();
Chris@534 266 // std::cout << "given: " << given << ", stored: " << stored << " ("
Chris@534 267 // << ((float(stored) / float(given)) * 100.f) << "%)" << std::endl;
Chris@534 268
Chris@535 269 // default case if nothing wacky worked out
Chris@534 270 m_data[index] = values;
Chris@534 271 return;
Chris@534 272 }
Chris@534 273
Chris@534 274 EditableDenseThreeDimensionalModel::Column
Chris@534 275 EditableDenseThreeDimensionalModel::expandAndRetrieve(size_t index) const
Chris@534 276 {
Chris@535 277 // See comment above m_trunc declaration in header
Chris@535 278
Chris@534 279 assert(index < m_data.size());
Chris@534 280 Column c = m_data.at(index);
Chris@534 281 if (index == 0) {
Chris@534 282 return c;
Chris@534 283 }
Chris@534 284 int trunc = (int)m_trunc[index];
Chris@534 285 if (trunc == 0) {
Chris@534 286 return c;
Chris@534 287 }
Chris@534 288 bool top = true;
Chris@534 289 int tdist = trunc;
Chris@534 290 if (trunc < 0) { top = false; tdist = -trunc; }
Chris@534 291 Column p = expandAndRetrieve(index - tdist);
Chris@534 292 if (p.size() != m_yBinCount) {
Chris@534 293 std::cerr << "WARNING: EditableDenseThreeDimensionalModel::expandAndRetrieve: Trying to expand from incorrectly sized column" << std::endl;
Chris@534 294 }
Chris@534 295 if (top) {
Chris@534 296 for (int i = c.size(); i < p.size(); ++i) {
Chris@534 297 c.push_back(p.at(i));
Chris@534 298 }
Chris@534 299 } else {
Chris@534 300 for (int i = int(p.size()) - int(c.size()); i >= 0; --i) {
Chris@534 301 c.push_front(p.at(i));
Chris@534 302 }
Chris@534 303 }
Chris@534 304 return c;
Chris@152 305 }
Chris@152 306
Chris@152 307 void
Chris@182 308 EditableDenseThreeDimensionalModel::setColumn(size_t index,
Chris@182 309 const Column &values)
Chris@152 310 {
Chris@152 311 QMutexLocker locker(&m_mutex);
Chris@152 312
Chris@182 313 while (index >= m_data.size()) {
Chris@182 314 m_data.push_back(Column());
Chris@534 315 m_trunc.push_back(0);
Chris@152 316 }
Chris@152 317
Chris@152 318 bool allChange = false;
Chris@152 319
Chris@534 320 // if (values.size() > m_yBinCount) m_yBinCount = values.size();
Chris@439 321
Chris@152 322 for (size_t i = 0; i < values.size(); ++i) {
Chris@256 323 float value = values[i];
Chris@257 324 if (std::isnan(value) || std::isinf(value)) {
Chris@256 325 continue;
Chris@256 326 }
Chris@256 327 if (!m_haveExtents || value < m_minimum) {
Chris@256 328 m_minimum = value;
Chris@152 329 allChange = true;
Chris@152 330 }
Chris@256 331 if (!m_haveExtents || value > m_maximum) {
Chris@256 332 m_maximum = value;
Chris@152 333 allChange = true;
Chris@152 334 }
Chris@256 335 m_haveExtents = true;
Chris@152 336 }
Chris@152 337
Chris@534 338 truncateAndStore(index, values);
Chris@534 339
Chris@534 340 assert(values == expandAndRetrieve(index));
Chris@152 341
Chris@182 342 long windowStart = index;
Chris@182 343 windowStart *= m_resolution;
Chris@182 344
Chris@152 345 if (m_notifyOnAdd) {
Chris@152 346 if (allChange) {
Chris@152 347 emit modelChanged();
Chris@152 348 } else {
Chris@152 349 emit modelChanged(windowStart, windowStart + m_resolution);
Chris@152 350 }
Chris@152 351 } else {
Chris@152 352 if (allChange) {
Chris@152 353 m_sinceLastNotifyMin = -1;
Chris@152 354 m_sinceLastNotifyMax = -1;
Chris@152 355 emit modelChanged();
Chris@152 356 } else {
Chris@152 357 if (m_sinceLastNotifyMin == -1 ||
Chris@152 358 windowStart < m_sinceLastNotifyMin) {
Chris@152 359 m_sinceLastNotifyMin = windowStart;
Chris@152 360 }
Chris@152 361 if (m_sinceLastNotifyMax == -1 ||
Chris@152 362 windowStart > m_sinceLastNotifyMax) {
Chris@152 363 m_sinceLastNotifyMax = windowStart;
Chris@152 364 }
Chris@152 365 }
Chris@152 366 }
Chris@152 367 }
Chris@152 368
Chris@152 369 QString
Chris@152 370 EditableDenseThreeDimensionalModel::getBinName(size_t n) const
Chris@152 371 {
Chris@152 372 if (m_binNames.size() > n) return m_binNames[n];
Chris@152 373 else return "";
Chris@152 374 }
Chris@152 375
Chris@152 376 void
Chris@152 377 EditableDenseThreeDimensionalModel::setBinName(size_t n, QString name)
Chris@152 378 {
Chris@152 379 while (m_binNames.size() <= n) m_binNames.push_back("");
Chris@152 380 m_binNames[n] = name;
Chris@152 381 emit modelChanged();
Chris@152 382 }
Chris@152 383
Chris@152 384 void
Chris@152 385 EditableDenseThreeDimensionalModel::setBinNames(std::vector<QString> names)
Chris@152 386 {
Chris@152 387 m_binNames = names;
Chris@152 388 emit modelChanged();
Chris@152 389 }
Chris@152 390
Chris@478 391 bool
Chris@478 392 EditableDenseThreeDimensionalModel::shouldUseLogValueScale() const
Chris@478 393 {
Chris@534 394 QMutexLocker locker(&m_mutex);
Chris@534 395
Chris@533 396 QVector<float> sample;
Chris@533 397 QVector<int> n;
Chris@478 398
Chris@478 399 for (int i = 0; i < 10; ++i) {
Chris@478 400 size_t index = i * 10;
Chris@478 401 if (index < m_data.size()) {
Chris@533 402 const Column &c = m_data.at(index);
Chris@478 403 while (c.size() > sample.size()) {
Chris@478 404 sample.push_back(0.f);
Chris@478 405 n.push_back(0);
Chris@478 406 }
Chris@478 407 for (int j = 0; j < c.size(); ++j) {
Chris@533 408 sample[j] += c.at(j);
Chris@478 409 ++n[j];
Chris@478 410 }
Chris@478 411 }
Chris@478 412 }
Chris@478 413
Chris@478 414 if (sample.empty()) return false;
Chris@478 415 for (int j = 0; j < sample.size(); ++j) {
Chris@478 416 if (n[j]) sample[j] /= n[j];
Chris@478 417 }
Chris@478 418
Chris@533 419 return LogRange::useLogScale(sample.toStdVector());
Chris@478 420 }
Chris@478 421
Chris@152 422 void
Chris@333 423 EditableDenseThreeDimensionalModel::setCompletion(int completion, bool update)
Chris@152 424 {
Chris@152 425 if (m_completion != completion) {
Chris@152 426 m_completion = completion;
Chris@152 427
Chris@152 428 if (completion == 100) {
Chris@152 429
Chris@152 430 m_notifyOnAdd = true; // henceforth
Chris@152 431 emit modelChanged();
Chris@152 432
Chris@152 433 } else if (!m_notifyOnAdd) {
Chris@152 434
Chris@333 435 if (update &&
Chris@333 436 m_sinceLastNotifyMin >= 0 &&
Chris@152 437 m_sinceLastNotifyMax >= 0) {
Chris@152 438 emit modelChanged(m_sinceLastNotifyMin,
Chris@152 439 m_sinceLastNotifyMax + m_resolution);
Chris@152 440 m_sinceLastNotifyMin = m_sinceLastNotifyMax = -1;
Chris@152 441 } else {
Chris@152 442 emit completionChanged();
Chris@152 443 }
Chris@152 444 } else {
Chris@152 445 emit completionChanged();
Chris@152 446 }
Chris@152 447 }
Chris@152 448 }
Chris@152 449
Chris@318 450 QString
Chris@318 451 EditableDenseThreeDimensionalModel::toDelimitedDataString(QString delimiter) const
Chris@318 452 {
Chris@534 453 QMutexLocker locker(&m_mutex);
Chris@318 454 QString s;
Chris@318 455 for (size_t i = 0; i < m_data.size(); ++i) {
Chris@318 456 QStringList list;
Chris@533 457 for (size_t j = 0; j < m_data.at(i).size(); ++j) {
Chris@533 458 list << QString("%1").arg(m_data.at(i).at(j));
Chris@318 459 }
Chris@318 460 s += list.join(delimiter) + "\n";
Chris@318 461 }
Chris@318 462 return s;
Chris@318 463 }
Chris@318 464
Chris@152 465 void
Chris@152 466 EditableDenseThreeDimensionalModel::toXml(QTextStream &out,
Chris@314 467 QString indent,
Chris@314 468 QString extraAttributes) const
Chris@152 469 {
Chris@534 470 QMutexLocker locker(&m_mutex);
Chris@534 471
Chris@152 472 // For historical reasons we read and write "resolution" as "windowSize"
Chris@152 473
Chris@318 474 std::cerr << "EditableDenseThreeDimensionalModel::toXml" << std::endl;
Chris@318 475
Chris@314 476 Model::toXml
Chris@314 477 (out, indent,
Chris@314 478 QString("type=\"dense\" dimensions=\"3\" windowSize=\"%1\" yBinCount=\"%2\" minimum=\"%3\" maximum=\"%4\" dataset=\"%5\" %6")
Chris@152 479 .arg(m_resolution)
Chris@152 480 .arg(m_yBinCount)
Chris@152 481 .arg(m_minimum)
Chris@152 482 .arg(m_maximum)
Chris@152 483 .arg(getObjectExportId(&m_data))
Chris@152 484 .arg(extraAttributes));
Chris@152 485
Chris@152 486 out << indent;
Chris@152 487 out << QString("<dataset id=\"%1\" dimensions=\"3\" separator=\" \">\n")
Chris@152 488 .arg(getObjectExportId(&m_data));
Chris@152 489
Chris@152 490 for (size_t i = 0; i < m_binNames.size(); ++i) {
Chris@152 491 if (m_binNames[i] != "") {
Chris@152 492 out << indent + " ";
Chris@152 493 out << QString("<bin number=\"%1\" name=\"%2\"/>\n")
Chris@152 494 .arg(i).arg(m_binNames[i]);
Chris@152 495 }
Chris@152 496 }
Chris@152 497
Chris@152 498 for (size_t i = 0; i < m_data.size(); ++i) {
Chris@152 499 out << indent + " ";
Chris@152 500 out << QString("<row n=\"%1\">").arg(i);
Chris@533 501 for (size_t j = 0; j < m_data.at(i).size(); ++j) {
Chris@152 502 if (j > 0) out << " ";
Chris@533 503 out << m_data.at(i).at(j);
Chris@152 504 }
Chris@152 505 out << QString("</row>\n");
Chris@318 506 out.flush();
Chris@152 507 }
Chris@152 508
Chris@152 509 out << indent + "</dataset>\n";
Chris@152 510 }
Chris@152 511
Chris@152 512