Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions External/KdTree/KdTreeLib/KdTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;

namespace UnityEngine.ProBuilder.KdTree
Expand All @@ -25,6 +25,7 @@ public DuplicateNodeError()
}

[Serializable]
[DataContract]
class KdTree<TKey, TValue> : IKdTree<TKey, TValue>
{
public KdTree(int dimensions, ITypeMath<TKey> typeMath)
Expand All @@ -40,12 +41,16 @@ public KdTree(int dimensions, ITypeMath<TKey> typeMath, AddDuplicateBehavior add
AddDuplicateBehavior = addDuplicateBehavior;
}

[DataMember]
private int dimensions;

[DataMember]
private ITypeMath<TKey> typeMath = null;

[DataMember]
private KdTreeNode<TKey, TValue> root = null;

[DataMember]
public AddDuplicateBehavior AddDuplicateBehavior { get; private set; }

public bool Add(TKey[] point, TValue value)
Expand Down Expand Up @@ -370,7 +375,8 @@ public KdTreeNode<TKey, TValue>[] RadialSearch(TKey[] center, TKey radius, int c
return neighbourArray;
}

public int Count { get; private set; }
[DataMember]
public int Count { get; private set; }

public bool TryFindValueAt(TKey[] point, out TValue value)
{
Expand Down Expand Up @@ -578,20 +584,20 @@ public void Clear()

public void SaveToFile(string filename)
{
BinaryFormatter formatter = new BinaryFormatter();
var serializer = new DataContractSerializer(typeof(KdTree<TKey, TValue>));
using (FileStream stream = File.Create(filename))
{
formatter.Serialize(stream, this);
serializer.WriteObject(stream, this);
stream.Flush();
}
}

public static KdTree<TKey, TValue> LoadFromFile(string filename)
{
BinaryFormatter formatter = new BinaryFormatter();
var serializer = new DataContractSerializer(typeof(KdTree<TKey, TValue>));
using (FileStream stream = File.Open(filename, FileMode.Open))
{
return (KdTree<TKey, TValue>)formatter.Deserialize(stream);
return (KdTree<TKey, TValue>)serializer.ReadObject(stream);
}

}
Expand Down
7 changes: 7 additions & 0 deletions External/KdTree/KdTreeLib/KdTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace UnityEngine.ProBuilder.KdTree
{
[Serializable]
[DataContract]
class KdTreeNode<TKey, TValue>
{
public KdTreeNode()
Expand All @@ -18,11 +20,16 @@ public KdTreeNode(TKey[] point, TValue value)
Value = value;
}

[DataMember]
public TKey[] Point;
[DataMember]
public TValue Value = default(TValue);
[DataMember]
public List<TValue> Duplicates = null;

[DataMember]
internal KdTreeNode<TKey, TValue> LeftChild = null;
[DataMember]
internal KdTreeNode<TKey, TValue> RightChild = null;

internal KdTreeNode<TKey, TValue> this[int compare]
Expand Down