/*============================================================================= Project: SharpImage Module: siShapeSelection.cs Language: C# Author: Dan Mueller Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ Revision: $Revision: 2 $ Copyright (c) Queensland University of Technology (QUT) 2007. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Windows.Forms; using System.Diagnostics; using SharpImage.Main; using SharpImage.Properties; namespace SharpImage.Rendering { /// /// An extrusion selection is an object which can be displayed /// in a number of different ways: as a two-dimensional slice, /// as a three-dimensional object, or as a two-dimensional shape /// extruded into the third dimension (index=2). /// public abstract class siShapeSelection : siSelection { #region Enumerations //===================================================================== public enum ShapeTypeEnum { /// /// Renders the selection as a 2D or 3D object. /// Eg. if the siCircleSelection is set to ShapeTypeEnum.Object /// it will be rendered as a circle (2D) or a sphere (3D). /// Object, /// /// Renders the selection as an extruded object with the primary shape /// extruded along the other axis. /// Eg. if the siCircleSelection is set to ShapeTypeEnum.Extrusion /// it will be rendered as a circle (2D) or a cynlinder(3D). /// Extrusion } //========================================================================= #endregion #region Construction and Disposal //===================================================================== //===================================================================== #endregion #region Properties //===================================================================== #region ExtrusionDimension //===================================================================== private int m_ExtrusionDimension = 0; /// /// Gets/sets the dimension to extrude the shape along. /// public virtual int ExtrusionDimension { get { return this.m_ExtrusionDimension; } set { this.m_ExtrusionDimension = value; } } //===================================================================== #endregion #region ShapeType //===================================================================== private ShapeTypeEnum m_ShapeType = ShapeTypeEnum.Object; /// /// Gets/sets the type of shape for the selection. /// public virtual ShapeTypeEnum ShapeType { get { return this.m_ShapeType; } set { this.m_ShapeType = value; } } //===================================================================== #endregion #region Start/End Extrusion Point //===================================================================== private double m_ExtrusionStartPoint = 0.0; private double m_ExtrusionEndPoint = 1.0; /// /// Gets/sets the starting point along the secondary plane for the extrusion. /// public virtual double ExtrusionStartPoint { get { return this.m_ExtrusionStartPoint; } set { this.m_ExtrusionStartPoint = value; this.RaiseModified(); } } /// /// Gets/sets the ending point along the secondary plane for the extrusion. /// public virtual double ExtrusionEndPoint { get { return this.m_ExtrusionEndPoint; } set { this.m_ExtrusionEndPoint = value; this.RaiseModified(); } } //===================================================================== #endregion //===================================================================== #endregion #region Public Methods //===================================================================== //===================================================================== #endregion #region Protected Methods //===================================================================== /// /// Returns the dimensions to renderer the 2D shape. /// Eg. if ExtrusionDimension=2, returns [0,1] etc. /// /// /// protected int[] GetShapeDimensions(uint numberOfDimensions) { if (numberOfDimensions == 2) return new int[] { 0, 1 }; int[] result = new int[2]; int j =0; for (int i = 0; i < numberOfDimensions; i++) if (this.ExtrusionDimension != i) result[j++] = i; return result; } /// /// Renderer the selection as an extruded object. /// /// protected virtual void OnPaintExtrusion(siGdiSliceRenderer renderer, PaintEventArgs e) { // Get the graphics handle Graphics g = e.Graphics; // Get the input image itk.itkImageBase input = this.GetInputAsImage(renderer); if (input == null) return; // Determine if we are painting the shape or the extrusion if (this.IsRenderingShape(renderer, input.Direction)) { // Rendering the shape this.OnPaintShape(renderer, e); } else { // Rendering the extrusion this.OnPaintExtrudedShape(renderer, e); } } protected abstract void OnPaintShape(siGdiSliceRenderer renderer, PaintEventArgs e); protected abstract void OnPaintExtrudedShape(siGdiSliceRenderer renderer, PaintEventArgs e); /// /// Computes if the selection should draw the shape or the extruded /// rectangle. /// /// /// /// True if drawing the shape (eg. circle), false otherwise. protected bool IsRenderingShape(siRenderer renderer, itk.itkMatrix direction) { // Construct a unit point itk.itkPoint point = new itk.itkPoint(direction.NumberOfCols); point.Fill(0.0); point[0] = 1.0; point[1] = 1.0; // Pass the point through the direction transform point = renderer.DirectionTransformPoint(point); // Compute if we are rendering the shape or the extrusion return (point[this.ExtrusionDimension] == 0.0 || direction.NumberOfCols == 2); } //===================================================================== #endregion } }