1 /*****************************************************************************
2 * Virtual Mockup for Machine Vision
3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr.,
4 * Carlos S. Santos.
5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP
6 *
7 * Permission is granted to redistribute and/or modify this
8 * software under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html)
16 * for more details.
17 *
18 *****************************************************************************/
19
20 package camera3d.action;
21
22
23
24 import java.util.*;
25 import camera3d.GUIControl;
26 import camera3d.TransformMode;
27
28 /***
29 *
30 * @author Fábio Roberto de Miranda
31 */
32 public class ActionQueue {
33
34 /*
35 * Turns on optimization. In optimized mode, some actions, such as translations
36 * or rotations are combined to save processing time
37 */
38 private boolean isOptimized = true;
39
40 /*
41 * prevents the addition of new actions to the queue;
42 * added to allow synchronous execution of methods from
43 * remote applications
44 */
45 private boolean blocked = false;
46
47 private Vector queue;
48
49 public ActionQueue() {
50 this(150, 100);
51 }
52
53 public ActionQueue(int initialCapacity, int increment) {
54 queue = new Vector(initialCapacity, increment);
55 }
56
57 public void setBlocked(boolean blocked){
58 this.blocked = blocked;
59 }
60
61 public boolean isEmpty(){
62 return queue.isEmpty();
63 }
64
65 public boolean isBlocked(){
66 return this.blocked;
67 }
68
69 public int size(){
70 return queue.size();
71 }
72
73 public synchronized void push(VcAction action){
74 if(!blocked){
75 debugln("queue size: " + queue.size());
76 //System.out.println(action.toString());
77 if(isOptimized()&&queue.size()>0){
78 Object obj = queue.lastElement();
79 if(obj instanceof OptimizableAction)
80 {
81 OptimizableAction last = (OptimizableAction)obj;
82 if(last.optimize(action)){
83 debugln("ActionQueue: optmizing");
84 //System.out.println("ActionQueue: optmizing");
85 notifyAll();
86 return;
87 }
88 }
89 }
90 //debugln("ActionQueue: not optmizing");
91 //System.out.println("ActionQueue: not optmizing");
92 queue.addElement(action);
93 notifyAll();
94 }
95 }
96
97 public synchronized VcAction pop(){
98 if (queue.isEmpty()){
99 try {
100 wait();
101 } catch (InterruptedException ie){
102 ie.printStackTrace();
103 System.out.println("Interrupted Exception while retrieving element from ActionQueue");
104 return null;
105 }
106 }
107 VcAction action = (VcAction)(queue.firstElement());
108 queue.removeElementAt(0);
109 /*
110 trying to replace optimization at pop() method with optimization
111 at push() method.
112 Carlos 09/09/2002
113 */
114 /*
115 if (isOptimized){
116 if (isAbsoluteTransformAction(action)){
117 int lastIndex = -1;
118 if (!queue.isEmpty()){
119
120 for (int i=0; i< queue.size(); i++){
121 VcAction anotherAction = (VcAction)queue.get(i);
122 if (sameActionType(action, anotherAction)){
123 if (isAbsoluteTransformAction(anotherAction)){
124 action = anotherAction;
125 lastIndex = i;
126 }
127 } else {
128 break;
129 }
130 }
131 }
132 debugln("ActionQueue: optimization eliminated "+lastIndex+" elements");
133 for (int i=0; i< lastIndex; i++){
134 queue.removeElementAt(0);
135 }
136 }
137 }
138 */
139 return action;
140 }
141
142 public VcAction peek(){
143 if (queue.isEmpty()){
144 return null;
145 } else {
146 return (VcAction)queue.firstElement();
147 }
148 }
149
150 public static void main(String[] args) {
151 ActionQueue actionQueue1 = new ActionQueue();
152 }
153
154 public void setOptimized(boolean b){
155 this.isOptimized = b;
156 }
157
158 public boolean isOptimized(){
159 return this.isOptimized;
160 }
161
162 protected boolean sameActionType(VcAction action1, VcAction action2){
163 return action1.getClass().equals(action2.getClass());
164 }
165
166 /*
167 boolean isAbsoluteTransformAction(VcAction action){
168 if (action instanceof TransformAction){
169 TransformAction transformAction = (TransformAction)action;
170 if (transformAction.getTransformMode()==TransformMode.ABSOLUTE){
171 return true;
172 }
173 }
174 return false;
175 }
176 */
177
178 public void debugln(String s){
179 if (GUIControl.debugflag){
180 System.out.println(s);
181 }
182 }
183 }
This page was automatically generated by Maven