summaryrefslogtreecommitdiff
path: root/trunk/users/metalab/AoI/scripts/MetaGUITest.bsh
blob: 7caf2cedaf753e6f07c24b2fcaa428144e67f864 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
  <?xml version='1.0' standalone='yes' ?>
  <!--  xml header for scripts & plugin manager --> 
  <script>
  <name>MetaGuiTest</name>
  <author>Philipp Tiefenbacher, Stefan Farthofer and Marius Kintel (Metalab)</author>
  <version>1.0</version>
  <beta>1</beta>
  <date>2/11/2008</date>
  <description>
  FIXME: MetaMagic
  </description>
  <comments>
  </comments>
  </script>
*/


/*
  TODO:

  * don't collapse hierarchy after operation: Need to get hold of the TreeList instance. Doesn't look doable..
  * ok/cancel -> close: Use superclass of ComponentsDialog?
  * Make evaluate work with object which have been converted to triangles
  * stop the UI thread while our methods execute to prevent race conditions

  */

class MetaCAD
{
    public static String opToString(int operation) {
        switch (operation) {
        case CSGObject.UNION:
            return "union";
            break;
        case CSGObject.INTERSECTION:
            return "intersection";
            break;
        case CSGObject.DIFFERENCE12:
            return "difference";
            break;
        default:
            return null;
        }
    }

    public static int stringToOp(String opstr) {
        String lower = opstr.toLowerCase();
        if (lower.startsWith("union") || lower.startsWith("+")) {
            return CSGObject.UNION;
        }
        else if (lower.startsWith("intersection") || lower.startsWith("/")) {
            return CSGObject.INTERSECTION;
        }
        else if (lower.startsWith("difference") || lower.startsWith("-")) {
            return CSGObject.DIFFERENCE12;
        }
        else return -1;
    }

    


    /*
      Recursively (Re-)evaluates the object tree rooted at the given root object
      based on the object name.

      The result should be one CSG object where the entire child tree is disabled.
    */
    public static ObjectInfo evaluateNode(ObjectInfo parent, UndoRecord undo)
    {
        int op = stringToOp(parent.name);
        if (op == -1) return parent;
        else return combine(parent, op, undo);
    }

    /*
      Recursively (Re-)deevaluates the object tree rooted at the given root object
      based on the object name.

      This disables all implicit (parent) objects and enabled the leaf nodes.
    */
    public static void devaluateNode(ObjectInfo parent, UndoRecord undo)
    {
        int op = stringToOp(parent.name);
        if (op != -1) {
            if (undo != null)
                undo.addCommand(UndoRecord.COPY_OBJECT_INFO, new Object [] {parent, parent.duplicate()});
            parent.setVisible(false);

            //TODO: should not be necessary since we don't modify the object (only the object info)
            window.getScene().objectModified(parent.getObject());

            ObjectInfo[] children = parent.getChildren();
            for (int i=0;i<children.length;i++) {
                devaluateNode(children[i], undo);
            }
        }
        else {
            if (undo != null)
                undo.addCommand(UndoRecord.COPY_OBJECT_INFO, new Object [] {parent, parent.duplicate()});
            parent.setVisible(true);

            //TODO: should not be necessary since we don't modify the object (only the object info)
            window.getScene().objectModified(parent.getObject());
        }
    }

