// ===================================================================
// $Id: spectrum.cpp,v 1.2 2002/12/20 20:36:09 havran Exp $
//
// spectrum.cpp
//
//     This is the implementation of color spectrum concept
//      that is suitable for photorealistic rendering
//
// Licence and usage conditions in November 2003:
// This file can be used and modified only with written permission of
// Vlastimil Havran, Pohodli 27, 57001 Litomysl, the Czech Republic.
// This library is granted to be used by Polina Kondratieva within
// her MPI HIWI job working on BRDFLIB library. The usage condition
// may change in the next version of the library. At the moment,
// the library development is supported by Max-Planck-Institut for
// Informatik, Stuhlsatzenhausweg 85, 66123 Saarbruecken Germany
// See WWW: http://www.mpi-sb.mpg.de
// REPLACEMENT_STRING
//
// Initial coding by Vlastimil Havran, November, 1999
//
// Given to BRDFLIB project for the Polina Kondratieva's
// HIWI JOB assignment in November 2003.
// ===================================================================


// BRDFLIB headers
#include "config.h"
#include "spectrum.h"


__BEGIN_BRDFLIB_SOURCE

// first wavelength is red(700nmm), second green(546.1nm), third blue(435.8nm)
const float WavelengthsRGB[3] = {700e-9, 546.1e-9, 435.8e-9};

// This is the spectrum for CColor3 class, the only instance
// in the whole program - it is specific RGB spectrum.
CSpectrum
SpectrumRGB(3, WavelengthsRGB);

// This constructor expects N bands values specifying the wavelengths
CSpectrum::CSpectrum(int NN, const float bandssrc[]):
  N(NN)
{
  assert( (N > 0) && (N < 65536));

  // the number of boundaries is N
  // allocate the memory to represent the boundary of spectrum
  bands = new float[N];
  assert(bands);

  int i;
  for (i = 0; i < N; i++)
    bands[i] = bandssrc[i];
}

// This is a copy constructor for N bands
CSpectrum::CSpectrum(const CSpectrum &spectrum):
  N(spectrum.N)
{
  // allocate the memory to represent the boundary of spectrum
  bands = new float[N];
  assert(bands);

  int i;
  for (i = 0; i < N; i++)
    bands[i] = spectrum.bands[i]; 
}

// destructor
CSpectrum::~CSpectrum()
{
  if (bands)
    delete [] bands;
}

// assignment operator .. deep copy
CSpectrum &
CSpectrum::operator=(const CSpectrum &spectrum)
{
  // if the new spectrum has another number of bands
  if ( ((bands) && (N != spectrum.N)) ||
       (bands == 0) ) {
    if (bands)
      delete [] bands;
    
    N = spectrum.N;
  
    // the number of boundaries is N+1
    // allocate the memory to represent the boundary of spectrum
    bands = new float[N];
    assert(bands);
  }
    
  int i;
  for (i = 0; i < N; i++)
    bands[i] = spectrum.bands[i];

  return *this;
}


__END_BRDFLIB_SOURCE
