/// <summary> /// Executes prediction - sets cam to capture correct area and calls RequestDecision() /// </summary> /// <param name="writingAreaInfo">info of the current area to capture</param> public void Predict(WritingAreaInfo writingAreaInfo) { //moves and sets camera size according to passed info struct _agentCam.transform.position = writingAreaInfo.center - writingAreaInfo.normal; _agentCam.transform.rotation = Quaternion.LookRotation(writingAreaInfo.normal, writingAreaInfo.upNormal); _agentCam.orthographicSize = writingAreaInfo.size; _agentCam.Render(); //make sure the camera renders updated view RequestDecision(); }
/// <summary> /// Gets the extent, center, normal, and up vector of current set of lines to be predicted in the form of /// WritingAreaInfo struct /// </summary> /// <param name="startIdx"></param> /// <returns></returns> protected WritingAreaInfo GetCurrentLinesData(int startIdx) { WritingAreaInfo areaInfo = new WritingAreaInfo(); int lineParentChildCount = _lineParent.childCount; float xMin = System.Single.PositiveInfinity; float xMax = System.Single.NegativeInfinity; float yMin = System.Single.PositiveInfinity; float yMax = System.Single.NegativeInfinity; int pointCount = 0; Vector3 pointSum = Vector3.zero; Vector3 sumNormal = Vector3.zero; Vector3 sumUp = Vector3.zero; //gets the extents and the sums of normals and positions if (startIdx < lineParentChildCount) { for (int i = 0; i < lineParentChildCount; i++) { Transform line = _lineParent.GetChild(i); if (i >= startIdx) { line.gameObject.layer = predictLayerIndex; sumNormal += line.forward; sumUp += line.up; LineRenderer lineRenderer = line.GetComponent <LineRenderer>(); pointCount += lineRenderer.positionCount; Vector3[] points = new Vector3[lineRenderer.positionCount]; lineRenderer.GetPositions(points); foreach (var locPoint in points) { Vector3 point = line.transform.TransformPoint(locPoint); //transform point to world space first var right = line.right; float x = Vector3.Dot(Vector3.Project(point, right), right); var up = line.up; float y = Vector3.Dot(Vector3.Project(point, up), up); xMin = x < xMin ? x : xMin; xMax = x > xMax ? x : xMax; yMin = y < yMin ? y : yMin; yMax = y > yMax ? y : yMax; pointSum += point; } } else { line.gameObject.layer = 0; } } } float sizeX = Mathf.Abs(xMax - xMin); float sizeY = Mathf.Abs(yMax - yMin); //average out normals and positions to get center and avg. normal Vector3 center = (pointCount > 0)?pointSum / pointCount:Vector3.zero; Vector3 normal = (lineParentChildCount > 0)?sumNormal / lineParentChildCount:Vector3.forward; Vector3 upNormal = (lineParentChildCount > 0)?sumUp / lineParentChildCount:Vector3.forward; areaInfo.size = Mathf.Max(sizeX, sizeY) * boundSizeFactor; //largest size between x and y, also multiply //by bound padding factor areaInfo.center = center; areaInfo.normal = normal.normalized; areaInfo.upNormal = upNormal.normalized; return(areaInfo); }