robertphillips | 3d32d76 | 2015-07-13 13:16:44 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkPoint3.h" |
| 9 | |
| 10 | // Returns the square of the Euclidian distance to (x,y,z). |
| 11 | static inline float get_length_squared(float x, float y, float z) { |
| 12 | return x * x + y * y + z * z; |
| 13 | } |
| 14 | |
| 15 | // Calculates the square of the Euclidian distance to (x,y,z) and stores it in |
| 16 | // *lengthSquared. Returns true if the distance is judged to be "nearly zero". |
| 17 | // |
| 18 | // This logic is encapsulated in a helper method to make it explicit that we |
| 19 | // always perform this check in the same manner, to avoid inconsistencies |
| 20 | // (see http://code.google.com/p/skia/issues/detail?id=560 ). |
| 21 | static inline bool is_length_nearly_zero(float x, float y, float z, float *lengthSquared) { |
| 22 | *lengthSquared = get_length_squared(x, y, z); |
| 23 | return *lengthSquared <= (SK_ScalarNearlyZero * SK_ScalarNearlyZero); |
| 24 | } |
| 25 | |
| 26 | SkScalar SkPoint3::Length(SkScalar x, SkScalar y, SkScalar z) { |
| 27 | float magSq = get_length_squared(x, y, z); |
| 28 | if (SkScalarIsFinite(magSq)) { |
| 29 | return sk_float_sqrt(magSq); |
| 30 | } else { |
| 31 | double xx = x; |
| 32 | double yy = y; |
| 33 | double zz = z; |
| 34 | return (float)sqrt(xx * xx + yy * yy + zz * zz); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * We have to worry about 2 tricky conditions: |
| 40 | * 1. underflow of magSq (compared against nearlyzero^2) |
| 41 | * 2. overflow of magSq (compared w/ isfinite) |
| 42 | * |
| 43 | * If we underflow, we return false. If we overflow, we compute again using |
| 44 | * doubles, which is much slower (3x in a desktop test) but will not overflow. |
| 45 | */ |
| 46 | bool SkPoint3::normalize() { |
| 47 | float magSq; |
| 48 | if (is_length_nearly_zero(fX, fY, fZ, &magSq)) { |
| 49 | this->set(0, 0, 0); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | float scale; |
| 54 | if (SkScalarIsFinite(magSq)) { |
| 55 | scale = 1.0f / sk_float_sqrt(magSq); |
| 56 | } else { |
| 57 | // our magSq step overflowed to infinity, so use doubles instead. |
| 58 | // much slower, but needed when x, y or z is very large, otherwise we |
| 59 | // divide by inf. and return (0,0,0) vector. |
| 60 | double xx = fX; |
| 61 | double yy = fY; |
| 62 | double zz = fZ; |
| 63 | #ifdef SK_CPU_FLUSH_TO_ZERO |
| 64 | // The iOS ARM processor discards small denormalized numbers to go faster. |
| 65 | // Casting this to a float would cause the scale to go to zero. Keeping it |
| 66 | // as a double for the multiply keeps the scale non-zero. |
| 67 | double dscale = 1.0f / sqrt(xx * xx + yy * yy + zz * zz); |
| 68 | fX = x * dscale; |
| 69 | fY = y * dscale; |
| 70 | fZ = z * dscale; |
| 71 | return true; |
| 72 | #else |
| 73 | scale = (float)(1.0f / sqrt(xx * xx + yy * yy + zz * zz)); |
| 74 | #endif |
| 75 | } |
| 76 | fX *= scale; |
| 77 | fY *= scale; |
| 78 | fZ *= scale; |
| 79 | return true; |
| 80 | } |