Improve variable name readability in line rendering code.

This commit is contained in:
Sasha Szpakowski
2023-01-14 12:13:35 -04:00
parent f6b74e9ce7
commit 8ae7342875
2 changed files with 79 additions and 79 deletions
+69 -69
View File
@@ -49,26 +49,26 @@ void Polyline::render(const Vector2 *coords, size_t count, size_t size_hint, flo
// compute sleeve // compute sleeve
bool is_looping = (coords[0] == coords[count - 1]); bool is_looping = (coords[0] == coords[count - 1]);
Vector2 s; Vector2 segment;
if (!is_looping) // virtual starting point at second point mirrored on first point if (!is_looping) // virtual starting point at second point mirrored on first point
s = coords[1] - coords[0]; segment = coords[1] - coords[0];
else // virtual starting point at last vertex else // virtual starting point at last vertex
s = coords[0] - coords[count - 2]; segment = coords[0] - coords[count - 2];
float len_s = s.getLength(); float segmentLength = segment.getLength();
Vector2 ns = s.getNormal(halfwidth / len_s); Vector2 segmentNormal = segment.getNormal(halfwidth / segmentLength);
Vector2 q, r(coords[0]); Vector2 pointA, pointB(coords[0]);
for (size_t i = 0; i + 1 < count; i++) for (size_t i = 0; i + 1 < count; i++)
{ {
q = r; pointA = pointB;
r = coords[i + 1]; pointB = coords[i + 1];
renderEdge(anchors, normals, s, len_s, ns, q, r, halfwidth); renderEdge(anchors, normals, segment, segmentLength, segmentNormal, pointA, pointB, halfwidth);
} }
q = r; pointA = pointB;
r = is_looping ? coords[1] : r + s; pointB = is_looping ? coords[1] : pointB + segment;
renderEdge(anchors, normals, s, len_s, ns, q, r, halfwidth); renderEdge(anchors, normals, segment, segmentLength, segmentNormal, pointA, pointB, halfwidth);
vertex_count = normals.size(); vertex_count = normals.size();
@@ -108,8 +108,8 @@ void Polyline::render(const Vector2 *coords, size_t count, size_t size_hint, flo
} }
void NoneJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals, void NoneJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals,
Vector2 &s, float &len_s, Vector2 &ns, Vector2 &segment, float &segmentLength, Vector2 &segmentNormal,
const Vector2 &q, const Vector2 &r, float hw) const Vector2 &pointA, const Vector2 &pointB, float halfWidth)
{ {
// ns1------ns2 // ns1------ns2
// | | // | |
@@ -117,19 +117,19 @@ void NoneJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vec
// | | // | |
// (-ns1)----(-ns2) // (-ns1)----(-ns2)
anchors.push_back(q); anchors.push_back(pointA);
anchors.push_back(q); anchors.push_back(pointA);
normals.push_back(ns); normals.push_back(segmentNormal);
normals.push_back(-ns); normals.push_back(-segmentNormal);
s = (r - q); segment = (pointB - pointA);
len_s = s.getLength(); segmentLength = segment.getLength();
ns = s.getNormal(hw / len_s); segmentNormal = segment.getNormal(halfWidth / segmentLength);
anchors.push_back(q); anchors.push_back(pointA);
anchors.push_back(q); anchors.push_back(pointA);
normals.push_back(ns); normals.push_back(segmentNormal);
normals.push_back(-ns); normals.push_back(-segmentNormal);
} }
@@ -171,41 +171,41 @@ void NoneJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vec
* the intersection points can be efficiently calculated using Cramer's rule. * the intersection points can be efficiently calculated using Cramer's rule.
*/ */
void MiterJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals, void MiterJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals,
Vector2 &s, float &len_s, Vector2 &ns, Vector2 &segment, float &segmentLength, Vector2 &segmentNormal,
const Vector2 &q, const Vector2 &r, float hw) const Vector2 &pointA, const Vector2 &pointB, float halfwidth)
{ {
Vector2 t = (r - q); Vector2 newSegment = (pointB - pointA);
float len_t = t.getLength(); float newSegmentLength = newSegment.getLength();
if (len_t == 0.0f) if (newSegmentLength == 0.0f)
{ {
// degenerate segment, skip it // degenerate segment, skip it
return; return;
} }
Vector2 nt = t.getNormal(hw / len_t); Vector2 newSegmentNormal = newSegment.getNormal(halfwidth / newSegmentLength);
anchors.push_back(q); anchors.push_back(pointA);
anchors.push_back(q); anchors.push_back(pointA);
float det = Vector2::cross(s, t); float det = Vector2::cross(segment, newSegment);
if (fabs(det) / (len_s * len_t) < LINES_PARALLEL_EPS && Vector2::dot(s, t) > 0) if (fabs(det) / (segmentLength * newSegmentLength) < LINES_PARALLEL_EPS && Vector2::dot(segment, newSegment) > 0)
{ {
// lines parallel, compute as u1 = q + ns * w/2, u2 = q - ns * w/2 // lines parallel, compute as u1 = q + ns * w/2, u2 = q - ns * w/2
normals.push_back(ns); normals.push_back(segmentNormal);
normals.push_back(-ns); normals.push_back(-segmentNormal);
} }
else else
{ {
// cramers rule // cramers rule
float lambda = Vector2::cross((nt - ns), t) / det; float lambda = Vector2::cross((newSegmentNormal - segmentNormal), newSegment) / det;
Vector2 d = ns + s * lambda; Vector2 d = segmentNormal + segment * lambda;
normals.push_back(d); normals.push_back(d);
normals.push_back(-d); normals.push_back(-d);
} }
s = t; segment = newSegment;
ns = nt; segmentNormal = newSegmentNormal;
len_s = len_t; segmentLength = newSegmentLength;
} }
/** Calculate line boundary points. /** Calculate line boundary points.
@@ -226,52 +226,52 @@ void MiterJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Ve
* uh1 = q + ns * w/2, uh2 = q + nt * w/2 * uh1 = q + ns * w/2, uh2 = q + nt * w/2
*/ */
void BevelJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals, void BevelJoinPolyline::renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals,
Vector2 &s, float &len_s, Vector2 &ns, Vector2 &segment, float &segmentLength, Vector2 &segmentNormal,
const Vector2 &q, const Vector2 &r, float hw) const Vector2 &pointA, const Vector2 &pointB, float halfWidth)
{ {
Vector2 t = (r - q); Vector2 newSegment = (pointB - pointA);
float len_t = t.getLength(); float newSegmentLength = newSegment.getLength();
float det = Vector2::cross(s, t); float det = Vector2::cross(segment, newSegment);
if (fabs(det) / (len_s * len_t) < LINES_PARALLEL_EPS && Vector2::dot(s, t) > 0) if (fabs(det) / (segmentLength * newSegmentLength) < LINES_PARALLEL_EPS && Vector2::dot(segment, newSegment) > 0)
{ {
// lines parallel, compute as u1 = q + ns * w/2, u2 = q - ns * w/2 // lines parallel, compute as u1 = q + ns * w/2, u2 = q - ns * w/2
Vector2 n = t.getNormal(hw / len_t); Vector2 newSegmentNormal = newSegment.getNormal(halfWidth / newSegmentLength);
anchors.push_back(q); anchors.push_back(pointA);
anchors.push_back(q); anchors.push_back(pointA);
normals.push_back(n); normals.push_back(newSegmentNormal);
normals.push_back(-n); normals.push_back(-newSegmentNormal);
s = t; segment = newSegment;
len_s = len_t; newSegmentLength = newSegmentLength;
return; // early out return; // early out
} }
// cramers rule // cramers rule
Vector2 nt = t.getNormal(hw / len_t); Vector2 newSegmentNormal = newSegment.getNormal(halfWidth / newSegmentLength);
float lambda = Vector2::cross((nt - ns), t) / det; float lambda = Vector2::cross((newSegmentNormal - segmentNormal), newSegment) / det;
Vector2 d = ns + s * lambda; Vector2 d = segmentNormal + segment * lambda;
anchors.push_back(q); anchors.push_back(pointA);
anchors.push_back(q); anchors.push_back(pointA);
anchors.push_back(q); anchors.push_back(pointA);
anchors.push_back(q); anchors.push_back(pointA);
if (det > 0) // 'left' turn -> intersection on the top if (det > 0) // 'left' turn -> intersection on the top
{ {
normals.push_back(d); normals.push_back(d);
normals.push_back(-ns); normals.push_back(-segmentNormal);
normals.push_back(d); normals.push_back(d);
normals.push_back(-nt); normals.push_back(-newSegmentNormal);
} }
else else
{ {
normals.push_back(ns); normals.push_back(segmentNormal);
normals.push_back(-d); normals.push_back(-d);
normals.push_back(nt); normals.push_back(newSegmentNormal);
normals.push_back(-d); normals.push_back(-d);
} }
s = t; segment = newSegment;
len_s = len_t; segmentLength = newSegmentLength;
ns = nt; segmentNormal = newSegmentNormal;
} }
void Polyline::calc_overdraw_vertex_count(bool is_looping) void Polyline::calc_overdraw_vertex_count(bool is_looping)
+10 -10
View File
@@ -77,18 +77,18 @@ protected:
/** Calculate line boundary points. /** Calculate line boundary points.
* *
* @param[out] anchors Anchor points defining the core line. * @param[out] anchors Anchor points defining the core line.
* @param[out] normals Normals defining the edge of the sleeve. * @param[out] normals Normals defining the edge of the sleeve.
* @param[in,out] s Direction of segment pq (updated to the segment qr). * @param[in,out] segment Direction of segment pq (updated to the segment qr).
* @param[in,out] len_s Length of segment pq (updated to the segment qr). * @param[in,out] segmentLength Length of segment pq (updated to the segment qr).
* @param[in,out] ns Normal on the segment pq (updated to the segment qr). * @param[in,out] segmentNormal Normal on the segment pq (updated to the segment qr).
* @param[in] q Current point on the line. * @param[in] pointA Current point on the line (q).
* @param[in] r Next point on the line. * @param[in] pointB Next point on the line (r).
* @param[in] hw Half line width (see Polyline.render()). * @param[in] halfWidth Half line width (see Polyline.render()).
*/ */
virtual void renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals, virtual void renderEdge(std::vector<Vector2> &anchors, std::vector<Vector2> &normals,
Vector2 &s, float &len_s, Vector2 &ns, Vector2 &segment, float &segmentLength, Vector2 &segmentNormal,
const Vector2 &q, const Vector2 &r, float hw) = 0; const Vector2 &pointA, const Vector2 &pointB, float halfWidth) = 0;
Vector2 *vertices; Vector2 *vertices;
Vector2 *overdraw; Vector2 *overdraw;