1 package edu.berkeley.qfat.bind;
2 import edu.berkeley.qfat.geom.*;
4 /** any object associated with a specific point in 3D space */
5 public abstract class HasQuadric extends HasBindingGroup {
7 public Matrix errorQuadric() { return quadric; }
8 public boolean quadricStale = false;
10 /** the nearest vertex in the "score_against" mesh */
11 public HasQuadric nearest_in_other_mesh;
13 /** the number of vertices in the other mesh for which this is the nearest_in_other_mesh */
14 public int quadric_count;
16 /** the total error quadric (contributions from all vertices in other mesh for which this is nearest) */
17 public Matrix quadric = Matrix.ZERO;
19 public Matrix fundamentalQuadric = null;
21 public void recomputeFundamentalQuadricIfNeighborChanged() {
22 HasQuadric oldv = nearest_in_other_mesh;
23 HasQuadric newv = nearest();
24 if (oldv==newv) return;
25 recomputeFundamentalQuadric();
26 if (oldv!=null) oldv.recomputeFundamentalQuadricIfNeighborChanged();
27 // for some reason this causes an infinite loop
28 //if (newv!=null) newv.recomputeFundamentalQuadricIfNeighborChanged();
30 public void recomputeFundamentalQuadricIfStale() {
31 if (quadricStale || fundamentalQuadric==null)
32 recomputeFundamentalQuadric();
34 public void recomputeFundamentalQuadric() {
35 unApplyQuadricToNeighbor();
36 if (quadricStale || fundamentalQuadric==null) {
37 fundamentalQuadric = _recomputeFundamentalQuadric();
40 applyQuadricToNeighbor();
42 public abstract Matrix _recomputeFundamentalQuadric();
43 public void unComputeError() { setError(0); }
44 public void reComputeError() {
48 public abstract void setError(float nerror);
49 public abstract void computeError();
50 public abstract HasQuadric nearest();
51 public void applyQuadricToNeighbor() {
52 HasQuadric new_nearest = nearest();
53 if (nearest_in_other_mesh != null && new_nearest == nearest_in_other_mesh) return;
55 if (nearest_in_other_mesh != null) unApplyQuadricToNeighbor();
56 if (nearest_in_other_mesh != null) throw new Error();
58 nearest_in_other_mesh = new_nearest;
60 if (nearest_in_other_mesh!=null) {
61 nearest_in_other_mesh.unComputeError();
62 nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.plus(fundamentalQuadric());
63 nearest_in_other_mesh.quadric_count++;
64 nearest_in_other_mesh.computeError();
69 public Matrix fundamentalQuadric() {
70 if (fundamentalQuadric == null) recomputeFundamentalQuadric();
71 return fundamentalQuadric;
73 public void unApplyQuadricToNeighbor() {
74 if (nearest_in_other_mesh == null) return;
75 if (fundamentalQuadric == null) return;
76 nearest_in_other_mesh.unComputeError();
77 nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.minus(fundamentalQuadric);
78 nearest_in_other_mesh.quadric_count--;
79 if (nearest_in_other_mesh.quadric_count==0)
80 nearest_in_other_mesh.quadric = Matrix.ZERO;
81 nearest_in_other_mesh.computeError();
82 nearest_in_other_mesh = null;