-
Notifications
You must be signed in to change notification settings - Fork 9
/
CSGUtility.cs
156 lines (140 loc) · 4.16 KB
/
CSGUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using System.Threading.Tasks;
using CSGDemo.CSGXML;
using CSGDemo.Geometry;
using CSGDemo.CSGHierarchy;
using OpenTK;
namespace CSGDemo
{
public static class CSGUtility
{
#region FindChildNodes
public static IEnumerable<CSGNode> FindChildNodes(CSGNode node)
{
yield return node;
if (node.NodeType != CSGNodeType.Brush)
{
foreach (var child in FindChildNodes(node.Left))
yield return child;
foreach (var child in FindChildNodes(node.Right))
yield return child;
}
}
#endregion
#region FindChildBrushes
public static IEnumerable<CSGNode> FindChildBrushes(CSGNode node)
{
if (node.NodeType != CSGNodeType.Brush)
{
foreach (var brush in FindChildBrushes(node.Left))
yield return brush;
foreach (var brush in FindChildBrushes(node.Right))
yield return brush;
yield break;
}
else
yield return node;
}
public static IEnumerable<CSGNode> FindChildBrushes(CSGTree tree)
{
return FindChildBrushes(tree.RootNode);
}
#endregion
#region UpdateChildTransformations
public static void UpdateChildTransformations(CSGNode node, Vector3 parentTranslation)
{
node.Translation = Vector3.Add(parentTranslation, node.LocalTranslation);
if (node.NodeType == CSGNodeType.Brush)
return;
UpdateChildTransformations(node.Left, node.Translation);
UpdateChildTransformations(node.Right, node.Translation);
}
public static void UpdateChildTransformations(CSGNode node)
{
if (node.NodeType == CSGNodeType.Brush)
return;
UpdateChildTransformations(node.Left, node.Translation);
UpdateChildTransformations(node.Right, node.Translation);
}
#endregion
#region UpdateBounds
public static void UpdateBounds(CSGNode node)
{
if (node.NodeType != CSGNodeType.Brush)
{
var leftNode = node.Left;
var rightNode = node.Right;
UpdateBounds(leftNode);
UpdateBounds(rightNode);
node.Bounds.Clear();
node.Bounds.Add(leftNode.Bounds.Translated(Vector3.Subtract(leftNode.Translation, node.Translation)));
node.Bounds.Add(rightNode.Bounds.Translated(Vector3.Subtract(rightNode.Translation, node.Translation)));
}
}
#endregion
#region RemoveInstances (private)
static XmlNode RemoveInstances(XmlNode node, Dictionary<string, XmlNode> lookup)
{
if (node == null)
return null;
var instance = node as XmlInstance;
if (instance != null)
{
if (string.IsNullOrWhiteSpace(instance.InstanceID))
return null;
XmlNode lookupNode;
if (!lookup.TryGetValue(instance.InstanceID, out lookupNode))
return null;
lookupNode = lookupNode.Clone();
lookupNode.ID = instance.ID;
if (lookupNode == null)
return null;
var result = RemoveInstances(lookupNode, lookup);
if (result == null)
return null;
result.ID = instance.ID;
result.Translation = Vector3.Add(instance.Translation, result.Translation);
result.Animate = result.Animate || instance.Animate;
return result;
}
var branch = node as XmlBranch;
if (branch != null)
{
branch.Left = RemoveInstances(branch.Left, lookup);
branch.Right = RemoveInstances(branch.Right, lookup);
if (branch.Left == null ||
branch.Right == null)
return null;
}
return node;
}
#endregion
#region LoadTree
static XmlSerializer importSerializer = new XmlSerializer(typeof(XmlCSGFile));
public static CSGTree LoadTree(string filename, out List<CSGNode> animatedNodes)
{
using (var myReadStream = File.OpenRead(filename))
{
var csgFile = importSerializer.Deserialize(myReadStream) as XmlCSGFile;
var lookup = new Dictionary<string, XmlNode>();
foreach (var instance in csgFile.Instances.Nodes)
lookup.Add(instance.ID, instance);
csgFile.XmlTree.RootNode = RemoveInstances(csgFile.XmlTree.RootNode, lookup);
if (csgFile.XmlTree.RootNode == null)
{
animatedNodes = null;
return null;
}
HashSet<CSGNode> animatedNodesHash;
var csgTree = csgFile.XmlTree.ToCSGTree(out animatedNodesHash);
animatedNodes = animatedNodesHash.ToList();
return csgTree;
}
}
#endregion
}
}