/*************************************************************************
 *    CompuCell - A software framework for multimodel simulations of     *
 * biocomplexity problems Copyright (C) 2003 University of Notre Dame,   *
 *                             Indiana                                   *
 *                                                                       *
 * This program is free software; IF YOU AGREE TO CITE USE OF CompuCell  *
 *  IN ALL RELATED RESEARCH PUBLICATIONS according to the terms of the   *
 *  CompuCell GNU General Public License RIDER you can redistribute it   *
 * and/or modify it under the terms of the GNU General Public License as *
 *  published by the Free Software Foundation; either version 2 of the   *
 *         License, or (at your option) any later version.               *
 *                                                                       *
 * This program is distributed in the hope that it will be useful, but   *
 *      WITHOUT ANY WARRANTY; without even the implied warranty of       *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    *
 *             General Public License for more details.                  *
 *                                                                       *
 *  You should have received a copy of the GNU General Public License    *
 *     along with this program; if not, write to the Free Software       *
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.        *
 *************************************************************************/

#include "AdditiveRenderer.h"

#include <CompuCell3D/Simulator.h>
#include <CompuCell3D/ClassRegistry.h>
using namespace CompuCell3D;

#include <XMLCereal/XMLPullParser.h>
#include <XMLCereal/XMLSerializer.h>

#include <BasicUtils/BasicString.h>

using namespace std;

AdditiveRenderer::AdditiveRenderer() {
  renderers[0] = renderers[1] = 0;
}


VizProperties AdditiveRenderer::get(const Point3D &pt) const {
  ASSERT_OR_THROW("get() Cell field not set!", cellFieldG);

  VizProperties v0 = renderers[0]->get(pt);
  VizProperties v1 = renderers[1]->get(pt);

  return VizProperties((v0.red + v1.red) % 256,
		       (v0.green + v1.green) % 256,
		       (v0.blue + v1.blue) % 256,
		       (v0.opacity + v1.opacity) % 256);
}

void AdditiveRenderer::readXML(XMLPullParser &in) {
  int rendererCount = 0;
  ClassRegistry *reg = simulator->getClassRegistry();

  in.skip(TEXT);

  while (in.check(START_ELEMENT)) {
    if (in.getName() == "Renderer") {
      ASSERT_OR_THROW("No more than 2 renderers allowed!", rendererCount < 2);

      renderers[rendererCount++] =
	reg->getRenderer(BasicString::trim(in.matchSimple()));

    } else {
      throw BasicException(string("Unexpected element '") + in.getName() +
                           "'!", in.getLocation());
    }

    in.skip(TEXT);
  }

  ASSERT_OR_THROW("You must specify 2 renderers.", rendererCount == 2);
}

void AdditiveRenderer::writeXML(XMLSerializer &out) {
}
