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_dev.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
309 public function RegionBoundsGlobal($RegionName)
310 {
311 global $pdo;
312
313 $sql_query = "select :region_name as region_name, ";
314 $sql_query .= "lor_edge_left as global_left, lor_edge_lower as global_bottom, 0 as global_low, ";
315 $sql_query .= "lor_edge_right as global_right, lor_edge_upper as global_top, lor_size_z as global_high ";
316 $sql_query .= "from ls_os_region where lor_name = :region_name";
317
318 try
319 {
320 $stmt = $pdo->prepare($sql_query);
321 $stmt->execute(['region_name' => $RegionName]);
322 $results = $stmt->fetchAll(PDO::FETCH_DEFAULT);
323
324 //#########################################################################################
325 //# Process Each Line #
326 //#########################################################################################
327
328 if ($results)
329 {
330 return $results[0];
331 }
332 else
333 {
334 $result['region_name'] = "";
335 $result['global_left'] = -1;
336 $result['global_bottom'] = -1;
337 $result['global_right'] = -1;
338 $result['global_top'] = -1;
339 $result['global_low'] = -1;
340 $result['global_high'] = -1;
341 return $result;
342 }
343 }
344 catch (PDOException $e)
345 {
346 // Catch the exception and display a user-friendly error message
347 // In a production environment, you might log the specific error
348
349 die("FAILURE=DATABASE:Connection failed " . $e->getMessage());
350 }
351 }
352
363 public function AddGlobal($FromGlobal, $DeltaGlobal)
364 {
365 $result['global_x'] = $ToGlobal['global_x'] + $DeltaGlobal['global_x'];
366 $result['global_y'] = $ToGlobal['global_y'] + $DeltaGlobal['global_y'];
367 $result['global_z'] = $ToGlobal['global_z'] + $DeltaGlobal['global_z'];
368 return $result;
369 }
370
371
380 public function DeltaGlobal($FromGlobal, $ToGlobal)
381 {
382 $result['global_x'] = $ToGlobal['global_x'] - $FromGlobal['global_x'];
383 $result['global_y'] = $ToGlobal['global_y'] - $FromGlobal['global_y'];
384 $result['global_z'] = $ToGlobal['global_z'] - $FromGlobal['global_z'];
385 return $result;
386 }
387
403 {
404 // Set the Local Position to the start of the Region
405 $FromLocal['local_x'] = 0; $FromLocal['local_y'] = 0; $FromLocal['local_z'] = 0;
406 $from_global = self::LocalToGlobal($FromLocal); $to_global = self::LocalToGlobal($ToLocal);
407 $delta_global = self::DeltaGlobal($from_global, $to_global);
408 $delta_global['region_name'] = $FromLocal['region_name'];
409 return $delta_global;
410 }
411
422 public function DeltaLocalToLocal($DeltaLocal)
423 {
424 $region_boundaries = self::RegionBoundsGlobal($DeltaLocal['region_name']);
425 $location_global['global_x'] = $DeltaLocal['global_x'] + $region_boundaries['global_left'];
426 $location_global['global_y'] = $DeltaLocal['global_y'] + $region_boundaries['global_bottom'];
427 $location_global['global_z'] = $DeltaLocal['global_z'] + $region_boundaries['global_low'];
428 $local_array = self::GlobalToLocal($location_global);
429 return $local_array;
430 }
431}
432
433/***************************************************************************************************
434 * TEST PROGRAM ENTRY
435 ***************************************************************************************************
436 * These routines are both an example of how to use the class and method of testing each of the routines
437 * in case a potential error is detected.
438 ***************************************************************************************************/
439
440 // Instantiate the Class with the database parameters. These parameters are provided by include file
441 // near the start of this file.
442 $global_calcs = new OS_GLOBAL_CALCS($db, $host, $db_username, $db_password);
443
444 // Examples of how data is formatted for inpuut to and output from the routines. This format makes the array
445 // directly compatible as the parameter field in the SQL queries.
446 $from_array = ['region_name'=>'BRIGADOON13', 'local_x' => 128, 'local_y' => 256, 'local_z' => 128];
447 $to_array = ['region_name'=>'BRIGADOON13', 'local_x' => 0, 'local_y' => 0, 'local_z' => 256];
450
451 // Convert the Local Coordinates into Global Coordinates
452 $global = $global_calcs->LocalToGlobal($from_array);
453 echo "LocalToGlobal: Global X = ".$global['global_x'].", Global Y = ".$global['global_y'].", Global Z = ".$global['global_x']."\n\n";
454
455 // Convert the Global Coordinates back to the Local Coordinates. They should be unchanged after the two conversion.
456 $from_array = $global_calcs->GlobalToLocal($global);
457 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";
458
459 // Take an Origin Local Location, convert to global, move the local and convert back into Local Coordinates
460 $course = $global_calcs->CourseFromTo($from_array, $to_array);
461 echo "CourseFromTo: Distance = ".$course['distance'].", bearing = ".rad2deg($course['bearing']). ", elevation = ".rad2deg($course['elevation'])."\n\n";
462
463 // 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
464 $course['bearing'] += M_PI;
465 $course['elevation'] *= -1;
466 $from_array = $global_calcs->FromCourseTo($to_array, $course);
467 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";
468
469 // Get the Region Information
470 $result = $global_calcs->RegionBoundsGlobal("Brigadoon13");
471 echo $result['region_name']." ".$result['global_left']." ".$result['global_bottom']." ".$result['global_right']." ".$result['global_top']." ".$result['global_low']." ".$result['global_high']."\n";
472
473 // Find the Distance for Teleporter Use
474 $FromLocal = ['region_name'=>'BRIGADOON01', 'local_x' => 128, 'local_y' => 256, 'local_z' => 128];
475 $ToLocal = ['region_name'=>'BRIGADOON05', 'local_x' => 0, 'local_y' => 0, 'local_z' => 128];
476 $result = $global_calcs->RegionToDeltaLocal($FromLocal, $ToLocal);
477 echo "RegionToDeltaLocal: Local X = ".$result['global_x'].", Local Y = ".$result['global_y'].", Global Z = ".$result['global_z']."\n\n";
478
479 // Convert an extended local position into a normal global position
480
481?>
482
A Class to do global to local conversion and some global calculations.
$Host
The Host running the Database.
AddGlobal($FromGlobal, $DeltaGlobal)
GlobalToLocal($GlobalArray)
RegionBoundsGlobal($RegionName)
Find the Bottom Left Corner and Top Right Corners in Global Coordinates.
$Database
The name of the Database.
CourseFromTo($FromLocalArray, $ToLocalArray)
RegionToDeltaLocal($FromLocal, $ToLocal)
DeltaLocalToLocal($DeltaLocal)
Convert a region based Location using global delta off to local coordinates.
DeltaGlobal($FromGlobal, $ToGlobal)
$Username
The Username to access the Database.
__construct($Database, $Host, $Username, $Password)
$Password
The password to access the Database.
FromCourseTo($FromLocalArray, $CourseArray)
$global_calcs