首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数学java-处理arcball四元数trig,浮点舍入误差

数学java-处理arcball四元数trig,浮点舍入误差
EN

Stack Overflow用户
提问于 2021-01-10 03:46:53
回答 1查看 78关注 0票数 3

我正在修改一个arcball类,使其在每次调用rollforward()时都旋转1度。我在阅读代码时遇到了困难,但我认为我需要编写一个替代XY_to_sphere()的方法,让Point1= v1,Point2= v2,这样就可以

代码语言:javascript
复制
pi/180 = (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z) / (abs(v1.x) * abs(v2.x) + abs(v1.y) * abs(v2.y) + abs(v1.z) * abs(v2.z))

<-更新编辑->

我尝试简单地添加一个度数((sin(PI/180)*/2)半径转换为y轴;(在rollforward()中)

代码语言:javascript
复制
v_drag = XY_to_sphere(center_x, center_y - ((sin(PI/180) * radius))/2);

经过100次旋转后,它会偏离几度(这可能是一个浮点舍入误差,我会试着找到它,但我必须学习一些东西)这里是200全旋转时的差异(应该是顶部立方体的镜像)

(新问题)当它达到完全旋转时,它消失了大约1度,我不知道为什么。(解决了!我在测试时将半径设置为1,感谢@laancelot)

我不确定我最初的想法,上面的解决方案是否会更好地工作

现在,我将尝试设置它,使其在达到360度后重置,以避免浮点错误

arcball类

代码语言:javascript
复制
// Ariel and V3ga's arcball class with a couple tiny mods by Robert Hodgin and smaller mods by cubes

class Arcball {
  float center_x, center_y, radius;
  Vec3 v_down, v_drag;
  Quat q_now, q_down, q_drag;
  Vec3[] axisSet;
  int axis;
  float mxv, myv;
  float x, y;
  
  Arcball(float center_x, float center_y, float radius){
    this.center_x = center_x;
    this.center_y = center_y;
    this.radius = radius;

    v_down = new Vec3();
    v_drag = new Vec3();

    q_now = new Quat();
    q_down = new Quat();
    q_drag = new Quat();

    axisSet = new Vec3[] {new Vec3(1.0f, 0.0f, 0.0f), new Vec3(0.0f, 1.0f, 0.0f), new Vec3(0.0f, 0.0f, 1.0f)};
    axis = -1;  // no constraints...    
  }

  void rollforward(){
    q_down.set(q_now);
    v_down = XY_to_sphere(center_x, center_y);
    q_down.set(q_now);
    q_drag.reset();
    
    v_drag = XY_to_sphere(center_x, center_y - ((sin(PI/180) * radius))/2);
    q_drag.set(Vec3.dot(v_down, v_drag), Vec3.cross(v_down, v_drag)); 
  }

/*
  void mousePressed(){
    v_down = XY_to_sphere(mouseX, mouseY);  
    q_down.set(q_now);
    q_drag.reset();
  }

  void mouseDragged(){
    v_drag = XY_to_sphere(mouseX, mouseY);
    q_drag.set(Vec3.dot(v_down, v_drag), Vec3.cross(v_down, v_drag));
  }
*/
  void run(){
    q_now = Quat.mul(q_drag, q_down);
    applyQuat2Matrix(q_now);
    
    x += mxv;
    y += myv;
    mxv -= mxv * .01;
    myv -= myv * .01;
  }
  
  Vec3 XY_to_sphere(float x, float y){
    Vec3 v = new Vec3();
    v.x = (x - center_x) / radius;
    v.y = (y - center_y) / radius;

    float mag = v.x * v.x + v.y * v.y;
    if (mag > 1.0f){
      v.normalize();
    } else {
      v.z = sqrt(1.0f - mag);
    }

    return (axis == -1) ? v : constrain_vector(v, axisSet[axis]);
  }

  Vec3 constrain_vector(Vec3 vector, Vec3 axis){
    Vec3 res = new Vec3();
    res.sub(vector, Vec3.mul(axis, Vec3.dot(axis, vector)));
    res.normalize();
    return res;
  }

  void applyQuat2Matrix(Quat q){
    // instead of transforming q into a matrix and applying it...

    float[] aa = q.getValue();
    rotate(aa[0], aa[1], aa[2], aa[3]);
  }
}

static class Vec3{
  float x, y, z;

  Vec3(){
  }

  Vec3(float x, float y, float z){
    this.x = x;
    this.y = y;
    this.z = z;
  }

  void normalize(){
    float length = length();
    x /= length;
    y /= length;
    z /= length;
  }

  float length(){
    return (float) Math.sqrt(x * x + y * y + z * z);
  }

  static Vec3 cross(Vec3 v1, Vec3 v2){
    Vec3 res = new Vec3();
    res.x = v1.y * v2.z - v1.z * v2.y;
    res.y = v1.z * v2.x - v1.x * v2.z;
    res.z = v1.x * v2.y - v1.y * v2.x;
    return res;
  }

  static float dot(Vec3 v1, Vec3 v2){
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
  }
  
  static Vec3 mul(Vec3 v, float d){
    Vec3 res = new Vec3();
    res.x = v.x * d;
    res.y = v.y * d;
    res.z = v.z * d;
    return res;
  }

  void sub(Vec3 v1, Vec3 v2){
    x = v1.x - v2.x;
    y = v1.y - v2.y;
    z = v1.z - v2.z;
  }
}

static class Quat{
  float w, x, y, z;

  Quat(){
    reset();
  }

  Quat(float w, float x, float y, float z){
    this.w = w;
    this.x = x;
    this.y = y;
    this.z = z;
  }

  void reset(){
    w = 1.0f;
    x = 0.0f;
    y = 0.0f;
    z = 0.0f;
  }

  void set(float w, Vec3 v){
    this.w = w;
    x = v.x;
    y = v.y;
    z = v.z;
  }

  void set(Quat q){
    w = q.w;
    x = q.x;
    y = q.y;
    z = q.z;
  }

  static Quat mul(Quat q1, Quat q2){
    Quat res = new Quat();
    res.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
    res.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
    res.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
    res.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
    return res;
  }
  
  float[] getValue(){
    // transforming this quat into an angle and an axis vector...

    float[] res = new float[4];

    float sa = (float) Math.sqrt(1.0f - w * w);
    if (sa < EPSILON){
      sa = 1.0f;
    }

    res[0] = (float) Math.acos(w) * 2.0f;
    res[1] = x / sa;
    res[2] = y / sa;
    res[3] = z / sa;
    return res;
  }
}

我的代码,使用w键滚动立方体(循环自动旋转一次)

代码语言:javascript
复制
Arcball arcball;

int i;
int test_count = 0;

boolean[] keys = new boolean[13];
    final int w = 0;


void setup() {
  size(900, 700, P3D); 
  frameRate(90);
  noStroke();
  arcball = new Arcball(width/2, height/2, 100);   //100 is radius
}

void draw() {
  lights();
  background(255,160,122);
  
 print(" \n degree = " + i );
  i++;
  if(i <= (360 * 1)) { arcball.rollforward(); }
  else { print(" break"); }
  
  if(keys[w]) { 
    arcball.rollforward(); 
    test_count = test_count + 1;
    print(" " + test_count);
  }

  translate(width/2, height/2-100, 0);
  box(50);
   
  translate(0, 200, 0);
  arcball.run();
  box(50);  
                           
}

void keyPressed() {
  switch(key) {
    case 119: 
        keys[w] = true;
        break;
  }
}
void keyReleased() {
  switch(key) {
    case 119: 
        keys[w] = false;
        break;
    } 
}
EN

回答 1

Stack Overflow用户

发布于 2021-01-12 11:05:25

使用我在问题中的想法来重置每2*个PI

代码语言:javascript
复制
  if(keys[w]) { 
    arcball.rollforward(PI/180);
    degreeW_count = degreeW_count + 1;
  }

  if(degreeW_count == 360) {
    arcball = new Arcball(width/2, height/2, 100);
    degreeW_count = 0;
  }

这完全避免了使用无理数和周期函数的任何数据类型会累积的任何舍入误差!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65646931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档