@Override public Double getValue(final Position p) { final double[] cord = p.getCartesianCoordinates(); return (cord[0] * a) + (cord[1] * b) + c; }
/** * Initialize a gradient layer which grows in concentration proportionaly in space. * * @param direction the {@link Position} representing the direction in which the gradient grows (here the positions is considered as a vector) * @param unitVariation unit variation of the gradient * @param offset minimum value of concentration reached by this spatial distribution */ public BiomolGradientLayer(final Position direction, final double unitVariation, final double offset) { this(direction.getCoordinate(0), direction.getCoordinate(1), unitVariation, offset); }
private PointAdapter(final Position pos) { assert pos.getDimensions() == 2; this.pos = pos; x = pos.getCoordinate(0); y = pos.getCoordinate(1); }
private boolean selectNodes(final CellWithCircularArea node, final CellWithCircularArea nodeToMove, final Position origin, final Position requestedPos, final double xVer, final double yVer) { // testing if node is between requested position and original position final Position nodePos = getPosition(node); final Position nodeOrientationFromOrigin = new Continuous2DEuclidean(nodePos.getCoordinate(0) - origin.getCoordinate(0), nodePos.getCoordinate(1) - origin.getCoordinate(1)); final double scalarProductResult1 = xVer * nodeOrientationFromOrigin.getCoordinate(0) + yVer * nodeOrientationFromOrigin.getCoordinate(1); // testing if node is near enough to requested position to be an obstacle final Position oppositeVersor = new Continuous2DEuclidean(-xVer, -yVer); final Position nodeOrientationFromReq = new Continuous2DEuclidean(nodePos.getCoordinate(0) - requestedPos.getCoordinate(0), nodePos.getCoordinate(1) - requestedPos.getCoordinate(1)); final double scalarProductResult2 = oppositeVersor.getCoordinate(0) * nodeOrientationFromReq.getCoordinate(0) + oppositeVersor.getCoordinate(1) * nodeOrientationFromReq.getCoordinate(1); if (scalarProductResult2 <= 0) { return (nodePos.getDistanceTo(requestedPos) < node.getRadius() + nodeToMove.getRadius()) && scalarProductResult1 >= 0; } return scalarProductResult1 >= 0; }
final double yn = possibleObstaclePosition.getCoordinate(1); final double xn = possibleObstaclePosition.getCoordinate(0); final double cat = intersection.getDistanceTo(possibleObstaclePosition); final double distToSum = originalPos.getDistanceTo(intersection) - cat2; final Position versor = new Continuous2DEuclidean((xIntersect - xo) / module, (yIntersect - yo) / module); final Position vectorToSum = new Continuous2DEuclidean(distToSum * (versor.getCoordinate(0)), distToSum * (versor.getCoordinate(1))); final Position result = originalPos.add(vectorToSum); return Optional.of(result);
@Override public void execute() { final double[] nodePos = env.getPosition(getNode()).getCartesianCoordinates(); .map(n -> { final double[] nPos = env.getPosition(n).getCartesianCoordinates(); } else { for (final Position p : pushForces) { for (int i = 0; i < p.getDimensions(); i++) { resVersor[i] = resVersor[i] + p.getCoordinate(i);
@Override public void addPolarization(final Position v) { final double[] tempCor = this.polarizationVersor.add(v).getCartesianCoordinates(); final double module = FastMath.sqrt(FastMath.pow(tempCor[0], 2) + FastMath.pow(tempCor[1], 2)); this.polarizationVersor = module == 0 ? new Continuous2DEuclidean(0, 0) : new Continuous2DEuclidean(tempCor[0] / module, tempCor[1] / module); }
@Override public void zoomTo(final Position center, final double zoomLevel) { assert center.getDimensions() == 2; wormhole.zoomOnPoint(wormhole.getViewPoint(center), zoomLevel); }
@Override public Double load(final Position dest) { if (env instanceof MapEnvironment<?>) { return ((MapEnvironment<Object>) env).computeRoute(node, dest).length(); } return getDevicePosition().getDistanceTo(dest); } });
@Override public void moveNode(final Node<T> node, final Position direction) { final Position oldcoord = getPosition(node); moveNodeToPosition(node, oldcoord.add(direction)); }
@Override public Field nbrVector() { return buildFieldWithPosition(p -> getDevicePosition().subtract(p)); }
private Position findNearestFreePosition(final CellWithCircularArea nodeToMove, final Position originalPos, final Position requestedPos) { final double distanceToReq = originalPos.getDistanceTo(requestedPos); if (maxDiameter == 0d || distanceToReq == 0) { return requestedPos; final double rx = requestedPos.getCoordinate(0); final double ox = originalPos.getCoordinate(0); final double xVec = rx - ox; final double ry = requestedPos.getCoordinate(1); final double oy = originalPos.getCoordinate(1); final double yVec = ry - oy; final double module = FastMath.sqrt(FastMath.pow(xVec, 2) + FastMath.pow(yVec, 2)); final double yVecToMid1 = yVer * halfDistance; final Position vecToMid1 = new Continuous2DEuclidean(xVecToMid1, yVecToMid1); final Position midPoint = originalPos.add(vecToMid1); final double newHalfDistance = newDistanceToScan / 2; final Position vecToMid2 = new Continuous2DEuclidean(xVer * newHalfDistance, yVer * newHalfDistance); final Position newMidPoint = originalPos.add(vecToMid2); range = FastMath.sqrt(FastMath.pow(newHalfDistance, 2) + FastMath.pow(newMaxDiameter, 2)); return getNodesWithinRange(newMidPoint, range).stream() .filter(Optional::isPresent) .map(Optional::get) .min((p1, p2) -> Double.compare(p1.getDistanceTo(originalPos), p2.getDistanceTo(originalPos))) .orElse(requestedPos);
@Override protected boolean nodeShouldBeAdded(final Node<Double> node, final Position p) { final boolean isWithinLimits = super.nodeShouldBeAdded(node, p); if (isWithinLimits) { if (node instanceof CellWithCircularArea) { final CellWithCircularArea thisNode = (CellWithCircularArea) node; double range = getMaxDiameterAmongCellWithCircularShape(); if (thisNode.getDiameter() > range) { range = thisNode.getDiameter(); } final double nodeRadius = thisNode.getRadius(); return isWithinLimits && (range <= 0 || !getNodesWithinRange(p, range).stream() .filter(n -> n instanceof CellWithCircularArea) .map(n -> (CellWithCircularArea) n) .filter(n -> getPosition(n).getDistanceTo(p) < nodeRadius + n.getRadius()) .findFirst() .isPresent()); } else { return true; } } else { return false; } }
@Override protected boolean isAllowed(final Position p) { return (p.getCoordinate(0) < maxX && p.getCoordinate(0) > minX && p.getCoordinate(1) < maxY && p.getCoordinate(1) > minY); }
/** * Updates the environment size to include the provided position. * * @param pos * the position to include */ protected final void includeObject(final Position pos) { assert pos.getDimensions() == 2; final double x = pos.getCoordinate(0); final double y = pos.getCoordinate(1); includeObject(x, x, y, y); }
@Override protected boolean isAllowed(final Position p) { final double[] coord = p.getCartesianCoordinates(); return allowed(coord[0], coord[1]); }
@Override public Field nbrRange() { final boolean useRoutesAsDistances = env instanceof MapEnvironment<?> && node.contains(USE_ROUTES_AS_DISTANCES); return buildFieldWithPosition(p -> { if (useRoutesAsDistances) { if (node.contains(APPROXIMATE_NBR_RANGE)) { try { final double tolerance = (double) node.getConcentration(APPROXIMATE_NBR_RANGE); final double currTime = env.getSimulation().getTime().toDouble(); if (currTime > nbrRangeTimeout) { nbrRangeTimeout = currTime + tolerance; precalcdRoutingDistance = routingDistance(p); } assert !Double.isNaN(precalcdRoutingDistance); return precalcdRoutingDistance; } catch (final ClassCastException e) { throw new IllegalStateException(APPROXIMATE_NBR_RANGE + " should be associated with a double concentration", e); } } return routingDistance(p); } return getDevicePosition().getDistanceTo(p); }); }
@Override public boolean intersectsObstacle(final Position p1, final Position p2) { return intersectsObstacle(p1.getCoordinate(0), p1.getCoordinate(1), p2.getCoordinate(0), p2.getCoordinate(1)); }
@Override public void moveNode(final Node<T> node, final Position direction) { final Position cur = getPosition(node); final double ox = cur.getCartesianCoordinates()[0]; final double oy = cur.getCartesianCoordinates()[1]; double nx = direction.getCartesianCoordinates()[0] + ox; double ny = direction.getCartesianCoordinates()[1] + oy; if (ox >= 0 && oy <= SIZE) { final Position next = next(ox, oy, nx, ny); nx = next.getCartesianCoordinates()[0] + ox; ny = next.getCartesianCoordinates()[1] + oy; if (nx < 1.0 && ny < 1.0 || nx > UPPER && ny > UPPER) { removeNode(node); } else { super.moveNode(node, next); } } }
private Position weightedAverageVectors(final List<Node<Double>> list, final CellNode thisNode) { Position res = new Continuous2DEuclidean(0, 0); final Position thisNodePos = env.getPosition(thisNode); for (final Node<Double> n : list) { final Position nPos = env.getPosition(n); Position vecTemp = new Continuous2DEuclidean( nPos.getCoordinate(0) - thisNodePos.getCoordinate(0), nPos.getCoordinate(1) - thisNodePos.getCoordinate(1)); final double vecTempModule = FastMath.sqrt(FastMath.pow(vecTemp.getCoordinate(0), 2) + FastMath.pow(vecTemp.getCoordinate(1), 2)); vecTemp = new Continuous2DEuclidean( n.getConcentration(biomol) * (vecTemp.getCoordinate(0) / vecTempModule), n.getConcentration(biomol) * (vecTemp.getCoordinate(1) / vecTempModule)); res = new Continuous2DEuclidean( res.getCoordinate(0) + vecTemp.getCoordinate(0), res.getCoordinate(1) + vecTemp.getCoordinate(1)); } return res; }