/// /// This file is part of ILNumerics Community Edition. /// /// ILNumerics Community Edition - high performance computing for applications. /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net /// /// ILNumerics Community Edition is free software: you can redistribute it and/or modify /// it under the terms of the GNU General Public License version 3 as published by /// the Free Software Foundation. /// /// ILNumerics Community Edition 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 ILNumerics Community Edition. See the file License.txt in the root /// of your distribution package. If not, see . /// /// In addition this software uses the following components and/or licenses: /// /// ================================================================================= /// The Open Toolkit Library License /// /// Copyright (c) 2006 - 2009 the Open Toolkit library. /// /// Permission is hereby granted, free of charge, to any person obtaining a copy /// of this software and associated documentation files (the "Software"), to deal /// in the Software without restriction, including without limitation the rights to /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of /// the Software, and to permit persons to whom the Software is furnished to do /// so, subject to the following conditions: /// /// The above copyright notice and this permission notice shall be included in all /// copies or substantial portions of the Software. /// /// ================================================================================= /// using System; using System.Collections.Generic; using System.Text; using System.Drawing; using ILNumerics.Drawing; using ILNumerics.Drawing.Graphs; using ILNumerics.Drawing.Labeling; using ILNumerics.Drawing.Interfaces; namespace ILNumerics.Drawing.Shapes { #region ILShape class definition /// /// abstract generic shape, specific vertex type /// /// the internal vertex type, IILVertexDefinition public abstract class ILShape : ILShape where VertexType : struct, IILVertexDefinition { #region attributes protected VertexType[] m_vertices; protected VertexType m_sampleVertex = new VertexType(); protected int m_numVerticesPerShape; #endregion #region properties /// /// access to internal vertex array /// public virtual VertexType[] Vertices { get { return m_vertices; } } /// /// number of vertices per shape /// public int VerticesPerShape { get { return m_numVerticesPerShape; } } /// /// get or manually define the center of the shape /// /// Once an individual (custom) center was assigned to the CustomCenter /// property, that point is returned and used on subsequent queries for Center. /// In order to clear the custom center, set the CustomCenter property to ILPoint3Df.Emtpy. /// The center is important to position the shape in the scene - according to /// other shapes. This defines the order in which the shapes are rendered, which /// is neccessary for transparency to appear correctly. In general one should /// not alter the center. This will compute the center of a shape accordingly to its /// real vertices weight average. However, for certain special shapes it might /// be helpful to override the center to manually determine the rendering order in the scene. public ILPoint3Df CustomCenter { get { return m_customCenter; } set { if (value.IsEmtpy()) { m_useCustomCenter = false; } else { m_useCustomCenter = true; m_customCenter = value; } OnChanged(); } } /// /// internal vertex definition (readonly) /// public override IILVertexDefinition VertexDefinition { get { return (IILVertexDefinition)m_sampleVertex; } } #endregion #region constructors public ILShape (ILPanel panel, int numberVertices, int verticesPerShape) : base (panel) { m_numVerticesPerShape = verticesPerShape; m_renderer = panel.GetCreationFactory().CreateVertexRenderer(typeof(VertexType), this); Resize(numberVertices); VertexType a = new VertexType(); m_vertexStoresColor = a.StoresColor; } #endregion #region public interface /// /// Query single vertex via IILVertexDefinition interface /// /// index of vertex in vertex array /// vertex definition public override IILVertexDefinition GetVertex(int i) { return (IILVertexDefinition)m_vertices[i]; } /// /// alter single vertex via IILVertexDefinition interface /// /// index of vertex in vertex array /// new vertex definition public override void SetVertex(int vertexIdx,IILVertexDefinition vertex) { VertexType curVert = m_vertices[vertexIdx]; if (VertexDefinition.StoresColor && vertex.StoresColor) { curVert.Color = vertex.Color; } if (VertexDefinition.StoresNormals && vertex.StoresNormals) { curVert.Normal = vertex.Normal; } curVert.Position = vertex.Position; m_vertices[vertexIdx] = curVert; } /// /// set color for single vertex (color only, no alpha!) /// /// index of vertex in vertex array /// new color public override void SetColor(int vertexID,Color color) { if (VertexDefinition.StoresColor) { VertexType vert = m_vertices[vertexID]; vert.Color = color; m_vertices[vertexID] = vert; } } /// /// set position of single vertex /// /// index of vertex in vertex array /// new position public override void SetPosition(int vertexID,ILPoint3Df position) { VertexType vert = m_vertices[vertexID]; vert.Position = position; m_vertices[vertexID] = vert; } /// /// set normal vector of single vertex /// /// index of vertex in vertex array /// new normal vector public override void SetNormal(int vertexID,ILPoint3Df normal) { if (VertexDefinition.StoresNormals) { VertexType vert = m_vertices[vertexID]; vert.Normal = normal; m_vertices[vertexID] = vert; } } /// /// translate all vertices of the shape /// /// offset, all vertices will be moved by that amount public override void Translate(ILPoint3Df offset) { for (int i = 0; i < m_vertCount; i++) { VertexType tmp = m_vertices[i]; tmp.Position += offset; m_vertices[i] = tmp; } } #endregion #region private helper protected void Resize(int numberVertices) { m_vertCount = numberVertices; m_vertices = new VertexType[numberVertices]; } protected override void ComputeLimits() { ILPoint3Df cent = new ILPoint3Df(); if (m_vertices.Length == 0) { // fast exit m_positionCenter = cent; m_positionMin = cent; m_positionMax = cent; return; } ILPoint3Df max = ILPoint3Df.MinValue, min = ILPoint3Df.MaxValue, cur; foreach (IILVertexDefinition vertex in m_vertices) { cur = vertex.Position; cent = cent + cur; max = ILPoint3Df.Max(max, cur); min = ILPoint3Df.Min(min, cur); } m_positionCenter = cent / m_vertices.Length; m_positionMax = max; m_positionMin = min; } #endregion } #endregion #region ILShape base class definition /// /// abstract base class for all shapes /// public abstract class ILShape : IDisposable { #region events /// /// fires, when any properties of the shape have changed /// public event EventHandler Changed; protected virtual void OnChanged() { if (Changed != null) { Changed(this, new EventArgs()); } } /// /// fires when the size of the shape has changed /// public event EventHandler SizeChanged; protected virtual void OnSizeChanged() { if (SizeChanged != null) { SizeChanged(this, new EventArgs()); } } #endregion #region attributes protected ILPanel m_panel; protected bool m_useCustomCenter; protected ILPoint3Df m_customCenter; protected ILPoint3Df m_positionMin; protected ILPoint3Df m_positionMax; protected ILPoint3Df m_positionCenter; protected Color m_fillColor; protected ShadingStyles m_shading; protected ILShapeLabel m_label; protected bool m_vertexStoresColor; private bool m_invalidated; protected int m_vertCount; protected ILVertexRenderer m_renderer; protected ILSceneGraphShapedLeaf m_sceneNode; protected bool m_visible; #endregion #region properties /// /// Gets visibility of the shape or sets it /// public bool Visible { get { return m_visible; } set { m_visible = value; } } /// /// returns the scene graph node holding this shape /// public ILSceneGraphShapedLeaf SceneGraphNode { get { return m_sceneNode; } set { m_sceneNode = value; } } /// /// Get minimum coordinate of the cube enclosing the shape /// public ILPoint3Df PositionMin { get { if (m_positionMin.IsEmtpy()) ComputeLimits(); return m_positionMin; } } /// /// Get maximum coordinate of the cube enclosing the shape /// public ILPoint3Df PositionMax { get { if (m_positionMax.IsEmtpy()) ComputeLimits(); return m_positionMax; } } /// /// Get weight center of vertices /// public ILPoint3Df Center { get { if (m_useCustomCenter && !m_customCenter.IsEmtpy()) return m_customCenter; if (m_positionCenter.IsEmtpy()) ComputeLimits(); return m_positionCenter; } } /// /// Get/set method of area filling /// /// Two methods are available: 'Interpolate' and 'Flat'. For 'Interpolate' mode, /// the color and alpha values stored in individual vertices are used for rendering. /// In 'Flat' mode, only the single properties 'FillColor' and 'Opacity' determine /// the color and transparency of the whole shape. Even if vertices store individual /// color values, those are ignored in that case. public ShadingStyles Shading { get { return m_shading; } set { if (m_shading == ShadingStyles.Interpolate && !VertexDefinition.StoresColor) { throw new NotSupportedException("The underlying vertex type does not support individual colors! Use shading mode 'Flat' instead!"); } m_shading = value; OnChanged(); } } /// /// fill color for shape areas in flat shading mode /// /// Setting this value will not alter the alpha value /// (opacity). The alpha of the new value will be ignored. public virtual Color FillColor { get { return m_fillColor; } set { m_fillColor = Color.FromArgb(m_fillColor.A, value); m_shading = ShadingStyles.Flat; OnChanged(); } } /// /// definition of internal vertex type (readonly) /// public abstract IILVertexDefinition VertexDefinition { get; } /// /// opacity for areas of the shape in flat shading mode /// public byte Opacity { get { return m_fillColor.A; } set { m_fillColor = Color.FromArgb(value, m_fillColor); } } /// /// Shape label /// public ILShapeLabel Label { get { return m_label; } } /// /// number of vertices a shape (or all components of a shape) of this type is made out of /// /// For triangle shapes and related (lit triangles, triangles etc.) this is 3, for quads it is 4 and so on... public int VertexCount { get { return m_vertCount; } } #endregion #region constructors /// /// create new shape (protected) /// /// panel hosting the scene protected ILShape (ILPanel panel) { m_panel = panel; m_fillColor = Color.White; m_label = new ILShapeLabel(panel); m_label.Changed += new EventHandler(m_label_Changed); m_label.Text = ""; //GetType().Name + " " + GetHashCode(); m_shading = ShadingStyles.Flat; m_visible = true; Invalidate(); } #endregion #region public interface /// /// may be overwritten in derived classes to clean up ressources /// public virtual void Dispose() {} /// /// Query single vertex via IILVertexDefinition interface /// /// index of vertex in vertex array /// vertex definition public abstract IILVertexDefinition GetVertex(int id); /// /// set color for single vertex (color only, no alpha!) /// /// index of vertex in vertex array /// new color public abstract void SetColor(int id, Color color); /// /// set position if single vertex /// /// index of vertex in vertex array /// new position public abstract void SetPosition(int id, ILPoint3Df position); /// /// set normal vector for single vertex /// /// index of vertex in vertex array /// new normal vector public abstract void SetNormal(int id, ILPoint3Df normal); /// /// alter single vertex via IILVertexDefinition interface /// /// index of vertex in vertex array /// new vertex definition public abstract void SetVertex(int vertexID, IILVertexDefinition vertex); /// /// translate all vertices of the shape /// /// offset, all vertices will be moved by that amount public abstract void Translate(ILPoint3Df offset); /// /// draw this shape (internal use) /// /// public void Draw (ILRenderProperties props) { if (m_visible) { IntDrawShape(props); IntDrawLabel(props); } } /// /// configure this shape (internal use) /// /// Configure is called once for each rendering frame. If the shape /// is invalidated, this causes IntConfigure to be called on the shape. public virtual void Configure() { if (m_invalidated) { m_invalidated = false; } } /// /// Invalidates this shape, needed after altering any vertex data /// public virtual void Invalidate() { m_invalidated = true; m_positionMin = ILPoint3Df.Empty; m_positionMax = ILPoint3Df.Empty; m_positionCenter = ILPoint3Df.Empty; if (m_sceneNode != null) { m_sceneNode.Invalidate(false); } } protected virtual void IntDrawLabel(ILRenderProperties p) { if (!String.IsNullOrEmpty(m_label.Text)) m_label.Draw(p, Center); } protected virtual void IntDrawShape(ILRenderProperties p) { m_renderer.Draw(p,this); } protected abstract void ComputeLimits(); #endregion #region private helpers void m_label_Changed(object sender, EventArgs e) { OnChanged(); } #endregion } #endregion }