void TransFuncEditorIntensityPet::rectifyTransferFunction() { for (int i = 0; i < transferFuncIntensity_->getNumKeys(); ++i) { TransFuncMappingKey* key = transferFuncIntensity_->getKey(i); key->setAlphaL(1.f); key = transferFuncGradient_->getKey(i); key->setAlphaL(1.f); } transferFuncIntensity_->invalidateTexture(); transferFuncGradient_->invalidateTexture(); }
bool TransFuncEditorIntensityPet::transferFunctionCorrect() { // iterate through all keys and test whether they are set to maximum alpha for (int i = 0; i < transferFuncIntensity_->getNumKeys(); ++i) { TransFuncMappingKey* key = transferFuncIntensity_->getKey(i); if ((key->getAlphaL() != 1.f) || (key->isSplit() && key->getAlphaR() != 1.f)) { return false; } } return true; }
void TransFuncEditorIntensityPet::resetTransferFunction() { transferFuncIntensity_->createStdFunc(); //set left key to maximum alpha TransFuncMappingKey* key = transferFuncIntensity_->getKey(0); key->setAlphaL(1.f); transferFuncGradient_->createStdFunc(); //set left key to maximum alpha key = transferFuncGradient_->getKey(0); key->setAlphaL(1.f); }
void TransFuncMappingCanvasRamp::mousePressEvent(QMouseEvent* event) { if (event->button() == Qt::LeftButton) { emit toggleInteractionMode(true); } event->accept(); tgt::vec2 sHit = tgt::vec2(event->x(), static_cast<float>(height()) - event->y()); tgt::vec2 hit = stow(sHit); // see if a key was selected selectedKey_ = 0; for (int i=0; i<tf_->getNumKeys(); ++i) { TransFuncMappingKey* key = tf_->getKey(i); tgt::vec2 sp = wtos(tgt::vec2(key->getIntensity(), key->getColorL().a / 255.f)); tgt::vec2 spr = wtos(tgt::vec2(key->getIntensity(), key->getColorR().a / 255.f)); if (sHit.x > sp.x - pointSize_ && sHit.x < sp.x + pointSize_ && sHit.y > sp.y - pointSize_ && sHit.y < sp.y + pointSize_) { selectedKey_ = key; } } if (event->button() == Qt::RightButton) { if (selectedKey_ == 0) showNoKeyContextMenu(event); else showKeyContextMenu(event); return; } if (selectedKey_ != 0 && event->button() == Qt::LeftButton) { dragging_ = true; //keep values within valid range hit = tgt::clamp(hit, 0.f, 1.f); updateCoordinates(event->pos(), hit); if (selectedKey_->isSplit() && !selectedLeftPart_) emit colorChanged(Col2QColor(selectedKey_->getColorR())); else emit colorChanged(Col2QColor(selectedKey_->getColorL())); } }
col4 TransFunc1DKeys::getMappingForValue(float value) const { // If there are no keys, any further calculation is meaningless if (keys_.empty()) return col4(0, 0, 0, 0); // Restrict value to [0,1] value = (value < 0.f) ? 0.f : value; value = (value > 1.f) ? 1.f : value; // iterate through all keys until we get to the correct position std::vector<TransFuncMappingKey*>::const_iterator keyIterator = keys_.begin(); while ((keyIterator != keys_.end()) && (value > (*keyIterator)->getIntensity())) keyIterator++; if (keyIterator == keys_.begin()) return keys_[0]->getColorL(); else if (keyIterator == keys_.end()) return (*(keyIterator-1))->getColorR(); else{ // calculate the value weighted by the destination to the next left and right key TransFuncMappingKey* leftKey = *(keyIterator-1); TransFuncMappingKey* rightKey = *keyIterator; float fraction = (value - leftKey->getIntensity()) / (rightKey->getIntensity() - leftKey->getIntensity()); col4 leftDest = leftKey->getColorR(); col4 rightDest = rightKey->getColorL(); col4 result = leftDest; result.r += static_cast<uint8_t>((rightDest.r - leftDest.r) * fraction); result.g += static_cast<uint8_t>((rightDest.g - leftDest.g) * fraction); result.b += static_cast<uint8_t>((rightDest.b - leftDest.b) * fraction); result.a += static_cast<uint8_t>((rightDest.a - leftDest.a) * fraction); return result; } }
void TransFunc1DKeys::generateKeys(unsigned char* data) { /* A short overview about the idea behind this method. For the sake of simplicity this is demonstrated with only one color channel, say 'Red'. It is generalized in the code below: We want to detect the peaks(=extrema) in the graph containing all the 'red'-values. In order to do this, we look at one 1/width-th at a time and compare the difference between the (i-2)th and (i-1)th point (= oldDelta_x) to the difference between the (i-1)th and ith point (stored in newDelta_x). Several possible things can happen: i) The difference doesn't change at all. This means that the difference between the points is linearly dependent and in this case, we don't have to add another key because we get linear interpolation from the the methods ii) The difference might be not-zero. This means the graph is discontinuous at this point and we have to insert a splitted point. On the left side we take the color from the (i-1)th point and on the right side from the ith point. The discontinuity is represented by the "jump" between the two parts of the splitted mapping key. iii) The difference could have changed by a factor of -1. In this case, we have to insert a mapping key right at the peak (i.e. at the ith point). If a key is placed, we don't want to place a key at the next location, because the difference will change not matter if there is a peak or not. There are a lot of cases, in this redundant keys will be generated and we don't want that In the code below, we generate a key whenever any of the colorchannels meets a criterion above. */ // Storage for the old values int oldDeltaRed; int oldDeltaGreen; int oldDeltaBlue; int oldDeltaAlpha; // Storage for the new values int newDeltaRed; int newDeltaGreen; int newDeltaBlue; int newDeltaAlpha; // We want at least 2 values in the data array if (dimensions_.x < 2) return; clearKeys(); addKey(new TransFuncMappingKey(0.f, col4(data[0], data[1], data[2], data[3]))); addKey(new TransFuncMappingKey(1.f, col4(data[4*(dimensions_.x-1)+0], data[4*(dimensions_.x-1)+1], data[4*(dimensions_.x-1)+2], data[4*(dimensions_.x-1)+3]))); // Calculate the starting point newDeltaRed = data[4*1 + 0] - data[4*0 + 0]; newDeltaGreen = data[4*1 + 1] - data[4*0 + 1]; newDeltaBlue = data[4*1 + 2] - data[4*0 + 2]; newDeltaAlpha = data[4*1 + 3] - data[4*0 + 3]; // The main loop. We start at 2 because the value for 1 already has been calculated. for (int iter = 2; iter < dimensions_.x; ++iter) { // Backup the old values and generate the new ones. oldDeltaRed = newDeltaRed; oldDeltaGreen = newDeltaGreen; oldDeltaBlue = newDeltaBlue; oldDeltaAlpha = newDeltaAlpha; newDeltaRed = data[4*iter + 0] - data[4*(iter-1) + 0]; newDeltaGreen = data[4*iter + 1] - data[4*(iter-1) + 1]; newDeltaBlue = data[4*iter + 2] - data[4*(iter-1) + 2]; newDeltaAlpha = data[4*iter + 3] - data[4*(iter-1) + 3]; // Has the difference quotient changed in any color channel? bool differenceQuotientChanged = ( (oldDeltaRed != newDeltaRed) || (oldDeltaGreen != newDeltaGreen) || (oldDeltaBlue != newDeltaBlue) || (oldDeltaAlpha != newDeltaAlpha)); // Is the difference quotient different from zero in any channel? bool differenceQuotientNotZero = ( (newDeltaRed != 0) || (newDeltaGreen != 0) || (newDeltaBlue != 0) || (newDeltaAlpha != 0)); // Has the difference quotient tilted in all channel's? // Mind the & instead of | bool differenceQuotientTilted = ( (oldDeltaRed == -newDeltaRed) && (oldDeltaGreen == -newDeltaGreen) && (oldDeltaBlue == -newDeltaBlue) && (oldDeltaAlpha == -newDeltaAlpha)); if (differenceQuotientChanged) { if (differenceQuotientNotZero) { // We want to put a splitted key here (see ii above) TransFuncMappingKey* newkey = new TransFuncMappingKey(iter/static_cast<float>(dimensions_.x-1) , col4( data[4*(iter-1) + 0], data[4*(iter-1) + 1], data[4*(iter-1) + 2], data[4*(iter-1) + 3] ) ); newkey->setSplit(true); newkey->setColorR(tgt::col4(data[4*iter + 0], data[4*iter + 1], data[4*iter + 2], data[4*iter + 3])); addKey(newkey); } else if (differenceQuotientTilted) { // We want a single key at i-1 here (see iii above) addKey( new TransFuncMappingKey((iter - 1)/static_cast<float>(dimensions_.x-1), col4(data[4*(iter-1) + 0], data[4*(iter-1) + 1], data[4*(iter-1) + 2], data[4*(iter-1) + 3]) )); } else { // Just add a key addKey( new TransFuncMappingKey(iter/static_cast<float>(dimensions_.x-1), col4(data[4*iter + 0], data[4*iter + 1], data[4*iter + 2], data[4*iter + 3]))); } } } }
TransFunc* InterpolationFunction<TransFunc*>::interpolate(TransFunc* startvalue, TransFunc* endvalue, float time) const { if (!startvalue || !endvalue) { LERROR("Null pointer passed"); return 0; } TransFuncIntensity* func1 = dynamic_cast<TransFuncIntensity*>(startvalue); TransFuncIntensity* func2 = dynamic_cast<TransFuncIntensity*>(endvalue); if (func1 && func2) { std::vector<TransFuncMappingKey*> keys1 = func1->getKeys(); std::vector<TransFuncMappingKey*> keys2 = func2->getKeys(); if (keys1.size() == keys2.size()) { TransFuncIntensity* func = new TransFuncIntensity(); tgt::vec2 t1 = func1->getThresholds(); tgt::vec2 t2 = func2->getThresholds(); func->setThresholds((1-time)*t1.x+time*t2.x, (1-time)*t1.y+time*t2.y); func->clearKeys(); std::vector<TransFuncMappingKey*>::iterator it1 = keys1.begin(); std::vector<TransFuncMappingKey*>::iterator it2 = keys2.begin(); while ((it1 != keys1.end()) && (it2 != keys2.end())) { tgt::col4 col = tgt::col4(); TransFuncMappingKey* key = new TransFuncMappingKey(0, col); key->setSplit((*it1)->isSplit()||(*it2)->isSplit(), true); col.r = static_cast<uint8_t>((1-time)*(*it1)->getColorL().r + time*(*it2)->getColorL().r); col.g = static_cast<uint8_t>((1-time)*(*it1)->getColorL().g + time*(*it2)->getColorL().g); col.b = static_cast<uint8_t>((1-time)*(*it1)->getColorL().b + time*(*it2)->getColorL().b); col.a = static_cast<uint8_t>((1-time)*(*it1)->getColorL().a + time*(*it2)->getColorL().a); key->setColorL(col); col.r = static_cast<uint8_t>((1-time)*(*it1)->getColorR().r + time*(*it2)->getColorR().r); col.g = static_cast<uint8_t>((1-time)*(*it1)->getColorR().g + time*(*it2)->getColorR().g); col.b = static_cast<uint8_t>((1-time)*(*it1)->getColorR().b + time*(*it2)->getColorR().b); col.a = static_cast<uint8_t>((1-time)*(*it1)->getColorR().a + time*(*it2)->getColorR().a); key->setColorR(col); key->setIntensity((1-time)*(*it1)->getIntensity() + time*(*it2)->getIntensity()); func->addKey(key); it1++; it2++; } func->invalidateTexture(); return func; } } float a2 = BasicFloatInterpolation::linearInterpolation(0, 1, time); float a1 = 1 - a2; tgtAssert(startvalue && endvalue, "null pointer"); tgt::ivec3 dimensions_start = startvalue->getDimensions(); tgt::ivec3 dimensions_end = endvalue->getDimensions(); tgt::ivec3 dim = tgt::ivec3(); dim.x = std::max(dimensions_start.x,dimensions_end.x); dim.y = std::max(dimensions_start.y,dimensions_end.y); dim.z = std::max(dimensions_start.z,dimensions_end.z); GLubyte* texture1 = startvalue->getPixelData(); GLubyte* texture2 = endvalue->getPixelData(); texture1 = convertTextureToRGBA(startvalue->getDimensions(), texture1 , startvalue->getFormat()); texture2 = convertTextureToRGBA(endvalue->getDimensions(), texture2 , endvalue->getFormat()); texture1 = changeTextureDimension(startvalue->getDimensions(), dim, texture1); texture2 = changeTextureDimension(startvalue->getDimensions(), dim, texture2); TransFunc* func = new TransFunc(dim.x, dim.y, dim.z); GLubyte* texture = new GLubyte[4 * dim.x * dim.y * dim.z]; for (int x = 0; x < dim.x; ++x) { for (int y = 0; y < dim.y; ++y) { for (int z = 0; z < dim.z; ++z) { for (int i = 0; i < 4; ++i) { int index = 4*(x * dim.y * dim.z + y * dim.z + z) + i; float f = (a1 * texture1[index] + a2 * texture2[index]); texture[index] = static_cast<GLubyte>(f); } } } } func->setPixelData(texture); return func; }
void TransFuncMappingCanvasRamp::mouseMoveEvent(QMouseEvent* event) { unsetCursor(); event->accept(); mousePos_ = event->pos(); vec2 sHit = vec2(event->x(), static_cast<float>(height()) - event->y()); vec2 hit = stow(sHit); // return when no key was inserted or selected if (!dragging_) return; // keep location within valid texture coord range hit = tgt::clamp(hit, 0.f, 1.f); if (selectedKey_ != 0) { TransFuncMappingKey* leftKey = tf_->getKey(0); TransFuncMappingKey* rightKey = tf_->getKey(1); if (selectedKey_ == leftKey) { // obey ramp function restrictions: // left key has to stay left of right key hit.x = std::min<float>(hit.x, rightKey->getIntensity()); // max width = 1.f, min center = 0.f float minX = rightKey->getIntensity() - 1.f; float maxY = std::min(-minX, 0.5f); hit.y = std::min(hit.y, maxY); if (rightKey->getIntensity() == 1.f) { minX = rightKey->getIntensity() - rightKey->getColorL().a / 255.f; hit.x = std::max(hit.x, minX); } // moving left upwards only allowed if at left border (ramp function) if (hit.x != 0.f) hit.y = 0.f; } else { // obey ramp function restrictions: // right key has to stay right of right key hit.x = std::max<float>(hit.x, leftKey->getIntensity()); // max width = 1.f, max center = 1.f float maxX = leftKey->getIntensity() + 1.f; float minY = std::max(2.f - maxX, 0.5f); hit.y = std::max(hit.y, minY); if (leftKey->getIntensity() == 0.f) { float maxX = 1.f - leftKey->getColorL().a / 255.f; hit.x = std::min(hit.x, maxX); } // moving right downwards only allowed if at right border (ramp function) if (hit.x != 1.f) hit.y = 1.f; } selectedKey_->setIntensity(hit.x); selectedKey_->setAlphaL(hit.y); calcRampParameterFromKeys(); updateCoordinates(event->pos(), vec2(hit.x, selectedKey_->getAlphaL())); repaint(); emit changed(); } }