FieldOfView.java 9.36 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
package osp;

import java.awt.geom.Point2D;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import jsky.image.fits.codec.FITSImage;
import osp.ui.FieldOfViewInterface;
import osp.ui.FieldOfViewSelectionInterface;

/**
 * This class is the main class for data. 
 * The Field of View contains a list of images. 
 * The methods allow managing images and working on and with them.
 */
public class FieldOfView extends Observable implements FieldOfViewInterface, FieldOfViewSelectionInterface {
	// plotSpectra constant values
	/** Don't plot any spectrum. */
	public static final int _PLOTNONE_ = 0;
	/** Plot only the selected spectra. */
	public static final int _PLOTSELECTED_ = 1;
	/** Plot all of the spectra. */
	public static final int _PLOTALL_ = 2;

	// Attributes
	// -----------
	/** Workspace path of the project */
	private String path;
	/** File name of the project */
	private String fileName;
	/** Selected image in the field of view, or -1 if none. */
	private int selectedImage;
	/** List of the images */
	private List<Image> tabImage = new ArrayList<Image>();
	/**
	 * The total number of images that have been added to the project (including
	 * deleted ones), used to give them unique names.
	 */
	private int totalImages = 0;
	/**
	 * Determines if spectra should be displayed or not, depending on the value,
	 * from the list of possible values defined as final attributes.
	 */
	private int plotSpectra = _PLOTNONE_;
	/** Brain of the project, for calculations. */
	private Brain theBrain;
	
	/** image file name */
	private String imageFileName;

	// Constructors
	// --------------
	/**
	 * Constructs a new field of view with no images.
	 */
	public FieldOfView() {
		setSelectedImage(-1);
		theBrain = new Brain();
	}

	// Methods
	// ---------
	/**
	 * Notifies observers of an update of the field of view.
	 */
	public void update() {
		setChanged();
		notifyObservers();
	}

	/**
	 * Sets the (physical) path of the project folder.
	 * 
	 * @param s : The path of the project.
	 */
	public void setPath(String s) {
		path = s;
	}

	/**
	 * Returns the (physical) path of the project folder.
	 * 
	 * @return The path of the project.
	 */
	public String getPath() {
		return path;
	}

	/**
	 * Sets the name of the file corresponding to the project.
	 * 
	 * @param n : The file name for the project.
	 */
	public void setFileName(String n) {
		fileName = n;
	}

	/**
	 * Returns the name of the file corresponding to the project.
	 * 
	 * @return The file name for the project.
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * Returns the complete path of the project, composed of both the path to
	 * the project folder and the filename
	 * 
	 * @return Complete path of the project.
	 */
	public String getCompletePath() {
		return path + File.separator + fileName;
	}

	/**
	 * Sets the currently selected image.
	 * 
	 * @param n : Number of the selected image.
	 */
	@Override
	public void setSelectedImage(int n) {
		selectedImage = n;

	}

	/**
	 * Returns the current image number.
	 * 
	 * @return Number of the current image.
	 */
	@Override
	public int getSelectedImage() {
		return selectedImage;
	}

	/**
	 * Adds a new image to the project and returns it
	 * 
	 * @param fitsIm :  The FITS image.
	 * @param position : Position of the image center in canvas position.
	 * @param scale :  Scale of the display.
	 * @param imName : Image Name
	 * @param imCat : Catalog where image is from.
	 */
	public Image addImage(FITSImage fitsIm, Point2D.Double position, float scale, String imName, String imCat) {
		Image im;
//System.out.println("Fov 151 "+imName+" "+imCat);
		
		if ((imName == null) ||  (imCat == null))
			im = new Image(fitsIm, totalImages);
		else 
			 im = new Image(fitsIm, totalImages,imName, imCat);
		
		im.setCenter(position);
		im.setScale(scale);

		tabImage.add(im);
		setSelectedImage(tabImage.size() - 1);
		totalImages++;
		return im;
	}

	/**
	 * Returns the n number image.
	 * 
	 * @param n : Number of the image.
	 * @return The corresponding image.
	 */
	@Override
	public Image getImage(int n) {
		return tabImage.get(n);
	}

	/**
	 * Returns the current image.
	 * 
	 * @return The current image.
	 */
	@Override
	public Image getCurrentImage() {
		Image res = null;
		if (selectedImage !=-1){
			res = tabImage.get(selectedImage);
		}
		return res;
	}

	/**
	 * Returns the total number of images in the project.
	 * 
	 * @return Number of images.
	 */
	@Override
	public int getNbrImages() {
		return tabImage.size();
	}

	/**
	 * Removes the num number image from the project.
	 * 
	 * @param num : The number of the image to remove.
	 */
	public void removeImage(int num) {
		tabImage.get(num).resetTotalMask();
		setSelectedImage(num-1);
		tabImage.remove(num);
		//setSelectedImage((tabImage.size() - 1));
		
		// Decrease total number of images
		if (num==tabImage.size()) 
		{
			if (totalImages>0) totalImages=tabImage.size();
			else totalImages=0;
		}
      
	}

