/** * Calculates the intersection point bnetween the current and two other planes * * @param Plane $plane1 * @param Plane $plane2 * @return bool|\Math_Vector3 * @throws InvalidArgumentException */ public function calculateIntersectionPointWithTwoPlanes(self $plane1, self $plane2) { $n1 = $this->normalVectorNormalized; $n2 = $plane1->normalVectorNormalized; $n3 = $plane2->normalVectorNormalized; $d1 = $this->distanceToOrigin; $d2 = $plane1->distanceToOrigin; $d3 = $plane2->distanceToOrigin; $n2_x_n3 = \Math_VectorOp::crossProduct($n2, $n3); $n3_x_n1 = \Math_VectorOp::crossProduct($n3, $n1); $n1_x_n2 = \Math_VectorOp::crossProduct($n1, $n2); $p = new \Math_Vector3(\Math_VectorOp::add(\Math_VectorOp::add(\Math_VectorOp::scale($d1, $n2_x_n3), \Math_VectorOp::scale($d2, $n3_x_n1)), \Math_VectorOp::scale($d3, $n1_x_n2))->getTuple()); $divisor = \Math_VectorOp::dotProduct($n1, $n2_x_n3); if ((double) 0 === $divisor) { throw new \InvalidArgumentException('no point-intersection'); } $p->scale(1 / $divisor); return $p; }
/** * Angle between vectors, using the equation: v . w = |v| |w| cos(theta) * * @access public * @param object Math_Vector2 or MathVector3 (or subclass) $v1 * @param object Math_Vector2 or MathVector3 (or subclass) $v2 * @return mixed the angle between vectors (float, in radians) on success, a PEAR_Error object otherwise * * @see isVector2() * @see isVector3() * @see dotProduct() */ function angleBetween($v1, $v2) { if (Math_VectorOp::isVector2($v1) && Math_VectorOp::isVector2($v2) || Math_VectorOp::isVector3($v1) && Math_VectorOp::isVector3($v2)) { $v1->normalize(); $v2->normalize(); return acos(Math_VectorOp::dotProduct($v1, $v2)); } else { return PEAR::raiseError("Vectors must be both of the same type"); } }
/** * Takes all point vectors ("vertexes") of the polygon describing the face and sorts them * in clockwise order. */ public function sortVerticesClockwise() { $center = $this->calculateCenter(); for ($n = 0; $n <= count($this->vertexes) - 3; $n++) { $a = new \Math_Vector3(\Math_VectorOp::substract($this->vertexes[$n], $center)->getTuple()); $a->normalize(); $p = Plane::getInstanceByThreePositionVectors($this->vertexes[$n], $center, new \Math_Vector3(\Math_VectorOp::add($center, $this->plane->getNormalVectorNormalized())->getTuple())); $smallestAngle = -1; $smallest = -1; for ($m = $n + 1; $m <= count($this->vertexes) - 1; $m++) { if ($p->calculateSideOfPointVector($this->vertexes[$m]) !== Plane::SIDE_BACK) { $b = new \Math_Vector3(\Math_VectorOp::substract($this->vertexes[$m], $center)->getTuple()); $b->normalize(); $angle = \Math_VectorOp::dotProduct($a, $b); if ($angle > $smallestAngle) { $smallestAngle = $angle; $smallest = $m; } } } if ($smallest == -1) { throw new \RuntimeException('Error: Degenerate polygon!'); } //swap vertices $temp = $this->vertexes[$n + 1]; $this->vertexes[$n + 1] = $this->vertexes[$smallest]; $this->vertexes[$smallest] = $temp; unset($temp); } // Check if vertex order needs to be reversed for back-facing polygon $newPlane = Plane::getInstanceByThreePositionVectors($this->vertexes[0], $this->vertexes[1], $this->vertexes[2]); if (\Math_VectorOp::dotProduct($newPlane->getNormalVectorNormalized(), $this->plane->getNormalVectorNormalized()) < 0) { array_reverse($this->vertexes); } }
echo date("Y-m-d H:i:s") . "\n"; echo "==\nVector v1: " . $v1->toString() . "\n"; echo "Vector v2: " . $v2->toString() . "\n"; $r = Math_VectorOp::add($v1, $v2); echo "v1 + v2: " . $r->toString() . "\n"; $r = Math_VectorOp::substract($v1, $v2); echo "v1 - v2: " . $r->toString() . "\n"; $r = Math_VectorOp::multiply($v1, $v2); echo "v1 * v2: " . $r->toString() . "\n"; $r = Math_VectorOp::divide($v1, $v2); echo "v1 / v2: " . $r->toString() . "\n"; echo "==\nVector w1: " . $w1->toString() . "\n"; echo "Vector w2: " . $w2->toString() . "\n"; echo "Vector w3: " . $w3->toString() . "\n"; $r = Math_VectorOp::scale(2.0, $w1); echo " 2.0 * w1 = " . $r->toString() . "\n"; $r = Math_VectorOp::dotProduct($w1, $w2); echo "w1 . w2 = {$r}\n"; $r = Math_VectorOp::crossProduct($w2, $w3); echo "w2 x w3 = " . $r->toString() . "\n"; echo "The triple scalar product of 3 vectors is the volume\r\nof the parallelepiped defined by the vectors. Three coplanar\r\nvectors must give a volume of zero, w1, w2 and w3 are coplanar\n"; $r = Math_VectorOp::tripleScalarProduct($w1, $w2, $w3); echo "w1 . (w2 x w3) = {$r}\n"; $z = Math_VectorOp::createOne(3); echo "Now we introduce z : " . $z->toString() . "\n"; echo "and z not being coplanar to w1, w2, or w3:\n"; $r = Math_VectorOp::tripleScalarProduct($z, $w2, $w3); echo "z * (w2 x w3) = {$r}\n"; $r = Math_VectorOp::angleBetween($z, $w1); echo "and the angle between z and w1 is {$r} radians\n"; echo "which is " . $r * 180.0 / M_PI . " degrees\n";
function testDotProduct() { $this->assertEquals(2, Math_VectorOp::dotProduct($this->x, $this->x)); }
/** * Angle between vectors, using the equation: v . w = |v| |w| cos(theta) * * @param object Math_Vector2 or MathVector3 (or subclass) $v1 * @param object Math_Vector2 or MathVector3 (or subclass) $v2 * @return mixed the angle between vectors (float, in radians) on success * @throws InvalidArgumentException * * @see isVector2() * @see isVector3() * @see dotProduct() */ public static function angleBetween($v1, $v2) { if (Math_VectorOp::isVector2($v1) && Math_VectorOp::isVector2($v2) || Math_VectorOp::isVector3($v1) && Math_VectorOp::isVector3($v2)) { $v1->normalize(); $v2->normalize(); return acos(Math_VectorOp::dotProduct($v1, $v2)); } throw new InvalidArgumentException("Vectors must be both of the same type"); }