summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/Sim_Display/Display_Art_Horizon.java
blob: 4b5c9fd4fe9c10215dc1dc096babd0b0f5ca3ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// $Id$


import java.awt.*;
import java.io.*;

public class Display_Art_Horizon
  extends Panel
  implements Display_Object
{
  private Alt_Horizon alt_hor_ = new Alt_Horizon ();
  private Position pos_ = new Position ();

  public Display_Art_Horizon ()
    {
      setLayout (new BorderLayout (0, 2));
      add ("Center", alt_hor_);
      add ("South", pos_);
    }

  public int update_display (Display_Push_Consumer display_push_consumer) 
    {
      Navigation navigation = display_push_consumer.get_navigation ();
      /*
      System.out.println ("Art_Horizon: lat " + latitude + " long " + longitude + " alt " +
			  altitude + " heading " + heading + " roll " +
			  roll + " pitch " + pitch); 
			  */
      
      alt_hor_.update_display (navigation.roll, navigation.pitch);
      pos_.update_display (navigation.position_latitude, navigation.position_longitude, navigation.altitude, navigation.heading);
      
      return 0;
    }
}

class Alt_Horizon
extends Canvas
{
  private final static Color GREEN = new Color (0, 100, 0),
    BLUE = new Color (30, 144, 255);
  
  private Graphics offgraphics_;
  private Image offscreen_;
  private Dimension offscreensize_;

  private int roll_ = 0, pitch_ = 0;
  
  public void update_display (int roll, int pitch)
    {
      roll_ = roll;
      pitch_ = pitch;

      repaint ();
    }
  
  public Dimension getPreferredSize ()
    {
      return new Dimension (180, 180);
    }
  
  public void paint (Graphics g)
    {
      update (g);
    }
  
  public void update (Graphics g)
    {
      Dimension d = getSize ();
      int rad, angles[] = { 180, 0 };
      Point center;
      
      if ((offscreen_ == null) || (d.width != offscreensize_.width) ||
	  (d.height != offscreensize_.height))
	{
	  offscreen_ = createImage (d.width, d.height);
	  offscreensize_ = new Dimension (d.width, d.height);
	  offgraphics_ = offscreen_.getGraphics ();
	  offgraphics_.setFont (getFont());
	  
	  //	  g.setColor (Color.lightGray);
	  //	  g.draw3DRect (0, 0, d.width - 1, d.height - 1, true);
	  //	  g.draw3DRect (1, 1, d.width - 3, d.height - 3, true);
	  //	  g.draw3DRect (2, 2, d.width - 5, d.height - 5, true);
	}
      
      offgraphics_.setColor (getBackground());
      offgraphics_.fillRect (0, 0, d.width, d.height);
      offgraphics_.setColor (BLUE);
      
      // Calculate from the dimensions, the largest square.
      center = new Point (d.width / 2, d.height / 2);
      rad = ((center.x < center.y) ? center.x : center.y);

      // Draw a circle of blue
      offgraphics_.fillOval (center.x - rad, center.y - rad,
			     2*rad, 2*rad);

      // Roll the horizon based on the roll angle
      if (roll_ != 0)
	roll_horizon (rad, angles);

      // Pitch the horizon based on the pitch angle
      if (pitch_ != 0)
	pitch_horizon (rad, angles);

      // Draw the resulting terrain
      draw_horizon (rad, center, angles);

      // Draw the plotted Image. 
      g.drawImage (offscreen_, 0, 0, null);
    }

  private void draw_horizon (int rad, Point center, int[] angles)
    {
      // Draw an arc
      int arc_angle =
	((angles[0] > angles[1]) ?
	 (360 - angles[0]) + angles[1] :
	 (angles[1] - angles[0]));
      
      Polygon remainder = new Polygon ();

      offgraphics_.setColor (GREEN);
      offgraphics_.fillArc (center.x - rad, center.y - rad,
			    2*rad, 2*rad,
			    angles[0], arc_angle);

      /*
      System.out.println ("Start Angle " + angles[0] + 
      			  " End Angle " + angles[1] + 
      			  " Arc Angle " + arc_angle);
			  */
      if (pitch_ != 0)
	{
	  if ((pitch_ > 0 && Math.abs (roll_) < 90) ||
	      (pitch_ < 0 && Math.abs (roll_) >= 90))
	    offgraphics_.setColor (BLUE);

	  int cover_angle = (angles[0] + arc_angle/2 + ((arc_angle < 180) ? 180 : 0)) % 360;

	  // System.out.println (points[0] + " " + points[1]);

	  // System.out.println (accepted_point);
	  
	  remainder.addPoint (center.x + polar_to_rect_x (rad, cover_angle),
			      center.y - polar_to_rect_y (rad, cover_angle)); 
	  remainder.addPoint (center.x + polar_to_rect_x (rad, angles[0]),
			      center.y - polar_to_rect_y (rad, angles[0]));
	  remainder.addPoint (center.x + polar_to_rect_x (rad, angles[1]),
			      center.y - polar_to_rect_y (rad, angles[1]));
	  offgraphics_.fillPolygon (remainder);
	  //offgraphics_.setColor (getBackground ());
	  //offgraphics_.drawPolygon (remainder);
	}	
    }
  
  private void pitch_horizon (int rad, int[] angles)
    {
      boolean upside_down = Math.abs (roll_) >= 90;
      int angle_shift = (int) Math.round ((double)(90 - (Math.abs (roll_) % 180)) / 90.0 * pitch_);

      //      System.out.println ("angle_shift " + angle_shift);

      angles[0] += angle_shift;
      angles[1] -= angle_shift;
      
      // Shift the terrain line to account for the pitch.
      //      if (Math.abs (roll_) != 90)
      //	{
	  /*
	    boolean upside_down = Math.abs (roll_) >= 90;
	    double offset = - (double)rad * (double)pitch_ / 90.0;
	    double y_intercept = (Math.abs (roll_) <= 90) ? offset : offset;
	    double slope = caclulate_slope (rad, angles);
	    Point[] roots = null;
	    int x_l, x_r, y_l, y_r;
	    
	    roots = line_circle_intesect (rad, y_intercept, slope);
	    
	    // System.out.println ("Y intercept " + y_intercept + " slope " + slope);
	    // System.out.println ("Root x1 " + roots[0].x + " x2 " + roots[1].x);
	    // System.out.println ("Root y1 " + roots[0].y + " y2 " + roots[1].y);
	    // System.out.println ("Was left_point.x " + left_point.x + " left_point.y " + left_point.y);
	    // System.out.println ("Was right_point.x " + right_point.x + " right_point.y " + right_point.y);
	    
	    x_l = upside_down ? roots[0].x : roots[1].x;
	    y_l = upside_down ? roots[0].y : roots[1].y;
	    x_r = upside_down ? roots[1].x : roots[0].x;
	    y_r = upside_down ? roots[1].y : roots[0].y;
	    
	    angles[0] = calculate_angle (rad, x_l, y_l);
	    angles[1] = calculate_angle (rad, x_r, y_r);
	  
	  // System.out.println ("Now left_point.x " + left_point.x + " left_point.y " + left_point.y);
	  // System.out.println ("Now right_point.x " + right_point.x + " right_point.y " + right_point.y);
	  */
      //	}
    }
  
  private void roll_horizon (int rad, int[] angles)
    {
      // Roll the left and right points of the terrain.
      angles[0] += roll_;
      angles[1] += roll_;

      if (angles[0] < 0)
	angles[0] += 360;

      if (angles[1] < 0)
	angles[1] += 360;
      
	  //	  boolean rightside_up = Math.abs (roll_) <= 90,
	  //	    positive_roll = roll_ >= 0;
	  //	  double roll_rad = (double) roll_ * Math.PI / 180.0;	  
	  //	  Point tmp =
	  //	    new Point ((int) Math.round ((double) rad * Math.cos (roll_rad)),
	  //		       (int) Math.round ((double) rad * Math.sin (roll_rad)));

	  // System.out.println ("Was left_point.x " + left_point.x + " left_point.y " + left_point.y);
	  // System.out.println ("Was right_point.x " + right_point.x + " right_point.y " + right_point.y);

	  //	  right_point.x = tmp.x;
	  //	  left_point.x  = -tmp.x;
	  //	  right_point.y = tmp.y;
	  //	  left_point.y  = -tmp.y;
	  
	  // System.out.println ("Now left_point.x " + left_point.x + " left_point.y " + left_point.y);
	  // System.out.println ("Now right_point.x " + right_point.x + " right_point.y " + right_point.y);
    }

  private int polar_to_rect_x (int rad, int angle)
    {
      return (int) Math.round (rad * Math.cos ((double)angle * Math.PI/180.0));
    }

  private int polar_to_rect_y (int rad, int angle)
    {
      return (int) Math.round (rad * Math.sin ((double)angle * Math.PI/180.0));
    }

  private double caclulate_slope (int rad, int[] angles)
    {
      int x1 = polar_to_rect_x (rad, angles[0]),
	x2 = polar_to_rect_x (rad, angles[1]),
	y1 = polar_to_rect_y (rad, angles[0]),
	y2 = polar_to_rect_y (rad, angles[1]);

      return ((double) (y2 - y1)) / ((double) (x2 - x1));
    }
  
  private Point[] line_circle_intesect (int rad, double y_intercept, double slope)
    {
      double r_2 = (double)(rad * rad),
	s_2 = slope * slope,
	a_x = s_2 + 1,
	b_x = 2.0 * slope * y_intercept,
	c_x = y_intercept * y_intercept - r_2;
      int[] x_roots = quad_eq (a_x, b_x, c_x),
	y_roots = { (int) Math.round ((double)((double) x_roots[0])*slope + y_intercept),
		    (int) Math.round ((double)((double) x_roots[1])*slope + y_intercept) };
      Point[] points = new Point [2];

      points[0] = new Point (x_roots[0], y_roots[0]);
      points[1] = new Point (x_roots[1], y_roots[1]);

      return points;
    }
  
  private int calculate_angle (int rad, int x, int y)
    {
      /*
	double angle = 0,
      sin_value = Math.asin ((double)y / (double)rad),
      tan_value = Math.atan ((double)y / (double)x);

	if (x >= 0)
      angle = (x !=  0) ? tan_value : sin_value +
      ((y < 0) ? 2*Math.PI : 0);
	  else
      angle = Math.PI + tan_value;

      return (int) Math.round (angle * 180.0 / Math.PI);
      */

      double angle = 0.0,
	sin_value = Math.asin ((double)Math.abs (y) / (double)rad);

      if (x >= 0 && y >= 0)
	angle = sin_value;
      else if (x < 0 && y >= 0)
	angle = sin_value + Math.PI/2.0;
      else if (x < 0 && y < 0)
	angle = sin_value + Math.PI;
      else if (x >= 0 && y < 0)
	angle = sin_value + 3.0*Math.PI/2.0;

      return (int) Math.round (angle * 180.0 / Math.PI);
    }

  private int[] quad_eq (double a, double b, double c)
    {
      int[] roots = new int [2];
      double body = Math.sqrt (b*b - 4.0*a*c);
      
      roots[0] = (int) Math.round ((-b + body) / (2.0 * a));
      roots[1] = (int) Math.round ((-b - body) / (2.0 * a));

      return roots;
    }

  private int distance (Point point1, Point point2)
    {
      double xdiff = point1.x - point2.x,
	ydiff = point1.y - point2.y;

      return (int) Math.round (Math.sqrt (xdiff*xdiff + ydiff*ydiff));
    }
}

