123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0.txt
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- // Qt includes
- #include <QDebug>
- #include <QDoubleSpinBox>
- #include <QHBoxLayout>
- // CTK includes
- #include "ctkCoordinatesWidget.h"
- #include "ctkDoubleSpinBox.h"
- #include "ctkUtils.h"
- #include "ctkValueProxy.h"
- // STD includes
- #include <cmath>
- //------------------------------------------------------------------------------
- ctkCoordinatesWidget::ctkCoordinatesWidget(QWidget* _parent) :QWidget(_parent)
- {
- this->Decimals = 3;
- ctkDoubleSpinBox temp;
- this->DecimalsOption = temp.decimalsOption();
- this->SingleStep = 1.;
- this->Minimum = -100000.;
- this->Maximum = 100000.;
- this->Normalized = false;
- this->Dimension = 0;
- this->Coordinates = 0;
- this->ChangingDecimals = false;
- QHBoxLayout* hboxLayout = new QHBoxLayout(this);
- hboxLayout->setContentsMargins(0, 0, 0, 0);
- this->setLayout(hboxLayout);
- this->setDimension(3);
- }
- //------------------------------------------------------------------------------
- ctkCoordinatesWidget::~ctkCoordinatesWidget()
- {
- delete [] this->Coordinates;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::addSpinBox()
- {
- ctkDoubleSpinBox* spinBox = new ctkDoubleSpinBox(this);
- spinBox->setDecimals(this->Decimals);
- spinBox->setDecimalsOption(this->DecimalsOption);
- spinBox->setSingleStep(this->SingleStep);
- spinBox->setMinimum(this->Minimum);
- spinBox->setMaximum(this->Maximum);
- spinBox->setValueProxy(this->Proxy.data());
- connect( spinBox, SIGNAL(valueChanged(double)),
- this, SLOT(updateCoordinate(double)));
- // Same number of decimals within the spinboxes.
- connect( spinBox, SIGNAL(decimalsChanged(int)),
- this, SLOT(updateOtherDecimals(int)));
- this->layout()->addWidget(spinBox);
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setDimension(int dim)
- {
- if (dim < 1)
- {
- return;
- }
- double* newPos = new double[dim];
- if (dim > this->Dimension)
- {
- memcpy(newPos, this->Coordinates, this->Dimension * sizeof(double));
- for (int i = this->Dimension; i < dim; ++i)
- {
- newPos[i] = 0.;
- this->addSpinBox();
- this->LastUserEditedCoordinates.push_back(i);
- }
- }
- else
- {
- memcpy(newPos, this->Coordinates, dim * sizeof(double));
- for (int i = this->Dimension - 1 ; i >= dim; --i)
- {
- QLayoutItem* item = this->layout()->takeAt(i);
- QWidget* widget = item ? item->widget() : 0;
- delete item;
- delete widget;
- this->LastUserEditedCoordinates.pop_back();
- }
- }
- delete [] this->Coordinates;
- this->Coordinates = newPos;
- this->Dimension = dim;
- this->updateGeometry();
-
- this->updateCoordinates();
- }
- //------------------------------------------------------------------------------
- int ctkCoordinatesWidget::dimension() const
- {
- return this->Dimension;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setMinimum(double min)
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->setMinimum(min);
- }
- this->Minimum = min;
- }
- //------------------------------------------------------------------------------
- double ctkCoordinatesWidget::minimum() const
- {
- return this->Minimum;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setMaximum(double max)
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->setMaximum(max);
- }
- this->Maximum = max;
- }
- //------------------------------------------------------------------------------
- double ctkCoordinatesWidget::maximum() const
- {
- return this->Maximum;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setRange(double min, double max)
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->setRange(min, max);
- }
- this->Minimum = min;
- this->Maximum = max;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setNormalized(bool normalized)
- {
- this->Normalized = normalized;
- if (this->Normalized)
- {
- double* normalizedCoordinates = new double[this->Dimension];
- memcpy(normalizedCoordinates, this->Coordinates, sizeof(double)*this->Dimension);
- ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
- this->setMinimum(-1.);
- this->setMaximum(1.);
- this->setCoordinates(normalizedCoordinates);
- delete [] normalizedCoordinates;
- }
- }
- //------------------------------------------------------------------------------
- bool ctkCoordinatesWidget::isNormalized() const
- {
- return this->Normalized;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setDecimals(int newDecimals)
- {
- this->Decimals = newDecimals;
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->setDecimals(newDecimals);
- }
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::updateDecimals()
- {
- if (this->ChangingDecimals)
- {
- return;
- }
- int maxDecimals = 0;
- for (int i = 0; i < this->Dimension; ++i)
- {
- maxDecimals = qMax(maxDecimals, this->spinBox(i)->decimals());
- }
- this->ChangingDecimals = true;
- this->setTemporaryDecimals(maxDecimals);
- this->ChangingDecimals = false;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::updateOtherDecimals(int decimals)
- {
- if (this->ChangingDecimals)
- {
- return;
- }
- int maxDecimals = decimals;
- for (int i = 0; i < this->Dimension; ++i)
- {
- if (this->sender() == this->spinBox(i))
- {
- continue;
- }
- int spinBoxDecimals = this->spinBox(i)->decimals();
- if (this->decimalsOption() & ctkDoubleSpinBox::DecimalsByValue)
- {
- spinBoxDecimals = ctk::significantDecimals(
- this->spinBox(i)->displayedValue(), maxDecimals);
- }
- maxDecimals = qMax(maxDecimals, spinBoxDecimals);
- }
- this->ChangingDecimals = true;
- this->setTemporaryDecimals(maxDecimals);
- this->ChangingDecimals = false;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setTemporaryDecimals(int newDecimals)
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- if (this->sender() == this->spinBox(i))
- {
- continue;
- }
- this->spinBox(i)->spinBox()->setDecimals(newDecimals);
- }
- }
- //------------------------------------------------------------------------------
- int ctkCoordinatesWidget::decimals() const
- {
- return this->Decimals;
- }
- // --------------------------------------------------------------------------
- ctkDoubleSpinBox::DecimalsOptions ctkCoordinatesWidget::decimalsOption()const
- {
- return this->DecimalsOption;
- }
- // --------------------------------------------------------------------------
- void ctkCoordinatesWidget
- ::setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions newDecimalsOption)
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->setDecimalsOption(newDecimalsOption);
- }
- this->DecimalsOption = newDecimalsOption;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setSingleStep(double step)
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->setSingleStep(step);
- }
- this->SingleStep = step;
- }
- //------------------------------------------------------------------------------
- double ctkCoordinatesWidget::singleStep() const
- {
- return this->SingleStep;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setCoordinatesAsString(QString _pos)
- {
- QStringList posList = _pos.split(',');
- if (posList.count() != this->Dimension)
- {
- return;
- }
- double* newPos = new double[this->Dimension];
- for (int i = 0; i < this->Dimension; ++i)
- {
- newPos[i] = posList[i].toDouble();
- }
- this->setCoordinates(newPos);
- delete [] newPos;
- }
- //------------------------------------------------------------------------------
- QString ctkCoordinatesWidget::coordinatesAsString()const
- {
- QString res;
- for (int i = 0; i < this->Dimension; ++i)
- {
- if (i != 0)
- {
- res += ",";
- }
- res += QString::number(this->Coordinates[i]);
- }
- return res;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setCoordinates(double* coordinates)
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->Coordinates[i] = coordinates[i];
- }
- if (this->Normalized)
- {
- this->normalize(this->Coordinates, this->Dimension);
- }
- bool valuesModified = false;
- bool blocked = this->blockSignals(true);
- for (int i = 0; i < this->Dimension; ++i)
- {
- ctkDoubleSpinBox* spinbox = this->spinBox(i);
- if (spinbox)
- {
- // we don't want updateCoordinate() to be called.
- // it could mess with the LastUserEditedCoordinates list.
- bool spinBoxSignalWasBlocked = spinbox->blockSignals(true);
- if (spinbox->value() != this->Coordinates[i])
- {
- valuesModified = true;
- }
- // Still setValue needs to be called to recompute the number of decimals
- // if DecimalsByValue is set.
- spinbox->setValue(this->Coordinates[i]);
- spinbox->blockSignals(spinBoxSignalWasBlocked);
- }
- }
- this->blockSignals(blocked);
- this->updateDecimals();
- if (valuesModified)
- {
- this->updateCoordinates();
- }
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::setCoordinates(double x, double y, double z, double w)
- {
- double* coordinates = new double[this->Dimension];
- if (this->Dimension >= 1)
- {
- coordinates[0] = x;
- }
- if (this->Dimension >= 2)
- {
- coordinates[1] = y;
- }
- if (this->Dimension >= 3)
- {
- coordinates[2] = z;
- }
- if (this->Dimension >= 4)
- {
- coordinates[3] = w;
- }
- for (int i = 4; i < this->Dimension; ++i)
- {
- coordinates[i] = this->Coordinates[i];
- }
- this->setCoordinates(coordinates);
- delete [] coordinates;
- }
- //------------------------------------------------------------------------------
- double const * ctkCoordinatesWidget::coordinates()const
- {
- return this->Coordinates;
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::updateCoordinate(double coordinate)
- {
- int element = -1;
- for (int i = 0; i < this->Dimension; ++i)
- {
- if ( this->spinBox(i) && this->spinBox(i) == this->sender())
- {
- this->Coordinates[i] = coordinate;
- element = i;
- }
- }
- Q_ASSERT(element != -1);
- // Update the last edited history by push first the element.
- for (int i = this->Dimension -1; i > 0; --i)
- {
- if (this->LastUserEditedCoordinates[i] == element)
- {
- this->LastUserEditedCoordinates.swap(i,i-1);
- }
- }
- // What is the oldest coordinate to be edited
- int oldestElement = this->LastUserEditedCoordinates.last();
- if (this->isNormalized())
- {
- // We have to ensure the coordinates are normalized.
- double den = 0.;
- double squaredNorm = this->squaredNorm();
- // Old Values xx + yy + zz = 1
- // New values: x'x' + y'y' + z'z' = 1
- // Say we are changing y into y':
- // x'x' + z'z' = 1 - y'y'
- if (oldestElement != -1 &&
- this->Coordinates[oldestElement] != 0.0 &&
- squaredNorm != 0.0)
- {
- // 1) Normalize only with the oldest user edited value
- // The oldest element is z, that means we try to have
- // x = x' (so that the user doesn't loose the edit he just made on the
- // element (x) he edited before this one (y).
- // Let's pose a the coef to multiply z into z' that keeps the norm to 1
- // xx + z'z' = 1 - y'y' (because x = x')
- // xx + azaz = 1 - y'y' (because z' = az)
- // aa*zz = 1 - y'y' - xx
- // a = sqrt( (1 - y'y' - xx) / zz )
- den = (1. - (squaredNorm -
- this->Coordinates[oldestElement] *
- this->Coordinates[oldestElement])) /
- (this->Coordinates[oldestElement] *
- this->Coordinates[oldestElement]);
- if (den > 0.)
- {
- den = sqrt(den);
- }
- }
- // Maybe 1) failed, then give 2) a chance.
- if (den <= 0)
- {
- oldestElement = -1;
- }
- bool mult = true;
- if (oldestElement == -1)
- {
- // 2) Normalize with all the coordinates
- // Let's pose a the coef to multiply x into x' and z into z' that keeps
- // the norm to 1:
- // axax + azaz = 1 - y'y'
- // aa(xx + zz) = 1 - y'y'
- // a = sqrt( (1 - y'y') / (xx + zz) )
- squaredNorm -= coordinate * coordinate;
- if (squaredNorm != 0.0)
- {
- den = sqrt( (1. - coordinate * coordinate) / squaredNorm);
- }
- else if (this->Dimension > 1)
- {
- mult = false;
- den = sqrt((1. - coordinate*coordinate) / (this->Dimension - 1));
- }
- }
- // Normalize coordinates
- double* normalizedCoordinates = new double[this->Dimension];
- for (int i = 0; i < this->Dimension; ++i)
- {
- if ((i != element && oldestElement == -1) ||
- (i == oldestElement && oldestElement != -1))
- {
- normalizedCoordinates[i] = mult ? this->Coordinates[i] * den : den;
- }
- else
- {
- normalizedCoordinates[i] = this->Coordinates[i];
- }
- }
- this->setCoordinates(normalizedCoordinates);
- delete [] normalizedCoordinates;
- }
- else
- {
- emit coordinatesChanged(this->Coordinates);
- }
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::updateCoordinates()
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->Coordinates[i] = this->spinBox(i)->value();
- }
- emit coordinatesChanged(this->Coordinates);
- }
- //------------------------------------------------------------------------------
- void ctkCoordinatesWidget::normalize()
- {
- double* normalizedCoordinates = new double[this->Dimension];
- memcpy(normalizedCoordinates, this->Coordinates,
- sizeof(double) * this->Dimension);
- ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
- this->setCoordinates(normalizedCoordinates);
- delete [] normalizedCoordinates;
- }
- //------------------------------------------------------------------------------
- double ctkCoordinatesWidget::normalize(double* coordinates, int dimension)
- {
- double den = ctkCoordinatesWidget::norm( coordinates, dimension );
- if ( den != 0.0 )
- {
- for (int i = 0; i < dimension; ++i)
- {
- coordinates[i] /= den;
- }
- }
- return den;
- }
- //------------------------------------------------------------------------------
- double ctkCoordinatesWidget::norm()const
- {
- return ctkCoordinatesWidget::norm(this->Coordinates, this->Dimension);
- }
- //------------------------------------------------------------------------------
- double ctkCoordinatesWidget::norm(double* coordinates, int dimension)
- {
- return sqrt(ctkCoordinatesWidget::squaredNorm(coordinates, dimension));
- }
- //------------------------------------------------------------------------------
- double ctkCoordinatesWidget::squaredNorm()const
- {
- return ctkCoordinatesWidget::squaredNorm(this->Coordinates, this->Dimension);
- }
- //------------------------------------------------------------------------------
- double ctkCoordinatesWidget::squaredNorm(double* coordinates, int dimension)
- {
- double sum = 0.;
- for (int i = 0; i < dimension; ++i)
- {
- sum += coordinates[i] * coordinates[i];
- }
- return sum;
- }
- //----------------------------------------------------------------------------
- ctkDoubleSpinBox* ctkCoordinatesWidget::spinBox(int i)
- {
- QLayoutItem* item = this->layout()->itemAt(i);
- ctkDoubleSpinBox* spinBox =
- item ? qobject_cast<ctkDoubleSpinBox*>(item->widget()) : 0;
- return spinBox;
- }
- //----------------------------------------------------------------------------
- void ctkCoordinatesWidget::setValueProxy(ctkValueProxy* proxy)
- {
- if (this->Proxy.data() == proxy)
- {
- return;
- }
- this->onValueProxyAboutToBeModified();
- if (this->Proxy)
- {
- disconnect(this->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
- this, SLOT(onValueProxyAboutToBeModified()));
- disconnect(this->Proxy.data(), SIGNAL(proxyModified()),
- this, SLOT(onValueProxyModified()));
- }
- this->Proxy = proxy;
- if (this->Proxy)
- {
- connect(this->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
- this, SLOT(onValueProxyAboutToBeModified()));
- }
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->setValueProxy(this->Proxy.data());
- }
- if (this->Proxy)
- {
- connect(this->Proxy.data(), SIGNAL(proxyModified()),
- this, SLOT(onValueProxyModified()));
- }
- this->onValueProxyModified();
- }
- //----------------------------------------------------------------------------
- ctkValueProxy* ctkCoordinatesWidget::valueProxy() const
- {
- return this->Proxy.data();
- }
- //----------------------------------------------------------------------------
- void ctkCoordinatesWidget::onValueProxyAboutToBeModified()
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->blockSignals(true);
- }
- }
- //----------------------------------------------------------------------------
- void ctkCoordinatesWidget::onValueProxyModified()
- {
- for (int i = 0; i < this->Dimension; ++i)
- {
- this->spinBox(i)->blockSignals(false);
- }
- // Only decimals (not range/nor value) may have change during a proxy
- // modification.
- this->updateDecimals();
- }
|