简体   繁体   English

C++ - 如何通过将 x/y/z 增加给定值 x 来生成 n 个 3D 坐标的每个可能组合

[英]C++ - How to generate every possible combination of n 3D coordinates by incrementing x/y/z by a given value x

As part of a larger program I need to generate every possible set of 3D coordinate points contained within the rectangular prism formed by the origin and point (Y1, Y2, Y3), given the number of points, n, that will be in the set, and the value by which the x/y/z values are to be incremented by.作为更大程序的一部分,我需要生成包含在由原点和点(Y1,Y2,Y3)形成的直角棱镜内的所有可能的 3D 坐标点集,给定点数 n,将在集合中,以及 x/y/z 值要增加的值。

This was what I initially wrote, which does the job of cycling through all possible coordinates correctly for an individual point, but does not correctly generate all the overall combinations of points needed.这是我最初编写的内容,它可以为单个点正确循环所有可能的坐标,但不能正确生成所需的点的所有整体组合。

In the program I created a point object, and created a vector of point objects with default x/y/z values of zero.在程序中,我创建了一个点对象,并创建了一个点对象向量,默认 x/y/z 值为零。

void allPoints(double Y1, double Y2, double Y3, double increment, vector<Point> pointset)
{
int count = pointset.size()-1;

while (count>=0)
{
    while (pointset.at(count).getX()<Y1) 
    {
        while (pointset.at(count).getY()<Y2)
        {
            while (pointset.at(count).getZ()<Y3)
            {
                //insert intended statistical test to be run on each possible set here 
            }
            pointset.at(count).setZ(0);
            pointset.at(count).incY(increment);
        }
        pointset.at(count).setY(0);
        pointset.at(count).incX(increment);

    }
    count--;
}

}

I am new to coding and may be approaching this entirely wrong, and am just looking for help getting in the right direction.我是编码新手,可能完全错误地解决了这个问题,我只是在寻找朝着正确方向前进的帮助。 If using a point object isn't the way to go, it's not needed in the rest of the program - I could use 3d arrays instead.如果使用点对象不是可行的方法,则程序的其余部分不需要它 - 我可以使用 3d 数组代替。

Thanks!谢谢!

The chief problem appears to be that your textual description is about creating a pointset.主要问题似乎是您的文本描述是关于创建一个点集。 The count isn't known up front.计数是不知道的。 The example code takes an already created pointset.示例代码采用一个已经创建的点集。 That just doesn't work.那是行不通的。

That's also why you end up with the // insert test here - that's not the location for a test, that's where you would add a new point to the pointset you have to create.这也是为什么您最终// insert test here使用// insert test here - 这不是测试的位置,而是您将新点添加到您必须创建的点集的位置。

Lets assume you have class Point3d which represents a point, Vec3d which represents a vector which can translate points (proper operators are defined).让我们假设你有类Point3d代表一个点, Vec3d代表一个可以转换点的向量(定义了适当的运算符)。 In such case this should go like this:在这种情况下,这应该是这样的:

std::vector<Point3d> CrystalNet(
    size_t size,
    const Point3d& origin,
    const Vec3d& a = { 1, 0, 0 },
    const Vec3d& b = { 0, 1, 0 },
    const Vec3d& c = { 0, 0, 1 })
{
    std::vector<Point3d> result;
    result.reserve(size * size * size);

    for (int i = 0; i < size; ++i)
    for (int j = 0; j < size; ++j)
    for (int k = 0; k < size; ++k) {
         result.empalce_back(origin + a * i + b * j + c * k);
    }
    return result;
}

Defining Point3d and Vec3d is quite standard and I'm sure there is ready library which can do it.定义Point3dVec3d是非常标准的,我相信有现成的库可以做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM