Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Shapes/ILShape.cs @ 9426

Last change on this file since 9426 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 19.4 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43using System.Drawing;
44using ILNumerics.Drawing;
45using ILNumerics.Drawing.Graphs;
46using ILNumerics.Drawing.Labeling;
47using ILNumerics.Drawing.Interfaces; 
48
49namespace ILNumerics.Drawing.Shapes {
50    #region ILShape<VertexType> class definition
51    /// <summary>
52    /// abstract generic shape, specific vertex type
53    /// </summary>
54    /// <typeparam name="VertexType">the internal vertex type, IILVertexDefinition</typeparam>
55    public abstract class ILShape<VertexType> : ILShape
56        where VertexType : struct, IILVertexDefinition {
57
58        #region attributes
59        protected VertexType[] m_vertices;
60        protected VertexType m_sampleVertex = new VertexType();
61        protected int m_numVerticesPerShape;
62        #endregion
63
64        #region properties
65
66        /// <summary>
67        /// access to internal vertex array
68        /// </summary>
69        public virtual VertexType[] Vertices {
70            get {
71                return m_vertices;
72            }
73        }
74        /// <summary>
75        /// number of vertices per shape
76        /// </summary>
77        public int VerticesPerShape {
78            get { return m_numVerticesPerShape; }
79        }
80        /// <summary>
81        /// get or manually define the center of the shape
82        /// </summary>
83        /// <remarks>Once an individual (custom) center was assigned to the CustomCenter
84        /// property, that point is returned and used on subsequent queries for Center.
85        /// <para>In order to clear the custom center, set the CustomCenter property to ILPoint3Df.Emtpy.</para>
86        /// <para>The center is important to position the shape in the scene - according to
87        /// other shapes. This defines the order in which the shapes are rendered, which
88        /// is neccessary for transparency to appear correctly. In general one should
89        /// not alter the center. This will compute the center of a shape accordingly to its
90        /// real vertices weight average. However, for certain special shapes it might
91        /// be helpful to override the center to manually determine the rendering order in the scene.</para></remarks>
92        public ILPoint3Df CustomCenter {
93            get {
94                return m_customCenter;
95            }
96            set {
97                if (value.IsEmtpy()) {
98                    m_useCustomCenter = false;
99                } else {
100                    m_useCustomCenter = true;
101                    m_customCenter = value;
102                }
103                OnChanged();
104            }
105        }
106        /// <summary>
107        /// internal vertex definition (readonly)
108        /// </summary>
109        public override IILVertexDefinition VertexDefinition {
110            get {
111                return (IILVertexDefinition)m_sampleVertex;
112            }
113        }
114        #endregion
115
116        #region constructors
117        public ILShape (ILPanel panel, int numberVertices, int verticesPerShape)
118            : base (panel) {
119            m_numVerticesPerShape = verticesPerShape;
120            m_renderer = panel.GetCreationFactory().CreateVertexRenderer(typeof(VertexType), this);
121            Resize(numberVertices);
122            VertexType a = new VertexType();
123            m_vertexStoresColor = a.StoresColor;
124        }
125
126        #endregion
127
128        #region public interface
129        /// <summary>
130        /// Query single vertex via IILVertexDefinition interface
131        /// </summary>
132        /// <param name="i">index of vertex in vertex array</param>
133        /// <returns>vertex definition</returns>
134        public override IILVertexDefinition GetVertex(int i) {
135            return (IILVertexDefinition)m_vertices[i];
136        }
137        /// <summary>
138        /// alter single vertex via IILVertexDefinition interface
139        /// </summary>
140        /// <param name="vertexIdx">index of vertex in vertex array</param>
141        /// <param name="vertex">new vertex definition</param>
142        public override void SetVertex(int vertexIdx,IILVertexDefinition vertex) {
143            VertexType curVert = m_vertices[vertexIdx];
144            if (VertexDefinition.StoresColor && vertex.StoresColor) {
145                curVert.Color = vertex.Color;
146            }
147            if (VertexDefinition.StoresNormals && vertex.StoresNormals) {
148                curVert.Normal = vertex.Normal;
149            }
150            curVert.Position = vertex.Position;
151            m_vertices[vertexIdx] = curVert;
152        }
153        /// <summary>
154        /// set color for single vertex (color only, no alpha!)
155        /// </summary>
156        /// <param name="vertexID">index of vertex in vertex array</param>
157        /// <param name="color">new color</param>
158        public override void SetColor(int vertexID,Color color) {
159            if (VertexDefinition.StoresColor) {
160                VertexType vert = m_vertices[vertexID];
161                vert.Color = color;
162                m_vertices[vertexID] = vert;
163            }
164        }
165        /// <summary>
166        /// set position of single vertex
167        /// </summary>
168        /// <param name="vertexID">index of vertex in vertex array</param>
169        /// <param name="position">new position </param>
170        public override void SetPosition(int vertexID,ILPoint3Df position) {
171            VertexType vert = m_vertices[vertexID];
172            vert.Position = position;
173            m_vertices[vertexID] = vert;
174        }
175        /// <summary>
176        /// set normal vector of single vertex
177        /// </summary>
178        /// <param name="vertexID">index of vertex in vertex array</param>
179        /// <param name="normal">new normal vector</param>
180        public override void SetNormal(int vertexID,ILPoint3Df normal) {
181            if (VertexDefinition.StoresNormals) {
182                VertexType vert = m_vertices[vertexID];
183                vert.Normal = normal;
184                m_vertices[vertexID] = vert;
185            }
186        }
187        /// <summary>
188        /// translate all vertices of the shape
189        /// </summary>
190        /// <param name="offset">offset, all vertices will be moved by that amount</param>
191        public override void Translate(ILPoint3Df offset) {
192            for (int i = 0; i < m_vertCount; i++) {
193                VertexType tmp = m_vertices[i];
194                tmp.Position += offset;
195                m_vertices[i] = tmp;
196      }
197        }
198        #endregion
199
200        #region private helper
201        protected void Resize(int numberVertices) {
202            m_vertCount = numberVertices;
203            m_vertices = new VertexType[numberVertices];
204        }
205        protected override void ComputeLimits() {
206            ILPoint3Df cent = new ILPoint3Df();
207            if (m_vertices.Length == 0) {
208                // fast exit
209                m_positionCenter = cent;
210                m_positionMin = cent;
211                m_positionMax = cent;
212                return;
213            }
214            ILPoint3Df max = ILPoint3Df.MinValue, min = ILPoint3Df.MaxValue, cur;
215            foreach (IILVertexDefinition vertex in m_vertices) {
216                cur = vertex.Position;
217                cent = cent + cur;
218                max = ILPoint3Df.Max(max, cur);
219                min = ILPoint3Df.Min(min, cur);
220            }
221            m_positionCenter = cent / m_vertices.Length;
222            m_positionMax = max;
223            m_positionMin = min;
224        }
225        #endregion
226
227    }
228    #endregion
229
230    #region ILShape base class definition
231    /// <summary>
232    /// abstract base class for all shapes
233    /// </summary>
234    public abstract class ILShape  :  IDisposable {
235
236        #region events
237        /// <summary>
238        /// fires, when any properties of the shape have changed
239        /// </summary>
240        public event EventHandler Changed;
241        protected virtual void OnChanged() {
242            if (Changed != null) {
243                Changed(this, new EventArgs());
244            }
245        }
246        /// <summary>
247        /// fires when the size of the shape has changed
248        /// </summary>
249        public event EventHandler SizeChanged;
250        protected virtual void OnSizeChanged() {
251            if (SizeChanged != null) {
252                SizeChanged(this, new EventArgs());
253            }
254        }
255        #endregion
256
257        #region attributes
258        protected ILPanel m_panel;
259        protected bool m_useCustomCenter;
260        protected ILPoint3Df m_customCenter;
261        protected ILPoint3Df m_positionMin;
262        protected ILPoint3Df m_positionMax;
263        protected ILPoint3Df m_positionCenter;
264        protected Color m_fillColor;
265        protected ShadingStyles m_shading;
266        protected ILShapeLabel m_label;
267        protected bool m_vertexStoresColor;
268        private bool m_invalidated;
269        protected int m_vertCount;
270        protected ILVertexRenderer m_renderer;
271        protected ILSceneGraphShapedLeaf m_sceneNode;
272        protected bool m_visible;
273        #endregion
274
275        #region properties
276        /// <summary>
277        /// Gets visibility of the shape or sets it
278        /// </summary>
279        public bool Visible {
280            get { return m_visible; }
281            set { m_visible = value; }
282        }
283        /// <summary>
284        /// returns the scene graph node holding this shape
285        /// </summary>
286        public ILSceneGraphShapedLeaf SceneGraphNode {
287            get {
288                return m_sceneNode;
289            }
290            set {
291                m_sceneNode = value;
292            }
293        }
294        /// <summary>
295        /// Get minimum coordinate of the cube enclosing the shape
296        /// </summary>
297        public ILPoint3Df PositionMin {
298            get {
299                if (m_positionMin.IsEmtpy())
300                    ComputeLimits();
301                return m_positionMin;
302            }
303        }
304        /// <summary>
305        /// Get maximum coordinate of the cube enclosing the shape
306        /// </summary>
307        public ILPoint3Df PositionMax {
308            get {
309                if (m_positionMax.IsEmtpy())
310                    ComputeLimits();
311                return m_positionMax;
312            }
313        }
314        /// <summary>
315        /// Get weight center of vertices
316        /// </summary>
317        public ILPoint3Df Center {
318            get {
319                if (m_useCustomCenter && !m_customCenter.IsEmtpy())
320                    return m_customCenter;
321                if (m_positionCenter.IsEmtpy())
322                    ComputeLimits();
323                return m_positionCenter;
324            }
325        }
326        /// <summary>
327        /// Get/set method of area filling
328        /// </summary>
329        /// <remarks><para>Two methods are available: 'Interpolate' and 'Flat'. For 'Interpolate' mode,
330        /// the color and alpha values stored in individual vertices are used for rendering.
331        /// In 'Flat' mode, only the single properties 'FillColor' and 'Opacity' determine
332        /// the color and transparency of the whole shape. Even if vertices store individual
333        /// color values, those are ignored in that case.</para></remarks>
334        public ShadingStyles Shading {
335            get {
336                return m_shading;
337            }
338            set {
339                if (m_shading == ShadingStyles.Interpolate && !VertexDefinition.StoresColor) {
340                    throw new NotSupportedException("The underlying vertex type does not support individual colors! Use shading mode 'Flat' instead!");
341                }
342                m_shading = value;
343                OnChanged();
344            }
345        }
346        /// <summary>
347        /// fill color for shape areas in flat shading mode
348        /// </summary>
349        /// <remarks>Setting this value will not alter the alpha value
350        /// (opacity). The alpha of the new value will be ignored.</remarks>
351        public virtual Color FillColor {
352            get {
353                return m_fillColor;
354            }
355            set {
356                m_fillColor = Color.FromArgb(m_fillColor.A, value);
357                m_shading = ShadingStyles.Flat;
358                OnChanged();
359            }
360        }
361        /// <summary>
362        /// definition of internal vertex type (readonly)
363        /// </summary>
364        public abstract IILVertexDefinition VertexDefinition { get; }
365        /// <summary>
366        /// opacity for areas of the shape in flat shading mode
367        /// </summary>
368        public byte Opacity {
369            get { return m_fillColor.A; }
370            set { m_fillColor = Color.FromArgb(value, m_fillColor); }
371        }
372        /// <summary>
373        /// Shape label
374        /// </summary>
375        public ILShapeLabel Label {
376            get {
377                return m_label;
378            }
379        }
380        /// <summary>
381        /// number of vertices a shape (or all components of a shape) of this type is made out of
382        /// </summary>
383        /// <remarks>For triangle shapes and related (lit triangles, triangles etc.) this is 3, for quads it is 4 and so on...</remarks>
384        public int VertexCount {
385            get { return m_vertCount; }
386        }
387        #endregion
388
389        #region constructors
390        /// <summary>
391        /// create new shape (protected)
392        /// </summary>
393        /// <param name="panel">panel hosting the scene</param>
394        protected ILShape (ILPanel panel) {
395            m_panel = panel;
396            m_fillColor = Color.White;
397            m_label = new ILShapeLabel(panel);
398            m_label.Changed += new EventHandler(m_label_Changed);
399            m_label.Text = ""; //GetType().Name + " " + GetHashCode();
400            m_shading = ShadingStyles.Flat;
401            m_visible = true;
402            Invalidate();
403        }
404        #endregion
405
406        #region public interface
407        /// <summary>
408        /// may be overwritten in derived classes to clean up ressources
409        /// </summary>
410        public virtual void Dispose() {}
411        /// <summary>
412        /// Query single vertex via IILVertexDefinition interface
413        /// </summary>
414        /// <param name="id">index of vertex in vertex array</param>
415        /// <returns>vertex definition</returns>
416        public abstract IILVertexDefinition GetVertex(int id);
417        /// <summary>
418        /// set color for single vertex (color only, no alpha!)
419        /// </summary>
420        /// <param name="id">index of vertex in vertex array</param>
421        /// <param name="color">new color</param>
422        public abstract void SetColor(int id, Color color);
423        /// <summary>
424        /// set position if single vertex
425        /// </summary>
426        /// <param name="id">index of vertex in vertex array</param>
427        /// <param name="position">new position </param>
428        public abstract void SetPosition(int id, ILPoint3Df position);
429        /// <summary>
430        /// set normal vector for single vertex
431        /// </summary>
432        /// <param name="id">index of vertex in vertex array</param>
433        /// <param name="normal">new normal vector</param>
434        public abstract void SetNormal(int id, ILPoint3Df normal);
435        /// <summary>
436        /// alter single vertex via IILVertexDefinition interface
437        /// </summary>
438        /// <param name="vertexID">index of vertex in vertex array</param>
439        /// <param name="vertex">new vertex definition</param>
440        public abstract void SetVertex(int vertexID, IILVertexDefinition vertex);
441        /// <summary>
442        /// translate all vertices of the shape
443        /// </summary>
444        /// <param name="offset">offset, all vertices will be moved by that amount</param>
445        public abstract void Translate(ILPoint3Df offset);
446        /// <summary>
447        /// draw this shape (internal use)
448        /// </summary>
449        /// <param name="props"></param>
450        public void Draw (ILRenderProperties props) {
451            if (m_visible) {
452                IntDrawShape(props);
453                IntDrawLabel(props);
454            }
455        }
456        /// <summary>
457        /// configure this shape (internal use)
458        /// </summary>
459        /// <remarks>Configure is called once for each rendering frame. If the shape
460        /// is invalidated, this causes IntConfigure to be called on the shape.</remarks>
461        public virtual void Configure() {
462            if (m_invalidated) {
463                m_invalidated = false;
464            }
465        }
466        /// <summary>
467        /// Invalidates this shape, needed after altering any vertex data
468        /// </summary>
469        public virtual void Invalidate() {
470            m_invalidated = true;
471            m_positionMin = ILPoint3Df.Empty;
472            m_positionMax = ILPoint3Df.Empty;
473            m_positionCenter = ILPoint3Df.Empty;
474            if (m_sceneNode != null) {
475                m_sceneNode.Invalidate(false);
476            }
477        }
478        protected virtual void IntDrawLabel(ILRenderProperties p) {
479            if (!String.IsNullOrEmpty(m_label.Text))
480                m_label.Draw(p, Center);
481        }
482        protected virtual void IntDrawShape(ILRenderProperties p) {
483            m_renderer.Draw(p,this);
484        }
485        protected abstract void ComputeLimits();
486        #endregion
487
488        #region private helpers
489        void m_label_Changed(object sender, EventArgs e) {
490            OnChanged();
491        }
492        #endregion
493    }
494    #endregion
495}
Note: See TracBrowser for help on using the repository browser.