| 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 "include/core/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 | // sqrtf does not provide enough precision; since sqrt takes a double, | 
|---|
| 53 | // there's no additional penalty to storing invScale in a double | 
|---|
| 54 | double invScale; | 
|---|
| 55 | if (sk_float_isfinite(magSq)) { | 
|---|
| 56 | invScale = magSq; | 
|---|
| 57 | } else { | 
|---|
| 58 | // our magSq step overflowed to infinity, so use doubles instead. | 
|---|
| 59 | // much slower, but needed when x, y or z is very large, otherwise we | 
|---|
| 60 | // divide by inf. and return (0,0,0) vector. | 
|---|
| 61 | double xx = fX; | 
|---|
| 62 | double yy = fY; | 
|---|
| 63 | double zz = fZ; | 
|---|
| 64 | invScale = xx * xx + yy * yy + zz * zz; | 
|---|
| 65 | } | 
|---|
| 66 | // using a float instead of a double for scale loses too much precision | 
|---|
| 67 | double scale = 1 / sqrt(invScale); | 
|---|
| 68 | fX *= scale; | 
|---|
| 69 | fY *= scale; | 
|---|
| 70 | fZ *= scale; | 
|---|
| 71 | if (!sk_float_isfinite(fX) || !sk_float_isfinite(fY) || !sk_float_isfinite(fZ)) { | 
|---|
| 72 | this->set(0, 0, 0); | 
|---|
| 73 | return false; | 
|---|
| 74 | } | 
|---|
| 75 | return true; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|