Convex hull class

Well I think i’ve just ironed out all the bugs in the view culling for this by now, so I thought maybe others would find it handy.

Basically you feed the class with a bunch of 2d points and then just call calculateHull(), which strips the points down to a minimal set (and puts them in the correct ordering, natch). It uses the Jarvis’ march method (also known as the gif-wrapping method) which makes it good for use with lots of points but few actual edges in the bounding hull (like i’m using it for). But even with complex objects it still seems to be pretty quick.
I use this with Cohen-Sutherland view clipping for my world sectors (see the isVisible method) but thats a pretty simple extension of any point-in-view testing.

Enjoy :smiley:


package PrismEngine;


import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.input.*;

import java.io.*;
import java.nio.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

import OrangyTang.OpenGLToolkit.*;


public class ConvexHull2D
{
      // Points is filled with points to test, then stripped down to minimal set when hull calcualted
      private ArrayList points;
      
      private ArrayList testedPoints;
      
      
      public ConvexHull2D()
      {
            points = new ArrayList();
            testedPoints = new ArrayList();
      }
      
      
      public void addPoint(float x, float y)
      {
            Point2f newPoint = new Point2f(x, y);
            
            points.add(newPoint);
      }
      
      public void clear()
      {
            points.clear();
      }
      
      public void calculateHull()
      {
            if (points.size() > 0)
            {
                  // Holds the points of the calculated hull
                  ArrayList hullPoints = new ArrayList();
                  
                  
                  // First find an extreme point guranteed to be on the hull
                  // Start from the first point and compare all others for minimal y coord
                  Point2f startPoint = (Point2f)points.get(0);
                  
                  for (int i=0; i<points.size(); i++)
                  {
                        Point2f testPoint = (Point2f)points.get(i);
                        
                        // Find lowest y, and lowest x if equal y values.
                        if (testPoint.y < startPoint.y)
                        {
                              startPoint = testPoint;
                        }
                        else if (testPoint.y == startPoint.y)
                        {
                              if (testPoint.x < startPoint.x)
                                    startPoint = testPoint;
                        }
                  }
                  
                  // Add the start point
                  hullPoints.add(startPoint);
                  
                  Point2f currentPoint = startPoint;
                  Point2f currentDirection = new Point2f(1.0f, 0.0f);
                  Point2f nextPoint = null;
                  
                  int debug = 0;
                  
                  // March around the edge. Finish when we get back to where we started
                  while (true)
                  {
                        // Find next point with largest right turn relative to current
                        float currentAngle = 181f;
                        for (int i=0; i<points.size(); i++)
                        {
                              Point2f testPoint = (Point2f)points.get(i);
                              
                              // Find angle between test and current points
                              Point2f testDirection = new Point2f(testPoint);
                              testDirection.subtract(currentPoint);
                              testDirection.normalise();
                              
                              float testAngle = currentDirection.angle(testDirection);
                              
                              // Update next point with test if smaller angle
                              if (testAngle < currentAngle)
                              {
                                    currentAngle = testAngle;
                                    nextPoint = testPoint;
                              }
                              else if (testAngle == currentAngle)
                              {
                                    // take point furthest away from current
                                    if (currentPoint.distanceTo(testPoint) > currentPoint.distanceTo(nextPoint) )
                                          nextPoint = testPoint;
                              }
                        }
                        
                        
                        // Exit?
                        if (nextPoint == hullPoints.get(0) || debug>1000)
                              break;
                        
                        
                        // Add and advance
                        hullPoints.add(nextPoint);
                        
                        currentDirection.set(nextPoint);
                        currentDirection.subtract(currentPoint);
                        
                        currentPoint = nextPoint;
                        nextPoint = null;
                        
                        debug++;
                  }
                  
                  points = hullPoints;
                  
            } // fi points>0
      }
      
      
      
      public boolean isVisible(Frustum viewFrustum)
      {
            return isVisible(viewFrustum, null);
      }
      
      public boolean isVisible(Frustum viewFrustum, Matrix4f transformMatrix)
      {
            // Debug stuff. Transform and store tested point for debug rendering.
            if (transformMatrix != null)
            {
                  // Transform each point and store
                  testedPoints.clear();
                  Vector3f currentVector = new Vector3f();
                  
                  for (int i=0; i<points.size(); i++)
                  {
                        Vector3f newVector = new Vector3f();
                        Point2f currentPoint = (Point2f)points.get(i);
                        currentVector.set(currentPoint.x, currentPoint.y, 0f);
                        
                        newVector.transformPoint(transformMatrix, currentVector);
                        testedPoints.add(newVector);
                  }
            }
            
            
            int regionCodes = 0;
            for (int i=0; i<points.size(); i++)
            {
                  Point2f testPoint = (Point2f)points.get(i);
                  
                  int currentCode = viewFrustum.findRegion(testPoint.x, testPoint.y, 0.0f, transformMatrix);
                  
                  if (i == 0)
                  {
                        // first point, just set.
                        regionCodes = currentCode;
                  }
                  else
                  {
                        // cumulative and'ing
                        regionCodes &= currentCode;
                  }
            }
            
            
            if (regionCodes == 0)
            {
                  return true;
            }
            else
            {
                  return false;
            }
      }
      
      
      public void render(GL gl, float height)
      {
            gl.begin(GL.LINE_LOOP);
            {
                  for (int i=0; i<points.size(); i++)
                  {
                        Point2f current = (Point2f)points.get(i);
                        gl.vertex3f(current.x, current.y, height);
                        gl.vertex3f(current.x, current.y, 0.0f);
                        gl.vertex3f(current.x, current.y, height);
                  }
            }
            gl.end();
      }
      
      
      public void renderTested(GL gl)
      {
            gl.begin(GL.LINES);
            {
                  for (int i=0; i<testedPoints.size(); i++)
                  {
                        Vector3f point = (Vector3f)testedPoints.get(i);
                        
                        gl.vertex3f(point.x, point.y, point.z);
                        gl.vertex3f(point.x, point.y, point.z+1f);
                  }
            }
            gl.end();
      }
}

And the associated Point2f class I use:


package PrismEngine;


import org.lwjgl.Math;

import OrangyTang.OpenGLToolkit.*;


public class Point2f
{
      public float x;
      public float y;
      
      public Point2f()
      {
            this.x = 0f;
            this.y = 0f;
      }
      
      public Point2f(float x, float y)
      {
            this.x = x;
            this.y = y;
      }
      
      public Point2f(Point2f point)
      {
            this.x = point.x;
            this.y = point.y;
      }
      
      public void set(Point2f point)
      {
            this.x = point.x;
            this.y = point.y;
      }
      
      public void set(float x, float y)
      {
            this.x = x;
            this.y = y;
      }
      
      // Subtracts a point from this one
      public void subtract(Point2f point)
      {
            x -= point.x;
            y -= point.y;
      }
      
      // Adds a point to this one
      public void add(Point2f point)
      {
            x += point.x;
            y += point.y;
      }
      
      public float dot(Point2f point)
      {
            return point.x*x + point.y*y;
      }
      
      public float length()
      {
            float dot = dot(this);
            return Math.sqrt(dot);            
      }
      
      public void normalise()
      {
            float length = length();
            x = x/length;
            y = y/length;
      }
      
      
      public float angle(Point2f point)
      {
            float angle = (this.dot(point))/(this.length()*point.length() );
            
            return Math.acos(angle);
      }
      
      
      public float distanceTo(Point2f point)
      {
            float dx = point.x - x;
            float dy = point.y - y;
            
            // Find length of dx,dy
            return Math.sqrt(dx*dx + dy*dy);
      }
}