citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 7722796e9a0c6ce95a5674af4ed06e285b6a958c
parent 65287d2284dca224e628a3ce4361572e3497a907
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Sat, 19 Mar 2016 20:20:43 -0600

viewer: move glyphy into viewer

Diffstat:
MMakefile | 2+-
Dglyphy/Makefile | 10----------
Dglyphy/glyphy-arc-bezier.hh | 228-------------------------------------------------------------------------------
Dglyphy/glyphy-blob.cxx | 328-------------------------------------------------------------------------------
Dglyphy/glyphy-extents.cxx | 89-------------------------------------------------------------------------------
Dglyphy/glyphy-geometry.hh | 742-------------------------------------------------------------------------------
Mviewer/Makefile | 19++++++++++++++-----
Mviewer/demo-common.h | 2+-
Mviewer/demo-font.cxx | 2+-
Aviewer/glyphy/Makefile | 15+++++++++++++++
Aviewer/glyphy/glyphy-arc-bezier.hh | 228+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rglyphy/glyphy-arc.cxx -> viewer/glyphy/glyphy-arc.cxx | 0
Rglyphy/glyphy-arcs-bezier.hh -> viewer/glyphy/glyphy-arcs-bezier.hh | 0
Rglyphy/glyphy-arcs.cxx -> viewer/glyphy/glyphy-arcs.cxx | 0
Aviewer/glyphy/glyphy-blob.cxx | 328+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rglyphy/glyphy-common-glsl.h -> viewer/glyphy/glyphy-common-glsl.h | 0
Rglyphy/glyphy-common.hh -> viewer/glyphy/glyphy-common.hh | 0
Aviewer/glyphy/glyphy-extents.cxx | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rglyphy/glyphy-freetype.h -> viewer/glyphy/glyphy-freetype.h | 0
Aviewer/glyphy/glyphy-geometry.hh | 742+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rglyphy/glyphy-outline.cxx -> viewer/glyphy/glyphy-outline.cxx | 0
Rglyphy/glyphy-sdf-glsl.h -> viewer/glyphy/glyphy-sdf-glsl.h | 0
Rglyphy/glyphy-sdf.cxx -> viewer/glyphy/glyphy-sdf.cxx | 0
Rglyphy/glyphy-shaders.cxx -> viewer/glyphy/glyphy-shaders.cxx | 0
Rglyphy/glyphy.h -> viewer/glyphy/glyphy.h | 0
25 files changed, 1419 insertions(+), 1405 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -SUBDIR= glyphy instrument lib viewer +SUBDIR= instrument lib viewer/glyphy viewer test: all prove diff --git a/glyphy/Makefile b/glyphy/Makefile @@ -1,10 +0,0 @@ -LIB= glyphy -SRCS= glyphy-arc.cxx glyphy-arcs.cxx glyphy-blob.cxx glyphy-extents.cxx -SRCS += glyphy-outline.cxx glyphy-sdf.cxx glyphy-shaders.cxx - -CXXFLAGS += -I. -DPKGDATADIR="" -LDADD += -lstdc++ - -NOPROFILE=1 -NO_PROFILE=1 -.include <bsd.lib.mk> diff --git a/glyphy/glyphy-arc-bezier.hh b/glyphy/glyphy-arc-bezier.hh @@ -1,228 +0,0 @@ -/* - * Copyright 2012,2013 Google, Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Google Author(s): Behdad Esfahbod, Maysum Panju - */ - -#ifndef GLYPHY_ARC_BEZIER_HH -#define GLYPHY_ARC_BEZIER_HH - -#include "glyphy-common.hh" -#include "glyphy-geometry.hh" - -namespace GLyphy { -namespace ArcBezier { - -using namespace Geometry; - - -class MaxDeviationApproximatorExact -{ - public: - /* Returns 3 max(abs(d₀ t (1-t)² + d₁ t² (1-t)) for 0≤t≤1. */ - static double approximate_deviation (double d0, double d1) - { - double candidates[4] = {0,1}; - unsigned int num_candidates = 2; - if (d0 == d1) - candidates[num_candidates++] = .5; - else { - double delta = d0*d0 - d0*d1 + d1*d1; - double t2 = 1. / (3 * (d0 - d1)); - double t0 = (2 * d0 - d1) * t2; - if (delta == 0) - candidates[num_candidates++] = t0; - else if (delta > 0) { - /* This code can be optimized to avoid the sqrt if the solution - * is not feasible (ie. lies outside (0,1)). I have implemented - * that in cairo-spline.c:_cairo_spline_bound(). Can be reused - * here. - */ - double t1 = sqrt (delta) * t2; - candidates[num_candidates++] = t0 - t1; - candidates[num_candidates++] = t0 + t1; - } - } - - double e = 0; - for (unsigned int i = 0; i < num_candidates; i++) { - double t = candidates[i]; - double ee; - if (t < 0. || t > 1.) - continue; - ee = fabs (3 * t * (1-t) * (d0 * (1 - t) + d1 * t)); - e = std::max (e, ee); - } - - return e; - } -}; - - - -template <class MaxDeviationApproximator> -class ArcBezierErrorApproximatorBehdad -{ - public: - static double approximate_bezier_arc_error (const Bezier &b0, const Arc &a) - { - assert (b0.p0 == a.p0); - assert (b0.p3 == a.p1); - - double ea; - Bezier b1 = a.approximate_bezier (&ea); - - assert (b0.p0 == b1.p0); - assert (b0.p3 == b1.p3); - - Vector v0 = b1.p1 - b0.p1; - Vector v1 = b1.p2 - b0.p2; - - Vector b = (b0.p3 - b0.p0).normalized (); - v0 = v0.rebase (b); - v1 = v1.rebase (b); - - Vector v (MaxDeviationApproximator::approximate_deviation (v0.dx, v1.dx), - MaxDeviationApproximator::approximate_deviation (v0.dy, v1.dy)); - - /* Edge cases: If d*d is too close too large default to a weak bound. */ - if (a.d * a.d > 1. - 1e-4) - return ea + v.len (); - - /* If the wedge doesn't contain control points, default to weak bound. */ - if (!a.wedge_contains_point (b0.p1) || !a.wedge_contains_point (b0.p2)) - return ea + v.len (); - - /* If straight line, return the max ortho deviation. */ - if (fabs (a.d) < 1e-6) - return ea + v.dy; - - /* We made sure that fabs(a.d) < 1 */ - double tan_half_alpha = fabs (tan2atan (a.d)); - - double tan_v = v.dx / v.dy; - - double eb; - if (fabs (tan_v) <= tan_half_alpha) - return ea + v.len (); - - double c2 = (a.p1 - a.p0).len () * .5; - double r = a.radius (); - - eb = Vector (c2 + v.dx, c2 / tan_half_alpha + v.dy).len () - r; - assert (eb >= 0); - - return ea + eb; - } -}; - - - -template <class ArcBezierErrorApproximator> -class ArcBezierApproximatorMidpointSimple -{ - public: - static const Arc approximate_bezier_with_arc (const Bezier &b, double *error) - { - Arc a (b.p0, b.p3, b.midpoint (), false); - - *error = ArcBezierErrorApproximator::approximate_bezier_arc_error (b, a); - - return a; - } -}; - -template <class ArcBezierErrorApproximator> -class ArcBezierApproximatorMidpointTwoPart -{ - public: - static const Arc approximate_bezier_with_arc (const Bezier &b, double *error, double mid_t = .5) - { - Pair<Bezier > pair = b.split (mid_t); - Point m = pair.second.p0; - - Arc a0 (b.p0, m, b.p3, true); - Arc a1 (m, b.p3, b.p0, true); - - double e0 = ArcBezierErrorApproximator::approximate_bezier_arc_error (pair.first, a0); - double e1 = ArcBezierErrorApproximator::approximate_bezier_arc_error (pair.second, a1); - *error = std::max (e0, e1); - - return Arc (b.p0, b.p3, m, false); - } -}; - -template <class ArcBezierErrorApproximator> -class ArcBezierApproximatorQuantized -{ - public: - ArcBezierApproximatorQuantized (double _max_d = GLYPHY_INFINITY, unsigned int _d_bits = 0) : - max_d (_max_d), d_bits (_d_bits) {}; - - protected: - double max_d; - unsigned int d_bits; - - public: - const Arc approximate_bezier_with_arc (const Bezier &b, double *error) const - { - double mid_t = .5; - Arc a (b.p0, b.p3, b.point (mid_t), false); - Arc orig_a = a; - - if (isfinite (max_d)) { - assert (max_d >= 0); - if (fabs (a.d) > max_d) - a.d = a.d < 0 ? -max_d : max_d; - } - if (d_bits && max_d != 0) { - assert (isfinite (max_d)); - assert (fabs (a.d) <= max_d); - int mult = (1 << (d_bits - 1)) - 1; - int id = round (a.d / max_d * mult); - assert (-mult <= id && id <= mult); - a.d = id * max_d / mult; - assert (fabs (a.d) <= max_d); - } - - /* Error introduced by arc quantization */ - double ed = fabs (a.d - orig_a.d) * (a.p1 - a.p0).len () * .5; - - ArcBezierApproximatorMidpointTwoPart<ArcBezierErrorApproximator> - ::approximate_bezier_with_arc (b, error, mid_t); - - if (ed) { - *error += ed; - - /* Try a simple one-arc approx which works with the quantized arc. - * May produce smaller error bound. */ - double e = ArcBezierErrorApproximator::approximate_bezier_arc_error (b, a); - if (e < *error) - *error = e; - } - - return a; - } -}; - -typedef MaxDeviationApproximatorExact MaxDeviationApproximatorDefault; -typedef ArcBezierErrorApproximatorBehdad<MaxDeviationApproximatorDefault> ArcBezierErrorApproximatorDefault; -typedef ArcBezierApproximatorMidpointTwoPart<ArcBezierErrorApproximatorDefault> ArcBezierApproximatorDefault; -typedef ArcBezierApproximatorQuantized<ArcBezierErrorApproximatorDefault> ArcBezierApproximatorQuantizedDefault; - -} /* namespace ArcBezier */ -} /* namespace GLyphy */ - -#endif /* GLYPHY_ARC_BEZIER_HH */ diff --git a/glyphy/glyphy-blob.cxx b/glyphy/glyphy-blob.cxx @@ -1,328 +0,0 @@ -/* - * Copyright 2012 Google, Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Google Author(s): Behdad Esfahbod, Maysum Panju, Wojciech Baranowski - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include "glyphy-common.hh" -#include "glyphy-geometry.hh" - -#define GRID_SIZE 24 - -using namespace GLyphy::Geometry; - - -#define UPPER_BITS(v,bits,total_bits) ((v) >> ((total_bits) - (bits))) -#define LOWER_BITS(v,bits,total_bits) ((v) & ((1 << (bits)) - 1)) - -#define MAX_X 4095 -#define MAX_Y 4095 - -static inline glyphy_rgba_t -arc_endpoint_encode (unsigned int ix, unsigned int iy, double d) -{ - glyphy_rgba_t v; - - /* 12 bits for each of x and y, 8 bits for d */ - assert (ix <= MAX_X); - assert (iy <= MAX_Y); - unsigned int id; - if (isinf (d)) - id = 0; - else { - assert (fabs (d) <= GLYPHY_MAX_D); - id = 128 + lround (d * 127 / GLYPHY_MAX_D); - } - assert (id < 256); - - v.r = id; - v.g = LOWER_BITS (ix, 8, 12); - v.b = LOWER_BITS (iy, 8, 12); - v.a = ((ix >> 8) << 4) | (iy >> 8); - return v; -} - -static inline glyphy_rgba_t -arc_list_encode (unsigned int offset, unsigned int num_points, int side) -{ - glyphy_rgba_t v; - v.r = 0; // unused for arc-list encoding - v.g = UPPER_BITS (offset, 8, 16); - v.b = LOWER_BITS (offset, 8, 16); - v.a = LOWER_BITS (num_points, 8, 8); - if (side < 0 && !num_points) - v.a = 255; - return v; -} - -static inline glyphy_rgba_t -line_encode (const Line &line) -{ - Line l = line.normalized (); - double angle = l.n.angle (); - double distance = l.c; - - int ia = lround (-angle / M_PI * 0x7FFF); - unsigned int ua = ia + 0x8000; - assert (0 == (ua & ~0xFFFF)); - - int id = lround (distance * 0x1FFF); - unsigned int ud = id + 0x4000; - assert (0 == (ud & ~0x7FFF)); - - /* Marker for line-encoded */ - ud |= 0x8000; - - glyphy_rgba_t v; - v.r = ud >> 8; - v.g = ud & 0xFF; - v.b = ua >> 8; - v.a = ua & 0xFF; - return v; -} - - -/* Given a cell, fills the vector closest_arcs with arcs that may be closest to some point in the cell. - * Uses idea that all close arcs to cell must be ~close to center of cell. - */ -static void -closest_arcs_to_cell (Point c0, Point c1, /* corners */ - double faraway, - const glyphy_arc_endpoint_t *endpoints, - unsigned int num_endpoints, - std::vector<glyphy_arc_endpoint_t> &near_endpoints, - int *side) -{ - // Find distance between cell center - Point c = c0.midpoint (c1); - double min_dist = glyphy_sdf_from_arc_list (endpoints, num_endpoints, &c, NULL); - - *side = min_dist >= 0 ? +1 : -1; - min_dist = fabs (min_dist); - std::vector<Arc> near_arcs; - - // If d is the distance from the center of the square to the nearest arc, then - // all nearest arcs to the square must be at most almost [d + half_diagonal] from the center. - double half_diagonal = (c - c0).len (); - double radius_squared = pow (min_dist + half_diagonal, 2); - if (min_dist - half_diagonal <= faraway) { - Point p0 (0, 0); - for (unsigned int i = 0; i < num_endpoints; i++) { - const glyphy_arc_endpoint_t &endpoint = endpoints[i]; - if (endpoint.d == GLYPHY_INFINITY) { - p0 = endpoint.p; - continue; - } - Arc arc (p0, endpoint.p, endpoint.d); - p0 = endpoint.p; - - if (arc.squared_distance_to_point (c) <= radius_squared) - near_arcs.push_back (arc); - } - } - - Point p1 = Point (0, 0); - for (unsigned i = 0; i < near_arcs.size (); i++) - { - Arc arc = near_arcs[i]; - - if (i == 0 || p1 != arc.p0) { - glyphy_arc_endpoint_t endpoint = {arc.p0, GLYPHY_INFINITY}; - near_endpoints.push_back (endpoint); - p1 = arc.p0; - } - - glyphy_arc_endpoint_t endpoint = {arc.p1, arc.d}; - near_endpoints.push_back (endpoint); - p1 = arc.p1; - } -} - - -glyphy_bool_t -glyphy_arc_list_encode_blob (const glyphy_arc_endpoint_t *endpoints, - unsigned int num_endpoints, - glyphy_rgba_t *blob, - unsigned int blob_size, - double faraway, - double avg_fetch_desired, - double *avg_fetch_achieved, - unsigned int *output_len, - unsigned int *nominal_width, /* 8bit */ - unsigned int *nominal_height, /* 8bit */ - glyphy_extents_t *pextents) -{ - glyphy_extents_t extents; - glyphy_extents_clear (&extents); - - glyphy_arc_list_extents (endpoints, num_endpoints, &extents); - - if (glyphy_extents_is_empty (&extents)) { - *pextents = extents; - if (!blob_size) - return false; - *blob = arc_list_encode (0, 0, +1); - *avg_fetch_achieved = 1; - *output_len = 1; - *nominal_width = *nominal_height = 1; - return true; - } - - /* Add antialiasing padding */ - extents.min_x -= faraway; - extents.min_y -= faraway; - extents.max_x += faraway; - extents.max_y += faraway; - - double glyph_width = extents.max_x - extents.min_x; - double glyph_height = extents.max_y - extents.min_y; - double unit = std::max (glyph_width, glyph_height); - - unsigned int grid_w = GRID_SIZE; - unsigned int grid_h = GRID_SIZE; - - if (glyph_width > glyph_height) { - while ((grid_h - 1) * unit / grid_w > glyph_height) - grid_h--; - glyph_height = grid_h * unit / grid_w; - extents.max_y = extents.min_y + glyph_height; - } else { - while ((grid_w - 1) * unit / grid_h > glyph_width) - grid_w--; - glyph_width = grid_w * unit / grid_h; - extents.max_x = extents.min_x + glyph_width; - } - - double cell_unit = unit / std::max (grid_w, grid_h); - - std::vector<glyphy_rgba_t> tex_data; - std::vector<glyphy_arc_endpoint_t> near_endpoints; - - unsigned int header_length = grid_w * grid_h; - unsigned int offset = header_length; - tex_data.resize (header_length); - Point origin = Point (extents.min_x, extents.min_y); - unsigned int total_arcs = 0; - - for (unsigned int row = 0; row < grid_h; row++) - for (unsigned int col = 0; col < grid_w; col++) - { - Point cp0 = origin + Vector ((col + 0) * cell_unit, (row + 0) * cell_unit); - Point cp1 = origin + Vector ((col + 1) * cell_unit, (row + 1) * cell_unit); - near_endpoints.clear (); - - int side; - closest_arcs_to_cell (cp0, cp1, - faraway, - endpoints, num_endpoints, - near_endpoints, - &side); - -#define QUANTIZE_X(X) (lround (MAX_X * ((X - extents.min_x) / glyph_width ))) -#define QUANTIZE_Y(Y) (lround (MAX_Y * ((Y - extents.min_y) / glyph_height))) -#define DEQUANTIZE_X(X) (double (X) / MAX_X * glyph_width + extents.min_x) -#define DEQUANTIZE_Y(Y) (double (Y) / MAX_Y * glyph_height + extents.min_y) -#define SNAP(P) (Point (DEQUANTIZE_X (QUANTIZE_X ((P).x)), DEQUANTIZE_Y (QUANTIZE_Y ((P).y)))) - - if (near_endpoints.size () == 2 && near_endpoints[1].d == 0) { - Point c (extents.min_x + glyph_width * .5, extents.min_y + glyph_height * .5); - Line line (SNAP (near_endpoints[0].p), SNAP (near_endpoints[1].p)); - line.c -= line.n * Vector (c); - line.c /= unit; - tex_data[row * grid_w + col] = line_encode (line); - continue; - } - - /* If the arclist is two arcs that can be combined in encoding if reordered, - * do that. */ - if (near_endpoints.size () == 4 && - isinf (near_endpoints[2].d) && - near_endpoints[0].p.x == near_endpoints[3].p.x && - near_endpoints[0].p.y == near_endpoints[3].p.y) - { - glyphy_arc_endpoint_t e0, e1, e2; - e0 = near_endpoints[2]; - e1 = near_endpoints[3]; - e2 = near_endpoints[1]; - near_endpoints.resize (0); - near_endpoints.push_back (e0); - near_endpoints.push_back (e1); - near_endpoints.push_back (e2); - } - - for (unsigned i = 0; i < near_endpoints.size (); i++) { - glyphy_arc_endpoint_t &endpoint = near_endpoints[i]; - tex_data.push_back (arc_endpoint_encode (QUANTIZE_X(endpoint.p.x), QUANTIZE_Y(endpoint.p.y), endpoint.d)); - } - - unsigned int current_endpoints = tex_data.size () - offset; - - if (current_endpoints) - { - /* See if we can fulfill this cell by using already-encoded arcs */ - const glyphy_rgba_t *needle = &tex_data[offset]; - unsigned int needle_len = current_endpoints; - const glyphy_rgba_t *haystack = &tex_data[header_length]; - unsigned int haystack_len = offset - header_length; - - bool found = false; - while (haystack_len >= needle_len) { - /* Trick: we don't care about first endpoint's d value, so skip one - * byte in comparison. This works because arc_encode() packs the - * d value in the first byte. */ - if (0 == memcmp (1 + (const char *) needle, - 1 + (const char *) haystack, - needle_len * sizeof (*needle) - 1)) { - found = true; - break; - } - haystack++; - haystack_len--; - } - if (found) { - unsigned int new_offset = haystack - &tex_data[0]; - tex_data.resize (offset); - haystack = needle = NULL; /* Invalidated by the resize. */ - offset = new_offset; - } - } - else - offset = 0; - - tex_data[row * grid_w + col] = arc_list_encode (offset, current_endpoints, side); - offset = tex_data.size (); - - total_arcs += current_endpoints; - } - - if (avg_fetch_achieved) - *avg_fetch_achieved = 1 + double (total_arcs) / (grid_w * grid_h); - - *pextents = extents; - - if (tex_data.size () > blob_size) - return false; - - memcpy (blob, &tex_data[0], tex_data.size () * sizeof(tex_data[0])); - *output_len = tex_data.size (); - *nominal_width = grid_w; - *nominal_height = grid_h; - - return true; -} diff --git a/glyphy/glyphy-extents.cxx b/glyphy/glyphy-extents.cxx @@ -1,89 +0,0 @@ -/* - * Copyright 2012 Google, Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Google Author(s): Behdad Esfahbod - */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include "glyphy-common.hh" - - -void -glyphy_extents_clear (glyphy_extents_t *extents) -{ - extents->min_x = GLYPHY_INFINITY; - extents->min_y = GLYPHY_INFINITY; - extents->max_x = -GLYPHY_INFINITY; - extents->max_y = -GLYPHY_INFINITY; -} - -glyphy_bool_t -glyphy_extents_is_empty (const glyphy_extents_t *extents) -{ - return isinf (extents->min_x); -} - -void -glyphy_extents_add (glyphy_extents_t *extents, - const glyphy_point_t *p) -{ - if (glyphy_extents_is_empty (extents)) { - extents->min_x = extents->max_x = p->x; - extents->min_y = extents->max_y = p->y; - return; - } - extents->min_x = std::min (extents->min_x, p->x); - extents->min_y = std::min (extents->min_y, p->y); - extents->max_x = std::max (extents->max_x, p->x); - extents->max_y = std::max (extents->max_y, p->y); -} - -void -glyphy_extents_extend (glyphy_extents_t *extents, - const glyphy_extents_t *other) -{ - if (glyphy_extents_is_empty (other)) - return; - if (glyphy_extents_is_empty (extents)) { - *extents = *other; - return; - } - extents->min_x = std::min (extents->min_x, other->min_x); - extents->min_y = std::min (extents->min_y, other->min_y); - extents->max_x = std::max (extents->max_x, other->max_x); - extents->max_y = std::max (extents->max_y, other->max_y); -} - -glyphy_bool_t -glyphy_extents_includes (const glyphy_extents_t *extents, - const glyphy_point_t *p) -{ - return extents->min_x <= p->x && p->x <= extents->max_x && - extents->min_y <= p->y && p->y <= extents->max_y; -} - -void -glyphy_extents_scale (glyphy_extents_t *extents, - double x_scale, - double y_scale) -{ - extents->min_x *= x_scale; - extents->max_x *= x_scale; - extents->min_y *= y_scale; - extents->max_y *= y_scale; -} diff --git a/glyphy/glyphy-geometry.hh b/glyphy/glyphy-geometry.hh @@ -1,742 +0,0 @@ -/* - * Copyright 2012,2013 Google, Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Google Author(s): Behdad Esfahbod, Maysum Panju - */ - -#ifndef GLYPHY_GEOMETRY_HH -#define GLYPHY_GEOMETRY_HH - -#include "glyphy-common.hh" - -namespace GLyphy { -namespace Geometry { - -template <typename Type> struct Pair; -struct Vector; -struct SignedVector; -struct Point; -struct Line; -struct Segment; -struct Arc; -struct Bezier; - -/* returns tan (2 * atan (d)) */ -inline double tan2atan (double d) { return 2 * d / (1 - d*d); } - -/* returns sin (2 * atan (d)) */ -inline double sin2atan (double d) { return 2 * d / (1 + d*d); } - -/* returns cos (2 * atan (d)) */ -inline double cos2atan (double d) { return (1 - d*d) / (1 + d*d); } - -template <typename Type> -struct Pair { - typedef Type ElementType; - - inline Pair (const Type &first_, const Type &second_) : first (first_), second (second_) {} - - Type first, second; -}; - -struct Point : glyphy_point_t { - inline Point (double x_, double y_) { x = x_; y = y_; } - inline explicit Point (const Vector &v); - inline Point (const glyphy_point_t &p) { *(glyphy_point_t *)this = p; } - - inline bool operator == (const Point &p) const; - inline bool operator != (const Point &p) const; - inline Point& operator+= (const Vector &v); - inline Point& operator-= (const Vector &v); - inline const Point operator+ (const Vector &v) const; - inline const Point operator- (const Vector &v) const; - inline const Vector operator- (const Point &p) const; - inline const Point midpoint (const Point &p) const; - inline const Line bisector (const Point &p) const; - inline double distance_to_point (const Point &p) const; /* distance to point! */ - inline double squared_distance_to_point (const Point &p) const; /* square of distance to point! */ - - inline bool is_finite (void) const; - inline const Point lerp (const double &a, const Point &p) const; -}; - -struct Vector { - inline Vector (double dx_, double dy_) : dx (dx_), dy (dy_) {} - inline explicit Vector (const Point &p) : dx (p.x), dy (p.y) {} - - inline bool operator == (const Vector &v) const; - inline bool operator != (const Vector &v) const; - inline const Vector operator+ (void) const; - inline const Vector operator- (void) const; - inline Vector& operator+= (const Vector &v); - inline Vector& operator-= (const Vector &v); - inline Vector& operator*= (const double &s); - inline Vector& operator/= (const double &s); - inline const Vector operator+ (const Vector &v) const; - inline const Vector operator- (const Vector &v) const; - inline const Vector operator* (const double &s) const; - inline const Vector operator/ (const double &s) const; - inline double operator* (const Vector &v) const; /* dot product */ - inline const Point operator+ (const Point &p) const; - - inline bool is_nonzero (void) const; - inline double len (void) const; - inline double len2 (void) const; - inline const Vector normalized (void) const; - inline const Vector ortho (void) const; - inline const Vector normal (void) const; /* ortho().normalized() */ - inline double angle (void) const; - - inline double cross (const Vector &other) const; - inline const Vector rebase (const Vector &bx, const Vector &by) const; - inline const Vector rebase (const Vector &bx) const; - - double dx, dy; -}; - -struct SignedVector : Vector { - inline SignedVector (const Vector &v, bool negative_) : Vector (v), negative (negative_) {} - - inline bool operator == (const SignedVector &v) const; - inline bool operator != (const SignedVector &v) const; - inline const SignedVector operator- (void) const; - - bool negative; -}; - -struct Line { - inline Line (double a_, double b_, double c_) : n (a_, b_), c (c_) {} - inline Line (Vector n_, double c_) : n (n_), c (c_) {} - inline Line (const Point &p0, const Point &p1) : - n ((p1 - p0).ortho ()), c (n * Vector (p0)) {} - - inline const Point operator+ (const Line &l) const; /* line intersection! */ - inline const SignedVector operator- (const Point &p) const; /* shortest vector from point to line */ - - - inline const Line normalized (void) const; - inline const Vector normal (void) const; - - Vector n; /* line normal */ - double c; /* n.dx*x + n.dy*y = c */ -}; - -struct Segment { - inline Segment (const Point &p0_, const Point &p1_) : - p0 (p0_), p1 (p1_) {} - - inline const SignedVector operator- (const Point &p) const; /* shortest vector from point to ***line*** */ - inline double distance_to_point (const Point &p) const; /* shortest distance from point to segment */ - inline double squared_distance_to_point (const Point &p) const; /* shortest distance squared from point to segment */ - inline bool contains_in_span (const Point &p) const; /* is p in the stripe formed by sliding this segment? */ - inline double max_distance_to_arc (const Arc &a) const; - - - Point p0; - Point p1; -}; - - - -struct Arc { - inline Arc (const Point &p0_, const Point &p1_, const Point &pm, bool complement) : - p0 (p0_), p1 (p1_), - d (p0_ == pm || p1_ == pm ? 0 : - tan (((p1_-pm).angle () - (p0_-pm).angle ()) / 2 - (complement ? 0 : M_PI_2))) {} - inline Arc (const Point &p0_, const Point &p1_, const double &d_) : - p0 (p0_), p1 (p1_), d (d_) {} - inline Arc (const Point &center, double radius, const double &a0, const double &a1, bool complement) : - p0 (center + Vector (cos(a0),sin(a0)) * radius), - p1 (center + Vector (cos(a1),sin(a1)) * radius), - d (tan ((a1 - a0) / 4 - (complement ? 0 : M_PI_2))) {} - inline Arc (const glyphy_arc_t &a) : p0 (a.p0), p1 (a.p1), d (a.d) {} - inline operator glyphy_arc_t (void) const { glyphy_arc_t a = {p0, p1, d}; return a; } - - inline bool operator == (const Arc &a) const; - inline bool operator != (const Arc &a) const; - inline const SignedVector operator- (const Point &p) const; /* shortest vector from point to arc */ - - inline double radius (void) const; - inline const Point center (void) const; - inline const Pair<Vector> tangents (void) const; - - inline Bezier approximate_bezier (double *error) const; - - inline bool wedge_contains_point (const Point &p) const; - inline double distance_to_point (const Point &p) const; - inline double squared_distance_to_point (const Point &p) const; - inline double extended_dist (const Point &p) const; - - inline void extents (glyphy_extents_t &extents) const; - - Point p0, p1; - double d; /* Depth */ -}; - -struct Bezier { - inline Bezier (const Point &p0_, const Point &p1_, - const Point &p2_, const Point &p3_) : - p0 (p0_), p1 (p1_), p2 (p2_), p3 (p3_) {} - - inline const Point point (const double &t) const; - inline const Point midpoint (void) const; - inline const Vector tangent (const double &t) const; - inline const Vector d_tangent (const double &t) const; - inline double curvature (const double &t) const; - inline const Pair<Bezier> split (const double &t) const; - inline const Pair<Bezier> halve (void) const; - inline const Bezier segment (const double &t0, const double &t1) const; - - Point p0, p1, p2, p3; -}; - - -/* Implementations */ - - -/* Point */ - -inline Point::Point (const Vector &v) { - x = v.dx; - y = v.dy; -} -inline bool Point::operator == (const Point &p) const { - return x == p.x && y == p.y; -} -inline bool Point::operator != (const Point &p) const { - return !(*this == p); -} -inline Point& Point::operator+= (const Vector &v) { - x += v.dx; - y += v.dy; - return *this; -} -inline Point& Point::operator-= (const Vector &v) { - x -= v.dx; - y -= v.dy; - return *this; -} -inline const Point Point::operator+ (const Vector &v) const { - return Point (*this) += v; -} -inline const Point Point::operator- (const Vector &v) const { - return Point (*this) -= v; -} -inline const Vector Point::operator- (const Point &p) const { - return Vector (x - p.x, y - p.y); -} - -inline const Point Point::midpoint (const Point &p) const { - return *this + (p - *this) / 2; -} -inline const Line Point::bisector (const Point &p) const { - Vector d = p - *this; - return Line (d.dx * 2, d.dy * 2, d * Vector (p) + d * Vector (*this)); -} - -inline double Point::distance_to_point (const Point &p) const { - return ((*this) - p).len (); -} - -inline double Point::squared_distance_to_point (const Point &p) const { - return ((*this) - p).len2 (); -} - -inline bool Point::is_finite (void) const { - return isfinite (x) && isfinite (y); -} -inline const Point Point::lerp (const double &a, const Point &p) const { - /* The following two cases are special-cased to get better floating - * point stability. We require that points that are the same be - * bit-equal. */ - if (a == 0) return *this; - if (a == 1.0) return p; - return Point ((1-a) * x + a * p.x, (1-a) * y + a * p.y); -} - - -/* Vector */ - -inline bool Vector::operator == (const Vector &v) const { - return dx == v.dx && dy == v.dy; -} -inline bool Vector::operator != (const Vector &v) const { - return !(*this == v); -} -inline const Vector Vector::operator+ (void) const { - return *this; -} -inline const Vector Vector::operator- (void) const { - return Vector (-dx, -dy); -} -inline Vector& Vector::operator+= (const Vector &v) { - dx += v.dx; - dy += v.dy; - return *this; -} -inline Vector& Vector::operator-= (const Vector &v) { - dx -= v.dx; - dy -= v.dy; - return *this; -} -inline Vector& Vector::operator*= (const double &s) { - dx *= s; - dy *= s; - return *this; -} -inline Vector& Vector::operator/= (const double &s) { - dx /= s; - dy /= s; - return *this; -} -inline const Vector Vector::operator+ (const Vector &v) const { - return Vector (*this) += v; -} -inline const Vector Vector::operator- (const Vector &v) const { - return Vector (*this) -= v; -} -inline const Vector Vector::operator* (const double &s) const { - return Vector (*this) *= s; -} -inline const Vector operator* (const double &s, const Vector &v) { - return v * s; -} -inline const Vector Vector::operator/ (const double &s) const { - return Vector (*this) /= s; -} -inline double Vector::operator* (const Vector &v) const { /* dot product */ - return dx * v.dx + dy * v.dy; -} -inline const Point Vector::operator+ (const Point &p) const { - return p + *this; -} - -inline bool Vector::is_nonzero (void) const { - return dx || dy; -} -inline double Vector::len (void) const { - return hypot (dx, dy); -} -inline double Vector::len2 (void) const { - return dx * dx + dy * dy; -} -inline const Vector Vector::normalized (void) const { - double d = len (); - return d ? *this / d : *this; -} -inline const Vector Vector::ortho (void) const { - return Vector (-dy, dx); -} -inline const Vector Vector::normal (void) const { - return ortho ().normalized (); -} -inline double Vector::angle (void) const { - return atan2 (dy, dx); -} - -inline double Vector::cross (const Vector &other) const { - return dx * other.dy - dy * other.dx; -} -inline const Vector Vector::rebase (const Vector &bx, - const Vector &by) const { - return Vector (*this * bx, *this * by); -} -inline const Vector Vector::rebase (const Vector &bx) const { - return rebase (bx, bx.ortho ()); -} - - -/* SignedVector */ - -inline bool SignedVector::operator == (const SignedVector &v) const { - return (const Vector &)(*this) == (const Vector &)(v) && negative == v.negative; -} -inline bool SignedVector::operator != (const SignedVector &v) const { - return !(*this == v); -} -inline const SignedVector SignedVector::operator- (void) const { - return SignedVector (-(const Vector &)(*this), !negative); -} - - -/* Line */ - -inline const Point Line::operator+ (const Line &l) const { - double det = n.dx * l.n.dy - n.dy * l.n.dx; - if (!det) - return Point (GLYPHY_INFINITY, GLYPHY_INFINITY); - return Point ((c * l.n.dy - n.dy * l.c) / det, - (n.dx * l.c - c * l.n.dx) / det); -} -inline const SignedVector Line::operator- (const Point &p) const { - double mag = -(n * Vector (p) - c) / n.len (); - return SignedVector (n.normalized () * mag, mag < 0); /******************************************************************************************* FIX. *************************************/ -} - -inline const SignedVector operator- (const Point &p, const Line &l) { - return -(l - p); -} - -inline const Line Line::normalized (void) const { - double d = n.len (); - return d ? Line (n / d, c / d) : *this; -} -inline const Vector Line::normal (void) const { - return n; -} - -/* Segment */ -inline const SignedVector Segment::operator- (const Point &p) const { - /* shortest vector from point to line */ - return p - Line (p1, p0); /************************************************************************************************** Should the order (p1, p0) depend on d?? ***********************/ -} - -/* Segment */ -inline bool Segment::contains_in_span (const Point &p) const { - if (p0 == p1) - return false; - - /* shortest vector from point to line */ - Line temp (p0, p1); - double mag = -(temp.n * Vector (p) - temp.c) / temp.n.len (); - Vector y (temp.n.normalized () * mag); - Point z = y + p; - - // Check if z is between p0 and p1. - - if (fabs (p1.y - p0.y) > fabs (p1.x - p0.x)) { - return ((z.y - p0.y > 0 && p1.y - p0.y > z.y - p0.y) || - (z.y - p0.y < 0 && p1.y - p0.y < z.y - p0.y)); - } - else { - return ((0 < z.x - p0.x && z.x - p0.x < p1.x - p0.x) || - (0 > z.x - p0.x && z.x - p0.x > p1.x - p0.x)); - } -} - -inline double Segment::distance_to_point (const Point &p) const { - if (p0 == p1) - return 0; - - // Check if z is between p0 and p1. - Line temp (p0, p1); - if (contains_in_span (p)) - return -(temp.n * Vector (p) - temp.c) / temp.n.len (); - - double dist_p_p0 = p.distance_to_point (p0); - double dist_p_p1 = p.distance_to_point (p1); - return (dist_p_p0 < dist_p_p1 ? dist_p_p0 : dist_p_p1) * (-(temp.n * Vector (p) - temp.c) < 0 ? -1 : 1); -} - - -inline double Segment::squared_distance_to_point (const Point &p) const { - if (p0 == p1) - return 0; - - // Check if z is between p0 and p1. - Line temp (p0, p1); - if (contains_in_span (p)) - return (temp.n * Vector (p) - temp.c) * (temp.n * Vector (p) - temp.c) / (temp.n * temp.n); - - double dist_p_p0 = p.squared_distance_to_point (p0); - double dist_p_p1 = p.squared_distance_to_point (p1); - return (dist_p_p0 < dist_p_p1 ? dist_p_p0 : dist_p_p1); -} - - -inline double Segment::max_distance_to_arc (const Arc &a) const { - double max_distance = fabs(a.distance_to_point(p0)) ; - return max_distance > fabs(a.distance_to_point(p1)) ? max_distance : fabs(a.distance_to_point(p1)) ; -} - - - -/* Arc */ - -inline bool Arc::operator == (const Arc &a) const { - return p0 == a.p0 && p1 == a.p1 && d == a.d; -} -inline bool Arc::operator != (const Arc &a) const { - return !(*this == a); -} - - -inline const SignedVector Arc::operator- (const Point &p) const { - - if (fabs(d) < 1e-5) { - Segment arc_segment (p0, p1); - return arc_segment - p; - } - if (wedge_contains_point (p)){ - Vector difference = (center () - p).normalized () * fabs (p.distance_to_point (center ()) - radius ()); - - return SignedVector (difference, ((p - center ()).len () < radius ()) ^ (d < 0)); - } - double d0 = p.squared_distance_to_point (p0); - double d1 = p.squared_distance_to_point (p1); - - Arc other_arc (p0, p1, (1.0 + d) / (1.0 - d)); /********************************* NOT Robust. But works? *****************/ - Vector normal = center () - (d0 < d1 ? p0 : p1) ; - - if (normal.len() == 0) - return SignedVector (Vector (0, 0), true); /************************************ Check sign of this S.D. *************/ - - return SignedVector (Line (normal.dx, normal.dy, normal * Vector ((d0 < d1 ? p0 : p1))) - p, !other_arc.wedge_contains_point(p)); -} - -inline const SignedVector operator- (const Point &p, const Arc &a) { - return -(a - p); -} - - - -inline double Arc::radius (void) const -{ - return fabs ((p1 - p0).len () / (2 * sin2atan (d))); -} - -inline const Point Arc::center (void) const -{ - return (p0.midpoint (p1)) + (p1 - p0).ortho () / (2 * tan2atan (d)); -} - -inline const Pair<Vector> Arc::tangents (void) const -{ - Vector dp = (p1 - p0) * .5; - Vector pp = dp.ortho () * -sin2atan (d); - dp = dp * cos2atan (d); - return Pair<Vector> (dp + pp, dp - pp); -} - - - -inline Bezier Arc::approximate_bezier (double *error) const -{ - Vector dp = p1 - p0; - Vector pp = dp.ortho (); - - if (error) - *error = dp.len () * pow (fabs (d), 5) / (54 * (1 + d*d)); - - dp *= ((1 - d*d) / 3); - pp *= (2 * d / 3); - - Point p0s = p0 + dp - pp; - Point p1s = p1 - dp - pp; - - return Bezier (p0, p0s, p1s, p1); -} - - -inline bool Arc::wedge_contains_point (const Point &p) const -{ - Pair<Vector> t = tangents (); - if (fabs (d) <= 1) - return (p - p0) * t.first >= 0 && (p - p1) * t.second <= 0; - else - return (p - p0) * t.first >= 0 || (p - p1) * t.second <= 0; -} - - -/* Distance may not always be positive, but will be to an endpoint whenever necessary. */ -inline double Arc::distance_to_point (const Point &p) const { - if (fabs(d) < 1e-5) { - Segment arc_segment (p0, p1); - return arc_segment.distance_to_point (p); - } - - SignedVector difference = *this - p; - - if (wedge_contains_point (p) && fabs(d) > 1e-5) - return fabs (p.distance_to_point (center ()) - radius ()) * (difference.negative ? -1 : 1); - double d1 = p.squared_distance_to_point (p0); - double d2 = p.squared_distance_to_point (p1); - return (d1 < d2 ? sqrt(d1) : sqrt(d2)) * (difference.negative ? -1 : 1); -} - -/* Distance will be to an endpoint whenever necessary. */ -inline double Arc::squared_distance_to_point (const Point &p) const { - if (fabs(d) < 1e-5) { - Segment arc_segment (p0, p1); - return arc_segment.squared_distance_to_point (p); - } - - //SignedVector difference = *this - p; - - if (wedge_contains_point (p) && fabs(d) > 1e-5) { - double answer = p.distance_to_point (center ()) - radius (); - return answer * answer; - } - double d1 = p.squared_distance_to_point (p0); - double d2 = p.squared_distance_to_point (p1); - return (d1 < d2 ? d1 : d2); -} - -inline double Arc::extended_dist (const Point &p) const { - Point m = p0.lerp (.5, p1); - Vector dp = p1 - p0; - Vector pp = dp.ortho (); - float d2 = tan2atan (d); - if ((p - m) * (p1 - m) < 0) - return (p - p0) * (pp + dp * d2).normalized (); - else - return (p - p1) * (pp - dp * d2).normalized (); -} - -inline void Arc::extents (glyphy_extents_t &extents) const { - glyphy_extents_clear (&extents); - glyphy_extents_add (&extents, &p0); - glyphy_extents_add (&extents, &p1); - Point c = center (); - double r = radius (); - Point p[4] = {c + r * Vector (-1, 0), - c + r * Vector (+1, 0), - c + r * Vector ( 0, -1), - c + r * Vector ( 0, +1)}; - for (unsigned int i = 0; i < 4; i++) - if (wedge_contains_point (p[i])) - glyphy_extents_add (&extents, &p[i]); -} - - -/* Bezier */ - -inline const Point Bezier::point (const double &t) const { - Point p01 = p0.lerp (t, p1); - Point p12 = p1.lerp (t, p2); - Point p23 = p2.lerp (t, p3); - Point p012 = p01.lerp (t, p12); - Point p123 = p12.lerp (t, p23); - Point p0123 = p012.lerp (t, p123); - return p0123; -} - -inline const Point Bezier::midpoint (void) const -{ - Point p01 = p0.midpoint (p1); - Point p12 = p1.midpoint (p2); - Point p23 = p2.midpoint (p3); - Point p012 = p01.midpoint (p12); - Point p123 = p12.midpoint (p23); - Point p0123 = p012.midpoint (p123); - return p0123; -} - -inline const Vector Bezier::tangent (const double &t) const -{ - double t_2_0 = t * t; - double t_0_2 = (1 - t) * (1 - t); - - double _1__4t_1_0_3t_2_0 = 1 - 4 * t + 3 * t_2_0; - double _2t_1_0_3t_2_0 = 2 * t - 3 * t_2_0; - - return Vector (-3 * p0.x * t_0_2 - +3 * p1.x * _1__4t_1_0_3t_2_0 - +3 * p2.x * _2t_1_0_3t_2_0 - +3 * p3.x * t_2_0, - -3 * p0.y * t_0_2 - +3 * p1.y * _1__4t_1_0_3t_2_0 - +3 * p2.y * _2t_1_0_3t_2_0 - +3 * p3.y * t_2_0); -} - -inline const Vector Bezier::d_tangent (const double &t) const { - return Vector (6 * ((-p0.x + 3*p1.x - 3*p2.x + p3.x) * t + (p0.x - 2*p1.x + p2.x)), - 6 * ((-p0.y + 3*p1.y - 3*p2.y + p3.y) * t + (p0.y - 2*p1.y + p2.y))); -} - -inline double Bezier::curvature (const double &t) const { - Vector dpp = tangent (t).ortho (); - Vector ddp = d_tangent (t); - /* normal vector len squared */ - double len = dpp.len (); - double curvature = (dpp * ddp) / (len * len * len); - return curvature; -} - -inline const Pair<Bezier > Bezier::split (const double &t) const { - Point p01 = p0.lerp (t, p1); - Point p12 = p1.lerp (t, p2); - Point p23 = p2.lerp (t, p3); - Point p012 = p01.lerp (t, p12); - Point p123 = p12.lerp (t, p23); - Point p0123 = p012.lerp (t, p123); - return Pair<Bezier> (Bezier (p0, p01, p012, p0123), - Bezier (p0123, p123, p23, p3)); -} - -inline const Pair<Bezier > Bezier::halve (void) const -{ - Point p01 = p0.midpoint (p1); - Point p12 = p1.midpoint (p2); - Point p23 = p2.midpoint (p3); - Point p012 = p01.midpoint (p12); - Point p123 = p12.midpoint (p23); - Point p0123 = p012.midpoint (p123); - return Pair<Bezier> (Bezier (p0, p01, p012, p0123), - Bezier (p0123, p123, p23, p3)); -} - -inline const Bezier Bezier::segment (const double &t0, const double &t1) const -{ - Point p01 = p0.lerp (t0, p1); - Point p12 = p1.lerp (t0, p2); - Point p23 = p2.lerp (t0, p3); - Point p012 = p01.lerp (t0, p12); - Point p123 = p12.lerp (t0, p23); - Point p0123 = p012.lerp (t0, p123); - - Point q01 = p0.lerp (t1, p1); - Point q12 = p1.lerp (t1, p2); - Point q23 = p2.lerp (t1, p3); - Point q012 = q01.lerp (t1, q12); - Point q123 = q12.lerp (t1, q23); - Point q0123 = q012.lerp (t1, q123); - - return Bezier (p0123, - p0123 + (p123 - p0123) * ((t1 - t0) / (1 - t0)), - q0123 + (q012 - q0123) * ((t1 - t0) / t1), - q0123); -} - - -/* insertion operator */ - - -static inline std::ostream& operator<<(std::ostream& os, const Point& p) -{ - os << "Point(" << p.x << "," << p.y << ")"; - return os; -} -static inline std::ostream& operator<<(std::ostream& os, const Vector& v) -{ - os << "Vector(" << v.dx << "," << v.dy << ")"; - return os; -} -static inline std::ostream& operator<<(std::ostream& os, const Arc& a) -{ - os << "Arc(" << a.p0 << ", " << a.p1 << ", " << a.d << ")"; - return os; -} -static inline std::ostream& operator<<(std::ostream& os, const Bezier& b) -{ - os << "Bezier(" << b.p0 << ", " << b.p1 << ", " << b.p2 << ", " << b.p3 << ")"; - return os; -} - -} /* namespace Geometry */ -} /* namespace GLyphy */ - -#endif /* GLYPHY_GEOMETRY_HH */ diff --git a/viewer/Makefile b/viewer/Makefile @@ -1,12 +1,21 @@ PROG = scv_viewer -SRCS = viewer.cxx text.cxx af_unix.cxx demo-atlas.cxx demo-buffer.cxx -SRCS += demo-font.cxx demo-glstate.cxx demo-shader.cxx demo-view.cxx trackball.c -SRCS += matrix4x4.c + +SRCS = viewer.cxx \ + text.cxx \ + af_unix.cxx \ + demo-atlas.cxx \ + demo-buffer.cxx \ + demo-font.cxx \ + demo-glstate.cxx \ + demo-shader.cxx \ + demo-view.cxx \ + matrix4x4.c \ + trackball.c CXXFLAGS += -std=c++11 -CXXFLAGS += `pkg-config gl glew freetype2 --cflags` +CXXFLAGS += `pkg-config gl glew freetype2 --cflags` -I/usr/local/include LDFLAGS += `pkg-config gl glew freetype2 --libs` -LDADD += -lstdc++ -lz -lglut ../glyphy/libglyphy.a +LDADD += -lstdc++ -lz -lglut glyphy/libglyphy.a .include <bsd.prog.mk> diff --git a/viewer/demo-common.h b/viewer/demo-common.h @@ -19,7 +19,7 @@ #ifndef DEMO_COMMON_H #define DEMO_COMMON_H -#include "../glyphy/glyphy.h" +#include "glyphy/glyphy.h" #include <stdlib.h> #include <string.h> diff --git a/viewer/demo-font.cxx b/viewer/demo-font.cxx @@ -22,7 +22,7 @@ #include "demo-font.h" -#include "../glyphy/glyphy-freetype.h" +#include "glyphy/glyphy-freetype.h" #include <ext/hash_map> diff --git a/viewer/glyphy/Makefile b/viewer/glyphy/Makefile @@ -0,0 +1,15 @@ +LIB = glyphy + +SRCS += glyphy-arc.cxx \ + glyphy-arcs.cxx \ + glyphy-blob.cxx \ + glyphy-extents.cxx \ + glyphy-outline.cxx \ + glyphy-sdf.cxx \ + glyphy-shaders.cxx + +CXXFLAGS += -std=c++11 -I. -DPKGDATADIR="" + +NOPROFILE=1 +NO_PROFILE=1 +.include <bsd.lib.mk> diff --git a/viewer/glyphy/glyphy-arc-bezier.hh b/viewer/glyphy/glyphy-arc-bezier.hh @@ -0,0 +1,228 @@ +/* + * Copyright 2012,2013 Google, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Google Author(s): Behdad Esfahbod, Maysum Panju + */ + +#ifndef GLYPHY_ARC_BEZIER_HH +#define GLYPHY_ARC_BEZIER_HH + +#include "glyphy-common.hh" +#include "glyphy-geometry.hh" + +namespace GLyphy { +namespace ArcBezier { + +using namespace Geometry; + + +class MaxDeviationApproximatorExact +{ + public: + /* Returns 3 max(abs(d₀ t (1-t)² + d₁ t² (1-t)) for 0≤t≤1. */ + static double approximate_deviation (double d0, double d1) + { + double candidates[4] = {0,1}; + unsigned int num_candidates = 2; + if (d0 == d1) + candidates[num_candidates++] = .5; + else { + double delta = d0*d0 - d0*d1 + d1*d1; + double t2 = 1. / (3 * (d0 - d1)); + double t0 = (2 * d0 - d1) * t2; + if (delta == 0) + candidates[num_candidates++] = t0; + else if (delta > 0) { + /* This code can be optimized to avoid the sqrt if the solution + * is not feasible (ie. lies outside (0,1)). I have implemented + * that in cairo-spline.c:_cairo_spline_bound(). Can be reused + * here. + */ + double t1 = sqrt (delta) * t2; + candidates[num_candidates++] = t0 - t1; + candidates[num_candidates++] = t0 + t1; + } + } + + double e = 0; + for (unsigned int i = 0; i < num_candidates; i++) { + double t = candidates[i]; + double ee; + if (t < 0. || t > 1.) + continue; + ee = fabs (3 * t * (1-t) * (d0 * (1 - t) + d1 * t)); + e = std::max (e, ee); + } + + return e; + } +}; + + + +template <class MaxDeviationApproximator> +class ArcBezierErrorApproximatorBehdad +{ + public: + static double approximate_bezier_arc_error (const Bezier &b0, const Arc &a) + { + assert (b0.p0 == a.p0); + assert (b0.p3 == a.p1); + + double ea; + Bezier b1 = a.approximate_bezier (&ea); + + assert (b0.p0 == b1.p0); + assert (b0.p3 == b1.p3); + + Vector v0 = b1.p1 - b0.p1; + Vector v1 = b1.p2 - b0.p2; + + Vector b = (b0.p3 - b0.p0).normalized (); + v0 = v0.rebase (b); + v1 = v1.rebase (b); + + Vector v (MaxDeviationApproximator::approximate_deviation (v0.dx, v1.dx), + MaxDeviationApproximator::approximate_deviation (v0.dy, v1.dy)); + + /* Edge cases: If d*d is too close too large default to a weak bound. */ + if (a.d * a.d > 1. - 1e-4) + return ea + v.len (); + + /* If the wedge doesn't contain control points, default to weak bound. */ + if (!a.wedge_contains_point (b0.p1) || !a.wedge_contains_point (b0.p2)) + return ea + v.len (); + + /* If straight line, return the max ortho deviation. */ + if (fabs (a.d) < 1e-6) + return ea + v.dy; + + /* We made sure that fabs(a.d) < 1 */ + double tan_half_alpha = fabs (tan2atan (a.d)); + + double tan_v = v.dx / v.dy; + + double eb; + if (fabs (tan_v) <= tan_half_alpha) + return ea + v.len (); + + double c2 = (a.p1 - a.p0).len () * .5; + double r = a.radius (); + + eb = Vector (c2 + v.dx, c2 / tan_half_alpha + v.dy).len () - r; + assert (eb >= 0); + + return ea + eb; + } +}; + + + +template <class ArcBezierErrorApproximator> +class ArcBezierApproximatorMidpointSimple +{ + public: + static const Arc approximate_bezier_with_arc (const Bezier &b, double *error) + { + Arc a (b.p0, b.p3, b.midpoint (), false); + + *error = ArcBezierErrorApproximator::approximate_bezier_arc_error (b, a); + + return a; + } +}; + +template <class ArcBezierErrorApproximator> +class ArcBezierApproximatorMidpointTwoPart +{ + public: + static const Arc approximate_bezier_with_arc (const Bezier &b, double *error, double mid_t = .5) + { + Pair<Bezier > pair = b.split (mid_t); + Point m = pair.second.p0; + + Arc a0 (b.p0, m, b.p3, true); + Arc a1 (m, b.p3, b.p0, true); + + double e0 = ArcBezierErrorApproximator::approximate_bezier_arc_error (pair.first, a0); + double e1 = ArcBezierErrorApproximator::approximate_bezier_arc_error (pair.second, a1); + *error = std::max (e0, e1); + + return Arc (b.p0, b.p3, m, false); + } +}; + +template <class ArcBezierErrorApproximator> +class ArcBezierApproximatorQuantized +{ + public: + ArcBezierApproximatorQuantized (double _max_d = GLYPHY_INFINITY, unsigned int _d_bits = 0) : + max_d (_max_d), d_bits (_d_bits) {}; + + protected: + double max_d; + unsigned int d_bits; + + public: + const Arc approximate_bezier_with_arc (const Bezier &b, double *error) const + { + double mid_t = .5; + Arc a (b.p0, b.p3, b.point (mid_t), false); + Arc orig_a = a; + + if (std::isfinite (max_d)) { + assert (max_d >= 0); + if (fabs (a.d) > max_d) + a.d = a.d < 0 ? -max_d : max_d; + } + if (d_bits && max_d != 0) { + assert (std::isfinite (max_d)); + assert (fabs (a.d) <= max_d); + int mult = (1 << (d_bits - 1)) - 1; + int id = round (a.d / max_d * mult); + assert (-mult <= id && id <= mult); + a.d = id * max_d / mult; + assert (fabs (a.d) <= max_d); + } + + /* Error introduced by arc quantization */ + double ed = fabs (a.d - orig_a.d) * (a.p1 - a.p0).len () * .5; + + ArcBezierApproximatorMidpointTwoPart<ArcBezierErrorApproximator> + ::approximate_bezier_with_arc (b, error, mid_t); + + if (ed) { + *error += ed; + + /* Try a simple one-arc approx which works with the quantized arc. + * May produce smaller error bound. */ + double e = ArcBezierErrorApproximator::approximate_bezier_arc_error (b, a); + if (e < *error) + *error = e; + } + + return a; + } +}; + +typedef MaxDeviationApproximatorExact MaxDeviationApproximatorDefault; +typedef ArcBezierErrorApproximatorBehdad<MaxDeviationApproximatorDefault> ArcBezierErrorApproximatorDefault; +typedef ArcBezierApproximatorMidpointTwoPart<ArcBezierErrorApproximatorDefault> ArcBezierApproximatorDefault; +typedef ArcBezierApproximatorQuantized<ArcBezierErrorApproximatorDefault> ArcBezierApproximatorQuantizedDefault; + +} /* namespace ArcBezier */ +} /* namespace GLyphy */ + +#endif /* GLYPHY_ARC_BEZIER_HH */ diff --git a/glyphy/glyphy-arc.cxx b/viewer/glyphy/glyphy-arc.cxx diff --git a/glyphy/glyphy-arcs-bezier.hh b/viewer/glyphy/glyphy-arcs-bezier.hh diff --git a/glyphy/glyphy-arcs.cxx b/viewer/glyphy/glyphy-arcs.cxx diff --git a/viewer/glyphy/glyphy-blob.cxx b/viewer/glyphy/glyphy-blob.cxx @@ -0,0 +1,328 @@ +/* + * Copyright 2012 Google, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Google Author(s): Behdad Esfahbod, Maysum Panju, Wojciech Baranowski + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "glyphy-common.hh" +#include "glyphy-geometry.hh" + +#define GRID_SIZE 24 + +using namespace GLyphy::Geometry; + + +#define UPPER_BITS(v,bits,total_bits) ((v) >> ((total_bits) - (bits))) +#define LOWER_BITS(v,bits,total_bits) ((v) & ((1 << (bits)) - 1)) + +#define MAX_X 4095 +#define MAX_Y 4095 + +static inline glyphy_rgba_t +arc_endpoint_encode (unsigned int ix, unsigned int iy, double d) +{ + glyphy_rgba_t v; + + /* 12 bits for each of x and y, 8 bits for d */ + assert (ix <= MAX_X); + assert (iy <= MAX_Y); + unsigned int id; + if (std::isinf (d)) + id = 0; + else { + assert (fabs (d) <= GLYPHY_MAX_D); + id = 128 + lround (d * 127 / GLYPHY_MAX_D); + } + assert (id < 256); + + v.r = id; + v.g = LOWER_BITS (ix, 8, 12); + v.b = LOWER_BITS (iy, 8, 12); + v.a = ((ix >> 8) << 4) | (iy >> 8); + return v; +} + +static inline glyphy_rgba_t +arc_list_encode (unsigned int offset, unsigned int num_points, int side) +{ + glyphy_rgba_t v; + v.r = 0; // unused for arc-list encoding + v.g = UPPER_BITS (offset, 8, 16); + v.b = LOWER_BITS (offset, 8, 16); + v.a = LOWER_BITS (num_points, 8, 8); + if (side < 0 && !num_points) + v.a = 255; + return v; +} + +static inline glyphy_rgba_t +line_encode (const Line &line) +{ + Line l = line.normalized (); + double angle = l.n.angle (); + double distance = l.c; + + int ia = lround (-angle / M_PI * 0x7FFF); + unsigned int ua = ia + 0x8000; + assert (0 == (ua & ~0xFFFF)); + + int id = lround (distance * 0x1FFF); + unsigned int ud = id + 0x4000; + assert (0 == (ud & ~0x7FFF)); + + /* Marker for line-encoded */ + ud |= 0x8000; + + glyphy_rgba_t v; + v.r = ud >> 8; + v.g = ud & 0xFF; + v.b = ua >> 8; + v.a = ua & 0xFF; + return v; +} + + +/* Given a cell, fills the vector closest_arcs with arcs that may be closest to some point in the cell. + * Uses idea that all close arcs to cell must be ~close to center of cell. + */ +static void +closest_arcs_to_cell (Point c0, Point c1, /* corners */ + double faraway, + const glyphy_arc_endpoint_t *endpoints, + unsigned int num_endpoints, + std::vector<glyphy_arc_endpoint_t> &near_endpoints, + int *side) +{ + // Find distance between cell center + Point c = c0.midpoint (c1); + double min_dist = glyphy_sdf_from_arc_list (endpoints, num_endpoints, &c, NULL); + + *side = min_dist >= 0 ? +1 : -1; + min_dist = fabs (min_dist); + std::vector<Arc> near_arcs; + + // If d is the distance from the center of the square to the nearest arc, then + // all nearest arcs to the square must be at most almost [d + half_diagonal] from the center. + double half_diagonal = (c - c0).len (); + double radius_squared = pow (min_dist + half_diagonal, 2); + if (min_dist - half_diagonal <= faraway) { + Point p0 (0, 0); + for (unsigned int i = 0; i < num_endpoints; i++) { + const glyphy_arc_endpoint_t &endpoint = endpoints[i]; + if (endpoint.d == GLYPHY_INFINITY) { + p0 = endpoint.p; + continue; + } + Arc arc (p0, endpoint.p, endpoint.d); + p0 = endpoint.p; + + if (arc.squared_distance_to_point (c) <= radius_squared) + near_arcs.push_back (arc); + } + } + + Point p1 = Point (0, 0); + for (unsigned i = 0; i < near_arcs.size (); i++) + { + Arc arc = near_arcs[i]; + + if (i == 0 || p1 != arc.p0) { + glyphy_arc_endpoint_t endpoint = {arc.p0, GLYPHY_INFINITY}; + near_endpoints.push_back (endpoint); + p1 = arc.p0; + } + + glyphy_arc_endpoint_t endpoint = {arc.p1, arc.d}; + near_endpoints.push_back (endpoint); + p1 = arc.p1; + } +} + + +glyphy_bool_t +glyphy_arc_list_encode_blob (const glyphy_arc_endpoint_t *endpoints, + unsigned int num_endpoints, + glyphy_rgba_t *blob, + unsigned int blob_size, + double faraway, + double avg_fetch_desired, + double *avg_fetch_achieved, + unsigned int *output_len, + unsigned int *nominal_width, /* 8bit */ + unsigned int *nominal_height, /* 8bit */ + glyphy_extents_t *pextents) +{ + glyphy_extents_t extents; + glyphy_extents_clear (&extents); + + glyphy_arc_list_extents (endpoints, num_endpoints, &extents); + + if (glyphy_extents_is_empty (&extents)) { + *pextents = extents; + if (!blob_size) + return false; + *blob = arc_list_encode (0, 0, +1); + *avg_fetch_achieved = 1; + *output_len = 1; + *nominal_width = *nominal_height = 1; + return true; + } + + /* Add antialiasing padding */ + extents.min_x -= faraway; + extents.min_y -= faraway; + extents.max_x += faraway; + extents.max_y += faraway; + + double glyph_width = extents.max_x - extents.min_x; + double glyph_height = extents.max_y - extents.min_y; + double unit = std::max (glyph_width, glyph_height); + + unsigned int grid_w = GRID_SIZE; + unsigned int grid_h = GRID_SIZE; + + if (glyph_width > glyph_height) { + while ((grid_h - 1) * unit / grid_w > glyph_height) + grid_h--; + glyph_height = grid_h * unit / grid_w; + extents.max_y = extents.min_y + glyph_height; + } else { + while ((grid_w - 1) * unit / grid_h > glyph_width) + grid_w--; + glyph_width = grid_w * unit / grid_h; + extents.max_x = extents.min_x + glyph_width; + } + + double cell_unit = unit / std::max (grid_w, grid_h); + + std::vector<glyphy_rgba_t> tex_data; + std::vector<glyphy_arc_endpoint_t> near_endpoints; + + unsigned int header_length = grid_w * grid_h; + unsigned int offset = header_length; + tex_data.resize (header_length); + Point origin = Point (extents.min_x, extents.min_y); + unsigned int total_arcs = 0; + + for (unsigned int row = 0; row < grid_h; row++) + for (unsigned int col = 0; col < grid_w; col++) + { + Point cp0 = origin + Vector ((col + 0) * cell_unit, (row + 0) * cell_unit); + Point cp1 = origin + Vector ((col + 1) * cell_unit, (row + 1) * cell_unit); + near_endpoints.clear (); + + int side; + closest_arcs_to_cell (cp0, cp1, + faraway, + endpoints, num_endpoints, + near_endpoints, + &side); + +#define QUANTIZE_X(X) (lround (MAX_X * ((X - extents.min_x) / glyph_width ))) +#define QUANTIZE_Y(Y) (lround (MAX_Y * ((Y - extents.min_y) / glyph_height))) +#define DEQUANTIZE_X(X) (double (X) / MAX_X * glyph_width + extents.min_x) +#define DEQUANTIZE_Y(Y) (double (Y) / MAX_Y * glyph_height + extents.min_y) +#define SNAP(P) (Point (DEQUANTIZE_X (QUANTIZE_X ((P).x)), DEQUANTIZE_Y (QUANTIZE_Y ((P).y)))) + + if (near_endpoints.size () == 2 && near_endpoints[1].d == 0) { + Point c (extents.min_x + glyph_width * .5, extents.min_y + glyph_height * .5); + Line line (SNAP (near_endpoints[0].p), SNAP (near_endpoints[1].p)); + line.c -= line.n * Vector (c); + line.c /= unit; + tex_data[row * grid_w + col] = line_encode (line); + continue; + } + + /* If the arclist is two arcs that can be combined in encoding if reordered, + * do that. */ + if (near_endpoints.size () == 4 && + std::isinf (near_endpoints[2].d) && + near_endpoints[0].p.x == near_endpoints[3].p.x && + near_endpoints[0].p.y == near_endpoints[3].p.y) + { + glyphy_arc_endpoint_t e0, e1, e2; + e0 = near_endpoints[2]; + e1 = near_endpoints[3]; + e2 = near_endpoints[1]; + near_endpoints.resize (0); + near_endpoints.push_back (e0); + near_endpoints.push_back (e1); + near_endpoints.push_back (e2); + } + + for (unsigned i = 0; i < near_endpoints.size (); i++) { + glyphy_arc_endpoint_t &endpoint = near_endpoints[i]; + tex_data.push_back (arc_endpoint_encode (QUANTIZE_X(endpoint.p.x), QUANTIZE_Y(endpoint.p.y), endpoint.d)); + } + + unsigned int current_endpoints = tex_data.size () - offset; + + if (current_endpoints) + { + /* See if we can fulfill this cell by using already-encoded arcs */ + const glyphy_rgba_t *needle = &tex_data[offset]; + unsigned int needle_len = current_endpoints; + const glyphy_rgba_t *haystack = &tex_data[header_length]; + unsigned int haystack_len = offset - header_length; + + bool found = false; + while (haystack_len >= needle_len) { + /* Trick: we don't care about first endpoint's d value, so skip one + * byte in comparison. This works because arc_encode() packs the + * d value in the first byte. */ + if (0 == memcmp (1 + (const char *) needle, + 1 + (const char *) haystack, + needle_len * sizeof (*needle) - 1)) { + found = true; + break; + } + haystack++; + haystack_len--; + } + if (found) { + unsigned int new_offset = haystack - &tex_data[0]; + tex_data.resize (offset); + haystack = needle = NULL; /* Invalidated by the resize. */ + offset = new_offset; + } + } + else + offset = 0; + + tex_data[row * grid_w + col] = arc_list_encode (offset, current_endpoints, side); + offset = tex_data.size (); + + total_arcs += current_endpoints; + } + + if (avg_fetch_achieved) + *avg_fetch_achieved = 1 + double (total_arcs) / (grid_w * grid_h); + + *pextents = extents; + + if (tex_data.size () > blob_size) + return false; + + memcpy (blob, &tex_data[0], tex_data.size () * sizeof(tex_data[0])); + *output_len = tex_data.size (); + *nominal_width = grid_w; + *nominal_height = grid_h; + + return true; +} diff --git a/glyphy/glyphy-common-glsl.h b/viewer/glyphy/glyphy-common-glsl.h diff --git a/glyphy/glyphy-common.hh b/viewer/glyphy/glyphy-common.hh diff --git a/viewer/glyphy/glyphy-extents.cxx b/viewer/glyphy/glyphy-extents.cxx @@ -0,0 +1,89 @@ +/* + * Copyright 2012 Google, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Google Author(s): Behdad Esfahbod + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "glyphy-common.hh" + + +void +glyphy_extents_clear (glyphy_extents_t *extents) +{ + extents->min_x = GLYPHY_INFINITY; + extents->min_y = GLYPHY_INFINITY; + extents->max_x = -GLYPHY_INFINITY; + extents->max_y = -GLYPHY_INFINITY; +} + +glyphy_bool_t +glyphy_extents_is_empty (const glyphy_extents_t *extents) +{ + return std::isinf (extents->min_x); +} + +void +glyphy_extents_add (glyphy_extents_t *extents, + const glyphy_point_t *p) +{ + if (glyphy_extents_is_empty (extents)) { + extents->min_x = extents->max_x = p->x; + extents->min_y = extents->max_y = p->y; + return; + } + extents->min_x = std::min (extents->min_x, p->x); + extents->min_y = std::min (extents->min_y, p->y); + extents->max_x = std::max (extents->max_x, p->x); + extents->max_y = std::max (extents->max_y, p->y); +} + +void +glyphy_extents_extend (glyphy_extents_t *extents, + const glyphy_extents_t *other) +{ + if (glyphy_extents_is_empty (other)) + return; + if (glyphy_extents_is_empty (extents)) { + *extents = *other; + return; + } + extents->min_x = std::min (extents->min_x, other->min_x); + extents->min_y = std::min (extents->min_y, other->min_y); + extents->max_x = std::max (extents->max_x, other->max_x); + extents->max_y = std::max (extents->max_y, other->max_y); +} + +glyphy_bool_t +glyphy_extents_includes (const glyphy_extents_t *extents, + const glyphy_point_t *p) +{ + return extents->min_x <= p->x && p->x <= extents->max_x && + extents->min_y <= p->y && p->y <= extents->max_y; +} + +void +glyphy_extents_scale (glyphy_extents_t *extents, + double x_scale, + double y_scale) +{ + extents->min_x *= x_scale; + extents->max_x *= x_scale; + extents->min_y *= y_scale; + extents->max_y *= y_scale; +} diff --git a/glyphy/glyphy-freetype.h b/viewer/glyphy/glyphy-freetype.h diff --git a/viewer/glyphy/glyphy-geometry.hh b/viewer/glyphy/glyphy-geometry.hh @@ -0,0 +1,742 @@ +/* + * Copyright 2012,2013 Google, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Google Author(s): Behdad Esfahbod, Maysum Panju + */ + +#ifndef GLYPHY_GEOMETRY_HH +#define GLYPHY_GEOMETRY_HH + +#include "glyphy-common.hh" + +namespace GLyphy { +namespace Geometry { + +template <typename Type> struct Pair; +struct Vector; +struct SignedVector; +struct Point; +struct Line; +struct Segment; +struct Arc; +struct Bezier; + +/* returns tan (2 * atan (d)) */ +inline double tan2atan (double d) { return 2 * d / (1 - d*d); } + +/* returns sin (2 * atan (d)) */ +inline double sin2atan (double d) { return 2 * d / (1 + d*d); } + +/* returns cos (2 * atan (d)) */ +inline double cos2atan (double d) { return (1 - d*d) / (1 + d*d); } + +template <typename Type> +struct Pair { + typedef Type ElementType; + + inline Pair (const Type &first_, const Type &second_) : first (first_), second (second_) {} + + Type first, second; +}; + +struct Point : glyphy_point_t { + inline Point (double x_, double y_) { x = x_; y = y_; } + inline explicit Point (const Vector &v); + inline Point (const glyphy_point_t &p) { *(glyphy_point_t *)this = p; } + + inline bool operator == (const Point &p) const; + inline bool operator != (const Point &p) const; + inline Point& operator+= (const Vector &v); + inline Point& operator-= (const Vector &v); + inline const Point operator+ (const Vector &v) const; + inline const Point operator- (const Vector &v) const; + inline const Vector operator- (const Point &p) const; + inline const Point midpoint (const Point &p) const; + inline const Line bisector (const Point &p) const; + inline double distance_to_point (const Point &p) const; /* distance to point! */ + inline double squared_distance_to_point (const Point &p) const; /* square of distance to point! */ + + inline bool is_finite (void) const; + inline const Point lerp (const double &a, const Point &p) const; +}; + +struct Vector { + inline Vector (double dx_, double dy_) : dx (dx_), dy (dy_) {} + inline explicit Vector (const Point &p) : dx (p.x), dy (p.y) {} + + inline bool operator == (const Vector &v) const; + inline bool operator != (const Vector &v) const; + inline const Vector operator+ (void) const; + inline const Vector operator- (void) const; + inline Vector& operator+= (const Vector &v); + inline Vector& operator-= (const Vector &v); + inline Vector& operator*= (const double &s); + inline Vector& operator/= (const double &s); + inline const Vector operator+ (const Vector &v) const; + inline const Vector operator- (const Vector &v) const; + inline const Vector operator* (const double &s) const; + inline const Vector operator/ (const double &s) const; + inline double operator* (const Vector &v) const; /* dot product */ + inline const Point operator+ (const Point &p) const; + + inline bool is_nonzero (void) const; + inline double len (void) const; + inline double len2 (void) const; + inline const Vector normalized (void) const; + inline const Vector ortho (void) const; + inline const Vector normal (void) const; /* ortho().normalized() */ + inline double angle (void) const; + + inline double cross (const Vector &other) const; + inline const Vector rebase (const Vector &bx, const Vector &by) const; + inline const Vector rebase (const Vector &bx) const; + + double dx, dy; +}; + +struct SignedVector : Vector { + inline SignedVector (const Vector &v, bool negative_) : Vector (v), negative (negative_) {} + + inline bool operator == (const SignedVector &v) const; + inline bool operator != (const SignedVector &v) const; + inline const SignedVector operator- (void) const; + + bool negative; +}; + +struct Line { + inline Line (double a_, double b_, double c_) : n (a_, b_), c (c_) {} + inline Line (Vector n_, double c_) : n (n_), c (c_) {} + inline Line (const Point &p0, const Point &p1) : + n ((p1 - p0).ortho ()), c (n * Vector (p0)) {} + + inline const Point operator+ (const Line &l) const; /* line intersection! */ + inline const SignedVector operator- (const Point &p) const; /* shortest vector from point to line */ + + + inline const Line normalized (void) const; + inline const Vector normal (void) const; + + Vector n; /* line normal */ + double c; /* n.dx*x + n.dy*y = c */ +}; + +struct Segment { + inline Segment (const Point &p0_, const Point &p1_) : + p0 (p0_), p1 (p1_) {} + + inline const SignedVector operator- (const Point &p) const; /* shortest vector from point to ***line*** */ + inline double distance_to_point (const Point &p) const; /* shortest distance from point to segment */ + inline double squared_distance_to_point (const Point &p) const; /* shortest distance squared from point to segment */ + inline bool contains_in_span (const Point &p) const; /* is p in the stripe formed by sliding this segment? */ + inline double max_distance_to_arc (const Arc &a) const; + + + Point p0; + Point p1; +}; + + + +struct Arc { + inline Arc (const Point &p0_, const Point &p1_, const Point &pm, bool complement) : + p0 (p0_), p1 (p1_), + d (p0_ == pm || p1_ == pm ? 0 : + tan (((p1_-pm).angle () - (p0_-pm).angle ()) / 2 - (complement ? 0 : M_PI_2))) {} + inline Arc (const Point &p0_, const Point &p1_, const double &d_) : + p0 (p0_), p1 (p1_), d (d_) {} + inline Arc (const Point &center, double radius, const double &a0, const double &a1, bool complement) : + p0 (center + Vector (cos(a0),sin(a0)) * radius), + p1 (center + Vector (cos(a1),sin(a1)) * radius), + d (tan ((a1 - a0) / 4 - (complement ? 0 : M_PI_2))) {} + inline Arc (const glyphy_arc_t &a) : p0 (a.p0), p1 (a.p1), d (a.d) {} + inline operator glyphy_arc_t (void) const { glyphy_arc_t a = {p0, p1, d}; return a; } + + inline bool operator == (const Arc &a) const; + inline bool operator != (const Arc &a) const; + inline const SignedVector operator- (const Point &p) const; /* shortest vector from point to arc */ + + inline double radius (void) const; + inline const Point center (void) const; + inline const Pair<Vector> tangents (void) const; + + inline Bezier approximate_bezier (double *error) const; + + inline bool wedge_contains_point (const Point &p) const; + inline double distance_to_point (const Point &p) const; + inline double squared_distance_to_point (const Point &p) const; + inline double extended_dist (const Point &p) const; + + inline void extents (glyphy_extents_t &extents) const; + + Point p0, p1; + double d; /* Depth */ +}; + +struct Bezier { + inline Bezier (const Point &p0_, const Point &p1_, + const Point &p2_, const Point &p3_) : + p0 (p0_), p1 (p1_), p2 (p2_), p3 (p3_) {} + + inline const Point point (const double &t) const; + inline const Point midpoint (void) const; + inline const Vector tangent (const double &t) const; + inline const Vector d_tangent (const double &t) const; + inline double curvature (const double &t) const; + inline const Pair<Bezier> split (const double &t) const; + inline const Pair<Bezier> halve (void) const; + inline const Bezier segment (const double &t0, const double &t1) const; + + Point p0, p1, p2, p3; +}; + + +/* Implementations */ + + +/* Point */ + +inline Point::Point (const Vector &v) { + x = v.dx; + y = v.dy; +} +inline bool Point::operator == (const Point &p) const { + return x == p.x && y == p.y; +} +inline bool Point::operator != (const Point &p) const { + return !(*this == p); +} +inline Point& Point::operator+= (const Vector &v) { + x += v.dx; + y += v.dy; + return *this; +} +inline Point& Point::operator-= (const Vector &v) { + x -= v.dx; + y -= v.dy; + return *this; +} +inline const Point Point::operator+ (const Vector &v) const { + return Point (*this) += v; +} +inline const Point Point::operator- (const Vector &v) const { + return Point (*this) -= v; +} +inline const Vector Point::operator- (const Point &p) const { + return Vector (x - p.x, y - p.y); +} + +inline const Point Point::midpoint (const Point &p) const { + return *this + (p - *this) / 2; +} +inline const Line Point::bisector (const Point &p) const { + Vector d = p - *this; + return Line (d.dx * 2, d.dy * 2, d * Vector (p) + d * Vector (*this)); +} + +inline double Point::distance_to_point (const Point &p) const { + return ((*this) - p).len (); +} + +inline double Point::squared_distance_to_point (const Point &p) const { + return ((*this) - p).len2 (); +} + +inline bool Point::is_finite (void) const { + return std::isfinite (x) && std::isfinite (y); +} +inline const Point Point::lerp (const double &a, const Point &p) const { + /* The following two cases are special-cased to get better floating + * point stability. We require that points that are the same be + * bit-equal. */ + if (a == 0) return *this; + if (a == 1.0) return p; + return Point ((1-a) * x + a * p.x, (1-a) * y + a * p.y); +} + + +/* Vector */ + +inline bool Vector::operator == (const Vector &v) const { + return dx == v.dx && dy == v.dy; +} +inline bool Vector::operator != (const Vector &v) const { + return !(*this == v); +} +inline const Vector Vector::operator+ (void) const { + return *this; +} +inline const Vector Vector::operator- (void) const { + return Vector (-dx, -dy); +} +inline Vector& Vector::operator+= (const Vector &v) { + dx += v.dx; + dy += v.dy; + return *this; +} +inline Vector& Vector::operator-= (const Vector &v) { + dx -= v.dx; + dy -= v.dy; + return *this; +} +inline Vector& Vector::operator*= (const double &s) { + dx *= s; + dy *= s; + return *this; +} +inline Vector& Vector::operator/= (const double &s) { + dx /= s; + dy /= s; + return *this; +} +inline const Vector Vector::operator+ (const Vector &v) const { + return Vector (*this) += v; +} +inline const Vector Vector::operator- (const Vector &v) const { + return Vector (*this) -= v; +} +inline const Vector Vector::operator* (const double &s) const { + return Vector (*this) *= s; +} +inline const Vector operator* (const double &s, const Vector &v) { + return v * s; +} +inline const Vector Vector::operator/ (const double &s) const { + return Vector (*this) /= s; +} +inline double Vector::operator* (const Vector &v) const { /* dot product */ + return dx * v.dx + dy * v.dy; +} +inline const Point Vector::operator+ (const Point &p) const { + return p + *this; +} + +inline bool Vector::is_nonzero (void) const { + return dx || dy; +} +inline double Vector::len (void) const { + return hypot (dx, dy); +} +inline double Vector::len2 (void) const { + return dx * dx + dy * dy; +} +inline const Vector Vector::normalized (void) const { + double d = len (); + return d ? *this / d : *this; +} +inline const Vector Vector::ortho (void) const { + return Vector (-dy, dx); +} +inline const Vector Vector::normal (void) const { + return ortho ().normalized (); +} +inline double Vector::angle (void) const { + return atan2 (dy, dx); +} + +inline double Vector::cross (const Vector &other) const { + return dx * other.dy - dy * other.dx; +} +inline const Vector Vector::rebase (const Vector &bx, + const Vector &by) const { + return Vector (*this * bx, *this * by); +} +inline const Vector Vector::rebase (const Vector &bx) const { + return rebase (bx, bx.ortho ()); +} + + +/* SignedVector */ + +inline bool SignedVector::operator == (const SignedVector &v) const { + return (const Vector &)(*this) == (const Vector &)(v) && negative == v.negative; +} +inline bool SignedVector::operator != (const SignedVector &v) const { + return !(*this == v); +} +inline const SignedVector SignedVector::operator- (void) const { + return SignedVector (-(const Vector &)(*this), !negative); +} + + +/* Line */ + +inline const Point Line::operator+ (const Line &l) const { + double det = n.dx * l.n.dy - n.dy * l.n.dx; + if (!det) + return Point (GLYPHY_INFINITY, GLYPHY_INFINITY); + return Point ((c * l.n.dy - n.dy * l.c) / det, + (n.dx * l.c - c * l.n.dx) / det); +} +inline const SignedVector Line::operator- (const Point &p) const { + double mag = -(n * Vector (p) - c) / n.len (); + return SignedVector (n.normalized () * mag, mag < 0); /******************************************************************************************* FIX. *************************************/ +} + +inline const SignedVector operator- (const Point &p, const Line &l) { + return -(l - p); +} + +inline const Line Line::normalized (void) const { + double d = n.len (); + return d ? Line (n / d, c / d) : *this; +} +inline const Vector Line::normal (void) const { + return n; +} + +/* Segment */ +inline const SignedVector Segment::operator- (const Point &p) const { + /* shortest vector from point to line */ + return p - Line (p1, p0); /************************************************************************************************** Should the order (p1, p0) depend on d?? ***********************/ +} + +/* Segment */ +inline bool Segment::contains_in_span (const Point &p) const { + if (p0 == p1) + return false; + + /* shortest vector from point to line */ + Line temp (p0, p1); + double mag = -(temp.n * Vector (p) - temp.c) / temp.n.len (); + Vector y (temp.n.normalized () * mag); + Point z = y + p; + + // Check if z is between p0 and p1. + + if (fabs (p1.y - p0.y) > fabs (p1.x - p0.x)) { + return ((z.y - p0.y > 0 && p1.y - p0.y > z.y - p0.y) || + (z.y - p0.y < 0 && p1.y - p0.y < z.y - p0.y)); + } + else { + return ((0 < z.x - p0.x && z.x - p0.x < p1.x - p0.x) || + (0 > z.x - p0.x && z.x - p0.x > p1.x - p0.x)); + } +} + +inline double Segment::distance_to_point (const Point &p) const { + if (p0 == p1) + return 0; + + // Check if z is between p0 and p1. + Line temp (p0, p1); + if (contains_in_span (p)) + return -(temp.n * Vector (p) - temp.c) / temp.n.len (); + + double dist_p_p0 = p.distance_to_point (p0); + double dist_p_p1 = p.distance_to_point (p1); + return (dist_p_p0 < dist_p_p1 ? dist_p_p0 : dist_p_p1) * (-(temp.n * Vector (p) - temp.c) < 0 ? -1 : 1); +} + + +inline double Segment::squared_distance_to_point (const Point &p) const { + if (p0 == p1) + return 0; + + // Check if z is between p0 and p1. + Line temp (p0, p1); + if (contains_in_span (p)) + return (temp.n * Vector (p) - temp.c) * (temp.n * Vector (p) - temp.c) / (temp.n * temp.n); + + double dist_p_p0 = p.squared_distance_to_point (p0); + double dist_p_p1 = p.squared_distance_to_point (p1); + return (dist_p_p0 < dist_p_p1 ? dist_p_p0 : dist_p_p1); +} + + +inline double Segment::max_distance_to_arc (const Arc &a) const { + double max_distance = fabs(a.distance_to_point(p0)) ; + return max_distance > fabs(a.distance_to_point(p1)) ? max_distance : fabs(a.distance_to_point(p1)) ; +} + + + +/* Arc */ + +inline bool Arc::operator == (const Arc &a) const { + return p0 == a.p0 && p1 == a.p1 && d == a.d; +} +inline bool Arc::operator != (const Arc &a) const { + return !(*this == a); +} + + +inline const SignedVector Arc::operator- (const Point &p) const { + + if (fabs(d) < 1e-5) { + Segment arc_segment (p0, p1); + return arc_segment - p; + } + if (wedge_contains_point (p)){ + Vector difference = (center () - p).normalized () * fabs (p.distance_to_point (center ()) - radius ()); + + return SignedVector (difference, ((p - center ()).len () < radius ()) ^ (d < 0)); + } + double d0 = p.squared_distance_to_point (p0); + double d1 = p.squared_distance_to_point (p1); + + Arc other_arc (p0, p1, (1.0 + d) / (1.0 - d)); /********************************* NOT Robust. But works? *****************/ + Vector normal = center () - (d0 < d1 ? p0 : p1) ; + + if (normal.len() == 0) + return SignedVector (Vector (0, 0), true); /************************************ Check sign of this S.D. *************/ + + return SignedVector (Line (normal.dx, normal.dy, normal * Vector ((d0 < d1 ? p0 : p1))) - p, !other_arc.wedge_contains_point(p)); +} + +inline const SignedVector operator- (const Point &p, const Arc &a) { + return -(a - p); +} + + + +inline double Arc::radius (void) const +{ + return fabs ((p1 - p0).len () / (2 * sin2atan (d))); +} + +inline const Point Arc::center (void) const +{ + return (p0.midpoint (p1)) + (p1 - p0).ortho () / (2 * tan2atan (d)); +} + +inline const Pair<Vector> Arc::tangents (void) const +{ + Vector dp = (p1 - p0) * .5; + Vector pp = dp.ortho () * -sin2atan (d); + dp = dp * cos2atan (d); + return Pair<Vector> (dp + pp, dp - pp); +} + + + +inline Bezier Arc::approximate_bezier (double *error) const +{ + Vector dp = p1 - p0; + Vector pp = dp.ortho (); + + if (error) + *error = dp.len () * pow (fabs (d), 5) / (54 * (1 + d*d)); + + dp *= ((1 - d*d) / 3); + pp *= (2 * d / 3); + + Point p0s = p0 + dp - pp; + Point p1s = p1 - dp - pp; + + return Bezier (p0, p0s, p1s, p1); +} + + +inline bool Arc::wedge_contains_point (const Point &p) const +{ + Pair<Vector> t = tangents (); + if (fabs (d) <= 1) + return (p - p0) * t.first >= 0 && (p - p1) * t.second <= 0; + else + return (p - p0) * t.first >= 0 || (p - p1) * t.second <= 0; +} + + +/* Distance may not always be positive, but will be to an endpoint whenever necessary. */ +inline double Arc::distance_to_point (const Point &p) const { + if (fabs(d) < 1e-5) { + Segment arc_segment (p0, p1); + return arc_segment.distance_to_point (p); + } + + SignedVector difference = *this - p; + + if (wedge_contains_point (p) && fabs(d) > 1e-5) + return fabs (p.distance_to_point (center ()) - radius ()) * (difference.negative ? -1 : 1); + double d1 = p.squared_distance_to_point (p0); + double d2 = p.squared_distance_to_point (p1); + return (d1 < d2 ? sqrt(d1) : sqrt(d2)) * (difference.negative ? -1 : 1); +} + +/* Distance will be to an endpoint whenever necessary. */ +inline double Arc::squared_distance_to_point (const Point &p) const { + if (fabs(d) < 1e-5) { + Segment arc_segment (p0, p1); + return arc_segment.squared_distance_to_point (p); + } + + //SignedVector difference = *this - p; + + if (wedge_contains_point (p) && fabs(d) > 1e-5) { + double answer = p.distance_to_point (center ()) - radius (); + return answer * answer; + } + double d1 = p.squared_distance_to_point (p0); + double d2 = p.squared_distance_to_point (p1); + return (d1 < d2 ? d1 : d2); +} + +inline double Arc::extended_dist (const Point &p) const { + Point m = p0.lerp (.5, p1); + Vector dp = p1 - p0; + Vector pp = dp.ortho (); + float d2 = tan2atan (d); + if ((p - m) * (p1 - m) < 0) + return (p - p0) * (pp + dp * d2).normalized (); + else + return (p - p1) * (pp - dp * d2).normalized (); +} + +inline void Arc::extents (glyphy_extents_t &extents) const { + glyphy_extents_clear (&extents); + glyphy_extents_add (&extents, &p0); + glyphy_extents_add (&extents, &p1); + Point c = center (); + double r = radius (); + Point p[4] = {c + r * Vector (-1, 0), + c + r * Vector (+1, 0), + c + r * Vector ( 0, -1), + c + r * Vector ( 0, +1)}; + for (unsigned int i = 0; i < 4; i++) + if (wedge_contains_point (p[i])) + glyphy_extents_add (&extents, &p[i]); +} + + +/* Bezier */ + +inline const Point Bezier::point (const double &t) const { + Point p01 = p0.lerp (t, p1); + Point p12 = p1.lerp (t, p2); + Point p23 = p2.lerp (t, p3); + Point p012 = p01.lerp (t, p12); + Point p123 = p12.lerp (t, p23); + Point p0123 = p012.lerp (t, p123); + return p0123; +} + +inline const Point Bezier::midpoint (void) const +{ + Point p01 = p0.midpoint (p1); + Point p12 = p1.midpoint (p2); + Point p23 = p2.midpoint (p3); + Point p012 = p01.midpoint (p12); + Point p123 = p12.midpoint (p23); + Point p0123 = p012.midpoint (p123); + return p0123; +} + +inline const Vector Bezier::tangent (const double &t) const +{ + double t_2_0 = t * t; + double t_0_2 = (1 - t) * (1 - t); + + double _1__4t_1_0_3t_2_0 = 1 - 4 * t + 3 * t_2_0; + double _2t_1_0_3t_2_0 = 2 * t - 3 * t_2_0; + + return Vector (-3 * p0.x * t_0_2 + +3 * p1.x * _1__4t_1_0_3t_2_0 + +3 * p2.x * _2t_1_0_3t_2_0 + +3 * p3.x * t_2_0, + -3 * p0.y * t_0_2 + +3 * p1.y * _1__4t_1_0_3t_2_0 + +3 * p2.y * _2t_1_0_3t_2_0 + +3 * p3.y * t_2_0); +} + +inline const Vector Bezier::d_tangent (const double &t) const { + return Vector (6 * ((-p0.x + 3*p1.x - 3*p2.x + p3.x) * t + (p0.x - 2*p1.x + p2.x)), + 6 * ((-p0.y + 3*p1.y - 3*p2.y + p3.y) * t + (p0.y - 2*p1.y + p2.y))); +} + +inline double Bezier::curvature (const double &t) const { + Vector dpp = tangent (t).ortho (); + Vector ddp = d_tangent (t); + /* normal vector len squared */ + double len = dpp.len (); + double curvature = (dpp * ddp) / (len * len * len); + return curvature; +} + +inline const Pair<Bezier > Bezier::split (const double &t) const { + Point p01 = p0.lerp (t, p1); + Point p12 = p1.lerp (t, p2); + Point p23 = p2.lerp (t, p3); + Point p012 = p01.lerp (t, p12); + Point p123 = p12.lerp (t, p23); + Point p0123 = p012.lerp (t, p123); + return Pair<Bezier> (Bezier (p0, p01, p012, p0123), + Bezier (p0123, p123, p23, p3)); +} + +inline const Pair<Bezier > Bezier::halve (void) const +{ + Point p01 = p0.midpoint (p1); + Point p12 = p1.midpoint (p2); + Point p23 = p2.midpoint (p3); + Point p012 = p01.midpoint (p12); + Point p123 = p12.midpoint (p23); + Point p0123 = p012.midpoint (p123); + return Pair<Bezier> (Bezier (p0, p01, p012, p0123), + Bezier (p0123, p123, p23, p3)); +} + +inline const Bezier Bezier::segment (const double &t0, const double &t1) const +{ + Point p01 = p0.lerp (t0, p1); + Point p12 = p1.lerp (t0, p2); + Point p23 = p2.lerp (t0, p3); + Point p012 = p01.lerp (t0, p12); + Point p123 = p12.lerp (t0, p23); + Point p0123 = p012.lerp (t0, p123); + + Point q01 = p0.lerp (t1, p1); + Point q12 = p1.lerp (t1, p2); + Point q23 = p2.lerp (t1, p3); + Point q012 = q01.lerp (t1, q12); + Point q123 = q12.lerp (t1, q23); + Point q0123 = q012.lerp (t1, q123); + + return Bezier (p0123, + p0123 + (p123 - p0123) * ((t1 - t0) / (1 - t0)), + q0123 + (q012 - q0123) * ((t1 - t0) / t1), + q0123); +} + + +/* insertion operator */ + + +static inline std::ostream& operator<<(std::ostream& os, const Point& p) +{ + os << "Point(" << p.x << "," << p.y << ")"; + return os; +} +static inline std::ostream& operator<<(std::ostream& os, const Vector& v) +{ + os << "Vector(" << v.dx << "," << v.dy << ")"; + return os; +} +static inline std::ostream& operator<<(std::ostream& os, const Arc& a) +{ + os << "Arc(" << a.p0 << ", " << a.p1 << ", " << a.d << ")"; + return os; +} +static inline std::ostream& operator<<(std::ostream& os, const Bezier& b) +{ + os << "Bezier(" << b.p0 << ", " << b.p1 << ", " << b.p2 << ", " << b.p3 << ")"; + return os; +} + +} /* namespace Geometry */ +} /* namespace GLyphy */ + +#endif /* GLYPHY_GEOMETRY_HH */ diff --git a/glyphy/glyphy-outline.cxx b/viewer/glyphy/glyphy-outline.cxx diff --git a/glyphy/glyphy-sdf-glsl.h b/viewer/glyphy/glyphy-sdf-glsl.h diff --git a/glyphy/glyphy-sdf.cxx b/viewer/glyphy/glyphy-sdf.cxx diff --git a/glyphy/glyphy-shaders.cxx b/viewer/glyphy/glyphy-shaders.cxx diff --git a/glyphy/glyphy.h b/viewer/glyphy/glyphy.h