Gui: replace floats with SbBox2f in SoFCColorGradient

This commit is contained in:
wmayer
2022-02-06 14:24:11 +01:00
parent 91c0895e26
commit b806115015
2 changed files with 71 additions and 89 deletions

View File

@@ -48,7 +48,7 @@ SO_NODE_SOURCE(SoFCColorGradient)
/*! /*!
Constructor. Constructor.
*/ */
SoFCColorGradient::SoFCColorGradient() : _fMaxX(4.5f), _fMinX(4.0f), _fMaxY(4.0f), _fMinY(-4.0f), _bOutInvisible(false), _precision(3) SoFCColorGradient::SoFCColorGradient() : _bbox(4.0f, -4.0f, 4.5f, 4.0f), _bOutInvisible(false), _precision(3)
{ {
SO_NODE_CONSTRUCTOR(SoFCColorGradient); SO_NODE_CONSTRUCTOR(SoFCColorGradient);
coords = new SoCoordinate3; coords = new SoCoordinate3;
@@ -86,24 +86,21 @@ void SoFCColorGradient::setMarkerLabel( const SoMFString& label )
{ {
coinRemoveAllChildren(labels); coinRemoveAllChildren(labels);
float fH=8.0f;
int num = label.getNum(); int num = label.getNum();
if ( num > 1 ) if (num > 1) {
{ float fStep = 8.0f / ((float)num-1);
float fStep = fH / ((float)num-1);
SoTransform* trans = new SoTransform; SoTransform* trans = new SoTransform;
trans->translation.setValue(_fMaxX+0.1f,_fMaxY-0.05f+fStep,0.0f); trans->translation.setValue(_bbox.getMax()[0] + 0.1f, _bbox.getMax()[1] - 0.05f + fStep, 0.0f);
labels->addChild(trans); labels->addChild(trans);
for ( int i=0; i<num; i++ ) for (int i=0; i<num; i++) {
{
SoTransform* trans = new SoTransform; SoTransform* trans = new SoTransform;
SoBaseColor* color = new SoBaseColor; SoBaseColor* color = new SoBaseColor;
SoText2 * text2 = new SoText2; SoText2 * text2 = new SoText2;
trans->translation.setValue(0,-fStep,0); trans->translation.setValue(0, -fStep, 0);
color->rgb.setValue(0,0,0); color->rgb.setValue(0, 0, 0);
text2->string.setValue( label[i] ); text2->string.setValue(label[i]);
labels->addChild(trans); labels->addChild(trans);
labels->addChild(color); labels->addChild(color);
labels->addChild(text2); labels->addChild(text2);
@@ -118,61 +115,41 @@ void SoFCColorGradient::setViewportSize( const SbVec2s& size )
float fMinX= 4.0f, fMaxX=4.5f; float fMinX= 4.0f, fMaxX=4.5f;
float fMinY= -4.0f, fMaxY=4.0f; float fMinY= -4.0f, fMaxY=4.0f;
if ( fRatio > 1.0f ) if (fRatio > 1.0f) {
{
fMinX = 4.0f * fRatio; fMinX = 4.0f * fRatio;
fMaxX = fMinX+0.5f; fMaxX = fMinX+0.5f;
} }
else if ( fRatio < 1.0f ) else if (fRatio < 1.0f) {
{ fMinY = -4.0f / fRatio;
fMinY = -4.0f / fRatio; fMaxY = 4.0f / fRatio;
fMaxY = 4.0f / fRatio;
} }
_fMaxX = fMaxX;
_fMinX = fMinX;
_fMaxY = fMaxY;
_fMinY = fMinY;
// search for the labels // search for the labels
int num=0; int num=0;
for ( int i=0; i<labels->getNumChildren(); i++ ) for (int i=0; i<labels->getNumChildren(); i++) {
{ if (labels->getChild(i)->getTypeId() == SoTransform::getClassTypeId())
if ( labels->getChild(i)->getTypeId() == SoTransform::getClassTypeId() )
num++; num++;
} }
if ( num > 2 ) if (num > 2) {
{
bool first=true; bool first=true;
float fStep = (fMaxY-fMinY) / ((float)num-2); float fStep = (fMaxY-fMinY) / ((float)num-2);
for ( int j=0; j<labels->getNumChildren(); j++ ) for (int j=0; j<labels->getNumChildren(); j++) {
{ if (labels->getChild(j)->getTypeId() == SoTransform::getClassTypeId()) {
if ( labels->getChild(j)->getTypeId() == SoTransform::getClassTypeId() ) if (first) {
{
if ( first )
{
first = false; first = false;
static_cast<SoTransform*>(labels->getChild(j))->translation.setValue(fMaxX+0.1f,fMaxY-0.05f+fStep,0.0f); static_cast<SoTransform*>(labels->getChild(j))->translation.setValue(fMaxX+0.1f,fMaxY-0.05f+fStep,0.0f);
} }
else else {
{
static_cast<SoTransform*>(labels->getChild(j))->translation.setValue(0,-fStep,0.0f); static_cast<SoTransform*>(labels->getChild(j))->translation.setValue(0,-fStep,0.0f);
} }
} }
} }
} }
// set the vertices spanning the faces for the color gradient _bbox.setBounds(fMinX, fMinY, fMaxX, fMaxY);
int ct = coords->point.getNum()/2; modifyPoints(_bbox);
for ( int j=0; j<ct; j++ )
{
float w = (float)j/(float)(ct-1);
float fPosY = (1.0f-w)*_fMaxY + w*_fMinY;
coords->point.set1Value(2*j, _fMinX, fPosY, 0.0f);
coords->point.set1Value(2*j+1, _fMaxX, fPosY, 0.0f);
}
} }
void SoFCColorGradient::setRange(float fMin, float fMax, int prec) void SoFCColorGradient::setRange(float fMin, float fMax, int prec)
@@ -204,27 +181,23 @@ std::vector<float> SoFCColorGradient::getMarkerValues(float fMin, float fMax, in
std::vector<float> labels; std::vector<float> labels;
// the middle of the bar is zero // the middle of the bar is zero
if ( fMin < 0.0f && fMax > 0.0f && _cColGrad.getStyle() == App::ColorGradient::ZERO_BASED ) if (fMin < 0.0f && fMax > 0.0f && _cColGrad.getStyle() == App::ColorGradient::ZERO_BASED) {
{ if (count % 2 == 0)
if ( count % 2 == 0) count++; count++;
int half = count / 2; int half = count / 2;
for (int j=0; j<half+1; j++) for (int j = 0; j < half + 1; j++) {
{
float w = (float)j/((float)half); float w = (float)j/((float)half);
float fValue = (1.0f-w)*fMax; float fValue = (1.0f-w)*fMax;
labels.push_back( fValue ); labels.push_back( fValue );
} }
for (int k=half+1; k<count; k++) for (int k = half + 1; k < count; k++) {
{
float w = (float)(k-half+1)/((float)(count-half)); float w = (float)(k-half+1)/((float)(count-half));
float fValue = w*fMin; float fValue = w*fMin;
labels.push_back( fValue ); labels.push_back( fValue );
} }
} }
else // either not zero based or 0 is not in between [fMin,fMax] else { // either not zero based or 0 is not in between [fMin,fMax]
{ for (int j = 0; j < count; j++) {
for (int j=0; j<count; j++)
{
float w = (float)j/((float)count-1.0f); float w = (float)j/((float)count-1.0f);
float fValue = (1.0f-w)*fMax+w*fMin; float fValue = (1.0f-w)*fMax+w*fMin;
labels.push_back( fValue ); labels.push_back( fValue );
@@ -234,6 +207,23 @@ std::vector<float> SoFCColorGradient::getMarkerValues(float fMin, float fMax, in
return labels; return labels;
} }
void SoFCColorGradient::modifyPoints(const SbBox2f& box)
{
float fMinX = box.getMin()[0];
float fMinY = box.getMin()[1];
float fMaxX = box.getMax()[0];
float fMaxY = box.getMax()[1];
// set the vertices spanning the faces for the color gradient
int intFields = coords->point.getNum() / 2;
for (int i = 0; i < intFields; i++) {
float w = static_cast<float>(i) / (intFields - 1);
float fPosY = (1.0f - w) * fMaxY + w * fMinY;
coords->point.set1Value(2 * i, fMinX, fPosY, 0.0f);
coords->point.set1Value(2 * i + 1, fMaxX, fPosY, 0.0f);
}
}
void SoFCColorGradient::setColorModel(std::size_t index) void SoFCColorGradient::setColorModel(std::size_t index)
{ {
_cColGrad.setColorModel(index); _cColGrad.setColorModel(index);
@@ -249,23 +239,16 @@ void SoFCColorGradient::setColorStyle (App::ColorGradient::TStyle tStyle)
void SoFCColorGradient::rebuildGradient() void SoFCColorGradient::rebuildGradient()
{ {
App::ColorModel model = _cColGrad.getColorModel(); App::ColorModel model = _cColGrad.getColorModel();
int uCtColors = (int)model.getCountColors(); int uCtColors = static_cast<int>(model.getCountColors());
coords->point.setNum(2*uCtColors); coords->point.setNum(2 * uCtColors);
for ( int i=0; i<uCtColors; i++ ) modifyPoints(_bbox);
{
float w = (float)i/(float)(uCtColors-1);
float fPosY = (1.0f-w)*_fMaxY + w*_fMinY;
coords->point.set1Value(2*i, _fMinX, fPosY, 0.0f);
coords->point.set1Value(2*i+1, _fMaxX, fPosY, 0.0f);
}
// for uCtColors colors we need 2*(uCtColors-1) facets and therefore an array with // for uCtColors colors we need 2*(uCtColors-1) facets and therefore an array with
// 8*(uCtColors-1) face indices // 8*(uCtColors-1) face indices
SoIndexedFaceSet * faceset = new SoIndexedFaceSet; SoIndexedFaceSet * faceset = new SoIndexedFaceSet;
faceset->coordIndex.setNum(8*(uCtColors-1)); faceset->coordIndex.setNum(8 * (uCtColors - 1));
for ( int j=0; j<uCtColors-1; j++ ) for (int j = 0; j < uCtColors - 1; j++) {
{
faceset->coordIndex.set1Value(8*j, 2*j); faceset->coordIndex.set1Value(8*j, 2*j);
faceset->coordIndex.set1Value(8*j+1, 2*j+3); faceset->coordIndex.set1Value(8*j+1, 2*j+3);
faceset->coordIndex.set1Value(8*j+2, 2*j+1); faceset->coordIndex.set1Value(8*j+2, 2*j+1);
@@ -281,9 +264,8 @@ void SoFCColorGradient::rebuildGradient()
ttype->value = SoGLRenderAction::DELAYED_BLEND; ttype->value = SoGLRenderAction::DELAYED_BLEND;
SoMaterial* mat = new SoMaterial; SoMaterial* mat = new SoMaterial;
//mat->transparency = 0.3f; //mat->transparency = 0.3f;
mat->diffuseColor.setNum(2*uCtColors); mat->diffuseColor.setNum(2 * uCtColors);
for ( int k=0; k<uCtColors; k++ ) for (int k = 0; k < uCtColors; k++) {
{
App::Color col = model.colors[uCtColors-k-1]; App::Color col = model.colors[uCtColors-k-1];
mat->diffuseColor.set1Value(2*k, col.r, col.g, col.b); mat->diffuseColor.set1Value(2*k, col.r, col.g, col.b);
mat->diffuseColor.set1Value(2*k+1, col.r, col.g, col.b); mat->diffuseColor.set1Value(2*k+1, col.r, col.g, col.b);
@@ -293,7 +275,7 @@ void SoFCColorGradient::rebuildGradient()
matBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED; matBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
// first clear the children // first clear the children
if ( getNumChildren() > 0 ) if (getNumChildren() > 0)
coinRemoveAllChildren(this); coinRemoveAllChildren(this);
addChild(ttype); addChild(ttype);
addChild(labels); addChild(labels);
@@ -305,8 +287,7 @@ void SoFCColorGradient::rebuildGradient()
bool SoFCColorGradient::isVisible (float fVal) const bool SoFCColorGradient::isVisible (float fVal) const
{ {
if (_bOutInvisible == true) if (_bOutInvisible) {
{
float fMin, fMax; float fMin, fMax;
_cColGrad.getRange(fMin, fMax); _cColGrad.getRange(fMin, fMax);
if ((fVal > fMax) || (fVal < fMin)) if ((fVal > fMax) || (fVal < fMin))
@@ -323,13 +304,13 @@ bool SoFCColorGradient::customize()
QWidget* parent = Gui::getMainWindow()->activeWindow(); QWidget* parent = Gui::getMainWindow()->activeWindow();
Gui::Dialog::DlgSettingsColorGradientImp dlg(parent); Gui::Dialog::DlgSettingsColorGradientImp dlg(parent);
dlg.setColorModelNames( _cColGrad.getColorModelNames() ); dlg.setColorModelNames(_cColGrad.getColorModelNames());
dlg.setColorModel( _cColGrad.getColorModelType() ); dlg.setColorModel(_cColGrad.getColorModelType());
dlg.setColorStyle( _cColGrad.getStyle() ); dlg.setColorStyle(_cColGrad.getStyle());
dlg.setOutGrayed( _cColGrad.isOutsideGrayed() ); dlg.setOutGrayed(_cColGrad.isOutsideGrayed());
dlg.setOutInvisible( _bOutInvisible ); dlg.setOutInvisible(_bOutInvisible);
dlg.setNumberOfLabels( _cColGrad.getCountColors() ); dlg.setNumberOfLabels(_cColGrad.getCountColors());
dlg.setNumberOfDecimals( _precision ); dlg.setNumberOfDecimals(_precision);
float fMin, fMax; float fMin, fMax;
_cColGrad.getRange(fMin, fMax); _cColGrad.getRange(fMin, fMax);
dlg.setRange(fMin, fMax); dlg.setRange(fMin, fMax);
@@ -338,17 +319,16 @@ bool SoFCColorGradient::customize()
pos += QPoint((int)(-1.1*dlg.width()),(int)(-0.1*dlg.height())); pos += QPoint((int)(-1.1*dlg.width()),(int)(-0.1*dlg.height()));
dlg.move( pos ); dlg.move( pos );
if ( dlg.exec() == QDialog::Accepted ) if (dlg.exec() == QDialog::Accepted) {
{ _cColGrad.setColorModel(dlg.colorModel());
_cColGrad.setColorModel( dlg.colorModel() ); _cColGrad.setStyle(dlg.colorStyle());
_cColGrad.setStyle( dlg.colorStyle() ); _cColGrad.setOutsideGrayed(dlg.isOutGrayed());
_cColGrad.setOutsideGrayed( dlg.isOutGrayed() );
_bOutInvisible = dlg.isOutInvisible(); _bOutInvisible = dlg.isOutInvisible();
_cColGrad.setCountColors( dlg.numberOfLabels() ); _cColGrad.setCountColors(dlg.numberOfLabels());
_precision = dlg.numberOfDecimals(); _precision = dlg.numberOfDecimals();
dlg.getRange( fMin, fMax ); dlg.getRange(fMin, fMax);
int dec = dlg.numberOfDecimals(); int dec = dlg.numberOfDecimals();
setRange( fMin, fMax, dec ); setRange(fMin, fMax, dec);
rebuildGradient(); rebuildGradient();
return true; return true;

View File

@@ -25,6 +25,7 @@
#define GUI_SOFCCOLORGRADIENT_H #define GUI_SOFCCOLORGRADIENT_H
#include <Inventor/nodes/SoSeparator.h> #include <Inventor/nodes/SoSeparator.h>
#include <Inventor/SbBox2f.h>
#include "SoFCColorBar.h" #include "SoFCColorBar.h"
#include <App/ColorModel.h> #include <App/ColorModel.h>
#include <vector> #include <vector>
@@ -97,11 +98,12 @@ protected:
private: private:
/** Sets the new labels. */ /** Sets the new labels. */
void setMarkerLabel( const SoMFString& label ); void setMarkerLabel( const SoMFString& label );
void modifyPoints(const SbBox2f&);
private: private:
SoCoordinate3* coords; SoCoordinate3* coords;
SoSeparator* labels; SoSeparator* labels;
float _fMaxX, _fMinX, _fMaxY, _fMinY; SbBox2f _bbox;
bool _bOutInvisible; bool _bOutInvisible;
int _precision; int _precision;
App::ColorGradient _cColGrad; App::ColorGradient _cColGrad;