-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetBoundingBox.cs
More file actions
194 lines (175 loc) · 7.51 KB
/
Copy pathgetBoundingBox.cs
File metadata and controls
194 lines (175 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Xml;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetBoundingBox : MonoBehaviour
{
public string label;
public Transform topLeft, topRight, bottomLeft, bottomRight;
public Camera cam;
public Texture aTexture;
private float[] boundingBox;
public float cutoffFraction;
public int screenShotCount;
public float[] getBounds()
{
return boundingBox;
}
// Start is called before the first frame update
void Start()
{
boundingBox = new float[4];
ScreenCapture.CaptureScreenshot("test");
cutoffFraction = 3f;
screenShotCount = 0;
}
public Boolean isInFrame()
{
Boolean above = false;
Boolean right = false;
Boolean below = false;
Boolean left = false;
if (boundingBox[0] == 0 && boundingBox[2] == 0) left = true;
if (boundingBox[0] == Screen.width && boundingBox[2] == Screen.width) right = true;
if (boundingBox[1] == 0 && boundingBox[3] == 0) above = true;
if (boundingBox[1] == Screen.height && boundingBox[3] == Screen.height) below = true;
if (boundingBox[0] == 0 && boundingBox[2] == Screen.width) return false;
if (boundingBox[0] == Screen.width && boundingBox[2] == 0) return false;
if (boundingBox[1] == 0 && boundingBox[3] == Screen.height) return false;
if (boundingBox[1] == Screen.height && boundingBox[2] == 0) return false;
if ((above && right) || (below && right) || (below && left) || (above && left))
{
return false;
}
else return true;
}
// Update is called once per frame
void FixedUpdate()
{
// get initial positions of points
Vector3 SPTopLeft = cam.WorldToScreenPoint(topLeft.position);
Vector3 SPTopRight = cam.WorldToScreenPoint(topRight.position);
Vector3 SPBottomLeft = cam.WorldToScreenPoint(bottomLeft.position);
Vector3 SPBottomRight = cam.WorldToScreenPoint(bottomRight.position);
// find min/max x/y values
if (SPTopLeft.x < SPBottomLeft.x) {
boundingBox[0] = SPTopLeft.x;
} else {
boundingBox[0] = SPBottomLeft.x;
}
if (SPBottomLeft.y < SPBottomRight.y) {
boundingBox[1] = SPBottomLeft.y;
} else {
boundingBox[1] = SPBottomRight.y;
}
if (SPTopRight.x > SPBottomRight.x) {
boundingBox[2] = SPTopRight.x;
} else {
boundingBox[2] = SPBottomRight.x;
}
if (SPTopLeft.y > SPTopRight.y) {
boundingBox[3] = SPTopLeft.y;
} else {
boundingBox[3] = SPTopRight.y;
}
// move origin to top left
float temp = boundingBox[1];
boundingBox[1] = Screen.height - boundingBox[3];
boundingBox[3] = Screen.height - temp;
// cut off values that are beyond screen dimensions
if (boundingBox[0] < 0f) boundingBox[0] = 0f;
if (boundingBox[0] > Screen.width) boundingBox[0] = Screen.width;
if (boundingBox[1] < 0f) boundingBox[1] = 0f;
if (boundingBox[1] > Screen.height) boundingBox[1] = Screen.height;
if (boundingBox[2] > Screen.width) boundingBox[2] = Screen.width;
if (boundingBox[2] < 0f) boundingBox[2] = 0f;
if (boundingBox[3] > Screen.height) boundingBox[3] = Screen.height;
if (boundingBox[3] < 0f) boundingBox[3] = 0f;
/*
Debug.Log("1: "+((boundingBox[0] > Screen.width / cutoffFraction) && boundingBox[0] < Screen.width - Screen.width/cutoffFraction)
+ " 2: "+ ((boundingBox[2] < (Screen.width - Screen.width / cutoffFraction)) && boundingBox[2] > Screen.width / cutoffFraction)
+ " 3: "+ (boundingBox[2] - boundingBox[0] > Screen.width / cutoffFraction)
+ " 4: "+ ((boundingBox[1] > Screen.height / cutoffFraction) && boundingBox[1] < Screen.height - Screen.height/cutoffFraction)
+ " 5: "+ ((boundingBox[3] < (Screen.height - Screen.height / cutoffFraction)) && boundingBox[3] > Screen.height / cutoffFraction)
+ " 6: "+ (boundingBox[3] - boundingBox[1] > Screen.height / cutoffFraction));
// checks if object is within 1/cutoffFraction of the screen dimensions ie; at least 1/3 of the screen's width and height is covered.
if (
(
(boundingBox[0] > Screen.width / cutoffFraction && boundingBox[0] < Screen.width - Screen.width / cutoffFraction)
|| (boundingBox[2] < Screen.width - Screen.width/cutoffFraction && boundingBox[2] > Screen.width/cutoffFraction)
|| (boundingBox[2]-boundingBox[0] > Screen.width/cutoffFraction)
)
&&
(
(boundingBox[1] > Screen.height / cutoffFraction && boundingBox[1] < Screen.height - Screen.height / cutoffFraction)
|| (boundingBox[3] < Screen.height - Screen.height / cutoffFraction && boundingBox[3] > Screen.height/cutoffFraction)
|| (boundingBox[3] - boundingBox[1] > Screen.height / cutoffFraction)
)
)
{
Debug.Log("Would include data!");
if (screenShotCount < 1)
{
string[] lines = { "xmin: " + boundingBox[0] / Screen.width, "ymin: " + boundingBox[1] / Screen.height, "xmax: " + boundingBox[2] / Screen.width, "ymax:" + boundingBox[3] / Screen.height };
// Set a variable to the Documents path.
//string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
String docPath = Application.dataPath;
if (!File.Exists(Path.Combine(docPath, "Images"))) Directory.CreateDirectory(Path.Combine(docPath, "Images"));
// Write the string array to a new file named "WriteLines.txt".
long ms = DateTimeOffset.Now.ToUnixTimeMilliseconds();
using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, ms + ".xml")))
{
foreach (string line in lines)
outputFile.WriteLine(line);
}
XmlTextWriter textWriter = new XmlTextWriter(Path.Combine(Path.Combine(docPath,"Images"), ms + ".xml"), null);
textWriter.WriteStartDocument();
// Write first element
textWriter.WriteStartElement("annotation");
textWriter.WriteStartElement("folder");
textWriter.WriteString("Images");
textWriter.WriteEndElement();
textWriter.WriteStartElement("size");
textWriter.WriteStartElement("width");
textWriter.WriteString(""+Screen.width);
textWriter.WriteEndElement();
textWriter.WriteStartElement("height");
textWriter.WriteString(""+Screen.height);
textWriter.WriteEndElement();
textWriter.WriteEndElement();
// Write next element
textWriter.WriteStartElement("Name", "");
textWriter.WriteString("Student");
textWriter.WriteEndElement();
// Write one more element
textWriter.WriteStartElement("Address", "");
textWriter.WriteString("Colony");
textWriter.WriteEndElement();
textWriter.WriteEndDocument();
// close writer
textWriter.Close();
screenShotCount++;
}
}
Debug.Log("xmin: " + boundingBox[0] + " ymin: " + boundingBox[1] +
" xmax: " + boundingBox[2] + " ymax: " + boundingBox[3] + " Screen width: " + Screen.width
+ " Screen Height: " + Screen.height + " Box height: "+(boundingBox[3]-boundingBox[1])+" Box Width: "
+ (boundingBox[2]-boundingBox[0]));
Debug.Log("BL: "+SPBottomLeft.x+", "+SPBottomLeft.y
+ "BR: " + SPBottomRight.x + ", " + SPBottomRight.y
+ "TL: " + SPTopLeft.x + ", " + SPTopLeft.y
+ "TR: " + SPTopRight.x + ", " + SPTopRight.y);
*/
}
void OnGUI()
{
if (Event.current.type.Equals(EventType.Repaint))
{
// testing texture for evaluating points
//Graphics.DrawTexture(new Rect(boundingBox[0], boundingBox[1], (boundingBox[2] - boundingBox[0]), (boundingBox[3] - boundingBox[1])), aTexture);
//Graphics.DrawTexture(new Rect(30, 10, 100, 50), aTexture);
}
}
}