How can I export the Face Tracker 3D mesh as an obj?

I’m trying to export the Face Tracker mesh:

source

I tried to export it like so:

void testApp::exportOBJ(ofMesh m,const string name){
	obj.open(ofToDataPath(name),ofFile::WriteOnly);
	obj << "vertices:\n";
	for(int i = 0 ; i < m.getNumVertices(); i++) {
		ofVec3f v = m.getVertex(i);
		obj << "v " + ofToString(v.x) + " " + ofToString(v.y) + " " + ofToString(v.z) + "\n";
	}
	obj << "faces:\n";
	for(int i = 0 ; i < m.getNumIndices(); i += 3)
		obj << "f " + ofToString(m.getIndex(i)) + " " + ofToString(m.getIndex(i+1)) + " " + ofToString(m.getIndex(i+2)) + "\n";
	obj << "\n";
	obj.close();
	cout << "wrote " << name << endl;
}

But on osx I can’t open the mesh (tried with meshlab/blender/wings3d).
I’ve managed to open the same mesh on windows with rhino, and it looks mostly like a face,
but some vertices are really distorted/in the wrong location, so my guess is I’m doing something wrong with the indices too.

Here’s a sample .obj:

vertices:
v -20.6022 -5.66773 21.5579
v -19.8261 -0.419152 20.9754
v -18.7688 4.80224 20.5902
v -17.4952 9.82323 18.4562
v -15.6561 14.3357 13.5869
v -12.5987 17.9449 8.00129
v -8.53776 20.5398 3.32444
v -3.94841 22.2104 -0.892325
v 1.02873 22.4121 -4.53705
v 6.18366 21.5766 -0.714729
v 10.605 19.0508 3.50892
v 14.2893 15.6505 8.12131
v 17.009 11.5584 13.606
v 18.5784 6.98063 18.3654
v 19.3276 2.1372 20.5367
v 19.64 -2.84804 20.831
v 19.5423 -7.82872 21.2609
v -17.327 -14.5225 0.123951
v -14.9373 -16.7636 -1.52573
v -11.8495 -17.9108 -2.87878
v -8.56141 -18.0651 -4.1774
v -5.32835 -17.4937 -5.38178
v 2.79548 -17.8005 -5.4358
v 5.63979 -18.6976 -4.14262
v 8.66089 -18.956 -2.80534
v 11.6497 -18.4135 -1.37405
v 14.2054 -16.789 0.341925
v -0.408693 -11.8925 -4.88226
v -0.180875 -8.73332 -7.72351
v 0.0312087 -5.60233 -10.714
v 0.231803 -2.47574 -13.552
v -2.84584 1.30904 -6.47237
v -1.12265 1.67075 -7.31997
v 0.671659 1.72685 -8.1004
v 2.29284 1.47247 -7.20141
v 3.78514 0.901742 -6.43822
v -12.9266 -9.93115 -0.801998
v -11.0504 -11.234 -1.02927
v -8.72255 -11.4499 -1.16927
v -6.69202 -10.4231 -1.045
v -8.72807 -9.76957 -1.15914
v -10.8849 -9.54587 -1.06901
v 4.84169 -11.0086 -0.905979
v 6.64327 -12.2275 -1.11565
v 8.91755 -12.4115 -0.967128
v 10.9016 -11.366 -0.685089
v 8.99931 -10.6743 -0.93854
v 6.88148 -10.6499 -0.986586
v -6.31415 9.57306 -2.88979
v -4.28127 7.70573 -5.03882
v -1.92705 6.22388 -6.66532
v 0.757197 6.1971 -8.35617
v 3.3569 5.91561 -6.45012
v 5.76649 7.07784 -4.82012
v 7.81309 8.75701 -2.65881
v 5.91403 10.1098 -3.89979
v 3.55874 10.8651 -5.05484
v 1.01962 11.0854 -6.49424
v -1.54786 11.1077 -5.23493
v -4.05109 10.6435 -4.02293
v -1.89044 8.35439 -5.36359
v 0.896875 8.06717 -7.44355
v 3.61175 8.07349 -5.06538
v 3.60898 8.43406 -4.47627
v 0.941186 8.54596 -6.41103
v -1.78114 8.73121 -4.70015
faces:
f 20 21 23
f 21 22 23
f 0 1 36
f 15 16 45
f 0 17 36
f 16 26 45
f 17 18 37
f 25 26 44
f 17 36 37
f 26 44 45
f 18 19 38
f 24 25 43
f 18 37 38
f 25 43 44
f 19 20 38
f 23 24 43
f 20 21 39
f 22 23 42
f 20 38 39
f 23 42 43
f 21 22 27
f 21 27 39
f 22 27 42
f 27 28 42
f 27 28 39
f 28 42 47
f 28 39 40
f 1 36 41
f 15 45 46
f 1 2 41
f 14 15 46
f 28 29 40
f 28 29 47
f 2 40 41
f 14 46 47
f 2 29 40
f 14 29 47
f 2 3 29
f 13 14 29
f 29 30 31
f 29 30 35
f 3 29 31
f 13 29 35
f 30 32 33
f 30 33 34
f 30 31 32
f 30 34 35
f 3 4 31
f 12 13 35
f 4 5 48
f 11 12 54
f 5 6 48
f 10 11 54
f 6 48 59
f 10 54 55
f 6 7 59
f 9 10 55
f 7 58 59
f 9 55 56
f 8 57 58
f 8 56 57
f 7 8 58
f 8 9 56
f 4 31 48
f 12 35 54
f 31 48 49
f 35 53 54
f 31 49 50
f 35 52 53
f 31 32 50
f 34 35 52
f 32 33 50
f 33 34 52
f 33 50 51
f 33 51 52
f 48 49 60
f 49 60 50
f 50 60 61
f 50 51 61
f 51 52 61
f 61 62 52
f 52 53 62
f 53 54 62
f 54 55 63
f 55 56 63
f 56 63 64
f 56 57 64
f 64 65 57
f 57 58 65
f 58 59 65
f 48 59 65

Any hints/tips on how I can export a mesh correctly, like the one in @kylemcdonald and Greg Borenstein’s article ?

just use mesh.save("mesh.ply") you should be able to open it on meshlab and convert it to obj

Thanks Arturo, good tip.

Also, wanted to point that that the obj faces index starts at 1, and when adding 1 to the index that needs to be grouped first, before the conversion to string:

obj << "f " + ofToString((m.getIndex(i)+1)) + " " + ofToString((m.getIndex(i+1)+1)) + " " + ofToString((m.getIndex(i+2)+1)) + "\n";