<commit_before>package org.helioviewer.jhv.plugins.swhvhekplugin;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.swing.ImageIcon;

import org.helioviewer.base.astronomy.Position;
import org.helioviewer.base.astronomy.Sun;
import org.helioviewer.base.math.GL3DMat4d;
import org.helioviewer.base.math.GL3DVec3d;
import org.helioviewer.jhv.camera.GL3DViewport;
import org.helioviewer.jhv.data.datatype.event.JHVCoordinateSystem;
import org.helioviewer.jhv.data.datatype.event.JHVEvent;
import org.helioviewer.jhv.data.datatype.event.JHVEventParameter;
import org.helioviewer.jhv.data.datatype.event.JHVPoint;
import org.helioviewer.jhv.data.datatype.event.JHVPositionInformation;
import org.helioviewer.jhv.gui.ImageViewerGui;
import org.helioviewer.jhv.layers.Layers;
import org.helioviewer.jhv.opengl.GLHelper;
import org.helioviewer.jhv.opengl.GLTexture;
import org.helioviewer.jhv.renderable.gui.AbstractRenderable;

import com.jogamp.opengl.GL2;

public class SWHVHEKPluginRenderable extends AbstractRenderable {

    private static final SWHVHEKImagePanelEventPopupController controller = new SWHVHEKImagePanelEventPopupController();

    private static final double LINEWIDTH = 0.5;
    private static final double LINEWIDTH_CACTUS = 1.01;

    private static final double LINEWIDTH_HI = 1;

    private static HashMap<String, GLTexture> iconCacheId = new HashMap<String, GLTexture>();

    private void bindTexture(GL2 gl, String key, ImageIcon icon) {
        GLTexture tex = iconCacheId.get(key);
        if (tex == null) {
            BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics graph = bi.createGraphics();
            icon.paintIcon(null, graph, 0, 0);
            graph.dispose();

            tex = new GLTexture(gl);
            tex.bind(gl, GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE0);
            tex.copyBufferedImage2D(gl, bi);
            iconCacheId.put(key, tex);
        }

        tex.bind(gl, GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE0);
    }

    private void interPolatedDraw(GL2 gl, double mres, double r_start, double r_end, double t_start, double t_end, double phi, double thetaDelta) {
        gl.glBegin(GL2.GL_LINE_STRIP);
        for (int i = 0; i <= mres; i++) {
            double alpha = 1. - i / mres;
            double r = alpha * r_start + (1 - alpha) * (r_end);
            double theta = alpha * t_start + (1 - alpha) * (t_end);

            double x = r * Math.cos(theta) * Math.sin(phi);
            double z = r * Math.cos(theta) * Math.cos(phi);
            double y = r * Math.sin(theta);
            double yrot = y * Math.cos(thetaDelta) + z * Math.sin(thetaDelta);
            double zrot = -y * Math.sin(thetaDelta) + z * Math.cos(thetaDelta);
            double xrot = x;

            gl.glVertex3f((float) xrot, (float) yrot, (float) zrot);
        }
        gl.glEnd();
    }

