In a function node which code construct would be more efficient?
Straightforward:
var inside = global.get('insidePolygon');
msg.InAlertRegion=true; // default is to accept detection anywhere in image
var poly=[[]];
// polygon points picked from image using GIMP
var DriveWayPoly = [ [600,1400], [2200,700], [3841,500], [3841,1500], [2200, 2161], [1400,2161] ];
var CliffwoodPoly =[ [-1,1300], [3841,1300], [3841,2161], [-1,2161] ];
var IntersectionPoly =[ [-1,850], [400,850], [700,1081], [-1,1081] ];
var HummingbirdRightPoly =[ [-1,1100], [1750,950], [1650,1200], [2900,1200], [3841,1800], [2800,2161], [-1, 2161] ];
var FrontDoorPoly =[[-1,900], [600,1100], [600,900], [3841,1100], [3841,2161], [-1,2161], ];
var HummingbirdLeftPoly =[[1200,850], [500,1600], [-1,2161], [3841,2161], [3841,1000], [3000,1000]];
if(msg.filename.includes("DriveWay"))
poly=DriveWayPoly;
else if(msg.filename.includes("Cliffwood"))
poly=CliffwoodPoly;
else if(msg.filename.includes("Intersection"))
poly=IntersectionPoly;
else if(msg.filename.includes("HummingbirdRight"))
poly=HummingbirdRightPoly;
else if(msg.filename.includes("FrontDoor"))
poly=FrontDoorPoly;
else if(msg.filename.includes("HummingbirdLeft"))
poly=HummingbirdLeftPoly;
if(poly.length > 2)
msg.InAlertRegion = inside([msg.endX, msg.endY], poly); // returns false if point not inside polygon
if(msg.InAlertRegion === true)
msg.filename=msg.filename.replace("AI", "AI.alert");
return msg;
Or this to skip the creation/initialization of probably unused static variables:
var inside = global.get('insidePolygon');
msg.InAlertRegion=true; // default is to accept detection anywhere in image
var poly=[[]];
if(msg.filename.includes("DriveWay"))
poly=[ [600,1400], [2200,700], [3841,500], [3841,1500], [2200, 2161], [1400,2161] ];
else if(msg.filename.includes("Cliffwood"))
poly=[ [-1,1300], [3841,1300], [3841,2161], [-1,2161] ];
else if(msg.filename.includes("Intersection"))
poly=[ [-1,850], [400,850], [700,1081], [-1,1081] ];
else if(msg.filename.includes("HummingbirdRight"))
poly=[ [-1,1100], [1750,950], [1650,1200], [2900,1200], [3841,1800], [2800,2161], [-1, 2161] ];
else if(msg.filename.includes("FrontDoor"))
poly=[[-1,900], [600,1100], [600,900], [3841,1100], [3841,2161], [-1,2161], ];
else if(msg.filename.includes("HummingbirdLeft"))
poly=[[1200,850], [500,1600], [-1,2161], [3841,2161], [3841,1000], [3000,1000]];
if(poly.length > 2)
msg.InAlertRegion = inside([msg.endX, msg.endY], poly); // returns false if point not inside polygon
if(msg.InAlertRegion === true)
msg.filename=msg.filename.replace("AI", "AI.alert");
return msg;
I like the second form better since its fewer lines of code by losing the "intermediate" variables, but I think the first is a bit easier to understand.
Or won't it matter? Expecting to run this on Pi3/4 class machines.
Ultimately on flow startup I'd like to read the variable values from a file and store them in flow context for use in the function, this would then seem to require the re-introduction of the intermediate variables.