NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/js/cwc_roi.js

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
1 /**
2  *
3  * @project     CWC2
4  * @revision    $Id: cwc_roi.js,v 1.27 2005/05/16 11:32:53 bartvde Exp $
5  * @purpose     The ROI Manager - a group of functions for managing
6  * regions of interest and the tools that define them.
7  * @author      DM Solutions Group (spencer@dmsolutions.ca)
8  * @copyright
9  * <b>Copyright (c) 2002, DM Solutions Group Inc.</b>
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28  
29  
30 var CWCIsNav4 = (document.layers) ? 1:0;
31
32 var CWCIsIE = (document.all) ? 1:0;
33 var CWCIsNav6 = (document.getElementById && !document.all) ? 1:0;
34
35 if (CWCIsNav4 || CWCIsNav6)
36 {
37     document.captureEvents(Event.MOUSEMOVE);
38     document.captureEvents(Event.MOUSEDOWN);
39     document.captureEvents(Event.MOUSEUP);
40     document.captureEvents(Event.RESIZE);
41     document.captureEvents(Event.KEYPRESS);
42 }
43
44 var ROI_MODE_FIRST = 1;
45 var ROI_MODE_NORMAL = ROI_MODE_FIRST;
46 var ROI_MODE_ADD = ROI_MODE_NORMAL + 1;
47 var ROI_MODE_SUBTRACT = ROI_MODE_ADD + 1;
48 var ROI_MODE_LAST = ROI_MODE_SUBTRACT;
49
50 /*
51  * ROI Events
52  */
53 var ROI_CHANGED = 0;
54  
55 var goCWCROIManager = new CWCROIManager();
56
57 /*
58  * The ROI Manager
59  */
60 function CWCROIManager()
61 {
62     this.aROI = new Array();
63     this.nActiveROITool = 0;
64     this.oForm = null;
65    
66     this.mode = ROI_MODE_NORMAL;
67     this.szRendererURL = "";
68
69     this.geoMinX = null;
70     this.geoMinY = null;   
71     this.geoMaxX = null;   
72     this.geoMaxY = null;
73    
74     this.pixMinX = null;
75     this.pixMinY = null;   
76     this.pixMaxX = null;   
77     this.pixMaxY = null;
78    
79     this.szEdgeColor = "#00FF00";
80     this.nEdgeWidth = 2;
81
82     this.szFillColor = this.szEdgeColor;
83     this.nFillOpacity = 0.5;
84     this.bFill = true;
85
86     this.onchange = new Array();
87     this.events = new Array();
88     this.events[ROI_CHANGED] = new Array();
89    
90     this.SetFormObject = CWCROIManager_SetFormObject;
91     this.Add = CWCROIManager_Add;
92     this.Insert = CWCROIManager_Insert;
93     this.Redraw = CWCROIManager_Redraw;
94     this.GetLast = CWCROIManager_GetLast;
95     this.RemoveLast = CWCROIManager_RemoveLast;
96     this.RemoveAll = CWCROIManager_RemoveAll;
97     this.SetROITool = CWCROIManager_SetROITool;
98     this.GetROITool = CWCROIManager_GetROITool;
99     this.SetGeoExtents = CWCROIManager_SetGeoExtents;
100     this.SetPixelExtents = CWCROIManager_SetPixelExtents;
101     this.SetMode = CWCROIManager_SetMode;
102     this.SetRendererURL = CWCROIManager_SetRendererURL;
103     this.GetRendererURL = CWCROIManager_GetRendererURL;
104     this.GetROI = CWCROIManager_GetROI;
105     this.Geo2Pix = CWCROIManager_Geo2Pix;
106     this.Pix2Geo = CWCROIManager_Pix2Geo;
107     this.UpdateLayerVisibility = CWCROIManager_UpdateLayerVisibility;
108     this.RegisterEvent = CWCROIManager_RegisterEvent;
109     this.DeregisterEvent = CWCROIManager_RegisterEvent;
110     this.TriggerEvent = CWCROIManager_TriggerEvent;
111 }
112
113 function CWCROIManager_UpdateLayerVisibility()
114 {
115     if (this.aROI.length > 0)
116     {
117         //window.status = "show layer";
118         CWCDHTML_ShowLayer( gROIRenderer );
119     }
120     else
121         CWCDHTML_HideLayer( gROIRenderer );
122 }
123
124
125 function CWCROIManager_GetROI()
126 {
127     return aROI;
128 }
129
130 function CWCROIManager_SetRendererURL( szURL )
131 {
132     this.szRendererURL = szURL;
133 }
134
135 function CWCROIManager_GetRendererURL()
136 {
137     var szURL = this.szRendererURL + "&blah=" + Math.random();
138     szURL = szURL + "&width=" + (this.pixMaxX - this.pixMinX);
139     szURL = szURL + "&height=" + (this.pixMaxY - this.pixMinY);
140     return szURL;
141 }
142
143 function CWCROIManager_SetMode( nMode )
144 {
145     if (nMode >= ROI_MODE_FIRST && nMode <= ROI_MODE_LAST)
146     {
147         this.mode = nMode;   
148     }
149    
150     if (this.oForm.ROI_MODE)
151     {
152         this.oForm.ROI_MODE.value = this.mode;
153     }
154 }
155
156 function CWCROIManager_SetGeoExtents( fMinX, fMinY, fMaxX, fMaxY )
157 {
158     this.geoMinX = parseFloat( fMinX );
159     this.geoMinY = parseFloat( fMinY );   
160     this.geoMaxX = parseFloat( fMaxX );   
161     this.geoMaxY = parseFloat( fMaxY );
162 }
163
164 function CWCROIManager_SetPixelExtents( nMinX, nMinY, nMaxX, nMaxY )
165 {
166     this.pixMinX = parseInt( nMinX );
167     this.pixMinY = parseInt( nMinY );   
168     this.pixMaxX = parseInt( nMaxX );   
169     this.pixMaxY = parseInt( nMaxY );
170 }
171
172 function CWCROIManager_SetFormObject( oObj )
173 {
174     this.oForm = oObj;
175 }
176
177 function CWCROIManager_RegisterEvent( event, szCode )
178 {
179     lastEvent = (this.events[event]).length;
180     this.events[ event ][ lastEvent ] = szCode;
181     return true;
182 }
183
184 function CWCROIManager_DeregisterEvent( event, szCode )
185 {
186     bFound = false
187     events = this.events[event];
188     for( i=0; i<events.length; i++ )
189     {
190         if (this.events[i] == szCode )
191         {
192             this.events[i] = "";
193             bFound = true;
194         }
195     }
196     return  bFound;
197 }
198
199 function CWCROIManager_TriggerEvent( event )
200 {
201     var i = 0;
202     var nEvents = (this.events[ event ]).length;
203     for (i=0; i<nEvents; i++)
204     {
205         szFunction =  this.events[ event ][ i ];
206         if (szFunction != "")
207         {
208             eval(szFunction);
209         }
210     }
211     return true;
212 }
213
214 function CWCROIManager_Add( oROI )
215 {
216     oROI.manager = this;
217     oROI.mode = this.mode;
218
219     var szURL = oROI.GetRendererURL();
220     if (szURL == false)
221     {
222         return false;
223     }   
224     if (this.mode == ROI_MODE_NORMAL)
225     {
226         this.RemoveAll();
227         this.aROI[0] = oROI;
228     }
229     else
230     {
231         this.aROI[this.aROI.length] = oROI;
232     }
233     if (this.oForm.ROI_MODE)
234     {
235         var oImage = CWCDHTML_GetImage( "ROIRenderer" );
236         oImage.src = szURL;
237     }
238    
239     this.UpdateLayerVisibility();
240     this.TriggerEvent( ROI_CHANGED );
241 }
242
243 /**
244  *  This function will redraw the ROI's according to current extent and info.
245  *  It is used for when things like extent or projection changes in a mapfile.
246  **/
247 function CWCROIManager_Redraw()
248 {
249     var szURL = this.GetRendererURL();
250     if (szURL == false)
251     {
252         return false;
253     }   
254     var oImage = CWCDHTML_GetImage( "ROIRenderer" );
255     oImage.src = szURL;
256        
257     this.UpdateLayerVisibility();
258 }
259
260 /**
261  * utility function to quickly insert an ROI object into the
262  * manager, used when reconstituting the objects after a
263  * page load.  This function does not do any error checking
264  * or validation of indexes.
265  *
266  * @param oROI an object to insert into the manager
267  * @param nIndex the index to insert the object at
268  */
269 function CWCROIManager_Insert( oROI, nIndex )
270 {
271     oROI.manager = this;
272     oROI.mode = this.mode;
273
274     this.aROI[nIndex] = oROI;
275 }
276
277
278 function CWCROIManager_GetLast( )
279 {
280     if (this.aROI.length > 0)
281         return this.aROI[this.aROI.length - 1];
282     else
283         return null;
284 }
285
286 function CWCROIManager_RemoveLast()
287 {
288     var newROI = new Array();
289     var i;
290     if (this.aROI.length > 0)
291     {
292         for (i=0; i < this.aROI.length - 1; i++)
293         {
294             newROI[i] = this.aROI[i];
295         }
296
297         this.aROI[this.aROI.length - 1].Hide();
298         this.aROI[this.aROI.length - 1] = null;
299         this.aROI = newROI;
300         this.TriggerEvent( ROI_CHANGED );
301     }
302    
303     if (this.oForm.ROI_MODE)
304     {
305         var szURL = this.GetRendererURL();
306         if (szURL != null)
307         {
308             szURL = szURL + "&mode=-1";
309             var oImage = CWCDHTML_GetImage( "ROIRenderer" );
310             oImage.src = szURL;
311         }
312     }   
313     this.UpdateLayerVisibility();
314 }
315
316 function CWCROIManager_RemoveAll()
317 {
318     var i;
319     if (this.aROI.length > 0)
320     {
321         for (i=0; i < this.aROI.length; i++)
322         {
323             if (this.aROI[i] != null)
324                 this.aROI[i].Hide();
325            this.aROI[i] = null;
326         }
327         this.TriggerEvent( ROI_CHANGED );
328     }
329     this.aROI = new Array();
330     if (this.oForm.ROI_MODE)
331     {
332         var szURL = this.GetRendererURL();
333         if (szURL != null)
334         {
335             szURL = szURL + "&mode=-2";
336             var oImage = CWCDHTML_GetImage( "ROIRenderer" );
337             oImage.src = szURL;
338         }
339     }
340     this.UpdateLayerVisibility();
341 }
342
343 function CWCROIManager_SetROITool( nTool )
344 {
345     this.nActiveTool = nTool;
346    
347     if (this.oForm != null)
348     {
349         if (this.oForm.NAV_CMD != null)
350             this.oForm.NAV_CMD.value = "ROI_TOOL";
351         if (this.oForm.NAV_SUBMIT != null)
352             this.oForm.NAV_SUBMIT.value = "0";
353     }
354 }
355
356 function CWCROIManager_GetROITool( )
357 {
358     return this.nActiveTool
359 }
360
361 /****************************************************************************
362 * Converts from Geographic coordinates to Pixel coordinates. Returns an
363 * array of 2 elements containing the resulting x and y positions.
364 *
365 * @param x : Value should be between inside the map extents
366 * @param y : Value should be between inside the map extents
367 *****************************************************************************/
368 function CWCROIManager_Geo2Pix( x, y )
369 {
370     if (x >= this.geoMinX && x <= this.geoMaxX &&
371         y >= this.geoMinY && y <= this.geoMaxY)
372     {
373         var width = this.pixMaxX - this.pixMinX;
374         var height = this.pixMaxY - this.pixMinY;
375        
376         var dfDeltaMaxGeoX = this.geoMaxX - this.geoMinX;
377         var dfDeltaMaxGeoY = this.geoMaxY - this.geoMinY;
378
379         var dfPixX = (width * (x - this.geoMinX))/ dfDeltaMaxGeoX;
380         var dfPixY = height - ((height * (y - this.geoMinY))/ dfDeltaMaxGeoY);
381
382         aReturn = new Array(2);
383         aReturn[0] = dfPixX;
384         aReturn[1] = dfPixY;
385
386         return aReturn;
387     }
388     else
389         return false;
390 }
391
392
393 /****************************************************************************
394 * Converts from Pixel to Geographic coordinates. Returns an
395 * array of 2 elements containing the resulting x and y positions.
396 *
397 * @param x : Value should be between 0 and the map width
398 * @param y : Value should be between 0 and the map height
399 *****************************************************************************/
400 function CWCROIManager_Pix2Geo(x, y)
401 {
402     var width = this.pixMaxX - this.pixMinX;
403     var height = this.pixMaxY - this.pixMinY;
404     if (x >= 0 && x <= width &&
405         y >= 0 && y <= height)
406     {
407        
408         var x_pct = (x / width);
409         var y_pct = (y / height);
410
411         var dfGeoX = this.geoMinX + ( ( this.geoMaxX -  this.geoMinX) * x_pct);
412         var dfGeoY = this.geoMaxY - ( (this.geoMaxY -  this.geoMinY) * y_pct);
413
414         var aReturn = new Array(2);
415         aReturn[0] = dfGeoX;
416         aReturn[1] = dfGeoY;
417         return aReturn;
418     }
419     else
420     {
421         return false;
422     }
423 }
424
425 /*
426  * a Feature-based ROI
427  */
428 function CWCFeatureROI(  )
429 {
430     this.type = "feature";
431     this.manager = null;
432     this.mode = ROI_MODE_NORMAL;
433    
434     this.edgeColor = "#00FF00";
435     this.edgeWidth = 2;
436
437     this.fillColor = this.edgeColor;
438     this.fillOpacity = 0.5;
439     this.fillLayer = null;
440     this.bFill = true;
441    
442     this.visible = false;
443    
444     this.szLayer = "";
445     this.x = -1;
446     this.y = -1;
447     this.geoX = null;
448     this.geoY = null;
449    
450     this.Draw = CWCFeatureROI_Draw;
451     this.Set = CWCFeatureROI_Set;
452     this.Show = CWCFeatureROI_Show;
453     this.Hide = CWCFeatureROI_Hide;
454     this.GetRendererURL = CWCFeatureROI_GetRendererURL;
455     this.GetROI = CWCFeatureROI_GetROI;
456     this.Serialize = CWCFeatureROI_Serialize;
457     this.CalculateGeoExtents = CWCFeatureROI_CalculateGeoExtents;
458 }
459
460
461 function CWCFeatureROI_GetROI()
462 {
463     return false;
464 }
465
466 function CWCFeatureROI_Draw()
467 {
468     return false;
469 }
470
471 function CWCFeatureROI_Set( x, y, szLayer )
472 {
473     this.x = x;
474     this.y = y;
475     this.szLayer = szLayer
476    
477     return false;
478 }
479
480 function CWCFeatureROI_Show()
481 {
482     return false;
483 }
484
485 function CWCFeatureROI_Hide()
486 {
487     return false;
488 }
489
490 function CWCFeatureROI_GetRendererURL()
491 {
492     var szSerialized = this.Serialize();
493     if (!szSerialized)
494     {
495         return false;
496     }
497     var szURL = this.manager.GetRendererURL();
498    
499     szURL = szURL + "&" + szSerialized;
500     return szURL;
501 }
502
503 function CWCFeatureROI_Serialize()
504 {
505     if (!this.CalculateGeoExtents())
506     {
507         return false;
508     }
509     if (this.szLayer == "")
510     {
511         return false;
512     }
513     var szURL = "type=" + this.type;
514     szURL = szURL + "&mode=" + this.mode;
515     szURL = szURL + "&coords=" + this.geoX + "," + this.geoY;
516     szURL = szURL + "&edgecolor=" + escape(this.edgeColor);
517     szURL = szURL + "&edgewidth=" + this.edgeWidth;
518     szURL = szURL + "&fillcolor=" + escape(this.fillColor);
519     if (CWCIsIE)
520     {
521         szURL = szURL + "&fillopacity=-1";
522     }
523     else
524     {
525         szURL = szURL + "&fillopacity=" + this.fillOpacity;
526     }
527     szURL = szURL + "&selectedLayer=" + this.szLayer
528     return szURL;
529 }
530
531 function CWCFeatureROI_CalculateGeoExtents()
532 {
533     if (this.manager != null)
534     {
535         var aGeo = this.manager.Pix2Geo( this.x, this.y );
536         this.geoX = aGeo[0];
537         this.geoY = aGeo[1];
538         return true;
539     }
540     return false;
541 }
542
543 /*
544  * a Rectangular ROI
545  */
546 function CWCRectangleROI(  )
547 {
548     this.type = "rectangle";
549     this.manager = null;
550     this.mode = ROI_MODE_NORMAL;
551    
552     this.left = -2;
553     this.top = -2;   
554     this.right = -2;   
555     this.bottom = -2;
556    
557     this.geoMinX = null;
558     this.geoMinY = null;   
559     this.geoMaxX = null;   
560     this.geoMaxY = null;
561    
562     this.topLayer = null;
563     this.leftLayer = null;
564     this.rightLayer = null;
565     this.bottomLayer = null;
566     this.edgeColor = "#00FF00";
567     this.edgeWidth = 2;
568
569     this.fillColor = this.edgeColor;
570     this.fillOpacity = 0.5;
571     this.fillLayer = null;
572     this.bFill = true;
573    
574     this.visible = false;
575    
576     this.Draw = CWCRectangleROI_Draw;
577     this.Set = CWCRectangleROI_Set;
578     this.Show = CWCRectangleROI_Show;
579     this.Hide = CWCRectangleROI_Hide;
580     this.GetRendererURL = CWCRectangleROI_GetRendererURL;
581     this.GetROI = CWCRectangleROI_GetROI;
582     this.Serialize = CWCRectangleROI_Serialize;
583     this.CalculateGeoExtents = CWCRectangleROI_CalculateGeoExtents;
584 }
585
586 function CWCRectangleROI_CalculateGeoExtents()
587 {
588     if (this.manager != null)
589     {
590         var aTL = this.manager.Pix2Geo( this.left, this.top );
591         var aBR = this.manager.Pix2Geo( this.right, this.bottom );
592         if (aTL != false && aBR != false)
593         {
594             this.geoMinX = Math.min(aTL[0], aBR[0]);
595             this.geoMaxX = Math.max(aTL[0], aBR[0]);   
596             this.geoMinY = Math.min(aTL[1], aBR[1]);
597             this.geoMaxY = Math.max(aTL[1], aBR[1]);
598            
599             //alert( this.geoMinX + "," + this.geoMinY + ":" + this.geoMaxX + "," + this.geoMaxY );
600             return true;
601         }
602         else
603         {
604             this.geoMinX = null;
605             this.geoMinY = null;   
606             this.geoMaxX = null;   
607             this.geoMaxY = null;
608             return false;
609         }
610     }
611     else
612     {
613         return false;
614     }
615 }
616
617 function CWCRectangleROI_GetROI()
618 {
619    
620 }
621
622 function CWCRectangleROI_Draw()
623 {
624     if (!this.visible)
625         return;
626        
627     if( this.topLayer == null || this.leftLayer == null ||
628         this.rightLayer == null || this.bottomLayer == null)
629     {
630         this.visible = false;
631         return;
632     }
633        
634     CWCDHTML_ClipLayer(this.topLayer, this.left, this.top, this.right, this.top+this.edgeWidth);
635     CWCDHTML_ClipLayer(this.leftLayer, this.left, this.top, this.left + this.edgeWidth, this.bottom);
636     CWCDHTML_ClipLayer(this.rightLayer, this.right - this.edgeWidth, this.top, this.right, this.bottom);
637     CWCDHTML_ClipLayer(this.bottomLayer, this.left, this.bottom - this.edgeWidth, this.right, this.bottom);
638     if (this.bFill)
639     {
640         if (this.fillLayer != null)
641         {
642             CWCDHTML_ClipLayer(this.fillLayer, this.left+this.edgeWidth, this.top+this.edgeWidth, this.right-this.edgeWidth, this.bottom - this.edgeWidth);
643         }
644         else
645         {
646             this.bFill = false;
647         }
648     }
649 }
650
651 function CWCRectangleROI_Set( left, top, right, bottom )
652 {
653     this.left = left;
654     this.top = top;   
655     this.right = right;   
656     this.bottom = bottom;
657    
658     if (this.visible)
659     {
660         this.Draw();
661     }
662 }
663
664 function CWCRectangleROI_Show()
665 {
666     if( this.topLayer == null || this.leftLayer == null ||
667         this.rightLayer == null || this.bottomLayer == null)
668     {
669         this.visible = false;
670         return;
671     }
672    
673     this.visible = true;
674    
675     CWCDHTML_ShowLayer(this.topLayer);
676     CWCDHTML_ShowLayer(this.leftLayer);
677     CWCDHTML_ShowLayer(this.rightLayer);
678     CWCDHTML_ShowLayer(this.bottomLayer);
679    
680     CWCDHTML_SetLayerBackgroundColor( this.topLayer, this.edgeColor );
681     CWCDHTML_SetLayerBackgroundColor( this.leftLayer, this.edgeColor );
682     CWCDHTML_SetLayerBackgroundColor( this.rightLayer, this.edgeColor );
683     CWCDHTML_SetLayerBackgroundColor( this.bottomLayer, this.edgeColor );
684    
685     if(this.bFill)
686     {
687         CWCDHTML_ShowLayer(this.fillLayer)
688         CWCDHTML_SetLayerBackgroundColor( this.fillLayer, this.fillColor );
689         CWCDHTML_SetLayerOpacity( this.fillLayer, this.fillOpacity );
690    
691     }   
692     this.Draw();
693 }
694
695 function CWCRectangleROI_Hide()
696 {
697     if( this.topLayer == null || this.leftLayer == null ||
698         this.rightLayer == null || this.bottomLayer == null)
699     {
700         this.visible = false;
701         return;
702     }
703    
704     this.visible = false;
705    
706     CWCDHTML_HideLayer(this.topLayer);
707     CWCDHTML_HideLayer(this.leftLayer);
708     CWCDHTML_HideLayer(this.rightLayer);
709     CWCDHTML_HideLayer(this.bottomLayer);
710    
711     if(this.bFill)
712     {
713         CWCDHTML_HideLayer(this.fillLayer)
714     }
715 }
716
717 function CWCRectangleROI_GetRendererURL()
718 {
719     var szSerialized = this.Serialize();
720     if (!szSerialized)
721     {
722         return false;
723     }
724     var szURL = this.manager.GetRendererURL();
725    
726     szURL = szURL + "&" + szSerialized;
727     return szURL;
728 }
729
730 function CWCRectangleROI_Serialize()
731 {
732     if (!this.CalculateGeoExtents())
733         return false;
734    
735     var szURL = "type=" + this.type;
736     szURL = szURL + "&mode=" + this.mode;
737     szURL = szURL + "&coords=" + this.geoMinX + "," + this.geoMinY + "," + this.geoMaxX + "," + this.geoMaxY;
738     szURL = szURL + "&edgecolor=" + escape(this.edgeColor);
739     szURL = szURL + "&edgewidth=" + this.edgeWidth;
740     szURL = szURL + "&fillcolor=" + escape(this.fillColor);
741     if (CWCIsIE)
742     {
743         szURL = szURL + "&fillopacity=-1";
744     }
745     else
746     {
747         szURL = szURL + "&fillopacity=" + this.fillOpacity;
748     }
749     return szURL;
750 }
751
752 /******************************************************************************
753  * a Circle ROI
754  *****************************************************************************/
755 function CWCCircleROI( nPoints )
756 {
757     this.type = "circle";
758     this.manager = null;
759     this.mode = ROI_MODE_NORMAL;
760    
761     // set the node image widths
762     this.centerNodeImageWidth = 7;
763     this.nodeImageWidth = 6;
764    
765     // set the operating limits for this tool
766     this.left = -2;
767     this.top = -2;   
768     this.right = -2;   
769     this.bottom = -2;
770    
771     // define the center and radius properties
772     this.centerX = -2;  // in pixels from corner of map
773     this.centerY = -2;  // in pixels from corner of map
774     this.radius = 0;
775  
776     // default the number of layers to create
777     this.numPoints = nPoints;
778    
779     // loop and build layer objects
780     for ( var i=1; i<=this.numPoints; i++ )
781     {
782         eval('this.Layer' + i + ' = null;');
783     }
784      
785     this.edgeColor = "#00FF00";
786     this.edgeWidth = 2;
787
788     this.fillColor = this.edgeColor;
789     this.fillOpacity = 0.5;
790     this.fillLayer = null;
791     this.bFill = true;
792    
793     this.visible = false;
794    
795     this.Draw = CWCCircleROI_Draw;
796     this.Show = CWCCircleROI_Show;
797     this.Hide = CWCCircleROI_Hide;
798     this.GetRendererURL = CWCCircleROI_GetRendererURL;
799     this.GetROI = CWCCircleROI_GetROI;
800     this.Serialize = CWCCircleROI_Serialize;
801     //this.CalculateGeoExtents = CWCCircleROI_CalculateGeoExtents;
802 }
803 /*
804 function CWCCircleROI_CalculateGeoExtents()
805 {
806     if (this.manager != null)
807     {
808         var aTL = this.manager.Pix2Geo( this.left, this.top );
809         var aBR = this.manager.Pix2Geo( this.right, this.bottom );
810         if (aTL != false && aBR != false)
811         {
812             this.geoMinX = Math.min(aTL[0], aBR[0]);
813             this.geoMaxX = Math.max(aTL[0], aBR[0]);   
814             this.geoMinY = Math.min(aTL[1], aBR[1]);
815             this.geoMaxY = Math.max(aTL[1], aBR[1]);
816            
817             //alert( this.geoMinX + "," + this.geoMinY + ":" + this.geoMaxX + "," + this.geoMaxY );
818             return true;
819         }
820         else
821         {
822             this.geoMinX = null;
823             this.geoMinY = null;   
824             this.geoMaxX = null;   
825             this.geoMaxY = null;
826             return false;
827         }
828     }
829     else
830     {
831         return false;
832     }
833 }
834 */
835 function CWCCircleROI_GetROI()
836 {
837    
838 }
839
840 function CWCCircleROI_Draw()
841 {
842     // init vars
843     var dX = 0;
844     var dY = 0;
845
846     if (!this.visible)
847         return;
848        
849     // calculate the distance to the mouse and angle interval (rad)
850     with (Math)
851     {
852         var dist = sqrt( pow( gROIToolmouseX - gROICircleToolx1, 2 ) + pow( gROIToolmouseY - gROICircleTooly1, 2 ) );
853         var angleInterval = 2 * PI / this.numPoints;
854     }
855
856     // set the center and radius
857     this.centerX = gROICircleToolx1 - this.left;
858     this.centerY = gROICircleTooly1 - this.top;
859     this.radius = dist;
860    
861     // position nodes
862     CWCDHTML_SetLayerPos( this.centerLayer, gROICircleToolx1-(this.centerNodeImageWidth/2), gROICircleTooly1-(this.centerNodeImageWidth/2) );
863
864     // loop and position each of the point layers around the circle
865     for ( var i=0; i<this.numPoints; i++ )
866     {
867         // calculate the x and y position (account for image width/height)
868         dX = (Math.cos(i*angleInterval) * dist) - (this.nodeImageWidth/2);
869         dY = (Math.sin(i*angleInterval) * dist) - (this.nodeImageWidth/2);;
870        
871         // position the layer
872         eval('CWCDHTML_SetLayerPos( this.Layer' + (i+1) + ', gROICircleToolx1 + dX, gROICircleTooly1 + dY );');
873        
874         // show/hide as necessary
875         if ( gROICircleToolx1 + dX < this.left || gROICircleToolx1 + dX > this.right - this.nodeImageWidth ||
876              gROICircleTooly1 + dY < this.top || gROICircleTooly1 + dY > this.bottom - this.nodeImageWidth )
877         {
878             // hide
879             eval('CWCDHTML_HideLayer(this.Layer' + (i+1) + ');');
880         }
881         else
882         {
883             // show
884             eval('CWCDHTML_ShowLayer(this.Layer' + (i+1) + ');');
885         }
886     }
887 }
888
889 function CWCCircleROI_Show()
890 {
891     // set flag   
892     this.visible = true;
893    
894     CWCDHTML_ShowLayer(this.centerLayer);
895    
896     // loop and show layer objects
897     for ( var i=1; i<=this.numPoints; i++ )
898     {
899         eval( 'CWCDHTML_ShowLayer(this.Layer' + i + ');' );
900     }
901    
902     this.Draw();
903 }
904
905 function CWCCircleROI_Hide()
906 {
907     // set flag
908     this.visible = false;
909    
910     // loop and hide layer objects
911     for ( var i=1; i<=this.numPoints; i++ )
912     {
913         eval( 'CWCDHTML_HideLayer(this.Layer' + i + ');' );
914     }
915    
916     CWCDHTML_HideLayer(this.centerLayer);
917 }
918
919 function CWCCircleROI_GetRendererURL()
920 {
921     var szSerialized = this.Serialize();
922     if (!szSerialized)
923         return false;
924        
925     var szURL = this.manager.GetRendererURL();
926    
927     szURL = szURL + "&" + szSerialized;
928     return szURL;
929 }
930
931 function CWCCircleROI_Serialize()
932 {
933     // init vars
934     var aCenter;
935     var aRadius;
936     var szURL;
937
938     // convert to ground units
939     if (this.manager != null)
940     {
941         aCenter = this.manager.Pix2Geo( this.centerX, this.centerY );
942         aRadius = this.manager.Pix2Geo( parseInt(this.centerX) + parseInt(this.radius),
943                                                                 this.centerY );
944     }
945
946     szURL = "type=" + this.type;
947     szURL = szURL + "&mode=" + this.mode;
948     szURL = szURL + "&coords=" + aCenter[0] + ',' +  aCenter[1] + ',' +
949                                                 aRadius[0] + ',' + aRadius[1];
950     szURL = szURL + "&edgecolor=" + escape(this.edgeColor);
951     szURL = szURL + "&edgewidth=" + this.edgeWidth;
952     szURL = szURL + "&fillcolor=" + escape(this.fillColor);
953     if (CWCIsIE)
954     {
955         szURL = szURL + "&fillopacity=-1";
956     }
957     else
958     {
959         szURL = szURL + "&fillopacity=" + this.fillOpacity;
960     }
961     return szURL;
962 }
963
964 /******************************************************************************
965  * a Polygon ROI
966  *****************************************************************************/
967 function CWCPolygonROI()
968 {
969     this.type = "polygon";
970     this.manager = null;
971     this.mode = ROI_MODE_NORMAL;
972    
973     this.edgeColor = "#00FF00";
974     this.edgeWidth = 2;
975
976     // set the operating limits for this tool
977     this.left = -2;
978     this.top = -2;   
979     this.right = -2;   
980     this.bottom = -2;   
981    
982     this.fillColor = this.edgeColor;
983     this.fillOpacity = 0.5;
984     this.fillLayer = null;
985     this.bFill = true;
986    
987     this.aNodeCoords = new Array();
988    
989     this.visible = false;
990    
991     this.Draw = CWCPolygonROI_Draw;
992     this.Show = CWCPolygonROI_Show;
993     this.Hide = CWCPolygonROI_Hide;
994     this.GetRendererURL = CWCPolygonROI_GetRendererURL;
995     this.GetROI = CWCPolygonROI_GetROI;
996     this.GetRendererURL = CWCPolygonROI_GetRendererURL;
997     this.Serialize = CWCPolygonROI_Serialize;
998
999 }
1000 function CWCPolygonROI_Draw(){return null;}
1001 function CWCPolygonROI_Show(){return null;}
1002 function CWCPolygonROI_Hide(){return null;}
1003 function CWCPolygonROI_GetRendererURL(){return null;}
1004 function CWCPolygonROI_GetROI(){return null;}
1005
1006 function CWCPolygonROI_GetRendererURL()
1007 {
1008     var szSerialized = this.Serialize();
1009     if (!szSerialized)
1010         return false;
1011        
1012     var szURL = this.manager.GetRendererURL();
1013    
1014     szURL = szURL + "&" + szSerialized;
1015     return szURL;
1016 }
1017
1018 function CWCPolygonROI_Serialize()
1019 {
1020     // init vars
1021     var szCoords = "";
1022     var szURL;
1023     var aTmp;
1024    
1025     // loop and build string of coordinates
1026     for ( var i=0; i < this.aNodeCoords.length; i++ )
1027     {
1028         // convert to ground units
1029         aTmp = this.manager.Pix2Geo( (this.aNodeCoords[i][0]- this.left),
1030             ( this.aNodeCoords[i][1] - this.top ) );
1031
1032         // store
1033         szCoords += aTmp[0] + ',' + aTmp[1];
1034        
1035         //szCoords += (this.aNodeCoords[i][0]  + ',' + ( this.aNodeCoords[i][1] - this.top);
1036
1037         // add separator if necessary
1038         if ( i < this.aNodeCoords.length - 1 )
1039             szCoords += ',';
1040     }
1041
1042     szURL = "type=" + this.type;
1043     szURL = szURL + "&mode=" + this.mode;
1044     szURL = szURL + "&coords=" + szCoords;
1045     szURL = szURL + "&edgecolor=" + escape(this.edgeColor);
1046     szURL = szURL + "&edgewidth=" + this.edgeWidth;
1047     szURL = szURL + "&fillcolor=" + escape(this.fillColor);
1048     if (CWCIsIE)
1049     {
1050         szURL = szURL + "&fillopacity=-1";
1051     }
1052     else
1053     {
1054         szURL = szURL + "&fillopacity=" + this.fillOpacity;
1055     }
1056     return szURL;
1057 }
Note: See TracBrowser for help on using the browser.