<commit_before>package de.hft_stuttgart.swp2.opencl.view;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.TimeZone;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jocl.CLException;

import com.jogamp.opengl.util.FPSAnimator;

import de.hft_stuttgart.swp2.model.Building;
import de.hft_stuttgart.swp2.model.City;
import de.hft_stuttgart.swp2.model.ShadowTriangle;
import de.hft_stuttgart.swp2.model.Triangle;
import de.hft_stuttgart.swp2.model.Vertex;
import de.hft_stuttgart.swp2.opencl.CalculatorImpl;
import de.hft_stuttgart.swp2.opencl.CalculatorInterface;
import de.hft_stuttgart.swp2.opencl.OpenClException;
import de.hft_stuttgart.swp2.opencl.ShadowPrecision;
import de.hft_stuttgart.swp2.opencl.SunPositionCalculator;
import de.hft_stuttgart.swp2.opencl.VolumeTest;

public class ShadowViewer extends JFrame implements GLEventListener,
		KeyListener, MouseListener, MouseMotionListener {

	private static final long serialVersionUID = 6681486095144440340L;
	private static final int FPS = 60;
	
	private GLU glu;
	private Camera camera;
	private Robot robot;
	private int halfScreenHeight;
	private int halfScreenWidth;
	private float r = 10000;
	private boolean enableDrawCenters = false;
	
	private Calendar utcCal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
	private SunPositionCalculator[][] sunPositions;
	private SunPositionCalculator sunPos;
	private int hour = 6;
	private int month = 0;
	
	private int ray = 0;
	
	private static final boolean showGrid = true;


	public static void main(String[] args) throws OpenClException {
//		CL.setLogLevel(CL.LogLevel.LOG_DEBUGTRACE);
		try {
			final ShadowViewer view = new ShadowViewer();
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					view.setVisible(true);
				}
			});
		} catch (CLException e){
			e.printStackTrace();
			System.out.println(e.getStatus());
		}
	}

	public ShadowViewer() throws OpenClException {
		super("Shadow view");
		
		// test values
		VolumeTest.testCity2();
		VolumeTest.testCity2();
		VolumeTest.testCity2();
		City.getInstance().getBuildings().get(1).translate(0, 0, 5);
		City.getInstance().getBuildings().get(2).translate(5, 0, 5);
		City.getInstance().getBuildings().get(2).scale(1, 5, 1);
		
		int size = 20;
		Vertex v0 = new Vertex(-size, 0, -size);
		Vertex v1 = new Vertex(-size, 0, size);
		Vertex v2 = new Vertex(size, 0, size);
		Vertex v3 = new Vertex(size, 0, -size);
		
		Triangle t1 = new Triangle(v0, v1, v2);
		Triangle t2 = new Triangle(v0, v2, v3);
		
		Building b = new Building();
		b.addTriangle(t1);
		b.addTriangle(t2);
		City.getInstance().addBuilding(b);
	
		sunPositions = new SunPositionCalculator[12][24];
		for (int j = 1; j < 13; ++j) {
			for (int i = 0; i < sunPositions[j - 1].length; i++) {
				utcCal.set(2014, j, 1, i, 0, 0);
				sunPositions[j - 1][i] = new SunPositionCalculator(utcCal.getTime(), 11.6, 48.1);
			}
		}
		utcCal.set(2014, month + 1, 1, hour, 0, 0);
		sunPos = new SunPositionCalculator(utcCal.getTime(), 11.6, 48.1);
		
		CalculatorInterface calc = new CalculatorImpl();
		System.out.println("Starting shadow calculation...");
		long start = System.currentTimeMillis(); 
		calc.calculateShadow(ShadowPrecision.ULTRA); //VERY_LOW(5f), LOW(2.5f), MID(1.25f), HIGH(0.75f), ULTRA(0.375f), HYPER(0.1f), AWESOME(0.01f)
		long end = System.currentTimeMillis();
		System.out.printf("calculate shadow took %d milliseconds\n", (end - start));
		
		
		halfScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height / 2;
		halfScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width / 2;

		try {
			robot = new Robot();
		} catch (AWTException e) {
		}

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// set up the drawing canvas
		GLCanvas canvas = new GLCanvas();
		canvas.setPreferredSize(new Dimension(1024, 768));
		getContentPane().add(canvas);
		
		// draw the scene at FPS fps
		FPSAnimator animator = new FPSAnimator(canvas, FPS);
		animator.start();
		
		canvas.addGLEventListener(this);
		this.addKeyListener(this);
		canvas.addKeyListener(this);
		canvas.addMouseListener(this);
		canvas.addMouseMotionListener(this);
		pack();
		this.setLocationRelativeTo(null);
		robot.mouseMove(halfScreenWidth, halfScreenHeight);
	}

	@Override
	public void display(GLAutoDrawable drawable) {
		GL2 gl = drawable.getGL().getGL2();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		// apply camera modifications
		camera.lookAt();
		// drawing buildings
		gl.glColor3f(1f, 1f, 1f);
		for (Building b : City.getInstance().getBuildings()) {
			for (ShadowTriangle t : b.getShadowTriangles()) {
//				gl.glBegin(GL2.GL_LINE_LOOP);
				gl.glBegin(GL2.GL_TRIANGLES);
				gl.glColor3f(0, 1, 0);
				if (ray != -1 && t.getShadowSet().get(ray)) {
					gl.glColor3f(1, 0, 0);
				}
				for (Vertex v : t.getVertices()) {
					gl.glVertex3fv(v.getCoordinates(), 0);
				}
				gl.glEnd();
				if(showGrid) {
					gl.glColor3f(0, 0, 0);
					gl.glBegin(GL2.GL_LINE_LOOP);
	//				gl.glBegin(GL2.GL_TRIANGLES);
					for (Vertex v : t.getVertices()) {
						gl.glVertex3fv(v.getCoordinates(), 0);
					}
					gl.glEnd();
				}
				gl.glColor3f(255f, 0, 255f);
				gl.glBegin(GL2.GL_LINE_LOOP);
				for (int i = 0; i < sunPositions[month].length; i++) {
					gl.glVertex3d(sunPositions[month][i].getX(), sunPositions[month][i].getY(), sunPositions[month][i].getZ());
				}
				gl.glEnd();
				
				gl.glBegin(GL2.GL_LINES);<commit_msg>inverted colors if sun is beneath horizon<commit_after>package de.hft_stuttgart.swp2.opencl.view;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.TimeZone;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jocl.CLException;

import com.jogamp.opengl.util.FPSAnimator;

import de.hft_stuttgart.swp2.model.Building;
import de.hft_stuttgart.swp2.model.City;
import de.hft_stuttgart.swp2.model.ShadowTriangle;
import de.hft_stuttgart.swp2.model.Triangle;
import de.hft_stuttgart.swp2.model.Vertex;
import de.hft_stuttgart.swp2.opencl.CalculatorImpl;
import de.hft_stuttgart.swp2.opencl.CalculatorInterface;
import de.hft_stuttgart.swp2.opencl.OpenClException;
import de.hft_stuttgart.swp2.opencl.ShadowPrecision;
import de.hft_stuttgart.swp2.opencl.SunPositionCalculator;
import de.hft_stuttgart.swp2.opencl.VolumeTest;

public class ShadowViewer extends JFrame implements GLEventListener,
		KeyListener, MouseListener, MouseMotionListener {

	private static final long serialVersionUID = 6681486095144440340L;
	private static final int FPS = 60;
	
	private GLU glu;
	private Camera camera;
	private Robot robot;
	private int halfScreenHeight;
	private int halfScreenWidth;
	private float r = 10000;
	private boolean enableDrawCenters = false;
	
	private Calendar utcCal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
	private SunPositionCalculator[][] sunPositions;
	private SunPositionCalculator sunPos;
	private int hour = 6;
	private int month = 0;
	
	private int ray = 0;
	
	private static final boolean showGrid = true;


	public static void main(String[] args) throws OpenClException {
//		CL.setLogLevel(CL.LogLevel.LOG_DEBUGTRACE);
		try {
			final ShadowViewer view = new ShadowViewer();
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					view.setVisible(true);
				}
			});
		} catch (CLException e){
			e.printStackTrace();
			System.out.println(e.getStatus());
		}
	}

	public ShadowViewer() throws OpenClException {
		super("Shadow view");
		
		// test values
		VolumeTest.testCity2();
		VolumeTest.testCity2();
		VolumeTest.testCity2();
		City.getInstance().getBuildings().get(1).translate(0, 0, 5);
		City.getInstance().getBuildings().get(2).translate(5, 0, 5);
		City.getInstance().getBuildings().get(2).scale(1, 5, 1);
		
		int size = 20;
		Vertex v0 = new Vertex(-size, 0, -size);
		Vertex v1 = new Vertex(-size, 0, size);
		Vertex v2 = new Vertex(size, 0, size);
		Vertex v3 = new Vertex(size, 0, -size);
		
		Triangle t1 = new Triangle(v0, v1, v2);
		Triangle t2 = new Triangle(v0, v2, v3);
		
		Building b = new Building();
		b.addTriangle(t1);
		b.addTriangle(t2);
		City.getInstance().addBuilding(b);
	
		sunPositions = new SunPositionCalculator[12][24];
		for (int j = 1; j < 13; ++j) {
			for (int i = 0; i < sunPositions[j - 1].length; i++) {
				utcCal.set(2014, j, 1, i, 0, 0);
				sunPositions[j - 1][i] = new SunPositionCalculator(utcCal.getTime(), 11.6, 48.1);
			}
		}
		utcCal.set(2014, month + 1, 1, hour, 0, 0);
		sunPos = new SunPositionCalculator(utcCal.getTime(), 11.6, 48.1);
		
		CalculatorInterface calc = new CalculatorImpl();
		System.out.println("Starting shadow calculation...");
		long start = System.currentTimeMillis(); 
		calc.calculateShadow(ShadowPrecision.ULTRA); //VERY_LOW(5f), LOW(2.5f), MID(1.25f), HIGH(0.75f), ULTRA(0.375f), HYPER(0.1f), AWESOME(0.01f)
		long end = System.currentTimeMillis();
		System.out.printf("calculate shadow took %d milliseconds\n", (end - start));
		
		
		halfScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height / 2;
		halfScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width / 2;

		try {
			robot = new Robot();
		} catch (AWTException e) {
		}

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// set up the drawing canvas
		GLCanvas canvas = new GLCanvas();
		canvas.setPreferredSize(new Dimension(1024, 768));
		getContentPane().add(canvas);
		
		// draw the scene at FPS fps
		FPSAnimator animator = new FPSAnimator(canvas, FPS);
		animator.start();
		
		canvas.addGLEventListener(this);
		this.addKeyListener(this);
		canvas.addKeyListener(this);
		canvas.addMouseListener(this);
		canvas.addMouseMotionListener(this);
		pack();
		this.setLocationRelativeTo(null);
		robot.mouseMove(halfScreenWidth, halfScreenHeight);
	}

	@Override
	public void display(GLAutoDrawable drawable) {
		GL2 gl = drawable.getGL().getGL2();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		// apply camera modifications
		camera.lookAt();
		// drawing buildings
		gl.glColor3f(1f, 1f, 1f);
		for (Building b : City.getInstance().getBuildings()) {
			for (ShadowTriangle t : b.getShadowTriangles()) {
//				gl.glBegin(GL2.GL_LINE_LOOP);
				gl.glBegin(GL2.GL_TRIANGLES);
				gl.glColor3f(1, 0, 0);
				if (ray != -1 && !t.getShadowSet().get(ray)) {
					gl.glColor3f(0, 1, 0);
				}
				for (Vertex v : t.getVertices()) {
					gl.glVertex3fv(v.getCoordinates(), 0);
				}
				gl.glEnd();
				if(showGrid) {
					gl.glColor3f(0, 0, 0);
					gl.glBegin(GL2.GL_LINE_LOOP);
	//				gl.glBegin(GL2.GL_TRIANGLES);
					for (Vertex v : t.getVertices()) {
						gl.glVertex3fv(v.getCoordinates(), 0);
					}
					gl.glEnd();
				}
				gl.glColor3f(255f, 0, 255f);
				gl.glBegin(GL2.GL_LINE_LOOP);
				for (int i = 0; i < sunPositions[month].length; i++) {
					gl.glVertex3d(sunPositions[month][i].getX(), sunPositions[month][i].getY(), sunPositions[month][i].getZ());
				}
				gl.glEnd();
				
				gl.glBegin(GL2.GL_LINES);
{'fraction_non_alphanumeric': 0.13667748107478578, 'fraction_numerical': 0.029947591714499625, 'mean_word_length': 4.275120666959193, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5526045', 'n_tokens_mistral': 5045, 'n_tokens_neox': 4340, 'n_words': 855}