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 |
|
---|
40 | using System;
|
---|
41 | using System.Collections.Generic;
|
---|
42 | using System.Text;
|
---|
43 | using ILNumerics.Drawing;
|
---|
44 | using ILNumerics.Drawing.Shapes;
|
---|
45 |
|
---|
46 | namespace ILNumerics.Drawing.Graphs {
|
---|
47 | /// <summary>
|
---|
48 | /// scene graph inner node, collection of children
|
---|
49 | /// </summary>
|
---|
50 | public class ILSceneGraphInnerNode : ILSceneGraphNode, ICollection<ILSceneGraphNode>,
|
---|
51 | IList<ILSceneGraphNode> {
|
---|
52 |
|
---|
53 | #region attributes
|
---|
54 | private List<ILSceneGraphNode> m_childs;
|
---|
55 | private ILArray<float> m_centers;
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region constructors
|
---|
59 | public ILSceneGraphInnerNode(ILPanel panel)
|
---|
60 | :base (panel) {
|
---|
61 |
|
---|
62 | m_centers = ILMath.returnType<float>();
|
---|
63 | m_childs = new List<ILSceneGraphNode>();
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | #region public interface
|
---|
70 | /// <summary>
|
---|
71 | /// recompute the size spanned by this node, may fires Changed() event
|
---|
72 | /// </summary>
|
---|
73 | internal override void Configure() {
|
---|
74 | if (m_invalidated) {
|
---|
75 | // configure childs (compute limits) first
|
---|
76 | foreach (ILSceneGraphNode child in m_childs) {
|
---|
77 | child.Configure();
|
---|
78 | }
|
---|
79 | bool sizechanged = false;
|
---|
80 | ILPoint3Df oldMin = m_positionMin;
|
---|
81 | ILPoint3Df oldMax = m_positionMax;
|
---|
82 | ComputeNodeLimits();
|
---|
83 | if (oldMin != m_positionMin) sizechanged = true;
|
---|
84 | if (oldMax != m_positionMax) sizechanged = true;
|
---|
85 | m_invalidated = false;
|
---|
86 | if (sizechanged)
|
---|
87 | OnSizeChanged();
|
---|
88 | }
|
---|
89 | }
|
---|
90 | internal override void Draw(ILRenderProperties props) {
|
---|
91 | if (!m_visible) {
|
---|
92 | return;
|
---|
93 | }
|
---|
94 | if (m_childs != null && m_childs.Count > 0) {
|
---|
95 | ILArray<int> indices = Computation.GetSortedIndices(
|
---|
96 | m_centers, m_panel.Camera.Position);
|
---|
97 | foreach (int i in indices) {
|
---|
98 | if (m_childs[i].Visible)
|
---|
99 | m_childs[i].Draw(props);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | public override void Invalidate() {
|
---|
104 | base.Invalidate();
|
---|
105 | m_centers.a = ILMath.empty<float>(ILSize.Empty00);
|
---|
106 | }
|
---|
107 | /// <summary>
|
---|
108 | /// compute limits of the cube tightly enclosing the branch below this node
|
---|
109 | /// </summary>
|
---|
110 | /// <returns></returns>
|
---|
111 | protected override void ComputeNodeLimits() {
|
---|
112 | if (m_childs == null || m_childs.Count == 0) {
|
---|
113 | m_positionMin = ILPoint3Df.Empty;
|
---|
114 | m_positionMax = ILPoint3Df.Empty;
|
---|
115 | return;
|
---|
116 | }
|
---|
117 | // acquire maximum position
|
---|
118 | ILPoint3Df curMax = ILPoint3Df.MinValue;
|
---|
119 | ILPoint3Df curMin = ILPoint3Df.MaxValue;
|
---|
120 | foreach (ILSceneGraphNode node in m_childs) {
|
---|
121 | curMin = ILPoint3Df.Min(node.PositionMin, curMin);
|
---|
122 | }
|
---|
123 | curMax = ILPoint3Df.MinValue;
|
---|
124 | foreach (ILSceneGraphNode node in m_childs) {
|
---|
125 | curMax = ILPoint3Df.Max(node.PositionMax, curMax);
|
---|
126 | }
|
---|
127 | m_positionMax = curMax;
|
---|
128 | m_positionMin = curMin;
|
---|
129 | m_center = (m_positionMax + m_positionMin) / 2;
|
---|
130 | // stores childs centers for sorting
|
---|
131 | if (m_centers.IsEmpty)
|
---|
132 | m_centers.a = ILMath.zeros<float>(m_childs.Count, 3);
|
---|
133 | for (int i = m_childs.Count; i-- > 0; ) {
|
---|
134 | ILPoint3Df center = m_childs[i].Center;
|
---|
135 | m_centers.SetValue(center.X, i, 0);
|
---|
136 | m_centers.SetValue(center.Y, i, 1);
|
---|
137 | m_centers.SetValue(center.Z, i, 2);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | internal override void CollectAllChildren(List<ILSceneGraphNode> nodes) {
|
---|
141 | nodes.Add(this); // base.CollectAllChildren(nodes); // add self
|
---|
142 | if (m_childs != null) {
|
---|
143 | foreach (ILSceneGraphNode child in m_childs) {
|
---|
144 | child.CollectAllChildren(nodes);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | public virtual void Add(ILShape shape) {
|
---|
149 | ILSceneGraphShapedLeaf node = new ILSceneGraphShapedLeaf(m_panel);
|
---|
150 | node.Parent = this;
|
---|
151 | node.Shape = shape;
|
---|
152 | m_childs.Add(node);
|
---|
153 | Invalidate();
|
---|
154 | OnNodeAdded(node);
|
---|
155 | }
|
---|
156 | public virtual void Remove(ILShape shape) {
|
---|
157 | ILSceneGraphNode node = null;
|
---|
158 | foreach (ILSceneGraphNode n in m_childs) {
|
---|
159 | if (n is ILSceneGraphShapedLeaf) {
|
---|
160 | if (object.Equals((n as ILSceneGraphShapedLeaf).Shape,shape)) {
|
---|
161 | node = n;
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | if (node != null) {
|
---|
167 | m_childs.Remove(node);
|
---|
168 | OnNodeRemoved(node);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | #endregion
|
---|
172 |
|
---|
173 | #region IList<ILSceneGraphNode> Member
|
---|
174 |
|
---|
175 | public virtual ILSceneGraphNode this[int i] {
|
---|
176 | get {
|
---|
177 | return m_childs[i];
|
---|
178 | }
|
---|
179 | set {
|
---|
180 | m_childs[i] = value;
|
---|
181 | Invalidate();
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | public virtual int IndexOf(ILSceneGraphNode item) {
|
---|
186 | return m_childs.IndexOf(item);
|
---|
187 | }
|
---|
188 |
|
---|
189 | public virtual void Insert(int index, ILSceneGraphNode item) {
|
---|
190 | m_childs.Insert(index, item);
|
---|
191 | Invalidate();
|
---|
192 | }
|
---|
193 |
|
---|
194 | public virtual void RemoveAt(int index) {
|
---|
195 | m_childs.RemoveAt(index);
|
---|
196 | Invalidate();
|
---|
197 | }
|
---|
198 | #endregion
|
---|
199 |
|
---|
200 | #region ICollection<ILSceneGraphNode> Member
|
---|
201 | /// <summary>
|
---|
202 | /// add a single node to the end of child collection
|
---|
203 | /// </summary>
|
---|
204 | /// <param name="item">node to add </param>
|
---|
205 | public virtual void Add(ILSceneGraphNode item) {
|
---|
206 | m_childs.Add(item);
|
---|
207 | item.Parent = this;
|
---|
208 | item.Invalidate();
|
---|
209 | OnNodeAdded(item);
|
---|
210 | }
|
---|
211 | /// <summary>
|
---|
212 | /// wipe all nodes from the collection
|
---|
213 | /// </summary>
|
---|
214 | public virtual void Clear() {
|
---|
215 | m_childs.Clear();
|
---|
216 | Invalidate();
|
---|
217 | }
|
---|
218 | /// <summary>
|
---|
219 | /// Determine, if this collection contains a specific node item
|
---|
220 | /// </summary>
|
---|
221 | /// <param name="item"></param>
|
---|
222 | /// <returns></returns>
|
---|
223 | public bool Contains(ILSceneGraphNode item) {
|
---|
224 | return m_childs.Contains(item);
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void CopyTo(ILSceneGraphNode[] array, int arrayIndex) {
|
---|
228 | m_childs.CopyTo(array, arrayIndex);
|
---|
229 | }
|
---|
230 | /// <summary>
|
---|
231 | /// Number of children contained in this node
|
---|
232 | /// </summary>
|
---|
233 | public virtual int Count {
|
---|
234 | get { return m_childs.Count; }
|
---|
235 | }
|
---|
236 | /// <summary>
|
---|
237 | /// determine if this collection is readonly, (returns 'false')
|
---|
238 | /// </summary>
|
---|
239 | public bool IsReadOnly {
|
---|
240 | get { return false; }
|
---|
241 | }
|
---|
242 | /// <summary>
|
---|
243 | /// remove a single child node from the collection
|
---|
244 | /// </summary>
|
---|
245 | /// <param name="item">node to be removed</param>
|
---|
246 | /// <returns>true</returns>
|
---|
247 | public virtual bool Remove(ILSceneGraphNode item) {
|
---|
248 | m_childs.Remove(item);
|
---|
249 | Invalidate();
|
---|
250 | return true;
|
---|
251 | }
|
---|
252 |
|
---|
253 | #endregion
|
---|
254 |
|
---|
255 | #region private helpers
|
---|
256 |
|
---|
257 | private class Computation : ILNumerics.ILMath {
|
---|
258 | /// <summary>
|
---|
259 | /// compute distance to camera and return sorted indices for rendering
|
---|
260 | /// </summary>
|
---|
261 | /// <param name="centers">current primitive centers</param>
|
---|
262 | /// <param name="position">current camera position</param>
|
---|
263 | /// <returns>sorted indices of primitives in descending order</returns>
|
---|
264 | internal static ILRetArray<int> GetSortedIndices(ILInArray<float> centers, ILPoint3Df position) {
|
---|
265 | using (ILScope.Enter(centers)) {
|
---|
266 | ILArray<float> pos = array<float>(size(1,3), -position.X, -position.Y, -position.Z);
|
---|
267 | // move camera outside of centers
|
---|
268 | pos.a *= maxall(abs(centers));
|
---|
269 | //pos.a = repmat(pos, centers.Dimensions[0], 1);
|
---|
270 | // compute distances
|
---|
271 | ILArray<float> dist = sum(pow(centers - pos, 2), 1);
|
---|
272 | ILArray<double> ret = empty();
|
---|
273 | sort(dist, ret, 0, false);
|
---|
274 | return toint32(ret);
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | #endregion
|
---|
280 |
|
---|
281 | }
|
---|
282 | }
|
---|