Merge pull request #26790 from benc-uk/master

New type definitions for obj-file-parser package
This commit is contained in:
Paul van Brenk
2018-06-25 14:30:17 -07:00
committed by GitHub
4 changed files with 132 additions and 0 deletions

55
types/obj-file-parser/index.d.ts vendored Normal file
View File

@@ -0,0 +1,55 @@
// Type definitions for obj-file-parser 0.5
// Project: https://github.com/wesunwin/obj-file-parser
// Definitions by: Ben Coleman <https://github.com/benc-uk>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export as namespace ObjFileParser;
/* Class module */
declare class ObjFileParser {
constructor(fileContents: any, defaultModelName?: any);
parse(): ObjFileParser.ObjFile;
}
/* Additional exported interfaces */
declare namespace ObjFileParser {
interface ObjFile {
models: ObjModel[];
materialLibraries: any[];
}
interface ObjModel {
name: string;
vertices: Vertex[];
textureCoords: VertexTexture[];
vertexNormals: Vertex[];
faces: Face[];
}
interface Face {
material: any;
group: string;
smoothingGroup: number;
vertices: FaceVertex[];
}
interface FaceVertex {
vertexIndex: number;
textureCoordsIndex: number;
vertexNormalIndex: number;
}
interface Vertex {
x: number;
y: number;
z: number;
}
interface VertexTexture {
u: number;
v: number;
w: number;
}
}
export = ObjFileParser;

View File

@@ -0,0 +1,53 @@
const testObjFile = `
# cube.obj
# Example object
o cube
v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0
vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
vn 0.0 1.0 0.0
vn 0.0 -1.0 0.0
vn 1.0 0.0 0.0
vn -1.0 0.0 0.0
g cube-faces
f 1//2 7//2 5//2
f 1//2 3//2 7//2
f 1//6 4//6 3//6
f 1//6 2//6 4//6
f 3//3 8//3 7//3
f 3//3 4//3 8//3
f 5//5 7//5 8//5
f 5//5 8//5 6//5
f 1//4 5//4 6//4
f 1//4 6//4 2//4
f 2//1 6//1 8//1
f 2//1 8//1 4//1`;
// Create parser and load test OBJ
const parser = new ObjFileParser(testObjFile, 'test');
const file = parser.parse();
// get first model in file
const model: ObjFileParser.ObjModel = file.models[0];
// gets object name
const name: string = model.name;
// gets first face in model
const face: ObjFileParser.Face = model.faces[0];
// gets first vertex in face
const faceVert: ObjFileParser.FaceVertex = face.vertices[0];
// gets vertex
const vertPoints: ObjFileParser.Vertex = model.vertices[faceVert.vertexIndex];
// gets x value of vertex
const x = vertPoints.x;

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"obj-file-parser-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }