Skip to main content

Command Palette

Search for a command to run...

Same models, different space

Updated
•2 min read•View as Markdown

Let's say you have some gaps an obj model, but LUCKILY you have that same model in a different format that can fill in the gaps. Phew saved. Oh no, they're not in the same space and your obj fits inside the tiniest gltf Node. So, how do I transform one model to another model? Well, luckily for us since its a triangular mesh, they're all affine transformations and can be solved using numpy. It should be noted through, you'll need four points that map to each other to build the matrix. So I fired up Meshlab (and blender because it was a large obj) and picked four points in each that map to each other, stuck it into

import sys
import numpy as np
def build_matrix():
  ins = [[14.537343, 0.310858, 17070940],[15.518601, 0.308673, 17.430876],[15.006434,0.332547,18.793961], 14.025344, 0.331390, 18.434074]];
  out = [[908.584, 19.429, 1066.934], [969.913, 19.292, 1089.430], [937.902, 20.784, 1174.623],[ 876.584, 20712.1152.130]];

  G = np.transpose(np.matrix(ins))
  O = np.transpose(np.matrix(out))
  G = np.vstack((G, [1,1,1,1]))
  O = np.vstack((O, [1,1,1,1]))
  A = O * G.I
  A, t = np.hsplit(np.array(A), [l-1])
  t = np.transpose(t)[0]
  return [A,t]

Congratulations, we constructed an A-matrix and t vector. We're trying to solve $$ G = O * A $$, where $G$ is a 4x4 matrix of points in the gltf model, $O$ is a 4x4 matrix of the obj model, and we're trying to solve for a 4x4 matrix $A$. Well, really we're trying to solve for A such that

$$ A = O * G^{-1} $$

Now what? Well...

[A,t] = build_matrix()
file = open(sys.argv[1])
for line in file:
  if (line[0:2] == 'v '):
    line = line.rstrip()
    print("v ", end =' ')
    vec = list()
    for i, num in enumerate(line[2:].split(' ')):
      vec.insert(i, float(num))
    vec = tuple(vec)
    vec = np.dot(A, vec) + t
    print(str(vec[0]) + ' ' + str9vec[1]) + ' '  + str9vec[2]))
  else:
    print(line, end'')

For each line that's a vertex in the obj, A-matrix by the vertex vector and then add t. And that's it.

More from this blog

It needs to be written down

59 posts