    private void drawCactusArc(GL2 gl, JHVEvent evt, Date now) {
        Collection<JHVEventParameter> params = evt.getAllEventParameters().values();
        double principleAngle = 0;
        double angularWidth = 0;
        double distSun = 2.4;
        double speed = 500;

        for (JHVEventParameter param : params) {
            String name = param.getParameterName();
            String value = param.getParameterValue();

            if (name.equals("cme_angularwidth")) {
                angularWidth = Double.parseDouble(value) * Math.PI / 180.;
            }
            if (name.equals("event_coord1")) {
                principleAngle = Math.PI / 2. - Double.parseDouble(value) * Math.PI / 180.;
            }
            if (name.equals("cme_radiallinvel")) {
                speed = Double.parseDouble(value);
            }
        }
        double factor = (Sun.RadiusMeter / 1000) * (1000);
        double distSunBegin = distSun;
        distSun += speed * (Layers.getLastUpdatedTimestamp().getTime() - evt.getStartDate().getTime()) / factor;
        int arcResolution = 50;
        int lineResolution = 2;

        Date date = new Date((evt.getStartDate().getTime() + evt.getEndDate().getTime()) / 2);
        Position.Latitudinal p = Sun.getEarth(date);

        double thetaDelta = -p.lat;
        double thetaStart = principleAngle - angularWidth / 2.;
        double thetaEnd = principleAngle + angularWidth / 2.;

        double phi = -Math.PI / 2. - p.lon;

        Color color = evt.getEventRelationShip().getRelationshipColor();
        if (color == null) {
            color = evt.getColor();
        }

        gl.glColor3f(0f, 0f, 0f);
        GLHelper.lineWidth(gl, LINEWIDTH_CACTUS * 1.2);

        interPolatedDraw(gl, arcResolution, distSun, distSun, thetaStart, principleAngle, phi, thetaDelta);
        interPolatedDraw(gl, arcResolution, distSun, distSun, principleAngle, thetaEnd, phi, thetaDelta);

        gl.glColor3f(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f);
        GLHelper.lineWidth(gl, LINEWIDTH_CACTUS);

        interPolatedDraw(gl, arcResolution, distSun, distSun, thetaStart, principleAngle, phi, thetaDelta);
        interPolatedDraw(gl, arcResolution, distSun, distSun, principleAngle, thetaEnd, phi, thetaDelta);

        interPolatedDraw(gl, lineResolution, distSunBegin, distSun + 0.05, thetaStart, thetaStart, phi, thetaDelta);
        interPolatedDraw(gl, lineResolution, distSunBegin, distSun + 0.05, principleAngle, principleAngle, phi, thetaDelta);
        interPolatedDraw(gl, lineResolution, distSunBegin, distSun + 0.05, thetaEnd, thetaEnd, phi, thetaDelta);

        double r, theta;
        double x, y, z;
        double xrot, yrot, zrot;
        String type = evt.getJHVEventType().getEventType();
        bindTexture(gl, type, evt.getIcon());
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);

