#pragma region CPL License
/*
Nuclex Native Framework
Copyright (C) 2002-2015 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#pragma endregion // CPL License
#ifndef NUCLEX_GEOMETRY_LINES_GENERATORS_SEGMENT3GENERATOR_H
#define NUCLEX_GEOMETRY_LINES_GENERATORS_SEGMENT3GENERATOR_H
#include "Nuclex/Geometry/Config.h"
#include "Nuclex/Geometry/Lines/Segment3.h"
namespace Nuclex { namespace Geometry { namespace Lines { namespace Generators {
// ------------------------------------------------------------------------------------------- //
/// Point and area generation methods for 3D line segments
template
class Segment3Generator {
/// Determines the closest point to another point upon the line segment
/// Line segment on which the closest point will be found
/// Point to which the closest point will be determined
/// The closest point upon the line segment to the specified point
public: static Point3 GetClosestPointUpon(
const Segment3 &segment, const Point3 &point
) {
Vector3 direction = (segment.End - segment.Start);
TScalar t = Vector3::Dot(point - segment.Start, direction);
if(t < 0) {
return segment.Start;
} else {
TScalar squaredSegmentLength = direction.GetSquaredLength();
if(t > squaredSegmentLength) {
return segment.End;
} else {
return segment.Start + (direction * (t / squaredSegmentLength));
}
}
}
/// Returns a random point upon the line segment
/// Line segment upon which a random point will be generated
/// Random number generator that will be used
/// A random point upon the line segment
public: template
static Point3 GetRandomPointUpon(
const Segment3 &segment, TRandomNumberEngine &random
) {
TScalar t = Math::Random(random, 1);
return Point3::Lerp(segment.Start, segment.End, t);
}
};
// ------------------------------------------------------------------------------------------- //
}}}} // namespace Nuclex::Geometry::Lines::Generators
#endif // NUCLEX_GEOMETRY_LINES_GENERATORS_SEGMENT3GENERATOR_H