inline bool segments_intersect(const coordinate& first_segment_a, const coordinate& first_segment_b, const coordinate& second_segment_a, const coordinate& second_segment_b) { auto params = segment_intersection(first_segment_a, first_segment_b, second_segment_a, second_segment_b); auto u = params.first_param; auto t = params.second_param; if (params.colinear) { return false; } return (u >= 0 && u <= 1.0) && (t >= 0 && t <= 1.0); }
void LineBuilder::build() { // Need at least 2 points to draw a line if (points.size() < 2) { clear_output(); return; } const float hw = width / 2.f; const float hw_sq = hw * hw; const float sharp_limit_sq = sharp_limit * sharp_limit; const int len = points.size(); // Initial values Vector2 pos0 = points[0]; Vector2 pos1 = points[1]; Vector2 f0 = (pos1 - pos0).normalized(); Vector2 u0 = rotate90(f0); Vector2 pos_up0 = pos0 + u0 * hw; Vector2 pos_down0 = pos0 - u0 * hw; Color color0; Color color1; float current_distance0 = 0.f; float current_distance1 = 0.f; float total_distance; _interpolate_color = gradient != NULL; bool distance_required = _interpolate_color || texture_mode == LINE_TEXTURE_TILE; if (distance_required) total_distance = calculate_total_distance(points); if (_interpolate_color) color0 = gradient->get_color(0); else colors.push_back(default_color); float uvx0 = 0.f; float uvx1 = 0.f; // Begin cap if (begin_cap_mode == LINE_CAP_BOX) { // Push back first vertices a little bit pos_up0 -= f0 * hw; pos_down0 -= f0 * hw; // The line's outer length will be a little higher due to begin and end caps total_distance += width; current_distance0 += hw; current_distance1 = current_distance0; } else if (begin_cap_mode == LINE_CAP_ROUND) { if (texture_mode == LINE_TEXTURE_TILE) { uvx0 = 0.5f; } new_arc(pos0, pos_up0 - pos0, -Math_PI, color0, Rect2(0.f, 0.f, 1.f, 1.f)); total_distance += width; current_distance0 += hw; current_distance1 = current_distance0; } strip_begin(pos_up0, pos_down0, color0, uvx0); // pos_up0 ------------- pos_up1 -------------------- // | | // pos0 - - - - - - - - - pos1 - - - - - - - - - pos2 // | | // pos_down0 ------------ pos_down1 ------------------ // // i-1 i i+1 // http://labs.hyperandroid.com/tag/opengl-lines // (not the same implementation but visuals help a lot) // For each additional segment for (int i = 1; i < len - 1; ++i) { pos1 = points[i]; Vector2 pos2 = points[i + 1]; Vector2 f1 = (pos2 - pos1).normalized(); Vector2 u1 = rotate90(f1); // Determine joint orientation const float dp = u0.dot(f1); const Orientation orientation = (dp > 0.f ? UP : DOWN); Vector2 inner_normal0, inner_normal1; if (orientation == UP) { inner_normal0 = u0 * hw; inner_normal1 = u1 * hw; } else { inner_normal0 = -u0 * hw; inner_normal1 = -u1 * hw; } // --------------------------- // / // 0 / 1 // / / // --------------------x------ / // / / (here shown with orientation == DOWN) // / / // / / // / / // 2 / // / // Find inner intersection at the joint Vector2 corner_pos_in, corner_pos_out; SegmentIntersectionResult intersection_result = segment_intersection( pos0 + inner_normal0, pos1 + inner_normal0, pos1 + inner_normal1, pos2 + inner_normal1, &corner_pos_in); if (intersection_result == SEGMENT_INTERSECT) // Inner parts of the segments intersect corner_pos_out = 2.f * pos1 - corner_pos_in; else { // No intersection, segments are either parallel or too sharp corner_pos_in = pos1 + inner_normal0; corner_pos_out = pos1 - inner_normal0; } Vector2 corner_pos_up, corner_pos_down; if (orientation == UP) { corner_pos_up = corner_pos_in; corner_pos_down = corner_pos_out; } else { corner_pos_up = corner_pos_out; corner_pos_down = corner_pos_in; } LineJointMode current_joint_mode = joint_mode; Vector2 pos_up1, pos_down1; if (intersection_result == SEGMENT_INTERSECT) { // Fallback on bevel if sharp angle is too high (because it would produce very long miters) if (current_joint_mode == LINE_JOINT_SHARP && corner_pos_out.distance_squared_to(pos1) / hw_sq > sharp_limit_sq) { current_joint_mode = LINE_JOINT_BEVEL; } if (current_joint_mode == LINE_JOINT_SHARP) { // In this case, we won't create joint geometry, // The previous and next line quads will directly share an edge. pos_up1 = corner_pos_up; pos_down1 = corner_pos_down; } else { // Bevel or round if (orientation == UP) { pos_up1 = corner_pos_up; pos_down1 = pos1 - u0 * hw; } else { pos_up1 = pos1 + u0 * hw; pos_down1 = corner_pos_down; } } } else { // No intersection: fallback pos_up1 = corner_pos_up; pos_down1 = corner_pos_down; } // Add current line body quad // Triangles are clockwise if (distance_required) { current_distance1 += pos0.distance_to(pos1); } if (_interpolate_color) { color1 = gradient->get_color_at_offset(current_distance1 / total_distance); } if (texture_mode == LINE_TEXTURE_TILE) { uvx0 = current_distance0 / width; uvx1 = current_distance1 / width; } strip_add_quad(pos_up1, pos_down1, color1, uvx1); // Swap vars for use in the next line color0 = color1; u0 = u1; f0 = f1; pos0 = pos1; current_distance0 = current_distance1; if (intersection_result == SEGMENT_INTERSECT) { if (current_joint_mode == LINE_JOINT_SHARP) { pos_up0 = pos_up1; pos_down0 = pos_down1; } else { if (orientation == UP) { pos_up0 = corner_pos_up; pos_down0 = pos1 - u1 * hw; } else { pos_up0 = pos1 + u1 * hw; pos_down0 = corner_pos_down; } } } else { pos_up0 = pos1 + u1 * hw; pos_down0 = pos1 - u1 * hw; } // From this point, bu0 and bd0 concern the next segment // Add joint geometry if (current_joint_mode != LINE_JOINT_SHARP) { // ________________ cbegin // / \ // / \ // ____________/_ _ _\ cend // | | // | | // | | Vector2 cbegin, cend; if (orientation == UP) { cbegin = pos_down1; cend = pos_down0; } else { cbegin = pos_up1; cend = pos_up0; } if (current_joint_mode == LINE_JOINT_BEVEL) { strip_add_tri(cend, orientation); } else if (current_joint_mode == LINE_JOINT_ROUND) { Vector2 vbegin = cbegin - pos1; Vector2 vend = cend - pos1; strip_add_arc(pos1, vend.angle_to(vbegin), orientation); } if (intersection_result != SEGMENT_INTERSECT) // In this case the joint is too f****d up to be re-used, // start again the strip with fallback points strip_begin(pos_up0, pos_down0, color1, uvx1); } } // Last (or only) segment pos1 = points[points.size() - 1]; Vector2 pos_up1 = pos1 + u0 * hw; Vector2 pos_down1 = pos1 - u0 * hw; // End cap (box) if (end_cap_mode == LINE_CAP_BOX) { pos_up1 += f0 * hw; pos_down1 += f0 * hw; } if (distance_required) { current_distance1 += pos0.distance_to(pos1); } if (_interpolate_color) { color1 = gradient->get_color(gradient->get_points_count() - 1); } if (texture_mode == LINE_TEXTURE_TILE) { uvx1 = current_distance1 / width; } strip_add_quad(pos_up1, pos_down1, color1, uvx1); // End cap (round) if (end_cap_mode == LINE_CAP_ROUND) { // Note: color is not used in case we don't interpolate... Color color = _interpolate_color ? gradient->get_color(gradient->get_points_count() - 1) : Color(0, 0, 0); new_arc(pos1, pos_up1 - pos1, Math_PI, color, Rect2(uvx1 - 0.5f, 0.f, 1.f, 1.f)); } }