Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Shapes/ILLitCompositeShape.cs @ 11826

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

#1967: ILNumerics source for experimentation

File size: 10.2 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 ILNumerics.Drawing.Interfaces;
44using ILNumerics.Drawing;
45using ILNumerics.Drawing.Lighting;
46using ILNumerics.Exceptions;
47
48namespace ILNumerics.Drawing.Shapes {
49    public abstract class ILLitCompositeShape<VertexType>
50        : ILCompositeShape<VertexType>, IILSupportsLight
51        where VertexType : struct, IILVertexDefinition {
52
53        #region attributes
54        bool m_autoNormals;
55        Dictionary<int, List<int>> m_shapeIndicesIndex; // used for speedy normal calculation
56        ILMaterial m_material;
57        #endregion
58
59        #region properties
60        public ILMaterial Material {
61            get { return m_material; }
62            set { m_material = value; }
63        }
64        public bool AutoNormals {
65            get { return m_autoNormals; }
66            set {
67                if (VerticesPerShape < 3 && value)
68                    throw new InvalidOperationException(
69                            "'auto normals' needs at least 3 vertices per primitive!");
70                m_autoNormals = value;
71                if (value) Invalidate();
72            }
73        }
74        #endregion
75
76        #region constructors
77        /// <summary>
78        /// create light composite shape
79        /// </summary>
80        /// <param name="panel">scene hosting the scene</param>
81        /// <param name="numVertices">number of overall vertices for the shape</param>
82        /// <param name="verticesPerShape">Number of vertices per shape</param>
83        public ILLitCompositeShape (ILPanel panel, int numVertices, int verticesPerShape)
84            : base(panel,numVertices, verticesPerShape) {
85            create();
86        }
87
88        private void create() {
89            if (VerticesPerShape > 2)             
90                m_autoNormals = true; 
91            else
92                m_autoNormals = false;
93            m_oldCameraPosition = ILPoint3Df.Empty;
94            m_material = new ILMaterial();
95        }
96        //public ILLitCompositeShape (ILPanel panel, int verticesPerShape, ILBaseArray X, ILBaseArray Y, ILBaseArray Z, ILBaseArray mapping)
97        //    : base (panel, verticesPerShape, X, Y, Z, mapping) {
98        //    create();
99        //}
100        //public ILLitCompositeShape (ILPanel panel, int verticesPerShape, ILBaseArray X, ILBaseArray Y, ILBaseArray Z)
101        //    : base (panel, verticesPerShape, X, Y, Z) {
102        //    create();
103        //}
104        //public ILLitCompositeShape (ILPanel panel, int verticesPerShape, ILBaseArray X, ILBaseArray Y, ILBaseArray Z, ILBaseArray colors, ILBaseArray mapping)
105        //    : base (panel, verticesPerShape, X, Y, Z, colors, mapping) {
106        //    create();
107        //}
108        #endregion
109
110        #region public interface
111        public override void Configure() {
112            if (m_shapeIndicesIndex == null) { // shape was invalidated
113                m_shapeIndicesIndex = Computation.CreateShapeIndicesIndex(m_shapeIndices);
114                if (m_autoNormals) {
115                    Computation.CalculateNormals(m_vertices, m_shapeIndices, m_shapeIndicesIndex);
116                }
117            }
118            base.Configure();
119        }
120        public override void Invalidate() {
121            base.Invalidate();
122            m_shapeIndicesIndex = null;
123        }
124        #endregion
125
126        #region private helpers
127        #endregion
128
129        protected class Computation : ILMath {
130
131            /// <summary>
132            /// Creates the index of the shape indices.
133            /// </summary>
134            /// <param name="shapeIndices">The shape indices.</param>
135            /// <returns>index of shape indices</returns>
136            /// <remarks>The index of shape indices is used for fast facette lookup while (auto) creating
137            /// the normal vectors for the vertices. Therefore, the index of every vertex used in the shape
138            /// serves as index for a list of those facettes, where that vertex occures.
139            /// <para>TODO: may be replaced by a custom data structure in order to decrease memory requirements?</para></remarks>
140            public static Dictionary<int, List<int>> CreateShapeIndicesIndex(ILInArray<int> shapeIndices) {
141                using (ILScope.Enter(shapeIndices)) {
142                    Dictionary<int, List<int>> ret = new Dictionary<int, List<int>>();
143                    int shapeCount = shapeIndices.Size[1];
144                    int vertPerShape = shapeIndices.Size[0];
145                    int curShapeIdx = 0;
146                    int curRowIdx = 0;
147                    foreach (int i in shapeIndices) {
148                        if (!ret.ContainsKey(i))
149                            ret.Add(i, new List<int>());
150                        ret[i].Add(curShapeIdx);
151                        curRowIdx++;
152                        if (curRowIdx >= vertPerShape) {
153                            curRowIdx = 0;
154                            curShapeIdx++;
155                        }
156                    }
157                    return ret;
158                }
159            }
160
161            /// <summary>
162            /// Calculates the normals.
163            /// </summary>
164            /// <param name="vertices">vertex array of the shape</param>
165            /// <param name="shapeIndices">The shape mapping indices.</param>
166            /// <param name="shapeIndicesIndex">Index of the shape indices.</param>
167            public static void CalculateNormals(
168                        VertexType[] vertices
169                        , ILInArray<int> shapeIndices
170                        , Dictionary<int, List<int>> shapeIndicesIndex) {
171                using (ILScope.Enter(shapeIndices)) {
172#if MEASURE_NORMAL_CALCULATION
173                DateTime start = DateTime.Now;
174#endif
175                    System.Diagnostics.Debug.Assert(vertices != null && vertices.Length > 0 && vertices[0].StoresNormals);
176                    // first calculate the normal for all vertices
177                    ILPoint3Df[] snormals = new ILPoint3Df[shapeIndices.Size[1]];
178                    for (int shapeCol = 0; shapeCol < snormals.Length; shapeCol++) {
179                        // crossproduct of vertex: (#1 - #0) x (#2 - #0)
180                        int vi0 = shapeIndices.GetValue(0, shapeCol);
181                        int vi1 = shapeIndices.GetValue(1, shapeCol);
182                        int vi2 = shapeIndices.GetValue(2, shapeCol);
183                        ILPoint3Df cross = ILPoint3Df.crossN(
184                            vertices[vi1].Position - vertices[vi0].Position,
185                            vertices[vi1].Position - vertices[vi2].Position);
186                        snormals[shapeCol] = cross;
187                    }
188#if MEASURE_NORMAL_CALCULATION
189                TimeSpan crossDone = DateTime.Now - start;
190                DateTime startSorting = DateTime.Now;
191                System.Diagnostics.Debug.WriteLine("Normals Calculation: cross products in " + crossDone.TotalMilliseconds.ToString() + "ms");
192#endif
193                    // find all facettes using this vertex
194                    for (int i = 0; i < vertices.Length; i++) {
195                        if (!shapeIndicesIndex.ContainsKey(i)) continue;
196                        ILPoint3Df normal = new ILPoint3Df();
197                        foreach (int shapeIdx in shapeIndicesIndex[i]) {
198                            normal += snormals[shapeIdx];
199                        }
200                        // or should we let OpenGL normalize the normals...?
201                        vertices[i].Normal = ILPoint3Df.normalize(normal);
202
203                        //System.Diagnostics.Debug.Assert(
204                        //    Math.Abs(Math.Sqrt(
205                        //    vertices[0].Normal.X * vertices[0].Normal.X +
206                        //    vertices[0].Normal.Y * vertices[0].Normal.Y +
207                        //    vertices[0].Normal.Z * vertices[0].Normal.Z) - 1.0) < (double)MachineParameterFloat.eps);
208                    }
209#if MEASURE_NORMAL_CALCULATION
210                TimeSpan sortDone = DateTime.Now - startSorting;
211                System.Diagnostics.Debug.WriteLine("Normals Calculation: sorting done in " + sortDone.TotalMilliseconds.ToString() + "ms");
212#endif
213
214                }
215            }
216        }
217
218    }
219}
Note: See TracBrowser for help on using the repository browser.