Brain.java 2.41 KB
package osp;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

/**
 * This class is used in the generation of automatic solutions. <br>
 * <br>
 * It is able to :<br>
 * <ul>
 * <li>Search the barycenter of a skyObject list (and save it for later access).
 * </li>
 * <li>Calculate the ratio of objects affected to slits on a mask.</li>
 * </ul>
 */
public class Brain {
	// Attributes
	// -----------
	/**
	 * Barycenter of the sky objects. It's saved so it can be accessed multiple
	 * times without needing to recalculate it each time.
	 */
	private Point2D.Double bary;

	/**
	 * Constructs a new brain.
	 */
	public Brain() {
		bary = new Point2D.Double();
	}

	/**
	 * Computes the barycenter of the <a href="./SkyObject.html">SkyObject</a>
	 * list so (all objects have the same mass). The barycenter is saved for
	 * later access.
	 * 
	 * @param so : The list of object to compute the barycenter of.
	 * @return bary :  Barycenter of the sky objects.
	 */
	public Point2D.Double computeBary(List<SkyObject> so) {
		List<Point2D.Double> lo = new ArrayList<Point2D.Double>();
		for (int i = 0; i < so.size(); i++)
			lo.add(so.get(i).getPosition());

		bary.x = 0.0;
		bary.y = 0.0;
		for (int i = 0; i < lo.size(); i++) {
			bary.x = bary.x + lo.get(i).getX();
			bary.y = bary.y + lo.get(i).getY();
		}
		bary.x = bary.x / lo.size();
		bary.y = bary.y / lo.size();

		return bary;
	}

	/**
	 * Returns the barycenter. <b>The barycenter is not recalculated.</b> If it
	 * has not been previously calculated, the barycenter will have the
	 * coordinates (0,0).
	 * 
	 * @return The barycenter previously calculated.
	 */
	public Point2D.Double getBary() {
		return bary;
	}

	/**
	 * Computes and sets (in the mask) the ratio of objects on the image that
	 * are affected to slits on the current mask. It goes from 0, if no object
	 * is affected to a slit on the current mask, to 1, if all object of the
	 * image are affected to slits on the current mask.
	 * 
	 * @param mask
	 *            Mask concerned by the calculations.
	 */
	public void computeRatioAffectedSlit(Mask mask) {
		double ratio = 0.0;
		for (int i = 0; i < Mask.get_NBRSLITS_(); i++) {
			if (mask.getSlit(i).getAffectedObject() != -1) {
				ratio = ratio + 1;
			}
		}
		ratio = ratio / mask.imagePater.getNbrObjects();

		mask.ratioAffectedSlit = ratio;
	}
}