-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlySharp.cs
312 lines (290 loc) · 10.6 KB
/
PlySharp.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Ply
{
public class PlySharp<TPosition> where TPosition : struct
{
public Format Format { get; }
public List<PlyPosition<TPosition>> Positions = new List<PlyPosition<TPosition>>();
public PlySharp(Format format)
{
Format = format;
}
public virtual void AddPoint(TPosition x, TPosition y, TPosition z)
{
Positions.Add(new PlyPosition<TPosition>(x, y, z));
}
public virtual void AddPoint(PlyPosition<TPosition> pos)
{
Positions.Add(pos);
}
internal string GetTypeAndName<T>() where T : struct
{
if (typeof(T) == typeof(sbyte))
return "char";
else if (typeof(T) == typeof(byte))
return "uchar";
else if (typeof(T) == typeof(short))
return "short";
else if (typeof(T) == typeof(ushort))
return "ushort";
else if (typeof(T) == typeof(int))
return "int";
else if (typeof(T) == typeof(uint))
return "uint";
else if (typeof(T) == typeof(float))
return "float";
else if (typeof(T) == typeof(double))
return "double";
throw new Exception($@"不支持的数据类型,请改用以下数据类型:
{typeof(sbyte).Name},
{typeof(byte).Name},
{typeof(short).Name},
{typeof(ushort).Name},
{typeof(int).Name},
{typeof(uint).Name},
{typeof(float).Name},
{typeof(double).Name}。");
}
internal void WriteBinaryLittleEndianToStream<T>(BinaryWriter bw, T data) where T : struct
{
if (data is sbyte sbyteData)
bw.Write(sbyteData);
else if (data is byte byteData)
bw.Write(byteData);
else if (data is short shorData)
bw.Write(shorData);
else if (data is ushort ushortData)
bw.Write(ushortData);
else if (data is int intData)
bw.Write(intData);
else if (data is uint uintData)
bw.Write(uintData);
else if (data is float floatData)
bw.Write(floatData);
else if (data is double doubleData)
bw.Write(doubleData);
else
throw new Exception($@"不支持的数据类型,请改用以下数据类型:
{typeof(sbyte).Name},
{typeof(byte).Name},
{typeof(short).Name},
{typeof(ushort).Name},
{typeof(int).Name},
{typeof(uint).Name},
{typeof(float).Name},
{typeof(double).Name}。");
}
internal void WriteBinaryBigEndianToStream<T>(BinaryWriter bw, T data) where T : struct
{
if (data is byte byteData)
{
bw.Write(byteData);
}
else if (data is sbyte sbyteData)
bw.Write(sbyteData);
else
{
byte[] bytes;
if (data is short shorData)
bytes = BitConverter.GetBytes(shorData);
else if (data is ushort ushortData)
bytes = BitConverter.GetBytes(ushortData);
else if (data is int intData)
bytes = BitConverter.GetBytes(intData);
else if (data is uint uintData)
bytes = BitConverter.GetBytes(uintData);
else if (data is float floatData)
bytes = BitConverter.GetBytes(floatData);
else if (data is double doubleData)
bytes = BitConverter.GetBytes(doubleData);
else
throw new Exception($@"不支持的数据类型,请改用以下数据类型:
{typeof(sbyte).Name},
{typeof(byte).Name},
{typeof(short).Name},
{typeof(ushort).Name},
{typeof(int).Name},
{typeof(uint).Name},
{typeof(float).Name},
{typeof(double).Name}。");
bytes = bytes.Reverse().ToArray();
bw.Write(bytes);
}
}
public virtual void BuildToStream(Stream stream)
{
using BinaryWriter bw = new BinaryWriter(stream);
string header = $@"ply
format {Format} 1.0
comment generated by Alex1911
element vertex {Positions.Count}
property {GetTypeAndName<TPosition>()} x
property {GetTypeAndName<TPosition>()} y
property {GetTypeAndName<TPosition>()} z
end_header
";
byte[] headerBytes = Encoding.ASCII.GetBytes(header);
bw.Write(headerBytes);
if (Format == Format.binary_little_endian)
{
for (int i = 0; i < Positions.Count; i++)
{
WriteBinaryLittleEndianToStream(bw, Positions[i].X);
WriteBinaryLittleEndianToStream(bw, Positions[i].Y);
WriteBinaryLittleEndianToStream(bw, Positions[i].Z);
}
}
else if (Format == Format.binary_big_endian)
{
for (int i = 0; i < Positions.Count; i++)
{
WriteBinaryBigEndianToStream(bw, Positions[i].X);
WriteBinaryBigEndianToStream(bw, Positions[i].Y);
WriteBinaryBigEndianToStream(bw, Positions[i].Z);
}
}
else if (Format == Format.ascii)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Positions.Count; i++)
{
sb.Append($"{Positions[i].X} {Positions[i].Y} {Positions[i].Z}\n");
}
byte[] dataBytes = Encoding.ASCII.GetBytes(sb.ToString());
bw.Write(dataBytes);
}
}
public virtual void BuildToFile(string fileName)
{
using FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
BuildToStream(fs);
}
public virtual byte[] BuildAsBytes()
{
using MemoryStream ms = new MemoryStream();
BuildToStream(ms);
return ms.ToArray();
}
}
public class PlySharp<TPosition, TColor> : PlySharp<TPosition> where TPosition : struct where TColor : struct
{
public List<IColorStruct<TColor>> Colors = new List<IColorStruct<TColor>>();
public PlySharp(Format format) : base(format)
{
}
public void AddPointAndColor(TPosition x, TPosition y, TPosition z, TColor red, TColor green, TColor blue)
{
AddPoint(x, y, z);
AddColor(red, green, blue);
}
public void AddPointAndColor(PlyPosition<TPosition> pos, PlyColor<TColor> color)
{
AddPoint(pos);
AddColor(color);
}
public void AddColor(TColor red, TColor green, TColor blue)
{
Colors.Add(new PlyColor<TColor>(red, green, blue));
}
public void AddColor(PlyColor<TColor> color)
{
Colors.Add(color);
}
public override void BuildToStream(Stream stream)
{
if (Positions.Count != Colors.Count)
{
throw new Exception("点数量和颜色数量不一致。");
}
using BinaryWriter bw = new BinaryWriter(stream);
string header = $@"ply
format {Format} 1.0
comment generated by Alex1911
element vertex {Positions.Count}
property {GetTypeAndName<TPosition>()} x
property {GetTypeAndName<TPosition>()} y
property {GetTypeAndName<TPosition>()} z
property {GetTypeAndName<TColor>()} red
property {GetTypeAndName<TColor>()} green
property {GetTypeAndName<TColor>()} blue
end_header
";
byte[] headerBytes = Encoding.ASCII.GetBytes(header);
bw.Write(headerBytes);
if (Format == Format.binary_little_endian)
{
for (int i = 0; i < Positions.Count; i++)
{
WriteBinaryLittleEndianToStream(bw, Positions[i].X);
WriteBinaryLittleEndianToStream(bw, Positions[i].Y);
WriteBinaryLittleEndianToStream(bw, Positions[i].Z);
WriteBinaryLittleEndianToStream(bw, Colors[i].Red);
WriteBinaryLittleEndianToStream(bw, Colors[i].Green);
WriteBinaryLittleEndianToStream(bw, Colors[i].Blue);
}
}
else if (Format == Format.binary_big_endian)
{
for (int i = 0; i < Positions.Count; i++)
{
WriteBinaryBigEndianToStream(bw, Positions[i].X);
WriteBinaryBigEndianToStream(bw, Positions[i].Y);
WriteBinaryBigEndianToStream(bw, Positions[i].Z);
WriteBinaryBigEndianToStream(bw, Colors[i].Red);
WriteBinaryBigEndianToStream(bw, Colors[i].Green);
WriteBinaryBigEndianToStream(bw, Colors[i].Blue);
}
}
else if (Format == Format.ascii)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Positions.Count; i++)
{
sb.Append($"{Positions[i].X} {Positions[i].Y} {Positions[i].Z} {Colors[i].Red} {Colors[i].Green} {Colors[i].Blue}\n");
}
byte[] dataBytes = Encoding.ASCII.GetBytes(sb.ToString());
bw.Write(dataBytes);
}
}
}
public struct PlyPosition<T>
{
public PlyPosition(T x, T y, T z)
{
X = x;
Y = y;
Z = z;
}
public T X { get; set; }
public T Y { get; set; }
public T Z { get; set; }
}
public struct PlyColor<T> : IColorStruct<T> where T : struct
{
public PlyColor(T red, T green, T blue)
{
Red = red;
Green = green;
Blue = blue;
}
public T Red { get; set; }
public T Green { get; set; }
public T Blue { get; set; }
}
public enum Format
{
binary_little_endian,
ascii,
binary_big_endian,
}
public interface IColorStruct<T> where T : struct
{
public T Red { get; set; }
public T Green { get; set; }
public T Blue { get; set; }
}
}