1page.title=RenderScript Vector Math Functions
2
3@jd:body
4
5<div class='renderscript'>
6<h2>Overview</h2>
7<p> These functions interpret the input arguments as representation of vectors in
8n-dimensional space.
9</p>
10
11<p> The precision of the mathematical operations on 32 bit floats is affected by the pragmas
12rs_fp_relaxed and rs_fp_full.  See <a href='rs_math.html'>Mathematical Constants and Functions</a> for details.
13</p>
14
15<p> Different precision/speed tradeoffs can be achieved by using variants of the common math
16functions.  Functions with a name starting with<ul>
17<li>native_: May have custom hardware implementations with weaker precision.  Additionally,
18  subnormal values may be flushed to zero, rounding towards zero may be used, and NaN and
19  infinity input may not be handled correctly.</li>
20<li>fast_: May perform internal computations using 16 bit floats.  Additionally, subnormal
21  values may be flushed to zero, and rounding towards zero may be used.</li>
22</ul>
23</p>
24<h2>Summary</h2>
25<table class='jd-sumtable'><tbody>
26  <tr><th colspan='2'>Functions</th></tr>
27  <tr class='alt-color api apilevel-1'>
28    <td class='jd-linkcol'>
29      <a href='rs_vector_math.html#android_rs:cross'>cross</a>
30    </td>
31    <td class='jd-descrcol' width='100%'>
32      Cross product of two vectors
33    </td>
34  </tr>
35  <tr class='alt-color api apilevel-1'>
36    <td class='jd-linkcol'>
37      <a href='rs_vector_math.html#android_rs:distance'>distance</a>
38    </td>
39    <td class='jd-descrcol' width='100%'>
40      Distance between two points
41    </td>
42  </tr>
43  <tr class='alt-color api apilevel-1'>
44    <td class='jd-linkcol'>
45      <a href='rs_vector_math.html#android_rs:dot'>dot</a>
46    </td>
47    <td class='jd-descrcol' width='100%'>
48      Dot product of two vectors
49    </td>
50  </tr>
51  <tr class='alt-color api apilevel-1'>
52    <td class='jd-linkcol'>
53      <a href='rs_vector_math.html#android_rs:fast_distance'>fast_distance</a>
54    </td>
55    <td class='jd-descrcol' width='100%'>
56      Approximate distance between two points
57    </td>
58  </tr>
59  <tr class='alt-color api apilevel-1'>
60    <td class='jd-linkcol'>
61      <a href='rs_vector_math.html#android_rs:fast_length'>fast_length</a>
62    </td>
63    <td class='jd-descrcol' width='100%'>
64      Approximate length of a vector
65    </td>
66  </tr>
67  <tr class='alt-color api apilevel-1'>
68    <td class='jd-linkcol'>
69      <a href='rs_vector_math.html#android_rs:fast_normalize'>fast_normalize</a>
70    </td>
71    <td class='jd-descrcol' width='100%'>
72      Approximate normalized vector
73    </td>
74  </tr>
75  <tr class='alt-color api apilevel-1'>
76    <td class='jd-linkcol'>
77      <a href='rs_vector_math.html#android_rs:length'>length</a>
78    </td>
79    <td class='jd-descrcol' width='100%'>
80      Length of a vector
81    </td>
82  </tr>
83  <tr class='alt-color api apilevel-1'>
84    <td class='jd-linkcol'>
85      <a href='rs_vector_math.html#android_rs:native_distance'>native_distance</a>
86    </td>
87    <td class='jd-descrcol' width='100%'>
88      Approximate distance between two points
89    </td>
90  </tr>
91  <tr class='alt-color api apilevel-1'>
92    <td class='jd-linkcol'>
93      <a href='rs_vector_math.html#android_rs:native_length'>native_length</a>
94    </td>
95    <td class='jd-descrcol' width='100%'>
96      Approximate length of a vector
97    </td>
98  </tr>
99  <tr class='alt-color api apilevel-1'>
100    <td class='jd-linkcol'>
101      <a href='rs_vector_math.html#android_rs:native_normalize'>native_normalize</a>
102    </td>
103    <td class='jd-descrcol' width='100%'>
104      Approximately normalize a vector
105    </td>
106  </tr>
107  <tr class='alt-color api apilevel-1'>
108    <td class='jd-linkcol'>
109      <a href='rs_vector_math.html#android_rs:normalize'>normalize</a>
110    </td>
111    <td class='jd-descrcol' width='100%'>
112      Normalize a vector
113    </td>
114  </tr>
115</tbody></table>
116<h2>Functions</h2>
117<a name='android_rs:cross'></a>
118<div class='jd-details'>
119  <h4 class='jd-details-title'>
120    <span class='sympad'>cross</span>
121    <span class='normal'>: Cross product of two vectors</span>
122  </h4>
123  <div class='jd-details-descr'>
124    <table class='jd-tagtable'><tbody>
125      <tr>
126        <td><a href='rs_value_types.html#android_rs:float3'>float3</a> cross(<a href='rs_value_types.html#android_rs:float3'>float3</a> left_vector, <a href='rs_value_types.html#android_rs:float3'>float3</a> right_vector);
127</td>
128        <td>        </td>
129      </tr>
130      <tr>
131        <td><a href='rs_value_types.html#android_rs:float4'>float4</a> cross(<a href='rs_value_types.html#android_rs:float4'>float4</a> left_vector, <a href='rs_value_types.html#android_rs:float4'>float4</a> right_vector);
132</td>
133        <td>        </td>
134      </tr>
135    </tbody></table>
136  </div>
137  <div class='jd-tagdata jd-tagdescr'>
138<p> Computes the cross product of two vectors.
139</p>
140  </div>
141</div>
142
143<a name='android_rs:distance'></a>
144<div class='jd-details'>
145  <h4 class='jd-details-title'>
146    <span class='sympad'>distance</span>
147    <span class='normal'>: Distance between two points</span>
148  </h4>
149  <div class='jd-details-descr'>
150    <table class='jd-tagtable'><tbody>
151      <tr>
152        <td>float distance(float left_vector, float right_vector);
153</td>
154        <td>        </td>
155      </tr>
156      <tr>
157        <td>float distance(<a href='rs_value_types.html#android_rs:float2'>float2</a> left_vector, <a href='rs_value_types.html#android_rs:float2'>float2</a> right_vector);
158</td>
159        <td>        </td>
160      </tr>
161      <tr>
162        <td>float distance(<a href='rs_value_types.html#android_rs:float3'>float3</a> left_vector, <a href='rs_value_types.html#android_rs:float3'>float3</a> right_vector);
163</td>
164        <td>        </td>
165      </tr>
166      <tr>
167        <td>float distance(<a href='rs_value_types.html#android_rs:float4'>float4</a> left_vector, <a href='rs_value_types.html#android_rs:float4'>float4</a> right_vector);
168</td>
169        <td>        </td>
170      </tr>
171    </tbody></table>
172  </div>
173  <div class='jd-tagdata jd-tagdescr'>
174<p> Compute the distance between two points.
175</p>
176
177<p> See also <a href='rs_vector_math.html#android_rs:fast_distance'>fast_distance</a>(), <a href='rs_vector_math.html#android_rs:native_distance'>native_distance</a>().
178</p>
179  </div>
180</div>
181
182<a name='android_rs:dot'></a>
183<div class='jd-details'>
184  <h4 class='jd-details-title'>
185    <span class='sympad'>dot</span>
186    <span class='normal'>: Dot product of two vectors</span>
187  </h4>
188  <div class='jd-details-descr'>
189    <table class='jd-tagtable'><tbody>
190      <tr>
191        <td>float dot(float left_vector, float right_vector);
192</td>
193        <td>        </td>
194      </tr>
195      <tr>
196        <td>float dot(<a href='rs_value_types.html#android_rs:float2'>float2</a> left_vector, <a href='rs_value_types.html#android_rs:float2'>float2</a> right_vector);
197</td>
198        <td>        </td>
199      </tr>
200      <tr>
201        <td>float dot(<a href='rs_value_types.html#android_rs:float3'>float3</a> left_vector, <a href='rs_value_types.html#android_rs:float3'>float3</a> right_vector);
202</td>
203        <td>        </td>
204      </tr>
205      <tr>
206        <td>float dot(<a href='rs_value_types.html#android_rs:float4'>float4</a> left_vector, <a href='rs_value_types.html#android_rs:float4'>float4</a> right_vector);
207</td>
208        <td>        </td>
209      </tr>
210    </tbody></table>
211  </div>
212  <div class='jd-tagdata jd-tagdescr'>
213<p> Computes the dot product of two vectors.
214</p>
215  </div>
216</div>
217
218<a name='android_rs:fast_distance'></a>
219<div class='jd-details'>
220  <h4 class='jd-details-title'>
221    <span class='sympad'>fast_distance</span>
222    <span class='normal'>: Approximate distance between two points</span>
223  </h4>
224  <div class='jd-details-descr'>
225    <table class='jd-tagtable'><tbody>
226      <tr>
227        <td>float fast_distance(float left_vector, float right_vector);
228</td>
229        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
230        </td>
231      </tr>
232      <tr>
233        <td>float fast_distance(<a href='rs_value_types.html#android_rs:float2'>float2</a> left_vector, <a href='rs_value_types.html#android_rs:float2'>float2</a> right_vector);
234</td>
235        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
236        </td>
237      </tr>
238      <tr>
239        <td>float fast_distance(<a href='rs_value_types.html#android_rs:float3'>float3</a> left_vector, <a href='rs_value_types.html#android_rs:float3'>float3</a> right_vector);
240</td>
241        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
242        </td>
243      </tr>
244      <tr>
245        <td>float fast_distance(<a href='rs_value_types.html#android_rs:float4'>float4</a> left_vector, <a href='rs_value_types.html#android_rs:float4'>float4</a> right_vector);
246</td>
247        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
248        </td>
249      </tr>
250    </tbody></table>
251  </div>
252  <div class='jd-tagdata jd-tagdescr'>
253<p> Computes the approximate distance between two points.
254</p>
255
256<p> The precision is what would be expected from doing the computation using 16 bit floating
257point values.
258</p>
259
260<p> See also <a href='rs_vector_math.html#android_rs:distance'>distance</a>(), <a href='rs_vector_math.html#android_rs:native_distance'>native_distance</a>().
261</p>
262  </div>
263</div>
264
265<a name='android_rs:fast_length'></a>
266<div class='jd-details'>
267  <h4 class='jd-details-title'>
268    <span class='sympad'>fast_length</span>
269    <span class='normal'>: Approximate length of a vector</span>
270  </h4>
271  <div class='jd-details-descr'>
272    <table class='jd-tagtable'><tbody>
273      <tr>
274        <td>float fast_length(float v);
275</td>
276        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
277        </td>
278      </tr>
279      <tr>
280        <td>float fast_length(<a href='rs_value_types.html#android_rs:float2'>float2</a> v);
281</td>
282        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
283        </td>
284      </tr>
285      <tr>
286        <td>float fast_length(<a href='rs_value_types.html#android_rs:float3'>float3</a> v);
287</td>
288        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
289        </td>
290      </tr>
291      <tr>
292        <td>float fast_length(<a href='rs_value_types.html#android_rs:float4'>float4</a> v);
293</td>
294        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
295        </td>
296      </tr>
297    </tbody></table>
298  </div>
299  <div class='jd-tagdata jd-tagdescr'>
300<p> Computes the approximate length of a vector.
301</p>
302
303<p> The precision is what would be expected from doing the computation using 16 bit floating
304point values.
305</p>
306
307<p> See also <a href='rs_vector_math.html#android_rs:length'>length</a>(), <a href='rs_vector_math.html#android_rs:native_length'>native_length</a>().
308</p>
309  </div>
310</div>
311
312<a name='android_rs:fast_normalize'></a>
313<div class='jd-details'>
314  <h4 class='jd-details-title'>
315    <span class='sympad'>fast_normalize</span>
316    <span class='normal'>: Approximate normalized vector</span>
317  </h4>
318  <div class='jd-details-descr'>
319    <table class='jd-tagtable'><tbody>
320      <tr>
321        <td>float fast_normalize(float v);
322</td>
323        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
324        </td>
325      </tr>
326      <tr>
327        <td><a href='rs_value_types.html#android_rs:float2'>float2</a> fast_normalize(<a href='rs_value_types.html#android_rs:float2'>float2</a> v);
328</td>
329        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
330        </td>
331      </tr>
332      <tr>
333        <td><a href='rs_value_types.html#android_rs:float3'>float3</a> fast_normalize(<a href='rs_value_types.html#android_rs:float3'>float3</a> v);
334</td>
335        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
336        </td>
337      </tr>
338      <tr>
339        <td><a href='rs_value_types.html#android_rs:float4'>float4</a> fast_normalize(<a href='rs_value_types.html#android_rs:float4'>float4</a> v);
340</td>
341        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 17</a>
342        </td>
343      </tr>
344    </tbody></table>
345  </div>
346  <div class='jd-tagdata jd-tagdescr'>
347<p> Approximately normalizes a vector.
348</p>
349
350<p> For vectors of size 1, returns -1.f for negative values, 0.f for null values, and 1.f for
351positive values.
352</p>
353
354<p> The precision is what would be expected from doing the computation using 16 bit floating
355point values.
356</p>
357
358<p> See also <a href='rs_vector_math.html#android_rs:normalize'>normalize</a>(), <a href='rs_vector_math.html#android_rs:native_normalize'>native_normalize</a>().
359</p>
360  </div>
361</div>
362
363<a name='android_rs:length'></a>
364<div class='jd-details'>
365  <h4 class='jd-details-title'>
366    <span class='sympad'>length</span>
367    <span class='normal'>: Length of a vector</span>
368  </h4>
369  <div class='jd-details-descr'>
370    <table class='jd-tagtable'><tbody>
371      <tr>
372        <td>float length(float v);
373</td>
374        <td>        </td>
375      </tr>
376      <tr>
377        <td>float length(<a href='rs_value_types.html#android_rs:float2'>float2</a> v);
378</td>
379        <td>        </td>
380      </tr>
381      <tr>
382        <td>float length(<a href='rs_value_types.html#android_rs:float3'>float3</a> v);
383</td>
384        <td>        </td>
385      </tr>
386      <tr>
387        <td>float length(<a href='rs_value_types.html#android_rs:float4'>float4</a> v);
388</td>
389        <td>        </td>
390      </tr>
391    </tbody></table>
392  </div>
393  <div class='jd-tagdata jd-tagdescr'>
394<p> Computes the length of a vector.
395</p>
396
397<p> See also <a href='rs_vector_math.html#android_rs:fast_length'>fast_length</a>(), <a href='rs_vector_math.html#android_rs:native_length'>native_length</a>().
398</p>
399  </div>
400</div>
401
402<a name='android_rs:native_distance'></a>
403<div class='jd-details'>
404  <h4 class='jd-details-title'>
405    <span class='sympad'>native_distance</span>
406    <span class='normal'>: Approximate distance between two points</span>
407  </h4>
408  <div class='jd-details-descr'>
409    <table class='jd-tagtable'><tbody>
410      <tr>
411        <td>float native_distance(float left_vector, float right_vector);
412</td>
413        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
414        </td>
415      </tr>
416      <tr>
417        <td>float native_distance(<a href='rs_value_types.html#android_rs:float2'>float2</a> left_vector, <a href='rs_value_types.html#android_rs:float2'>float2</a> right_vector);
418</td>
419        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
420        </td>
421      </tr>
422      <tr>
423        <td>float native_distance(<a href='rs_value_types.html#android_rs:float3'>float3</a> left_vector, <a href='rs_value_types.html#android_rs:float3'>float3</a> right_vector);
424</td>
425        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
426        </td>
427      </tr>
428      <tr>
429        <td>float native_distance(<a href='rs_value_types.html#android_rs:float4'>float4</a> left_vector, <a href='rs_value_types.html#android_rs:float4'>float4</a> right_vector);
430</td>
431        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
432        </td>
433      </tr>
434    </tbody></table>
435  </div>
436  <div class='jd-tagdata jd-tagdescr'>
437<p> Computes the approximate distance between two points.
438</p>
439
440<p> See also <a href='rs_vector_math.html#android_rs:distance'>distance</a>(), <a href='rs_vector_math.html#android_rs:fast_distance'>fast_distance</a>().
441</p>
442  </div>
443</div>
444
445<a name='android_rs:native_length'></a>
446<div class='jd-details'>
447  <h4 class='jd-details-title'>
448    <span class='sympad'>native_length</span>
449    <span class='normal'>: Approximate length of a vector</span>
450  </h4>
451  <div class='jd-details-descr'>
452    <table class='jd-tagtable'><tbody>
453      <tr>
454        <td>float native_length(float v);
455</td>
456        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
457        </td>
458      </tr>
459      <tr>
460        <td>float native_length(<a href='rs_value_types.html#android_rs:float2'>float2</a> v);
461</td>
462        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
463        </td>
464      </tr>
465      <tr>
466        <td>float native_length(<a href='rs_value_types.html#android_rs:float3'>float3</a> v);
467</td>
468        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
469        </td>
470      </tr>
471      <tr>
472        <td>float native_length(<a href='rs_value_types.html#android_rs:float4'>float4</a> v);
473</td>
474        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
475        </td>
476      </tr>
477    </tbody></table>
478  </div>
479  <div class='jd-tagdata jd-tagdescr'>
480<p> Compute the approximate length of a vector.
481</p>
482
483<p> See also <a href='rs_vector_math.html#android_rs:length'>length</a>(), <a href='rs_vector_math.html#android_rs:fast_length'>fast_length</a>().
484</p>
485  </div>
486</div>
487
488<a name='android_rs:native_normalize'></a>
489<div class='jd-details'>
490  <h4 class='jd-details-title'>
491    <span class='sympad'>native_normalize</span>
492    <span class='normal'>: Approximately normalize a vector</span>
493  </h4>
494  <div class='jd-details-descr'>
495    <table class='jd-tagtable'><tbody>
496      <tr>
497        <td>float native_normalize(float v);
498</td>
499        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
500        </td>
501      </tr>
502      <tr>
503        <td><a href='rs_value_types.html#android_rs:float2'>float2</a> native_normalize(<a href='rs_value_types.html#android_rs:float2'>float2</a> v);
504</td>
505        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
506        </td>
507      </tr>
508      <tr>
509        <td><a href='rs_value_types.html#android_rs:float3'>float3</a> native_normalize(<a href='rs_value_types.html#android_rs:float3'>float3</a> v);
510</td>
511        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
512        </td>
513      </tr>
514      <tr>
515        <td><a href='rs_value_types.html#android_rs:float4'>float4</a> native_normalize(<a href='rs_value_types.html#android_rs:float4'>float4</a> v);
516</td>
517        <td>    Added in <a href='http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels'>API level 21</a>
518        </td>
519      </tr>
520    </tbody></table>
521  </div>
522  <div class='jd-tagdata jd-tagdescr'>
523<p> Approximately normalizes a vector.
524</p>
525
526<p> See also <a href='rs_vector_math.html#android_rs:normalize'>normalize</a>(), <a href='rs_vector_math.html#android_rs:fast_normalize'>fast_normalize</a>().
527</p>
528  </div>
529</div>
530
531<a name='android_rs:normalize'></a>
532<div class='jd-details'>
533  <h4 class='jd-details-title'>
534    <span class='sympad'>normalize</span>
535    <span class='normal'>: Normalize a vector</span>
536  </h4>
537  <div class='jd-details-descr'>
538    <table class='jd-tagtable'><tbody>
539      <tr>
540        <td>float normalize(float v);
541</td>
542        <td>        </td>
543      </tr>
544      <tr>
545        <td><a href='rs_value_types.html#android_rs:float2'>float2</a> normalize(<a href='rs_value_types.html#android_rs:float2'>float2</a> v);
546</td>
547        <td>        </td>
548      </tr>
549      <tr>
550        <td><a href='rs_value_types.html#android_rs:float3'>float3</a> normalize(<a href='rs_value_types.html#android_rs:float3'>float3</a> v);
551</td>
552        <td>        </td>
553      </tr>
554      <tr>
555        <td><a href='rs_value_types.html#android_rs:float4'>float4</a> normalize(<a href='rs_value_types.html#android_rs:float4'>float4</a> v);
556</td>
557        <td>        </td>
558      </tr>
559    </tbody></table>
560  </div>
561  <div class='jd-tagdata jd-tagdescr'>
562<p> Normalize a vector.
563</p>
564
565<p> For vectors of size 1, returns -1.f for negative values, 0.f for null values, and 1.f for
566positive values.
567</p>
568
569<p> See also <a href='rs_vector_math.html#android_rs:fast_normalize'>fast_normalize</a>(), <a href='rs_vector_math.html#android_rs:native_normalize'>native_normalize</a>().
570</p>
571  </div>
572</div>
573
574</div>
575