xref: /aosp_15_r20/external/skia/src/core/SkPoint3.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "include/private/base/SkFloatingPoint.h"
10 
11 #include <cmath>
12 
13 // Returns the square of the Euclidian distance to (x,y,z).
get_length_squared(float x,float y,float z)14 static inline float get_length_squared(float x, float y, float z) {
15     return x * x + y * y + z * z;
16 }
17 
18 // Calculates the square of the Euclidian distance to (x,y,z) and stores it in
19 // *lengthSquared.  Returns true if the distance is judged to be "nearly zero".
20 //
21 // This logic is encapsulated in a helper method to make it explicit that we
22 // always perform this check in the same manner, to avoid inconsistencies
23 // (see http://code.google.com/p/skia/issues/detail?id=560 ).
is_length_nearly_zero(float x,float y,float z,float * lengthSquared)24 static inline bool is_length_nearly_zero(float x, float y, float z, float *lengthSquared) {
25     *lengthSquared = get_length_squared(x, y, z);
26     return *lengthSquared <= (SK_ScalarNearlyZero * SK_ScalarNearlyZero);
27 }
28 
Length(SkScalar x,SkScalar y,SkScalar z)29 SkScalar SkPoint3::Length(SkScalar x, SkScalar y, SkScalar z) {
30     float magSq = get_length_squared(x, y, z);
31     if (SkIsFinite(magSq)) {
32         return std::sqrt(magSq);
33     } else {
34         double xx = x;
35         double yy = y;
36         double zz = z;
37         return (float)sqrt(xx * xx + yy * yy + zz * zz);
38     }
39 }
40 
41 /*
42  *  We have to worry about 2 tricky conditions:
43  *  1. underflow of magSq (compared against nearlyzero^2)
44  *  2. overflow of magSq (compared w/ isfinite)
45  *
46  *  If we underflow, we return false. If we overflow, we compute again using
47  *  doubles, which is much slower (3x in a desktop test) but will not overflow.
48  */
normalize()49 bool SkPoint3::normalize() {
50     float magSq;
51     if (is_length_nearly_zero(fX, fY, fZ, &magSq)) {
52         this->set(0, 0, 0);
53         return false;
54     }
55     // sqrtf does not provide enough precision; since sqrt takes a double,
56     // there's no additional penalty to storing invScale in a double
57     double invScale;
58     if (SkIsFinite(magSq)) {
59         invScale = magSq;
60     } else {
61         // our magSq step overflowed to infinity, so use doubles instead.
62         // much slower, but needed when x, y or z is very large, otherwise we
63         // divide by inf. and return (0,0,0) vector.
64         double xx = fX;
65         double yy = fY;
66         double zz = fZ;
67         invScale = xx * xx + yy * yy + zz * zz;
68     }
69     // using a float instead of a double for scale loses too much precision
70     double scale = 1 / sqrt(invScale);
71     fX *= scale;
72     fY *= scale;
73     fZ *= scale;
74     if (!SkIsFinite(fX, fY, fZ)) {
75         this->set(0, 0, 0);
76         return false;
77     }
78     return true;
79 }
80