


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: Quaternion</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">org.engineFRP.maths</a> ]
</div>

<h1>Coverage Summary for Class: Quaternion (org.engineFRP.maths)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">Quaternion</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/ 24)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/ 98)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;package org.engineFRP.maths;
<i>2</i>&nbsp;
<i>3</i>&nbsp;/**
<i>4</i>&nbsp; * Based on BennyBox&#39;s GameEngine https://github.com/BennyQBD/3DGameEngine.
<i>5</i>&nbsp; * Created by TekMaTek on 05/08/2014.
<i>6</i>&nbsp; */
<i>7</i>&nbsp;public class Quaternion {
<i>8</i>&nbsp;
<i>9</i>&nbsp;	public float x;
<i>10</i>&nbsp;    public float y;
<i>11</i>&nbsp;    public float z;
<i>12</i>&nbsp;    public float w;
<i>13</i>&nbsp;
<b class="nc"><i>14</i>&nbsp;	public Quaternion( float x, float y, float z, float w ) {</b>
<b class="nc"><i>15</i>&nbsp;		this.x = x;</b>
<b class="nc"><i>16</i>&nbsp;		this.y = y;</b>
<b class="nc"><i>17</i>&nbsp;		this.z = z;</b>
<b class="nc"><i>18</i>&nbsp;		this.w = w;</b>
<i>19</i>&nbsp;	}
<i>20</i>&nbsp;
<b class="nc"><i>21</i>&nbsp;	public Quaternion( Vector3f axis, float angle ) {</b>
<b class="nc"><i>22</i>&nbsp;		float sinHalfAngle = ( float ) Math.sin(angle / 2);</b>
<b class="nc"><i>23</i>&nbsp;		float cosHalfAngle = ( float ) Math.cos(angle / 2);</b>
<i>24</i>&nbsp;
<b class="nc"><i>25</i>&nbsp;		this.x = axis.x * sinHalfAngle;</b>
<b class="nc"><i>26</i>&nbsp;		this.y = axis.y * sinHalfAngle;</b>
<b class="nc"><i>27</i>&nbsp;		this.z = axis.z * sinHalfAngle;</b>
<b class="nc"><i>28</i>&nbsp;		this.w = cosHalfAngle;</b>
<i>29</i>&nbsp;	}
<i>30</i>&nbsp;
<b class="nc"><i>31</i>&nbsp;	public Quaternion( Matrix4f rot ) {</b>
<b class="nc"><i>32</i>&nbsp;		float trace = rot.get( 0, 0 ) + rot.get( 1, 1 ) + rot.get( 2, 2 );</b>
<i>33</i>&nbsp;
<b class="nc"><i>34</i>&nbsp;		if( trace &gt; 0 ) {</b>
<b class="nc"><i>35</i>&nbsp;			float s = 0.5f / ( float ) Math.sqrt(trace + 1.0f);</b>
<b class="nc"><i>36</i>&nbsp;			w = 0.25f / s;</b>
<b class="nc"><i>37</i>&nbsp;			x = ( rot.get( 1, 2 ) - rot.get( 2, 1 ) ) * s;</b>
<b class="nc"><i>38</i>&nbsp;			y = ( rot.get( 2, 0 ) - rot.get( 0, 2 ) ) * s;</b>
<b class="nc"><i>39</i>&nbsp;			z = ( rot.get( 0, 1 ) - rot.get( 1, 0 ) ) * s;</b>
<b class="nc"><i>40</i>&nbsp;		} else {</b>
<b class="nc"><i>41</i>&nbsp;			if( rot.get( 0, 0 ) &gt; rot.get( 1, 1 ) &amp;&amp; rot.get( 0, 0 ) &gt; rot.get( 2, 2 ) ) {</b>
<b class="nc"><i>42</i>&nbsp;				float s = 2.0f * ( float ) Math.sqrt(1.0f + rot.get(0, 0) - rot.get(1, 1) - rot.get(2, 2));</b>
<b class="nc"><i>43</i>&nbsp;				w = ( rot.get( 1, 2 ) - rot.get( 2, 1 ) ) / s;</b>
<b class="nc"><i>44</i>&nbsp;				x = 0.25f * s;</b>
<b class="nc"><i>45</i>&nbsp;				y = ( rot.get( 1, 0 ) + rot.get( 0, 1 ) ) / s;</b>
<b class="nc"><i>46</i>&nbsp;				z = ( rot.get( 2, 0 ) + rot.get( 0, 2 ) ) / s;</b>
<b class="nc"><i>47</i>&nbsp;			} else if( rot.get( 1, 1 ) &gt; rot.get( 2, 2 ) ) {</b>
<b class="nc"><i>48</i>&nbsp;				float s = 2.0f * ( float ) Math.sqrt(1.0f + rot.get(1, 1) - rot.get(0, 0) - rot.get(2, 2));</b>
<b class="nc"><i>49</i>&nbsp;				w = ( rot.get( 2, 0 ) - rot.get( 0, 2 ) ) / s;</b>
<b class="nc"><i>50</i>&nbsp;				x = ( rot.get( 1, 0 ) + rot.get( 0, 1 ) ) / s;</b>
<b class="nc"><i>51</i>&nbsp;				y = 0.25f * s;</b>
<b class="nc"><i>52</i>&nbsp;				z = ( rot.get( 2, 1 ) + rot.get( 1, 2 ) ) / s;</b>
<b class="nc"><i>53</i>&nbsp;			} else {</b>
<b class="nc"><i>54</i>&nbsp;				float s = 2.0f * ( float ) Math.sqrt(1.0f + rot.get(2, 2) - rot.get(0, 0) - rot.get(1, 1));</b>
<b class="nc"><i>55</i>&nbsp;				w = ( rot.get( 0, 1 ) - rot.get( 1, 0 ) ) / s;</b>
<b class="nc"><i>56</i>&nbsp;				x = ( rot.get( 2, 0 ) + rot.get( 0, 2 ) ) / s;</b>
<b class="nc"><i>57</i>&nbsp;				y = ( rot.get( 1, 2 ) + rot.get( 2, 1 ) ) / s;</b>
<b class="nc"><i>58</i>&nbsp;				z = 0.25f * s;</b>
<i>59</i>&nbsp;			}
<i>60</i>&nbsp;		}
<i>61</i>&nbsp;
<b class="nc"><i>62</i>&nbsp;		float length = ( float ) Math.sqrt(x * x + y * y + z * z + w * w);</b>
<b class="nc"><i>63</i>&nbsp;		x /= length;</b>
<b class="nc"><i>64</i>&nbsp;		y /= length;</b>
<b class="nc"><i>65</i>&nbsp;		z /= length;</b>
<b class="nc"><i>66</i>&nbsp;		w /= length;</b>
<i>67</i>&nbsp;	}
<i>68</i>&nbsp;
<i>69</i>&nbsp;	public float length( ) {
<b class="nc"><i>70</i>&nbsp;		return ( float ) Math.sqrt(x * x + y * y + z * z + w * w);</b>
<i>71</i>&nbsp;	}
<i>72</i>&nbsp;
<i>73</i>&nbsp;	public Quaternion normalized( ) {
<b class="nc"><i>74</i>&nbsp;		float length = length( );</b>
<i>75</i>&nbsp;
<b class="nc"><i>76</i>&nbsp;		return new Quaternion( x / length, y / length, z / length, w / length );</b>
<i>77</i>&nbsp;	}
<i>78</i>&nbsp;
<i>79</i>&nbsp;	public Quaternion conjugate( ) {
<b class="nc"><i>80</i>&nbsp;		return new Quaternion( -x, -y, -z, w );</b>
<i>81</i>&nbsp;	}
<i>82</i>&nbsp;
<i>83</i>&nbsp;	public Quaternion mul( float factor ) {
<b class="nc"><i>84</i>&nbsp;		return new Quaternion( x * factor, y * factor, z * factor, w * factor );</b>
<i>85</i>&nbsp;	}
<i>86</i>&nbsp;
<i>87</i>&nbsp;	public Quaternion mul( Quaternion quaternion ) {
<b class="nc"><i>88</i>&nbsp;		float w_ = w * quaternion.w - x * quaternion.x - y * quaternion.y - z * quaternion.z;</b>
<b class="nc"><i>89</i>&nbsp;		float x_ = x * quaternion.w + w * quaternion.x + y * quaternion.z - z * quaternion.y;</b>
<b class="nc"><i>90</i>&nbsp;		float y_ = y * quaternion.w + w * quaternion.y + z * quaternion.x - x * quaternion.z;</b>
<b class="nc"><i>91</i>&nbsp;		float z_ = z * quaternion.w + w * quaternion.z + x * quaternion.y - y * quaternion.x;</b>
<i>92</i>&nbsp;
<b class="nc"><i>93</i>&nbsp;		return new Quaternion( x_, y_, z_, w_ );</b>
<i>94</i>&nbsp;	}
<i>95</i>&nbsp;
<i>96</i>&nbsp;	public Quaternion mul( Vector3f vector ) {
<b class="nc"><i>97</i>&nbsp;		float w_ = -x * vector.x - y * vector.y - z * vector.z;</b>
<b class="nc"><i>98</i>&nbsp;		float x_ = w * vector.x + y * vector.z - z * vector.y;</b>
<b class="nc"><i>99</i>&nbsp;		float y_ = w * vector.y + z * vector.x - x * vector.z;</b>
<b class="nc"><i>100</i>&nbsp;		float z_ = w * vector.z + x * vector.y - y * vector.x;</b>
<i>101</i>&nbsp;
<b class="nc"><i>102</i>&nbsp;		return new Quaternion( x_, y_, z_, w_ );</b>
<i>103</i>&nbsp;	}
<i>104</i>&nbsp;
<i>105</i>&nbsp;	public Quaternion sub( Quaternion quaternion ) {
<b class="nc"><i>106</i>&nbsp;		return new Quaternion( x - quaternion.x, y - quaternion.y, z - quaternion.z,</b>
<i>107</i>&nbsp;							   w - quaternion.w );
<i>108</i>&nbsp;	}
<i>109</i>&nbsp;
<i>110</i>&nbsp;	public Quaternion add( Quaternion quaternion ) {
<b class="nc"><i>111</i>&nbsp;		return new Quaternion( x + quaternion.x, y + quaternion.y, z + quaternion.z,</b>
<i>112</i>&nbsp;							   w + quaternion.w );
<i>113</i>&nbsp;	}
<i>114</i>&nbsp;
<i>115</i>&nbsp;	public Matrix4f toRotationMatrix( ) {
<b class="nc"><i>116</i>&nbsp;		Vector3f forward =</b>
<i>117</i>&nbsp;				new Vector3f( 2.0f * ( x * z - w * y ), 2.0f * ( y * z + w * x ), 1.0f - 2.0f * ( x * x + y * y ) );
<b class="nc"><i>118</i>&nbsp;		Vector3f up =</b>
<i>119</i>&nbsp;				new Vector3f( 2.0f * ( x * y + w * z ), 1.0f - 2.0f * ( x * x + z * z ), 2.0f * ( y * z - w * x ) );
<b class="nc"><i>120</i>&nbsp;		Vector3f right =</b>
<i>121</i>&nbsp;				new Vector3f( 1.0f - 2.0f * ( y * y + z * z ), 2.0f * ( x * y - w * z ), 2.0f * ( x * z + w * y ) );
<i>122</i>&nbsp;
<b class="nc"><i>123</i>&nbsp;		return new Matrix4f( ).initRotation( forward, up, right );</b>
<i>124</i>&nbsp;	}
<i>125</i>&nbsp;
<i>126</i>&nbsp;	public float dot( Quaternion quaternion ) {
<b class="nc"><i>127</i>&nbsp;		return x * quaternion.x + y * quaternion.y + z * quaternion.z + w * quaternion.w;</b>
<i>128</i>&nbsp;	}
<i>129</i>&nbsp;
<i>130</i>&nbsp;	public Quaternion nlerp( Quaternion quaternion, float lerpFactor, boolean shortest ) {
<b class="nc"><i>131</i>&nbsp;		Quaternion correctQuaternion = quaternion;</b>
<i>132</i>&nbsp;
<b class="nc"><i>133</i>&nbsp;		if( shortest &amp;&amp; this.dot( quaternion ) &lt; 0 ) {</b>
<b class="nc"><i>134</i>&nbsp;			correctQuaternion = new Quaternion( -quaternion.x, -quaternion.y, -quaternion.z,</b>
<i>135</i>&nbsp;												-quaternion.w );
<i>136</i>&nbsp;		}
<i>137</i>&nbsp;
<b class="nc"><i>138</i>&nbsp;		return correctQuaternion.sub( this ).mul( lerpFactor ).add( this ).normalized( );</b>
<i>139</i>&nbsp;	}
<i>140</i>&nbsp;
<i>141</i>&nbsp;	public Quaternion slerp( Quaternion quaternion, float lerpFactor, boolean shortest ) {
<b class="nc"><i>142</i>&nbsp;		final float EPSILON = 1e3f;</b>
<i>143</i>&nbsp;
<b class="nc"><i>144</i>&nbsp;		float cos = this.dot( quaternion );</b>
<b class="nc"><i>145</i>&nbsp;		Quaternion correctQuaternion = quaternion;</b>
<i>146</i>&nbsp;
<b class="nc"><i>147</i>&nbsp;		if( shortest &amp;&amp; cos &lt; 0 ) {</b>
<b class="nc"><i>148</i>&nbsp;			cos = -cos;</b>
<b class="nc"><i>149</i>&nbsp;			correctQuaternion = new Quaternion( -quaternion.x, -quaternion.y, -quaternion.z,</b>
<i>150</i>&nbsp;												-quaternion.w );
<i>151</i>&nbsp;		}
<i>152</i>&nbsp;
<b class="nc"><i>153</i>&nbsp;		if( Math.abs(cos) &gt;= 1 - EPSILON ) {</b>
<b class="nc"><i>154</i>&nbsp;			return nlerp( correctQuaternion, lerpFactor, false );</b>
<i>155</i>&nbsp;		}
<i>156</i>&nbsp;
<b class="nc"><i>157</i>&nbsp;		float sin = ( float ) Math.sqrt(1.0f - cos * cos);</b>
<b class="nc"><i>158</i>&nbsp;		float angle = ( float ) Math.atan2(sin, cos);</b>
<b class="nc"><i>159</i>&nbsp;		float invSin = 1.0f / sin;</b>
<i>160</i>&nbsp;
<b class="nc"><i>161</i>&nbsp;		float srcFactor = ( float ) Math.sin((1.0f - lerpFactor) * angle) * invSin;</b>
<b class="nc"><i>162</i>&nbsp;		float destFactor = ( float ) Math.sin((lerpFactor) * angle) * invSin;</b>
<i>163</i>&nbsp;
<b class="nc"><i>164</i>&nbsp;		return this.mul( srcFactor ).add( correctQuaternion.mul( destFactor ) );</b>
<i>165</i>&nbsp;	}
<i>166</i>&nbsp;
<i>167</i>&nbsp;	public Vector3f getForward( ) {
<b class="nc"><i>168</i>&nbsp;		return new Vector3f( 0, 0, 1 ).rotate( this );</b>
<i>169</i>&nbsp;	}
<i>170</i>&nbsp;
<i>171</i>&nbsp;	public Vector3f getBack( ) {
<b class="nc"><i>172</i>&nbsp;		return new Vector3f( 0, 0, -1 ).rotate( this );</b>
<i>173</i>&nbsp;	}
<i>174</i>&nbsp;
<i>175</i>&nbsp;	public Vector3f getUp( ) {
<b class="nc"><i>176</i>&nbsp;		return new Vector3f( 0, 1, 0 ).rotate( this );</b>
<i>177</i>&nbsp;	}
<i>178</i>&nbsp;
<i>179</i>&nbsp;	public Vector3f getDown( ) {
<b class="nc"><i>180</i>&nbsp;		return new Vector3f( 0, -1, 0 ).rotate( this );</b>
<i>181</i>&nbsp;	}
<i>182</i>&nbsp;
<i>183</i>&nbsp;	public Vector3f getRight( ) {
<b class="nc"><i>184</i>&nbsp;		return new Vector3f( 1, 0, 0 ).rotate( this );</b>
<i>185</i>&nbsp;	}
<i>186</i>&nbsp;
<i>187</i>&nbsp;	public Vector3f getLeft( ) {
<b class="nc"><i>188</i>&nbsp;		return new Vector3f( -1, 0, 0 ).rotate( this );</b>
<i>189</i>&nbsp;	}
<i>190</i>&nbsp;
<i>191</i>&nbsp;	public Quaternion set( float x, float y, float z, float w ) {
<b class="nc"><i>192</i>&nbsp;		this.x = x;</b>
<b class="nc"><i>193</i>&nbsp;		this.y = y;</b>
<b class="nc"><i>194</i>&nbsp;		this.z = z;</b>
<b class="nc"><i>195</i>&nbsp;		this.w = w;</b>
<b class="nc"><i>196</i>&nbsp;		return this;</b>
<i>197</i>&nbsp;	}
<i>198</i>&nbsp;
<i>199</i>&nbsp;	public Quaternion set( Quaternion quaternion ) {
<b class="nc"><i>200</i>&nbsp;		set( quaternion.x, quaternion.y, quaternion.z, quaternion.w );</b>
<b class="nc"><i>201</i>&nbsp;		return this;</b>
<i>202</i>&nbsp;	}
<i>203</i>&nbsp;
<i>204</i>&nbsp;	public boolean equals( Quaternion quaternion ) {
<b class="nc"><i>205</i>&nbsp;		return x == quaternion.x &amp;&amp; y == quaternion.y &amp;&amp; z == quaternion.z &amp;&amp; w == quaternion.w;</b>
<i>206</i>&nbsp;	}
<i>207</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2015-05-03 14:32</div>
</div>
</body>
</html>
