CSE 477 / G. Farin

Exploring .obj format

(SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[
EvaluationNotebook[]],{1},FrontEnd`FileName]]];)


mesh= Import["http://www.farinhansford.com/gerald/classes/cse477/nbs/test3D.obj"]
(* needs to be in same directory as this notebook *)
(* or use "teapot.obj" *)
(* or use "Jasper.obj" *)

vertex= mesh[[1,2]][[1]];  (* vertex list *)
nvertex = Length[vertex];      (*how many vertices *)

triangle= mesh[[1,2]][[2]][[1]][[1]][[1]]; (*triangle list *)
ntriangle=Length[triangle];   (*how many triangles *)

qneighbor[i_,j_]:=If[Length[triangle[[i]] ⋂ triangle[[j]]]==2,True,False];  (* query: are triangles i and j neighbors? *)

qedge[i_,j_,k_]:=If[Length[{j,k}⋂ triangle[[i]]]==2,True,False]; (* query: do {vertex[[j]],vertex[[k]]} form an edge in triangle [[i]] *)

star[i_]:=Delete[Union[ Table[If[MemberQ[triangle[[j]],i],j,-1],{j,1,ntriangle}]],1]; (* list of all triangles containing vertex[[i]] *)

starz=Table[star[i],{i,1,nvertex}]; (* list of all stars *)
lstar=Table[Length[starz[[j]]],{j,1,nvertex}]; (* lengths of all stars *)

starbdy[i_]:=Union[Flatten[Table[triangle[[starz[[i]][[jj]]]],{jj,1,lstar[[i]]}]]];(* indices of vertices comprising star[i]; these point to triangle[] and contain vertex[[i]]*)

starcoords[i_]:=Table[vertex[[starbdy[i][[j]]]],{j,1,Length[starbdy[i]]}];
(*coordinates of all vertices in vertex[[i]]'s star *)

centroid[i_]:=Sum[starcoords[i][[j]],{j,1,Length[starbdy[i]]}]/Length[starbdy[i]];
(*coordinates of centroid of vertex[[i]]'s star *)

starvx=Table[starbdy[i],{i,1,nvertex}]; (* list of all dstars *)
lstarvx=Table[Length[starvx[[j]]],{j,1,nvertex}]; (* lengths of all dstars *)

dstarvx[i_]:=Table[triangle[[starvx[[i]][[jj]]]],{jj,1,lstarvx[[i]]}];
(* indices of vertices of double star[i]; these point to triangle[] and contain vertex[[i]]*)

Print["data set has ",nvertex, " vertices and ", ntriangle" triangles "]

Print["  vertex       triangle     starz   "]
Print[MatrixForm[vertex],MatrixForm[triangle],MatrixForm[starz]]