paysages3d/src/basics/CappedCylinder.cpp

56 lines
1.6 KiB
C++
Raw Normal View History

2015-10-15 18:21:32 +00:00
#include "CappedCylinder.h"
#include "Vector3.h"
#include "PackStream.h"
CappedCylinder::CappedCylinder(const Vector3 &base, const Vector3 &direction, double radius, double length)
: InfiniteCylinder(InfiniteRay(base, direction), radius), length(length) {
2015-10-15 18:21:32 +00:00
}
int CappedCylinder::checkRayIntersection(const InfiniteRay &ray, Vector3 *first_intersection,
Vector3 *second_intersection) const {
2015-10-15 18:21:32 +00:00
// TODO Apply the caps
int count = InfiniteCylinder::checkRayIntersection(ray, first_intersection, second_intersection);
if (count == 0) {
2015-10-15 18:21:32 +00:00
return 0;
} else if (count == 2) {
if (checkPointProjection(first_intersection)) {
if (checkPointProjection(second_intersection)) {
2015-10-15 18:21:32 +00:00
return 2;
} else {
2015-10-15 18:21:32 +00:00
return 1;
}
} else {
if (checkPointProjection(second_intersection)) {
2015-10-15 18:21:32 +00:00
*first_intersection = *second_intersection;
return 1;
} else {
2015-10-15 18:21:32 +00:00
return 0;
}
}
} else // count == 1
2015-10-15 18:21:32 +00:00
{
if (checkPointProjection(first_intersection)) {
2015-10-15 18:21:32 +00:00
return 1;
} else {
2015-10-15 18:21:32 +00:00
return 0;
}
}
}
bool CappedCylinder::checkPointProjection(Vector3 *point) const {
2015-10-15 18:21:32 +00:00
double proj_length = axis.getDirection().dotProduct(point->sub(axis.getOrigin()));
return 0.0 <= proj_length && proj_length <= length;
}
void CappedCylinder::save(PackStream *stream) const {
2015-10-15 18:21:32 +00:00
InfiniteCylinder::save(stream);
stream->write(&length);
}
void CappedCylinder::load(PackStream *stream) {
2015-10-15 18:21:32 +00:00
InfiniteCylinder::load(stream);
stream->read(&length);
}