-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmFileUtils.cs
More file actions
110 lines (105 loc) · 3.71 KB
/
Copy pathAlgorithmFileUtils.cs
File metadata and controls
110 lines (105 loc) · 3.71 KB
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
/********************************************************************************
** All rights reserved
** Auth: kay.yang
** E-mail: 1025115216@qq.com
** Date: 6/30/2017 11:13:04 AM
** Version: v1.0.0
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UnityEngine;
namespace KayAlgorithm
{
public class AlgorithmFileUtils
{
public static void Save(byte[,] skeletonResult, string fileName, bool change = true)
{
int w = skeletonResult.GetLength(0);
int h = skeletonResult.GetLength(1);
System.IO.StreamWriter write = new System.IO.StreamWriter(fileName, false, Encoding.UTF8);
for (int i = 0; i < w; ++i)
{
string tmpStr = "";
for (int j = 0; j < h; ++j)
{
int tmp = skeletonResult[i, j];
if (change)
{
tmp = tmp > 10 ? 1 : 0;
}
tmpStr = string.Format("{0} {1}", tmpStr, tmp);
}
write.WriteLine(tmpStr);
}
write.Close();
}
public static void ParseBytes(byte[] all, out List<Vector3> vertList, out List<int> faceindices)
{
vertList = new List<Vector3>();
faceindices = new List<int>();
if (all != null)
{
int idx = 0;
int verNum = BitConverter.ToInt32(all, idx);
idx += 4;
for (int i = 0; i < verNum; ++i)
{
float v1 = BitConverter.ToSingle(all, idx);
idx += 4;
float v2 = BitConverter.ToSingle(all, idx);
idx += 4;
float v3 = BitConverter.ToSingle(all, idx);
idx += 4;
vertList.Add(new Vector3(v1, v2, v3));
}
int faceNum = BitConverter.ToInt32(all, idx);
idx += 4;
for (int i = 0; i < faceNum; ++i)
{
faceindices.Add(BitConverter.ToInt32(all, idx));
idx += 4;
}
}
}
public static void LoadFromFile(string filePath, out List<Vector3> vertList, out List<int> faceindices)
{
FileStream stream = new FileStream(filePath, FileMode.Open);
byte[] all = null;
if (stream.CanRead)
{
int len = (int)stream.Length;
all = new byte[len];
stream.Read(all, 0, len);
}
stream.Close();
vertList = new List<Vector3>();
faceindices = new List<int>();
if (all != null)
{
int idx = 0;
int verNum = BitConverter.ToInt32(all, idx);
idx += 4;
for (int i = 0; i < verNum; ++i)
{
float v1 = BitConverter.ToSingle(all, idx);
idx += 4;
float v2 = BitConverter.ToSingle(all, idx);
idx += 4;
float v3 = BitConverter.ToSingle(all, idx);
idx += 4;
vertList.Add(new Vector3(v1, v2, v3));
}
int faceNum = BitConverter.ToInt32(all, idx);
idx += 4;
for (int i = 0; i < faceNum; ++i)
{
faceindices.Add(BitConverter.ToInt32(all, idx));
idx += 4;
}
}
}
}
}