All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.sld.cgmes.dl.iidm.extensions.LineDiagramData Maven / Gradle / Ivy

There is a newer version: 4.5.1
Show newest version
/**
 * Copyright (c) 2019, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.powsybl.sld.cgmes.dl.iidm.extensions;

import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.iidm.network.*;
import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @author Massimo Ferraro {@literal }
 */
public class LineDiagramData> extends AbstractExtension {

    static final String NAME = "line-diagram-data";

    private Map> diagramsDetails = new HashMap<>();

    private LineDiagramData(T line) {
        super(line);
    }

    public LineDiagramData(Line line) {
        this((T) line);
    }

    public LineDiagramData(DanglingLine danglingLine) {
        this((T) danglingLine);
    }

    public LineDiagramData(HvdcLine hvdcLine) {
        this((T) hvdcLine);
    }

    @Override
    public String getName() {
        return NAME;
    }

    public void addPoint(String diagramName, DiagramPoint point) {
        Objects.requireNonNull(diagramName);
        Objects.requireNonNull(point);
        List points = diagramsDetails.getOrDefault(diagramName, new ArrayList<>());
        points.add(point);
        diagramsDetails.put(diagramName, points);
    }

    public List getPoints(String diagramName) {
        return diagramsDetails.getOrDefault(diagramName, Collections.emptyList()).stream().sorted().collect(Collectors.toList());
    }

    public DiagramPoint getFirstPoint(String diagramName) {
        return diagramsDetails.getOrDefault(diagramName, Collections.emptyList()).stream().sorted().findFirst().orElse(new DiagramPoint(0, 0, 0));
    }

    public DiagramPoint getLastPoint(String diagramName) {
        return diagramsDetails.getOrDefault(diagramName, Collections.emptyList()).stream().sorted(Comparator.reverseOrder()).findFirst().orElse(new DiagramPoint(0, 0, 0));
    }

    public DiagramPoint getFirstPoint(String diagramName, double offset) {
        List points = diagramsDetails.getOrDefault(diagramName, Collections.emptyList());
        if (points.size() < 2) {
            return getFirstPoint(diagramName);
        }
        DiagramPoint firstPoint = points.stream().sorted().findFirst().orElseThrow(AssertionError::new);
        DiagramPoint secondPoint = points.stream().sorted().skip(1).findFirst().orElseThrow(AssertionError::new);
        return shiftPoint(firstPoint, secondPoint, offset);
    }

    public DiagramPoint getLastPoint(String diagramName, double offset) {
        List points = diagramsDetails.getOrDefault(diagramName, Collections.emptyList());
        if (points.size() < 2) {
            return getLastPoint(diagramName);
        }
        DiagramPoint lastPoint = points.stream().sorted(Comparator.reverseOrder()).findFirst().orElseThrow(AssertionError::new);
        DiagramPoint secondLastPoint = points.stream().sorted(Comparator.reverseOrder()).skip(1).findFirst().orElseThrow(AssertionError::new);
        return shiftPoint(lastPoint, secondLastPoint, offset);
    }

    private DiagramPoint shiftPoint(DiagramPoint point, DiagramPoint otherPoint, double offset) {
        Vector2D pointVector = new Vector2D(point.getX(), point.getY());
        Vector2D otherPointVector = new Vector2D(otherPoint.getX(), otherPoint.getY());
        Vector2D shiftedPointVector = pointVector.add(otherPointVector.subtract(pointVector).normalize().scalarMultiply(offset));
        return new DiagramPoint(shiftedPointVector.getX(), shiftedPointVector.getY(), point.getSeq());
    }

    public List getDiagramsNames() {
        return new ArrayList<>(diagramsDetails.keySet());
    }

    public static LineDiagramData getOrCreateDiagramData(Line line) {
        LineDiagramData lineDiagramData = line.getExtension(LineDiagramData.class);
        if (lineDiagramData == null) {
            lineDiagramData = new LineDiagramData<>(line);
        }
        return lineDiagramData;
    }

    public static LineDiagramData getOrCreateDiagramData(DanglingLine danglingLine) {
        LineDiagramData danglingLineData = danglingLine.getExtension(LineDiagramData.class);
        if (danglingLineData == null) {
            danglingLineData = new LineDiagramData<>(danglingLine);
        }
        return danglingLineData;
    }

    public static LineDiagramData getOrCreateDiagramData(VscConverterStation vsc) {
        LineDiagramData vscLineData = vsc.getExtension(LineDiagramData.class);
        if (vscLineData == null) {
            vscLineData = new LineDiagramData<>(vsc);
        }
        return vscLineData;
    }

    public static LineDiagramData getOrCreateDiagramData(LccConverterStation lcc) {
        LineDiagramData lccLineData = lcc.getExtension(LineDiagramData.class);
        if (lccLineData == null) {
            lccLineData = new LineDiagramData<>(lcc);
        }
        return lccLineData;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy