在过去的几天里,我对使用基于编程语言的软件创建3D模型的想法感兴趣。我一直在玩的语言之一是OpenSCAD,它已被证明对创造有趣的形状非常有帮助。如何将函数作为参数传递给OpenSCAD模块?
我目前正试图用OpenSCAD创建一朵花,而且我遇到了一个问题,我无法绕过使用我在网上找到的文档或其他资源。
这是问题的简短形式:
我可以通过一个函数作为参数传递给了OpenSCAD模块?
如果是这样,怎么样?如果没有,为什么不能,我能做些什么呢?
这使我与具体到我的情况下,问题的长形:
我试图创建一个使用二维极性功能的线性挤压花瓣,相交,与具有3D功能。
为此,我开始在http://spolearninglab.com/curriculum/lessonPlans/hacking/resources/software/3d/openscad/openscad_math.html上找到两个非常好的模块。我并不是说他们首先写了他们。
首先 - 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绘图器 /* * 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有没有一种清晰的方式来实现我目前的目标,而不会在渲染过程中产生大量的计算负担?
我花了足够多的时间试图解决这个问题,我发布了这个冗长的问题,如果我已经看到了一个简单的现有解决方案,我很抱歉。我非常感谢任何愿意帮助的人。