bool toxi::geom::AABB::intersectsSphere( const Vec3D & c, const float & r ) { double s, d = 0; // find the square of the distance // from the sphere to the box if (c.getX() < min.getX()) { s = c.getX() - min.getX(); d = s * s; } else if (c.getX() > max.getX()) { s = c.getX() - max.getX(); d += s * s; } if (c.getY() < min.getY()) { s = c.getY() - min.getY(); d += s * s; } else if (c.getY() > max.getY()) { s = c.getY() - max.getY(); d += s * s; } if (c.getZ() < min.getZ()) { s = c.getZ() - min.getZ(); d += s * s; } else if (c.getZ() > max.getZ()) { s = c.getZ() - max.getZ(); d += s * s; } return d <= r * r; }
toxi::geom::Matrix4x4 toxi::geom::Matrix4x4::lookAt( Vec3D & eye, Vec3D & target, Vec3D & up ) { Vec3D f = eye.sub( target ).normalize(); Vec3D s = up.cross( f ).normalize(); Vec3D t = f.cross( s ).normalize(); return set( s.getX(), s.getY(), s.getZ(), -s.dot( eye ), t.getX(), t.getY(), t.getZ(), -t.dot( eye ), f.getX(), f.getY(), f.getZ(), -f.dot( eye ), 0, 0, 0, 1 ); }
inline bool isAlone(Vec3D<int> p, int xDim, int yDim, int zDim, int ***strArr) { // x axis -- check neighbors if(p.getX() + 1 < xDim){if(strArr[p.getX()+1][p.getY()][p.getZ()] == 1) return false;} if(p.getX() - 1 >= 0) {if(strArr[p.getX()-1][p.getY()][p.getZ()] == 1) return false;} // y axis -- check neighbors if(p.getY() + 1 < yDim){if(strArr[p.getX()][p.getY()+1][p.getZ()] == 1) return false;} if(p.getY() - 1 >= 0) {if(strArr[p.getX()][p.getY()-1][p.getZ()] == 1) return false;} // z axis -- check neighbors if(p.getZ() + 1 < zDim){if(strArr[p.getX()][p.getY()][p.getZ()+1] == 1) return false;} if(p.getZ() - 1 >= 0) {if(strArr[p.getX()][p.getY()][p.getZ()-1] == 1) return false;} return true; }
bool toxi::geom::AABB::intersectsBox( AABB & box ) { Vec3D t = box.sub(x, y, z); return toxi::math::MathUtils::abs( t.getX() ) <= ( extent.getX() + box.extent.getX() ) && toxi::math::MathUtils::abs( t.getY() ) <= ( extent.getZ() + box.extent.getY() ) && toxi::math::MathUtils::abs( t.getY() ) <= ( extent.getY() + box.extent.getZ() ); }
toxi::geom::Vec3D toxi::geom::AABB::getNormalForPoint( Vec3D & p ) { p = p.sub( Vec3D( x, y, z ) ); Vec3D pabs = extent.sub( p.getAbs() ); Vec3D psign = p.getSignum(); Vec3D normal = Vec3D::X_AXIS().scale( static_cast< float > ( psign.getX() ) ); double minDist = pabs.getX(); if (pabs.getY() < minDist) { minDist = pabs.getY(); normal = Vec3D::Y_AXIS().scale( static_cast< float > ( psign.getY() ) ); } if (pabs.getZ() < minDist) { normal = Vec3D::Z_AXIS().scale( static_cast< float > ( psign.getZ() ) ); } return normal; }
toxi::geom::AABB::AABB( const Vec3D & point, const Vec3D & extend ) { this->x = point.getX(); this->y = point.getY(); this->z = point.getZ(); setExtend( extend ); }
toxi::geom::Vec3D toxi::geom::Matrix4x4::applyToSelf( Vec3D & v ) { for ( int i = 0; i < 4; i++ ) { double* m = matrix[ i ]; temp[ i ] = v.getX() * m[ 0 ] + v.getY() * m[ 1 ] + v.getZ() * m[ 2 ] + m[ 3 ]; } v.set( ( float ) temp[ 0 ], ( float ) temp[ 1 ], ( float ) temp[ 2 ] ).scaleSelf( ( float ) ( 1.0 / temp[ 3 ] ) ); return v; }
toxi::geom::Vec3D toxi::geom::AABB::intersectsRay( toxi::geom::Ray3D & ray, const float & minDist, const float & maxDist ) { Vec3D invDir = ray.getDirection().getReciprocal(); bool signDirX = invDir.getX() < 0; bool signDirY = invDir.getY() < 0; bool signDirZ = invDir.getZ() < 0; Vec3D bbox = signDirX ? max : min; double tmin = (bbox.getX() - ray.getX()) * invDir.getX(); bbox = signDirX ? min : max; double tmax = (bbox.getX() - ray.getX()) * invDir.getX(); bbox = signDirY ? max : min; double tymin = (bbox.getY() - ray.getY()) * invDir.getY(); bbox = signDirY ? min : max; double tymax = (bbox.getY() - ray.getY()) * invDir.getY(); if ((tmin > tymax) || (tymin > tmax)) { return Vec3D(); } if (tymin > tmin) { tmin = tymin; } if (tymax < tmax) { tmax = tymax; } bbox = signDirZ ? max : min; double tzmin = (bbox.getZ() - ray.getZ()) * invDir.getZ(); bbox = signDirZ ? min : max; double tzmax = (bbox.getZ() - ray.getZ()) * invDir.getZ(); if ((tmin > tzmax) || (tzmin > tmax)) { return Vec3D(); } if (tzmin > tmin) { tmin = tzmin; } if (tzmax < tmax) { tmax = tzmax; } if ((tmin < maxDist) && (tmax > minDist)) { return ray.getPointAtDistance( static_cast < float > ( tmin ) ); } return Vec3D(); }
void Spline::displayline() { glColor3f(0.7, 0.7, 0.0); glPointSize(8.0); glBegin(GL_POINTS); int length = getNumKeyFrames() + 1; for (int i = -1; i < length; ++i) { Vec3D vec = getKeyPoint(i); glVertex3f(vec.getX(), vec.getY(), vec.getZ()); } glEnd(); glColor3f(1.0, 1.0, 1.0); glBegin(GL_LINE_STRIP); length = getNumKeyFrames() - 1; for (float u = 0; u < length; u += 0.02) { Vec3D v = getPoint(u); //glColor3f(0.5 + sin(u * 2 * M_PI) / 2.0, 0.5 + cos(u * 2 * M_PI) / 2.0, 0); glVertex3f(v.getX(), v.getY(), v.getZ()); } glEnd(); }
bool toxi::geom::AABB::planeBoxOverlap( Vec3D & normal, const float & d, const Vec3D & maxBox ) { Vec3D vmin = Vec3D(); Vec3D vmax = Vec3D(); if (normal.getX() > 0.0f) { vmin.setX( -maxBox.getX() ); vmax.setX( maxBox.getX() ); } else { vmin.setX( maxBox.getX() ); vmax.setX( -maxBox.getX() ); } if (normal.getY() > 0.0f) { vmin.setY( -maxBox.getY() ); vmax.setY( maxBox.getY() ); } else { vmin.setY( maxBox.getY() ); vmax.setY( -maxBox.getY() ); } if (normal.getZ() > 0.0f) { vmin.setZ( -maxBox.getZ() ); vmax.setZ( maxBox.getZ() ); } else { vmin.setZ( maxBox.getZ() ); vmax.setZ( -maxBox.getZ() ); } if (normal.dot(vmin) + d > 0.0f) { return false; } if (normal.dot(vmax) + d >= 0.0f) { return true; } return false; }
toxi::geom::Matrix4x4 toxi::geom::Matrix4x4::rotateAroundAxis( const Vec3D & axis, const double & theta ) { double x, y, z, s, c, t, tx, ty; x = axis.getX(); y = axis.getY(); z = axis.getZ(); s = toxi::math::MathUtils::sin( theta ); c = toxi::math::MathUtils::cos( theta ); t = 1 - c; tx = t * x; ty = t * y; TEMP->set( tx * x + c, tx * y + s * z, tx * z - s * y, 0, tx * y - s * z, ty * y + c, ty * z + s * x, 0, tx * z + s * y, ty * z - s * x, t * z * z + c, 0, 0, 0, 0, 1 ); return this->multiplySelf( *TEMP ); }
toxi::geom::Matrix4x4 toxi::geom::Matrix4x4::translate( const Vec3D & v ) { return Matrix4x4( *this ).translateSelf( v.getX(), v.getY(), v.getZ() ); }
toxi::geom::Matrix4x4 toxi::geom::Matrix4x4::scaleSelf( const Vec3D & scale ) { return scaleSelf( scale.getX(), scale.getY(), scale.getZ() ); }
toxi::geom::Vec3D toxi::geom::Triangle3D::fromVarycentric( Vec3D & v ) { return Vec3D( a.getX() * v.getX() + b.getX() * v.getY() + c.getX() * v.getZ(), a.getY() * v.getX() + b.getY() * v.getY() + c.getY() * v.getZ(), a.getZ() * v.getX() + b.getZ() * v.getY() + c.getZ() * v.getZ() ); }
toxi::geom::Vec3D toxi::geom::AABB::add( const Vec3D & v ) { return Vec3D( x + v.getX(), y + v.getY(), z + v.getZ() ); }