public function Render(ALiVE_Universe $world) { // do NOT overload me $black = new ALiVe_Colour(0, 0, 0); $this->Initialize(); // virtual $world->Transform(); $polygons = $world->GetPolygons(); $points = $world->GetTransformedPoints(); foreach ($polygons as $polygon) { w_assert(isset($polygon[0])); w_assert(isset($polygon[1])); w_assert(isset($polygon[2])); w_assert(isset($points[$polygon[0]])); w_assert(isset($points[$polygon[1]])); w_assert(isset($points[$polygon[2]])); $a = $points[$polygon[0]]; $b = $points[$polygon[1]]; $c = $points[$polygon[2]]; // TODO: Don't draw polygons to +oo / -oo // (Z-buffer it) $this->DrawPolygon($a, $b, $c, $black); } $this->Finalize(); // virtual }
public function Apply($target) { if ($target instanceof ALiVE_Matrix) { return ALiVE_Matrices_Multiply($target, $this->ToMatrix()); } else { if ($target instanceof ALiVE_Vector) { return new ALiVE_2D_Vector($this->Apply($target->ToMatrix())); } else { if (is_array($target)) { foreach ($target as $vector) { w_assert($vector instanceof ALiVE_Vector); } $result = $this->Apply(ALiVE_Vectors_ToMatrix($target)); $ret = array(); for ($i = 0; $i < $result->M(); ++$i) { $thismatrix = new ALiVE_Matrix(1, 4); $thismatrix->Set(0, 0, $result->Get($i, 0)); $thismatrix->Set(0, 1, $result->Get($i, 1)); $thismatrix->Set(0, 2, $result->Get($i, 2)); $thismatrix->Set(0, 3, $result->Get($i, 3)); $ret[] = new ALiVE_2D_Vector($thismatrix); } return $ret; } } } throw new Exception('Invalid projection application argument'); }
public function AddPolygon($i, $j, $k) { // keep the points in clockwise-order from viewpoint! w_assert(is_int($i) && is_int($j) && is_int($k)); w_assert(isset($this->mPoints[$i]) && isset($this->mPoints[$j]) && isset($this->mPoints[$k])); $this->mPolygons[] = array($i, $j, $k); }
protected function Build() { $this->mPoints = array(); $this->mPolygons = array(); foreach ($this->mObjects as $object) { // apply object transformations (object scaling, object rotation, object translation) $object->Transform(); $objectpoints = $object->GetTransformedPoints(); // offset of first point of this object in world points array $objectoffset = count($this->mPoints); foreach ($objectpoints as $vector) { $this->mPoints[] = $vector; } $objectpolygons = $object->GetPolygons(); foreach ($objectpolygons as $polygon) { // $polygon is a copy // offset should change from $object->GetTransformedPoints() offset // to $this->mPoints offset w_assert(isset($polygon[0])); w_assert(isset($polygon[1])); w_assert(isset($polygon[2])); w_assert(isset($objectpoints[$polygon[0]])); w_assert(isset($objectpoints[$polygon[1]])); w_assert(isset($objectpoints[$polygon[2]])); $polygon[0] += $objectoffset; $polygon[1] += $objectoffset; $polygon[2] += $objectoffset; $this->mPolygons[] = $polygon; } } }
public function SetSize($width, $height) { w_assert(is_int($width)); w_assert(is_int($height)); w_assert($width > 0); w_assert($height > 0); $this->mWidth = $width; $this->mHeight = $height; }
public function ALiVE_Rotation($pitch, $yaw, $roll) { w_assert(is_int($pitch) || is_float($pitch)); w_assert(is_int($yaw) || is_float($yaw)); w_assert(is_int($roll) || is_float($roll)); $this->mPitch = $pitch; $this->mYaw = $yaw; $this->mRoll = $roll; }
public function Finalize() { header('Content-type: ' . $this->mType); switch ($this->mType) { case 'image/png': imagepng($this->mImg); break; case 'image/jpeg': imagejpeg($this->mImg); break; case 'image/gif': imagegif($this->mImg); break; default: w_assert(false); } }
public function ALiVE_Plane($arg0, $arg1, $arg2 = null) { if ($arg2 === null) { // new ALiVE_Plane( $point, $normal ) w_assert($arg0 instanceof ALiVE_Vector); w_assert($arg1 instanceof ALiVE_Unit_Vector); $this->mPoint = $arg0; $this->mNormal = $arg1; } else { // new ALiVE_Plane( $point1, $point2, $point3 ); w_assert($arg0 instanceof ALiVE_Vector); w_assert($arg1 instanceof ALiVE_Vector); w_assert($arg2 instanceof ALiVE_Vector); // make sure we have three distinct points w_assert($arg0 != $arg1); w_assert($arg1 != $arg2); w_assert($arg0 != $arg2); // compute the normal vector $this->mNormal = ALiVE_Vectors_CrossProduct(ALiVE_Vectors_Subtract($arg1, $arg0), ALiVE_Vectors_Subtract($arg2, $arg0)); $this->mPoint = $arg0; } }
public function Power($pow) { w_assert(is_int($pow) && $pow >= 0); switch ($pow) { case 1: return clone $this; default: $result = ALiVE_Matrix_Create_Identity($this->Order()); for ($i = 0; $i < $pow; ++$i) { $result = ALiVE_Matrices_Multiply($result, $this); } return $result; } }
public function ALiVE_Unit_Vector($x, $y, $z) { $this->ALiVE_Vector($x, $y, $z); w_assert($this->Length() == 1); }
$myplane = new ALiVE_Plane(new ALiVE_Vector(0, 0, 0), new ALiVE_Vector(12, 3, 108), new ALiVE_Vector(5, -3, 12)); w_assert($myplane->Contains($invector)); w_assert(!$myplane->Contains($outvector)); $i = new ALiVE_Vector_I(); $j = new ALiVE_Vector_J(); $k = new ALiVE_Vector_K(); w_assert(ALiVE_Vectors_Equal(ALiVE_Vectors_CrossProduct($i, $j), $k)); w_assert(ALiVE_Vectors_Equal(ALiVE_Vectors_CrossProduct($j, $k), $i)); w_assert(ALiVE_Vectors_Equal(ALiVE_Vectors_CrossProduct($k, $i), $j)); $matrix = ALiVE_Matrix_Create_Identity(4); w_assert($matrix->M() == 4 && $matrix->N() == 4); w_assert($matrix->Get(0, 1) == 0); w_assert($matrix->Get(0, 0) == 1); w_assert($matrix->Get(1, 1) == 1); w_assert($matrix->Get(2, 2) == 1); w_assert($matrix->Get(3, 3) == 1); $neutral = new ALiVE_Transformation_Neutral(); $vector = new ALiVE_Vector(10.5, 7, -34.67); $result = $neutral->Apply($vector); w_assert($result instanceof ALiVE_Vector); w_assert($result->X() == 10.5); w_assert($result->Y() == 7); w_assert($result->Z() == -34.67); $p = new ALiVE_Vector(0.4, 0, 0); $neutral1 = new ALiVE_Translation(0, 0, 0); $neutral2 = new ALiVE_Rotation(0, 0, 0); $neutral3 = new ALiVE_Scaling(1, 1, 1); w_assert(ALiVE_Matrices_Equal($p->ToMatrix(), $neutral1->Apply($p->ToMatrix()))); w_assert(ALiVE_Matrices_Equal($p->ToMatrix(), $neutral2->Apply($p->ToMatrix()))); w_assert(ALiVE_Matrices_Equal($p->ToMatrix(), $neutral3->Apply($p->ToMatrix())));