| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 | /*=========================================================================  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.commontk.org/LICENSE  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 <QGraphicsScene>#include <QLinearGradient>#include <QResizeEvent>#include <QDebug>/// CTK includes#include "ctkTransferFunction.h"#include "ctkTransferFunctionRepresentation.h"/// STL includes#include <limits>//-----------------------------------------------------------------------------class ctkTransferFunctionRepresentationPrivate{public:  ctkTransferFunctionRepresentationPrivate();  ctkTransferFunction* TransferFunction;  QColor               VerticalGradientColor;  QPainterPath         Path;  QLinearGradient      Gradient;  QList<QPointF>       Points;  QRectF       rect()const;  qreal        width()const;  qreal        height()const;  qreal        WorldRangeX[2];  QVariant     WorldRangeY[2];  qreal        RangeXDiff;  qreal        RangeXOffSet;  qreal        RangeYDiff;  qreal        RangeYOffSet;};//-----------------------------------------------------------------------------ctkTransferFunctionRepresentationPrivate::ctkTransferFunctionRepresentationPrivate(){  this->TransferFunction = 0;  this->VerticalGradientColor = QColor::fromRgbF(1., 0., 0., 1. );}//-----------------------------------------------------------------------------QRectF ctkTransferFunctionRepresentationPrivate::rect()const{  return QRectF(0.,0.,1.,1.);}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentationPrivate::width()const{  return 1.;}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentationPrivate::height()const{  return 1.;}//-----------------------------------------------------------------------------ctkTransferFunctionRepresentation::ctkTransferFunctionRepresentation(QObject* parentObject)  :QObject(parentObject)  , d_ptr(new ctkTransferFunctionRepresentationPrivate){}//-----------------------------------------------------------------------------ctkTransferFunctionRepresentation::ctkTransferFunctionRepresentation(  ctkTransferFunction* transferFunction, QObject* parentObject)  :QObject(parentObject)  , d_ptr(new ctkTransferFunctionRepresentationPrivate){  this->setTransferFunction(transferFunction);}//-----------------------------------------------------------------------------ctkTransferFunctionRepresentation::~ctkTransferFunctionRepresentation(){}//-----------------------------------------------------------------------------QColor ctkTransferFunctionRepresentation::verticalGradientColor() const{  Q_D( const ctkTransferFunctionRepresentation );  return d->VerticalGradientColor;}//-----------------------------------------------------------------------------void ctkTransferFunctionRepresentation::setVerticalGradientColor( QColor verticalGradientColor ){  Q_D( ctkTransferFunctionRepresentation );  d->VerticalGradientColor = verticalGradientColor;}//-----------------------------------------------------------------------------void ctkTransferFunctionRepresentation::setTransferFunction(ctkTransferFunction* transferFunction){  Q_D(ctkTransferFunctionRepresentation);  if (d->TransferFunction == transferFunction)    {    return;    }  d->TransferFunction = transferFunction;  connect( d->TransferFunction, SIGNAL(changed()),           this, SLOT(onTransferFunctionChanged()),           Qt::UniqueConnection);  this->onTransferFunctionChanged();}//-----------------------------------------------------------------------------ctkTransferFunction* ctkTransferFunctionRepresentation::transferFunction()const{  Q_D(const ctkTransferFunctionRepresentation);  return d->TransferFunction;}//-----------------------------------------------------------------------------void ctkTransferFunctionRepresentation::onTransferFunctionChanged(){  Q_D(ctkTransferFunctionRepresentation);  // delete cache here  d->Path = QPainterPath();  d->Points.clear();}//-----------------------------------------------------------------------------const QPainterPath& ctkTransferFunctionRepresentation::curve()const{  Q_D(const ctkTransferFunctionRepresentation);  if (d->Path.isEmpty())    {    const_cast<ctkTransferFunctionRepresentation*>(this)->computeCurve();    const_cast<ctkTransferFunctionRepresentation*>(this)->computeGradient();    }  return d->Path;}//-----------------------------------------------------------------------------const QList<QPointF>& ctkTransferFunctionRepresentation::points()const{  Q_D(const ctkTransferFunctionRepresentation);  if (d->Path.isEmpty())    {    const_cast<ctkTransferFunctionRepresentation*>(this)->computeCurve();    const_cast<ctkTransferFunctionRepresentation*>(this)->computeGradient();    }  return d->Points;}//-----------------------------------------------------------------------------const QGradient& ctkTransferFunctionRepresentation::gradient()const{  Q_D(const ctkTransferFunctionRepresentation);  if (d->Path.isEmpty())    {    const_cast<ctkTransferFunctionRepresentation*>(this)->computeCurve();    const_cast<ctkTransferFunctionRepresentation*>(this)->computeGradient();    }  return d->Gradient;}//-----------------------------------------------------------------------------void ctkTransferFunctionRepresentation::computeCurve(){  Q_D(ctkTransferFunctionRepresentation);  int count = d->TransferFunction ? d->TransferFunction->count() : 0;  if (count <= 0)    {    return;    }  d->TransferFunction->range(d->WorldRangeX[0], d->WorldRangeX[1]);  d->WorldRangeY[0] = this->posY(d->TransferFunction->minValue());  d->WorldRangeY[1] = this->posY(d->TransferFunction->maxValue());  d->RangeXDiff   = this->computeRangeXDiff(d->rect(), d->WorldRangeX);  d->RangeXOffSet = this->computeRangeXOffset(d->WorldRangeX);  d->RangeYDiff   = this->computeRangeYDiff(d->rect(), d->WorldRangeY);  d->RangeYOffSet = this->computeRangeYOffset(d->WorldRangeY);  ctkControlPoint* startCP = d->TransferFunction->controlPoint(0);  ctkControlPoint* nextCP = 0;  QPointF startPos = this->mapPointToScene(startCP);  d->Points.clear();  d->Points << startPos;  d->Path = QPainterPath();  d->Path.moveTo(startPos);  for(int i = 1; i < count; ++i)    {    nextCP = d->TransferFunction->controlPoint(i);    if (this->transferFunction()->isDiscrete())      {      QPointF nextPos = this->mapPointToScene(nextCP);      qreal midPosX = (startPos.x() + nextPos.x()) / 2.;      d->Path.lineTo(QPointF(midPosX, startPos.y()));      d->Path.lineTo(QPointF(midPosX, nextPos.y()));      d->Points << nextPos;      startPos = nextPos;      if (i == count -1)        {        d->Path.lineTo(nextPos);        }      }    else if (dynamic_cast<ctkNonLinearControlPoint*>(startCP))      {      QList<ctkPoint> points = this->nonLinearPoints(startCP, nextCP);      int j;      for (j = 1; j < points.count(); ++j)        {        d->Path.lineTo(this->mapPointToScene(points[j]));        }      j = points.count() - 1;      d->Points << this->mapPointToScene(points[j]);      }    else //dynamic_cast<ctkBezierControlPoint*>(startCP))      {      QList<ctkPoint> points = this->bezierParams(startCP, nextCP);      QList<ctkPoint>::iterator it = points.begin();      QList<QPointF> bezierPoints;      foreach(const ctkPoint& p, points)        {        bezierPoints << this->mapPointToScene(p);        }      d->Path.cubicTo(bezierPoints[1], bezierPoints[2], bezierPoints[3]);      d->Points << bezierPoints[3];      }    //qDebug() << i << points[0] << points[1] << points[2] << points[3];    delete startCP;    startCP = nextCP;    }  if (startCP)    {    delete startCP;    }}//-----------------------------------------------------------------------------void ctkTransferFunctionRepresentation::computeGradient(){  Q_D(ctkTransferFunctionRepresentation);  int count = d->TransferFunction ? d->TransferFunction->count() : 0;  if (count <= 0)    {    return;    }  d->TransferFunction->range(d->WorldRangeX[0], d->WorldRangeX[1]);  d->WorldRangeY[0] = this->posY(d->TransferFunction->minValue());  d->WorldRangeY[1] = this->posY(d->TransferFunction->maxValue());  d->RangeXDiff   = this->computeRangeXDiff(QRectF(0.,0.,1.,1.), d->WorldRangeX);  d->RangeXOffSet = this->computeRangeXOffset(d->WorldRangeX);  d->RangeYDiff   = this->computeRangeYDiff(QRectF(0.,0.,1.,1.), d->WorldRangeY);  d->RangeYOffSet = this->computeRangeYOffset(d->WorldRangeY);  ctkControlPoint* startCP = d->TransferFunction->controlPoint(0);  ctkControlPoint* nextCP = 0;  qreal startPos = this->mapXToScene(this->posX(startCP->x()));  qreal nextPos;  //  //if we have no colors in value (i.e. can't convert value to color)  if (! d->TransferFunction->value(0).canConvert<QColor>())    {    // create vertical gradient    d->Gradient = QLinearGradient(0., 0., 0., 1.);    // red    d->Gradient.setColorAt(0, d->VerticalGradientColor );    // to black    d->Gradient.setColorAt(1, QColor::fromRgbF(0., 0., 0., 1. ));    return;    }  // classic gradient if we have colors in value  d->Gradient = QLinearGradient(0., 0., 1., 0.);  d->Gradient.setColorAt(startPos, this->color(startCP));  for(int i = 1; i < count; ++i)    {    nextCP = d->TransferFunction->controlPoint(i);    nextPos = this->mapXToScene(this->posX(nextCP));    if (this->transferFunction()->isDiscrete())      {      qreal midPoint = (startPos + nextPos)  / 2;      d->Gradient.setColorAt(midPoint, this->color(startCP));      d->Gradient.setColorAt(midPoint + std::numeric_limits<qreal>::epsilon(), this->color(nextCP));      }    else if (dynamic_cast<ctkNonLinearControlPoint*>(startCP))      {      QList<ctkPoint> points = this->nonLinearPoints(startCP, nextCP);      foreach(const ctkPoint& p, points)        {        d->Gradient.setColorAt(this->mapXToScene(this->posX(p)), this->color(p));        }      //no need, d->Gradient.setColorAt(nextPos, this->color(nextCP));      }    else //dynamic_cast<ctkBezierControlPoint*>(startCP))      { // TODO handle bezier points with color      QList<ctkPoint> points = this->bezierParams(startCP, nextCP);      QList<ctkPoint>::iterator it = points.begin();      QList<QPointF> bezierPoints;      foreach(const ctkPoint& p, points)        {        d->Gradient.setColorAt(this->mapXToScene(this->posX(p)), this->color(p));        }      nextPos = this->mapXToScene(this->posX(points[points.size() - 1]));      }    //qDebug() << i << points[0] << points[1] << points[2] << points[3];    delete startCP;    startCP = nextCP;    startPos = nextPos;    }  d->Gradient.setColorAt(startPos, this->color(startCP));  if (startCP)    {    delete startCP;    }}//-----------------------------------------------------------------------------QList<ctkPoint> ctkTransferFunctionRepresentation::bezierParams(  ctkControlPoint* start, ctkControlPoint* end) const{  Q_ASSERT(start);  Q_ASSERT(end);  QList<ctkPoint> points;  ctkBezierControlPoint* bezierCP = dynamic_cast<ctkBezierControlPoint*>(start);  if (!bezierCP)    {// just duplicate start and end into p1 and p2    points << start->P;    points << start->P;    points << end->P;    points << end->P;    return points;    }  points << start->P;  points << bezierCP->P1;  points << bezierCP->P2;  points << end->P;  return points;}//-----------------------------------------------------------------------------QList<ctkPoint> ctkTransferFunctionRepresentation::nonLinearPoints(  ctkControlPoint* start, ctkControlPoint* end) const{  Q_ASSERT(start);  ctkNonLinearControlPoint* nonLinearCP =    dynamic_cast<ctkNonLinearControlPoint*>(start);  if (!nonLinearCP)    {    QList<ctkPoint> points;    points << start->P;    points << end->P;    return points;    }  return nonLinearCP->SubPoints;}//-----------------------------------------------------------------------------QColor ctkTransferFunctionRepresentation::color(const QVariant& v) const{  //Q_ASSERT(v.canConvert<QColor>());  if (v.canConvert<QColor>())    {    return v.value<QColor>();    }  else    {    //black background    QColor defaultColor(0., 0., 0.);    return defaultColor;    }  return QColor();}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::computeRangeXDiff(const QRectF& rect, qreal rangeX[2]){  return rect.width() / (rangeX[1] - rangeX[0]);}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::computeRangeXOffset(qreal rangeX[2]){  return rangeX[0];}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::computeRangeYDiff(const QRectF& rect, const QVariant rangeY[2]){  qreal rangeYDiff = rect.height();  qreal rangePosY[2];  rangePosY[0] = this->posY(rangeY[0]);  rangePosY[1] = this->posY(rangeY[1]);  if (rangePosY[1] == rangePosY[0])    {    rangeYDiff /= rangePosY[0];    return rangeYDiff;    }  rangeYDiff /= rangePosY[1] - rangePosY[0];  return rangeYDiff;}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::computeRangeYOffset(const QVariant rangeY[2]){  qreal rangePosY[2];  rangePosY[0] = this->posY(rangeY[0]);  rangePosY[1] = this->posY(rangeY[1]);  if (rangePosY[1] == rangePosY[0])    {    return 0.;    }  return rangePosY[0];}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::posX(const qreal& x)const{  return x;}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::posY(const QVariant& value)const{  Q_ASSERT(value.canConvert<qreal>() || value.canConvert<QColor>());  if (value.canConvert<QColor>())    {    return value.value<QColor>().alphaF();    }  return value.toReal();}//-----------------------------------------------------------------------------QPointF ctkTransferFunctionRepresentation::mapPointToScene(const ctkControlPoint* cp)const{  return QPointF(this->mapXToScene(this->posX(cp->x())),                 this->mapYToScene(this->posY(cp->value())));}//-----------------------------------------------------------------------------QPointF ctkTransferFunctionRepresentation::mapPointToScene(const ctkPoint& point)const{  return QPointF( this->mapXToScene(this->posX(point.X)),                  this->mapYToScene(this->posY(point.Value)));}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::mapXToScene(qreal xPos)const{  Q_D(const ctkTransferFunctionRepresentation);  return (xPos - d->RangeXOffSet) * d->RangeXDiff;}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::mapYToScene(qreal yPos)const{  Q_D(const ctkTransferFunctionRepresentation);  return d->height() - (yPos - d->RangeYOffSet) * d->RangeYDiff;}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::mapXFromScene(qreal scenePosX)const{  Q_D(const ctkTransferFunctionRepresentation);  return (scenePosX / d->RangeXDiff) + d->RangeXOffSet;}//-----------------------------------------------------------------------------qreal ctkTransferFunctionRepresentation::mapYFromScene(qreal scenePosY)const{  Q_D(const ctkTransferFunctionRepresentation);  return ((d->height() - scenePosY) / d->RangeYDiff) + d->RangeYOffSet ;}
 |