forked from LogicalError/CSG-blogpost-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSGUtility.cs
80 lines (74 loc) · 2.1 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RealtimeCSG
{
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
}
}