| 157 | * @author Jim Chen |
| 158 | */ |
| 159 | export class Matrix3D implements ISerializable { |
| 160 | private _data:Array<number>; |
| 161 | |
| 162 | constructor(iv:Array<number> = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]) { |
| 163 | if (iv.length === 16) { |
| 164 | this._data = iv; |
| 165 | } else if (iv.length === 0){ |
| 166 | this.identity(); |
| 167 | }else { |
| 168 | __trace('Matrix3D initialization vector invalid', 'warn'); |
| 169 | this.identity(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private dotProduct(a:Array<number>, b:Array<number>):Array<number> { |
| 174 | if (a.length !== 16 || b.length !== 16) { |
| 175 | throw new Error('Matrix3D dot product expects a 4xr Matrix3D'); |
| 176 | } |
| 177 | var res:Array<number> = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
| 178 | for (var i = 0; i < 4; i++) { |
| 179 | for (var j = 0; j < 4; j++) { |
| 180 | for (var k = 0; k < 4; k++) { |
| 181 | res[i * 4 + j] += a[i * 4 + k] * b[k * 4 + j]; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | return res; |
| 186 | } |
| 187 | |
| 188 | private rotationMatrix(angle:number, axis:Vector3D):Array<number> { |
| 189 | var sT:number = Math.sin(angle), cT:number = Math.cos(angle); |
| 190 | return [ |
| 191 | cT + axis.x * axis.x * (1 - cT), axis.x * axis.y * (1 - cT) - axis.z * sT, axis.x * axis.z * (1 - cT) + axis.y * sT, 0, |
| 192 | axis.x * axis.y * (1 - cT) + axis.z * sT, cT + axis.y * axis.y * (1 - cT), axis.y * axis.z * (1 - cT) - axis.x * sT, 0, |
| 193 | axis.z * axis.x * (1 - cT) - axis.y * sT, axis.z * axis.y * (1 - cT) + axis.x * sT, cT + axis.z * axis.z * (1 - cT), 0, |
| 194 | 0, 0, 0, 1 |
| 195 | ]; |
| 196 | } |
| 197 | |
| 198 | public identity():void { |
| 199 | this._data = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; |
| 200 | } |
| 201 | |
| 202 | public append(lhs:Matrix3D):void { |
| 203 | this._data = this.dotProduct(lhs._data, this._data); |
| 204 | } |
| 205 | |
| 206 | public appendRotation(degrees:number, axis:Vector3D, pivotPoint:Vector3D = null):void { |
| 207 | if (pivotPoint !== null) { |
| 208 | this.appendTranslation(pivotPoint.x, pivotPoint.y, pivotPoint.z); |
| 209 | } |
| 210 | this._data = this.dotProduct(this.rotationMatrix(degrees * Math.PI / 180, axis), this._data); |
| 211 | if (pivotPoint !== null) { |
| 212 | this.appendTranslation(-pivotPoint.x, -pivotPoint.y, -pivotPoint.z); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | public appendTranslation(x:number, y:number, z:number):void { |
nothing calls this directly
no outgoing calls
no test coverage detected