Revert "Bugs 2547 ...
[scilab.git] / scilab / modules / renderer / src / java / org / scilab / modules / renderer / JoGLView / mark / MarkSpriteFactory.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009-2010 - DIGITEO - Pierre Lando
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  */
11
12 package org.scilab.modules.renderer.JoGLView.mark;
13
14 import org.scilab.forge.scirenderer.shapes.appearance.Appearance;
15 import org.scilab.forge.scirenderer.shapes.appearance.Color;
16 import org.scilab.forge.scirenderer.texture.Texture;
17 import org.scilab.forge.scirenderer.texture.TextureDrawer;
18 import org.scilab.forge.scirenderer.texture.TextureDrawingTools;
19 import org.scilab.forge.scirenderer.texture.TextureManager;
20 import org.scilab.modules.graphic_objects.contouredObject.Mark;
21 import org.scilab.modules.graphic_objects.figure.ColorMap;
22 import org.scilab.modules.renderer.JoGLView.util.ColorFactory;
23
24 import java.awt.Dimension;
25
26 /**
27  * @author Pierre Lando
28  */
29 public class MarkSpriteFactory {
30
31     /**
32      * Return a mark sprite corresponding to the given scilab mark.
33      *
34      *
35      * @param spriteManager the current sprite manager.
36      * @param mark the scilab mark.
37      * @param colorMap the scilab color map.
38      * @return a mark sprite corresponding to the given scilab mark.
39      */
40     public static Texture getMarkSprite(TextureManager spriteManager, Mark mark, ColorMap colorMap) {
41         int finalSize;
42
43         /**
44          * Compute the sprite pixel size.
45          */
46         if (mark.getMarkSizeUnit() == Mark.MarkSizeUnitType.TABULATED) {
47             // Special case for compatibility with older versions
48             // dot mark with tabulated size of 0 is a single pixel
49             // drawn in pixel size instead of tabulated size
50             if (mark.getStyle() == 0 && mark.getSize() == 0) {
51                 finalSize = 1;
52             } else {
53                 finalSize = (8 + 2 * mark.getSize());
54             }
55         } else {
56             finalSize = mark.getSize();
57         }
58
59         Texture sprite = spriteManager.createTexture();
60         sprite.setDrawer(getSpriteDrawer(mark, finalSize, colorMap));
61
62         return sprite;
63     }
64
65     /**
66      * Return the sprite drawer corresponding to the given mark.
67      * @param mark the mark to draw.
68      * @param finalSize the final mark size.
69      * @param colorMap the scilab colormap to use.
70      * @return the sprite drawer corresponding to the given mark.
71      */
72     private static TextureDrawer getSpriteDrawer(Mark mark, int finalSize, ColorMap colorMap) {
73
74         final Appearance appearance = new Appearance();
75         Color backgroundColor = ColorFactory.createColor(colorMap, mark.getBackground());
76         Color foregroundColor = ColorFactory.createColor(colorMap, mark.getForeground());
77
78         if (mark.getBackground() != 0) {
79             appearance.setFillColor(backgroundColor);
80         } else {
81             appearance.setFillColor(new Color(0.0f, 0.0f, 0.0f, 0.0f));
82         }
83         appearance.setLineColor(foregroundColor);
84
85         if (finalSize != 1) {
86             switch (mark.getStyle()) {
87                 case  0:
88                     return new DotSpriteDrawer(foregroundColor, finalSize);
89                 case  1:
90                     return new PlusSpriteDrawer(appearance, finalSize);
91                 case  2:
92                     return new CrossSpriteDrawer(appearance, finalSize);
93                 case  3:
94                     return new StarSpriteDrawer(appearance, finalSize);
95                 case  4:
96                     return new FilledDiamondSpriteDrawer(foregroundColor, finalSize);
97                 case  5:
98                     return new DiamondSpriteDrawer(appearance, finalSize);
99                 case  6:
100                     return new TriangleUpSpriteDrawer(appearance, finalSize);
101                 case  7:
102                     return new TriangleDownSpriteDrawer(appearance, finalSize);
103                 case  8:
104                     return new DiamondPlusSpriteDrawer(appearance, finalSize);
105                 case  9:
106                     return new CircleSpriteDrawer(appearance, finalSize);
107                 case 10:
108                     return new AsteriskSpriteDrawer(appearance, finalSize);
109                 case 11:
110                     return new SquareSpriteDrawer(appearance, finalSize);
111                 case 12:
112                     return new TriangleRightSpriteDrawer(appearance, finalSize);
113                 case 13:
114                     return new TriangleLeftSpriteDrawer(appearance, finalSize);
115                 case 14:
116                     return new PentagramSpriteDrawer(appearance, finalSize);
117
118                 default:
119                     return new PlusSpriteDrawer(appearance, finalSize);
120             }
121         } else {
122             return new PlusSpriteDrawer(appearance, finalSize);
123         }
124     }
125
126     /**
127      * Abstract class for all scilab mark sprites.
128      */
129     private static abstract class ScilabSpriteDrawer implements TextureDrawer {
130         protected final Appearance appearance;
131         protected final int size;
132
133         public ScilabSpriteDrawer(Appearance appearance, int size) {
134             this.appearance = appearance;
135             this.size = size;
136         }
137
138         @Override
139         public OriginPosition getOriginPosition() {
140             return TextureDrawer.OriginPosition.CENTER;
141         }
142
143         @Override
144         public Dimension getTextureSize() {
145
146             /** Add a margin such that (0, 0) was pixel aligned. */
147             int margin;
148             if (size % 2 == 0) {
149                 margin = 3;
150             } else {
151                 margin = 2;
152             }
153             return new Dimension(size + margin, size + margin);
154         }
155     }
156
157     /**
158      * Dot sprite
159      * Scilab ID = 0
160      */
161     private static class DotSpriteDrawer extends ScilabSpriteDrawer {
162         private final Color color;
163
164         public DotSpriteDrawer(Color color, int size) {
165             super(null, size);
166             this.color = color;
167         }
168
169         @Override
170         public void draw(TextureDrawingTools textureDrawingTools) {
171             textureDrawingTools.fillDisc(0, 0, size, color);
172         }
173     }
174
175     /**
176      * Plus sprite
177      * Scilab ID = 1
178      */
179     private static class PlusSpriteDrawer extends ScilabSpriteDrawer {
180         private final int[] coordinate1;
181         private final int[] coordinate2;
182
183         public PlusSpriteDrawer(Appearance appearance, int size) {
184             super(appearance, size);
185             int r = size / 2;
186             coordinate1 = new int[] { -r, 0, r, 0};
187             coordinate2 = new int[] { 0, -r, 0, r};
188         }
189
190         @Override
191         public void draw(TextureDrawingTools textureDrawingTools) {
192             textureDrawingTools.drawPolyline(coordinate1, appearance);
193             textureDrawingTools.drawPolyline(coordinate2, appearance);
194         }
195     }
196
197     /**
198      * Cross sprite
199      * Scilab ID = 2
200      */
201     private static class CrossSpriteDrawer extends ScilabSpriteDrawer {
202         private final int[] coordinate1;
203         private final int[] coordinate2;
204
205         public CrossSpriteDrawer(Appearance appearance, int size) {
206             super(appearance, size);
207             int r = (int) (size * Math.sqrt(2.0) / 2.0);
208             coordinate1 = new int[] { -r, -r, r,  r};
209             coordinate2 = new int[] { -r,  r, r, -r};
210         }
211
212         @Override
213         public void draw(TextureDrawingTools textureDrawingTools) {
214             textureDrawingTools.drawPolyline(coordinate1, appearance);
215             textureDrawingTools.drawPolyline(coordinate2, appearance);
216         }
217     }
218
219     /**
220      * Star sprite
221      * Scilab ID = 3
222      */
223     private static class StarSpriteDrawer extends ScilabSpriteDrawer {
224         private final int[] coordinate1;
225         private final int[] coordinate2;
226
227         public StarSpriteDrawer(Appearance appearance, int size) {
228             super(appearance, size);
229             int r = size / 2;
230             coordinate1 = new int[] { -r, 0, r, 0};
231             coordinate2 = new int[] { 0, -r, 0, r};
232         }
233
234         @Override
235         public void draw(TextureDrawingTools textureDrawingTools) {
236             textureDrawingTools.fillDisc(0, 0, size, appearance.getFillColor());
237             textureDrawingTools.drawCircle(0, 0, size, appearance);
238             textureDrawingTools.drawPolyline(coordinate1, appearance);
239             textureDrawingTools.drawPolyline(coordinate2, appearance);
240         }
241     }
242
243     /**
244      * Filled diamond sprite
245      * Scilab ID = 4
246      */
247     private static class FilledDiamondSpriteDrawer extends ScilabSpriteDrawer {
248         private final Appearance appearance;
249         private final int[] coordinates;
250
251         public FilledDiamondSpriteDrawer(Color color, int size) {
252             super(null, size);
253             int r = size / 2;
254
255             appearance = new Appearance();
256             appearance.setFillColor(color);
257             appearance.setLineColor(color);
258
259             coordinates = new int[] { -r, 0, 0, -r, +r, 0, 0, +r};
260         }
261
262         @Override
263         public void draw(TextureDrawingTools textureDrawingTools) {
264             textureDrawingTools.fillPolygon(coordinates, appearance);
265         }
266     }
267
268     /**
269      * Abstract class for all the triangle mark sprites.
270      */
271     private static abstract class TriangleSpriteDrawer extends ScilabSpriteDrawer {
272         protected final int r;
273         protected final int basex;
274         protected final int basey;
275         protected int[] coordinates;
276
277         public TriangleSpriteDrawer(Appearance appearance, int size) {
278             super(appearance, size);
279             double tmpr = (double)(size / 2);
280
281             r = (int) (tmpr);
282
283             basex = (int) (tmpr * 0.5 * Math.sqrt(3));
284             basey = (int) (tmpr * 0.5);
285         }
286
287         @Override
288         public void draw(TextureDrawingTools textureDrawingTools) {
289             textureDrawingTools.fillPolygon(coordinates, appearance);
290         }
291     }
292
293     /**
294      * Diamond sprite
295      * Scilab ID = 5
296      */
297     private static class DiamondSpriteDrawer extends ScilabSpriteDrawer {
298         protected final int[] coordinates;
299
300         public DiamondSpriteDrawer(Appearance appearance, int size) {
301             super(appearance, size);
302             int r = size / 2;
303
304             coordinates = new int[] { -r, 0, 0, -r, r, 0, 0, r};
305         }
306
307         @Override
308         public void draw(TextureDrawingTools textureDrawingTools) {
309             textureDrawingTools.fillPolygon(coordinates, appearance);
310         }
311     }
312
313     /**
314      * Triangle up sprite
315      * Scilab ID = 6
316      */
317     private static class TriangleUpSpriteDrawer extends TriangleSpriteDrawer {
318         public TriangleUpSpriteDrawer(Appearance appearance, int size) {
319             super(appearance, size);
320
321             coordinates = new int[] { -basex, basey, 0, -r, basex, basey};
322         }
323     }
324
325     /**
326      * Triangle down sprite
327      * Scilab ID = 7
328      */
329     private static class TriangleDownSpriteDrawer extends TriangleSpriteDrawer {
330         public TriangleDownSpriteDrawer(Appearance appearance, int size) {
331             super(appearance, size);
332
333             coordinates = new int[] { -basex, -basey, basex, -basey, 0, r};
334         }
335     }
336
337     /**
338      * Diamond plus sprite
339      * Scilab ID = 8
340      */
341     private static class DiamondPlusSpriteDrawer extends DiamondSpriteDrawer {
342         private final int[] coordinate1;
343         private final int[] coordinate2;
344
345         public DiamondPlusSpriteDrawer(Appearance appearance, int size) {
346             super(appearance, size);
347             coordinate1 = new int[] {coordinates[0], coordinates[1], coordinates[4], coordinates[5]};
348             coordinate2 = new int[] {coordinates[2], coordinates[3], coordinates[6], coordinates[7]};
349         }
350
351         @Override
352         public void draw(TextureDrawingTools textureDrawingTools) {
353             super.draw(textureDrawingTools);
354             textureDrawingTools.drawPolyline(coordinate1, appearance);
355             textureDrawingTools.drawPolyline(coordinate2, appearance);
356         }
357     }
358
359     /**
360      * Circle sprite
361      * Scilab ID = 9
362      */
363     private static class CircleSpriteDrawer extends ScilabSpriteDrawer {
364
365         public CircleSpriteDrawer(Appearance appearance, int size) {
366             super(appearance, size);
367         }
368
369         @Override
370         public void draw(TextureDrawingTools textureDrawingTools) {
371             textureDrawingTools.fillDisc(0, 0, size, appearance.getFillColor());
372             textureDrawingTools.drawCircle(0, 0, size, appearance);
373         }
374     }
375
376     /**
377      * Asterisk sprite
378      * Scilab ID = 10
379      */
380     private static class AsteriskSpriteDrawer extends ScilabSpriteDrawer {
381         private final int[] coordinate1;
382         private final int[] coordinate2;
383         private final int[] coordinate3;
384         private final int[] coordinate4;
385
386         public AsteriskSpriteDrawer(Appearance appearance, int size) {
387             super(appearance, size);
388             int r = size / 2;
389             coordinate1 = new int[] { -r, 0, r, 0};
390             coordinate2 = new int[] { 0, -r, 0, r};
391             r = (int) (r * Math.sqrt(2.0) / 2.0);
392             coordinate3 = new int[] { -r, -r, r, r};
393             coordinate4 = new int[] { -r, r, +r, -r};
394         }
395
396         @Override
397         public void draw(TextureDrawingTools textureDrawingTools) {
398             textureDrawingTools.drawPolyline(coordinate1, appearance);
399             textureDrawingTools.drawPolyline(coordinate2, appearance);
400             textureDrawingTools.drawPolyline(coordinate3, appearance);
401             textureDrawingTools.drawPolyline(coordinate4, appearance);
402         }
403     }
404
405     /**
406      * Square sprite
407      * Scilab ID = 11
408      */
409     private static class SquareSpriteDrawer extends ScilabSpriteDrawer {
410         private final int[] coordinate;
411
412         public SquareSpriteDrawer(Appearance appearance, int size) {
413             super(appearance, size);
414             int r = size / 2;
415             coordinate = new int[] { -r, -r, +r, -r, +r, +r, -r, +r};
416         }
417
418         @Override
419         public void draw(TextureDrawingTools textureDrawingTools) {
420             textureDrawingTools.fillPolygon(coordinate, appearance);
421         }
422     }
423
424     /**
425      * Triangle right sprite
426      * Scilab ID = 12
427      */
428     private static class TriangleRightSpriteDrawer extends TriangleSpriteDrawer {
429         public TriangleRightSpriteDrawer(Appearance appearance, int size) {
430             super(appearance, size);
431
432             coordinates = new int[] { -basey, basex, -basey, -basex, r, 0};
433         }
434     }
435
436     /**
437      * Triangle left sprite
438      * Scilab ID = 13
439      */
440     private static class TriangleLeftSpriteDrawer extends TriangleSpriteDrawer {
441         public TriangleLeftSpriteDrawer(Appearance appearance, int size) {
442             super(appearance, size);
443
444             coordinates = new int[] {basey, basex, -r, 0, basey, -basex};
445         }
446     }
447
448     /**
449      * Pentagram sprite (five-pointed star)
450      * Scilab ID = 14
451      */
452     private static class PentagramSpriteDrawer extends ScilabSpriteDrawer {
453         private final int[] coordinates;
454
455         private static final double COS_PI_OVER_10 = Math.cos(Math.PI / 10.0);
456         private static final double SIN_PI_OVER_10 = Math.sin(Math.PI / 10.0);
457         private static final double COS_3_PI_OVER_10 = Math.cos(3.0 * Math.PI / 10.0);
458         private static final double SIN_3_PI_OVER_10 = Math.sin(3.0 * Math.PI / 10.0);
459
460         public PentagramSpriteDrawer(Appearance appearance, int size) {
461             super(appearance, size);
462
463             int r = size / 2;
464
465             double tmpy = SIN_PI_OVER_10 * (double)r;
466
467             int x = (int)(COS_PI_OVER_10 * (double)r);
468             int y = (int)(SIN_PI_OVER_10 * (double)r);
469
470             int x2 = (int)(COS_3_PI_OVER_10 * (double)r);
471             int y2 = (int)(SIN_3_PI_OVER_10 * (double)r);
472
473             double tmpinnr = tmpy / SIN_3_PI_OVER_10;
474
475
476             int xinn = (int)(tmpinnr * COS_3_PI_OVER_10);
477             int yinn = y;
478
479             int xinn2 = (int)(tmpinnr * COS_PI_OVER_10);
480             int yinn2 = (int)(tmpinnr * SIN_PI_OVER_10);
481
482             int innr = (int)tmpinnr;
483
484             coordinates = new int[] { -x2, y2, -xinn2, yinn2, -x, -y, -xinn, -yinn, 0, -r, xinn, -yinn, x, -y, xinn2, yinn2, x2, y2, 0, innr};
485         }
486
487         @Override
488         public void draw(TextureDrawingTools textureDrawingTools) {
489             textureDrawingTools.fillPolygon(coordinates, appearance);
490         }
491     }
492 }