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.Collections;
|
---|
43 | using System.Text;
|
---|
44 | using ILNumerics.Drawing.Collections;
|
---|
45 | using ILNumerics.Drawing.Shapes;
|
---|
46 | using ILNumerics.Drawing;
|
---|
47 |
|
---|
48 | namespace ILNumerics.Drawing.Graphs {
|
---|
49 | /// <summary>
|
---|
50 | /// abstract base class for all scene graph nodes
|
---|
51 | /// </summary>
|
---|
52 | public abstract class ILSceneGraphNode :
|
---|
53 | //ICollection<ILSceneGraphNode>,
|
---|
54 | IEnumerable<ILSceneGraphNode>
|
---|
55 | ,IEnumerable {
|
---|
56 |
|
---|
57 | #region attributes
|
---|
58 | protected ILSceneGraphNode m_parent;
|
---|
59 | protected ILPoint3Df m_center;
|
---|
60 | protected ILPoint3Df m_positionMin;
|
---|
61 | protected ILPoint3Df m_positionMax;
|
---|
62 | protected bool m_invalidated;
|
---|
63 | protected bool m_visible;
|
---|
64 | protected ILPanel m_panel; // needed for camera position
|
---|
65 | protected bool m_eventingSuspended;
|
---|
66 | #endregion
|
---|
67 |
|
---|
68 | #region constructors
|
---|
69 | internal ILSceneGraphNode(ILPanel panel) {
|
---|
70 | m_panel = panel;
|
---|
71 | m_invalidated = true;
|
---|
72 | m_visible = true;
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region eventing
|
---|
77 | /// <summary>
|
---|
78 | /// fires when the size of the cube tigthly enclosing the shape has changed
|
---|
79 | /// </summary>
|
---|
80 | public event EventHandler SizeChanged;
|
---|
81 | protected virtual void OnSizeChanged() {
|
---|
82 | if (SizeChanged != null && !m_eventingSuspended)
|
---|
83 | SizeChanged(this, new EventArgs());
|
---|
84 | }
|
---|
85 | /// <summary>
|
---|
86 | /// fires when the (size)cache of the node was invalidated
|
---|
87 | /// </summary>
|
---|
88 | public event EventHandler Invalidated;
|
---|
89 | protected virtual void OnInvalidated() {
|
---|
90 | if (Invalidated != null && !m_eventingSuspended)
|
---|
91 | Invalidated(this, new EventArgs());
|
---|
92 | }
|
---|
93 | protected virtual void OnNodeAdded(ILSceneGraphNode node) {
|
---|
94 | if (m_parent != null && !m_eventingSuspended) {
|
---|
95 | m_parent.OnNodeAdded(node);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | protected virtual void OnNodeRemoved(ILSceneGraphNode node) {
|
---|
99 | if (m_parent != null && !m_eventingSuspended) {
|
---|
100 | m_parent.OnNodeRemoved(node);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | /// <summary>
|
---|
104 | /// stop firing events from this node
|
---|
105 | /// </summary>
|
---|
106 | public void EventingSuspend() {
|
---|
107 | m_eventingSuspended = true;
|
---|
108 | }
|
---|
109 | /// <summary>
|
---|
110 | /// resume firing events from this node
|
---|
111 | /// </summary>
|
---|
112 | public void EventingResume() {
|
---|
113 | m_eventingSuspended = false;
|
---|
114 | }
|
---|
115 |
|
---|
116 | #endregion
|
---|
117 |
|
---|
118 | #region properties
|
---|
119 |
|
---|
120 | /// <summary>
|
---|
121 | /// Get/set visiblility for the scene graph branch
|
---|
122 | /// </summary>
|
---|
123 | public bool Visible {
|
---|
124 | get { return m_visible; }
|
---|
125 | set { m_visible = value;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | /// <summary>
|
---|
129 | /// reference to the scene graph node this node is a child of
|
---|
130 | /// </summary>
|
---|
131 | public ILSceneGraphNode Parent {
|
---|
132 | get {
|
---|
133 | return m_parent;
|
---|
134 | }
|
---|
135 | set {
|
---|
136 | m_parent = value;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | /// <summary>
|
---|
140 | /// the minimum coordinate of a cube tightly enclosing the node (and all childs)
|
---|
141 | /// </summary>
|
---|
142 | /// <returns></returns>
|
---|
143 | public virtual ILPoint3Df PositionMin {
|
---|
144 | get {
|
---|
145 | if (m_positionMin.IsEmtpy()) {
|
---|
146 | ComputeNodeLimits();
|
---|
147 | }
|
---|
148 | return m_positionMin;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | /// <summary>
|
---|
152 | /// the maximum coordinate of a cube tightly enclosing the node (and all childs)
|
---|
153 | /// </summary>
|
---|
154 | /// <returns></returns>
|
---|
155 | public virtual ILPoint3Df PositionMax {
|
---|
156 | get {
|
---|
157 | if (m_positionMax.IsEmtpy()) {
|
---|
158 | ComputeNodeLimits();
|
---|
159 | }
|
---|
160 | return m_positionMax;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | /// <summary>
|
---|
164 | /// current geometric center according of scene graph branch
|
---|
165 | /// </summary>
|
---|
166 | public virtual ILPoint3Df Center {
|
---|
167 | get {
|
---|
168 | if (m_center.IsEmtpy()) {
|
---|
169 | ComputeNodeLimits();
|
---|
170 | }
|
---|
171 | return m_center;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | #endregion
|
---|
175 |
|
---|
176 | #region public interface
|
---|
177 | /// <summary>
|
---|
178 | /// invalidate geometry cache for this and all nodes up to root
|
---|
179 | /// </summary>
|
---|
180 | public virtual void Invalidate() {
|
---|
181 | m_center = ILPoint3Df.Empty;
|
---|
182 | m_positionMin = ILPoint3Df.Empty;
|
---|
183 | m_positionMax = ILPoint3Df.Empty;
|
---|
184 | m_invalidated = true;
|
---|
185 | if (Parent != null) {
|
---|
186 | Parent.Invalidate();
|
---|
187 | } else {
|
---|
188 | OnInvalidated();
|
---|
189 | }
|
---|
190 | }
|
---|
191 | /// <summary>
|
---|
192 | /// recompute the size spanned by this node, may fires Changed() event
|
---|
193 | /// </summary>
|
---|
194 | internal abstract void Configure();
|
---|
195 | /// <summary>
|
---|
196 | /// draw all children contained in this node
|
---|
197 | /// </summary>
|
---|
198 | /// <param name="props">extended rendering properties</param>
|
---|
199 | internal abstract void Draw(ILRenderProperties props);
|
---|
200 |
|
---|
201 | #endregion
|
---|
202 |
|
---|
203 | #region private helper
|
---|
204 | /// <summary>
|
---|
205 | /// compute limits of the cube tightly enclosing the branch below this node
|
---|
206 | /// </summary>
|
---|
207 | /// <returns></returns>
|
---|
208 | protected abstract void ComputeNodeLimits();
|
---|
209 |
|
---|
210 | //protected void invalidateChilds(ILSceneGraphNode parent) {
|
---|
211 | // parent.m_center = ILPoint3Df.Empty;
|
---|
212 | // parent.m_positionMin = ILPoint3Df.Empty;
|
---|
213 | // parent.m_positionMax = ILPoint3Df.Empty;
|
---|
214 | // foreach (ILSceneGraphNode child in parent.m_childs) {
|
---|
215 | // child.invalidateChilds(child);
|
---|
216 | // }
|
---|
217 | //}
|
---|
218 | #endregion
|
---|
219 |
|
---|
220 | #region IEnumerable<ILSceneGraphNode> Member
|
---|
221 |
|
---|
222 | /// <summary>
|
---|
223 | /// Create & returns a typed enumerator
|
---|
224 | /// </summary>
|
---|
225 | /// <returns>enumerator of all scene graph nodes below this node</returns>
|
---|
226 | public IEnumerator<ILSceneGraphNode> GetEnumerator() {
|
---|
227 | List<ILSceneGraphNode> ret = new List<ILSceneGraphNode>();
|
---|
228 | CollectAllChildren(ret);
|
---|
229 | return ret.GetEnumerator();
|
---|
230 | }
|
---|
231 | #endregion
|
---|
232 |
|
---|
233 | #region IEnumerable Member
|
---|
234 | /// <summary>
|
---|
235 | /// Create and return untyped enumerator
|
---|
236 | /// </summary>
|
---|
237 | /// <returns>IEnumerator of all scene graph nodes below this node</returns>
|
---|
238 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
|
---|
239 | List<ILSceneGraphNode> children = new List<ILSceneGraphNode>();
|
---|
240 | CollectAllChildren(children);
|
---|
241 | foreach (ILSceneGraphNode node in children) {
|
---|
242 | yield return node;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | internal virtual void CollectAllChildren(List<ILSceneGraphNode> nodes) {
|
---|
246 | nodes.Add(this);
|
---|
247 | }
|
---|
248 |
|
---|
249 | #endregion
|
---|
250 |
|
---|
251 | }
|
---|
252 |
|
---|
253 | }
|
---|