-
Notifications
You must be signed in to change notification settings - Fork 10
/
NodeWalker.cs
135 lines (124 loc) · 3.81 KB
/
NodeWalker.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
/*
* DAWN OF LIGHT - The first free open source DAoC server emulator
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
namespace Niflib.Extensions
{
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;
using Niflib;
/// <summary>
/// Helper Class for Browsing Nodes in a Nif File
/// </summary>
public static class NodeWalker
{
/// <summary>
/// Retrieves Roots from this File
/// </summary>
/// <param name="nif"></param>
/// <returns></returns>
public static IEnumerable<NiNode> GetRoots(this NiFile nif)
{
return nif.Footer.RootNodes.Where(rf => rf.IsValid()).Select(rf => rf.Object).OfType<NiNode>();
}
/// <summary>
/// Retrieve Direct Children from this Node
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public static IEnumerable<NiAVObject> GetChildren(this NiNode node)
{
return node.Children.Where(rf => rf.IsValid() && rf.Object != null).Select(rf => rf.Object);
}
/// <summary>
/// Retrieve LOD Children given Distance
/// </summary>
/// <param name="node"></param>
/// <param name="dist"></param>
/// <returns></returns>
public static IEnumerable<NiNode> GetChildrenFromLOD(this NiLODNode node, float dist)
{
var lods = node.GetChildren().OfType<NiNode>().ToArray();
var lodlevels = node.LODLevels;
if (lodlevels == null && node.LODLevelData.IsValid() && node.LODLevelData.Object != null)
{
var lodData = node.LODLevelData.Object as NiRangeLODData;
if (lodData != null)
lodlevels = lodData.LODLevels;
}
if (lodlevels == null)
{
// Default to first LOD Level for other LOD mechanisms
var result = lods.FirstOrDefault();
if (result != null)
yield return result;
yield break;
}
foreach (var match in lodlevels
.Select((l, i) => new { Ind = i, Lod = l })
.Where(o => dist >= o.Lod.NearExtent && dist < o.Lod.FarExtent && o.Ind < lods.Length))
yield return lods.ElementAt(match.Ind);
yield break;
}
/// <summary>
/// Retrieve Tri Based Nodes with 0f LoD Distance in all tree Depth
/// </summary>
/// <param name="node">Starting Lookup Node</param>
/// <returns>NiTriBasedGeometry Collection</returns>
public static IEnumerable<NiTriBasedGeometry> GetTriBasedNode(this NiNode node)
{
var stack = new Stack<NiNode>();
stack.Push(node);
do
{
var current = stack.Pop();
var nilod = current as NiLODNode;
if (nilod != null)
{
foreach (var child in nilod.GetChildrenFromLOD(0f))
stack.Push(child);
}
else
{
foreach (var child in current.GetChildren())
{
var childNode = child as NiNode;
if (childNode != null)
stack.Push(childNode);
var childGeom = child as NiTriBasedGeometry;
if (childGeom != null)
yield return childGeom;
}
}
}
while(stack.Count > 0);
yield break;
}
/// <summary>
/// Is This Node Invisible
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool IsInvisible(this NiAVObject obj)
{
// Invisible Flag
return (obj.Flags & 1) == 1;
}
}
}