Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.73 KB | None | 0 0
  1. ex02:
  2. class Vector
  3. {
  4. private $_x;
  5. private $_y;
  6. private $_z;
  7. private $_w = 0;
  8. static $verbose = false;
  9.  
  10. public function __construct( array $kwargs)
  11. {
  12. if (isset($kwargs['dest']) && is_a($kwargs['dest'], "Vertex"))
  13. {
  14. if (isset($kwargs['orig']) && is_a($kwargs['orig'], "Vertex"))
  15. $orig = new Vertex(array('x' => $kwargs['orig']->getX(), 'y' => $kwargs['orig']->getY(), 'z' => $kwargs['orig']->getZ()));
  16. else
  17. $orig = new Vertex(array('x' => 0, 'y' => 0, 'z' => 0));
  18. $this->_x = $kwargs['dest']->getX() - $orig->getX();
  19. $this->_y = $kwargs['dest']->getY() - $orig->getY();
  20. $this->_z = $kwargs['dest']->getZ() - $orig->getZ();
  21. $this->_w = 0;
  22. }
  23. if (self::$verbose)
  24. printf("Vector( x:%0.2f, y:%0.2f, z:%0.2f, w:%0.2f ) constructed\n", $this->_x, $this->_y, $this->_z, $this->_w);
  25. }
  26. public function magnitude()
  27. {
  28. return (float)sqrt(($this->_x * $this->_x) + ($this->_y * $this->_y) + ($this->_z * $this->_z));
  29. }
  30. public function normalize()
  31. {
  32. $long = $this->magnitude();
  33. if ($long == 1) {
  34. return clone $this;
  35. }
  36. return new Vector(array('dest' => new Vertex(array('x' => $this->_x / $long, 'y' => $this->_y / $long, 'z' => $this->_z / $long))));
  37. }
  38. public function add(Vector $point)
  39. {
  40. return new Vector(array('dest' => new Vertex(array('x' => $this->_x + $point->_x, 'y' => $this->_y + $point->_y, 'z' => $this->_z + $point->_z))));
  41. }
  42. public function sub(Vector $point)
  43. {
  44. return new Vector(array('dest' => new Vertex(array('x' => $this->_x - $point->_x, 'y' => $this->_y - $point->_y, 'z' => $this->_z - $point->_z))));
  45. }
  46. public function opposite()
  47. {
  48. return new Vector(array('dest' => new Vertex(array('x' => $this->_x * -1, 'y' => $this->_y * -1, 'z' => $this->_z * -1))));
  49. }
  50. public function scalarProduct($p)
  51. {
  52. return new Vector(array('dest' => new Vertex(array('x' => $this->_x * $p, 'y' => $this->_y * $p, 'z' => $this->_z * $p))));
  53. }
  54. public function dotProduct(Vector $point)
  55. {
  56. return (float)(($this->_x * $point->_x) + ($this->_y * $point->_y) + ($this->_z * $point->_z));
  57. }
  58. public function crossProduct(Vector $point)
  59. {
  60. return new Vector(array('dest' => new Vertex(array(
  61. 'x' => $this->_y * $point->getZ() - $this->_z * $point->getY(),
  62. 'y' => $this->_z * $point->getX() - $this->_x * $point->getZ(),
  63. 'z' => $this->_x * $point->getY() - $this->_y * $point->getX()
  64. ))));
  65. }
  66. public function cos(Vector $point)
  67. {
  68. return ((($this->_x * $point->_x) + ($this->_y * $point->_y) + ($this->_z * $point->_z)) / sqrt((($this->_x * $this->_x) + ($this->_y * $this->_y) + ($this->_z * $this->_z)) * (($point->_x * $point->_x) + ($point->_y * $point->_y) + ($point->_z * $point->_z))));
  69. }
  70. function __destruct()
  71. {
  72. if (Self::$verbose)
  73. printf("Vector( x:%0.2f, y:%0.2f, z:%0.2f, w:%0.2f ) destructed\n", $this->_x, $this->_y, $this->_z, $this->_w);
  74. }
  75. function __toString()
  76. {
  77. return (vsprintf("Vector( x:%0.2f, y:%0.2f, z:%0.2f, w:%0.2f )", array($this->_x, $this->_y, $this->_z, $this->_w)));
  78. }
  79. public static function doc()
  80. {
  81. print(file_get_contents("Vector.doc.txt"));
  82. }
  83. public function getX()
  84. {
  85. return $this->_x;
  86. }
  87. public function getY()
  88. {
  89. return $this->_y;
  90. }
  91. public function getZ()
  92. {
  93. return $this->_z;
  94. }
  95. public function getW()
  96. {
  97. return $this->_w;
  98. }
  99. }
  100.  
  101. <- Vector ----------------------------------------------------------------------
  102. The Vector class handles space 3D Vectors.
  103. More than three coordinates, it has got an homogenous parameter (always=0)
  104.  
  105. An instance is contructed from two Vertex orig and dest.
  106. It represents then the vector between thos two points.
  107. new Vector( array( 'dest' => #instanceOfVertex, 'orig' => #instanceOfVertex );
  108. Ff the vertex 'orig' is not precised, it will then be (0,0,0) as default.
  109.  
  110. All Attributes are private, you may want to use getters.
  111. When a Vector is created, you wont be able to change its values after.
  112. aka-> no setters;
  113.  
  114. The following method are providing for this Class:
  115.  
  116. - magnitude() : give the norme of the instanced Vector.
  117.  
  118. - normalize() : give the normalized Vector correspondig (a fresh copy
  119. if the instance was already normalized)
  120.  
  121. - add( Vector $v ) : give the result of the addition between
  122. the current instance and $.
  123.  
  124. - sub( Vector $v ) : give the result of the substraction between
  125. the current instance and $.
  126.  
  127. - opposite() : return the opposite Vector;
  128.  
  129. - scalarProduct( $k ) : return the product of the constant $k
  130. and the current instance;
  131.  
  132. - dotProduct( Vector $v ) : return the scalar product of the current
  133. instance and $v.
  134.  
  135. - cos( Vector $v ) : return the cosinus between the current instance and $v
  136.  
  137. Vector crossProduct( Vector $rhs ) : return the cross product
  138. (right handed set) of the current instance and $v
  139. ---------------------------------------------------------------------- Vector ->
  140.  
  141. ex03:
  142. class Matrix
  143. {
  144. const IDENTITY = "IDENTITY";
  145. const SCALE = "SCALE";
  146. const RX = "Ox ROTATION";
  147. const RY = "Oy ROTATION";
  148. const RZ = "Oz ROTATION";
  149. const TRANSLATION = "TRANSLATION";
  150. const PROJECTION = "PROJECTION";
  151. protected $matrix = array();
  152. private $_preset;
  153. private $_scale;
  154. private $_angle;
  155. private $_vtc;
  156. private $_fov;
  157. private $_ratio;
  158. private $_near;
  159. private $_far;
  160. static $verbose = false;
  161.  
  162. public function __construct( array $kwargs = null)
  163. {
  164. if (isset($kwargs))
  165. {
  166. if (isset($kwargs['preset']))
  167. $this->_preset = $kwargs['preset'];
  168. if (isset($kwargs['scale']))
  169. $this->_scale = $kwargs['scale'];
  170. if (isset($kwargs['angle']))
  171. $this->_angle = $kwargs['angle'];
  172. if (isset($kwargs['vtc']))
  173. $this->_vtc = $kwargs['vtc'];
  174. if (isset($kwargs['fov']))
  175. $this->_fov = $kwargs['fov'];
  176. if (isset($kwargs['ratio']))
  177. $this->_ratio = $kwargs['ratio'];
  178. if (isset($kwargs['near']))
  179. $this->_near = $kwargs['near'];
  180. if (isset($kwargs['far']))
  181. $this->_far = $kwargs['far'];
  182. $this->check();
  183. $this->createMatrix();
  184. if (self::$verbose) {
  185. if ($this->_preset == self::IDENTITY)
  186. echo "Matrix " . $this->_preset . " instance constructed\n";
  187. else
  188. echo "Matrix " . $this->_preset . " preset instance constructed\n";
  189. }
  190. $this->dispatch();
  191. }
  192. }
  193. private function dispatch()
  194. {
  195. switch ($this->_preset) {
  196. case (self::IDENTITY) :
  197. $this->identity(1);
  198. break;
  199. case (self::TRANSLATION) :
  200. $this->translation();
  201. break;
  202. case (self::SCALE) :
  203. $this->identity($this->_scale);
  204. break;
  205. case (self::RX) :
  206. $this->rotation_x();
  207. break;
  208. case (self::RY) :
  209. $this->rotation_y();
  210. break;
  211. case (self::RZ) :
  212. $this->rotation_z();
  213. break;
  214. case (self::PROJECTION) :
  215. $this->projection();
  216. break;
  217. }
  218. }
  219. private function createMatrix()
  220. {
  221. for ($i = 0; $i < 16; $i++) {
  222. $this->matrix[$i] = 0;
  223. }
  224. }
  225. private function identity($scale)
  226. {
  227. $this->matrix[0] = $scale;
  228. $this->matrix[5] = $scale;
  229. $this->matrix[10] = $scale;
  230. $this->matrix[15] = 1;
  231. }
  232. private function translation()
  233. {
  234. $this->identity(1);
  235. $this->matrix[3] = $this->_vtc->getX();
  236. $this->matrix[7] = $this->_vtc->getY();
  237. $this->matrix[11] = $this->_vtc->getZ();
  238. }
  239. private function rotation_x()
  240. {
  241. $this->identity(1);
  242. $this->matrix[0] = 1;
  243. $this->matrix[5] = cos($this->_angle);
  244. $this->matrix[6] = -sin($this->_angle);
  245. $this->matrix[9] = sin($this->_angle);
  246. $this->matrix[10] = cos($this->_angle);
  247. }
  248. private function rotation_y()
  249. {
  250. $this->identity(1);
  251. $this->matrix[0] = cos($this->_angle);
  252. $this->matrix[2] = sin($this->_angle);
  253. $this->matrix[5] = 1;
  254. $this->matrix[8] = -sin($this->_angle);
  255. $this->matrix[10] = cos($this->_angle);
  256. }
  257. private function rotation_z()
  258. {
  259. $this->identity(1);
  260. $this->matrix[0] = cos($this->_angle);
  261. $this->matrix[1] = -sin($this->_angle);
  262. $this->matrix[4] = sin($this->_angle);
  263. $this->matrix[5] = cos($this->_angle);
  264. $this->matrix[10] = 1;
  265. }
  266. private function projection()
  267. {
  268. $this->identity(1);
  269. $this->matrix[5] = 1 / tan(0.5 * deg2rad($this->_fov));
  270. $this->matrix[0] = $this->matrix[5] / $this->_ratio;
  271. $this->matrix[10] = -1 * (-$this->_near - $this->_far) / ($this->_near - $this->_far);
  272. $this->matrix[14] = -1;
  273. $this->matrix[11] = (2 * $this->_near * $this->_far) / ($this->_near - $this->_far);
  274. $this->matrix[15] = 0;
  275. }
  276. private function check()
  277. {
  278. if (empty($this->_preset))
  279. return "error";
  280. if ($this->_preset == self::SCALE && empty($this->_scale))
  281. return "error";
  282. if (($this->_preset == self::RX || $this->_preset == self::RY || $this->_preset == self::RZ) && empty($this->_angle))
  283. return "error";
  284. if ($this->_preset == self::TRANSLATION && empty($this->_vtc))
  285. return "error";
  286. if ($this->_preset == self::PROJECTION && (empty($this->_fov) || empty($this->_radio) || empty($this->_near) || empty($this->_far)))
  287. return "error";
  288. }
  289. public function mult(Matrix $mat)
  290. {
  291. $tmp = array();
  292. for ($i = 0; $i < 16; $i += 4) {
  293. for ($j = 0; $j < 4; $j++) {
  294. $tmp[$i + $j] = 0;
  295. $tmp[$i + $j] += $this->matrix[$i + 0] * $mat->matrix[$j + 0];
  296. $tmp[$i + $j] += $this->matrix[$i + 1] * $mat->matrix[$j + 4];
  297. $tmp[$i + $j] += $this->matrix[$i + 2] * $mat->matrix[$j + 8];
  298. $tmp[$i + $j] += $this->matrix[$i + 3] * $mat->matrix[$j + 12];
  299. }
  300. }
  301. $matrice = new Matrix();
  302. $matrice->matrix = $tmp;
  303. return $matrice;
  304. }
  305. public function transformVertex(Vertex $vec)
  306. {
  307. $tmp = array();
  308. $tmp['x'] = ($vec->getX() * $this->matrix[0]) + ($vec->getY() * $this->matrix[1]) + ($vec->getZ() * $this->matrix[2]) + ($vec->getW() * $this->matrix[3]);
  309. $tmp['y'] = ($vec->getX() * $this->matrix[4]) + ($vec->getY() * $this->matrix[5]) + ($vec->getZ() * $this->matrix[6]) + ($vec->getW() * $this->matrix[7]);
  310. $tmp['z'] = ($vec->getX() * $this->matrix[8]) + ($vec->getY() * $this->matrix[9]) + ($vec->getZ() * $this->matrix[10]) + ($vec->getW() * $this->matrix[11]);
  311. $tmp['w'] = ($vec->getX() * $this->matrix[11]) + ($vec->getY() * $this->matrix[13]) + ($vec->getZ() * $this->matrix[14]) + ($vec->getW() * $this->matrix[15]);
  312. $tmp['color'] = $vec->getColor();
  313. $vertex = new Vertex($tmp);
  314. return $vertex;
  315. }
  316. function __destruct()
  317. {
  318. if (self::$verbose)
  319. printf("Matrix instance destructed\n");
  320. }
  321. function __toString()
  322. {
  323. $tmp = "M | vtcX | vtcY | vtcZ | vtxO\n";
  324. $tmp .= "-----------------------------\n";
  325. $tmp .= "x | %0.2f | %0.2f | %0.2f | %0.2f\n";
  326. $tmp .= "y | %0.2f | %0.2f | %0.2f | %0.2f\n";
  327. $tmp .= "z | %0.2f | %0.2f | %0.2f | %0.2f\n";
  328. $tmp .= "w | %0.2f | %0.2f | %0.2f | %0.2f";
  329. return (vsprintf($tmp, array($this->matrix[0], $this->matrix[1], $this->matrix[2], $this->matrix[3], $this->matrix[4], $this->matrix[5], $this->matrix[6], $this->matrix[7], $this->matrix[8], $this->matrix[9], $this->matrix[10], $this->matrix[11], $this->matrix[12], $this->matrix[13], $this->matrix[14], $this->matrix[15])));
  330. }
  331. public static function doc()
  332. {
  333. print(file_get_contents("Matrix.doc.txt"));
  334. }
  335. }
  336.  
  337. <- Matrix ----------------------------------------------------------------------
  338. The Matrix class handles 4x4 Matrixs.
  339. it's got a finite constante values for the preset :
  340. IDENTITY = 'IDENTITY',
  341. SCALE = 'SCALE',
  342. RX = 'Ox ROTATION',
  343. RY = 'Oy ROTATION',
  344. RZ = 'Oz ROTATION',
  345. TRANSLATION = 'TRANSLATION',
  346. PROJECTION = 'PROJECTION';
  347.  
  348. On wich depends the following arguments respectivly :
  349. IDENTITY ->nothing
  350. SCALE -> the scale
  351. RX -> angle of rotation
  352. RY -> angle of rotation
  353. RZ -> angle of rotation
  354. TRANSLATION -> vector ( class Vector ) of the translation
  355. PROJECTION -> Field of view ( in degrees ), Ratio (width/height of screen), near and far z distances
  356.  
  357. When a Matrix is created, you wont be able to change its values after.
  358. aka-> no setters;
  359.  
  360. The following method are providing for this Class:
  361.  
  362. - mult() : return a new matrix corresponding to the multiplication of the instance by the argument
  363.  
  364. - transformVertex() : return a new Vertex corresponding to the matrix apply to the one sent in argument
  365. ---------------------------------------------------------------------- Matrix ->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement