Commit 4e0f7dd4 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

old reaction.getCenterLine method removed

parent 2da95dd8
Pipeline #10421 failed with stage
in 8 minutes and 45 seconds
package lcsb.mapviewer.converter.model.sbgnml;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -11,7 +10,8 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sbgn.ArcClazz;
import org.sbgn.GlyphClazz;
import org.sbgn.Language;
......@@ -30,6 +30,7 @@ import lcsb.mapviewer.common.comparator.DoubleComparator;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.converter.graphics.bioEntity.reaction.ReactionConverter;
import lcsb.mapviewer.model.graphics.ArrowType;
import lcsb.mapviewer.model.graphics.PolylineData;
import lcsb.mapviewer.model.map.compartment.Compartment;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.modifier.Catalysis;
......@@ -580,9 +581,9 @@ public class SbgnmlXmlExporter {
processGlyph.setClazz(getGlyphClazzFromReaction(reaction).getClazz());
Bbox bbox = new Bbox();
Line2D line = reaction.getCenterLine();
Point2D startPoint = line.getP1();
Point2D endPoint = line.getP2();
PolylineData line = reaction.getLine();
Point2D startPoint = line.getBeginPoint();
Point2D endPoint = line.getEndPoint();
double pointX = (startPoint.getX() + endPoint.getX()) / 2;
double pointY = (startPoint.getY() + endPoint.getY()) / 2;
......
......@@ -562,7 +562,7 @@ public class SbgnmlXmlParser {
productPointList.add(2, newPoint);
}
}
int reactantPointEnds = (productPointList.size()-1) / 2;
int reactantPointEnds = (productPointList.size() - 1) / 2;
int productPointStarts = reactantPointEnds + 1;
Reactant reactant = new Reactant(model.getElementByElementId(source.getId()));
......@@ -949,7 +949,7 @@ public class SbgnmlXmlParser {
ModifierTypeUtils utils = new ModifierTypeUtils();
Point2D result;
Line2D centerLine = reaction.getCenterLine();
Line2D centerLine = reaction.getLine().getLines().get(reaction.getLine().getLines().size() / 2);
double dx = centerLine.getX2() - centerLine.getX1();
double dy = centerLine.getY2() - centerLine.getY1();
double centerLineAngle = Math.atan2(dy, dx);
......@@ -1315,7 +1315,7 @@ public class SbgnmlXmlParser {
} else {
reaction.getProducts().get(0).getLine().trimBegin(ReactionCellDesignerConverter.RECT_SIZE / 2 - 1);
}
PolylineData centerLine = new PolylineData(pt.copyPoint(centerPointStart), pt.copyPoint(centerPointEnd));
reaction.setLine(centerLine);
......
......@@ -6,11 +6,11 @@ import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lcsb.mapviewer.commands.ColorExtractor;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.geometry.EllipseTransformation;
import lcsb.mapviewer.common.geometry.LineTransformation;
import lcsb.mapviewer.converter.graphics.ConverterParams;
import lcsb.mapviewer.converter.graphics.DrawingException;
......
......@@ -12,11 +12,11 @@ import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lcsb.mapviewer.commands.ColorExtractor;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.geometry.EllipseTransformation;
import lcsb.mapviewer.common.geometry.LineTransformation;
import lcsb.mapviewer.converter.graphics.ConverterParams;
import lcsb.mapviewer.converter.graphics.DrawingException;
......
......@@ -11,7 +11,8 @@ import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lcsb.mapviewer.commands.ColorExtractor;
import lcsb.mapviewer.common.Pair;
......@@ -42,412 +43,439 @@ import lcsb.mapviewer.model.map.reaction.type.ReactionRect;
*/
public class ReactionConverter extends BioEntityConverter<Reaction> {
/**
* When drawing operator this value defines radius of the joining operator
* circle.
*/
private static final int DEFAULT_OPERATOR_RADIUS = 6;
/**
* Size of the rectangle drawn on the central line of the reaction.
*/
public static final double RECT_SIZE = 10;
/**
* Default font size of reaction description.
*/
public static final int DESCRIPTION_FONT_SIZE = 10;
/**
* Radius multiplier for bigger logic operators circles in SBGN view.
*/
public static final double SBGN_RADIUS_MULTIPLIER = 2.5;
/**
* {@link ColorSchema} used for coloring reactions where we have more than one
* layout.
*/
public static final ColorSchema DEFAULT_COLOR_SCHEMA = new GenericColorSchema();
/**
* Line width of #DEFAULT_COLOR_SCHEMA used for coloring reactions where we
* have more than one layout.
*/
private static final double DEFAULT_COLOR_SCHEMA_LINE_WIDTH = 3.0;
static {
DEFAULT_COLOR_SCHEMA.setColor(Color.BLACK);
DEFAULT_COLOR_SCHEMA.setLineWidth(DEFAULT_COLOR_SCHEMA_LINE_WIDTH);
}
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private static Logger logger = LogManager.getLogger(ReactionConverter.class.getName());
/**
* Font used for drawing reaction description on the map.
*/
private Font descFont = null;
/**
* Graphical helper object with line transformation functions.
*/
private LineTransformation lineTransformation = new LineTransformation();
/**
* This objects helps drawing arrows.
*/
private ArrowTransformation arrowTransformation = new ArrowTransformation();
/**
* Object used to perform transformations on point objects.
*/
private PointTransformation pointTransformation = new PointTransformation();
/**
* Object that helps to convert {@link ColorSchema} values into colors.
*/
private ColorExtractor colorExtractor;
/**
* Default constructor.
*
* @param colorExtractor
* Object that helps to convert {@link ColorSchema} values into
* colors when drawing {@link Reaction}
*/
public ReactionConverter(ColorExtractor colorExtractor) {
super();
descFont = new Font(Font.SANS_SERIF, Font.BOLD, DESCRIPTION_FONT_SIZE);
this.colorExtractor = colorExtractor;
}
/**
* This method draws a rectangle in the center of reaction (defined by line).
*
* @param line
* line on which the rectangle is drawn
* @param insideType
* type of the rectangle to be drawn
* @param graphics
* where the rectangle should be drawn
*/
protected void drawRectangleData(final Line2D line, final ReactionRect insideType, final Graphics2D graphics) {
Point2D startPoint = line.getP1();
Point2D endPoint = line.getP2();
double pointX = (startPoint.getX() + endPoint.getX()) / 2;
double pointY = (startPoint.getY() + endPoint.getY()) / 2;
Point2D centerPoint = new Point2D.Double(pointX, pointY);
double dx = endPoint.getX() - startPoint.getX();
double dy = endPoint.getY() - startPoint.getY();
double angle = Math.atan2(dy, dx);
if (insideType == null) {
Point2D topPoint = new Point2D.Double(pointX + RECT_SIZE / 2, pointY);
topPoint = new PointTransformation().rotatePoint(topPoint , angle, centerPoint);
Point2D bottomPoint = new Point2D.Double(pointX - RECT_SIZE / 2, pointY);
bottomPoint = new PointTransformation().rotatePoint(bottomPoint , angle, centerPoint);
graphics.draw(new Line2D.Double(topPoint, bottomPoint));
} else {
// find rectangle
Rectangle2D rect = new Rectangle2D.Double();
rect.setRect(pointX - RECT_SIZE / 2, pointY - RECT_SIZE / 2, RECT_SIZE, RECT_SIZE);
// rotate graphics by the angle defined by line (instead of rotating
// rectangle)
graphics.rotate(angle, pointX, pointY);
// fill rect
Color color = graphics.getColor();
graphics.setColor(Color.white);
graphics.fill(rect);
graphics.setColor(color);
// draw rect border
graphics.draw(rect);
// unrotate the graphics
graphics.rotate(-angle, pointX, pointY);
// draw text inside rect
Font tmpFont = graphics.getFont();
graphics.setFont(descFont);
String insideDesc = insideType.getText();
double textWidth = graphics.getFontMetrics().stringWidth(insideDesc);
double textHeight = graphics.getFontMetrics().getAscent() - 2;
graphics.drawString(insideDesc, (int) (pointX - textWidth / 2), (int) (pointY + textHeight / 2));
graphics.setFont(tmpFont);
// if we should put bolt character inside then do it
if (insideType == ReactionRect.RECT_BOLT) {
GeneralPath path = new GeneralPath();
// CHECKSTYLE:OFF
path.moveTo(pointX + 2, pointY - RECT_SIZE / 2 + 1);
path.lineTo(pointX + 2, pointY + RECT_SIZE / 2 - 3);
path.lineTo(pointX - 2, pointY - RECT_SIZE / 2 + 3);
path.lineTo(pointX - 2, pointY + RECT_SIZE / 2 - 1);
// CHECKSTYLE:ON
graphics.draw(path);
}
}
}
@Override
protected void drawImpl(final Reaction reaction, final Graphics2D graphics, final ConverterParams params) {
Color color = graphics.getColor();
graphics.setColor(reaction.getReactants().get(0).getLine().getColor());
// first reactants
for (Reactant reactant : reaction.getReactants()) {
if (isVisible(reactant, params)) {
drawReactant(graphics, reactant);
}
}
// now products
for (Product product : reaction.getProducts()) {
if (isVisible(product, params)) {
drawProduct(graphics, product);
}
}
// draw modifiers
for (Modifier modifier : reaction.getModifiers()) {
if (isVisible(modifier, params)) {
drawModifier(graphics, modifier);
}
}
// and operators
for (NodeOperator operator : reaction.getOperators()) {
if (isVisible(operator, params)) {
drawOperator(graphics, operator, params.isSbgnFormat());
}
}
// in the end we draw rectangle in the middle
drawRectangleData(reaction.getCenterLine(), reaction.getReactionRect(), graphics);
graphics.setColor(color);
}
@Override
public void draw(final Reaction reaction, final Graphics2D graphics, final ConverterParams params, List<ColorSchema> visualizedLayoutsColorSchemas) throws DrawingException {
if (visualizedLayoutsColorSchemas.size() == 0) {
drawImpl(reaction, graphics, params);
} else if (visualizedLayoutsColorSchemas.size() == 1) {
if (visualizedLayoutsColorSchemas.get(0) == null) {
drawImpl(reaction, graphics, params);
} else {
List<Pair<AbstractNode, PolylineData>> oldData = new ArrayList<>();
for (AbstractNode node : reaction.getNodes()) {
oldData.add(new Pair<AbstractNode, PolylineData>(node, node.getLine()));
}
applyColorSchema(reaction, visualizedLayoutsColorSchemas.get(0));
drawImpl(reaction, graphics, params);
for (Pair<AbstractNode, PolylineData> pair : oldData) {
pair.getLeft().setLine(pair.getRight());
}
}
} else {
int count = 0;
for (ColorSchema schema : visualizedLayoutsColorSchemas) {
if (schema != null) {
count++;
}
}
if (count == 0) {
drawImpl(reaction, graphics, params);
} else {
List<Pair<AbstractNode, PolylineData>> oldData = new ArrayList<>();
for (AbstractNode node : reaction.getNodes()) {
oldData.add(new Pair<AbstractNode, PolylineData>(node, node.getLine()));
}
applyColorSchema(reaction, DEFAULT_COLOR_SCHEMA);
drawImpl(reaction, graphics, params);
for (Pair<AbstractNode, PolylineData> pair : oldData) {
pair.getLeft().setLine(pair.getRight());
}
}
}
}
/**
* Modify reaction with data from {@link ColorSchema}.
*
* @param reaction
* reaction to modify
* @param colorSchema
* {@link ColorSchema} to modify reaction
*/
private void applyColorSchema(Reaction reaction, ColorSchema colorSchema) {
for (AbstractNode node : reaction.getNodes()) {
PolylineData pd = new PolylineData(node.getLine());
pd.setColor(colorExtractor.getNormalizedColor(colorSchema));
if (colorSchema.getLineWidth() != null) {
pd.setWidth(colorSchema.getLineWidth());
}
if (colorSchema.getReverseReaction() != null && colorSchema.getReverseReaction()) {
ArrowTypeData atd = pd.getBeginAtd();
pd.setBeginAtd(pd.getEndAtd());
pd.setEndAtd(atd);
}
node.setLine(pd);
}
}
/**
* Draw modifier on the graphics2d.
*
* @param graphics
* where we want to draw the object
* @param modifier
* object to be drawn
*/
private void drawModifier(final Graphics2D graphics, final Modifier modifier) {
// modifier consists only from the arrow
arrowTransformation.drawLine(modifier.getLine(), graphics);
}
/**
* Draw operator on the graphics2d.
*
* @param graphics
* where we want to draw the object
* @param operator
* object to be drawn
* @param sbgnFormat
* true if operator is to be drawn in SBGN format
*/
private void drawOperator(final Graphics2D graphics, final NodeOperator operator, boolean sbgnFormat) {
// draw line
arrowTransformation.drawLine(operator.getLine(), graphics);
// in SBGN view - draw connecting point only if it's not connected to
// reactants or products
if (!sbgnFormat || operator.getLine().getEndAtd().getArrowType() != ArrowType.NONE
|| (!operator.getOutputs().isEmpty() && operator.getOutputs().get(0) instanceof NodeOperator)) {
// and now connecting point
Point2D centerPoint = operator.getLine().getPoints().get(0);
int radius = DEFAULT_OPERATOR_RADIUS;
// bigger connecting point circles in SBGN view
if (sbgnFormat) {
radius *= SBGN_RADIUS_MULTIPLIER;
}
// it's a circle
Ellipse2D cir = new Ellipse2D.Double(centerPoint.getX() - radius, centerPoint.getY() - radius, 2 * radius, 2 * radius);
Color color = graphics.getColor();
graphics.setColor(Color.white);
graphics.fill(cir);
graphics.setColor(operator.getLine().getColor());
graphics.draw(cir);
// and text defined by operator
String text;
if (!sbgnFormat) {
text = operator.getOperatorText();
} else {
text = operator.getSBGNOperatorText();
}
Rectangle2D rect = graphics.getFontMetrics().getStringBounds(text, graphics);
graphics.drawString(text, (int) (centerPoint.getX() - rect.getWidth() / 2 + 1), (int) (centerPoint.getY() + rect.getHeight() / 2) - 2);
graphics.setColor(color);
}
}
/**
* Draw product on the graphics2D.
*
* @param graphics
* where we want to draw the object
* @param product
* object to be drawn
*/
private void drawProduct(final Graphics2D graphics, final Product product) {
arrowTransformation.drawLine(product.getLine(), graphics);
}
/**
* Draw reactant on the graphics2D.
*
* @param graphics
* where the reactant should be drawn
* @param reactant
* object to be drawn on the graphics
*/
private void drawReactant(final Graphics2D graphics, final Reactant reactant) {
arrowTransformation.drawLine(reactant.getLine(), graphics);
}
/**
*
* @return {@link #descFont}
*/
protected Font getDescFont() {
return descFont;
}
/**
*
* @param descFont
* new {@link #descFont} value
*/
protected void setDescFont(final Font descFont) {
this.descFont = descFont;
}
/**
*
* @return {@link #lineTransformation}
*/
protected LineTransformation getLineTransformation() {
return lineTransformation;
}
/**
*
* @param lineTransformation
* new {@link #lineTransformation}
*/
protected void setLineTransformation(final LineTransformation lineTransformation) {
this.lineTransformation = lineTransformation;
}
/**
*
* @return {@link #arrowTransformation}
*/
protected ArrowTransformation getArrowTransformation() {
return arrowTransformation;
}
/**
*
* @param arrowTransformation
* new {@link #arrowTransformation}
*/
protected void setArrowTransformation(final ArrowTransformation arrowTransformation) {
this.arrowTransformation = arrowTransformation;
}
/**
*
* @return {@link #pointTransformation}
*/
protected PointTransformation getPointTransformation() {
return pointTransformation;
}
/**
*
* @param pointTransformation
* new {@link #pointTransformation}
*/
protected void setPointTransformation(final PointTransformation pointTransformation) {
this.pointTransformation = pointTransformation;
}
@Override
public void drawText(Reaction bioEntity, Graphics2D graphics, ConverterParams params) throws DrawingException {
}
/**
* When drawing operator this value defines radius of the joining operator
* circle.
*/
private static final int DEFAULT_OPERATOR_RADIUS = 6;
/**
* Size of the rectangle drawn on the central line of the reaction.
*/
public static final double RECT_SIZE = 10;
/**
* Default font size of reaction description.
*/
public static final int DESCRIPTION_FONT_SIZE = 10;
/**
* Radius multiplier for bigger logic operators circles in SBGN view.
*/
public static final double SBGN_RADIUS_MULTIPLIER = 2.5;
/**
* {@link ColorSchema} used for coloring reactions where we have more than one
* layout.
*/
public static final ColorSchema DEFAULT_COLOR_SCHEMA = new GenericColorSchema();
/**
* Line width of #DEFAULT_COLOR_SCHEMA used for coloring reactions where we have
* more than one layout.
*/
private static final double DEFAULT_COLOR_SCHEMA_LINE_WIDTH = 3.0;
static {
DEFAULT_COLOR_SCHEMA.setColor(Color.BLACK);
DEFAULT_COLOR_SCHEMA.setLineWidth(DEFAULT_COLOR_SCHEMA_LINE_WIDTH);
}
/**