Open Sim Global Calculations BRIGADOON-0011
Provide a php class that provides a series of calculations using global coordinates
Loading...
Searching...
No Matches
os_global_calcs.php
Go to the documentation of this file.
1<?php
18
19// Passwords for the database are stored outside of the webserver accessible directories to improve security.
20// Ensure that passwor file has the correct permission to read, but not be written.
21include '/usr/local/share/brigadoon/apache/os_passwords.php';
22
33{
34 // Database parameters that are obtained when the constructor is run
35
40 public $Database;
41
46 public $Host;
47
52 public $Username;
53
58 public $Password;
59
60
72 {
73 global $pdo;
74
75 // Data Source Name (DSN) string
76 $dsn = "mysql:host=$Host;dbname=$Database;charset=utf8mb4";
77
78 // Set PDO options for better error handling and security
79 $options = [
80 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // Throw exceptions on errors
81 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // Set default fetch mode to associative array
82 PDO::ATTR_EMULATE_PREPARES => true, // Disable emulated prepares for better security
83 ];
84
85 try
86 {
87 // Create a new PDO connection instance
88 $pdo = new PDO($dsn, $Username, $Password, $options);
89 }
90 catch (PDOException $e)
91 {
92 // Catch the exception and display a user-friendly error message
93 // In a production environment, you might log the specific error
94
95 die("FAILURE=DATABASE:Connection failed " . $e->getMessage());
96 }
97 }
98
130 public function GlobalToLocal($GlobalArray)
131 {
132 global $pdo;
133 $sql_query = " select lor_name as region_name, :global_x - lor_edge_left as local_x, :global_y - lor_edge_lower as local_y, :global_z as local_z from ls_os_region ";
134 $sql_query .= " where :global_x >= lor_edge_left and :global_x <= lor_edge_right and :global_y >= lor_edge_lower and :global_y <= lor_edge_upper ";
135
136 try
137 {
138 $stmt = $pdo->prepare($sql_query);
139 $stmt->execute($GlobalArray);
140 $results = $stmt->fetchAll(PDO::FETCH_DEFAULT);
141
142 //#########################################################################################
143 //# Process Each Line #
144 //#########################################################################################
145
146 if ($results)
147 {
148 return $results[0];
149 }
150
151 // If the Code gets here, then the global location is outside of the known world.
152 else
153 {
154 $result['region_name'] = ""; // Flag no known Region
155 $result['local_x'] = -1.0; // Flag bad X reading
156 $result['local_y'] = -1.0; // Flag bad Y reading
157 $result['local_z'] = -1.0; // Flag bad Z reading
158 return $result;
159 }
160 }
161 catch (PDOException $e)
162 {
163 // Catch the exception and display a user-friendly error message
164 // In a production environment, you might log the specific error
165
166 die("FAILURE=DATABASE:Connection failed " . $e->getMessage());
167 }
168 }
169
179 public function LocalToGlobal($LocalArray)
180 {
181 global $pdo;
182
183 $sql_query = "select lor_edge_left + :local_x as global_x, lor_edge_lower + :local_y as global_y, :local_z as global_z from ls_os_region where lor_name = :region_name";
184
185 try
186 {
187 $stmt = $pdo->prepare($sql_query);
188 $stmt->execute($LocalArray);
189 $results = $stmt->fetchAll(PDO::FETCH_DEFAULT);
190
191 //#########################################################################################
192 //# Process Each Line #
193 //#########################################################################################
194
195 if ($results)
196 {
197 return $results[0];
198 }
199 }
200 catch (PDOException $e)
201 {
202 // Catch the exception and display a user-friendly error message
203 // In a production environment, you might log the specific error
204
205 die("FAILURE=DATABASE:Connection failed " . $e->getMessage());
206 }
207 }
208
230 public function CourseFromTo($FromLocalArray, $ToLocalArray)
231 {
232 $FromGlobal = self::LocalToGlobal($FromLocalArray); $ToGlobal = self::LocalToGlobal($ToLocalArray);
233 $delta_global['global_x'] = $ToGlobal['global_x'] - $FromGlobal['global_x'];
234 $delta_global['global_y'] = $ToGlobal['global_y'] - $FromGlobal['global_y'];
235 $delta_global['global_z'] = $ToGlobal['global_z'] - $FromGlobal['global_z'];
236 $ground_distance = sqrt($delta_global['global_x'] ** 2.0 + $delta_global['global_y'] ** 2.0);
237 $result['distance'] = sqrt($ground_distance ** 2.0 + $delta_global['global_z'] ** 2);
238 $result['bearing'] = atan2($delta_global['global_x'], $delta_global['global_y']);
239 $result['elevation'] = atan2($delta_global['global_z'], $ground_distance) ;
240 return $result;
241 }
242
259 public function FromCourseTo($FromLocalArray, $CourseArray)
260 {
261 $FromGlobal = self::LocalToGlobal($FromLocalArray);
262 $total_distance = $CourseArray['distance'];
263 $elevation_angle = $CourseArray['elevation'];
264 $bearing_angle = $CourseArray['bearing'];
265
266 $ground_distance = $total_distance * cos($elevation_angle);
267 $delta_z = $total_distance * sin($elevation_angle);
268
269 $delta_x = $ground_distance * sin($bearing_angle);
270 $delta_y = $ground_distance * cos($bearing_angle);
271
272 $GlobalArray['global_x'] = $FromGlobal['global_x'] + $delta_x;
273 $GlobalArray['global_y'] = $FromGlobal['global_y'] + $delta_y;
274 $GlobalArray['global_z'] = $FromGlobal['global_z'] + $delta_z;
275
276 return self::GlobalToLocal($GlobalArray);
277 }
278
279}
280
281/***************************************************************************************************
282 * TEST PROGRAM ENTRY
283 ***************************************************************************************************
284 * These routines are both an example of how to use the class and method of testing each of the routines
285 * in case a potential error is detected.
286 ***************************************************************************************************/
287
288 // Instantiate the Class with the database parameters. These parameters are provided by include file
289 // near the start of this file.
290 $global_calcs = new OS_GLOBAL_CALCS($db, $host, $db_username, $db_password);
291
292 // Examples of how data is formatted for inpuut to and output from the routines. This format makes the array
293 // directly compatible as the parameter field in the SQL queries.
294 $from_array = ['region_name'=>'BRIGADOON13', 'local_x' => 128, 'local_y' => 256, 'local_z' => 128];
295 $to_array = ['region_name'=>'BRIGADOON13', 'local_x' => 256, 'local_y' => 128, 'local_z' => 256];
298
299 // Convert the Local Coordinates into Global Coordinates
300 $global = $global_calcs->LocalToGlobal($from_array);
301 echo "LocalToGlobal: Global X = ".$global['global_x'].", Global Y = ".$global['global_y'].", Global Z = ".$global['global_x']."\n\n";
302
303 // Convert the Global Coordinates back to the Local Coordinates. They should be unchanged after the two conversion.
304 $from_array = $global_calcs->GlobalToLocal($global);
305 echo "GlobalToLocal: Region Name = ".$from_array['region_name'].", local_x = ".$from_array['local_x'].", local_y = ".$from_array['local_y'].", local_z = ".$from_array['local_z']."\n\n";
306
307 // Take an Origin Local Location, convert to global, move the local and convert back into Local Coordinates
308 $course = $global_calcs->CourseFromTo($from_array, $to_array);
309 echo "CourseFromTo: Distance = ".$course['distance'].", bearing = ".rad2deg($course['bearing']). ", elevation = ".rad2deg($course['elevation'])."\n\n";
310
311 // Try to Move back to the Orgin using the previous Destination as the start and reverse the calculated course to get back the Origin in Local Coordinates
312 $course['bearing'] += M_PI;
313 $course['elevation'] *= -1;
314 $from_array = $global_calcs->FromCourseTo($to_array, $course);
315 echo "FromCourseTo: Region Name = ".$from_array['region_name'].", local_x = ".$from_array['local_x'].", local_y = ".$from_array['local_y'].", local_z = ".$from_array['local_z']."\n\n";
316
317?>
318
A Class to do global to local conversion and some global calculations.
$Host
The Host running the Database.
GlobalToLocal($GlobalArray)
$Database
The name of the Database.
CourseFromTo($FromLocalArray, $ToLocalArray)
Find the course and distance from one point to another using local coordinates.
$Username
The Username to access the Database.
__construct($Database, $Host, $Username, $Password)
Open the Support Database for Use.
$Password
The password to access the Database.
LocalToGlobal($LocalArray)
Convert a local regional location into a global location.
FromCourseTo($FromLocalArray, $CourseArray)
Routine to Calculate a Destination from an origin and a Course.
$global_calcs