1 /**
2 Copyright: Copyright (c) 2016-2017 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module voxelman.model.obj;
7 
8 import std.algorithm : splitter, startsWith;
9 import std.conv : to;
10 import std..string : lineSplitter, stripLeft;
11 
12 import voxelman.math;
13 
14 import voxelman.container.buffer : Buffer;
15 import voxelman.model.utils : unrollFaces;
16 import voxelman.model.mesh;
17 
18 Vector!(T, 3)[] readObjFile(T)(string fileName)
19 {
20 	import std.file : read;
21 	string data = cast(string)read(fileName);
22 	return parseObj!T(data);
23 }
24 
25 Vector!(T, 3)[] parseObj(T)(string fileData)
26 {
27 	auto lines = fileData.lineSplitter;
28 
29 	Buffer!(Vector!(T, 3)) vertices;
30 	Buffer!Face3 faces;
31 
32 	foreach (line; lines)
33 	{
34 		if (line.startsWith("v "))
35 		{
36 			Vector!(T, 3) v;
37 			string items = cast(string)line[2..$];
38 
39 			size_t i;
40 			foreach(num; items.splitter)
41 			{
42 				v.arrayof[i] = to!T(num);
43 				++i;
44 			}
45 			vertices.put(v);
46 		}
47 		else if (line.startsWith("f "))
48 		{
49 			int[3] face;
50 
51 			size_t i;
52 			foreach (pol; line[2..$].splitter)
53 			{
54 				int ind = to!int(pol.splitter("/").front) - 1;
55 				face[i] = ind;
56 				++i;
57 			}
58 			faces.put(Face3(face));
59 		}
60 	}
61 
62 	return unrollFaces(vertices.data, faces.data);
63 }