Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wearenocomputer committed Aug 12, 2013
0 parents commit bbc6122
Show file tree
Hide file tree
Showing 17 changed files with 3,006 additions and 0 deletions.
16 changes: 16 additions & 0 deletions example/src/main.cpp
@@ -0,0 +1,16 @@
#include "ofMain.h"
#include "testApp.h"
#include "ofAppGlutWindow.h"

//========================================================================
int main( ){

ofAppGlutWindow window;
ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context

// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new testApp());

}
88 changes: 88 additions & 0 deletions example/src/testApp.cpp
@@ -0,0 +1,88 @@
#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

ofBackground(255, 255, 255);

/*************************
//SET BOUNDS
*************************/
//CREATE A RECTANGLE with ofPolyLine with multiple points on each side
int numberofverticesperside=8;
bounds.addVertex(ofPoint(0,0));
bounds.bezierTo(ofPoint(0,ofGetHeight()/3), ofPoint(0,ofGetHeight()-(ofGetHeight()/3)), ofPoint(0,ofGetHeight()), numberofverticesperside);
bounds.bezierTo(ofPoint(ofGetWidth()/3,ofGetHeight()), ofPoint(ofGetWidth()-(ofGetWidth()/3),ofGetHeight()), ofPoint(ofGetWidth(),ofGetHeight()), numberofverticesperside);
bounds.bezierTo(ofPoint(ofGetWidth(),ofGetHeight()-(ofGetHeight()/3)), ofPoint(ofGetWidth(),ofGetHeight()/3), ofPoint(ofGetWidth(),0), numberofverticesperside);
bounds.bezierTo(ofPoint(ofGetWidth()-(ofGetWidth()/3),0), ofPoint(ofGetWidth()/3,0), ofPoint(0,0), numberofverticesperside);

//COPY THE VERTICES form ofPolyLine into ofMesh
//We dot this te easily remove double vertices created by the bezierTo
for (int i=0;i<bounds.size();++i) {
polypoints.addVertex(ofVec3f(bounds[i].x,bounds[i].y,0));
}
polypoints.setMode(OF_PRIMITIVE_POINTS);
//REMOVE DOUBLE VERTICES
//POLY2TRI DOES NOT LIKE THAT
polypoints.removeVertex(0);
polypoints.removeVertex(numberofverticesperside-1);
polypoints.removeVertex((numberofverticesperside-1)*2);
polypoints.removeVertex((numberofverticesperside-1)*3);
polypoints.removeVertex((numberofverticesperside-1)*4);


/*************************
//SET POINTS (in bounds)
*************************/

for (int i=0;i<200;++i) {
ofPoint newpoint;
newpoint.x = ofRandom(ofGetWidth());
newpoint.y = ofRandom(ofGetHeight());
points.push_back(newpoint);
}


/*************************
//ADD HOLES
*************************/

ofPolyline line0;
line0.addVertex(10,10);
line0.addVertex(10,200);
line0.addVertex(200,200);
line0.addVertex(200,10);
line0.close();
ofPolyline line1;
line1.addVertex(300,300);
line1.addVertex(300,500);
line1.addVertex(500,500);
line1.addVertex(500,300);
line1.close();
holes.push_back(line0);
holes.push_back(line1);


triangulator.setSteinerPoints(points);
triangulator.setHoles(holes);


//DO TRIANGULATION
triangulator.triangulate(polypoints);

}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){
triangulator.triangulatedMesh.draw();
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
triangulator.triangulate(polypoints);
}
24 changes: 24 additions & 0 deletions example/src/testApp.h
@@ -0,0 +1,24 @@
#pragma once

#include "ofMain.h"
#include "ofxPoly2Tri.h"


class testApp : public ofBaseApp{

public:
void setup();
void update();
void draw();

void keyPressed (int key);


ofxPoly2Tri triangulator;

ofPolyline bounds;
ofMesh polypoints;

vector<ofPoint> points;
vector<ofPolyline>holes;
};
108 changes: 108 additions & 0 deletions libs/poly2tri/advancing_front.cc
@@ -0,0 +1,108 @@
/*
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
* http://code.google.com/p/poly2tri/
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Poly2Tri nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "advancing_front.h"

namespace p2t {

AdvancingFront::AdvancingFront(Node& head, Node& tail)
{
head_ = &head;
tail_ = &tail;
search_node_ = &head;
}

Node* AdvancingFront::LocateNode(const double& x)
{
Node* node = search_node_;

if (x < node->value) {
while ((node = node->prev) != NULL) {
if (x >= node->value) {
search_node_ = node;
return node;
}
}
} else {
while ((node = node->next) != NULL) {
if (x < node->value) {
search_node_ = node->prev;
return node->prev;
}
}
}
return NULL;
}

Node* AdvancingFront::FindSearchNode(const double& x)
{
(void)x; // suppress compiler warnings "unused parameter 'x'"
// TODO: implement BST index
return search_node_;
}

Node* AdvancingFront::LocatePoint(const Point* point)
{
const double px = point->x;
Node* node = FindSearchNode(px);
const double nx = node->point->x;

if (px == nx) {
if (point != node->point) {
// We might have two nodes with same x value for a short time
if (point == node->prev->point) {
node = node->prev;
} else if (point == node->next->point) {
node = node->next;
} else {
assert(0);
}
}
} else if (px < nx) {
while ((node = node->prev) != NULL) {
if (point == node->point) {
break;
}
}
} else {
while ((node = node->next) != NULL) {
if (point == node->point)
break;
}
}
if(node) search_node_ = node;
return node;
}

AdvancingFront::~AdvancingFront()
{
}

}
118 changes: 118 additions & 0 deletions libs/poly2tri/advancing_front.h
@@ -0,0 +1,118 @@
/*
* Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
* http://code.google.com/p/poly2tri/
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Poly2Tri nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ADVANCED_FRONT_H
#define ADVANCED_FRONT_H

#include "shapes.h"

namespace p2t {

struct Node;

// Advancing front node
struct Node {
Point* point;
Triangle* triangle;

Node* next;
Node* prev;

double value;

Node(Point& p) : point(&p), triangle(NULL), next(NULL), prev(NULL), value(p.x)
{
}

Node(Point& p, Triangle& t) : point(&p), triangle(&t), next(NULL), prev(NULL), value(p.x)
{
}

};

// Advancing front
class AdvancingFront {
public:

AdvancingFront(Node& head, Node& tail);
// Destructor
~AdvancingFront();

Node* head();
void set_head(Node* node);
Node* tail();
void set_tail(Node* node);
Node* search();
void set_search(Node* node);

/// Locate insertion point along advancing front
Node* LocateNode(const double& x);

Node* LocatePoint(const Point* point);

private:

Node* head_, *tail_, *search_node_;

Node* FindSearchNode(const double& x);
};

inline Node* AdvancingFront::head()
{
return head_;
}
inline void AdvancingFront::set_head(Node* node)
{
head_ = node;
}

inline Node* AdvancingFront::tail()
{
return tail_;
}
inline void AdvancingFront::set_tail(Node* node)
{
tail_ = node;
}

inline Node* AdvancingFront::search()
{
return search_node_;
}

inline void AdvancingFront::set_search(Node* node)
{
search_node_ = node;
}

}

#endif

0 comments on commit bbc6122

Please sign in to comment.