    /*
      Performs the given operation on all the children of the given object.
      Recursively calls evaluateNode() on each child.
    */ 
    public static ObjectInfo combine(ObjectInfo parent, int operation, UndoRecord undo)
    {
        debug("start combine single ObjectInfo");
        //compute combined object
        ObjectInfo unioninfo = combine(parent.getChildren(), operation, undo);
        debug("children combined, before adding undo");

        //    parent.setObject(unioninfo.object.duplicate());
        //   parent.clearCachedMeshes();
        //   window.getScene().objectModified(parent.object);
        // window.getScene().replaceObject(parent.object, unioninfo.object);

        //add undo
        //      if (undo != null)
        //        undo.addCommandAtBeginning(UndoRecord.COPY_OBJECT_INFO, new Object[] { parent, parent.duplicate()});
        //      debug("combine single ObjectInfo, after adding undo record");


        //and replace the object info
        undo.addCommandAtBeginning(UndoRecord.COPY_OBJECT, new Object[] { parent.object, parent.object.duplicate() });
        parent.object.copyObject(unioninfo.object);

        undo.addCommandAtBeginning(UndoRecord.COPY_COORDS, new Object[] { parent.coords, parent.coords.duplicate()});
        parent.coords.copyCoords(new CoordinateSystem());
      
        undo.addCommandAtBeginning(UndoRecord.COPY_OBJECT_INFO, new Object [] { parent, parent.duplicate() });
        parent.setVisible(true);

        parent.clearCachedMeshes();
        window.getScene().objectModified(parent.object);
        window.rebuildItemList();



        //parent.setVisible(false);
        //parent.clearCachedMeshes();
        //parent.setObject(unioninfo.getObject());
        //window.getScene().objectModified(parent.getObject());
        //parent.setVisible(true);
        //parent.setObject(unioninfo.getObject());
        //window.getScene().objectModified(parent.getObject());
        //window.rebuildItemList();

        debug("end combine single ObjectInfo");
        return parent;
    }

    /*
      Performs the given operation on the list of objects (of size >= 2),
      and returns the resulting ObjectInfo containing a CSGObject.

      Calls evaluateNode() on each child.
    */
    public static ObjectInfo combine(ObjectInfo[] objects, int operation, UndoRecord undo)
    {
        debug("start combine");
      
        if (objects.length < 1) return null;
        if (objects.length < 2) return objects[0];

        ObjectInfo a, b;

        a = evaluateNode(objects[0], undo);
        b = evaluateNode(objects[1], undo);

        Object3D unionobj = new CSGObject(a, 
                                          b, 
                                          operation);
        ObjectInfo unioninfo = new ObjectInfo(unionobj, new CoordinateSystem(), "tmp");

        for (int i=2;i<objects.length;i++) {
            unionobj = new CSGObject(unioninfo, evaluateNode(objects[i], undo), operation);
            unioninfo = new ObjectInfo(unionobj, new CoordinateSystem(), "tmp");
        }

        debug("before modify visibility");

        for (int i=0;i<objects.length;i++) {
            if (undo != null)
                undo.addCommandAtBeginning(UndoRecord.COPY_OBJECT_INFO, new Object[] {objects[i], objects[i].duplicate()});

            objects[i].setVisible(false);

            //TODO: is this needed? comments in AOI source say no...
            window.getScene().objectModified(objects[i].getObject());
        }

        debug("end combine");
        return unioninfo;
    }

    /*
      Creates a new object consisting of the result of performing the given
      operation on the given list of objects (of length >= 2).

      Inserts the new object into the scene and makes the original objects
      children of the new object. Also hides the children.
    */
    public static ObjectInfo create(ObjectInfo[] objects, int operation, UndoRecord undo)
    {
        debug("start create");
        Scene scene = window.getScene();

        //create ObjectInfo for combined object
        ObjectInfo result = combine(objects, operation, undo);
        result.setName(opToString(operation));

        debug("after combine");
        //add the object info to the wimdow (which adds it to the scene and the item tree
        // and creates the proper undo record commands)
        window.getScene().addObject(result, undo);

        debug("after addObject");

        //move children
        for (int i=0;i<objects.length;i++) {
            result.addChild(objects[i], i);

            if (undo != null)
                undo.addCommandAtBeginning(UndoRecord.REMOVE_FROM_GROUP, new Object[] {result, objects[i]});
            debug("added one");
        }

        debug("after making selected objects children");

        //select new node
        int[] oldSelection = window.getScene().getSelection();
        debug("after get old selection");

        window.setSelection(window.getScene().indexOf(result));
        debug("after setting new node as selected");

        if (undo != null)
            undo.addCommandAtBeginning(UndoRecord.SET_SCENE_SELECTION, new Object[] { oldSelection});
 
        debug("after adding selection undo");
        window.rebuildItemList();
        return result;
    }

