Skip to content

glm/gtx/polar_coordinates: polar function computes distance using normalized vector to position #1442

Description

@papasitopapi

Current implementation:

	template<typename T, qualifier Q>
	GLM_FUNC_QUALIFIER vec<3, T, Q> polar
	(
		vec<3, T, Q> const& euclidean
	)
	{
		T const Length(length(euclidean));
		vec<3, T, Q> const tmp(euclidean / Length);
		T const xz_dist(sqrt(tmp.x * tmp.x + tmp.z * tmp.z));

		return vec<3, T, Q>(
			asin(tmp.y),	// latitude
			atan(tmp.x, tmp.z),		// longitude
			xz_dist);				// xz distance
	}

The third coordinate is calculated using the normalized tmp vector, which makes glm::polar(vec3) map all points situated on the same direction from the origin to the same polar coordinates, regardless of distance.

#include <glm/gtx/polar_coordinates.hpp>
#include <glm/gtx/string_cast.hpp>

#include <iostream>

int main() {
    glm::vec3 position1 = {1.0f, 2.0f, 3.0f};
    glm::vec3 position2 = 100.0f * position1;

    std::cout << glm::to_string(glm::polar(position1)) << std::endl;
    std::cout << glm::to_string(glm::polar(position2)) << std::endl;
}

Output:

vec3(0.563943, 0.321751, 0.845154)
vec3(0.563943, 0.321751, 0.845154)

Also I'm not sure what xz_distance is supposed to be, but I find it bizarre to use that instead of distance to origin.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions