处理中渲染极地带面体时出现问题

2024-05-09

我最近一直在研究 Zohedrons 和Rob Bell http://zomadic.com/做出了美丽的。我玩了免费的极地带面体 Sketchup 插件 http://zomebuilder.com/并考虑使用几何图形加工 http://processing.org/。到目前为止,我已经打开了插件/Ruby 脚本并尝试直接移植它,但我对 Ruby 没有经验,并且一直在使用SketchUp Ruby API 参考 https://developers.google.com/sketchup/docs/.

代码的几何部分主要在polar_zonohedron功能:

def polar_zonohedron #frequency, pitch = atan(sqrt(2)/2), len = 1.0 # frequency,pitch,length

  mo = Sketchup.active_model
  mo.start_operation "polar_zonohedron"

  prompts = ["Frequency", "Pitch in radians", "Length"]
  values = [8, "atan( sqrt(2)/2 )", 12.inch]
  results = inputbox prompts, values, "Polar Zonohedron"

  return if not results # This means that the user canceld the operation

  ents = mo.active_entities
  grp = ents.add_group
  ents = grp.entities

  grp.frequency = results[0]
  grp.pitch = eval( results[1] )
  grp.length = results[2]

  pts=[]

  #we begin by setting pts[0] to the origin
  pts[0] = Geom::Point3d.new(0,0,0)

  vector = Geom::Vector3d.new(cos(grp.pitch),0,sin(grp.pitch) ) #tilt pitch vector up the xz plane
  vector.length = grp.length

  #Using the origin as the initial generator we iterate thru each zone of the zonohedron
  #our first task is to define the four points of the base rhomb for this zone
  #at the end the pts[3] becomes our new origin for the rhomb of the next zone
  1.upto(grp.frequency-1){ |i| 
    p_rotate = Geom::Transformation.rotation( pts[0] , Geom::Vector3d.new(0,0,1), i*2*PI/grp.frequency )

    #obtain the other three points of the rhomb face
    pts[1] = pts[0].transform vector
    pts[3] = pts[1].transform( p_rotate )
    pts[2] = pts[3].transform( vector )

    #we now have the 4 points which make this zone's base rhomb
    #so we rotate around the origin frequency times making a star pattern of faces
    0.upto(grp.frequency-1){ |j| 
      f_rotate = Geom::Transformation.rotation( Geom::Point3d.new(0,0,0) , Geom::Vector3d.new(0,0,1), j*2*PI/grp.frequency )     
      ents.add_face( pts.collect{|p| p.transform(f_rotate)} )
    }

    #set the origin for the rhomb of the next zone
    pts[0] = pts[3]
  }

  mo.commit_operation
end

我已经理解了循环,但对转换有点困惑:

pts[1] = pts[0].transform vector
pts[3] = pts[1].transform( p_rotate )
pts[2] = pts[3].transform( vector )

据我所知pts[1]是向量加法pts[0] and vector, and pts[3] is pts[1]乘以p_rotate旋转矩阵。会pts[2]也可以是一个添加(之间pts[3] and vector )?

到目前为止,我的尝试如下:

//a port attempt of Rob Bell's polar_zonohedron.rb script - http://zomebuilder.com/

int frequency = 3;
float pitch   = atan(sqrt(2)/2);
float length  = 24;

ArrayList<Face> faces = new ArrayList<Face>(); 

void setup(){
  size(400,400,P3D);
  strokeWeight(3);
  setupZome();
}
void setupZome(){
  faces.clear();
  PVector[] pts = new PVector[4];
  pts[0] = new PVector();

  PVector vector = new PVector(cos(pitch),0,sin(pitch));
  vector.mult(length);

  for(int i = 1 ; i < frequency; i++){
    PMatrix3D p_rotate = new PMatrix3D();
    p_rotate.rotate(i * TWO_PI / frequency,  0,0,1);
    //PVector v = new PVector();
    //p_rotate.mult(pts[0],v);
    //pts[0] = v;

    pts[1] = PVector.add(pts[0],vector);
    pts[3] = new PVector();
    p_rotate.mult(pts[1],pts[3]);
    pts[2] = PVector.add(pts[3],vector);

    for(int j = 0; j < frequency; j++){
      PMatrix3D f_rotate = new PMatrix3D();
      f_rotate.rotate(j*2*PI/frequency , 0,0,1);

      Face f = new Face();
      for(PVector pt : pts){
        PVector p = new PVector();
        f_rotate.mult(pt,p);
        f.add(p.get());
      }
      faces.add(f);
    }

    pts[0] = pts[3];
  } 
}
void draw(){
  background(255);
  lights();

  translate(width * .5, height * .5,0);
  rotateY(map(mouseX,0,width,-PI,PI));
  rotateX(map(mouseY,0,height,-PI,PI));
  drawAxes(100);
  pushMatrix();
  translate(0,0,-frequency * length * .25);
  for(Face f : faces){
    beginShape(mousePressed ? QUADS : POINTS);
      for(PVector p : f.pts) vertex(p.x,p.y,p.z);
    endShape();
  }
  popMatrix(); 
}
void keyPressed(){
  if(keyCode == UP  && frequency < 32) frequency++;
  if(keyCode == DOWN && frequency > 2) frequency--;
  setupZome(); 
}
void drawAxes(int size){
  stroke(192,0,0);
  line(0,0,0,size,0,0);
  stroke(0,192,0);
  line(0,0,0,0,size,0);
  stroke(0,0,192);  
  line(0,0,0,0,0,size);
}
class Face{
  ArrayList<PVector> pts = new ArrayList<PVector>();
  Face(){}
  void add(PVector p){
    if(pts.size() <= 4) pts.add(p);
  }
}

