789c7682a5d5affddd975dfa5f6e41b44363c855
[scilab.git] / scilab / modules / graphic_objects / src / java / org / scilab / modules / graphic_objects / builder / Builder.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
4  * Copyright (C) 2013 - Scilab Enterprises - Antoine ELIAS
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11  *
12  */
13
14 package org.scilab.modules.graphic_objects.builder;
15
16 import java.util.ArrayList;
17 import java.util.StringTokenizer;
18
19 import org.scilab.modules.graphic_objects.ScilabNativeView;
20 import org.scilab.modules.graphic_objects.arc.Arc;
21 import org.scilab.modules.graphic_objects.axes.Axes;
22 import org.scilab.modules.graphic_objects.axis.Axis;
23 import org.scilab.modules.graphic_objects.fec.Fec;
24 import org.scilab.modules.graphic_objects.figure.Figure;
25 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
26 import org.scilab.modules.graphic_objects.graphicModel.GraphicModel;
27 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
28 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject.Type;
29 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
30 import org.scilab.modules.graphic_objects.imageplot.Imageplot;
31 import org.scilab.modules.graphic_objects.label.Label;
32 import org.scilab.modules.graphic_objects.legend.Legend;
33 import org.scilab.modules.graphic_objects.lighting.Light;
34 import org.scilab.modules.graphic_objects.lighting.Light.LightType;
35 import org.scilab.modules.graphic_objects.polyline.Polyline;
36 import org.scilab.modules.graphic_objects.rectangle.Rectangle;
37 import org.scilab.modules.graphic_objects.surface.Surface;
38 import org.scilab.modules.graphic_objects.textObject.Font;
39 import org.scilab.modules.graphic_objects.textObject.Text;
40 import org.scilab.modules.graphic_objects.vectfield.Champ;
41 import org.scilab.modules.graphic_objects.vectfield.Segs;
42
43 public final class Builder {
44     public final static int createRect(int parentSubwin, double x, double y,
45                                        double height, double width, int foreground,
46                                        int background, int isfilled, int isline) {
47         GraphicController controller = GraphicController.getController();
48         Axes axes = (Axes)controller.getObjectFromId(parentSubwin);
49
50         if (height < 0 || width < 0) {
51             return 0;
52         }
53
54         Integer iRect = controller.askObject(Type.RECTANGLE);
55
56         /*
57          * Sets the rectangle's parent in order to initialize the former's Contoured properties
58          * with the latter's values (cloneGraphicContext call below)
59          */
60
61         Rectangle rect = (Rectangle) controller.getObjectFromId(iRect);
62         rect.setUpperLeftPoint(new Double[] {x, y, 0.0});
63         rect.setHeight(height);
64         rect.setWidth(width);
65
66         rect.setVisible(axes.getVisible());
67
68         /* Clip state and region */
69         /* To be checked for consistency */
70
71         rect.setClipBox(axes.getClipBox());
72         rect.setClipBoxSet(axes.getClipBoxSet());
73         rect.setClipState(axes.getClipState());
74         rect.setMarkMode(axes.getMarkMode());
75
76         /*
77          * Initializes the contour properties (background, foreground, etc)
78          * to the default values (those of the parent Axes).
79          */
80         cloneGraphicContext(parentSubwin, iRect);
81
82         /* Contour settings */
83         rect.setLineMode(isline == 1);
84         rect.setFillMode(isfilled == 1);
85
86         if (foreground != -1) {
87             rect.setLineColor(foreground);
88         }
89
90         if (background != -1) {
91             rect.setBackground(background);
92         }
93
94         controller.objectCreated(iRect);
95
96         /*
97          * Sets the Axes as the rectangle's parent and adds the rectangle to
98          * its parent's list of children.
99          */
100         //setGraphicObjectRelationship(pparentsubwinUID, pobjUID);
101
102         return iRect;
103     }
104
105     public static int cloneGraphicContext(int sourceIdentifier, int destIdentifier) {
106         GraphicController controller = GraphicController.getController();
107         Boolean lineMode = false;
108         Integer foreground = 0;
109         Integer lineStyle = 0;
110         Integer background = 0;
111         Integer markForeground = 0;
112         Integer markBackground = 0;
113         Integer markStyle = 0;
114         Integer markSize = 0;
115         Integer markSizeUnit = 0;
116         Double lineThickness = 0.;
117
118         /*
119          * All these properties are passed by value thus do not care to release them
120          * and do not call releaseGraphicObjectProperty on purpose.
121          */
122
123         lineMode = (Boolean) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_LINE_MODE__);
124         foreground = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_LINE_COLOR__);
125         lineThickness = (Double) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_LINE_THICKNESS__);
126         lineStyle = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_LINE_STYLE__);
127
128         background = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_BACKGROUND__);
129
130         markForeground = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_MARK_FOREGROUND__);
131
132         markBackground = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_MARK_BACKGROUND__);
133
134         markStyle = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_MARK_STYLE__);
135
136         markSize = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_MARK_SIZE__);
137
138         markSizeUnit = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_MARK_SIZE_UNIT__);
139
140         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_LINE_MODE__, lineMode);
141         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_LINE_COLOR__, foreground);
142         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_LINE_THICKNESS__, lineThickness);
143         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_LINE_STYLE__, lineStyle);
144
145         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_BACKGROUND__, background);
146
147         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_MARK_FOREGROUND__, markForeground);
148         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_MARK_BACKGROUND__, markBackground);
149         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_MARK_STYLE__, markStyle);
150         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_MARK_SIZE__, markSize);
151         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_MARK_SIZE_UNIT__, markSizeUnit);
152
153         return 0;
154     }
155
156     public static int cloneFontContext(int sourceIdentifier, int destIdentifier) {
157         GraphicController controller = GraphicController.getController();
158
159         Integer fontColor = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_FONT_COLOR__);
160         Integer fontStyle = (Integer) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_FONT_STYLE__);
161         Double fontSize = (Double) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_FONT_SIZE__);
162         Boolean fontFractional = (Boolean) controller.getProperty(sourceIdentifier, GraphicObjectProperties.__GO_FONT_FRACTIONAL__);
163
164         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_FONT_COLOR__, fontColor);
165         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_FONT_STYLE__, fontStyle);
166         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_FONT_SIZE__, fontSize);
167         controller.setProperty(destIdentifier, GraphicObjectProperties.__GO_FONT_FRACTIONAL__, fontFractional);
168
169         return 0;
170     }
171
172     public static int createHiddenLabel(int parent) {
173
174         GraphicController controller = GraphicController.getController();
175
176         Integer iLabel = controller.askObject(Type.LABEL);
177         Label label = (Label) controller.getObjectFromId(iLabel);
178
179         //Hide Label as they are non explicit children
180         label.setHidden(true);
181         label.setAutoPosition(true);
182         label.setAutoRotation(true);
183
184         cloneGraphicContext(parent, iLabel);
185         cloneFontContext(parent, iLabel);
186         controller.objectCreated(iLabel);
187
188         // Sets the label's parent
189         controller.setGraphicObjectRelationship(parent, iLabel);
190
191         return iLabel;
192     }
193
194     public static void initSubWinBounds(int subWin) {
195         GraphicController controller = GraphicController.getController();
196
197         controller.setProperty(subWin, GraphicObjectProperties.__GO_X_AXIS_LOG_FLAG__, false);
198         controller.setProperty(subWin, GraphicObjectProperties.__GO_Y_AXIS_LOG_FLAG__, false);
199         controller.setProperty(subWin, GraphicObjectProperties.__GO_Z_AXIS_LOG_FLAG__, false);
200     }
201
202     public static void reinitSubWin(int subWin) {
203         GraphicController controller = GraphicController.getController();
204         Integer label = 0;
205
206         for (Integer childId : (Integer[]) controller.getProperty(subWin, GraphicObjectProperties.__GO_CHILDREN__)) {
207             GraphicModel.getModel().deleteObject(childId);
208         }
209
210         label = createHiddenLabel(subWin);
211         controller.setProperty(subWin, GraphicObjectProperties.__GO_TITLE__, label);
212
213         label = createHiddenLabel(subWin);
214         controller.setProperty(subWin, GraphicObjectProperties.__GO_X_AXIS_LABEL__, label);
215
216         label = createHiddenLabel(subWin);
217         controller.setProperty(subWin, GraphicObjectProperties.__GO_Y_AXIS_LABEL__, label);
218
219         label = createHiddenLabel(subWin);
220         controller.setProperty(subWin, GraphicObjectProperties.__GO_Z_AXIS_LABEL__, label);
221
222         controller.setProperty(subWin, GraphicObjectProperties.__GO_X_AXIS_LOCATION__, 0);
223         controller.setProperty(subWin, GraphicObjectProperties.__GO_Y_AXIS_LOCATION__, 4);
224         controller.setProperty(subWin, GraphicObjectProperties.__GO_VISIBLE__, true);
225         controller.setProperty(subWin, GraphicObjectProperties.__GO_FIRST_PLOT__, true);
226
227         Integer axesModel = GraphicModel.getAxesModel().getIdentifier();
228         Axes axes = (Axes) controller.getObjectFromId(axesModel);
229
230         controller.setProperty(subWin, GraphicObjectProperties.__GO_VIEW__, axes.getView());
231         controller.setProperty(subWin, GraphicObjectProperties.__GO_ROTATION_ANGLES__, axes.getRotationAngles());
232         controller.setProperty(subWin, GraphicObjectProperties.__GO_ROTATION_ANGLES_3D__, axes.getRotationAngles3d());
233     }
234
235     public static boolean isAxesRedrawing(int subWin) {
236         GraphicController controller = GraphicController.getController();
237         Axes axes = (Axes)controller.getObjectFromId(subWin);
238
239         if (axes.getAutoClear()) {
240             reinitSubWin(subWin);
241             return true;
242         }
243         return false;
244     }
245
246     public static int createLabel(int parent, int type) {
247         GraphicController controller = GraphicController.getController();
248         Double[] position = new Double[3];
249         position[0] = 1.0;
250         position[1] = 1.0;
251         position[2] = 1.0;
252
253         //check parent type
254         Integer parentType = (Integer) controller.getProperty(parent, GraphicObjectProperties.__GO_TYPE__);
255         if (parentType != GraphicObjectProperties.__GO_AXES__) {
256             return 1;
257         }
258
259         //get axes model
260         Integer axesModel = GraphicModel.getAxesModel().getIdentifier();
261
262         //get type label from axes model
263         Integer labelSource = (Integer) controller.getProperty(axesModel, type);
264
265         //clone label
266         Integer newLabel = controller.cloneObject(labelSource);
267         controller.setProperty(newLabel, GraphicObjectProperties.__GO_POSITION__, position);
268         // Auto position must be reset as setting the position has set it to false
269         Boolean autoPosition = (Boolean ) controller.getProperty(labelSource, GraphicObjectProperties.__GO_AUTO_POSITION__);
270         controller.setProperty(newLabel, GraphicObjectProperties.__GO_AUTO_POSITION__, autoPosition);
271
272         // Set relation between newLabel and parent
273         controller.setProperty(parent, type, newLabel);
274         controller.setGraphicObjectRelationship(parent, newLabel);
275         return 0;
276     }
277
278     public final static int createFigureFromModel() {
279         GraphicController controller = GraphicController.getController();
280         return controller.cloneObject(GraphicModel.getFigureModel().getIdentifier());
281     }
282
283     public final static int cloneAxesModel(int parent) {
284         GraphicController controller = GraphicController.getController();
285
286         Integer newAxes = controller.cloneObject(GraphicModel.getAxesModel().getIdentifier());
287         createLabel(newAxes, GraphicObjectProperties.__GO_X_AXIS_LABEL__);
288         createLabel(newAxes, GraphicObjectProperties.__GO_Y_AXIS_LABEL__);
289         createLabel(newAxes, GraphicObjectProperties.__GO_Z_AXIS_LABEL__);
290         createLabel(newAxes, GraphicObjectProperties.__GO_TITLE__);
291
292         controller.setGraphicObjectRelationship(parent, newAxes);
293         controller.setProperty(parent, GraphicObjectProperties.__GO_SELECTED_CHILD__, newAxes);
294
295         ScilabNativeView.ScilabNativeView__setCurrentSubWin(newAxes);
296         ScilabNativeView.ScilabNativeView__setCurrentObject(newAxes);
297         return newAxes;
298     }
299
300     public final static void cloneMenus(int iModel, int iParent) {
301         GraphicController controller = GraphicController.getController();
302         GraphicObject model = controller.getObjectFromId(iModel);
303
304         Integer[] children = model.getChildren();
305
306         for (int i = children.length - 1 ; i >= 0 ; i--) {
307             GraphicObject child = controller.getObjectFromId(children[i]);
308             if (child.getType() == GraphicObjectProperties.__GO_UIMENU__) {
309                 Integer newMenu = controller.cloneObject(children[i]);
310                 controller.setGraphicObjectRelationship(iParent, newMenu);
311
312                 cloneMenus(children[i], newMenu);
313             }
314         }
315     }
316     
317     public final static int createNewFigureWithAxes() {
318         GraphicController controller = GraphicController.getController();
319         Integer figModel  = GraphicModel.getFigureModel().getIdentifier();
320
321         //clone default figure
322         Integer newFigure = createFigureFromModel();
323
324         //Clone the default menus
325         cloneMenus(figModel, newFigure);
326
327         //set ID to 0
328         controller.setProperty(newFigure, GraphicObjectProperties.__GO_ID__, 0);
329
330         //clone default axes
331         cloneAxesModel(newFigure);
332         ScilabNativeView.ScilabNativeView__setCurrentFigure(newFigure);
333
334         //Force axes size after window creation
335         Integer[] figureSize = (Integer[]) controller.getProperty(figModel, GraphicObjectProperties.__GO_SIZE__);
336         controller.setProperty(newFigure, GraphicObjectProperties.__GO_SIZE__, figureSize);
337         Integer[] axesSize = (Integer[]) controller.getProperty(figModel, GraphicObjectProperties.__GO_AXES_SIZE__);
338         controller.setProperty(newFigure, GraphicObjectProperties.__GO_AXES_SIZE__, axesSize);
339
340         controller.setProperty(newFigure, GraphicObjectProperties.__GO_VALID__, true);
341
342
343         return newFigure;
344     }
345
346     public final static int createSubWin(int parentFigure) {
347         GraphicController controller = GraphicController.getController();
348         GraphicObject parent = controller.getObjectFromId(parentFigure);
349         if (parent.getType() != GraphicObjectProperties.__GO_FIGURE__) {
350             return 0;
351         }
352
353         return cloneAxesModel(parentFigure);
354     }
355
356     public final static int createText(int iParentsubwinUID, String[] str, int nbRow, int nbCol,
357                                        double x, double y, boolean autoSize, double[] userSize, int centerPos,
358                                        int foreground, boolean isForeground, int background, boolean isBackground,
359                                        boolean isBoxed, boolean isLine,
360                                        boolean isFilled, int align) {
361
362         GraphicController controller = GraphicController.getController();
363         int iText = controller.askObject(Type.TEXT);
364
365         Axes axes = (Axes) controller.getObjectFromId(iParentsubwinUID);
366         Text text = (Text) controller.getObjectFromId(iText);
367
368         //clip
369         text.setClipBox(axes.getClipBox());
370         text.setClipBoxSet(axes.getClipBoxSet());
371         text.setClipState(axes.getClipState());
372
373         //text
374         Integer[] dimensions = new Integer[2];
375         dimensions[0] = nbRow;
376         dimensions[1] = nbCol;
377         text.setTextArrayDimensions(dimensions);
378         text.setTextStrings(str);
379
380         //position
381         Double[] position = new Double[3];
382         position[0] = x;
383         position[1] = y;
384         position[2] = 0.0;
385         text.setPosition(position);
386
387         //test box
388         Double[] setUserSize = new Double[2];
389         text.setTextBoxMode(centerPos);
390         text.setAutoDimensioning(autoSize);
391
392         if (autoSize == false || centerPos != 0) {
393             setUserSize[0] = userSize[0];
394             setUserSize[1] = userSize[1];
395         } else {
396             setUserSize[0] = 0.0;
397             setUserSize[1] = 0.0;
398         }
399         text.setTextBox(setUserSize);
400
401         int alignment = align - 1;
402         if (alignment < 0 || alignment > 2) {
403             alignment = 0;
404         }
405
406         text.setAlignment(alignment);
407
408         cloneGraphicContext(iParentsubwinUID, iText);
409         cloneFontContext(iParentsubwinUID, iText);
410
411         text.setBox(isBoxed);
412         text.setLineMode(isLine);
413         text.setFillMode(isFilled);
414
415         if (isForeground) {
416             text.setLineColor(foreground);
417         }
418
419         if (isBackground) {
420             text.setBackground(background);
421         }
422
423         text.setVisible(axes.getVisible());
424         controller.objectCreated(iText);
425         return iText;
426     }
427
428     public final static int createArc(int parent, double x, double y, double h, double w,
429                                       double startAngle, double endAngle,
430                                       int foreground, boolean isForeground, int background, boolean isBackground,
431                                       boolean filled, boolean line) {
432
433         GraphicController controller = GraphicController.getController();
434         int iArc = controller.askObject(Type.ARC);
435
436         Axes axes = (Axes) controller.getObjectFromId(parent);
437         Arc arc = (Arc) controller.getObjectFromId(iArc);
438
439         //set visible false during construction
440         arc.setVisible(false);
441
442         Double[] upperLeftPoint = new Double[3];
443         upperLeftPoint[0] = x;
444         upperLeftPoint[1] = y;
445         upperLeftPoint[2] = 0.0;
446
447         arc.setUpperLeftPoint(upperLeftPoint);
448         arc.setHeight(h);
449         arc.setWidth(w);
450         arc.setStartAngle(startAngle);
451         arc.setEndAngle(endAngle);
452         arc.setArcDrawingMethod(axes.getArcDrawingMethod());
453
454         //clip
455         arc.setClipBox(axes.getClipBox());
456         arc.setClipBoxSet(axes.getClipBoxSet());
457         arc.setClipState(axes.getClipState());
458
459         cloneGraphicContext(parent, iArc);
460
461         arc.setLineMode(line);
462         arc.setFillMode(filled);
463
464         if (isForeground) {
465             arc.setLineColor(foreground);
466         }
467
468         if (isBackground) {
469             arc.setBackground(background);
470         }
471
472         arc.setVisible(axes.getVisible());
473         controller.objectCreated(iArc);
474         controller.setGraphicObjectRelationship(parent, iArc);
475         return iArc;
476     }
477
478     public final static int createAxis(int parent, int dir, int tics, double[] vx, double[] vy, int subint, String format,
479                                        int fontSize, int textColor, int ticsColor, boolean seg) {
480
481         GraphicController controller = GraphicController.getController();
482         int iAxis = controller.askObject(Type.AXIS);
483
484         Axes axes = (Axes) controller.getObjectFromId(parent);
485         Axis axis = (Axis) controller.getObjectFromId(iAxis);
486
487         //clip box
488         axis.setClipBox(axes.getClipBox());
489         axis.setClipBoxSet(false);
490
491         axis.setTicksDirection(dir);
492         axis.setTicksStyle(tics);
493
494         axis.setXTicksCoords(toDouble(vx));
495         axis.setYTicksCoords(toDouble(vy));
496
497         if (format != null && format.equals("") == false) {
498             axis.setFormatn(format);
499         }
500
501         axis.setSubticks(subint);
502         axis.setTicksSegment(seg);
503         cloneGraphicContext(parent, iAxis);
504         cloneFontContext(parent, iAxis);
505
506         Font font = axis.getFont();
507         font.setSize(new Double(fontSize));
508         font.setColor(textColor);
509         axis.setTicksColor(ticsColor);
510
511         controller.objectCreated(iAxis);
512         controller.setGraphicObjectRelationship(parent, iAxis);
513         return iAxis;
514     }
515
516     public static int createCompound(int parent, int[] children) {
517         GraphicController controller = GraphicController.getController();
518         int iCompound = controller.askObject(Type.COMPOUND);
519         controller.objectCreated(iCompound);
520
521         GraphicObject obj = controller.getObjectFromId(parent);
522         for (int i = 0 ; i < children.length ; i++) {
523             controller.setGraphicObjectRelationship(iCompound, children[i]);
524         }
525
526         controller.setGraphicObjectRelationship(parent, iCompound);
527         controller.setProperty(iCompound, GraphicObjectProperties.__GO_VISIBLE__, obj.getVisible());
528         return iCompound;
529     }
530
531     public static int createCompoundSeq(int parent, int childrenCount) {
532         GraphicController controller = GraphicController.getController();
533         int iCompound = controller.askObject(Type.COMPOUND);
534         controller.objectCreated(iCompound);
535         GraphicObject axes = controller.getObjectFromId(parent);
536
537         Integer[] children = axes.getChildren();
538
539         /*
540          * Remove the last "number" created objects (located at the children list's head)
541          * and add them to the compound in the same order
542          */
543         for (int i = 0 ; i < childrenCount ; i++) {
544             /*
545              * Set the parent-child relationship between the Compound and each aggregated object.
546              * Children are added to the Compound from the least recent to the most recent, to
547              * preserve their former ordering.
548              */
549             controller.setGraphicObjectRelationship(iCompound, children[childrenCount - i - 1]);
550         }
551
552         controller.setGraphicObjectRelationship(parent, iCompound);
553
554         /*
555          * visibility is obtained from the parent Figure, whereas it is retrieved from the
556          * parent Axes in ConstructCompound.
557          * To be made consistent.
558          */
559         Figure fig = (Figure)controller.getObjectFromId(axes.getParentFigure());
560         controller.setProperty(iCompound, GraphicObjectProperties.__GO_VISIBLE__, fig.getVisible());
561         return iCompound;
562     }
563
564     public static int createFec(int parent, double[] zminmax, int[] colminmax, int[] colout, boolean with_mesh) {
565
566         GraphicController controller = GraphicController.getController();
567         int iFec = controller.askObject(Type.FEC);
568         Axes axes = (Axes) controller.getObjectFromId(parent);
569         Fec fec = (Fec) controller.getObjectFromId(iFec);
570
571
572         fec.setZBounds(toDouble(zminmax));
573         fec.setColorRange(toInteger(colminmax));
574         fec.setOutsideColor(toInteger(colout));
575
576         fec.setVisible(axes.getVisible());
577
578         //clip box
579         fec.setClipBox(axes.getClipBox());
580         fec.setClipBoxSet(axes.getClipBoxSet());
581         fec.setClipState(axes.getClipState());
582
583         cloneGraphicContext(parent, iFec);
584
585         fec.setLineMode(with_mesh);
586
587         controller.objectCreated(iFec);
588         return iFec;
589     }
590
591     public static int createGrayplot(int parent, int type, double[] pvecx, int n1, int n2) {
592
593         int[] objectTypes = new int[] {GraphicObjectProperties.__GO_GRAYPLOT__, GraphicObjectProperties.__GO_MATPLOT__, GraphicObjectProperties.__GO_MATPLOT__};
594
595         GraphicController controller = GraphicController.getController();
596         int iPlot = controller.askObject(GraphicObject.getTypeFromName(objectTypes[type]));
597         Axes axes = (Axes) controller.getObjectFromId(parent);
598         Imageplot plot = (Imageplot) controller.getObjectFromId(iPlot);
599
600         if (type == 2) { //Matplot1
601             Double[] data = new Double[pvecx.length];
602             for (int i = 0 ; i < pvecx.length ; i++) {
603                 data[i] = pvecx[i];
604             }
605             plot.setTranslate(data);
606
607             Double[] scale = new Double[2];
608             scale[0] = (pvecx[2] - pvecx[0]) / (n2 - 1.0);
609             scale[1] = (pvecx[3] - pvecx[1]) / (n1 - 1.0);
610             plot.setScale(scale);
611         }
612
613         plot.setVisible(axes.getVisible());
614
615         //clip box
616         plot.setClipBox(axes.getClipBox());
617         plot.setClipBoxSet(axes.getClipBoxSet());
618         plot.setClipState(axes.getClipState());
619
620         cloneGraphicContext(parent, iPlot);
621         controller.objectCreated(iPlot);
622         return iPlot;
623     }
624
625
626     public static int createPolyline(int parent, boolean closed, int plot,
627                                      int foreground, boolean isForeground, int[] background, int mark_style, boolean isMarkStyle,
628                                      int mark_foreground, boolean isMarkForeground,
629                                      int mark_background, boolean isMarkBackground,
630                                      boolean isline, boolean isfilled, boolean ismark, boolean isinterp) {
631
632         GraphicController controller = GraphicController.getController();
633         int iPoly = controller.askObject(Type.POLYLINE);
634         Axes axes = (Axes) controller.getObjectFromId(parent);
635         Polyline poly = (Polyline) controller.getObjectFromId(iPoly);
636
637         poly.setVisible(false);
638
639         //clip box
640         poly.setClipBox(axes.getClipBox());
641         poly.setClipBoxSet(axes.getClipBoxSet());
642         poly.setClipState(axes.getClipState());
643
644         poly.setClosed(closed);
645         poly.setPolylineStyle(plot);
646
647         cloneGraphicContext(parent, iPoly);
648
649         poly.setMarkMode(ismark);
650         poly.setLineMode(isline);
651         poly.setFillMode(isfilled);
652         poly.setInterpColorMode(isinterp);
653
654         if (isForeground) {
655             poly.setLineColor(foreground);
656         }
657
658         if (background.length != 0) {
659             if (isinterp) {
660                 /* 3 or 4 values to store */
661                 Integer[] color = new Integer[background.length];
662                 for (int i = 0 ; i < background.length ; i++) {
663                     color[i] = background[i];
664                 }
665
666                 poly.setInterpColorVector(color);
667             } else {
668                 poly.setBackground(background[0]);
669             }
670         }
671
672         if (isMarkStyle) {
673             poly.setMarkStyle(mark_style);
674         }
675
676         if (isMarkForeground) {
677             poly.setMarkForeground(mark_foreground);
678         }
679
680         if (isMarkBackground) {
681             poly.setMarkBackground(mark_background);
682         }
683
684         poly.setVisible(true);
685         controller.objectCreated(iPoly);
686         return iPoly;
687     }
688
689     public static int createLegend(int parent, String[] text, int[] handles) {
690         GraphicController controller = GraphicController.getController();
691         Axes axes = (Axes) controller.getObjectFromId(parent);
692
693         if (axes.getHasLegendChild()) {
694             controller.removeRelationShipAndDelete(axes.getLegendChild());
695         }
696
697
698         int iLeg = controller.askObject(Type.LEGEND);
699         Legend leg = (Legend)controller.getObjectFromId(iLeg);
700
701         leg.setParent(parent);
702         leg.setVisible(axes.getVisible());
703
704         int count = handles.length;
705         Integer[] textDims = new Integer[2];
706         textDims[0] = count;
707         textDims[1] = 1;
708
709         leg.setTextArrayDimensions(textDims);
710         leg.setTextStrings(text);
711
712         /*
713          * Links are ordered from most recent to least recent,
714          * as their referred-to Polylines in the latter's parent Compound object.
715          */
716
717         ArrayList<Integer> links = new ArrayList<Integer>();
718         for (int i = count - 1 ; i >= 0 ; i--) {
719             links.add(count - i - 1, handles[i]);
720         }
721
722         leg.setLinks(links);
723
724         leg.setClipBoxSet(false);
725         leg.setClipState(0); //OFF
726         leg.setClipBox(axes.getClipBox());
727
728         cloneGraphicContext(parent,  iLeg);
729         cloneFontContext(parent, iLeg);
730
731         leg.setFillMode(true);
732
733         controller.objectCreated(iLeg);
734         leg.setParent(0);
735         controller.setGraphicObjectRelationship(parent, iLeg);
736         return iLeg;
737     }
738
739     public static int createSegs(int parent, double[] vx, double[] vy, double[] vz, boolean isVZ,
740                                  int[] style, double arsize) {
741         GraphicController controller = GraphicController.getController();
742         Axes axes = (Axes) controller.getObjectFromId(parent);
743         int iSegs = controller.askObject(Type.SEGS);
744         Segs segs = (Segs)controller.getObjectFromId(iSegs);
745
746         segs.setVisible(axes.getVisible());
747
748         //clip box
749         segs.setClipBox(axes.getClipBox());
750         segs.setClipBoxSet(axes.getClipBoxSet());
751         segs.setClipState(axes.getClipState());
752
753         /* Segs: Nbr1/2 arrows, Nbr1 is the number of endpoints */
754         int numberArrows = vx.length / 2;
755
756         segs.setNumberArrows(numberArrows);
757         segs.setArrowSize(arsize);
758
759         Double[] arrowCoords = new Double[3 * numberArrows];
760         for (int i = 0 ; i < numberArrows ; i++) {
761             arrowCoords[3 * i] = vx[2 * i];
762             arrowCoords[3 * i + 1] = vy[2 * i];
763             if (isVZ) {
764                 arrowCoords[3 * i + 2] = vz[2 * i];
765             } else {
766                 arrowCoords[3 * i + 2] = 0.0;
767             }
768         }
769
770         segs.setBase(arrowCoords);
771
772         for (int i = 0 ; i < numberArrows ; i++) {
773             arrowCoords[3 * i] = vx[2 * i + 1];
774             arrowCoords[3 * i + 1] = vy[2 * i + 1];
775             if (isVZ) {
776                 arrowCoords[3 * i + 2] = vz[2 * i + 1];
777             } else {
778                 arrowCoords[3 * i + 2] = 0.0;
779             }
780         }
781
782         segs.setDirection(arrowCoords);
783         segs.setColors(toInteger(style));
784
785         cloneGraphicContext(parent, iSegs);
786         controller.objectCreated(iSegs);
787         controller.setGraphicObjectRelationship(parent, iSegs);
788         return iSegs;
789     }
790
791     public static int createChamp(int parent, double[] vx, double[] vy,
792                                   double[] vfx, double[] vfy, double arsize, boolean typeofchamp) {
793
794         GraphicController controller = GraphicController.getController();
795         Axes axes = (Axes) controller.getObjectFromId(parent);
796
797         int iChamp = controller.askObject(Type.CHAMP);
798         Champ champ = (Champ)controller.getObjectFromId(iChamp);
799
800         champ.setVisible(axes.getVisible());
801
802         //clip box
803         champ.setClipBox(axes.getClipBox());
804         champ.setClipBoxSet(axes.getClipBoxSet());
805         champ.setClipState(axes.getClipState());
806
807         int numberArrows = vx.length * vy.length;
808         champ.setNumberArrows(numberArrows);
809
810         Integer[] dimensions = new Integer[] {vx.length, vy.length};
811         champ.setDimensions(dimensions);
812
813         champ.setArrowSize(arsize);
814
815         Double[] arrowCoords = new Double[3 * numberArrows];
816
817         //convert vx Double
818         Double[] temp = new Double[vx.length];
819         for (int i =  0 ; i < vx.length ; i++) {
820             temp[i] = vx[i];
821         }
822         champ.setBaseX(temp);
823
824         //convert vy Double
825         temp = new Double[vy.length];
826         for (int i =  0 ; i < vy.length ; i++) {
827             temp[i] = vy[i];
828         }
829         champ.setBaseY(temp);
830
831         for (int i = 0 ; i < numberArrows ; i++) {
832             arrowCoords[3 * i] = vfx[i];
833             arrowCoords[3 * i + 1] = vfy[i];
834             arrowCoords[3 * i + 2] = 0.0;
835         }
836
837         champ.setDirection(arrowCoords);
838         champ.setColored(typeofchamp);
839
840         cloneGraphicContext(parent, iChamp);
841
842         controller.objectCreated(iChamp);
843         controller.setGraphicObjectRelationship(parent, iChamp);
844
845         return iChamp;
846     }
847
848     public static int createSurface(int parent, int typeof3d, int colorFlag, int colorMode) {
849         GraphicController controller = GraphicController.getController();
850         Axes axes = (Axes) controller.getObjectFromId(parent);
851
852         int iSurf = controller.askObject(GraphicObject.getTypeFromName(typeof3d));
853         Surface surf = (Surface) controller.getObjectFromId(iSurf);
854
855         surf.setVisible(axes.getVisible());
856
857         //clip box
858         surf.setClipBox(axes.getClipBox());
859         surf.setClipBoxSet(axes.getClipBoxSet());
860         surf.setClipState(axes.getClipState());
861
862         surf.setColorFlag(colorFlag);
863         surf.setColorMode(colorMode);
864
865         surf.setHiddenColor(axes.getHiddenColor());
866
867         surf.setSurfaceMode(true);
868         cloneGraphicContext(parent, iSurf);
869         controller.objectCreated(iSurf);
870         controller.setGraphicObjectRelationship(parent, iSurf);
871
872         return iSurf;
873     }
874
875     public static void initSubWinTo3d(int iSubwin, String legend, boolean isLegend,
876                                       int[] flag, double alpha, double theta,
877                                       double[] ebox, double[] x, double[] y, double[] z) {
878         GraphicController controller = GraphicController.getController();
879
880         // Force 3d view
881         controller.setProperty(iSubwin, GraphicObjectProperties.__GO_VIEW__, 1);
882
883         if (isLegend) {
884             StringTokenizer strTok =  new StringTokenizer(legend, "@");
885             int iToken = strTok.countTokens();
886             if (iToken > 0) {
887                 //X
888                 String str[] = new String[] {strTok.nextToken()};
889                 Integer label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_LABEL__);
890                 Integer[] dims = new Integer[] {1, 1};
891                 controller.setProperty(label, GraphicObjectProperties.__GO_TEXT_ARRAY_DIMENSIONS__, dims);
892                 controller.setProperty(label, GraphicObjectProperties.__GO_TEXT_STRINGS__, str);
893             }
894
895             if (iToken > 1) {
896                 //Y
897                 String str[] = new String[] {strTok.nextToken()};
898                 Integer label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_LABEL__);
899                 Integer[] dims = new Integer[] {1, 1};
900                 controller.setProperty(label, GraphicObjectProperties.__GO_TEXT_ARRAY_DIMENSIONS__, dims);
901                 controller.setProperty(label, GraphicObjectProperties.__GO_TEXT_STRINGS__, str);
902             }
903
904             if (iToken > 2) {
905                 //Z
906                 String str[] = new String[] {strTok.nextToken()};
907                 Integer label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_LABEL__);
908                 Integer[] dims = new Integer[] {1, 1};
909                 controller.setProperty(label, GraphicObjectProperties.__GO_TEXT_ARRAY_DIMENSIONS__, dims);
910                 controller.setProperty(label, GraphicObjectProperties.__GO_TEXT_STRINGS__, str);
911             }
912         }
913
914         // Force psubwin->logflags to linear
915         controller.setProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_LOG_FLAG__, false);
916         controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_LOG_FLAG__, false);
917         controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_LOG_FLAG__, false);
918
919         Boolean firstPlot = (Boolean) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_FIRST_PLOT__);
920
921         if (firstPlot && (flag[2] == 0 || flag[2] == 1)) {
922             /* Nothing to do */
923         } else {
924             if (flag[2] == 0 || flag[2] == 1) {
925                 if (firstPlot) {
926                     controller.setProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_VISIBLE__, false);
927                     controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_VISIBLE__, false);
928                     controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_VISIBLE__, false);
929
930                     // 0: OFF
931                     controller.setProperty(iSubwin, GraphicObjectProperties.__GO_BOX_TYPE__, 0);
932
933                     Integer label = 0;
934                     label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_LABEL__);
935                     controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, false);
936                     label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_LABEL__);
937                     controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, false);
938                     label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_LABEL__);
939                     controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, false);
940                 }
941             } else if (flag[2] == 2) {
942                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_VISIBLE__, false);
943                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_VISIBLE__, false);
944                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_VISIBLE__, false);
945
946                 //2: HIDDEN_AXES
947                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_BOX_TYPE__, 2);
948
949                 Integer label = 0;
950                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_LABEL__);
951                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, false);
952                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_LABEL__);
953                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, false);
954                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_LABEL__);
955                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, false);
956
957             } else if (flag[2] == 3) {
958                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_VISIBLE__, false);
959                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_VISIBLE__, false);
960                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_VISIBLE__, false);
961
962                 //1: ON
963                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_BOX_TYPE__, 1);
964
965                 Integer label = 0;
966                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_LABEL__);
967                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, true);
968                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_LABEL__);
969                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, true);
970                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_LABEL__);
971                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, true);
972
973             } else if (flag[2] == 4) {
974                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_VISIBLE__, true);
975                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_VISIBLE__, true);
976                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_VISIBLE__, true);
977
978                 //1: ON
979                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_BOX_TYPE__, 1);
980
981                 Integer label = 0;
982                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_X_AXIS_LABEL__);
983                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, true);
984                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Y_AXIS_LABEL__);
985                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, true);
986                 label = (Integer) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_Z_AXIS_LABEL__);
987                 controller.setProperty(label, GraphicObjectProperties.__GO_VISIBLE__, true);
988             }
989         }
990
991         Double[] rotationAngles = new Double[] {alpha, theta};
992         controller.setProperty(iSubwin, GraphicObjectProperties.__GO_ROTATION_ANGLES__, rotationAngles);
993
994         Double[] dataBounds = (Double[]) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_DATA_BOUNDS__);
995         Boolean autoScale = (Boolean) controller.getProperty(iSubwin, GraphicObjectProperties.__GO_AUTO_SCALE__);
996
997         Double rect[] = new Double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
998         if (autoScale) {
999             // compute and merge new specified bounds with data bounds
1000             switch (flag[1]) {
1001                 case 0 :
1002                     break;
1003                 case 1 :
1004                 case 3 :
1005                 case 5 :
1006                 case 7 :
1007                     rect[0] = ebox[0]; // xmin
1008                     rect[1] = ebox[1]; // xmax
1009                     rect[2] = ebox[2]; // ymin
1010                     rect[3] = ebox[3]; // ymax
1011                     rect[4] = ebox[4]; // zmin
1012                     rect[5] = ebox[5]; // zmax
1013                     break;
1014                 case 2 :
1015                 case 4 :
1016                 case 6 :
1017                 case 8 :
1018                     double[] res = getDrect(x, rect[0], rect[1], dataBounds[0], dataBounds[1]);
1019                     rect[0] = res[0];
1020                     rect[1] = res[1];
1021
1022                     res = getDrect(y, rect[2], rect[3], dataBounds[2], dataBounds[3]);
1023                     rect[2] = res[0];
1024                     rect[3] = res[1];
1025
1026                     res = getDrect(z, rect[4], rect[5], dataBounds[4], dataBounds[5]);
1027                     rect[4] = res[0];
1028                     rect[5] = res[1];
1029                     break;
1030             }
1031
1032             if (firstPlot == false) {
1033                 rect[0] = Math.min(dataBounds[0], rect[0]); // xmin
1034                 rect[1] = Math.max(dataBounds[1], rect[1]); // xmax
1035                 rect[2] = Math.min(dataBounds[2], rect[2]); // ymin
1036                 rect[3] = Math.max(dataBounds[3], rect[3]); // ymax
1037                 rect[4] = Math.min(dataBounds[4], rect[4]); // zmin
1038                 rect[5] = Math.max(dataBounds[5], rect[5]); // zmax
1039             }
1040
1041             if (flag[1] != 0) {
1042                 controller.setProperty(iSubwin, GraphicObjectProperties.__GO_DATA_BOUNDS__, rect);
1043             }
1044         }
1045
1046         if (flag[1] != 0) {
1047             boolean isoview = (flag[1] == 3 || flag[1] == 4 || flag[1] == 5 || flag[1] == 6);
1048             controller.setProperty(iSubwin, GraphicObjectProperties.__GO_ISOVIEW__, isoview);
1049         }
1050     }
1051
1052     private static double[] getDrect(double[] x, double min, double max, double defaultMin, double defaultMax) {
1053         double refMax = Double.NEGATIVE_INFINITY;
1054         double refMin = Double.POSITIVE_INFINITY;
1055         boolean isInfinite = true;
1056
1057         for (int i = 0 ; i < x.length ; i++) {
1058             Double tmp = x[i];
1059             if (tmp.isInfinite() == false && tmp.isNaN() == false) {
1060                 refMin = Math.min(refMin, tmp);
1061                 refMax = Math.max(refMax, tmp);
1062                 isInfinite = false;
1063             }
1064         }
1065
1066         if (isInfinite) {
1067             refMin = defaultMin;
1068             refMax = defaultMax;
1069         }
1070
1071         return new double[] {refMin, refMax};
1072     }
1073
1074     private static boolean  isValidType(int type) {
1075         return type >= 0 && type <= 1;
1076     }
1077
1078     private static boolean isValidColor(double[] color) {
1079         return (color[0] >= 0.0 && color[0] <= 1.0)
1080                && (color[1] >= 0.0 && color[1] <= 1.0)
1081                && (color[2] >= 0.0 && color[2] <= 1.0);
1082     }
1083
1084     public static int createLight(int parent, int type, boolean visible, double[] pos,
1085                                   double[] dir, double[] ambient, double[] diffuse, double[] specular) {
1086         GraphicController controller = GraphicController.getController();
1087
1088         int iLight = controller.askObject(Type.LIGHT);
1089         Light light = (Light) controller.getObjectFromId(iLight);
1090
1091         light.setVisible(visible);
1092         if (isValidType(type)) {
1093             light.setLightType(LightType.intToEnum(type));
1094         }
1095
1096         if (pos.length == 3) {
1097             light.setPosition(toDouble(pos));
1098         }
1099
1100         if (dir.length == 3) {
1101             light.setDirection(toDouble(dir));
1102         }
1103         if (ambient.length == 3) {
1104             if (isValidColor(ambient)) {
1105                 light.setAmbientColor(toDouble(ambient));
1106             }
1107         }
1108         if (diffuse.length == 3) {
1109             if (isValidColor(diffuse)) {
1110                 light.setDiffuseColor(toDouble(diffuse));
1111             }
1112         }
1113         if (specular.length == 3) {
1114             if (isValidColor(specular)) {
1115                 light.setSpecularColor(toDouble(specular));
1116             }
1117         }
1118
1119         controller.objectCreated(iLight);
1120         // Set light's parent
1121         controller.setGraphicObjectRelationship(parent, iLight);
1122         return iLight;
1123     }
1124
1125     private static Double[] toDouble(double[] var) {
1126         Double[] ret = new Double[var.length];
1127         for (int i = 0 ; i < var.length ; i++) {
1128             ret[i] = var[i];
1129         }
1130
1131         return ret;
1132     }
1133
1134     private static Integer[] toInteger(int[] var) {
1135         Integer[] ret = new Integer[var.length];
1136         for (int i = 0 ; i < var.length ; i++) {
1137             ret[i] = var[i];
1138         }
1139
1140         return ret;
1141     }
1142 }
1143