/*******************************************************************************
 * Copyright (c) 2010 Attila Kovacs <attila_kovacs@post.harvard.edu>.
 * All rights reserved. 
 * 
 * This file is part of crush.
 * 
 *     crush is free software: 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 3 of the License, or
 *     (at your option) any later version.
 * 
 *     crush 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 crush.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Attila Kovacs <attila_kovacs@post.harvard.edu> - initial API and implementation
 ******************************************************************************/
// Copyright (c) 2009 Attila Kovacs 

package crush.array;

import java.util.*;

import crush.*;

import util.Util;
import util.Vector2D;

public class SimplePixel extends Channel implements Pixel {
	public Vector2D position;
	public double resolution;
	public Vector<? extends SimplePixel> neighbours;
	public boolean independent = false;
	
	public SimplePixel(int backendIndex) { 
		super(backendIndex); 
	}
	
	@Override
	public Object clone() {
		SimplePixel clone = (SimplePixel) super.clone();
		if(position != null) clone.position = (Vector2D) position.clone();
		return clone;
	}
	
	public boolean isValid() { return isFlagged(); }
	
	public final int getIndex() { return index; }
	
	public final int getDataIndex() { return dataIndex; }
	
	public final Vector2D getPosition() { return position; }
	
	public final double distanceTo(final Pixel pixel) {
		return position.distanceTo(pixel.getPosition());
	}
	
	public void setIndependent(boolean value) {
		independent = value;
	}
	
	@Override
	// Assume Gaussian response with FWHM = resolution;
	public double overlap(final Channel channel) {
		if(independent) return 0.0;
		
		if(channel instanceof Pixel) {
			double dev = distanceTo((Pixel) channel) * Util.sigmasInFWHM / resolution;
			return Math.exp(-0.5 * dev * dev);
		}
		// If other channel is not a pixel assume it is independent...
		return 0.0;
	}
	
	public final Iterator<Channel> iterator() {
		final Channel channel = this;
		
		return new Iterator<Channel>() {
			boolean unused = true;
			
			public final boolean hasNext() { return unused; }

			public Channel next() {
				unused = false;
				return channel;
			}

			public void remove() {}
		};			
	}

	public double getResolution() {
		return resolution;
	}

	public void setResolution(double value) {
		resolution = value;
	}

	public final int channels() {
		return 1;
	}

	public final Channel getChannel(int i) {
		return i == 0 ? this : null; 
	}
	
	
}
