/*========================================================================= Program: FusionViewer Module: $RCSfile: PositionModel.java,v $ Language: Java Date: $Date: 2007/02/02 19:27:10 $ Version: $Revision: 1.3 $ Copyright (c) Insightful Corporation. All rights reserved. See Copyright.txt for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ package org.fusionviewer.model; /**\class PositionModel *\brief Stores image coordinates and size in physical scale (millimeters) */ public class PositionModel { /* * A position in physical coordinates (millimeters) */ private float[] position; /* * Dimension of the coordinates (dimension > 0) */ private int dimension; /** * Construct a position container. For example, * new PositionModel(3) creates a 3D position model. * * @param dimension dimension of the position */ public PositionModel(int dimension) { if (dimension <= 0) throw new IllegalArgumentException("Position dimension must be >= 0"); this.position = new float[dimension]; this.dimension = dimension; } /** * Get a component of the position. The components * are indexed from 0. * * @param idx position index * @return position at index */ public float getPosition(int idx) { return position[idx]; } /** * Get the position as an array. * * @return the position */ public float[] getPosition() { float[] pos = new float[dimension]; for (int i = 0; i < dimension; i++) pos[i] = position[i]; return pos; } /** * Set a component of the position. The components are indexed from 0. * * @param idx component to set * @param value set the position component to this value * @return true if the object was changed */ public boolean setPosition(int idx, float value) { if (position[idx] == value) return false; position[idx] = value; return true; } /** * Set the position. * * @param pos an array containing the coordinates * @return true if the object was changed */ public boolean setPosition(float[] pos) { if (dimension != pos.length) throw new IllegalArgumentException("New position does not have the expected number of components (" + dimension + ')'); boolean positionChanged = false; for (int i = 0; i < dimension; i++) { if (position[i] != pos[i]) positionChanged = true; position[i] = pos[i]; } return positionChanged; } }