Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Shapes/ILBorderedShape.cs @ 9102

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

#1967: ILNumerics source for experimentation

File size: 8.3 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.Drawing;
42using System.Collections.Generic;
43using System.Text;
44using ILNumerics.Drawing.Graphs;
45using ILNumerics.Exceptions;
46using ILNumerics.Drawing.Interfaces; 
47using ILNumerics.Drawing;
48namespace ILNumerics.Drawing.Shapes {
49    /// <summary>
50    /// Base class for all simple shapes, rendering a primitive
51    /// with a border, which is independantly configurable.
52    /// </summary>
53    /// <typeparam name="VertexType">inner vertex element type (struct)</typeparam>
54    public abstract class ILBorderedShape<VertexType> : ILShape<VertexType>
55        where VertexType : struct, IILVertexDefinition {
56
57        #region eventing
58        #endregion
59
60        #region attributes
61        protected ILLineProperties m_border;
62        #endregion
63
64        #region properties
65        /// <summary>
66        /// get reference to the properties of the shapes border
67        /// </summary>
68        public ILLineProperties Border {
69            get {
70                return m_border;
71            }
72        }
73        #endregion
74
75        /// <summary>
76        /// create new bordered shape
77        /// </summary>
78        /// <param name="panel">panel hosting the scene</param>
79        /// <param name="numVertices">number of vertices, this bordered shape will be made out of</param>
80        public ILBorderedShape (ILPanel panel, int numVertices)
81            : base(panel,numVertices,numVertices) {
82            m_border = new ILLineProperties();
83            m_border.Changed += new EventHandler(m_border_Changed);
84            m_fillColor = Color.Yellow;
85            m_border.Width = 1;
86            m_border.Antialiasing = false;
87            m_border.Color = Color.Bisque;
88            m_shading = ShadingStyles.Flat;
89            m_vertexStoresColor = false;
90        }
91
92        /// <summary>
93        /// update vertices of the shape
94        /// </summary>
95        /// <param name="X">X positions vector</param>
96        /// <param name="Y">Y positions vector</param>
97        /// <param name="Z">Z positions vector</param>
98        /// <remarks><para> The positions of the vertices are altered only. Other attributes are not altered. X, Y and Z must be
99        /// vectors, scalars or <c>null</c>.</para>
100        /// <para>If any of <paramref name="X"/>,<paramref name="Y"/> or <paramref name="Z"/> is a vector, its elements are transfered to the corresponding position part (X,Y or Z)
101        /// of the corresponding vertices. If any vector is longer than the current <see cref="VertexCount"/>, the number of vertices currently managed by this shape is expanded. If any of
102        /// the vectors is smaller than <see cref="VertexCount"/>, trailing vertices are not touched.</para>
103        /// <para>If any of <paramref name="X"/>,<paramref name="Y"/> or <paramref name="Z"/> is a scalar, its value is applied to <b>all</b> corresponding positioning parts of all vertices.</para>
104        /// <para>If any of <paramref name="X"/>,<paramref name="Y"/> or <paramref name="Z"/> is <c>null</c>, the corresponding part of the vertices are not altered.</para></remarks>
105        public virtual void Update(ILInArray<float> X = null, ILInArray<float> Y = null, ILInArray<float> Z = null) {
106            using (ILScope.Enter(X,Y,Z)) {
107                int minLen = 0;
108                ILArray<float> x = ILMath.check(X, (a) => { if (a.IsVector) { minLen = a.Length; return a; } else { return null; } }, true, "X must be vector, scalar or null");
109                ILArray<float> y = ILMath.check(Y, (a) => { if (a.IsVector) { minLen = Math.Max(minLen, a.Length); return a; } else { return null; } }, true, "Y must be vector, scalar or null");
110                ILArray<float> z = ILMath.check(Z, (a) => { if (a.IsVector) { minLen = Math.Max(minLen, a.Length); return a; } else { return null; } }, true, "Z must be vector, scalar or null");
111               
112                if (VertexCount < minLen) {
113                    m_numVerticesPerShape = minLen;
114                    Resize(minLen);
115                }
116                if (!ILMath.isnull(x)) {
117                    if (x.IsScalar) {
118                        float scalar = x.GetValue(0);
119                        for (int i = 0; i < VertexCount; i++) {
120                            Vertices[i].XPosition = scalar;
121                        }
122                    } else {
123                        for (int i = 0; i < x.Length; i++) {
124                            Vertices[i].XPosition = x.GetValue(i);
125                        }
126                    }
127                }
128                if (!ILMath.isnull(y)) {
129                    if (y.IsScalar) {
130                        float scalar = y.GetValue(0);
131                        for (int i = 0; i < VertexCount; i++) {
132                            Vertices[i].YPosition = scalar;
133                        }
134                    } else {
135                        for (int i = 0; i < y.Length; i++) {
136                            Vertices[i].YPosition = y.GetValue(i);
137                        }
138                    }
139                }
140                if (!ILMath.isnull(z)) {
141                    if (z.IsScalar) {
142                        float scalar = z.GetValue(0);
143                        for (int i = 0; i < VertexCount; i++) {
144                            Vertices[i].ZPosition = scalar;
145                        }
146                    } else {
147                        for (int i = 0; i < z.Length; i++) {
148                            Vertices[i].ZPosition = z.GetValue(i);
149                        }
150                    }
151                }
152                Invalidate();
153            }
154        }
155
156
157        void m_border_Changed(object sender, EventArgs e) {
158            OnChanged();
159        }
160        protected override void IntDrawShape(ILRenderProperties props) {
161            if (m_vertCount == 0 || m_vertCount >= VerticesPerShape) {
162                m_renderer.Draw(props,this);   
163            }
164        }
165        protected override void IntDrawLabel(ILRenderProperties props) {
166            if (m_vertCount == 0 || m_vertCount >= VerticesPerShape) {
167                if (!String.IsNullOrEmpty(m_label.Text) && m_vertCount > 1) {
168                    ILPoint3Df cent = m_vertices[0].Position + m_vertices[1].Position;
169                    m_label.Draw(props, cent / 2);
170                }
171            }
172        }
173    }
174}
Note: See TracBrowser for help on using the repository browser.