131 lines
3.5 KiB
C++
131 lines
3.5 KiB
C++
/*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
* Copyright 2016-2018 Danny Robson <danny@nerdcruft.net>
|
|
*/
|
|
|
|
#ifndef CRUFT_GEOM_TRI_HPP
|
|
#define CRUFT_GEOM_TRI_HPP
|
|
|
|
#include "../point.hpp"
|
|
#include "sample.hpp"
|
|
|
|
#include <cstddef>
|
|
#include <random>
|
|
|
|
namespace util::geom {
|
|
///////////////////////////////////////////////////////////////////////////
|
|
template <size_t S, typename T>
|
|
struct tri {
|
|
tri () = default;
|
|
tri (point<S,T> _a, point<S,T> _b, point<S,T> _c):
|
|
a (_a), b (_b), c (_c)
|
|
{ ; }
|
|
|
|
util::point<S,T> a, b, c;
|
|
};
|
|
|
|
template <size_t S, typename T>
|
|
tri (point<S,T>, point<S,T>, point<S,T>) -> tri<S,T>;
|
|
|
|
using tri3f = tri<3,float>;
|
|
|
|
|
|
namespace surface {
|
|
template <size_t S, typename T>
|
|
class sampler<tri<S,T>> {
|
|
public:
|
|
sampler (tri<S,T> _target):
|
|
base (_target.a),
|
|
v0 (_target.b - _target.a),
|
|
v1 (_target.c - _target.a)
|
|
{ ; }
|
|
|
|
template <typename GeneratorT>
|
|
util::point<S,T>
|
|
operator() (GeneratorT &&gen) const noexcept
|
|
{
|
|
std::uniform_real_distribution<float> dist (0, 1);
|
|
return base + dist (gen) * v0 + dist (gen) * v1;
|
|
}
|
|
|
|
private:
|
|
util::point<S,T> base;
|
|
util::vector<S,T> v0, v1;
|
|
};
|
|
};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// n-dimensional triangle area
|
|
template <std::size_t DimensionV, typename ValueT>
|
|
ValueT
|
|
area (tri<DimensionV,ValueT> obj)
|
|
{
|
|
// heron's formula
|
|
const auto ab = util::distance (obj.a, obj.b);
|
|
const auto bc = util::distance (obj.b, obj.c);
|
|
const auto ca = util::distance (obj.c, obj.a);
|
|
|
|
const auto s = (ab + bc + ca) / 2;
|
|
|
|
return std::sqrt (s * (s - ab) * (s - bc) * (s - ca));
|
|
}
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
// 2-dimension triangle area
|
|
template <typename T>
|
|
T
|
|
area (tri<2,T> obj)
|
|
{
|
|
// | x1 y1 1 |
|
|
// area = 0.5 det | x2 y2 1 |
|
|
// | x3 y3 1 |
|
|
|
|
return std::abs (
|
|
-obj.b.x * obj.a.y
|
|
+obj.c.x * obj.a.y
|
|
+obj.a.x * obj.b.y
|
|
-obj.c.x * obj.b.y
|
|
-obj.a.x * obj.c.y
|
|
+obj.b.x * obj.c.y
|
|
) / 2;
|
|
}
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
// 3-dimension triangle area
|
|
template <typename T>
|
|
T
|
|
area (tri<3,T> obj)
|
|
{
|
|
const auto ab = obj.a - obj.b;
|
|
const auto ac = obj.a - obj.c;
|
|
|
|
return norm (cross (ab, ac)) / 2;
|
|
}
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
// convenience forwarder
|
|
template <size_t S, typename T>
|
|
T
|
|
area (point<S,T> a, point<S,T> b, point<S,T> c)
|
|
{
|
|
return area (tri (a, b, c));
|
|
}
|
|
};
|
|
|
|
#endif
|