1 | |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | #include <osgManipulator/AntiSquish> |
---|
16 | |
---|
17 | using namespace osgManipulator; |
---|
18 | |
---|
19 | namespace |
---|
20 | { |
---|
21 | class AntiSquishCallback: public osg::NodeCallback |
---|
22 | { |
---|
23 | public: |
---|
24 | AntiSquishCallback(AntiSquish* asq) : osg::NodeCallback(), _antiSquish(asq) {} |
---|
25 | virtual ~AntiSquishCallback() {}; |
---|
26 | |
---|
27 | virtual void operator() (osg::Node*, osg::NodeVisitor* nv) |
---|
28 | { |
---|
29 | |
---|
30 | osg::NodePath np = nv->getNodePath(); |
---|
31 | |
---|
32 | |
---|
33 | np.pop_back(); |
---|
34 | |
---|
35 | |
---|
36 | osg::Matrix localToWorld = osg::computeLocalToWorld(np); |
---|
37 | |
---|
38 | |
---|
39 | bool flag = false; |
---|
40 | osg::Matrix _unsquishedMatrix = _antiSquish->computeUnSquishedMatrix(localToWorld, flag); |
---|
41 | if (flag) |
---|
42 | _antiSquish->setMatrix(_unsquishedMatrix); |
---|
43 | } |
---|
44 | |
---|
45 | protected: |
---|
46 | AntiSquish* _antiSquish; |
---|
47 | }; |
---|
48 | |
---|
49 | } |
---|
50 | |
---|
51 | AntiSquish::AntiSquish() : _usePivot(true), _usePosition(false) |
---|
52 | { |
---|
53 | _asqCallback = new AntiSquishCallback(this); |
---|
54 | setUpdateCallback(_asqCallback); |
---|
55 | } |
---|
56 | |
---|
57 | AntiSquish::AntiSquish(const osg::Vec3& pivot) : _pivot(pivot), _usePivot(true), _usePosition(false) |
---|
58 | { |
---|
59 | _asqCallback = new AntiSquishCallback(this); |
---|
60 | setUpdateCallback(_asqCallback); |
---|
61 | } |
---|
62 | |
---|
63 | AntiSquish::AntiSquish(const osg::Vec3& pivot, const osg::Vec3& pos) |
---|
64 | : _pivot(pivot), _usePivot(true), _position(pos), _usePosition(true) |
---|
65 | { |
---|
66 | _asqCallback = new AntiSquishCallback(this); |
---|
67 | setUpdateCallback(_asqCallback); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | AntiSquish::AntiSquish(const AntiSquish& pat,const osg::CopyOp& copyop) : |
---|
72 | MatrixTransform(pat,copyop), |
---|
73 | _asqCallback(pat._asqCallback), |
---|
74 | _pivot(pat._pivot), |
---|
75 | _usePivot(pat._usePivot), |
---|
76 | _position(pat._position), |
---|
77 | _usePosition(pat._usePosition), |
---|
78 | _cachedLocalToWorld(pat._cachedLocalToWorld) |
---|
79 | { |
---|
80 | } |
---|
81 | |
---|
82 | AntiSquish::~AntiSquish() |
---|
83 | { |
---|
84 | } |
---|
85 | |
---|
86 | osg::Matrix AntiSquish::computeUnSquishedMatrix(const osg::Matrix& LTW, bool& flag) |
---|
87 | { |
---|
88 | osg::Vec3d t, s; |
---|
89 | osg::Quat r, so; |
---|
90 | |
---|
91 | if (LTW == _cachedLocalToWorld && _dirty == false) |
---|
92 | { |
---|
93 | flag = false; |
---|
94 | return osg::Matrix::identity(); |
---|
95 | } |
---|
96 | |
---|
97 | _cachedLocalToWorld = LTW; |
---|
98 | |
---|
99 | LTW.decompose(t, r, s, so); |
---|
100 | |
---|
101 | |
---|
102 | double av = (s[0] + s[1] + s[2])/3.0; |
---|
103 | s[0] = av; s[1] = av; s[2]=av; |
---|
104 | |
---|
105 | if (av == 0) |
---|
106 | { |
---|
107 | flag = false; |
---|
108 | return osg::Matrix::identity(); |
---|
109 | } |
---|
110 | |
---|
111 | osg::Matrix unsquished; |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | if (_usePivot) |
---|
118 | { |
---|
119 | unsquished.postMultTranslate(-_pivot); |
---|
120 | |
---|
121 | osg::Matrix tmps, invtmps; |
---|
122 | so.get(tmps); |
---|
123 | if (!invtmps.invert(tmps)) |
---|
124 | { |
---|
125 | flag = false; |
---|
126 | return osg::Matrix::identity(); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | unsquished.postMult(invtmps); |
---|
131 | |
---|
132 | unsquished.postMultScale(s); |
---|
133 | |
---|
134 | unsquished.postMult(tmps); |
---|
135 | |
---|
136 | unsquished.postMultRotate(r); |
---|
137 | |
---|
138 | unsquished.postMultTranslate(t); |
---|
139 | |
---|
140 | osg::Matrix invltw; |
---|
141 | if (!invltw.invert(LTW)) |
---|
142 | { |
---|
143 | flag = false; |
---|
144 | return osg::Matrix::identity(); |
---|
145 | } |
---|
146 | |
---|
147 | unsquished.postMult( invltw ); |
---|
148 | |
---|
149 | |
---|
150 | if (_usePosition) |
---|
151 | unsquished.postMult(_position); |
---|
152 | else |
---|
153 | unsquished.postMult(_pivot); |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | osg::Matrix tmps, invtmps; |
---|
158 | so.get(tmps); |
---|
159 | if (!invtmps.invert(tmps)) |
---|
160 | { |
---|
161 | flag = false; |
---|
162 | return osg::Matrix::identity(); |
---|
163 | } |
---|
164 | unsquished.postMult(invtmps); |
---|
165 | unsquished.postMultScale(s); |
---|
166 | unsquished.postMult(tmps); |
---|
167 | unsquished.postMultRotate(r); |
---|
168 | unsquished.postMultTranslate(t); |
---|
169 | osg::Matrix invltw; |
---|
170 | if (!invltw.invert(LTW)) |
---|
171 | { |
---|
172 | flag = false; |
---|
173 | return osg::Matrix::identity(); |
---|
174 | } |
---|
175 | unsquished.postMult( invltw ); |
---|
176 | } |
---|
177 | |
---|
178 | if (unsquished.isNaN()) |
---|
179 | { |
---|
180 | flag = false; |
---|
181 | return osg::Matrix::identity(); |
---|
182 | } |
---|
183 | |
---|
184 | flag = true; |
---|
185 | _dirty = false; |
---|
186 | return unsquished; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|