-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.cs
256 lines (247 loc) · 10.4 KB
/
Model.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
using System;
using System.Collections.Generic;
using System.IO;
namespace ObjLoader
{
public class Model
{
private List<Pair<Color, int>> colors;
private List<Polygon> polygons;
private List<Point3D> nodes;
private string colorsFile;
public Model()
{
polygons = new List<Polygon>();
nodes = new List<Point3D>();
colors = new List<Pair<Color, int>>();
}
public List<Polygon> GetPolygons()
{
return polygons;
}
public List<Point3D> GetNodes()
{
return nodes;
}
public List<Pair<Color, int>> getColors()
{
return colors;
}
public string GetColorsFile()
{
return colorsFile;
}
public bool IsLoaded()
{
return nodes != null && nodes.Count != 0;
}
public void ApplyOperator(Matrix A)
{
Point3D center = new Point3D(0, 0, 0);
double yMin = Double.MaxValue;
for (int i = 0; i < nodes.Count; ++i)
{
Point3D point = nodes[i];
Matrix v = new Matrix(point);
v = MatrixExtractor.GetMultiply(A, v);
point.x = v.Get(0); point.y = v.Get(1); point.z = v.Get(2);
yMin = Math.Min(yMin, point.y);
center.Sum(point);
nodes[i] = point;
}
center.Mult(1.0/nodes.Count);
yMin -= center.y;
ApplySum(-center.x, -center.y-yMin, -center.z);
}
public void ApplyMultiple(double xMult, double yMult, double zMult)
{
for (int i = 0; i < nodes.Count; ++i)
{
Point3D point = nodes[i];
point.x *= xMult;
point.y *= yMult;
point.z *= zMult;
nodes[i] = point;
}
}
public void ApplySum(double xShift, double yShift, double zShift)
{
for (int i = 0; i < nodes.Count; ++i)
{
Point3D point = nodes[i];
point.x += xShift;
point.y += yShift;
point.z += zShift;
nodes[i] = point;
}
}
public void Save(string path)
{
string[] spl = path.Split('.');
string[] splBySlesh = path.Split('\\');
if (spl[spl.Length - 1] == "obj")
{
using (StreamWriter writer = new StreamWriter(path))
{
if (colorsFile == null) colorsFile = splBySlesh[splBySlesh.Length - 1].Split('.')[0] + ".mtl";
using (StreamWriter mtlWriter = new StreamWriter(path.Replace(splBySlesh[splBySlesh.Length - 1], colorsFile)))
{
for(int i = 0; i < colors.Count; ++i)
{
Color color = colors[i].first;
mtlWriter.WriteLine("newmtl "+i);
mtlWriter.WriteLine("\tKd " + color.R/255.0 + " " + color.G/255.0 + " " + color.B/255.0);
}
}
writer.WriteLine("mtllib " + colorsFile);
foreach(Point3D point in nodes)
{
writer.WriteLine("v " + point.x + " " + point.z + " " + point.y);
}
int colorKey = 0;
int lastIndex = 0;
for(int i = 0; i < polygons.Count; ++i)
{
if(i >= lastIndex)
{
writer.WriteLine("usemtl " + colorKey);
lastIndex += colors[colorKey].second;
++colorKey;
}
writer.Write("f ");
List<int> indexes = polygons[i].indexes;
for (int j = 0; j < indexes.Count-1; ++j)
{
writer.Write(indexes[j] + " ");
}
writer.WriteLine(indexes[indexes.Count-1]);
}
}
}
else throw new Exception("Not .obj file");
}
public void Add(string path)
{
if (File.Exists(path))
{
string[] spl = path.Split('.');
if (spl[spl.Length - 1] == "obj")
{
string[] lines = File.ReadAllLines(path);
string key;
bool mtlExist = false;
Dictionary<string, Color> colorByKey = new Dictionary<string, Color>();
int last = -1;
int count = nodes.Count;
double yMin = Double.MaxValue;
Point3D center = new Point3D(0, 0, 0);
foreach (string line in lines)
{
if (!line.Contains("#"))
{
string[] splitted = line.Replace(" ", " ").Split(' ');
if (splitted[0] == "mtllib")
{
string[] pathSplit = path.Split('\\');
string mtlPath = path;
mtlPath = mtlPath.Replace(pathSplit[pathSplit.Length - 1], splitted[1]);
if (File.Exists(mtlPath))
{
mtlExist = true;
if(colorsFile == null) colorsFile = splitted[1];
string[] mtlLines = File.ReadAllLines(mtlPath);
string currentKey = "";
foreach (string mtlLine in mtlLines)
{
if (!mtlLine.Contains("#"))
{
string[] mtlSplitted = mtlLine.Split(' ');
if (mtlSplitted[0] == "newmtl")
{
currentKey = mtlSplitted[1];
}
else if (mtlSplitted[0].Contains("Kd"))
{
byte R = (byte)(255 * Double.Parse(mtlSplitted[1]));
byte G = (byte)(255 * Double.Parse(mtlSplitted[2]));
byte B = (byte)(255 * Double.Parse(mtlSplitted[3]));
Color color = new Color(R, G, B);
colorByKey.Add(currentKey, color);
}
}
}
}
}
else if (splitted[0] == "v")
{
double x = Double.Parse(splitted[1]);
double y = Double.Parse(splitted[3]);
double z = Double.Parse(splitted[2]);
Point3D point = new Point3D(x, y, z);
center.Sum(point);
yMin = Math.Min(yMin, point.y);
nodes.Add(point);
}
else if (splitted[0] == "usemtl")
{
if (mtlExist)
{
key = splitted[1];
++last;
if (!colorByKey.ContainsKey(key)) throw new Exception("Color " + key + " doesn't exist");
colors.Add(new Pair<Color, int>(colorByKey[key], 0));
}
}
else if (splitted[0] == "f")
{
Polygon polygon = new Polygon();
if (last == -1)
{
colors.Add(new Pair<Color, int>(Constants.defaultColor, 0));
++last;
}
colors[last].second++;
for (int i = 1; i < splitted.Length; ++i)
{
string s = splitted[i].Split('/')[0];
if (s != "") polygon.AddIndex(count+Int32.Parse(s));
}
polygons.Add(polygon);
}
}
}
//Shift points to mid
center.Mult(1.0/nodes.Count);
ApplySum(-center.x, -center.y, -center.z);
//Scale points
double maxWidth = 0;
foreach (Point3D p in nodes)
{
if (p.x > maxWidth) maxWidth = p.x;
if (p.y > maxWidth) maxWidth = p.y;
if (p.z > maxWidth) maxWidth = p.z;
}
if (maxWidth > 0)
{
double K = Constants.modelMaxWidth / maxWidth;
ApplyMultiple(K, K, K);
//Shift points above axis y = 0
yMin -= center.y;
yMin *= K;
ApplySum(0, -yMin, 0);
}
}
else throw new Exception("Not .obj file");
}
else throw new Exception(path + " doesn't exist");
}
public void Load(string path)
{
polygons = new List<Polygon>();
nodes = new List<Point3D>();
colors = new List<Pair<Color, int>>();
colorsFile = null;
Add(path);
}
}
}