我觉得我已经很接近了,但我的循环条件和顶点索引是错误的。 关于如何解决这个问题有什么建议吗?


我非常接近,但没有注意所有细节。 事实证明,如果我不增加旋转,我会得到正确的网格p_rotate:

p_rotate.rotate(TWO_PI / frequency,  0,0,1);

代替

p_rotate.rotate(i * TWO_PI / frequency,  0,0,1);

这是完整的代码清单:

//a port attempt of Rob Bell's polar_zonohedron.rb script - http://zomebuilder.com/

int frequency = 3;
float pitch   = atan(sqrt(2)/2);
float length  = 24;

ArrayList<Face> faces = new ArrayList<Face>(); 

void setup(){
  size(400,400,P3D);
  strokeWeight(3);
  setupZome();
}
void setupZome(){
  faces.clear();
  PVector[] pts = new PVector[4];
  pts[0] = new PVector();

  PVector vector = new PVector(cos(pitch),0,sin(pitch));
  vector.mult(length);

  for(int i = 1 ; i < frequency-1; i++){
    PMatrix3D p_rotate = new PMatrix3D();
    p_rotate.rotate(TWO_PI / frequency,  0,0,1);

    pts[1] = PVector.add(pts[0],vector);
    pts[3] = new PVector();
    p_rotate.mult(pts[1],pts[3]);
    pts[2] = PVector.add(pts[3],vector);

    for(int j = 0; j < frequency; j++){
      PMatrix3D f_rotate = new PMatrix3D();
      f_rotate.rotate(j*2*PI/frequency , 0,0,1);

      Face f = new Face();
      for(PVector pt : pts){
        PVector p = new PVector();
        f_rotate.mult(pt,p);
        f.add(p.get());
      }
      faces.add(f);
    }

    pts[0] = pts[3];
  } 
}
void draw(){
  background(255);
  lights();

  translate(width * .5, height * .5,0);
  rotateY(map(mouseX,0,width,-PI,PI));
  rotateX(map(mouseY,0,height,-PI,PI));
  drawAxes(100);
  pushMatrix();
  translate(0,0,-frequency * length * .25);
  for(Face f : faces){
    beginShape(mousePressed ? QUADS : POINTS);
      for(PVector p : f.pts) vertex(p.x,p.y,p.z);
    endShape();
  }
  popMatrix(); 
}
void keyPressed(){
  if(keyCode == UP  && frequency < 32) frequency++;
  if(keyCode == DOWN && frequency > 3) frequency--;
  setupZome(); 
}
void drawAxes(int size){
  stroke(192,0,0);
  line(0,0,0,size,0,0);
  stroke(0,192,0);
  line(0,0,0,0,size,0);
  stroke(0,0,192);  
  line(0,0,0,0,0,size);
}
class Face{
  ArrayList<PVector> pts = new ArrayList<PVector>();
  Face(){}
  void add(PVector p){
    if(pts.size() <= 4) pts.add(p);
  }
}

这里有一些屏幕截图:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

