text: /*
  Copyright (c) 2011, Los Alamos National Security, LLC
  All rights reserved.
  Copyright 2011. Los Alamos National Security, LLC. This software was produced under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL),
  which is operated by Los Alamos National Security, LLC for the U.S. Department of Energy. The U.S. Government has rights to use, reproduce, and distribute this software.
  
  NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.
  
  If software is modified to produce derivative works, such modified software should be clearly marked, so as not to confuse it with the version available from LANL.
  
  Additionally, redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  ·         Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  ·         Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other
            materials provided with the distribution.
  ·         Neither the name of Los Alamos National Security, LLC, Los Alamos National Laboratory, LANL, the U.S. Government, nor the names of its contributors may be used
            to endorse or promote products derived from this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  
  #ifndef CAYLEY_FIELD_H_
  #define CAYLEY_FIELD_H_
  
  #include <piston/image3d.h>
  #include <piston/choose_container.h>
  
  namespace piston {
  
  template <typename MemorySpace>
  struct cayley_field : public piston::image3d<MemorySpace>
  {
      typedef piston::image3d<MemorySpace> Parent;
  
      //TODO: move this to parent class?
      typedef typename thrust::iterator_traits<typename Parent::GridCoordinatesIterator>::value_type
  	    GridCoordinatesType;
  
      // transform grid_coordinates (i, j, k) generated by image2d to shifted and
      // scaled physical_coordinates (x, y, z)
      struct physical_coordinates_functor : public thrust::unary_function<GridCoordinatesType,
  									thrust::tuple<float, float, float> >
      {
  	const float xscale;
  	const float yscale;
  	const float zscale;
  
  	physical_coordinates_functor(unsigned xdim, unsigned ydim, unsigned zdim) :
  	    xscale(2.0f/(xdim - 1.0f)),
  	    yscale(2.0f/(ydim - 1.0f)),
  	    zscale(2.0f/(zdim - 1.0f))
  	{}
  
  	thrust::tuple<float, float, float> operator()(const GridCoordinatesType& pos) const {
  	    // scale and shift such that x, y, z <- [-1,1]
  	    const float x = thrust::get<0>(pos)*xscale - 1.0f;
  	    const float y = thrust::get<1>(pos)*yscale - 1.0f;
  	    const float z = thrust::get<2>(pos)*zscale - 1.0f;
  
  	    return thrust::make_tuple(x, y, z);
  	}
      };
  
      typedef thrust::transform_iterator<physical_coordinates_functor,
  	    typename Parent::GridCoordinatesIterator> PhysicalCoordinatesIterator;
      PhysicalCoordinatesIterator phys_coordinates_iterator;
      typedef typename thrust::iterator_traits<PhysicalCoordinatesIterator>::value_type
   	    PhysicalCoordinatesType;
  
      // transform (x, y, z) physical coordinates into scalar values
      struct cayley_functor : public thrust::unary_function<PhysicalCoordinatesType, float>
      {
  	__host__ __device__
  	float operator()(const PhysicalCoordinatesType& pos) const {
  	    const float x = thrust::get<0>(pos);
  	    const float y = thrust::get<1>(pos);
  	    const float z = thrust::get<2>(pos);
  
  	    const float v = -1.0f + 16.0f*x*y*z + 4.0f*x*x + 4.0f*y*y + 4.0f*z*z;
  
  	    return v;
  	}
      };
  
      // Instead of computing the scalar values on the fly with a transform iterator
      // as the Mandelbrot field, we cache them in the point_data_vector.
      typedef typename detail::choose_container<typename Parent::CountingIterator, float>::type PointDataContainer;
      PointDataContainer point_data_vector;
      typedef typename PointDataContainer::iterator PointDataIterator;
  
      cayley_field(unsigned dim0, unsigned dim1, unsigned dim2) :
  	Parent(dim0, dim1, dim2),
  	phys_coordinates_iterator(Parent::grid_coordinates_iterator,
  	                          physical_coordinates_functor(dim0, dim1, dim2)),
  	point_data_vector(thrust::make_transform_iterator(physical_coordinates_begin(), cayley_functor()),
  	                  thrust::make_transform_iterator(physical_coordinates_end(),   cayley_functor()))
  	                  {}
  
      PhysicalCoordinatesIterator physical_coordinates_begin() {
  	return phys_coordinates_iterator;
      }
      PhysicalCoordinatesIterator physical_coordinates_end() {
  	return phys_coordinates_iterator+this->NPoints;
      }
  
      PointDataIterator point_data_begin() {
  	return point_data_vector.begin();
      }
      PointDataIterator point_data_end() {
  	return point_data_vector.end();
      }
  };
  
  }
  
  #endif /* CAYLEY_FIELD_H_ */
  