MCPcopy
hub / github.com/g3n/engine / GetInverse

Method GetInverse

math32/matrix4.go:435–486  ·  view source on GitHub ↗

GetInverse sets this matrix to the inverse of the src matrix. If the src matrix cannot be inverted returns error and sets this matrix to the identity matrix.

(src *Matrix4)

Source from the content-addressed store, hash-verified

433// If the src matrix cannot be inverted returns error and
434// sets this matrix to the identity matrix.
435func (m *Matrix4) GetInverse(src *Matrix4) error {
436
437 n11 := src[0]
438 n12 := src[4]
439 n13 := src[8]
440 n14 := src[12]
441 n21 := src[1]
442 n22 := src[5]
443 n23 := src[9]
444 n24 := src[13]
445 n31 := src[2]
446 n32 := src[6]
447 n33 := src[10]
448 n34 := src[14]
449 n41 := src[3]
450 n42 := src[7]
451 n43 := src[11]
452 n44 := src[15]
453
454 t11 := n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44
455 t12 := n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44
456 t13 := n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44
457 t14 := n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34
458
459 det := n11*t11 + n21*t12 + n31*t13 + n41*t14
460
461 if det == 0 {
462 m.Identity()
463 return errors.New("cannot invert matrix")
464 }
465
466 m[0] = t11
467 m[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44
468 m[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44
469 m[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43
470 m[4] = t12
471 m[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44
472 m[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44
473 m[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43
474 m[8] = t13
475 m[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44
476 m[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44
477 m[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43
478 m[12] = t14
479 m[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34
480 m[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34
481 m[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33
482
483 m.MultiplyScalar(1.0 / det)
484
485 return nil
486}
487
488// Scale multiply the first column of this matrix by the vector X component,
489// the second column by the vector Y component and the third column by

Callers 7

RenderSetupMethod · 0.95
UnprojectMethod · 0.95
RaycastPointsMethod · 0.95
RaycastMeshMethod · 0.95
lineRaycastFunction · 0.95
ViewMatrixMethod · 0.45
UpdateMassPropertiesMethod · 0.45

Calls 2

IdentityMethod · 0.95
MultiplyScalarMethod · 0.95

Tested by

no test coverage detected