KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > loadprofile > gui > ScaleConvertor


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.loadprofile.gui;
24
25 import java.util.Vector JavaDoc;
26
27 import org.apache.log4j.Category;
28 import org.objectweb.clif.scenario.util.isac.loadprofile.GroupDescription;
29 import org.objectweb.clif.scenario.util.isac.loadprofile.Point;
30 import org.objectweb.clif.scenario.util.isac.loadprofile.RampDescription;
31
32 /**
33  * This class is a scale convertor object, it will store the drawable canvas
34  * visible size, and The drawable canvas size, and the scale, and some methods
35  * to transform point on the canvas in coord in the scale selected
36  *
37  * @author JC Meillaud
38  * @author A Peyrard
39  */

40 public class ScaleConvertor {
41     // logger
42
static Category cat = Category.getInstance(ScaleConvertor.class.getName());
43
44     /**
45      * Define a default scale
46      */

47     private static final int DEFAULT_NB_THREADS = 1000;
48     private static final int DEFAULT_NB_SEC = 3600;
49     private static final Size DEFAULT_SCALE = new Size(DEFAULT_NB_SEC,
50             DEFAULT_NB_THREADS);;
51     public static final Size DEFAULT_MIN_CANVAS_SIZE = new Size(300, 200);
52
53     /**
54      * Define the margins of the mark
55      */

56     public static final int MARGIN_TOP = 20;
57     public static final int MARGIN_BACK = 50;
58     public static final int MARGIN_LEFT = 50;
59     public static final int MARGIN_RIGHT = 20;
60     
61     public static final int GRAD_SIZE = 5 ;
62
63     /**
64      * Define error constants
65      */

66     private static final int ERROR_RATE = DrawableCanvas.SELECTION_SIZE;
67     private static final Point ERROR_POINT = new Point(-1, -1);
68
69     private Size canvasSize;
70     private Point visibleLocation;
71     private Size scale;
72
73     /**
74      * Build a new scale convertor with a default scale
75      */

76     public ScaleConvertor() {
77         // set null size to the canvas and the visible sizes
78
this.canvasSize = new Size();
79         this.visibleLocation = new Point(-1, -1);
80         // set the default scale
81
this.scale = DEFAULT_SCALE;
82     }
83
84     /////////////////////////////////////////////////////////////////////////////
85
// Scale convertion methods
86
/////////////////////////////////////////////////////////////////////////////
87

88     /**
89      * Transform editor point into real point
90      *
91      * @param editor
92      * The editor point
93      * @return The real point
94      */

95     public Point editorPointToRealPoint(Point editor) {
96         // calculate the real editor height and width
97
int realEditorHeight = this.canvasSize.getHeight()
98                 - (MARGIN_BACK + MARGIN_TOP);
99         int realEditorWidth = this.canvasSize.getWidth()
100                 - (MARGIN_LEFT + MARGIN_RIGHT);
101         // calcul the proportions
102
double propHeight = (double) this.scale.getHeight()
103                 / (double) realEditorHeight;
104         double propWidth = (double) this.scale.getWidth()
105                 / (double) realEditorWidth;
106         // change the height of the point in correct direction
107
Point editorWithCorrectHeightDirectionWithoutMargins = new Point(
108                 editor.x - MARGIN_LEFT, canvasSize.getHeight()
109                         - (editor.y + MARGIN_BACK));
110         Point real = new Point(
111                 (int) ((double) editorWithCorrectHeightDirectionWithoutMargins.x * propWidth),
112                 (int) ((double) editorWithCorrectHeightDirectionWithoutMargins.y * propHeight));
113         return real;
114     }
115
116     /**
117      * Transform real poitn into editor point
118      *
119      * @param real
120      * The real point
121      * @return The editor point
122      */

123     public Point realPointToEditorPoint(Point real) {
124         // calculate the real editor height and width
125
int realEditorHeight = this.canvasSize.getHeight()
126                 - (MARGIN_BACK + MARGIN_TOP);
127         int realEditorWidth = this.canvasSize.getWidth()
128                 - (MARGIN_LEFT + MARGIN_RIGHT);
129         // calcul the proportions
130
double propHeight = (double) this.scale.getHeight()
131                 / (double) realEditorHeight;
132         double propWidth = (double) this.scale.getWidth()
133                 / (double) realEditorWidth;
134         // change the point in editor proportions
135
Point editor = new Point((int) ((double) real.x / propWidth),
136                 (int) ((double) real.y / propHeight));
137         // change the height of the point in wrong direction
138
Point editorWithCorrectDirectionAndMargins = new Point(editor.x
139                 + MARGIN_LEFT, canvasSize.getHeight()
140                 - (editor.y + MARGIN_BACK));
141         return editorWithCorrectDirectionAndMargins;
142     }
143
144     /**
145      * Transform a real ramp to a ramp for the editor
146      *
147      * @param real
148      * The real ramp to be transformed
149      * @return The ramp transformed
150      */

151     public RampDescription realRampToEditorRamp(RampDescription real) {
152         // analyse the type of the ramp and redirect to the correct method
153
switch (real.getType()) {
154             case RampDescription.LINE :
155                 return realLineRampToEditorLineRamp(real);
156             case RampDescription.ARC :
157                 return realArcRampToEditorArcRamp(real);
158             case RampDescription.CRENEL_HV :
159             case RampDescription.CRENEL_VH :
160                 return realCrenelRampToEditorCrenelRamp(real) ;
161             default :
162                 cat.warn("UNKNOW RAMP TYPE... " + real.getType());
163                 return null;
164         }
165     }
166
167     /**
168      * Transform an editor ramp to a ramp for the real scale
169      *
170      * @param editor
171      * The editor ramp to be transformed
172      * @return The ramp transformed
173      */

174     public RampDescription editorRampToRealRamp(RampDescription editor) {
175         // analyse the type of the ramp and redirect to the correct method
176
switch (editor.getType()) {
177             case RampDescription.LINE :
178                 return editorLineRampToRealLineRamp(editor);
179             case RampDescription.ARC :
180                 return editorArcRampToRealArcRamp(editor);
181             case RampDescription.CRENEL_HV :
182             case RampDescription.CRENEL_VH :
183                 return editorCrenelRampToRealCrenelRamp(editor) ;
184             default :
185                 cat.warn("UNKNOW RAMP TYPE... " + editor.getType());
186                 return null;
187         }
188     }
189
190     /**
191      * Transform a crenel ramp with editor point to a crenel with real points
192      *
193      * @param editor
194      * The editor ramp to be transfromed
195      * @return The ramp transformed
196      */

197     private RampDescription editorCrenelRampToRealCrenelRamp(RampDescription editor) {
198         // build a new result curve
199
RampDescription real = new RampDescription(editor.getRampId(), editor
200                 .getType());
201         // transfrom the starting point
202
Point start = this.editorPointToRealPoint(editor.getStart());
203         Point end = this.editorPointToRealPoint(editor.getEnd());
204         // set the new calculated points to the result curve
205
real.setStart(start);
206         real.setEnd(end);
207         // return the result
208
return real;
209     }
210     
211     /**
212      * Transform a crenel ramp with real point to a crenel with editor points
213      *
214      * @param real
215      * The real ramp to be transfromed
216      * @return The ramp transformed
217      */

218     private RampDescription realCrenelRampToEditorCrenelRamp(RampDescription real) {
219         // build a new result curve
220
RampDescription editor = new RampDescription(real.getRampId(), real
221                 .getType());
222         // transfrom the starting point
223
Point start = this.realPointToEditorPoint(real.getStart());
224         Point end = this.realPointToEditorPoint(real.getEnd());
225         // set the new calculated points to the result curve
226
editor.setStart(start);
227         editor.setEnd(end);
228         // return the result
229
return editor;
230     }
231     
232     /**
233      * Transform a line ramp with editor point to a line with real points
234      *
235      * @param editor
236      * The editor ramp to be transfromed
237      * @return The ramp transformed
238      */

239     private RampDescription editorLineRampToRealLineRamp(RampDescription editor) {
240         // build a new result curve
241
RampDescription real = new RampDescription(editor.getRampId(), editor
242                 .getType());
243         // transfrom the starting point
244
Point start = this.editorPointToRealPoint(editor.getStart());
245         Point end = this.editorPointToRealPoint(editor.getEnd());
246         // set the new calculated points to the result curve
247
real.setStart(start);
248         real.setEnd(end);
249         // return the result
250
return real;
251     }
252
253     /**
254      * Transform a line ramp with real point to a line with editor points
255      *
256      * @param real
257      * The real ramp to be transfromed
258      * @return The ramp transformed
259      */

260     private RampDescription realLineRampToEditorLineRamp(RampDescription real) {
261         // build a new result ramp
262
RampDescription editor = new RampDescription(real.getRampId(), real
263                 .getType());
264         // transfrom the starting point
265
Point start = this.realPointToEditorPoint(real.getStart());
266         Point end = this.realPointToEditorPoint(real.getEnd());
267         // set the new calculated points to the result ramp
268
editor.setStart(start);
269         editor.setEnd(end);
270         // return the result
271
return editor;
272     }
273
274     /**
275      * Transform an arc ramp with real point to an arc with editor points
276      *
277      * @param real
278      * The real ramp to be transfromed
279      * @return The ramp transformed
280      */

281     private RampDescription realArcRampToEditorArcRamp(RampDescription real) {
282         // TODO
283
return null;
284     }
285
286     /**
287      * Transform an editor arc ramp to a real arc ramp
288      *
289      * @return The ramp description transformed
290      */

291     private RampDescription editorArcRampToRealArcRamp(RampDescription editor) {
292         // TODO
293
return null;
294     }
295
296     /**
297      * Transform a real group to an editor group
298      * @param real The real group
299      * @return The editor group
300      */

301     public GroupDescription realGroupToEditorGroup(GroupDescription real) {
302         // build a temporary group desc with editor points
303
GroupDescription editor = new GroupDescription(real.getGroupId()) ;
304         editor.setBehaviorId(real.getBehaviorId()) ;
305         editor.setCurveColor(real.getCurveColor()) ;
306         Vector JavaDoc ramps = new Vector JavaDoc() ;
307         for (int i=0;i<real.getRamps().size();i++) {
308             RampDescription editorRamp = this.realRampToEditorRamp((RampDescription)real.getRamps().elementAt(i)) ;
309             ramps.add(editorRamp) ;
310         }
311         // put the ramps transformed on the group
312
editor.setRamps(ramps) ;
313         return editor ;
314     }
315     
316     /**
317      * Transform an editor group to a real group
318      * @param editor The editor group
319      * @return The real group
320      */

321     public GroupDescription editorGroupToRealGroup(GroupDescription editor) {
322         // build a temporary group desc with real points
323
GroupDescription real = new GroupDescription(editor.getGroupId()) ;
324         real.setBehaviorId(editor.getBehaviorId()) ;
325         real.setCurveColor(editor.getCurveColor()) ;
326         Vector JavaDoc ramps = new Vector JavaDoc() ;
327         for (int i=0;i<real.getRamps().size();i++) {
328             RampDescription realRamp = this.editorRampToRealRamp((RampDescription)editor.getRamps().elementAt(i)) ;
329             ramps.add(realRamp) ;
330         }
331         // put the ramps transformed on the group
332
real.setRamps(ramps) ;
333         return real ;
334     }
335     
336     /////////////////////////////////////////////////////////////////////////////
337
// Test methods...
338
////////////////////////////////////////////////////////////////////////////
339

340     /**
341      * This method test if the given point is in margin
342      *
343      * @param p
344      * The point to be analysed
345      * @return True if the point is in margins
346      */

347     public boolean isInMarginPoint(Point p) {
348         // test all cases
349
if (p.x < MARGIN_LEFT)
350             return true;
351         if (p.x > (this.canvasSize.getWidth() - MARGIN_RIGHT))
352             return true;
353         if (p.y < MARGIN_TOP)
354             return true;
355         if (p.y > (this.canvasSize.getHeight() - MARGIN_BACK))
356             return true;
357         // all cases have been tested, so we are not on margin parts
358
return false;
359     }
360
361     /**
362      * Test if the given editor point is a selectable point of the given group,
363      * we test with a error rate
364      *
365      * @param editor
366      * The editor point
367      * @param gd
368      * The groupDescription
369      * @return The editor point which is the selctable point, else {-1,-1}
370      */

371     public Point editorPointIsSelectableRealGroupPoint(Point editor,
372             GroupDescription gd) {
373         // create a new empty point
374
Point result = new Point(-1, -1);
375         // test for each subramp of the ramp
376
Vector JavaDoc ramps = gd.getRamps();
377         for (int i = 0; i < ramps.size(); i++) {
378             Point temp = editorPointIsSelectableRampPoint(editor,
379                     (RampDescription) ramps.elementAt(i));
380             if (!temp.equals(new Point(-1, -1)))
381                 return temp;
382         }
383         return result;
384     }
385
386     /**
387      * Test if the given editor point is a selectable point of the given ramp,
388      * we test with a error rate
389      *
390      * @param editor
391      * The editor point
392      * @param rd
393      * The rampDescription
394      * @return The editor point which is the selctable point, else {-1,-1}
395      */

396     private Point editorPointIsSelectableRampPoint(Point editor,
397             RampDescription rd) {
398         // create a new empty point
399
Point result = new Point(-1, -1);
400         // switch between the ramp type
401
switch (rd.getType()) {
402             case RampDescription.LINE :
403                 Point temp = this.editorPointIsSelectableLineRampPoint(editor,
404                         rd);
405                 return temp;
406             case RampDescription.ARC :
407                 break;
408             case RampDescription.CRENEL_HV :
409             case RampDescription.CRENEL_VH :
410                 Point tempCrenel = this.editorPointIsSelectableCrenelRampPoint(editor,
411                         rd);
412                 return tempCrenel ;
413             default :
414                 cat.warn("UNKNOW DRAW TYPE : " + rd.getType());
415         }
416         return result;
417     }
418
419     /**
420      * Test if the given editor point is a selectable point of the line ramp, we
421      * test with a error rate
422      *
423      * @param editor
424      * The editor point
425      * @param rd
426      * The rampDescription
427      * @return The real point which is the selctable point, else {-1,-1}
428      */

429     private Point editorPointIsSelectableLineRampPoint(Point editor,
430             RampDescription rd) {
431         // create a new empty point
432
Point result = new Point(-1, -1);
433         // change the real start point to an editor point, in order to compare
434
// it with the given point
435
Point editorStart = this.realPointToEditorPoint(rd.getStart());
436         if (editorStart.x > editor.x - ERROR_RATE
437                 && editorStart.x < editor.x + ERROR_RATE)
438             if (editorStart.y > editor.y - ERROR_RATE
439                     && editorStart.y < editor.y + ERROR_RATE)
440                 return rd.getStart();
441         // change the real end point to an editor point, in order to compare
442
// it with the given point
443
Point editorEnd = this.realPointToEditorPoint(rd.getEnd());
444         if (editorEnd.x > editor.x - ERROR_RATE
445                 && editorEnd.x < editor.x + ERROR_RATE)
446             if (editorEnd.y > editor.y - ERROR_RATE
447                     && editorEnd.y < editor.y + ERROR_RATE)
448                 return rd.getEnd();
449
450         return result;
451     }
452     
453     /**
454      * Test if the given editor point is a selectable point of the crenel ramp, we
455      * test with a error rate
456      *
457      * @param editor
458      * The editor point
459      * @param rd
460      * The rampDescription
461      * @return The real point which is the selctable point, else {-1,-1}
462      */

463     private Point editorPointIsSelectableCrenelRampPoint(Point editor,
464             RampDescription rd) {
465         // create a new empty point
466
Point result = new Point(-1, -1);
467         // change the real start point to an editor point, in order to compare
468
// it with the given point
469
Point editorStart = this.realPointToEditorPoint(rd.getStart());
470         if (editorStart.x > editor.x - ERROR_RATE
471                 && editorStart.x < editor.x + ERROR_RATE)
472             if (editorStart.y > editor.y - ERROR_RATE
473                     && editorStart.y < editor.y + ERROR_RATE)
474                 return rd.getStart();
475         // change the real end point to an editor point, in order to compare
476
// it with the given point
477
Point editorEnd = this.realPointToEditorPoint(rd.getEnd());
478         if (editorEnd.x > editor.x - ERROR_RATE
479                 && editorEnd.x < editor.x + ERROR_RATE)
480             if (editorEnd.y > editor.y - ERROR_RATE
481                     && editorEnd.y < editor.y + ERROR_RATE)
482                 return rd.getEnd();
483
484         return result;
485     }
486
487     /**
488      * Test if the editor point is a member of the group given, with an error
489      * rate
490      *
491      * @param editor
492      * The editor point
493      * @param gd
494      * The group description
495      * @return The rampId
496      */

497     public String JavaDoc editorPointIsMemberOfGroup(Point editor, GroupDescription gd) {
498         Vector JavaDoc ramps = gd.getRamps();
499         // test for each subramp
500
for (int i = 0; i < ramps.size(); i++) {
501             if (editorPointIsMemberOfCurve(editor, (RampDescription) ramps
502                     .elementAt(i)))
503                 return ((RampDescription) ramps.elementAt(i)).getRampId();
504         }
505         // the point is not a member of the ramp
506
return null;
507     }
508
509     /**
510      * Test if the editor point is a member of the ramp given, with an error
511      * rate
512      *
513      * @param editor
514      * The editor point
515      * @param rd
516      * The ramp description
517      * @return True if the point is a member of the ramp
518      */

519     private boolean editorPointIsMemberOfCurve(Point editor, RampDescription rd) {
520         // switch between the ramp type
521
switch (rd.getType()) {
522             case RampDescription.LINE :
523                 if (editorPointIsMemberOfLineRamp(editor, rd))
524                     return true;
525                 break;
526             case RampDescription.ARC :
527                 break;
528             default :
529                 cat.warn("UNKNOW DRAW TYPE : " + rd.getType());
530         }
531         return false;
532     }
533
534     /**
535      * Test if the editor point is a member of the line ramp given, with an
536      * error rate
537      *
538      * @param editor
539      * The editor point
540      * @param rd
541      * The ramp description
542      * @return True if the point is a member of the ramp
543      */

544     private boolean editorPointIsMemberOfLineRamp(Point editor,
545             RampDescription rd) {
546         Point start = rd.getStart();
547         Point end = rd.getEnd();
548         // calculate the real point
549
Point real = editorPointToRealPoint(editor);
550         // verify that the X-Axis of the real point is between the X-axis of the
551
// start point and the end point
552
if (real.x > end.x || real.x < start.x) {
553             return false;
554         }
555         // calculate the equation line
556
Double JavaDoc a = calculateLineEquationA(start, end);
557         if (a == null) {
558             // the line is vertical, test if the y point is between the two
559
// points
560
if (start.y < end.y) {
561                 if (editor.y > start.y && editor.y < end.y)
562                     return true;
563                 else
564                     return false;
565             } else {
566                 if (editor.y < start.y && editor.y > end.y)
567                     return true;
568                 else
569                     return false;
570             }
571         }
572         // calculate b element of the equation
573
Double JavaDoc b = calculateLineEquationB(start, end, a);
574         // calculate the Y-axis which may be if the point would be a member of
575
// the line
576
int y = calculateYAxisPoint(a, b, real.x);
577         // test if the Y-axis of the point is equals of the real Y-axis, with an
578
// error rate
579
if (real.y > y - ERROR_RATE && real.y < y + ERROR_RATE)
580             return true;
581
582         // the point is not a member of the line
583
return false;
584     }
585
586     /////////////////////////////////////////////////////////////////////////////
587
// Mathematics methods...
588
////////////////////////////////////////////////////////////////////////////
589

590     /**
591      * Calculate the equation line A with the two point given
592      *
593      * @param p
594      * The first point of the line
595      * @param q
596      * the second point of the line
597      * @return a : the equation line is y=ax+b, if we can not calculate the
598      * equation return -1
599      */

600     public static Double JavaDoc calculateLineEquationA(Point p, Point q) {
601         // test if the two point have not the same X axis
602
if (p.x == q.x) {
603             return null;
604         }
605         // calculate a
606
double a = ((double) (q.y - p.y)) / ((double) (q.x - p.x));
607         return new Double JavaDoc(a);
608     }
609
610     /**
611      * Calculate the equation line B with the two given point and the a
612      * parameter of the equation
613      *
614      * @param p
615      * The first point of the line
616      * @param q
617      * The second point of the line
618      * @param a
619      * @return b : the equation line is y=ax+b
620      */

621     public static Double JavaDoc calculateLineEquationB(Point p, Point q, Double JavaDoc a) {
622         return new Double JavaDoc(q.y - (q.x * a.doubleValue()));
623     }
624
625     /**
626      * Calculate Y-axis of the point which is point of the given line
627      *
628      * @return Y-axis of the point
629      */

630     public static int calculateYAxisPoint(Double JavaDoc a, Double JavaDoc b, int x) {
631         return (int) (a.doubleValue() * x + b.doubleValue());
632     }
633     
634     /**
635      * Calculate the log ten of the number specified
636      * @param n The number to calcul the log
637      * @return The log
638      */

639     public static int logTen(int n) {
640         if (n <= 10) return 1;
641         return 1+logTen(n/10) ;
642     }
643
644     /////////////////////////////////////////////////////////////////////////////
645
// Attributes getters
646
/////////////////////////////////////////////////////////////////////////////
647

648     /**
649      * @return Returns the canvasSize.
650      */

651     public Size getCanvasSize() {
652         return canvasSize;
653     }
654
655     /**
656      * @param canvasSize
657      * The canvasSize to set.
658      */

659     public void setCanvasSize(Size canvasSize) {
660         this.canvasSize = canvasSize;
661     }
662
663     /**
664      * @return Returns the visibleLocation.
665      */

666     public Point getVisibleLocation() {
667         return visibleLocation;
668     }
669     /**
670      * @param visibleLocation
671      * The visibleLocation to set.
672      */

673     public void setVisibleLocation(Point visibleLocation) {
674         this.visibleLocation = visibleLocation;
675     }
676
677     /**
678      * @return Returns the scale.
679      */

680     public Size getScale() {
681         return scale;
682     }
683
684     /**
685      * @param scale
686      * The scale to set.
687      */

688     public void setScale(Size scale) {
689         this.scale = scale;
690     }
691 }
Popular Tags