class Position
extends Panel
{
  private final static Font FONT = new Font ("Dialog", Font.BOLD, 12);
  private final static char DEGREE = '\u00B0'; 
  
  private Label lat_ = new Label ("0" + DEGREE + " N", Label.RIGHT),
    long_ = new Label ("0" + DEGREE + " S", Label.RIGHT),
    alt_ = new Label ("0 Kft", Label.RIGHT),
    heading_ = new Label ("0" + DEGREE + "  ", Label.RIGHT);
  
  public Position ()
    {
      Panel grid_panel = new Panel ();
      
      lat_.setFont (FONT);
      long_.setFont (FONT);
      alt_.setFont (FONT);
      heading_.setFont (FONT);

      setLayout (new GridLayout (1, 4));
      add (lat_);
      add (long_);
      add (heading_);
      add (alt_);
      
      //      lat_.setBackground (Color.lightGray);
      //      long_.setBackground (Color.lightGray);
      //      alt_.setBackground (Color.lightGray);
      //      heading_.setBackground (Color.lightGray);

      //      setBackground (Color.black);
      //      setLayout (new CardLayout (1, 1));

      //      grid_panel.setBackground (Color.black);
      //      grid_panel.setLayout (new GridLayout(1, 4, 1, 0));
      //      grid_panel.add (lat_);
      //      grid_panel.add (long_);
      //      grid_panel.add (alt_);
      //      grid_panel.add (heading_);

      //      add ("Grid Panel", grid_panel);
    }

  public void update_display (int lat, int lon, int alt, int heading)
    {
      String lat_str =
	Math.abs (lat) + "" + DEGREE + ((lat > 0) ? " N" : " S");
      String long_str =
	Math.abs (lon) + "" + DEGREE + ((lon > 0) ? " E" : " W");

      lat_.setText (lat_str);
      long_.setText (long_str);
      alt_.setText (alt + " Kft");
      heading_.setText (heading + "" + DEGREE + "  ");
    }
}