gpt4 book ai didi

STL - 如何将函数作为参数传递给 OpenSCAD 模块?

转载 作者:行者123 更新时间:2023-12-04 17:43:55 33 4
gpt4 key购买 nike

在过去的几天里,我对使用基于编程语言的软件创建 3D 模型的想法产生了兴趣。我一直在玩的一种语言是 OpenSCAD,事实证明它对创建有趣的形状非常有帮助。

我目前正在尝试使用 OpenSCAD 创建一朵花,我遇到了一个问题,我无法使用我在网上找到的文档或其他资源来规避。

这里是问题的简短形式:

我可以将函数作为参数传递给 OpenSCAD 模块吗?

如果可以,怎么做?如果没有,为什么不,我可以做什么?

这让我想到了问题的长篇形式,并针对我的情况进行了详细说明:

我正在尝试使用 2D 极坐标函数的线性拉伸(stretch)来创建花瓣,并将其与 3D 函数相交。

为此,我从在 http://spolearninglab.com/curriculum/lessonPlans/hacking/resources/software/3d/openscad/openscad_math.html 上找到的两个非常好的模块开始.我并没有声称自己一开始就写了它们。

首先 - Dan Newman 的 3D 绘图仪/* 3Dplot.scad */

// 3dplot -- the 3d surface generator    
// x_range -- 2-tuple [x_min, x_max], the minimum and maximum x values
// y_range -- 2-tuple [y_min, y_max], the minimum and maximum y values

// grid -- 2-tuple [grid_x, grid_y] indicating the number of grid cells along the x and y axes

// z_min -- Minimum expected z-value; used to bound the underside of the surface

// dims -- 2-tuple [x_length, y_length], the physical dimensions in millimeters

//Want to pass in z(x,y) as parameter

module 3dplot(x_range=[-10, +10], y_range=[-10,10], grid=[50,50], z_min=-5, dims=[80,80]){
dx = ( x_range[1] - x_range[0] ) / grid[0];
dy = ( y_range[1] - y_range[0] ) / grid[1];

// The translation moves the object so that its center is at (x,y)=(0,0)
// and the underside rests on the plane z=0

scale([dims[0]/(max(x_range[1],x_range[0])-min(x_range[0],x_range[1])),
dims[1]/(max(y_range[1],y_range[0])-min(y_range[0],y_range[1])),1])
translate([-(x_range[0]+x_range[1])/2, -(y_range[0]+y_range[1])/2, -z_min])
union()
{
for ( x = [x_range[0] : dx : x_range[1]] )
{
for ( y = [y_range[0] : dy : y_range[1]] )
{
polyhedron(points=[[x,y,z_min], [x+dx,y,z_min], [x,y,z(x,y)], [x+dx,y,z(x+dx,y)],
[x+dx,y+dy,z_min], [x+dx,y+dy,z(x+dx,y+dy)]],
faces=prism_faces_1);
polyhedron(points=[[x,y,z_min], [x,y,z(x,y)], [x,y+dy,z_min], [x+dx,y+dy,z_min],
[x,y+dy,z(x,y+dy)], [x+dx,y+dy,z(x+dx,y+dy)]],
faces=prism_faces_2);
}
}
}
}

第二个 - 2D Grapher/* 2dgraphing.scad */

// function to convert degrees to radians
function d2r(theta) = theta*360/(2*pi);

// These functions are here to help get the slope of each segment, and use that to find points for a correctly oriented polygon
function diffx(x1, y1, x2, y2, th) = cos(atan((y2-y1)/(x2-x1)) + 90)*(th/2);
function diffy(x1, y1, x2, y2, th) = sin(atan((y2-y1)/(x2-x1)) + 90)*(th/2);
function point1(x1, y1, x2, y2, th) = [x1-diffx(x1, y1, x2, y2, th), y1-diffy(x1, y1, x2, y2, th)];
function point2(x1, y1, x2, y2, th) = [x2-diffx(x1, y1, x2, y2, th), y2-diffy(x1, y1, x2, y2, th)];
function point3(x1, y1, x2, y2, th) = [x2+diffx(x1, y1, x2, y2, th), y2+diffy(x1, y1, x2, y2, th)];
function point4(x1, y1, x2, y2, th) = [x1+diffx(x1, y1, x2, y2, th), y1+diffy(x1, y1, x2, y2, th)];
function polarX(theta) = cos(theta)*r(theta);
function polarY(theta) = sin(theta)*r(theta);

module nextPolygon(x1, y1, x2, y2, x3, y3, th) {
if((x2 > x1 && x2-diffx(x2, y2, x3, y3, th) < x2-diffx(x1, y1, x2, y2, th) || (x2 <= x1 && x2-diffx(x2, y2, x3, y3, th) > x2-diffx(x1, y1, x2, y2, th)))) {
polygon(
points = [
point1(x1, y1, x2, y2, th),
point2(x1, y1, x2, y2, th),
// This point connects this segment to the next
point4(x2, y2, x3, y3, th),
point3(x1, y1, x2, y2, th),
point4(x1, y1, x2, y2, th)
],
paths = [[0,1,2,3,4]]
);
}
else if((x2 > x1 && x2-diffx(x2, y2, x3, y3, th) > x2-diffx(x1, y1, x2, y2, th) || (x2 <= x1 && x2-diffx(x2, y2, x3, y3, th) < x2-diffx(x1, y1, x2, y2, th)))) {
polygon(
points = [
point1(x1, y1, x2, y2, th),
point2(x1, y1, x2, y2, th),
// This point connects this segment to the next
point1(x2, y2, x3, y3, th),
point3(x1, y1, x2, y2, th),
point4(x1, y1, x2, y2, th)
],
paths = [[0,1,2,3,4]]
);
}
else {
polygon(
points = [
point1(x1, y1, x2, y2, th),
point2(x1, y1, x2, y2, th),
point3(x1, y1, x2, y2, th),
point4(x1, y1, x2, y2, th)
],
paths = [[0,1,2,3]]
);
}
}