	/**
	 * Resets (sets to 0) the total number of images that have been added to the
	 * project (including deleted ones).
	 */
	public void resetTotalImage() {
		totalImages = 0;
	}

	/**
	 * Sets if the spectra of the slits should be displayed. The possible values
	 * are defined by the constants _PLOTNONE_, _PLOTSELECTED_ and _PLOTALL_.
	 * They correspond to the following behaviour:<br>
	 * <ul>
	 * <li>_PLOTNONE_ = don't plot any spectrum.</li>
	 * <li>_PLOTSELECTED_ = plot only the selected spectra (manually selected).</li>
	 * <li>_PLOTALL_ = plot all of the spectra.</li>
	 * </ul>
	 * 
	 * @param spec : The value corresponding to the wanted behaviour.
	 */
	public void setPlotSpectra(int spec) {
		plotSpectra = spec;
	}

	/**
	 * Returns a value corresponding to what spectra are being displayed.<br/>
	 * See <a href="#setPlotSpectra(int)">setPlotSpectra()</a> or the constants
	 * definitions for more details.
	 * 
	 * @return The value corresponding to what spectra are being displayed.
	 */
	public int getPlotSpectra() {
		return plotSpectra;
	}

	/**
	 * Returns the brain of the project, used for some calculations.
	 * 
	 * @return The brain of the project.
	 */
	public Brain getTheBrain() {
		return theBrain;
	}

	/**
	 * Sets the brain of the project, used for some calculations.
	 * 
	 * @param aBrain : The brain of the project.
	 */
	public void setTheBrain(Brain aBrain) {
		this.theBrain = aBrain;
	}

	/**
	 * Returns the selected mask of current image
	 */
	@Override
	public int getSelectedMaskOfCurrentImage() {
		return getCurrentImage().getSelectedMask();
	}

	/**
	 * Returns the current mask of current image
	 */
	@Override
	public Mask getCurrentMaskOfCurrentImage() {
		return getCurrentImage().getCurrentMask();
	}

	/**
	 * Calculation of the objects affected in slits ratio
	 * on the field total objects number.
	 * 
	 */
	@Override
	public void computeWithTheBrain() {
		if (getNbrImages() > 0 && getSelectedImage() >= 0) {
			getTheBrain().computeBary(getCurrentImage().getListObjects());

			if (getCurrentMaskOfCurrentImage() != null)
				getTheBrain().computeRatioAffectedSlit(getCurrentImage().getCurrentMask());
		}
		
	}

	/**Returns the I image masks number.
	 * @param i : image indice
	 */
	@Override
	public int getNbrMasksOfImage(int i) {
		return getImage(i).getNbrMasks();
	}

	/**Returns the J mask name of the I image.
	 * @param i : image indice
	 * @param j : mask indice 
	 */
	@Override
	public String getNameOfMaskOfImage(int i, int j) {
		return getImage(j).getMask(i).getName();
	}

	@Override
	public String getNameOfImage(int j) {
		return getImage(j).getProjImName();
	}

	/**
	 * selecting  the J mask of the I image.
	 * @param i : image indice
	 * @param j : mask indice 
	 */
	@Override
	public void setSelectedMaskOfImage(int j, int i) {
		getImage(i).setSelectedMask(j);
		
	}

	/**
	 * Returns true if the j mask is validated 
	 * i indice of image
	 * j indice of mask
	 */
	@Override
	public boolean isMaskOfImageValidated(int j, int i) {
		return getImage(i).getMask(j).validated;
	}

	/**
	 * Returns the name of the image file
	 * @return String imageFileName : image file name
	 */
	public String getImageFileName() {
		return imageFileName;
	}

	/**
	 * Sets the image file name
	 * @param imageFileName
	 */
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	/**
	 * Returns the current image objects number.
	 * @return int : objects number
	 */
	public int getNbObjectsOfCurrentImage() {
		return getCurrentImage().getNbrObjects();
	}

	/**
	 * sets the image displaying scale 
	 * @param zoomFactor
	 */
	public void setScale(float zoomFactor) {
		if (getSelectedImage() >= 0){
			getCurrentImage().setScale(zoomFactor);
		}
	}
	
	/**
	 * Returns the fov elements (images and masks) number.
	 * @return int : elements number
	 */
	@Override
	public int getNbItems() {
		int res = 0;
		for (int j = 0; j < getNbrImages(); j++) {
			res++;
			for (int i = 0; i < getNbrMasksOfImage(j); i++) {
				res++;
			}
		}
		return res;
	}

	@Override
	public void setOmega(float angle) {
		// TODO Auto-generated method stub
		
	}
}