        double sz = 0.1;
        if (evt.isHighlighted()) {
            sz = 0.16;
        }
        gl.glColor3f(1, 1, 1);
        gl.glEnable(GL2.GL_CULL_FACE);
        gl.glEnable(GL2.GL_TEXTURE_2D);
        gl.glBegin(GL2.GL_QUADS);
        {
            double deltatheta = sz / distSun;
            r = distSun - sz;
            theta = principleAngle - deltatheta;
            x = r * Math.cos(theta) * Math.sin(phi);
            z = r * Math.cos(theta) * Math.cos(phi);
            y = r * Math.sin(theta);
            yrot = y * Math.cos(thetaDelta) + z * Math.sin(thetaDelta);
            zrot = -y * Math.sin(thetaDelta) + z * Math.cos(thetaDelta);
            xrot = x;
            gl.glTexCoord2f(0f, 0f);
            gl.glVertex3f((float) xrot, (float) yrot, (float) zrot);
            r = distSun - sz;
            theta = principleAngle + deltatheta;
            x = r * Math.cos(theta) * Math.sin(phi);
            z = r * Math.cos(theta) * Math.cos(phi);
            y = r * Math.sin(theta);
            yrot = y * Math.cos(thetaDelta) + z * Math.sin(thetaDelta);
            zrot = -y * Math.sin(thetaDelta) + z * Math.cos(thetaDelta);
            xrot = x;
            gl.glTexCoord2f(0f, 1f);
            gl.glVertex3f((float) xrot, (float) yrot, (float) zrot);
            r = distSun + sz;
            theta = principleAngle + deltatheta;
            x = r * Math.cos(theta) * Math.sin(phi);
            z = r * Math.cos(theta) * Math.cos(phi);
            y = r * Math.sin(theta);
            yrot = y * Math.cos(thetaDelta) + z * Math.sin(thetaDelta);
            zrot = -y * Math.sin(thetaDelta) + z * Math.cos(thetaDelta);
            xrot = x;
            gl.glTexCoord2f(1f, 1f);
            gl.glVertex3f((float) xrot, (float) yrot, (float) zrot);
            r = distSun + sz;
            theta = principleAngle - deltatheta;
            x = r * Math.cos(theta) * Math.sin(phi);
            z = r * Math.cos(theta) * Math.cos(phi);
            y = r * Math.sin(theta);
            yrot = y * Math.cos(thetaDelta) + z * Math.sin(thetaDelta);
            zrot = -y * Math.sin(thetaDelta) + z * Math.cos(thetaDelta);
            xrot = x;
            gl.glTexCoord2f(1f, 0f);
            gl.glVertex3f((float) xrot, (float) yrot, (float) zrot);
        }
        gl.glEnd();
        gl.glDisable(GL2.GL_TEXTURE_2D);
        gl.glDisable(GL2.GL_CULL_FACE);
    }

    private void drawPolygon(GL2 gl, JHVEvent evt, Date now) {
        HashMap<JHVCoordinateSystem, JHVPositionInformation> pi = evt.getPositioningInformation();

        if (!pi.containsKey(JHVCoordinateSystem.JHV)) {
            return;
        }
<commit_msg>reduce<commit_after>package org.helioviewer.jhv.plugins.swhvhekplugin;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.swing.ImageIcon;

import org.helioviewer.base.astronomy.Position;
import org.helioviewer.base.astronomy.Sun;
import org.helioviewer.base.math.GL3DMat4d;
import org.helioviewer.base.math.GL3DVec2d;
import org.helioviewer.base.math.GL3DVec3d;
import org.helioviewer.jhv.camera.GL3DViewport;
import org.helioviewer.jhv.data.datatype.event.JHVCoordinateSystem;
import org.helioviewer.jhv.data.datatype.event.JHVEvent;
import org.helioviewer.jhv.data.datatype.event.JHVEventParameter;
import org.helioviewer.jhv.data.datatype.event.JHVPoint;
import org.helioviewer.jhv.data.datatype.event.JHVPositionInformation;
import org.helioviewer.jhv.gui.ImageViewerGui;
import org.helioviewer.jhv.layers.Layers;
import org.helioviewer.jhv.opengl.GLHelper;
import org.helioviewer.jhv.opengl.GLTexture;
import org.helioviewer.jhv.renderable.gui.AbstractRenderable;

import com.jogamp.opengl.GL2;

public class SWHVHEKPluginRenderable extends AbstractRenderable {

    private static final SWHVHEKImagePanelEventPopupController controller = new SWHVHEKImagePanelEventPopupController();

    private static final double LINEWIDTH = 0.5;
    private static final double LINEWIDTH_CACTUS = 1.01;

    private static final double LINEWIDTH_HI = 1;

    private static HashMap<String, GLTexture> iconCacheId = new HashMap<String, GLTexture>();

    private void bindTexture(GL2 gl, String key, ImageIcon icon) {
        GLTexture tex = iconCacheId.get(key);
        if (tex == null) {
            BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics graph = bi.createGraphics();
            icon.paintIcon(null, graph, 0, 0);
            graph.dispose();

            tex = new GLTexture(gl);
            tex.bind(gl, GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE0);
            tex.copyBufferedImage2D(gl, bi);
            iconCacheId.put(key, tex);
        }

        tex.bind(gl, GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE0);
    }

    private void interPolatedDraw(GL2 gl, double mres, double r_start, double r_end, double t_start, double t_end, double phi, double thetaDelta) {
        gl.glBegin(GL2.GL_LINE_STRIP);
        for (int i = 0; i <= mres; i++) {
            double alpha = 1. - i / mres;
            double r = alpha * r_start + (1 - alpha) * (r_end);
            double theta = alpha * t_start + (1 - alpha) * (t_end);

            double x = r * Math.cos(theta) * Math.sin(phi);
            double z = r * Math.cos(theta) * Math.cos(phi);
            double y = r * Math.sin(theta);
            double yrot = y * Math.cos(thetaDelta) + z * Math.sin(thetaDelta);
            double zrot = -y * Math.sin(thetaDelta) + z * Math.cos(thetaDelta);
            double xrot = x;

            gl.glVertex3f((float) xrot, (float) yrot, (float) zrot);
        }
        gl.glEnd();
    }

    private final GL3DVec2d[] texcoords = { new GL3DVec2d(0, 0), new GL3DVec2d(0, 1), new GL3DVec2d(1, 1), new GL3DVec2d(1, 0) };
    private final int texCoordHelpers[][] = { { 0, 0 }, { 0, 1 }, { 1, 1 }, { 1, 0 } };;

    private void drawCactusArc(GL2 gl, JHVEvent evt, Date now) {
        Collection<JHVEventParameter> params = evt.getAllEventParameters().values();
        double principleAngle = 0;
        double angularWidth = 0;
        double distSun = 2.4;
        double speed = 500;

        for (JHVEventParameter param : params) {
            String name = param.getParameterName();
            String value = param.getParameterValue();

            if (name.equals("cme_angularwidth")) {
                angularWidth = Double.parseDouble(value) * Math.PI / 180.;
            }
            if (name.equals("event_coord1")) {
                principleAngle = Math.PI / 2. - Double.parseDouble(value) * Math.PI / 180.;
            }
            if (name.equals("cme_radiallinvel")) {
                speed = Double.parseDouble(value);
            }
        }
        double factor = (Sun.RadiusMeter / 1000) * (1000);
        double distSunBegin = distSun;
        distSun += speed * (Layers.getLastUpdatedTimestamp().getTime() - evt.getStartDate().getTime()) / factor;
        int arcResolution = 50;
        int lineResolution = 2;

        Date date = new Date((evt.getStartDate().getTime() + evt.getEndDate().getTime()) / 2);
        Position.Latitudinal p = Sun.getEarth(date);

        double thetaDelta = -p.lat;
        double thetaStart = principleAngle - angularWidth / 2.;
        double thetaEnd = principleAngle + angularWidth / 2.;

        double phi = -Math.PI / 2. - p.lon;

        Color color = evt.getEventRelationShip().getRelationshipColor();
        if (color == null) {
            color = evt.getColor();
        }

        gl.glColor3f(0f, 0f, 0f);
        GLHelper.lineWidth(gl, LINEWIDTH_CACTUS * 1.2);

        interPolatedDraw(gl, arcResolution, distSun, distSun, thetaStart, principleAngle, phi, thetaDelta);
        interPolatedDraw(gl, arcResolution, distSun, distSun, principleAngle, thetaEnd, phi, thetaDelta);

        gl.glColor3f(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f);
        GLHelper.lineWidth(gl, LINEWIDTH_CACTUS);

        interPolatedDraw(gl, arcResolution, distSun, distSun, thetaStart, principleAngle, phi, thetaDelta);
        interPolatedDraw(gl, arcResolution, distSun, distSun, principleAngle, thetaEnd, phi, thetaDelta);

        interPolatedDraw(gl, lineResolution, distSunBegin, distSun + 0.05, thetaStart, thetaStart, phi, thetaDelta);
        interPolatedDraw(gl, lineResolution, distSunBegin, distSun + 0.05, principleAngle, principleAngle, phi, thetaDelta);
        interPolatedDraw(gl, lineResolution, distSunBegin, distSun + 0.05, thetaEnd, thetaEnd, phi, thetaDelta);

        double r, theta;
        double x, y, z;
        double xrot, yrot, zrot;
        String type = evt.getJHVEventType().getEventType();
        bindTexture(gl, type, evt.getIcon());
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);

        double sz = 0.1;
        if (evt.isHighlighted()) {
            sz = 0.16;
        }
        gl.glColor3f(1, 1, 1);
        gl.glEnable(GL2.GL_CULL_FACE);
        gl.glEnable(GL2.GL_TEXTURE_2D);
        gl.glBegin(GL2.GL_QUADS);
        {
            for (int i = 0; i < texCoordHelpers.length; i++) {
                int[] el = texCoordHelpers[i];
                double deltatheta = sz / distSun * (el[1] * 2 - 1);
                double deltar = sz * (el[0] * 2 - 1);
                r = distSun + deltar;
                theta = principleAngle + deltatheta;
                x = r * Math.cos(theta) * Math.sin(phi);
                z = r * Math.cos(theta) * Math.cos(phi);
                y = r * Math.sin(theta);
                yrot = y * Math.cos(thetaDelta) + z * Math.sin(thetaDelta);
                zrot = -y * Math.sin(thetaDelta) + z * Math.cos(thetaDelta);
                xrot = x;
                gl.glTexCoord2f(el[0], el[1]);
                gl.glVertex3f((float) xrot, (float) yrot, (float) zrot);
            }
        }
        gl.glEnd();
        gl.glDisable(GL2.GL_TEXTURE_2D);
        gl.glDisable(GL2.GL_CULL_FACE);
    }

    private void drawPolygon(GL2 gl, JHVEvent evt, Date now) {
        HashMap<JHVCoordinateSystem, JHVPositionInformation> pi = evt.getPositioningInformation();

        if (!pi.containsKey(JHVCoordinateSystem.JHV)) {
            return;
        }

{'fraction_non_alphanumeric': 0.12090388597880375, 'fraction_numerical': 0.01650627360214399, 'mean_word_length': 2.494891443167305, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 11, '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': '1095295', 'n_tokens_mistral': 5690, 'n_tokens_neox': 5380, 'n_words': 1128}