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).
get_length_squared(float x,float y,float 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 ).
is_length_nearly_zero(float x,float y,float z,float * lengthSquared)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
dump(std::string & desc,int depth) const26 void SkPoint3::dump(std::string& desc, int depth) const {
27 std::string split(depth, '\t');
28 desc += split + "\n SkPoint3:{ \n";
29 desc += split + "\t fX: " + std::to_string(fX) + "\n";
30 desc += split + "\t fY: " + std::to_string(fY) + "\n";
31 desc += split + "\t fZ: " + std::to_string(fZ) + "\n";
32 desc += split + "}\n";
33 }
34
Length(SkScalar x,SkScalar y,SkScalar z)35 SkScalar SkPoint3::Length(SkScalar x, SkScalar y, SkScalar z) {
36 float magSq = get_length_squared(x, y, z);
37 if (SkScalarIsFinite(magSq)) {
38 return sk_float_sqrt(magSq);
39 } else {
40 double xx = x;
41 double yy = y;
42 double zz = z;
43 return (float)sqrt(xx * xx + yy * yy + zz * zz);
44 }
45 }
46
47 /*
48 * We have to worry about 2 tricky conditions:
49 * 1. underflow of magSq (compared against nearlyzero^2)
50 * 2. overflow of magSq (compared w/ isfinite)
51 *
52 * If we underflow, we return false. If we overflow, we compute again using
53 * doubles, which is much slower (3x in a desktop test) but will not overflow.
54 */
normalize()55 bool SkPoint3::normalize() {
56 float magSq;
57 if (is_length_nearly_zero(fX, fY, fZ, &magSq)) {
58 this->set(0, 0, 0);
59 return false;
60 }
61 // sqrtf does not provide enough precision; since sqrt takes a double,
62 // there's no additional penalty to storing invScale in a double
63 double invScale;
64 if (sk_float_isfinite(magSq)) {
65 invScale = magSq;
66 } else {
67 // our magSq step overflowed to infinity, so use doubles instead.
68 // much slower, but needed when x, y or z is very large, otherwise we
69 // divide by inf. and return (0,0,0) vector.
70 double xx = fX;
71 double yy = fY;
72 double zz = fZ;
73 invScale = xx * xx + yy * yy + zz * zz;
74 }
75 // using a float instead of a double for scale loses too much precision
76 double scale = 1 / sqrt(invScale);
77 fX *= scale;
78 fY *= scale;
79 fZ *= scale;
80 if (!sk_float_isfinite(fX) || !sk_float_isfinite(fY) || !sk_float_isfinite(fZ)) {
81 this->set(0, 0, 0);
82 return false;
83 }
84 return true;
85 }
86