| 1 | /*************************************************************************** |
| 2 | targetmotionanalysis.cpp - description |
| 3 | ------------------- |
| 4 | begin : Sat Mar 16 2002 |
| 5 | copyright : (C) 2002 by Michael Bridak |
| 6 | email : [email protected] |
| 7 | $Id: targetmotionanalysis.cpp,v 1.7 2003/05/17 22:27:28 mbridak Exp $ |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | /*************************************************************************** |
| 11 | * * |
| 12 | * This program is free software; you can redistribute it and/or modify * |
| 13 | * it under the terms of the GNU General Public License as published by * |
| 14 | * the Free Software Foundation; either version 2 of the License. * |
| 15 | * . * |
| 16 | * * |
| 17 | ***************************************************************************/ |
| 18 | |
| 19 | #include "SDL/SDL.h" |
| 20 | #include "SDL/SDL_image.h" |
| 21 | #include "SDL/SDL_thread.h" |
| 22 | #include "targetmotionanalysis.h" |
| 23 | #include "math.h" |
| 24 | |
| 25 | TargetMotionAnalysis::TargetMotionAnalysis(){ |
| 26 | MutexLock = SDL_CreateMutex(); |
| 27 | plotscale = 1; |
| 28 | centerGeoPlot = true; |
| 29 | } |
| 30 | |
| 31 | TargetMotionAnalysis::~TargetMotionAnalysis(){ |
| 32 | SDL_DestroyMutex(MutexLock); |
| 33 | } |
| 34 | |
| 35 | /** Returns TRUE if tracker is in use */ |
| 36 | bool TargetMotionAnalysis::GetTrackerState(int tracker) const{ |
| 37 | return trackerstate[tracker]; |
| 38 | } |
| 39 | |
| 40 | /** Frees a tracker to be used later. */ |
| 41 | void TargetMotionAnalysis::ClearTracker(int tracker){ |
| 42 | trackerstate[tracker]=false; |
| 43 | } |
| 44 | |
| 45 | /** Assign a tracker to a target */ |
| 46 | void TargetMotionAnalysis::AssignTracker(int tracker, int target){ |
| 47 | trackerstate[tracker]=true; |
| 48 | } |
| 49 | |
| 50 | /** Assign Target designation and Tracker to trace currently under |
| 51 | cursor on SONAR screen. */ |
| 52 | void TargetMotionAnalysis::DesignateContact(){ |
| 53 | } |
| 54 | |
| 55 | /** Display a GeoPlot for the designated contact based on the |
| 56 | position of the boat and the bearing history of the contact. */ |
| 57 | void TargetMotionAnalysis::DisplayGeoPlot(int xoffset, int yoffset){ |
| 58 | ClearGeoPlot(); |
| 59 | if(size !=0){ |
| 60 | PlotHistory(plotscale,xoffset,yoffset); |
| 61 | } |
| 62 | SDL_Rect rectangle; |
| 63 | rectangle.x=0; |
| 64 | rectangle.y=0; |
| 65 | rectangle.h=500; |
| 66 | rectangle.w=500; |
| 67 | SDL_UpdateRects(GeoPlotScreen, 1, &rectangle); |
| 68 | } |
| 69 | |
| 70 | /** Creates a speed ruler which we will use to drag around the |
| 71 | GeoPlot to test solutions. |
| 72 | Extend ruler backwards in time by plotting course using reciprocal |
| 73 | bearings. */ |
| 74 | void TargetMotionAnalysis::CreateSpeedRuler(){ |
| 75 | } |
| 76 | |
| 77 | /** Display the LOS Diagram*/ |
| 78 | void TargetMotionAnalysis::DisplayLOS(){ |
| 79 | |
| 80 | double bearing_to_target; |
| 81 | double relative_bearing; |
| 82 | double recipbearing; |
| 83 | double var1;//a place to hold a value so we don't have to calc it twice |
| 84 | double destinationx; //the dest x&y point for |
| 85 | double destinationy; //the drawn line. |
| 86 | double direction; |
| 87 | int x,y; |
| 88 | |
| 89 | bearing_to_target = storage[size-1].bearing; |
| 90 | ClearGeoPlot(); |
| 91 | |
| 92 | //Draw LOS |
| 93 | DrawLine(250, 292, 250, 100, white); |
| 94 | |
| 95 | |
| 96 | //Draw our ships angle relative to the LOS |
| 97 | if(our_heading > bearing_to_target) bearing_to_target += 360; |
| 98 | relative_bearing = bearing_to_target - our_heading; |
| 99 | |
| 100 | x=250; |
| 101 | y=292; |
| 102 | direction = 360 - relative_bearing; |
| 103 | our_speed=(our_speed*3); |
| 104 | var1 = direction * 0.017453; //convert degrees to radians. |
| 105 | destinationx = (sin(var1) * our_speed) + x; |
| 106 | destinationy = (-1) * (cos(var1) * our_speed) + y; |
| 107 | DrawLine(x, y, (int)destinationx, (int)destinationy, yellow); |
| 108 | |
| 109 | //Draw targets angle relative to the LOS |
| 110 | x=250; |
| 111 | y=100; |
| 112 | if (bearing_to_target >= 180){ |
| 113 | recipbearing = bearing_to_target - 180; |
| 114 | }else{ |
| 115 | recipbearing = bearing_to_target + 180; |
| 116 | } |
| 117 | direction = target_heading + (180 - recipbearing); |
| 118 | target_speed=(target_speed*3); |
| 119 | var1 = direction * 0.017453; //convert degrees to radians. |
| 120 | destinationx = (sin(var1) * target_speed) + x; |
| 121 | destinationy = (-1) * (cos(var1) * target_speed) + y; |
| 122 | DrawLine(x, y, (int)destinationx, (int)destinationy, red); |
| 123 | |
| 124 | SDL_Rect rectangle; |
| 125 | rectangle.x=0; |
| 126 | rectangle.y=0; |
| 127 | rectangle.h=500; |
| 128 | rectangle.w=500; |
| 129 | SDL_UpdateRects(GeoPlotScreen, 1, &rectangle); |
| 130 | |
| 131 | } |
| 132 | |
| 133 | /** Plots the amount of error from the expected bearing to target |
| 134 | and the actual bearing recieved. */ |
| 135 | void TargetMotionAnalysis::BearingDevitaionPlot(){ |
| 136 | } |
| 137 | |
| 138 | /** Record the boats current position*/ |
| 139 | void TargetMotionAnalysis::RecordBoatPosition(float latitude, float longitude, double bearing, Uint32 tick){ |
| 140 | Lock(); |
| 141 | position temp; |
| 142 | temp.lat = latitude; |
| 143 | temp.lon = longitude; |
| 144 | temp.bearing = bearing; |
| 145 | temp.tick = tick; |
| 146 | RecBoatPos(temp); |
| 147 | UnLock(); |
| 148 | } |
| 149 | |
| 150 | /** Lock Mutex */ |
| 151 | void TargetMotionAnalysis::Lock(){ |
| 152 | SDL_mutexP(MutexLock); |
| 153 | } |
| 154 | |
| 155 | /** Unlock Mutex */ |
| 156 | void TargetMotionAnalysis::UnLock(){ |
| 157 | SDL_mutexV(MutexLock); |
| 158 | } |
| 159 | |
| 160 | /** Initialize the graphics */ |
| 161 | void TargetMotionAnalysis::InitGraphics(){ |
| 162 | SDL_Surface *screen = SDL_GetVideoSurface(); |
| 163 | SDL_Surface *temp = SDL_CreateRGBSurface(SDL_SWSURFACE, 500, 500, 16, |
| 164 | screen->format->Rmask, |
| 165 | screen->format->Gmask, |
| 166 | screen->format->Bmask, |
| 167 | screen->format->Amask); |
| 168 | GeoPlotScreen=SDL_DisplayFormat(temp); |
| 169 | SDL_FreeSurface(temp); |
| 170 | textcolor = SDL_MapRGB(screen->format, 192, 255, 6); |
| 171 | black = SDL_MapRGB(screen->format, 0, 0, 0); |
| 172 | white = SDL_MapRGB(screen->format, 255, 255, 255); |
| 173 | red = SDL_MapRGB(screen->format, 255, 99, 20); |
| 174 | green = SDL_MapRGB(screen->format, 0, 255, 0); |
| 175 | yellow = SDL_MapRGB(screen->format, 216, 255, 0); |
| 176 | mapcolor = SDL_MapRGB(screen->format, 130, 201, 225); |
| 177 | ClearGeoPlot(); |
| 178 | } |
| 179 | /** No descriptions */ |
| 180 | void TargetMotionAnalysis::PlotPixel(int x, int y, Uint32 color){ |
| 181 | if (x > 499 || x < 0 || y > 499 || y < 0) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | //place the pixel on the screen |
| 186 | Uint16 *pixel_location; |
| 187 | pixel_location = (Uint16 *)GeoPlotScreen->pixels + y*GeoPlotScreen->pitch/2 + x; |
| 188 | *pixel_location = color; |
| 189 | } |
| 190 | /** erases the work screen */ |
| 191 | void TargetMotionAnalysis::ClearGeoPlot(){ |
| 192 | SDL_Rect rectangle; |
| 193 | rectangle.x=0; |
| 194 | rectangle.y=0; //define a rectangle on the screen and make it black |
| 195 | rectangle.h=500; |
| 196 | rectangle.w=500; |
| 197 | SDL_FillRect(GeoPlotScreen, &rectangle, black); |
| 198 | SDL_UpdateRects(GeoPlotScreen, 1, &rectangle); |
| 199 | } |
| 200 | /** Plot the boats history */ |
| 201 | void TargetMotionAnalysis::PlotHistory(int scale, int change_scrollx, int change_scrolly){ |
| 202 | //scale is in YDS per pixel |
| 203 | int x, y; //where to place the ships |
| 204 | int xoffset = 0; //offsets to move the ships to |
| 205 | int yoffset = 0; //the defined place on map screen |
| 206 | static int scrolloffsetx=0; //offset to center map |
| 207 | static int scrolloffsety=0; |
| 208 | double var1; //just a place to hold a value so we don't have to calc it twice. |
| 209 | double destinationx; //the dest X&Y point for |
| 210 | double destinationy; //the drawn line. |
| 211 | int length = 2000; |
| 212 | |
| 213 | scale = scale * 5; |
| 214 | if(centerGeoPlot){ //center map onto our own ships location |
| 215 | scrolloffsetx = 250 - (int)(storage[size-1].lat / scale); |
| 216 | scrolloffsety = 250 - (int)(storage[size-1].lon / scale); |
| 217 | }else{ |
| 218 | scrolloffsetx += change_scrollx; |
| 219 | scrolloffsety += change_scrolly; |
| 220 | } |
| 221 | for (int index = 0; index < size; index ++){ |
| 222 | x = 500 - (int)(storage[index].lat / scale); |
| 223 | x = x - scrolloffsetx; |
| 224 | y = 500 - (int)(storage[index].lon / scale); |
| 225 | y = y - scrolloffsety; |
| 226 | if (x>10 && x<490 && y>10 && y<490){ |
| 227 | x = x + xoffset; |
| 228 | y = y + yoffset; |
| 229 | DrawDot(x, y, yellow); |
| 230 | } |
| 231 | var1 = storage[index].bearing * 0.017453; //convert degrees to radians. |
| 232 | destinationx = (sin(var1) * length) + x; |
| 233 | destinationy = (-1) * (cos(var1) * (length/.75)) + y; //adjust to account for 4:3 screen ratio. |
| 234 | DrawLine(x, y, (int)destinationx, (int)destinationy, white); |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | /** Draws a line....What were you expecting? */ |
| 239 | void TargetMotionAnalysis::DrawLine(int X1,int Y1,int X2,int Y2,Uint32 Color){ |
| 240 | int dx,dy,sdx,sdy,py,px,x,y; |
| 241 | dx = X2 - X1; |
| 242 | dy = Y2 - Y1; |
| 243 | if (dx < 0) sdx = -1; |
| 244 | else sdx = 1; |
| 245 | if (dy < 0) sdy = -1; |
| 246 | else sdy = 1; |
| 247 | dx = sdx * dx + 1; |
| 248 | dy = sdy * dy + 1; |
| 249 | x = 0; |
| 250 | y = 0; |
| 251 | px = X1; |
| 252 | py = Y1; |
| 253 | if (dx >= dy){ |
| 254 | for (int x = 0; x < dx; x++){ |
| 255 | PlotPixel(px, py, Color); |
| 256 | y = y + dy; |
| 257 | if (y >= dx){ |
| 258 | y = y - dx; |
| 259 | py = py + sdy; |
| 260 | } |
| 261 | px = px + sdx; |
| 262 | } |
| 263 | }else{ |
| 264 | for (int y = 0; y < dy; y++){ |
| 265 | PlotPixel(px, py, Color); |
| 266 | x = x + dx; |
| 267 | if (x >= dy){ |
| 268 | x = x - dy; |
| 269 | px = px + sdx; |
| 270 | } |
| 271 | py = py + sdy; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | /** No descriptions */ |
| 276 | void TargetMotionAnalysis::DrawDot(int x, int y, Uint32 color){ |
| 277 | int xc, yc, radius; |
| 278 | int theta, theta1, theta2; |
| 279 | xc = x; |
| 280 | yc = y; |
| 281 | radius = 2; |
| 282 | theta1 = 0; |
| 283 | theta2 = 360; |
| 284 | |
| 285 | for(theta=theta1;theta<=theta2;theta+=5) { |
| 286 | x = xc + int(radius*cos(theta*3.14/180.0)); |
| 287 | y = yc - int(radius*sin(theta*3.14/180.0)); |
| 288 | PlotPixel(x, y, color); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /** Increments plotscale to zoom in on the GeoPlot */ |
| 293 | void TargetMotionAnalysis::IncreasePlotScale(){ |
| 294 | plotscale++; |
| 295 | if(plotscale > 15){ |
| 296 | plotscale = 15; |
| 297 | } |
| 298 | } |
| 299 | /** Decriment the plotscale to zoom out of GeoPlot */ |
| 300 | void TargetMotionAnalysis::DecreasePlotScale(){ |
| 301 | plotscale--; |
| 302 | if(plotscale < 1){ |
| 303 | plotscale = 1; |
| 304 | } |
| 305 | } |
| 306 | /** No descriptions */ |
| 307 | void TargetMotionAnalysis::ToggleGeoPlotCenter(){ |
| 308 | centerGeoPlot = !centerGeoPlot; |
| 309 | } |
| 310 | /** Fill in the initial values for the solution. */ |
| 311 | void TargetMotionAnalysis::InitializeSolution(float LatYards, float LonYards, int Speed, int Heading){ |
| 312 | Solution[0].Lat_TotalYards = LatYards; |
| 313 | Solution[0].Lon_TotalYards = LonYards; |
| 314 | Solution[0].Speed = Speed; |
| 315 | Solution[0].Heading = Heading; |
| 316 | } |