0
$\begingroup$

I have the following problem I am currently facing.

I am creating the vertices for a cone based on the number of segments and the radius of the base and top.

This is done as follows:

#include <cmath>
#include <fstream>
#include <numbers>
#include <iostream>
#include <fmt/core.h>

struct Vec3D
{
    double x, y, z;
    Vec3D(double x, double y, double z) : x(x), y(y), z(z) {}
};

int main()
{
    int n = 4;       // number of segments
    double r1 = 0.8; // radius of the base, assume meters
    double r2 = 1.0; // radius of the top, assume meters
    double h = 1.0;  // height of the cone, assume meters

    auto get_coordinates = [&n](int32_t vertex_id, double radius, double height) {
        double theta = vertex_id * 2 * std::numbers::pi / n;
        double x = radius * cos(theta);
        double y = radius * sin(theta);
        double z = height;

        return Vec3D(x, y, z);
    };

    for (int i = 0; i < n; i++)
    {
        Vec3D pt = get_coordinates(i, r1, 0.0);
        std::cout << fmt::format("v {} {} {}\n", pt.x, pt.y, pt.z);
        pt = get_coordinates(i, r1, h);
        std::cout << fmt::format("v {} {} {}\n", pt.x, pt.y, pt.z);
    }
}

(godbolt: https://godbolt.org/z/njdxznj9c)

Which works perfectly fine, but now I want to automatically create a UV map, which of course in a cylinder case can be very straight forward by just distributing the uv coordinates for the top vertices between [0, 1] for u and v = 1.0, and for the bottom the same with v = 0.0.

Now in a cone case this would lead to texture distorting.. which I want to avoid and therfore find an arc-like mapping, such that if the printed texture has the correct ratios and sizes (to the actual cup) you can wrap it without distortion, basically the same as blender can do

Blender UV Unwrapping

Assuming now only calculating the following points a, b, c, d my plan was that those points should be possible to calculate and from there on use some sort-of arc parametrisation based on the radii to calculate the other points. points a and b can be calculated under the assumptions of the coordinates that it is in relation to the ratio of the 2 cone radii.

But I am not really sure how to follow up on that.

Sketch of uv map

I'm glad for any help on that.

$\endgroup$

1 Answer 1

0
$\begingroup$

Consider a circular sector of radius R equal to the apothem of the whole cone, and angular aperture 2α such that 2αR = 2πr, where r is the radius of the large base.

Subdivide the (truncated) sector in N faces. The coordinates are easily computed from the polar system.

enter image description here

Now you can scale and translate so that the bounding box (found from four extreme points) goes where you want. Note that anisotropic scaling will stretch the texture.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.