Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkPoint3.cpp
Go to the documentation of this file.
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
10
11#include <cmath>
12
13// Returns the square of the Euclidian distance to (x,y,z).
14static 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 ).
24static 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
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 */
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}
static bool SkIsFinite(T x, Pack... values)
static float get_length_squared(float x, float y, float z)
Definition SkPoint3.cpp:14
static bool is_length_nearly_zero(float x, float y, float z, float *lengthSquared)
Definition SkPoint3.cpp:24
#define SK_ScalarNearlyZero
Definition SkScalar.h:99
float SkScalar
Definition extension.cpp:12
double y
double x
const Scalar scale
SkScalar fX
Definition SkPoint3.h:16
SkScalar x() const
Definition SkPoint3.h:24
static SkScalar Length(SkScalar x, SkScalar y, SkScalar z)
Definition SkPoint3.cpp:29
SkScalar y() const
Definition SkPoint3.h:25
SkScalar fZ
Definition SkPoint3.h:16
bool normalize()
Definition SkPoint3.cpp:49
void set(SkScalar x, SkScalar y, SkScalar z)
Definition SkPoint3.h:28
SkScalar z() const
Definition SkPoint3.h:26
SkScalar fY
Definition SkPoint3.h:16