处理中渲染极地带面体时出现问题 的相关文章

  • 我在 Rails 中使用了保留字吗?

    这是我的模型 class Record lt ActiveRecord Base belongs to user belongs to directory end class Directory lt ActiveRecord Base h
  • Rails:named_scope、lambda 和块

    我认为以下两个是等效的 named scope admin lambda company id conditions gt company id company id named scope admin lambda do company
  • 用户未定义的方法 attr_accessible 错误

    我正在尝试创建某种登录 我创建了一个用户脚手架并将此代码放在我的 user rb 中 class User lt ActiveRecord Base attr accessible name password digest password
  • 有 JavaScript 的微积分库吗? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有人知道 JavaScript 的微积分库吗 我做了一些谷歌搜索 但没有想出任何东西 我申请了 Wolf
  • ruby从1.8.7升级到1.9.2(使用Rails 3.1.1)后本地服务器错误

    我刚刚安装了rvm并使用rvm将ruby从1 8 7升级到1 9 2 我在我的应用程序上运行了捆绑安装 它重新安装了我的 gems 当我在本地运行 Rails 服务器并将浏览器导航到 localhost 3000 时 服务器日志中显示以下错
  • Shoulda/RSpec 匹配器 - 条件验证

    在我的代码中 我使用 Shoulda 匹配器进行了以下验证 效果很好 it should validate presence of name 在我的模型中 我已将条件添加到验证中 validates presence of name if
  • 带有 OAuth2 的 YouTube API v3:更新和删除失败并出现“权限不足”错误

    我在尝试着update and delete视频使用YouTube API v3 https developers google com youtube v3 docs videos with OAuth2 用于身份验证 https dev
  • 正则表达式的 o 修饰符是什么意思?

    Ruby 正则表达式有一些选项 例如i x m o i例如 意味着忽略大小写 什么是o选项是什么意思 在ri Regexp 它说o意味着执行 仅插值一次 但是当我这样做时 a one b a a two b不改变 它保持 one 我缺少什么
  • 使用按位运算符相乘

    我想知道如何使用按位运算符将一系列二进制位相乘 但是 我有兴趣这样做来查找二进制值的十进制小数值 这是我正在尝试做的一个例子 假设 1010010 我想使用每个单独的位 以便将其计算为 1 2 1 0 2 2 1 2 3 0 2 4 虽然我
  • 如何舍入、取整、取整、截断

    如何对 jq jq 1 5 1 a5b5cbe 中的数字进行舍入 取整 取整和截断 例如 与 mass 188 72 我想 mass 188 有地板 mass 189 与天花板和圆形 舍入示例 5 52 gt 6 5 50 gt 5 or
  • 如何声明依赖于参数化任务的 Rake 任务?

    我见过一些任务具有参数和依赖项任务的示例 例如 task name first name last name gt pre name do t args args with defaults first name gt John last
  • 矩阵乘法 - 视图/投影、世界/投影等

    在 HLSL 中有很多矩阵乘法 虽然我了解如何以及在何处使用它们 但我不确定它们是如何导出的或它们的实际目标是什么 所以我想知道是否有在线资源可以解释这一点 我特别好奇将世界矩阵乘以视图矩阵以及世界 视图矩阵乘以投影矩阵背后的目的是什么 您
  • rake db 问题:迁移 -

    我无法为 Ruby on Rails 设置 MySQL 数据库 设置数据库并确保 config database yml 文件匹配后 我遇到了以下错误消息 U Rails alpha gt rake db migrate trace in
  • 如何在 mongoid 中使用 or 条件进行查询

    如何在 Mongoid 中使用 or 条件进行查询 这是 OR 在 mongoid 中查询 如果你想要像下面这样的查询 select from user where id 10 or name hitesh 在带有 mongoid 的 Ra
  • Ruby:如何在不创建新实例的情况下检查实例方法的参数?

    在 Ruby 1 9 2 中 您可以使用 method symbol 检查任何方法的参数 如何在不创建新车的情况下检查 Car initialize 方法 class Car def initialize fuel type passeng
  • 从 Ruby on Rails 应用程序运行 phantomjs

    我有兴趣使用幻影 http www phantomjs org 我想从我的 Ruby on Rails 应用程序运行它 然而 这是一个命令行工具 即我需要运行类似phantomjs rasterize js http raphaeljs c
  • Ruby:用于检查 nil /false 条件语句的干净代码?

    我总是遇到这个Ruby问题 我想写得更干净 var a can be nil a value can also be nil a value has possible true or false value if not a nil not
  • 如何确保整数除法始终向上舍入?

    我想确保如有必要 整数除法总是向上舍入 还有比这更好的方法吗 目前正在进行大量选角工作 int Math Ceiling double myInt1 myInt2 更新 这个问题是我2013年1月博客的主题 http ericlippert
  • 将名称字符串编码为唯一的数字

    我有一大堆名字 数以百万计 他们每个人都有一个名字 一个可选的中间名和一个姓氏 我需要将这些名称编码为唯一代表这些名称的数字 编码应该是一对一的 即一个名称只能与一个数字相关联 一个数字只能与一个名称相关联 对此进行编码的明智方法是什么 我
  • 在 2D 中将一个点旋转另一个点

    我想知道当一个点相对于另一个点旋转一定角度时如何计算出新的坐标 我有一个块箭头 想要将其相对于箭头底部中间的点旋转角度 theta 这是允许我在两个屏幕控件之间绘制多边形所必需的 我无法使用和旋转图像 从我到目前为止所考虑的情况来看 使问题

随机推荐