All pastes #2064989 Raw Edit

Unnamed

public text v1 · immutable
#2064989 ·published 2011-05-19 19:12 UTC
rendered paste body
#include <fstream>
#include <vector>
#include <cmath>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_2.h>
#include <CGAL/ch_graham_andrew.h>
#include <CGAL/Delaunay_triangulation_adaptation_traits_2.h>
#include <CGAL/Voronoi_diagram_2.h>
#include <CGAL/Interpolation_traits_2.h>
#include <CGAL/natural_neighbor_coordinates_2.h>
#include <CGAL/interpolation_functions.h>
#include <CGAL/point_generators_2.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef K::Point_3 Point_3;
typedef CGAL::Segment_2<K> Segment_2;
typedef K::FT Coord_type;
typedef CGAL::Interpolation_traits_2<K> Traits;
typedef K::Iso_rectangle_2 Iso_rectangle_2;
typedef CGAL::Delaunay_triangulation_2<K> Delaunay;
typedef CGAL::Delaunay_triangulation_2<K>::Finite_edges_iterator Finite_edges_iterator;

#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QtDebug>



using namespace std;

class Widget : public QWidget 
{
	private:
	double X;
	double Y;
	double Width;
	double Height;
	QImage ColorBar;
	
	Delaunay dt;
	std::vector<K::Point_3> DataPoints;
	std::vector<K::Point_2> EdgePoints;
	std::vector<K::Point_2> ConvexHull;
	
	double Min;
	double Max;
	
	typedef CGAL::Data_access< std::map<Point_2, Coord_type, K::Less_xy_2 > > ValueAccess;
	std::map<Point_2, Coord_type, K::Less_xy_2> FunctionValues;
	
	
	public:
	
	void Widget(
		std::vector<K::Point_3> Data_Points,
		std::vector<K::Point_2> Edge_Points,
		std::vector<K::Point_2> Convex_Hull,
		double ColorBarMin,
		double ColorBarMax,
		char *colorbarfile)
	{
		DataPoints = Data_Points;
		EdgePoints = Edge_Points;
		ConvexHull = Convex_Hull;
		Min = ColorBarMin;
		Max = ColorBarMax;
		ColorBar("Colorbar.png");
		
		X = 0;
		Y = 66;
		Width = 35;
		Height = -13;
		
	}
	
	void SetInterpolation()
	{
		for (unsigned int i = 0; i < DataPoints.size(); i++)
		{	
			//TODO: Once file stuff worked out switch 1 and 0 around
			K::Point_2 tempXY(DataPoints[i].cartesian(1), DataPoints[i].cartesian(0));
			double tempValue = DataPoints[i].cartesian(2);
			
			FunctionValues.insert(std::make_pair(tempXY,tempValue));
    	}
	}
	
	double LinearInterpolation(double X,double Y)
	{
		K::Point_2 p(X,Y);
		std::vector< std::pair< Point_2, Coord_type > > coords;
		
		Coord_type norm = CGAL::natural_neighbor_coordinates_2(dt,p,std::back_inserter(coords)).second;
		  
		Coord_type res =  CGAL::linear_interpolation(coords.begin(), 
			coords.end(), norm, ValueAccess(FunctionValues));
			
		return res;

	}
	
	
	void DrawInterpolation(QPainter painter)
	{

	
		for(int x = 0; x < width()+100; x++)
		{
			for(int y = 0; y < height()+100; y++)
			{
				double tempX = X + Width*(double)x/(double)(width()+100);
				double tempY = Y + Height*(double)y/(double)(height()+100);


				
				double InterpolResult = LinearInterpolation(tempX,tempY);
				double Scale = Max - Min;
				
				//Check if defined
				if(InterpolResult > 0.01 )
				{
					
					double HeightPixel = (InterpolResult -Min)  / Scale *ColorBar.height() ;
					
					if(HeightPixel > ColorBar.height()-1)
					{
						HeightPixel = ColorBar.height()-1;
					}
					else if(HeightPixel < 1)
					{
						HeightPixel = 1;
					}
					
					
					painter.setPen(  QColor( ColorBar.pixel(0,HeightPixel)  )  );
					painter.drawPoint(QPointF (tempX,tempY));
				}
				
			}
		}
	}
	
	void DrawDataPoints(QPainter painter)
	{

	
		painter.setPen(Qt::red);
		for(unsigned int i = 0; i < DataPoints.size(); i++)
		{
			//TODO: Once file stuff worked out switch 1 and 0 around
			painter.drawPoint(QPointF (DataPoints[i].cartesian(1),DataPoints[i].cartesian(0)));
		}
	}
	
	void DrawTriangulation(QPainter painter)
	{

		
		painter.setPen(Qt::blue);
		for(Finite_edges_iterator eit = dt.finite_edges_begin(); 
		eit != dt.finite_edges_end(); eit++)
		{
		
			painter.drawLine(QLineF(
				dt.segment(eit).source().cartesian(0), dt.segment(eit).source().cartesian(1),
				dt.segment(eit).target().cartesian(0), dt.segment(eit).target().cartesian(1)
			));
		}
	}
	
	void DrawEdgesOfMap(QPainter painter)
	{

	
		painter.setPen(Qt::black);
		for(unsigned int i = 1; i < EdgePoints.size(); i++)
		{
			if(EdgePoints[i].cartesian(0) == 999 || EdgePoints[i-1].cartesian(0) == 999)
				i++;
			
			else
				painter.drawLine(QLineF(
					EdgePoints[i-1].cartesian(0), EdgePoints[i-1].cartesian(1),
					EdgePoints[i  ].cartesian(0), EdgePoints[i  ].cartesian(1))
				);
				
		}
	}
	

	public:
	
	void paintEvent(QPaintEvent *) 
	{
		QPainter painter(this);
		painter.setWindow(X,Y,Width,Height);
	
		SetInterpolation();
		
		DrawInterpolation(painter);
		DrawDataPoints(painter);
		DrawTriangulation(painter);
		DrawEdgesOfMap(painter);
		
	}
};