StoccoDavide / acme

A Small 3D Geometry Library
https://StoccoDavide.github.io/acme/
Other
2 stars 3 forks source link

UV coordinates within plane #10

Closed ppizarror closed 1 year ago

ppizarror commented 1 year ago

Hi! I need the (U, V) coordinates given a (X, Y, Z) coplanar to a plane p, for example, for plane defined in:

acme::point pA(5.02339, -0.81223, -1.13819); // Origin
acme::point pB(2.32428, 2.78375, 1.50523);
acme::point pC(-0.29562, -0.53994, 2.78661);
acme::triangle t(pA, pB, pC);
acme::plane p(pA, t.normal());

And taking as basis e1, e2:

acme::vec3 n = p.normal();
acme::vec3 e1 = n.cross(acme::vec3(1, 0, 0)).normalized();
if (e1.isZero()) e1 = n.cross(acme::vec3(0, 0, 1)).normalized();
acme::vec3 e2 = n.cross(e1).normalized();

Computing UV for any coordinate is relatively easy, by computing the dot product of the base and the relative distance of any point to the origin of the plane. For example, for point point:

acme::point pointRelative = point - p.origin();
acme::vec2 pointUV(e1.dot(pointRelative), e2.dot(pointRelative));

The question is, can we add this feature to the plane object? It is desirable in acme? All previous steps can be resumed on a single function

p.getUV(acme::point);
p.fromUV(acme::vec2);

Examples:

acme::point xyz(1.10808, -2.2061, 1.45032);
acme::vec2 uv = p.getUV(xyz); // [ -8.902579e-01, 4.814598e+00 ]'
acme::point p =p.fromUV(uv); // p == xyz

Greetings!

StoccoDavide commented 1 year ago

Hi! acme has been initially thought only for strictly 3D geometrical operations but I can add the feature you requested.

If you need 2D support, you have good ideas on how to support mixed 2/3D operations, and you want to contribute to the repo, we can think about adding together the 2D section to acme.

Greetings!

ppizarror commented 1 year ago

I think you are right... maybe we can address that in the future, I'll close this for now as the main goal must be a complete 3D Framework. Thanks!