LineClipper: name and unify clip states (#519)

This commit is contained in:
Gustl22 2018-03-23 11:04:39 +01:00 committed by Emux
parent c3b62fbdf5
commit 27a3c5a926
No known key found for this signature in database
GPG Key ID: 89C6921D7AF2BDD0
4 changed files with 22 additions and 20 deletions

View File

@ -396,11 +396,11 @@ public class PathLayer extends Layer implements GestureListener {
} }
int clip = mClipper.clipNext(x, y); int clip = mClipper.clipNext(x, y);
if (clip < 1) { if (clip != LineClipper.INSIDE) {
if (i > 2) if (i > 2)
ll.addLine(projected, i, false); ll.addLine(projected, i, false);
if (clip < 0) { if (clip == LineClipper.INTERSECTION) {
/* add line segment */ /* add line segment */
segment = mClipper.getLine(segment, 0); segment = mClipper.getLine(segment, 0);
ll.addLine(segment, 4, false); ll.addLine(segment, 4, false);
@ -412,7 +412,7 @@ public class PathLayer extends Layer implements GestureListener {
} }
i = 0; i = 0;
// if the end point is inside, add it // if the end point is inside, add it
if (mClipper.getPrevOutcode() == 0) { if (mClipper.getPrevOutcode() == LineClipper.INSIDE) {
projected[i++] = prevX; projected[i++] = prevX;
projected[i++] = prevY; projected[i++] = prevY;
} }

View File

@ -556,7 +556,7 @@ public class ExtrusionBucket extends RenderBucket {
} }
/* check if face is within tile */ /* check if face is within tile */
if (mClipper.clipNext((int) nx, (int) ny) == 0) { if (mClipper.clipNext((int) nx, (int) ny) == LineClipper.OUTSIDE) {
even = ++even % 2; even = ++even % 2;
continue; continue;
} }

View File

@ -333,28 +333,27 @@ public final class PolygonBucket extends RenderBucket {
/* project bbox of polygon to screen */ /* project bbox of polygon to screen */
v.mvp.prj2D(pb.bbox, 0, box, 0, 4); v.mvp.prj2D(pb.bbox, 0, box, 0, 4);
int out = 0; int out = LineClipper.INSIDE;
for (int i = 0; i < 8; i += 2) { for (int i = 0; i < 8; i += 2) {
int o = mScreenClip.outcode(box[i], box[i + 1]); int o = mScreenClip.outcode(box[i], box[i + 1]);
if (o == 0) { if (o == LineClipper.INSIDE) {
/* at least one corner is inside */ /* at least one corner is inside */
out = 0; out = LineClipper.INSIDE;
break; break;
} }
out |= o; out |= o;
} }
/* Check if any polygon-bucket edge intersects the screen. /* Check if any polygon-bucket edge intersects the screen.
* Also check the very unlikely case where the view might * Also check the very unlikely case where the view might
* be * be completely contained within box */
* completly contained within box */ if ((out != LineClipper.INSIDE) && (out != LineClipper.OUTSIDE)) {
if ((out != 0) && (out != 0xF)) {
mScreenClip.clipStart(box[6], box[7]); mScreenClip.clipStart(box[6], box[7]);
out = 0; out = LineClipper.OUTSIDE;
for (int i = 0; i < 8 && out == 0; i += 2) for (int i = 0; i < 8 && out == LineClipper.OUTSIDE; i += 2)
out = mScreenClip.clipNext(box[i], box[i + 1]); out = mScreenClip.clipNext(box[i], box[i + 1]);
if (out == 0) { if (out == LineClipper.OUTSIDE) {
//log.debug("out {}\n {}\n {}", out, Arrays.toString(pb.bbox), Arrays.toString(box)); //log.debug("out {}\n {}\n {}", out, Arrays.toString(pb.bbox), Arrays.toString(box));
// log.debug("outside {} {} {}", out, // log.debug("outside {} {} {}", out,

View File

@ -1,6 +1,7 @@
/* /*
* Copyright 2012, 2013 Hannes Janetzek * Copyright 2012, 2013 Hannes Janetzek
* Copyright 2016 Bezzu * Copyright 2016 Bezzu
* Copyright 2018 Gustl22
* *
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
* *
@ -31,6 +32,8 @@ public class LineClipper {
public static final int RIGHT = 2; // 0010 public static final int RIGHT = 2; // 0010
public static final int BOTTOM = 4; // 0100 public static final int BOTTOM = 4; // 0100
public static final int TOP = 8; // 1000 public static final int TOP = 8; // 1000
public static final int OUTSIDE = 0xF; // 1111
public static final int INTERSECTION = -1;
private float xmin, xmax, ymin, ymax; private float xmin, xmax, ymin, ymax;
@ -90,8 +93,8 @@ public class LineClipper {
} }
/** /**
* @return 0 if not intersection, 1 fully within, -1 clipped (and 'out' set * @return OUTSIDE (= 1111) if not intersection, INSIDE (= 0) if fully within,
* to new points) * INTERSECTION (= -1) if clipped (and 'out' set to new points)
*/ */
public int clipNext(float x1, float y1) { public int clipNext(float x1, float y1) {
int accept; int accept;
@ -109,12 +112,12 @@ public class LineClipper {
if ((mPrevOutcode | outcode) == 0) { if ((mPrevOutcode | outcode) == 0) {
// Bitwise OR is 0. Trivially accept // Bitwise OR is 0. Trivially accept
accept = 1; accept = INSIDE;
} else if ((mPrevOutcode & outcode) != 0) { } else if ((mPrevOutcode & outcode) != 0) {
// Bitwise AND is not 0. Trivially reject // Bitwise AND is not 0. Trivially reject
accept = 0; accept = OUTSIDE;
} else { } else {
accept = clip(mPrevX, mPrevY, x1, y1, mPrevOutcode, outcode) ? -1 : 0; accept = clip(mPrevX, mPrevY, x1, y1, mPrevOutcode, outcode) ? INTERSECTION : OUTSIDE;
} }
mPrevOutcode = outcode; mPrevOutcode = outcode;
mPrevX = x1; mPrevX = x1;
@ -258,10 +261,10 @@ public class LineClipper {
y = in.points[inPos++]; y = in.points[inPos++];
int clip = clipNext(x, y); int clip = clipNext(x, y);
if (clip == 0) { if (clip == OUTSIDE) {
/* current segment is fully outside */ /* current segment is fully outside */
inside = false; // needed? inside = false; // needed?
} else if (clip == 1) { } else if (clip == INSIDE) {
/* current segment is fully within */ /* current segment is fully within */
out.addPoint(x, y); out.addPoint(x, y);
} else { /* clip == -1 */ } else { /* clip == -1 */