    public static void debug(String text) {
        //new MessageDialog(window, text);
    }
}

// dummy needed to make ComponentsDialog nonmodal
class MetaCADDummyCallback implements java.lang.Runnable
{
    public void run()
    {
        // We don't care if we are called
    }
}

class MetaCADCallbacks
{
    /**
       Returns a normalized selection, meaning children of a selected parent
       are removed.
    */
    public ObjectInfo[] getSelection() {
        sel = window.getSelectedObjects();
        ObjectInfo[] objects = new ObjectInfo[sel.size()];
        it = sel.iterator();
        int i = 0;
        while (it.hasNext()) {
            ObjectInfo objinfo = it.next();
            ObjectInfo p = objinfo.getParent();
            while (p != null) { // Check if any parent is selected
                if (window.isObjectSelected(p)) break;
                p = p.getParent();
            }
            if (p == null) objects[i++] = objinfo;
        }
        ObjectInfo[] cleanedobjects = new ObjectInfo[i];
        System.arraycopy(objects, 0, cleanedobjects, 0, i);
        return cleanedobjects;
    }

    public void evaluate()
    {
        try {
            UndoRecord undo = new UndoRecord(window, false);
            ObjectInfo[] objects = getSelection();
            if (object != null) {
                for (int i=0;i<objects.length;i++) {
                    MetaCAD.evaluateNode(objects[i], undo);
                }

                window.setUndoRecord(undo);
                window.rebuildItemList();
                window.updateImage();
                window.setModified();
            }
        }
        catch (Exception ex) {
            MetaCAD.debug("exception in avaluate: " + ex.toString());
            throw ex;
        }
    }
 
    public void devaluate()
    {
        UndoRecord undo = new UndoRecord(window, false);
        ObjectInfo[] objects = getSelection();
        if (objects != null) {
            for (int i=0;i<objects.length;i++) {
                MetaCAD.devaluateNode(objects[i], undo);
            }

            window.setUndoRecord(undo);
            window.rebuildItemList();
            window.updateImage();
            window.setModified();
        }
    }
 
    public void execute(int operation) {
        UndoRecord record = new UndoRecord(window, false);
        ObjectInfo[] objects = getSelection();
        if (objects == null || objects.length < 2) {
            new MessageDialog(window, "Minimum two objects must be selected.");
        }
        else {
            MetaCAD.debug("before create");
            ObjectInfo result = MetaCAD.create(objects, operation, record);

            window.setUndoRecord(record);
            window.updateImage();
            window.setModified();
        }
    }

    public void union()
    {
        MetaCAD.debug("starting union");
        execute(CSGObject.UNION);
    }
 
    public void intersect()
    {
        execute(CSGObject.INTERSECTION);
    }
 
    public void subtract()
    {
        execute(CSGObject.DIFFERENCE12);
    }
}

 
 
BButton evaluateButton, devaluateButton;
BButton unionButton, intersectButton, subtractButton;


dlg = new ComponentsDialog(window, "MetaCAD Control Panel" ,
                           new Widget [] { 
                               evaluateButton = new BButton( "evaluate" ),
                               devaluateButton = new BButton( "devaluate" ),

                               unionButton = new BButton( "union" ),
                               intersectButton = new BButton( "intersect" ),
                               subtractButton = new BButton( "subtract" ),
                           },
                           new String [] {"Actions", "", "Boolean Operations", "", ""}, new MetaCADDummyCallback(), new MetaCADDummyCallback() );
dlg.setOkEnabled(true);

MetaCADCallbacks callbacks = new MetaCADCallbacks();

evaluateButton.addEventLink(CommandEvent.class, callbacks, "evaluate" );
devaluateButton.addEventLink(CommandEvent.class, callbacks, "devaluate" );

unionButton.addEventLink(CommandEvent.class, callbacks, "union" );
intersectButton.addEventLink(CommandEvent.class, callbacks, "intersect" );
subtractButton.addEventLink(CommandEvent.class, callbacks, "subtract" );