2 * Copyright 2012 Google, Inc. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * Google Author(s): Behdad Esfahbod, Maysum Panju, Wojciech Baranowski
23 #include "glyphy-common.hh"
24 #include "glyphy-geometry.hh"
26 using namespace GLyphy::Geometry;
29 glyphy_sdf_from_arc_list (const glyphy_arc_endpoint_t *endpoints,
30 unsigned int num_endpoints,
31 const glyphy_point_t *p,
32 glyphy_point_t *closest_p /* may be NULL; TBD not implemented yet */)
36 Arc closest_arc (p0, p0, 0);
37 double min_dist = GLYPHY_INFINITY;
39 for (unsigned int i = 0; i < num_endpoints; i++) {
40 const glyphy_arc_endpoint_t &endpoint = endpoints[i];
41 if (endpoint.d == GLYPHY_INFINITY) {
45 Arc arc (p0, endpoint.p, endpoint.d);
48 if (arc.wedge_contains_point (c)) {
49 double sdist = arc.distance_to_point (c); /* TODO This distance has the wrong sign. Fix */
50 double udist = fabs (sdist) * (1 - GLYPHY_EPSILON);
51 if (udist <= min_dist) {
53 side = sdist >= 0 ? -1 : +1;
56 double udist = std::min ((arc.p0 - c).len (), (arc.p1 - c).len ());
57 if (udist < min_dist) {
59 side = 0; /* unsure */
61 } else if (side == 0 && udist == min_dist) {
62 /* If this new distance is the same as the current minimum,
63 * compare extended distances. Take the sign from the arc
64 * with larger extended distance. */
65 double old_ext_dist = closest_arc.extended_dist (c);
66 double new_ext_dist = arc.extended_dist (c);
68 double ext_dist = fabs (new_ext_dist) <= fabs (old_ext_dist) ?
69 old_ext_dist : new_ext_dist;
71 /* For emboldening and stuff: */
72 // min_dist = fabs (ext_dist);
73 side = ext_dist >= 0 ? +1 : -1;
79 // Technically speaking this should not happen, but it does. So try to fix it.
80 double ext_dist = closest_arc.extended_dist (c);
81 side = ext_dist >= 0 ? +1 : -1;
84 return side * min_dist;