void OpacityTransferFunctionWidget::RestoreControlPoints(int number,
                                                         float max,
                                                         float* scalar, 
                                                         float* opacity) {

  // Temporary control point
  ControlPoint tmp;

  // Set number of control points
  _numberOfControlPoints = number;

  // Set maximum scalar value
  _scalarMaximum = max;

  // Clear all control points
  _controlPoint.clear();

  // Set each control point from arrays
  for (int i = 0 ; i < number ; i++) {
    tmp.SetScalar(scalar[i]);
    tmp.SetOpacity(opacity[i]);
    _controlPoint.push_back(tmp);
  }

  // Set focus point
  _focusPoint = -1;

  return;

}
OpacityTransferFunctionWidget::OpacityTransferFunctionWidget(int w, int h) {

  // Initialize background color
  _backgroundColor[0] = 0.0;
  _backgroundColor[1] = 0.0;
  _backgroundColor[2] = 0.0;
  _backgroundColor[3] = 0.0;

  // Initialize box bound
  _boxBound[0] = 0.0;
  _boxBound[1] = 0.0;
  _boxBound[2] = 0.0;
  _boxBound[3] = 0.0;

  // Initialize box color
  _boxColor[0] = 1.0;
  _boxColor[1] = 1.0;
  _boxColor[2] = 1.0;
  _boxColor[3] = 1.0;

  // Initialize control point color
  _controlPointColor[0] = 1.0;
  _controlPointColor[1] = 1.0;
  _controlPointColor[2] = 1.0;
  _controlPointColor[3] = 1.0;

  // Initialize control point size
  _controlPointSize = 8;

  // Initialize focus point index
  _focusPoint = -1;

  // Initialize focus point color
  _focusPointColor[0] = 0.0;
  _focusPointColor[1] = 0.0;
  _focusPointColor[2] = 0.0;
  _focusPointColor[3] = 1.0;

  // Initialize frustum
  _frustum[0] = 0.0;
  _frustum[1] = 1.0;
  _frustum[2] = 0.0;
  _frustum[3] = 1.0;
  _frustum[4] = -1.0;
  _frustum[5] = 1.0;

  // Initialize line color
  _lineColor[0] = 1.0;
  _lineColor[1] = 1.0;  
  _lineColor[2] = 1.0;
  _lineColor[3] = 1.0;

  // Initialize number of control points
  _numberOfControlPoints = 0;

  // Initialize opacity range
  _opacityMaximum = 1.0;
  _opacityMinimum = 0.0;

  // Initialize size of window
  _pixelDimensions[0] = w;
  _pixelDimensions[1] = h;

  // Initialize scalar range
  _scalarMaximum = 1.0;
  _scalarMinimum = 0.0;

  // Initialize text color
  _textColor[0] = 1.0;
  _textColor[1] = 1.0;
  _textColor[2] = 1.0;
  _textColor[3] = 1.0;

  // Initialize transfer function
  ControlPoint controlPoint;

  // First control point
  controlPoint.SetScalar(0.0);
  controlPoint.SetOpacity(0.0);
  _controlPoint.push_back(controlPoint);

  // Second control point
  controlPoint.SetScalar(1.0);
  controlPoint.SetOpacity(1.0);
  _controlPoint.push_back(controlPoint);

  // Increment number of control points
  _numberOfControlPoints = 2;

}
bool OpacityTransferFunctionWidget::AddControlPoint(int px, int py) {

  // Box coordinates
  float bx = 0.0;
  float by = 0.0;

  // Control point
  ControlPoint controlPoint;

  // Function coordinates
  float scalar = 0.0;
  float opacity = 0.0;

  // World coordinates
  float wx = 0.0;
  float wy = 0.0;

  // Check that pixel coordinates are within world space
  if (IsPixelInWorld(px, py) == true) {

    // Get corresponding world coordinates
    PixelToWorld(px, py, &wx, &wy);

    // Check that world coordinates are within box space
    if (IsWorldInBox(wx, wy) == true) {

      // Get corresponding box coordinates
      WorldToBox(wx, wy, &bx, &by);

      // Get corresponding function coordinates
      BoxToFunction(bx, by, &scalar, &opacity);

      // Set control point
      controlPoint.SetOpacity(opacity);
      controlPoint.SetScalar(scalar);

      // Add control point
      _controlPoint.push_back(controlPoint);

      // Sort control points
      sort(_controlPoint.begin(), _controlPoint.end());

      // Get index of newly added point
      _focusPoint = GetControlPointIndex(scalar, opacity);

      // Increment number of control points
      _numberOfControlPoints++;

      // Control point added
      return true;

    }

  }

  // Set focus point
  _focusPoint = -1;

  // Did not add control point
  return false;

}