-
Notifications
You must be signed in to change notification settings - Fork 0
/
DICOM & STL File Manager.txt
209 lines (177 loc) · 6.21 KB
/
DICOM & STL File Manager.txt
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
using Dicom;
using Dicom.Imaging;
using Parabox.Stl;
using SFB;
using System.IO;
using System.Text;
using UnityEngine;
using VolumeRendering;
public class FileLoader : MonoBehaviour
{
public Texture2DArrayToTexture3DConverter converter;
string error;
public TestController controller;
public GameObject objMeshToExport;
public GameObject source;
string path;
public Mesh[] meshs;
public void OnGUI()
{
GUI.matrix = Matrix4x4.TRS(new Vector3(50, 50, 1), Quaternion.identity, new Vector3(3, 2.5f, 1));
if (GUILayout.Button("Open STL File"))
{
WriteSTL(StandaloneFileBrowser.OpenFilePanel("Open File", "", "", false));
}
/*GUI.matrix = Matrix4x4.TRS(new Vector3(50, 100, 1), Quaternion.identity, new Vector3(3, 2.5f, 1));
if (GUILayout.Button("Open Texture File"))
{
WriteTexture(StandaloneFileBrowser.OpenFilePanel("Open File", "", "", false));
}*/
GUI.matrix = Matrix4x4.TRS(new Vector3(50, 50, 1), Quaternion.identity, new Vector3(3, 2.5f, 1));
if (GUILayout.Button("Open .dcm Folder"))
{
var paths = StandaloneFileBrowser.OpenFolderPanel("Select Folder", "", true);
WriteTexture(paths);
}
GUILayout.Space(15);
}
public void WriteSTL(string[] paths)
{
bool b_isUpper = false;
if (paths.Length == 0)
{
return;
}
path = "";
foreach (var p in paths)
{
// 확장자명까지 포함
path += p;
}
string str = "UpperJaw";
if (path.Contains(str))
{
b_isUpper = true;
}
objMeshToExport.transform.position = Vector3.zero;
objMeshToExport.transform.rotation = Quaternion.identity;
Transform[] sourceArray = objMeshToExport.GetComponentsInChildren<Transform>();
foreach (Transform t in sourceArray)
{
if (t.name != objMeshToExport.name)
Destroy(t.gameObject);
}
Mesh[] mesh = Importer.Import(path);
meshs = mesh;
foreach (Mesh m in meshs)
{
GameObject obj = Instantiate(source, Vector3.zero, Quaternion.identity);
obj.GetComponent<MeshFilter>().mesh = m;
obj.transform.SetParent(objMeshToExport.transform);
}
objMeshToExport.transform.position = new Vector3(-100f, 0f, 0f);
if (b_isUpper)
{
objMeshToExport.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 45f));
}
else
{
objMeshToExport.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, -45f));
}
}
public void WriteTexture(string[] paths)
{
/*
if (paths.Length == 0)
{
return;
}
path = "";
foreach (var p in paths)
{
path += p;
}
controller.ChangeTexture(path);
*/
converter.texture2DArray.Clear();
if (paths.Length == 0)
{
return;
}
path = "";
foreach (var p in paths)
{
path += p;
}
SystemIOFileLoad(path);
}
public string test;
public string s;
private void SystemIOFileLoad(string Path)
{
//Dicom.Imaging.Codec.TranscoderManager.SetImplementation(new Dicom.Imaging.NativeCodec.NativeTranscoderManager());
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var info = new DirectoryInfo(path);
var fileInfo = info.GetFiles();
foreach (var file in fileInfo)
{
if (file.Extension == ".dcm")
{
DicomFile dicomFile = DicomFile.Open(file.FullName);
DicomImage image = new DicomImage(dicomFile.Dataset);
test = image.RenderImage().ToString();
var tex2d = image.RenderImage().As<Texture2D>();
converter.texture2DArray.Add(tex2d);
//var f = DicomFile.Open(file.FullName);
//f.Dataset.AddOrUpdate(DicomTag.SpecificCharacterSet, "x-cp20949");
//f.Dataset.AddOrUpdate(new DicomPersonName(DicomTag.PatientName, DicomEncoding.GetEncoding("x-cp20949"), "???"));
//byte[] bData = DicomEncoding.Default.GetBytes(f.File.Name);
//var image = new Dicom.Imaging.DicomImage(DicomEncoding.Default.GetString(bData));
//Encoding enkr = Encoding.GetEncoding(949);
//byte[] bData = Encoding.Default.GetBytes(file.FullName);
//byte[] bConvert = Encoding.Convert(enkr, Encoding.Default, bData);
//string str = Encoding.Default.GetString(bConvert);
//var image = new Dicom.Imaging.DicomImage(str);
//s = str;
//byte[] bData = Encoding.UTF8.GetBytes(file.FullName);
//var image = new Dicom.Imaging.DicomImage(Encoding.UTF8.GetString(bData));
//var image = new Dicom.Imaging.DicomImage(file.FullName);
//test = image.RenderImage().ToString();
//var tex2d = image.RenderImage().As<Texture2D>();
//converter.texture2DArray.Add(tex2d);
}
}
Convert();
}
public void Convert()
{
var tex2dArray = converter.texture2DArray;
tex2dArray.Reverse();
if (tex2dArray.Count == 0)
{
error = "no image";
}
var w = tex2dArray[0].width;
var h = tex2dArray[0].height;
var d = tex2dArray.Count;
var format = tex2dArray[0].format;
var colors = new UnityEngine.Color32[w * h * d];
for (int i = 0; i < d; ++i)
{
var tex2d = tex2dArray[i];
if (tex2d.width != w || tex2d.height != h)
{
error = "texture size error";
}
if (tex2d.format != format)
{
error = "texture format error";
}
tex2d.GetPixels32().CopyTo(colors, w * h * i);
}
var tex3d = new Texture3D(w, h, d, format, false);
tex3d.SetPixels32(colors);
tex3d.Apply();
controller.ChangeTexture(tex3d);
}
}