Loading...
Searching...
No Matches
segment.cuh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022-2023, NVIDIA CORPORATION.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cuspatial/cuda_utils.hpp>
21
22#include <thrust/device_reference.h>
23
24#include <iostream>
25namespace cuspatial {
26
39template <typename T, typename Vertex = cuspatial::vec_2d<T>>
40class alignas(sizeof(Vertex)) segment {
41 public:
42 using value_type = T;
43 Vertex v1;
44 Vertex v2;
45
47 segment<T> CUSPATIAL_HOST_DEVICE translate(Vertex const& v) const
48 {
49 return segment<T>{v1 + v, v2 + v};
50 }
51
53 Vertex CUSPATIAL_HOST_DEVICE center() const { return midpoint(v1, v2); }
54
56 T CUSPATIAL_HOST_DEVICE length2() const { return dot(v2 - v1, v2 - v1); }
57
59 T CUSPATIAL_HOST_DEVICE slope() { return (v2.y - v1.y) / (v2.x - v1.x); }
60
62 Vertex CUSPATIAL_HOST_DEVICE lower_left() { return v1 < v2 ? v1 : v2; }
63
67 bool CUSPATIAL_HOST_DEVICE collinear(segment<T> const& other)
68 {
69 return (v1.x - v2.x) * (other.v1.y - other.v2.y) == (v1.y - v2.y) * (other.v1.x - other.v2.x);
70 }
71
72 private:
73 friend std::ostream& operator<<(std::ostream& os, segment<T> const& seg)
74 {
75 return os << seg.v1 << " -> " << seg.v2;
76 }
77};
78
79// deduction guide, enables CTAD
80template <typename T>
81segment(vec_2d<T> a, vec_2d<T> b) -> segment<T, vec_2d<T>>;
82
83template <typename T>
84segment(thrust::device_reference<vec_2d<T>> a, thrust::device_reference<vec_2d<T>> b)
85 -> segment<T, vec_2d<T>>;
86} // namespace cuspatial
A generic segment type.
Definition segment.cuh:40
T CUSPATIAL_HOST_DEVICE length2() const
Return the length squared of segment.
Definition segment.cuh:56
bool CUSPATIAL_HOST_DEVICE collinear(segment< T > const &other)
Definition segment.cuh:67
vec_2d< T > CUSPATIAL_HOST_DEVICE midpoint(vec_2d< T > const &first, vec_2d< T > const &second)
Compute the midpoint of first and second.
Definition vec_2d.hpp:211
Vertex CUSPATIAL_HOST_DEVICE lower_left()
Return the lower left vertex of segment.
Definition segment.cuh:62
T CUSPATIAL_HOST_DEVICE dot(vec_2d< T > const &a, vec_2d< T > const &b)
Compute dot product of two 2D vectors.
Definition vec_2d.hpp:167
Vertex CUSPATIAL_HOST_DEVICE center() const
Return the geometric center of segment.
Definition segment.cuh:53
T CUSPATIAL_HOST_DEVICE slope()
Return slope of segment.
Definition segment.cuh:59
segment< T > CUSPATIAL_HOST_DEVICE translate(Vertex const &v) const
Return a copy of segment, translated by v.
Definition segment.cuh:47