2 * bnac : virtual museum environment creation/manipulation project
3 * Copyright (C) 2010 Felipe CaƱas Sabat
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package cl
.uchile
.dcc
.bnac
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Properties
;
24 import javax
.vecmath
.Point2f
;
25 import javax
.vecmath
.Point3f
;
29 protected float height
;
30 protected ArrayList
<Point2f
> corners
;
31 protected ArrayList
<Wall
> walls
;
32 protected ArrayList
<StartingPoint
> sps
;
33 protected Point2f min
;
34 protected Point2f max
;
35 protected Properties props
;
37 public Hall (String id
)
39 BnacLog
.trace("new Hall %s", id
);
40 props
= new Properties();
41 props
.setProperty(Attribute
.Id
.name(), id
);
42 props
.setProperty("name", id
);
45 this.corners
= new ArrayList
<Point2f
>();
46 walls
= new ArrayList
<Wall
>();
47 sps
= new ArrayList
<StartingPoint
>();
50 new Point2f(0.0f
, 0.0f
),
51 new Point2f(0.0f
, 10.0f
),
52 new Point2f(10.0f
, 10.0f
),
53 new Point2f(10.0f
, 0.0f
)
58 public Hall (String id
, float height
, Point2f
[] corners
)
60 if (corners
.length
> 0) {
61 BnacLog
.trace("new Hall %s %.02f >>", id
, height
);
62 for (Point2f corner
: corners
) {
63 BnacLog
.trace("\t(%.02f,%.02f)", corner
.x
, corner
.y
);
66 BnacLog
.trace("new Hall %s %.02f >><<", id
, height
);
69 props
= new Properties();
70 props
.setProperty(Attribute
.Id
.name(), id
);
73 this.corners
= new ArrayList
<Point2f
>();
74 walls
= new ArrayList
<Wall
>();
75 sps
= new ArrayList
<StartingPoint
>();
80 public void setHeight (float h
)
82 BnacLog
.debug("set height %.02f", h
);
86 public float getHeight () { return height
; }
88 public void setCorners (float[] ps
)
90 Point2f
[] r
= new Point2f
[ps
.length
/2];
91 for (int i
=0; i
<ps
.length
; i
+=2) {
92 r
[i
/2] = new Point2f(ps
[i
], ps
[i
+1]);
96 public void setCorners (Point2f
[] ps
)
98 if (!Util
.isClockwise(ps
)) { return; }
101 for (Point2f p
: ps
) { corners
.add(p
); }
103 for (int i
=0; i
<ps
.length
; ++i
) {
104 walls
.add(new Wall(ps
[i
], ps
[(i
+1)%ps
.length
]));
109 public Wall
addCorner (int idx
, Point2f p
)
111 BnacLog
.debug("addCorner %d (%.02f,%.02f)", idx
, p
.x
, p
.y
);
112 Wall wal
= walls
.get((idx
>0)?idx
-1:walls
.size()-1);
113 Wall newwall
= wal
.split(p
);
116 walls
.add(idx
, newwall
);
119 for (Point2f corner
: corners
) {
120 BnacLog
.trace("(%.02f,%.02f)", corner
.x
, corner
.y
);
122 for (int i
=0; i
<walls
.size(); ++i
) {
123 BnacLog
.trace("%d (%.02f,%.02f)-(%.02f,%.02f)", i
,
124 walls
.get(i
).origin2f().x
, walls
.get(i
).origin2f().y
,
125 walls
.get(i
).end2f().x
, walls
.get(i
).end2f().y
);
129 public void moveCorner (int idx
, Point2f p
)
131 BnacLog
.debug("moveCorner %d (%.02f,%.02f)->(%.02f,%.02f)",
132 idx
, corners
.get(idx
).x
, corners
.get(idx
).y
, p
.x
, p
.y
);
133 int prv
= (idx
>0) ? idx
-1 : corners
.size()-1;
135 corners
.get(idx
).set(p
);
136 walls
.get(prv
).setEnd(p
);
137 walls
.get(idx
).setOrigin(p
);
141 public Wall
removeCorner (int idx
)
143 BnacLog
.debug("removeCorner %d", idx
);
144 if (corners
.size() > 3) {
145 Wall prev
= walls
.get((idx
>0)?idx
-1:walls
.size()-1);
146 Wall wall
= walls
.get(idx
);
147 Wall ret
= new Wall(prev
.origin2f(), wall
.end2f());
151 walls
.remove((idx
>0)?idx
-1:walls
.size()-1);
152 walls
.remove((idx
>0)?idx
-1:walls
.size()-1);
153 walls
.add(idx
-1, ret
);
155 walls
.remove(walls
.size()-1);
157 walls
.add(walls
.size(), ret
);
160 for (Point2f corner
: corners
) {
161 BnacLog
.trace("(%.02f,%.02f)", corner
.x
, corner
.y
);
163 for (int i
=0; i
<walls
.size(); ++i
) {
164 BnacLog
.trace("%d (%.02f,%.02f)-(%.02f,%.02f)", i
,
165 walls
.get(i
).origin2f().x
, walls
.get(i
).origin2f().y
,
166 walls
.get(i
).end2f().x
, walls
.get(i
).end2f().y
);
173 public Point2f
getCorner (int idx
)
175 return new Point2f(corners
.get(idx
));
177 public Point2f
[] cornerArray () {
178 return corners
.toArray(new Point2f
[0]);
180 public int cornerCount () { return corners
.size(); }
182 protected void updateMaxMin ()
184 min
= new Point2f(Float
.MAX_VALUE
, Float
.MAX_VALUE
);
185 max
= new Point2f(Float
.MIN_VALUE
, Float
.MIN_VALUE
);
186 for (Point2f p
: corners
) {
187 if (p
.x
< min
.x
) { min
.x
= p
.x
; }
188 else if (p
.x
> max
.x
) { max
.x
= p
.x
; }
189 if (p
.y
< min
.y
) { min
.y
= p
.y
; }
190 else if (p
.y
> max
.y
) { max
.y
= p
.y
; }
194 public Wall
setWall (int idx
, Wall w
)
196 BnacLog
.debug("setWall %d (%.02f,%.02f)-(%.02f,%.02f)", idx
,
197 w
.origin2f().x
, w
.origin2f().y
,
198 w
.end2f().x
, w
.end2f().y
);
200 return walls
.set(idx
, w
);
202 public Wall
[] wallArray () { return walls
.toArray(new Wall
[0]); }
203 public Wall
getWall (int i
) { return walls
.get(i
); }
204 public int wallCount () { return walls
.size(); }
206 public int getParentWall (String id
)
208 for (int i
=0; i
<walls
.size(); ++i
) {
209 if (walls
.get(i
).getDoor(id
) != null ||
210 walls
.get(i
).getPlacedCapture(id
) != null) {
217 public PlacedCapture
getPlacedCapture (String id
)
219 return walls
.get(getParentWall(id
)).getPlacedCapture(id
);
222 public Door
getDoor (String id
)
224 for (Wall wall
: walls
) {
225 if (wall
.getDoor(id
) != null) {
226 return wall
.getDoor(id
);
232 public Point2f
dif ()
236 return new Point2f(max
.x
-min
.x
, max
.y
-min
.y
);
238 public Point2f
min ()
240 if (min
== null || max
== null) { updateMaxMin(); }
241 return new Point2f(min
);
243 public Point2f
max ()
245 if (min
== null || max
== null) { updateMaxMin(); }
246 return new Point2f(max
);
248 public Point2f
centre2f (boolean negY
)
250 return new Point2f(dif().x
/2, dif().y
/2 * (negY ?
-1 : 1));
252 public Point3f
centre3f ()
254 return new Point3f(dif().x
/2, 0.0f
, -dif().y
/2);
257 public void set (Attribute prop
, String val
)
259 props
.setProperty(prop
.name(), val
);
261 public String
get (Attribute prop
)
263 return props
.getProperty(prop
.name());
266 public void set (String prop
, String val
)
268 props
.setProperty(prop
, val
);
270 public String
get (String prop
)
272 return props
.getProperty(prop
);
275 public void setId (String id
)
277 props
.setProperty(Attribute
.Id
.name(), id
);
279 public String
getId () { return props
.getProperty(Attribute
.Id
.name()); }
281 public String
getName () { return props
.getProperty("name"); }
283 public void setName (String name
) { props
.setProperty("name", name
); }
285 public void addStartingPoint (String id
, Point2f pos
, float angle
)
287 sps
.add(new StartingPoint(id
, pos
, angle
));
289 public StartingPoint
getStartingPoint (String id
)
291 for (StartingPoint sp
: sps
)
293 if (sp
.getId().equals(id
)) { return sp
; }
297 public StartingPoint
getFirstStartingPoint () { return sps
.get(0); }
298 public StartingPoint
[] startingPointArray ()
300 return sps
.toArray(new StartingPoint
[0]);
303 public class StartingPoint
309 public StartingPoint (String id
, Point2f pos
, float angle
)
312 this.pos
= new Point2f(pos
);
316 public String
getId () { return id
; }
317 public Point3f
getPosition3f ()
319 return new Point3f(pos
.x
, 1.70f
, -pos
.y
);