geom/segment: add trivial segment representation
This commit is contained in:
parent
cce9333b65
commit
aab066593b
@ -244,6 +244,8 @@ list (
|
||||
geom/rect.hpp
|
||||
geom/sample.cpp
|
||||
geom/sample.hpp
|
||||
geom/segment.cpp
|
||||
geom/segment.hpp
|
||||
geom/sphere.cpp
|
||||
geom/sphere.hpp
|
||||
geom/tri.cpp
|
||||
@ -501,6 +503,7 @@ if (TESTS)
|
||||
geom/line
|
||||
geom/plane
|
||||
geom/ray
|
||||
geom/segment
|
||||
geom/sphere
|
||||
hash/checksum
|
||||
hash/crc
|
||||
|
20
geom/segment.cpp
Normal file
20
geom/segment.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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 2018 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "segment.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
58
geom/segment.hpp
Normal file
58
geom/segment.hpp
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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 2018 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../point.hpp"
|
||||
|
||||
|
||||
namespace util::geom {
|
||||
template <size_t S, typename T>
|
||||
struct segment {
|
||||
util::point<S,T> a;
|
||||
util::point<S,T> b;
|
||||
};
|
||||
|
||||
|
||||
using segment2f = segment<2,float>;
|
||||
using segment3f = segment<3,float>;
|
||||
|
||||
|
||||
template <size_t S, typename T>
|
||||
T
|
||||
distance2 (segment<S,T> s, point<S,T> p)
|
||||
{
|
||||
const auto dir = s.b - s.a;
|
||||
const auto t1 = dot (p - s.a, dir);
|
||||
if (t1 < 0)
|
||||
return distance2 (p, s.a);
|
||||
|
||||
const auto t2 = dot (dir, dir);
|
||||
if (t2 < t1)
|
||||
return distance2 (p, s.b);
|
||||
|
||||
auto t = t1 / t2;
|
||||
return distance2 (p, s.a + t * dir);
|
||||
}
|
||||
|
||||
|
||||
template <size_t S, typename T>
|
||||
T
|
||||
distance (segment<S,T> s, point<S,T> p)
|
||||
{
|
||||
return std::sqrt (distance2 (s, p));
|
||||
}
|
||||
};
|
28
test/geom/segment.cpp
Normal file
28
test/geom/segment.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include "tap.hpp"
|
||||
|
||||
#include "geom/segment.hpp"
|
||||
|
||||
int
|
||||
main (int, char**)
|
||||
{
|
||||
util::TAP::logger tap;
|
||||
|
||||
static struct {
|
||||
util::point3f a, b;
|
||||
util::point3f q;
|
||||
float distance;
|
||||
const char *message;
|
||||
} const TESTS[] = {
|
||||
{ { 0, 0, 0 }, { 1, 1, 1 }, { 0, 0, 0 }, 0, "origin line, origin point" },
|
||||
{ { 0, 0, 0 }, { 1, 1, 1 }, { 1, 1, 1 }, 0, "origin line, point on line" },
|
||||
{ { 0, 1, 0 }, { 1, 1, 0 }, { 0, 0, 0 }, 1, "+x line, unit distance" },
|
||||
};
|
||||
|
||||
for (auto const& t: TESTS) {
|
||||
util::geom::segment3f s {t.a, t.b};
|
||||
auto d = distance (s, t.q);
|
||||
tap.expect_eq (d, t.distance, "%!", t.message);
|
||||
}
|
||||
|
||||
return tap.status ();
|
||||
}
|
Loading…
Reference in New Issue
Block a user