Finding the closest triangle matching the inverse uv mapping

120 Views Asked by At

I have an algorithm which takes 2 triangles ABC and PMN, where ABC is the triangle that is rendered and PMN is an extra triangle that is used for texture mapping and converts the PMN triangle to UV, and an algorithm that can reverse that(from UV to PMN) (well to a very close extent) the 2 algorithms are from: Reversing plane projection texture mapping algorithm

Now even though the PMN triangle it gives have almost identical UV's(when converted back to UV's) compared to the original one, this is not exactly what I want. What I want is to get the closest PMN triangle that already exists in the model

So, for example if I had some triangle ABC(part of the mesh of course)

$$A = {-34, -58, 22}$$

$$B = {-18, -40, 24}$$

$$C = {-26, -34, 22}$$

and the following PMN triangle for it:

$$P = {-38; -80; 25}$$

$$M = {38; -80; 25}$$

$$N = {-38; -30; 28}$$ Which would give me the following UV's if I used the algorithm to convert from PMN to UV:

$$U = [0.05263158, 0.2631579, 0.15789473]$$

$$V = [0.4348346, 0.7959346, 0.9131128]$$

Now if I used the same triangle(ABC) and those UV coordinates as an input to the reverse function, it would give me the following PMN triangle:

$$P = {-38.0000024438, -79.8594424725, 22.6572964191}$$

$$M = {38.0000114441, -80.7750759125, 37.918340683}$$

$$N = {-38, -29.4779157639, 19.2985277176}$$

those are obviously not same but they correspond to extremely similiar UV's(the error rate is < 0.0001 usually) for example, if i converted those PMN triangle again to UV coordinates the result i get is: $$U = [0.052631587, 0.26315784, 0.15789472]$$ $$V = [0.43483466, 0.79593456, 0.91311276]$$

Very similar to the ones above, even tho the PMN triangle is fairly different

My goal is generate a PMN triangle from UV's that exists in the mesh, however I am not sure on how I can do this efficiently, the only way I've thought of is to brute force it and try every possible PMN combination but that would be far too slow if there are many triangles(it should be n ^ 3 where n is the amount of triangles in the mesh) the brute force approach would basically iterate every combination, and for each one it would generate the UV coordinates for a triangle ABC and that combination, then it would check if the generated UV is close to the UV of ABC)