在处理中渲染极地正二十面体时遇到问题 [英] Problems rendering a Polar Zonohedron in Processing

查看:98
本文介绍了在处理中渲染极地正二十面体时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究Zonohedrons,而罗伯·贝尔做的很漂亮.我玩过免费的 Polar Zonohedron Sketchup插件,并考虑过使用

I've recently been looking into Zonohedrons and Rob Bell made beautiful ones. I had a play with the free Polar Zonohedron Sketchup Plugin and thought about playing with the geometry using Processing. So far I've open up the plugin/Ruby script and tried to port it directly, but I am not experienced with Ruby and have been using the Sketchup Ruby API reference.

代码的几何部分大部分在polar_zonohedron函数中:

The geometry part of the code is mostly in the polar_zonohedron function:

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

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

I've understood the loops but am slightly confused by transforms:

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

据我所知pts[1]pts[0]vector的媒介成瘾, pts[3]pts[1]乘以p_rotate旋转矩阵. pts[2]还会加(在pts[3]vector之间)吗?

As far as I can tell pts[1] is the vector addiction of pts[0] and vector, and pts[3] is pts[1] multiplied by the p_rotate rotation matrix. Would pts[2] also be an addition (between pts[3] and vector )?

这是到目前为止我的尝试:

Here's what my attempt looks like so far:

//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);
  }
}

我觉得我已经接近了,但是我弄错了循环条件和顶点索引. 有关如何解决此问题的任何提示?

I feel I'm close, but I'm getting the loop conditionals and vertex indices wrong. Any tips on how to fix this?

推荐答案

我非常亲密,但是没有注意所有细节. 事实证明,如果不增加p_rotate上的旋转,我将获得正确的网格:

I was very close, but not paying attention to all the details. Turns out I get the correct mesh if I don't increment the rotation on 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);
  }
}

还有一些屏幕截图:

"> >

这篇关于在处理中渲染极地正二十面体时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