java处理浮点舍入错误,如何保持弧度合理

问题描述

修改了这个 arcball 类,以便每次调用 arcball.rollforward(PI/180);将矩阵旋转 1 度。 我试图设置它,所以 arcball.rollback() 被调用时累积浮点数rotatebywithincludedfloaterror 但它具有与回滚360度相同的度数误差而没有浮点误差。 这是 1000 次完整旋转后的距离,应该是顶部立方体在 x 上的 1:1 反射

degree error

这里是主要功能,循环 1 * 360 度旋转和用于测试的帧率(将帧率设置为 900 以进行多次旋转,因此不会永远花费)

id_up

和轨迹球类

Arcball arcball;

int i;

//framecount
int fcount,lastm;
float frate;
int fint = 3;

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


void setup() {
  size(900,700,P3D); 
  frameRate(60);
  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(PI/180); }
  else { print(" break"); }
  
  if(keys[w]) { arcball.rollforward(PI/180); }

  translate(width/2,height/2-100,0);
  Box(50);
   
  translate(0,200,0);
  arcball.run();
  Box(50);  
  
  
  fcount += 1;
  int m = millis();
  if (m - lastm > 1000 * fint) {
    frate = float(fcount) / fint;
    fcount = 0;
    lastm = m;
    println("fps: " + frate);
  }
                           
}

void keypressed() {
  switch(key) {
    case 119: 
        keys[w] = true;
        break;
  }
}
void keyreleased() {
  switch(key) {
    case 119: 
        keys[w] = false;
        break;
    } 
}

跟踪浮动误差范围以返回相同的度数 arcball.rollforward()

// Ariel and V3ga's arcball class with a couple tiny mods by Robert Hodgin and smaller mods by cubesareneat

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;
  
  float degreeW_count = 0;
  float degrees_count = 0;
  float rotatebywithincludedfloaterror =0;
  
  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,1.0f)};
    axis = -1;  // no constraints...    
  }

  void rollforward(float radians2turn) { 
    rotatebywithincludedfloaterror = rotatebywithincludedfloaterror + (-1 * (((sin(radians2turn) * radius))/2));
    if(degreeW_count >= 360) {
      arcball.rollback(rotatebywithincludedfloaterror);
      degreeW_count = 0;
      rotatebywithincludedfloaterror = 0;
    }
    rollortilt(0,-1 * (((sin(radians2turn) * radius))/2)); 
    degreeW_count = degreeW_count + 1; // need to edit this later to work with rotations other then 1 degree
  }
  void rollback(float radians2turn) { 
    rollortilt(0,((sin(radians2turn) * radius))/2);
  }
  
  void rollortilt(float xtra,float ytra){
    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 + xtra,center_y + ytra);
    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));
  }
*/
  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,z;

  Quat(){
    reset();
  }

  Quat(float w,float x,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;
  }
}

解决方法

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

  if(keys[w]) { 
    arcball.rollforward(PI/180);
    degreeW_count = degreeW_count + 1;
  }

  if(degreeW_count == 360) {
    arcball = new Arcball(width/2,height/2,100); // setset to original arcball at 0 degrees
    degreeW_count = 0;
  }

在轨迹球中

  void rollforward(float degrees2turn) { 
    rollortilt(0,-1 * (((sin(degrees2turn) * radius))/2));  // one degree forward 180/PI
  }

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...