【问题标题】:How can i use multimatrix in a loop in OpenSCAD?如何在 OpenSCAD 中循环使用多矩阵?
【发布时间】:2015-08-21 13:16:57
【问题描述】:

我希望将多维数据集端到端链接在一个循环内,这样我就可以仅使用它们如何添加在一起的动态规则而不是参数方程来构建由多维数据集组成的螺旋。即平移 1,倾斜 20',旋转 -z 10'...将制作一个 3d 螺旋图。

这是一个使用 openscad 递归的示例文件(与 openscad 循环相比,它的内存崩溃非常快)我希望使用循环来做同样的事情吗?我很困惑。

levels = 50; // number of levels for the recursion

len = 100; // length of the first segment
thickness = 5; // thickness of the first segment

// the identity matrix
identity = [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ];

// random generator
function rnd(s, e) = rands(s, e, 1)[0];

// generate 4x4 translation matrix
function mt(x, y) = [ [ 1, 0, 0, x ], [ 0, 1, 0, y ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ];

// generate 4x4 rotation matrix around Z axis
function mr(a) = [ [ cos(a), -sin(a), 0, 0 ], [ sin(a), cos(a), 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ];

module tree(length, thickness, count, m = identity) {
    color([0, 1 - (0.8 / levels * count), 0])
        multmatrix(m)
            cube([thickness, length, thickness]);

    if (count > 0) {
        tree( length, thickness, count - 1, m * mt(0, length) * mr(31.4159));

    }
}

tree(len, thickness, levels);

【问题讨论】:

    标签: openscad


    【解决方案1】:

    我们可以先在递归函数中生成矩阵列表 并使用 for() 模块中的结果来生成实际的几何图形。

    // the identity matrix 
    identity = [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]; 
    
    // generate 4x4 rotation matrix around Z axis 
    function mr(a) = [ [ cos(a), -sin(a), 0, 0 ], [ sin(a), cos(a), 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]; 
    
    // generate 4x4 translation matrix 
    function mt(x, y) = [ [ 1, 0, 0, x ], [ 0, 1, 0, y ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]; 
    
    function matrices(i = 10, m = identity, ret = []) = i >= 0 
            ? matrices(i - 1, m * mt(0, 10) * mr(20), concat(ret, [ m ])) 
            : ret; 
    
    for (m = matrices(17)) 
      multmatrix(m) 
        cube([5, 10, 5]); 
    

    在托尔斯滕·保罗的帮助下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多