1 /* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef LoopBlinnMathUtils_h 27 #define LoopBlinnMathUtils_h 28 29 #include "FloatPoint.h" 30 #include "FloatPoint3D.h" 31 #include <math.h> 32 33 namespace WebCore { 34 35 // Use a namespace for these so we can easily import them. 36 namespace LoopBlinnMathUtils { 37 38 float roundToZero(float val); 39 bool approxEqual(const FloatPoint& v0, const FloatPoint& v1); 40 bool approxEqual(const FloatPoint3D& v0, const FloatPoint3D& v1); 41 bool approxEqual(float f0, float f1); 42 43 // Determines whether the line segment between (p1, q1) intersects 44 // that between (p2, q2). 45 bool linesIntersect(const FloatPoint& p1, 46 const FloatPoint& q1, 47 const FloatPoint& p2, 48 const FloatPoint& q2); 49 50 // Determines whether "point" is inside the 2D triangle defined by 51 // vertices a, b, and c. This test defines that points exactly on an 52 // edge are not considered to be inside the triangle. 53 bool pointInTriangle(const FloatPoint& point, 54 const FloatPoint& a, 55 const FloatPoint& b, 56 const FloatPoint& c); 57 58 // Determines whether the triangles defined by the points (a1, b1, c1) 59 // and (a2, b2, c2) overlap. The definition of this function is that 60 // if the two triangles only share an adjacent edge or vertex, they 61 // are not considered to overlap. 62 bool trianglesOverlap(const FloatPoint& a1, 63 const FloatPoint& b1, 64 const FloatPoint& c1, 65 const FloatPoint& a2, 66 const FloatPoint& b2, 67 const FloatPoint& c2); 68 69 // Given a src cubic bezier, chops it at the specified t value, 70 // where 0 < t < 1, and returns the two new cubics in dst[0..3] 71 // and dst[3..6]. 72 void chopCubicAt(const FloatPoint src[4], FloatPoint dst[7], float t); 73 74 // "X-Ray" queries. An XRay is a half-line originating at the given 75 // point and extending to x=+infinity. 76 typedef FloatPoint XRay; 77 78 // Given an arbitrary cubic bezier, return the number of times an XRay 79 // crosses the cubic. Valid return values are [0..3]. 80 // 81 // By definition the cubic is open at the starting point; in other 82 // words, if pt.fY is equivalent to cubic[0].fY, and pt.fX is to the 83 // left of the curve, the line is not considered to cross the curve, 84 // but if it is equal to cubic[3].fY then it is considered to 85 // cross. 86 // 87 // Outgoing "ambiguous" argument indicates whether the answer is ambiguous 88 // because the query occurred exactly at one of the endpoints' y 89 // coordinates or at a tangent point, indicating that another query y 90 // coordinate is preferred for robustness. 91 int numXRayCrossingsForCubic(const XRay& xRay, 92 const FloatPoint cubic[4], 93 bool& ambiguous); 94 95 // Given a line segment from lineEndpoints[0] to lineEndpoints[1], and an 96 // XRay, returns true if they intersect. Outgoing "ambiguous" argument 97 // indicates whether the answer is ambiguous because the query occurred 98 // exactly at one of the endpoints' y coordinates, indicating that another 99 // query y coordinate is preferred for robustness. 100 bool xRayCrossesLine(const XRay& xRay, 101 const FloatPoint lineEndpoints[2], 102 bool& ambiguous); 103 104 105 bool isConvex(const FloatPoint* vertices, int nVertices); 106 107 } // namespace LoopBlinnMathUtils 108 109 } // namespace WebCore 110 111 #endif // LoopBlinnMathUtils_h 112