Commit bacb3859 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

end point for additional reactants is computed based on the new data model

parent 6b35d387
package lcsb.mapviewer.converter.model.celldesigner.reaction;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;
......@@ -11,7 +10,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
......@@ -1157,6 +1157,11 @@ public class ReactionFromXml {
reactant.setLine(reactantLine);
product.setLine(productLine);
PolylineData reactionLine = new PolylineData();
reactionLine.addPoint(reactantLine.getEndPoint());
reactionLine.addPoint(productLine.getBeginPoint());
reaction.setLine(reactionLine);
ReactionLineData rld = ReactionLineData.getByReactionType(reaction.getClass());
reactantLine.setType(rld.getLineType());
productLine.setType(rld.getLineType());
......@@ -1310,14 +1315,7 @@ public class ReactionFromXml {
ArrowTypeData atd = new ArrowTypeData();
atd.setArrowType(ArrowType.NONE);
Point2D endPoint;
Line2D centerLine = reaction.getCenterLine();
double dx = centerLine.getX2() - centerLine.getX1();
double dy = centerLine.getY2() - centerLine.getY1();
double coef = REACTANT_END_RATIO;
endPoint = new Point2D.Double(centerLine.getX1() + dx * coef, centerLine.getY1() + dy * coef);
Point2D endPoint = getPointForAdditionalNode(reaction, REACTANT_END_RATIO);
PolylineData polyline = PolylineDataFactory.createPolylineDataFromEditPoints(additionalPoint, endPoint, points);
......@@ -1329,6 +1327,45 @@ public class ReactionFromXml {
return result;
}
/**
* Returns end point for additional reactants/products.
*
* @param reaction
* reaction for which we need to compute end point
* @param coef
* coefficient determining location of the reactant product end point.
* It should be one of hard-coded values: {@link #REACTANT_END_RATIO},
* {@link #PRODUCT_START_RATIO}.
* @return
*/
private Point2D getPointForAdditionalNode(Reaction reaction, double coef) {
Point2D endPoint;
// central line points
Point2D p1 = reaction.getReactants().get(0).getLine().getPoints()
.get(reaction.getReactants().get(0).getLine().getPoints().size() - 2);
Point2D p2 = reaction.getProducts().get(0).getLine().getPoints().get(1);
Set<AbstractNode> toExclude = new HashSet<AbstractNode>();
Set<AbstractNode> toInclude = new HashSet<AbstractNode>();
for (NodeOperator operator : reaction.getOperators()) {
toExclude.addAll(operator.getInputs());
if (operator.isReactantOperator()) {
toInclude.add(operator);
// if we have operator in input then central line changes
p1 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2);
}
if (operator.isProductOperator()) {
// if we have operator in output then central line changes
p2 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2);
}
}
double dx = p2.getX() - p1.getX();
double dy = p2.getY() - p1.getY();
endPoint = new Point2D.Double(p1.getX() + dx * coef, p1.getY() + dy * coef);
return endPoint;
}
/**
* Parses xml node with productLink nodes and creates products in the reaction.
*
......@@ -1413,32 +1450,7 @@ public class ReactionFromXml {
ArrowTypeData atd = new ArrowTypeData();
atd.setArrowType(ArrowType.NONE);
Point2D endPoint;
// central line points
Point2D p1 = reaction.getReactants().get(0).getLine().getPoints()
.get(reaction.getReactants().get(0).getLine().getPoints().size() - 2);
Point2D p2 = reaction.getProducts().get(0).getLine().getPoints().get(1);
Set<AbstractNode> toExclude = new HashSet<AbstractNode>();
Set<AbstractNode> toInclude = new HashSet<AbstractNode>();
for (NodeOperator operator : reaction.getOperators()) {
toExclude.addAll(operator.getInputs());
if (operator.isReactantOperator()) {
toInclude.add(operator);
// if we have operator in input then central line changes
p1 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2);
}
if (operator.isProductOperator()) {
// if we have operator in output then central line changes
p2 = operator.getLine().getPoints().get(operator.getLine().getPoints().size() - 2);
}
}
double dx = p2.getX() - p1.getX();
double dy = p2.getY() - p1.getY();
double coef = PRODUCT_START_RATIO;
endPoint = new Point2D.Double(p1.getX() + dx * coef, p1.getY() + dy * coef);
Point2D endPoint = getPointForAdditionalNode(reaction, PRODUCT_START_RATIO);
PolylineData polyline = PolylineDataFactory.createPolylineDataFromEditPoints(endPoint, additionalPoint, points);
additionalPoint = reactantConverter.getAnchorPointCoordinates(element, anchor, polyline.reverse());
......
......@@ -29,6 +29,7 @@ import lcsb.mapviewer.model.graphics.PolylineData;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelComparator;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.reaction.AbstractNode;
import lcsb.mapviewer.model.map.reaction.AndOperator;
import lcsb.mapviewer.model.map.reaction.AssociationOperator;
import lcsb.mapviewer.model.map.reaction.DissociationOperator;
......@@ -161,28 +162,6 @@ public class ReactionParserTests extends CellDesignerTestFunctions {
}
}
@Test
public void testCenterLineInSimpleReaction() throws Exception {
Model model;
try {
model = getModelForFile("testFiles/reactions/transition.xml");
assertEquals(1, model.getReactions().size());
Reaction reaction = model.getReactions().iterator().next();
Reactant reactant = reaction.getReactants().get(0);
Product product = reaction.getProducts().get(0);
// center of the line should be different than edge points of
// reactant/product description
assertFalse(reaction.getCenterLine().getP1().distance(reactant.getLine().getEndPoint()) < 1e-6);
assertFalse(reaction.getCenterLine().getP2().distance(product.getLine().getBeginPoint()) < 1e-6);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testTransition2() throws Exception {
Model model;
......@@ -1374,7 +1353,7 @@ public class ReactionParserTests extends CellDesignerTestFunctions {
List<Line2D> lines1 = reaction1.getLines();
List<Line2D> lines2 = reaction2.getLines();
for (int i = 0; i < lines1.size(); i++) {
assertEquals(lines1.get(i).getX1(), lines2.get(i).getX1(), 1e-6);
assertEquals(lines1.get(i).getX2(), lines2.get(i).getX2(), 1e-6);
......@@ -1383,7 +1362,7 @@ public class ReactionParserTests extends CellDesignerTestFunctions {
}
assertEquals(reaction1.getClass(), reaction2.getClass());
} catch (Exception e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
throw e;
}
}
......@@ -1824,7 +1803,7 @@ public class ReactionParserTests extends CellDesignerTestFunctions {
try {
model = getModelForFile("testFiles/problematic/reaction_drawing_problem.xml");
Reaction reaction = model.getReactionByReactionId("re1");
assertEquals(0, reaction.getCenterPoint().distance(new Point2D.Double(258.5, 145.0)), EPSILON);
assertNotNull(reaction.getLine());
} catch (Exception e) {
e.printStackTrace();
throw e;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment