Search code examples
c++geometrycomputational-geometry

How to convert spherical geometry to ellipsoid geometry c++ vertex generation


I have a question on how to generate ellipsoid vertex geometry in c++. I have the following code:

Sphere::Sphere(const LengthT radius, const LengthT a, const LengthT b, const LengthT c, , std::uint16_t sectorCount, std::uint16_t stackCount) :
    m_radius(radius),
    m_sectorCount(sectorCount),
    m_stackCount(stackCount) {
    const double sectorStep{2 * std::numbers::pi / m_sectorCount};
    const double stackStep{std::numbers::pi / m_stackCount};
    constexpr double half_pi{std::numbers::pi / 2};
    double xy, sectorAngle, stackAngle;
    const auto lengthInv{double(1) / m_radius};
    for(std::uint16_t i{0}; i <= stackCount; i++) {
        Vertex vertex;
        stackAngle = (half_pi) - (i * stackStep);
        xy = m_radius * cos(stackAngle);
        vertex.pos.z = m_radius * sin(stackAngle);
        for(std::uint16_t j{0}; j <= sectorCount; j++) {
            sectorAngle = j * sectorStep;
            vertex.pos.x = xy * cos(sectorAngle);
            vertex.pos.y = xy * sin(sectorAngle);
            vertex.normals.x = vertex.pos.x * lengthInv;
            vertex.normals.y = vertex.pos.y * lengthInv;
            vertex.normals.z = vertex.pos.z * lengthInv;
            vertex.texCords.x = double(j) / sectorCount;
            vertex.texCords.y = double(i) / stackCount;
            verticies.push_back(vertex);
        }
    }
}

This generates a list of spherical coordinates but I am wondering how to do it for the equation: ((x^2)/(a^2)+((y^2)/(b^2)+((z^2)/(c^2)=1 where a, b, and c are used to define the ellipse itself. I am wondering how to incorporate the a, b, and c into the generative equation

Update! I developed and tested a solution, it is selected as the answer!


Solution

  • According to the comments I think this will do in terms of: ((x^2)/(a^2)+((y^2)/(b^2)+((z^2)/(c^2)=1

    Ellipsoid::Ellipsoid(const double a, const double b, const double c, std::uint16_t sectorCount, std::uint16_t stackCount) :
        m_a(a),
        m_b(b),
        m_c(c),
        m_sectorCount(sectorCount),
        m_stackCount(stackCount) {
        const double sectorStep{2 * std::numbers::pi / m_sectorCount};
        const double stackStep{std::numbers::pi / m_stackCount};
        constexpr double half_pi{std::numbers::pi / 2};
    
        for(std::uint16_t i{0}; i <= stackCount; i++) {
            Vertex vertex;
            double stackAngle{(half_pi) - (i * stackStep)};
            double xy{m_b * cos(stackAngle)};
            vertex.pos.z = m_c * sin(stackAngle);
            for(std::uint16_t j{0}; j <= sectorCount; j++) {
                double sectorAngle{j * sectorStep};
                vertex.pos.x = m_a * xy * cos(sectorAngle);
                vertex.pos.y = m_a * xy * sin(sectorAngle);
                vertex.normals.x = 2 * vertex.pos.x / (m_a * m_a);
                vertex.normals.y = 2 * vertex.pos.y / (m_b * m_b);
                vertex.normals.z = 2 * vertex.pos.z / (m_c * m_c);
                vertex.texCords.x = double(j) / sectorCount;
                vertex.texCords.y = double(i) / stackCount;
                vertices.push_back(vertex);
            }
        }
    }
    

    I am in the process of testing it to make sure there are no errors, but I will post an update if I got something wrong...