module 2dgraph(bounds=[-10,10], th=2, steps=10, polar=false, parametric=false) {

step = (bounds[1]-bounds[0])/steps;
union() {
for(i = [bounds[0]:step:bounds[1]-step]) {
if(polar) {
nextPolygon(polarX(i), polarY(i), polarX(i+step), polarY(i+step), polarX(i+2*step), polarY(i+2*step), th);
}
else if(parametric) {
nextPolygon(x(i), y(i), x(i+step), y(i+step), x(i+2*step), y(i+2*step), th);
}
else {
nextPolygon(i, f(i), i+step, f(i+step), i+2*step, f(i+2*step), th);
}
}
}
}

我的包装代码:

include <2dgraphing.scad>;
include <3dplot.scad>;

function z(x,y) = pow(x,2)+pow(y,2); //function used in 3dplot
function r(theta) = cos(4*theta); //function used in 2dgraph

module Petals () {
difference () {
union () { //everything to add
intersection () {
3dplot([-4,4],[-4,4],[50,50],-2.5);
scale([20, 20, 20]) linear_extrude(height=0.35)
2dgraph([0, 720], 0.1, steps=160, polar=true);
}
}
union () { //everything to subtract

}
}

}

Petals();

当我渲染世界上计算成本最高的花瓣时,世界一切都很好,花花公子。

[在这里我会发布一张图片,但由于这是我的第一篇文章,我没有先决条件 10 点声望]

但是,现在我想从花瓣底部减去多余部分。因此,我可以使用具有更陡函数和更低起点的 3D 图,并从原始 3D 图中减去它。

所以在同一个程序中,我想使用两个不同的函数来实现 3Dplot 模块的两种不同用途。

我尝试修改 3dplot 和我的代码来这样做:

Modified 3dplot:

module 3dplot(x_range=[-10, +10], y_range=[-10,10], grid=[50,50], z_min=-5, dims=[80,80], input_function)
{
dx = ( x_range[1] - x_range[0] ) / grid[0];
dy = ( y_range[1] - y_range[0] ) / grid[1];

// The translation moves the object so that its center is at (x,y)=(0,0)
// and the underside rests on the plane z=0

scale([dims[0]/(max(x_range[1],x_range[0])-min(x_range[0],x_range[1])),
dims[1]/(max(y_range[1],y_range[0])-min(y_range[0],y_range[1])),1])
translate([-(x_range[0]+x_range[1])/2, -(y_range[0]+y_range[1])/2, -z_min])
union()
{
for ( x = [x_range[0] : dx : x_range[1]] )
{
for ( y = [y_range[0] : dy : y_range[1]] )
{
polyhedron(points=[[x,y,z_min], [x+dx,y,z_min], [x,y,input_function(x,y)], [x+dx,y,input_function(x+dx,y)],
[x+dx,y+dy,z_min], [x+dx,y+dy,input_function(x+dx,y+dy)]],
faces=prism_faces_1);
polyhedron(points=[[x,y,z_min], [x,y,input_function(x,y)], [x,y+dy,z_min], [x+dx,y+dy,z_min],
[x,y+dy,input_function(x,y+dy)], [x+dx,y+dy,input_function(x+dx,y+dy)]],
faces=prism_faces_2);
}
}
}
}

修改了我的代码:

include <2dgraphing.scad>;
include <3dplot.scad>;

function z1(x,y) = pow(x,2)+pow(y,2); //function used in 3dplot
function z2(x,y) = pow(pow(x,2)+pow(y,2),1.5)-1; //function to be subtracted out
function r(theta) = cos(4*theta); //function used in 2dgraph

module Petals () {
difference () {
union () { //everything to add
intersection () {
3dplot([-4,4],[-4,4],[50,50],-2.5);
scale([20, 20, 20]) linear_extrude(height=0.35)
2dgraph([0, 720], 0.1, steps=160, polar=true, input_function=z1);
}
}
union () { //everything to subtract
3dplot([-4,4],[-4,4],[50,50],-2.5,input_function=z2);
}
}

}

Petals();

我收到以下错误:警告:忽略未知函数“input_function”。

那么我该如何着手将函数作为参数传递呢?

在此之前我没有用任何函数式语言编写过,但我从 OpenSCAD 用户手册中了解到“从 2015.03 版开始,现在可以在任何范围内分配变量。”所以我应该能够为 3dplot 的每次运行更改 input_function 的值,就像 3dplot 中的变量一样。我是不是理解错了?

作为一个可选的附带问题:OpenSCAD 是否有明确的方法来实现我当前的目标,而不会在渲染过程中产生大量计算负载?

我已经花了足够多的时间来尝试解决这个问题,所以我发布了这个冗长的问题,如果我对一个简单的现有解决方案感到困惑,我深表歉意。我非常感谢任何愿意提供帮助的人。

最佳答案

目前无法将函数作为参数传递。同时生成大量小对象(例如 3dplot 模块中的多面体)会使模型渲染非常慢。为了这特定用例还有其他选项来生成模型。

最新的 OpenSCAD 版本提供的新列表生成功能允许基于函数生成单个多面体。

参见 3d-functions.scad在演示存储库中。这plots这函数 f(x, y)。

关于STL - 如何将函数作为参数传递给 OpenSCAD 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34275683/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com